From 1ce78a108604b3987c1fbb3f69df09789da2d3fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Wed, 11 Nov 2020 20:01:58 +0100 Subject: [PATCH] Addons: handle inline suppressions internally in cppcheckdata --- addons/cppcheckdata.py | 18 +++++++++++++++--- addons/misra.py | 33 ++------------------------------- 2 files changed, 17 insertions(+), 34 deletions(-) diff --git a/addons/cppcheckdata.py b/addons/cppcheckdata.py index 55c27c448..2c049070d 100755 --- a/addons/cppcheckdata.py +++ b/addons/cppcheckdata.py @@ -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. @@ -636,12 +638,11 @@ 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 + return False class Configuration: @@ -871,6 +872,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] @@ -1096,6 +1100,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, @@ -1108,6 +1118,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 + ')' diff --git a/addons/misra.py b/addons/misra.py index 9aa96db42..96089417d 100755 --- a/addons/misra.py +++ b/addons/misra.py @@ -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)) @@ -2809,29 +2806,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 @@ -3057,9 +3031,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