From 0debd8d520c4bb03c3d694630d72fecc0b386b51 Mon Sep 17 00:00:00 2001 From: eivindt Date: Fri, 5 Apr 2019 12:36:58 +0200 Subject: [PATCH] Misra xml output (#1787) * Add (very) simple XML output support * Removed xml top and tail, better handled outside --- addons/cppcheckdata.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/addons/cppcheckdata.py b/addons/cppcheckdata.py index 133b82d14..4352cd034 100755 --- a/addons/cppcheckdata.py +++ b/addons/cppcheckdata.py @@ -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")