Optimize checkother.cpp by taking symbolDatabase into use.

This commit is contained in:
PKEuS 2011-12-04 20:49:01 +02:00 committed by Reijo Tomperi
parent 9cadd7c17a
commit 501d7ccb2d
1 changed files with 23 additions and 41 deletions

View File

@ -226,7 +226,7 @@ void CheckOther::checkBitwiseOnBoolean()
return; return;
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) { for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) {
if (Token::Match(tok, "(|.|return %var% [&|]")) { if (Token::Match(tok, "(|.|return|&&|%oror% %var% [&|]")) {
if (tok->next()->varId()) { if (tok->next()->varId()) {
const Variable *var = _tokenizer->getSymbolDatabase()->getVariableFromVarId(tok->next()->varId()); const Variable *var = _tokenizer->getSymbolDatabase()->getVariableFromVarId(tok->next()->varId());
if (var && (var->typeStartToken() == var->typeEndToken()) && if (var && (var->typeStartToken() == var->typeEndToken()) &&
@ -288,7 +288,7 @@ void CheckOther::warningOldStylePointerCast()
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) { for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) {
// Old style pointer casting.. // Old style pointer casting..
if (!Token::Match(tok, "( const| %type% * ) %var%") && if (!Token::Match(tok, "( const| %type% * ) (| %var%") &&
!Token::Match(tok, "( const| %type% * ) (| new")) !Token::Match(tok, "( const| %type% * ) (| new"))
continue; continue;
@ -1173,7 +1173,7 @@ void CheckOther::invalidScanf()
const Token *formatToken = 0; const Token *formatToken = 0;
if (Token::Match(tok, "scanf|vscanf ( %str% ,")) if (Token::Match(tok, "scanf|vscanf ( %str% ,"))
formatToken = tok->tokAt(2); formatToken = tok->tokAt(2);
else if (Token::Match(tok, "fscanf|vfscanf (")) { else if (Token::Match(tok, "sscanf|vsscanf|fscanf|vfscanf (")) {
const Token* nextArg = tok->tokAt(2)->nextArgument(); const Token* nextArg = tok->tokAt(2)->nextArgument();
if (nextArg && Token::Match(nextArg, "%str%")) if (nextArg && Token::Match(nextArg, "%str%"))
formatToken = nextArg; formatToken = nextArg;
@ -1768,44 +1768,26 @@ void CheckOther::checkConstantFunctionParameter()
const SymbolDatabase * const symbolDatabase = _tokenizer->getSymbolDatabase(); const SymbolDatabase * const symbolDatabase = _tokenizer->getSymbolDatabase();
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) { for (std::list<Scope>::const_iterator i = symbolDatabase->scopeList.begin(); i != symbolDatabase->scopeList.end(); ++i) {
// TODO: False negatives. This pattern only checks for string. for (std::list<Function>::const_iterator j = i->functionList.begin(); j != i->functionList.end(); ++j) {
// Investigate if there are other classes in the std for (const Token* tok = j->arg->next(); tok; tok = tok->nextArgument()) {
// namespace and add them to the pattern. There are // TODO: False negatives. This pattern only checks for string.
// streams for example (however it seems strange with // Investigate if there are other classes in the std
// const stream parameter). // namespace and add them to the pattern. There are
if (Token::Match(tok, "[,(] const std :: string %var% [,)]")) { // streams for example (however it seems strange with
passedByValueError(tok, tok->strAt(5)); // const stream parameter).
} if (Token::Match(tok, "const std :: string %var% [,)]")) {
passedByValueError(tok, tok->strAt(4));
else if (Token::Match(tok, "[,(] const std :: %type% < %type% > %var% [,)]")) { } else if (Token::Match(tok, "const std :: %type% < std| ::| %type% > %var% [,)]")) {
passedByValueError(tok, tok->strAt(8)); passedByValueError(tok, Token::findsimplematch(tok->tokAt(5), ">")->strAt(1));
} } else if (Token::Match(tok, "const std :: %type% < std| ::| %type% , std| ::| %type% > %var% [,)]")) {
passedByValueError(tok, Token::findsimplematch(tok->tokAt(7), ">")->strAt(1));
else if (Token::Match(tok, "[,(] const std :: %type% < std :: %type% > %var% [,)]")) { } else if (Token::Match(tok, "const %type% %var% [,)]")) {
passedByValueError(tok, tok->strAt(10)); // Check if type is a struct or class.
} if (symbolDatabase->isClassOrStruct(tok->strAt(1))) {
passedByValueError(tok, tok->strAt(2));
else if (Token::Match(tok, "[,(] const std :: %type% < std :: %type% , std :: %type% > %var% [,)]")) { }
passedByValueError(tok, tok->strAt(14)); }
}
else if (Token::Match(tok, "[,(] const std :: %type% < %type% , std :: %type% > %var% [,)]")) {
passedByValueError(tok, tok->strAt(12));
}
else if (Token::Match(tok, "[,(] const std :: %type% < std :: %type% , %type% > %var% [,)]")) {
passedByValueError(tok, tok->strAt(12));
}
else if (Token::Match(tok, "[,(] const std :: %type% < %type% , %type% > %var% [,)]")) {
passedByValueError(tok, tok->strAt(10));
}
else if (Token::Match(tok, "[,(] const %type% %var% [,)]")) {
// Check if type is a struct or class.
if (symbolDatabase->isClassOrStruct(tok->strAt(2))) {
passedByValueError(tok, tok->strAt(3));
} }
} }
} }