From 47179ab344230191b873e72a8b836a30230e89bf Mon Sep 17 00:00:00 2001 From: Guillaume Chauvel Date: Mon, 30 Nov 2026 20:44:00 +0100 Subject: [PATCH 1/2] Fix #15293 (Slow cached run with --cppcheck-build-dir on projects with many files) --- cli/cppcheckexecutor.cpp | 9 ++++- gui/checkthread.cpp | 2 +- gui/mainwindow.cpp | 2 ++ lib/analyzerinfo.cpp | 56 +++++++++++++++++++++++++++----- lib/analyzerinfo.h | 14 ++++++++ lib/cppcheck.cpp | 10 +++--- lib/settings.h | 4 +++ lib/summaries.cpp | 2 +- test/testanalyzerinformation.cpp | 38 ++++++++++++++++++++++ 9 files changed, 121 insertions(+), 16 deletions(-) diff --git a/cli/cppcheckexecutor.cpp b/cli/cppcheckexecutor.cpp index d590b6c48d3..da583855c53 100644 --- a/cli/cppcheckexecutor.cpp +++ b/cli/cppcheckexecutor.cpp @@ -277,6 +277,13 @@ int CppCheckExecutor::check(int argc, const char* const argv[]) mFiles = parser.getFiles(); mFileSettings = parser.getFileSettings(); + if (!settings.buildDir.empty()) { + std::list fileNames; + for (auto i = mFiles.cbegin(); i != mFiles.cend(); ++i) + fileNames.emplace_back(i->path()); + settings.buildDirFilesTxt = AnalyzerInformation::getFilesTxtIndex(fileNames, mFileSettings); + } + const int ret = check_wrapper(settings, supprs); return ret; @@ -382,7 +389,7 @@ bool CppCheckExecutor::reportUnmatchedSuppressions(const Settings &settings, con // TODO: what if sourcefile is empty? for (const AnalyzerInfoId &id : ids) { - AnalyzerInformation analyzerInfo; + AnalyzerInformation analyzerInfo(settings.buildDirFilesTxt); // FIXME: this is a horrible hack // we need to "re-open" the file so we can add the unmatchedSuppression findings. // we cannot keep it open conditionally because the whole program analysis reads the XML. diff --git a/gui/checkthread.cpp b/gui/checkthread.cpp index 2b4c9ba654f..8b80f60ba73 100644 --- a/gui/checkthread.cpp +++ b/gui/checkthread.cpp @@ -253,7 +253,7 @@ void CheckThread::runAddonsAndTools(const Settings& settings, const FileSettings const std::string &buildDir = settings.buildDir; if (!buildDir.empty()) { - analyzerInfoFile = QString::fromStdString(AnalyzerInformation::getAnalyzerInfoFile(buildDir, fileSettings->sfilename(), fileSettings->cfg, fileSettings->file.fsFileId())); + analyzerInfoFile = QString::fromStdString(AnalyzerInformation::getAnalyzerInfoFile(settings.buildDirFilesTxt, buildDir, fileSettings->sfilename(), fileSettings->cfg, fileSettings->file.fsFileId())); QStringList args2(args); args2.insert(0,"-E"); diff --git a/gui/mainwindow.cpp b/gui/mainwindow.cpp index 5e40f13e6c1..b5e58a4f819 100644 --- a/gui/mainwindow.cpp +++ b/gui/mainwindow.cpp @@ -653,6 +653,7 @@ void MainWindow::doAnalyzeProject(ImportProject p, const bool checkLib, const bo checkSettings.loadSummaries(); std::list sourcefiles; AnalyzerInformation::writeFilesTxt(checkSettings.buildDir, sourcefiles, p.fileSettings); + checkSettings.buildDirFilesTxt = AnalyzerInformation::getFilesTxtIndex(sourcefiles, p.fileSettings); } //mThread->SetanalyzeProject(true); @@ -731,6 +732,7 @@ void MainWindow::doAnalyzeFiles(const QStringList &files, const bool checkLib, c checkSettings.loadSummaries(); const auto& sourcefiles = toStdStringList>(fileNames); AnalyzerInformation::writeFilesTxt(checkSettings.buildDir, sourcefiles, {}); + checkSettings.buildDirFilesTxt = AnalyzerInformation::getFilesTxtIndex(sourcefiles, {}); } mThread->setCheckFiles(true); diff --git a/lib/analyzerinfo.cpp b/lib/analyzerinfo.cpp index f7fa3699a8d..27e31e783d0 100644 --- a/lib/analyzerinfo.cpp +++ b/lib/analyzerinfo.cpp @@ -33,6 +33,25 @@ #include "xml.h" +static AnalyzerInformation::FilesTxt readFilesTxt(std::istream& filesTxt) +{ + AnalyzerInformation::FilesTxt index; + std::string line; + while (std::getline(filesTxt, line)) { + AnalyzerInformation::Info filesTxtInfo; + if (!filesTxtInfo.parse(line)) + continue; // TODO: report error? + index.emplace(std::make_tuple(filesTxtInfo.sourceFile, filesTxtInfo.cfg, filesTxtInfo.fsFileId), filesTxtInfo.afile); + } + return index; +} + +static std::string findAfile(const AnalyzerInformation::FilesTxt& index, const std::string &sourcefile, const std::string &cfg, std::size_t fsFileId) +{ + const auto it = index.find(std::make_tuple(Path::simplifyPath(sourcefile), cfg, fsFileId)); + return it == index.end() ? "" : it->second; +} + // Not XMLDocument::LoadFile(): with MSVC it denies writing, and addons inherit the handle (#15207) static tinyxml2::XMLError loadFile(tinyxml2::XMLDocument &doc, const std::string &filename) { @@ -69,6 +88,18 @@ void AnalyzerInformation::writeFilesTxt(const std::string &buildDir, const std:: fout << getFilesTxt(sourcefiles, fileSettings); } +AnalyzerInformation::FilesTxt AnalyzerInformation::getFilesTxtIndex(const std::list &sourcefiles, const std::list &fileSettings) +{ + std::istringstream istr(getFilesTxt(sourcefiles, fileSettings)); + return readFilesTxt(istr); +} + +const AnalyzerInformation::FilesTxt &AnalyzerInformation::filesTxt() const +{ + static const FilesTxt notRead; + return mFilesTxt ? *mFilesTxt : notRead; +} + void AnalyzerInformation::writeIncludes(const std::set &files) { if (!files.empty() && mOutputStream.is_open()) { @@ -90,7 +121,7 @@ std::set AnalyzerInformation::getIncludes(const std::string &buildD if (buildDir.empty() || sourcefile.empty()) return files; - const std::string analyzerInfoFile = AnalyzerInformation::getAnalyzerInfoFile(buildDir, sourcefile, cfg, fsFileId); + const std::string analyzerInfoFile = AnalyzerInformation::getAnalyzerInfoFile(filesTxt(), buildDir, sourcefile, cfg, fsFileId); tinyxml2::XMLDocument analyzerInfoDoc; if (loadFile(analyzerInfoDoc, analyzerInfoFile) != tinyxml2::XML_SUCCESS) @@ -222,12 +253,21 @@ std::string AnalyzerInformation::getAnalyzerInfoFileFromFilesTxt(std::istream& f std::string AnalyzerInformation::getAnalyzerInfoFile(const std::string &buildDir, const std::string &sourcefile, const std::string &cfg, std::size_t fsFileId) { - std::ifstream fin(Path::join(buildDir, "files.txt")); - if (fin.is_open()) { - const std::string& ret = getAnalyzerInfoFileFromFilesTxt(fin, sourcefile, cfg, fsFileId); - if (!ret.empty()) - return Path::join(buildDir, ret); + return getAnalyzerInfoFile(FilesTxt(), buildDir, sourcefile, cfg, fsFileId); +} + +std::string AnalyzerInformation::getAnalyzerInfoFile(const FilesTxt &filesTxt, const std::string &buildDir, const std::string &sourcefile, const std::string &cfg, std::size_t fsFileId) +{ + std::string afile; + if (filesTxt.empty()) { + std::ifstream fin(Path::join(buildDir, "files.txt")); + if (fin.is_open()) + afile = getAnalyzerInfoFileFromFilesTxt(fin, sourcefile, cfg, fsFileId); + } else { + afile = findAfile(filesTxt, sourcefile, cfg, fsFileId); } + if (!afile.empty()) + return Path::join(buildDir, afile); const std::string::size_type pos = sourcefile.rfind('/'); std::string filename; @@ -247,7 +287,7 @@ bool AnalyzerInformation::analyzeFile(const std::string &buildDir, const std::st if (buildDir.empty() || sourcefile.empty()) return true; - const std::string analyzerInfoFile = AnalyzerInformation::getAnalyzerInfoFile(buildDir,sourcefile,cfg,fsFileId); + const std::string analyzerInfoFile = AnalyzerInformation::getAnalyzerInfoFile(filesTxt(), buildDir, sourcefile, cfg, fsFileId); { tinyxml2::XMLDocument analyzerInfoDoc; @@ -380,7 +420,7 @@ void AnalyzerInformation::reopen(const std::string &buildDir, const std::string if (buildDir.empty() || sourcefile.empty()) return; - const std::string analyzerInfoFile = AnalyzerInformation::getAnalyzerInfoFile(buildDir,sourcefile,cfg,fsFileId); + const std::string analyzerInfoFile = AnalyzerInformation::getAnalyzerInfoFile(filesTxt(), buildDir, sourcefile, cfg, fsFileId); std::ifstream ifs(analyzerInfoFile); if (!ifs.is_open()) return; diff --git a/lib/analyzerinfo.h b/lib/analyzerinfo.h index 75ff519db84..6291277ed1c 100644 --- a/lib/analyzerinfo.h +++ b/lib/analyzerinfo.h @@ -27,8 +27,10 @@ #include #include #include +#include #include #include +#include class ErrorMessage; struct FileSettings; @@ -56,9 +58,16 @@ namespace tinyxml2 { */ class CPPCHECKLIB AnalyzerInformation { public: + /** afile of each source file, cfg and fsFileId of files.txt */ + using FilesTxt = std::map, std::string>; + + AnalyzerInformation() = default; + /** find the analyzer information files in filesTxt instead of reading files.txt */ + explicit AnalyzerInformation(const FilesTxt &filesTxt) : mFilesTxt(&filesTxt) {} ~AnalyzerInformation(); static void writeFilesTxt(const std::string &buildDir, const std::list &sourcefiles, const std::list &fileSettings); + static FilesTxt getFilesTxtIndex(const std::list &sourcefiles, const std::list &fileSettings); /** Close current TU.analyzerinfo file */ void close(); @@ -72,6 +81,8 @@ class CPPCHECKLIB AnalyzerInformation { std::set getIncludes(const std::string &buildDir, const std::string &sourcefile, const std::string &cfg, std::size_t fsFileId) const; void writeHash(std::size_t hash); static std::string getAnalyzerInfoFile(const std::string &buildDir, const std::string &sourcefile, const std::string &cfg, std::size_t fsFileId); + /** files.txt is read when filesTxt is empty */ + static std::string getAnalyzerInfoFile(const FilesTxt &filesTxt, const std::string &buildDir, const std::string &sourcefile, const std::string &cfg, std::size_t fsFileId); void reopen(const std::string &buildDir, const std::string &sourcefile, const std::string &cfg, std::size_t fsFileId); @@ -96,6 +107,9 @@ class CPPCHECKLIB AnalyzerInformation { static std::string skipAnalysis(const tinyxml2::XMLDocument &analyzerInfoDoc, std::size_t hash, std::list &errors); private: + const FilesTxt &filesTxt() const; + + const FilesTxt *mFilesTxt = nullptr; std::ofstream mOutputStream; }; diff --git a/lib/cppcheck.cpp b/lib/cppcheck.cpp index c7ac0fb9069..2ef5945e29b 100644 --- a/lib/cppcheck.cpp +++ b/lib/cppcheck.cpp @@ -366,7 +366,7 @@ static std::string getDumpFileName(const Settings& settings, const FileWithDetai extension = "." + std::to_string(settings.pid) + extension; if (!settings.dump && !settings.buildDir.empty()) - return AnalyzerInformation::getAnalyzerInfoFile(settings.buildDir, file.spath(), cfg, file.fsFileId()) + extension; + return AnalyzerInformation::getAnalyzerInfoFile(settings.buildDirFilesTxt, settings.buildDir, file.spath(), cfg, file.fsFileId()) + extension; return file.spath() + extension; } @@ -710,7 +710,7 @@ unsigned int CppCheck::checkClang(const FileWithDetails &file) // TODO: get language from FileWithDetails object std::string clangStderr; if (!mSettings.buildDir.empty()) - clangStderr = AnalyzerInformation::getAnalyzerInfoFile(mSettings.buildDir, file.spath(), "", file.fsFileId()) + ".clang-stderr"; + clangStderr = AnalyzerInformation::getAnalyzerInfoFile(mSettings.buildDirFilesTxt, mSettings.buildDir, file.spath(), "", file.fsFileId()) + ".clang-stderr"; std::string exe = mSettings.clangExecutable; #ifdef _WIN32 @@ -996,7 +996,7 @@ unsigned int CppCheck::checkInternal(const FileWithDetails& file, const std::str // TODO: if an exception occurs in this block it will continue in an unexpected code path if (!mSettings.buildDir.empty()) { - analyzerInformation.reset(new AnalyzerInformation); + analyzerInformation.reset(new AnalyzerInformation(mSettings.buildDirFilesTxt)); mLogger->setAnalyzerInfo(analyzerInformation.get()); } @@ -1092,7 +1092,7 @@ unsigned int CppCheck::checkInternal(const FileWithDetails& file, const std::str preprocessor.setPlatformInfo(); if (!mSettings.buildDir.empty()) { - analyzerInformation.reset(new AnalyzerInformation); + analyzerInformation.reset(new AnalyzerInformation(mSettings.buildDirFilesTxt)); mLogger->setAnalyzerInfo(analyzerInformation.get()); } @@ -1844,7 +1844,7 @@ void CppCheck::analyseClangTidy(const FileSettings &fileSettings) std::string line; if (!mSettings.buildDir.empty()) { - const std::string analyzerInfoFile = AnalyzerInformation::getAnalyzerInfoFile(mSettings.buildDir, fileSettings.sfilename(), fileSettings.cfg, fileSettings.file.fsFileId()); + const std::string analyzerInfoFile = AnalyzerInformation::getAnalyzerInfoFile(mSettings.buildDirFilesTxt, mSettings.buildDir, fileSettings.sfilename(), fileSettings.cfg, fileSettings.file.fsFileId()); std::ofstream fcmd(analyzerInfoFile + ".clang-tidy-cmd"); fcmd << istr.str(); } diff --git a/lib/settings.h b/lib/settings.h index 52f20afb7c4..a4c16ea245a 100644 --- a/lib/settings.h +++ b/lib/settings.h @@ -31,6 +31,7 @@ #include #include #include +#include #include #include #include @@ -151,6 +152,9 @@ class CPPCHECKLIB WARN_UNUSED Settings { /** @brief --cppcheck-build-dir. Always uses / as path separator. No trailing path separator. */ std::string buildDir; + /** AnalyzerInformation::FilesTxt of buildDir, files.txt is read when it is empty */ + std::map, std::string> buildDirFilesTxt; + /** Is the 'configuration checking' wanted? */ bool checkConfiguration{}; diff --git a/lib/summaries.cpp b/lib/summaries.cpp index 3c21a962fae..dbb40daf544 100644 --- a/lib/summaries.cpp +++ b/lib/summaries.cpp @@ -91,7 +91,7 @@ std::string Summaries::create(const Tokenizer &tokenizer, const std::string &cfg } if (!settings.buildDir.empty()) { - const std::string filename = getSummaryFile(AnalyzerInformation::getAnalyzerInfoFile(settings.buildDir, Path::simplifyPath(tokenizer.list.getSourceFilePath()), cfg, fsFileId)); + const std::string filename = getSummaryFile(AnalyzerInformation::getAnalyzerInfoFile(settings.buildDirFilesTxt, settings.buildDir, Path::simplifyPath(tokenizer.list.getSourceFilePath()), cfg, fsFileId)); if (!filename.empty()) { std::ofstream fout(filename); fout << ostr.str(); diff --git a/test/testanalyzerinformation.cpp b/test/testanalyzerinformation.cpp index 8f615b27b93..9a6036b869d 100644 --- a/test/testanalyzerinformation.cpp +++ b/test/testanalyzerinformation.cpp @@ -45,6 +45,8 @@ class TestAnalyzerInformation : public TestFixture { void run() override { TEST_CASE(getAnalyzerInfoFileFromFilesTxt); TEST_CASE(getAnalyzerInfoFile); + TEST_CASE(writeFilesTxt); + TEST_CASE(getFilesTxtIndex); TEST_CASE(duplicateFile); TEST_CASE(filesTextDuplicateFile); TEST_CASE(parse); @@ -100,6 +102,42 @@ class TestAnalyzerInformation : public TestFixture { ASSERT_EQUALS("builddir/file1.c.analyzerinfo", AnalyzerInformation::getAnalyzerInfoFile("builddir", "some/path/file1.c", "", 0)); } + static std::list filesTxtFileSettings() { + std::list fileSettings; + fileSettings.emplace_back("a.c", Standards::Language::C, 10); + fileSettings.back().cfg = "Debug"; + fileSettings.emplace_back("a.c", Standards::Language::C, 10); + fileSettings.back().file.setFsFileId(1); + return fileSettings; + } + + void writeFilesTxt() const { + const std::string buildDir = "testanalyzerinfo-filestxt"; + const ScopedFile filesTxt("files.txt", "", buildDir); + + AnalyzerInformation::writeFilesTxt(buildDir, {"a.c", "src/a.c"}, filesTxtFileSettings()); + ASSERT_EQUALS(buildDir + "/a.a1", AnalyzerInformation::getAnalyzerInfoFile(buildDir, "a.c", "", 0)); + ASSERT_EQUALS(buildDir + "/a.a2", AnalyzerInformation::getAnalyzerInfoFile(buildDir, "./src/a.c", "", 0)); + ASSERT_EQUALS(buildDir + "/a.a3", AnalyzerInformation::getAnalyzerInfoFile(buildDir, "a.c", "Debug", 0)); + ASSERT_EQUALS(buildDir + "/a.a4", AnalyzerInformation::getAnalyzerInfoFile(buildDir, "a.c", "", 1)); + ASSERT_EQUALS(buildDir + "/b.c.analyzerinfo", AnalyzerInformation::getAnalyzerInfoFile(buildDir, "b.c", "", 0)); + + AnalyzerInformation::writeFilesTxt(buildDir, {"src/a.c", "a.c"}, {}); + ASSERT_EQUALS(buildDir + "/a.a2", AnalyzerInformation::getAnalyzerInfoFile(buildDir, "a.c", "", 0)); + ASSERT_EQUALS(buildDir + "/a.a1", AnalyzerInformation::getAnalyzerInfoFile(buildDir, "src/a.c", "", 0)); + ASSERT_EQUALS(buildDir + "/a.c.analyzerinfo", AnalyzerInformation::getAnalyzerInfoFile(buildDir, "a.c", "Debug", 0)); + } + + void getFilesTxtIndex() const { + // there is no files.txt in builddir + const AnalyzerInformation::FilesTxt filesTxt = AnalyzerInformation::getFilesTxtIndex({"a.c", "src/a.c"}, filesTxtFileSettings()); + ASSERT_EQUALS("builddir/a.a1", AnalyzerInformation::getAnalyzerInfoFile(filesTxt, "builddir", "a.c", "", 0)); + ASSERT_EQUALS("builddir/a.a2", AnalyzerInformation::getAnalyzerInfoFile(filesTxt, "builddir", "./src/a.c", "", 0)); + ASSERT_EQUALS("builddir/a.a3", AnalyzerInformation::getAnalyzerInfoFile(filesTxt, "builddir", "a.c", "Debug", 0)); + ASSERT_EQUALS("builddir/a.a4", AnalyzerInformation::getAnalyzerInfoFile(filesTxt, "builddir", "a.c", "", 1)); + ASSERT_EQUALS("builddir/b.c.analyzerinfo", AnalyzerInformation::getAnalyzerInfoFile(filesTxt, "builddir", "b.c", "", 0)); + } + void filesTextDuplicateFile() const { std::list fileSettings; fileSettings.emplace_back("a.c", Standards::Language::C, 10); From fcfb4ee2be2f215215fd273b6753ce836680686a Mon Sep 17 00:00:00 2001 From: Guillaume Chauvel Date: Mon, 30 Nov 2026 22:07:20 +0100 Subject: [PATCH 2/2] Remove the duplicated loop over files.txt --- lib/analyzerinfo.cpp | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/lib/analyzerinfo.cpp b/lib/analyzerinfo.cpp index 27e31e783d0..36cbae66b5f 100644 --- a/lib/analyzerinfo.cpp +++ b/lib/analyzerinfo.cpp @@ -33,16 +33,23 @@ #include "xml.h" -static AnalyzerInformation::FilesTxt readFilesTxt(std::istream& filesTxt) +static bool nextFilesTxtInfo(std::istream& filesTxt, AnalyzerInformation::Info& filesTxtInfo) { - AnalyzerInformation::FilesTxt index; std::string line; while (std::getline(filesTxt, line)) { - AnalyzerInformation::Info filesTxtInfo; - if (!filesTxtInfo.parse(line)) - continue; // TODO: report error? - index.emplace(std::make_tuple(filesTxtInfo.sourceFile, filesTxtInfo.cfg, filesTxtInfo.fsFileId), filesTxtInfo.afile); + if (filesTxtInfo.parse(line)) + return true; + // TODO: report error? } + return false; +} + +static AnalyzerInformation::FilesTxt readFilesTxt(std::istream& filesTxt) +{ + AnalyzerInformation::FilesTxt index; + AnalyzerInformation::Info filesTxtInfo; + while (nextFilesTxtInfo(filesTxt, filesTxtInfo)) + index.emplace(std::make_tuple(filesTxtInfo.sourceFile, filesTxtInfo.cfg, filesTxtInfo.fsFileId), filesTxtInfo.afile); return index; } @@ -240,11 +247,8 @@ std::string AnalyzerInformation::skipAnalysis(const tinyxml2::XMLDocument &analy std::string AnalyzerInformation::getAnalyzerInfoFileFromFilesTxt(std::istream& filesTxt, const std::string &sourcefile, const std::string &cfg, size_t fsFileId) { const std::string simplifiedSourcefile = Path::simplifyPath(sourcefile); - std::string line; - while (std::getline(filesTxt,line)) { - AnalyzerInformation::Info filesTxtInfo; - if (!filesTxtInfo.parse(line)) - continue; // TODO: report error? + AnalyzerInformation::Info filesTxtInfo; + while (nextFilesTxtInfo(filesTxt, filesTxtInfo)) { if (filesTxtInfo.sourceFile == simplifiedSourcefile && filesTxtInfo.cfg == cfg && filesTxtInfo.fsFileId == fsFileId) return filesTxtInfo.afile; }