Skip to content

Commit ca1f19b

Browse files
committed
Fixed danmar#6378 (valueFlowForward: decrement not handled)
1 parent aab1d83 commit ca1f19b

File tree

3 files changed

+31
-9
lines changed

3 files changed

+31
-9
lines changed

lib/valueflow.cpp

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,13 +1043,6 @@ static bool valueFlowForward(Token * const startToken,
10431043
return false;
10441044
}
10451045

1046-
// bailout increment/decrement for now..
1047-
if (Token::Match(tok2->previous(), "++|-- %name%") || Token::Match(tok2, "%name% ++|--")) {
1048-
if (settings->debugwarnings)
1049-
bailout(tokenlist, errorLogger, tok2, "increment/decrement of " + tok2->str());
1050-
return false;
1051-
}
1052-
10531046
// bailout: possible assignment using >>
10541047
if (Token::Match(tok2->previous(), ">> %name% >>|;")) {
10551048
const Token *parent = tok2->previous();
@@ -1081,6 +1074,34 @@ static bool valueFlowForward(Token * const startToken,
10811074
setTokenValue(tok2, *it);
10821075
}
10831076

1077+
// increment/decrement
1078+
if (Token::Match(tok2->previous(), "++|-- %name%") || Token::Match(tok2, "%name% ++|--")) {
1079+
const bool pre = Token::Match(tok2->previous(), "++|--");
1080+
Token * const op = pre ? tok2->previous() : tok2->next();
1081+
const bool inc = (op->str() == "++");
1082+
std::list<ValueFlow::Value>::iterator it;
1083+
// Erase values that are not int values..
1084+
for (it = values.begin(); it != values.end();) {
1085+
if (it->tokvalue)
1086+
it = values.erase(it);
1087+
else
1088+
++it;
1089+
}
1090+
if (values.empty()) {
1091+
if (settings->debugwarnings)
1092+
bailout(tokenlist, errorLogger, tok2, "increment/decrement of " + tok2->str());
1093+
return false;
1094+
}
1095+
// Perform increment/decrement..
1096+
for (it = values.begin(); it != values.end(); ++it) {
1097+
if (!pre)
1098+
setTokenValue(op, *it);
1099+
it->intvalue += (inc ? 1 : -1);
1100+
if (pre)
1101+
setTokenValue(op, *it);
1102+
}
1103+
}
1104+
10841105
// bailout if address of var is taken..
10851106
if (tok2->astParent() && tok2->astParent()->str() == "&" && !tok2->astParent()->astOperand2()) {
10861107
if (settings->debugwarnings)

test/testsimplifytypedef.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1099,7 +1099,7 @@ class TestSimplifyTypedef : public TestFixture {
10991099
"[test.cpp:20] -> [test.cpp:1]: (style, inconclusive) The function parameter 'A' hides a typedef with the same name.\n"
11001100
"[test.cpp:21] -> [test.cpp:1]: (style, inconclusive) The variable 'A' hides a typedef with the same name.\n"
11011101
"[test.cpp:24] -> [test.cpp:1]: (style, inconclusive) The typedef 'A' hides a typedef with the same name.\n"
1102-
"[test.cpp:21]: (debug) ValueFlow bailout: increment/decrement of a\n", errout.str());
1102+
, errout.str());
11031103
}
11041104

11051105
void simplifyTypedef36() {

test/testvalueflow.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,7 @@ class TestValueFlow : public TestFixture {
641641
" return x;\n"
642642
"}";
643643
ASSERT_EQUALS(false, testValueOfX(code, 4U, 9));
644+
ASSERT_EQUALS(true, testValueOfX(code, 4U, 8));
644645

645646
code = "void f() {\n"
646647
" int x = 0;\n"
@@ -1346,7 +1347,7 @@ class TestValueFlow : public TestFixture {
13461347
" for (x = 0; x < 20; x++) {}\n"
13471348
" a = x++;\n"
13481349
"}\n";
1349-
TODO_ASSERT_EQUALS(true, false, testValueOfX(code, 4U, 20));
1350+
ASSERT_EQUALS(true, testValueOfX(code, 4U, 20));
13501351

13511352
code = "void f() {\n"
13521353
" int x;\n"

0 commit comments

Comments
 (0)