Skip to content

Commit 488bc99

Browse files
Improve isSameExpression for same valued literals with followvar (cppcheck-opensource#2519)
It allows (for example) cppcheck to detect that the lhs and the rhs are the same in the following example: double g() { double a = 1e1 return a & 10.0; }
1 parent 0d361f8 commit 488bc99

2 files changed

Lines changed: 8 additions & 4 deletions

File tree

lib/astutils.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -593,16 +593,16 @@ bool isSameExpression(bool cpp, bool macro, const Token *tok1, const Token *tok2
593593
// Follow variable
594594
if (followVar && tok1->str() != tok2->str() && (Token::Match(tok1, "%var%") || Token::Match(tok2, "%var%"))) {
595595
const Token * varTok1 = followVariableExpression(tok1, cpp, tok2);
596-
if (varTok1->str() == tok2->str()) {
596+
if ((varTok1->str() == tok2->str()) || isSameConstantValue(macro, varTok1, tok2)) {
597597
followVariableExpressionError(tok1, varTok1, errors);
598598
return isSameExpression(cpp, macro, varTok1, tok2, library, true, followVar, errors);
599599
}
600600
const Token * varTok2 = followVariableExpression(tok2, cpp, tok1);
601-
if (tok1->str() == varTok2->str()) {
601+
if ((tok1->str() == varTok2->str()) || isSameConstantValue(macro, tok1, varTok2)) {
602602
followVariableExpressionError(tok2, varTok2, errors);
603603
return isSameExpression(cpp, macro, tok1, varTok2, library, true, followVar, errors);
604604
}
605-
if (varTok1->str() == varTok2->str()) {
605+
if ((varTok1->str() == varTok2->str()) || isSameConstantValue(macro, varTok1, varTok2)) {
606606
followVariableExpressionError(tok1, varTok1, errors);
607607
followVariableExpressionError(tok2, varTok2, errors);
608608
return isSameExpression(cpp, macro, varTok1, varTok2, library, true, followVar, errors);

test/testastutils.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,10 @@ class TestAstUtils : public TestFixture {
142142
Tokenizer tokenizer(&settings, this);
143143
std::istringstream istr(code);
144144
tokenizer.tokenize(istr, "test.cpp");
145+
tokenizer.simplifyTokens1("");
145146
const Token * const tok1 = Token::findsimplematch(tokenizer.tokens(), tokStr1);
146147
const Token * const tok2 = Token::findsimplematch(tok1->next(), tokStr2);
147-
return ::isSameExpression(false, false, tok1, tok2, library, false, false, nullptr);
148+
return ::isSameExpression(false, false, tok1, tok2, library, false, true, nullptr);
148149
}
149150

150151
void isSameExpression() {
@@ -161,6 +162,9 @@ class TestAstUtils : public TestFixture {
161162
ASSERT_EQUALS(true, isSameExpression("(1 + x) < (x + 1);", "+", "+"));
162163
ASSERT_EQUALS(false, isSameExpression("(1.0l + x) < (1.0 + x);", "+", "+"));
163164
ASSERT_EQUALS(true, isSameExpression("(0.0 + x) < (x + 0x0p+0);", "+", "+"));
165+
ASSERT_EQUALS(true, isSameExpression("void f() {double y = 1e1; (x + y) < (x + 10.0); } ", "+", "+"));
166+
ASSERT_EQUALS(true, isSameExpression("void f() {double y = 1e1; (x + 10.0) < (y + x); } ", "+", "+"));
167+
ASSERT_EQUALS(true, isSameExpression("void f() {double y = 1e1; double z = 10.0; (x + y) < (x + z); } ", "+", "+"));
164168
}
165169

166170
bool isVariableChanged(const char code[], const char startPattern[], const char endPattern[]) {

0 commit comments

Comments
 (0)