ValueFlow: Change value to 'Possible' after conditional assignment in inner loop

This commit is contained in:
Daniel Marjamäki 2015-07-16 21:08:32 +02:00
parent 38214b6907
commit 09efe140fe
2 changed files with 22 additions and 1 deletions

View File

@ -1002,10 +1002,13 @@ static bool valueFlowForward(Token * const startToken,
for (it = values.begin(); it != values.end();) { for (it = values.begin(); it != values.end();) {
if (it->condition || it->conditional) if (it->condition || it->conditional)
values.erase(it++); values.erase(it++);
else else {
if (it->valueKind == ValueFlow::Value::Known)
it->valueKind = ValueFlow::Value::Possible;
++it; ++it;
} }
} }
}
// stop after conditional noreturn scopes that are executed // stop after conditional noreturn scopes that are executed
if (isReturn(end)) { if (isReturn(end)) {

View File

@ -1548,6 +1548,24 @@ private:
ASSERT_EQUALS(9, value.intvalue); ASSERT_EQUALS(9, value.intvalue);
ASSERT_EQUALS(ValueFlow::Value::ValueKind::Possible, value.valueKind); ASSERT_EQUALS(ValueFlow::Value::ValueKind::Possible, value.valueKind);
code = "void f() {\n"
" int x = 0;\n"
" for (int i = 0; i < 10; i++) {\n"
" if (cond) {\n"
" x = 1;\n"
" break;\n"
" }\n"
" }\n"
" if (!x) {}\n" // <- possible value
"}";
std::list<ValueFlow::Value> values = tokenValues(code, "!");
bool possible = false;
for (std::list<ValueFlow::Value>::const_iterator it = values.begin(); it != values.end(); ++it) {
if (it->valueKind == ValueFlow::Value::Possible)
possible = true;
}
ASSERT_EQUALS(true, possible);
// after condition // after condition
code = "int f(int x) {\n" code = "int f(int x) {\n"
" if (x == 4) {}\n" " if (x == 4) {}\n"