Skip to content

Commit ed7bf0e

Browse files
author
Bill Eggert
committed
Fix symbolName suppressions not matching results loaded from the build dir
ErrorMessage stores symbol names newline-separated. toXML() writes one <symbol> 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.
1 parent 6a4fedd commit ed7bf0e

3 files changed

Lines changed: 79 additions & 0 deletions

File tree

‎lib/errorlogger.cpp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ ErrorMessage::ErrorMessage(const tinyxml2::XMLElement * const errmsg)
241241
callStack.front().setfile(file);
242242
} else if (std::strcmp(name,"symbol")==0) {
243243
mSymbolNames += e->GetText();
244+
mSymbolNames += '\n';
244245
}
245246
}
246247
}

‎test/cli/other_test.py‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3794,6 +3794,69 @@ def test_suppress_unmatched_wildcard_cached(tmp_path): # #14585
37943794
assert stderr.splitlines() == stderr_exp
37953795

37963796

3797+
def __test_suppress_xml_symbolname(tmp_path, extra_args):
3798+
test_file = tmp_path / 'test.cpp'
3799+
with open(test_file, 'wt') as f:
3800+
f.write(
3801+
"""struct Base {
3802+
void init() {}
3803+
};
3804+
struct Derived : Base {
3805+
void init() {}
3806+
};
3807+
""")
3808+
3809+
suppr_file = tmp_path / 'suppr.xml'
3810+
with open(suppr_file, 'wt') as f:
3811+
f.write(
3812+
"""<?xml version="1.0"?>
3813+
<suppressions>
3814+
<suppress>
3815+
<id>duplInheritedMember</id>
3816+
<symbolName>init</symbolName>
3817+
</suppress>
3818+
</suppressions>
3819+
""")
3820+
3821+
args = [
3822+
'-q',
3823+
'--template=simple',
3824+
'--enable=warning',
3825+
'--suppress-xml={}'.format(suppr_file),
3826+
str(test_file)
3827+
]
3828+
3829+
args += extra_args
3830+
3831+
exitcode, stdout, stderr = cppcheck(args)
3832+
assert exitcode == 0, stdout
3833+
assert stdout.splitlines() == []
3834+
assert stderr.splitlines() == []
3835+
3836+
3837+
def test_suppress_xml_symbolname(tmp_path):
3838+
__test_suppress_xml_symbolname(tmp_path, ['-j1'])
3839+
3840+
3841+
def test_suppress_xml_symbolname_j(tmp_path):
3842+
__test_suppress_xml_symbolname(tmp_path, ['-j2'])
3843+
3844+
3845+
def test_suppress_xml_symbolname_builddir_cached(tmp_path):
3846+
build_dir = tmp_path / 'b1'
3847+
os.mkdir(build_dir)
3848+
__test_suppress_xml_symbolname(tmp_path, ['--cppcheck-build-dir={}'.format(build_dir), '-j1'])
3849+
__test_suppress_xml_symbolname(tmp_path, ['--cppcheck-build-dir={}'.format(build_dir), '-j1'])
3850+
3851+
3852+
def test_suppress_xml_symbolname_builddir_j_cached(tmp_path):
3853+
# the symbol names of results loaded from the build dir must still match the suppression
3854+
build_dir = tmp_path / 'b1'
3855+
os.mkdir(build_dir)
3856+
__test_suppress_xml_symbolname(tmp_path, ['--cppcheck-build-dir={}'.format(build_dir), '-j2'])
3857+
__test_suppress_xml_symbolname(tmp_path, ['--cppcheck-build-dir={}'.format(build_dir), '-j2'])
3858+
3859+
37973860
def test_suppress_unmatched_wildcard_unchecked(tmp_path):
37983861
# make sure that unmatched wildcards suppressions are reported if files matching the expressions were processesd
37993862
# but isSuppressed() has never been called (i.e. no findings in file at all)

‎test/testerrorlogger.cpp‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ class TestErrorLogger : public TestFixture {
6565
TEST_CASE(ToXmlLocations);
6666
TEST_CASE(ToXmlV2Encoding);
6767
TEST_CASE(FromXmlV2);
68+
TEST_CASE(FromXmlSymbolNames);
6869
TEST_CASE(ToXmlV3);
6970

7071
// Inconclusive results in xml reports..
@@ -601,6 +602,20 @@ class TestErrorLogger : public TestFixture {
601602
ASSERT_EQUALS(1u, msg.callStack.back().column);
602603
}
603604

605+
void FromXmlSymbolNames() const {
606+
// symbol names must survive a toXML() / XML constructor round trip (as done for results cached in the build dir)
607+
const ErrorMessage msg({}, "", Severity::warning, "$symbol:sym1\n$symbol:sym2\nmessage $symbol", "id", Certainty::normal);
608+
ASSERT_EQUALS("sym1\nsym2\n", msg.symbolNames());
609+
610+
const std::string xmldata = msg.toXML();
611+
tinyxml2::XMLDocument doc;
612+
ASSERT(doc.Parse(xmldata.c_str(), xmldata.size()) == tinyxml2::XML_SUCCESS);
613+
const auto * const rootnode = doc.FirstChildElement();
614+
ASSERT(rootnode);
615+
const ErrorMessage msg2(rootnode);
616+
ASSERT_EQUALS(msg.symbolNames(), msg2.symbolNames());
617+
}
618+
604619
void ToXmlV3() const {
605620
std::string header("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<results version=\"3\">\n");
606621
header += " <cppcheck version=\"";

0 commit comments

Comments
 (0)