ValueFlow: fixed known/possible value of static variable

This commit is contained in:
Daniel Marjamäki 2015-07-17 20:48:37 +02:00
parent f1e410a878
commit 25d9ebedd8
2 changed files with 18 additions and 1 deletions

View File

@ -1318,8 +1318,17 @@ static void valueFlowAfterAssign(TokenList *tokenlist, SymbolDatabase* symboldat
if (!tok->astOperand2() || tok->astOperand2()->values.empty())
continue;
const std::list<ValueFlow::Value>& values = tok->astOperand2()->values;
std::list<ValueFlow::Value> values = tok->astOperand2()->values;
const bool constValue = tok->astOperand2()->isNumber();
// Static variable initialisation?
if (var->isStatic() && var->nameToken() == tok->astOperand1()) {
for (std::list<ValueFlow::Value>::iterator it = values.begin(); it != values.end(); ++it) {
if (it->valueKind == ValueFlow::Value::ValueKind::Known)
it->valueKind = ValueFlow::Value::ValueKind::Possible;
}
}
valueFlowForward(tok, endOfVarScope, var, varid, values, constValue, tokenlist, errorLogger, settings);
}
}

View File

@ -1576,6 +1576,14 @@ private:
ASSERT_EQUALS(1, value.intvalue);
ASSERT_EQUALS(ValueFlow::Value::ValueKind::Possible, value.valueKind);
code = "void f() {\n"
" static int x = 0;\n"
" return x + 1;\n" // <- possible value
"}\n";
value = valueOfTok(code, "+");
ASSERT_EQUALS(1, value.intvalue);
ASSERT_EQUALS(ValueFlow::Value::ValueKind::Possible, value.valueKind);
code = "void f() {\n"
" int x = 0;\n"
" do {\n"