2012-09-07 18:44:46 +02:00
|
|
|
#!/usr/bin/env python
|
2013-10-23 15:27:27 +02:00
|
|
|
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
|
|
|
import io
|
2009-12-08 21:09:21 +01:00
|
|
|
import sys
|
|
|
|
import optparse
|
|
|
|
import os
|
2013-10-23 15:27:27 +02:00
|
|
|
|
2009-12-08 21:09:21 +01:00
|
|
|
from pygments import highlight
|
2012-09-07 18:44:46 +02:00
|
|
|
from pygments.lexers import guess_lexer_for_filename
|
2009-12-08 21:09:21 +01:00
|
|
|
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.
|
|
|
|
"""
|
|
|
|
|
2009-12-11 12:39:39 +01:00
|
|
|
STYLE_FILE = """
|
2013-10-13 10:03:09 +02:00
|
|
|
body {
|
|
|
|
font: normal 13px/1 Arial, Verdana, Sans-Serif;
|
2012-09-28 20:25:46 +02:00
|
|
|
background-color: black;
|
2012-12-23 11:50:52 +01:00
|
|
|
padding: 0;
|
|
|
|
margin: 0;
|
2009-12-11 12:39:39 +01:00
|
|
|
}
|
|
|
|
|
2012-09-15 22:41:56 +02:00
|
|
|
.error {
|
2012-09-28 20:25:46 +02:00
|
|
|
font-size: 13px;
|
|
|
|
background-color: #ffb7b7;
|
2012-12-23 11:50:52 +01:00
|
|
|
padding: 0;
|
|
|
|
margin: 0;
|
2012-09-15 22:41:56 +02:00
|
|
|
}
|
|
|
|
|
2009-12-11 12:39:39 +01:00
|
|
|
|
|
|
|
#page {
|
2012-09-28 20:25:46 +02:00
|
|
|
width: 860px;
|
|
|
|
margin: auto;
|
2012-12-23 11:50:52 +01:00
|
|
|
border-left: 2px solid #aaa;
|
|
|
|
border-right: 2px solid #aaa;
|
|
|
|
background-color: white;
|
2012-09-28 20:25:46 +02:00
|
|
|
padding: 20px;
|
2009-12-11 12:39:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#header {
|
2012-09-28 20:25:46 +02:00
|
|
|
width: 100%;
|
|
|
|
height: 70px;
|
2012-12-23 11:50:52 +01:00
|
|
|
border-bottom: thin solid #aaa;
|
2009-12-11 12:39:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#menu {
|
2012-09-28 20:25:46 +02:00
|
|
|
margin-top: 5px;
|
|
|
|
text-align: left;
|
|
|
|
float: left;
|
|
|
|
width: 100px;
|
|
|
|
height: 300px;
|
2009-12-11 12:39:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#menu > a {
|
2012-09-28 20:25:46 +02:00
|
|
|
margin-left: 10px;
|
|
|
|
display: block;
|
2009-12-11 12:39:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#content {
|
2012-09-28 20:25:46 +02:00
|
|
|
float: left;
|
|
|
|
width: 720px;
|
|
|
|
margin: 5px;
|
2012-12-23 11:50:52 +01:00
|
|
|
padding: 0 10px 10px 10px;
|
|
|
|
border-left: thin solid #aaa;
|
2009-12-11 12:39:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#footer {
|
2012-09-28 20:25:46 +02:00
|
|
|
padding-bottom: 5px;
|
|
|
|
padding-top: 5px;
|
2012-12-23 11:50:52 +01:00
|
|
|
border-top: thin solid #aaa;
|
2012-09-28 20:25:46 +02:00
|
|
|
clear: both;
|
2013-10-13 10:03:09 +02:00
|
|
|
font-size: 90%;
|
2009-12-11 12:39:39 +01:00
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
2009-12-08 21:09:21 +01:00
|
|
|
HTML_HEAD = """
|
2013-10-13 10:03:09 +02:00
|
|
|
<!DOCTYPE HTML>
|
|
|
|
<html lang="en">
|
2009-12-08 21:09:21 +01:00
|
|
|
<head>
|
2013-10-13 10:03:09 +02:00
|
|
|
<meta charset="utf-8">
|
|
|
|
<title>Cppcheck - HTML report - %s</title>
|
|
|
|
<link href="style.css" rel="stylesheet">
|
|
|
|
<style>
|
2010-01-12 20:34:55 +01:00
|
|
|
%s
|
|
|
|
</style>
|
2009-12-08 21:09:21 +01:00
|
|
|
</head>
|
2013-10-13 10:03:09 +02:00
|
|
|
<body>
|
2009-12-11 12:39:39 +01:00
|
|
|
<div id="page">
|
|
|
|
<div id="header">
|
2013-10-13 10:03:09 +02:00
|
|
|
<h1>Cppcheck report - %s</h1>
|
2009-12-11 12:39:39 +01:00
|
|
|
</div>
|
|
|
|
<div id="menu">
|
|
|
|
<a href="index.html">Defect list</a>
|
|
|
|
</div>
|
|
|
|
<div id="content">
|
2009-12-08 21:09:21 +01:00
|
|
|
"""
|
|
|
|
|
|
|
|
HTML_FOOTER = """
|
2009-12-11 12:39:39 +01:00
|
|
|
</div>
|
|
|
|
<div id="footer">
|
2013-10-13 10:03:09 +02:00
|
|
|
<p>
|
|
|
|
Cppcheck - a tool for static C/C++ code analysis
|
|
|
|
</p>
|
|
|
|
<p>
|
2009-12-11 12:39:39 +01:00
|
|
|
Internet: <a href="http://cppcheck.sourceforge.net">http://cppcheck.sourceforge.net</a><br/>
|
|
|
|
Forum: <a href="http://apps.sourceforge.net/phpbb/cppcheck/">http://apps.sourceforge.net/phpbb/cppcheck/</a><br/>
|
|
|
|
IRC: #cppcheck at irc.freenode.net
|
2013-10-13 10:03:09 +02:00
|
|
|
</p>
|
2009-12-11 12:39:39 +01:00
|
|
|
</div>
|
|
|
|
</div>
|
2009-12-08 21:09:21 +01:00
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
"""
|
|
|
|
|
2010-01-12 21:35:54 +01:00
|
|
|
HTML_ERROR = "<span style=\"border-width: 2px;border-color: black;border-style: solid;background: #ffaaaa;padding: 3px;\"><--- %s</span>\n"
|
|
|
|
|
2013-03-02 16:45:26 +01:00
|
|
|
|
2010-01-12 21:35:54 +01:00
|
|
|
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
|
|
|
|
|
2013-03-02 16:45:26 +01:00
|
|
|
|
2009-12-08 21:09:21 +01:00
|
|
|
class CppCheckHandler(XmlContentHandler):
|
|
|
|
"""Parses the cppcheck xml file and produces a list of all its errors."""
|
2013-10-05 17:03:31 +02:00
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
XmlContentHandler.__init__(self)
|
|
|
|
self.errors = []
|
|
|
|
self.version = "1"
|
2009-12-08 21:09:21 +01:00
|
|
|
|
|
|
|
def startElement(self, name, attributes):
|
2013-10-05 17:03:31 +02:00
|
|
|
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):
|
2009-12-08 21:09:21 +01:00
|
|
|
if name != "error":
|
|
|
|
return
|
|
|
|
|
2013-10-05 17:03:31 +02:00
|
|
|
self.errors.append({
|
2013-10-12 10:47:37 +02:00
|
|
|
"file": attributes.get("file", ""),
|
|
|
|
"line": int(attributes.get("line", 0)),
|
|
|
|
"id": attributes["id"],
|
|
|
|
"severity": attributes["severity"],
|
|
|
|
"msg": attributes["msg"]
|
2013-10-05 17:03:31 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
def handleVersion2(self, name, attributes):
|
|
|
|
if name == "error":
|
|
|
|
self.errors.append({
|
2013-10-12 10:47:37 +02:00
|
|
|
"file": "",
|
|
|
|
"line": 0,
|
|
|
|
"id": attributes["id"],
|
|
|
|
"severity": attributes["severity"],
|
|
|
|
"msg": attributes["msg"]
|
2013-10-05 12:20:25 +02:00
|
|
|
})
|
2013-10-05 17:03:31 +02:00
|
|
|
elif name == "location":
|
|
|
|
assert self.errors
|
|
|
|
self.errors[-1]["file"] = attributes["file"]
|
|
|
|
self.errors[-1]["line"] = int(attributes["line"])
|
|
|
|
|
2009-12-08 21:09:21 +01:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
# Configure all the options this little utility is using.
|
|
|
|
parser = optparse.OptionParser()
|
2010-01-13 14:11:54 +01:00
|
|
|
parser.add_option("--title", dest="title", help="The title of the project.", default="[project name]")
|
2009-12-08 21:09:21 +01:00
|
|
|
parser.add_option("--file", dest="file", help="The cppcheck xml output file to read defects from. Default is reading from stdin.")
|
2013-10-13 10:03:09 +02:00
|
|
|
parser.add_option("--report-dir", dest="report_dir", help="The directory where the HTML report content is written.")
|
2009-12-08 21:09:21 +01:00
|
|
|
parser.add_option("--source-dir", dest="source_dir", help="Base directory where source code files can be found.")
|
2013-10-23 15:27:27 +02:00
|
|
|
parser.add_option("--source-encoding", dest="source_encoding", help="Encoding of source code.", default='utf-8')
|
2009-12-08 21:09:21 +01:00
|
|
|
|
|
|
|
# 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.
|
|
|
|
stream = sys.stdin
|
|
|
|
if options.file:
|
2013-10-05 17:03:31 +02:00
|
|
|
if not os.path.exists(options.file):
|
2009-12-08 21:09:21 +01:00
|
|
|
parser.error("cppcheck xml file: %s not found." % options.file)
|
2013-10-23 15:27:27 +02:00
|
|
|
stream = io.open(options.file, "r")
|
2009-12-08 21:09:21 +01:00
|
|
|
|
|
|
|
# Parse the xml file and produce a simple list of errors.
|
|
|
|
print("Parsing xml report.")
|
|
|
|
try:
|
|
|
|
contentHandler = CppCheckHandler()
|
|
|
|
xml_parse(stream, contentHandler)
|
2012-09-07 18:44:46 +02:00
|
|
|
except XmlParseException as msg:
|
2009-12-08 21:09:21 +01:00
|
|
|
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
|
2013-10-13 10:03:09 +02:00
|
|
|
# file we will also generate a HTML filename to use.
|
2009-12-08 21:09:21 +01:00
|
|
|
files = {}
|
|
|
|
file_no = 0
|
|
|
|
for error in contentHandler.errors:
|
|
|
|
filename = error["file"]
|
|
|
|
if filename not in files.keys():
|
2013-10-12 10:47:37 +02:00
|
|
|
files[filename] = {"errors": [], "htmlfile": str(file_no) + ".html"}
|
2009-12-08 21:09:21 +01:00
|
|
|
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)
|
|
|
|
|
2013-10-13 10:03:09 +02:00
|
|
|
# Generate a HTML file with syntax highlighted source code for each
|
2009-12-08 21:09:21 +01:00
|
|
|
# file that contains one or more errors.
|
|
|
|
print("Processing errors")
|
2012-09-07 18:44:46 +02:00
|
|
|
for filename, data in files.items():
|
2013-10-23 15:27:27 +02:00
|
|
|
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)
|
|
|
|
if not os.path.isfile(source_filename):
|
|
|
|
sys.stderr.write("ERROR: Source file '%s' not found.\n" %
|
|
|
|
source_filename)
|
|
|
|
continue
|
|
|
|
with io.open(source_filename, 'r') as input_file:
|
|
|
|
content = input_file.read()
|
|
|
|
|
|
|
|
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, "")
|
2011-04-16 11:22:44 +02:00
|
|
|
if options.source_encoding:
|
|
|
|
lexer.encoding = options.source_encoding
|
2010-01-14 09:47:38 +01:00
|
|
|
|
2013-10-23 15:27:27 +02:00
|
|
|
output_file.write(
|
|
|
|
highlight(content, lexer, htmlFormatter).decode(
|
|
|
|
options.source_encoding))
|
|
|
|
|
|
|
|
output_file.write(HTML_FOOTER)
|
|
|
|
|
|
|
|
print(" " + filename)
|
2009-12-08 21:09:21 +01:00
|
|
|
|
|
|
|
# Generate a master index.html file that will contain a list of
|
|
|
|
# all the errors created.
|
|
|
|
print("Creating index.html")
|
2013-10-23 15:27:27 +02:00
|
|
|
stream = io.open(os.path.join(options.report_dir, "index.html"), "w")
|
2010-01-13 14:11:54 +01:00
|
|
|
stream.write(HTML_HEAD % (options.title, "", options.title))
|
2009-12-11 12:39:39 +01:00
|
|
|
stream.write("<table>")
|
|
|
|
stream.write("<tr><th>Line</th><th>Id</th><th>Severity</th><th>Message</th></tr>")
|
2012-09-07 18:44:46 +02:00
|
|
|
for filename, data in files.items():
|
2009-12-11 12:39:39 +01:00
|
|
|
stream.write("<tr><td colspan='4'><a href=\"%s\">%s</a></td></tr>" % (data["htmlfile"], filename))
|
|
|
|
for error in data["errors"]:
|
2012-09-15 22:41:56 +02:00
|
|
|
if error['severity'] == 'error':
|
|
|
|
error_class = 'class="error"'
|
|
|
|
else:
|
|
|
|
error_class = ''
|
|
|
|
|
2011-08-30 20:30:52 +02:00
|
|
|
if error["id"] == "missingInclude":
|
|
|
|
stream.write("<tr><td></td><td>%s</td><td>%s</td><td>%s</td></tr>" %
|
|
|
|
(error["id"], error["severity"], error["msg"]))
|
|
|
|
else:
|
2012-09-15 22:41:56 +02:00
|
|
|
stream.write("<tr><td><a href='%s#line-%d'>%d</a></td><td>%s</td><td>%s</td><td %s>%s</td></tr>" %
|
2011-08-30 20:30:52 +02:00
|
|
|
(data["htmlfile"], error["line"], error["line"], error["id"],
|
2012-09-15 22:41:56 +02:00
|
|
|
error["severity"], error_class, error["msg"]))
|
2009-12-11 12:39:39 +01:00
|
|
|
stream.write("</table>")
|
2009-12-08 21:09:21 +01:00
|
|
|
stream.write(HTML_FOOTER)
|
|
|
|
stream.close()
|
2009-12-11 12:39:39 +01:00
|
|
|
|
|
|
|
print("Creating style.css file")
|
2013-10-23 15:27:27 +02:00
|
|
|
stream = io.open(os.path.join(options.report_dir, "style.css"), "w")
|
2009-12-11 12:39:39 +01:00
|
|
|
stream.write(STYLE_FILE)
|
|
|
|
stream.close()
|