Skip to content

Commit

Permalink
MISRA - support for per file/line error suppression (#1275)
Browse files Browse the repository at this point in the history
* cppcheckdata.py: fixed Suppression.isMatch method

* cppcheckdata.py: fixed parsing <suppressions> tag

* misra.py: now uses cppcheckdata.reportError and supports suppressions
  • Loading branch information
hcorg authored and danmar committed Jun 4, 2018
1 parent 74b18d7 commit 0c6d60d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
22 changes: 12 additions & 10 deletions addons/cppcheckdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -483,14 +483,14 @@ def __init__(self, element):
self.lineNumber = element.get('lineNumber')
self.symbolName = element.get('symbolName')

def isMatch(file, line, message, errorId):
def isMatch(self, file, line, message, errorId):
if (fnmatch(file, self.fileName)
and (self.lineNumber is None or line == self.lineNumber)
and fnmatch(message, '*'+self.symbolName+'*')
and (self.symbolName is None or fnmatch(message, '*'+self.symbolName+'*'))
and fnmatch(errorId, self.errorId)):
return true
return True
else:
return false
return False

class Configuration:
"""
Expand All @@ -506,7 +506,6 @@ class Configuration:
functions List of Function items
variables List of Variable items
valueflow List of ValueFlow values
suppressions List of warning suppressions
"""

name = ''
Expand All @@ -516,7 +515,6 @@ class Configuration:
functions = []
variables = []
valueflow = []
suppressions = []

def __init__(self, confignode):
self.name = confignode.get('cfg')
Expand All @@ -526,7 +524,6 @@ def __init__(self, confignode):
self.functions = []
self.variables = []
self.valueflow = []
self.suppressions = []
arguments = []

for element in confignode:
Expand Down Expand Up @@ -562,9 +559,6 @@ def __init__(self, confignode):
if element.tag == 'valueflow':
for values in element:
self.valueflow.append(ValueFlow(values))
if element.tag == "suppressions":
for suppression in element:
self.suppressions.append(Suppression(suppression))

IdMap = {None: None, '0': None, '00000000': None, '0000000000000000': None}
for token in self.tokenlist:
Expand Down Expand Up @@ -664,6 +658,7 @@ class CppcheckData:
rawTokens = []
platform = None
configurations = []
suppressions = []

def __init__(self, filename):
self.configurations = []
Expand All @@ -689,6 +684,13 @@ def __init__(self, filename):
self.rawTokens[i + 1].previous = self.rawTokens[i]
self.rawTokens[i].next = self.rawTokens[i + 1]


for suppressionsNode in data.getroot():
if suppressionsNode.tag == "suppressions":
for suppression in suppressionsNode:
self.suppressions.append(Suppression(suppression))


# root is 'dumps' node, each config has its own 'dump' subnode.
for cfgnode in data.getroot():
if cfgnode.tag == 'dump':
Expand Down
15 changes: 12 additions & 3 deletions addons/misra.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

ruleTexts = {}
suppressRules = {}
suppressions = None
typeBits = {
'CHAR': None,
'SHORT': None,
Expand Down Expand Up @@ -60,9 +61,14 @@ def reportError(location, num1, num2):
errmsg = 'misra violation (use --rule-texts=<file> to get proper output) [' + id + ']'
else:
return
sys.stderr.write('[' + location.file + ':' + str(location.linenr) + '] (style): ' + errmsg + '\n')

VIOLATIONS.append(errmsg)
if not cppcheckdata.reportError('[{file}:{line}] {severity}: {message}',
callstack=[(location.file, location.linenr)],
severity='style',
message = errmsg,
errorId = id,
suppressions = suppressions,
outputFunc = sys.stderr.write) is None:
VIOLATIONS.append(errmsg)


def simpleMatch(token, pattern):
Expand Down Expand Up @@ -1700,6 +1706,9 @@ def parseDump(dumpfile):

data = cppcheckdata.parsedump(dumpfile)

global suppressions
suppressions = data.suppressions

typeBits['CHAR'] = data.platform.char_bit
typeBits['SHORT'] = data.platform.short_bit
typeBits['INT'] = data.platform.int_bit
Expand Down

0 comments on commit 0c6d60d

Please sign in to comment.