Skip to content

Commit 67e9b55

Browse files
Refs #10958: add missing operators in valueFlowSameExpressions() (#8350)
Co-authored-by: chrchr-github <noreply@github.com>
1 parent f0b0d84 commit 67e9b55

4 files changed

Lines changed: 45 additions & 3 deletions

File tree

lib/token.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,7 @@ int multiCompareImpl(const Token *tok, const char *haystack, nonneg int varid)
554554
const char *needle = tok->str().c_str();
555555
const char *needlePointer = needle;
556556
for (;;) {
557-
if (needlePointer == needle && haystack[0] == '%' && haystack[1] != '|' && haystack[1] != '\0' && haystack[1] != ' ') {
557+
if (needlePointer == needle && haystack[0] == '%' && haystack[1] != '|' && haystack[1] != '\0' && haystack[1] != ' ' && haystack[1] != '=') {
558558
const int ret = multiComparePercent(tok, haystack, varid);
559559
if (ret < 2)
560560
return ret;

lib/valueflow.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -903,10 +903,10 @@ static void valueFlowSameExpressions(TokenList& tokenlist, const Settings& setti
903903

904904
long long val;
905905

906-
if (Token::Match(tok, "==|>=|<=|/")) {
906+
if (Token::Match(tok, "==|>=|<=|/|/=")) {
907907
val = 1;
908908
}
909-
else if (Token::Match(tok, "!=|>|<|%|-")) {
909+
else if (Token::Match(tok, "!=|>|<|%|%=|-|-=|^|^=")) {
910910
val = 0;
911911
}
912912
else

test/testtoken.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ class TestToken : public TestFixture {
6464
TEST_CASE(multiCompare3); // false positive for %or% on code using "|="
6565
TEST_CASE(multiCompare4);
6666
TEST_CASE(multiCompare5);
67+
TEST_CASE(multiCompare6);
6768
TEST_CASE(charTypes);
6869
TEST_CASE(stringTypes);
6970
TEST_CASE(getStrLength);
@@ -322,6 +323,17 @@ class TestToken : public TestFixture {
322323
ASSERT_EQUALS(true, TokenTest::multiCompare(&tok, "+|%or%|%oror%", 0) >= 0);
323324
}
324325

326+
void multiCompare6() const {
327+
{
328+
const SimpleTokenList stl("x %= y;");
329+
ASSERT_EQUALS(true, Token::Match(stl.front(), "%name% %= %name%"));
330+
}
331+
{
332+
const SimpleTokenList stl("x += y;");
333+
ASSERT_EQUALS(false, Token::Match(stl.front(), "%name% %= %name%"));
334+
}
335+
}
336+
325337
void charTypes() const {
326338
auto tokensFrontBack = std::make_shared<TokensFrontBack>();
327339
Token tok(list, std::move(tokensFrontBack));

test/testvalueflow.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5805,6 +5805,36 @@ class TestValueFlow : public TestFixture {
58055805
" bool b = x;\n"
58065806
"}\n";
58075807
ASSERT_EQUALS(false, testValueOfX(code, 3U, 1));
5808+
5809+
code = "int f(int a) {\n"
5810+
" int x = a ^ a;\n"
5811+
" return x;\n"
5812+
"}\n";
5813+
ASSERT_EQUALS(true, testValueOfX(code, 3U, 0));
5814+
5815+
code = "int f(int a) {\n"
5816+
" int x = a /= a;\n"
5817+
" return x;\n"
5818+
"}\n";
5819+
ASSERT_EQUALS(true, testValueOfX(code, 3U, 1));
5820+
5821+
code = "int f(int a) {\n"
5822+
" int x = a -= a;\n"
5823+
" return x;\n"
5824+
"}\n";
5825+
ASSERT_EQUALS(true, testValueOfX(code, 3U, 0));
5826+
5827+
code = "int f(int a) {\n"
5828+
" int x = a %= a;\n"
5829+
" return x;\n"
5830+
"}\n";
5831+
ASSERT_EQUALS(true, testValueOfX(code, 3U, 0));
5832+
5833+
code = "int f(int a) {\n"
5834+
" int x = a ^= a;\n"
5835+
" return x;\n"
5836+
"}\n";
5837+
ASSERT_EQUALS(true, testValueOfX(code, 3U, 0));
58085838
}
58095839

58105840
void valueFlowUninit() {

0 commit comments

Comments
 (0)