From 0a5b7b2af54449bbe5c264b06fc651d1972eb0ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Wed, 13 Oct 2021 20:02:48 +0200 Subject: [PATCH] Make it possible to configure default addons in a cppcheck.cfg file. Example: { "addons":["cert"] } With that cppcheck.cfg file cppcheck will always run the "cert" addon. Explicit options such as --addon=cert will not be needed. --- cli/cmdlineparser.cpp | 2 ++ gui/mainwindow.cpp | 4 ++++ gui/threadhandler.cpp | 9 ++++++++- lib/settings.cpp | 31 +++++++++++++++++++++++++++++++ lib/settings.h | 2 ++ 5 files changed, 47 insertions(+), 1 deletion(-) diff --git a/cli/cmdlineparser.cpp b/cli/cmdlineparser.cpp index 3d0dcd32c..5fec02cda 100644 --- a/cli/cmdlineparser.cpp +++ b/cli/cmdlineparser.cpp @@ -121,6 +121,8 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[]) mSettings->exename = argv[0]; + mSettings->loadCppcheckCfg(Path::getPathFromFilename(argv[0]) + "cppcheck.cfg"); + for (int i = 1; i < argc; i++) { if (argv[i][0] == '-') { // User define diff --git a/gui/mainwindow.cpp b/gui/mainwindow.cpp index 4de67be70..812970e66 100644 --- a/gui/mainwindow.cpp +++ b/gui/mainwindow.cpp @@ -508,6 +508,10 @@ void MainWindow::doAnalyzeFiles(const QStringList &files, const bool checkLibrar checkSettings.checkLibrary = checkLibrary; checkSettings.checkConfiguration = checkConfiguration; + const QString applicationFilePath = QCoreApplication::applicationFilePath(); + const QString appPath = QFileInfo(applicationFilePath).canonicalPath(); + checkSettings.loadCppcheckCfg(appPath.toStdString() + "/cppcheck.cfg"); + if (mProjectFile) qDebug() << "Checking project file" << mProjectFile->getFilename(); diff --git a/gui/threadhandler.cpp b/gui/threadhandler.cpp index 450e8bc18..30246714e 100644 --- a/gui/threadhandler.cpp +++ b/gui/threadhandler.cpp @@ -92,8 +92,15 @@ void ThreadHandler::check(const Settings &settings) mRunningThreadCount = mResults.getFileCount(); } + QStringList addonsAndTools = mAddonsAndTools; + for (const std::string& addon: settings.addons) { + QString s = QString::fromStdString(addon); + if (!addonsAndTools.contains(s)) + addonsAndTools << s; + } + for (int i = 0; i < mRunningThreadCount; i++) { - mThreads[i]->setAddonsAndTools(mAddonsAndTools); + mThreads[i]->setAddonsAndTools(addonsAndTools); mThreads[i]->setSuppressions(mSuppressions); mThreads[i]->setClangIncludePaths(mClangIncludePaths); mThreads[i]->setDataDir(mDataDir); diff --git a/lib/settings.cpp b/lib/settings.cpp index 7d68200cf..d2a022845 100644 --- a/lib/settings.cpp +++ b/lib/settings.cpp @@ -17,9 +17,15 @@ */ #include "settings.h" +#include "path.h" #include "summaries.h" #include "valueflow.h" +#include + +#define PICOJSON_USE_INT64 +#include + std::atomic Settings::mTerminated; const char Settings::SafeChecks::XmlRootName[] = "safe-checks"; @@ -70,6 +76,31 @@ Settings::Settings() certainty.setEnabled(Certainty::normal, true); } +void Settings::loadCppcheckCfg(const std::string &filename) +{ + std::ifstream fin(filename); + if (!fin.is_open()) + return; + picojson::value json; + fin >> json; + if (!picojson::get_last_error().empty()) + return; + picojson::object obj = json.get(); + if (obj.count("addons") && obj["addons"].is()) { + for (const picojson::value &v : obj["addons"].get()) { + const std::string &s = v.get(); + if (!Path::isAbsolute(s)) + addons.push_back(Path::getPathFromFilename(filename) + s); + else + addons.push_back(s); + } + } + if (obj.count("suppressions") && obj["suppressions"].is()) { + for (const picojson::value &v : obj["suppressions"].get()) + nomsg.addSuppressionLine(v.get()); + } +} + std::string Settings::addEnabled(const std::string &str) { // Enable parameters may be comma separated... diff --git a/lib/settings.h b/lib/settings.h index 9c080a91a..6447816e7 100644 --- a/lib/settings.h +++ b/lib/settings.h @@ -95,6 +95,8 @@ private: public: Settings(); + void loadCppcheckCfg(const std::string &filename); + /** @brief addons, either filename of python/json file or json data */ std::list addons;