Skip to content

Commit 9ee9bd1

Browse files
authored
Fix #13417 (addons: Line suppressions acts as global suppressions) (#9023)
1 parent e618844 commit 9ee9bd1

4 files changed

Lines changed: 36 additions & 6 deletions

File tree

‎addons/cppcheckdata.py‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -997,6 +997,7 @@ class Suppression:
997997
errorId The id string of the error to suppress, can be a wildcard
998998
fileName The name of the file to suppress warnings for, can include wildcards
999999
lineNumber The number of the line to suppress warnings from, can be 0 to represent any line
1000+
thisAndNextLine Set if the line after lineNumber is suppressed too
10001001
symbolName The name of the symbol to match warnings for, can include wildcards
10011002
lineBegin The first line to suppress warnings from
10021003
lineEnd The last line to suppress warnings from
@@ -1006,6 +1007,7 @@ class Suppression:
10061007
errorId = None
10071008
fileName = None
10081009
lineNumber = None
1010+
thisAndNextLine = None
10091011
symbolName = None
10101012
lineBegin = None
10111013
lineEnd = None
@@ -1015,13 +1017,14 @@ def __init__(self, element):
10151017
self.errorId = element.get('errorId')
10161018
self.fileName = element.get('fileName')
10171019
self.lineNumber = element.get('lineNumber')
1020+
self.thisAndNextLine = element.get('thisAndNextLine')
10181021
self.symbolName = element.get('symbolName')
10191022
self.lineBegin = element.get('lineBegin')
10201023
self.lineEnd = element.get('lineEnd')
10211024
self.suppressionType = element.get('type')
10221025

10231026
def __repr__(self):
1024-
attrs = ["errorId", "fileName", "lineNumber", "symbolName", "lineBegin", "lineEnd","suppressionType"]
1027+
attrs = ["errorId", "fileName", "lineNumber", "thisAndNextLine", "symbolName", "lineBegin", "lineEnd","suppressionType"]
10251028
return "{}({})".format(
10261029
"Suppression",
10271030
", ".join(("{}={}".format(a, repr(getattr(self, a))) for a in attrs))
@@ -1031,7 +1034,7 @@ def isMatch(self, file, line, message, errorId):
10311034
# Line Suppression
10321035
if ((self.fileName is None or fnmatch(file, self.fileName))
10331036
and (self.suppressionType is None) # Verify use of default suppression type (None = unique)
1034-
and (self.lineNumber is not None and int(line) == int(self.lineNumber))
1037+
and (self.lineNumber is not None and (int(line) == int(self.lineNumber) or (self.thisAndNextLine and int(line) == int(self.lineNumber) + 1)))
10351038
and (self.symbolName is None or fnmatch(message, '*'+self.symbolName+'*'))
10361039
and fnmatch(errorId, self.errorId)):
10371040
return True
@@ -1052,6 +1055,7 @@ def isMatch(self, file, line, message, errorId):
10521055
# Other Suppression (Globally set via suppression file or cli command)
10531056
if ((self.fileName is None or fnmatch(file, self.fileName))
10541057
and (self.suppressionType is None)
1058+
and (self.lineNumber is None)
10551059
and (self.symbolName is None or fnmatch(message, '*'+self.symbolName+'*'))
10561060
and fnmatch(errorId, self.errorId)):
10571061
return True

‎lib/suppressions.cpp‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,8 @@ void SuppressionList::dump(std::ostream & out, const std::string& filePath) cons
609609
out << " fileName=\"" << ErrorLogger::toxml(suppression.fileName) << '"';
610610
if (suppression.lineNumber != Suppression::NO_LINE)
611611
out << " lineNumber=\"" << suppression.lineNumber << '"';
612+
if (suppression.thisAndNextLine)
613+
out << " thisAndNextLine=\"true\"";
612614
if (!suppression.symbolName.empty())
613615
out << " symbolName=\"" << ErrorLogger::toxml(suppression.symbolName) << '\"';
614616
if (suppression.hash > 0)

‎test/addon/test_cppcheckdata.py‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -771,13 +771,17 @@ def test_line_suppression(self):
771771
assert supp.isMatch('a.c', '5', 'msg', 'zerodiv')
772772
assert not supp.isMatch('a.c', '5', 'msg', 'nullPointer')
773773

774-
@pytest.mark.xfail(strict=False,
775-
reason='the "other suppression" fallback in Suppression.isMatch() does not check '
776-
'that lineNumber is unset, so a line suppression matches every line')
777774
def test_line_suppression_other_line(self):
778775
supp = self.make_suppression(errorId='zerodiv', lineNumber='5')
779776
assert not supp.isMatch('a.c', '6', 'msg', 'zerodiv')
780777

778+
def test_line_suppression_this_and_next_line(self):
779+
supp = self.make_suppression(errorId='zerodiv', lineNumber='5', thisAndNextLine='true')
780+
assert supp.isMatch('a.c', '5', 'msg', 'zerodiv')
781+
assert supp.isMatch('a.c', '6', 'msg', 'zerodiv')
782+
assert not supp.isMatch('a.c', '4', 'msg', 'zerodiv')
783+
assert not supp.isMatch('a.c', '7', 'msg', 'zerodiv')
784+
781785
def test_wildcard_errorId(self):
782786
supp = self.make_suppression(errorId='*', lineNumber='5')
783787
assert supp.isMatch('a.c', '5', 'msg', 'anything')
@@ -821,7 +825,8 @@ def test_dumpfile_suppressions(self, dump_factory):
821825
location = cppcheckdata.Location({'file': supp.fileName, 'line': '5', 'column': '5'})
822826
assert cppcheckdata.is_suppressed(location, 'msg', 'uninitvar')
823827
assert not cppcheckdata.is_suppressed(location, 'msg', 'nullPointer')
824-
# NOTE: a non-matching line is not checked here - see test_line_suppression_other_line
828+
location = cppcheckdata.Location({'file': supp.fileName, 'line': '6', 'column': '5'})
829+
assert not cppcheckdata.is_suppressed(location, 'msg', 'uninitvar')
825830

826831

827832
class TestPureHelpers:

‎test/cli/dumpfile_test.py‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,3 +146,22 @@ def test_line_macro(tmpdir):
146146
with open(dumpfile, 'rt') as f:
147147
dump = f.read()
148148
assert 'type="number" isInt="true" macroName="__LINE__"' in dump
149+
150+
151+
def test_suppression_this_and_next_line(tmpdir):
152+
test_file = str(tmpdir / 'test.c')
153+
with open(test_file, 'wt') as f:
154+
f.write('void f(void)\n')
155+
f.write('{ // cppcheck-suppress id1\n')
156+
f.write(' // cppcheck-suppress id2\n')
157+
f.write(' x = 0;\n')
158+
f.write('}\n')
159+
160+
exitcode, _, stderr = cppcheck(['--dump', '--inline-suppr', test_file])
161+
assert exitcode == 0, stderr
162+
163+
dumpfile = test_file + '.dump'
164+
with open(dumpfile, 'rt') as f:
165+
dump = f.read()
166+
assert 'lineNumber="2" thisAndNextLine="true" inline="true"' in dump
167+
assert 'lineNumber="4" inline="true"' in dump

0 commit comments

Comments
 (0)