Refactorization: Renamed Token::Match pattern %var% to %name%, implement new pattern %var% which is true if varId > 0.

This commit is contained in:
PKEuS 2015-01-31 10:50:39 +01:00
parent e0b77a975f
commit b2835051df
37 changed files with 759 additions and 788 deletions

View File

@ -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);

View File

@ -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..

View File

@ -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;
}

View File

@ -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);

View File

@ -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<const Token *> 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::vector<std::str
continue;
const Token *tok3 = tok->previous();
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<std::str
if (declarationId == 0 && size > 0) {
std::list<const Token *> 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<std::str
}
// Check function call..
if (Token::Match(tok, "%var% (")) {
if (Token::Match(tok, "%name% (")) {
// No varid => 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<const Token*>());
@ -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..

View File

@ -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.
*/

View File

@ -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::list<const Functio
// Class constructor.. initializing variables like this
// clKalle::clKalle() : var(value) { }
if (initList) {
if (level == 0 && Token::Match(ftok, "%var% {|(")) {
if (level == 0 && Token::Match(ftok, "%name% {|(")) {
if (ftok->str() != func.name()) {
initVar(ftok->str(), scope, usage);
} else { // c++11 delegate constructor
@ -471,7 +471,7 @@ void CheckClass::initializeVarList(const Function &func, std::list<const Functio
}
ftok = ftok->next();
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<const Functio
continue;
// Variable getting value from stream?
if (Token::Match(ftok, ">> %var%")) {
if (Token::Match(ftok, ">> %name%")) {
assignVar(ftok->strAt(1), scope, usage);
}
@ -508,13 +508,13 @@ void CheckClass::initializeVarList(const Function &func, std::list<const Functio
}
// Using swap to assign all variables..
if (func.type == Function::eOperatorEqual && Token::Match(ftok, "[;{}] %var% (") && Token::Match(ftok->linkAt(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<Variable>::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::list<const Functio
ftok = ftok->tokAt(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::list<const Functio
ftok = ftok->tokAt(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::list<const Functio
}
// Clearing array..
else if (Token::Match(ftok, "::| memset ( %var% ,")) {
else if (Token::Match(ftok, "::| memset ( %name% ,")) {
if (ftok->str() == "::")
ftok = ftok->next();
assignVar(ftok->strAt(2), scope, usage);
@ -601,7 +601,7 @@ void CheckClass::initializeVarList(const Function &func, std::list<const Functio
else {
assignAllVar(usage);
}
} else if (Token::Match(ftok, "::| %var% (") && ftok->str() != "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::list<const Functio
for (const Token *tok2 = ftok; tok2; tok2 = tok2->next()) {
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::list<const Functio
}
// Assignment of member variable?
else if (Token::Match(ftok, "%var% =")) {
else if (Token::Match(ftok, "%name% =")) {
assignVar(ftok->str(), scope, usage);
}
// Assignment of array item of member variable?
else if (Token::Match(ftok, "%var% [|.")) {
else if (Token::Match(ftok, "%name% [|.")) {
const Token *tok2 = ftok;
while (tok2) {
if (tok2->strAt(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::list<const Functio
}
// Assignment of array item of member variable?
else if (Token::Match(ftok, "* %var% =")) {
else if (Token::Match(ftok, "* %name% =")) {
assignVar(ftok->next()->str(), scope, usage);
} else if (Token::Match(ftok, "* this . %var% =")) {
} else if (Token::Match(ftok, "* this . %name% =")) {
assignVar(ftok->strAt(3), scope, usage);
}
// The functions 'clear' and 'Clear' are supposed to initialize variable.
if (Token::Match(ftok, "%var% . clear|Clear (")) {
if (Token::Match(ftok, "%name% . clear|Clear (")) {
assignVar(ftok->str(), 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<Function>::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));
}
}

View File

@ -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<WS_CHILD|WS_VISIBLE>::..
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;

View File

@ -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 + "\""
);
}

View File

@ -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<unsigned int, Filepointer>::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());

View File

@ -157,14 +157,14 @@ void CheckLeakAutoVar::checkScope(const Token * const startToken,
if (tok->varId() > 0) {
const std::map<unsigned int, VarInfo::AllocInfo>::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;
}

View File

@ -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::list<co
return ret;
}
}
if (Token::Match(tok, "%varid% . %var% [,)]", varid))
if (Token::Match(tok, "%varid% . %name% [,)]", varid))
return "use";
}
return (eq || _settings->experimental) ? 0 : "callfunc";
@ -746,9 +746,9 @@ Token *CheckMemoryLeakInFunction::getcode(const Token *tok, std::list<const Toke
// function calls are interesting..
const Token *tok2 = tok;
while (Token::Match(tok2->next(), "%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::list<const Toke
//TODO: this assignment is redundant, should be fixed
//realloc = true;
tok = tok->tokAt(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::list<const Toke
alloctype = alloc;
if (Token::Match(tok, "%var% = %type% (")) {
if (Token::Match(tok, "%name% = %type% (")) {
tok = tok->linkAt(3);
continue;
}
@ -935,7 +935,7 @@ Token *CheckMemoryLeakInFunction::getcode(const Token *tok, std::list<const Toke
}
}
if (Token::Match(tok->previous(), "%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::list<const Toke
dep = true;
} else if (Token::Match(tok2, "! %varid%", varid)) {
dep = true;
} else if (Token::Match(tok2, "%var% (") && !test_white_list(tok2->str(), _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::list<const Toke
}
}
if (Token::Match(tok, "& %var% = %varid% ;", varid)) {
if (Token::Match(tok, "& %name% = %varid% ;", varid)) {
while (rethead->next())
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::list<const Toke
}
// Investigate function calls..
if (Token::Match(tok, "%var% (")) {
if (Token::Match(tok, "%name% (")) {
// A function call should normally be followed by ";"
if (Token::simpleMatch(tok->next()->link(), ") {")) {
if (!Token::Match(tok, "if|for|while|switch")) {
@ -1294,7 +1294,7 @@ Token *CheckMemoryLeakInFunction::getcode(const Token *tok, std::list<const Toke
}
else {
if (varid > 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::list<const Toke
}
// Callback..
if (Token::Match(tok, "( *| %var%") && Token::simpleMatch(tok->link(),") (")) {
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) + ">";
}
}

View File

@ -46,14 +46,14 @@ static bool checkNullpointerFunctionCallPlausibility(const Function* func, unsig
*/
void CheckNullPointer::parseFunctionCall(const Token &tok, std::list<const Token *> &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::list<const Token
continue;
if ((*i == 'n' || *i == 's' || scan) && (!scan || value == 0)) {
if ((value == 0 && argListTok->str() == "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<unsigned int>& 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<unsigned int> 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;
}

View File

@ -100,8 +100,8 @@ bool isSameExpression(const Token *tok1, const Token *tok2, const std::set<std::
return false;
}
// templates/casts
if ((Token::Match(tok1, "%var% <") && tok1->next()->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<unsigned int, const Token*>::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<int>{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)) {

View File

@ -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;

View File

@ -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<std::multimap<std::string, unsigned int>::const_iterator, std::multimap<std::string, unsigned int>::const_iterator> range = c_strFuncParam.equal_range(tok->str());
for (std::multimap<std::string, unsigned int>::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<unsigned int>::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);

View File

@ -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

View File

@ -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<ExecutionPath *> &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<ExecutionPath *> &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<const Token *> 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::map<unsigned
if (NOT)
vartok = vartok->next();
while (Token::Match(vartok, "%var% . %var%"))
while (Token::Match(vartok, "%name% . %name%"))
vartok = vartok->tokAt(2);
std::map<unsigned int, int>::const_iterator it = variableValue.find(vartok->varId());
@ -1176,26 +1176,26 @@ static void conditionAlwaysTrueOrFalse(const Token *tok, const std::map<unsigned
return;
// always true
if (Token::Match(vartok, "%var% %oror%|)")) {
if (Token::Match(vartok, "%name% %oror%|)")) {
if (NOT)
*alwaysTrue = bool(it->second == 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<unsigned int,int>::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<unsigned int, int> 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<unsigned int, int> 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;

View File

@ -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();

View File

@ -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;

View File

@ -139,7 +139,7 @@ void ExecutionPath::checkScope(const Token *tok, std::list<ExecutionPath *> &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<ExecutionPath *> &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<ExecutionPath *> &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<ExecutionPath *> &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;
}

View File

@ -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)) {

View File

@ -1336,8 +1336,8 @@ std::list<std::string> 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<std::string> 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<std::string> Preprocessor::getcfgs(const std::string &filedata, const
const Token *tok = tokenizer.tokens();
std::set<std::string> 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<std::string, std::string> &c
return;
}
if (Token::Match(tokenizer.tokens(), "( %var% )")) {
if (Token::Match(tokenizer.tokens(), "( %name% )")) {
std::map<std::string,std::string>::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<std::string, std::string> &c
return;
}
if (Token::Match(tokenizer.tokens(), "( ! %var% )")) {
if (Token::Match(tokenizer.tokens(), "( ! %name% )")) {
std::map<std::string,std::string>::const_iterator var = cfg.find(tokenizer.tokens()->strAt(2));
if (var == cfg.end())
@ -1630,7 +1630,7 @@ void Preprocessor::simplifyCondition(const std::map<std::string, std::string> &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<std::string, std::string> &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 += " ";
}

View File

@ -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<Token *>(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<Token *>(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);
}
}

View File

@ -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<Token *> 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<Token *> 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<Token *> &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;

View File

@ -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();

View File

@ -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 &apos;-character.
* - "%comp%" Any token such that isComparisonOp() returns true.
* - "%str%" Any token starting with &quot;-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.

File diff suppressed because it is too large Load Diff

View File

@ -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 :

View File

@ -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);

View File

@ -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<unsigned int, MathLib::bigint> 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<Token*>(parent->astParent())) {
std::stack<Token *> 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<ValueFlow::Value> 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<ValueFlow::Value>::const_iterator val = argvalues.begin(); val != argvalues.end(); ++val)
@ -1660,7 +1655,7 @@ static void valueFlowFunctionReturn(TokenList *tokenlist, ErrorLogger *errorLogg
std::map<unsigned int, MathLib::bigint> 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();

View File

@ -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());
}

View File

@ -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);

View File

@ -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) {

View File

@ -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 {

View File

@ -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());

View File

@ -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] == "%"):