Skip to content

Commit

Permalink
Merge pull request danmar#173 from myint/master
Browse files Browse the repository at this point in the history
Handle "--xml-version=2" in cppcheck-htmlreport
  • Loading branch information
danmar committed Oct 7, 2013
2 parents 3f1e074 + 6ad30a1 commit f5593d5
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 24 deletions.
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
48 changes: 32 additions & 16 deletions htmlreport/cppcheck-htmlreport
Original file line number Diff line number Diff line change
Expand Up @@ -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" : file,
"line" : line,
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({
"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.
Expand All @@ -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")

Expand All @@ -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)

Expand Down
27 changes: 19 additions & 8 deletions htmlreport/test_htmlreport.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,14 @@
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('<html', report)

self.assertIn('Memory leak:', report)
Expand All @@ -42,9 +47,14 @@ def testReportError(self):
self.assertIn('Memory leak:', detail_contents)

def testReportNoError(self):
with runCheck(os.path.join(
ROOT_DIR,
'samples', 'memleak', 'good.c')) as (report, output_directory):
for xml_version in ['1', '2']:
self.checkReportNoError(xml_version)

def checkReportNoError(self, xml_version):
with runCheck(
os.path.join(ROOT_DIR, 'samples', 'memleak', 'good.c'),
xml_version=xml_version
) as (report, output_directory):
self.assertIn('<html', report)

self.assertNotIn('Memory leak:', report)
Expand All @@ -55,7 +65,7 @@ def testReportNoError(self):


@contextlib.contextmanager
def runCheck(source_file):
def runCheck(source_file, xml_version):
"""Run cppcheck and cppcheck-htmlreport.
Yield a tuple containing the resulting HTML report index and the directory
Expand All @@ -67,7 +77,8 @@ def runCheck(source_file):

with open(xml_filename, 'w') as output_file:
subprocess.check_call(
[CPPCHECK_BIN, '--xml', source_file],
[CPPCHECK_BIN, '--xml', source_file,
'--xml-version=' + xml_version],
stderr=output_file)

assert os.path.exists(xml_filename)
Expand Down

0 comments on commit f5593d5

Please sign in to comment.