From b2835051df0c9ef4aff067bfd33251f0f625b195 Mon Sep 17 00:00:00 2001 From: PKEuS Date: Sat, 31 Jan 2015 10:50:39 +0100 Subject: [PATCH] Refactorization: Renamed Token::Match pattern %var% to %name%, implement new pattern %var% which is true if varId > 0. --- lib/check64bit.cpp | 4 +- lib/checkautovariables.cpp | 6 +- lib/checkbool.cpp | 4 +- lib/checkboost.cpp | 6 +- lib/checkbufferoverrun.cpp | 57 +++--- lib/checkbufferoverrun.h | 2 +- lib/checkclass.cpp | 80 ++++---- lib/checkcondition.cpp | 18 +- lib/checkinternal.cpp | 7 +- lib/checkio.cpp | 26 +-- lib/checkleakautovar.cpp | 19 +- lib/checkmemoryleak.cpp | 90 ++++----- lib/checknullpointer.cpp | 31 ++- lib/checkother.cpp | 70 +++---- lib/checksizeof.cpp | 16 +- lib/checkstl.cpp | 124 ++++++------ lib/checkstring.cpp | 10 +- lib/checkuninitvar.cpp | 154 +++++++-------- lib/checkunusedfunctions.cpp | 10 +- lib/checkunusedvar.cpp | 46 ++--- lib/executionpath.cpp | 8 +- lib/library.cpp | 6 +- lib/preprocessor.cpp | 36 ++-- lib/symboldatabase.cpp | 95 +++++---- lib/templatesimplifier.cpp | 48 ++--- lib/token.cpp | 22 ++- lib/token.h | 3 +- lib/tokenize.cpp | 362 +++++++++++++++++------------------ lib/tokenize.h | 6 +- lib/tokenlist.cpp | 22 +-- lib/valueflow.cpp | 63 +++--- test/testinternal.cpp | 8 +- test/testsimplifytokens.cpp | 2 +- test/testsymboldatabase.cpp | 2 +- test/testtoken.cpp | 78 ++++---- test/testtokenize.cpp | 2 +- tools/matchcompiler.py | 4 +- 37 files changed, 759 insertions(+), 788 deletions(-) diff --git a/lib/check64bit.cpp b/lib/check64bit.cpp index f418a8e21..abfb17321 100644 --- a/lib/check64bit.cpp +++ b/lib/check64bit.cpp @@ -65,7 +65,7 @@ void Check64BitPortability::pointerassignment() continue; for (const Token* tok = scope->classStart->next(); tok != scope->classEnd; tok = tok->next()) { - if (Token::Match(tok, "return %var%|%num% [;+]") && !Token::simpleMatch(tok, "return 0 ;")) { + if (Token::Match(tok, "return %name%|%num% [;+]") && !Token::simpleMatch(tok, "return 0 ;")) { enum { NO, INT, PTR, PTRDIFF } type = NO; for (const Token *tok2 = tok->next(); tok2; tok2 = tok2->next()) { if ((type == NO || type == INT) && Token::Match(tok2, "%var% [+;]") && isaddr(tok2->variable())) @@ -96,7 +96,7 @@ void Check64BitPortability::pointerassignment() 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% = %name%")) { const Token* tok2 = tok->tokAt(3); while (Token::Match(tok2->next(), ".|::")) tok2 = tok2->tokAt(2); diff --git a/lib/checkautovariables.cpp b/lib/checkautovariables.cpp index 9c38401e8..cd5836e80 100644 --- a/lib/checkautovariables.cpp +++ b/lib/checkautovariables.cpp @@ -86,12 +86,12 @@ static bool checkRvalueExpression(const Token * const vartok) if (var == nullptr) return false; - if (Token::Match(vartok->previous(), "& %var% [") && var->isPointer()) + if (Token::Match(vartok->previous(), "& %name% [") && var->isPointer()) return false; const Token * const next = vartok->next(); // &a.b[0] - if (Token::Match(vartok, "%var% . %var% [") && !var->isPointer()) { + if (Token::Match(vartok, "%name% . %var% [") && !var->isPointer()) { const Variable *var2 = next->next()->variable(); return var2 && !var2->isPointer(); } @@ -421,7 +421,7 @@ void CheckAutoVariables::returnReference() } // return reference to temporary.. - else if (Token::Match(tok2, "return %var% (") && + else if (Token::Match(tok2, "return %name% (") && Token::simpleMatch(tok2->linkAt(2), ") ;")) { if (returnTemporary(tok2->next())) { // report error.. diff --git a/lib/checkbool.cpp b/lib/checkbool.cpp index 77bf66299..4480eb5ff 100644 --- a/lib/checkbool.cpp +++ b/lib/checkbool.cpp @@ -46,7 +46,7 @@ void CheckBool::checkIncrementBoolean() for (std::size_t i = 0; i < functions; ++i) { const Scope * scope = symbolDatabase->functionScopes[i]; for (const Token* tok = scope->classStart->next(); tok != scope->classEnd; tok = tok->next()) { - if (tok->variable() && Token::Match(tok, "%var% ++")) { + if (Token::Match(tok, "%var% ++")) { const Variable *var = tok->variable(); if (var && var->typeEndToken()->str() == "bool") incrementBooleanError(tok); @@ -202,7 +202,7 @@ void CheckBool::comparisonOfBoolWithInvalidComparator(const Token *tok, const st static bool tokenIsFunctionReturningBool(const Token* tok) { const Function* func = tok->function(); - if (func && Token::Match(tok, "%var% (")) { + if (func && Token::Match(tok, "%name% (")) { if (func->tokenDef && func->tokenDef->strAt(-1) == "bool") { return true; } diff --git a/lib/checkboost.cpp b/lib/checkboost.cpp index af242374e..28a828fa3 100644 --- a/lib/checkboost.cpp +++ b/lib/checkboost.cpp @@ -38,14 +38,10 @@ void CheckBoost::checkBoostForeachModification() if (!Token::Match(containerTok, "%var% ) {")) continue; - const unsigned int containerId = containerTok->varId(); - if (containerId == 0) - continue; - const Token *tok2 = containerTok->tokAt(2); const Token *end = tok2->link(); for (; tok2 != end; tok2 = tok2->next()) { - if (Token::Match(tok2, "%varid% . insert|erase|push_back|push_front|pop_front|pop_back|clear|swap|resize|assign|merge|remove|remove_if|reverse|sort|splice|unique|pop|push", containerId)) { + if (Token::Match(tok2, "%varid% . insert|erase|push_back|push_front|pop_front|pop_back|clear|swap|resize|assign|merge|remove|remove_if|reverse|sort|splice|unique|pop|push", containerTok->varId())) { const Token* nextStatement = Token::findsimplematch(tok2->linkAt(3), ";", end); if (!Token::Match(nextStatement, "; break|return|throw")) boostForeachError(tok2); diff --git a/lib/checkbufferoverrun.cpp b/lib/checkbufferoverrun.cpp index 823c8a4a0..dae17d67f 100644 --- a/lib/checkbufferoverrun.cpp +++ b/lib/checkbufferoverrun.cpp @@ -405,11 +405,11 @@ void CheckBufferOverrun::checkFunctionParameter(const Token &ftok, unsigned int break; if (ftok2->varId() == parameter->declarationId()) { - if (Token::Match(ftok2->previous(), "-- %var%") || - Token::Match(ftok2, "%var% --")) + if (Token::Match(ftok2->previous(), "-- %name%") || + Token::Match(ftok2, "%name% --")) break; - if (Token::Match(ftok2->previous(), ";|{|}|%op% %var% [ %num% ]")) { + if (Token::Match(ftok2->previous(), ";|{|}|%op% %name% [ %num% ]")) { const MathLib::bigint index = MathLib::toLongNumber(ftok2->strAt(2)); if (index >= 0 && arrayInfo.num(0) > 0 && index >= arrayInfo.num(0)) { std::list callstack2(callstack); @@ -424,7 +424,7 @@ void CheckBufferOverrun::checkFunctionParameter(const Token &ftok, unsigned int } // Calling function.. - if (Token::Match(ftok2, "%var% (")) { + if (Token::Match(ftok2, "%name% (")) { ArrayInfo ai(arrayInfo); ai.declarationId(parameter->declarationId()); checkFunctionCall(ftok2, ai, callstack); @@ -603,7 +603,7 @@ void CheckBufferOverrun::checkScope(const Token *tok, const std::vectorprevious(); - while (tok3 && Token::Match(tok3->previous(), "%var% .")) + while (tok3 && Token::Match(tok3->previous(), "%name% .")) tok3 = tok3->tokAt(-2); // taking address of 1 past end? @@ -646,9 +646,9 @@ void CheckBufferOverrun::checkScope(const Token *tok, const std::vector 0) { std::list callstack; callstack.push_back(tok); - if (Token::Match(tok, ("%var% ( " + varnames + " ,").c_str())) + if (Token::Match(tok, ("%name% ( " + varnames + " ,").c_str())) checkFunctionParameter(*tok, 1, arrayInfo, callstack); - if (Token::Match(tok, ("%var% ( %var% , " + varnames + " ,").c_str())) + if (Token::Match(tok, ("%name% ( %name% , " + varnames + " ,").c_str())) checkFunctionParameter(*tok, 2, arrayInfo, callstack); } @@ -705,7 +705,7 @@ void CheckBufferOverrun::checkScope(const Token *tok, const std::vector function calls are not handled if (declarationId == 0) continue; @@ -739,7 +739,7 @@ void CheckBufferOverrun::valueFlowCheckArrayIndex(const Token * const tok, const /* { const Token *parent = tok->astParent(); - while (Token::Match(parent, "%var%|::|*|&")) + while (Token::Match(parent, "%name%|::|*|&")) parent = parent->astParent(); if (parent && !Token::simpleMatch(parent, "=")) return; @@ -749,7 +749,7 @@ void CheckBufferOverrun::valueFlowCheckArrayIndex(const Token * const tok, const bool addressOf = false; { const Token *tok2 = tok->astParent(); - while (Token::Match(tok2, "%var%|.|::|[")) + while (Token::Match(tok2, "%name%|.|::|[")) tok2 = tok2->astParent(); addressOf = tok2 && tok2->str() == "&" && !(tok2->astOperand1() && tok2->astOperand2()); } @@ -903,7 +903,7 @@ void CheckBufferOverrun::checkScope(const Token *tok, const ArrayInfo &arrayInfo else if (!tok->scope()->isExecutable()) // No executable code outside of executable scope - continue to increase performance continue; - else if (Token::Match(tok, "%var% (")) { + else if (Token::Match(tok, "%name% (")) { // Check function call.. checkFunctionCall(tok, arrayInfo, std::list()); @@ -1003,7 +1003,7 @@ void CheckBufferOverrun::checkScope(const Token *tok, const ArrayInfo &arrayInfo //--------------------------------------------------------------------------- bool CheckBufferOverrun::isArrayOfStruct(const Token* tok, int &position) { - if (Token::Match(tok->next(), "%var% [ %num% ]")) { + if (Token::Match(tok->next(), "%name% [ %num% ]")) { tok = tok->tokAt(4); int i = 1; for (;;) { @@ -1284,8 +1284,8 @@ void CheckBufferOverrun::checkStructVariable() // dynamically allocated so could be variable sized structure if (tok3->next()->str() == "*") { // check for allocation - if ((Token::Match(tok3->tokAt(3), "; %var% = malloc ( %num% ) ;") || - (Token::Match(tok3->tokAt(3), "; %var% = (") && + if ((Token::Match(tok3->tokAt(3), "; %name% = malloc ( %num% ) ;") || + (Token::Match(tok3->tokAt(3), "; %name% = (") && Token::Match(tok3->linkAt(6), ") malloc ( %num% ) ;"))) && (tok3->strAt(4) == tok3->strAt(2))) { MathLib::bigint size; @@ -1384,7 +1384,7 @@ void CheckBufferOverrun::bufferOverrun2() // singlepass checking using ast, symboldatabase and valueflow for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) { // Array index - if (!Token::Match(tok, "%var% [")) + if (!Token::Match(tok, "%name% [")) continue; // TODO: what to do about negative index.. @@ -1578,15 +1578,15 @@ void CheckBufferOverrun::checkBufferAllocatedWithStrlen() unsigned int srcVarId; // Look for allocation of a buffer based on the size of a string - if (Token::Match(tok, "%var% = malloc|g_malloc|g_try_malloc ( strlen ( %var% ) )")) { + if (Token::Match(tok, "%var% = malloc|g_malloc|g_try_malloc ( strlen ( %name% ) )")) { dstVarId = tok->varId(); srcVarId = tok->tokAt(6)->varId(); tok = tok->tokAt(8); - } else if (Token::Match(tok, "%var% = new char [ strlen ( %var% ) ]")) { + } else if (Token::Match(tok, "%var% = new char [ strlen ( %name% ) ]")) { dstVarId = tok->varId(); srcVarId = tok->tokAt(7)->varId(); tok = tok->tokAt(9); - } else if (Token::Match(tok, "%var% = realloc|g_realloc|g_try_realloc ( %var% , strlen ( %var% ) )")) { + } else if (Token::Match(tok, "%var% = realloc|g_realloc|g_try_realloc ( %name% , strlen ( %name% ) )")) { dstVarId = tok->varId(); srcVarId = tok->tokAt(8)->varId(); tok = tok->tokAt(10); @@ -1625,12 +1625,12 @@ void CheckBufferOverrun::checkStringArgument() for (std::size_t functionIndex = 0; functionIndex < functions; ++functionIndex) { const Scope * const scope = symbolDatabase->functionScopes[functionIndex]; for (const Token *tok = scope->classStart; tok != scope->classEnd; tok = tok->next()) { - if (!Token::Match(tok, "%var% (") || !_settings->library.hasminsize(tok->str())) + if (!Token::Match(tok, "%name% (") || !_settings->library.hasminsize(tok->str())) continue; unsigned int argnr = 1; for (const Token *argtok = tok->tokAt(2); argtok; argtok = argtok->nextArgument(), argnr++) { - if (!Token::Match(argtok, "%var%|%str% ,|)")) + if (!Token::Match(argtok, "%name%|%str% ,|)")) continue; const Token *strtoken = argtok->getValueTokenMinStrSize(); if (!strtoken) @@ -1673,8 +1673,7 @@ void CheckBufferOverrun::checkInsecureCmdLineArgs() } else if (Token::Match(tok, "main ( int %var% , char * * %var% ,|)")) { varid = tok->tokAt(8)->varId(); - } - if (varid == 0) + } else continue; // Jump to the opening curly brace @@ -1688,15 +1687,15 @@ void CheckBufferOverrun::checkInsecureCmdLineArgs() // Match common patterns that can result in a buffer overrun // e.g. strcpy(buffer, argv[0]) - if (Token::Match(tok, "strcpy|strcat ( %var% , * %varid%", varid) || - Token::Match(tok, "strcpy|strcat ( %var% , %varid% [", varid)) { + if (Token::Match(tok, "strcpy|strcat ( %name% , * %varid%", varid) || + Token::Match(tok, "strcpy|strcat ( %name% , %varid% [", varid)) { cmdLineArgsError(tok); tok = tok->linkAt(1); - } else if (Token::Match(tok, "sprintf ( %var% , %str% , %varid% [", varid) && + } else if (Token::Match(tok, "sprintf ( %name% , %str% , %varid% [", varid) && tok->strAt(4).find("%s") != std::string::npos) { cmdLineArgsError(tok); tok = tok->linkAt(1); - } else if (Token::Match(tok, "sprintf ( %var% , %str% , * %varid%", varid) && + } else if (Token::Match(tok, "sprintf ( %name% , %str% , * %varid%", varid) && tok->strAt(4).find("%s") != std::string::npos) { cmdLineArgsError(tok); tok = tok->linkAt(1); @@ -1783,12 +1782,10 @@ void CheckBufferOverrun::arrayIndexThenCheck() for (std::size_t i = 0; i < functions; ++i) { const Scope * const 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, "%name% [ %var% ]")) { tok = tok->tokAt(2); - unsigned int indexID = tok->varId(); - if (!indexID) - continue; + unsigned int indexID = tok->varId(); const std::string& indexName(tok->str()); // skip array index.. diff --git a/lib/checkbufferoverrun.h b/lib/checkbufferoverrun.h index 4038a3dfa..9f1fb06d1 100644 --- a/lib/checkbufferoverrun.h +++ b/lib/checkbufferoverrun.h @@ -194,7 +194,7 @@ public: /** * Helper function that checks if the array is used and if so calls the checkFunctionCall - * @param tok token that matches "%var% (" + * @param tok token that matches "%name% (" * @param arrayInfo the array information * \param callstack call stack. This is used to prevent recursion and to provide better error messages. Pass a empty list from checkScope etc. */ diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index 5fc7834a2..d0e82d198 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -256,9 +256,9 @@ void CheckClass::copyconstructors() const Token* tok = func->tokenDef->linkAt(1)->next(); if (tok->str()==":") { tok=tok->next(); - while (Token::Match(tok, "%var% (")) { + while (Token::Match(tok, "%name% (")) { if (allocatedVars.find(tok->varId()) != allocatedVars.end()) { - if (tok->varId() && Token::Match(tok->tokAt(2), "%var% . %var% )")) + if (tok->varId() && Token::Match(tok->tokAt(2), "%name% . %name% )")) copiedVars.insert(tok); else if (!Token::Match(tok->tokAt(2), "%any% )")) allocatedVars.erase(tok->varId()); // Assume memory is allocated @@ -269,7 +269,7 @@ void CheckClass::copyconstructors() for (tok=func->functionScope->classStart; tok!=func->functionScope->classEnd; tok=tok->next()) { if (Token::Match(tok, "%var% = new|malloc|g_malloc|g_try_malloc|realloc|g_realloc|g_try_realloc")) { allocatedVars.erase(tok->varId()); - } else if (Token::Match(tok, "%var% = %var% . %var% ;") && allocatedVars.find(tok->varId()) != allocatedVars.end()) { + } else if (Token::Match(tok, "%var% = %name% . %name% ;") && allocatedVars.find(tok->varId()) != allocatedVars.end()) { copiedVars.insert(tok); } } @@ -440,7 +440,7 @@ void CheckClass::initializeVarList(const Function &func, std::liststr() != func.name()) { initVar(ftok->str(), scope, usage); } else { // c++11 delegate constructor @@ -471,7 +471,7 @@ void CheckClass::initializeVarList(const Function &func, std::listnext(); level++; - } else if (level != 0 && Token::Match(ftok, "%var% =")) // assignment in the initializer: var(value = x) + } else if (level != 0 && Token::Match(ftok, "%name% =")) // assignment in the initializer: var(value = x) assignVar(ftok->str(), scope, usage); else if (ftok->str() == "(") @@ -490,7 +490,7 @@ void CheckClass::initializeVarList(const Function &func, std::list> %var%")) { + if (Token::Match(ftok, ">> %name%")) { assignVar(ftok->strAt(1), scope, usage); } @@ -508,13 +508,13 @@ void CheckClass::initializeVarList(const Function &func, std::listlinkAt(2), ") . %var% ( *| this ) ;")) { + if (func.type == Function::eOperatorEqual && Token::Match(ftok, "[;{}] %name% (") && Token::Match(ftok->linkAt(2), ") . %name% ( *| this ) ;")) { assignAllVar(usage); break; } // Calling member variable function? - if (Token::Match(ftok->next(), "%var% . %var% (")) { + if (Token::Match(ftok->next(), "%var% . %name% (")) { std::list::const_iterator var; for (var = scope->varlist.begin(); var != scope->varlist.end(); ++var) { if (var->declarationId() == ftok->next()->varId()) { @@ -527,10 +527,10 @@ void CheckClass::initializeVarList(const Function &func, std::listtokAt(2); } - if (!Token::Match(ftok->next(), "::| %var%") && - !Token::Match(ftok->next(), "*| this . %var%") && - !Token::Match(ftok->next(), "* %var% =") && - !Token::Match(ftok->next(), "( * this ) . %var%")) + if (!Token::Match(ftok->next(), "::| %name%") && + !Token::Match(ftok->next(), "*| this . %name%") && + !Token::Match(ftok->next(), "* %name% =") && + !Token::Match(ftok->next(), "( * this ) . %name%")) continue; // Goto the first token in this statement.. @@ -550,9 +550,9 @@ void CheckClass::initializeVarList(const Function &func, std::listtokAt(2); // Skip "classname :: " - if (Token::Match(ftok, ":: %var%")) + if (Token::Match(ftok, ":: %name%")) ftok = ftok->next(); - while (Token::Match(ftok, "%var% ::")) + while (Token::Match(ftok, "%name% ::")) ftok = ftok->tokAt(2); // Clearing all variables.. @@ -562,7 +562,7 @@ void CheckClass::initializeVarList(const Function &func, std::liststr() == "::") ftok = ftok->next(); assignVar(ftok->strAt(2), scope, usage); @@ -601,7 +601,7 @@ void CheckClass::initializeVarList(const Function &func, std::liststr() != "if") { + } else if (Token::Match(ftok, "::| %name% (") && ftok->str() != "if") { if (ftok->str() == "::") ftok = ftok->next(); @@ -636,7 +636,7 @@ void CheckClass::initializeVarList(const Function &func, std::listnext()) { if (Token::Match(tok2, "[;{}]")) break; - if (Token::Match(tok2, "[(,] &| %var% [,)]")) { + if (Token::Match(tok2, "[(,] &| %name% [,)]")) { tok2 = tok2->next(); if (tok2->str() == "&") tok2 = tok2->next(); @@ -678,17 +678,17 @@ void CheckClass::initializeVarList(const Function &func, std::liststrAt(1) == "[") tok2 = tok2->next()->link(); - else if (Token::Match(tok2->next(), ". %var%")) + else if (Token::Match(tok2->next(), ". %name%")) tok2 = tok2->tokAt(2); else break; @@ -698,14 +698,14 @@ void CheckClass::initializeVarList(const Function &func, std::liststr(), scope, usage); } } @@ -752,11 +752,11 @@ void CheckClass::initializationListUsage() const Scope* owner = scope->functionOf; for (const Token* tok = scope->classStart; tok != scope->classEnd; tok = tok->next()) { - if (Token::Match(tok, "%var% (")) // Assignments might depend on this function call or if/for/while/switch statement from now on. + if (Token::Match(tok, "%name% (")) // Assignments might depend on this function call or if/for/while/switch statement from now on. break; if (Token::Match(tok, "try|do {")) break; - if (tok->varId() && Token::Match(tok, "%var% = %any%")) { + if (Token::Match(tok, "%var% = %any%")) { const Variable* var = tok->variable(); if (var && var->scope() == owner && !var->isStatic()) { bool allowed = true; @@ -774,7 +774,7 @@ void CheckClass::initializationListUsage() } else if (tok2->str() == "this") { // 'this' instance is not completely constructed in initialization list allowed = false; break; - } else if (Token::Match(tok2, "%var% (") && tok2->strAt(-1) != "." && isMemberFunc(owner, tok2)) { // Member function called? + } else if (Token::Match(tok2, "%name% (") && tok2->strAt(-1) != "." && isMemberFunc(owner, tok2)) { // Member function called? allowed = false; break; } @@ -808,9 +808,9 @@ static bool checkFunctionUsage(const std::string& name, const Scope* scope) for (std::list::const_iterator func = scope->functionList.begin(); func != scope->functionList.end(); ++func) { if (func->functionScope) { - if (Token::Match(func->tokenDef, "%var% (")) { + if (Token::Match(func->tokenDef, "%name% (")) { for (const Token *ftok = func->tokenDef->tokAt(2); ftok && ftok->str() != ")"; ftok = ftok->next()) { - if (Token::Match(ftok, "= %var% [(,)]") && ftok->strAt(1) == name) + if (Token::Match(ftok, "= %name% [(,)]") && ftok->strAt(1) == name) return true; if (ftok->str() == "(") ftok = ftok->link(); @@ -1124,7 +1124,7 @@ void CheckClass::operatorEq() // use definition for check so we don't have to deal with qualification if (!(Token::Match(func->retDef, "%type% &") && func->retDef->str() == scope->className)) { // make sure we really have a copy assignment operator - if (Token::Match(func->tokenDef->tokAt(2), "const| %var% &")) { + if (Token::Match(func->tokenDef->tokAt(2), "const| %name% &")) { if (func->tokenDef->strAt(2) == "const" && func->tokenDef->strAt(3) == scope->className) operatorEqReturnError(func->retDef, scope->className); @@ -1369,10 +1369,10 @@ bool CheckClass::hasAssignSelf(const Function *func, const Token *rhs) if (tok1 && tok2) { for (; tok1 && tok1 != tok2; tok1 = tok1->next()) { - if (Token::Match(tok1, "this ==|!= & %var%")) { + if (Token::Match(tok1, "this ==|!= & %name%")) { if (tok1->strAt(3) == rhs->str()) return true; - } else if (Token::Match(tok1, "& %var% ==|!= this")) { + } else if (Token::Match(tok1, "& %name% ==|!= this")) { if (tok1->strAt(1) == rhs->str()) return true; } @@ -1474,7 +1474,6 @@ void CheckClass::virtualDestructor() for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) { if (Token::Match(tok, "[;{}] %var% =") && - tok->next()->varId() > 0 && basepointer.find(tok->next()->varId()) != basepointer.end()) { // new derived class.. if (Token::simpleMatch(tok->tokAt(3), ("new " + derivedClass->str()).c_str())) { @@ -1484,7 +1483,6 @@ void CheckClass::virtualDestructor() // Delete base class pointer that might point at derived class else if (Token::Match(tok, "delete %var% ;") && - tok->next()->varId() && dontDelete.find(tok->next()->varId()) != dontDelete.end()) { ok = false; break; @@ -1562,7 +1560,7 @@ void CheckClass::thisSubtraction() const Token *tok = _tokenizer->tokens(); for (;;) { - tok = Token::findmatch(tok, "this - %var%"); + tok = Token::findmatch(tok, "this - %name%"); if (!tok) break; @@ -1679,10 +1677,10 @@ bool CheckClass::isMemberVar(const Scope *scope, const Token *tok) const return true; } else if (Token::simpleMatch(tok->tokAt(-3), "( * this )")) { return true; - } else if (Token::Match(tok->tokAt(-2), "%var% . %var%")) { + } else if (Token::Match(tok->tokAt(-2), "%name% . %name%")) { tok = tok->tokAt(-2); again = true; - } else if (Token::Match(tok->tokAt(-2), "] . %var%")) { + } else if (Token::Match(tok->tokAt(-2), "] . %name%")) { tok = tok->linkAt(-2)->previous(); again = true; } else if (tok->str() == "]") { @@ -1798,7 +1796,7 @@ bool CheckClass::checkConstFunc(const Scope *scope, const Function *func, bool& const Token *lastVarTok = tok1; const Token *end = tok1; for (;;) { - if (Token::Match(end->next(), ". %var%")) { + if (Token::Match(end->next(), ". %name%")) { end = end->tokAt(2); if (end->varId()) lastVarTok = end; @@ -1868,7 +1866,7 @@ bool CheckClass::checkConstFunc(const Scope *scope, const Function *func, bool& // function call.. - else if (Token::Match(tok1, "%var% (") && !tok1->isStandardType() && + else if (Token::Match(tok1, "%name% (") && !tok1->isStandardType() && !Token::Match(tok1, "return|if|string|switch|while|catch|for")) { if (isMemberFunc(scope, tok1) && tok1->strAt(-1) != ".") { if (!isConstMemberFunc(scope, tok1)) @@ -1963,13 +1961,13 @@ void CheckClass::initializerListOrder() // find all variable initializations in list while (tok && tok != func->functionScope->classStart) { - if (Token::Match(tok, "%var% (|{")) { + if (Token::Match(tok, "%name% (|{")) { const Variable *var = info->getVariable(tok->str()); if (var) vars.push_back(VarInfo(var, tok)); - if (Token::Match(tok->tokAt(2), "%var% =")) { + if (Token::Match(tok->tokAt(2), "%name% =")) { var = info->getVariable(tok->strAt(2)); if (var) @@ -2026,7 +2024,7 @@ void CheckClass::checkSelfInitialization() continue; for (; tok != scope->classStart; tok = tok->next()) { - if (Token::Match(tok, "[:,] %var% (|{ %var% )|}") && tok->next()->varId() && tok->next()->varId() == tok->tokAt(3)->varId()) { + if (Token::Match(tok, "[:,] %var% (|{ %var% )|}") && tok->next()->varId() == tok->tokAt(3)->varId()) { selfInitializationError(tok, tok->strAt(1)); } } diff --git a/lib/checkcondition.cpp b/lib/checkcondition.cpp index 2a12f07cc..fb566e57e 100644 --- a/lib/checkcondition.cpp +++ b/lib/checkcondition.cpp @@ -124,7 +124,7 @@ bool CheckCondition::assignIfParseScope(const Token * const assignTok, ret = true; if (ret && tok2->str() == ";") return false; - if (!islocal && Token::Match(tok2, "%var% (") && !Token::simpleMatch(tok2->next()->link(), ") {")) + if (!islocal && Token::Match(tok2, "%name% (") && !Token::simpleMatch(tok2->next()->link(), ") {")) return true; if (Token::Match(tok2, "if|while (")) { if (!islocal && tok2->str() == "while") @@ -415,23 +415,23 @@ void CheckCondition::oppositeInnerCondition() break; else if ((tok->varId() && vars.find(tok->varId()) != vars.end()) || (!tok->varId() && nonlocal)) { - if (Token::Match(tok, "%var% ++|--|=")) + if (Token::Match(tok, "%name% ++|--|=")) break; - if (Token::Match(tok, "%var% [")) { + if (Token::Match(tok, "%name% [")) { const Token *tok2 = tok->linkAt(1); while (Token::simpleMatch(tok2, "] [")) tok2 = tok2->linkAt(1); if (Token::simpleMatch(tok2, "] =")) break; } - if (Token::Match(tok->previous(), "++|--|& %var%")) + if (Token::Match(tok->previous(), "++|--|& %name%")) break; if (tok->variable() && - Token::Match(tok, "%var% . %var% (") && + Token::Match(tok, "%name% . %name% (") && !tok->variable()->isConst() && !(tok->tokAt(2)->function() && tok->tokAt(2)->function()->isConst())) break; - if (Token::Match(tok->previous(), "[(,] %var% [,)]")) { + if (Token::Match(tok->previous(), "[(,] %name% [,)]")) { // is variable unchanged? default is false.. bool unchanged = false; @@ -773,7 +773,7 @@ void CheckCondition::clarifyCondition() for (std::size_t i = 0; i < functions; ++i) { const Scope * scope = symbolDatabase->functionScopes[i]; for (const Token* tok = scope->classStart->next(); tok != scope->classEnd; tok = tok->next()) { - if (Token::Match(tok, "( %var% [=&|^]")) { + if (Token::Match(tok, "( %name% [=&|^]")) { for (const Token *tok2 = tok->tokAt(3); tok2; tok2 = tok2->next()) { if (tok2->str() == "(" || tok2->str() == "[") tok2 = tok2->link(); @@ -819,9 +819,9 @@ void CheckCondition::clarifyCondition() continue; // #3609 - CWinTraits::.. - if (!isC && Token::Match(tok->previous(), "%var% <")) { + if (!isC && Token::Match(tok->previous(), "%name% <")) { const Token *tok3 = tok2; - while (Token::Match(tok3, "[&|^] %var%")) + while (Token::Match(tok3, "[&|^] %name%")) tok3 = tok3->tokAt(2); if (Token::Match(tok3, ",|>")) continue; diff --git a/lib/checkinternal.cpp b/lib/checkinternal.cpp index 39a5ccfab..8184cd348 100644 --- a/lib/checkinternal.cpp +++ b/lib/checkinternal.cpp @@ -69,7 +69,7 @@ void CheckInternal::checkTokenMatchPatterns() std::strncmp(s+1,"%op%",4)!=0 && std::strncmp(s+1,"%or%",4)!=0 && std::strncmp(s+1,"%cop%",5)!=0 && - std::strncmp(s+1,"%var%",5)!=0 && + std::strncmp(s+1,"%name%",5)!=0 && std::strncmp(s+1,"%oror%",6)!=0) { multiComparePatternError(tok, pattern, funcname); } @@ -186,7 +186,7 @@ void CheckInternal::checkMissingPercentCharacter() magics.insert("%oror%"); magics.insert("%str%"); magics.insert("%type%"); - magics.insert("%var%"); + magics.insert("%name%"); magics.insert("%varid%"); } @@ -235,6 +235,7 @@ void CheckInternal::checkUnknownPattern() knownPatterns.insert("%bool%"); knownPatterns.insert("%char%"); knownPatterns.insert("%comp%"); + knownPatterns.insert("%name%"); knownPatterns.insert("%num%"); knownPatterns.insert("%op%"); knownPatterns.insert("%cop%"); @@ -328,7 +329,7 @@ void CheckInternal::checkExtraWhitespace() void CheckInternal::multiComparePatternError(const Token* tok, const std::string& pattern, const std::string &funcname) { reportError(tok, Severity::error, "multiComparePatternError", - "Bad multicompare pattern (a %cmd% must be first unless it is %or%,%op%,%cop%,%var%,%oror%) inside Token::" + funcname + "() call: \"" + pattern + "\"" + "Bad multicompare pattern (a %cmd% must be first unless it is %or%,%op%,%cop%,%name%,%oror%) inside Token::" + funcname + "() call: \"" + pattern + "\"" ); } diff --git a/lib/checkio.cpp b/lib/checkio.cpp index 770580e49..8886f8226 100644 --- a/lib/checkio.cpp +++ b/lib/checkio.cpp @@ -147,7 +147,7 @@ void CheckIO::checkFileUsage() i->second.op_indent = 0; i->second.lastOperation = Filepointer::UNKNOWN_OP; } - } else if (tok->varId() && Token::Match(tok, "%var% =") && + } else if (Token::Match(tok, "%var% =") && (tok->strAt(2) != "fopen" && tok->strAt(2) != "freopen" && tok->strAt(2) != "tmpfile" && (windows ? (tok->str() != "_wfopen" && tok->str() != "_wfreopen") : true))) { std::map::iterator i = filepointers.find(tok->varId()); @@ -155,7 +155,7 @@ void CheckIO::checkFileUsage() i->second.mode = UNKNOWN_OM; i->second.lastOperation = Filepointer::UNKNOWN_OP; } - } else if (Token::Match(tok, "%var% (") && tok->previous() && (!tok->previous()->isName() || Token::Match(tok->previous(), "return|throw"))) { + } else if (Token::Match(tok, "%name% (") && tok->previous() && (!tok->previous()->isName() || Token::Match(tok->previous(), "return|throw"))) { std::string mode; const Token* fileTok = 0; Filepointer::Operation operation = Filepointer::NONE; @@ -171,7 +171,7 @@ void CheckIO::checkFileUsage() mode = "wb+"; fileTok = tok->tokAt(-2); operation = Filepointer::OPEN; - } else if (windows && Token::Match(tok, "fopen_s|freopen_s|_wfopen_s|_wfreopen_s ( & %var%")) { + } else if (windows && Token::Match(tok, "fopen_s|freopen_s|_wfopen_s|_wfreopen_s ( & %name%")) { const Token* modeTok = tok->tokAt(2)->nextArgument()->nextArgument(); if (modeTok && modeTok->type() == Token::eString) mode = modeTok->strValue(); @@ -245,7 +245,7 @@ void CheckIO::checkFileUsage() } } - while (Token::Match(fileTok, "%var% .")) + while (Token::Match(fileTok, "%name% .")) fileTok = fileTok->tokAt(2); if (!fileTok || !fileTok->varId()) @@ -479,21 +479,21 @@ static bool findFormat(unsigned int arg, const Token *firstArg, *formatStringTok = argTok; return true; } else if (Token::Match(argTok, "%var% [,)]") && - (argTok->variable() && - Token::Match(argTok->variable()->typeStartToken(), "char|wchar_t") && - (argTok->variable()->isPointer() || - (argTok->variable()->dimensions().size() == 1 && - argTok->variable()->dimensionKnown(0) && - argTok->variable()->dimension(0) != 0)))) { + argTok->variable() && + Token::Match(argTok->variable()->typeStartToken(), "char|wchar_t") && + (argTok->variable()->isPointer() || + (argTok->variable()->dimensions().size() == 1 && + argTok->variable()->dimensionKnown(0) && + argTok->variable()->dimension(0) != 0))) { *formatArgTok = argTok->nextArgument(); *formatStringTok = nullptr; if (argTok->variable()) { const Token *varTok = argTok->variable()->nameToken(); - if (Token::Match(varTok, "%var% ; %var% = %str% ;") && + if (Token::Match(varTok, "%name% ; %name% = %str% ;") && varTok->str() == varTok->strAt(2) && Token::Match(varTok->tokAt(-4), "const char|wchar_t * const")) { *formatStringTok = varTok->tokAt(4); - } else if (Token::Match(varTok, "%var% [ %num% ] = %str% ;") && + } else if (Token::Match(varTok, "%name% [ %num% ] = %str% ;") && Token::Match(varTok->tokAt(-2), "const char|wchar_t")) { *formatStringTok = varTok->tokAt(5); } @@ -1442,7 +1442,7 @@ CheckIO::ArgumentInfo::ArgumentInfo(const Token * tok, const Settings *settings) // check for some common well known functions else if ((Token::Match(tok1->previous(), "%var% . size|empty|c_str ( ) [,)]") && isStdContainer(tok1->previous())) || - (Token::Match(tok1->previous(), "] . size|empty|c_str ( ) [,)]") && Token::Match(tok1->previous()->link()->previous(), "%var%") && isStdContainer(tok1->previous()->link()->previous()))) { + (Token::Match(tok1->previous(), "] . size|empty|c_str ( ) [,)]") && isStdContainer(tok1->previous()->link()->previous()))) { tempToken = new Token(0); tempToken->fileIndex(tok1->fileIndex()); tempToken->linenr(tok1->linenr()); diff --git a/lib/checkleakautovar.cpp b/lib/checkleakautovar.cpp index 3fa6b52ca..694a84c68 100644 --- a/lib/checkleakautovar.cpp +++ b/lib/checkleakautovar.cpp @@ -157,14 +157,14 @@ void CheckLeakAutoVar::checkScope(const Token * const startToken, if (tok->varId() > 0) { const std::map::iterator var = alloctype.find(tok->varId()); if (var != alloctype.end()) { - if (var->second.status == VarInfo::DEALLOC && (!Token::Match(tok, "%var% =") || tok->strAt(-1) == "*")) { + if (var->second.status == VarInfo::DEALLOC && (!Token::Match(tok, "%name% =") || tok->strAt(-1) == "*")) { deallocUseError(tok, tok->str()); } else if (Token::simpleMatch(tok->tokAt(-2), "= &")) { varInfo->erase(tok->varId()); } else if (tok->strAt(-1) == "=") { varInfo->erase(tok->varId()); } - } else if (Token::Match(tok->previous(), "& %var% = %var% ;")) { + } else if (Token::Match(tok->previous(), "& %name% = %var% ;")) { varInfo->referenced.insert(tok->tokAt(2)->varId()); } } @@ -185,11 +185,11 @@ void CheckLeakAutoVar::checkScope(const Token * const startToken, break; // parse statement, skip to last member - while (Token::Match(tok, "%var% ::|. %var% !!(")) + while (Token::Match(tok, "%name% ::|. %name% !!(")) tok = tok->tokAt(2); // assignment.. - if (tok->varId() && Token::Match(tok, "%var% =")) { + if (Token::Match(tok, "%var% =")) { // taking address of another variable.. if (Token::Match(tok->next(), "= %var% [+;]")) { if (tok->tokAt(2)->varId() != tok->varId()) { @@ -290,7 +290,7 @@ void CheckLeakAutoVar::checkScope(const Token * const startToken, varInfo2.clear(); } else if (Token::Match(tok->next(), "( ! %var% )|&&")) { varInfo1.erase(tok->tokAt(3)->varId()); - } else if (Token::Match(tok->next(), "( %var% ( ! %var% ) )|&&")) { + } else if (Token::Match(tok->next(), "( %name% ( ! %var% ) )|&&")) { varInfo1.erase(tok->tokAt(5)->varId()); } else if (Token::Match(tok->next(), "( %var% < 0 )|&&")) { varInfo1.erase(tok->tokAt(2)->varId()); @@ -415,7 +415,7 @@ void CheckLeakAutoVar::checkScope(const Token * const startToken, tok = tok->tokAt(3); else tok = tok->next(); - while (Token::Match(tok, "%var% ::|.")) + while (Token::Match(tok, "%name% ::|.")) tok = tok->tokAt(2); if (tok->varId() && tok->strAt(1) != "[") { VarInfo::AllocInfo allocation(-1, VarInfo::DEALLOC); @@ -473,8 +473,7 @@ void CheckLeakAutoVar::functionCall(const Token *tok, VarInfo *varInfo, const Va if (arg->str() == "new") arg = arg->next(); - if ((Token::Match(arg, "%var% [-,)]") && arg->varId() > 0) || - (Token::Match(arg, "& %var%") && arg->next()->varId() > 0)) { + if (Token::Match(arg, "%var% [-,)]") || Token::Match(arg, "& %var%")) { // goto variable if (arg->str() == "&") @@ -482,7 +481,7 @@ void CheckLeakAutoVar::functionCall(const Token *tok, VarInfo *varInfo, const Va // Is variable allocated? changeAllocStatus(varInfo, allocation, tok, arg); - } else if (Token::Match(arg, "%var% (")) { + } else if (Token::Match(arg, "%name% (")) { functionCall(arg, varInfo, allocation); } } @@ -532,7 +531,7 @@ void CheckLeakAutoVar::ret(const Token *tok, const VarInfo &varInfo) used = true; break; } - if (Token::Match(tok2, "return|(|, & %varid% . %var% [);,]", varid)) { + if (Token::Match(tok2, "return|(|, & %varid% . %name% [);,]", varid)) { used = true; break; } diff --git a/lib/checkmemoryleak.cpp b/lib/checkmemoryleak.cpp index 257899468..18ce60a60 100644 --- a/lib/checkmemoryleak.cpp +++ b/lib/checkmemoryleak.cpp @@ -130,7 +130,7 @@ CheckMemoryLeak::AllocType CheckMemoryLeak::getAllocationType(const Token *tok2, if (! tok2->isName()) return No; - if (!Token::Match(tok2, "%type%|%var% ::|. %type%")) { + if (!Token::Match(tok2, "%type%|%name% ::|. %type%")) { // Does tok2 point on "malloc", "strdup" or "kmalloc".. static const char * const mallocfunc[] = { "malloc", @@ -192,7 +192,7 @@ CheckMemoryLeak::AllocType CheckMemoryLeak::getAllocationType(const Token *tok2, } } - while (Token::Match(tok2,"%type%|%var% ::|. %type%")) + while (Token::Match(tok2,"%type%|%name% ::|. %type%")) tok2 = tok2->tokAt(2); // User function @@ -224,7 +224,7 @@ CheckMemoryLeak::AllocType CheckMemoryLeak::getReallocationType(const Token *tok if (! tok2) return No; - if (varid > 0 && ! Token::Match(tok2, "%var% ( %varid% [,)]", varid)) + if (varid > 0 && ! Token::Match(tok2, "%name% ( %varid% [,)]", varid)) return No; if (tok2->str() == "realloc") @@ -415,7 +415,7 @@ CheckMemoryLeak::AllocType CheckMemoryLeak::functionReturnType(const Function* f return No; --indentlevel; } - if (Token::Match(tok2, "return %var% ;")) { + if (Token::Match(tok2, "return %name% ;")) { if (indentlevel != 1) return No; varid = tok2->next()->varId(); @@ -494,10 +494,10 @@ const char *CheckMemoryLeak::functionArgAlloc(const Function *func, unsigned int int realloc = 0; for (tok = func->functionScope->classStart; tok && tok != func->functionScope->classEnd; tok = tok->next()) { if (tok->varId() == arg->declarationId()) { - if (Token::Match(tok->tokAt(-3), "free ( * %var% )")) { + if (Token::Match(tok->tokAt(-3), "free ( * %name% )")) { realloc = 1; allocType = No; - } else if (Token::Match(tok->previous(), "* %var% =")) { + } else if (Token::Match(tok->previous(), "* %name% =")) { allocType = getAllocationType(tok->tokAt(2), arg->declarationId()); if (allocType == No) { allocType = getReallocationType(tok->tokAt(2), arg->declarationId()); @@ -689,7 +689,7 @@ const char * CheckMemoryLeakInFunction::call_func(const Token *tok, std::listexperimental) ? 0 : "callfunc"; @@ -746,9 +746,9 @@ Token *CheckMemoryLeakInFunction::getcode(const Token *tok, std::listnext(), "%var% .")) + while (Token::Match(tok2->next(), "%name% .")) tok2 = tok2->tokAt(2); - if (Token::Match(tok2->next(), "%var% (")) + if (Token::Match(tok2->next(), "%name% (")) ; else if (Token::Match(tok->next(), "continue|break|return|throw|goto|do|else")) @@ -846,7 +846,7 @@ Token *CheckMemoryLeakInFunction::getcode(const Token *tok, std::listtokAt(2); - if (Token::Match(tok, "%var% (")) + if (Token::Match(tok, "%name% (")) tok = tok->next()->link(); continue; } @@ -890,7 +890,7 @@ Token *CheckMemoryLeakInFunction::getcode(const Token *tok, std::listlinkAt(3); continue; } @@ -935,7 +935,7 @@ Token *CheckMemoryLeakInFunction::getcode(const Token *tok, std::listprevious(), "%op%|;|{|}|) ::| %var%") || (Token::Match(tok->previous(), "( ::| %var%") && (!rettail || rettail->str() != "loop"))) { + if (Token::Match(tok->previous(), "%op%|;|{|}|) ::| %name%") || (Token::Match(tok->previous(), "( ::| %name%") && (!rettail || rettail->str() != "loop"))) { if (Token::Match(tok, "%varid% ?", varid)) tok = tok->tokAt(2); @@ -1020,7 +1020,7 @@ Token *CheckMemoryLeakInFunction::getcode(const Token *tok, std::liststr(), _settings, tokenizer->isCPP())) { + } else if (Token::Match(tok2, "%name% (") && !test_white_list(tok2->str(), _settings, tokenizer->isCPP())) { bool use = false; for (const Token *tok3 = tok2->tokAt(2); tok3; tok3 = tok3->nextArgument()) { if (Token::Match(tok3->previous(), "(|, &| %varid% ,|)", varid)) { @@ -1235,18 +1235,18 @@ Token *CheckMemoryLeakInFunction::getcode(const Token *tok, std::listnext()) rethead->deleteNext(); return rethead; } if (Token::Match(tok, "[)=] %varid% [+;)]", varid) || - (Token::Match(tok, "%var% + %varid%", varid) && + (Token::Match(tok, "%name% + %varid%", varid) && tok->strAt(3) != "[" && tok->strAt(3) != ".") || Token::Match(tok, "<< %varid% ;", varid) || Token::Match(tok, "= strcpy|strcat|memmove|memcpy ( %varid% ,", varid) || - Token::Match(tok, "[;{}] %var% [ %varid% ]", varid)) { + Token::Match(tok, "[;{}] %name% [ %varid% ]", varid)) { addtoken(&rettail, tok, "use"); } else if (Token::Match(tok->previous(), ";|{|}|=|(|,|%cop% %varid% [", varid) || Token::Match(tok->previous(), ";|{|}|=|(|,|%cop% %varid% .", varid)) { @@ -1257,7 +1257,7 @@ Token *CheckMemoryLeakInFunction::getcode(const Token *tok, std::listnext()->link(), ") {")) { if (!Token::Match(tok, "if|for|while|switch")) { @@ -1294,7 +1294,7 @@ Token *CheckMemoryLeakInFunction::getcode(const Token *tok, std::list 0 && Token::Match(tok, "%var% ( close|fclose|pclose ( %varid% ) ) ;", varid)) { + if (varid > 0 && Token::Match(tok, "%name% ( close|fclose|pclose ( %varid% ) ) ;", varid)) { addtoken(&rettail, tok, "dealloc"); tok = tok->next()->link(); continue; @@ -1326,13 +1326,13 @@ Token *CheckMemoryLeakInFunction::getcode(const Token *tok, std::listlink(),") (")) { + if (Token::Match(tok, "( *| %name%") && Token::simpleMatch(tok->link(),") (")) { const Token *tok2 = tok->next(); if (tok2->str() == "*") tok2 = tok2->next(); tok2 = tok2->next(); - while (Token::Match(tok2, ". %var%")) + while (Token::Match(tok2, ". %name%")) tok2 = tok2->tokAt(2); if (Token::simpleMatch(tok2, ") (")) { @@ -1502,12 +1502,12 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok) const } // Delete braces around a single instruction.. - if (Token::Match(tok2->next(), "{ %var% ; }")) { + if (Token::Match(tok2->next(), "{ %name% ; }")) { tok2->deleteNext(); tok2->tokAt(2)->deleteNext(); done = false; } - if (Token::Match(tok2->next(), "{ %var% %var% ; }")) { + if (Token::Match(tok2->next(), "{ %name% %name% ; }")) { tok2->deleteNext(); tok2->tokAt(3)->deleteNext(); done = false; @@ -1534,7 +1534,7 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok) const } // Reduce "if X ; else X ;" => "X ;" - else if (Token::Match(tok2->next(), "if %var% ; else %var% ;") && + else if (Token::Match(tok2->next(), "if %name% ; else %name% ;") && tok2->strAt(2) == tok2->strAt(5)) { tok2->deleteNext(4); done = false; @@ -1561,8 +1561,8 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok) const done = false; } - // Reduce "if ; else %var% ;" => "if %var% ;" - else if (Token::Match(tok2->next(), "if ; else %var% ;")) { + // Reduce "if ; else %name% ;" => "if %name% ;" + else if (Token::Match(tok2->next(), "if ; else %name% ;")) { tok2->next()->deleteNext(2); done = false; } @@ -1808,8 +1808,8 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok) const done = false; } - // Reduce "[;{}] return use ; %var%" => "[;{}] return use ;" - if (Token::Match(tok2, "[;{}] return use ; %var%")) { + // Reduce "[;{}] return use ; %name%" => "[;{}] return use ;" + if (Token::Match(tok2, "[;{}] return use ; %name%")) { tok2->tokAt(3)->deleteNext(); done = false; } @@ -2160,12 +2160,12 @@ void CheckMemoryLeakInFunction::checkReallocUsage() // Search for the "var = realloc(var, 100" pattern within this function for (const Token *tok = scope->classStart->next(); tok != scope->classEnd; tok = tok->next()) { if (tok->varId() > 0 && - Token::Match(tok, "%var% = realloc|g_try_realloc ( %var% , %any%") && + Token::Match(tok, "%name% = realloc|g_try_realloc ( %name% , %any%") && tok->varId() == tok->tokAt(4)->varId() && isNoArgument(symbolDatabase, tok->varId())) { // Check that another copy of the pointer wasn't saved earlier in the function - if (Token::findmatch(scope->classStart, "%var% = %varid% ;", tok, tok->varId()) || - Token::findmatch(scope->classStart, "[{};] %varid% = %var% [;=]", tok, tok->varId())) + if (Token::findmatch(scope->classStart, "%name% = %varid% ;", tok, tok->varId()) || + Token::findmatch(scope->classStart, "[{};] %varid% = %name% [;=]", tok, tok->varId())) continue; const Token* tokEndRealloc = tok->linkAt(3); @@ -2173,18 +2173,18 @@ void CheckMemoryLeakInFunction::checkReallocUsage() if (Token::Match(tokEndRealloc->next(), "; if ( ! %varid% ) {", tok->varId())) { const Token* tokEndBrace = tokEndRealloc->linkAt(7); if (tokEndBrace && Token::simpleMatch(tokEndBrace->tokAt(-2), ") ;") && - Token::Match(tokEndBrace->linkAt(-2)->tokAt(-2), "{|}|; %var% (")) + Token::Match(tokEndBrace->linkAt(-2)->tokAt(-2), "{|}|; %name% (")) continue; } memleakUponReallocFailureError(tok, tok->str()); } else if (tok->next()->varId() > 0 && - (Token::Match(tok, "* %var% = realloc|g_try_realloc ( * %var% , %any%") && + (Token::Match(tok, "* %name% = realloc|g_try_realloc ( * %name% , %any%") && tok->next()->varId() == tok->tokAt(6)->varId()) && isNoArgument(symbolDatabase, tok->next()->varId())) { // Check that another copy of the pointer wasn't saved earlier in the function - if (Token::findmatch(scope->classStart, "%var% = * %varid% ;", tok, tok->next()->varId()) || - Token::findmatch(scope->classStart, "[{};] * %varid% = %var% [;=]", tok, tok->next()->varId())) + if (Token::findmatch(scope->classStart, "%name% = * %varid% ;", tok, tok->next()->varId()) || + Token::findmatch(scope->classStart, "[{};] * %varid% = %name% [;=]", tok, tok->next()->varId())) continue; const Token* tokEndRealloc = tok->linkAt(4); @@ -2192,7 +2192,7 @@ void CheckMemoryLeakInFunction::checkReallocUsage() if (Token::Match(tokEndRealloc->next(), "; if ( ! * %varid% ) {", tok->next()->varId())) { const Token* tokEndBrace = tokEndRealloc->linkAt(8); if (tokEndBrace && Token::simpleMatch(tokEndBrace->tokAt(-2), ") ;") && - Token::Match(tokEndBrace->linkAt(-2)->tokAt(-2), "{|}|; %var% (")) + Token::Match(tokEndBrace->linkAt(-2)->tokAt(-2), "{|}|; %name% (")) continue; } memleakUponReallocFailureError(tok->next(), tok->strAt(1)); @@ -2397,7 +2397,7 @@ void CheckMemoryLeakInClass::variable(const Scope *scope, const Token *tokVarnam } // Function call .. possible deallocation - else if (Token::Match(tok->previous(), "[{};] %var% (")) { + else if (Token::Match(tok->previous(), "[{};] %name% (")) { if (!CheckMemoryLeakInFunction::test_white_list(tok->str(), _settings, tokenizer->isCPP())) { return; } @@ -2642,7 +2642,7 @@ void CheckMemoryLeakStructMember::checkStructVariable(const Variable * const var break; // using struct in a function call.. - else if (Token::Match(tok3, "%var% (")) { + else if (Token::Match(tok3, "%name% (")) { // Calling non-function / function that doesn't deallocate? if (ignoredFunctions.find(tok3->str()) != ignoredFunctions.end()) continue; @@ -2657,7 +2657,7 @@ void CheckMemoryLeakStructMember::checkStructVariable(const Variable * const var break; } - if (Token::Match(tok4, "[(,] &| %varid% . %var% [,)]", structid)) { + if (Token::Match(tok4, "[(,] &| %varid% . %name% [,)]", structid)) { /** @todo check if the function deallocates the memory */ deallocated = true; break; @@ -2704,14 +2704,14 @@ void CheckMemoryLeakNoVar::check() // parse the executable scope until tok is reached... for (const Token *tok2 = tok->link(); tok2 && tok2 != tok; tok2 = tok2->next()) { // allocating memory in parameter for function call.. - if (Token::Match(tok2, "[(,] %var% (") && Token::Match(tok2->linkAt(2), ") [,)]")) { + if (Token::Match(tok2, "[(,] %name% (") && Token::Match(tok2->linkAt(2), ") [,)]")) { const AllocType allocType = getAllocationType(tok2->next(), 0); if (allocType != No) { // locate outer function call.. for (const Token *tok3 = tok2; tok3; tok3 = tok3->previous()) { if (tok3->str() == "(") { // Is it a function call.. - if (!Token::Match(tok3->tokAt(-2), "= %var% (")) { + if (!Token::Match(tok3->tokAt(-2), "= %name% (")) { const std::string& functionName = tok3->strAt(-1); if ((tokenizer->isCPP() && functionName == "delete") || functionName == "free" || @@ -2745,7 +2745,7 @@ void CheckMemoryLeakNoVar::check() void CheckMemoryLeakNoVar::checkForUnusedReturnValue(const Scope *scope) { for (const Token *tok = scope->classStart; tok != scope->classEnd; tok = tok->next()) { - if (Token::Match(tok, "{|}|; %var% (") && tok->strAt(-1) != "=") { + if (Token::Match(tok, "{|}|; %name% (") && tok->strAt(-1) != "=") { tok = tok->next(); const int allocationId = _settings->library.alloc(tok); if (allocationId > 0) @@ -2772,7 +2772,7 @@ void CheckMemoryLeakNoVar::checkForUnsafeArgAlloc(const Scope *scope) return; for (const Token *tok = scope->classStart; tok != scope->classEnd; tok = tok->next()) { - if (Token::Match(tok, "%var% (")) { + if (Token::Match(tok, "%name% (")) { const Token *endParamToken = tok->next()->link(); std::string pointerType; std::string objectType; @@ -2783,13 +2783,13 @@ void CheckMemoryLeakNoVar::checkForUnsafeArgAlloc(const Scope *scope) const Function *pFunc = tok2->function(); const bool isNothrow = pFunc && pFunc->isAttributeNothrow(); - if (Token::Match(tok2, "shared_ptr|unique_ptr < %var% > ( new %var%")) { + if (Token::Match(tok2, "shared_ptr|unique_ptr < %name% > ( new %name%")) { pointerType = tok2->str(); objectType = tok2->strAt(6); } else if (!isNothrow) { - if (Token::Match(tok2, "%var% (")) + if (Token::Match(tok2, "%name% (")) functionCalled = tok2->str(); - else if (Token::Match(tok2, "%var% < %var% > (")) + else if (Token::Match(tok2, "%name% < %name% > (")) functionCalled = tok2->str() + "<" + tok2->strAt(2) + ">"; } } diff --git a/lib/checknullpointer.cpp b/lib/checknullpointer.cpp index 54d5c40f7..5fd23e297 100644 --- a/lib/checknullpointer.cpp +++ b/lib/checknullpointer.cpp @@ -46,14 +46,14 @@ static bool checkNullpointerFunctionCallPlausibility(const Function* func, unsig */ void CheckNullPointer::parseFunctionCall(const Token &tok, std::list &var, const Library *library, unsigned char value) { - if (Token::Match(&tok, "%var% ( )") || !tok.tokAt(2)) + if (Token::Match(&tok, "%name% ( )") || !tok.tokAt(2)) return; const Token* firstParam = tok.tokAt(2); const Token* secondParam = firstParam->nextArgument(); // 1st parameter.. - if ((Token::Match(firstParam, "%var% ,|)") && firstParam->varId() > 0) || + if (Token::Match(firstParam, "%var% ,|)") || (value == 0 && Token::Match(firstParam, "0|NULL ,|)"))) { if (value == 0 && Token::Match(&tok, "snprintf|vsnprintf|fnprintf|vfnprintf") && secondParam && secondParam->str() != "0") // Only if length (second parameter) is not zero var.push_back(firstParam); @@ -117,7 +117,7 @@ void CheckNullPointer::parseFunctionCall(const Token &tok, std::liststr() == "0") || (argListTok->varId() > 0 && Token::Match(argListTok,"%var% [,)]"))) { + if ((value == 0 && argListTok->str() == "0") || Token::Match(argListTok, "%var% [,)]")) { var.push_back(argListTok); } } @@ -186,18 +186,17 @@ bool CheckNullPointer::isPointerDeRef(const Token *tok, bool &unknown) return false; } - if (Token::Match(tok, "%var% (")) + if (Token::Match(tok, "%name% (")) return true; if (Token::Match(tok, "%var% = %var% .") && - tok->varId() > 0 && tok->varId() == tok->tokAt(2)->varId()) return true; // std::string dereferences nullpointers if (Token::Match(parent->tokAt(-3), "std :: string|wstring (") && tok->strAt(1) == ")") return true; - if (Token::Match(parent->previous(), "%var% (") && tok->strAt(1) == ")") { + if (Token::Match(parent->previous(), "%name% (") && tok->strAt(1) == ")") { const Variable* var = tok->tokAt(-2)->variable(); if (var && !var->isPointer() && !var->isArray() && var->isStlStringType()) return true; @@ -257,7 +256,7 @@ void CheckNullPointer::nullPointerLinkedList() const Token* end2 = tok1->linkAt(1); for (const Token *tok2 = tok1->tokAt(2); tok2 != end2; tok2 = tok2->next()) { // Dereferencing a variable inside the "for" parentheses.. - if (Token::Match(tok2, "%var% . %var%")) { + if (Token::Match(tok2, "%var% . %name%")) { // Is this variable a pointer? const Variable *var = tok2->variable(); if (!var || !var->isPointer()) @@ -266,10 +265,6 @@ void CheckNullPointer::nullPointerLinkedList() // Variable id for dereferenced variable const unsigned int varid(tok2->varId()); - // We don't support variables without a varid - if (varid == 0) - continue; - if (Token::Match(tok2->tokAt(-2), "%varid% ?", varid)) continue; @@ -319,7 +314,7 @@ void CheckNullPointer::nullPointerByDeRefAndChec() continue; // Is pointer used as function parameter? - if (Token::Match(tok->previous(), "[(,] %var% [,)]")) { + if (Token::Match(tok->previous(), "[(,] %name% [,)]")) { const Token *ftok = tok->previous(); while (ftok && ftok->str() != "(") { if (ftok->str() == ")") @@ -400,7 +395,7 @@ void CheckNullPointer::nullConstantDereference() else if (Token::Match(tok, "0 [") && (tok->previous()->str() != "&" || !Token::Match(tok->next()->link()->next(), "[.(]"))) nullPointerError(tok); - else if (Token::Match(tok->previous(), "!!. %var% (") && (tok->previous()->str() != "::" || tok->strAt(-2) == "std")) { + else if (Token::Match(tok->previous(), "!!. %name% (") && (tok->previous()->str() != "::" || tok->strAt(-2) == "std")) { if (Token::simpleMatch(tok->tokAt(2), "0 )") && tok->varId()) { // constructor call const Variable *var = tok->variable(); if (var && !var->isPointer() && !var->isArray() && var->isStlStringType()) @@ -457,7 +452,7 @@ void CheckNullPointer::nullConstantDereference() void CheckNullPointer::removeAssignedVarFromSet(const Token* tok, std::set& pointerArgs) { // If a pointer's address is passed into a function, stop considering it - if (Token::Match(tok->previous(), "[;{}] %var% (")) { + if (Token::Match(tok->previous(), "[;{}] %name% (")) { const Token* endParen = tok->next()->link(); for (const Token* tok2 = tok->next(); tok2 != endParen; tok2 = tok2->next()) { if (tok2->isName() && tok2->varId() > 0 @@ -491,7 +486,7 @@ void CheckNullPointer::nullPointerDefaultArgument() std::set pointerArgs; for (const Token *tok = scope->function->arg; tok != scope->function->arg->link(); tok = tok->next()) { - if (Token::Match(tok, "%var% = 0 ,|)") && tok->varId() != 0) { + if (Token::Match(tok, "%var% = 0 ,|)")) { const Variable *var = tok->variable(); if (var && var->isPointer()) pointerArgs.insert(tok->varId()); @@ -531,10 +526,10 @@ void CheckNullPointer::nullPointerDefaultArgument() Token::findmatch(startOfIfBlock, "exit|return|throw", endOfIf) != nullptr; if (Token::Match(tok, "if ( %var% == 0 )")) { - const unsigned int var = tok->tokAt(2)->varId(); - if (var > 0 && pointerArgs.count(var) > 0) { + const unsigned int varid = tok->tokAt(2)->varId(); + if (pointerArgs.count(varid) > 0) { if (isExitOrReturn) - pointerArgs.erase(var); + pointerArgs.erase(varid); else dependsOnPointer = true; } diff --git a/lib/checkother.cpp b/lib/checkother.cpp index f97445c7a..6acbe3470 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -100,8 +100,8 @@ bool isSameExpression(const Token *tok1, const Token *tok2, const std::setnext()->link()) || - (Token::Match(tok2, "%var% <") && tok2->next()->link())) { + if ((Token::Match(tok1, "%name% <") && tok1->next()->link()) || + (Token::Match(tok2, "%name% <") && tok2->next()->link())) { // non-const template function that is not a dynamic_cast => return false if (Token::simpleMatch(tok1->next()->link(), "> (") && @@ -297,7 +297,7 @@ void CheckOther::clarifyStatement() 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%") && tok->astOperand1()) { + if (Token::Match(tok, "* %name%") && tok->astOperand1()) { const Token *tok2=tok->previous(); while (tok2 && tok2->str() == "*") @@ -370,7 +370,7 @@ void CheckOther::warningOldStylePointerCast() tok = scope->classStart; for (; tok && tok != scope->classEnd; tok = tok->next()) { // Old style pointer casting.. - if (!Token::Match(tok, "( const| %type% * const| ) (| %var%|%num%|%bool%|%char%|%str%")) + if (!Token::Match(tok, "( const| %type% * const| ) (| %name%|%num%|%bool%|%char%|%str%")) continue; // skip first "const" in "const Type* const" @@ -617,7 +617,7 @@ void CheckOther::checkRedundantAssignment() } else if (Token::Match(tok, "break|return|continue|throw|goto")) { varAssignments.clear(); memAssignments.clear(); - } else if (tok->type() == Token::eVariable && !Token::Match(tok,"%var% (")) { + } else if (tok->type() == Token::eVariable && !Token::Match(tok, "%name% (")) { // Set initialization flag if (!Token::Match(tok, "%var% [")) initialized.insert(tok->varId()); @@ -630,9 +630,9 @@ void CheckOther::checkRedundantAssignment() } const Token *startToken = tok; - while (Token::Match(startToken, "%var%|::|.")) { + while (Token::Match(startToken, "%name%|::|.")) { startToken = startToken->previous(); - if (Token::Match(startToken, "%var% . %var%")) + if (Token::Match(startToken, "%name% . %var%")) membervars[startToken->varId()].insert(startToken->tokAt(2)->varId()); } @@ -645,7 +645,7 @@ void CheckOther::checkRedundantAssignment() break; else if (tok2->varId() == tok->varId()) error = false; - else if (Token::Match(tok2, "%var% (") && nonLocal(tok->variable())) { // Called function might use the variable + else if (Token::Match(tok2, "%name% (") && nonLocal(tok->variable())) { // Called function might use the variable const Function* const func = tok2->function(); const Variable* const var = tok->variable(); if (!var || var->isGlobal() || var->isReference() || ((!func || func->nestedIn) && tok2->strAt(-1) != ".")) // Global variable, or member function @@ -677,7 +677,7 @@ void CheckOther::checkRedundantAssignment() if (!writtenArgumentsEnd) // Indicates that we are in the first argument of strcpy/memcpy/... function memAssignments.erase(tok->varId()); } - } else if (Token::Match(tok, "%var% (") && _settings->library.functionpure.find(tok->str()) == _settings->library.functionpure.end()) { // Function call. Global variables might be used. Reset their status + } else if (Token::Match(tok, "%name% (") && _settings->library.functionpure.find(tok->str()) == _settings->library.functionpure.end()) { // Function call. Global variables might be used. Reset their status const bool memfunc = Token::Match(tok, "memcpy|memmove|memset|strcpy|strncpy|sprintf|snprintf|strcat|strncat|wcscpy|wcsncpy|swprintf|wcscat|wcsncat"); if (tok->varId()) // operator() or function pointer varAssignments.erase(tok->varId()); @@ -776,7 +776,7 @@ void CheckOther::redundantAssignmentInSwitchError(const Token *tok1, const Token //--------------------------------------------------------------------------- static inline bool isFunctionOrBreakPattern(const Token *tok) { - if (Token::Match(tok, "%var% (") || Token::Match(tok, "break|continue|return|exit|goto|throw")) + if (Token::Match(tok, "%name% (") || Token::Match(tok, "break|continue|return|exit|goto|throw")) return true; return false; @@ -823,7 +823,7 @@ void CheckOther::checkRedundantAssignmentInSwitch() // case 3: b = 1; // <== redundant // case 4: b = 2; - if (Token::Match(tok2->previous(), ";|{|}|: %var% = %any% ;") && tok2->varId() != 0) { + if (Token::Match(tok2->previous(), ";|{|}|: %var% = %any% ;")) { varsWithBitsSet.erase(tok2->varId()); bitOperations.erase(tok2->varId()); } @@ -831,8 +831,8 @@ void CheckOther::checkRedundantAssignmentInSwitch() // Bitwise operation. Report an error if it's performed twice before a break. E.g.: // case 3: b |= 1; // <== redundant // case 4: b |= 1; - else if (Token::Match(tok2->previous(), ";|{|}|: %var% = %var% %or%|& %num% ;") && - tok2->varId() != 0 && tok2->varId() == tok2->tokAt(2)->varId()) { + else if (Token::Match(tok2->previous(), ";|{|}|: %var% = %name% %or%|& %num% ;") && + tok2->varId() == tok2->tokAt(2)->varId()) { std::string bitOp = tok2->strAt(3) + tok2->strAt(4); std::map::iterator i2 = varsWithBitsSet.find(tok2->varId()); @@ -1017,7 +1017,7 @@ void CheckOther::checkSuspiciousEqualityComparison() // for (i == 2; i < 10; i++) // or // for (i = 0; i < 10; i == a) - if (Token::Match(openParen->next(), "%var% ==")) + if (Token::Match(openParen->next(), "%name% ==")) suspiciousEqualityComparisonError(openParen->tokAt(2)); if (Token::Match(closeParen->tokAt(-2), "== %any%")) suspiciousEqualityComparisonError(closeParen->tokAt(-2)); @@ -1027,15 +1027,15 @@ void CheckOther::checkSuspiciousEqualityComparison() // in the initialization or increment-decrement parts of the for() loop. // For example: // for (!i; i < 10; i++) - if (Token::Match(openParen->next(), "! %var%")) + if (Token::Match(openParen->next(), "! %name%")) suspiciousEqualityComparisonError(openParen->next()); - if (Token::Match(closeParen->tokAt(-2), "! %var%")) + if (Token::Match(closeParen->tokAt(-2), "! %name%")) suspiciousEqualityComparisonError(closeParen->tokAt(-2)); // Skip over for() loop conditions because "for (;running==1;)" // is a bit strange, but not necessarily incorrect. tok = closeParen; - } else if (Token::Match(tok, "[;{}] *| %var% == %any% ;")) { + } else if (Token::Match(tok, "[;{}] *| %name% == %any% ;")) { // Exclude compound statements surrounded by parentheses, such as // printf("%i\n", ({x==0;})); @@ -1066,7 +1066,7 @@ void CheckOther::invalidFunctionUsage() for (std::size_t i = 0; i < functions; ++i) { const Scope * scope = symbolDatabase->functionScopes[i]; for (const Token* tok = scope->classStart->next(); tok != scope->classEnd; tok = tok->next()) { - if (!Token::Match(tok, "%var% ( !!)")) + if (!Token::Match(tok, "%name% ( !!)")) continue; const Token * const functionToken = tok; int argnr = 1; @@ -1154,7 +1154,7 @@ void CheckOther::checkUnreachableCode() } else if (Token::Match(tok, "goto %any% ;")) { secondBreak = tok->tokAt(3); labelName = tok->next(); - } else if (Token::Match(tok, "%var% (") && _settings->library.isnoreturn(tok)) { + } else if (Token::Match(tok, "%name% (") && _settings->library.isnoreturn(tok)) { if ((!tok->function() || (tok->function()->token != tok && tok->function()->tokenDef != tok)) && tok->linkAt(1)->strAt(1) != "{") secondBreak = tok->linkAt(1)->tokAt(2); } @@ -1196,7 +1196,7 @@ void CheckOther::checkUnreachableCode() bool silencedCompilerWarningOnly = false; const Token *silencedWarning = secondBreak; for (;;) { - if (Token::Match(silencedWarning, "( void ) %var% ;")) { + if (Token::Match(silencedWarning, "( void ) %name% ;")) { silencedWarning = silencedWarning->tokAt(5); continue; } else if (silencedWarning && silencedWarning == scope->classEnd) @@ -1719,11 +1719,11 @@ void CheckOther::checkIncompleteStatement() tok = tok->next()->link(); // C++11 struct/array/etc initialization in initializer list - else if (Token::Match(tok->previous(), "%var%|] {") && !Token::findsimplematch(tok,";",tok->link())) + else if (Token::Match(tok->previous(), "%name%|] {") && !Token::findsimplematch(tok,";",tok->link())) tok = tok->link(); // C++11 vector initialization / return { .. } - else if (Token::Match(tok,"> %var% {") || Token::Match(tok, "[;{}] return {")) + else if (Token::Match(tok,"> %name% {") || Token::Match(tok, "[;{}] return {")) tok = tok->linkAt(2); // C++11 initialize set in initalizer list : [,:] std::set{1} [{,] @@ -1963,7 +1963,7 @@ void CheckOther::checkMisusedScopedObject() 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% (") + if (Token::Match(tok, "[;{}] %name% (") && Token::Match(tok->linkAt(2), ") ; !!}") && symbolDatabase->isClassOrStruct(tok->next()->str()) && (!tok->next()->function() || // is not a function on this scope @@ -2073,7 +2073,7 @@ void CheckOther::checkInvalidFree() // If a previously-allocated pointer is incremented or decremented, any subsequent // free involving pointer arithmetic may or may not be invalid, so we should only // report an inconclusive result. - else if (Token::Match(tok, "%var% = %var% +|-") && + else if (Token::Match(tok, "%var% = %name% +|-") && tok->varId() == tok->tokAt(2)->varId() && allocatedVariables.find(tok->varId()) != allocatedVariables.end()) { if (_settings->inconclusive) @@ -2110,7 +2110,7 @@ void CheckOther::checkInvalidFree() // If the previously-allocated variable is passed in to another function // as a parameter, it might be modified, so we shouldn't report an error // if it is later used to free memory - else if (Token::Match(tok, "%var% (") && _settings->library.functionpure.find(tok->str()) == _settings->library.functionpure.end()) { + else if (Token::Match(tok, "%name% (") && _settings->library.functionpure.find(tok->str()) == _settings->library.functionpure.end()) { const Token* tok2 = Token::findmatch(tok->next(), "%var%", tok->linkAt(1)); while (tok2 != nullptr) { allocatedVariables.erase(tok2->varId()); @@ -2292,7 +2292,7 @@ void CheckOther::checkComparisonFunctionIsAlwaysTrueOrFalse() const unsigned int varidRight = tok->tokAt(4)->varId();// get the right varid // compare varids: if they are not zero but equal // --> the comparison function is calles with the same variables - if (varidLeft != 0 && varidLeft == varidRight) { + if (varidLeft == varidRight) { const std::string& functionName = tok->str(); // store function name const std::string& varNameLeft = tok->strAt(2); // get the left variable name if (functionName == "isgreater" || functionName == "isless" || functionName == "islessgreater") { @@ -2371,7 +2371,7 @@ void CheckOther::checkSignOfUnsignedVariable() const Scope * scope = symbolDatabase->functionScopes[i]; // check all the code in the function for (const Token *tok = scope->classStart->next(); tok != scope->classEnd; tok = tok->next()) { - if (Token::Match(tok, "%var% <|<= 0") && tok->varId() && !Token::Match(tok->tokAt(3), "+|-")) { + if (Token::Match(tok, "%name% <|<= 0") && tok->varId() && !Token::Match(tok->tokAt(3), "+|-")) { // TODO: handle a[10].b , a::b , (unsigned int)x , etc const Token *prev = tok->previous(); while (prev && (prev->isName() || prev->str() == ".")) @@ -2383,19 +2383,19 @@ void CheckOther::checkSignOfUnsignedVariable() unsignedLessThanZeroError(tok, var->name(), inconclusive); else if (var && (var->isPointer() || var->isArray())) pointerLessThanZeroError(tok, inconclusive); - } else if (Token::Match(tok, "0 >|>= %var%") && tok->tokAt(2)->varId() && !Token::Match(tok->tokAt(3), "+|-|*|/") && !Token::Match(tok->previous(), "+|-|<<|>>|~")) { + } else if (Token::Match(tok, "0 >|>= %name%") && tok->tokAt(2)->varId() && !Token::Match(tok->tokAt(3), "+|-|*|/") && !Token::Match(tok->previous(), "+|-|<<|>>|~")) { const Variable *var = tok->tokAt(2)->variable(); if (var && var->typeEndToken()->isUnsigned()) unsignedLessThanZeroError(tok, var->name(), inconclusive); else if (var && var->isPointer() && !Token::Match(tok->tokAt(3), "[.[(]")) pointerLessThanZeroError(tok, inconclusive); - } else if (Token::Match(tok, "0 <= %var%") && tok->tokAt(2)->varId() && !Token::Match(tok->tokAt(3), "+|-|*|/") && !Token::Match(tok->previous(), "+|-|<<|>>|~")) { + } else if (Token::Match(tok, "0 <= %name%") && tok->tokAt(2)->varId() && !Token::Match(tok->tokAt(3), "+|-|*|/") && !Token::Match(tok->previous(), "+|-|<<|>>|~")) { const Variable *var = tok->tokAt(2)->variable(); if (var && var->typeEndToken()->isUnsigned()) unsignedPositiveError(tok, var->name(), inconclusive); else if (var && var->isPointer() && !Token::Match(tok->tokAt(3), "[.[]")) pointerPositiveError(tok, inconclusive); - } else if (Token::Match(tok, "%var% >= 0") && tok->varId() && !Token::Match(tok->previous(), "++|--|)|+|-|*|/|~|<<|>>") && !Token::Match(tok->tokAt(3), "+|-")) { + } else if (Token::Match(tok, "%name% >= 0") && tok->varId() && !Token::Match(tok->previous(), "++|--|)|+|-|*|/|~|<<|>>") && !Token::Match(tok->tokAt(3), "+|-")) { const Variable *var = tok->variable(); if (var && var->typeEndToken()->isUnsigned()) unsignedPositiveError(tok, var->name(), inconclusive); @@ -2484,7 +2484,7 @@ void CheckOther::checkRedundantCopy() continue; const Token* startTok = var->nameToken(); - if (startTok->strAt(1) == "=") // %type% %var% = ... ; + if (startTok->strAt(1) == "=") // %type% %name% = ... ; ; else if (startTok->strAt(1) == "(" && var->isClass() && var->typeScope()) { // Object is instantiated. Warn if constructor takes arguments by value. @@ -2496,7 +2496,7 @@ void CheckOther::checkRedundantCopy() const Token* tok = startTok->next()->astOperand2(); if (!tok) continue; - if (!Token::Match(tok->previous(), "%var% (")) + if (!Token::Match(tok->previous(), "%name% (")) continue; if (!Token::Match(tok->link(), ") )| ;")) // bailout for usage like "const A a = getA()+3" continue; @@ -2720,13 +2720,13 @@ void CheckOther::checkIgnoredReturnValue() for (std::size_t i = 0; i < functions; ++i) { const Scope * scope = symbolDatabase->functionScopes[i]; for (const Token* tok = scope->classStart; tok != scope->classEnd; tok = tok->next()) { - if (tok->varId() || !Token::Match(tok, "%var% (") || tok->strAt(-1) == "." || tok->next()->astOperand1() != tok) + if (tok->varId() || !Token::Match(tok, "%name% (") || tok->strAt(-1) == "." || tok->next()->astOperand1() != tok) continue; if (!tok->scope()->isExecutable()) tok = tok->scope()->classEnd; - if (!tok->next()->astParent() && (!tok->function() || !Token::Match(tok->function()->retDef, "void %var%")) && _settings->library.useretval.find(tok->str()) != _settings->library.useretval.end()) + if (!tok->next()->astParent() && (!tok->function() || !Token::Match(tok->function()->retDef, "void %name%")) && _settings->library.useretval.find(tok->str()) != _settings->library.useretval.end()) ignoredReturnValueError(tok, tok->str()); } } @@ -2781,7 +2781,7 @@ void CheckOther::checkLibraryMatchFunctions() return; for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) { - if (Token::Match(tok, "%var% (") && + if (Token::Match(tok, "%name% (") && !tok->function() && tok->astParent() == tok->next() && _settings->library.isNotLibraryFunction(tok)) { diff --git a/lib/checksizeof.cpp b/lib/checksizeof.cpp index c0e5668ec..7dde154b4 100644 --- a/lib/checksizeof.cpp +++ b/lib/checksizeof.cpp @@ -76,12 +76,10 @@ void CheckSizeof::checkSizeofForArrayParameter() if (varTok->str() == "(") { varTok = varTok->next(); } - if (varTok->varId() > 0) { - const Variable *var = varTok->variable(); - if (var && var->isArray() && var->isArgument()) { - sizeofForArrayParameterError(tok); - } - } + + const Variable *var = varTok->variable(); + if (var && var->isArray() && var->isArgument()) + sizeofForArrayParameterError(tok); } } } @@ -311,8 +309,8 @@ void CheckSizeof::sizeofVoid() (Token::Match(tok->tokAt(3)->variable()->typeStartToken(), "void * !!*")) && (!tok->tokAt(3)->variable()->isArray())) { // sizeof(*p) where p is of type "void*" sizeofDereferencedVoidPointerError(tok, tok->strAt(3)); - } else if (Token::Match(tok, "%var% +|-|++|--") || - Token::Match(tok, "+|-|++|-- %var%")) { // Arithmetic operations on variable of type "void*" + } else if (Token::Match(tok, "%name% +|-|++|--") || + Token::Match(tok, "+|-|++|-- %name%")) { // Arithmetic operations on variable of type "void*" const int index = (tok->isName()) ? 0 : 1; const Variable* var = tok->tokAt(index)->variable(); if (var && !var->isArray() && Token::Match(var->typeStartToken(), "void * !!*")) { @@ -338,7 +336,7 @@ void CheckSizeof::sizeofVoid() } } // Check for cast on operations with '+|-' - if (Token::Match(tok, "%var% +|-")) { + if (Token::Match(tok, "%name% +|-")) { // Check for cast expression if (Token::simpleMatch(tok2->previous(), ")") && !Token::Match(tok2->previous()->link(), "( const| void *")) continue; diff --git a/lib/checkstl.cpp b/lib/checkstl.cpp index 5d080cc9b..e7c10febb 100644 --- a/lib/checkstl.cpp +++ b/lib/checkstl.cpp @@ -60,7 +60,7 @@ void CheckStl::dereferenceErasedError(const Token *erased, const Token* deref, c static const Token *skipMembers(const Token *tok) { - while (Token::Match(tok, "%var% .")) + while (Token::Match(tok, "%name% .")) tok = tok->tokAt(2); return tok; } @@ -78,7 +78,7 @@ void CheckStl::iterators() if (!var || !var->isLocal() || !Token::Match(var->typeEndToken(), "iterator|const_iterator|reverse_iterator|const_reverse_iterator|auto")) continue; - if (var->typeEndToken()->str() == "auto" && !Token::Match(var->typeEndToken(), "auto %var% ; %var% = %var% . begin|end ( )")) + if (var->typeEndToken()->str() == "auto" && !Token::Match(var->typeEndToken(), "auto %name% ; %name% = %name% . begin|end ( )")) continue; if (var->type()) { // If it is defined, ensure that it is defined like an iterator @@ -114,13 +114,13 @@ void CheckStl::iterators() validIterator = true; // Is iterator compared against different container? - if (Token::Match(tok2, "%varid% !=|== %var% . end|rend|cend|crend ( )", iteratorId) && container && tok2->tokAt(2)->varId() != container->declarationId()) { + if (Token::Match(tok2, "%varid% !=|== %name% . end|rend|cend|crend ( )", iteratorId) && container && tok2->tokAt(2)->varId() != container->declarationId()) { iteratorsError(tok2, container->name(), tok2->strAt(2)); tok2 = tok2->tokAt(6); } // Is the iterator used in a insert/erase operation? - else if (Token::Match(tok2, "%var% . insert|erase ( *| %varid% )|,", iteratorId)) { + else if (Token::Match(tok2, "%name% . insert|erase ( *| %varid% )|,", iteratorId)) { const Token* itTok = tok2->tokAt(4); if (itTok->str() == "*") { if (tok2->strAt(2) == "insert") @@ -163,7 +163,7 @@ void CheckStl::iterators() // it = foo.erase(.. // taking the result of an erase is ok - else if (Token::Match(tok2, "%varid% = %var% .", iteratorId) && + else if (Token::Match(tok2, "%varid% = %name% .", iteratorId) && Token::simpleMatch(skipMembers(tok2->tokAt(2)), "erase (")) { // the returned iterator is valid validatingToken = tok2->linkAt(5); @@ -171,7 +171,7 @@ void CheckStl::iterators() } // Reassign the iterator - else if (Token::Match(tok2, "%varid% = %var% . begin|rbegin|cbegin|crbegin|find (", iteratorId)) { + else if (Token::Match(tok2, "%varid% = %name% . begin|rbegin|cbegin|crbegin|find (", iteratorId)) { validatingToken = tok2->linkAt(5); container = tok2->tokAt(2)->variable(); containerAssignScope = tok2->scope(); @@ -199,7 +199,7 @@ void CheckStl::iterators() else if (!validIterator && Token::Match(tok2, "* %varid%", iteratorId)) { dereferenceErasedError(eraseToken, tok2, tok2->strAt(1)); tok2 = tok2->next(); - } else if (!validIterator && Token::Match(tok2, "%varid% . %var%", iteratorId)) { + } else if (!validIterator && Token::Match(tok2, "%varid% . %name%", iteratorId)) { dereferenceErasedError(eraseToken, tok2, tok2->str()); tok2 = tok2->tokAt(2); } @@ -253,8 +253,8 @@ void CheckStl::mismatchingContainers() static const std::string iteratorBeginFuncPattern = "begin|cbegin|rbegin|crbegin"; static const std::string iteratorEndFuncPattern = "end|cend|rend|crend"; - static const std::string pattern1x1_1 = "%var% . " + iteratorBeginFuncPattern + " ( ) , "; - static const std::string pattern1x1_2 = "%var% . " + iteratorEndFuncPattern + " ( ) ,|)"; + static const std::string pattern1x1_1 = "%name% . " + iteratorBeginFuncPattern + " ( ) , "; + static const std::string pattern1x1_2 = "%name% . " + iteratorEndFuncPattern + " ( ) ,|)"; static const std::string pattern2 = pattern1x1_1 + pattern1x1_2; // Check if different containers are used in various calls of standard functions @@ -319,7 +319,7 @@ void CheckStl::stlOutOfBounds() tok = tok->next(); // check if the for loop condition is wrong - if (Token::Match(tok, "%var% <= %var% . %var% ( ) ;|)|%oror%")) { + if (Token::Match(tok, "%var% <= %var% . %name% ( ) ;|)|%oror%")) { // Is it a vector? const Variable *var = tok->tokAt(2)->variable(); if (!var) @@ -341,12 +341,12 @@ void CheckStl::stlOutOfBounds() for (const Token *tok3 = i->classStart; tok3 && tok3 != i->classEnd; tok3 = tok3->next()) { if (tok3->varId() == declarationId) { tok3 = tok3->next(); - if (Token::Match(tok3, ". %var% ( )")) { + if (Token::Match(tok3, ". %name% ( )")) { if (container->getYield(tok3->strAt(1)) == Library::Container::SIZE) break; } else if (container->arrayLike_indexOp && Token::Match(tok3, "[ %varid% ]", numId)) stlOutOfBoundsError(tok3, tok3->strAt(1), var->name(), false); - else if (Token::Match(tok3, ". %var% ( %varid% )", numId)) { + else if (Token::Match(tok3, ". %name% ( %varid% )", numId)) { Library::Container::Yield yield = container->getYield(tok3->strAt(1)); if (yield == Library::Container::AT_INDEX) stlOutOfBoundsError(tok3, tok3->strAt(3), var->name(), true); @@ -383,7 +383,7 @@ public: break; // reassigning iterator in loop head - else if (Token::Match(tok, "%var% =") && tok->str() == it->str()) + else if (Token::Match(tok, "%name% =") && tok->str() == it->str()) break; } @@ -440,7 +440,7 @@ private: } // erasing iterator => it is invalidated - if (Token::Match(&tok, "erase ( ++|--| %var% )")) { + if (Token::Match(&tok, "erase ( ++|--| %name% )")) { // check if there is a "it = ints.erase(it);" pattern. if so // the it is not invalidated. const Token *token = &tok; @@ -528,7 +528,7 @@ void CheckStl::erase() break; } - if (Token::Match(tok2, "%var% = %var% . begin|rbegin|cbegin|crbegin ( ) ; %var% != %var% . end|rend|cend|crend ( )") && + if (Token::Match(tok2, "%name% = %name% . begin|rbegin|cbegin|crbegin ( ) ; %name% != %name% . end|rend|cend|crend ( )") && tok2->str() == tok2->strAt(8) && tok2->strAt(2) == tok2->strAt(10)) { EraseCheckLoop::checkScope(this, tok2); @@ -557,13 +557,9 @@ void CheckStl::pushback() if (Token::Match(tok, "%var% = & %var% [")) { // Variable id for pointer const unsigned int pointerId(tok->varId()); - if (pointerId == 0) - continue; // Variable id for the container variable const unsigned int containerId(tok->tokAt(3)->varId()); - if (containerId == 0) - continue; bool invalidPointer = false; const Token* function = nullptr; @@ -621,8 +617,6 @@ void CheckStl::pushback() if (Token::Match(tok2, "%varid% = %var% . begin|rbegin|cbegin|crbegin ( ) ; %varid% != %var% . end|rend|cend|crend ( ) ; ++| %varid% ++| ) {", iteratorId)) { // variable id for the loop iterator const unsigned int varId(tok2->tokAt(2)->varId()); - if (varId == 0) - continue; const Token *pushbackTok = nullptr; @@ -720,8 +714,6 @@ void CheckStl::stlBoundaries() if (Token::Match(tok, "> :: iterator|const_iterator %var% =|;")) { const unsigned int iteratorid(tok->tokAt(3)->varId()); - if (iteratorid == 0) - continue; // Using "iterator < ..." is not allowed const Token* const end = tok->scope()->classEnd; @@ -785,7 +777,7 @@ void CheckStl::if_find() const Token* funcTok = nullptr; const Library::Container* container = nullptr; - if (tok->variable() && Token::Match(tok, "%var% . %var% (")) { + if (tok->variable() && Token::Match(tok, "%var% . %name% (")) { container = _settings->library.detectContainer(tok->variable()->typeStartToken()); funcTok = tok->tokAt(2); } @@ -794,7 +786,7 @@ void CheckStl::if_find() else if (tok->variable() && tok->astParent() && (tok->astParent()->str() == "*" || tok->astParent()->str() == "[")) { const Token *tok2 = tok->astParent(); - if (!Token::Match(tok2->astParent(), ". %var% (")) + if (!Token::Match(tok2->astParent(), ". %name% (")) continue; funcTok = tok2->astParent()->next(); @@ -879,7 +871,7 @@ void CheckStl::size() const Scope * scope = symbolDatabase->functionScopes[i]; for (const Token* tok = scope->classStart->next(); tok != scope->classEnd; tok = tok->next()) { if (Token::Match(tok, "%var% . size ( )") || - Token::Match(tok, "%var% . %var% . size ( )")) { + Token::Match(tok, "%name% . %var% . size ( )")) { const Token *tok1 = tok; // get the variable @@ -888,27 +880,25 @@ void CheckStl::size() const Token* const end = tok1->tokAt(5); - if (tok1->varId()) { - // check for comparison to zero - if ((tok->previous() && !tok->previous()->isArithmeticalOp() && Token::Match(end, "==|<=|!=|> 0")) || - (end->next() && !end->next()->isArithmeticalOp() && Token::Match(tok->tokAt(-2), "0 ==|>=|!=|<"))) { - if (isContainerSizeSlow(tok1)) - sizeError(tok1); - } + // check for comparison to zero + if ((tok->previous() && !tok->previous()->isArithmeticalOp() && Token::Match(end, "==|<=|!=|> 0")) || + (end->next() && !end->next()->isArithmeticalOp() && Token::Match(tok->tokAt(-2), "0 ==|>=|!=|<"))) { + if (isContainerSizeSlow(tok1)) + sizeError(tok1); + } - // check for comparison to one - if ((tok->previous() && !tok->previous()->isArithmeticalOp() && Token::Match(end, ">=|< 1") && !end->tokAt(2)->isArithmeticalOp()) || - (end->next() && !end->next()->isArithmeticalOp() && Token::Match(tok->tokAt(-2), "1 <=|>") && !tok->tokAt(-3)->isArithmeticalOp())) { - if (isContainerSizeSlow(tok1)) - sizeError(tok1); - } + // check for comparison to one + if ((tok->previous() && !tok->previous()->isArithmeticalOp() && Token::Match(end, ">=|< 1") && !end->tokAt(2)->isArithmeticalOp()) || + (end->next() && !end->next()->isArithmeticalOp() && Token::Match(tok->tokAt(-2), "1 <=|>") && !tok->tokAt(-3)->isArithmeticalOp())) { + if (isContainerSizeSlow(tok1)) + sizeError(tok1); + } - // check for using as boolean expression - else if ((Token::Match(tok->tokAt(-2), "if|while (") && end->str() == ")") || - (tok->previous()->type() == Token::eLogicalOp && Token::Match(end, "&&|)|,|;|%oror%"))) { - if (isContainerSizeSlow(tok1)) - sizeError(tok1); - } + // check for using as boolean expression + else if ((Token::Match(tok->tokAt(-2), "if|while (") && end->str() == ")") || + (tok->previous()->type() == Token::eLogicalOp && Token::Match(end, "&&|)|,|;|%oror%"))) { + if (isContainerSizeSlow(tok1)) + sizeError(tok1); } } } @@ -935,17 +925,17 @@ void CheckStl::redundantCondition() continue; const Token* tok = i->classDef->tokAt(2); - if (!Token::Match(tok, "%var% . find ( %any% ) != %var% . end|rend|cend|crend ( ) ) { %var% . remove|erase ( %any% ) ;")) + if (!Token::Match(tok, "%name% . find ( %any% ) != %name% . end|rend|cend|crend ( ) ) { %name% . remove|erase ( %any% ) ;")) continue; - // Get tokens for the fields %var% and %any% + // Get tokens for the fields %name% and %any% const Token *var1 = tok; const Token *any1 = var1->tokAt(4); const Token *var2 = any1->tokAt(3); const Token *var3 = var2->tokAt(7); const Token *any2 = var3->tokAt(4); - // Check if all the "%var%" fields are the same and if all the "%any%" are the same.. + // Check if all the "%name%" fields are the same and if all the "%any%" are the same.. if (var1->str() == var2->str() && var2->str() == var3->str() && any1->str() == any2->str()) { @@ -977,7 +967,7 @@ void CheckStl::missingComparison() if (tok2->str() == ";") break; - if (!Token::Match(tok2, "%var% = %var% . begin|rbegin|cbegin|crbegin ( ) ; %var% != %var% . end|rend|cend|crend ( ) ; ++| %var% ++| ) {")) + if (!Token::Match(tok2, "%var% = %name% . begin|rbegin|cbegin|crbegin ( ) ; %name% != %name% . end|rend|cend|crend ( ) ; ++| %name% ++| ) {")) continue; // same container @@ -985,8 +975,6 @@ void CheckStl::missingComparison() break; const unsigned int iteratorId(tok2->varId()); - if (iteratorId == 0) - break; // same iterator if (iteratorId == tok2->tokAt(10)->varId()) @@ -1010,7 +998,7 @@ void CheckStl::missingComparison() incrementToken = 0; else if (tok3->str() == "break" || tok3->str() == "return") incrementToken = 0; - else if (Token::Match(tok3, "%varid% = %var% . insert ( ++| %varid% ++| ,", iteratorId)) { + else if (Token::Match(tok3, "%varid% = %name% . insert ( ++| %varid% ++| ,", iteratorId)) { // skip insertion.. tok3 = tok3->linkAt(6); if (!tok3) @@ -1104,18 +1092,18 @@ void CheckStl::string_c_str() if (Token::Match(tok, "throw %var% . c_str|data ( ) ;") && isLocal(tok->next()) && tok->next()->variable() && tok->next()->variable()->isStlType(stl_string)) { string_c_strThrowError(tok); - } else if (Token::Match(tok, "[;{}] %var% = %var% . str ( ) . c_str|data ( ) ;")) { + } else if (Token::Match(tok, "[;{}] %name% = %var% . str ( ) . c_str|data ( ) ;")) { const Variable* var = tok->next()->variable(); const Variable* var2 = tok->tokAt(3)->variable(); if (var && var->isPointer() && var2 && var2->isStlType(stl_string_stream)) string_c_strError(tok); - } else if (Token::Match(tok, "[;{}] %var% = %var% (") && + } else if (Token::Match(tok, "[;{}] %var% = %name% (") && Token::Match(tok->linkAt(4), ") . c_str|data ( ) ;") && - tok->tokAt(3)->function() && Token::Match(tok->tokAt(3)->function()->retDef, "std :: string|wstring %var%")) { + tok->tokAt(3)->function() && Token::Match(tok->tokAt(3)->function()->retDef, "std :: string|wstring %name%")) { const Variable* var = tok->next()->variable(); if (var && var->isPointer()) string_c_strError(tok); - } else if (performance && Token::Match(tok, "%var% ( !!)") && c_strFuncParam.find(tok->str()) != c_strFuncParam.end() && + } else if (performance && Token::Match(tok, "%name% ( !!)") && c_strFuncParam.find(tok->str()) != c_strFuncParam.end() && !Token::Match(tok->previous(), "::|.") && tok->varId() == 0 && tok->str() != scope->className) { // calling function. TODO: Add support for member functions std::pair::const_iterator, std::multimap::const_iterator> range = c_strFuncParam.equal_range(tok->str()); for (std::multimap::const_iterator i = range.first; i != range.second; ++i) { @@ -1138,7 +1126,7 @@ void CheckStl::string_c_str() const Variable* var = tok2->tokAt(-5)->variable(); if (var && var->isStlType(stl_string)) { string_c_strParam(tok, i->second); - } else if (Token::Match(tok2->tokAt(-9), "%var% . str ( )")) { // Check ss.str().c_str() as parameter + } else if (Token::Match(tok2->tokAt(-9), "%name% . str ( )")) { // Check ss.str().c_str() as parameter const Variable* ssVar = tok2->tokAt(-9)->variable(); if (ssVar && ssVar->isStlType(stl_string_stream)) string_c_strParam(tok, i->second); @@ -1159,7 +1147,7 @@ void CheckStl::string_c_str() } else if (Token::Match(tok, "return std :: string|wstring (") && Token::Match(tok->linkAt(4), ") . c_str|data ( ) ;")) { string_c_strError(tok); - } else if (Token::Match(tok, "return %var% (") && Token::Match(tok->linkAt(2), ") . c_str|data ( ) ;")) { + } else if (Token::Match(tok, "return %name% (") && Token::Match(tok->linkAt(2), ") . c_str|data ( ) ;")) { const Function* func = tok->next()->function(); if (func && Token::Match(func->tokenDef->tokAt(-3), "std :: string|wstring")) string_c_strError(tok); @@ -1254,7 +1242,7 @@ void CheckStl::checkAutoPointer() } else { const Token *tok2 = tok->linkAt(1); - if (Token::Match(tok2, "> %var%")) { + if (Token::Match(tok2, "> %name%")) { const Token *tok3 = tok2->tokAt(2); if (Token::Match(tok3, "( new %type%") && hasArrayEndParen(tok3)) { autoPointerArrayError(tok2->next()); @@ -1279,7 +1267,7 @@ void CheckStl::checkAutoPointer() } } } else { - if (Token::Match(tok, "%var% = %var% ;")) { + if (Token::Match(tok, "%name% = %var% ;")) { if (_settings->isEnabled("style")) { std::set::const_iterator iter = autoPtrVarId.find(tok->tokAt(2)->varId()); if (iter != autoPtrVarId.end()) { @@ -1346,10 +1334,10 @@ void CheckStl::uselessCalls() for (std::size_t i = 0; i < functions; ++i) { const Scope * scope = symbolDatabase->functionScopes[i]; for (const Token* tok = scope->classStart; tok != scope->classEnd; tok = tok->next()) { - if (warning && tok->varId() && Token::Match(tok, "%var% . compare|find|rfind|find_first_not_of|find_first_of|find_last_not_of|find_last_of ( %var% [,)]") && + if (warning && Token::Match(tok, "%var% . compare|find|rfind|find_first_not_of|find_first_of|find_last_not_of|find_last_of ( %name% [,)]") && tok->varId() == tok->tokAt(4)->varId()) { uselessCallsReturnValueError(tok->tokAt(4), tok->str(), tok->strAt(2)); - } else if (performance && tok->varId() && Token::Match(tok, "%var% . swap ( %var% )") && + } else if (performance && Token::Match(tok, "%var% . swap ( %name% )") && tok->varId() == tok->tokAt(4)->varId()) { uselessCallsSwapError(tok, tok->str()); } else if (performance && Token::Match(tok, "%var% . substr (") && @@ -1454,17 +1442,15 @@ void CheckStl::checkDereferenceInvalidIterator() const Token* validityCheckTok = 0; if (!isOrExpression && isAndExpression) { validityCheckTok = - Token::findmatch(startOfCondition, "&& %var% != %var% . end|rend|cend|crend ( )", endOfCondition); + Token::findmatch(startOfCondition, "&& %var% != %name% . end|rend|cend|crend ( )", endOfCondition); } else if (isOrExpression && !isAndExpression) { validityCheckTok = - Token::findmatch(startOfCondition, "%oror% %var% == %var% . end|rend|cend|crend ( )", endOfCondition); + Token::findmatch(startOfCondition, "%oror% %var% == %name% . end|rend|cend|crend ( )", endOfCondition); } if (!validityCheckTok) continue; const unsigned int iteratorVarId = validityCheckTok->next()->varId(); - if (!iteratorVarId) - continue; // If the iterator dereference is to the left of the check for // the iterator's validity, report an error. @@ -1526,7 +1512,7 @@ void CheckStl::readingEmptyStlContainer() bool insert = false; if (var->nameToken() == tok && var->isLocal() && !var->isStatic()) { // Local variable declared insert = !Token::Match(tok->tokAt(1), "[(=]"); // Only if not initialized - } else if (Token::Match(tok, "%var% . clear ( ) ;")) { + } else if (Token::Match(tok, "%name% . clear ( ) ;")) { insert = true; } @@ -1550,7 +1536,7 @@ void CheckStl::readingEmptyStlContainer() empty_map.erase(tok->varId()); else empty_nonmap.erase(tok->varId()); - } else if (Token::Match(tok, "%var% [")) { + } else if (Token::Match(tok, "%name% [")) { // Access through operator[] if (map) { // operator[] inserts an element, if used on a std::map if (tok->strAt(-1) == "=") @@ -1558,7 +1544,7 @@ void CheckStl::readingEmptyStlContainer() empty_map.erase(tok->varId()); } else readingEmptyStlContainerError(tok); - } else if (Token::Match(tok, "%var% . %type% (")) { + } else if (Token::Match(tok, "%name% . %type% (")) { // Member function call if (Token::Match(tok->tokAt(2), "find|at|data|c_str|back|front|empty|top|size|count")) // These functions read from the container readingEmptyStlContainerError(tok); diff --git a/lib/checkstring.cpp b/lib/checkstring.cpp index c9dd8760b..936dce4ab 100644 --- a/lib/checkstring.cpp +++ b/lib/checkstring.cpp @@ -45,13 +45,13 @@ void CheckString::checkAlwaysTrueOrFalseStringCompare() const std::string &str2 = tok->strAt(4); alwaysTrueFalseStringCompareError(tok, str1, str2); tok = tok->tokAt(5); - } else if (Token::Match(tok->tokAt(2), "%var% , %var% ,|)")) { + } else if (Token::Match(tok->tokAt(2), "%name% , %name% ,|)")) { const std::string &str1 = tok->strAt(2); const std::string &str2 = tok->strAt(4); if (str1 == str2) alwaysTrueStringVariableCompareError(tok, str1, str2); tok = tok->tokAt(5); - } else if (Token::Match(tok->tokAt(2), "%var% . c_str ( ) , %var% . c_str ( ) ,|)")) { + } else if (Token::Match(tok->tokAt(2), "%name% . c_str ( ) , %name% . c_str ( ) ,|)")) { const std::string &str1 = tok->strAt(2); const std::string &str2 = tok->strAt(8); if (str1 == str2) @@ -218,7 +218,7 @@ void CheckString::checkIncorrectStringCompare() for (const Token* tok = scope->classStart->next(); tok != scope->classEnd; tok = tok->next()) { // skip "assert(str && ..)" and "assert(.. && str)" if (tok->str().size() >= 6U && (tok->str().find("assert") == tok->str().size() - 6U || tok->str().find("ASSERT") == tok->str().size() - 6U) && - Token::Match(tok, "%var% (") && + Token::Match(tok, "%name% (") && (Token::Match(tok->tokAt(2), "%str% &&") || Token::Match(tok->next()->link()->tokAt(-2), "&& %str% )"))) tok = tok->next()->link(); @@ -281,10 +281,10 @@ void CheckString::sprintfOverlappingData() if (Token::Match(tok, "sprintf|snprintf|swprintf ( %var% ,")) varid = tok->tokAt(2)->varId(); - else if (Token::Match(tok, "sprintf|snprintf|swprintf ( %var% . %var% ,")) + else if (Token::Match(tok, "sprintf|snprintf|swprintf ( %name% . %var% ,")) varid = tok->tokAt(4)->varId(); - if (varid == 0) + else continue; // goto next argument diff --git a/lib/checkuninitvar.cpp b/lib/checkuninitvar.cpp index f717b04a2..99a23e583 100644 --- a/lib/checkuninitvar.cpp +++ b/lib/checkuninitvar.cpp @@ -369,9 +369,9 @@ private: while (nullptr != (tok2 = tok2->next())) { if (Token::Match(tok2, "[;)=]")) break; - if (Token::Match(tok2, "%var% (")) + if (Token::Match(tok2, "%name% (")) break; - if (Token::Match(tok2, "%var% <") && Token::simpleMatch(tok2->linkAt(1), "> (")) + if (Token::Match(tok2, "%name% <") && Token::simpleMatch(tok2->linkAt(1), "> (")) break; if (tok2->varId() && !Token::Match(tok2->previous(), "&|::") && @@ -381,7 +381,7 @@ private: if (Token::Match(tok2->next(), ".|[")) { const Token * tok3 = tok2; while (tok3) { - if (Token::Match(tok3->next(), ". %var%")) + if (Token::Match(tok3->next(), ". %name%")) tok3 = tok3->tokAt(2); else if (tok3->strAt(1) == "[") tok3 = tok3->next()->link(); @@ -409,7 +409,7 @@ private: /** parse tokens. @sa ExecutionPath::parse */ const Token *parse(const Token &tok, std::list &checks) const { // Variable declaration.. - if (tok.varId() && Token::Match(&tok, "%var% [[;]")) { + if (Token::Match(&tok, "%var% [[;]")) { const Variable* var2 = tok.variable(); if (var2 && var2->nameToken() == &tok && !var2->isStatic() && !var2->isExtern() && !var2->isConst() && !Token::simpleMatch(tok.linkAt(1), "] [")) { if (tok.linkAt(1)) { // array @@ -504,9 +504,9 @@ private: return &tok; } - if (Token::Match(tok.previous(), "[;{}] %var% [=[.]")) { + if (Token::Match(tok.previous(), "[;{}] %name% [=[.]")) { if (tok.next()->str() == ".") { - if (Token::Match(&tok, "%var% . %var% (")) { + if (Token::Match(&tok, "%name% . %name% (")) { const Function *function = tok.tokAt(2)->function(); if (function && function->isStatic()) return &tok; @@ -540,7 +540,7 @@ private: } // pointer aliasing? - if (Token::Match(tok.tokAt(2), "%var% ;")) { + if (Token::Match(tok.tokAt(2), "%name% ;")) { pointer_assignment(checks, &tok, tok.tokAt(2)); } } @@ -572,13 +572,13 @@ private: } if (Token::Match(tok.next(), "= malloc|kmalloc") || Token::simpleMatch(tok.next(), "= new char [") || - (Token::Match(tok.next(), "= %var% (") && library->returnuninitdata.find(tok.strAt(2)) != library->returnuninitdata.end())) { + (Token::Match(tok.next(), "= %name% (") && library->returnuninitdata.find(tok.strAt(2)) != library->returnuninitdata.end())) { alloc_pointer(checks, tok.varId()); if (tok.strAt(3) == "(") return tok.tokAt(3); } - else if ((!isC && (Token::Match(tok.previous(), "<<|>>") || Token::Match(tok.previous(), "[;{}] %var% <<"))) || + else if ((!isC && (Token::Match(tok.previous(), "<<|>>") || Token::Match(tok.previous(), "[;{}] %name% <<"))) || tok.strAt(1) == "=") { // TODO: Don't bail out for "<<" and ">>" if these are // just computations @@ -601,7 +601,7 @@ private: } } - if (Token::Match(&tok, "%var% (")) { + if (Token::Match(&tok, "%name% (")) { // sizeof/typeof doesn't dereference. A function name that is all uppercase // might be an unexpanded macro that uses sizeof/typeof if (Token::Match(&tok, "sizeof|typeof (")) @@ -609,7 +609,7 @@ private: // deallocate pointer if (Token::Match(&tok, "free|kfree|fclose ( %var% )") || - Token::Match(&tok, "realloc ( %var%")) { + Token::Match(&tok, "realloc ( %name%")) { dealloc_pointer(checks, tok.tokAt(2)); if (tok.str() == "realloc") ExecutionPath::bailOutVar(checks, tok.tokAt(2)->varId()); @@ -648,7 +648,7 @@ private: } // strncpy doesn't null-terminate first parameter - if (Token::Match(&tok, "strncpy ( %var% ,")) { + if (Token::Match(&tok, "strncpy ( %name% ,")) { if (Token::Match(tok.tokAt(4), "%str% ,")) { if (Token::Match(tok.tokAt(6), "%num% )")) { const std::size_t len = Token::getStrLength(tok.tokAt(4)); @@ -665,7 +665,7 @@ private: } // memset (not zero terminated).. - if (Token::Match(&tok, "memset ( %var% , !!0 , %num% )")) { + if (Token::Match(&tok, "memset ( %name% , !!0 , %num% )")) { init_memset_nonzero(checks, tok.tokAt(2)); return tok.next()->link(); } @@ -702,7 +702,7 @@ private: } else if (tok2->varId()) { - if (Token::Match(tok2->tokAt(-2), "[(,] *") || Token::Match(tok2->next(), ". %var%")) { + if (Token::Match(tok2->tokAt(-2), "[(,] *") || Token::Match(tok2->next(), ". %name%")) { // find function call.. const Token *functionCall = tok2; while (nullptr != (functionCall = functionCall ? functionCall->previous() : 0)) { @@ -743,8 +743,8 @@ private: } // function call via function pointer - if (Token::Match(&tok, "( * %var% ) (") || - (Token::Match(&tok, "( *| %var% .|::") && Token::Match(tok.link()->tokAt(-2), ".|:: %var% ) ("))) { + if (Token::Match(&tok, "( * %name% ) (") || + (Token::Match(&tok, "( *| %name% .|::") && Token::Match(tok.link()->tokAt(-2), ".|:: %name% ) ("))) { // is the variable passed as a parameter to some function? const Token *tok2 = tok.link()->next(); for (const Token* const end2 = tok2->link(); tok2 != end2; tok2 = tok2->next()) { @@ -757,9 +757,9 @@ private: if (tok.str() == "return") { // Todo: if (!array && .. - if (Token::Match(tok.next(), "%var% ;")) { + if (Token::Match(tok.next(), "%name% ;")) { use(checks, tok.next()); - } else if (Token::Match(tok.next(), "%var% [")) { + } else if (Token::Match(tok.next(), "%name% [")) { use_array_or_pointer_data(checks, tok.next()); } } @@ -771,8 +771,8 @@ private: return &tok; } - if (!Token::Match(tok.tokAt(-3), ". %var% =")) { - if (!Token::Match(tok.tokAt(-3), "[;{}] %var% =")) { + if (!Token::Match(tok.tokAt(-3), ". %name% =")) { + if (!Token::Match(tok.tokAt(-3), "[;{}] %name% =")) { use(checks, &tok); return &tok; } @@ -874,13 +874,13 @@ private: } bool parseCondition(const Token &tok, std::list &checks) { - if (tok.varId() && Token::Match(&tok, "%var% <|<=|==|!=|)")) + if (tok.varId() && Token::Match(&tok, "%name% <|<=|==|!=|)")) use(checks, &tok); - else if (Token::Match(&tok, "!| %var% [") && !Token::simpleMatch(skipBrackets(tok.next()), "=")) + else if (Token::Match(&tok, "!| %name% [") && !Token::simpleMatch(skipBrackets(tok.next()), "=")) use_array_or_pointer_data(checks, tok.str() == "!" ? tok.next() : &tok); - else if (Token::Match(&tok, "!| %var% (")) { + else if (Token::Match(&tok, "!| %name% (")) { const Token * const ftok = (tok.str() == "!") ? tok.next() : &tok; std::list var1; CheckNullPointer::parseFunctionCall(*ftok, var1, library, 1); @@ -893,7 +893,7 @@ private: } } - else if (Token::Match(&tok, "! %var% )")) { + else if (Token::Match(&tok, "! %name% )")) { use(checks, &tok); return false; } @@ -926,7 +926,7 @@ public: tok = tok->link(); continue; } - if (tok->str() != "::" && Token::Match(tok->next(), "%var% ( %type%")) { + if (tok->str() != "::" && Token::Match(tok->next(), "%name% ( %type%")) { if (!Token::Match(tok->linkAt(2), ") [{;]")) continue; const Token *tok2 = tok->tokAt(3); @@ -934,12 +934,12 @@ public: if (tok2->str() == ",") tok2 = tok2->next(); - if (Token::Match(tok2, "%type% %var% ,|)") && tok2->isStandardType()) { + if (Token::Match(tok2, "%type% %name% ,|)") && tok2->isStandardType()) { tok2 = tok2->tokAt(2); continue; } - if (tok2->isStandardType() && Token::Match(tok2, "%type% & %var% ,|)")) { + if (tok2->isStandardType() && Token::Match(tok2, "%type% & %name% ,|)")) { const unsigned int varid(tok2->tokAt(2)->varId()); // flags for read/write @@ -976,21 +976,21 @@ public: continue; } - if (Token::Match(tok2, "const %type% &|*| const| %var% ,|)") && tok2->next()->isStandardType()) { + if (Token::Match(tok2, "const %type% &|*| const| %name% ,|)") && tok2->next()->isStandardType()) { tok2 = tok2->tokAt(3); while (tok2->isName()) tok2 = tok2->next(); continue; } - if (Token::Match(tok2, "const %type% %var% [ ] ,|)") && tok2->next()->isStandardType()) { + if (Token::Match(tok2, "const %type% %name% [ ] ,|)") && tok2->next()->isStandardType()) { tok2 = tok2->tokAt(5); continue; } /// @todo enable this code. if pointer is written in function then dead pointer is invalid but valid pointer is ok. /* - if (Token::Match(tok2, "const| struct| %type% * %var% ,|)")) + if (Token::Match(tok2, "const| struct| %type% * %name% ,|)")) { while (tok2->isName()) tok2 = tok2->next(); @@ -1068,11 +1068,11 @@ void CheckUninitVar::checkScope(const Scope* scope) if (i->nameToken()->strAt(1) == "(" || i->nameToken()->strAt(1) == "{") continue; - if (Token::Match(i->nameToken(), "%var% =")) { // Variable is initialized, but Rhs might be not + if (Token::Match(i->nameToken(), "%name% =")) { // Variable is initialized, but Rhs might be not checkRhs(i->nameToken(), *i, NO_ALLOC, ""); continue; } - if (Token::Match(i->nameToken(), "%var% ) (") && Token::simpleMatch(i->nameToken()->linkAt(2), ") =")) { // Function pointer is initialized, but Rhs might be not + if (Token::Match(i->nameToken(), "%name% ) (") && Token::simpleMatch(i->nameToken()->linkAt(2), ") =")) { // Function pointer is initialized, but Rhs might be not checkRhs(i->nameToken()->linkAt(2)->next(), *i, NO_ALLOC, ""); continue; } @@ -1100,10 +1100,10 @@ void CheckUninitVar::checkScope(const Scope* scope) if (scope->function) { for (unsigned int i = 0; i < scope->function->argCount(); i++) { const Variable *arg = scope->function->getArgumentVar(i); - if (arg && arg->declarationId() && Token::Match(arg->typeStartToken(), "struct| %type% * %var% [,)]")) { + if (arg && arg->declarationId() && Token::Match(arg->typeStartToken(), "struct| %type% * %name% [,)]")) { // Treat the pointer as initialized until it is assigned by malloc for (const Token *tok = scope->classStart; tok != scope->classEnd; tok = tok->next()) { - if (Token::Match(tok, "[;{}] %varid% = %var% (", arg->declarationId()) && + if (Token::Match(tok, "[;{}] %varid% = %name% (", arg->declarationId()) && _settings->library.returnuninitdata.count(tok->strAt(3)) == 1U) { if (arg->typeStartToken()->str() == "struct") checkStruct(tok, *arg); @@ -1168,7 +1168,7 @@ static void conditionAlwaysTrueOrFalse(const Token *tok, const std::mapnext(); - while (Token::Match(vartok, "%var% . %var%")) + while (Token::Match(vartok, "%name% . %name%")) vartok = vartok->tokAt(2); std::map::const_iterator it = variableValue.find(vartok->varId()); @@ -1176,26 +1176,26 @@ static void conditionAlwaysTrueOrFalse(const Token *tok, const std::mapsecond == 0); else *alwaysTrue = bool(it->second != 0); - } else if (Token::Match(vartok, "%var% == %num% %or%|)")) { + } else if (Token::Match(vartok, "%name% == %num% %or%|)")) { *alwaysTrue = bool(it->second == MathLib::toLongNumber(vartok->strAt(2))); - } else if (Token::Match(vartok, "%var% != %num% %or%|)")) { + } else if (Token::Match(vartok, "%name% != %num% %or%|)")) { *alwaysTrue = bool(it->second != MathLib::toLongNumber(vartok->strAt(2))); } // always false - if (Token::Match(vartok, "%var% &&|)")) { + if (Token::Match(vartok, "%name% &&|)")) { if (NOT) *alwaysFalse = bool(it->second != 0); else *alwaysFalse = bool(it->second == 0); - } else if (Token::Match(vartok, "%var% == %num% &&|)")) { + } else if (Token::Match(vartok, "%name% == %num% &&|)")) { *alwaysFalse = bool(it->second != MathLib::toLongNumber(vartok->strAt(2))); - } else if (Token::Match(vartok, "%var% != %num% &&|)")) { + } else if (Token::Match(vartok, "%name% != %num% &&|)")) { *alwaysFalse = bool(it->second == MathLib::toLongNumber(vartok->strAt(2))); } } @@ -1241,7 +1241,7 @@ bool CheckUninitVar::checkScopeForVariable(const Token *tok, const Variable& var } // assignment with nonzero constant.. - if (Token::Match(tok->previous(), "[;{}] %var% = - %var% ;") && tok->varId() > 0) + if (Token::Match(tok->previous(), "[;{}] %var% = - %name% ;")) variableValue[tok->varId()] = NOT_ZERO; // Inner scope.. @@ -1257,7 +1257,7 @@ bool CheckUninitVar::checkScopeForVariable(const Token *tok, const Variable& var // checking if a not-zero variable is zero => bail out unsigned int condVarId = 0, condVarValue = 0; - if (Token::Match(tok, "if ( %var% )")) { + if (Token::Match(tok, "if ( %name% )")) { std::map::const_iterator it = variableValue.find(tok->tokAt(2)->varId()); if (it != variableValue.end() && it->second == NOT_ZERO) return true; // this scope is not fully analysed => return true @@ -1300,9 +1300,9 @@ bool CheckUninitVar::checkScopeForVariable(const Token *tok, const Variable& var std::map varValueIf; if (!alwaysFalse && !initif && !noreturnIf) { for (const Token *tok2 = tok; tok2 && tok2 != tok->link(); tok2 = tok2->next()) { - if (Token::Match(tok2, "[;{}.] %var% = - %var% ;")) + if (Token::Match(tok2, "[;{}.] %name% = - %name% ;")) varValueIf[tok2->next()->varId()] = NOT_ZERO; - if (Token::Match(tok2, "[;{}.] %var% = %num% ;")) + if (Token::Match(tok2, "[;{}.] %name% = %num% ;")) varValueIf[tok2->next()->varId()] = (int)MathLib::toLongNumber(tok2->strAt(3)); } } @@ -1330,7 +1330,7 @@ bool CheckUninitVar::checkScopeForVariable(const Token *tok, const Variable& var std::map varValueElse; if (!alwaysTrue && !initelse && !noreturnElse) { for (const Token *tok2 = tok; tok2 && tok2 != tok->link(); tok2 = tok2->next()) { - if (Token::Match(tok2, "[;{}.] %var% = - %var% ;")) + if (Token::Match(tok2, "[;{}.] %var% = - %name% ;")) varValueElse[tok2->next()->varId()] = NOT_ZERO; if (Token::Match(tok2, "[;{}.] %var% = %num% ;")) varValueElse[tok2->next()->varId()] = (int)MathLib::toLongNumber(tok2->strAt(3)); @@ -1426,7 +1426,7 @@ bool CheckUninitVar::checkScopeForVariable(const Token *tok, const Variable& var } // Unknown or unhandled inner scope - else if (Token::simpleMatch(tok, ") {") || (Token::Match(tok, "%var% {") && tok->str() != "try")) { + else if (Token::simpleMatch(tok, ") {") || (Token::Match(tok, "%name% {") && tok->str() != "try")) { if (tok->str() == "struct" || tok->str() == "union") { tok = tok->linkAt(1); continue; @@ -1457,7 +1457,7 @@ bool CheckUninitVar::checkScopeForVariable(const Token *tok, const Variable& var // variable is seen.. if (tok->varId() == var.declarationId()) { if (!membervar.empty()) { - if (Token::Match(tok, "%var% . %var% ;|%cop%") && tok->strAt(2) == membervar) + if (Token::Match(tok, "%name% . %name% ;|%cop%") && tok->strAt(2) == membervar) uninitStructMemberError(tok, tok->str() + "." + membervar); else return true; @@ -1495,7 +1495,7 @@ bool CheckUninitVar::checkScopeForVariable(const Token *tok, const Variable& var if (tok->varId() == var.declarationId()) { // calling function that returns uninit data through pointer.. if (var.isPointer() && - Token::Match(tok->next(), "= %var% (") && + Token::Match(tok->next(), "= %name% (") && Token::simpleMatch(tok->linkAt(3), ") ;") && _settings->library.returnuninitdata.count(tok->strAt(2)) > 0U) { if (alloc) @@ -1521,7 +1521,7 @@ bool CheckUninitVar::checkScopeForVariable(const Token *tok, const Variable& var if (isMemberVariableUsage(tok, var.isPointer(), *alloc, membervar)) uninitStructMemberError(tok, tok->str() + "." + membervar); - else if (Token::Match(tok->previous(), "[(,] %var% [,)]")) + else if (Token::Match(tok->previous(), "[(,] %name% [,)]")) return true; } else { @@ -1550,11 +1550,11 @@ bool CheckUninitVar::checkScopeForVariable(const Token *tok, const Variable& var bool CheckUninitVar::checkIfForWhileHead(const Token *startparentheses, const Variable& var, bool suppressErrors, bool isuninit, Alloc alloc, const std::string &membervar) { const Token * const endpar = startparentheses->link(); - if (Token::Match(startparentheses, "( ! %var% %oror%") && startparentheses->tokAt(2)->getValue(0)) + if (Token::Match(startparentheses, "( ! %name% %oror%") && startparentheses->tokAt(2)->getValue(0)) suppressErrors = true; for (const Token *tok = startparentheses->next(); tok && tok != endpar; tok = tok->next()) { if (tok->varId() == var.declarationId()) { - if (Token::Match(tok, "%var% . %var%")) { + if (Token::Match(tok, "%name% . %name%")) { if (tok->strAt(2) == membervar) { if (isMemberVariableAssignment(tok, membervar)) return true; @@ -1607,12 +1607,12 @@ bool CheckUninitVar::checkLoopBody(const Token *tok, const Variable& var, const return true; } - if (Token::Match(tok, "%var% =")) + if (Token::Match(tok, "%name% =")) return true; if (isMemberVariableUsage(tok, var.isPointer(), alloc, membervar)) usetok = tok; - else if (Token::Match(tok->previous(), "[(,] %var% [,)]")) + else if (Token::Match(tok->previous(), "[(,] %name% [,)]")) return true; } else { if (isVariableUsage(tok, var.isPointer(), alloc)) @@ -1695,22 +1695,22 @@ bool CheckUninitVar::isVariableUsage(const Token *vartok, bool pointer, Alloc al return true; // Passing variable to typeof/__alignof__ - if (Token::Match(vartok->tokAt(-3), "typeof|__alignof__ ( * %var%")) + if (Token::Match(vartok->tokAt(-3), "typeof|__alignof__ ( * %name%")) return false; // Accessing Rvalue member using "." or "->" if (vartok->strAt(1) == "." && vartok->strAt(-1) != "&") { // Is struct member passed to function? - if (!pointer && Token::Match(vartok->previous(), "[,(] %var% . %var%")) { + if (!pointer && Token::Match(vartok->previous(), "[,(] %name% . %name%")) { // TODO: there are FN currently: // - should only return false if struct member is (or might be) array. // - should only return false if function argument is (or might be) non-const pointer or reference const Token *tok2 = vartok->next(); - while (Token::Match(tok2,". %var%")) + while (Token::Match(tok2,". %name%")) tok2 = tok2->tokAt(2); if (Token::Match(tok2, "[,)]")) return false; - } else if (pointer && alloc != CTOR_CALL && Token::Match(vartok, "%var% . %var% (")) { + } else if (pointer && alloc != CTOR_CALL && Token::Match(vartok, "%name% . %name% (")) { return true; } @@ -1734,7 +1734,7 @@ bool CheckUninitVar::isVariableUsage(const Token *vartok, bool pointer, Alloc al } // Passing variable to function.. - if (Token::Match(vartok->previous(), "[(,] %var% [,)]") || Token::Match(vartok->tokAt(-2), "[(,] & %var% [,)]")) { + if (Token::Match(vartok->previous(), "[(,] %name% [,)]") || Token::Match(vartok->tokAt(-2), "[(,] & %name% [,)]")) { // locate start parentheses in function call.. unsigned int argumentNumber = 0; const Token *start = vartok; @@ -1747,7 +1747,7 @@ bool CheckUninitVar::isVariableUsage(const Token *vartok, bool pointer, Alloc al } // is this a function call? - if (start && Token::Match(start->previous(), "%var% (")) { + if (start && Token::Match(start->previous(), "%name% (")) { const bool address(vartok->previous()->str() == "&"); // check how function handle uninitialized data arguments.. const Function *func = start->previous()->function(); @@ -1757,17 +1757,17 @@ bool CheckUninitVar::isVariableUsage(const Token *vartok, bool pointer, Alloc al const Token *argStart = arg->typeStartToken(); if (!address && Token::Match(argStart, "struct| %type% [,)]")) return true; - if (!address && Token::Match(argStart, "struct| %type% %var% [,)]")) + if (!address && Token::Match(argStart, "struct| %type% %name% [,)]")) return true; - if (pointer && !address && alloc == NO_ALLOC && Token::Match(argStart, "struct| %type% * %var% [,)]")) + if (pointer && !address && alloc == NO_ALLOC && Token::Match(argStart, "struct| %type% * %name% [,)]")) return true; while (argStart->previous() && argStart->previous()->isName()) argStart = argStart->previous(); - if (Token::Match(argStart, "const %type% & %var% [,)]")) + if (Token::Match(argStart, "const %type% & %name% [,)]")) return true; - if ((pointer || address) && alloc == NO_ALLOC && Token::Match(argStart, "const struct| %type% * %var% [,)]")) + if ((pointer || address) && alloc == NO_ALLOC && Token::Match(argStart, "const struct| %type% * %name% [,)]")) return true; - if ((pointer || address) && Token::Match(argStart, "const %type% %var% [") && Token::Match(argStart->linkAt(3), "] [,)]")) + if ((pointer || address) && Token::Match(argStart, "const %type% %name% [") && Token::Match(argStart->linkAt(3), "] [,)]")) return true; } @@ -1822,10 +1822,10 @@ bool CheckUninitVar::isVariableUsage(const Token *vartok, bool pointer, Alloc al } } - if (alloc == NO_ALLOC && Token::Match(vartok->previous(), "= %var% ;|%cop%")) + if (alloc == NO_ALLOC && Token::Match(vartok->previous(), "= %name% ;|%cop%")) return true; - if (Token::Match(vartok->previous(), "? %var%")) { + if (Token::Match(vartok->previous(), "? %name%")) { // this is only variable usage if variable is either: // * unconditionally uninitialized // * used in both rhs and lhs of ':' operator @@ -1850,7 +1850,7 @@ bool CheckUninitVar::isVariableUsage(const Token *vartok, bool pointer, Alloc al // function parameter? bool functionParameter = false; - if (Token::Match(vartok->tokAt(-2), "%var% (") || vartok->previous()->str() == ",") + if (Token::Match(vartok->tokAt(-2), "%name% (") || vartok->previous()->str() == ",") functionParameter = true; // if this is not a function parameter report this dereference as variable usage @@ -1861,7 +1861,7 @@ bool CheckUninitVar::isVariableUsage(const Token *vartok, bool pointer, Alloc al if (_tokenizer->isCPP() && Token::Match(vartok->next(), "<<|>>")) { // Is this calculation done in rhs? const Token *tok = vartok; - while (tok && Token::Match(tok, "%var%|.|::")) + while (tok && Token::Match(tok, "%name%|.|::")) tok = tok->previous(); if (Token::Match(tok, "[;{}]")) return false; @@ -1883,7 +1883,7 @@ bool CheckUninitVar::isVariableUsage(const Token *vartok, bool pointer, Alloc al bool CheckUninitVar::isMemberVariableAssignment(const Token *tok, const std::string &membervar) { - if (Token::Match(tok, "%var% . %var%") && tok->strAt(2) == membervar) { + if (Token::Match(tok, "%name% . %name%") && tok->strAt(2) == membervar) { if (Token::Match(tok->tokAt(3), "[=.[]")) return true; else if (Token::Match(tok->tokAt(-2), "[(,=] &")) @@ -1897,7 +1897,7 @@ bool CheckUninitVar::isMemberVariableAssignment(const Token *tok, const std::str } else if (tok->strAt(1) == "=") return true; else if (tok->strAt(-1) == "&") { - if (Token::Match(tok->tokAt(-2), "[(,] & %var%")) { + if (Token::Match(tok->tokAt(-2), "[(,] & %name%")) { // locate start parentheses in function call.. unsigned int argumentNumber = 0; const Token *ftok = tok; @@ -1911,14 +1911,14 @@ bool CheckUninitVar::isMemberVariableAssignment(const Token *tok, const std::str // is this a function call? ftok = ftok ? ftok->previous() : NULL; - if (Token::Match(ftok, "%var% (")) { + if (Token::Match(ftok, "%name% (")) { // check how function handle uninitialized data arguments.. const Function *function = ftok->function(); const Variable *arg = function ? function->getArgumentVar(argumentNumber) : NULL; const Token *argStart = arg ? arg->typeStartToken() : NULL; while (argStart && argStart->previous() && argStart->previous()->isName()) argStart = argStart->previous(); - if (Token::Match(argStart, "const struct| %type% * const| %var% [,)]")) + if (Token::Match(argStart, "const struct| %type% * const| %name% [,)]")) return false; } @@ -1935,12 +1935,12 @@ bool CheckUninitVar::isMemberVariableUsage(const Token *tok, bool isPointer, All if (isMemberVariableAssignment(tok, membervar)) return false; - if (Token::Match(tok, "%var% . %var%") && tok->strAt(2) == membervar) + if (Token::Match(tok, "%name% . %name%") && tok->strAt(2) == membervar) return true; - else if (!isPointer && Token::Match(tok->previous(), "[(,] %var% [,)]") && isVariableUsage(tok, isPointer, alloc)) + else if (!isPointer && Token::Match(tok->previous(), "[(,] %name% [,)]") && isVariableUsage(tok, isPointer, alloc)) return true; - else if (!isPointer && Token::Match(tok->previous(), "= %var% ;")) + else if (!isPointer && Token::Match(tok->previous(), "= %name% ;")) return true; // = *(&var); @@ -1953,7 +1953,7 @@ bool CheckUninitVar::isMemberVariableUsage(const Token *tok, bool isPointer, All else if (_settings->experimental && !isPointer && - Token::Match(tok->tokAt(-2), "[(,] & %var% [,)]") && + Token::Match(tok->tokAt(-2), "[(,] & %name% [,)]") && isVariableUsage(tok, isPointer, alloc)) return true; diff --git a/lib/checkunusedfunctions.cpp b/lib/checkunusedfunctions.cpp index 2f3017bcd..bd47dc75c 100644 --- a/lib/checkunusedfunctions.cpp +++ b/lib/checkunusedfunctions.cpp @@ -165,9 +165,9 @@ void CheckUnusedFunctions::parseTokens(const Tokenizer &tokenizer, const char Fi const Token *funcname = nullptr; - if (tok->scope()->isExecutable() && Token::Match(tok, "%var% (")) { + if (tok->scope()->isExecutable() && Token::Match(tok, "%name% (")) { funcname = tok; - } else if (tok->scope()->isExecutable() && Token::Match(tok, "%var% <") && Token::simpleMatch(tok->linkAt(1), "> (")) { + } else if (tok->scope()->isExecutable() && Token::Match(tok, "%name% <") && Token::simpleMatch(tok->linkAt(1), "> (")) { funcname = tok; } else if (Token::Match(tok, "[;{}.,()[=+-/|!?:]")) { funcname = tok->next(); @@ -175,10 +175,10 @@ void CheckUnusedFunctions::parseTokens(const Tokenizer &tokenizer, const char Fi funcname = funcname->next(); if (funcname && funcname->str() == "::") funcname = funcname->next(); - while (Token::Match(funcname, "%var% :: %var%")) + while (Token::Match(funcname, "%name% :: %name%")) funcname = funcname->tokAt(2); - if (!Token::Match(funcname, "%var% [(),;]:}]")) + if (!Token::Match(funcname, "%name% [(),;]:}]")) continue; } @@ -186,7 +186,7 @@ void CheckUnusedFunctions::parseTokens(const Tokenizer &tokenizer, const char Fi continue; // funcname ( => Assert that the end parentheses isn't followed by { - if (Token::Match(funcname, "%var% (|<")) { + if (Token::Match(funcname, "%name% (|<")) { const Token *ftok = funcname->next(); if (ftok->str() == "<") ftok = ftok->link(); diff --git a/lib/checkunusedvar.cpp b/lib/checkunusedvar.cpp index 630b01d8b..59e7a7a06 100644 --- a/lib/checkunusedvar.cpp +++ b/lib/checkunusedvar.cpp @@ -439,8 +439,8 @@ static const Token* doAssignment(Variables &variables, const Token *tok, bool de if (Token::Match(tok, "( [(<] const| struct|union| %type% *| [>)]")) tok = tok->next(); - if (Token::Match(tok, "(| &| %var%") || - Token::Match(tok->next(), "< const| struct|union| %type% *| > ( &| %var%")) { + if (Token::Match(tok, "(| &| %name%") || + Token::Match(tok->next(), "< const| struct|union| %type% *| > ( &| %name%")) { bool addressOf = false; if (Token::Match(tok, "%var% .")) @@ -494,7 +494,7 @@ static const Token* doAssignment(Variables &variables, const Token *tok, bool de } // no cast, no ? - else if (!Token::Match(tok, "%var% ?")) { + else if (!Token::Match(tok, "%name% ?")) { if (tok->str() == "&") { addressOf = true; tok = tok->next(); @@ -598,7 +598,7 @@ static const Token* doAssignment(Variables &variables, const Token *tok, bool de // check for alias to struct member // char c[10]; a.b = c; - else if (Token::Match(tok->tokAt(-2), "%var% .")) { + else if (Token::Match(tok->tokAt(-2), "%name% .")) { if (tok->tokAt(2)->varId()) { unsigned int varid2 = tok->tokAt(2)->varId(); Variables::VariableUsage *var2 = variables.find(varid2); @@ -614,7 +614,7 @@ static const Token* doAssignment(Variables &variables, const Token *tok, bool de } // Possible pointer alias - else if (Token::Match(tok, "%var% = %var% ;")) { + else if (Token::Match(tok, "%name% = %name% ;")) { const unsigned int varid2 = tok->tokAt(2)->varId(); Variables::VariableUsage *var2 = variables.find(varid2); if (var2 && (var2->_type == Variables::array || @@ -655,7 +655,7 @@ static const Token * skipBracketsAndMembers(const Token *tok) while (tok) { if (tok->str() == "[") tok = tok->link()->next(); - else if (Token::Match(tok, ". %var%")) + else if (Token::Match(tok, ". %name%")) tok = tok->tokAt(2); else break; @@ -712,7 +712,7 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const } if (i->isArray() && i->isClass()) // Array of class/struct members. Initialized by ctor. variables.write(i->declarationId(), i->nameToken()); - if (i->isArray() && Token::Match(i->nameToken(), "%var% [ %var% ]")) // Array index variable read. + if (i->isArray() && Token::Match(i->nameToken(), "%name% [ %var% ]")) // Array index variable read. variables.read(i->nameToken()->tokAt(2)->varId(), i->nameToken()); if (defValTok && defValTok->str() == "=") { @@ -767,7 +767,7 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const // bailout when for_each is used - if (Token::Match(tok, "%var% (") && Token::simpleMatch(tok->linkAt(1), ") {") && !Token::Match(tok, "if|for|while|switch")) { + if (Token::Match(tok, "%name% (") && Token::simpleMatch(tok->linkAt(1), ") {") && !Token::Match(tok, "if|for|while|switch")) { // does the name contain "for_each" or "foreach"? std::string nameTok; nameTok.resize(tok->str().size()); @@ -812,7 +812,7 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const const Variable *var = tok2->variable(); if (var && var->nameToken() == tok2) { // Declaration: Skip tok = tok2->next(); - if (Token::Match(tok, "( %var% )")) // Simple initialization through copy ctor + if (Token::Match(tok, "( %name% )")) // Simple initialization through copy ctor tok = tok->next(); else if (Token::Match(tok, "= %var% ;")) { // Simple initialization tok = tok->next(); @@ -861,8 +861,8 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const variables.use(tok->tokAt(2)->varId(), tok); } // assignment - else if (Token::Match(tok, "*| ++|--| %var% ++|--| =") || - Token::Match(tok, "*| ( const| %type% *| ) %var% =")) { + else if (Token::Match(tok, "*| ++|--| %name% ++|--| =") || + Token::Match(tok, "*| ( const| %type% *| ) %name% =")) { bool dereference = false; bool pre = false; bool post = false; @@ -872,7 +872,7 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const tok = tok->next(); } - if (Token::Match(tok, "( const| %type% *| ) %var% =")) + if (Token::Match(tok, "( const| %type% *| ) %name% =")) tok = tok->link()->next(); else if (tok->str() == "(") @@ -910,7 +910,7 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const } // Consider allocating memory separately because allocating/freeing alone does not constitute using the variable else if (var && var->_type == Variables::pointer && - Token::Match(start, "%var% = new|malloc|calloc|kmalloc|kzalloc|kcalloc|strdup|strndup|vmalloc|g_new0|g_try_new|g_new|g_malloc|g_malloc0|g_try_malloc|g_try_malloc0|g_strdup|g_strndup|g_strdup_printf")) { + Token::Match(start, "%name% = new|malloc|calloc|kmalloc|kzalloc|kcalloc|strdup|strndup|vmalloc|g_new0|g_try_new|g_new|g_malloc|g_malloc0|g_try_malloc|g_try_malloc0|g_strdup|g_strndup|g_strdup_printf")) { bool allocate = true; if (start->strAt(2) == "new") { @@ -944,7 +944,7 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const if (var2->_type == Variables::reference) { variables.writeAliases(tok->varId(), tok); variables.read(tok->varId(), tok); - } else if (tok->varId() != varid1 && Token::Match(tok, "%var% .")) + } else if (tok->varId() != varid1 && Token::Match(tok, "%name% .")) variables.read(tok->varId(), tok); else if (tok->varId() != varid1 && var2->_type == Variables::standard && @@ -969,7 +969,7 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const } // assignment - else if ((Token::Match(tok, "%var% [") && Token::simpleMatch(skipBracketsAndMembers(tok->next()), "=")) || + else if ((Token::Match(tok, "%name% [") && Token::simpleMatch(skipBracketsAndMembers(tok->next()), "=")) || (Token::simpleMatch(tok, "* (") && Token::simpleMatch(tok->next()->link(), ") ="))) { if (tok->str() == "*") { tok = tok->tokAt(2); @@ -1004,7 +1004,7 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const variables.read(tok->next()->varId(), tok); } else // addressof variables.use(tok->next()->varId(), tok); // use = read + write - } else if (Token::Match(tok, ">>|>>= %var%")) { + } else if (Token::Match(tok, ">>|>>= %name%")) { if (_tokenizer->isC() || (tok->previous()->variable() && tok->previous()->variable()->typeEndToken()->isStandardType() && tok->astOperand1() && tok->astOperand1()->str() != ">>")) variables.read(tok->next()->varId(), tok); else @@ -1185,13 +1185,13 @@ void CheckUnusedVar::checkStructMemberUsage() structname.clear(); // Bail out if some data is casted to struct.. - const std::string s("( struct| " + tok->next()->str() + " * ) & %var% ["); + const std::string s("( struct| " + tok->next()->str() + " * ) & %name% ["); if (Token::findmatch(tok, s.c_str())) structname.clear(); // Bail out if instance is initialized with {}.. if (!structname.empty()) { - const std::string pattern1(structname + " %var% ;"); + const std::string pattern1(structname + " %name% ;"); const Token *tok2 = tok; while (nullptr != (tok2 = Token::findmatch(tok2->next(), pattern1.c_str()))) { if (Token::simpleMatch(tok2->tokAt(3), (tok2->strAt(1) + " = {").c_str())) { @@ -1202,9 +1202,9 @@ void CheckUnusedVar::checkStructMemberUsage() } // bail out for extern/global struct - for (const Token *tok2 = Token::findmatch(tok, (structname + " %var%").c_str()); + for (const Token *tok2 = Token::findmatch(tok, (structname + " %name%").c_str()); tok2 && tok2->next(); - tok2 = Token::findmatch(tok2->next(), (structname + " %var%").c_str())) { + tok2 = Token::findmatch(tok2->next(), (structname + " %name%").c_str())) { const Variable *var = tok2->next()->variable(); if (var && (var->isExtern() || (var->isGlobal() && !var->isStatic()))) { @@ -1233,11 +1233,11 @@ void CheckUnusedVar::checkStructMemberUsage() if (!tok->next()->isStandardType()) continue; - if (Token::Match(tok->next(), "%type% %var% [;[]")) + if (Token::Match(tok->next(), "%type% %name% [;[]")) varname = tok->strAt(2); - else if (Token::Match(tok->next(), "%type% %type%|* %var% [;[]")) + else if (Token::Match(tok->next(), "%type% %type%|* %name% [;[]")) varname = tok->strAt(3); - else if (Token::Match(tok->next(), "%type% %type% * %var% [;[]")) + else if (Token::Match(tok->next(), "%type% %type% * %name% [;[]")) varname = tok->strAt(4); else continue; diff --git a/lib/executionpath.cpp b/lib/executionpath.cpp index 02fb32d61..dd54bbc02 100644 --- a/lib/executionpath.cpp +++ b/lib/executionpath.cpp @@ -139,7 +139,7 @@ void ExecutionPath::checkScope(const Token *tok, std::list &che for (; tok; tok = tok->next()) { // might be a noreturn function.. if (Token::simpleMatch(tok->tokAt(-2), ") ; }") && - Token::Match(tok->linkAt(-2)->tokAt(-2), "[;{}] %var% (") && + Token::Match(tok->linkAt(-2)->tokAt(-2), "[;{}] %name% (") && tok->linkAt(-2)->previous()->varId() == 0) { ExecutionPath::bailOut(checks); return; @@ -268,7 +268,7 @@ void ExecutionPath::checkScope(const Token *tok, std::list &che // End } for the if block const Token *tok4 = tok3->link(); - if (Token::Match(tok3, "{ %var% =") && + if (Token::Match(tok3, "{ %name% =") && Token::simpleMatch(tok4, "} }") && Token::simpleMatch(tok4->tokAt(-2), "break ;")) { // Is there a assignment and then a break? @@ -311,7 +311,7 @@ void ExecutionPath::checkScope(const Token *tok, std::list &che } // bailout used variables in '; FOREACH ( .. ) { .. }' - else if (tok->str() != "if" && Token::Match(tok->previous(), "[;{}] %var% (")) { + else if (tok->str() != "if" && Token::Match(tok->previous(), "[;{}] %name% (")) { // goto { const Token *tok2 = tok->next()->link()->next(); if (tok2 && tok2->str() == "{") { @@ -353,7 +353,7 @@ void ExecutionPath::checkScope(const Token *tok, std::list &che if (Token::simpleMatch(tok, "= {")) { // GCC struct initialization.. bail out - if (Token::Match(tok->tokAt(2), ". %var% =")) { + if (Token::Match(tok->tokAt(2), ". %name% =")) { ExecutionPath::bailOut(checks); return; } diff --git a/lib/library.cpp b/lib/library.cpp index e30958028..5f0952833 100644 --- a/lib/library.cpp +++ b/lib/library.cpp @@ -635,16 +635,16 @@ bool Library::isScopeNoReturn(const Token *end, std::string *unknownFunc) const const Token *funcname = end->linkAt(-2)->previous(); const Token *start = funcname; - if (funcname && Token::Match(funcname->tokAt(-3),"( * %var% )")) { + if (funcname && Token::Match(funcname->tokAt(-3),"( * %name% )")) { funcname = funcname->previous(); start = funcname->tokAt(-3); } else if (funcname->isName()) { - while (Token::Match(start, "%var%|.|::")) + while (Token::Match(start, "%name%|.|::")) start = start->previous(); } else { return false; } - if (Token::Match(start,"[;{}]") && Token::Match(funcname, "%var% )| (")) { + if (Token::Match(start,"[;{}]") && Token::Match(funcname, "%name% )| (")) { if (funcname->str() == "exit") return true; if (!isnotnoreturn(funcname)) { diff --git a/lib/preprocessor.cpp b/lib/preprocessor.cpp index 5eaf4fd09..58c17af82 100644 --- a/lib/preprocessor.cpp +++ b/lib/preprocessor.cpp @@ -1336,8 +1336,8 @@ std::list Preprocessor::getcfgs(const std::string &filedata, const // translate A==1 condition to A=1 configuration if (def.find("==") != std::string::npos) { - // Check if condition match pattern "%var% == %num%" - // %var% + // Check if condition match pattern "%name% == %num%" + // %name% std::string::size_type pos = 0; if (std::isalpha((unsigned char)def[pos]) || def[pos] == '_') { ++pos; @@ -1362,7 +1362,7 @@ std::list Preprocessor::getcfgs(const std::string &filedata, const ++pos; } - // Does the condition match the pattern "%var% == %num%"? + // Does the condition match the pattern "%name% == %num%"? if (pos == def.size()) { def.erase(def.find("=="),1); } @@ -1508,13 +1508,13 @@ std::list Preprocessor::getcfgs(const std::string &filedata, const const Token *tok = tokenizer.tokens(); std::set varList; while (tok) { - if (Token::Match(tok, "defined ( %var% )")) { + if (Token::Match(tok, "defined ( %name% )")) { varList.insert(tok->strAt(2)); tok = tok->tokAt(4); if (tok && tok->str() == "&&") { tok = tok->next(); } - } else if (Token::Match(tok, "%var% ;")) { + } else if (Token::Match(tok, "%name% ;")) { varList.insert(tok->str()); tok = tok->tokAt(2); } else { @@ -1603,7 +1603,7 @@ void Preprocessor::simplifyCondition(const std::map &c return; } - if (Token::Match(tokenizer.tokens(), "( %var% )")) { + if (Token::Match(tokenizer.tokens(), "( %name% )")) { std::map::const_iterator var = cfg.find(tokenizer.tokens()->strAt(1)); if (var != cfg.end()) { const std::string &value = (*var).second; @@ -1613,7 +1613,7 @@ void Preprocessor::simplifyCondition(const std::map &c return; } - if (Token::Match(tokenizer.tokens(), "( ! %var% )")) { + if (Token::Match(tokenizer.tokens(), "( ! %name% )")) { std::map::const_iterator var = cfg.find(tokenizer.tokens()->strAt(2)); if (var == cfg.end()) @@ -1630,7 +1630,7 @@ void Preprocessor::simplifyCondition(const std::map &c if (!tok->isName()) continue; - if (Token::Match(tok, "defined ( %var% )")) { + if (Token::Match(tok, "defined ( %name% )")) { if (cfg.find(tok->strAt(2)) != cfg.end()) tok->str("1"); else if (match) @@ -1641,7 +1641,7 @@ void Preprocessor::simplifyCondition(const std::map &c continue; } - if (Token::Match(tok, "defined %var%")) { + if (Token::Match(tok, "defined %name%")) { if (cfg.find(tok->strAt(1)) != cfg.end()) tok->str("1"); else if (match) @@ -1799,7 +1799,7 @@ std::string Preprocessor::getcode(const std::string &filedata, const std::string line.erase(0, sizeof("#pragma endasm")); std::istringstream tempIstr(line); tokenizer.tokenize(tempIstr, "", "", true); - if (Token::Match(tokenizer.tokens(), "( %var% = %any% )")) { + if (Token::Match(tokenizer.tokens(), "( %name% = %any% )")) { ret << "asm(" << tokenizer.tokens()->strAt(1) << ");"; } } @@ -2558,12 +2558,12 @@ private: // Is there an inner macro.. { const Token *tok = Token::findsimplematch(tokens(), ")"); - if (!Token::Match(tok, ") %var% (")) + if (!Token::Match(tok, ") %name% (")) return params1; innerMacroName = tok->strAt(1); tok = tok->tokAt(3); unsigned int par = 0; - while (Token::Match(tok, "%var% ,|)")) { + while (Token::Match(tok, "%name% ,|)")) { tok = tok->tokAt(2); par++; } @@ -2627,7 +2627,7 @@ public: std::string::size_type pos = macro.find_first_of(" ("); if (pos != std::string::npos && macro[pos] == '(') { // Extract macro parameters - if (Token::Match(tokens(), "%var% ( %var%")) { + if (Token::Match(tokens(), "%name% ( %name%")) { for (const Token *tok = tokens()->tokAt(2); tok; tok = tok->next()) { if (tok->str() == ")") break; @@ -2642,10 +2642,10 @@ public: } } - else if (Token::Match(tokens(), "%var% ( . . . )")) + else if (Token::Match(tokens(), "%name% ( . . . )")) _variadic = true; - else if (Token::Match(tokens(), "%var% ( )")) + else if (Token::Match(tokens(), "%name% ( )")) _nopar = true; } } @@ -2795,9 +2795,9 @@ public: } optcomma = false; macrocode += str; - if (Token::Match(tok, "%var% %var%") || - Token::Match(tok, "%var% %num%") || - Token::Match(tok, "%num% %var%") || + if (Token::Match(tok, "%name% %name%") || + Token::Match(tok, "%name% %num%") || + Token::Match(tok, "%num% %name%") || Token::simpleMatch(tok, "> >")) macrocode += " "; } diff --git a/lib/symboldatabase.cpp b/lib/symboldatabase.cpp index 6b13b656e..9a3e646a2 100644 --- a/lib/symboldatabase.cpp +++ b/lib/symboldatabase.cpp @@ -52,7 +52,7 @@ SymbolDatabase::SymbolDatabase(const Tokenizer *tokenizer, const Settings *setti "SymbolDatabase", tok->progressValue()); // Locate next class - if (Token::Match(tok, "class|struct|union|namespace ::| %var% {|:|::|<") && + if (Token::Match(tok, "class|struct|union|namespace ::| %name% {|:|::|<") && tok->strAt(-1) != "friend") { const Token *tok2 = tok->tokAt(2); @@ -166,7 +166,7 @@ SymbolDatabase::SymbolDatabase(const Tokenizer *tokenizer, const Settings *setti // Namespace and unknown macro (#3854) else if (_tokenizer->isCPP() && - Token::Match(tok, "namespace %var% %type% (") && + Token::Match(tok, "namespace %name% %type% (") && tok->tokAt(2)->isUpperCaseName() && Token::simpleMatch(tok->linkAt(3), ") {")) { scopeList.push_back(Scope(this, tok, scope)); @@ -193,7 +193,7 @@ SymbolDatabase::SymbolDatabase(const Tokenizer *tokenizer, const Settings *setti } // forward declaration - else if (Token::Match(tok, "class|struct|union %var% ;") && + else if (Token::Match(tok, "class|struct|union %name% ;") && tok->strAt(-1) != "friend") { if (!findType(tok->next(), scope)) { // fill typeList.. @@ -225,7 +225,7 @@ SymbolDatabase::SymbolDatabase(const Tokenizer *tokenizer, const Settings *setti // unnamed struct and union else if (Token::Match(tok, "struct|union {") && - Token::Match(tok->next()->link(), "} *|&| %var% ;|[")) { + Token::Match(tok->next()->link(), "} *|&| %name% ;|[")) { scopeList.push_back(Scope(this, tok, scope)); Scope *new_scope = &scopeList.back(); @@ -313,7 +313,7 @@ SymbolDatabase::SymbolDatabase(const Tokenizer *tokenizer, const Settings *setti access[scope] = Protected; else if (tok->str() == "public:" || tok->str() == "__published:") access[scope] = Public; - else if (Token::Match(tok, "public|protected|private %var% :")) { + else if (Token::Match(tok, "public|protected|private %name% :")) { if (tok->str() == "private") access[scope] = Private; else if (tok->str() == "protected") @@ -357,15 +357,15 @@ SymbolDatabase::SymbolDatabase(const Tokenizer *tokenizer, const Settings *setti function.type = Function::eDestructor; // copy/move constructor? - else if (Token::Match(function.tokenDef, "%var% ( const| %var% &|&& &| %var%| )") || - Token::Match(function.tokenDef, "%var% ( const| %var% <")) { + else if (Token::Match(function.tokenDef, "%name% ( const| %name% &|&& &| %name%| )") || + Token::Match(function.tokenDef, "%name% ( const| %name% <")) { const Token* typTok = function.tokenDef->tokAt(2); if (typTok->str() == "const") typTok = typTok->next(); if (typTok->strAt(1) == "<") { // TODO: Remove this branch (#4710) - if (Token::Match(typTok->linkAt(1), "> & %var%| )")) + if (Token::Match(typTok->linkAt(1), "> & %name%| )")) function.type = Function::eCopyConstructor; - else if (Token::Match(typTok->linkAt(1), "> &&|& & %var%| )")) + else if (Token::Match(typTok->linkAt(1), "> &&|& & %name%| )")) function.type = Function::eMoveConstructor; else function.type = Function::eConstructor; @@ -1169,7 +1169,7 @@ SymbolDatabase::SymbolDatabase(const Tokenizer *tokenizer, const Settings *setti // Set function call pointers for (const Token* tok = _tokenizer->list.front(); tok != _tokenizer->list.back(); tok = tok->next()) { - if (Token::Match(tok, "%var% (")) { + if (Token::Match(tok, "%name% (")) { if (!tok->function() && tok->varId() == 0) const_cast(tok)->function(findFunction(tok)); } @@ -1183,7 +1183,7 @@ SymbolDatabase::SymbolDatabase(const Tokenizer *tokenizer, const Settings *setti func->functionScope->functionOf && func->arg && func->arg->link()->strAt(1) == ":") { const Token * tok = func->arg->link()->tokAt(2); while (tok && tok != func->functionScope->classStart) { - if (Token::Match(tok, "%var% {")) { + if (Token::Match(tok, "%name% {")) { if (tok->str() == func->tokenDef->str()) { const_cast(tok)->function(func->functionScope->functionOf->findFunction(tok)); break; @@ -1205,7 +1205,7 @@ SymbolDatabase::SymbolDatabase(const Tokenizer *tokenizer, const Settings *setti // Since it doesn't point at a fixed location it doesn't have varid if (tok->variable() != nullptr && tok->variable()->typeScope() && - Token::Match(tok, "%var% [|.")) { + Token::Match(tok, "%name% [|.")) { Token *tok2 = tok->next(); // Locate "]" @@ -1215,9 +1215,9 @@ SymbolDatabase::SymbolDatabase(const Tokenizer *tokenizer, const Settings *setti } Token *membertok = nullptr; - if (Token::Match(tok2, ". %var%")) + if (Token::Match(tok2, ". %name%")) membertok = tok2->next(); - else if (Token::Match(tok2, ") . %var%") && tok->strAt(-1) == "(") + else if (Token::Match(tok2, ") . %name%") && tok->strAt(-1) == "(") membertok = tok2->tokAt(2); if (membertok) { @@ -1234,8 +1234,8 @@ SymbolDatabase::SymbolDatabase(const Tokenizer *tokenizer, const Settings *setti // func(...).var // func(...)[...].var else if (tok->function() && tok->next()->str() == "(" && - (Token::Match(tok->next()->link(), ") . %var% !!(") || - (Token::Match(tok->next()->link(), ") [") && Token::Match(tok->next()->link()->next()->link(), "] . %var% !!(")))) { + (Token::Match(tok->next()->link(), ") . %name% !!(") || + (Token::Match(tok->next()->link(), ") [") && Token::Match(tok->next()->link()->next()->link(), "] . %name% !!(")))) { const Type *type = tok->function()->retType; if (type) { Token *membertok; @@ -1271,7 +1271,7 @@ bool SymbolDatabase::isFunction(const Token *tok, const Scope* outerScope, const if (tok->varId()) return false; - // function returning function pointer? '... ( ... %var% ( ... ))( ... ) {' + // function returning function pointer? '... ( ... %name% ( ... ))( ... ) {' if (tok->str() == "(" && tok->link()->previous()->str() == ")") { const Token* tok2 = tok->link()->next(); @@ -1284,16 +1284,16 @@ bool SymbolDatabase::isFunction(const Token *tok, const Scope* outerScope, const } // regular function? - else if (Token::Match(tok, "%var% (") && tok->previous() && + else if (Token::Match(tok, "%name% (") && tok->previous() && (tok->previous()->isName() || tok->strAt(-1) == ">" || tok->strAt(-1) == "&" || tok->strAt(-1) == "*" || // Either a return type in front of tok tok->strAt(-1) == "::" || tok->strAt(-1) == "~" || // or a scope qualifier in front of tok outerScope->isClassOrStruct())) { // or a ctor/dtor const Token* tok2 = tok->next()->link()->next(); if (tok2 && (Token::Match(tok2, "const| ;|{|=") || - (tok2->isUpperCaseName() && Token::Match(tok2, "%var% ;|{")) || - (tok2->isUpperCaseName() && Token::Match(tok2, "%var% (") && tok2->next()->link()->strAt(1) == "{") || - Token::Match(tok2, ": ::| %var% (|::|<|{") || + (tok2->isUpperCaseName() && Token::Match(tok2, "%name% ;|{")) || + (tok2->isUpperCaseName() && Token::Match(tok2, "%name% (") && tok2->next()->link()->strAt(1) == "{") || + Token::Match(tok2, ": ::| %name% (|::|<|{") || Token::Match(tok2, "= delete|default ;") || Token::Match(tok2, "const| noexcept {|:|;|=") || (Token::Match(tok2, "const| noexcept|throw (") && @@ -1307,7 +1307,7 @@ bool SymbolDatabase::isFunction(const Token *tok, const Scope* outerScope, const // UNKNOWN_MACRO(a,b) { ... } else if (outerScope->type == Scope::eGlobal && - Token::Match(tok, "%var% (") && + Token::Match(tok, "%name% (") && tok->isUpperCaseName() && Token::simpleMatch(tok->linkAt(1), ") {") && (!tok->previous() || Token::Match(tok->previous(), "[;{}]"))) { @@ -1317,10 +1317,10 @@ bool SymbolDatabase::isFunction(const Token *tok, const Scope* outerScope, const } // template constructor? - else if (Token::Match(tok, "%var% <") && Token::simpleMatch(tok->next()->link(), "> (")) { + else if (Token::Match(tok, "%name% <") && Token::simpleMatch(tok->next()->link(), "> (")) { const Token* tok2 = tok->next()->link()->next()->link(); if (Token::Match(tok2, ") const| ;|{|=") || - Token::Match(tok2, ") : ::| %var% (|::|<|{") || + Token::Match(tok2, ") : ::| %name% (|::|<|{") || Token::Match(tok->next()->link()->next()->link(), ") const| noexcept {|;|(")) { *funcStart = tok; *argStart = tok2->link(); @@ -1411,7 +1411,7 @@ void Variable::evaluate(const Library* lib) // type var = {x} // type var = x; gets simplified to: type var ; var = x ; Token const * declEnd = declEndToken(); - if ((Token::Match(declEnd, "; %var% =") && declEnd->strAt(1) == _name->str()) || + if ((Token::Match(declEnd, "; %name% =") && declEnd->strAt(1) == _name->str()) || Token::Match(declEnd, "=|{")) setFlag(fHasDefault, true); } @@ -1437,9 +1437,9 @@ bool Function::argsMatch(const Scope *scope, const Token *first, const Token *se second = second->next(); // skip const on type passed by value - if (Token::Match(first, "const %type% %var%|,|)")) + if (Token::Match(first, "const %type% %name%|,|)")) first = first->next(); - if (Token::Match(second, "const %type% %var%|,|)")) + if (Token::Match(second, "const %type% %name%|,|)")) second = second->next(); while (first->str() == second->str() && @@ -1515,8 +1515,8 @@ bool Function::argsMatch(const Scope *scope, const Token *first, const Token *se break; // variable names are different - else if ((Token::Match(first->next(), "%var% ,|)|=") && - Token::Match(second->next(), "%var% ,|)")) && + else if ((Token::Match(first->next(), "%name% ,|)|=") && + Token::Match(second->next(), "%name% ,|)")) && (first->next()->str() != second->next()->str())) { // skip variable names first = first->next(); @@ -1524,7 +1524,7 @@ bool Function::argsMatch(const Scope *scope, const Token *first, const Token *se } // variable with class path - else if (depth && Token::Match(first->next(), "%var%")) { + else if (depth && Token::Match(first->next(), "%name%")) { std::string param = path + first->next()->str(); if (Token::Match(second->next(), param.c_str())) { @@ -1547,7 +1547,7 @@ bool Function::argsMatch(const Scope *scope, const Token *first, const Token *se } // nested class variable - else if (depth == 0 && Token::Match(first->next(), "%var%") && + else if (depth == 0 && Token::Match(first->next(), "%name%") && second->next()->str() == scope->className && second->strAt(2) == "::" && first->next()->str() == second->strAt(3)) { second = second->tokAt(2); @@ -1563,9 +1563,9 @@ bool Function::argsMatch(const Scope *scope, const Token *first, const Token *se second = second->next(); // skip const on type passed by value - if (Token::Match(first, "const %type% %var%|,|)")) + if (Token::Match(first, "const %type% %name%|,|)")) first = first->next(); - if (Token::Match(second, "const %type% %var%|,|)")) + if (Token::Match(second, "const %type% %name%|,|)")) second = second->next(); } @@ -1873,7 +1873,7 @@ const Token *Type::initBaseInfo(const Token *tok, const Token *tok1) } // handle derived base classes - while (Token::Match(tok2, "%var% ::")) { + while (Token::Match(tok2, "%name% ::")) { tok2 = tok2->tokAt(2); } @@ -2711,7 +2711,7 @@ void Scope::getVariableList(const Library* lib) } else break; } else if (Token::Match(tok, "struct|union {")) { - if (Token::Match(tok->next()->link(), "} %var% ;|[")) { + if (Token::Match(tok->next()->link(), "} %name% ;|[")) { tok = tok->next()->link()->tokAt(2); continue; } else if (Token::simpleMatch(tok->next()->link(), "} ;")) { @@ -2749,7 +2749,7 @@ void Scope::getVariableList(const Library* lib) } // Is it a forward declaration? - else if (Token::Match(tok, "class|struct|union %var% ;")) { + else if (Token::Match(tok, "class|struct|union %name% ;")) { tok = tok->tokAt(2); continue; } @@ -2773,7 +2773,7 @@ void Scope::getVariableList(const Library* lib) continue; else if (Token::Match(tok, ";|{|}")) continue; - else if (Token::Match(tok, "goto %var% ;")) { + else if (Token::Match(tok, "goto %name% ;")) { tok = tok->tokAt(2); continue; } @@ -2910,7 +2910,7 @@ bool Scope::isVariableDeclaration(const Token* tok, const Token*& vartok, const if (closeTok) { localVarTok = skipPointers(closeTok->next()); - if (Token::Match(localVarTok, ":: %type% %var% [;=({]")) { + if (Token::Match(localVarTok, ":: %type% %name% [;=({]")) { if (localVarTok->strAt(3) != "(" || Token::Match(localVarTok->linkAt(3), "[)}] ;")) { localTypeTok = localVarTok->next(); @@ -2925,18 +2925,18 @@ bool Scope::isVariableDeclaration(const Token* tok, const Token*& vartok, const if (localVarTok && localVarTok->str() == "const") localVarTok = localVarTok->next(); - if (Token::Match(localVarTok, "%var% ;|=")) { + if (Token::Match(localVarTok, "%name% ;|=")) { vartok = localVarTok; typetok = localTypeTok; - } else if (Token::Match(localVarTok, "%var% )|[") && localVarTok->str() != "operator") { + } else if (Token::Match(localVarTok, "%name% )|[") && localVarTok->str() != "operator") { vartok = localVarTok; typetok = localTypeTok; - } else if (localVarTok && localVarTok->varId() && Token::Match(localVarTok, "%var% (|{") && + } else if (localVarTok && localVarTok->varId() && Token::Match(localVarTok, "%name% (|{") && Token::Match(localVarTok->next()->link(), ")|} ;")) { vartok = localVarTok; typetok = localTypeTok; } else if (type == eCatch && - Token::Match(localVarTok, "%var% )")) { + Token::Match(localVarTok, "%name% )")) { vartok = localVarTok; typetok = localTypeTok; } @@ -3054,7 +3054,7 @@ const Function* Scope::findFunction(const Token *tok) const for (std::size_t j = 0; j < args; ++j) { const Variable *funcarg = func->getArgumentVar(j); // check for a match with a variable - if (Token::Match(arguments[j], "%var% ,|)") && arguments[j]->varId()) { + if (Token::Match(arguments[j], "%var% ,|)")) { const Variable * callarg = check->getVariableFromVarId(arguments[j]->varId()); if (callarg && callarg->typeStartToken()->str() == funcarg->typeStartToken()->str() && @@ -3233,12 +3233,9 @@ const Function* SymbolDatabase::findFunction(const Token *tok) const else if (Token::Match(tok->tokAt(-2), "!!this .")) { const Token *tok1 = tok->tokAt(-2); if (Token::Match(tok1, "%var% .")) { - - if (tok1->varId()) { - const Variable *var = getVariableFromVarId(tok1->varId()); - if (var && var->typeScope()) - return var->typeScope()->findFunction(tok); - } + const Variable *var = getVariableFromVarId(tok1->varId()); + if (var && var->typeScope()) + return var->typeScope()->findFunction(tok); } } diff --git a/lib/templatesimplifier.cpp b/lib/templatesimplifier.cpp index 0fb09a401..0ea4b164a 100644 --- a/lib/templatesimplifier.cpp +++ b/lib/templatesimplifier.cpp @@ -77,7 +77,7 @@ void TemplateSimplifier::cleanupAfterSimplify(Token *tokens) if (tok->str() == "(") tok = tok->link(); - else if (Token::Match(tok, "template < > %var%")) { + else if (Token::Match(tok, "template < > %name%")) { const Token *end = tok; while (end) { if (end->str() == ";") @@ -86,7 +86,7 @@ void TemplateSimplifier::cleanupAfterSimplify(Token *tokens) end = end->link()->next(); break; } - if (!Token::Match(end, "%var%|::|<|>|>>|,")) { + if (!Token::Match(end, "%name%|::|<|>|>>|,")) { end = nullptr; break; } @@ -237,7 +237,7 @@ unsigned int TemplateSimplifier::templateParameters(const Token *tok) tok = tok->next(); // Skip '&' - if (Token::Match(tok, "& ::| %var%")) + if (Token::Match(tok, "& ::| %name%")) tok = tok->next(); // Skip variadic types (Ticket #5774, #6059, #6172) @@ -262,7 +262,7 @@ unsigned int TemplateSimplifier::templateParameters(const Token *tok) // skip std:: if (tok && tok->str() == "::") tok = tok->next(); - while (Token::Match(tok, "%var% ::")) { + while (Token::Match(tok, "%name% ::")) { tok = tok->tokAt(2); if (tok->str() == "*") // Ticket #5759: Class member pointer as a template argument; skip '*' tok = tok->next(); @@ -387,7 +387,7 @@ bool TemplateSimplifier::removeTemplate(Token *tok) else if (indentlevel >= 2 && tok2->str() == ">") --indentlevel; - else if (Token::Match(tok2, "> class|struct %var% [,)]")) { + else if (Token::Match(tok2, "> class|struct %name% [,)]")) { tok2 = tok2->next(); Token::eraseTokens(tok, tok2); tok->deleteThis(); @@ -507,9 +507,9 @@ std::list TemplateSimplifier::getTemplateInstantiations(Token *tokens) tok = tok->next()->findClosingBracket(); if (!tok) break; - } else if (Token::Match(tok->previous(), "[({};=] %var% <") || - Token::Match(tok->previous(), "%type% %var% <") || - Token::Match(tok->tokAt(-2), "[,:] private|protected|public %var% <")) { + } else if (Token::Match(tok->previous(), "[({};=] %name% <") || + Token::Match(tok->previous(), "%type% %name% <") || + Token::Match(tok->tokAt(-2), "[,:] private|protected|public %name% <")) { // Add inner template instantiations first => go to the ">" // and then parse backwards, adding all seen instantiations @@ -517,7 +517,7 @@ std::list TemplateSimplifier::getTemplateInstantiations(Token *tokens) // parse backwards and add template instantiations for (; tok2 && tok2 != tok; tok2 = tok2->previous()) { - if (Token::Match(tok2, ", %var% <") && + if (Token::Match(tok2, ", %name% <") && TemplateSimplifier::templateParameters(tok2->tokAt(2))) { used.push_back(tok2->next()); } @@ -565,7 +565,7 @@ void TemplateSimplifier::useDefaultArgumentValues(const std::list &temp // end of template parameters? if (Token::Match(tok, ">|>>")) { - if (Token::Match(tok, ">|>> class|struct %var%")) + if (Token::Match(tok, ">|>> class|struct %name%")) classname = tok->strAt(2); templateParmDepth -= (1 + (tok->str() == ">>")); if (0 == templateParmDepth) @@ -681,7 +681,7 @@ bool TemplateSimplifier::instantiateMatch(const Token *instance, const std::stri const Token *tok = instance; unsigned int indentlevel = 0; for (tok = instance; tok && (tok->str() != ">" || indentlevel > 0) && (tok->str() != ">>" || indentlevel > 1); tok = tok->next()) { - if (Token::Match(tok, "[<,] %var% <") && templateParameters(tok->tokAt(2)) > 0) + if (Token::Match(tok, "[<,] %name% <") && templateParameters(tok->tokAt(2)) > 0) ++indentlevel; if (indentlevel > 0 && tok->str() == ">") --indentlevel; @@ -749,7 +749,7 @@ void TemplateSimplifier::expandTemplate( // member function implemented outside class definition else if (inTemplateDefinition && - TemplateSimplifier::instantiateMatch(tok3, name, typeParametersInDeclaration.size(), ":: ~| %var% (")) { + TemplateSimplifier::instantiateMatch(tok3, name, typeParametersInDeclaration.size(), ":: ~| %name% (")) { tokenlist.addtoken(newName, tok3->linenr(), tok3->fileIndex()); while (tok3 && tok3->str() != "::") tok3 = tok3->next(); @@ -775,7 +775,7 @@ void TemplateSimplifier::expandTemplate( for (const Token *typetok = typesUsedInTemplateInstantiation[itype]; typetok && (typeindentlevel>0 || !Token::Match(typetok, ",|>|>>")); typetok = typetok->next()) { - if (Token::Match(typetok, "%var% <") && templateParameters(typetok->next()) > 0) + if (Token::Match(typetok, "%name% <") && templateParameters(typetok->next()) > 0) ++typeindentlevel; else if (typeindentlevel > 0 && typetok->str() == ">") --typeindentlevel; @@ -993,17 +993,17 @@ bool TemplateSimplifier::simplifyCalculations(Token *_tokens) // keep parentheses here: operator new [] (size_t); // keep parentheses here: Functor()(a ... ) // keep parentheses here: ) ( var ) ; - if ((Token::Match(tok->next(), "( %var% ) ;|)|,|]") || - (Token::Match(tok->next(), "( %var% ) %cop%") && (tok->tokAt(2)->varId()>0 || !Token::Match(tok->tokAt(4), "[*&+-]")))) && + if ((Token::Match(tok->next(), "( %name% ) ;|)|,|]") || + (Token::Match(tok->next(), "( %name% ) %cop%") && (tok->tokAt(2)->varId()>0 || !Token::Match(tok->tokAt(4), "[*&+-]")))) && !tok->isName() && tok->str() != ">" && tok->str() != "]" && tok->strAt(-1) != "operator" && !Token::simpleMatch(tok->previous(), "* )") && !Token::simpleMatch(tok->previous(), ") )") && - !Token::Match(tok->tokAt(-2), "* %var% )") && - !Token::Match(tok->tokAt(-2), "%type% ( ) ( %var%") && - !Token::Match(tok, ") ( %var% ) ;") + !Token::Match(tok->tokAt(-2), "* %name% )") && + !Token::Match(tok->tokAt(-2), "%type% ( ) ( %name%") && + !Token::Match(tok, ") ( %name% ) ;") ) { tok->deleteNext(); tok = tok->next(); @@ -1041,7 +1041,7 @@ bool TemplateSimplifier::simplifyCalculations(Token *_tokens) if (tok->str() == "0") { if (Token::Match(tok->previous(), "[+-|] 0")) { tok = tok->previous(); - if (Token::Match(tok->tokAt(-4), "[;{}] %var% = %var% [+-|] 0 ;") && + if (Token::Match(tok->tokAt(-4), "[;{}] %name% = %name% [+-|] 0 ;") && tok->strAt(-3) == tok->previous()->str()) { tok = tok->tokAt(-3); tok->deleteNext(2); @@ -1055,10 +1055,10 @@ bool TemplateSimplifier::simplifyCalculations(Token *_tokens) tok->deleteNext(); tok->deleteThis(); ret = true; - } else if (Token::Match(tok->previous(), "[=[(,] 0 * %var% ,|]|)|;|=|%cop%") || + } else if (Token::Match(tok->previous(), "[=[(,] 0 * %name% ,|]|)|;|=|%cop%") || Token::Match(tok->previous(), "[=[(,] 0 * %num% ,|]|)|;|%op%") || Token::Match(tok->previous(), "[=[(,] 0 * (") || - Token::Match(tok->previous(), "return|case 0 *|&& %var% ,|:|;|=|%cop%") || + Token::Match(tok->previous(), "return|case 0 *|&& %name% ,|:|;|=|%cop%") || Token::Match(tok->previous(), "return|case 0 *|&& %num% ,|:|;|%op%") || Token::Match(tok->previous(), "return|case 0 *|&& (")) { tok->deleteNext(); @@ -1174,7 +1174,7 @@ const Token * TemplateSimplifier::TemplateParametersInDeclaration( { typeParametersInDeclaration.clear(); for (; tok && tok->str() != ">"; tok = tok->next()) { - if (Token::Match(tok, "%var% ,|>")) + if (Token::Match(tok, "%name% ,|>")) typeParametersInDeclaration.push_back(tok); } return tok; @@ -1238,7 +1238,7 @@ bool TemplateSimplifier::simplifyTemplateInstantiations( continue; if (Token::Match(tok2->previous(), "[;{}=]") && - !TemplateSimplifier::instantiateMatch(*iter2, name, typeParametersInDeclaration.size(), isfunc ? "(" : "*| %var%")) + !TemplateSimplifier::instantiateMatch(*iter2, name, typeParametersInDeclaration.size(), isfunc ? "(" : "*| %name%")) continue; // New type.. @@ -1257,7 +1257,7 @@ bool TemplateSimplifier::simplifyTemplateInstantiations( typeForNewNameStr.clear(); break; } - if (Token::Match(tok3->tokAt(-2), "[<,] %var% <") && templateParameters(tok3) > 0) + if (Token::Match(tok3->tokAt(-2), "[<,] %name% <") && templateParameters(tok3) > 0) ++indentlevel; else if (indentlevel > 0 && Token::Match(tok3, "> [,>]")) --indentlevel; diff --git a/lib/token.cpp b/lib/token.cpp index cb85f6fc5..e324420f7 100644 --- a/lib/token.cpp +++ b/lib/token.cpp @@ -338,13 +338,9 @@ static int multiComparePercent(const Token *tok, const char*& haystack, bool emp return 1; break; case 'v': - // TODO: %var% should match only for - // variables that have varId != 0, but that needs a lot of - // work, before that change can be made. - // Any symbolname.. if (haystack[3] == '%') { // %var% haystack += 4; - if (tok->isName()) + if (tok->varId() != 0) return 1; } else { // %varid% if (varid == 0) { @@ -372,11 +368,17 @@ static int multiComparePercent(const Token *tok, const char*& haystack, bool emp return 1; } case 'n': - // Number (%num%) + // Number (%num%) or name (%name%) { - haystack += 4; - if (tok->isNumber()) - return 1; + if (haystack[4] == '%') { // %name% + haystack += 5; + if (tok->isName()) + return 1; + } else { + haystack += 4; + if (tok->isNumber()) + return 1; + } } break; case 'c': { @@ -1178,7 +1180,7 @@ std::string Token::expressionString() const std::string ret; for (const Token *tok = start; tok && tok != end; tok = tok->next()) { ret += tok->str(); - if (Token::Match(tok, "%var%|%num% %var%|%num%")) + if (Token::Match(tok, "%name%|%num% %name%|%num%")) ret += " "; } return ret + end->str(); diff --git a/lib/token.h b/lib/token.h index 3f0d376c9..1d2c48069 100644 --- a/lib/token.h +++ b/lib/token.h @@ -148,13 +148,14 @@ public: * * Possible patterns * - "%any%" any token - * - "%var%" any token which is a name or type e.g. "hello" or "int" + * - "%name%" any token which is a name, variable or type e.g. "hello" or "int" * - "%type%" Anything that can be a variable type, e.g. "int", but not "delete". * - "%num%" Any numeric token, e.g. "23" * - "%bool%" true or false * - "%char%" Any token enclosed in '-character. * - "%comp%" Any token such that isComparisonOp() returns true. * - "%str%" Any token starting with "-character (C-string). + * - "%var%" Match with token with varId > 0 * - "%varid%" Match with parameter varid * - "%op%" Any token such that isOp() returns true. * - "%cop%" Any token such that isConstOp() returns true. diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 261197c0f..5990930f6 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -475,7 +475,7 @@ static Token *processFunc(Token *tok2, bool inOperator) tok2 = tok2->next(); // skip over namespace - while (Token::Match(tok2, "%var% ::")) + while (Token::Match(tok2, "%name% ::")) tok2 = tok2->tokAt(2); if (!tok2) @@ -760,7 +760,7 @@ void Tokenizer::simplifyTypedef() } // function pointer - else if (Token::Match(tokOffset, "( * %var% ) (")) { + else if (Token::Match(tokOffset, "( * %name% ) (")) { // name token wasn't a name, it was part of the type typeEnd = typeEnd->next(); functionPtr = true; @@ -1453,7 +1453,7 @@ void Tokenizer::simplifyTypedef() else if (tok2->next()->str().at(0) == '\"') tok2 = tok2->tokAt(2); } - } while (Token::Match(tok2, ", %var% ;|'|=|,")); + } while (Token::Match(tok2, ", %name% ;|'|=|,")); } simplifyType = false; @@ -1728,7 +1728,7 @@ void Tokenizer::findComplicatedSyntaxErrorsInTemplates() bool Tokenizer::hasEnumsWithTypedef() { for (const Token *tok = list.front(); tok; tok = tok->next()) { - if (Token::Match(tok, "enum %var% {")) { + if (Token::Match(tok, "enum %name% {")) { tok = tok->tokAt(2); const Token *tok2 = Token::findsimplematch(tok, "typedef", tok->link()); if (tok2) @@ -1838,7 +1838,7 @@ void Tokenizer::combineStrings() void Tokenizer::concatenateDoubleSharp() { for (Token *tok = list.front(); tok; tok = tok->next()) { - while (Token::Match(tok, "%num%|%var% ## %num%|%var%")) { + while (Token::Match(tok, "%num%|%name% ## %num%|%name%")) { tok->str(tok->str() + tok->strAt(2)); tok->deleteNext(2); } @@ -1919,7 +1919,7 @@ void Tokenizer::simplifyRoundCurlyParentheses() tok->linkAt(2)->previous()->deleteNext(3); tok->deleteNext(2); } - if (Token::Match(tok, "( { %bool%|%char%|%num%|%str%|%var% ; } )")) { + if (Token::Match(tok, "( { %bool%|%char%|%num%|%str%|%name% ; } )")) { tok->deleteNext(); tok->deleteThis(); tok->deleteNext(3); @@ -1969,7 +1969,7 @@ void Tokenizer::simplifyArrayAccessSyntax() { // 0[a] -> a[0] for (Token *tok = list.front(); tok; tok = tok->next()) { - if (Token::Match(tok, "%num% [ %var% ]")) { + if (Token::Match(tok, "%num% [ %name% ]")) { std::string temp = tok->str(); tok->str(tok->strAt(2)); tok->tokAt(2)->str(temp); @@ -1980,7 +1980,7 @@ void Tokenizer::simplifyArrayAccessSyntax() void Tokenizer::simplifyParameterVoid() { for (Token* tok = list.front(); tok; tok = tok->next()) { - if (Token::Match(tok, "%var% ( void )")) + if (Token::Match(tok, "%name% ( void )")) tok->next()->deleteNext(); } } @@ -2052,14 +2052,14 @@ void Tokenizer::arraySize() { bool addlength = false; for (Token *tok = list.front(); tok; tok = tok->next()) { - if (Token::Match(tok, "%var% [ ] = { %str% } ;")) { + if (Token::Match(tok, "%name% [ ] = { %str% } ;")) { Token *t = tok->tokAt(3); t->deleteNext(); t->next()->deleteNext(); addlength = true; } - if (addlength || Token::Match(tok, "%var% [ ] = %str% ;")) { + if (addlength || Token::Match(tok, "%name% [ ] = %str% ;")) { tok = tok->next(); std::size_t sz = tok->strAt(3).length() - 1; tok->insertToken(MathLib::toString((unsigned int)sz)); @@ -2067,7 +2067,7 @@ void Tokenizer::arraySize() tok = tok->tokAt(5); } - else if (Token::Match(tok, "%var% [ ] = {")) { + else if (Token::Match(tok, "%name% [ ] = {")) { unsigned int sz = 1; tok = tok->next(); Token *end = tok->linkAt(3); @@ -2257,7 +2257,7 @@ void Tokenizer::simplifyLabelsCaseDefault() } else { syntaxError(tok); } - } else if (Token::Match(tok, "[;{}] %var% : !!;")) { + } else if (Token::Match(tok, "[;{}] %name% : !!;")) { tok = tok->tokAt(2); tok->insertToken(";"); } @@ -2281,7 +2281,7 @@ void Tokenizer::simplifyTemplates() for (Token *tok = list.front(); tok; tok = tok->next()) { // #2648 - simple fix for sizeof used as template parameter // TODO: this is a bit hardcoded. make a bit more generic - if (Token::Match(tok, "%var% < sizeof ( %type% ) >") && tok->tokAt(4)->isStandardType()) { + if (Token::Match(tok, "%name% < sizeof ( %type% ) >") && tok->tokAt(4)->isStandardType()) { Token * const tok3 = tok->next(); const unsigned int sizeOfResult = sizeOfType(tok3->tokAt(3)); tok3->deleteNext(4); @@ -2382,7 +2382,7 @@ static void setVarIdStructMembers(Token **tok1, unsigned int *_varId) { Token *tok = *tok1; - while (Token::Match(tok->next(), ". %var% !!(")) { + while (Token::Match(tok->next(), ". %name% !!(")) { const unsigned int struct_varid = tok->varId(); tok = tok->tokAt(2); if (struct_varid == 0) @@ -2425,7 +2425,7 @@ static const Token * findInitListEndToken(const Token *tok) while (tok && Token::Match(tok, "%type% ::")) tok = tok->tokAt(2); - if (Token::Match(tok, "%var% [({]")) { + if (Token::Match(tok, "%name% [({]")) { tok = tok->linkAt(1); if (!tok) return nullptr; @@ -2472,7 +2472,7 @@ static void setVarIdClassDeclaration(Token * const startToken, ++indentlevel; } else if (tok->str() == "}") --indentlevel; - else if (initListEndToken && indentlevel == 0 && Token::Match(tok->previous(), "[,:] %var% [({]")) { + else if (initListEndToken && indentlevel == 0 && Token::Match(tok->previous(), "[,:] %name% [({]")) { const std::map::const_iterator it = variableId.find(tok->str()); if (it != variableId.end()) { tok->varId(it->second); @@ -2515,7 +2515,7 @@ static void setVarIdClassFunction(const std::string &classname, continue; if (Token::Match(tok2->tokAt(-2), ("!!" + classname + " ::").c_str())) continue; - if (Token::Match(tok2->tokAt(-4), "%var% :: %var% ::")) // Currently unsupported + if (Token::Match(tok2->tokAt(-4), "%name% :: %name% ::")) // Currently unsupported continue; if (Token::Match(tok2->tokAt(-2), "!!this .")) continue; @@ -2729,7 +2729,7 @@ void Tokenizer::setVarId() tok->varId(it->second); setVarIdStructMembers(&tok, &structMembers, &_varId); } - } else if (Token::Match(tok, "::|. %var%")) { + } else if (Token::Match(tok, "::|. %name%")) { // Don't set varid after a :: or . token tok = tok->next(); } else if (tok->str() == ":" && Token::Match(tok->tokAt(-2), "class %type%")) { @@ -2751,7 +2751,7 @@ void Tokenizer::setVarId() std::list allMemberVars; if (!isC()) { for (Token *tok2 = list.front(); tok2; tok2 = tok2->next()) { - if (Token::Match(tok2, "%var% :: %var%")) { + if (Token::Match(tok2, "%name% :: %name%")) { if (tok2->strAt(3) == "(") allMemberFunctions.push_back(tok2); else if (tok2->strAt(3) != "::" && tok2->strAt(-1) != "::") // Support only one depth @@ -2763,7 +2763,7 @@ void Tokenizer::setVarId() // class members.. std::map > varlist; for (Token *tok = list.front(); tok; tok = tok->next()) { - if (Token::Match(tok, "namespace|class|struct %var% {|:")) { + if (Token::Match(tok, "namespace|class|struct %name% {|:")) { const std::string &classname(tok->next()->str()); bool namesp = tok->str() == "namespace"; @@ -2813,7 +2813,7 @@ void Tokenizer::setVarId() continue; // Set variable ids in member functions for this class.. - const std::string funcpattern(classname + " :: %var% ("); + const std::string funcpattern(classname + " :: %name% ("); for (std::list::iterator func = allMemberFunctions.begin(); func != allMemberFunctions.end(); ++func) { Token *tok2 = *func; @@ -2831,9 +2831,9 @@ void Tokenizer::setVarId() } // constructor with initializer list - if (Token::Match(tok2, ") : %var% (")) { + if (Token::Match(tok2, ") : %name% (")) { Token *tok3 = tok2; - while (Token::Match(tok3, ") [:,] %var% (")) { + while (Token::Match(tok3, ") [:,] %name% (")) { Token *vartok = tok3->tokAt(2); if (varlist[classname].find(vartok->str()) != varlist[classname].end()) vartok->varId(varlist[classname][vartok->str()]); @@ -2929,7 +2929,7 @@ void Tokenizer::createLinks2() else if (Token::Match(token, ">|>>")) { if (type.empty() || type.top()->str() != "<") // < and > don't match. continue; - if (token->next() && !Token::Match(token->next(), "%var%|>|>>|&|*|::|,|(|)|{|;|[")) + if (token->next() && !Token::Match(token->next(), "%name%|>|>>|&|*|::|,|(|)|{|;|[")) continue; // Check type of open link @@ -2939,7 +2939,7 @@ void Tokenizer::createLinks2() // if > is followed by [ .. "new a[" is expected if (token->strAt(1) == "[") { Token *prev = type.top()->previous(); - while (prev && Token::Match(prev->previous(), ":: %var%")) + while (prev && Token::Match(prev->previous(), ":: %name%")) prev = prev->tokAt(-2); if (prev && prev->str() != "new") prev = prev->previous(); @@ -2967,15 +2967,15 @@ void Tokenizer::createLinks2() void Tokenizer::sizeofAddParentheses() { for (Token *tok = list.front(); tok; tok = tok->next()) { - if (Token::Match(tok, "sizeof %var%")) { + if (Token::Match(tok, "sizeof %name%")) { Token *tempToken = tok->next(); - while (Token::Match(tempToken, "%var%")) { + while (Token::Match(tempToken, "%name%")) { while (tempToken && tempToken->next() && tempToken->next()->str() == "[") tempToken = tempToken->next()->link(); if (!tempToken || !tempToken->next()) break; - if (Token::Match(tempToken->next(), ". %var%")) { + if (Token::Match(tempToken->next(), ". %name%")) { // We are checking a class or struct, search next varname tempToken = tempToken->tokAt(2); continue; @@ -3008,10 +3008,10 @@ bool Tokenizer::simplifySizeof() for (Token *tok = list.front(); tok; tok = tok->next()) { if (tok->varId() != 0 && sizeOfVar.find(tok->varId()) == sizeOfVar.end()) { const unsigned int varId = tok->varId(); - if (Token::Match(tok->tokAt(-3), "[;{}(,] %type% * %var% [;,)]") || - Token::Match(tok->tokAt(-4), "[;{}(,] const %type% * %var% [;),]") || - Token::Match(tok->tokAt(-2), "[;{}(,] %type% %var% [;),]") || - Token::Match(tok->tokAt(-3), "[;{}(,] const %type% %var% [;),]")) { + if (Token::Match(tok->tokAt(-3), "[;{}(,] %type% * %name% [;,)]") || + Token::Match(tok->tokAt(-4), "[;{}(,] const %type% * %name% [;),]") || + Token::Match(tok->tokAt(-2), "[;{}(,] %type% %name% [;),]") || + Token::Match(tok->tokAt(-3), "[;{}(,] const %type% %name% [;),]")) { const unsigned int size = sizeOfType(tok->previous()); if (size == 0) { continue; @@ -3021,8 +3021,8 @@ bool Tokenizer::simplifySizeof() declTokOfVar[varId] = tok; } - else if (Token::Match(tok->previous(), "%type% %var% [ %num% ] [[;=]") || - Token::Match(tok->tokAt(-2), "%type% * %var% [ %num% ] [[;=]")) { + else if (Token::Match(tok->previous(), "%type% %name% [ %num% ] [[;=]") || + Token::Match(tok->tokAt(-2), "%type% * %name% [ %num% ] [[;=]")) { const unsigned int size = sizeOfType(tok->previous()); if (size == 0) continue; @@ -3031,8 +3031,8 @@ bool Tokenizer::simplifySizeof() declTokOfVar[varId] = tok; } - else if (Token::Match(tok->previous(), "%type% %var% [ %num% ] [,)]") || - Token::Match(tok->tokAt(-2), "%type% * %var% [ %num% ] [,)]")) { + else if (Token::Match(tok->previous(), "%type% %name% [ %num% ] [,)]") || + Token::Match(tok->tokAt(-2), "%type% * %name% [ %num% ] [,)]")) { Token tempTok(0); tempTok.str("*"); sizeOfVar[varId] = sizeOfType(&tempTok); @@ -3107,7 +3107,7 @@ bool Tokenizer::simplifySizeof() } // sizeof a++ -> sizeof(a++) - if (Token::Match(tok->next(), "++|-- %var% !!.") || Token::Match(tok->next(), "%var% ++|--")) { + if (Token::Match(tok->next(), "++|-- %name% !!.") || Token::Match(tok->next(), "%name% ++|--")) { tok->insertToken("("); tok->tokAt(3)->insertToken(")"); Token::createMutualLinks(tok->next(), tok->tokAt(4)); @@ -3130,7 +3130,7 @@ bool Tokenizer::simplifySizeof() ++parlevel; else if (tempToken->str() == ")") { --parlevel; - if (parlevel == 0 && !Token::Match(tempToken, ") . %var%")) { + if (parlevel == 0 && !Token::Match(tempToken, ") . %name%")) { // Ok, we should be clean. Add ) after tempToken tok->insertToken("("); tempToken->insertToken(")"); @@ -3138,7 +3138,7 @@ bool Tokenizer::simplifySizeof() break; } } - if (parlevel == 0 && Token::Match(tempToken, "%var%")) { + if (parlevel == 0 && Token::Match(tempToken, "%name%")) { while (tempToken && tempToken->next() && tempToken->next()->str() == "[") { tempToken = tempToken->next()->link(); } @@ -3177,7 +3177,7 @@ bool Tokenizer::simplifySizeof() } // sizeof( a ) - else if (Token::Match(tok->next(), "( %var% )") && tok->tokAt(2)->varId() != 0) { + else if (Token::Match(tok->next(), "( %var% )")) { if (sizeOfVar.find(tok->tokAt(2)->varId()) != sizeOfVar.end()) { tok->deleteNext(); tok->deleteThis(); @@ -3199,7 +3199,7 @@ bool Tokenizer::simplifySizeof() } } - else if (Token::Match(tok, "sizeof ( * %var% )") || Token::Match(tok, "sizeof ( %var% [ %num% ] )")) { + else if (Token::Match(tok, "sizeof ( * %name% )") || Token::Match(tok, "sizeof ( %name% [ %num% ] )")) { // Some default value.. std::size_t sz = 0; @@ -3207,13 +3207,13 @@ bool Tokenizer::simplifySizeof() if ((varid != 0) && (declTokOfVar.find(varid) != declTokOfVar.end())) { // Try to locate variable declaration.. const Token *decltok = declTokOfVar[varid]; - if (Token::Match(decltok->previous(), "%type%|* %var% [")) { + if (Token::Match(decltok->previous(), "%type%|* %name% [")) { sz = sizeOfType(decltok->previous()); - } else if (Token::Match(decltok->tokAt(-2), "%type% * %var%")) { + } else if (Token::Match(decltok->tokAt(-2), "%type% * %name%")) { sz = sizeOfType(decltok->tokAt(-2)); } // Multi-dimensional array.. - if (Token::Match(decltok,"%var% [") && Token::simpleMatch(decltok->linkAt(1), "] [")) { + if (Token::Match(decltok,"%name% [") && Token::simpleMatch(decltok->linkAt(1), "] [")) { const Token *tok2 = decltok->linkAt(1); while (Token::Match(tok2, "] [ %num% ]")) { sz = sz * MathLib::toLongNumber(tok2->strAt(2)); @@ -3246,7 +3246,7 @@ bool Tokenizer::simplifyTokenList1(const char FileName[]) // if MACRO for (Token *tok = list.front(); tok; tok = tok->next()) { - if (Token::Match(tok, "if|for|while|BOOST_FOREACH %var% (")) { + if (Token::Match(tok, "if|for|while|BOOST_FOREACH %name% (")) { if (Token::simpleMatch(tok, "for each")) { // 'for each ( )' -> 'asm ( )' tok->str("asm"); @@ -3279,7 +3279,7 @@ bool Tokenizer::simplifyTokenList1(const char FileName[]) for (const Token *tok = list.front(); tok; tok = tok->next()) { if (Token::simpleMatch(tok, "if (")) { tok = tok->next()->link(); - if (Token::Match(tok, ") %var% (") && + if (Token::Match(tok, ") %name% (") && tok->next()->isUpperCaseName() && Token::Match(tok->linkAt(2), ") {|else")) { syntaxError(tok->next()); @@ -3682,8 +3682,8 @@ bool Tokenizer::simplifyTokenList2() // Replace constants.. for (Token *tok = list.front(); tok; tok = tok->next()) { - if (Token::Match(tok, "const static| %type% %var% = %num% ;") || - Token::Match(tok, "const static| %type% %var% ( %num% ) ;")) { + if (Token::Match(tok, "const static| %type% %name% = %num% ;") || + Token::Match(tok, "const static| %type% %name% ( %num% ) ;")) { int offset = 0; if (tok->strAt(1) == "static") offset = 1; @@ -3739,8 +3739,8 @@ bool Tokenizer::simplifyTokenList2() } for (Token *tok = list.front(); tok; tok = tok->next()) { - if (!Token::Match(tok, "%num%|%var%|]|)") && - (Token::Match(tok->next(), "& %var% [ %num%|%var% ] !!["))) { + if (!Token::Match(tok, "%num%|%name%|]|)") && + (Token::Match(tok->next(), "& %name% [ %num%|%name% ] !!["))) { tok = tok->next(); if (tok->next()->varId()) { @@ -4075,7 +4075,7 @@ void Tokenizer::removeMacrosInGlobalScope() void Tokenizer::removeMacroInClassDef() { for (Token *tok = list.front(); tok; tok = tok->next()) { - if (Token::Match(tok, "class|struct %var% %var% {|:") && + if (Token::Match(tok, "class|struct %name% %name% {|:") && (tok->next()->isUpperCaseName() || tok->tokAt(2)->isUpperCaseName())) { if (tok->next()->isUpperCaseName() && !tok->tokAt(2)->isUpperCaseName()) tok->deleteNext(); @@ -4090,7 +4090,7 @@ void Tokenizer::removeMacroInClassDef() void Tokenizer::removeMacroInVarDecl() { for (Token *tok = list.front(); tok; tok = tok->next()) { - if (Token::Match(tok, "[;{}] %var% (") && tok->next()->isUpperCaseName()) { + if (Token::Match(tok, "[;{}] %name% (") && tok->next()->isUpperCaseName()) { // goto ')' parentheses const Token *tok2 = tok; int parlevel = 0; @@ -4139,25 +4139,25 @@ void Tokenizer::removeRedundantAssignment() tok2 = tok2->link(); // skip local class or struct else return; - } else if (Token::Match(tok2, "[;{}] %type% * %var% ;") && tok2->next()->str() != "return") { + } else if (Token::Match(tok2, "[;{}] %type% * %name% ;") && tok2->next()->str() != "return") { tok2 = tok2->tokAt(3); localvars.insert(tok2->varId()); - } else if (Token::Match(tok2, "[;{}] %type% %var% ;") && tok2->next()->isStandardType()) { + } else if (Token::Match(tok2, "[;{}] %type% %name% ;") && tok2->next()->isStandardType()) { tok2 = tok2->tokAt(2); localvars.insert(tok2->varId()); } else if (tok2->varId() && - !Token::Match(tok2->previous(), "[;{}] %var% = %char%|%num%|%var% ;")) { + !Token::Match(tok2->previous(), "[;{}] %name% = %char%|%num%|%name% ;")) { localvars.erase(tok2->varId()); } } localvars.erase(0); if (!localvars.empty()) { for (Token *tok2 = tok->next(); tok2 && tok2 != end;) { - if (Token::Match(tok2, "[;{}] %type% %var% ;") && localvars.find(tok2->tokAt(2)->varId()) != localvars.end()) { + if (Token::Match(tok2, "[;{}] %type% %name% ;") && localvars.find(tok2->tokAt(2)->varId()) != localvars.end()) { tok2->deleteNext(3); - } else if ((Token::Match(tok2, "[;{}] %type% * %var% ;") && + } else if ((Token::Match(tok2, "[;{}] %type% * %name% ;") && localvars.find(tok2->tokAt(3)->varId()) != localvars.end()) || - (Token::Match(tok2, "[;{}] %var% = %any% ;") && + (Token::Match(tok2, "[;{}] %name% = %any% ;") && localvars.find(tok2->next()->varId()) != localvars.end())) { tok2->deleteNext(4); } else @@ -4174,7 +4174,7 @@ void Tokenizer::simplifyRealloc() if (Token::Match(tok, "(|[") || (tok->str() == "{" && tok->previous() && tok->previous()->str() == "=")) tok = tok->link(); - else if (Token::Match(tok, "[;{}] %var% = realloc (")) { + else if (Token::Match(tok, "[;{}] %name% = realloc (")) { tok = tok->tokAt(3); if (Token::simpleMatch(tok->next(), "( 0 ,")) { //no "x = realloc(0,);" @@ -4228,10 +4228,10 @@ void Tokenizer::simplifyEmptyNamespaces() tok = tok->link(); continue; } - if (!Token::Match(tok, "namespace %var% {")) + if (!Token::Match(tok, "namespace %name% {")) continue; if (tok->strAt(3) == "}") { - tok->deleteNext(3); // remove '%var% { }' + tok->deleteNext(3); // remove '%name% { }' if (!tok->previous()) { // remove 'namespace' or replace it with ';' if isolated tok->deleteThis(); @@ -4255,7 +4255,7 @@ void Tokenizer::simplifyFlowControl() begin = begin->link(); //function scope - if (!Token::simpleMatch(begin, ") {") && !Token::Match(begin, ") %var% {")) + if (!Token::simpleMatch(begin, ") {") && !Token::Match(begin, ") %name% {")) continue; Token *end = begin->linkAt(1+(begin->next()->str() == "{" ? 0 : 1)); @@ -4294,7 +4294,7 @@ void Tokenizer::simplifyFlowControl() eraseDeadCode(tok, 0); } else if (Token::Match(tok,"return|goto") || - (Token::Match(tok->previous(), "[;{}] %var% (") && + (Token::Match(tok->previous(), "[;{}] %name% (") && _settings->library.isnoreturn(tok)) || (tok->str() == "throw" && !isC())) { //TODO: ensure that we exclude user-defined 'exit|abort|throw', except for 'noreturn' @@ -4392,8 +4392,8 @@ bool Tokenizer::removeRedundantConditions() void Tokenizer::removeRedundantFor() { for (Token *tok = list.front(); tok; tok = tok->next()) { - if (Token::Match(tok, "[;{}] for ( %var% = %num% ; %var% < %num% ; ++| %var% ++| ) {") || - Token::Match(tok, "[;{}] for ( %type% %var% = %num% ; %var% < %num% ; ++| %var% ++| ) {")) { + if (Token::Match(tok, "[;{}] for ( %name% = %num% ; %name% < %num% ; ++| %name% ++| ) {") || + Token::Match(tok, "[;{}] for ( %type% %name% = %num% ; %name% < %num% ; ++| %name% ++| ) {")) { // Same variable name.. const Token* varTok = tok->tokAt(3); bool type = varTok->next()->isName(); @@ -4642,7 +4642,7 @@ void Tokenizer::simplifyCompoundAssignment() // Simplify compound assignments: // "a+=b" => "a = a + b" for (Token *tok = list.front(); tok; tok = tok->next()) { - if (Token::Match(tok, "[;{}] (| *| (| %var%")) { + if (Token::Match(tok, "[;{}] (| *| (| %name%")) { // backup current token.. Token * const tok1 = tok; @@ -4654,7 +4654,7 @@ void Tokenizer::simplifyCompoundAssignment() } else { // variable.. tok = tok->tokAt(2); - while (Token::Match(tok, ". %var%") || + while (Token::Match(tok, ". %name%") || Token::Match(tok, "[|(")) { if (tok->str() == ".") tok = tok->tokAt(2); @@ -4979,7 +4979,7 @@ void Tokenizer::simplifyUndefinedSizeArray() Token *tok2 = tok->next(); while (tok2 && tok2->str() == "*") tok2 = tok2->next(); - if (!Token::Match(tok2, "%var% [ ] ;|[")) + if (!Token::Match(tok2, "%name% [ ] ;|[")) continue; tok = tok2->previous(); @@ -5023,7 +5023,7 @@ void Tokenizer::simplifyCasts() // (*(void (*)(char *))fp)(x); if (!tok->isName() && Token::simpleMatch(tok->next(), "* (") && - !Token::Match(tok->linkAt(2), ") %var%|&")) { + !Token::Match(tok->linkAt(2), ") %name%|&")) { tok = tok->linkAt(2); continue; } @@ -5046,9 +5046,9 @@ void Tokenizer::simplifyCasts() continue; } - while ((Token::Match(tok->next(), "( %type% *| *| *|&| ) *|&| %var%") && (tok->str() != ")" || tok->tokAt(2)->isStandardType())) || - Token::Match(tok->next(), "( const| %type% * *| *|&| ) *|&| %var%") || - Token::Match(tok->next(), "( const| %type% %type% *| *| *|&| ) *|&| %var%") || + while ((Token::Match(tok->next(), "( %type% *| *| *|&| ) *|&| %name%") && (tok->str() != ")" || tok->tokAt(2)->isStandardType())) || + Token::Match(tok->next(), "( const| %type% * *| *|&| ) *|&| %name%") || + Token::Match(tok->next(), "( const| %type% %type% *| *| *|&| ) *|&| %name%") || (!tok->isName() && (Token::Match(tok->next(), "( %type% * *| *|&| ) (") || Token::Match(tok->next(), "( const| %type% %type% * *| *|&| ) (")))) { if (tok->isName() && tok->str() != "return") @@ -5062,7 +5062,7 @@ void Tokenizer::simplifyCasts() // Set isCasted flag. Token *tok2 = tok->next(); - if (!Token::Match(tok2, "%var% [|.")) + if (!Token::Match(tok2, "%name% [|.")) tok2->isCast(true); else { // TODO: handle more complex expressions @@ -5113,7 +5113,7 @@ void Tokenizer::simplifyFunctionParameters() } // Find the function e.g. foo( x ) or foo( x, y ) - else if (Token::Match(tok, "%var% ( %var% [,)]") && + else if (Token::Match(tok, "%name% ( %name% [,)]") && !(tok->strAt(-1) == ":" || tok->strAt(-1) == ",")) { // We have found old style function, now we need to change it @@ -5129,7 +5129,7 @@ void Tokenizer::simplifyFunctionParameters() Token *tok1 = tok; while (nullptr != (tok1 = tok1->tokAt(2))) { - if (!Token::Match(tok1, "%var% [,)]")) { + if (!Token::Match(tok1, "%name% [,)]")) { bailOut = true; break; } @@ -5280,7 +5280,7 @@ void Tokenizer::simplifyPointerToStandardType() return; for (Token *tok = list.front(); tok; tok = tok->next()) { - if (!Token::Match(tok, "& %var% [ 0 ] !![")) + if (!Token::Match(tok, "& %name% [ 0 ] !![")) continue; // Remove '[ 0 ]' suffix @@ -5342,15 +5342,15 @@ void Tokenizer:: simplifyFunctionPointers() continue; while (Token::Match(tok2, "(|:: %type%")) tok2 = tok2->tokAt(2); - if (!Token::Match(tok2, "(|:: * %var%")) + if (!Token::Match(tok2, "(|:: * %name%")) continue; tok2 = tok2->tokAt(2); while (Token::Match(tok2, "%type%|:: %type%|::")) tok2 = tok2->next(); - if (!Token::Match(tok2, "%var% ) (") && - !Token::Match(tok2, "%var% [ ] ) (") && - !(Token::Match(tok2, "%var% (") && Token::simpleMatch(tok2->linkAt(1), ") ) ("))) + if (!Token::Match(tok2, "%name% ) (") && + !Token::Match(tok2, "%name% [ ] ) (") && + !(Token::Match(tok2, "%name% (") && Token::simpleMatch(tok2->linkAt(1), ") ) ("))) continue; while (tok->str() != "(") @@ -5378,7 +5378,7 @@ bool Tokenizer::simplifyFunctionReturn() if (tok->str() == "{") tok = tok->link(); - else if (Token::Match(tok, "%var% ( ) { return %bool%|%char%|%num%|%str% ; }") && tok->strAt(-1) != "::") { + else if (Token::Match(tok, "%name% ( ) { return %bool%|%char%|%num%|%str% ; }") && tok->strAt(-1) != "::") { const Token* const any = tok->tokAt(5); const std::string pattern("(|[|=|%cop% " + tok->str() + " ( ) ;|]|)|%cop%"); @@ -5485,7 +5485,7 @@ void Tokenizer::simplifyVarDecl(Token * tokBegin, Token * tokEnd, bool only_k_r_ } // strange looking variable declaration => don't split up. - if (Token::Match(tok2, "%type% *| %var% , %type% *| %var%")) + if (Token::Match(tok2, "%type% *| %name% , %type% *| %name%")) continue; if (Token::Match(tok2, "struct|union|class %type%")) { @@ -5548,7 +5548,7 @@ void Tokenizer::simplifyVarDecl(Token * tokBegin, Token * tokEnd, bool only_k_r_ } } - //pattern: "%type% *| ... *| const| %var% ,|=" + //pattern: "%type% *| ... *| const| %name% ,|=" if (Token::Match(tok2, "%type%") || (tok2 && tok2->previous() && tok2->previous()->str() == ">")) { Token *varName = tok2; @@ -5570,7 +5570,7 @@ void Tokenizer::simplifyVarDecl(Token * tokBegin, Token * tokEnd, bool only_k_r_ varName = varName->next(); } //non-VLA case - if (Token::Match(varName, "%var% ,|=")) { + if (Token::Match(varName, "%name% ,|=")) { if (varName->str() != "operator") { tok2 = varName->next(); // The ',' or '=' token @@ -5591,7 +5591,7 @@ void Tokenizer::simplifyVarDecl(Token * tokBegin, Token * tokEnd, bool only_k_r_ } //VLA case - else if (Token::Match(varName, "%var% [")) { + else if (Token::Match(varName, "%name% [")) { tok2 = varName->next(); while (Token::Match(tok2->link(), "] ,|=|[")) @@ -5610,7 +5610,7 @@ void Tokenizer::simplifyVarDecl(Token * tokBegin, Token * tokEnd, bool only_k_r_ } // brace initialization - else if (Token::Match(varName, "%var% {")) { + else if (Token::Match(varName, "%name% {")) { tok2 = varName->next(); tok2 = tok2->link(); if (tok2) @@ -5915,8 +5915,8 @@ void Tokenizer::simplifyStaticConst() void Tokenizer::simplifyIfAndWhileAssign() { for (Token *tok = list.front(); tok; tok = tok->next()) { - if (!Token::Match(tok->next(), "if|while ( !| (| %var% =") && - !Token::Match(tok->next(), "if|while ( !| (| %var% . %var% =")) + if (!Token::Match(tok->next(), "if|while ( !| (| %name% =") && + !Token::Match(tok->next(), "if|while ( !| (| %name% . %name% =")) continue; // simplifying a "while(cond) { }" condition ? @@ -5942,7 +5942,7 @@ void Tokenizer::simplifyIfAndWhileAssign() tok->deleteNext(); } - // Skip the "%var% = ..." + // Skip the "%name% = ..." Token *tok2; for (tok2 = tok->next(); tok2; tok2 = tok2->next()) { if (tok2->str() == "(") @@ -6026,12 +6026,12 @@ void Tokenizer::simplifyIfAndWhileAssign() void Tokenizer::simplifyVariableMultipleAssign() { for (Token *tok = list.front(); tok; tok = tok->next()) { - if (Token::Match(tok, "%var% = %var% = %num%|%var% ;")) { + if (Token::Match(tok, "%name% = %name% = %num%|%name% ;")) { // skip intermediate assignments Token *tok2 = tok->previous(); while (tok2 && tok2->str() == "=" && - Token::Match(tok2->previous(), "%var%")) { + Token::Match(tok2->previous(), "%name%")) { tok2 = tok2->tokAt(-2); } @@ -6066,26 +6066,26 @@ void Tokenizer::simplifyIfNot() break; if (Token::Match(tok, "0|false == (") || - Token::Match(tok, "0|false == %var%")) { + Token::Match(tok, "0|false == %name%")) { tok->deleteNext(); tok->str("!"); } - else if (Token::Match(tok, "%var% == 0|false")) { + else if (Token::Match(tok, "%name% == 0|false")) { tok->deleteNext(2); tok = tok->previous(); tok->insertToken("!"); tok = tok->next(); } - else if (Token::Match(tok, "%var% .|:: %var% == 0|false")) { + else if (Token::Match(tok, "%name% .|:: %name% == 0|false")) { tok = tok->previous(); tok->insertToken("!"); tok = tok->tokAt(4); tok->deleteNext(2); } - else if (Token::Match(tok, "* %var% == 0|false")) { + else if (Token::Match(tok, "* %name% == 0|false")) { tok = tok->previous(); tok->insertToken("!"); tok = tok->tokAt(3); @@ -6095,7 +6095,7 @@ void Tokenizer::simplifyIfNot() else if (tok->link() && Token::Match(tok, ") == 0|false")) { // if( foo(x) == 0 ) - if (Token::Match(tok->link()->tokAt(-2), "( %var%")) { + if (Token::Match(tok->link()->tokAt(-2), "( %name%")) { tok->deleteNext(2); tok->link()->previous()->insertToken(tok->link()->previous()->str()); tok->link()->tokAt(-2)->str("!"); @@ -6122,7 +6122,7 @@ void Tokenizer::simplifyIfNotNull() // Remove 'x = (x != 0)' if (Token::simpleMatch(tok, "= (")) { - if (Token::Match(tok->tokAt(-2), "[;{}] %var%")) { + if (Token::Match(tok->tokAt(-2), "[;{}] %name%")) { const std::string& varname(tok->previous()->str()); if (Token::simpleMatch(tok->tokAt(2), (varname + " != 0 ) ;").c_str()) || @@ -6144,19 +6144,19 @@ void Tokenizer::simplifyIfNotNull() break; if (Token::simpleMatch(tok, "0 != (") || - Token::Match(tok, "0 != %var%")) { + Token::Match(tok, "0 != %name%")) { deleteFrom = tok->previous(); if (tok->tokAt(2)) tok->tokAt(2)->isPointerCompare(true); } - else if (Token::Match(tok, "%var% != 0")) { + else if (Token::Match(tok, "%name% != 0")) { deleteFrom = tok; tok->isPointerCompare(true); tok->isExpandedMacro(tok->isExpandedMacro() || tok->tokAt(2)->isExpandedMacro()); } - else if (Token::Match(tok, "%var% .|:: %var% != 0")) { + else if (Token::Match(tok, "%name% .|:: %name% != 0")) { tok = tok->tokAt(2); deleteFrom = tok; tok->isPointerCompare(true); @@ -6176,7 +6176,7 @@ void Tokenizer::simplifyIfSameInnerCondition() { // same inner condition for (Token *tok = list.front(); tok; tok = tok->next()) { - if (Token::Match(tok, "if ( %var% ) {")) { + if (Token::Match(tok, "if ( %name% ) {")) { const unsigned int varid(tok->tokAt(2)->varId()); if (!varid) continue; @@ -6243,13 +6243,13 @@ bool Tokenizer::simplifyCAlternativeTokens() const std::map::const_iterator cOpIt = cAlternativeTokens.find(tok->str()); if (cOpIt != cAlternativeTokens.end()) { - if (isC() && !Token::Match(tok->previous(), "%var%|%num%|%char%|)|]|> %var% %var%|%num%|%char%|%op%|(")) + if (isC() && !Token::Match(tok->previous(), "%name%|%num%|%char%|)|]|> %name% %name%|%num%|%char%|%op%|(")) continue; tok->str(cOpIt->second); ret = true; } else if (Token::Match(tok, "not|compl")) { // Don't simplify 'not p;' (in case 'not' is a type) - if (isC() && (!Token::Match(tok->next(), "%var%|%op%|(") || + if (isC() && (!Token::Match(tok->next(), "%name%|%op%|(") || Token::Match(tok->previous(), "[;{}]") || (executableScopeLevel == 0U && tok->strAt(-1) == "("))) continue; @@ -6275,15 +6275,15 @@ void Tokenizer::simplifyInitVar() if (tok->str() == "return") continue; - if (Token::Match(tok, "class|struct|union| %type% *| %var% ( &| %any% ) ;")) { + if (Token::Match(tok, "class|struct|union| %type% *| %name% ( &| %any% ) ;")) { tok = initVar(tok); - } else if (Token::Match(tok, "%type% *| %var% ( %type% (")) { + } else if (Token::Match(tok, "%type% *| %name% ( %type% (")) { const Token* tok2 = tok->tokAt(2); if (!tok2->link()) tok2 = tok2->next(); if (!tok2->link() || (tok2->link()->strAt(1) == ";" && !Token::simpleMatch(tok2->linkAt(2), ") ("))) tok = initVar(tok); - } else if (Token::Match(tok, "class|struct|union| %type% *| %var% ( &| %any% ) ,")) { + } else if (Token::Match(tok, "class|struct|union| %type% *| %name% ( &| %any% ) ,")) { Token *tok1 = tok; while (tok1->str() != ",") tok1 = tok1->next(); @@ -6357,7 +6357,7 @@ bool Tokenizer::simplifyKnownVariables() goback = false; } // Reference to variable - if (Token::Match(tok, "%type%|* & %var% = %var% ;")) { + if (Token::Match(tok, "%type%|* & %name% = %name% ;")) { Token *start = tok->previous(); while (Token::Match(start,"%type%|*|&")) start = start->previous(); @@ -6382,8 +6382,8 @@ bool Tokenizer::simplifyKnownVariables() tok = start; } - if (tok->isName() && (Token::Match(tok, "static| const| static| %type% const| %var% = %any% ;") || - Token::Match(tok, "static| const| static| %type% const| %var% ( %any% ) ;"))) { + if (tok->isName() && (Token::Match(tok, "static| const| static| %type% const| %name% = %any% ;") || + Token::Match(tok, "static| const| static| %type% const| %name% ( %any% ) ;"))) { bool isconst = false; for (const Token *tok2 = tok; (tok2->str() != "=") && (tok2->str() != "("); tok2 = tok2->next()) { if (tok2->str() == "const") { @@ -6454,7 +6454,7 @@ bool Tokenizer::simplifyKnownVariables() int indentlevel = 0; Token *tok2 = tok; for (; tok2; tok2 = tok2->next()) { - if (Token::Match(tok2, "[;{}] float|double %var% ;")) { + if (Token::Match(tok2, "[;{}] float|double %name% ;")) { floatvars.insert(tok2->tokAt(2)->varId()); } @@ -6471,11 +6471,11 @@ bool Tokenizer::simplifyKnownVariables() tok2 = tok2->next()->link(); else if (tok2->previous()->str() != "*" && !Token::Match(tok2->tokAt(-2), "* --|++") && - (Token::Match(tok2, "%var% = %bool%|%char%|%num%|%str%|%var% ;") || - Token::Match(tok2, "%var% [ ] = %str% ;") || - Token::Match(tok2, "%var% [ %num% ] = %str% ;") || - Token::Match(tok2, "%var% = & %var% ;") || - Token::Match(tok2, "%var% = & %var% [ 0 ] ;"))) { + (Token::Match(tok2, "%name% = %bool%|%char%|%num%|%str%|%name% ;") || + Token::Match(tok2, "%name% [ ] = %str% ;") || + Token::Match(tok2, "%name% [ %num% ] = %str% ;") || + Token::Match(tok2, "%name% = & %name% ;") || + Token::Match(tok2, "%name% = & %name% [ 0 ] ;"))) { const unsigned int varid = tok2->varId(); if (varid == 0) continue; @@ -6508,7 +6508,7 @@ bool Tokenizer::simplifyKnownVariables() if (Token::Match(tok2, "%varid% = &| %varid%", tok2->varId())) continue; - const std::string structname = Token::Match(tok2->tokAt(-3), "[;{}] %var% .") ? + const std::string structname = Token::Match(tok2->tokAt(-3), "[;{}] %name% .") ? std::string(tok2->strAt(-2) + " .") : std::string(""); @@ -6530,7 +6530,7 @@ bool Tokenizer::simplifyKnownVariables() ret |= simplifyKnownVariablesSimplify(&tok2, tok3, varid, structname, value, valueVarId, valueIsPointer, valueToken, indentlevel); } - else if (Token::Match(tok2, "( %var% == %num% ) {")) { + else if (Token::Match(tok2, "( %name% == %num% ) {")) { const unsigned int varid = tok2->next()->varId(); if (varid == 0) continue; @@ -6542,7 +6542,7 @@ bool Tokenizer::simplifyKnownVariables() const unsigned int valueVarId = 0; const bool valueIsPointer = false; - // Insert a "%var% = %num% ;" at the beginning of the scope as simplifyKnownVariablesSimplify might compute an updated value + // Insert a "%name% = %num% ;" at the beginning of the scope as simplifyKnownVariablesSimplify might compute an updated value Token *scopeStart = tok2->tokAt(5); scopeStart->insertToken(tok2->strAt(1)); scopeStart = scopeStart->next(); @@ -6562,7 +6562,7 @@ bool Tokenizer::simplifyKnownVariables() } } - else if (Token::Match(tok2, "strcpy|sprintf ( %var% , %str% ) ;")) { + else if (Token::Match(tok2, "strcpy|sprintf ( %name% , %str% ) ;")) { const unsigned int varid(tok2->tokAt(2)->varId()); if (varid == 0) continue; @@ -6665,7 +6665,7 @@ bool Tokenizer::simplifyKnownVariablesGetData(unsigned int varid, Token **_tok2, bool Tokenizer::simplifyKnownVariablesSimplify(Token **tok2, Token *tok3, unsigned int varid, const std::string &structname, std::string &value, unsigned int valueVarId, bool valueIsPointer, const Token * const valueToken, int indentlevel) const { - const bool pointeralias(valueToken->isName() || Token::Match(valueToken, "& %var% [")); + const bool pointeralias(valueToken->isName() || Token::Match(valueToken, "& %name% [")); if (_errorLogger && !list.getFiles().empty()) _errorLogger->reportProgress(list.getFiles()[0], "Tokenize (simplifyKnownVariables)", tok3->progressValue()); @@ -6686,7 +6686,7 @@ bool Tokenizer::simplifyKnownVariablesSimplify(Token **tok2, Token *tok3, unsign } else if (tok3->str() == "}") { --indentlevel3; if (indentlevel3 < indentlevel) { - if (Token::Match((*tok2)->tokAt(-7), "%type% * %var% ; %var% = & %var% ;") && + if (Token::Match((*tok2)->tokAt(-7), "%type% * %name% ; %name% = & %name% ;") && (*tok2)->strAt(-5) == (*tok2)->strAt(-3)) { (*tok2) = (*tok2)->tokAt(-4); Token::eraseTokens((*tok2), (*tok2)->tokAt(6)); @@ -6751,7 +6751,7 @@ bool Tokenizer::simplifyKnownVariablesSimplify(Token **tok2, Token *tok3, unsign // TODO: don't bail out if the variable is a local variable, // then it can't be changed by the function call. if (tok3->str() == ")" && tok3->link() && - Token::Match(tok3->link()->tokAt(-2), "[;{}] %var% (") && + Token::Match(tok3->link()->tokAt(-2), "[;{}] %name% (") && !Token::Match(tok3->link()->previous(), "if|for|while|switch|BOOST_FOREACH")) break; @@ -6796,7 +6796,7 @@ bool Tokenizer::simplifyKnownVariablesSimplify(Token **tok2, Token *tok3, unsign } // Variable used in realloc (see Ticket #1649) - if (Token::Match(tok3, "%var% = realloc ( %var% ,") && + if (Token::Match(tok3, "%name% = realloc ( %name% ,") && tok3->varId() == varid && tok3->tokAt(4)->varId() == varid) { tok3->tokAt(4)->str(value); @@ -6804,7 +6804,7 @@ bool Tokenizer::simplifyKnownVariablesSimplify(Token **tok2, Token *tok3, unsign } // condition "(|&&|%OROR% %varid% )|&&|%OROR% - if (!Token::Match(tok3->previous(), "( %var% )") && + if (!Token::Match(tok3->previous(), "( %name% )") && Token::Match(tok3->previous(), "&&|(|%oror% %varid% &&|%oror%|)", varid)) { tok3->str(value); tok3->varId(valueVarId); @@ -6812,7 +6812,7 @@ bool Tokenizer::simplifyKnownVariablesSimplify(Token **tok2, Token *tok3, unsign } // parameter in function call.. - if (tok3->varId() == varid && Token::Match(tok3->previous(), "[(,] %var% [,)]")) { + if (tok3->varId() == varid && Token::Match(tok3->previous(), "[(,] %name% [,)]")) { // If the parameter is passed by value then simplify it if (isFunctionParameterPassedByValue(tok3)) { tok3->str(value); @@ -6832,7 +6832,7 @@ bool Tokenizer::simplifyKnownVariablesSimplify(Token **tok2, Token *tok3, unsign break; // suppress debug-warning when calling member function - if (Token::Match(tok3->next(), ". %var% (")) + if (Token::Match(tok3->next(), ". %name% (")) break; // suppress debug-warning when assignment @@ -6840,17 +6840,17 @@ bool Tokenizer::simplifyKnownVariablesSimplify(Token **tok2, Token *tok3, unsign break; // taking address of variable.. - if (Token::Match(tok3->tokAt(-2), "return|= & %var% ;")) + if (Token::Match(tok3->tokAt(-2), "return|= & %name% ;")) break; // parameter in function call.. - if (Token::Match(tok3->tokAt(-2), "%var% ( %var% ,|)") || - Token::Match(tok3->previous(), ", %var% ,|)")) + if (Token::Match(tok3->tokAt(-2), "%name% ( %name% ,|)") || + Token::Match(tok3->previous(), ", %name% ,|)")) break; // conditional increment if (Token::Match(tok3->tokAt(-3), ") { ++|--") || - Token::Match(tok3->tokAt(-2), ") { %var% ++|--")) + Token::Match(tok3->tokAt(-2), ") { %name% ++|--")) break; reportError(tok3, Severity::debug, "debug", @@ -6873,7 +6873,7 @@ bool Tokenizer::simplifyKnownVariablesSimplify(Token **tok2, Token *tok3, unsign if (!structname.empty()) { tok3->deleteNext(2); } - if (Token::Match(valueToken, "& %var% ;")) { + if (Token::Match(valueToken, "& %name% ;")) { tok3->insertToken("&"); tok3 = tok3->next(); } @@ -6884,7 +6884,7 @@ bool Tokenizer::simplifyKnownVariablesSimplify(Token **tok2, Token *tok3, unsign } // pointer alias used in condition.. - if (Token::Match(valueToken,"& %var% ;") && Token::Match(tok3, ("( * " + structname + " %varid% %cop%").c_str(), varid)) { + if (Token::Match(valueToken,"& %name% ;") && Token::Match(tok3, ("( * " + structname + " %varid% %cop%").c_str(), varid)) { tok3->deleteNext(); if (!structname.empty()) tok3->deleteNext(2); @@ -6905,7 +6905,7 @@ bool Tokenizer::simplifyKnownVariablesSimplify(Token **tok2, Token *tok3, unsign } // Variable is used in function call.. - if (Token::Match(tok3, ("%var% ( " + structname + " %varid% ,").c_str(), varid)) { + if (Token::Match(tok3, ("%name% ( " + structname + " %varid% ,").c_str(), varid)) { static const char * const functionName[] = { // always simplify "strcmp", "strdup", @@ -6929,7 +6929,7 @@ bool Tokenizer::simplifyKnownVariablesSimplify(Token **tok2, Token *tok3, unsign } // Variable is used as 2nd parameter in function call.. - if (Token::Match(tok3, ("%var% ( %any% , " + structname + " %varid% ,|)").c_str(), varid)) { + if (Token::Match(tok3, ("%name% ( %any% , " + structname + " %varid% ,|)").c_str(), varid)) { static const char * const functionName[] = { // always simplify "strcmp","strcpy","strncmp","strncpy", @@ -6967,7 +6967,7 @@ bool Tokenizer::simplifyKnownVariablesSimplify(Token **tok2, Token *tok3, unsign if (isCPP() && Token::Match(tok3, (">> " + structname + " %varid%").c_str(), varid)) { // bailout for such code: ; std :: cin >> i ; const Token *prev = tok3->previous(); - while (prev && prev->str() != "return" && Token::Match(prev, "%var%|::|*")) + while (prev && prev->str() != "return" && Token::Match(prev, "%name%|::|*")) prev = prev->previous(); if (Token::Match(prev, ";|{|}|>>")) break; @@ -6975,7 +6975,7 @@ bool Tokenizer::simplifyKnownVariablesSimplify(Token **tok2, Token *tok3, unsign // Variable is used in calculation.. if (((tok3->previous()->varId() > 0) && Token::Match(tok3, ("& " + structname + " %varid%").c_str(), varid)) || - (Token::Match(tok3, ("[=+-*/%^|[] " + structname + " %varid% [=?+-*/%^|;])]").c_str(), varid) && !Token::Match(tok3, ("= " + structname + " %var% =").c_str())) || + (Token::Match(tok3, ("[=+-*/%^|[] " + structname + " %varid% [=?+-*/%^|;])]").c_str(), varid) && !Token::Match(tok3, ("= " + structname + " %name% =").c_str())) || Token::Match(tok3, ("[(=+-*/%^|[] " + structname + " %varid% <<|>>").c_str(), varid) || Token::Match(tok3, ("<<|>> " + structname + " %varid% %cop%|;|]|)").c_str(), varid) || Token::Match(tok3->previous(), ("[=+-*/%^|[] ( " + structname + " %varid% !!=").c_str(), varid)) { @@ -6990,7 +6990,7 @@ bool Tokenizer::simplifyKnownVariablesSimplify(Token **tok2, Token *tok3, unsign ret = true; tok3->str(value); tok3->varId(valueVarId); - if (tok3->previous()->str() == "*" && (valueIsPointer || Token::Match(valueToken, "& %var% ;"))) { + if (tok3->previous()->str() == "*" && (valueIsPointer || Token::Match(valueToken, "& %name% ;"))) { tok3 = tok3->previous(); tok3->deleteThis(); ret = true; @@ -7015,7 +7015,7 @@ bool Tokenizer::simplifyKnownVariablesSimplify(Token **tok2, Token *tok3, unsign break; // Replace variable used in condition.. - if (Token::Match(tok4, "; %var% <|<=|!= %var% ; ++| %var% ++| )")) { + if (Token::Match(tok4, "; %name% <|<=|!= %name% ; ++| %name% ++| )")) { const Token *inctok = tok4->tokAt(5); if (inctok->str() == "++") inctok = inctok->next(); @@ -7152,21 +7152,21 @@ bool Tokenizer::simplifyRedundantParentheses() ret = true; } - if (Token::Match(tok->previous(), "! ( %var% )")) { + if (Token::Match(tok->previous(), "! ( %name% )")) { // Remove the parentheses tok->deleteThis(); tok->deleteNext(); ret = true; } - if (Token::Match(tok->previous(), "[(,;{}] ( %var% ) .")) { + if (Token::Match(tok->previous(), "[(,;{}] ( %name% ) .")) { // Remove the parentheses tok->deleteThis(); tok->deleteNext(); ret = true; } - if (Token::Match(tok->previous(), "[(,;{}] ( %var% (") && + if (Token::Match(tok->previous(), "[(,;{}] ( %name% (") && tok->link()->previous() == tok->linkAt(2)) { // We have "( func ( *something* ))", remove the outer // parentheses @@ -7175,7 +7175,7 @@ bool Tokenizer::simplifyRedundantParentheses() ret = true; } - if (Token::Match(tok->previous(), "[,;{}] ( delete [| ]| %var% ) ;")) { + if (Token::Match(tok->previous(), "[,;{}] ( delete [| ]| %name% ) ;")) { // We have "( delete [| ]| var )", remove the outer // parentheses tok->link()->deleteThis(); @@ -7192,7 +7192,7 @@ bool Tokenizer::simplifyRedundantParentheses() ret = true; } - if (Token::Match(tok->previous(), "[(!*;{}] ( %var% )") && + if (Token::Match(tok->previous(), "[(!*;{}] ( %name% )") && (tok->next()->varId() != 0 || Token::Match(tok->tokAt(3), "[+-/=]")) && !tok->next()->isStandardType()) { // We have "( var )", remove the parentheses tok->deleteThis(); @@ -7200,9 +7200,9 @@ bool Tokenizer::simplifyRedundantParentheses() ret = true; } - while (Token::Match(tok->previous(), "[;{}[]().,!*] ( %var% .")) { + while (Token::Match(tok->previous(), "[;{}[]().,!*] ( %name% .")) { Token *tok2 = tok->tokAt(2); - while (Token::Match(tok2, ". %var%")) { + while (Token::Match(tok2, ". %name%")) { tok2 = tok2->tokAt(2); } if (tok2 != tok->link()) @@ -7217,7 +7217,7 @@ bool Tokenizer::simplifyRedundantParentheses() if (Token::simpleMatch(tok->previous(), "? (") && Token::simpleMatch(tok->link(), ") :")) { const Token *tok2 = tok->next(); - while (tok2 && (Token::Match(tok2,"%bool%|%num%|%var%") || tok2->isArithmeticalOp())) + while (tok2 && (Token::Match(tok2,"%bool%|%num%|%name%") || tok2->isArithmeticalOp())) tok2 = tok2->next(); if (tok2 && tok2->str() == ")") { tok->link()->deleteThis(); @@ -7244,24 +7244,24 @@ bool Tokenizer::simplifyRedundantParentheses() ret = true; } - // Simplify "!!operator !!%var%|)|>|>> ( %num%|%bool% ) %op%|;|,|)" + // Simplify "!!operator !!%name%|)|>|>> ( %num%|%bool% ) %op%|;|,|)" if (Token::Match(tok, "( %bool%|%num% ) %cop%|;|,|)") && tok->strAt(-2) != "operator" && tok->previous() && - !Token::Match(tok->previous(), "%var%|)") && + !Token::Match(tok->previous(), "%name%|)") && (!(isCPP() && Token::Match(tok->previous(),">|>>")))) { tok->link()->deleteThis(); tok->deleteThis(); ret = true; } - if (Token::Match(tok->previous(), "%type% ( * %var% ) [") && tok->previous()->isStandardType()) { + if (Token::Match(tok->previous(), "%type% ( * %name% ) [") && tok->previous()->isStandardType()) { tok->link()->deleteThis(); tok->deleteThis(); ret = true; } - if (Token::Match(tok->previous(), "*|& ( %var% )")) { + if (Token::Match(tok->previous(), "*|& ( %name% )")) { // We may have a variable declaration looking like "type_name *(var_name)" Token *tok2 = tok->tokAt(-2); while (tok2 && Token::Match(tok2, "%type%|static|const|extern") && tok2->str() != "operator") { @@ -7307,7 +7307,7 @@ void Tokenizer::simplifyReference() Token * const end = tok->link(); for (Token *tok2 = tok; tok2 && tok2 != end; tok2 = tok2->next()) { // found a reference.. - if (Token::Match(tok2, "[;{}] %type% & %var% (|= %var% )| ;")) { + if (Token::Match(tok2, "[;{}] %type% & %name% (|= %name% )| ;")) { const unsigned int ref_id = tok2->tokAt(3)->varId(); if (!ref_id) continue; @@ -7340,7 +7340,7 @@ void Tokenizer::simplifyOffsetPointerDereference() for (Token *tok = list.front(); tok; tok = tok->next()) { if (!tok->isName() && !tok->isLiteral() && !Token::Match(tok, "]|)|++|--") - && Token::Match(tok->next(), "* ( %var% +|- %num%|%var% )")) { + && Token::Match(tok->next(), "* ( %name% +|- %num%|%name% )")) { // remove '* (' tok->deleteNext(2); @@ -7728,7 +7728,7 @@ void Tokenizer::simplifyEnum() endtoken = endtoken->next(); if (endtoken->str() == "<" && TemplateSimplifier::templateParameters(endtoken)) endtoken = endtoken->findClosingBracket(); - } while (Token::Match(endtoken, "%var%|%num% *| [,>]") || Token::Match(endtoken, "%var%|%num% ::|< %any%")); + } while (Token::Match(endtoken, "%name%|%num% *| [,>]") || Token::Match(endtoken, "%name%|%num% ::|< %any%")); if (endtoken->str() == ">") { enumValueEnd = endtoken; if (Token::simpleMatch(endtoken, "> ( )")) @@ -7870,7 +7870,7 @@ void Tokenizer::simplifyEnum() } // Function head - } else if (Token::Match(tok2, "%var% (")) { + } else if (Token::Match(tok2, "%name% (")) { const Token *prev = tok2->previous(); bool type = false; while (prev && (prev->isName() || Token::Match(prev, "*|&|::"))) { @@ -8078,7 +8078,7 @@ void Tokenizer::simplifyStd() if (tok->str() != "std") continue; - if (Token::Match(tok->previous(), "[(,{};] std :: %var% (") && + if (Token::Match(tok->previous(), "[(,{};] std :: %name% (") && f.find(tok->strAt(2)) != f.end()) { tok->deleteNext(); tok->deleteThis(); @@ -8133,7 +8133,7 @@ bool Tokenizer::isFunctionParameterPassedByValue(const Token *fpar) const } // Is this a function call? - if (ftok && Token::Match(ftok->tokAt(-2), "[;{}=] %var% (")) { + if (ftok && Token::Match(ftok->tokAt(-2), "[;{}=] %name% (")) { const std::string& functionName(ftok->previous()->str()); if (functionName == "return") @@ -8186,7 +8186,7 @@ void Tokenizer::eraseDeadCode(Token *begin, const Token *end) { if (!begin) return; - const bool isgoto = Token::Match(begin->tokAt(-2), "goto %var% ;"); + const bool isgoto = Token::Match(begin->tokAt(-2), "goto %name% ;"); unsigned int indentlevel = 1, indentcase = 0, indentswitch = 0, @@ -8302,7 +8302,7 @@ void Tokenizer::eraseDeadCode(Token *begin, const Token *end) } else { tok = tok->tokAt(3); } - } else if (Token::Match(tok, "[{};] %var% : ;") && tok->next()->str() != "default") { + } else if (Token::Match(tok, "[{};] %name% : ;") && tok->next()->str() != "default") { if (checklabel) { indentlabel = indentlevel; tok = tokcheck->next(); @@ -8757,7 +8757,7 @@ void Tokenizer::simplifyComma() tok->str(";"); } - if (Token::Match(tok->tokAt(-2), "delete %var% , %var% ;") && + if (Token::Match(tok->tokAt(-2), "delete %name% , %name% ;") && tok->next()->varId() != 0) { // Handle "delete a, b;" tok->str(";"); @@ -8768,8 +8768,8 @@ void Tokenizer::simplifyComma() if (tok2->str() == "=") { // Handle "a = 0, b = 0;" replace = true; - } else if (Token::Match(tok2, "delete %var%") || - Token::Match(tok2, "delete [ ] %var%")) { + } else if (Token::Match(tok2, "delete %name%") || + Token::Match(tok2, "delete [ ] %name%")) { // Handle "delete a, a = 0;" replace = true; } else if (Token::Match(tok2, "[?:;,{}()]")) { @@ -8954,9 +8954,9 @@ void Tokenizer::simplifyWhile0() const bool while0(Token::Match(tok->previous(), "[{};] while ( 0|false )")); // for (0) - not banal, ticket #3140 - const bool for0((Token::Match(tok->previous(), "[{};] for ( %var% = %num% ; %var% < %num% ;") && + const bool for0((Token::Match(tok->previous(), "[{};] for ( %name% = %num% ; %name% < %num% ;") && tok->strAt(2) == tok->strAt(6) && tok->strAt(4) == tok->strAt(8)) || - (Token::Match(tok->previous(), "[{};] for ( %type% %var% = %num% ; %var% < %num% ;") && + (Token::Match(tok->previous(), "[{};] for ( %type% %name% = %num% ; %name% < %num% ;") && tok->strAt(3) == tok->strAt(7) && tok->strAt(5) == tok->strAt(9))); if (!while0 && !for0) @@ -9033,7 +9033,7 @@ void Tokenizer::simplifyFuncInWhile() { unsigned int count = 0; for (Token *tok = list.front(); tok; tok = tok->next()) { - if (!Token::Match(tok, "while ( %var% ( %var% ) ) {")) + if (!Token::Match(tok, "while ( %name% ( %name% ) ) {")) continue; Token *func = tok->tokAt(2); @@ -9147,7 +9147,7 @@ void Tokenizer::simplifyStructDecl() // Try to create references in the union.. Token *tok2 = tok1->tokAt(2); while (tok2) { - if (Token::Match(tok2, "%type% %var% ;")) + if (Token::Match(tok2, "%type% %name% ;")) tok2 = tok2->tokAt(3); else break; @@ -9156,7 +9156,7 @@ void Tokenizer::simplifyStructDecl() continue; Token *vartok = nullptr; tok2 = tok1->tokAt(2); - while (Token::Match(tok2, "%type% %var% ;")) { + while (Token::Match(tok2, "%type% %name% ;")) { if (!vartok) { vartok = tok2->next(); tok2 = tok2->tokAt(3); @@ -9213,7 +9213,7 @@ void Tokenizer::simplifyDeclspec() while (Token::simpleMatch(tok, "__declspec (") && tok->next()->link() && tok->next()->link()->next()) { if (Token::Match(tok->tokAt(2), "noreturn|nothrow")) { Token *tok1 = tok->next()->link()->next(); - while (tok1 && !Token::Match(tok1, "%var%")) { + while (tok1 && !Token::Match(tok1, "%name%")) { tok1 = tok1->next(); } if (tok1) { @@ -9288,7 +9288,7 @@ void Tokenizer::simplifyAttribute() Token *functok = nullptr; // type func(...) __attribute__((attribute)); - if (tok->previous() && tok->previous()->link() && Token::Match(tok->previous()->link()->previous(), "%var% (")) + if (tok->previous() && tok->previous()->link() && Token::Match(tok->previous()->link()->previous(), "%name% (")) functok = tok->previous()->link()->previous(); // type __attribute__((attribute)) func() { } @@ -9296,7 +9296,7 @@ void Tokenizer::simplifyAttribute() Token *tok2 = tok->next()->link(); while (Token::Match(tok2, ") __attribute__|__attribute (")) tok2 = tok2->linkAt(2); - if (Token::Match(tok2, ") %var% (")) + if (Token::Match(tok2, ") %name% (")) functok = tok2->next(); } @@ -9409,7 +9409,7 @@ void Tokenizer::simplifyAssignmentInFunctionCall() tok = tok->link(); // Find 'foo(var='. Exclude 'assert(var=' to allow tests to check that assert(...) does not contain side-effects - else if (Token::Match(tok, "[;{}] %var% ( %var% =") && + else if (Token::Match(tok, "[;{}] %name% ( %name% =") && Token::simpleMatch(tok->linkAt(2), ") ;") && !Token::Match(tok->next(), "assert|while")) { const std::string funcname(tok->next()->str()); @@ -9444,7 +9444,7 @@ void Tokenizer::simplifyAssignmentInFunctionCall() void Tokenizer::simplifyAssignmentBlock() { for (Token *tok = list.front(); tok; tok = tok->next()) { - if (Token::Match(tok, "[;{}] %var% = ( {")) { + if (Token::Match(tok, "[;{}] %name% = ( {")) { const std::string &varname = tok->next()->str(); // goto the "} )" @@ -9463,7 +9463,7 @@ void Tokenizer::simplifyAssignmentBlock() } if (indentlevel == 2 && Token::simpleMatch(tok2, "} )")) { tok2 = tok2->tokAt(-3); - if (Token::Match(tok2, "[;{}] %num%|%var% ;")) { + if (Token::Match(tok2, "[;{}] %num%|%name% ;")) { tok2->insertToken("="); tok2->insertToken(tok->next()->str()); tok2->next()->varId(tok->next()->varId()); @@ -9546,7 +9546,7 @@ void Tokenizer::simplifyBitfields() } Token *last = nullptr; - if (Token::Match(tok, ";|{|}|public:|protected:|private: const| %type% %var% :") && + if (Token::Match(tok, ";|{|}|public:|protected:|private: const| %type% %name% :") && !Token::Match(tok->next(), "case|public|protected|private|class|struct") && !Token::simpleMatch(tok->tokAt(2), "default :")) { Token *tok1 = (tok->next()->str() == "const") ? tok->tokAt(3) : tok->tokAt(2); @@ -9673,9 +9673,9 @@ void Tokenizer::simplifyNamespaceStd() for (const Token* tok = Token::findsimplematch(list.front(), "using namespace std ;"); tok; tok = tok->next()) { bool insert = false; - if (Token::Match(tok, "%var% (") && !Token::Match(tok->previous(), ".|::") && !Token::Match(tok->linkAt(1)->next(), "%var%|{") && stdFunctions.find(tok->str()) != stdFunctions.end()) + if (Token::Match(tok, "%name% (") && !Token::Match(tok->previous(), ".|::") && !Token::Match(tok->linkAt(1)->next(), "%name%|{") && stdFunctions.find(tok->str()) != stdFunctions.end()) insert = true; - else if (Token::Match(tok, "%var% <") && !Token::Match(tok->previous(), ".|::") && stdTemplates.find(tok->str()) != stdTemplates.end()) + else if (Token::Match(tok, "%name% <") && !Token::Match(tok->previous(), ".|::") && stdTemplates.find(tok->str()) != stdTemplates.end()) insert = true; else if (tok->isName() && !tok->varId() && !Token::Match(tok->next(), "(|<") && !Token::Match(tok->previous(), ".|::") && stdTypes.find(tok->str()) != stdTypes.end()) insert = true; @@ -10003,7 +10003,7 @@ void Tokenizer::simplifyMicrosoftStringFunctions() void Tokenizer::simplifyBorland() { for (Token *tok = list.front(); tok; tok = tok->next()) { - if (Token::Match(tok, "( __closure * %var% )")) { + if (Token::Match(tok, "( __closure * %name% )")) { tok->deleteNext(); } } @@ -10017,7 +10017,7 @@ void Tokenizer::simplifyBorland() break; } - else if (Token::Match(tok, "class %var% :|{")) { + else if (Token::Match(tok, "class %name% :|{")) { while (tok && tok->str() != "{" && tok->str() != ";") tok = tok->next(); if (!tok) @@ -10053,10 +10053,10 @@ void Tokenizer::simplifyQtSignalsSlots() { for (Token *tok = list.front(); tok; tok = tok->next()) { // check for emit which can be outside of class - if (Token::Match(tok, "emit|Q_EMIT %var% (") && + if (Token::Match(tok, "emit|Q_EMIT %name% (") && Token::simpleMatch(tok->linkAt(2), ") ;")) { tok->deleteThis(); - } else if (!Token::Match(tok, "class %var% :")) + } else if (!Token::Match(tok, "class %name% :")) continue; if (tok->previous() && tok->previous()->str() == "enum") { @@ -10090,7 +10090,7 @@ void Tokenizer::simplifyQtSignalsSlots() tok2 = tok2->next(); tok2->str("protected:"); tok2->deleteNext(); - } else if (Token::Match(tok2->next(), "emit|Q_EMIT %var% (") && + } else if (Token::Match(tok2->next(), "emit|Q_EMIT %name% (") && Token::simpleMatch(tok2->linkAt(3), ") ;")) { tok2->deleteNext(); } @@ -10158,7 +10158,7 @@ void Tokenizer::simplifyOperatorName() op += par->str(); par = par->next(); // merge namespaces eg. 'operator std :: string () const {' - if (Token::Match(par, ":: %var%|%op%|.")) { + if (Token::Match(par, ":: %name%|%op%|.")) { op += par->str(); par = par->next(); } diff --git a/lib/tokenize.h b/lib/tokenize.h index 3a912dc6c..3b44eb504 100644 --- a/lib/tokenize.h +++ b/lib/tokenize.h @@ -139,8 +139,8 @@ public: static void eraseDeadCode(Token *begin, const Token *end); /** - * Simplify '* & ( %var% ) =' or any combination of '* &' and '()' - * parentheses around '%var%' to '%var% =' + * Simplify '* & ( %name% ) =' or any combination of '* &' and '()' + * parentheses around '%name%' to '%name% =' */ void simplifyMulAndParens(); @@ -386,7 +386,7 @@ public: */ bool simplifyKnownVariablesSimplify(Token **tok2, Token *tok3, unsigned int varid, const std::string &structname, std::string &value, unsigned int valueVarId, bool valueIsPointer, const Token * const valueToken, int indentlevel) const; - /** Simplify useless C++ empty namespaces, like: 'namespace %var% { }'*/ + /** Simplify useless C++ empty namespaces, like: 'namespace %name% { }'*/ void simplifyEmptyNamespaces(); /** Simplify redundant code placed after control flow statements : diff --git a/lib/tokenlist.cpp b/lib/tokenlist.cpp index 31261ee03..ae9585171 100644 --- a/lib/tokenlist.cpp +++ b/lib/tokenlist.cpp @@ -432,7 +432,7 @@ struct AST_state { static bool iscast(const Token *tok) { - if (!Token::Match(tok, "( %var%")) + if (!Token::Match(tok, "( %name%")) return false; if (tok->previous() && tok->previous()->isName() && tok->previous()->str() != "return") @@ -450,7 +450,7 @@ static bool iscast(const Token *tok) return type || tok2->strAt(-1) == "*" || (Token::Match(tok2, ") %any%") && (tok2->strAt(1) == "&" || (!tok2->next()->isOp() && !Token::Match(tok2->next(), "[[]);,?:.]")))); - if (!Token::Match(tok2, "%var%|*|&|::")) + if (!Token::Match(tok2, "%name%|*|&|::")) return false; if (tok2->isStandardType() && tok2->next()->str() != "(") @@ -521,11 +521,11 @@ static void compileTerm(Token *&tok, AST_state& state) if (tok->str() == "return") { compileUnaryOp(tok, state, compileExpression); state.op.pop(); - } else if (!state.cpp || !Token::Match(tok, "new|delete %var%|*|&|::|(|[")) { + } else if (!state.cpp || !Token::Match(tok, "new|delete %name%|*|&|::|(|[")) { while (tok->next() && tok->next()->isName()) tok = tok->next(); state.op.push(tok); - if (tok->next() && tok->linkAt(1) && Token::Match(tok, "%var% <")) + if (tok->next() && tok->linkAt(1) && Token::Match(tok, "%name% <")) tok = tok->linkAt(1); tok = tok->next(); } @@ -656,19 +656,19 @@ static void compilePrecedence3(Token *&tok, AST_state& state) tok = tok->link()->next(); compilePrecedence3(tok, state); compileUnaryOp(tok2, state, nullptr); - } else if (state.cpp && Token::Match(tok, "new %var%|::|(")) { + } else if (state.cpp && Token::Match(tok, "new %name%|::|(")) { Token* newtok = tok; tok = tok->next(); bool innertype = false; if (tok->str() == "(") { - if (Token::Match(tok, "( &| %var%") && Token::Match(tok->link(), ") ( %type%") && Token::simpleMatch(tok->link()->linkAt(1), ") (")) + if (Token::Match(tok, "( &| %name%") && Token::Match(tok->link(), ") ( %type%") && Token::simpleMatch(tok->link()->linkAt(1), ") (")) tok = tok->link()->next(); if (Token::Match(tok->link(), ") ::| %type%")) tok = tok->link()->next(); else if (Token::Match(tok, "( %type%") && Token::Match(tok->link(), ") [();,[]")) { tok = tok->next(); innertype = true; - } else if (Token::Match(tok, "( &| %var%") && Token::simpleMatch(tok->link(), ") (")) { + } else if (Token::Match(tok, "( &| %name%") && Token::simpleMatch(tok->link(), ") (")) { tok = tok->next(); innertype = true; } else { @@ -677,7 +677,7 @@ static void compilePrecedence3(Token *&tok, AST_state& state) } } state.op.push(tok); - while (Token::Match(tok, "%var%|*|&|<|::")) { + while (Token::Match(tok, "%name%|*|&|<|::")) { if (tok->link()) tok = tok->link(); tok = tok->next(); @@ -695,7 +695,7 @@ static void compilePrecedence3(Token *&tok, AST_state& state) compileUnaryOp(newtok, state, nullptr); if (innertype && Token::simpleMatch(tok, ") ,")) tok = tok->next(); - } else if (state.cpp && Token::Match(tok, "delete %var%|*|&|::|(|[")) { + } else if (state.cpp && Token::Match(tok, "delete %name%|*|&|::|(|[")) { Token* tok2 = tok; tok = tok->next(); if (tok->str() == "[") @@ -880,7 +880,7 @@ static Token * createAstAtToken(Token *tok, bool cpp) tok2 = tok2->link(); if (!tok2) break; - } else if (Token::Match(tok2, "%var% %op%|(|[|.|:|::") || Token::Match(tok2->previous(), "[(;{}] %cop%|(")) { + } else if (Token::Match(tok2, "%name% %op%|(|[|.|:|::") || Token::Match(tok2->previous(), "[(;{}] %cop%|(")) { init1 = tok2; AST_state state1(cpp); compileExpression(tok2, state1); @@ -936,7 +936,7 @@ static Token * createAstAtToken(Token *tok, bool cpp) if (Token::Match(tok, "%type% <") && Token::Match(tok->linkAt(1), "> !!(")) return tok->linkAt(1); - if (tok->str() == "return" || !tok->previous() || Token::Match(tok, "%var% %op%|(|[|.|::|<") || Token::Match(tok->previous(), "[;{}] %cop%|++|--|( !!{")) { + if (tok->str() == "return" || !tok->previous() || Token::Match(tok, "%name% %op%|(|[|.|::|<") || Token::Match(tok->previous(), "[;{}] %cop%|++|--|( !!{")) { Token * const tok1 = tok; AST_state state(cpp); compileExpression(tok, state); diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index a3b8a3d9d..19b15b385 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -47,15 +47,15 @@ static bool bailoutFunctionPar(const Token *tok, const ValueFlow::Value &value, const bool addressOf = tok && Token::simpleMatch(tok->previous(), "&"); // passing variable to subfunction? - if (Token::Match(tok->tokAt(-2), ") & %var% [,)]") && Token::Match(tok->linkAt(-2)->previous(), "[,(] (")) + if (Token::Match(tok->tokAt(-2), ") & %name% [,)]") && Token::Match(tok->linkAt(-2)->previous(), "[,(] (")) ; - else if (Token::Match(tok->tokAt(addressOf?-2:-1), "[(,] &| %var% [,)]")) + else if (Token::Match(tok->tokAt(addressOf?-2:-1), "[(,] &| %name% [,)]")) ; else return false; // reinterpret_cast etc.. - if (Token::Match(tok->tokAt(-3), "> ( & %var% ) [,)]") && + if (Token::Match(tok->tokAt(-3), "> ( & %name% ) [,)]") && tok->linkAt(-3) && Token::Match(tok->linkAt(-3)->tokAt(-2), "[,(] %type% <")) tok = tok->linkAt(-3); @@ -70,7 +70,7 @@ static bool bailoutFunctionPar(const Token *tok, const ValueFlow::Value &value, tok = tok->previous(); } tok = tok ? tok->previous() : nullptr; - if (!Token::Match(tok,"%var% (")) + if (!Token::Match(tok,"%name% (")) return false; // not a function => do not bailout if (!tok->function()) { @@ -148,13 +148,12 @@ static std::map getProgramMemory(const Token *tok if (Token::Match(tok2, "[;{}] %var% = %num% ;")) { const Token *vartok = tok2->next(); const Token *numtok = tok2->tokAt(3); - if (vartok->varId() != 0U && programMemory.find(vartok->varId()) == programMemory.end()) + if (programMemory.find(vartok->varId()) == programMemory.end()) programMemory[vartok->varId()] = MathLib::toLongNumber(numtok->str()); } if (Token::Match(tok2, "[;{}] %varid% = %var% ;", varid)) { const Token *vartok = tok2->tokAt(3); - if (vartok->varId() != 0U) - programMemory[vartok->varId()] = value.intvalue; + programMemory[vartok->varId()] = value.intvalue; } if (tok2->str() == "{") { if (indentlevel <= 0) @@ -241,7 +240,7 @@ static bool isReturn(const Token *tok) return isReturn(prev) && isReturn(prev->link()->tokAt(-2)); if (Token::simpleMatch(prev, ";")) { // noreturn function - if (Token::simpleMatch(prev->previous(), ") ;") && Token::Match(prev->linkAt(-1)->tokAt(-2), "[;{}] %var% (")) + if (Token::simpleMatch(prev->previous(), ") ;") && Token::Match(prev->linkAt(-1)->tokAt(-2), "[;{}] %name% (")) return true; // return/goto statement prev = prev->previous(); @@ -256,7 +255,7 @@ static bool isVariableChanged(const Token *start, const Token *end, const unsign { for (const Token *tok = start; tok != end; tok = tok->next()) { if (tok->varId() == varid) { - if (Token::Match(tok, "%var% =")) + if (Token::Match(tok, "%name% =")) return true; const Token *parent = tok->astParent(); @@ -385,7 +384,7 @@ static void valueFlowPointerAlias(TokenList *tokenlist) continue; // child should be some buffer or variable - if (!Token::Match(tok->astOperand1(), "%var%|.|[|;")) + if (!Token::Match(tok->astOperand1(), "%name%|.|[|;")) continue; ValueFlow::Value value; @@ -440,8 +439,8 @@ static void valueFlowBeforeCondition(TokenList *tokenlist, ErrorLogger *errorLog } else { continue; } - } else if (Token::Match(tok->previous(), "if|while ( %var% %oror%|&&|)") || - Token::Match(tok, "%oror%|&& %var% %oror%|&&|)")) { + } else if (Token::Match(tok->previous(), "if|while ( %name% %oror%|&&|)") || + Token::Match(tok, "%oror%|&& %name% %oror%|&&|)")) { varid = tok->next()->varId(); var = tok->next()->variable(); num = 0; @@ -528,18 +527,18 @@ static void valueFlowBeforeCondition(TokenList *tokenlist, ErrorLogger *errorLog if (tok2->varId() == varid) { // bailout: assignment - if (Token::Match(tok2->previous(), "!!* %var% =")) { + if (Token::Match(tok2->previous(), "!!* %name% =")) { if (settings->debugwarnings) bailout(tokenlist, errorLogger, tok2, "assignment of " + tok2->str()); break; } // increment/decrement - if (Token::Match(tok2->previous(), "[;{}] %var% ++|-- ;")) + if (Token::Match(tok2->previous(), "[;{}] %name% ++|-- ;")) val.intvalue += (tok2->strAt(1)=="++") ? -1 : 1; - else if (Token::Match(tok2->tokAt(-2), "[;{}] ++|-- %var% ;")) + else if (Token::Match(tok2->tokAt(-2), "[;{}] ++|-- %name% ;")) val.intvalue += (tok2->strAt(-1)=="++") ? -1 : 1; - else if (Token::Match(tok2->previous(), "++|-- %var%") || Token::Match(tok2, "%var% ++|--")) { + else if (Token::Match(tok2->previous(), "++|-- %name%") || Token::Match(tok2, "%name% ++|--")) { if (settings->debugwarnings) bailout(tokenlist, errorLogger, tok2, "increment/decrement of " + tok2->str()); break; @@ -554,7 +553,7 @@ static void valueFlowBeforeCondition(TokenList *tokenlist, ErrorLogger *errorLog if (Token::Match(tok2->previous(), "sizeof|.")) { const Token *prev = tok2->previous(); - while (Token::Match(prev,"%var%|.") && prev->str() != "sizeof") + while (Token::Match(prev,"%name%|.") && prev->str() != "sizeof") prev = prev->previous(); if (prev && prev->str() == "sizeof") continue; @@ -592,7 +591,7 @@ static void valueFlowBeforeCondition(TokenList *tokenlist, ErrorLogger *errorLog tok2 = tok2->link(); // goto label - if (Token::Match(tok2, "[;{}] %var% :")) { + if (Token::Match(tok2, "[;{}] %name% :")) { if (settings->debugwarnings) bailout(tokenlist, errorLogger, tok2->next(), "variable " + var->name() + " stopping on goto label"); break; @@ -600,7 +599,7 @@ static void valueFlowBeforeCondition(TokenList *tokenlist, ErrorLogger *errorLog if (tok2->str() == "}") { const Token *vartok = Token::findmatch(tok2->link(), "%varid%", tok2, varid); - while (Token::Match(vartok, "%var% = %num% ;") && !vartok->tokAt(2)->getValue(num)) + while (Token::Match(vartok, "%name% = %num% ;") && !vartok->tokAt(2)->getValue(num)) vartok = Token::findmatch(vartok->next(), "%varid%", tok2, varid); if (vartok) { if (settings->debugwarnings) { @@ -746,7 +745,7 @@ static bool valueFlowForward(Token * const startToken, } // conditional block of code that assigns variable.. - else if (Token::Match(tok2, "%var% (") && Token::simpleMatch(tok2->linkAt(1), ") {")) { + else if (Token::Match(tok2, "%name% (") && Token::simpleMatch(tok2->linkAt(1), ") {")) { // is variable changed in condition? if (isVariableChanged(tok2->next(), tok2->next()->link(), varid)) { if (settings->debugwarnings) @@ -927,7 +926,7 @@ static bool valueFlowForward(Token * const startToken, if (tok2->varId() == varid) { // bailout: assignment - if (Token::Match(tok2->previous(), "!!* %var% %op%") && tok2->next()->isAssignmentOp()) { + if (Token::Match(tok2->previous(), "!!* %name% %op%") && tok2->next()->isAssignmentOp()) { // simplify rhs for (Token *tok3 = tok2->tokAt(2); tok3; tok3 = tok3->next()) { if (tok3->varId() == varid) { @@ -943,14 +942,14 @@ static bool valueFlowForward(Token * const startToken, } // bailout increment/decrement for now.. - if (Token::Match(tok2->previous(), "++|-- %var%") || Token::Match(tok2, "%var% ++|--")) { + if (Token::Match(tok2->previous(), "++|-- %name%") || Token::Match(tok2, "%name% ++|--")) { if (settings->debugwarnings) bailout(tokenlist, errorLogger, tok2, "increment/decrement of " + tok2->str()); return false; } // bailout: possible assignment using >> - if (Token::Match(tok2->previous(), ">> %var% >>|;")) { + if (Token::Match(tok2->previous(), ">> %name% >>|;")) { const Token *parent = tok2->previous(); while (Token::simpleMatch(parent,">>")) parent = parent->astParent(); @@ -988,7 +987,7 @@ static bool valueFlowForward(Token * const startToken, } // bailout if reference is created.. - if (tok2->astParent() && Token::Match(tok2->astParent()->tokAt(-2), "& %var% =")) { + if (tok2->astParent() && Token::Match(tok2->astParent()->tokAt(-2), "& %name% =")) { if (settings->debugwarnings) bailout(tokenlist, errorLogger, tok2, "Reference of " + tok2->str()); return false; @@ -1091,7 +1090,7 @@ static void valueFlowAfterCondition(TokenList *tokenlist, ErrorLogger *errorLogg if (parent->astOperand1() == tok && ((op == "&&" && Token::Match(tok, "==|>=|<=|!")) || - (op == "||" && Token::Match(tok, "%var%|!=")))) { + (op == "||" && Token::Match(tok, "%name%|!=")))) { bool assign = false; for (; !assign && parent && parent->str() == op; parent = const_cast(parent->astParent())) { std::stack tokens; @@ -1134,7 +1133,7 @@ static void valueFlowAfterCondition(TokenList *tokenlist, ErrorLogger *errorLogg int codeblock = 0; if (Token::Match(tok, "==|>=|<=|!")) codeblock = 1; - else if (Token::Match(tok, "%var%|!=")) + else if (Token::Match(tok, "%name%|!=")) codeblock = 2; // determine startToken based on codeblock @@ -1318,14 +1317,12 @@ static bool valueFlowForLoop1(const Token *tok, unsigned int * const varid, Math if (!Token::Match(tok, "%type%| %var% =")) return false; const Token * const vartok = Token::Match(tok, "%var% =") ? tok : tok->next(); - if (vartok->varId() == 0U) - return false; *varid = vartok->varId(); const Token * const num1tok = Token::Match(vartok->tokAt(2), "%num% ;") ? vartok->tokAt(2) : nullptr; if (num1tok) *num1 = MathLib::toLongNumber(num1tok->str()); tok = vartok->tokAt(2); - while (Token::Match(tok, "%var%|%num%|%or%|+|-|*|/|&|[|]|(")) + while (Token::Match(tok, "%name%|%num%|%or%|+|-|*|/|&|[|]|(")) tok = (tok->str() == "(") ? tok->link()->next() : tok->next(); if (!tok || tok->str() != ";") return false; @@ -1555,7 +1552,7 @@ static void valueFlowForLoop(TokenList *tokenlist, ErrorLogger *errorLogger, con static void valueFlowSubFunction(TokenList *tokenlist, ErrorLogger *errorLogger, const Settings *settings) { for (Token *tok = tokenlist->front(); tok; tok = tok->next()) { - if (!Token::Match(tok, "%var% (")) + if (!Token::Match(tok, "%name% (")) continue; const Function * const currentFunction = tok->function(); @@ -1577,7 +1574,7 @@ static void valueFlowSubFunction(TokenList *tokenlist, ErrorLogger *errorLogger, std::list argvalues; // passing value(s) to function - if (!argtok->values.empty() && Token::Match(argtok, "%var%|%num%|%str% [,)]")) + if (!argtok->values.empty() && Token::Match(argtok, "%name%|%num%|%str% [,)]")) argvalues = argtok->values; else { // bool operator => values 1/0 are passed to function.. @@ -1603,8 +1600,6 @@ static void valueFlowSubFunction(TokenList *tokenlist, ErrorLogger *errorLogger, // Set value in function scope.. const unsigned int varid2 = arg->declarationId(); - if (!varid2) - continue; for (const Token *tok2 = functionScope->classStart->next(); tok2 != functionScope->classEnd; tok2 = tok2->next()) { if (Token::Match(tok2, "%varid% !!=", varid2)) { for (std::list::const_iterator val = argvalues.begin(); val != argvalues.end(); ++val) @@ -1660,7 +1655,7 @@ static void valueFlowFunctionReturn(TokenList *tokenlist, ErrorLogger *errorLogg std::map programMemory; for (std::size_t i = 0; i < parvalues.size(); ++i) { const Variable * const arg = function->getArgumentVar(i); - if (!arg || !Token::Match(arg->typeStartToken(), "%type% %var% ,|)")) { + if (!arg || !Token::Match(arg->typeStartToken(), "%type% %name% ,|)")) { if (settings->debugwarnings) bailout(tokenlist, errorLogger, tok, "function return; unhandled argument type"); programMemory.clear(); diff --git a/test/testinternal.cpp b/test/testinternal.cpp index 9c0969990..1606280cb 100644 --- a/test/testinternal.cpp +++ b/test/testinternal.cpp @@ -237,7 +237,7 @@ private: " const Token *tok;\n" " Token::Match(tok, \"foo|%type|bar\");\n" "}"); - ASSERT_EQUALS("[test.cpp:3]: (error) Bad multicompare pattern (a %cmd% must be first unless it is %or%,%op%,%cop%,%var%,%oror%) inside Token::Match() call: \"foo|%type|bar\"\n" + ASSERT_EQUALS("[test.cpp:3]: (error) Bad multicompare pattern (a %cmd% must be first unless it is %or%,%op%,%cop%,%name%,%oror%) inside Token::Match() call: \"foo|%type|bar\"\n" "[test.cpp:3]: (error) Missing percent end character in Token::Match() pattern: \"foo|%type|bar\"\n" , errout.str()); @@ -336,7 +336,7 @@ private: " const Token *tok;\n" " Token::Match(tok, \";|%type%\");\n" "}"); - ASSERT_EQUALS("[test.cpp:3]: (error) Bad multicompare pattern (a %cmd% must be first unless it is %or%,%op%,%cop%,%var%,%oror%) inside Token::Match() call: \";|%type%\"\n", errout.str()); + ASSERT_EQUALS("[test.cpp:3]: (error) Bad multicompare pattern (a %cmd% must be first unless it is %or%,%op%,%cop%,%name%,%oror%) inside Token::Match() call: \";|%type%\"\n", errout.str()); check("void f() {\n" " const Token *tok;\n" @@ -344,9 +344,9 @@ private: "}"); ASSERT_EQUALS("", errout.str()); - check("void f() {\n" // The %var%|%num% works.. + check("void f() {\n" // The %name%|%num% works.. " const Token *tok;\n" - " Token::Match(tok, \"%var%|%num%\");\n" + " Token::Match(tok, \"%name%|%num%\");\n" "}"); ASSERT_EQUALS("", errout.str()); } diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index fbfc2f0e1..dae4de592 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -53,7 +53,7 @@ private: TEST_CASE(redundant_plus); TEST_CASE(redundant_plus_numbers); TEST_CASE(parentheses1); - TEST_CASE(parenthesesVar); // Remove redundant parentheses around variable .. "( %var% )" + TEST_CASE(parenthesesVar); // Remove redundant parentheses around variable .. "( %name% )" TEST_CASE(declareVar); TEST_CASE(declareArray); diff --git a/test/testsymboldatabase.cpp b/test/testsymboldatabase.cpp index fa943833a..e0bafaf24 100644 --- a/test/testsymboldatabase.cpp +++ b/test/testsymboldatabase.cpp @@ -2272,7 +2272,7 @@ private: unsigned int linenrs[] = { 2, 1 }; unsigned int index = 0; for (const Token * tok = bar->classStart->next(); tok != bar->classEnd; tok = tok->next()) { - if (Token::Match(tok, "%var% (") && !tok->varId() && Token::simpleMatch(tok->linkAt(1), ") ;")) { + if (Token::Match(tok, "%name% (") && !tok->varId() && Token::simpleMatch(tok->linkAt(1), ") ;")) { const Function * function = db->findFunction(tok); ASSERT(function != nullptr); if (function) { diff --git a/test/testtoken.cpp b/test/testtoken.cpp index 021c33f17..29fb1bf4f 100644 --- a/test/testtoken.cpp +++ b/test/testtoken.cpp @@ -183,55 +183,55 @@ private: } void multiCompare3() const { - // Original pattern that failed: "return|(|&&|%oror% %var% &&|%oror%|==|!=|<=|>=|<|>|-|%or% %var% )|&&|%oror%|;" + // Original pattern that failed: "return|(|&&|%oror% %name% &&|%oror%|==|!=|<=|>=|<|>|-|%or% %name% )|&&|%oror%|;" // Code snippet that failed: "return lv@86 |= rv@87 ;" // Note: Also test "reverse" alternative pattern, two different code paths to handle it givenACodeSampleToTokenize toks("return a |= b ;", true); - ASSERT_EQUALS(false, Token::Match(toks.tokens(), "return %var% xyz|%or% %var% ;")); - ASSERT_EQUALS(false, Token::Match(toks.tokens(), "return %var% %or%|xyz %var% ;")); + ASSERT_EQUALS(false, Token::Match(toks.tokens(), "return %name% xyz|%or% %name% ;")); + ASSERT_EQUALS(false, Token::Match(toks.tokens(), "return %name% %or%|xyz %name% ;")); givenACodeSampleToTokenize toks2("return a | b ;", true); - ASSERT_EQUALS(true, Token::Match(toks2.tokens(), "return %var% xyz|%or% %var% ;")); - ASSERT_EQUALS(true, Token::Match(toks2.tokens(), "return %var% %or%|xyz %var% ;")); + ASSERT_EQUALS(true, Token::Match(toks2.tokens(), "return %name% xyz|%or% %name% ;")); + ASSERT_EQUALS(true, Token::Match(toks2.tokens(), "return %name% %or%|xyz %name% ;")); givenACodeSampleToTokenize toks3("return a || b ;", true); - ASSERT_EQUALS(false, Token::Match(toks3.tokens(), "return %var% xyz|%or% %var% ;")); - ASSERT_EQUALS(false, Token::Match(toks3.tokens(), "return %var% %or%|xyz %var% ;")); + ASSERT_EQUALS(false, Token::Match(toks3.tokens(), "return %name% xyz|%or% %name% ;")); + ASSERT_EQUALS(false, Token::Match(toks3.tokens(), "return %name% %or%|xyz %name% ;")); - ASSERT_EQUALS(true, Token::Match(toks3.tokens(), "return %var% xyz|%oror% %var% ;")); - ASSERT_EQUALS(true, Token::Match(toks3.tokens(), "return %var% %oror%|xyz %var% ;")); + ASSERT_EQUALS(true, Token::Match(toks3.tokens(), "return %name% xyz|%oror% %name% ;")); + ASSERT_EQUALS(true, Token::Match(toks3.tokens(), "return %name% %oror%|xyz %name% ;")); givenACodeSampleToTokenize toks4("a % b ;", true); - ASSERT_EQUALS(true, Token::Match(toks4.tokens(), "%var% >>|<<|&|%or%|^|% %var% ;")); - ASSERT_EQUALS(true, Token::Match(toks4.tokens(), "%var% %|>>|<<|&|%or%|^ %var% ;")); - ASSERT_EQUALS(true, Token::Match(toks4.tokens(), "%var% >>|<<|&|%or%|%|^ %var% ;")); + ASSERT_EQUALS(true, Token::Match(toks4.tokens(), "%name% >>|<<|&|%or%|^|% %name% ;")); + ASSERT_EQUALS(true, Token::Match(toks4.tokens(), "%name% %|>>|<<|&|%or%|^ %name% ;")); + ASSERT_EQUALS(true, Token::Match(toks4.tokens(), "%name% >>|<<|&|%or%|%|^ %name% ;")); - //%var%|%num% support + //%name%|%num% support givenACodeSampleToTokenize num("100", true); - ASSERT_EQUALS(true, Token::Match(num.tokens(), "%num%|%var%")); - ASSERT_EQUALS(true, Token::Match(num.tokens(), "%var%|%num%")); - ASSERT_EQUALS(true, Token::Match(num.tokens(), "%var%|%num%|%bool%")); - ASSERT_EQUALS(true, Token::Match(num.tokens(), "%var%|%bool%|%num%")); - ASSERT_EQUALS(true, Token::Match(num.tokens(), "%var%|%bool%|%str%|%num%")); - ASSERT_EQUALS(false, Token::Match(num.tokens(), "%bool%|%var%")); + ASSERT_EQUALS(true, Token::Match(num.tokens(), "%num%|%name%")); + ASSERT_EQUALS(true, Token::Match(num.tokens(), "%name%|%num%")); + ASSERT_EQUALS(true, Token::Match(num.tokens(), "%name%|%num%|%bool%")); + ASSERT_EQUALS(true, Token::Match(num.tokens(), "%name%|%bool%|%num%")); + ASSERT_EQUALS(true, Token::Match(num.tokens(), "%name%|%bool%|%str%|%num%")); + ASSERT_EQUALS(false, Token::Match(num.tokens(), "%bool%|%name%")); ASSERT_EQUALS(false, Token::Match(num.tokens(), "%type%|%bool%|%char%")); ASSERT_EQUALS(true, Token::Match(num.tokens(), "%type%|%bool%|100")); givenACodeSampleToTokenize numparen("( 100 )", true); - ASSERT_EQUALS(true, Token::Match(numparen.tokens(), "(| %num%|%var% )|")); - ASSERT_EQUALS(true, Token::Match(numparen.tokens(), "(| %var%|%num% )|")); - ASSERT_EQUALS(true, Token::Match(numparen.tokens(), "(| %var%|%num%|%bool% )|")); - ASSERT_EQUALS(true, Token::Match(numparen.tokens(), "(| %var%|%bool%|%num% )|")); - ASSERT_EQUALS(true, Token::Match(numparen.tokens(), "(| %var%|%bool%|%str%|%num% )|")); - ASSERT_EQUALS(false, Token::Match(numparen.tokens(), "(| %bool%|%var% )|")); + ASSERT_EQUALS(true, Token::Match(numparen.tokens(), "(| %num%|%name% )|")); + ASSERT_EQUALS(true, Token::Match(numparen.tokens(), "(| %name%|%num% )|")); + ASSERT_EQUALS(true, Token::Match(numparen.tokens(), "(| %name%|%num%|%bool% )|")); + ASSERT_EQUALS(true, Token::Match(numparen.tokens(), "(| %name%|%bool%|%num% )|")); + ASSERT_EQUALS(true, Token::Match(numparen.tokens(), "(| %name%|%bool%|%str%|%num% )|")); + ASSERT_EQUALS(false, Token::Match(numparen.tokens(), "(| %bool%|%name% )|")); - ASSERT_EQUALS(true, Token::Match(numparen.tokens(), "(| 100 %num%|%var%| )|")); - ASSERT_EQUALS(true, Token::Match(numparen.tokens(), "(| 100 %var%|%num%| )|")); - ASSERT_EQUALS(true, Token::Match(numparen.tokens(), "(| 100 %var%|%num%|%bool%| )|")); - ASSERT_EQUALS(true, Token::Match(numparen.tokens(), "(| 100 %var%|%bool%|%num%| )|")); - ASSERT_EQUALS(true, Token::Match(numparen.tokens(), "(| 100 %var%|%bool%|%str%|%num%| )|")); - ASSERT_EQUALS(true, Token::Match(numparen.tokens(), "(| 100 %bool%|%var%| )|")); + ASSERT_EQUALS(true, Token::Match(numparen.tokens(), "(| 100 %num%|%name%| )|")); + ASSERT_EQUALS(true, Token::Match(numparen.tokens(), "(| 100 %name%|%num%| )|")); + ASSERT_EQUALS(true, Token::Match(numparen.tokens(), "(| 100 %name%|%num%|%bool%| )|")); + ASSERT_EQUALS(true, Token::Match(numparen.tokens(), "(| 100 %name%|%bool%|%num%| )|")); + ASSERT_EQUALS(true, Token::Match(numparen.tokens(), "(| 100 %name%|%bool%|%str%|%num%| )|")); + ASSERT_EQUALS(true, Token::Match(numparen.tokens(), "(| 100 %bool%|%name%| )|")); } void multiCompare4() const { @@ -323,10 +323,10 @@ private: void matchAny() const { givenACodeSampleToTokenize varBitOrVar("abc|def", true); - ASSERT_EQUALS(true, Token::Match(varBitOrVar.tokens(), "%var% %or% %var%")); + ASSERT_EQUALS(true, Token::Match(varBitOrVar.tokens(), "%name% %or% %name%")); givenACodeSampleToTokenize varLogOrVar("abc||def", true); - ASSERT_EQUALS(true, Token::Match(varLogOrVar.tokens(), "%var% %oror% %var%")); + ASSERT_EQUALS(true, Token::Match(varLogOrVar.tokens(), "%name% %oror% %name%")); } void matchSingleChar() const { @@ -363,7 +363,7 @@ private: givenACodeSampleToTokenize isVar("int a = 3 ;"); ASSERT_EQUALS(true, Token::Match(isVar.tokens(), "%type%")); - ASSERT_EQUALS(true, Token::Match(isVar.tokens(), "%type% %var%")); + ASSERT_EQUALS(true, Token::Match(isVar.tokens(), "%type% %name%")); ASSERT_EQUALS(false, Token::Match(isVar.tokens(), "%type% %type%")); givenACodeSampleToTokenize noType1_cpp("delete", true, true); @@ -417,13 +417,17 @@ private: givenACodeSampleToTokenize var("int a ; int b ;"); // Varid == 0 should throw exception - ASSERT_THROW(Token::Match(var.tokens(), "%type% %varid% ; %type% %var%", 0),InternalError); + ASSERT_THROW(Token::Match(var.tokens(), "%type% %varid% ; %type% %name%", 0),InternalError); - ASSERT_EQUALS(true, Token::Match(var.tokens(), "%type% %varid% ; %type% %var%", 1)); - ASSERT_EQUALS(true, Token::Match(var.tokens(), "%type% %var% ; %type% %varid%", 2)); + ASSERT_EQUALS(true, Token::Match(var.tokens(), "%type% %varid% ; %type% %name%", 1)); + ASSERT_EQUALS(true, Token::Match(var.tokens(), "%type% %name% ; %type% %varid%", 2)); // Try to match two different varids in one match call ASSERT_EQUALS(false, Token::Match(var.tokens(), "%type% %varid% ; %type% %varid%", 2)); + + // %var% matches with every varid other than 0 + ASSERT_EQUALS(true, Token::Match(var.tokens(), "%type% %var% ;")); + ASSERT_EQUALS(false, Token::Match(var.tokens(), "%var% %var% ;")); } void matchNumeric() const { diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 9da0bbc73..b7def1bb4 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -851,7 +851,7 @@ private: tokenizeAndStringify("void f() {switch (n) { case 0?(1?3:4):2 : z(); break;}}"); ASSERT_EQUALS("", errout.str()); - //allow GCC '({ %var%|%num%|%bool% ; })' statement expression extension + //allow GCC '({ %name%|%num%|%bool% ; })' statement expression extension tokenizeAndStringify("void f() {switch (n) { case 0?({0;}):1: z(); break;}}"); ASSERT_EQUALS("", errout.str()); diff --git a/tools/matchcompiler.py b/tools/matchcompiler.py index 0cd21b1b2..64b6bbbff 100755 --- a/tools/matchcompiler.py +++ b/tools/matchcompiler.py @@ -116,8 +116,10 @@ class MatchCompiler: return ( '(tok->isName() && tok->varId()==0U && !tok->isKeyword())' ) - elif tok == '%var%': + elif tok == '%name%': return 'tok->isName()' + elif tok == '%var%': + return 'tok->varId() != 0' elif tok == '%varid%': return '(tok->isName() && tok->varId()==varid)' elif (len(tok) > 2) and (tok[0] == "%"):