Addons: handle inline suppressions internally in cppcheckdata
This commit is contained in:
parent
d95ccefab4
commit
a810678b83
|
@ -15,6 +15,8 @@ from fnmatch import fnmatch
|
|||
|
||||
EXIT_CODE = 0
|
||||
|
||||
current_dumpfile_suppressions = []
|
||||
|
||||
class Directive:
|
||||
"""
|
||||
Directive class. Contains information about each preprocessor directive in the source code.
|
||||
|
@ -654,11 +656,10 @@ class Suppression:
|
|||
|
||||
def isMatch(self, file, line, message, errorId):
|
||||
if ((self.fileName is None or fnmatch(file, self.fileName))
|
||||
and (self.lineNumber is None or line == self.lineNumber)
|
||||
and (self.lineNumber is None or int(line) == int(self.lineNumber))
|
||||
and (self.symbolName is None or fnmatch(message, '*'+self.symbolName+'*'))
|
||||
and fnmatch(errorId, self.errorId)):
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
|
||||
|
@ -889,6 +890,9 @@ class CppcheckData:
|
|||
self.suppressions.append(Suppression(suppressions_node))
|
||||
suppressions_done = True
|
||||
|
||||
global current_dumpfile_suppressions
|
||||
current_dumpfile_suppressions = self.suppressions
|
||||
|
||||
# Set links between rawTokens.
|
||||
for i in range(len(self.rawTokens)-1):
|
||||
self.rawTokens[i+1].previous = self.rawTokens[i]
|
||||
|
@ -1112,6 +1116,12 @@ def simpleMatch(token, pattern):
|
|||
return True
|
||||
|
||||
|
||||
def is_suppressed(location, message, errorId):
|
||||
for suppression in current_dumpfile_suppressions:
|
||||
if suppression.isMatch(location.file, location.linenr, message, errorId):
|
||||
return True
|
||||
return False
|
||||
|
||||
def reportError(location, severity, message, addon, errorId, extra=''):
|
||||
if '--cli' in sys.argv:
|
||||
msg = { 'file': location.file,
|
||||
|
@ -1124,6 +1134,8 @@ def reportError(location, severity, message, addon, errorId, extra=''):
|
|||
'extra': extra}
|
||||
sys.stdout.write(json.dumps(msg) + '\n')
|
||||
else:
|
||||
if is_suppressed(location, message, '%s-%s' % (addon, errorId)):
|
||||
return
|
||||
loc = '[%s:%i]' % (location.file, location.linenr)
|
||||
if len(extra) > 0:
|
||||
message += ' (' + extra + ')'
|
||||
|
|
|
@ -1088,9 +1088,6 @@ class MisraChecker:
|
|||
# should not be None for both.
|
||||
self.suppressedRules = dict()
|
||||
|
||||
# List of suppression extracted from the dumpfile
|
||||
self.dumpfileSuppressions = None
|
||||
|
||||
# Prefix to ignore when matching suppression files.
|
||||
self.filePrefix = None
|
||||
|
||||
|
@ -1105,8 +1102,8 @@ class MisraChecker:
|
|||
|
||||
def __repr__(self):
|
||||
attrs = ["settings", "verify_expected", "verify_actual", "violations",
|
||||
"ruleTexts", "suppressedRules", "dumpfileSuppressions",
|
||||
"filePrefix", "suppressionStats", "stdversion", "severity"]
|
||||
"ruleTexts", "suppressedRules", "filePrefix",
|
||||
"suppressionStats", "stdversion", "severity"]
|
||||
return "{}({})".format(
|
||||
"MisraChecker",
|
||||
", ".join(("{}={}".format(a, repr(getattr(self, a))) for a in attrs))
|
||||
|
@ -3098,29 +3095,6 @@ class MisraChecker:
|
|||
return False
|
||||
return None in self.suppressedRules[rule_num]
|
||||
|
||||
def parseSuppressions(self):
|
||||
"""
|
||||
Parse the suppression list provided by cppcheck looking for
|
||||
rules that start with 'misra' or MISRA. The MISRA rule number
|
||||
follows using either '_' or '.' to separate the numbers.
|
||||
Examples:
|
||||
misra_6.0
|
||||
misra_7_0
|
||||
misra.21.11
|
||||
"""
|
||||
rule_pattern = re.compile(r'^(misra|MISRA)[_.]([0-9]+)[_.]([0-9]+)')
|
||||
|
||||
for each in self.dumpfileSuppressions:
|
||||
res = rule_pattern.match(each.errorId)
|
||||
|
||||
if res:
|
||||
num1 = int(res.group(2)) * 100
|
||||
ruleNum = num1 + int(res.group(3))
|
||||
linenr = None
|
||||
if each.lineNumber:
|
||||
linenr = int(each.lineNumber)
|
||||
self.addSuppressedRule(ruleNum, each.fileName, linenr, each.symbolName)
|
||||
|
||||
def showSuppressedRules(self):
|
||||
"""
|
||||
Print out rules in suppression list sorted by Rule Number
|
||||
|
@ -3346,9 +3320,6 @@ class MisraChecker:
|
|||
filename = '.'.join(dumpfile.split('.')[:-1])
|
||||
data = cppcheckdata.parsedump(dumpfile)
|
||||
|
||||
self.dumpfileSuppressions = data.suppressions
|
||||
self.parseSuppressions()
|
||||
|
||||
typeBits['CHAR'] = data.platform.char_bit
|
||||
typeBits['SHORT'] = data.platform.short_bit
|
||||
typeBits['INT'] = data.platform.int_bit
|
||||
|
|
|
@ -107,8 +107,6 @@ def test_rules_cppcheck_severity_custom(checker, capsys):
|
|||
assert("(custom-severity)" in captured)
|
||||
|
||||
def test_rules_suppression(checker, capsys):
|
||||
return # this test is temporarily disabled
|
||||
|
||||
test_sources = ["addons/test/misra/misra-suppressions1-test.c",
|
||||
"addons/test/misra/misra-suppressions2-test.c"]
|
||||
|
||||
|
|
Loading…
Reference in New Issue