Skip to content

Commit 14833a4

Browse files
authored
reduced Tokenizer::isC() usage (#5724)
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`.
1 parent 77406bd commit 14833a4

10 files changed

+22
-17
lines changed

lib/checkautovariables.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -241,10 +241,10 @@ static bool isAutoVariableRHS(const Token* tok) {
241241
return isAddressOfLocalVariable(tok) || isAutoVarArray(tok) || isLocalContainerBuffer(tok);
242242
}
243243

244-
static bool hasOverloadedAssignment(const Token* tok, bool c, bool& inconclusive)
244+
static bool hasOverloadedAssignment(const Token* tok, bool& inconclusive)
245245
{
246246
inconclusive = false;
247-
if (c)
247+
if (tok->isC())
248248
return false;
249249
if (const ValueType* vt = tok->valueType()) {
250250
if (vt->pointer && !Token::simpleMatch(tok->astParent(), "*"))
@@ -280,13 +280,13 @@ void CheckAutoVariables::autoVariables()
280280
} else if (Token::Match(tok, "[;{}] * %var% =") && isPtrArg(tok->tokAt(2)) && isAutoVariableRHS(tok->tokAt(3)->astOperand2())) {
281281
const Token* lhs = tok->tokAt(2);
282282
bool inconclusive = false;
283-
if (!hasOverloadedAssignment(lhs, mTokenizer->isC(), inconclusive) || (printInconclusive && inconclusive))
283+
if (!hasOverloadedAssignment(lhs, inconclusive) || (printInconclusive && inconclusive))
284284
checkAutoVariableAssignment(tok->next(), inconclusive);
285285
tok = tok->tokAt(4);
286286
} else if (Token::Match(tok, "[;{}] %var% . %var% =") && isPtrArg(tok->next()) && isAutoVariableRHS(tok->tokAt(4)->astOperand2())) {
287287
const Token* lhs = tok->tokAt(3);
288288
bool inconclusive = false;
289-
if (!hasOverloadedAssignment(lhs, mTokenizer->isC(), inconclusive) || (printInconclusive && inconclusive))
289+
if (!hasOverloadedAssignment(lhs, inconclusive) || (printInconclusive && inconclusive))
290290
checkAutoVariableAssignment(tok->next(), inconclusive);
291291
tok = tok->tokAt(5);
292292
} else if (Token::Match(tok, "[;{}] %var% [") && Token::simpleMatch(tok->linkAt(2), "] =") &&

lib/checkcondition.cpp

+1-3
Original file line numberDiff line numberDiff line change
@@ -1419,8 +1419,6 @@ void CheckCondition::clarifyCondition()
14191419
if (!mSettings->severity.isEnabled(Severity::style) && !mSettings->isPremiumEnabled("clarifyCondition"))
14201420
return;
14211421

1422-
const bool isC = mTokenizer->isC();
1423-
14241422
logChecker("CheckCondition::clarifyCondition"); // style
14251423

14261424
const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase();
@@ -1432,7 +1430,7 @@ void CheckCondition::clarifyCondition()
14321430
tok2 = tok2->link();
14331431
else if (tok2->isComparisonOp()) {
14341432
// This might be a template
1435-
if (!isC && tok2->link())
1433+
if (!tok2->isC() && tok2->link())
14361434
break;
14371435
if (Token::simpleMatch(tok2->astParent(), "?"))
14381436
break;

lib/checkfunctions.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ static const CWE CWE688(688U); // Function Call With Incorrect Variable or Refe
5656

5757
void CheckFunctions::checkProhibitedFunctions()
5858
{
59-
const bool checkAlloca = mSettings->severity.isEnabled(Severity::warning) && ((mSettings->standards.c >= Standards::C99 && mTokenizer->isC()) || mSettings->standards.cpp >= Standards::CPP11);
59+
const bool checkAlloca = mSettings->severity.isEnabled(Severity::warning) && ((mTokenizer->isC() && mSettings->standards.c >= Standards::C99) || mSettings->standards.cpp >= Standards::CPP11);
6060

6161
logChecker("CheckFunctions::checkProhibitedFunctions");
6262

@@ -67,7 +67,7 @@ void CheckFunctions::checkProhibitedFunctions()
6767
continue;
6868
// alloca() is special as it depends on the code being C or C++, so it is not in Library
6969
if (checkAlloca && Token::simpleMatch(tok, "alloca (") && (!tok->function() || tok->function()->nestedIn->type == Scope::eGlobal)) {
70-
if (mTokenizer->isC()) {
70+
if (tok->isC()) {
7171
if (mSettings->standards.c > Standards::C89)
7272
reportError(tok, Severity::warning, "allocaCalled",
7373
"$symbol:alloca\n"
@@ -315,7 +315,7 @@ void CheckFunctions::checkMissingReturn()
315315
const Function *function = scope->function;
316316
if (!function || !function->hasBody())
317317
continue;
318-
if (function->name() == "main" && !(mSettings->standards.c < Standards::C99 && mTokenizer->isC()))
318+
if (function->name() == "main" && !(mTokenizer->isC() && mSettings->standards.c < Standards::C99))
319319
continue;
320320
if (function->type != Function::Type::eFunction && function->type != Function::Type::eOperatorEqual)
321321
continue;

lib/checkmemoryleak.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ CheckMemoryLeak::AllocType CheckMemoryLeak::functionReturnType(const Function* f
375375
if (Token::Match(tok, "= %varid% ;", varid)) {
376376
return No;
377377
}
378-
if (!mTokenizer_->isC() && Token::Match(tok, "[(,] %varid% [,)]", varid)) {
378+
if (!tok->isC() && Token::Match(tok, "[(,] %varid% [,)]", varid)) {
379379
return No;
380380
}
381381
if (Token::Match(tok, "[(,] & %varid% [.,)]", varid)) {

lib/checkother.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -945,7 +945,7 @@ void CheckOther::checkVariableScope()
945945
continue;
946946

947947
const bool isPtrOrRef = var->isPointer() || var->isReference();
948-
const bool isSimpleType = var->typeStartToken()->isStandardType() || var->typeStartToken()->isEnumType() || (mTokenizer->isC() && var->type() && var->type()->isStructType());
948+
const bool isSimpleType = var->typeStartToken()->isStandardType() || var->typeStartToken()->isEnumType() || (var->typeStartToken()->isC() && var->type() && var->type()->isStructType());
949949
if (!isPtrOrRef && !isSimpleType && !astIsContainer(var->nameToken()))
950950
continue;
951951

lib/checkstring.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ void CheckString::checkSuspiciousStringCompare()
193193
continue;
194194

195195
if (litTok->tokType() == Token::eString) {
196-
if (mTokenizer->isC() || (varType && varType->pointer))
196+
if (varTok->isC() || (varType && varType->pointer))
197197
suspiciousStringCompareError(tok, varTok->expressionString(), litTok->isLong());
198198
} else if (litTok->tokType() == Token::eChar && varType && varType->pointer) {
199199
suspiciousStringCompareError_char(tok, varTok->expressionString());

lib/checkuninitvar.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ void CheckUninitVar::checkScope(const Scope* scope, const std::set<std::string>
166166
continue;
167167
}
168168

169-
bool stdtype = mTokenizer->isC() && arrayTypeDefs.find(var.typeStartToken()->str()) == arrayTypeDefs.end();
169+
bool stdtype = var.typeStartToken()->isC() && arrayTypeDefs.find(var.typeStartToken()->str()) == arrayTypeDefs.end();
170170
const Token* tok = var.typeStartToken();
171171
for (; tok != var.nameToken() && tok->str() != "<"; tok = tok->next()) {
172172
if (tok->isStandardType() || tok->isEnumType())

lib/symboldatabase.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ void SymbolDatabase::createSymbolDatabaseFindAllScopes()
167167
!Token::Match(tok->previous(), "new|friend|const|enum|typedef|mutable|volatile|using|)|(|<")) ||
168168
(Token::Match(tok, "enum class| %name% {") ||
169169
Token::Match(tok, "enum class| %name% : %name% {"))))
170-
|| (mTokenizer.isC() && tok->isKeyword() && Token::Match(tok, "struct|union|enum %name% {"))) {
170+
|| (tok->isC() && tok->isKeyword() && Token::Match(tok, "struct|union|enum %name% {"))) {
171171
const Token *tok2 = tok->tokAt(2);
172172

173173
if (tok->strAt(1) == "::")
@@ -2116,7 +2116,7 @@ bool SymbolDatabase::isFunction(const Token *tok, const Scope* outerScope, const
21162116
else if (Token::Match(tok, "%name% (") && !isReservedName(tok->str()) &&
21172117
Token::simpleMatch(tok->linkAt(1), ") {") &&
21182118
(!tok->previous() || Token::Match(tok->previous(), ";|}"))) {
2119-
if (mTokenizer.isC()) {
2119+
if (tok->isC()) {
21202120
returnImplicitIntError(tok);
21212121
*funcStart = tok;
21222122
*argStart = tok->next();
@@ -6253,7 +6253,7 @@ const Type* SymbolDatabase::findType(const Token *startTok, const Scope *startSc
62536253
if (startTok->str() == startScope->className && startScope->isClassOrStruct() && startTok->strAt(1) != "::")
62546254
return startScope->definedType;
62556255

6256-
if (mTokenizer.isC()) {
6256+
if (startTok->isC()) {
62576257
const Scope* scope = startScope;
62586258
while (scope) {
62596259
if (startTok->str() == scope->className && scope->isClassOrStruct())

lib/token.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -2753,3 +2753,8 @@ bool Token::isCpp() const
27532753
{
27542754
return mTokensFrontBack.list.isCPP();
27552755
}
2756+
2757+
bool Token::isC() const
2758+
{
2759+
return mTokensFrontBack.list.isC();
2760+
}

lib/token.h

+2
Original file line numberDiff line numberDiff line change
@@ -1480,6 +1480,8 @@ class CPPCHECKLIB Token {
14801480
}
14811481

14821482
bool isCpp() const;
1483+
1484+
bool isC() const;
14831485
};
14841486

14851487
Token* findTypeEnd(Token* tok);

0 commit comments

Comments
 (0)