Skip to content

Commit a6c6b21

Browse files
committed
reduced Tokenizer::isC() usage [skip ci]
1 parent f80effe commit a6c6b21

10 files changed

Lines changed: 27 additions & 18 deletions

lib/checkautovariables.cpp

Lines changed: 4 additions & 4 deletions
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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1418,8 +1418,6 @@ void CheckCondition::clarifyCondition()
14181418
if (!mSettings->severity.isEnabled(Severity::style))
14191419
return;
14201420

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

14251423
const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase();
@@ -1431,7 +1429,7 @@ void CheckCondition::clarifyCondition()
14311429
tok2 = tok2->link();
14321430
else if (tok2->isComparisonOp()) {
14331431
// This might be a template
1434-
if (!isC && tok2->link())
1432+
if (!tok2->isC() && tok2->link())
14351433
break;
14361434
if (Token::simpleMatch(tok2->astParent(), "?"))
14371435
break;

lib/checkfunctions.cpp

Lines changed: 3 additions & 3 deletions
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"
@@ -313,7 +313,7 @@ void CheckFunctions::checkMissingReturn()
313313
const Function *function = scope->function;
314314
if (!function || !function->hasBody())
315315
continue;
316-
if (function->name() == "main" && !(mSettings->standards.c < Standards::C99 && mTokenizer->isC()))
316+
if (function->name() == "main" && !(mTokenizer->isC() && mSettings->standards.c < Standards::C99))
317317
continue;
318318
if (function->type != Function::Type::eFunction && function->type != Function::Type::eOperatorEqual)
319319
continue;

lib/checkmemoryleak.cpp

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -918,7 +918,7 @@ void CheckOther::checkVariableScope()
918918
continue;
919919

920920
const bool isPtrOrRef = var->isPointer() || var->isReference();
921-
const bool isSimpleType = var->typeStartToken()->isStandardType() || var->typeStartToken()->isEnumType() || (mTokenizer->isC() && var->type() && var->type()->isStructType());
921+
const bool isSimpleType = var->typeStartToken()->isStandardType() || var->typeStartToken()->isEnumType() || (var->typeStartToken()->isC() && var->type() && var->type()->isStructType());
922922
if (!isPtrOrRef && !isSimpleType && !astIsContainer(var->nameToken()))
923923
continue;
924924

lib/checkstring.cpp

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 3 additions & 3 deletions
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) == "::")
@@ -2079,7 +2079,7 @@ bool SymbolDatabase::isFunction(const Token *tok, const Scope* outerScope, const
20792079
else if (Token::Match(tok, "%name% (") && !isReservedName(tok->str()) &&
20802080
Token::simpleMatch(tok->linkAt(1), ") {") &&
20812081
(!tok->previous() || Token::Match(tok->previous(), ";|}"))) {
2082-
if (mTokenizer.isC()) {
2082+
if (tok->isC()) {
20832083
returnImplicitIntError(tok);
20842084
*funcStart = tok;
20852085
*argStart = tok->next();
@@ -6189,7 +6189,7 @@ const Type* SymbolDatabase::findType(const Token *startTok, const Scope *startSc
61896189
if (startTok->str() == startScope->className && startScope->isClassOrStruct() && startTok->strAt(1) != "::")
61906190
return startScope->definedType;
61916191

6192-
if (mTokenizer.isC()) {
6192+
if (startTok->isC()) {
61936193
const Scope* scope = startScope;
61946194
while (scope) {
61956195
if (startTok->str() == scope->className && scope->isClassOrStruct())

lib/token.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2743,3 +2743,11 @@ bool Token::isCpp() const
27432743
}
27442744
return true; // assume C++ by default
27452745
}
2746+
2747+
bool Token::isC() const
2748+
{
2749+
if (mTokensFrontBack && mTokensFrontBack->list) {
2750+
return mTokensFrontBack->list->isC();
2751+
}
2752+
return false; // assume C++ by default
2753+
}

lib/token.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1477,8 +1477,11 @@ class CPPCHECKLIB Token {
14771477
mImpl->mDebug = td;
14781478
}
14791479

1480-
/** defaults to C++ if it cannot be determined */
1480+
/** defaults to true if it cannot be determined */
14811481
bool isCpp() const;
1482+
1483+
/** defaults to false if it cannot be determined */
1484+
bool isC() const;
14821485
};
14831486

14841487
Token* findTypeEnd(Token* tok);

0 commit comments

Comments
 (0)