Addons: handle inline suppressions internally in cppcheckdata
This commit is contained in:
parent
bfa8c6fb98
commit
1ce78a1086
|
@ -15,6 +15,8 @@ from fnmatch import fnmatch
|
||||||
|
|
||||||
EXIT_CODE = 0
|
EXIT_CODE = 0
|
||||||
|
|
||||||
|
current_dumpfile_suppressions = []
|
||||||
|
|
||||||
class Directive:
|
class Directive:
|
||||||
"""
|
"""
|
||||||
Directive class. Contains information about each preprocessor directive in the source code.
|
Directive class. Contains information about each preprocessor directive in the source code.
|
||||||
|
@ -636,12 +638,11 @@ class Suppression:
|
||||||
|
|
||||||
def isMatch(self, file, line, message, errorId):
|
def isMatch(self, file, line, message, errorId):
|
||||||
if ((self.fileName is None or fnmatch(file, self.fileName))
|
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 (self.symbolName is None or fnmatch(message, '*'+self.symbolName+'*'))
|
||||||
and fnmatch(errorId, self.errorId)):
|
and fnmatch(errorId, self.errorId)):
|
||||||
return True
|
return True
|
||||||
else:
|
return False
|
||||||
return False
|
|
||||||
|
|
||||||
|
|
||||||
class Configuration:
|
class Configuration:
|
||||||
|
@ -871,6 +872,9 @@ class CppcheckData:
|
||||||
self.suppressions.append(Suppression(suppressions_node))
|
self.suppressions.append(Suppression(suppressions_node))
|
||||||
suppressions_done = True
|
suppressions_done = True
|
||||||
|
|
||||||
|
global current_dumpfile_suppressions
|
||||||
|
current_dumpfile_suppressions = self.suppressions
|
||||||
|
|
||||||
# Set links between rawTokens.
|
# Set links between rawTokens.
|
||||||
for i in range(len(self.rawTokens)-1):
|
for i in range(len(self.rawTokens)-1):
|
||||||
self.rawTokens[i+1].previous = self.rawTokens[i]
|
self.rawTokens[i+1].previous = self.rawTokens[i]
|
||||||
|
@ -1096,6 +1100,12 @@ def simpleMatch(token, pattern):
|
||||||
return True
|
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=''):
|
def reportError(location, severity, message, addon, errorId, extra=''):
|
||||||
if '--cli' in sys.argv:
|
if '--cli' in sys.argv:
|
||||||
msg = { 'file': location.file,
|
msg = { 'file': location.file,
|
||||||
|
@ -1108,6 +1118,8 @@ def reportError(location, severity, message, addon, errorId, extra=''):
|
||||||
'extra': extra}
|
'extra': extra}
|
||||||
sys.stdout.write(json.dumps(msg) + '\n')
|
sys.stdout.write(json.dumps(msg) + '\n')
|
||||||
else:
|
else:
|
||||||
|
if is_suppressed(location, message, '%s-%s' % (addon, errorId)):
|
||||||
|
return
|
||||||
loc = '[%s:%i]' % (location.file, location.linenr)
|
loc = '[%s:%i]' % (location.file, location.linenr)
|
||||||
if len(extra) > 0:
|
if len(extra) > 0:
|
||||||
message += ' (' + extra + ')'
|
message += ' (' + extra + ')'
|
||||||
|
|
|
@ -1088,9 +1088,6 @@ class MisraChecker:
|
||||||
# should not be None for both.
|
# should not be None for both.
|
||||||
self.suppressedRules = dict()
|
self.suppressedRules = dict()
|
||||||
|
|
||||||
# List of suppression extracted from the dumpfile
|
|
||||||
self.dumpfileSuppressions = None
|
|
||||||
|
|
||||||
# Prefix to ignore when matching suppression files.
|
# Prefix to ignore when matching suppression files.
|
||||||
self.filePrefix = None
|
self.filePrefix = None
|
||||||
|
|
||||||
|
@ -1105,8 +1102,8 @@ class MisraChecker:
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
attrs = ["settings", "verify_expected", "verify_actual", "violations",
|
attrs = ["settings", "verify_expected", "verify_actual", "violations",
|
||||||
"ruleTexts", "suppressedRules", "dumpfileSuppressions",
|
"ruleTexts", "suppressedRules", "filePrefix",
|
||||||
"filePrefix", "suppressionStats", "stdversion", "severity"]
|
"suppressionStats", "stdversion", "severity"]
|
||||||
return "{}({})".format(
|
return "{}({})".format(
|
||||||
"MisraChecker",
|
"MisraChecker",
|
||||||
", ".join(("{}={}".format(a, repr(getattr(self, a))) for a in attrs))
|
", ".join(("{}={}".format(a, repr(getattr(self, a))) for a in attrs))
|
||||||
|
@ -2809,29 +2806,6 @@ class MisraChecker:
|
||||||
return False
|
return False
|
||||||
return None in self.suppressedRules[rule_num]
|
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):
|
def showSuppressedRules(self):
|
||||||
"""
|
"""
|
||||||
Print out rules in suppression list sorted by Rule Number
|
Print out rules in suppression list sorted by Rule Number
|
||||||
|
@ -3057,9 +3031,6 @@ class MisraChecker:
|
||||||
filename = '.'.join(dumpfile.split('.')[:-1])
|
filename = '.'.join(dumpfile.split('.')[:-1])
|
||||||
data = cppcheckdata.parsedump(dumpfile)
|
data = cppcheckdata.parsedump(dumpfile)
|
||||||
|
|
||||||
self.dumpfileSuppressions = data.suppressions
|
|
||||||
self.parseSuppressions()
|
|
||||||
|
|
||||||
typeBits['CHAR'] = data.platform.char_bit
|
typeBits['CHAR'] = data.platform.char_bit
|
||||||
typeBits['SHORT'] = data.platform.short_bit
|
typeBits['SHORT'] = data.platform.short_bit
|
||||||
typeBits['INT'] = data.platform.int_bit
|
typeBits['INT'] = data.platform.int_bit
|
||||||
|
|
Loading…
Reference in New Issue