Skip to content

Commit

Permalink
added some missing language checks and adjusted some others (#6112)
Browse files Browse the repository at this point in the history
`Settings::Standards` are always both set so we cannot simply check that
but need to combine with an check for the actual language being used.
  • Loading branch information
firewave authored Mar 12, 2024
1 parent b555246 commit 7134e9e
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 11 deletions.
4 changes: 2 additions & 2 deletions lib/checkfunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ void CheckFunctions::checkProhibitedFunctions()

const Library::WarnInfo* wi = mSettings->library.getWarnInfo(tok);
if (wi) {
if (mSettings->severity.isEnabled(wi->severity) && mSettings->standards.c >= wi->standards.c && mSettings->standards.cpp >= wi->standards.cpp) {
if (mSettings->severity.isEnabled(wi->severity) && ((tok->isC() && mSettings->standards.c >= wi->standards.c) || (tok->isCpp() && mSettings->standards.cpp >= wi->standards.cpp))) {
const std::string daca = mSettings->daca ? "prohibited" : "";
reportError(tok, wi->severity, daca + tok->str() + "Called", wi->message, CWE477, Certainty::normal);
}
Expand Down Expand Up @@ -423,7 +423,7 @@ void CheckFunctions::missingReturnError(const Token* tok)
//---------------------------------------------------------------------------
void CheckFunctions::checkMathFunctions()
{
const bool styleC99 = mSettings->severity.isEnabled(Severity::style) && mSettings->standards.c != Standards::C89 && mSettings->standards.cpp != Standards::CPP03;
const bool styleC99 = mSettings->severity.isEnabled(Severity::style) && ((mTokenizer->isC() && mSettings->standards.c != Standards::C89) || (mTokenizer->isCPP() && mSettings->standards.cpp != Standards::CPP03));
const bool printWarnings = mSettings->severity.isEnabled(Severity::warning);

if (!styleC99 && !printWarnings && !mSettings->isPremiumEnabled("wrongmathcall"))
Expand Down
2 changes: 1 addition & 1 deletion lib/checktype.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ void CheckType::tooBigSignedBitwiseShiftError(const Token *tok, int lhsbits, con
{
constexpr char id[] = "shiftTooManyBitsSigned";

const bool cpp14 = mSettings->standards.cpp >= Standards::CPP14;
const bool cpp14 = ((tok && tok->isCpp()) || (mTokenizer && mTokenizer->isCPP())) && (mSettings->standards.cpp >= Standards::CPP14);

std::string behaviour = "undefined";
if (cpp14)
Expand Down
6 changes: 3 additions & 3 deletions lib/symboldatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1536,7 +1536,7 @@ void SymbolDatabase::createSymbolDatabaseIncompleteVars()
// TODO: handle all C/C++ standards
if (cppkeywords.count(tok->str()) > 0)
continue;
if (mSettings.standards.cpp >= Standards::CPP20 && cpp20keywords.count(tok->str()) > 0)
if (tok->isCpp() && (mSettings.standards.cpp >= Standards::CPP20) && cpp20keywords.count(tok->str()) > 0)
continue;
std::string fstr = tok->str();
const Token* ftok = tok->previous();
Expand Down Expand Up @@ -3645,7 +3645,7 @@ void SymbolDatabase::debugMessage(const Token *tok, const std::string &type, con

void SymbolDatabase::returnImplicitIntError(const Token *tok) const
{
if (tok && mSettings.severity.isEnabled(Severity::portability) && mSettings.standards.c != Standards::C89 && mErrorLogger) {
if (tok && mSettings.severity.isEnabled(Severity::portability) && (tok->isC() && mSettings.standards.c != Standards::C89) && mErrorLogger) {
const std::list<const Token*> locationList(1, tok);
const ErrorMessage errmsg(locationList, &mTokenizer.list,
Severity::portability,
Expand Down Expand Up @@ -4968,7 +4968,7 @@ const Token *Scope::checkVariable(const Token *tok, AccessControl varaccess, con
const Token *typestart = tok;

// C++17 structured bindings
if (settings.standards.cpp >= Standards::CPP17 && Token::Match(tok, "auto &|&&| [")) {
if (tok && tok->isCpp() && (settings.standards.cpp >= Standards::CPP17) && Token::Match(tok, "auto &|&&| [")) {
const Token *typeend = Token::findsimplematch(typestart, "[")->previous();
for (tok = typeend->tokAt(2); Token::Match(tok, "%name%|,"); tok = tok->next()) {
if (tok->varId())
Expand Down
9 changes: 4 additions & 5 deletions lib/tokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5759,10 +5759,9 @@ bool Tokenizer::simplifyTokenList1(const char FileName[])
// Remove redundant parentheses
simplifyRedundantParentheses();

if (isCPP())
if (isCPP()) {
simplifyTypeIntrinsics();

if (!isC()) {
// Handle templates..
if (mTimerResults) {
Timer t("Tokenizer::simplifyTokens1::simplifyTokenList1::simplifyTemplates", mSettings.showtime, mTimerResults);
Expand Down Expand Up @@ -7003,7 +7002,7 @@ void Tokenizer::simplifyVarDecl(const bool only_k_r_fpar)

void Tokenizer::simplifyVarDecl(Token * tokBegin, const Token * const tokEnd, const bool only_k_r_fpar)
{
const bool isCPP11 = mSettings.standards.cpp >= Standards::CPP11;
const bool isCPP11 = isCPP() && (mSettings.standards.cpp >= Standards::CPP11);

// Split up variable declarations..
// "int a=4;" => "int a; a=4;"
Expand Down Expand Up @@ -8445,7 +8444,7 @@ void Tokenizer::findGarbageCode() const
if (!Token::Match(tok->next(), "( !!)"))
syntaxError(tok);
if (tok->str() != "for") {
if (isGarbageExpr(tok->next(), tok->linkAt(1), mSettings.standards.cpp>=Standards::cppstd_t::CPP17))
if (isGarbageExpr(tok->next(), tok->linkAt(1), isCPP() && (mSettings.standards.cpp>=Standards::cppstd_t::CPP17)))
syntaxError(tok);
}
}
Expand Down Expand Up @@ -9134,7 +9133,7 @@ void Tokenizer::simplifyCppcheckAttribute()

void Tokenizer::simplifyCPPAttribute()
{
if (mSettings.standards.cpp < Standards::CPP11 || isC())
if (!isCPP() || mSettings.standards.cpp < Standards::CPP11)
return;

for (Token *tok = list.front(); tok;) {
Expand Down

0 comments on commit 7134e9e

Please sign in to comment.