ValueFlow: Make known loop value possible
This commit is contained in:
parent
9bdee7fce9
commit
61cf224f32
|
@ -932,6 +932,23 @@ static bool valueFlowForward(Token * const startToken,
|
|||
return false;
|
||||
}
|
||||
|
||||
// if known variable is changed in loop body, change it to a possible value..
|
||||
if (tok2->str() != "if") {
|
||||
bool isChanged = false;
|
||||
for (std::list<ValueFlow::Value>::iterator it = values.begin(); it != values.end(); ++it) {
|
||||
if (it->valueKind == ValueFlow::Value::Known) {
|
||||
if (!isChanged) {
|
||||
const Token *bodyStart = tok2->linkAt(1)->next();
|
||||
if (!isVariableChanged(bodyStart, bodyStart->link(), varid))
|
||||
break;
|
||||
isChanged = true;
|
||||
}
|
||||
|
||||
it->valueKind = ValueFlow::Value::Possible;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Set values in condition
|
||||
for (Token* tok3 = tok2->tokAt(2); tok3 != tok2->next()->link(); tok3 = tok3->next()) {
|
||||
if (tok3->varId() == varid) {
|
||||
|
|
|
@ -1566,6 +1566,16 @@ private:
|
|||
}
|
||||
ASSERT_EQUALS(true, possible);
|
||||
|
||||
code = "void f() {\n"
|
||||
" int x = 0;\n"
|
||||
" while (!x) {\n" // <- possible value
|
||||
" x = dostuff();\n"
|
||||
" }\n"
|
||||
"}";
|
||||
value = valueOfTok(code, "!");
|
||||
ASSERT_EQUALS(1, value.intvalue);
|
||||
ASSERT_EQUALS(ValueFlow::Value::ValueKind::Possible, value.valueKind);
|
||||
|
||||
// after condition
|
||||
code = "int f(int x) {\n"
|
||||
" if (x == 4) {}\n"
|
||||
|
|
Loading…
Reference in New Issue