ValueFlow: Avoid FPs for aliased variables

This commit is contained in:
Daniel Marjamäki 2017-09-03 23:24:55 +02:00
parent bdef3f4582
commit a02fd54a69
2 changed files with 20 additions and 0 deletions

View File

@ -2171,9 +2171,13 @@ static void valueFlowAfterCondition(TokenList *tokenlist, SymbolDatabase* symbol
const std::size_t functions = symboldatabase->functionScopes.size();
for (std::size_t i = 0; i < functions; ++i) {
const Scope * scope = symboldatabase->functionScopes[i];
std::set<unsigned> aliased;
for (Token* tok = const_cast<Token*>(scope->classStart); tok != scope->classEnd; tok = tok->next()) {
const Token *vartok, *numtok;
if (Token::Match(tok, "= & %var% ;"))
aliased.insert(tok->tokAt(2)->varId());
// Comparison
if (Token::Match(tok, "==|!=|>=|<=")) {
if (!tok->astOperand1() || !tok->astOperand2())
@ -2211,6 +2215,11 @@ static void valueFlowAfterCondition(TokenList *tokenlist, SymbolDatabase* symbol
const Variable *var = vartok->variable();
if (!var || !(var->isLocal() || var->isGlobal() || var->isArgument()))
continue;
if (aliased.find(varid) != aliased.end()) {
if (settings->debugwarnings)
bailout(tokenlist, errorLogger, vartok, "variable is aliased so we just skip all valueflow after condition");
continue;
}
std::list<ValueFlow::Value> values;
values.push_back(ValueFlow::Value(tok, numtok ? numtok->values().front().intvalue : 0LL));

View File

@ -1743,6 +1743,17 @@ private:
" a = x;\n"
"}";
ASSERT_EQUALS(false, testValueOfX(code, 3U, 0));
// aliased variable
code = "void f() {\n"
" int x = 1;\n"
" int *data = &x;\n"
" if (!x) {\n"
" calc(data);\n"
" a = x;\n" // <- x might be changed by calc
" }\n"
"}";
ASSERT_EQUALS(false, testValueOfX(code, 6U, 0));
}
void valueFlowForwardCompoundAssign() {