From 0dc53ac7fe8ca1e4ccf1c13538a7012dc351f972 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 4 Nov 2023 17:07:30 +0100 Subject: [PATCH] Fix #12144 (Error messages from addons has the wrong file0) (#5621) --- lib/cppcheck.cpp | 14 +++++++------- lib/cppcheck.h | 4 ++-- test/cli/test-other.py | 12 ++++++++++++ 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/lib/cppcheck.cpp b/lib/cppcheck.cpp index 2a8033b0e..ea6577b48 100644 --- a/lib/cppcheck.cpp +++ b/lib/cppcheck.cpp @@ -522,7 +522,7 @@ unsigned int CppCheck::check(const std::string &path) } // run addons - executeAddons(dumpFile); + executeAddons(dumpFile, path); } catch (const InternalError &e) { const ErrorMessage errmsg = ErrorMessage::fromInternalError(e, nullptr, path, "Bailing out from analysis: Processing Clang AST dump failed"); @@ -967,7 +967,7 @@ unsigned int CppCheck::checkFile(const std::string& filename, const std::string fdump.close(); } - executeAddons(dumpFile); + executeAddons(dumpFile, Path::simplifyPath(filename)); } catch (const TerminateException &) { // Analysis is terminated @@ -1371,15 +1371,15 @@ void CppCheck::executeRules(const std::string &tokenlist, const Tokenizer &token } #endif -void CppCheck::executeAddons(const std::string& dumpFile) +void CppCheck::executeAddons(const std::string& dumpFile, const std::string& file0) { if (!dumpFile.empty()) { std::vector f{dumpFile}; - executeAddons(f); + executeAddons(f, file0); } } -void CppCheck::executeAddons(const std::vector& files) +void CppCheck::executeAddons(const std::vector& files, const std::string& file0) { if (mSettings.addons.empty() || files.empty()) return; @@ -1444,7 +1444,7 @@ void CppCheck::executeAddons(const std::vector& files) } else if (!mSettings.severity.isEnabled(errmsg.severity)) continue; - errmsg.file0 = ((files.size() == 1) ? files[0] : ""); + errmsg.file0 = file0; reportErr(errmsg); } @@ -1463,7 +1463,7 @@ void CppCheck::executeAddonsWholeProgram(const std::map& files); - void executeAddons(const std::string &dumpFile); + void executeAddons(const std::vector& files, const std::string& file0); + void executeAddons(const std::string &dumpFile, const std::string& file0); /** * Execute addons diff --git a/test/cli/test-other.py b/test/cli/test-other.py index 4a75ba619..dffa3297f 100644 --- a/test/cli/test-other.py +++ b/test/cli/test-other.py @@ -211,6 +211,18 @@ def test_execute_addon_failure_2(tmpdir): assert stderr == "{}:0:0: error: Bailing out from analysis: Checking file failed: Failed to execute addon 'naming' - exitcode is {} [internalError]\n\n^\n".format(test_file, ec) +def test_execute_addon_file0(tmpdir): + test_file = os.path.join(tmpdir, 'test.c') + with open(test_file, 'wt') as f: + f.write('void foo() {}\n') + + args = ['--xml', '--addon=misra', '--enable=style', test_file] + + _, _, stderr = cppcheck(args) + assert 'misra-c2012-8.2' in stderr + assert '.dump' not in stderr + + # TODO: find a test case which always fails @pytest.mark.skip def test_internal_error(tmpdir):