Fixed #8356 (ValueFlow: variable is not changed in for loop)
This commit is contained in:
parent
280a0a7b2f
commit
56df6169fb
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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"
|
||||
|
|
Loading…
Reference in New Issue