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) { if (scope && scope->type == Scope::eSwitch) {
tok2 = const_cast<Token *>(scope->classEnd); tok2 = const_cast<Token *>(scope->classEnd);
--indentlevel; --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; continue;
} }
} }

View File

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