Misra xml output (#1787)

* Add (very) simple XML output support

* Removed xml top and tail, better handled outside
This commit is contained in:
eivindt 2019-04-05 12:36:58 +02:00 committed by Daniel Marjamäki
parent 5eeeb0def2
commit 0debd8d520
1 changed files with 29 additions and 0 deletions

View File

@ -808,6 +808,8 @@ def reportError(template, callstack=(), severity='', message='', errorId='', sup
template = '{file}({line}): {severity}: {message}'
elif template == 'edit':
template = '{file} +{line}: {severity}: {message}'
elif template == 'xml':
return reportErrorXML(callstack, severity, message, errorId, suppressions, outputFunc)
# compute 'callstack}, {file} and {line} replacements
stack = ' -> '.join('[' + f + ':' + str(l) + ']' for (f, l) in callstack)
file = callstack[-1][0]
@ -822,3 +824,30 @@ def reportError(template, callstack=(), severity='', message='', errorId='', sup
outputFunc(outputLine)
# format message
return outputLine
def reportErrorXML(callstack, severity, message, errorId, suppressions, outputFunc):
"""
Format an error message according to cppcheck's xml format.
:param callstack: e.g. [['file1.cpp',10],['file2.h','20'], ... ]
:param severity: e.g. 'error', 'warning' ...
:param id: message ID.
:param message: message text.
"""
node = ET.Element("error")
if errorId:
node.set("id", errorId)
if severity:
node.set("severity", severity)
node.set("msg", message)
file = callstack[-1][0]
line = str(callstack[-1][1])
if suppressions is not None and any(suppression.isMatch(file, line, message, errorId) for suppression in suppressions):
return None
for filename, lineno in callstack:
location = ET.Element("location", { 'file': filename, 'line': str(lineno) } )
node.append(location)
return ET.tostring(node, encoding="unicode")