From bb703f4d0ba91bd3d911115b8b298876347fc1cd Mon Sep 17 00:00:00 2001 From: Edoardo Prezioso Date: Sat, 28 Jan 2012 20:31:12 +0100 Subject: [PATCH 1/3] Fixed: (warning) Found simple pattern inside Token::Match() call. --- lib/checknullpointer.cpp | 2 +- lib/checkother.cpp | 6 +++--- lib/tokenize.cpp | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/checknullpointer.cpp b/lib/checknullpointer.cpp index 970f2e4d7..5a7024e63 100644 --- a/lib/checknullpointer.cpp +++ b/lib/checknullpointer.cpp @@ -1036,7 +1036,7 @@ void CheckNullPointer::nullConstantDereference() nullPointerError(*it); } } - } else if (Token::Match(tok, "std :: string ( 0 )")) + } else if (Token::simpleMatch(tok, "std :: string ( 0 )")) nullPointerError(tok); else if (Token::Match(tok, "%var% ( 0 )")) { const Variable* var = symbolDatabase->getVariableFromVarId(tok->varId()); diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 7276258a2..cb3c94596 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -1706,7 +1706,7 @@ void CheckOther::checkUnsignedDivision() if ((isUnsigned(var1) && isSigned(var2)) || (isUnsigned(var2) && isSigned(var1))) { udivError(tok->next(), true); } - } else if (!ifTok && Token::Match(tok, "if (")) + } else if (!ifTok && Token::simpleMatch(tok, "if (")) ifTok = tok->next()->link()->next()->link(); else if (ifTok == tok) ifTok = 0; @@ -2534,12 +2534,12 @@ void CheckOther::checkDoubleFree() else if (Token::Match(tok, "%var% (") && !Token::Match(tok, "printf|sprintf|snprintf|fprintf|if|while")) { // If this is a new function definition, clear all variables - if (Token::Match(tok->next()->link(), ") {")) { + if (Token::simpleMatch(tok->next()->link(), ") {")) { freedVariables.clear(); closeDirVariables.clear(); } // If it is a function call, then clear those variables in its argument list - else if (Token::Match(tok->next()->link(), ") ;")) { + else if (Token::simpleMatch(tok->next()->link(), ") ;")) { for (const Token* tok2 = tok->tokAt(2); tok2 != tok->linkAt(1); tok2 = tok2->next()) { if (Token::Match(tok2, "%var%")) { unsigned int var = tok2->varId(); diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index ce21e242d..6ef8334de 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -3854,9 +3854,9 @@ void Tokenizer::simplifyRealloc() tok = tok->link(); else if (Token::Match(tok, "[;{}] %var% = realloc (")) { tok = tok->tokAt(3); - if (Token::Match(tok->next(), "( 0 ,")) { + if (Token::simpleMatch(tok->next(), "( 0 ,")) { //no "x = realloc(0,);" - if (!Token::Match(tok->next()->link(), ") ;") || tok->next()->link()->previous() == tok->tokAt(3)) + if (!Token::simpleMatch(tok->next()->link(), ") ;") || tok->next()->link()->previous() == tok->tokAt(3)) continue; // delete "0 ," From fa77f98b1c4dc170430ee51f72f34d66aaf60927 Mon Sep 17 00:00:00 2001 From: Edoardo Prezioso Date: Sat, 28 Jan 2012 21:35:16 +0100 Subject: [PATCH 2/3] Tokenizer: it's possible to set null lower bound for eraseDeadCode when used inside simplifyFlowControl. Remove now unused 'beginindent' and moved 'goto' simplification together with 'return' group. --- lib/tokenize.cpp | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 6ef8334de..b62835725 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -3894,7 +3894,6 @@ void Tokenizer::simplifyRealloc() void Tokenizer::simplifyFlowControl() { unsigned int indentlevel = 0; - Token *beginindent = 0; for (Token *tok = _tokens; tok; tok = tok->next()) { if (tok->str() == "(" || tok->str() == "[") { tok = tok->link(); @@ -3906,7 +3905,6 @@ void Tokenizer::simplifyFlowControl() tok = tok->link(); continue; } - beginindent = tok; ++indentlevel; } else if (tok->str() == "}") { if (!indentlevel) @@ -3917,22 +3915,18 @@ void Tokenizer::simplifyFlowControl() if (!indentlevel) continue; - if (Token::Match(tok, "goto %var% ;")) { - tok = tok->tokAt(2); - eraseDeadCode(tok, beginindent->link()); - - } else if (Token::Match(tok,"continue|break ;")) { + if (Token::Match(tok,"continue|break ;")) { tok = tok->next(); - eraseDeadCode(tok, beginindent->link()); + eraseDeadCode(tok, 0); - } else if (Token::Match(tok,"return|throw|exit|abort")) { + } else if (Token::Match(tok,"return|throw|exit|abort|goto")) { //catch the first ';' for (Token *tok2 = tok->next(); tok2; tok2 = tok2->next()) { if (tok2->str() == "(" || tok2->str() == "[") { tok2 = tok2->link(); } else if (tok2->str() == ";") { tok = tok2; - eraseDeadCode(tok, beginindent->link()); + eraseDeadCode(tok, 0); break; } else if (Token::Match(tok2, "[{}]")) break; //Wrong code. From 697af4f7beac8488953472dacb81a363a0788a08 Mon Sep 17 00:00:00 2001 From: Edoardo Prezioso Date: Sat, 28 Jan 2012 23:23:02 +0100 Subject: [PATCH 3/3] Improve 'CheckMemoryLeakInClass::check'. --- lib/checkmemoryleak.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/checkmemoryleak.cpp b/lib/checkmemoryleak.cpp index 329c92b5b..b78e3ba5c 100644 --- a/lib/checkmemoryleak.cpp +++ b/lib/checkmemoryleak.cpp @@ -2401,9 +2401,12 @@ void CheckMemoryLeakInClass::check() if (scope->isClassOrStruct()) { std::list::const_iterator var; for (var = scope->varlist.begin(); var != scope->varlist.end(); ++var) { - if (!var->isStatic() && var->nameToken()->previous()->str() == "*") { + if (!var->isStatic() && var->isPointer()) { // allocation but no deallocation of private variables in public function.. - if (var->nameToken()->tokAt(-2)->isStandardType()) { + const Token *tok = var->typeStartToken(); + if (tok->str() == "const") + tok = tok->next(); + if (tok && tok->isStandardType()) { if (var->isPrivate()) checkPublicFunctions(&(*scope), var->nameToken());