From ed7bf0e061f8c072e829e087609798d94f6741af Mon Sep 17 00:00:00 2001 From: Bill Eggert Date: Tue, 1 Dec 2026 12:37:35 -0600 Subject: [PATCH] Fix symbolName suppressions not matching results loaded from the build dir ErrorMessage stores symbol names newline-separated. toXML() writes one element per name, but the XML constructor appended them without the separator, so results reloaded from --cppcheck-build-dir had a single concatenated symbol name and symbolName suppressions no longer matched. --- lib/errorlogger.cpp | 1 + test/cli/other_test.py | 63 ++++++++++++++++++++++++++++++++++++++++ test/testerrorlogger.cpp | 15 ++++++++++ 3 files changed, 79 insertions(+) diff --git a/lib/errorlogger.cpp b/lib/errorlogger.cpp index 30097a4f21c..37ddc7edca9 100644 --- a/lib/errorlogger.cpp +++ b/lib/errorlogger.cpp @@ -241,6 +241,7 @@ ErrorMessage::ErrorMessage(const tinyxml2::XMLElement * const errmsg) callStack.front().setfile(file); } else if (std::strcmp(name,"symbol")==0) { mSymbolNames += e->GetText(); + mSymbolNames += '\n'; } } } diff --git a/test/cli/other_test.py b/test/cli/other_test.py index 17893df9106..54841ef2e25 100644 --- a/test/cli/other_test.py +++ b/test/cli/other_test.py @@ -3794,6 +3794,69 @@ def test_suppress_unmatched_wildcard_cached(tmp_path): # #14585 assert stderr.splitlines() == stderr_exp +def __test_suppress_xml_symbolname(tmp_path, extra_args): + test_file = tmp_path / 'test.cpp' + with open(test_file, 'wt') as f: + f.write( +"""struct Base { + void init() {} +}; +struct Derived : Base { + void init() {} +}; +""") + + suppr_file = tmp_path / 'suppr.xml' + with open(suppr_file, 'wt') as f: + f.write( +""" + + + duplInheritedMember + init + + +""") + + args = [ + '-q', + '--template=simple', + '--enable=warning', + '--suppress-xml={}'.format(suppr_file), + str(test_file) + ] + + args += extra_args + + exitcode, stdout, stderr = cppcheck(args) + assert exitcode == 0, stdout + assert stdout.splitlines() == [] + assert stderr.splitlines() == [] + + +def test_suppress_xml_symbolname(tmp_path): + __test_suppress_xml_symbolname(tmp_path, ['-j1']) + + +def test_suppress_xml_symbolname_j(tmp_path): + __test_suppress_xml_symbolname(tmp_path, ['-j2']) + + +def test_suppress_xml_symbolname_builddir_cached(tmp_path): + build_dir = tmp_path / 'b1' + os.mkdir(build_dir) + __test_suppress_xml_symbolname(tmp_path, ['--cppcheck-build-dir={}'.format(build_dir), '-j1']) + __test_suppress_xml_symbolname(tmp_path, ['--cppcheck-build-dir={}'.format(build_dir), '-j1']) + + +def test_suppress_xml_symbolname_builddir_j_cached(tmp_path): + # the symbol names of results loaded from the build dir must still match the suppression + build_dir = tmp_path / 'b1' + os.mkdir(build_dir) + __test_suppress_xml_symbolname(tmp_path, ['--cppcheck-build-dir={}'.format(build_dir), '-j2']) + __test_suppress_xml_symbolname(tmp_path, ['--cppcheck-build-dir={}'.format(build_dir), '-j2']) + + def test_suppress_unmatched_wildcard_unchecked(tmp_path): # make sure that unmatched wildcards suppressions are reported if files matching the expressions were processesd # but isSuppressed() has never been called (i.e. no findings in file at all) diff --git a/test/testerrorlogger.cpp b/test/testerrorlogger.cpp index 68d473420b9..c498d09d802 100644 --- a/test/testerrorlogger.cpp +++ b/test/testerrorlogger.cpp @@ -65,6 +65,7 @@ class TestErrorLogger : public TestFixture { TEST_CASE(ToXmlLocations); TEST_CASE(ToXmlV2Encoding); TEST_CASE(FromXmlV2); + TEST_CASE(FromXmlSymbolNames); TEST_CASE(ToXmlV3); // Inconclusive results in xml reports.. @@ -601,6 +602,20 @@ class TestErrorLogger : public TestFixture { ASSERT_EQUALS(1u, msg.callStack.back().column); } + void FromXmlSymbolNames() const { + // symbol names must survive a toXML() / XML constructor round trip (as done for results cached in the build dir) + const ErrorMessage msg({}, "", Severity::warning, "$symbol:sym1\n$symbol:sym2\nmessage $symbol", "id", Certainty::normal); + ASSERT_EQUALS("sym1\nsym2\n", msg.symbolNames()); + + const std::string xmldata = msg.toXML(); + tinyxml2::XMLDocument doc; + ASSERT(doc.Parse(xmldata.c_str(), xmldata.size()) == tinyxml2::XML_SUCCESS); + const auto * const rootnode = doc.FirstChildElement(); + ASSERT(rootnode); + const ErrorMessage msg2(rootnode); + ASSERT_EQUALS(msg.symbolNames(), msg2.symbolNames()); + } + void ToXmlV3() const { std::string header("\n\n"); header += "