Skip to content

Commit 542da04

Browse files
authored
fix #13961: Additional information for inline suppression in XML output (danmar#7622)
1 parent a7f827d commit 542da04

File tree

6 files changed

+55
-8
lines changed

6 files changed

+55
-8
lines changed

cli/processexecutor.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ namespace {
116116
suppr_str += suppr.checked ? "1" : "0";
117117
suppr_str += ";";
118118
suppr_str += suppr.matched ? "1" : "0";
119+
suppr_str += ";";
120+
suppr_str += suppr.extraComment;
119121
return suppr_str;
120122
}
121123

@@ -239,7 +241,7 @@ bool ProcessExecutor::handleRead(int rpipe, unsigned int &result, const std::str
239241
if (!buf.empty()) {
240242
// TODO: avoid string splitting
241243
auto parts = splitString(buf, ';');
242-
if (parts.size() != 3)
244+
if (parts.size() < 4)
243245
{
244246
// TODO: make this non-fatal
245247
std::cerr << "#### ThreadExecutor::handleRead(" << filename << ") adding of inline suppression failed - insufficient data" << std::endl;
@@ -249,6 +251,10 @@ bool ProcessExecutor::handleRead(int rpipe, unsigned int &result, const std::str
249251
suppr.isInline = (type == PipeWriter::REPORT_SUPPR_INLINE);
250252
suppr.checked = parts[1] == "1";
251253
suppr.matched = parts[2] == "1";
254+
suppr.extraComment = parts[3];
255+
for (std::size_t i = 4; i < parts.size(); i++) {
256+
suppr.extraComment += ";" + parts[i];
257+
}
252258
const std::string err = mSuppressions.nomsg.addSuppression(suppr);
253259
if (!err.empty()) {
254260
// TODO: only update state if it doesn't exist - otherwise propagate error

lib/suppressions.cpp

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -334,15 +334,24 @@ bool SuppressionList::Suppression::parseComment(std::string comment, std::string
334334
if (comment.size() < 2)
335335
return false;
336336

337-
if (comment.find(';') != std::string::npos)
338-
comment.erase(comment.find(';'));
339-
340-
if (comment.find("//", 2) != std::string::npos)
341-
comment.erase(comment.find("//",2));
342-
343337
if (comment.compare(comment.size() - 2, 2, "*/") == 0)
344338
comment.erase(comment.size() - 2, 2);
345339

340+
std::string::size_type extraPos = comment.find(';');
341+
std::string::size_type extraDelimiterSize = 1;
342+
343+
if (extraPos == std::string::npos) {
344+
extraPos = comment.find("//", 2);
345+
extraDelimiterSize = 2;
346+
}
347+
348+
if (extraPos != std::string::npos) {
349+
extraComment = trim(comment.substr(extraPos + extraDelimiterSize));
350+
for (auto it = extraComment.begin(); it != extraComment.end();)
351+
it = *it & 0x80 ? extraComment.erase(it) : it + 1;
352+
comment.erase(extraPos);
353+
}
354+
346355
const std::set<std::string> cppchecksuppress{
347356
"cppcheck-suppress",
348357
"cppcheck-suppress-begin",
@@ -532,6 +541,12 @@ void SuppressionList::dump(std::ostream & out) const
532541
out << " type=\"blockEnd\"";
533542
else if (suppression.type == SuppressionList::Type::macro)
534543
out << " type=\"macro\"";
544+
if (suppression.isInline)
545+
out << " inline=\"true\"";
546+
else
547+
out << " inline=\"false\"";
548+
if (!suppression.extraComment.empty())
549+
out << " comment=\"" << ErrorLogger::toxml(suppression.extraComment) << "\"";
535550
out << " />" << std::endl;
536551
}
537552
out << " </suppressions>" << std::endl;

lib/suppressions.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ class CPPCHECKLIB SuppressionList {
151151

152152
std::string errorId;
153153
std::string fileName;
154+
std::string extraComment;
154155
int lineNumber = NO_LINE;
155156
int lineBegin = NO_LINE;
156157
int lineEnd = NO_LINE;

test/cli/inline-suppress_test.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,20 @@ def test_2():
5555
assert ret == 0, stdout
5656

5757

58+
def test_xml():
59+
args = [
60+
'-q',
61+
'--template=simple',
62+
'--inline-suppr',
63+
'--xml-version=3',
64+
'proj-inline-suppress'
65+
]
66+
ret, stdout, stderr = cppcheck(args, cwd=__script_dir)
67+
assert '<suppression errorId="some_warning_id" fileName="proj-inline-suppress/2.c" lineNumber="2" inline="true" comment="there should be a unmatchedSuppression warning about this" />' in stderr
68+
assert stdout == ''
69+
assert ret == 0, stdout
70+
71+
5872
def test_unmatched_suppression():
5973
args = [
6074
'-q',

test/cli/premium_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def test_misra_c_builtin_style_checks(tmpdir):
6969

7070
exitcode, _, stderr = cppcheck(['--xml-version=3', '--suppress=foo', test_file], cppcheck_exe=exe)
7171
assert exitcode == 0
72-
assert '<suppression errorId="foo" />' in stderr
72+
assert '<suppression errorId="foo" inline="false" />' in stderr
7373

7474

7575
def test_build_dir_hash_cppcheck_product(tmpdir):

test/testsuppressions.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,6 +1014,17 @@ class TestSuppressions : public TestFixture {
10141014
msg.clear();
10151015
ASSERT_EQUALS(true, s.parseComment("/* cppcheck-suppress id */", &msg));
10161016
ASSERT_EQUALS("", msg);
1017+
ASSERT_EQUALS("", s.extraComment);
1018+
1019+
msg.clear();
1020+
ASSERT_EQUALS(true, s.parseComment("/* cppcheck-suppress id ; extra */", &msg));
1021+
ASSERT_EQUALS("", msg);
1022+
ASSERT_EQUALS("extra", s.extraComment);
1023+
1024+
msg.clear();
1025+
ASSERT_EQUALS(true, s.parseComment("/* cppcheck-suppress id // extra */", &msg));
1026+
ASSERT_EQUALS("", msg);
1027+
ASSERT_EQUALS("extra", s.extraComment);
10171028

10181029
msg.clear();
10191030
ASSERT_EQUALS(true, s.parseComment("/* cppcheck-suppress-file id */", &msg));

0 commit comments

Comments
 (0)