Fixed #10 (ValueFlow: condition variable has known value in switch case)

This commit is contained in:
Daniel Marjamäki 2017-10-16 21:51:30 +02:00
parent f27e98f910
commit 6d59290858
2 changed files with 13 additions and 2 deletions

View File

@ -2885,12 +2885,18 @@ static void valueFlowSwitchVariable(TokenList *tokenlist, SymbolDatabase* symbol
std::list<ValueFlow::Value> values; std::list<ValueFlow::Value> values;
values.push_back(ValueFlow::Value(MathLib::toLongNumber(tok->next()->str()))); values.push_back(ValueFlow::Value(MathLib::toLongNumber(tok->next()->str())));
values.back().condition = tok; values.back().condition = tok;
const std::string info("case " + tok->next()->str() + ": " + vartok->str() + " is " + tok->next()->str() + " here.");
values.back().errorPath.push_back(ErrorPathItem(tok, info));
if ((Token::simpleMatch(tok->previous(), "{") || Token::simpleMatch(tok->tokAt(-2), "break ;")) && !Token::Match(tok->tokAt(3), ";| case"))
values.back().setKnown();
while (Token::Match(tok->tokAt(3), ";| case %num% :")) { while (Token::Match(tok->tokAt(3), ";| case %num% :")) {
tok = tok->tokAt(3); tok = tok->tokAt(3);
if (!tok->isName()) if (!tok->isName())
tok = tok->next(); tok = tok->next();
values.push_back(ValueFlow::Value(MathLib::toLongNumber(tok->next()->str()))); values.push_back(ValueFlow::Value(MathLib::toLongNumber(tok->next()->str())));
values.back().condition = tok; values.back().condition = tok;
const std::string info2("case " + tok->next()->str() + ": " + vartok->str() + " is " + tok->next()->str() + " here.");
values.back().errorPath.push_back(ErrorPathItem(tok, info2));
} }
for (std::list<ValueFlow::Value>::const_iterator val = values.begin(); val != values.end(); ++val) { for (std::list<ValueFlow::Value>::const_iterator val = values.begin(); val != values.end(); ++val) {
valueFlowReverse(tokenlist, valueFlowReverse(tokenlist,
@ -2902,7 +2908,7 @@ static void valueFlowSwitchVariable(TokenList *tokenlist, SymbolDatabase* symbol
settings); settings);
} }
if (vartok->variable()->scope()) // #7257 if (vartok->variable()->scope()) // #7257
valueFlowForward(tok, vartok->variable()->scope()->classEnd, vartok->variable(), vartok->varId(), values, false, false, tokenlist, errorLogger, settings); valueFlowForward(tok->tokAt(3), vartok->variable()->scope()->classEnd, vartok->variable(), vartok->varId(), values, values.back().isKnown(), false, tokenlist, errorLogger, settings);
} }
} }
} }

View File

@ -1875,13 +1875,18 @@ private:
code = "void f(int x) {\n" code = "void f(int x) {\n"
" a = x;\n" // <- x can be 14 " a = x;\n" // <- x can be 14
" switch (x) {\n" " switch (x) {\n"
" case 14: a=x; break;\n" // <- x is 14 " case 14: a=x+2; break;\n" // <- x is 14
" };\n" " };\n"
" a = x;\n" // <- x can be 14 " a = x;\n" // <- x can be 14
"}"; "}";
ASSERT_EQUALS(true, testConditionalValueOfX(code, 2U, 14)); ASSERT_EQUALS(true, testConditionalValueOfX(code, 2U, 14));
ASSERT_EQUALS(true, testConditionalValueOfX(code, 4U, 14)); ASSERT_EQUALS(true, testConditionalValueOfX(code, 4U, 14));
ASSERT_EQUALS(true, testConditionalValueOfX(code, 6U, 14)); ASSERT_EQUALS(true, testConditionalValueOfX(code, 6U, 14));
ValueFlow::Value value = valueOfTok(code, "+");
ASSERT_EQUALS(16, value.intvalue);
ASSERT(value.isKnown());
} }
void valueFlowForLoop() { void valueFlowForLoop() {