2009-08-07 05:15:33 +08:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
"""
|
|
|
|
CmpRuns - A simple tool for comparing two static analyzer runs to determine
|
|
|
|
which reports have been added, removed, or changed.
|
|
|
|
|
|
|
|
This is designed to support automated testing using the static analyzer, from
|
2015-09-08 11:50:52 +08:00
|
|
|
two perspectives:
|
2009-08-07 05:15:33 +08:00
|
|
|
1. To monitor changes in the static analyzer's reports on real code bases, for
|
|
|
|
regression testing.
|
|
|
|
|
|
|
|
2. For use by end users who want to integrate regular static analyzer testing
|
|
|
|
into a buildbot like environment.
|
2012-07-17 04:21:42 +08:00
|
|
|
|
|
|
|
Usage:
|
|
|
|
|
|
|
|
# Load the results of both runs, to obtain lists of the corresponding
|
|
|
|
# AnalysisDiagnostic objects.
|
2012-08-02 08:41:40 +08:00
|
|
|
#
|
2012-10-16 06:48:21 +08:00
|
|
|
resultsA = loadResultsFromSingleRun(singleRunInfoA, deleteEmpty)
|
|
|
|
resultsB = loadResultsFromSingleRun(singleRunInfoB, deleteEmpty)
|
2015-09-08 11:50:52 +08:00
|
|
|
|
|
|
|
# Generate a relation from diagnostics in run A to diagnostics in run B
|
|
|
|
# to obtain a list of triples (a, b, confidence).
|
2012-07-17 04:21:42 +08:00
|
|
|
diff = compareResults(resultsA, resultsB)
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2009-08-07 05:15:33 +08:00
|
|
|
"""
|
|
|
|
|
|
|
|
import os
|
|
|
|
import plistlib
|
2012-10-16 06:48:21 +08:00
|
|
|
import CmpRuns
|
|
|
|
|
|
|
|
# Information about analysis run:
|
|
|
|
# path - the analysis output directory
|
2015-09-08 11:50:52 +08:00
|
|
|
# root - the name of the root directory, which will be disregarded when
|
2012-10-16 06:48:21 +08:00
|
|
|
# determining the source file name
|
|
|
|
class SingleRunInfo:
|
|
|
|
def __init__(self, path, root="", verboseLog=None):
|
|
|
|
self.path = path
|
2015-07-09 02:39:31 +08:00
|
|
|
self.root = root.rstrip("/\\")
|
2012-10-16 06:48:21 +08:00
|
|
|
self.verboseLog = verboseLog
|
2009-08-07 05:15:33 +08:00
|
|
|
|
2012-07-17 04:21:42 +08:00
|
|
|
class AnalysisDiagnostic:
|
|
|
|
def __init__(self, data, report, htmlReport):
|
|
|
|
self._data = data
|
|
|
|
self._loc = self._data['location']
|
|
|
|
self._report = report
|
|
|
|
self._htmlReport = htmlReport
|
|
|
|
|
|
|
|
def getFileName(self):
|
2012-10-16 06:48:21 +08:00
|
|
|
root = self._report.run.root
|
2012-10-18 05:09:26 +08:00
|
|
|
fileName = self._report.files[self._loc['file']]
|
2015-07-09 02:39:31 +08:00
|
|
|
if fileName.startswith(root) and len(root) > 0:
|
|
|
|
return fileName[len(root)+1:]
|
2012-10-16 06:48:21 +08:00
|
|
|
return fileName
|
|
|
|
|
2012-07-17 04:21:42 +08:00
|
|
|
def getLine(self):
|
|
|
|
return self._loc['line']
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2012-07-17 04:21:42 +08:00
|
|
|
def getColumn(self):
|
|
|
|
return self._loc['col']
|
|
|
|
|
|
|
|
def getCategory(self):
|
|
|
|
return self._data['category']
|
|
|
|
|
|
|
|
def getDescription(self):
|
|
|
|
return self._data['description']
|
|
|
|
|
|
|
|
def getIssueIdentifier(self) :
|
2012-10-16 06:48:21 +08:00
|
|
|
id = self.getFileName() + "+"
|
2012-07-17 04:21:42 +08:00
|
|
|
if 'issue_context' in self._data :
|
2012-10-16 06:48:21 +08:00
|
|
|
id += self._data['issue_context'] + "+"
|
2015-10-22 19:53:04 +08:00
|
|
|
if 'issue_hash_content_of_line_in_context' in self._data :
|
|
|
|
id += str(self._data['issue_hash_content_of_line_in_context'])
|
2012-10-16 06:48:21 +08:00
|
|
|
return id
|
2012-07-17 04:21:42 +08:00
|
|
|
|
|
|
|
def getReport(self):
|
|
|
|
if self._htmlReport is None:
|
|
|
|
return " "
|
|
|
|
return os.path.join(self._report.run.path, self._htmlReport)
|
|
|
|
|
|
|
|
def getReadableName(self):
|
2015-09-08 11:50:52 +08:00
|
|
|
return '%s:%d:%d, %s: %s' % (self.getFileName(), self.getLine(),
|
|
|
|
self.getColumn(), self.getCategory(),
|
2012-07-17 04:21:42 +08:00
|
|
|
self.getDescription())
|
2015-09-08 11:50:52 +08:00
|
|
|
|
|
|
|
# Note, the data format is not an API and may change from one analyzer
|
|
|
|
# version to another.
|
2012-10-18 05:09:26 +08:00
|
|
|
def getRawData(self):
|
|
|
|
return self._data
|
2009-08-07 05:15:33 +08:00
|
|
|
|
|
|
|
class multidict:
|
|
|
|
def __init__(self, elts=()):
|
|
|
|
self.data = {}
|
|
|
|
for key,value in elts:
|
|
|
|
self[key] = value
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2009-08-07 05:15:33 +08:00
|
|
|
def __getitem__(self, item):
|
|
|
|
return self.data[item]
|
|
|
|
def __setitem__(self, key, value):
|
|
|
|
if key in self.data:
|
|
|
|
self.data[key].append(value)
|
|
|
|
else:
|
|
|
|
self.data[key] = [value]
|
|
|
|
def items(self):
|
|
|
|
return self.data.items()
|
|
|
|
def values(self):
|
|
|
|
return self.data.values()
|
|
|
|
def keys(self):
|
|
|
|
return self.data.keys()
|
|
|
|
def __len__(self):
|
|
|
|
return len(self.data)
|
|
|
|
def get(self, key, default=None):
|
|
|
|
return self.data.get(key, default)
|
|
|
|
|
2011-09-13 05:32:41 +08:00
|
|
|
class CmpOptions:
|
2012-08-02 08:41:40 +08:00
|
|
|
def __init__(self, verboseLog=None, rootA="", rootB=""):
|
|
|
|
self.rootA = rootA
|
|
|
|
self.rootB = rootB
|
2011-09-13 05:32:41 +08:00
|
|
|
self.verboseLog = verboseLog
|
|
|
|
|
2009-08-07 05:15:33 +08:00
|
|
|
class AnalysisReport:
|
2012-11-16 06:42:44 +08:00
|
|
|
def __init__(self, run, files):
|
2009-08-07 05:15:33 +08:00
|
|
|
self.run = run
|
2012-10-18 05:09:26 +08:00
|
|
|
self.files = files
|
|
|
|
self.diagnostics = []
|
2009-08-07 05:15:33 +08:00
|
|
|
|
|
|
|
class AnalysisRun:
|
2012-10-16 06:48:21 +08:00
|
|
|
def __init__(self, info):
|
|
|
|
self.path = info.path
|
|
|
|
self.root = info.root
|
|
|
|
self.info = info
|
2009-08-07 05:15:33 +08:00
|
|
|
self.reports = []
|
2012-10-18 05:09:26 +08:00
|
|
|
# Cumulative list of all diagnostics from all the reports.
|
2009-08-07 05:15:33 +08:00
|
|
|
self.diagnostics = []
|
2012-11-16 06:42:44 +08:00
|
|
|
self.clang_version = None
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2012-11-16 06:42:44 +08:00
|
|
|
def getClangVersion(self):
|
|
|
|
return self.clang_version
|
2009-08-07 05:15:33 +08:00
|
|
|
|
2013-03-23 09:21:26 +08:00
|
|
|
def readSingleFile(self, p, deleteEmpty):
|
|
|
|
data = plistlib.readPlist(p)
|
|
|
|
|
2015-09-08 11:50:52 +08:00
|
|
|
# We want to retrieve the clang version even if there are no
|
|
|
|
# reports. Assume that all reports were created using the same
|
2013-03-23 09:21:26 +08:00
|
|
|
# clang version (this is always true and is more efficient).
|
|
|
|
if 'clang_version' in data:
|
|
|
|
if self.clang_version == None:
|
|
|
|
self.clang_version = data.pop('clang_version')
|
|
|
|
else:
|
|
|
|
data.pop('clang_version')
|
|
|
|
|
|
|
|
# Ignore/delete empty reports.
|
|
|
|
if not data['files']:
|
|
|
|
if deleteEmpty == True:
|
|
|
|
os.remove(p)
|
|
|
|
return
|
|
|
|
|
|
|
|
# Extract the HTML reports, if they exists.
|
|
|
|
if 'HTMLDiagnostics_files' in data['diagnostics'][0]:
|
|
|
|
htmlFiles = []
|
|
|
|
for d in data['diagnostics']:
|
|
|
|
# FIXME: Why is this named files, when does it have multiple
|
|
|
|
# files?
|
|
|
|
assert len(d['HTMLDiagnostics_files']) == 1
|
|
|
|
htmlFiles.append(d.pop('HTMLDiagnostics_files')[0])
|
|
|
|
else:
|
|
|
|
htmlFiles = [None] * len(data['diagnostics'])
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2013-03-23 09:21:26 +08:00
|
|
|
report = AnalysisReport(self, data.pop('files'))
|
2015-09-08 11:50:52 +08:00
|
|
|
diagnostics = [AnalysisDiagnostic(d, report, h)
|
2013-03-23 09:21:26 +08:00
|
|
|
for d,h in zip(data.pop('diagnostics'),
|
|
|
|
htmlFiles)]
|
|
|
|
|
|
|
|
assert not data
|
|
|
|
|
|
|
|
report.diagnostics.extend(diagnostics)
|
|
|
|
self.reports.append(report)
|
|
|
|
self.diagnostics.extend(diagnostics)
|
|
|
|
|
2009-08-07 05:15:33 +08:00
|
|
|
|
2015-09-08 11:50:52 +08:00
|
|
|
# Backward compatibility API.
|
2012-08-02 08:41:40 +08:00
|
|
|
def loadResults(path, opts, root = "", deleteEmpty=True):
|
2012-10-16 06:48:21 +08:00
|
|
|
return loadResultsFromSingleRun(SingleRunInfo(path, root, opts.verboseLog),
|
|
|
|
deleteEmpty)
|
|
|
|
|
|
|
|
# Load results of the analyzes from a given output folder.
|
|
|
|
# - info is the SingleRunInfo object
|
|
|
|
# - deleteEmpty specifies if the empty plist files should be deleted
|
|
|
|
def loadResultsFromSingleRun(info, deleteEmpty=True):
|
|
|
|
path = info.path
|
|
|
|
run = AnalysisRun(info)
|
2013-03-23 09:21:26 +08:00
|
|
|
|
|
|
|
if os.path.isfile(path):
|
|
|
|
run.readSingleFile(path, deleteEmpty)
|
|
|
|
else:
|
|
|
|
for (dirpath, dirnames, filenames) in os.walk(path):
|
|
|
|
for f in filenames:
|
|
|
|
if (not f.endswith('plist')):
|
|
|
|
continue
|
|
|
|
p = os.path.join(dirpath, f)
|
|
|
|
run.readSingleFile(p, deleteEmpty)
|
|
|
|
|
2009-08-07 05:15:33 +08:00
|
|
|
return run
|
|
|
|
|
2012-07-17 04:21:42 +08:00
|
|
|
def cmpAnalysisDiagnostic(d) :
|
|
|
|
return d.getIssueIdentifier()
|
2012-06-08 09:50:49 +08:00
|
|
|
|
2009-08-07 05:15:33 +08:00
|
|
|
def compareResults(A, B):
|
|
|
|
"""
|
|
|
|
compareResults - Generate a relation from diagnostics in run A to
|
|
|
|
diagnostics in run B.
|
|
|
|
|
|
|
|
The result is the relation as a list of triples (a, b, confidence) where
|
|
|
|
each element {a,b} is None or an element from the respective run, and
|
|
|
|
confidence is a measure of the match quality (where 0 indicates equality,
|
|
|
|
and None is used if either element is None).
|
|
|
|
"""
|
|
|
|
|
|
|
|
res = []
|
|
|
|
|
|
|
|
# Quickly eliminate equal elements.
|
|
|
|
neqA = []
|
|
|
|
neqB = []
|
|
|
|
eltsA = list(A.diagnostics)
|
|
|
|
eltsB = list(B.diagnostics)
|
2012-07-17 04:21:42 +08:00
|
|
|
eltsA.sort(key = cmpAnalysisDiagnostic)
|
|
|
|
eltsB.sort(key = cmpAnalysisDiagnostic)
|
2009-08-07 05:15:33 +08:00
|
|
|
while eltsA and eltsB:
|
|
|
|
a = eltsA.pop()
|
|
|
|
b = eltsB.pop()
|
2012-07-17 04:21:42 +08:00
|
|
|
if (a.getIssueIdentifier() == b.getIssueIdentifier()) :
|
2009-08-07 05:15:33 +08:00
|
|
|
res.append((a, b, 0))
|
2012-10-18 05:09:26 +08:00
|
|
|
elif a.getIssueIdentifier() > b.getIssueIdentifier():
|
2009-08-07 05:15:33 +08:00
|
|
|
eltsB.append(b)
|
2012-10-18 05:09:26 +08:00
|
|
|
neqA.append(a)
|
2009-08-07 05:15:33 +08:00
|
|
|
else:
|
|
|
|
eltsA.append(a)
|
2012-10-18 05:09:26 +08:00
|
|
|
neqB.append(b)
|
2009-08-07 05:15:33 +08:00
|
|
|
neqA.extend(eltsA)
|
|
|
|
neqB.extend(eltsB)
|
|
|
|
|
|
|
|
# FIXME: Add fuzzy matching. One simple and possible effective idea would be
|
|
|
|
# to bin the diagnostics, print them in a normalized form (based solely on
|
|
|
|
# the structure of the diagnostic), compute the diff, then use that as the
|
|
|
|
# basis for matching. This has the nice property that we don't depend in any
|
|
|
|
# way on the diagnostic format.
|
|
|
|
|
|
|
|
for a in neqA:
|
|
|
|
res.append((a, None, None))
|
|
|
|
for b in neqB:
|
|
|
|
res.append((None, b, None))
|
|
|
|
|
|
|
|
return res
|
|
|
|
|
2012-07-17 04:21:42 +08:00
|
|
|
def dumpScanBuildResultsDiff(dirA, dirB, opts, deleteEmpty=True):
|
2009-08-07 05:15:33 +08:00
|
|
|
# Load the run results.
|
2012-08-02 08:41:40 +08:00
|
|
|
resultsA = loadResults(dirA, opts, opts.rootA, deleteEmpty)
|
|
|
|
resultsB = loadResults(dirB, opts, opts.rootB, deleteEmpty)
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2009-08-07 05:15:33 +08:00
|
|
|
# Open the verbose log, if given.
|
|
|
|
if opts.verboseLog:
|
|
|
|
auxLog = open(opts.verboseLog, "wb")
|
|
|
|
else:
|
|
|
|
auxLog = None
|
|
|
|
|
|
|
|
diff = compareResults(resultsA, resultsB)
|
2011-11-09 03:56:31 +08:00
|
|
|
foundDiffs = 0
|
2009-08-07 05:15:33 +08:00
|
|
|
for res in diff:
|
|
|
|
a,b,confidence = res
|
|
|
|
if a is None:
|
|
|
|
print "ADDED: %r" % b.getReadableName()
|
2011-11-09 03:56:31 +08:00
|
|
|
foundDiffs += 1
|
2009-08-07 05:15:33 +08:00
|
|
|
if auxLog:
|
|
|
|
print >>auxLog, ("('ADDED', %r, %r)" % (b.getReadableName(),
|
2012-07-17 04:21:42 +08:00
|
|
|
b.getReport()))
|
2009-08-07 05:15:33 +08:00
|
|
|
elif b is None:
|
|
|
|
print "REMOVED: %r" % a.getReadableName()
|
2011-11-09 03:56:31 +08:00
|
|
|
foundDiffs += 1
|
2009-08-07 05:15:33 +08:00
|
|
|
if auxLog:
|
|
|
|
print >>auxLog, ("('REMOVED', %r, %r)" % (a.getReadableName(),
|
2012-07-17 04:21:42 +08:00
|
|
|
a.getReport()))
|
2009-08-07 05:15:33 +08:00
|
|
|
elif confidence:
|
|
|
|
print "CHANGED: %r to %r" % (a.getReadableName(),
|
|
|
|
b.getReadableName())
|
2011-11-09 03:56:31 +08:00
|
|
|
foundDiffs += 1
|
2009-08-07 05:15:33 +08:00
|
|
|
if auxLog:
|
2015-09-08 11:50:52 +08:00
|
|
|
print >>auxLog, ("('CHANGED', %r, %r, %r, %r)"
|
2009-08-07 05:15:33 +08:00
|
|
|
% (a.getReadableName(),
|
|
|
|
b.getReadableName(),
|
2012-07-17 04:21:42 +08:00
|
|
|
a.getReport(),
|
|
|
|
b.getReport()))
|
2009-08-07 05:15:33 +08:00
|
|
|
else:
|
|
|
|
pass
|
|
|
|
|
2011-11-09 03:56:31 +08:00
|
|
|
TotalReports = len(resultsB.diagnostics)
|
|
|
|
print "TOTAL REPORTS: %r" % TotalReports
|
|
|
|
print "TOTAL DIFFERENCES: %r" % foundDiffs
|
2009-08-07 05:15:33 +08:00
|
|
|
if auxLog:
|
2011-11-09 03:56:31 +08:00
|
|
|
print >>auxLog, "('TOTAL NEW REPORTS', %r)" % TotalReports
|
|
|
|
print >>auxLog, "('TOTAL DIFFERENCES', %r)" % foundDiffs
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2015-06-30 23:31:17 +08:00
|
|
|
return foundDiffs, len(resultsA.diagnostics), len(resultsB.diagnostics)
|
2011-09-13 05:32:41 +08:00
|
|
|
|
|
|
|
def main():
|
|
|
|
from optparse import OptionParser
|
|
|
|
parser = OptionParser("usage: %prog [options] [dir A] [dir B]")
|
2012-08-02 08:41:40 +08:00
|
|
|
parser.add_option("", "--rootA", dest="rootA",
|
|
|
|
help="Prefix to ignore on source files for directory A",
|
|
|
|
action="store", type=str, default="")
|
|
|
|
parser.add_option("", "--rootB", dest="rootB",
|
|
|
|
help="Prefix to ignore on source files for directory B",
|
2011-09-13 05:32:41 +08:00
|
|
|
action="store", type=str, default="")
|
|
|
|
parser.add_option("", "--verbose-log", dest="verboseLog",
|
|
|
|
help="Write additional information to LOG [default=None]",
|
|
|
|
action="store", type=str, default=None,
|
|
|
|
metavar="LOG")
|
|
|
|
(opts, args) = parser.parse_args()
|
|
|
|
|
|
|
|
if len(args) != 2:
|
|
|
|
parser.error("invalid number of arguments")
|
|
|
|
|
|
|
|
dirA,dirB = args
|
|
|
|
|
2015-09-08 11:50:52 +08:00
|
|
|
dumpScanBuildResultsDiff(dirA, dirB, opts)
|
2009-08-07 05:15:33 +08:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|