move member variable lookup code from a check to the symbol database so it can be reused by other checks

This commit is contained in:
Robert Reif 2011-09-03 12:22:13 -04:00
parent e8daaa69d6
commit 7cb5c97e7d
2 changed files with 28 additions and 13 deletions

View File

@ -1931,19 +1931,6 @@ void CheckBufferOverrun::negativeIndex()
const Variable *var = _tokenizer->getSymbolDatabase()->getVariableFromVarId(tok2->previous()->varId());
if (var && var->isArray())
negativeIndexError(tok, index);
// check if this variable is a member of a class/struct
else if (!var && Token::Match(tok2->tokAt(-3), "%var% ."))
{
var = _tokenizer->getSymbolDatabase()->getVariableFromVarId(tok2->tokAt(-3)->varId());
if (var && var->type())
{
// get the variable type from the class/struct
const Variable *var2 = var->type()->getVariable(tok2->previous()->str());
if (var2 && var2->isArray())
negativeIndexError(tok, index);
}
}
}
}
}

View File

@ -803,6 +803,34 @@ SymbolDatabase::SymbolDatabase(const Tokenizer *tokenizer, const Settings *setti
}
}
// search for member variables of record types and add them to the variable list
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next())
{
if (Token::Match(tok, ". %var%") && tok->next()->varId())
{
unsigned int varid1 = tok->next()->varId();
if (_variableList[varid1] == NULL)
{
if (Token::Match(tok->previous(), "%var%") && tok->previous()->varId())
{
unsigned int varid2 = tok->previous()->varId();
if (_variableList[varid2])
{
// get the variable type from the class/struct
const Scope * type = _variableList[varid2]->type();
if (type)
{
const Variable *var = type->getVariable(tok->next()->str());
if (var)
_variableList[varid1] = _variableList[var->varId()];
}
}
}
}
}
}
/* set all unknown array dimensions that are set by a variable to the maximum size of that variable type */
for (size_t i = 1; i <= _tokenizer->varIdCount(); i++)
{