From d35ce5f0db2d0c03fab8391594cd246a83a7a350 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Mon, 4 Aug 2014 12:31:04 +0200 Subject: [PATCH] ValueFlow: Better handling of calculated function arguments in valueFlowSubFunction --- lib/valueflow.cpp | 8 ++++---- test/testvalueflow.cpp | 10 ++++++++++ 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 527b004a7..9f87e4c74 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -1327,11 +1327,9 @@ static void valueFlowSubFunction(TokenList *tokenlist, ErrorLogger *errorLogger, continue; // passing value(s) to function - if (Token::Match(tok, "[(,] %var% [,)]") && !tok->next()->values.empty()) + if (Token::Match(tok, "[(,] %var%|%num%|%str% [,)]") && !tok->next()->values.empty()) argvalues = tok->next()->values; - else if (Token::Match(tok, "[(,] %num%|%str% [,)]") && !tok->next()->values.empty()) { - argvalues = tok->next()->values; - } else { + else { // bool operator => values 1/0 are passed to function.. const Token *op = tok->next(); while (op && op->astParent() && !Token::Match(op->astParent(), "[(,]")) @@ -1340,6 +1338,8 @@ static void valueFlowSubFunction(TokenList *tokenlist, ErrorLogger *errorLogger, argvalues.clear(); argvalues.push_back(ValueFlow::Value(0)); argvalues.push_back(ValueFlow::Value(1)); + } else if (Token::Match(op, "%cop%") && !op->values.empty()) { + argvalues = op->values; } else { // possible values are unknown.. continue; diff --git a/test/testvalueflow.cpp b/test/testvalueflow.cpp index 67776de44..3d14ebcb9 100644 --- a/test/testvalueflow.cpp +++ b/test/testvalueflow.cpp @@ -1096,6 +1096,16 @@ private: "}\n" "void f2() { f1(0.5); }"; ASSERT_EQUALS(false, testValueOfX(code, 2U, 0)); + + code = "void dostuff(int x) {\n" + " return x/x;\n" + "}\n" + "\n" + "void test(int x) {\n" + " if(x==1) {}\n" + " dostuff(x+1);\n" + "}\n"; + ASSERT_EQUALS(true, testValueOfX(code, 2U, 2)); } void valueFlowFunctionReturn() {