Fixed #6153 (ValueFlowBeforeCondition: Handle global variables)

This commit is contained in:
Daniel Marjamäki 2017-10-17 16:55:37 +02:00
parent f1e5f64690
commit ad1749738e
2 changed files with 19 additions and 17 deletions

View File

@ -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(), ") {"))

View File

@ -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"