From 31fe97bd5190a58a6211d2651add3f6431e072ee Mon Sep 17 00:00:00 2001 From: chrchr Date: Wed, 25 Oct 2023 17:00:26 +0200 Subject: [PATCH 1/2] Fix #12122 FP knownConditionTrueFalse with type traits --- lib/astutils.cpp | 14 ++++++++++---- test/testother.cpp | 6 ++++++ 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/lib/astutils.cpp b/lib/astutils.cpp index a66bcf1bc73..57f13274211 100644 --- a/lib/astutils.cpp +++ b/lib/astutils.cpp @@ -1506,8 +1506,6 @@ bool isSameExpression(bool cpp, bool macro, const Token *tok1, const Token *tok2 if (cpp) { if (tok1->str() == "." && tok1->astOperand1() && tok1->astOperand1()->str() == "this") tok1 = tok1->astOperand2(); - while (Token::simpleMatch(tok1, "::") && tok1->astOperand2()) - tok1 = tok1->astOperand2(); if (tok2->str() == "." && tok2->astOperand1() && tok2->astOperand1()->str() == "this") tok2 = tok2->astOperand2(); while (Token::simpleMatch(tok2, "::") && tok2->astOperand2()) @@ -1523,8 +1521,16 @@ bool isSameExpression(bool cpp, bool macro, const Token *tok1, const Token *tok2 const bool tok_str_eq = tok1->str() == tok2->str(); if (!tok_str_eq && isDifferentKnownValues(tok1, tok2)) return false; - if (isSameConstantValue(macro, tok1, tok2)) - return true; + + { + const Token *constTok1 = tok1, *constTok2 = tok2; + while (Token::simpleMatch(constTok1, "::") && constTok1->astOperand2()) + constTok1 = constTok1->astOperand2(); + while (Token::simpleMatch(constTok2, "::") && constTok2->astOperand2()) + constTok2 = constTok2->astOperand2(); + if (isSameConstantValue(macro, constTok1, constTok2)) + return true; + } // Follow variable if (followVar && !tok_str_eq && (tok1->varId() || tok2->varId() || tok1->enumerator() || tok2->enumerator())) { diff --git a/test/testother.cpp b/test/testother.cpp index 6715ce90b78..a77bd38a45b 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -6900,6 +6900,12 @@ class TestOther : public TestFixture { "[test.cpp:14]: (style) The comparison '0 > S::E0' is always false.\n" "[test.cpp:15]: (style) The comparison '0 > S::F::F0' is always false.\n", errout.str()); + + check("template\n" // #12122 + "void f() {\n" + " static_assert(std::is_same::value || std::is_integral::value);\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); } void duplicateExpressionLoop() { From 9fb0806c7338214c13ff4a5936f97cc4269af448 Mon Sep 17 00:00:00 2001 From: chrchr Date: Wed, 25 Oct 2023 17:28:02 +0200 Subject: [PATCH 2/2] Fix --- lib/astutils.cpp | 38 +++++++++++++++++--------------------- 1 file changed, 17 insertions(+), 21 deletions(-) diff --git a/lib/astutils.cpp b/lib/astutils.cpp index 57f13274211..34ec31e6c97 100644 --- a/lib/astutils.cpp +++ b/lib/astutils.cpp @@ -1508,8 +1508,6 @@ bool isSameExpression(bool cpp, bool macro, const Token *tok1, const Token *tok2 tok1 = tok1->astOperand2(); if (tok2->str() == "." && tok2->astOperand1() && tok2->astOperand1()->str() == "this") tok2 = tok2->astOperand2(); - while (Token::simpleMatch(tok2, "::") && tok2->astOperand2()) - tok2 = tok2->astOperand2(); } // Skip double not if (Token::simpleMatch(tok1, "!") && Token::simpleMatch(tok1->astOperand1(), "!") && !Token::simpleMatch(tok1->astParent(), "=") && astIsBoolLike(tok2)) { @@ -1522,27 +1520,25 @@ bool isSameExpression(bool cpp, bool macro, const Token *tok1, const Token *tok2 if (!tok_str_eq && isDifferentKnownValues(tok1, tok2)) return false; - { - const Token *constTok1 = tok1, *constTok2 = tok2; - while (Token::simpleMatch(constTok1, "::") && constTok1->astOperand2()) - constTok1 = constTok1->astOperand2(); - while (Token::simpleMatch(constTok2, "::") && constTok2->astOperand2()) - constTok2 = constTok2->astOperand2(); - if (isSameConstantValue(macro, constTok1, constTok2)) - return true; - } + const Token *followTok1 = tok1, *followTok2 = tok2; + while (Token::simpleMatch(followTok1, "::") && followTok1->astOperand2()) + followTok1 = followTok1->astOperand2(); + while (Token::simpleMatch(followTok2, "::") && followTok2->astOperand2()) + followTok2 = followTok2->astOperand2(); + if (isSameConstantValue(macro, followTok1, followTok2)) + return true; // Follow variable - if (followVar && !tok_str_eq && (tok1->varId() || tok2->varId() || tok1->enumerator() || tok2->enumerator())) { - const Token * varTok1 = followVariableExpression(tok1, cpp, tok2); - if ((varTok1->str() == tok2->str()) || isSameConstantValue(macro, varTok1, tok2)) { - followVariableExpressionError(tok1, varTok1, errors); - return isSameExpression(cpp, macro, varTok1, tok2, library, true, followVar, errors); - } - const Token * varTok2 = followVariableExpression(tok2, cpp, tok1); - if ((tok1->str() == varTok2->str()) || isSameConstantValue(macro, tok1, varTok2)) { - followVariableExpressionError(tok2, varTok2, errors); - return isSameExpression(cpp, macro, tok1, varTok2, library, true, followVar, errors); + if (followVar && !tok_str_eq && (followTok1->varId() || followTok2->varId() || followTok1->enumerator() || followTok2->enumerator())) { + const Token * varTok1 = followVariableExpression(followTok1, cpp, followTok2); + if ((varTok1->str() == followTok2->str()) || isSameConstantValue(macro, varTok1, followTok2)) { + followVariableExpressionError(followTok1, varTok1, errors); + return isSameExpression(cpp, macro, varTok1, followTok2, library, true, followVar, errors); + } + const Token * varTok2 = followVariableExpression(followTok2, cpp, followTok1); + if ((followTok1->str() == varTok2->str()) || isSameConstantValue(macro, followTok1, varTok2)) { + followVariableExpressionError(followTok2, varTok2, errors); + return isSameExpression(cpp, macro, followTok1, varTok2, library, true, followVar, errors); } if ((varTok1->str() == varTok2->str()) || isSameConstantValue(macro, varTok1, varTok2)) { followVariableExpressionError(tok1, varTok1, errors);