ValueFlow: Set 'known' property for function return values

This commit is contained in:
Daniel Marjamäki 2016-10-22 12:24:15 +02:00
parent 4130207edc
commit 77cd6c194f
2 changed files with 8 additions and 7 deletions

View File

@ -2484,9 +2484,9 @@ static void valueFlowFunctionDefaultParameter(TokenList *tokenlist, SymbolDataba
}
}
static bool constval(const Token * tok)
static bool isKnown(const Token * tok)
{
return tok && tok->values.size() == 1U && tok->values.front().varId == 0U;
return tok && tok->hasKnownIntValue();
}
static void valueFlowFunctionReturn(TokenList *tokenlist, ErrorLogger *errorLogger, const Settings *settings)
@ -2499,9 +2499,9 @@ static void valueFlowFunctionReturn(TokenList *tokenlist, ErrorLogger *errorLogg
std::vector<MathLib::bigint> parvalues;
if (tok->astOperand2()) {
const Token *partok = tok->astOperand2();
while (partok && partok->str() == "," && constval(partok->astOperand2()))
while (partok && partok->str() == "," && isKnown(partok->astOperand2()))
partok = partok->astOperand1();
if (!constval(partok))
if (!isKnown(partok))
continue;
parvalues.push_back(partok->values.front().intvalue);
partok = partok->astParent();
@ -2545,9 +2545,7 @@ static void valueFlowFunctionReturn(TokenList *tokenlist, ErrorLogger *errorLogg
&error);
if (!error) {
ValueFlow::Value v(result);
if (functionScope->classStart->next()->astOperand1()->hasKnownIntValue())
v.setKnown();
// TODO: Known input parameters => known return value
setTokenValue(tok, v);
}
}

View File

@ -1768,6 +1768,7 @@ private:
" x = 10 - f1(2);\n"
"}";
ASSERT_EQUALS(7, valueOfTok(code, "-").intvalue);
ASSERT_EQUALS(true, valueOfTok(code, "-").isKnown());
code = "int add(int x, int y) {\n"
" return x+y;\n"
@ -1776,10 +1777,12 @@ private:
" x = 1 * add(10+1,4);\n"
"}";
ASSERT_EQUALS(15, valueOfTok(code, "*").intvalue);
ASSERT_EQUALS(true, valueOfTok(code, "*").isKnown());
code = "int one() { return 1; }\n"
"void f() { x = 1 * one(); }";
ASSERT_EQUALS(1, valueOfTok(code, "*").intvalue);
ASSERT_EQUALS(true, valueOfTok(code, "*").isKnown());
}
void valueFlowFunctionDefaultParameter() {