From 7cb5c97e7d9c08e69ffe94dacec55a205ec83081 Mon Sep 17 00:00:00 2001 From: Robert Reif Date: Sat, 3 Sep 2011 12:22:13 -0400 Subject: [PATCH] move member variable lookup code from a check to the symbol database so it can be reused by other checks --- lib/checkbufferoverrun.cpp | 13 ------------- lib/symboldatabase.cpp | 28 ++++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 13 deletions(-) diff --git a/lib/checkbufferoverrun.cpp b/lib/checkbufferoverrun.cpp index aca212211..2a5d82577 100644 --- a/lib/checkbufferoverrun.cpp +++ b/lib/checkbufferoverrun.cpp @@ -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); - } - } } } } diff --git a/lib/symboldatabase.cpp b/lib/symboldatabase.cpp index bf5f1f476..39ebdce1a 100644 --- a/lib/symboldatabase.cpp +++ b/lib/symboldatabase.cpp @@ -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++) {