Fix #7754 FP Same expression on both sides of '||' (#3635)

This commit is contained in:
chrchr-github 2021-12-31 08:24:05 +01:00 committed by GitHub
parent d8c8487a98
commit 922e27de4c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 14 deletions

View File

@ -1274,16 +1274,22 @@ bool isSameExpression(bool cpp, bool macro, const Token *tok1, const Token *tok2
} }
return false; return false;
} }
auto flagsDiffer = [](const Token* tok1, const Token* tok2, bool macro) {
if (macro && (tok1->isExpandedMacro() || tok2->isExpandedMacro() || tok1->isTemplateArg() || tok2->isTemplateArg())) if (macro && (tok1->isExpandedMacro() || tok2->isExpandedMacro() || tok1->isTemplateArg() || tok2->isTemplateArg()))
return false; return true;
if (tok1->isComplex() != tok2->isComplex()) if (tok1->isComplex() != tok2->isComplex())
return false; return true;
if (tok1->isLong() != tok2->isLong()) if (tok1->isLong() != tok2->isLong())
return false; return true;
if (tok1->isUnsigned() != tok2->isUnsigned()) if (tok1->isUnsigned() != tok2->isUnsigned())
return false; return true;
if (tok1->isSigned() != tok2->isSigned()) if (tok1->isSigned() != tok2->isSigned())
return true;
return false; return false;
};
if (flagsDiffer(tok1, tok2, macro))
return false;
if (pure && tok1->isName() && tok1->next()->str() == "(" && tok1->str() != "sizeof" && !(tok1->variable() && tok1 == tok1->variable()->nameToken())) { if (pure && tok1->isName() && tok1->next()->str() == "(" && tok1->str() != "sizeof" && !(tok1->variable() && tok1 == tok1->variable()->nameToken())) {
if (!tok1->function()) { if (!tok1->function()) {
if (Token::simpleMatch(tok1->previous(), ".")) { if (Token::simpleMatch(tok1->previous(), ".")) {
@ -1325,7 +1331,7 @@ bool isSameExpression(bool cpp, bool macro, const Token *tok1, const Token *tok2
const Token *end1 = t1->link(); const Token *end1 = t1->link();
const Token *end2 = t2->link(); const Token *end2 = t2->link();
while (t1 && t2 && t1 != end1 && t2 != end2) { while (t1 && t2 && t1 != end1 && t2 != end2) {
if (t1->str() != t2->str()) if (t1->str() != t2->str() || flagsDiffer(t1, t2, macro))
return false; return false;
t1 = t1->next(); t1 = t1->next();
t2 = t2->next(); t2 = t2->next();
@ -1346,9 +1352,7 @@ bool isSameExpression(bool cpp, bool macro, const Token *tok1, const Token *tok2
const Token *t2 = tok2->next(); const Token *t2 = tok2->next();
while (t1 && t2 && while (t1 && t2 &&
t1->str() == t2->str() && t1->str() == t2->str() &&
t1->isLong() == t2->isLong() && !flagsDiffer(t1, t2, macro) &&
t1->isUnsigned() == t2->isUnsigned() &&
t1->isSigned() == t2->isSigned() &&
(t1->isName() || t1->str() == "*")) { (t1->isName() || t1->str() == "*")) {
t1 = t1->next(); t1 = t1->next();
t2 = t2->next(); t2 = t2->next();

View File

@ -5603,13 +5603,33 @@ private:
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
void duplicateExpressionTemplate() { // #6930 void duplicateExpressionTemplate() {
check("template <int I> void f() {\n" check("template <int I> void f() {\n" // #6930
" if (I >= 0 && I < 3) {}\n" " if (I >= 0 && I < 3) {}\n"
"}\n" "}\n"
"\n" "\n"
"static auto a = f<0>();"); "static auto a = f<0>();");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
check("template<typename T>\n" // #7754
"void f() {\n"
" if (std::is_same_v<T, char> || std::is_same_v<T, unsigned char>) {}\n"
"}\n");
ASSERT_EQUALS("", errout.str());
check("typedef long long int64_t;"
"template<typename T>\n"
"void f() {\n"
" if (std::is_same_v<T, long> || std::is_same_v<T, int64_t>) {}\n"
"}\n");
ASSERT_EQUALS("", errout.str());
checkP("#define int32_t int"
"template<typename T>\n"
"void f() {\n"
" if (std::is_same_v<T, int> || std::is_same_v<T, int32_t>) {}\n"
"}\n");
ASSERT_EQUALS("", errout.str());
} }
void duplicateExpressionCompareWithZero() { void duplicateExpressionCompareWithZero() {