speed up checks by caching commonly looked up stuff in the symbol database (CheckBufferOverrun, CheckBoost)

This commit is contained in:
Robert Reif 2012-10-13 11:16:48 +02:00 committed by Daniel Marjamäki
parent d7c7f8c9af
commit 0f8db28d30
2 changed files with 99 additions and 86 deletions

View File

@ -17,6 +17,7 @@
*/ */
#include "checkboost.h" #include "checkboost.h"
#include "symboldatabase.h"
// Register this check class (by creating a static instance of it) // Register this check class (by creating a static instance of it)
namespace { namespace {
@ -25,7 +26,11 @@ namespace {
void CheckBoost::checkBoostForeachModification() void CheckBoost::checkBoostForeachModification()
{ {
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) { const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase();
const std::size_t functions = symbolDatabase->functionScopes.size();
for (std::size_t i = 0; i < functions; ++i) {
const Scope * scope = symbolDatabase->functionScopes[i];
for (const Token *tok = scope->classStart->next(); tok && tok != scope->classEnd; tok = tok->next()) {
if (!Token::simpleMatch(tok, "BOOST_FOREACH (")) if (!Token::simpleMatch(tok, "BOOST_FOREACH ("))
continue; continue;
@ -47,6 +52,7 @@ void CheckBoost::checkBoostForeachModification()
} }
} }
} }
}
void CheckBoost::boostForeachError(const Token *tok) void CheckBoost::boostForeachError(const Token *tok)
{ {

View File

@ -1326,11 +1326,10 @@ void CheckBufferOverrun::checkGlobalAndLocalVariable()
} }
} }
// find all dynamically allocated arrays next by parsing the token stream // find all dynamically allocated arrays next
// Count { and } when parsing all tokens const std::size_t functions = symbolDatabase->functionScopes.size();
for (std::list<Scope>::const_iterator scope = symbolDatabase->scopeList.begin(); scope != symbolDatabase->scopeList.end(); ++scope) { for (std::size_t i = 0; i < functions; ++i) {
if (scope->type != Scope::eFunction) const Scope * scope = symbolDatabase->functionScopes[i];
continue;
for (const Token *tok = scope->classStart; tok != scope->classEnd; tok = tok->next()) { for (const Token *tok = scope->classStart; tok != scope->classEnd; tok = tok->next()) {
// if the previous token exists, it must be either a variable name or "[;{}]" // if the previous token exists, it must be either a variable name or "[;{}]"
@ -1423,13 +1422,10 @@ void CheckBufferOverrun::checkStructVariable()
{ {
const SymbolDatabase * symbolDatabase = _tokenizer->getSymbolDatabase(); const SymbolDatabase * symbolDatabase = _tokenizer->getSymbolDatabase();
std::list<Scope>::const_iterator scope;
// find every class and struct // find every class and struct
for (scope = symbolDatabase->scopeList.begin(); scope != symbolDatabase->scopeList.end(); ++scope) { const std::size_t classes = symbolDatabase->classAndStructScopes.size();
// only check classes and structures for (std::size_t i = 0; i < classes; ++i) {
if (!scope->isClassOrStruct()) const Scope * scope = symbolDatabase->classAndStructScopes[i];
continue;
// check all variables to see if they are arrays // check all variables to see if they are arrays
std::list<Variable>::const_iterator var; std::list<Variable>::const_iterator var;
@ -1439,13 +1435,10 @@ void CheckBufferOverrun::checkStructVariable()
// create ArrayInfo from the array variable // create ArrayInfo from the array variable
ArrayInfo arrayInfo(&*var, _tokenizer); ArrayInfo arrayInfo(&*var, _tokenizer);
std::list<Scope>::const_iterator func_scope;
// find every function // find every function
for (func_scope = symbolDatabase->scopeList.begin(); func_scope != symbolDatabase->scopeList.end(); ++func_scope) { const std::size_t functions = symbolDatabase->functionScopes.size();
// only check functions for (std::size_t j = 0; j < functions; ++j) {
if (func_scope->type != Scope::eFunction) const Scope * func_scope = symbolDatabase->functionScopes[j];
continue;
// If struct is declared in a function then check // If struct is declared in a function then check
// if scope_func matches // if scope_func matches
@ -1570,8 +1563,8 @@ void CheckBufferOverrun::checkStructVariable()
ArrayInfo temp = arrayInfo; ArrayInfo temp = arrayInfo;
temp.varid(0); // do variable lookup by variable and member names rather than varid temp.varid(0); // do variable lookup by variable and member names rather than varid
std::string varnames; // use class and member name for messages std::string varnames; // use class and member name for messages
for (unsigned int i = 0; i < varname.size(); ++i) for (unsigned int k = 0; k < varname.size(); ++k)
varnames += (i == 0 ? "" : ".") + varname[i]; varnames += (k == 0 ? "" : ".") + varname[k];
temp.varname(varnames); temp.varname(varnames);
checkScope(CheckTok, varname, temp); checkScope(CheckTok, varname, temp);
} }
@ -1734,7 +1727,12 @@ void CheckBufferOverrun::checkSprintfCall(const Token *tok, const MathLib::bigin
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
void CheckBufferOverrun::checkBufferAllocatedWithStrlen() void CheckBufferOverrun::checkBufferAllocatedWithStrlen()
{ {
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) { const SymbolDatabase* const symbolDatabase = _tokenizer->getSymbolDatabase();
const std::size_t functions = symbolDatabase->functionScopes.size();
for (std::size_t i = 0; i < functions; ++i) {
const Scope * scope = symbolDatabase->functionScopes[i];
for (const Token *tok = scope->classStart->next(); tok && tok != scope->classEnd; tok = tok->next()) {
unsigned int dstVarId; unsigned int dstVarId;
unsigned int srcVarId; unsigned int srcVarId;
@ -1774,6 +1772,7 @@ void CheckBufferOverrun::checkBufferAllocatedWithStrlen()
return; return;
} }
} }
}
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
// Checking for buffer overflow caused by copying command line arguments // Checking for buffer overflow caused by copying command line arguments
@ -1790,8 +1789,11 @@ void CheckBufferOverrun::checkInsecureCmdLineArgs()
{ {
const SymbolDatabase* const symbolDatabase = _tokenizer->getSymbolDatabase(); const SymbolDatabase* const symbolDatabase = _tokenizer->getSymbolDatabase();
for (std::list<Scope>::const_iterator i = symbolDatabase->scopeList.begin(); i != symbolDatabase->scopeList.end(); ++i) { std::size_t functions = symbolDatabase->functionScopes.size();
for (std::list<Function>::const_iterator j = i->functionList.begin(); j != i->functionList.end(); ++j) { for (std::size_t i = 0; i < functions; ++i) {
const Scope * scope = symbolDatabase->functionScopes[i];
Function * j = scope->function;
if (j) {
const Token* tok = j->token; const Token* tok = j->token;
// Get the name of the argv variable // Get the name of the argv variable
@ -1829,7 +1831,6 @@ void CheckBufferOverrun::checkInsecureCmdLineArgs()
tok->strAt(4).find("%s") != std::string::npos) { tok->strAt(4).find("%s") != std::string::npos) {
cmdLineArgsError(tok); cmdLineArgsError(tok);
} }
} }
} }
} }
@ -2087,7 +2088,12 @@ void CheckBufferOverrun::arrayIndexThenCheck()
{ {
if (!_settings->isEnabled("style")) if (!_settings->isEnabled("style"))
return; return;
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) {
const SymbolDatabase* symbolDatabase = _tokenizer->getSymbolDatabase();
const std::size_t functions = symbolDatabase->functionScopes.size();
for (std::size_t i = 0; i < functions; ++i) {
const Scope * scope = symbolDatabase->functionScopes[i];
for (const Token *tok = scope->classStart; tok && tok != scope->classEnd; tok = tok->next()) {
if (Token::Match(tok, "%var% [ %var% ]")) { if (Token::Match(tok, "%var% [ %var% ]")) {
const std::string& indexName(tok->strAt(2)); const std::string& indexName(tok->strAt(2));
@ -2110,6 +2116,7 @@ void CheckBufferOverrun::arrayIndexThenCheck()
} }
} }
} }
}
void CheckBufferOverrun::arrayIndexThenCheckError(const Token *tok, const std::string &indexName) void CheckBufferOverrun::arrayIndexThenCheckError(const Token *tok, const std::string &indexName)
{ {