Skip to content

Commit 40cc208

Browse files
authored
reduced Tokenizer::isCPP() usage / SymbolDatabase: removed mIsCpp and isCPP() (#5725)
Each `Token` (should) be connected to a `TokenList`. `Tokenizer` just encapsulates that so we have no need to check the `Tokenizer` but can simply ask the `Token`. Also if we have function calls we pass in a flag to tell it if it is C/C++ we can get rid of that flag and simply ask the `Token`. In several cases we were only passing that flag to be used in some underlying calls making it completely unnecessary in the top call.
1 parent fadc93f commit 40cc208

29 files changed

+445
-488
lines changed

lib/astutils.cpp

Lines changed: 109 additions & 111 deletions
Large diffs are not rendered by default.

lib/astutils.h

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ const Token * astIsVariableComparison(const Token *tok, const std::string &comp,
194194
bool isVariableDecl(const Token* tok);
195195
bool isStlStringType(const Token* tok);
196196

197-
bool isTemporary(bool cpp, const Token* tok, const Library* library, bool unknown = false);
197+
bool isTemporary(const Token* tok, const Library* library, bool unknown = false);
198198

199199
const Token* previousBeforeAstLeftmostLeaf(const Token* tok);
200200
Token* previousBeforeAstLeftmostLeaf(Token* tok);
@@ -208,7 +208,7 @@ const Token* astParentSkipParens(const Token* tok);
208208
const Token* getParentMember(const Token * tok);
209209

210210
const Token* getParentLifetime(const Token* tok);
211-
const Token* getParentLifetime(bool cpp, const Token* tok, const Library* library);
211+
const Token* getParentLifetime(const Token* tok, const Library* library);
212212

213213
std::vector<ValueType> getParentValueTypes(const Token* tok,
214214
const Settings* settings = nullptr,
@@ -261,7 +261,7 @@ SmallVector<ReferenceToken> followAllReferences(const Token* tok,
261261
int depth = 20);
262262
const Token* followReferences(const Token* tok, ErrorPath* errors = nullptr);
263263

264-
CPPCHECKLIB bool isSameExpression(bool cpp, bool macro, const Token *tok1, const Token *tok2, const Library& library, bool pure, bool followVar, ErrorPath* errors=nullptr);
264+
CPPCHECKLIB bool isSameExpression(bool macro, const Token *tok1, const Token *tok2, const Library& library, bool pure, bool followVar, ErrorPath* errors=nullptr);
265265

266266
bool isEqualKnownValue(const Token * const tok1, const Token * const tok2);
267267

@@ -282,21 +282,20 @@ bool compareTokenFlags(const Token* tok1, const Token* tok2, bool macro);
282282
/**
283283
* Are two conditions opposite
284284
* @param isNot do you want to know if cond1 is !cond2 or if cond1 and cond2 are non-overlapping. true: cond1==!cond2 false: cond1==true => cond2==false
285-
* @param cpp c++ file
286285
* @param cond1 condition1
287286
* @param cond2 condition2
288287
* @param library files data
289288
* @param pure boolean
290289
*/
291-
bool isOppositeCond(bool isNot, bool cpp, const Token * const cond1, const Token * const cond2, const Library& library, bool pure, bool followVar, ErrorPath* errors=nullptr);
290+
bool isOppositeCond(bool isNot, const Token * const cond1, const Token * const cond2, const Library& library, bool pure, bool followVar, ErrorPath* errors=nullptr);
292291

293-
bool isOppositeExpression(bool cpp, const Token * const tok1, const Token * const tok2, const Library& library, bool pure, bool followVar, ErrorPath* errors=nullptr);
292+
bool isOppositeExpression(const Token * const tok1, const Token * const tok2, const Library& library, bool pure, bool followVar, ErrorPath* errors=nullptr);
294293

295294
bool isConstFunctionCall(const Token* ftok, const Library& library);
296295

297-
bool isConstExpression(const Token *tok, const Library& library, bool cpp);
296+
bool isConstExpression(const Token *tok, const Library& library);
298297

299-
bool isWithoutSideEffects(bool cpp, const Token* tok, bool checkArrayAccess = false, bool checkReference = true);
298+
bool isWithoutSideEffects(const Token* tok, bool checkArrayAccess = false, bool checkReference = true);
300299

301300
bool isUniqueExpression(const Token* tok);
302301

@@ -341,38 +340,35 @@ bool isVariableChangedByFunctionCall(const Token *tok, int indirect, nonneg int
341340
CPPCHECKLIB bool isVariableChangedByFunctionCall(const Token *tok, int indirect, const Settings *settings, bool *inconclusive);
342341

343342
/** Is variable changed in block of code? */
344-
CPPCHECKLIB bool isVariableChanged(const Token *start, const Token *end, const nonneg int exprid, bool globalvar, const Settings *settings, bool cpp, int depth = 20);
345-
bool isVariableChanged(const Token *start, const Token *end, int indirect, const nonneg int exprid, bool globalvar, const Settings *settings, bool cpp, int depth = 20);
343+
CPPCHECKLIB bool isVariableChanged(const Token *start, const Token *end, const nonneg int exprid, bool globalvar, const Settings *settings, int depth = 20);
344+
bool isVariableChanged(const Token *start, const Token *end, int indirect, const nonneg int exprid, bool globalvar, const Settings *settings, int depth = 20);
346345

347-
bool isVariableChanged(const Token *tok, int indirect, const Settings *settings, bool cpp, int depth = 20);
346+
bool isVariableChanged(const Token *tok, int indirect, const Settings *settings, int depth = 20);
348347

349-
bool isVariableChanged(const Variable * var, const Settings *settings, bool cpp, int depth = 20);
348+
bool isVariableChanged(const Variable * var, const Settings *settings, int depth = 20);
350349

351350
bool isVariablesChanged(const Token* start,
352351
const Token* end,
353352
int indirect,
354353
const std::vector<const Variable*> &vars,
355-
const Settings* settings,
356-
bool cpp);
354+
const Settings* settings);
357355

358-
bool isThisChanged(const Token* tok, int indirect, const Settings* settings, bool cpp);
359-
const Token* findThisChanged(const Token* start, const Token* end, int indirect, const Settings* settings, bool cpp);
356+
bool isThisChanged(const Token* tok, int indirect, const Settings* settings);
357+
const Token* findThisChanged(const Token* start, const Token* end, int indirect, const Settings* settings);
360358

361-
const Token* findVariableChanged(const Token *start, const Token *end, int indirect, const nonneg int exprid, bool globalvar, const Settings *settings, bool cpp, int depth = 20);
362-
Token* findVariableChanged(Token *start, const Token *end, int indirect, const nonneg int exprid, bool globalvar, const Settings *settings, bool cpp, int depth = 20);
359+
const Token* findVariableChanged(const Token *start, const Token *end, int indirect, const nonneg int exprid, bool globalvar, const Settings *settings, int depth = 20);
360+
Token* findVariableChanged(Token *start, const Token *end, int indirect, const nonneg int exprid, bool globalvar, const Settings *settings, int depth = 20);
363361

364362
CPPCHECKLIB const Token* findExpressionChanged(const Token* expr,
365363
const Token* start,
366364
const Token* end,
367365
const Settings* settings,
368-
bool cpp,
369366
int depth = 20);
370367

371368
const Token* findExpressionChangedSkipDeadCode(const Token* expr,
372369
const Token* start,
373370
const Token* end,
374371
const Settings* settings,
375-
bool cpp,
376372
const std::function<std::vector<MathLib::bigint>(const Token* tok)>& evaluate,
377373
int depth = 20);
378374

@@ -381,7 +377,6 @@ bool isExpressionChangedAt(const Token* expr,
381377
int indirect,
382378
bool globalvar,
383379
const Settings* settings,
384-
bool cpp,
385380
int depth = 20);
386381

387382
/// If token is an alias if another variable
@@ -426,14 +421,14 @@ CPPCHECKLIB const Token *findLambdaStartToken(const Token *last);
426421
CPPCHECKLIB const Token *findLambdaEndToken(const Token *first);
427422
CPPCHECKLIB Token* findLambdaEndToken(Token* first);
428423

429-
bool isLikelyStream(bool cpp, const Token *stream);
424+
bool isLikelyStream(const Token *stream);
430425

431426
/**
432427
* do we see a likely write of rhs through overloaded operator
433428
* s >> x;
434429
* a & x;
435430
*/
436-
bool isLikelyStreamRead(bool cpp, const Token *op);
431+
bool isLikelyStreamRead(const Token *op);
437432

438433
bool isCPPCast(const Token* tok);
439434

@@ -443,7 +438,7 @@ bool isLeafDot(const Token* tok);
443438

444439
enum class ExprUsage { None, NotUsed, PassedByReference, Used, Inconclusive };
445440

446-
ExprUsage getExprUsage(const Token* tok, int indirect, const Settings* settings, bool cpp);
441+
ExprUsage getExprUsage(const Token* tok, int indirect, const Settings* settings);
447442

448443
const Variable *getLHSVariable(const Token *tok);
449444

@@ -458,7 +453,7 @@ bool isScopeBracket(const Token* tok);
458453

459454
CPPCHECKLIB bool isNullOperand(const Token *expr);
460455

461-
bool isGlobalData(const Token *expr, bool cpp);
456+
bool isGlobalData(const Token *expr);
462457

463458
bool isUnevaluated(const Token *tok);
464459

lib/checkautovariables.cpp

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ void CheckAutoVariables::autoVariables()
296296
}
297297
// Invalid pointer deallocation
298298
else if ((Token::Match(tok, "%name% ( %var%|%str% ) ;") && mSettings->library.getDeallocFuncInfo(tok)) ||
299-
(mTokenizer->isCPP() && Token::Match(tok, "delete [| ]| (| %var%|%str% !!["))) {
299+
(tok->isCpp() && Token::Match(tok, "delete [| ]| (| %var%|%str% !!["))) {
300300
tok = Token::findmatch(tok->next(), "%var%|%str%");
301301
if (Token::simpleMatch(tok->astParent(), "."))
302302
continue;
@@ -314,7 +314,7 @@ void CheckAutoVariables::autoVariables()
314314
}
315315
}
316316
} else if ((Token::Match(tok, "%name% ( & %var% ) ;") && mSettings->library.getDeallocFuncInfo(tok)) ||
317-
(mTokenizer->isCPP() && Token::Match(tok, "delete [| ]| (| & %var% !!["))) {
317+
(tok->isCpp() && Token::Match(tok, "delete [| ]| (| & %var% !!["))) {
318318
tok = Token::findmatch(tok->next(), "%var%");
319319
if (isAutoVar(tok))
320320
errorInvalidDeallocation(tok, nullptr);
@@ -463,9 +463,9 @@ static int getPointerDepth(const Token *tok)
463463
return n;
464464
}
465465

466-
static bool isDeadTemporary(bool cpp, const Token* tok, const Token* expr, const Library* library)
466+
static bool isDeadTemporary(const Token* tok, const Token* expr, const Library* library)
467467
{
468-
if (!isTemporary(cpp, tok, library))
468+
if (!isTemporary(tok, library))
469469
return false;
470470
if (expr) {
471471
if (!precedes(nextAfterAstRightmostLeaf(tok->astTop()), nextAfterAstRightmostLeaf(expr->astTop())))
@@ -495,7 +495,7 @@ static bool isEscapedReference(const Variable* var)
495495
if (!Token::simpleMatch(varDeclEndToken, "="))
496496
return false;
497497
const Token* vartok = varDeclEndToken->astOperand2();
498-
return !isTemporary(true, vartok, nullptr, false);
498+
return !isTemporary(vartok, nullptr, false);
499499
}
500500

501501
static bool isDanglingSubFunction(const Token* tokvalue, const Token* tok)
@@ -562,7 +562,7 @@ void CheckAutoVariables::checkVarLifetimeScope(const Token * start, const Token
562562
errorReturnReference(tok, lt.errorPath, lt.inconclusive);
563563
break;
564564
}
565-
if (isDeadTemporary(mTokenizer->isCPP(), lt.token, nullptr, &mSettings->library)) {
565+
if (isDeadTemporary(lt.token, nullptr, &mSettings->library)) {
566566
errorReturnTempReference(tok, lt.errorPath, lt.inconclusive);
567567
break;
568568
}
@@ -584,7 +584,7 @@ void CheckAutoVariables::checkVarLifetimeScope(const Token * start, const Token
584584
if (!printInconclusive && lt.inconclusive)
585585
continue;
586586
const Token * tokvalue = lt.token;
587-
if (isDeadTemporary(mTokenizer->isCPP(), tokvalue, tok, &mSettings->library)) {
587+
if (isDeadTemporary(tokvalue, tok, &mSettings->library)) {
588588
errorDanglingTempReference(tok, lt.errorPath, lt.inconclusive);
589589
break;
590590
}
@@ -598,7 +598,7 @@ void CheckAutoVariables::checkVarLifetimeScope(const Token * start, const Token
598598
continue;
599599
if (!printInconclusive && val.isInconclusive())
600600
continue;
601-
const Token* parent = getParentLifetime(mTokenizer->isCPP(), val.tokvalue, &mSettings->library);
601+
const Token* parent = getParentLifetime(val.tokvalue, &mSettings->library);
602602
if (!exprs.insert(parent).second)
603603
continue;
604604
for (const ValueFlow::LifetimeToken& lt : ValueFlow::getLifetimeTokens(parent, escape || isAssignedToNonLocal(tok))) {
@@ -614,15 +614,15 @@ void CheckAutoVariables::checkVarLifetimeScope(const Token * start, const Token
614614
continue;
615615
if ((tokvalue->variable() && !isEscapedReference(tokvalue->variable()) &&
616616
isInScope(tokvalue->variable()->nameToken(), scope)) ||
617-
isDeadTemporary(mTokenizer->isCPP(), tokvalue, nullptr, &mSettings->library)) {
617+
isDeadTemporary(tokvalue, nullptr, &mSettings->library)) {
618618
errorReturnDanglingLifetime(tok, &val);
619619
break;
620620
}
621621
} else if (tokvalue->variable() && isDeadScope(tokvalue->variable()->nameToken(), tok->scope())) {
622622
errorInvalidLifetime(tok, &val);
623623
break;
624624
} else if (!tokvalue->variable() &&
625-
isDeadTemporary(mTokenizer->isCPP(), tokvalue, tok, &mSettings->library)) {
625+
isDeadTemporary(tokvalue, tok, &mSettings->library)) {
626626
if (!diag(tokvalue))
627627
errorDanglingTemporaryLifetime(tok, &val, tokvalue);
628628
break;
@@ -650,8 +650,7 @@ void CheckAutoVariables::checkVarLifetimeScope(const Token * start, const Token
650650
tok->scope()->bodyEnd,
651651
var->declarationId(),
652652
var->isGlobal(),
653-
mSettings,
654-
mTokenizer->isCPP())) {
653+
mSettings)) {
655654
errorDanglngLifetime(tok2, &val);
656655
break;
657656
}

lib/checkbool.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ void CheckBool::checkBitwiseOnBoolean()
122122
if (tok->str() == "|" && !isConvertedToBool(tok) && !(isBoolOp1 && isBoolOp2))
123123
continue;
124124
// first operand will always be evaluated
125-
if (!isConstExpression(tok->astOperand2(), mSettings->library, mTokenizer->isCPP()))
125+
if (!isConstExpression(tok->astOperand2(), mSettings->library))
126126
continue;
127127
if (tok->astOperand2()->variable() && tok->astOperand2()->variable()->nameToken() == tok->astOperand2())
128128
continue;

lib/checkbufferoverrun.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ void CheckBufferOverrun::arrayIndex()
327327
const Token* changeTok = var->scope()->bodyStart;
328328
bool isChanged = false;
329329
while ((changeTok = findVariableChanged(changeTok->next(), var->scope()->bodyEnd, /*indirect*/ 0, var->declarationId(),
330-
/*globalvar*/ false, mSettings, mTokenizer->isCPP()))) {
330+
/*globalvar*/ false, mSettings))) {
331331
if (!Token::simpleMatch(changeTok->astParent(), "[")) {
332332
isChanged = true;
333333
break;
@@ -793,7 +793,7 @@ void CheckBufferOverrun::stringNotZeroTerminated()
793793
const Token *rhs = tok2->next()->astOperand2();
794794
if (!rhs || !rhs->hasKnownIntValue() || rhs->getKnownIntValue() != 0)
795795
continue;
796-
if (isSameExpression(mTokenizer->isCPP(), false, args[0], tok2->link()->astOperand1(), mSettings->library, false, false))
796+
if (isSameExpression(false, args[0], tok2->link()->astOperand1(), mSettings->library, false, false))
797797
isZeroTerminated = true;
798798
}
799799
if (isZeroTerminated)
@@ -1090,7 +1090,7 @@ void CheckBufferOverrun::objectIndex()
10901090
if (var->valueType()->pointer > obj->valueType()->pointer)
10911091
continue;
10921092
}
1093-
if (obj->valueType() && var->valueType() && (obj->isCast() || (mTokenizer->isCPP() && isCPPCast(obj)) || obj->valueType()->pointer)) { // allow cast to a different type
1093+
if (obj->valueType() && var->valueType() && (obj->isCast() || (obj->isCpp() && isCPPCast(obj)) || obj->valueType()->pointer)) { // allow cast to a different type
10941094
const auto varSize = var->valueType()->typeSize(mSettings->platform);
10951095
if (varSize == 0)
10961096
continue;

lib/checkclass.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ void CheckClass::copyconstructors()
503503
}
504504
}
505505
for (tok = func.functionScope->bodyStart; tok != func.functionScope->bodyEnd; tok = tok->next()) {
506-
if ((mTokenizer->isCPP() && Token::Match(tok, "%var% = new")) ||
506+
if ((tok->isCpp() && Token::Match(tok, "%var% = new")) ||
507507
(Token::Match(tok, "%var% = %name% (") && (mSettings->library.getAllocFuncInfo(tok->tokAt(2)) || mSettings->library.getReallocFuncInfo(tok->tokAt(2))))) {
508508
allocatedVars.erase(tok->varId());
509509
} else if (Token::Match(tok, "%var% = %name% . %name% ;") && allocatedVars.find(tok->varId()) != allocatedVars.end()) {
@@ -817,7 +817,7 @@ void CheckClass::initializeVarList(const Function &func, std::list<const Functio
817817
continue;
818818

819819
// Variable getting value from stream?
820-
if (Token::Match(ftok, ">>|& %name%") && isLikelyStreamRead(true, ftok)) {
820+
if (Token::Match(ftok, ">>|& %name%") && isLikelyStreamRead(ftok)) {
821821
assignVar(usage, ftok->next()->varId());
822822
}
823823

@@ -1771,7 +1771,7 @@ bool CheckClass::hasAllocation(const Function *func, const Scope* scope, const T
17711771
if (!end)
17721772
end = func->functionScope->bodyEnd;
17731773
for (const Token *tok = start; tok && (tok != end); tok = tok->next()) {
1774-
if (((mTokenizer->isCPP() && Token::Match(tok, "%var% = new")) ||
1774+
if (((tok->isCpp() && Token::Match(tok, "%var% = new")) ||
17751775
(Token::Match(tok, "%var% = %name% (") && mSettings->library.getAllocFuncInfo(tok->tokAt(2)))) &&
17761776
isMemberVar(scope, tok))
17771777
return true;
@@ -1780,9 +1780,9 @@ bool CheckClass::hasAllocation(const Function *func, const Scope* scope, const T
17801780
const Token *var;
17811781
if (Token::Match(tok, "%name% ( %var%") && mSettings->library.getDeallocFuncInfo(tok))
17821782
var = tok->tokAt(2);
1783-
else if (mTokenizer->isCPP() && Token::Match(tok, "delete [ ] %var%"))
1783+
else if (tok->isCpp() && Token::Match(tok, "delete [ ] %var%"))
17841784
var = tok->tokAt(3);
1785-
else if (mTokenizer->isCPP() && Token::Match(tok, "delete %var%"))
1785+
else if (tok->isCpp() && Token::Match(tok, "delete %var%"))
17861786
var = tok->next();
17871787
else
17881788
continue;
@@ -2552,7 +2552,7 @@ bool CheckClass::checkConstFunc(const Scope *scope, const Function *func, Member
25522552
// Streaming
25532553
else if (end->strAt(1) == "<<" && tok1->strAt(-1) != "<<")
25542554
return false;
2555-
else if (isLikelyStreamRead(true, tok1->previous()))
2555+
else if (isLikelyStreamRead(tok1->previous()))
25562556
return false;
25572557

25582558
// ++/--
@@ -2581,7 +2581,7 @@ bool CheckClass::checkConstFunc(const Scope *scope, const Function *func, Member
25812581
}
25822582

25832583
// streaming: >> *this
2584-
else if (Token::simpleMatch(tok1, ">> * this") && isLikelyStreamRead(true, tok1)) {
2584+
else if (Token::simpleMatch(tok1, ">> * this") && isLikelyStreamRead(tok1)) {
25852585
return false;
25862586
}
25872587

0 commit comments

Comments
 (0)