ValueFlow: Values that are assigned in switch are possible after switch

This commit is contained in:
Daniel Marjamäki 2015-07-20 09:36:56 +02:00
parent 8416be4d9d
commit c0880c8d79
2 changed files with 28 additions and 7 deletions

View File

@ -1141,6 +1141,10 @@ static bool valueFlowForward(Token * const startToken,
if (scope && scope->type == Scope::eSwitch) {
tok2 = const_cast<Token *>(scope->classEnd);
--indentlevel;
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;
}
continue;
}
}

View File

@ -1527,6 +1527,18 @@ private:
testValueOfX(code, 2U, 2); // Don't crash (#6494)
}
bool isNotKnownValues(const char code[], const char str[]) {
const std::list<ValueFlow::Value> values = tokenValues(code, str);
bool possible = false;
for (std::list<ValueFlow::Value>::const_iterator it = values.begin(); it != values.end(); ++it) {
if (it->valueKind == ValueFlow::Value::Known)
return false;
if (it->valueKind == ValueFlow::Value::Possible)
possible = true;
}
return possible;
}
void knownValue() {
const char *code;
ValueFlow::Value value;
@ -1561,13 +1573,18 @@ private:
" }\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);
ASSERT(isNotKnownValues(code, "!"));
code = "void f() {\n"
" int x = 0;\n"
" switch (state) {\n"
" case 1:\n"
" x = 1;\n"
" break;\n"
" }\n"
" if (!x) {}\n" // <- possible value
"}";
ASSERT(isNotKnownValues(code, "!"));
code = "void f() {\n"
" int x = 0;\n"