huangcm
2025-08-25 2f2fd745743ad500687c6985119d523146531958
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
#!/usr/bin/env python
 
from __future__ import unicode_literals
 
import io
import sys
import optparse
import os
 
from pygments import highlight
from pygments.lexers import guess_lexer_for_filename
from pygments.formatters import HtmlFormatter
from xml.sax import parse as xml_parse
from xml.sax import SAXParseException as XmlParseException
from xml.sax.handler import ContentHandler as XmlContentHandler
 
"""
Turns a cppcheck xml file into a browsable html report along
with syntax highlighted source code.
"""
 
STYLE_FILE = """
body {
    background-color: black;
    font: 13px Arial, Verdana, Sans-Serif;
    margin: 0;
    padding: 0;
}
 
.error {
    background-color: #ffb7b7;
}
 
.error2 {
    background-color: #faa;
    border: 1px dotted black;
    display: inline-block;
    margin-left: 4px;
}
 
.highlight .hll {
    padding: 1px;
}
 
#page {
    background-color: white;
    border: 2px solid #aaa;
    -webkit-box-sizing: content-box;
    -moz-box-sizing: content-box;
    box-sizing: content-box;
    margin: 30px;
    overflow: auto;
    padding: 5px 20px;
    width: auto;
}
 
#header {
    border-bottom: thin solid #aaa;
}
 
#menu {
    float: left;
    margin-top: 5px;
    text-align: left;
    width: 100px;
    height: auto;
}
 
#menu > a {
    display: block;
    margin-left: 10px;
}
 
#content {
    -webkit-box-sizing: content-box;
    -moz-box-sizing: content-box;
    box-sizing: content-box;
    border-left: thin solid #aaa;
    float: left;
    margin: 5px;
    padding: 0 10px 10px 10px;
    width: 80%;
}
 
.linenos {
    border-right: thin solid #aaa;
    color: lightgray;
    padding-right: 6px;
}
 
#footer {
    border-top: thin solid #aaa;
    clear: both;
    font-size: 90%;
    margin-top: 5px;
}
 
#footer ul {
    list-style-type: none;
    padding-left: 0;
}
"""
 
HTML_HEAD = """
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Cppcheck - HTML report - %s</title>
    <link rel="stylesheet" href="style.css">
    <style>
%s
    </style>
  </head>
  <body>
    <div id="page">
      <div id="header">
        <h1>Cppcheck report - %s</h1>
      </div>
      <div id="menu">
        <a href="index.html">Defect list</a>
      </div>
      <div id="content">
"""
 
HTML_FOOTER = """
      </div>
      <div id="footer">
        <p>
          Cppcheck - a tool for static C/C++ code analysis
        </p>
        <ul>
          <li>Internet: <a href="http://cppcheck.sourceforge.net">http://cppcheck.sourceforge.net</a></li>
          <li>Forum: <a href="http://apps.sourceforge.net/phpbb/cppcheck/">http://apps.sourceforge.net/phpbb/cppcheck/</a></li>
          <li>IRC: #cppcheck at irc.freenode.net</li>
        </ul>
      </div>
    </div>
  </body>
</html>
"""
 
HTML_ERROR = "<span class='error2'>&lt;--- %s</span>\n"
 
 
class AnnotateCodeFormatter(HtmlFormatter):
    errors = []
 
    def wrap(self, source, outfile):
        line_no = 1
        for i, t in HtmlFormatter.wrap(self, source, outfile):
            # If this is a source code line we want to add a span tag at the
            # end.
            if i == 1:
                for error in self.errors:
                    if error['line'] == line_no:
                        t = t.replace('\n', HTML_ERROR % error['msg'])
                line_no = line_no + 1
            yield i, t
 
 
class CppCheckHandler(XmlContentHandler):
 
    """Parses the cppcheck xml file and produces a list of all its errors."""
 
    def __init__(self):
        XmlContentHandler.__init__(self)
        self.errors = []
        self.version = '1'
 
    def startElement(self, name, attributes):
        if name == 'results':
            self.version = attributes.get('version', self.version)
 
        if self.version == '1':
            self.handleVersion1(name, attributes)
        else:
            self.handleVersion2(name, attributes)
 
    def handleVersion1(self, name, attributes):
        if name != 'error':
            return
 
        self.errors.append({
            'file': attributes.get('file', ''),
            'line': int(attributes.get('line', 0)),
            'id': attributes['id'],
            'severity': attributes['severity'],
            'msg': attributes['msg']
        })
 
    def handleVersion2(self, name, attributes):
        if name == 'error':
            self.errors.append({
                'file': '',
                'line': 0,
                'id': attributes['id'],
                'severity': attributes['severity'],
                'msg': attributes['msg']
            })
        elif name == 'location':
            assert self.errors
            self.errors[-1]['file'] = attributes['file']
            self.errors[-1]['line'] = int(attributes['line'])
 
 
if __name__ == '__main__':
    # Configure all the options this little utility is using.
    parser = optparse.OptionParser()
    parser.add_option('--title', dest='title',
                      help='The title of the project.',
                      default='[project name]')
    parser.add_option('--file', dest='file',
                      help='The cppcheck xml output file to read defects '
                           'from. Default is reading from stdin.')
    parser.add_option('--report-dir', dest='report_dir',
                      help='The directory where the HTML report content is '
                           'written.')
    parser.add_option('--source-dir', dest='source_dir',
                      help='Base directory where source code files can be '
                           'found.')
    parser.add_option('--source-encoding', dest='source_encoding',
                      help='Encoding of source code.', default='utf-8')
 
    # Parse options and make sure that we have an output directory set.
    options, args = parser.parse_args()
    if not options.report_dir:
        parser.error('No report directory set.')
 
    # Get the directory where source code files are located.
    source_dir = os.getcwd()
    if options.source_dir:
        source_dir = options.source_dir
 
    # Get the stream that we read cppcheck errors from.
    input_file = sys.stdin
    if options.file:
        if not os.path.exists(options.file):
            parser.error('cppcheck xml file: %s not found.' % options.file)
        input_file = io.open(options.file, 'r')
 
    # Parse the xml file and produce a simple list of errors.
    print('Parsing xml report.')
    try:
        contentHandler = CppCheckHandler()
        xml_parse(input_file, contentHandler)
    except XmlParseException as msg:
        print('Failed to parse cppcheck xml file: %s' % msg)
        sys.exit(1)
 
    # We have a list of errors. But now we want to group them on
    # each source code file. Lets create a files dictionary that
    # will contain a list of all the errors in that file. For each
    # file we will also generate a HTML filename to use.
    files = {}
    file_no = 0
    for error in contentHandler.errors:
        filename = error['file']
        if filename not in files.keys():
            files[filename] = {
                'errors': [], 'htmlfile': str(file_no) + '.html'}
            file_no = file_no + 1
        files[filename]['errors'].append(error)
 
    # Make sure that the report directory is created if it doesn't exist.
    print('Creating %s directory' % options.report_dir)
    if not os.path.exists(options.report_dir):
        os.mkdir(options.report_dir)
 
    # Generate a HTML file with syntax highlighted source code for each
    # file that contains one or more errors.
    print('Processing errors')
    for filename, data in files.items():
        htmlfile = data['htmlfile']
        errors = data['errors']
 
        lines = []
        for error in errors:
            lines.append(error['line'])
 
        if filename == '':
            continue
 
        source_filename = os.path.join(source_dir, filename)
        try:
            with io.open(source_filename, 'r') as input_file:
                content = input_file.read()
        except IOError:
            sys.stderr.write("ERROR: Source file '%s' not found.\n" %
                             source_filename)
            continue
 
        htmlFormatter = AnnotateCodeFormatter(linenos=True,
                                              style='colorful',
                                              hl_lines=lines,
                                              lineanchors='line',
                                              encoding=options.source_encoding)
        htmlFormatter.errors = errors
        with io.open(os.path.join(options.report_dir, htmlfile),
                     'w') as output_file:
            output_file.write(HTML_HEAD %
                              (options.title,
                               htmlFormatter.get_style_defs('.highlight'),
                               options.title))
 
            lexer = guess_lexer_for_filename(source_filename, '')
            if options.source_encoding:
                lexer.encoding = options.source_encoding
 
            output_file.write(
                highlight(content, lexer, htmlFormatter).decode(
                    options.source_encoding))
 
            output_file.write(HTML_FOOTER)
 
        print('  ' + filename)
 
    # Generate a master index.html file that will contain a list of
    # all the errors created.
    print('Creating index.html')
    with io.open(os.path.join(options.report_dir, 'index.html'),
                 'w') as output_file:
        output_file.write(HTML_HEAD % (options.title, '', options.title))
        output_file.write('<table>')
        output_file.write(
            '<tr><th>Line</th><th>Id</th><th>Severity</th><th>Message</th></tr>')
        for filename, data in files.items():
            output_file.write(
                "<tr><td colspan='4'><a href='%s'>%s</a></td></tr>" %
                (data['htmlfile'], filename))
            for error in data['errors']:
                if error['severity'] == 'error':
                    error_class = 'class="error"'
                else:
                    error_class = ''
 
                if error['id'] == 'missingInclude':
                    output_file.write(
                        '<tr><td></td><td>%s</td><td>%s</td><td>%s</td></tr>' %
                        (error['id'], error['severity'], error['msg']))
                else:
                    output_file.write(
                        "<tr><td><a href='%s#line-%d'>%d</a></td><td>%s</td><td>%s</td><td %s>%s</td></tr>" %
                        (data['htmlfile'], error['line'], error['line'],
                         error['id'], error['severity'], error_class,
                         error['msg']))
        output_file.write('</table>')
        output_file.write(HTML_FOOTER)
 
    print('Creating style.css file')
    with io.open(os.path.join(options.report_dir, 'style.css'),
                 'w') as css_file:
        css_file.write(STYLE_FILE)