From 4182f943aa6fb06fac72c23420199b3253be1268 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20St=C3=B6neberg?= Date: Fri, 1 Dec 2023 15:59:01 +0100 Subject: [PATCH] use more granular suppressions in selfcheck and prefer inline suppressions (#5703) --- .selfcheck_suppressions | 14 +++++++------- gui/resultstree.cpp | 16 ++++++++-------- lib/checkbufferoverrun.cpp | 1 + lib/checkclass.cpp | 1 + lib/checknullpointer.cpp | 2 ++ lib/checkunusedfunctions.cpp | 4 +++- lib/cppcheck.cpp | 5 +++++ lib/errorlogger.cpp | 1 + lib/fwdanalysis.cpp | 1 + lib/library.cpp | 7 ++++++- lib/symboldatabase.cpp | 1 + lib/templatesimplifier.cpp | 1 + lib/token.cpp | 16 ++++++++++++++++ tools/triage/mainwindow.cpp | 1 + 14 files changed, 54 insertions(+), 17 deletions(-) diff --git a/.selfcheck_suppressions b/.selfcheck_suppressions index b6478d871..03e548744 100644 --- a/.selfcheck_suppressions +++ b/.selfcheck_suppressions @@ -1,12 +1,9 @@ missingIncludeSystem -shadowFunction -bitwiseOnBoolean # temporary suppressions - fix the warnings! simplifyUsing:lib/valueptr.h varid0:gui/projectfile.cpp naming-privateMemberVariable:gui/test/cppchecklibrarydata/testcppchecklibrarydata.h -templateInstantiation symbolDatabaseWarning:*/moc_*.cpp simplifyUsing:*/moc_*.cpp @@ -19,10 +16,13 @@ functionStatic:*/ui_fileview.h valueFlowBailout valueFlowBailoutIncompleteVar autoNoType -bailoutUninitVar naming-varname:externals/simplecpp/simplecpp.h naming-privateMemberVariable:externals/simplecpp/simplecpp.h -# TODO: use more granular suppressions - might expose false positives -*:externals/picojson/* -*:externals/tinyxml2/* + +# these warnings need to be addressed upstream +uninitMemberVar:externals/tinyxml2/tinyxml2.h +noExplicitConstructor:externals/tinyxml2/tinyxml2.h +missingOverride:externals/tinyxml2/tinyxml2.h +invalidPrintfArgType_sint:externals/tinyxml2/tinyxml2.h +naming-privateMemberVariable:externals/tinyxml2/tinyxml2.h \ No newline at end of file diff --git a/gui/resultstree.cpp b/gui/resultstree.cpp index 71494a19d..e73475cf8 100644 --- a/gui/resultstree.cpp +++ b/gui/resultstree.cpp @@ -643,8 +643,8 @@ void ResultsTree::contextMenuEvent(QContextMenuEvent * e) } //Create an action for the application - QAction *recheckSelectedFiles = new QAction(tr("Recheck"), &menu); - QAction *copy = new QAction(tr("Copy"), &menu); + QAction *recheckAction = new QAction(tr("Recheck"), &menu); + QAction *copyAction = new QAction(tr("Copy"), &menu); QAction *hide = new QAction(tr("Hide"), &menu); QAction *hideallid = new QAction(tr("Hide all with id"), &menu); QAction *opencontainingfolder = new QAction(tr("Open containing folder"), &menu); @@ -654,13 +654,13 @@ void ResultsTree::contextMenuEvent(QContextMenuEvent * e) opencontainingfolder->setDisabled(true); } if (mThread->isChecking()) - recheckSelectedFiles->setDisabled(true); + recheckAction->setDisabled(true); else - recheckSelectedFiles->setDisabled(false); + recheckAction->setDisabled(false); - menu.addAction(recheckSelectedFiles); + menu.addAction(recheckAction); menu.addSeparator(); - menu.addAction(copy); + menu.addAction(copyAction); menu.addSeparator(); menu.addAction(hide); menu.addAction(hideallid); @@ -672,8 +672,8 @@ void ResultsTree::contextMenuEvent(QContextMenuEvent * e) menu.addSeparator(); menu.addAction(opencontainingfolder); - connect(recheckSelectedFiles, SIGNAL(triggered()), this, SLOT(recheckSelectedFiles())); - connect(copy, SIGNAL(triggered()), this, SLOT(copy())); + connect(recheckAction, SIGNAL(triggered()), this, SLOT(recheckAction())); + connect(copyAction, SIGNAL(triggered()), this, SLOT(copyAction())); connect(hide, SIGNAL(triggered()), this, SLOT(hideResult())); connect(hideallid, SIGNAL(triggered()), this, SLOT(hideAllIdResult())); connect(opencontainingfolder, SIGNAL(triggered()), this, SLOT(openContainingFolder())); diff --git a/lib/checkbufferoverrun.cpp b/lib/checkbufferoverrun.cpp index d9acf3892..c0c5ddb64 100644 --- a/lib/checkbufferoverrun.cpp +++ b/lib/checkbufferoverrun.cpp @@ -953,6 +953,7 @@ Check::FileInfo *CheckBufferOverrun::getFileInfo(const Tokenizer *tokenizer, con Check::FileInfo * CheckBufferOverrun::loadFileInfoFromXml(const tinyxml2::XMLElement *xmlElement) const { + // cppcheck-suppress shadowFunction - TODO: fix this const std::string arrayIndex("array-index"); const std::string pointerArith("pointer-arith"); diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index c6005eca1..245455ad4 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -2479,6 +2479,7 @@ bool CheckClass::checkConstFunc(const Scope *scope, const Function *func, Member return false; const Token* assignTok = end->next()->astParent(); if (var && assignTok && assignTok->isAssignmentOp() && assignTok->astOperand1() && assignTok->astOperand1()->variable()) { + // cppcheck-suppress shadowFunction - TODO: fix this const Variable* assignVar = assignTok->astOperand1()->variable(); if (assignVar->isPointer() && !assignVar->isConst() && var->typeScope()) { const auto& funcMap = var->typeScope()->functionMap; diff --git a/lib/checknullpointer.cpp b/lib/checknullpointer.cpp index 86a6542f0..cf70f7293 100644 --- a/lib/checknullpointer.cpp +++ b/lib/checknullpointer.cpp @@ -514,6 +514,7 @@ static std::string arithmeticTypeString(const Token *tok) void CheckNullPointer::pointerArithmeticError(const Token* tok, const ValueFlow::Value *value, bool inconclusive) { + // cppcheck-suppress shadowFunction - TODO: fix this std::string arithmetic = arithmeticTypeString(tok); std::string errmsg; if (tok && tok->str()[0] == '-') { @@ -532,6 +533,7 @@ void CheckNullPointer::pointerArithmeticError(const Token* tok, const ValueFlow: void CheckNullPointer::redundantConditionWarning(const Token* tok, const ValueFlow::Value *value, const Token *condition, bool inconclusive) { + // cppcheck-suppress shadowFunction - TODO: fix this std::string arithmetic = arithmeticTypeString(tok); std::string errmsg; if (tok && tok->str()[0] == '-') { diff --git a/lib/checkunusedfunctions.cpp b/lib/checkunusedfunctions.cpp index 24151998c..eb789fdf7 100644 --- a/lib/checkunusedfunctions.cpp +++ b/lib/checkunusedfunctions.cpp @@ -459,8 +459,10 @@ void CheckUnusedFunctions::analyseWholeProgram(const Settings &settings, ErrorLo } if (std::strcmp(e2->Name(),"functiondecl") == 0) { const char* lineNumber = e2->Attribute("lineNumber"); - if (lineNumber) + if (lineNumber) { + // cppcheck-suppress templateInstantiation - TODO: fix this - see #11631 decls[functionName] = Location(sourcefile, strToInt(lineNumber)); + } } } } diff --git a/lib/cppcheck.cpp b/lib/cppcheck.cpp index f70bca725..2d772662d 100644 --- a/lib/cppcheck.cpp +++ b/lib/cppcheck.cpp @@ -1062,6 +1062,7 @@ void CppCheck::checkNormalTokens(const Tokenizer &tokenizer) const std::time_t maxTime = mSettings.checksMaxTime > 0 ? std::time(nullptr) + mSettings.checksMaxTime : 0; // call all "runChecks" in all registered Check classes + // cppcheck-suppress shadowFunction - TODO: fix this for (Check *check : Check::instances()) { if (Settings::terminated()) return; @@ -1104,6 +1105,7 @@ void CppCheck::checkNormalTokens(const Tokenizer &tokenizer) mAnalyzerInformation.setFileInfo("ctu", fi1->toString()); } + // cppcheck-suppress shadowFunction - TODO: fix this for (const Check *check : Check::instances()) { if (doUnusedFunctionOnly && dynamic_cast(check) == nullptr) continue; @@ -1735,6 +1737,7 @@ bool CppCheck::analyseWholeProgram() ctu.nestedCalls.insert(ctu.nestedCalls.end(), fi2->nestedCalls.cbegin(), fi2->nestedCalls.cend()); } } + // cppcheck-suppress shadowFunction - TODO: fix this for (Check *check : Check::instances()) errors |= check->analyseWholeProgram(&ctu, mFileInfo, mSettings, *this); // TODO: ctu return errors && (mExitCode > 0); @@ -1785,6 +1788,7 @@ void CppCheck::analyseWholeProgram(const std::string &buildDir, const std::list< ctuFileInfo.loadFromXml(e); continue; } + // cppcheck-suppress shadowFunction - TODO: fix this for (const Check *check : Check::instances()) { if (checkClassAttr == check->name()) fileInfoList.push_back(check->loadFileInfoFromXml(e)); @@ -1796,6 +1800,7 @@ void CppCheck::analyseWholeProgram(const std::string &buildDir, const std::list< CTU::maxCtuDepth = mSettings.maxCtuDepth; // Analyse the tokens + // cppcheck-suppress shadowFunction - TODO: fix this for (Check *check : Check::instances()) check->analyseWholeProgram(&ctuFileInfo, fileInfoList, mSettings, *this); diff --git a/lib/errorlogger.cpp b/lib/errorlogger.cpp index a8a18b6c3..09aeba231 100644 --- a/lib/errorlogger.cpp +++ b/lib/errorlogger.cpp @@ -169,6 +169,7 @@ ErrorMessage::ErrorMessage(const tinyxml2::XMLElement * const errmsg) severity = attr ? severityFromString(attr) : Severity::none; attr = errmsg->Attribute("cwe"); + // cppcheck-suppress templateInstantiation - TODO: fix this - see #11631 cwe.id = attr ? strToInt(attr) : 0; attr = errmsg->Attribute("inconclusive"); diff --git a/lib/fwdanalysis.cpp b/lib/fwdanalysis.cpp index 1dd44759d..e43b5dfec 100644 --- a/lib/fwdanalysis.cpp +++ b/lib/fwdanalysis.cpp @@ -317,6 +317,7 @@ FwdAnalysis::Result FwdAnalysis::checkRecursive(const Token *expr, const Token * // ({ .. }) if (hasGccCompoundStatement(parent->astParent()->astOperand2())) return Result(Result::Type::BAILOUT); + // cppcheck-suppress shadowFunction - TODO: fix this const bool reassign = isSameExpression(mCpp, false, expr, parent, mLibrary, false, false, nullptr); if (reassign) return Result(Result::Type::WRITE, parent->astParent()); diff --git a/lib/library.cpp b/lib/library.cpp index 9ce1a4f36..f1058c788 100644 --- a/lib/library.cpp +++ b/lib/library.cpp @@ -392,8 +392,10 @@ Library::Error Library::load(const tinyxml2::XMLDocument &doc) if (end) mExecutableBlocks[extension].setEnd(end); const char * offset = blocknode->Attribute("offset"); - if (offset) + if (offset) { + // cppcheck-suppress templateInstantiation - TODO: fix this - see #11631 mExecutableBlocks[extension].setOffset(strToInt(offset)); + } } else @@ -706,6 +708,7 @@ Library::Error Library::loadFunction(const tinyxml2::XMLElement * const node, co mReturnValueType[name] = type; if (const char *container = functionnode->Attribute("container")) mReturnValueContainer[name] = strToInt(container); + // cppcheck-suppress shadowFunction - TODO: fix this if (const char *unknownReturnValues = functionnode->Attribute("unknownValues")) { if (std::strcmp(unknownReturnValues, "all") == 0) { std::vector values{LLONG_MIN, LLONG_MAX}; @@ -1305,6 +1308,7 @@ bool Library::isCompliantValidationExpression(const char* p) error |= (*(p + 1) == '-'); } else if (*p == ':') { + // cppcheck-suppress bitwiseOnBoolean - TODO: fix this error |= range | (*(p + 1) == '.'); range = true; has_dot = false; @@ -1319,6 +1323,7 @@ bool Library::isCompliantValidationExpression(const char* p) has_dot = false; has_E = false; } else if (*p == '.') { + // cppcheck-suppress bitwiseOnBoolean - TODO: fix this error |= has_dot | (!std::isdigit(*(p + 1))); has_dot = true; } else if (*p == 'E' || *p == 'e') { diff --git a/lib/symboldatabase.cpp b/lib/symboldatabase.cpp index ac053cfe5..960deb886 100644 --- a/lib/symboldatabase.cpp +++ b/lib/symboldatabase.cpp @@ -2364,6 +2364,7 @@ const Type* Variable::smartPointerType() const while (Token::Match(typeTok, "%name%|::")) typeTok = typeTok->next(); if (Token::Match(typeTok, "< %name% >")) { + // cppcheck-suppress shadowFunction - TODO: fix this const Scope* scope = typeTok->scope(); const Type* ptrType{}; while (scope && !ptrType) { diff --git a/lib/templatesimplifier.cpp b/lib/templatesimplifier.cpp index e1af536d3..e156e4be7 100644 --- a/lib/templatesimplifier.cpp +++ b/lib/templatesimplifier.cpp @@ -2946,6 +2946,7 @@ bool TemplateSimplifier::matchSpecialization( startToken = startToken->previous(); if (!Token::simpleMatch(startToken, "template <")) continue; + // cppcheck-suppress shadowFunction - TODO: fix this std::vector templateParameters; getTemplateParametersInDeclaration(startToken->tokAt(2), templateParameters); diff --git a/lib/token.cpp b/lib/token.cpp index be6d7730e..e77b2dfcf 100644 --- a/lib/token.cpp +++ b/lib/token.cpp @@ -316,11 +316,13 @@ void Token::swapWithNext() std::swap(mFlags, mNext->mFlags); std::swap(mImpl, mNext->mImpl); if (mImpl->mTemplateSimplifierPointers) + // cppcheck-suppress shadowFunction - TODO: fix this for (auto *templateSimplifierPointer : *mImpl->mTemplateSimplifierPointers) { templateSimplifierPointer->token(this); } if (mNext->mImpl->mTemplateSimplifierPointers) + // cppcheck-suppress shadowFunction - TODO: fix this for (auto *templateSimplifierPointer : *mNext->mImpl->mTemplateSimplifierPointers) { templateSimplifierPointer->token(mNext); } @@ -341,6 +343,7 @@ void Token::takeData(Token *fromToken) mImpl = fromToken->mImpl; fromToken->mImpl = nullptr; if (mImpl->mTemplateSimplifierPointers) + // cppcheck-suppress shadowFunction - TODO: fix this for (auto *templateSimplifierPointer : *mImpl->mTemplateSimplifierPointers) { templateSimplifierPointer->token(this); } @@ -636,6 +639,7 @@ bool Token::simpleMatch(const Token *tok, const char pattern[], size_t pattern_l return false; // shortcut const char *current = pattern; const char *end = pattern + pattern_len; + // cppcheck-suppress shadowFunction - TODO: fix this const char *next = static_cast(std::memchr(pattern, ' ', pattern_len)); if (!next) next = end; @@ -781,6 +785,7 @@ nonneg int Token::getStrLength(const Token *tok) assert(tok->mTokType == eString); int len = 0; + // cppcheck-suppress shadowFunction - TODO: fix this const std::string str(getStringLiteral(tok->str())); std::string::const_iterator it = str.cbegin(); const std::string::const_iterator end = str.cend(); @@ -808,6 +813,7 @@ nonneg int Token::getStrArraySize(const Token *tok) { assert(tok != nullptr); assert(tok->tokType() == eString); + // cppcheck-suppress shadowFunction - TODO: fix this const std::string str(getStringLiteral(tok->str())); int sizeofstring = 1; for (int i = 0; i < (int)str.size(); i++) { @@ -1110,6 +1116,7 @@ Token* Token::insertToken(const std::string& tokenStr, const std::string& origin tok1 = tok1->previous()->findOpeningBracket(); if (tok1 && Token::Match(tok1->tokAt(-3), "%name% :: %name%")) { tok1 = tok1->tokAt(-2); + // cppcheck-suppress shadowFunction - TODO: fix this std::string scope = tok1->strAt(-1); while (Token::Match(tok1->tokAt(-2), ":: %name%")) { scope = tok1->strAt(-3) + " :: " + scope; @@ -1285,6 +1292,7 @@ std::string Token::stringifyList(const stringifyOptions& options, const std::vec std::string ret; unsigned int lineNumber = mImpl->mLineNumber - (options.linenumbers ? 1U : 0U); + // cppcheck-suppress shadowFunction - TODO: fix this unsigned int fileIndex = options.files ? ~0U : mImpl->mFileIndex; std::map lineNumbers; for (const Token *tok = this; tok != end; tok = tok->next()) { @@ -1701,6 +1709,7 @@ void Token::printValueFlow(bool xml, std::ostream &out) const { std::string outs; + // cppcheck-suppress shadowFunction int fileIndex = -1; int line = 0; if (xml) @@ -1708,6 +1717,7 @@ void Token::printValueFlow(bool xml, std::ostream &out) const else outs += "\n\n##Value flow\n"; for (const Token *tok = this; tok; tok = tok->next()) { + // cppcheck-suppress shadowFunction - TODO: fix this const auto* const values = tok->mImpl->mValues; if (!values) continue; @@ -2246,6 +2256,7 @@ void Token::assignProgressValues(Token *tok) void Token::assignIndexes() { + // cppcheck-suppress shadowFunction - TODO: fix this int index = (mPrevious ? mPrevious->mImpl->mIndex : 0) + 1; for (Token *tok = this; tok; tok = tok->next()) tok->mImpl->mIndex = index++; @@ -2283,9 +2294,11 @@ const ::Type* Token::typeOf(const Token* tok, const Token** typeTok) if (tok->function()) return tok->function()->retType; if (Token::simpleMatch(tok, "return")) { + // cppcheck-suppress shadowFunction - TODO: fix this const Scope *scope = tok->scope(); if (!scope) return nullptr; + // cppcheck-suppress shadowFunction - TODO: fix this const Function *function = scope->function; if (!function) return nullptr; @@ -2395,15 +2408,18 @@ std::pair Token::typeDecl(const Token* tok, bool poi return {var->typeStartToken(), var->typeEndToken()->next()}; } if (Token::simpleMatch(tok, "return")) { + // cppcheck-suppress shadowFunction - TODO: fix this const Scope* scope = tok->scope(); if (!scope) return {}; + // cppcheck-suppress shadowFunction - TODO: fix this const Function* function = scope->function; if (!function) return {}; return { function->retDef, function->returnDefEnd() }; } if (tok->previous() && tok->previous()->function()) { + // cppcheck-suppress shadowFunction - TODO: fix this const Function *function = tok->previous()->function(); return {function->retDef, function->returnDefEnd()}; } diff --git a/tools/triage/mainwindow.cpp b/tools/triage/mainwindow.cpp index 22ffe8e53..27aa47b5b 100644 --- a/tools/triage/mainwindow.cpp +++ b/tools/triage/mainwindow.cpp @@ -359,6 +359,7 @@ void MainWindow::findInFilesClicked() ui->inFilesResult->clear(); const QString text = ui->filterEdit->text(); + // cppcheck-suppress shadowFunction - TODO: fix this QStringList filter; if (ui->hFilesFilter->isChecked()) filter.append(hFiles);