ValueFlow: Set 'known' property for function return values
This commit is contained in:
parent
4130207edc
commit
77cd6c194f
|
@ -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)
|
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;
|
std::vector<MathLib::bigint> parvalues;
|
||||||
if (tok->astOperand2()) {
|
if (tok->astOperand2()) {
|
||||||
const Token *partok = tok->astOperand2();
|
const Token *partok = tok->astOperand2();
|
||||||
while (partok && partok->str() == "," && constval(partok->astOperand2()))
|
while (partok && partok->str() == "," && isKnown(partok->astOperand2()))
|
||||||
partok = partok->astOperand1();
|
partok = partok->astOperand1();
|
||||||
if (!constval(partok))
|
if (!isKnown(partok))
|
||||||
continue;
|
continue;
|
||||||
parvalues.push_back(partok->values.front().intvalue);
|
parvalues.push_back(partok->values.front().intvalue);
|
||||||
partok = partok->astParent();
|
partok = partok->astParent();
|
||||||
|
@ -2545,9 +2545,7 @@ static void valueFlowFunctionReturn(TokenList *tokenlist, ErrorLogger *errorLogg
|
||||||
&error);
|
&error);
|
||||||
if (!error) {
|
if (!error) {
|
||||||
ValueFlow::Value v(result);
|
ValueFlow::Value v(result);
|
||||||
if (functionScope->classStart->next()->astOperand1()->hasKnownIntValue())
|
|
||||||
v.setKnown();
|
v.setKnown();
|
||||||
// TODO: Known input parameters => known return value
|
|
||||||
setTokenValue(tok, v);
|
setTokenValue(tok, v);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1768,6 +1768,7 @@ private:
|
||||||
" x = 10 - f1(2);\n"
|
" x = 10 - f1(2);\n"
|
||||||
"}";
|
"}";
|
||||||
ASSERT_EQUALS(7, valueOfTok(code, "-").intvalue);
|
ASSERT_EQUALS(7, valueOfTok(code, "-").intvalue);
|
||||||
|
ASSERT_EQUALS(true, valueOfTok(code, "-").isKnown());
|
||||||
|
|
||||||
code = "int add(int x, int y) {\n"
|
code = "int add(int x, int y) {\n"
|
||||||
" return x+y;\n"
|
" return x+y;\n"
|
||||||
|
@ -1776,10 +1777,12 @@ private:
|
||||||
" x = 1 * add(10+1,4);\n"
|
" x = 1 * add(10+1,4);\n"
|
||||||
"}";
|
"}";
|
||||||
ASSERT_EQUALS(15, valueOfTok(code, "*").intvalue);
|
ASSERT_EQUALS(15, valueOfTok(code, "*").intvalue);
|
||||||
|
ASSERT_EQUALS(true, valueOfTok(code, "*").isKnown());
|
||||||
|
|
||||||
code = "int one() { return 1; }\n"
|
code = "int one() { return 1; }\n"
|
||||||
"void f() { x = 1 * one(); }";
|
"void f() { x = 1 * one(); }";
|
||||||
ASSERT_EQUALS(1, valueOfTok(code, "*").intvalue);
|
ASSERT_EQUALS(1, valueOfTok(code, "*").intvalue);
|
||||||
|
ASSERT_EQUALS(true, valueOfTok(code, "*").isKnown());
|
||||||
}
|
}
|
||||||
|
|
||||||
void valueFlowFunctionDefaultParameter() {
|
void valueFlowFunctionDefaultParameter() {
|
||||||
|
|
Loading…
Reference in New Issue