From 77cd6c194f5f44e13b0f69fa69942f04b1151a8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 22 Oct 2016 12:24:15 +0200 Subject: [PATCH] ValueFlow: Set 'known' property for function return values --- lib/valueflow.cpp | 12 +++++------- test/testvalueflow.cpp | 3 +++ 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index b43e6cd27..e89fad9e6 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -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 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 + v.setKnown(); setTokenValue(tok, v); } } diff --git a/test/testvalueflow.cpp b/test/testvalueflow.cpp index ae4e2eacf..90ebcd762 100644 --- a/test/testvalueflow.cpp +++ b/test/testvalueflow.cpp @@ -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() {