From 6ad30a1c696e02f35f7ecb0314862e2558a4250a Mon Sep 17 00:00:00 2001 From: Steven Myint Date: Sat, 5 Oct 2013 08:03:31 -0700 Subject: [PATCH] Handle "--xml-version=2" This also adds "test_htmlreport.py" to the Travis CI tests. --- .travis.yml | 3 +++ htmlreport/cppcheck-htmlreport | 46 +++++++++++++++++++++++----------- htmlreport/test_htmlreport.py | 27 ++++++++++++++------ 3 files changed, 53 insertions(+), 23 deletions(-) diff --git a/.travis.yml b/.travis.yml index 969520854..7e5789476 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,6 +6,9 @@ script: - mkdir build - make test SRCDIR=build VERIFY=1 - ./cppcheck --error-exitcode=1 -Ilib --enable=style --suppress=duplicateBranch -q cli gui lib -igui/test + - sudo apt-get update + - sudo apt-get install python-pygments + - ./htmlreport/test_htmlreport.py notifications: irc: channels: diff --git a/htmlreport/cppcheck-htmlreport b/htmlreport/cppcheck-htmlreport index c35346943..763cfbc0b 100755 --- a/htmlreport/cppcheck-htmlreport +++ b/htmlreport/cppcheck-htmlreport @@ -166,29 +166,45 @@ class AnnotateCodeFormatter(HtmlFormatter): class CppCheckHandler(XmlContentHandler): """Parses the cppcheck xml file and produces a list of all its errors.""" - 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 - try: - file = attributes["file"] - line = int(attributes["line"]) - except: - file = "" - line = 0 - if attributes["id"] != "missingInclude" and attributes["id"] != "toomanyconfigs": - sys.stderr.write("ERROR: cppcheck error reported without a file name.\n") + self.errors.append({ + "file" : attributes.get("file", ""), + "line" : int(attributes.get("line"), 0), + "id" : attributes["id"], + "severity" : attributes["severity"], + "msg" : attributes["msg"] + }) - self.errors.append( - { - "file" : file, - "line" : line, + def handleVersion2(self, name, attributes): + if name == "error": + self.errors.append({ "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. @@ -212,7 +228,7 @@ if __name__ == '__main__': # Get the stream that we read cppcheck errors from. stream = sys.stdin if options.file: - if os.path.exists(options.file) == False: + if not os.path.exists(options.file): parser.error("cppcheck xml file: %s not found." % options.file) stream = open(options.file, "r") @@ -234,7 +250,7 @@ if __name__ == '__main__': for error in contentHandler.errors: filename = error["file"] if filename not in files.keys(): - files[filename] = { "errors" : [], "htmlfile" : str(file_no) + ".html" } + files[filename] = {"errors" : [], "htmlfile" : str(file_no) + ".html"} file_no = file_no + 1 files[filename]["errors"].append(error) diff --git a/htmlreport/test_htmlreport.py b/htmlreport/test_htmlreport.py index f8042ce73..17b344656 100755 --- a/htmlreport/test_htmlreport.py +++ b/htmlreport/test_htmlreport.py @@ -24,9 +24,14 @@ HTML_REPORT_BIN = os.path.join(os.path.abspath(os.path.dirname(__file__)), class TestHTMLReport(unittest.TestCase): def testReportError(self): - with runCheck(os.path.join( - ROOT_DIR, - 'samples', 'memleak', 'bad.c')) as (report, output_directory): + for xml_version in ['1', '2']: + self.checkReportError(xml_version) + + def checkReportError(self, xml_version): + with runCheck( + os.path.join(ROOT_DIR, 'samples', 'memleak', 'bad.c'), + xml_version=xml_version + ) as (report, output_directory): self.assertIn('