Skip to content

Commit 3707019

Browse files
committed
reduced usage of Tokenizer::isCPP()
1 parent 41e6c87 commit 3707019

28 files changed

Lines changed: 390 additions & 420 deletions

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
@@ -178,7 +178,7 @@ const Token * astIsVariableComparison(const Token *tok, const std::string &comp,
178178
bool isVariableDecl(const Token* tok);
179179
bool isStlStringType(const Token* tok);
180180

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

183183
const Token* previousBeforeAstLeftmostLeaf(const Token* tok);
184184
Token* previousBeforeAstLeftmostLeaf(Token* tok);
@@ -192,7 +192,7 @@ const Token* astParentSkipParens(const Token* tok);
192192
const Token* getParentMember(const Token * tok);
193193

194194
const Token* getParentLifetime(const Token* tok);
195-
const Token* getParentLifetime(bool cpp, const Token* tok, const Library* library);
195+
const Token* getParentLifetime(const Token* tok, const Library* library);
196196

197197
std::vector<ValueType> getParentValueTypes(const Token* tok,
198198
const Settings* settings = nullptr,
@@ -245,7 +245,7 @@ SmallVector<ReferenceToken> followAllReferences(const Token* tok,
245245
int depth = 20);
246246
const Token* followReferences(const Token* tok, ErrorPath* errors = nullptr);
247247

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

250250
bool isEqualKnownValue(const Token * const tok1, const Token * const tok2);
251251

@@ -261,21 +261,20 @@ CPPCHECKLIB bool isUsedAsBool(const Token* const tok, const Settings* settings =
261261
/**
262262
* Are two conditions opposite
263263
* @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
264-
* @param cpp c++ file
265264
* @param cond1 condition1
266265
* @param cond2 condition2
267266
* @param library files data
268267
* @param pure boolean
269268
*/
270-
bool isOppositeCond(bool isNot, bool cpp, const Token * const cond1, const Token * const cond2, const Library& library, bool pure, bool followVar, ErrorPath* errors=nullptr);
269+
bool isOppositeCond(bool isNot, const Token * const cond1, const Token * const cond2, const Library& library, bool pure, bool followVar, ErrorPath* errors=nullptr);
271270

272-
bool isOppositeExpression(bool cpp, const Token * const tok1, const Token * const tok2, const Library& library, bool pure, bool followVar, ErrorPath* errors=nullptr);
271+
bool isOppositeExpression(const Token * const tok1, const Token * const tok2, const Library& library, bool pure, bool followVar, ErrorPath* errors=nullptr);
273272

274273
bool isConstFunctionCall(const Token* ftok, const Library& library);
275274

276-
bool isConstExpression(const Token *tok, const Library& library, bool cpp);
275+
bool isConstExpression(const Token *tok, const Library& library);
277276

278-
bool isWithoutSideEffects(bool cpp, const Token* tok, bool checkArrayAccess = false, bool checkReference = true);
277+
bool isWithoutSideEffects(const Token* tok, bool checkArrayAccess = false, bool checkReference = true);
279278

280279
bool isUniqueExpression(const Token* tok);
281280

@@ -320,38 +319,35 @@ bool isVariableChangedByFunctionCall(const Token *tok, int indirect, nonneg int
320319
CPPCHECKLIB bool isVariableChangedByFunctionCall(const Token *tok, int indirect, const Settings *settings, bool *inconclusive);
321320

322321
/** Is variable changed in block of code? */
323-
CPPCHECKLIB bool isVariableChanged(const Token *start, const Token *end, const nonneg int exprid, bool globalvar, const Settings *settings, bool cpp, int depth = 20);
324-
bool isVariableChanged(const Token *start, const Token *end, int indirect, const nonneg int exprid, bool globalvar, const Settings *settings, bool cpp, int depth = 20);
322+
CPPCHECKLIB bool isVariableChanged(const Token *start, const Token *end, const nonneg int exprid, bool globalvar, const Settings *settings, int depth = 20);
323+
bool isVariableChanged(const Token *start, const Token *end, int indirect, const nonneg int exprid, bool globalvar, const Settings *settings, int depth = 20);
325324

326-
bool isVariableChanged(const Token *tok, int indirect, const Settings *settings, bool cpp, int depth = 20);
325+
bool isVariableChanged(const Token *tok, int indirect, const Settings *settings, int depth = 20);
327326

328-
bool isVariableChanged(const Variable * var, const Settings *settings, bool cpp, int depth = 20);
327+
bool isVariableChanged(const Variable * var, const Settings *settings, int depth = 20);
329328

330329
bool isVariablesChanged(const Token* start,
331330
const Token* end,
332331
int indirect,
333332
const std::vector<const Variable*> &vars,
334-
const Settings* settings,
335-
bool cpp);
333+
const Settings* settings);
336334

337-
bool isThisChanged(const Token* tok, int indirect, const Settings* settings, bool cpp);
338-
const Token* findThisChanged(const Token* start, const Token* end, int indirect, const Settings* settings, bool cpp);
335+
bool isThisChanged(const Token* tok, int indirect, const Settings* settings);
336+
const Token* findThisChanged(const Token* start, const Token* end, int indirect, const Settings* settings);
339337

340-
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);
341-
Token* findVariableChanged(Token *start, const Token *end, int indirect, const nonneg int exprid, bool globalvar, const Settings *settings, bool cpp, int depth = 20);
338+
const Token* findVariableChanged(const Token *start, const Token *end, int indirect, const nonneg int exprid, bool globalvar, const Settings *settings, int depth = 20);
339+
Token* findVariableChanged(Token *start, const Token *end, int indirect, const nonneg int exprid, bool globalvar, const Settings *settings, int depth = 20);
342340

343341
CPPCHECKLIB const Token* findExpressionChanged(const Token* expr,
344342
const Token* start,
345343
const Token* end,
346344
const Settings* settings,
347-
bool cpp,
348345
int depth = 20);
349346

350347
const Token* findExpressionChangedSkipDeadCode(const Token* expr,
351348
const Token* start,
352349
const Token* end,
353350
const Settings* settings,
354-
bool cpp,
355351
const std::function<std::vector<MathLib::bigint>(const Token* tok)>& evaluate,
356352
int depth = 20);
357353

@@ -360,7 +356,6 @@ bool isExpressionChangedAt(const Token* expr,
360356
int indirect,
361357
bool globalvar,
362358
const Settings* settings,
363-
bool cpp,
364359
int depth = 20);
365360

366361
/// If token is an alias if another variable
@@ -405,14 +400,14 @@ CPPCHECKLIB const Token *findLambdaStartToken(const Token *last);
405400
CPPCHECKLIB const Token *findLambdaEndToken(const Token *first);
406401
CPPCHECKLIB Token* findLambdaEndToken(Token* first);
407402

408-
bool isLikelyStream(bool cpp, const Token *stream);
403+
bool isLikelyStream(const Token *stream);
409404

410405
/**
411406
* do we see a likely write of rhs through overloaded operator
412407
* s >> x;
413408
* a & x;
414409
*/
415-
bool isLikelyStreamRead(bool cpp, const Token *op);
410+
bool isLikelyStreamRead(const Token *op);
416411

417412
bool isCPPCast(const Token* tok);
418413

@@ -422,7 +417,7 @@ bool isLeafDot(const Token* tok);
422417

423418
enum class ExprUsage { None, NotUsed, PassedByReference, Used, Inconclusive };
424419

425-
ExprUsage getExprUsage(const Token* tok, int indirect, const Settings* settings, bool cpp);
420+
ExprUsage getExprUsage(const Token* tok, int indirect, const Settings* settings);
426421

427422
const Variable *getLHSVariable(const Token *tok);
428423

@@ -437,7 +432,7 @@ bool isScopeBracket(const Token* tok);
437432

438433
CPPCHECKLIB bool isNullOperand(const Token *expr);
439434

440-
bool isGlobalData(const Token *expr, bool cpp);
435+
bool isGlobalData(const Token *expr);
441436

442437
bool isUnevaluated(const Token *tok);
443438

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
@@ -120,7 +120,7 @@ void CheckBool::checkBitwiseOnBoolean()
120120
if (tok->str() == "|" && !isConvertedToBool(tok) && !(isBoolOp1 && isBoolOp2))
121121
continue;
122122
// first operand will always be evaluated
123-
if (!isConstExpression(tok->astOperand2(), mSettings->library, mTokenizer->isCPP()))
123+
if (!isConstExpression(tok->astOperand2(), mSettings->library))
124124
continue;
125125
if (tok->astOperand2()->variable() && tok->astOperand2()->variable()->nameToken() == tok->astOperand2())
126126
continue;

lib/checkbufferoverrun.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ void CheckBufferOverrun::arrayIndex()
326326
const Token* changeTok = var->scope()->bodyStart;
327327
bool isChanged = false;
328328
while ((changeTok = findVariableChanged(changeTok->next(), var->scope()->bodyEnd, /*indirect*/ 0, var->declarationId(),
329-
/*globalvar*/ false, mSettings, mTokenizer->isCPP()))) {
329+
/*globalvar*/ false, mSettings))) {
330330
if (!Token::simpleMatch(changeTok->astParent(), "[")) {
331331
isChanged = true;
332332
break;
@@ -792,7 +792,7 @@ void CheckBufferOverrun::stringNotZeroTerminated()
792792
const Token *rhs = tok2->next()->astOperand2();
793793
if (!rhs || !rhs->hasKnownIntValue() || rhs->getKnownIntValue() != 0)
794794
continue;
795-
if (isSameExpression(mTokenizer->isCPP(), false, args[0], tok2->link()->astOperand1(), mSettings->library, false, false))
795+
if (isSameExpression(false, args[0], tok2->link()->astOperand1(), mSettings->library, false, false))
796796
isZeroTerminated = true;
797797
}
798798
if (isZeroTerminated)
@@ -1081,7 +1081,7 @@ void CheckBufferOverrun::objectIndex()
10811081
if (var->valueType()->pointer > obj->valueType()->pointer)
10821082
continue;
10831083
}
1084-
if (obj->valueType() && var->valueType() && (obj->isCast() || (mTokenizer->isCPP() && isCPPCast(obj)) || obj->valueType()->pointer)) { // allow cast to a different type
1084+
if (obj->valueType() && var->valueType() && (obj->isCast() || (obj->isCpp() && isCPPCast(obj)) || obj->valueType()->pointer)) { // allow cast to a different type
10851085
const auto varSize = var->valueType()->typeSize(mSettings->platform);
10861086
if (varSize == 0)
10871087
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

@@ -1767,7 +1767,7 @@ bool CheckClass::hasAllocation(const Function *func, const Scope* scope, const T
17671767
if (!end)
17681768
end = func->functionScope->bodyEnd;
17691769
for (const Token *tok = start; tok && (tok != end); tok = tok->next()) {
1770-
if (((mTokenizer->isCPP() && Token::Match(tok, "%var% = new")) ||
1770+
if (((tok->isCpp() && Token::Match(tok, "%var% = new")) ||
17711771
(Token::Match(tok, "%var% = %name% (") && mSettings->library.getAllocFuncInfo(tok->tokAt(2)))) &&
17721772
isMemberVar(scope, tok))
17731773
return true;
@@ -1776,9 +1776,9 @@ bool CheckClass::hasAllocation(const Function *func, const Scope* scope, const T
17761776
const Token *var;
17771777
if (Token::Match(tok, "%name% ( %var%") && mSettings->library.getDeallocFuncInfo(tok))
17781778
var = tok->tokAt(2);
1779-
else if (mTokenizer->isCPP() && Token::Match(tok, "delete [ ] %var%"))
1779+
else if (tok->isCpp() && Token::Match(tok, "delete [ ] %var%"))
17801780
var = tok->tokAt(3);
1781-
else if (mTokenizer->isCPP() && Token::Match(tok, "delete %var%"))
1781+
else if (tok->isCpp() && Token::Match(tok, "delete %var%"))
17821782
var = tok->next();
17831783
else
17841784
continue;
@@ -2546,7 +2546,7 @@ bool CheckClass::checkConstFunc(const Scope *scope, const Function *func, Member
25462546
// Streaming
25472547
else if (end->strAt(1) == "<<" && tok1->strAt(-1) != "<<")
25482548
return false;
2549-
else if (isLikelyStreamRead(true, tok1->previous()))
2549+
else if (isLikelyStreamRead(tok1->previous()))
25502550
return false;
25512551

25522552
// ++/--
@@ -2575,7 +2575,7 @@ bool CheckClass::checkConstFunc(const Scope *scope, const Function *func, Member
25752575
}
25762576

25772577
// streaming: >> *this
2578-
else if (Token::simpleMatch(tok1, ">> * this") && isLikelyStreamRead(true, tok1)) {
2578+
else if (Token::simpleMatch(tok1, ">> * this") && isLikelyStreamRead(tok1)) {
25792579
return false;
25802580
}
25812581

0 commit comments

Comments
 (0)