From 922e27de4c7a9bd396d90944553433661c40a2fd Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Fri, 31 Dec 2021 08:24:05 +0100 Subject: [PATCH] Fix #7754 FP Same expression on both sides of '||' (#3635) --- lib/astutils.cpp | 28 ++++++++++++++++------------ test/testother.cpp | 24 ++++++++++++++++++++++-- 2 files changed, 38 insertions(+), 14 deletions(-) diff --git a/lib/astutils.cpp b/lib/astutils.cpp index eb11d28a1..151ebed3b 100644 --- a/lib/astutils.cpp +++ b/lib/astutils.cpp @@ -1274,16 +1274,22 @@ bool isSameExpression(bool cpp, bool macro, const Token *tok1, const Token *tok2 } return false; } - if (macro && (tok1->isExpandedMacro() || tok2->isExpandedMacro() || tok1->isTemplateArg() || tok2->isTemplateArg())) + auto flagsDiffer = [](const Token* tok1, const Token* tok2, bool macro) { + if (macro && (tok1->isExpandedMacro() || tok2->isExpandedMacro() || tok1->isTemplateArg() || tok2->isTemplateArg())) + return true; + if (tok1->isComplex() != tok2->isComplex()) + return true; + if (tok1->isLong() != tok2->isLong()) + return true; + if (tok1->isUnsigned() != tok2->isUnsigned()) + return true; + if (tok1->isSigned() != tok2->isSigned()) + return true; return false; - if (tok1->isComplex() != tok2->isComplex()) - return false; - if (tok1->isLong() != tok2->isLong()) - return false; - if (tok1->isUnsigned() != tok2->isUnsigned()) - return false; - if (tok1->isSigned() != tok2->isSigned()) + }; + if (flagsDiffer(tok1, tok2, macro)) return false; + if (pure && tok1->isName() && tok1->next()->str() == "(" && tok1->str() != "sizeof" && !(tok1->variable() && tok1 == tok1->variable()->nameToken())) { if (!tok1->function()) { 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 *end2 = t2->link(); while (t1 && t2 && t1 != end1 && t2 != end2) { - if (t1->str() != t2->str()) + if (t1->str() != t2->str() || flagsDiffer(t1, t2, macro)) return false; t1 = t1->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(); while (t1 && t2 && t1->str() == t2->str() && - t1->isLong() == t2->isLong() && - t1->isUnsigned() == t2->isUnsigned() && - t1->isSigned() == t2->isSigned() && + !flagsDiffer(t1, t2, macro) && (t1->isName() || t1->str() == "*")) { t1 = t1->next(); t2 = t2->next(); diff --git a/test/testother.cpp b/test/testother.cpp index 2b2380ff2..0b4f13fdc 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -5603,13 +5603,33 @@ private: ASSERT_EQUALS("", errout.str()); } - void duplicateExpressionTemplate() { // #6930 - check("template void f() {\n" + void duplicateExpressionTemplate() { + check("template void f() {\n" // #6930 " if (I >= 0 && I < 3) {}\n" "}\n" "\n" "static auto a = f<0>();"); ASSERT_EQUALS("", errout.str()); + + check("template\n" // #7754 + "void f() {\n" + " if (std::is_same_v || std::is_same_v) {}\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + + check("typedef long long int64_t;" + "template\n" + "void f() {\n" + " if (std::is_same_v || std::is_same_v) {}\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + + checkP("#define int32_t int" + "template\n" + "void f() {\n" + " if (std::is_same_v || std::is_same_v) {}\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); } void duplicateExpressionCompareWithZero() {