From 6263ec93591f95e266712b28896827bb68209f19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ludvig=20Gunne=20Lindstr=C3=B6m?= Date: Wed, 11 Nov 2026 16:26:22 +0100 Subject: [PATCH 1/5] test --- test/cli/dumpfile_test.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/test/cli/dumpfile_test.py b/test/cli/dumpfile_test.py index b4af9f74549..d1f5e6af1cf 100644 --- a/test/cli/dumpfile_test.py +++ b/test/cli/dumpfile_test.py @@ -165,3 +165,32 @@ def test_suppression_this_and_next_line(tmpdir): dump = f.read() assert 'lineNumber="2" thisAndNextLine="true" inline="true"' in dump assert 'lineNumber="4" inline="true"' in dump + +def test_build_dir_project_configurations(tmp_path): + test_file = tmp_path / 'main.c' + with open(test_file, 'wt') as f: + f.write('int main(void) { return 0; }\n') + + configs = ''.join( + f'{c}{p}' + for c in ('Debug', 'Release') for p in ('Win32', 'x64')) + project_file = tmp_path / 'test.vcxproj' + with open(project_file, 'wt') as f: + f.write('' + '' + f'{configs}' + '' + '') + + build_dir = tmp_path / 'b' + os.mkdir(build_dir) + + args = ['-q', '--project=test.vcxproj', '--cppcheck-build-dir=b', '--addon=misra'] + exitcode, stdout, stderr = cppcheck(args, cwd=str(tmp_path)) + assert exitcode == 0, stdout + assert stderr == '' + + files = os.listdir(build_dir) + assert 'main.c.analyzerinfo.ctu-info' not in files + for i in range(1, 5): + assert f'main.a{i}.ctu-info' in files From ec956000fa1acd514840604c90a22e51be89faf6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ludvig=20Gunne=20Lindstr=C3=B6m?= Date: Wed, 11 Nov 2026 16:27:15 +0100 Subject: [PATCH 2/5] fix --- lib/cppcheck.cpp | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/lib/cppcheck.cpp b/lib/cppcheck.cpp index 8fac158ec51..71688640ba9 100644 --- a/lib/cppcheck.cpp +++ b/lib/cppcheck.cpp @@ -357,7 +357,7 @@ static std::vector split(const std::string &str, const std::string return ret; } -static std::string getDumpFileName(const Settings& settings, const FileWithDetails& file) +static std::string getDumpFileName(const Settings& settings, const FileWithDetails& file, const std::string& cfg) { std::string extension = ".dump"; if (file.fsFileId() > 0) @@ -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(), "", file.fsFileId()) + extension; + return AnalyzerInformation::getAnalyzerInfoFile(settings.buildDir, file.spath(), cfg, file.fsFileId()) + extension; return file.spath() + extension; } @@ -377,12 +377,13 @@ static std::string getCtuInfoFileName(const std::string &dumpFile) static void createDumpFile(const Settings& settings, const FileWithDetails& file, + const std::string& cfg, std::ofstream& fdump, std::string& dumpFile) { if (!settings.dump && settings.addons.empty()) return; - dumpFile = getDumpFileName(settings, file); + dumpFile = getDumpFileName(settings, file, cfg); fdump.open(dumpFile); if (!fdump.is_open()) @@ -783,7 +784,7 @@ unsigned int CppCheck::checkClang(const FileWithDetails &file) // create dumpfile std::ofstream fdump; std::string dumpFile; - createDumpFile(mSettings, file, fdump, dumpFile); + createDumpFile(mSettings, file, "", fdump, dumpFile); if (fdump.is_open()) { fdump << getLibraryDumpData(); // TODO: use tinyxml2 to create XML @@ -1156,7 +1157,7 @@ unsigned int CppCheck::checkInternal(const FileWithDetails& file, const std::str // write dump file xml prolog std::ofstream fdump; std::string dumpFile; - createDumpFile(mSettings, file, fdump, dumpFile); + createDumpFile(mSettings, file, cfgname, fdump, dumpFile); if (fdump.is_open()) { fdump << getLibraryDumpData(); if (!mSettings.dump) @@ -1725,12 +1726,12 @@ void CppCheck::executeAddonsWholeProgram(const std::list &files std::vector ctuInfoFiles; for (const auto &f: files) { - const std::string &dumpFileName = getDumpFileName(mSettings, f); + const std::string &dumpFileName = getDumpFileName(mSettings, f, ""); ctuInfoFiles.push_back(getCtuInfoFileName(dumpFileName)); } for (const auto &fs: fileSettings) { - const std::string &dumpFileName = getDumpFileName(mSettings, fs.file); + const std::string &dumpFileName = getDumpFileName(mSettings, fs.file, fs.cfg); ctuInfoFiles.push_back(getCtuInfoFileName(dumpFileName)); } From 6f95fcbd928bda6679463d87a94e9d365ff107b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ludvig=20Gunne=20Lindstr=C3=B6m?= Date: Tue, 24 Nov 2026 10:25:46 +0100 Subject: [PATCH 3/5] fix missing unmatchedSuppressions --- cli/cppcheckexecutor.cpp | 43 ++++++++++++++++++++++++-------------- test/cli/other_test.py | 45 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 15 deletions(-) diff --git a/cli/cppcheckexecutor.cpp b/cli/cppcheckexecutor.cpp index 82d4d70c0a0..c9526fc5b62 100644 --- a/cli/cppcheckexecutor.cpp +++ b/cli/cppcheckexecutor.cpp @@ -363,23 +363,37 @@ bool CppCheckExecutor::reportUnmatchedSuppressions(const Settings &settings, con supprlist.addSuppression(std::move(s)); } - const auto reportErrorsFn = [&](const std::string& sourcefile, std::size_t fsFileId, const std::vector& errors) -> bool { + const auto getCfgs = [&](const std::string &sourcefile) { + std::vector cfgs; + for (const FileSettings &fs : fileSettings) { + if (fs.file.spath() == Path::simplifyPath(sourcefile)) + cfgs.push_back(fs.cfg); + } + if (cfgs.empty()) + cfgs.emplace_back(); + return cfgs; + }; + + const auto reportErrorsFn = [&](const std::string &sourcefile, const std::vector &cfgs, std::size_t fsFileId, const std::vector &errors) -> bool { if (errors.empty()) return false; // TODO: what if sourcefile is empty? - AnalyzerInformation analyzerInfo; - // 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. - // re-ordering the code is also not an option because the unmatched suppression reporting needs to be run after all other checks. - analyzerInfo.reopen(settings.buildDir, sourcefile, /*cfgname*/ "", fsFileId); + for (const std::string &cfg : cfgs) { + AnalyzerInformation analyzerInfo; + // 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. + // re-ordering the code is also not an option because the unmatched suppression reporting needs to be run after all other checks. + analyzerInfo.reopen(settings.buildDir, sourcefile, cfg, fsFileId); - for (const auto& errmsg : errors) { - analyzerInfo.reportErr(errmsg); - errorLogger.reportErr(errmsg); + for (const auto &errmsg : errors) + analyzerInfo.reportErr(errmsg); } + + for (const auto &errmsg : errors) + errorLogger.reportErr(errmsg); return true; }; @@ -387,12 +401,12 @@ bool CppCheckExecutor::reportUnmatchedSuppressions(const Settings &settings, con for (auto i = files.cbegin(); i != files.cend(); ++i) { const std::vector errors = getUnmatchedSuppressions(supprlist.getUnmatchedLocalSuppressions(*i), settings.unmatchedSuppressionFilters); - err |= reportErrorsFn(i->spath(), i->fsFileId(), errors); + err |= reportErrorsFn(i->spath(), {""}, i->fsFileId(), errors); } for (auto i = fileSettings.cbegin(); i != fileSettings.cend(); ++i) { const std::vector errors = getUnmatchedSuppressions(supprlist.getUnmatchedLocalSuppressions(i->file), settings.unmatchedSuppressionFilters); - err |= reportErrorsFn(i->file.spath(), i->file.fsFileId(), errors); + err |= reportErrorsFn(i->file.spath(), {i->cfg}, i->file.fsFileId(), errors); } if (settings.inlineSuppressions) { @@ -401,7 +415,7 @@ bool CppCheckExecutor::reportUnmatchedSuppressions(const Settings &settings, con std::string sourcefile; if (!errmsg.callStack.empty()) sourcefile = errmsg.callStack.cbegin()->getfile(false); // TODO: simplify path? - err |= reportErrorsFn(sourcefile, 0, {errmsg}); + err |= reportErrorsFn(sourcefile, getCfgs(sourcefile), 0, {errmsg}); } } @@ -410,7 +424,7 @@ bool CppCheckExecutor::reportUnmatchedSuppressions(const Settings &settings, con std::string sourcefile; if (!errmsg.callStack.empty()) sourcefile = errmsg.callStack.cbegin()->getfile(false); // TODO: simplify path? - err |= reportErrorsFn(sourcefile, 0, {errmsg}); + err |= reportErrorsFn(sourcefile, getCfgs(sourcefile), 0, {errmsg}); } return err; } @@ -750,4 +764,3 @@ int CppCheckExecutor::executeCommand(std::string exe, std::vector a #endif return res; } - diff --git a/test/cli/other_test.py b/test/cli/other_test.py index 3dfc1c0baa5..abe3b517076 100644 --- a/test/cli/other_test.py +++ b/test/cli/other_test.py @@ -2621,6 +2621,51 @@ def test_inline_suppr_builddir_j_cached(tmp_path): __test_inline_suppr(tmp_path, ['--cppcheck-build-dir={}'.format(build_dir), '-j2']) +def __test_inline_suppr_project(tmp_path, extra_args): + test_file = tmp_path / 'test.c' + with open(test_file, 'wt') as f: + f.write(""" +void f() { + // cppcheck-suppress memleak +} +""") + + configs = ''.join( + f'{c}x64' + for c in ('Debug', 'Release')) + project_file = tmp_path / 'test.vcxproj' + with open(project_file, 'wt') as f: + f.write('' + '' + f'{configs}' + '' + '') + + args = [ + '-q', + '--template=simple', + '--enable=information', + '--inline-suppr', + '--project={}'.format(project_file) + ] + + args += extra_args + + exitcode, stdout, stderr, = cppcheck(args) + assert exitcode == 0, stdout + assert stdout == '' + assert stderr.splitlines() == [ + '{}:4:1: information: Unmatched suppression: memleak [unmatchedSuppression]'.format(test_file) + ] + + +def test_inline_suppr_builddir_project_cached(tmp_path): + build_dir = tmp_path / 'b1' + os.mkdir(build_dir) + __test_inline_suppr_project(tmp_path, ['--cppcheck-build-dir={}'.format(build_dir), '-j1']) + __test_inline_suppr_project(tmp_path, ['--cppcheck-build-dir={}'.format(build_dir), '-j1']) + + def test_duplicate_suppression(tmp_path): test_file = tmp_path / 'file.cpp' with open(test_file, 'wt'): From 46b3715f1eaf93c8ea8fec8f7cfd715930c00a6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ludvig=20Gunne=20Lindstr=C3=B6m?= Date: Tue, 24 Nov 2026 15:05:38 +0100 Subject: [PATCH 4/5] test --- test/cli/other_test.py | 65 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/test/cli/other_test.py b/test/cli/other_test.py index abe3b517076..b3769320ca0 100644 --- a/test/cli/other_test.py +++ b/test/cli/other_test.py @@ -1165,6 +1165,36 @@ def test_showtime_summary_clang_tidy_compdb(tmp_path): __test_showtime_summary(tmp_path, use_clang_tidy=True, use_compdb=True) +@pytest.mark.skipif(not __has_clang_tidy, reason='clang-tidy is not available') +def test_clang_tidy_builddir_project_configurations(tmp_path): + test_file = tmp_path / 'test.c' + with open(test_file, 'wt') as f: + f.write('int main(void) { return 0; }\n') + + configs = ''.join( + f'{c}x64' + for c in ('Debug', 'Release')) + project_file = tmp_path / 'test.vcxproj' + with open(project_file, 'wt') as f: + f.write('' + '' + f'{configs}' + '' + '') + + build_dir = tmp_path / 'b' + os.mkdir(build_dir) + + args = ['-q', '--clang-tidy', '--project=test.vcxproj', '--cppcheck-build-dir=b'] + exitcode, stdout, _ = cppcheck(args, cwd=str(tmp_path)) + assert exitcode == 0, stdout + + files = os.listdir(build_dir) + assert 'test.c.analyzerinfo.clang-tidy-cmd' not in files + for i in range(1, 3): + assert f'test.a{i}.clang-tidy-cmd' in files + + def __test_showtime_file_total(tmp_path, use_compdb=False): __test_showtime(tmp_path, 'file-total', 0, 'Check time: ', use_compdb=use_compdb) @@ -2666,6 +2696,41 @@ def test_inline_suppr_builddir_project_cached(tmp_path): __test_inline_suppr_project(tmp_path, ['--cppcheck-build-dir={}'.format(build_dir), '-j1']) +def test_inline_suppr_builddir_compdb_duplicate(tmp_path): + test_file = tmp_path / 'test.c' + with open(test_file, 'wt') as f: + f.write(""" +void f() { + // cppcheck-suppress memleak +} +""") + + # The same file listed twice gets one analyzer information file per entry + compdb_file = tmp_path / 'compile_commands.json' + create_compile_commands(compdb_file, [test_file, test_file]) + + build_dir = tmp_path / 'b1' + os.mkdir(build_dir) + + args = [ + '-q', + '--template=simple', + '--enable=information', + '--inline-suppr', + '--cppcheck-build-dir={}'.format(build_dir), + '--project={}'.format(compdb_file) + ] + + exitcode, stdout, stderr, = cppcheck(args) + assert exitcode == 0, stdout + assert stdout == '' + assert '{}:4:1: information: Unmatched suppression: memleak [unmatchedSuppression]'.format(test_file) in stderr.splitlines() + + for afile in ('test.a1', 'test.a2'): + with open(build_dir / afile, 'rt') as f: + assert f.read().count('id="unmatchedSuppression"') == 1, afile + + def test_duplicate_suppression(tmp_path): test_file = tmp_path / 'file.cpp' with open(test_file, 'wt'): From 87d32f1fa07543faa0f28ba3aeec795a56070588 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ludvig=20Gunne=20Lindstr=C3=B6m?= Date: Tue, 24 Nov 2026 15:06:27 +0100 Subject: [PATCH 5/5] fix clang-tidy and duplicates --- cli/cppcheckexecutor.cpp | 27 ++++++++++++++------------- lib/cppcheck.cpp | 2 +- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/cli/cppcheckexecutor.cpp b/cli/cppcheckexecutor.cpp index c9526fc5b62..d590b6c48d3 100644 --- a/cli/cppcheckexecutor.cpp +++ b/cli/cppcheckexecutor.cpp @@ -363,30 +363,31 @@ bool CppCheckExecutor::reportUnmatchedSuppressions(const Settings &settings, con supprlist.addSuppression(std::move(s)); } - const auto getCfgs = [&](const std::string &sourcefile) { - std::vector cfgs; + using AnalyzerInfoId = std::pair; + const auto getAnalyzerInfoIds = [&](const std::string &sourcefile) { + std::vector ids; for (const FileSettings &fs : fileSettings) { if (fs.file.spath() == Path::simplifyPath(sourcefile)) - cfgs.push_back(fs.cfg); + ids.emplace_back(fs.cfg, fs.file.fsFileId()); } - if (cfgs.empty()) - cfgs.emplace_back(); - return cfgs; + if (ids.empty()) + ids.emplace_back("", 0); + return ids; }; - const auto reportErrorsFn = [&](const std::string &sourcefile, const std::vector &cfgs, std::size_t fsFileId, const std::vector &errors) -> bool { + const auto reportErrorsFn = [&](const std::string &sourcefile, const std::vector &ids, const std::vector &errors) -> bool { if (errors.empty()) return false; // TODO: what if sourcefile is empty? - for (const std::string &cfg : cfgs) { + for (const AnalyzerInfoId &id : ids) { AnalyzerInformation analyzerInfo; // 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. // re-ordering the code is also not an option because the unmatched suppression reporting needs to be run after all other checks. - analyzerInfo.reopen(settings.buildDir, sourcefile, cfg, fsFileId); + analyzerInfo.reopen(settings.buildDir, sourcefile, id.first, id.second); for (const auto &errmsg : errors) analyzerInfo.reportErr(errmsg); @@ -401,12 +402,12 @@ bool CppCheckExecutor::reportUnmatchedSuppressions(const Settings &settings, con for (auto i = files.cbegin(); i != files.cend(); ++i) { const std::vector errors = getUnmatchedSuppressions(supprlist.getUnmatchedLocalSuppressions(*i), settings.unmatchedSuppressionFilters); - err |= reportErrorsFn(i->spath(), {""}, i->fsFileId(), errors); + err |= reportErrorsFn(i->spath(), {{"", i->fsFileId()}}, errors); } for (auto i = fileSettings.cbegin(); i != fileSettings.cend(); ++i) { const std::vector errors = getUnmatchedSuppressions(supprlist.getUnmatchedLocalSuppressions(i->file), settings.unmatchedSuppressionFilters); - err |= reportErrorsFn(i->file.spath(), {i->cfg}, i->file.fsFileId(), errors); + err |= reportErrorsFn(i->file.spath(), {{i->cfg, i->file.fsFileId()}}, errors); } if (settings.inlineSuppressions) { @@ -415,7 +416,7 @@ bool CppCheckExecutor::reportUnmatchedSuppressions(const Settings &settings, con std::string sourcefile; if (!errmsg.callStack.empty()) sourcefile = errmsg.callStack.cbegin()->getfile(false); // TODO: simplify path? - err |= reportErrorsFn(sourcefile, getCfgs(sourcefile), 0, {errmsg}); + err |= reportErrorsFn(sourcefile, getAnalyzerInfoIds(sourcefile), {errmsg}); } } @@ -424,7 +425,7 @@ bool CppCheckExecutor::reportUnmatchedSuppressions(const Settings &settings, con std::string sourcefile; if (!errmsg.callStack.empty()) sourcefile = errmsg.callStack.cbegin()->getfile(false); // TODO: simplify path? - err |= reportErrorsFn(sourcefile, getCfgs(sourcefile), 0, {errmsg}); + err |= reportErrorsFn(sourcefile, getAnalyzerInfoIds(sourcefile), {errmsg}); } return err; } diff --git a/lib/cppcheck.cpp b/lib/cppcheck.cpp index 71688640ba9..b0a70be768a 100644 --- a/lib/cppcheck.cpp +++ b/lib/cppcheck.cpp @@ -1843,7 +1843,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.file.fsFileId()); + const std::string analyzerInfoFile = AnalyzerInformation::getAnalyzerInfoFile(mSettings.buildDir, fileSettings.sfilename(), fileSettings.cfg, fileSettings.file.fsFileId()); std::ofstream fcmd(analyzerInfoFile + ".clang-tidy-cmd"); fcmd << istr.str(); }