Skip to content

Instantly share code, notes, and snippets.

@esutton
Last active August 11, 2021 14:48
Show Gist options
  • Save esutton/c19606e6962bfe535b1d80d672afb82b to your computer and use it in GitHub Desktop.
Save esutton/c19606e6962bfe535b1d80d672afb82b to your computer and use it in GitHub Desktop.
Convert output from Google's cpplint.py to the cppcheck XML format for consumption by Jenkins cppcheck plugin
#!/usr/bin/env python
# Convert output from Google's cpplint.py to the cppcheck XML format for
# consumption by the Jenkins cppcheck plugin.
# Reads from stdin and writes to stderr (to mimic cppcheck)
# https://stackoverflow.com/questions/14172232/how-to-make-cpplint-work-with-jenkins-warnings-plugin
import sys
import re
import xml.sax.saxutils
def cpplint_score_to_cppcheck_severity(score):
# I'm making this up
if score == 1:
return 'style'
elif score == 2:
return 'style'
elif score == 3:
return 'warning'
elif score == 4:
return 'warning'
elif score == 5:
return 'error'
def parse():
# TODO: do this properly, using the xml module.
# Write header
sys.stderr.write('''<?xml version="1.0" encoding="UTF-8"?>\n''')
sys.stderr.write('''<results>\n''')
# Do line-by-line conversion
r = re.compile('([^:]*):([0-9]*): ([^\[]*)\[([^\]]*)\] \[([0-9]*)\].*')
for l in sys.stdin.readlines():
m = r.match(l.strip())
if not m:
continue
g = m.groups()
if len(g) != 5:
continue
fname, lineno, rawmsg, label, score = g
# Protect Jenkins from bad XML, which makes it barf
msg = xml.sax.saxutils.escape(rawmsg)
# A "[google] prefix to make easy to distinguish ccplint warning from cppcheck messages
label = "[google]/" + label
# prepare data to be used as an attribute value
msg = xml.sax.saxutils.quoteattr(msg)
severity = cpplint_score_to_cppcheck_severity(int(score))
sys.stderr.write('''<error file="%s" line="%s" id="%s" severity="%s" msg=%s/>\n'''%(fname, lineno, label, severity, msg))
# Write footer
sys.stderr.write('''</results>\n''')
if __name__ == '__main__':
parse()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment