ValueFlow: Better errorpath for increment/decrement

This commit is contained in:
Daniel Marjamäki 2017-08-25 23:07:26 +02:00
parent b1b8e5270a
commit 529b255e99
2 changed files with 11 additions and 2 deletions

View File

@ -959,15 +959,21 @@ static void valueFlowReverse(TokenList *tokenlist,
}
// increment/decrement
int inc = 0;
if (Token::Match(tok2->previous(), "[;{}] %name% ++|-- ;"))
val.intvalue += (tok2->strAt(1)=="++") ? -1 : 1;
inc = (tok2->strAt(1)=="++") ? -1 : 1;
else if (Token::Match(tok2->tokAt(-2), "[;{}] ++|-- %name% ;"))
val.intvalue += (tok2->strAt(-1)=="++") ? -1 : 1;
inc = (tok2->strAt(-1)=="++") ? -1 : 1;
else if (Token::Match(tok2->previous(), "++|-- %name%") || Token::Match(tok2, "%name% ++|--")) {
if (settings->debugwarnings)
bailout(tokenlist, errorLogger, tok2, "increment/decrement of " + tok2->str());
break;
}
if (inc != 0) {
val.intvalue += inc;
const std::string info(tok2->str() + " is " + std::string(inc==1 ? "decremented" : "incremented") + ", before this " + (inc==1?"decrement":"increment") + " the value is " + val.infoString());
val.errorPath.push_back(ErrorPathItem(tok2, info));
}
// compound assignment
if (Token::Match(tok2->previous(), "[;{}] %var% %assign%") && tok2->next()->str() != "=") {

View File

@ -730,6 +730,9 @@ private:
" if (x == 4);\n"
"}";
ASSERT_EQUALS(true, testValueOfX(code, 2U, 3));
ASSERT_EQUALS("4,Assuming that condition 'x==4' is not redundant\n"
"3,x is incremented, before this increment the value is 3\n",
getErrorPathForX(code, 2U));
// compound assignment += , -= , ...
code = "void f(int x) {\n"