Fixed #8356 (ValueFlow: variable is not changed in for loop)

This commit is contained in:
Daniel Marjamäki 2019-06-30 17:49:44 +02:00
parent 280a0a7b2f
commit 56df6169fb
2 changed files with 16 additions and 1 deletions

View File

@ -3635,6 +3635,11 @@ static std::list<ValueFlow::Value> truncateValues(std::list<ValueFlow::Value> va
return values;
}
static bool isLiteralNumber(const Token *tok, bool cpp)
{
return tok->isNumber() || tok->str() == "NULL" || (cpp && Token::Match(tok, "false|true|nullptr"));
}
static void valueFlowAfterAssign(TokenList *tokenlist, SymbolDatabase* symboldatabase, ErrorLogger *errorLogger, const Settings *settings)
{
for (const Scope * scope : symboldatabase->functionScopes) {
@ -3665,7 +3670,7 @@ static void valueFlowAfterAssign(TokenList *tokenlist, SymbolDatabase* symboldat
continue;
std::list<ValueFlow::Value> values = truncateValues(tok->astOperand2()->values(), tok->astOperand1()->valueType(), settings);
const bool constValue = tok->astOperand2()->isNumber();
const bool constValue = isLiteralNumber(tok->astOperand2(), tokenlist->isCPP());
const bool init = var->nameToken() == tok->astOperand1();
valueFlowForwardAssign(tok->astOperand2(), var, values, constValue, init, tokenlist, errorLogger, settings);
}

View File

@ -3097,6 +3097,16 @@ private:
"}";
ASSERT(isNotKnownValues(code, "!"));
code = "void f() {\n" // #8356
" bool b = false;\n"
" for(int x = 3; !b && x < 10; x++) {\n" // <- b has known value
" for(int y = 4; !b && y < 20; y++) {}\n"
" }\n"
"}";
value = valueOfTok(code, "!");
ASSERT_EQUALS(1, value.intvalue);
ASSERT(value.isKnown());
code = "void f() {\n"
" int x = 0;\n"
" switch (state) {\n"