Fixed #6153 (ValueFlowBeforeCondition: Handle global variables)
This commit is contained in:
parent
f1e5f64690
commit
ad1749738e
|
@ -1169,8 +1169,16 @@ static void valueFlowReverse(TokenList *tokenlist,
|
||||||
break;
|
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)
|
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)
|
if (varid == 0U || !var)
|
||||||
continue;
|
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
|
// bailout: for/while-condition, variable is changed in while loop
|
||||||
for (const Token *tok2 = tok; tok2; tok2 = tok2->astParent()) {
|
for (const Token *tok2 = tok; tok2; tok2 = tok2->astParent()) {
|
||||||
if (tok2->astParent() || tok2->str() != "(" || !Token::simpleMatch(tok2->link(), ") {"))
|
if (tok2->astParent() || tok2->str() != "(" || !Token::simpleMatch(tok2->link(), ") {"))
|
||||||
|
|
|
@ -963,16 +963,17 @@ private:
|
||||||
}
|
}
|
||||||
|
|
||||||
void valueFlowBeforeConditionGlobalVariables() {
|
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;
|
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"
|
code = "class Fred { int x; void clear(); void f(); };\n"
|
||||||
"void Fred::f() {\n"
|
"void Fred::f() {\n"
|
||||||
" int a = x;\n"
|
" int a = x;\n"
|
||||||
|
|
Loading…
Reference in New Issue