2019-02-27 17:24:10 +01:00
|
|
|
"""
|
|
|
|
In this file we define a ProblemVault class where we store all the
|
|
|
|
exceptions and all the problems we find with the code.
|
|
|
|
|
|
|
|
The ProblemVault is capable of registering problems and also figuring out if a
|
|
|
|
problem is worse than a registered exception so that it only warns when things
|
|
|
|
get worse.
|
|
|
|
"""
|
|
|
|
|
2019-03-14 01:15:32 +01:00
|
|
|
from __future__ import print_function
|
|
|
|
|
2019-03-05 14:03:27 +01:00
|
|
|
import os.path
|
2019-03-25 14:28:24 +01:00
|
|
|
import re
|
2019-03-12 13:35:26 +01:00
|
|
|
import sys
|
2019-03-05 14:03:27 +01:00
|
|
|
|
2019-07-30 16:15:11 +02:00
|
|
|
STATUS_ERR = 2
|
|
|
|
STATUS_WARN = 1
|
|
|
|
STATUS_OK = 0
|
|
|
|
|
2019-02-27 17:24:10 +01:00
|
|
|
class ProblemVault(object):
|
|
|
|
"""
|
|
|
|
Singleton where we store the various new problems we
|
|
|
|
found in the code, and also the old problems we read from the exception
|
|
|
|
file.
|
|
|
|
"""
|
2019-03-25 21:06:26 +01:00
|
|
|
def __init__(self, exception_fname=None):
|
2019-02-27 17:24:10 +01:00
|
|
|
# Exception dictionary: { problem.key() : Problem object }
|
|
|
|
self.exceptions = {}
|
2019-07-17 15:06:34 +02:00
|
|
|
# Exception dictionary: maps key to the problem it was used to
|
|
|
|
# suppress.
|
|
|
|
self.used_exception_for = {}
|
2019-02-27 17:24:10 +01:00
|
|
|
|
2019-03-25 21:06:26 +01:00
|
|
|
if exception_fname == None:
|
|
|
|
return
|
|
|
|
|
2019-02-27 18:30:39 +01:00
|
|
|
try:
|
|
|
|
with open(exception_fname, 'r') as exception_f:
|
|
|
|
self.register_exceptions(exception_f)
|
|
|
|
except IOError:
|
2019-03-12 13:35:26 +01:00
|
|
|
print("No exception file provided", file=sys.stderr)
|
2019-02-27 17:24:10 +01:00
|
|
|
|
|
|
|
def register_exceptions(self, exception_file):
|
|
|
|
# Register exceptions
|
2019-03-25 14:28:24 +01:00
|
|
|
for lineno, line in enumerate(exception_file, 1):
|
|
|
|
try:
|
|
|
|
problem = get_old_problem_from_exception_str(line)
|
|
|
|
except ValueError as v:
|
|
|
|
print("Exception file line {} not recognized: {}"
|
|
|
|
.format(lineno,v),
|
|
|
|
file=sys.stderr)
|
|
|
|
continue
|
|
|
|
|
2019-02-27 17:24:10 +01:00
|
|
|
if problem is None:
|
|
|
|
continue
|
|
|
|
|
2019-03-13 11:59:44 +01:00
|
|
|
# Fail if we see dup exceptions. There is really no reason to have dup exceptions.
|
|
|
|
if problem.key() in self.exceptions:
|
|
|
|
print("Duplicate exceptions lines found in exception file:\n\t{}\n\t{}\nAborting...".format(problem, self.exceptions[problem.key()]),
|
|
|
|
file=sys.stderr)
|
|
|
|
sys.exit(1)
|
|
|
|
|
2019-02-27 17:24:10 +01:00
|
|
|
self.exceptions[problem.key()] = problem
|
|
|
|
#print "Registering exception: %s" % problem
|
|
|
|
|
|
|
|
def register_problem(self, problem):
|
2019-02-27 18:30:39 +01:00
|
|
|
"""
|
2019-07-30 16:15:11 +02:00
|
|
|
Register this problem to the problem value. Return true if it was a new
|
|
|
|
problem or it worsens an already existing problem. A true
|
|
|
|
value may be STATUS_ERR to indicate a hard violation, or STATUS_WARN
|
|
|
|
to indicate a warning.
|
2019-02-27 18:30:39 +01:00
|
|
|
"""
|
2019-02-27 17:24:10 +01:00
|
|
|
# This is a new problem, print it
|
|
|
|
if problem.key() not in self.exceptions:
|
2019-07-30 16:15:11 +02:00
|
|
|
return STATUS_ERR
|
2019-02-27 17:24:10 +01:00
|
|
|
|
|
|
|
# If it's an old problem, we don't warn if the situation got better
|
|
|
|
# (e.g. we went from 4k LoC to 3k LoC), but we do warn if the
|
|
|
|
# situation worsened (e.g. we went from 60 includes to 80).
|
2019-07-30 16:15:11 +02:00
|
|
|
status = problem.is_worse_than(self.exceptions[problem.key()])
|
2019-09-18 14:52:01 +02:00
|
|
|
|
|
|
|
# Remember that we used this exception, so that we can later
|
|
|
|
# determine whether the exception was overbroad.
|
|
|
|
self.used_exception_for[problem.key()] = problem
|
2019-02-27 17:24:10 +01:00
|
|
|
|
2019-07-30 16:15:11 +02:00
|
|
|
return status
|
2019-02-27 17:24:10 +01:00
|
|
|
|
2019-08-01 15:35:33 +02:00
|
|
|
def list_overbroad_exceptions(self):
|
2019-07-17 15:06:34 +02:00
|
|
|
"""Return an iterator of tuples containing (ex,prob) where ex is an
|
|
|
|
exceptions in this vault that are stricter than it needs to be, and
|
|
|
|
prob is the worst problem (if any) that it covered.
|
|
|
|
"""
|
|
|
|
for k in self.exceptions:
|
|
|
|
e = self.exceptions[k]
|
|
|
|
p = self.used_exception_for.get(k)
|
|
|
|
if p is None or e.is_worse_than(p):
|
|
|
|
yield (e, p)
|
|
|
|
|
2019-07-17 15:20:58 +02:00
|
|
|
def set_tolerances(self, fns):
|
|
|
|
"""Adjust the tolerances for the exceptions in this vault. Takes
|
|
|
|
a map of problem type to a function that adjusts the permitted
|
|
|
|
function to its new maximum value."""
|
|
|
|
for k in self.exceptions:
|
|
|
|
ex = self.exceptions[k]
|
|
|
|
fn = fns.get(ex.problem_type)
|
|
|
|
if fn is not None:
|
|
|
|
ex.metric_value = fn(ex.metric_value)
|
2019-07-17 15:06:34 +02:00
|
|
|
|
2019-07-30 15:20:08 +02:00
|
|
|
class ProblemFilter(object):
|
|
|
|
def __init__(self):
|
|
|
|
self.thresholds = dict()
|
|
|
|
|
|
|
|
def addThreshold(self, item):
|
2019-08-05 16:31:02 +02:00
|
|
|
self.thresholds[(item.get_type(),item.get_file_type())] = item
|
2019-07-30 15:20:08 +02:00
|
|
|
|
|
|
|
def matches(self, item):
|
2019-08-05 16:31:02 +02:00
|
|
|
key = (item.get_type(), item.get_file_type())
|
|
|
|
filt = self.thresholds.get(key, None)
|
2019-07-30 15:20:08 +02:00
|
|
|
if filt is None:
|
|
|
|
return False
|
|
|
|
return item.is_worse_than(filt)
|
|
|
|
|
|
|
|
def filter(self, sequence):
|
|
|
|
for item in iter(sequence):
|
|
|
|
if self.matches(item):
|
|
|
|
yield item
|
|
|
|
|
2019-07-30 15:03:55 +02:00
|
|
|
class Item(object):
|
2019-03-05 14:01:07 +01:00
|
|
|
"""
|
2019-07-30 15:03:55 +02:00
|
|
|
A generic measurement about some aspect of our source code. See
|
|
|
|
the subclasses below for the specific problems we are trying to tackle.
|
2019-03-05 14:01:07 +01:00
|
|
|
"""
|
2019-02-27 17:24:10 +01:00
|
|
|
def __init__(self, problem_type, problem_location, metric_value):
|
|
|
|
self.problem_location = problem_location
|
|
|
|
self.metric_value = int(metric_value)
|
2019-07-17 15:20:58 +02:00
|
|
|
self.warning_threshold = self.metric_value
|
2019-02-27 17:24:10 +01:00
|
|
|
self.problem_type = problem_type
|
|
|
|
|
|
|
|
def is_worse_than(self, other_problem):
|
2019-07-30 16:15:11 +02:00
|
|
|
"""Return STATUS_ERR if this is a worse problem than other_problem.
|
|
|
|
Return STATUS_WARN if it is a little worse, but falls within the
|
|
|
|
warning threshold. Return STATUS_OK if this problem is not
|
|
|
|
at all worse than other_problem.
|
|
|
|
"""
|
2019-02-27 17:24:10 +01:00
|
|
|
if self.metric_value > other_problem.metric_value:
|
2019-07-30 16:15:11 +02:00
|
|
|
return STATUS_ERR
|
2019-07-17 15:20:58 +02:00
|
|
|
elif self.metric_value > other_problem.warning_threshold:
|
2019-07-30 16:15:11 +02:00
|
|
|
return STATUS_WARN
|
|
|
|
else:
|
|
|
|
return STATUS_OK
|
2019-02-27 17:24:10 +01:00
|
|
|
|
|
|
|
def key(self):
|
|
|
|
"""Generate a unique key that describes this problem that can be used as a dictionary key"""
|
2019-07-30 15:03:55 +02:00
|
|
|
# Item location is a filesystem path, so we need to normalize this
|
2019-03-05 14:03:27 +01:00
|
|
|
# across platforms otherwise same paths are not gonna match.
|
|
|
|
canonical_location = os.path.normcase(self.problem_location)
|
|
|
|
return "%s:%s" % (canonical_location, self.problem_type)
|
2019-02-27 17:24:10 +01:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return "problem %s %s %s" % (self.problem_type, self.problem_location, self.metric_value)
|
|
|
|
|
2019-07-30 15:20:08 +02:00
|
|
|
def get_type(self):
|
|
|
|
return self.problem_type
|
|
|
|
|
2019-08-05 16:31:02 +02:00
|
|
|
def get_file_type(self):
|
|
|
|
if self.problem_location.endswith(".h"):
|
|
|
|
return "*.h"
|
|
|
|
else:
|
|
|
|
return "*.c"
|
|
|
|
|
2019-07-30 15:03:55 +02:00
|
|
|
class FileSizeItem(Item):
|
2019-03-05 14:01:07 +01:00
|
|
|
"""
|
|
|
|
Denotes a problem with the size of a .c file.
|
|
|
|
|
|
|
|
The 'problem_location' is the filesystem path of the .c file, and the
|
|
|
|
'metric_value' is the number of lines in the .c file.
|
|
|
|
"""
|
2019-02-27 17:24:10 +01:00
|
|
|
def __init__(self, problem_location, metric_value):
|
2019-07-30 15:03:55 +02:00
|
|
|
super(FileSizeItem, self).__init__("file-size", problem_location, metric_value)
|
2019-02-27 17:24:10 +01:00
|
|
|
|
2019-07-30 15:03:55 +02:00
|
|
|
class IncludeCountItem(Item):
|
2019-03-05 14:01:07 +01:00
|
|
|
"""
|
|
|
|
Denotes a problem with the number of #includes in a .c file.
|
|
|
|
|
|
|
|
The 'problem_location' is the filesystem path of the .c file, and the
|
|
|
|
'metric_value' is the number of #includes in the .c file.
|
|
|
|
"""
|
2019-02-27 17:24:10 +01:00
|
|
|
def __init__(self, problem_location, metric_value):
|
2019-07-30 15:03:55 +02:00
|
|
|
super(IncludeCountItem, self).__init__("include-count", problem_location, metric_value)
|
2019-02-27 17:24:10 +01:00
|
|
|
|
2019-07-30 15:03:55 +02:00
|
|
|
class FunctionSizeItem(Item):
|
2019-03-05 14:01:07 +01:00
|
|
|
"""
|
|
|
|
Denotes a problem with a size of a function in a .c file.
|
|
|
|
|
|
|
|
The 'problem_location' is "<path>:<function>()" where <path> is the
|
|
|
|
filesystem path of the .c file and <function> is the name of the offending
|
|
|
|
function.
|
|
|
|
|
|
|
|
The 'metric_value' is the size of the offending function in lines.
|
|
|
|
"""
|
2019-02-27 17:24:10 +01:00
|
|
|
def __init__(self, problem_location, metric_value):
|
2019-07-30 15:03:55 +02:00
|
|
|
super(FunctionSizeItem, self).__init__("function-size", problem_location, metric_value)
|
2019-02-27 17:24:10 +01:00
|
|
|
|
2019-08-05 23:17:50 +02:00
|
|
|
class DependencyViolationItem(Item):
|
|
|
|
"""
|
|
|
|
Denotes a dependency violation in a .c or .h file. A dependency violation
|
|
|
|
occurs when a file includes a file from some module that is not listed
|
|
|
|
in its .may_include file.
|
|
|
|
|
|
|
|
The 'problem_location' is the file that contains the problem.
|
|
|
|
|
|
|
|
The 'metric_value' is the number of forbidden includes.
|
|
|
|
"""
|
|
|
|
def __init__(self, problem_location, metric_value):
|
|
|
|
super(DependencyViolationItem, self).__init__("dependency-violation",
|
|
|
|
problem_location,
|
|
|
|
metric_value)
|
|
|
|
|
2019-03-25 14:28:24 +01:00
|
|
|
comment_re = re.compile(r'#.*$')
|
|
|
|
|
2019-02-27 17:24:10 +01:00
|
|
|
def get_old_problem_from_exception_str(exception_str):
|
2019-03-25 14:28:24 +01:00
|
|
|
orig_str = exception_str
|
|
|
|
exception_str = comment_re.sub("", exception_str)
|
|
|
|
fields = exception_str.split()
|
|
|
|
if len(fields) == 0:
|
|
|
|
# empty line or comment
|
2019-02-27 17:24:10 +01:00
|
|
|
return None
|
2019-03-25 14:28:24 +01:00
|
|
|
elif len(fields) == 4:
|
|
|
|
# valid line
|
|
|
|
_, problem_type, problem_location, metric_value = fields
|
|
|
|
else:
|
|
|
|
raise ValueError("Misformatted line {!r}".format(orig_str))
|
2019-02-27 17:24:10 +01:00
|
|
|
|
|
|
|
if problem_type == "file-size":
|
2019-07-30 15:03:55 +02:00
|
|
|
return FileSizeItem(problem_location, metric_value)
|
2019-02-27 17:24:10 +01:00
|
|
|
elif problem_type == "include-count":
|
2019-07-30 15:03:55 +02:00
|
|
|
return IncludeCountItem(problem_location, metric_value)
|
2019-02-27 17:24:10 +01:00
|
|
|
elif problem_type == "function-size":
|
2019-07-30 15:03:55 +02:00
|
|
|
return FunctionSizeItem(problem_location, metric_value)
|
2019-08-05 23:17:50 +02:00
|
|
|
elif problem_type == "dependency-violation":
|
|
|
|
return DependencyViolationItem(problem_location, metric_value)
|
2019-02-27 17:24:10 +01:00
|
|
|
else:
|
2019-03-25 14:28:24 +01:00
|
|
|
raise ValueError("Unknown exception type {!r}".format(orig_str))
|