Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/token.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ int multiCompareImpl(const Token *tok, const char *haystack, nonneg int varid)
const char *needle = tok->str().c_str();
const char *needlePointer = needle;
for (;;) {
if (needlePointer == needle && haystack[0] == '%' && haystack[1] != '|' && haystack[1] != '\0' && haystack[1] != ' ') {
if (needlePointer == needle && haystack[0] == '%' && haystack[1] != '|' && haystack[1] != '\0' && haystack[1] != ' ' && haystack[1] != '=') {
const int ret = multiComparePercent(tok, haystack, varid);
if (ret < 2)
return ret;
Expand Down
4 changes: 2 additions & 2 deletions lib/valueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -903,10 +903,10 @@ static void valueFlowSameExpressions(TokenList& tokenlist, const Settings& setti

long long val;

if (Token::Match(tok, "==|>=|<=|/")) {
if (Token::Match(tok, "==|>=|<=|/|/=")) {
val = 1;
}
else if (Token::Match(tok, "!=|>|<|%|-")) {
else if (Token::Match(tok, "!=|>|<|%|%=|-|-=|^|^=")) {
val = 0;
}
else
Expand Down
12 changes: 12 additions & 0 deletions test/testtoken.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ class TestToken : public TestFixture {
TEST_CASE(multiCompare3); // false positive for %or% on code using "|="
TEST_CASE(multiCompare4);
TEST_CASE(multiCompare5);
TEST_CASE(multiCompare6);
TEST_CASE(charTypes);
TEST_CASE(stringTypes);
TEST_CASE(getStrLength);
Expand Down Expand Up @@ -322,6 +323,17 @@ class TestToken : public TestFixture {
ASSERT_EQUALS(true, TokenTest::multiCompare(&tok, "+|%or%|%oror%", 0) >= 0);
}

void multiCompare6() const {
{
const SimpleTokenList stl("x %= y;");
ASSERT_EQUALS(true, Token::Match(stl.front(), "%name% %= %name%"));
}
{
const SimpleTokenList stl("x += y;");
ASSERT_EQUALS(false, Token::Match(stl.front(), "%name% %= %name%"));
}
}

void charTypes() const {
auto tokensFrontBack = std::make_shared<TokensFrontBack>();
Token tok(list, std::move(tokensFrontBack));
Expand Down
30 changes: 30 additions & 0 deletions test/testvalueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5805,6 +5805,36 @@ class TestValueFlow : public TestFixture {
" bool b = x;\n"
"}\n";
ASSERT_EQUALS(false, testValueOfX(code, 3U, 1));

code = "int f(int a) {\n"
" int x = a ^ a;\n"
" return x;\n"
"}\n";
ASSERT_EQUALS(true, testValueOfX(code, 3U, 0));

code = "int f(int a) {\n"
" int x = a /= a;\n"
" return x;\n"
"}\n";
ASSERT_EQUALS(true, testValueOfX(code, 3U, 1));

code = "int f(int a) {\n"
" int x = a -= a;\n"
" return x;\n"
"}\n";
ASSERT_EQUALS(true, testValueOfX(code, 3U, 0));

code = "int f(int a) {\n"
" int x = a %= a;\n"
" return x;\n"
"}\n";
ASSERT_EQUALS(true, testValueOfX(code, 3U, 0));

code = "int f(int a) {\n"
" int x = a ^= a;\n"
" return x;\n"
"}\n";
ASSERT_EQUALS(true, testValueOfX(code, 3U, 0));
}

void valueFlowUninit() {
Expand Down
Loading