ValueFlow: Known values in conditional code

This commit is contained in:
Daniel Marjamäki 2017-03-03 01:29:34 +01:00
parent 45c5860ca4
commit d1c66edb03
2 changed files with 27 additions and 0 deletions

View File

@ -2125,8 +2125,16 @@ static void valueFlowAfterCondition(TokenList *tokenlist, SymbolDatabase* symbol
}
if (startToken) {
if (values.size() == 1U && Token::Match(tok, "==|!")) {
const Token *parent = tok->astParent();
while (parent && parent->str() == "&&")
parent = parent->astParent();
if (parent && parent->str() == "(")
values.front().setKnown();
}
if (!valueFlowForward(startToken->next(), startToken->link(), var, varid, values, true, false, tokenlist, errorLogger, settings))
continue;
values.front().setPossible();
if (isVariableChanged(startToken, startToken->link(), varid, settings)) {
// TODO: The endToken should not be startToken->link() in the valueFlowForward call
if (settings->debugwarnings)

View File

@ -2247,6 +2247,25 @@ private:
ASSERT_EQUALS(1, value.intvalue);
ASSERT(value.isPossible());
// in conditional code
code = "void f(int x) {\n"
" if (!x) {\n"
" a = x+1;\n" // <- known value
" }\n"
"}";
value = valueOfTok(code, "+");
ASSERT_EQUALS(1, value.intvalue);
ASSERT(value.isKnown());
code = "void f(int x) {\n"
" if (a && 4==x && y) {\n"
" a = x+12;\n" // <- known value
" }\n"
"}";
value = valueOfTok(code, "+");
ASSERT_EQUALS(16, value.intvalue);
ASSERT(value.isKnown());
// after condition
code = "int f(int x) {\n"
" if (x == 4) {}\n"