From ad1749738e062ff53b6b2ad439e0b87af07b6683 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Tue, 17 Oct 2017 16:55:37 +0200 Subject: [PATCH] Fixed #6153 (ValueFlowBeforeCondition: Handle global variables) --- lib/valueflow.cpp | 17 +++++++++-------- test/testvalueflow.cpp | 19 ++++++++++--------- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index fe788b5dd..3cb9c058d 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -1169,8 +1169,16 @@ static void valueFlowReverse(TokenList *tokenlist, break; } } - } + if (Token::Match(tok2, "%name% (") && !Token::simpleMatch(tok2->linkAt(1), ") {")) { + // bailout: global non-const variables + if (!(var->isLocal() || var->isArgument()) && !var->isConst()) { + if (settings->debugwarnings) + bailout(tokenlist, errorLogger, tok, "global variable " + var->name()); + return; + } + } + } } static void valueFlowBeforeCondition(TokenList *tokenlist, SymbolDatabase *symboldatabase, ErrorLogger *errorLogger, const Settings *settings) @@ -1208,13 +1216,6 @@ static void valueFlowBeforeCondition(TokenList *tokenlist, SymbolDatabase *symbo if (varid == 0U || !var) continue; - // bailout: global non-const variables - if (!(var->isLocal() || var->isArgument()) && !var->isConst()) { - if (settings->debugwarnings) - bailout(tokenlist, errorLogger, tok, "global variable " + var->name()); - continue; - } - // bailout: for/while-condition, variable is changed in while loop for (const Token *tok2 = tok; tok2; tok2 = tok2->astParent()) { if (tok2->astParent() || tok2->str() != "(" || !Token::simpleMatch(tok2->link(), ") {")) diff --git a/test/testvalueflow.cpp b/test/testvalueflow.cpp index 36f0ad918..4a6588e58 100644 --- a/test/testvalueflow.cpp +++ b/test/testvalueflow.cpp @@ -963,16 +963,17 @@ private: } void valueFlowBeforeConditionGlobalVariables() { - // bailout: global variables - bailout("int x;\n" - "void f() {\n" - " int a = x;\n" - " if (x == 123) {}\n" - "}"); - ASSERT_EQUALS_WITHOUT_LINENUMBERS("[test.cpp:4]: (debug) valueflow.cpp:1226:valueFlowBeforeCondition bailout: global variable x\n", errout.str()); - - // class variable const char *code; + + // handle global variables + code = "int x;\n" + "void f() {\n" + " int a = x;\n" + " if (x == 123) {}\n" + "}"; + ASSERT_EQUALS(true, testValueOfX(code,3,123)); + + // bailout when there is function call code = "class Fred { int x; void clear(); void f(); };\n" "void Fred::f() {\n" " int a = x;\n"