Skip to content

Commit 7e42652

Browse files
authored
Fix #12477 [Dump] Added additional Info to the dump file for suppressions (cppcheck-opensource#6050)
For Reference see here: https://sourceforge.net/p/cppcheck/discussion/development/thread/55941f914e/ Added the type, line begin and line end to the suppression file so addons have improved use of suppressions
1 parent 49897e6 commit 7e42652

6 files changed

Lines changed: 53 additions & 6 deletions

File tree

addons/cppcheckdata.py

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -946,29 +946,60 @@ class Suppression:
946946
fileName The name of the file to suppress warnings for, can include wildcards
947947
lineNumber The number of the line to suppress warnings from, can be 0 to represent any line
948948
symbolName The name of the symbol to match warnings for, can include wildcards
949+
lineBegin The first line to suppress warnings from
950+
lineEnd The last line to suppress warnings from
951+
suppressionType The type of suppression which is applied (unique = None (default), file, block, blockBegin, blockEnd, macro)
949952
"""
950953

951954
errorId = None
952955
fileName = None
953956
lineNumber = None
954957
symbolName = None
958+
lineBegin = None
959+
lineEnd = None
960+
suppressionType = None
955961

956962
def __init__(self, element):
957963
self.errorId = element.get('errorId')
958964
self.fileName = element.get('fileName')
959965
self.lineNumber = element.get('lineNumber')
960966
self.symbolName = element.get('symbolName')
967+
self.lineBegin = element.get('lineBegin')
968+
self.lineEnd = element.get('lineEnd')
969+
self.suppressionType = element.get('type')
961970

962971
def __repr__(self):
963-
attrs = ['errorId' , "fileName", "lineNumber", "symbolName"]
972+
attrs = ["errorId", "fileName", "lineNumber", "symbolName", "lineBegin", "lineEnd","suppressionType"]
964973
return "{}({})".format(
965974
"Suppression",
966975
", ".join(("{}={}".format(a, repr(getattr(self, a))) for a in attrs))
967976
)
968977

969978
def isMatch(self, file, line, message, errorId):
979+
# Line Suppression
970980
if ((self.fileName is None or fnmatch(file, self.fileName))
971-
and (self.lineNumber is None or int(line) == int(self.lineNumber))
981+
and (self.suppressionType == None) # Verify use of default suppression type (None = unique)
982+
and (self.lineNumber != None and int(line) == int(self.lineNumber))
983+
and (self.symbolName is None or fnmatch(message, '*'+self.symbolName+'*'))
984+
and fnmatch(errorId, self.errorId)):
985+
return True
986+
# File Suppression
987+
if ((self.fileName is None or fnmatch(file, self.fileName))
988+
and (self.suppressionType != None and self.suppressionType == "file") # Verify use of file (global) suppression type
989+
and (self.symbolName is None or fnmatch(message, '*'+self.symbolName+'*'))
990+
and fnmatch(errorId, self.errorId)):
991+
return True
992+
# Block Suppression Mode
993+
if ((self.fileName is None or fnmatch(file, self.fileName))
994+
and (self.suppressionType != None and self.suppressionType == "block") # Type for Block suppression
995+
and (self.lineBegin != None and int(line) > int(self.lineBegin)) # Code Match is between the Block suppression
996+
and (self.lineEnd != None and int(line) < int(self.lineEnd)) # Code Match is between the Block suppression
997+
and (self.symbolName is None or fnmatch(message, '*'+self.symbolName+'*'))
998+
and fnmatch(errorId, self.errorId)):
999+
return True
1000+
# Other Suppression (Globaly set via suppression file or cli command)
1001+
if ((self.fileName is None or fnmatch(file, self.fileName))
1002+
and (self.suppressionType is None)
9721003
and (self.symbolName is None or fnmatch(message, '*'+self.symbolName+'*'))
9731004
and fnmatch(errorId, self.errorId)):
9741005
return True

addons/test/misra/misra-suppressions1-test.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
// This needs to stay at line number 7 to make the test pass
66
// If it is changed update suppressions.txt with the new line number
7+
// cppcheck-suppress-file misra-c2012-5.2
78
#include <stdio.h> //21.6
89

910
extern int misra_5_2_var_hides_var______31x;//8.4
@@ -13,6 +14,8 @@ static void misra_5_2_function_hides_var_31y(void) {}//5.2
1314
static void foo(void)
1415
{
1516
int i;
17+
// cppcheck-suppress-begin misra-c2012-16.4
18+
// cppcheck-suppress misra-c2012-16.6
1619
switch(misra_5_2_func1()) //16.4 16.6
1720
{
1821
case 1:
@@ -30,4 +33,5 @@ static void foo(void)
3033
} while(misra_5_2_func2()); //17.3
3134
}
3235
}
36+
// cppcheck-suppress-end misra-c2012-16.4
3337
}

addons/test/misra/misra-suppressions2-test.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// ../../cppcheck --suppressions-list=suppressions.txt --dump misra-suppressions*-test.c && python ../misra.py misra-suppressions*-test.c.dump
33
// There should be no violations reported
44

5+
// cppcheck-suppress-file misra-c2012-5.2
56
union misra_5_2_field_hides_field__63x { //19.2
67
int misra_5_2_field_hides_field__31x;
78
int misra_5_2_field_hides_field__31y;//5.2

addons/test/misra/suppressions.txt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
misra-c2012-21.6:*/misra-suppressions1-test.c:7
22
misra-c2012-17.3
3-
misra-c2012-5.2
43
misra-c2012-8.4:*/misra-suppressions1-test.c
5-
misra-c2012-16.4:*/misra-suppressions1-test.c
6-
misra-c2012-16.6:*/misra-suppressions1-test.c
74
misra-c2012-4.1:*/misra-suppressions2-test.c
85
misra-c2012-8.4:*/misra-suppressions2-test.c
96
misra-c2012-19.2:*/misra-suppressions2-test.c

addons/test/misra_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ def test_rules_suppression(checker, capsys):
122122
for src in test_sources:
123123
re_suppressed= r"\[%s\:[0-9]+\]" % src
124124
dump_remove(src)
125-
dump_create(src, "--suppressions-list=addons/test/misra/suppressions.txt")
125+
dump_create(src, "--suppressions-list=addons/test/misra/suppressions.txt","--inline-suppr")
126126
checker.parseDump(src + ".dump")
127127
captured = capsys.readouterr().err
128128
found = re.search(re_suppressed, captured)

lib/suppressions.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,20 @@ void SuppressionList::dump(std::ostream & out) const
456456
out << " symbolName=\"" << ErrorLogger::toxml(suppression.symbolName) << '\"';
457457
if (suppression.hash > 0)
458458
out << " hash=\"" << suppression.hash << '\"';
459+
if (suppression.lineBegin != Suppression::NO_LINE)
460+
out << " lineBegin=\"" << suppression.lineBegin << '"';
461+
if (suppression.lineEnd != Suppression::NO_LINE)
462+
out << " lineEnd=\"" << suppression.lineEnd << '"';
463+
if (suppression.type == SuppressionList::Type::file)
464+
out << " type=\"file\"";
465+
else if (suppression.type == SuppressionList::Type::block)
466+
out << " type=\"block\"";
467+
else if (suppression.type == SuppressionList::Type::blockBegin)
468+
out << " type=\"blockBegin\"";
469+
else if (suppression.type == SuppressionList::Type::blockEnd)
470+
out << " type=\"blockEnd\"";
471+
else if (suppression.type == SuppressionList::Type::macro)
472+
out << " type=\"macro\"";
459473
out << " />" << std::endl;
460474
}
461475
out << " </suppressions>" << std::endl;

0 commit comments

Comments
 (0)