diff --git a/lib/checkautovariables.cpp b/lib/checkautovariables.cpp index 01e4586cf94c..3a528bd68290 100644 --- a/lib/checkautovariables.cpp +++ b/lib/checkautovariables.cpp @@ -241,10 +241,10 @@ static bool isAutoVariableRHS(const Token* tok) { return isAddressOfLocalVariable(tok) || isAutoVarArray(tok) || isLocalContainerBuffer(tok); } -static bool hasOverloadedAssignment(const Token* tok, bool c, bool& inconclusive) +static bool hasOverloadedAssignment(const Token* tok, bool& inconclusive) { inconclusive = false; - if (c) + if (tok->isC()) return false; if (const ValueType* vt = tok->valueType()) { if (vt->pointer && !Token::simpleMatch(tok->astParent(), "*")) @@ -280,13 +280,13 @@ void CheckAutoVariables::autoVariables() } else if (Token::Match(tok, "[;{}] * %var% =") && isPtrArg(tok->tokAt(2)) && isAutoVariableRHS(tok->tokAt(3)->astOperand2())) { const Token* lhs = tok->tokAt(2); bool inconclusive = false; - if (!hasOverloadedAssignment(lhs, mTokenizer->isC(), inconclusive) || (printInconclusive && inconclusive)) + if (!hasOverloadedAssignment(lhs, inconclusive) || (printInconclusive && inconclusive)) checkAutoVariableAssignment(tok->next(), inconclusive); tok = tok->tokAt(4); } else if (Token::Match(tok, "[;{}] %var% . %var% =") && isPtrArg(tok->next()) && isAutoVariableRHS(tok->tokAt(4)->astOperand2())) { const Token* lhs = tok->tokAt(3); bool inconclusive = false; - if (!hasOverloadedAssignment(lhs, mTokenizer->isC(), inconclusive) || (printInconclusive && inconclusive)) + if (!hasOverloadedAssignment(lhs, inconclusive) || (printInconclusive && inconclusive)) checkAutoVariableAssignment(tok->next(), inconclusive); tok = tok->tokAt(5); } else if (Token::Match(tok, "[;{}] %var% [") && Token::simpleMatch(tok->linkAt(2), "] =") && diff --git a/lib/checkcondition.cpp b/lib/checkcondition.cpp index e3f69a0c2f62..983f73b3c21e 100644 --- a/lib/checkcondition.cpp +++ b/lib/checkcondition.cpp @@ -1418,8 +1418,6 @@ void CheckCondition::clarifyCondition() if (!mSettings->severity.isEnabled(Severity::style)) return; - const bool isC = mTokenizer->isC(); - logChecker("CheckCondition::clarifyCondition"); // style const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase(); @@ -1431,7 +1429,7 @@ void CheckCondition::clarifyCondition() tok2 = tok2->link(); else if (tok2->isComparisonOp()) { // This might be a template - if (!isC && tok2->link()) + if (!tok2->isC() && tok2->link()) break; if (Token::simpleMatch(tok2->astParent(), "?")) break; diff --git a/lib/checkfunctions.cpp b/lib/checkfunctions.cpp index f9643f3c2e14..0f3f6b5ddc90 100644 --- a/lib/checkfunctions.cpp +++ b/lib/checkfunctions.cpp @@ -56,7 +56,7 @@ static const CWE CWE688(688U); // Function Call With Incorrect Variable or Refe void CheckFunctions::checkProhibitedFunctions() { - const bool checkAlloca = mSettings->severity.isEnabled(Severity::warning) && ((mSettings->standards.c >= Standards::C99 && mTokenizer->isC()) || mSettings->standards.cpp >= Standards::CPP11); + const bool checkAlloca = mSettings->severity.isEnabled(Severity::warning) && ((mTokenizer->isC() && mSettings->standards.c >= Standards::C99) || mSettings->standards.cpp >= Standards::CPP11); logChecker("CheckFunctions::checkProhibitedFunctions"); @@ -67,7 +67,7 @@ void CheckFunctions::checkProhibitedFunctions() continue; // alloca() is special as it depends on the code being C or C++, so it is not in Library if (checkAlloca && Token::simpleMatch(tok, "alloca (") && (!tok->function() || tok->function()->nestedIn->type == Scope::eGlobal)) { - if (mTokenizer->isC()) { + if (tok->isC()) { if (mSettings->standards.c > Standards::C89) reportError(tok, Severity::warning, "allocaCalled", "$symbol:alloca\n" @@ -313,7 +313,7 @@ void CheckFunctions::checkMissingReturn() const Function *function = scope->function; if (!function || !function->hasBody()) continue; - if (function->name() == "main" && !(mSettings->standards.c < Standards::C99 && mTokenizer->isC())) + if (function->name() == "main" && !(mTokenizer->isC() && mSettings->standards.c < Standards::C99)) continue; if (function->type != Function::Type::eFunction && function->type != Function::Type::eOperatorEqual) continue; diff --git a/lib/checkmemoryleak.cpp b/lib/checkmemoryleak.cpp index a3c7d4a01772..c0deb2998b85 100644 --- a/lib/checkmemoryleak.cpp +++ b/lib/checkmemoryleak.cpp @@ -375,7 +375,7 @@ CheckMemoryLeak::AllocType CheckMemoryLeak::functionReturnType(const Function* f if (Token::Match(tok, "= %varid% ;", varid)) { return No; } - if (!mTokenizer_->isC() && Token::Match(tok, "[(,] %varid% [,)]", varid)) { + if (!tok->isC() && Token::Match(tok, "[(,] %varid% [,)]", varid)) { return No; } if (Token::Match(tok, "[(,] & %varid% [.,)]", varid)) { diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 4c88092638b2..934e02cf6af1 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -918,7 +918,7 @@ void CheckOther::checkVariableScope() continue; const bool isPtrOrRef = var->isPointer() || var->isReference(); - const bool isSimpleType = var->typeStartToken()->isStandardType() || var->typeStartToken()->isEnumType() || (mTokenizer->isC() && var->type() && var->type()->isStructType()); + const bool isSimpleType = var->typeStartToken()->isStandardType() || var->typeStartToken()->isEnumType() || (var->typeStartToken()->isC() && var->type() && var->type()->isStructType()); if (!isPtrOrRef && !isSimpleType && !astIsContainer(var->nameToken())) continue; diff --git a/lib/checkstring.cpp b/lib/checkstring.cpp index 510653ab8800..cc9a59398968 100644 --- a/lib/checkstring.cpp +++ b/lib/checkstring.cpp @@ -193,7 +193,7 @@ void CheckString::checkSuspiciousStringCompare() continue; if (litTok->tokType() == Token::eString) { - if (mTokenizer->isC() || (varType && varType->pointer)) + if (varTok->isC() || (varType && varType->pointer)) suspiciousStringCompareError(tok, varTok->expressionString(), litTok->isLong()); } else if (litTok->tokType() == Token::eChar && varType && varType->pointer) { suspiciousStringCompareError_char(tok, varTok->expressionString()); diff --git a/lib/checkuninitvar.cpp b/lib/checkuninitvar.cpp index 4f611a94812c..44083f96567d 100644 --- a/lib/checkuninitvar.cpp +++ b/lib/checkuninitvar.cpp @@ -166,7 +166,7 @@ void CheckUninitVar::checkScope(const Scope* scope, const std::set continue; } - bool stdtype = mTokenizer->isC() && arrayTypeDefs.find(var.typeStartToken()->str()) == arrayTypeDefs.end(); + bool stdtype = var.typeStartToken()->isC() && arrayTypeDefs.find(var.typeStartToken()->str()) == arrayTypeDefs.end(); const Token* tok = var.typeStartToken(); for (; tok != var.nameToken() && tok->str() != "<"; tok = tok->next()) { if (tok->isStandardType() || tok->isEnumType()) diff --git a/lib/symboldatabase.cpp b/lib/symboldatabase.cpp index bffafe3eaa31..3d648d802a2b 100644 --- a/lib/symboldatabase.cpp +++ b/lib/symboldatabase.cpp @@ -167,7 +167,7 @@ void SymbolDatabase::createSymbolDatabaseFindAllScopes() !Token::Match(tok->previous(), "new|friend|const|enum|typedef|mutable|volatile|using|)|(|<")) || (Token::Match(tok, "enum class| %name% {") || Token::Match(tok, "enum class| %name% : %name% {")))) - || (mTokenizer.isC() && tok->isKeyword() && Token::Match(tok, "struct|union|enum %name% {"))) { + || (tok->isC() && tok->isKeyword() && Token::Match(tok, "struct|union|enum %name% {"))) { const Token *tok2 = tok->tokAt(2); if (tok->strAt(1) == "::") @@ -2079,7 +2079,7 @@ bool SymbolDatabase::isFunction(const Token *tok, const Scope* outerScope, const else if (Token::Match(tok, "%name% (") && !isReservedName(tok->str()) && Token::simpleMatch(tok->linkAt(1), ") {") && (!tok->previous() || Token::Match(tok->previous(), ";|}"))) { - if (mTokenizer.isC()) { + if (tok->isC()) { returnImplicitIntError(tok); *funcStart = tok; *argStart = tok->next(); @@ -6189,7 +6189,7 @@ const Type* SymbolDatabase::findType(const Token *startTok, const Scope *startSc if (startTok->str() == startScope->className && startScope->isClassOrStruct() && startTok->strAt(1) != "::") return startScope->definedType; - if (mTokenizer.isC()) { + if (startTok->isC()) { const Scope* scope = startScope; while (scope) { if (startTok->str() == scope->className && scope->isClassOrStruct()) diff --git a/lib/token.cpp b/lib/token.cpp index 4d2baa12e826..a6aefdd75cc4 100644 --- a/lib/token.cpp +++ b/lib/token.cpp @@ -2743,3 +2743,11 @@ bool Token::isCpp() const } return true; // assume C++ by default } + +bool Token::isC() const +{ + if (mTokensFrontBack && mTokensFrontBack->list) { + return mTokensFrontBack->list->isC(); + } + return false; // assume C++ by default +} diff --git a/lib/token.h b/lib/token.h index cb0beaf51c81..3adc93fae374 100644 --- a/lib/token.h +++ b/lib/token.h @@ -1477,8 +1477,11 @@ class CPPCHECKLIB Token { mImpl->mDebug = td; } - /** defaults to C++ if it cannot be determined */ + /** defaults to true if it cannot be determined */ bool isCpp() const; + + /** defaults to false if it cannot be determined */ + bool isC() const; }; Token* findTypeEnd(Token* tok);