Merge branch 'master' of git://github.com/nsmoooose/cppcheck

This commit is contained in:
Daniel Marjamäki 2010-01-16 15:28:01 +01:00
commit 999176396b
1 changed files with 49 additions and 23 deletions

View File

@ -112,10 +112,14 @@ body.body {
""" """
HTML_HEAD = """ HTML_HEAD = """
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html> <html>
<head> <head>
<title>CppCheck - Html report</title> <title>CppCheck - Html report - %s</title>
<link href="style.css" rel="stylesheet" /> <link href="style.css" rel="stylesheet" type="text/css" />
<style type="text/css">
%s
</style>
</head> </head>
<body class="body"> <body class="body">
<div id="page-header"> <div id="page-header">
@ -123,7 +127,7 @@ HTML_HEAD = """
</div> </div>
<div id="page"> <div id="page">
<div id="header"> <div id="header">
<h1>CppCheck report</h1> <h1>CppCheck report - %s</h1>
</div> </div>
<div id="menu"> <div id="menu">
<a href="index.html">Defect list</a> <a href="index.html">Defect list</a>
@ -153,6 +157,22 @@ HTML_FOOTER = """
</html> </html>
""" """
HTML_ERROR = "<span style=\"border-width: 2px;border-color: black;border-style: solid;background: #ffaaaa;padding: 3px;\">&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): class CppCheckHandler(XmlContentHandler):
"""Parses the cppcheck xml file and produces a list of all its errors.""" """Parses the cppcheck xml file and produces a list of all its errors."""
errors = [] errors = []
@ -175,9 +195,11 @@ class CppCheckHandler(XmlContentHandler):
if __name__ == '__main__': if __name__ == '__main__':
# Configure all the options this little utility is using. # Configure all the options this little utility is using.
parser = optparse.OptionParser() 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("--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("--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-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=None)
# Parse options and make sure that we have an output directory set. # Parse options and make sure that we have an output directory set.
options, args = parser.parse_args() options, args = parser.parse_args()
@ -227,35 +249,39 @@ if __name__ == '__main__':
# file that contains one or more errors. # file that contains one or more errors.
print("Processing errors") print("Processing errors")
for filename, data in files.iteritems(): for filename, data in files.iteritems():
htmlfile = data["htmlfile"] try:
errors = data["errors"] htmlfile = data["htmlfile"]
errors = data["errors"]
lines = [] lines = []
for error in errors: for error in errors:
lines.append(error["line"]) lines.append(error["line"])
source_file = os.path.join(source_dir, filename) source_file = os.path.join(source_dir, filename)
if not os.path.isfile(source_file): if not os.path.isfile(source_file):
sys.stderr.write("ERROR: Source file '%s' not found.\n" % source_file) sys.stderr.write("ERROR: Source file '%s' not found.\n" % source_file)
continue continue
stream = file(source_file) stream = file(source_file)
content = stream.read() content = stream.read()
stream.close() stream.close()
htmlFormatter = HtmlFormatter(linenos=True, style='colorful', full=True, hl_lines=lines, lineanchors="line") htmlFormatter = AnnotateCodeFormatter(linenos=True, style='colorful', hl_lines=lines, lineanchors="line", encoding=options.source_encoding)
stream = file(os.path.join(options.report_dir, htmlfile), "w") htmlFormatter.errors = errors
stream.write(HTML_HEAD) stream = file(os.path.join(options.report_dir, htmlfile), "w")
stream.write(highlight(content, guess_lexer_for_filename(source_file, ""), htmlFormatter)) stream.write(HTML_HEAD % (options.title, htmlFormatter.get_style_defs(".highlight"), options.title))
stream.write(HTML_FOOTER) stream.write(highlight(content, guess_lexer_for_filename(source_file, ""), htmlFormatter))
stream.close() stream.write(HTML_FOOTER)
stream.close()
print(" " + filename) print(" " + filename)
except Exception, message:
print("ERROR: Filename: %s, %s" % (filename, message))
# Generate a master index.html file that will contain a list of # Generate a master index.html file that will contain a list of
# all the errors created. # all the errors created.
print("Creating index.html") print("Creating index.html")
stream = file(os.path.join(options.report_dir, "index.html"), "w") stream = file(os.path.join(options.report_dir, "index.html"), "w")
stream.write(HTML_HEAD) stream.write(HTML_HEAD % (options.title, "", options.title))
stream.write("<table>") stream.write("<table>")
stream.write("<tr><th>Line</th><th>Id</th><th>Severity</th><th>Message</th></tr>") stream.write("<tr><th>Line</th><th>Id</th><th>Severity</th><th>Message</th></tr>")
for filename, data in files.iteritems(): for filename, data in files.iteritems():