diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 1576bb448..b43e6cd27 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -2497,7 +2497,7 @@ static void valueFlowFunctionReturn(TokenList *tokenlist, ErrorLogger *errorLogg // Arguments.. std::vector parvalues; - { + if (tok->astOperand2()) { const Token *partok = tok->astOperand2(); while (partok && partok->str() == "," && constval(partok->astOperand2())) partok = partok->astOperand1(); @@ -2517,7 +2517,7 @@ static void valueFlowFunctionReturn(TokenList *tokenlist, ErrorLogger *errorLogg const Function * const function = tok->astOperand1()->function(); const Scope * const functionScope = function->functionScope; if (!functionScope || !Token::simpleMatch(functionScope->classStart, "{ return")) { - if (functionScope && settings->debugwarnings) + if (functionScope && settings->debugwarnings && Token::findsimplematch(functionScope->classStart, "return", functionScope->classEnd)) bailout(tokenlist, errorLogger, tok, "function return; nontrivial function body"); continue; } @@ -2533,7 +2533,7 @@ static void valueFlowFunctionReturn(TokenList *tokenlist, ErrorLogger *errorLogg } programMemory.setIntValue(arg->declarationId(), parvalues[i]); } - if (programMemory.empty()) + if (programMemory.empty() && !parvalues.empty()) continue; // Determine return value of subfunction.. @@ -2543,8 +2543,13 @@ static void valueFlowFunctionReturn(TokenList *tokenlist, ErrorLogger *errorLogg &programMemory, &result, &error); - if (!error) - setTokenValue(tok, ValueFlow::Value(result)); + if (!error) { + ValueFlow::Value v(result); + if (functionScope->classStart->next()->astOperand1()->hasKnownIntValue()) + v.setKnown(); + // TODO: Known input parameters => known return value + setTokenValue(tok, v); + } } } diff --git a/test/testvalueflow.cpp b/test/testvalueflow.cpp index f396acf56..ae4e2eacf 100644 --- a/test/testvalueflow.cpp +++ b/test/testvalueflow.cpp @@ -1761,7 +1761,7 @@ private: void valueFlowFunctionReturn() { const char *code; - code = "void f1(int x) {\n" + code = "int f1(int x) {\n" " return x+1;\n" "}\n" "void f2() {\n" @@ -1769,13 +1769,17 @@ private: "}"; ASSERT_EQUALS(7, valueOfTok(code, "-").intvalue); - code = "void add(int x, int y) {\n" + code = "int add(int x, int y) {\n" " return x+y;\n" "}\n" "void f2() {\n" " x = 1 * add(10+1,4);\n" "}"; ASSERT_EQUALS(15, valueOfTok(code, "*").intvalue); + + code = "int one() { return 1; }\n" + "void f() { x = 1 * one(); }"; + ASSERT_EQUALS(1, valueOfTok(code, "*").intvalue); } void valueFlowFunctionDefaultParameter() {