value flow: fixed multivariable problem in condition

This commit is contained in:
Daniel Marjamäki 2014-01-25 19:13:33 +01:00
parent d227ed245f
commit c1e35e1df1
2 changed files with 31 additions and 5 deletions

View File

@ -487,12 +487,24 @@ static void valueFlowAfterAssign(TokenList *tokenlist, ErrorLogger *errorLogger,
if (Token::Match(tok2, "%var% (") && Token::Match(tok2->linkAt(1), ") {")) {
Token * const start = tok2->linkAt(1)->next();
Token * const end = start->link();
if (Token::findmatch(start, "%varid%", end, varid)) {
// TODO: don't check noreturn scopes
if (number_of_if > 0U && Token::findmatch(start, "%varid%", end, varid)) {
if (number_of_if > 0U) {
if (settings->debugwarnings)
bailout(tokenlist, errorLogger, tok2, "variable " + var->nameToken()->str() + " is assigned in conditional code");
break;
}
// Remove conditional values
std::list<ValueFlow::Value>::iterator it;
for (it = values.begin(); it != values.end();) {
if (it->condition) {
values.erase(it++);
} else {
++it;
}
}
}
if (Token::findmatch(start, "++|-- %varid%", end, varid) ||
Token::findmatch(start, "%varid% ++|--|=", end, varid)) {
if (number_of_if == 0 &&

View File

@ -546,6 +546,20 @@ private:
" a = 2 + x;\n"
"}";
ASSERT_EQUALS(false, testValueOfX(code, 5U, 1));
// multivariables
code = "void f(int a) {\n"
" int x = a;\n"
" if (a!=132) { b = x; }\n"
"}";
ASSERT_EQUALS(false, testValueOfX(code, 3U, 132));
code = "void f(int a) {\n"
" int x = a;\n"
" b = x;\n"
" if (a!=132) {}\n"
"}";
ASSERT_EQUALS(true, testValueOfX(code, 3U, 132));
}
void valueFlowForLoop() {