Fixed #7778 (valueFlowAfterAssign: alias)

This commit is contained in:
Daniel Marjamäki 2016-11-21 17:26:36 +01:00
parent 21364b4401
commit d40f4e4a55
2 changed files with 20 additions and 0 deletions

View File

@ -1800,8 +1800,15 @@ static void valueFlowAfterAssign(TokenList *tokenlist, SymbolDatabase* symboldat
{ {
const std::size_t functions = symboldatabase->functionScopes.size(); const std::size_t functions = symboldatabase->functionScopes.size();
for (std::size_t i = 0; i < functions; ++i) { for (std::size_t i = 0; i < functions; ++i) {
std::set<unsigned int> aliased;
const Scope * scope = symboldatabase->functionScopes[i]; const Scope * scope = symboldatabase->functionScopes[i];
for (Token* tok = const_cast<Token*>(scope->classStart); tok != scope->classEnd; tok = tok->next()) { for (Token* tok = const_cast<Token*>(scope->classStart); tok != scope->classEnd; tok = tok->next()) {
// Alias
if (tok->str() == "&" && !tok->astOperand2() && tok->astOperand1()) {
aliased.insert(tok->astOperand1()->varId());
continue;
}
// Assignment // Assignment
if ((tok->str() != "=") || (tok->astParent())) if ((tok->str() != "=") || (tok->astParent()))
continue; continue;
@ -1810,6 +1817,8 @@ static void valueFlowAfterAssign(TokenList *tokenlist, SymbolDatabase* symboldat
if (!tok->astOperand1() || !tok->astOperand1()->varId()) if (!tok->astOperand1() || !tok->astOperand1()->varId())
continue; continue;
const unsigned int varid = tok->astOperand1()->varId(); const unsigned int varid = tok->astOperand1()->varId();
if (aliased.find(varid) != aliased.end())
continue;
const Variable *var = tok->astOperand1()->variable(); const Variable *var = tok->astOperand1()->variable();
if (!var || (!var->isLocal() && !var->isArgument())) if (!var || (!var->isLocal() && !var->isArgument()))
continue; continue;

View File

@ -1342,6 +1342,17 @@ private:
" }\n" " }\n"
"}\n"; "}\n";
ASSERT_EQUALS(false, testValueOfX(code, 3U, 0)); ASSERT_EQUALS(false, testValueOfX(code, 3U, 0));
// alias
code = "void f() {\n" // #7778
" int x = 0;\n"
" int *p = &x;\n"
" x = 3;\n"
" *p = 2;\n"
" a = x;\n"
"}\n";
ASSERT_EQUALS(false, testValueOfX(code, 6U, 3));
TODO_ASSERT_EQUALS(true, false, testValueOfX(code, 6U, 2));
} }
void valueFlowAfterCondition() { void valueFlowAfterCondition() {