From 00340efc57abdd3f021409b61b367623068677fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Tue, 6 Nov 2018 06:38:26 +0100 Subject: [PATCH] ValueFlow: Refactoring valueFlowCallFunction --- lib/valueflow.cpp | 44 +++++++++++++++++++++++++----------------- test/testvalueflow.cpp | 2 +- 2 files changed, 27 insertions(+), 19 deletions(-) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index ebf3a4ee7..5c537b086 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -3439,11 +3439,25 @@ static bool evaluate(const Token *expr, const std::vector getFunctionArgumentValues(const Token *argtok) +{ + std::list argvalues(argtok->values()); + if (argvalues.empty()) { + if (Token::Match(argtok, "%comp%|%oror%|&&|!")) { + argvalues.emplace_back(0); + argvalues.emplace_back(1); + } else { + argvalues = argtok->values(); + } + } + return argvalues; +} + static void valueFlowLibraryFunction(Token *tok, const std::string &returnValue, const Settings *settings) { std::vector> argValues; for (const Token *argtok : getArguments(tok->previous())) { - argValues.emplace_back(argtok->values()); + argValues.emplace_back(getFunctionArgumentValues(argtok)); if (argValues.back().empty()) return; } @@ -3509,30 +3523,24 @@ static void valueFlowSubFunction(TokenList *tokenlist, ErrorLogger *errorLogger, break; // passing value(s) to function - std::list argvalues; - if (Token::Match(argtok, "%comp%|%oror%|&&|!") && !argtok->hasKnownIntValue()) { - argvalues.emplace_back(0); - argvalues.emplace_back(1); - } else { - argvalues = argtok->values(); - } + std::list argvalues(getFunctionArgumentValues(argtok)); if (argvalues.empty()) continue; // Error path.. - for (std::list::iterator it = argvalues.begin(); it != argvalues.end(); ++it) { + for (ValueFlow::Value &v : argvalues) { const std::string nr = MathLib::toString(argnr + 1) + getOrdinalText(argnr + 1); - it->errorPath.emplace_back(argtok, - "Calling function '" + - calledFunction->name() + - "', " + - nr + - " argument '" + - argvar->name() + - "' value is " + - it->infoString()); + v.errorPath.emplace_back(argtok, + "Calling function '" + + calledFunction->name() + + "', " + + nr + + " argument '" + + argtok->expressionString() + + "' value is " + + v.infoString()); } // passed values are not "known".. diff --git a/test/testvalueflow.cpp b/test/testvalueflow.cpp index 128a9ce68..252cae26d 100644 --- a/test/testvalueflow.cpp +++ b/test/testvalueflow.cpp @@ -876,7 +876,7 @@ private: " f1(x+1);\n" "}\n"; ASSERT_EQUALS("5,Assignment 'x=3', assigned value is 3\n" - "6,Calling function 'f1', 1st argument 'x' value is 4\n", + "6,Calling function 'f1', 1st argument 'x+1' value is 4\n", getErrorPathForX(code, 2U)); code = "void f(int a) {\n"