Skip to content

Commit ad17497

Browse files
committed
Fixed danmar#6153 (ValueFlowBeforeCondition: Handle global variables)
1 parent f1e5f64 commit ad17497

2 files changed

Lines changed: 19 additions & 17 deletions

File tree

lib/valueflow.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1169,8 +1169,16 @@ static void valueFlowReverse(TokenList *tokenlist,
11691169
break;
11701170
}
11711171
}
1172-
}
11731172

1173+
if (Token::Match(tok2, "%name% (") && !Token::simpleMatch(tok2->linkAt(1), ") {")) {
1174+
// bailout: global non-const variables
1175+
if (!(var->isLocal() || var->isArgument()) && !var->isConst()) {
1176+
if (settings->debugwarnings)
1177+
bailout(tokenlist, errorLogger, tok, "global variable " + var->name());
1178+
return;
1179+
}
1180+
}
1181+
}
11741182
}
11751183

11761184
static void valueFlowBeforeCondition(TokenList *tokenlist, SymbolDatabase *symboldatabase, ErrorLogger *errorLogger, const Settings *settings)
@@ -1208,13 +1216,6 @@ static void valueFlowBeforeCondition(TokenList *tokenlist, SymbolDatabase *symbo
12081216
if (varid == 0U || !var)
12091217
continue;
12101218

1211-
// bailout: global non-const variables
1212-
if (!(var->isLocal() || var->isArgument()) && !var->isConst()) {
1213-
if (settings->debugwarnings)
1214-
bailout(tokenlist, errorLogger, tok, "global variable " + var->name());
1215-
continue;
1216-
}
1217-
12181219
// bailout: for/while-condition, variable is changed in while loop
12191220
for (const Token *tok2 = tok; tok2; tok2 = tok2->astParent()) {
12201221
if (tok2->astParent() || tok2->str() != "(" || !Token::simpleMatch(tok2->link(), ") {"))

test/testvalueflow.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -963,16 +963,17 @@ class TestValueFlow : public TestFixture {
963963
}
964964

965965
void valueFlowBeforeConditionGlobalVariables() {
966-
// bailout: global variables
967-
bailout("int x;\n"
968-
"void f() {\n"
969-
" int a = x;\n"
970-
" if (x == 123) {}\n"
971-
"}");
972-
ASSERT_EQUALS_WITHOUT_LINENUMBERS("[test.cpp:4]: (debug) valueflow.cpp:1226:valueFlowBeforeCondition bailout: global variable x\n", errout.str());
973-
974-
// class variable
975966
const char *code;
967+
968+
// handle global variables
969+
code = "int x;\n"
970+
"void f() {\n"
971+
" int a = x;\n"
972+
" if (x == 123) {}\n"
973+
"}";
974+
ASSERT_EQUALS(true, testValueOfX(code,3,123));
975+
976+
// bailout when there is function call
976977
code = "class Fred { int x; void clear(); void f(); };\n"
977978
"void Fred::f() {\n"
978979
" int a = x;\n"

0 commit comments

Comments
 (0)