Skip to content

Commit b283505

Browse files
committed
Refactorization: Renamed Token::Match pattern %var% to %name%, implement new pattern %var% which is true if varId > 0.
1 parent e0b77a9 commit b283505

37 files changed

+759
-788
lines changed

lib/check64bit.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ void Check64BitPortability::pointerassignment()
6565
continue;
6666

6767
for (const Token* tok = scope->classStart->next(); tok != scope->classEnd; tok = tok->next()) {
68-
if (Token::Match(tok, "return %var%|%num% [;+]") && !Token::simpleMatch(tok, "return 0 ;")) {
68+
if (Token::Match(tok, "return %name%|%num% [;+]") && !Token::simpleMatch(tok, "return 0 ;")) {
6969
enum { NO, INT, PTR, PTRDIFF } type = NO;
7070
for (const Token *tok2 = tok->next(); tok2; tok2 = tok2->next()) {
7171
if ((type == NO || type == INT) && Token::Match(tok2, "%var% [+;]") && isaddr(tok2->variable()))
@@ -96,7 +96,7 @@ void Check64BitPortability::pointerassignment()
9696
for (std::size_t i = 0; i < functions; ++i) {
9797
const Scope * scope = symbolDatabase->functionScopes[i];
9898
for (const Token *tok = scope->classStart; tok && tok != scope->classEnd; tok = tok->next()) {
99-
if (Token::Match(tok, "[;{}] %var% = %var%")) {
99+
if (Token::Match(tok, "[;{}] %var% = %name%")) {
100100
const Token* tok2 = tok->tokAt(3);
101101
while (Token::Match(tok2->next(), ".|::"))
102102
tok2 = tok2->tokAt(2);

lib/checkautovariables.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,12 @@ static bool checkRvalueExpression(const Token * const vartok)
8686
if (var == nullptr)
8787
return false;
8888

89-
if (Token::Match(vartok->previous(), "& %var% [") && var->isPointer())
89+
if (Token::Match(vartok->previous(), "& %name% [") && var->isPointer())
9090
return false;
9191

9292
const Token * const next = vartok->next();
9393
// &a.b[0]
94-
if (Token::Match(vartok, "%var% . %var% [") && !var->isPointer()) {
94+
if (Token::Match(vartok, "%name% . %var% [") && !var->isPointer()) {
9595
const Variable *var2 = next->next()->variable();
9696
return var2 && !var2->isPointer();
9797
}
@@ -421,7 +421,7 @@ void CheckAutoVariables::returnReference()
421421
}
422422

423423
// return reference to temporary..
424-
else if (Token::Match(tok2, "return %var% (") &&
424+
else if (Token::Match(tok2, "return %name% (") &&
425425
Token::simpleMatch(tok2->linkAt(2), ") ;")) {
426426
if (returnTemporary(tok2->next())) {
427427
// report error..

lib/checkbool.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ void CheckBool::checkIncrementBoolean()
4646
for (std::size_t i = 0; i < functions; ++i) {
4747
const Scope * scope = symbolDatabase->functionScopes[i];
4848
for (const Token* tok = scope->classStart->next(); tok != scope->classEnd; tok = tok->next()) {
49-
if (tok->variable() && Token::Match(tok, "%var% ++")) {
49+
if (Token::Match(tok, "%var% ++")) {
5050
const Variable *var = tok->variable();
5151
if (var && var->typeEndToken()->str() == "bool")
5252
incrementBooleanError(tok);
@@ -202,7 +202,7 @@ void CheckBool::comparisonOfBoolWithInvalidComparator(const Token *tok, const st
202202
static bool tokenIsFunctionReturningBool(const Token* tok)
203203
{
204204
const Function* func = tok->function();
205-
if (func && Token::Match(tok, "%var% (")) {
205+
if (func && Token::Match(tok, "%name% (")) {
206206
if (func->tokenDef && func->tokenDef->strAt(-1) == "bool") {
207207
return true;
208208
}

lib/checkboost.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,10 @@ void CheckBoost::checkBoostForeachModification()
3838
if (!Token::Match(containerTok, "%var% ) {"))
3939
continue;
4040

41-
const unsigned int containerId = containerTok->varId();
42-
if (containerId == 0)
43-
continue;
44-
4541
const Token *tok2 = containerTok->tokAt(2);
4642
const Token *end = tok2->link();
4743
for (; tok2 != end; tok2 = tok2->next()) {
48-
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)) {
44+
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())) {
4945
const Token* nextStatement = Token::findsimplematch(tok2->linkAt(3), ";", end);
5046
if (!Token::Match(nextStatement, "; break|return|throw"))
5147
boostForeachError(tok2);

lib/checkbufferoverrun.cpp

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -405,11 +405,11 @@ void CheckBufferOverrun::checkFunctionParameter(const Token &ftok, unsigned int
405405
break;
406406

407407
if (ftok2->varId() == parameter->declarationId()) {
408-
if (Token::Match(ftok2->previous(), "-- %var%") ||
409-
Token::Match(ftok2, "%var% --"))
408+
if (Token::Match(ftok2->previous(), "-- %name%") ||
409+
Token::Match(ftok2, "%name% --"))
410410
break;
411411

412-
if (Token::Match(ftok2->previous(), ";|{|}|%op% %var% [ %num% ]")) {
412+
if (Token::Match(ftok2->previous(), ";|{|}|%op% %name% [ %num% ]")) {
413413
const MathLib::bigint index = MathLib::toLongNumber(ftok2->strAt(2));
414414
if (index >= 0 && arrayInfo.num(0) > 0 && index >= arrayInfo.num(0)) {
415415
std::list<const Token *> callstack2(callstack);
@@ -424,7 +424,7 @@ void CheckBufferOverrun::checkFunctionParameter(const Token &ftok, unsigned int
424424
}
425425

426426
// Calling function..
427-
if (Token::Match(ftok2, "%var% (")) {
427+
if (Token::Match(ftok2, "%name% (")) {
428428
ArrayInfo ai(arrayInfo);
429429
ai.declarationId(parameter->declarationId());
430430
checkFunctionCall(ftok2, ai, callstack);
@@ -603,7 +603,7 @@ void CheckBufferOverrun::checkScope(const Token *tok, const std::vector<std::str
603603
continue;
604604

605605
const Token *tok3 = tok->previous();
606-
while (tok3 && Token::Match(tok3->previous(), "%var% ."))
606+
while (tok3 && Token::Match(tok3->previous(), "%name% ."))
607607
tok3 = tok3->tokAt(-2);
608608

609609
// taking address of 1 past end?
@@ -646,9 +646,9 @@ void CheckBufferOverrun::checkScope(const Token *tok, const std::vector<std::str
646646
if (declarationId == 0 && size > 0) {
647647
std::list<const Token *> callstack;
648648
callstack.push_back(tok);
649-
if (Token::Match(tok, ("%var% ( " + varnames + " ,").c_str()))
649+
if (Token::Match(tok, ("%name% ( " + varnames + " ,").c_str()))
650650
checkFunctionParameter(*tok, 1, arrayInfo, callstack);
651-
if (Token::Match(tok, ("%var% ( %var% , " + varnames + " ,").c_str()))
651+
if (Token::Match(tok, ("%name% ( %name% , " + varnames + " ,").c_str()))
652652
checkFunctionParameter(*tok, 2, arrayInfo, callstack);
653653
}
654654

@@ -705,7 +705,7 @@ void CheckBufferOverrun::checkScope(const Token *tok, const std::vector<std::str
705705
}
706706

707707
// Check function call..
708-
if (Token::Match(tok, "%var% (")) {
708+
if (Token::Match(tok, "%name% (")) {
709709
// No varid => function calls are not handled
710710
if (declarationId == 0)
711711
continue;
@@ -739,7 +739,7 @@ void CheckBufferOverrun::valueFlowCheckArrayIndex(const Token * const tok, const
739739
/*
740740
{
741741
const Token *parent = tok->astParent();
742-
while (Token::Match(parent, "%var%|::|*|&"))
742+
while (Token::Match(parent, "%name%|::|*|&"))
743743
parent = parent->astParent();
744744
if (parent && !Token::simpleMatch(parent, "="))
745745
return;
@@ -749,7 +749,7 @@ void CheckBufferOverrun::valueFlowCheckArrayIndex(const Token * const tok, const
749749
bool addressOf = false;
750750
{
751751
const Token *tok2 = tok->astParent();
752-
while (Token::Match(tok2, "%var%|.|::|["))
752+
while (Token::Match(tok2, "%name%|.|::|["))
753753
tok2 = tok2->astParent();
754754
addressOf = tok2 && tok2->str() == "&" && !(tok2->astOperand1() && tok2->astOperand2());
755755
}
@@ -903,7 +903,7 @@ void CheckBufferOverrun::checkScope(const Token *tok, const ArrayInfo &arrayInfo
903903
else if (!tok->scope()->isExecutable()) // No executable code outside of executable scope - continue to increase performance
904904
continue;
905905

906-
else if (Token::Match(tok, "%var% (")) {
906+
else if (Token::Match(tok, "%name% (")) {
907907
// Check function call..
908908
checkFunctionCall(tok, arrayInfo, std::list<const Token*>());
909909

@@ -1003,7 +1003,7 @@ void CheckBufferOverrun::checkScope(const Token *tok, const ArrayInfo &arrayInfo
10031003
//---------------------------------------------------------------------------
10041004
bool CheckBufferOverrun::isArrayOfStruct(const Token* tok, int &position)
10051005
{
1006-
if (Token::Match(tok->next(), "%var% [ %num% ]")) {
1006+
if (Token::Match(tok->next(), "%name% [ %num% ]")) {
10071007
tok = tok->tokAt(4);
10081008
int i = 1;
10091009
for (;;) {
@@ -1284,8 +1284,8 @@ void CheckBufferOverrun::checkStructVariable()
12841284
// dynamically allocated so could be variable sized structure
12851285
if (tok3->next()->str() == "*") {
12861286
// check for allocation
1287-
if ((Token::Match(tok3->tokAt(3), "; %var% = malloc ( %num% ) ;") ||
1288-
(Token::Match(tok3->tokAt(3), "; %var% = (") &&
1287+
if ((Token::Match(tok3->tokAt(3), "; %name% = malloc ( %num% ) ;") ||
1288+
(Token::Match(tok3->tokAt(3), "; %name% = (") &&
12891289
Token::Match(tok3->linkAt(6), ") malloc ( %num% ) ;"))) &&
12901290
(tok3->strAt(4) == tok3->strAt(2))) {
12911291
MathLib::bigint size;
@@ -1384,7 +1384,7 @@ void CheckBufferOverrun::bufferOverrun2()
13841384
// singlepass checking using ast, symboldatabase and valueflow
13851385
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) {
13861386
// Array index
1387-
if (!Token::Match(tok, "%var% ["))
1387+
if (!Token::Match(tok, "%name% ["))
13881388
continue;
13891389

13901390
// TODO: what to do about negative index..
@@ -1578,15 +1578,15 @@ void CheckBufferOverrun::checkBufferAllocatedWithStrlen()
15781578
unsigned int srcVarId;
15791579

15801580
// Look for allocation of a buffer based on the size of a string
1581-
if (Token::Match(tok, "%var% = malloc|g_malloc|g_try_malloc ( strlen ( %var% ) )")) {
1581+
if (Token::Match(tok, "%var% = malloc|g_malloc|g_try_malloc ( strlen ( %name% ) )")) {
15821582
dstVarId = tok->varId();
15831583
srcVarId = tok->tokAt(6)->varId();
15841584
tok = tok->tokAt(8);
1585-
} else if (Token::Match(tok, "%var% = new char [ strlen ( %var% ) ]")) {
1585+
} else if (Token::Match(tok, "%var% = new char [ strlen ( %name% ) ]")) {
15861586
dstVarId = tok->varId();
15871587
srcVarId = tok->tokAt(7)->varId();
15881588
tok = tok->tokAt(9);
1589-
} else if (Token::Match(tok, "%var% = realloc|g_realloc|g_try_realloc ( %var% , strlen ( %var% ) )")) {
1589+
} else if (Token::Match(tok, "%var% = realloc|g_realloc|g_try_realloc ( %name% , strlen ( %name% ) )")) {
15901590
dstVarId = tok->varId();
15911591
srcVarId = tok->tokAt(8)->varId();
15921592
tok = tok->tokAt(10);
@@ -1625,12 +1625,12 @@ void CheckBufferOverrun::checkStringArgument()
16251625
for (std::size_t functionIndex = 0; functionIndex < functions; ++functionIndex) {
16261626
const Scope * const scope = symbolDatabase->functionScopes[functionIndex];
16271627
for (const Token *tok = scope->classStart; tok != scope->classEnd; tok = tok->next()) {
1628-
if (!Token::Match(tok, "%var% (") || !_settings->library.hasminsize(tok->str()))
1628+
if (!Token::Match(tok, "%name% (") || !_settings->library.hasminsize(tok->str()))
16291629
continue;
16301630

16311631
unsigned int argnr = 1;
16321632
for (const Token *argtok = tok->tokAt(2); argtok; argtok = argtok->nextArgument(), argnr++) {
1633-
if (!Token::Match(argtok, "%var%|%str% ,|)"))
1633+
if (!Token::Match(argtok, "%name%|%str% ,|)"))
16341634
continue;
16351635
const Token *strtoken = argtok->getValueTokenMinStrSize();
16361636
if (!strtoken)
@@ -1673,8 +1673,7 @@ void CheckBufferOverrun::checkInsecureCmdLineArgs()
16731673

16741674
} else if (Token::Match(tok, "main ( int %var% , char * * %var% ,|)")) {
16751675
varid = tok->tokAt(8)->varId();
1676-
}
1677-
if (varid == 0)
1676+
} else
16781677
continue;
16791678

16801679
// Jump to the opening curly brace
@@ -1688,15 +1687,15 @@ void CheckBufferOverrun::checkInsecureCmdLineArgs()
16881687

16891688
// Match common patterns that can result in a buffer overrun
16901689
// e.g. strcpy(buffer, argv[0])
1691-
if (Token::Match(tok, "strcpy|strcat ( %var% , * %varid%", varid) ||
1692-
Token::Match(tok, "strcpy|strcat ( %var% , %varid% [", varid)) {
1690+
if (Token::Match(tok, "strcpy|strcat ( %name% , * %varid%", varid) ||
1691+
Token::Match(tok, "strcpy|strcat ( %name% , %varid% [", varid)) {
16931692
cmdLineArgsError(tok);
16941693
tok = tok->linkAt(1);
1695-
} else if (Token::Match(tok, "sprintf ( %var% , %str% , %varid% [", varid) &&
1694+
} else if (Token::Match(tok, "sprintf ( %name% , %str% , %varid% [", varid) &&
16961695
tok->strAt(4).find("%s") != std::string::npos) {
16971696
cmdLineArgsError(tok);
16981697
tok = tok->linkAt(1);
1699-
} else if (Token::Match(tok, "sprintf ( %var% , %str% , * %varid%", varid) &&
1698+
} else if (Token::Match(tok, "sprintf ( %name% , %str% , * %varid%", varid) &&
17001699
tok->strAt(4).find("%s") != std::string::npos) {
17011700
cmdLineArgsError(tok);
17021701
tok = tok->linkAt(1);
@@ -1783,12 +1782,10 @@ void CheckBufferOverrun::arrayIndexThenCheck()
17831782
for (std::size_t i = 0; i < functions; ++i) {
17841783
const Scope * const scope = symbolDatabase->functionScopes[i];
17851784
for (const Token *tok = scope->classStart; tok && tok != scope->classEnd; tok = tok->next()) {
1786-
if (Token::Match(tok, "%var% [ %var% ]")) {
1785+
if (Token::Match(tok, "%name% [ %var% ]")) {
17871786
tok = tok->tokAt(2);
1788-
unsigned int indexID = tok->varId();
1789-
if (!indexID)
1790-
continue;
17911787

1788+
unsigned int indexID = tok->varId();
17921789
const std::string& indexName(tok->str());
17931790

17941791
// skip array index..

lib/checkbufferoverrun.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ class CPPCHECKLIB CheckBufferOverrun : public Check {
194194

195195
/**
196196
* Helper function that checks if the array is used and if so calls the checkFunctionCall
197-
* @param tok token that matches "%var% ("
197+
* @param tok token that matches "%name% ("
198198
* @param arrayInfo the array information
199199
* \param callstack call stack. This is used to prevent recursion and to provide better error messages. Pass a empty list from checkScope etc.
200200
*/

0 commit comments

Comments
 (0)