From acb103e214044b0922ff1ec655fa0da118dd5aab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Fri, 10 Jan 2014 16:13:39 +0100 Subject: [PATCH] value flow: skip scopes that don't contain variable --- lib/valueflow.cpp | 12 ++++++++---- test/testvalueflow.cpp | 9 ++++++++- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index c0fc0c105..a0cba6317 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -103,7 +103,7 @@ static void valueFlowBeforeCondition(TokenList *tokenlist, ErrorLogger *errorLog } break; } - + if (tok2->varId() == varid) { // bailout: assignment if (Token::Match(tok2->previous(), "!!* %var% =")) { @@ -130,9 +130,13 @@ static void valueFlowBeforeCondition(TokenList *tokenlist, ErrorLogger *errorLog } if (tok2->str() == "}") { - if (settings->debugwarnings) - bailout(tokenlist, errorLogger, tok2, "variable " + var->nameToken()->str() + " stopping on }"); - break; + if (Token::findmatch(tok2->link(), "%varid%", tok2, varid)) { + if (settings->debugwarnings) + bailout(tokenlist, errorLogger, tok2, "variable " + var->nameToken()->str() + " stopping on }"); + break; + } else { + tok2 = tok2->link(); + } } } } diff --git a/test/testvalueflow.cpp b/test/testvalueflow.cpp index e71842c7c..113223af3 100644 --- a/test/testvalueflow.cpp +++ b/test/testvalueflow.cpp @@ -117,12 +117,19 @@ private: ASSERT_EQUALS("[test.cpp:2]: (debug) ValueFlow bailout: no simplification of x within ?: expression\n", errout.str()); code = "void f(int x) {\n" - " int a = x;\n" + " int a =v x;\n" " a = b ? x/2 : 20/x;\n" " if (x == 123) {}\n" "}"; ASSERT_EQUALS(true, testValueOfX(code, 2U, 123)); + code = "void f(int x, int y) {\n" + " a = x;\n" + " if (y){}\n" + " if (x==123){}\n" + "}"; + ASSERT_EQUALS(true, testValueOfX(code, 2U, 123)); + // bailout: if/else/etc bailout("void f(int x) {\n" " if (x != 123) { b = x; }\n"