summaryrefslogtreecommitdiff
path: root/scripts/cpplint_to_cppcheckxml.py
blob: 2447178fc99392a343a297eee1a0dcc549975a66 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/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)


import sys
import re

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''')
    # VR : sys.stderr.write('''<results>\n''')
    # Add from VR  + [
    sys.stderr.write('''<results version="2">\n''') 
    sys.stderr.write('''    <cppcheck version="1.63"/>\n''')
    sys.stderr.write('''    <errors>\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, msg, label, score = g  
        severity = cpplint_score_to_cppcheck_severity(int(score))
        # VR : sys.stderr.write('''<error file="%s" line="%s" id="%s" severity="%s" msg="%s"/>\n'''%(fname, lineno, label, severity, msg))
        # Add from VR  + [
        sys.stderr.write('''        <error id="%s" severity="%s" msg="%s"/>\n'''%(label, severity, msg))
        sys.stderr.write('''            <location file="%s" line="%s"/>\n'''%(fname, lineno))
        sys.stderr.write('''        </error>\n'''%(fname, lineno))
        # -]

    # Write footer
    # Add from VR  + [
    sys.stderr.write('''</errors>\n''')
    # -]
    sys.stderr.write('''</results>\n''')


if __name__ == '__main__':
    parse()