From c1e35e1df10623d154494c5cdb7a8d9c8746ea6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 25 Jan 2014 19:13:33 +0100 Subject: [PATCH] value flow: fixed multivariable problem in condition --- lib/valueflow.cpp | 22 +++++++++++++++++----- test/testvalueflow.cpp | 14 ++++++++++++++ 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index d7633a2fa..4e29d6248 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -487,11 +487,23 @@ static void valueFlowAfterAssign(TokenList *tokenlist, ErrorLogger *errorLogger, if (Token::Match(tok2, "%var% (") && Token::Match(tok2->linkAt(1), ") {")) { Token * const start = tok2->linkAt(1)->next(); Token * const end = start->link(); - // TODO: don't check noreturn scopes - if (number_of_if > 0U && Token::findmatch(start, "%varid%", end, varid)) { - if (settings->debugwarnings) - bailout(tokenlist, errorLogger, tok2, "variable " + var->nameToken()->str() + " is assigned in conditional code"); - break; + if (Token::findmatch(start, "%varid%", end, varid)) { + // TODO: don't check noreturn scopes + if (number_of_if > 0U) { + if (settings->debugwarnings) + bailout(tokenlist, errorLogger, tok2, "variable " + var->nameToken()->str() + " is assigned in conditional code"); + break; + } + + // Remove conditional values + std::list::iterator it; + for (it = values.begin(); it != values.end();) { + if (it->condition) { + values.erase(it++); + } else { + ++it; + } + } } if (Token::findmatch(start, "++|-- %varid%", end, varid) || Token::findmatch(start, "%varid% ++|--|=", end, varid)) { diff --git a/test/testvalueflow.cpp b/test/testvalueflow.cpp index 746886a98..3fca492f7 100644 --- a/test/testvalueflow.cpp +++ b/test/testvalueflow.cpp @@ -546,6 +546,20 @@ private: " a = 2 + x;\n" "}"; ASSERT_EQUALS(false, testValueOfX(code, 5U, 1)); + + // multivariables + code = "void f(int a) {\n" + " int x = a;\n" + " if (a!=132) { b = x; }\n" + "}"; + ASSERT_EQUALS(false, testValueOfX(code, 3U, 132)); + + code = "void f(int a) {\n" + " int x = a;\n" + " b = x;\n" + " if (a!=132) {}\n" + "}"; + ASSERT_EQUALS(true, testValueOfX(code, 3U, 132)); } void valueFlowForLoop() {