From 13cd0f41f678dd427ae507177e36748a3f7d6a3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 24 Nov 2013 15:17:08 +0100 Subject: [PATCH] AST: Fixed FP in isSameExpression when there are different casts --- lib/checkother.cpp | 10 ++++++++++ test/testother.cpp | 11 +++++++++++ 2 files changed, 21 insertions(+) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 2b4a4828a..bfcb5c3b5 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -52,6 +52,16 @@ static bool isSameExpression(const Token *tok1, const Token *tok2, const std::se } if (Token::Match(tok1, "++|--")) return false; + if (tok1->str() == "(" && tok1->previous() && !tok1->previous()->isName()) { // cast => assert that the casts are equal + const Token *t1 = tok1->next(); + const Token *t2 = tok2->next(); + while (t1 && t2 && t1->str() == t2->str() && (t1->isName() || t1->str() == "*")) { + t1 = t1->next(); + t2 = t2->next(); + } + if (!t1 || !t2 || t1->str() != ")" || t2->str() != ")") + return false; + } if (!isSameExpression(tok1->astOperand1(), tok2->astOperand1(), constFunctions)) return false; if (!isSameExpression(tok1->astOperand2(), tok2->astOperand2(), constFunctions)) diff --git a/test/testother.cpp b/test/testother.cpp index 03a13b542..74eb73377 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -4820,6 +4820,17 @@ private: check("int f() { return !!y; }"); // No FP ASSERT_EQUALS("", errout.str()); + + // make sure there are not "same expression" fp when there are different casts + check("void f(long x) { if ((int32_t)x == (int64_t)x) {} }", + NULL, // filename + false, // experimental + false, // inconclusive + false, // posix + false, // runSimpleChecks + NULL // settings + ); + ASSERT_EQUALS("", errout.str()); } void duplicateIf1() { // ticket 3689 ( avoid false positive )