2019-02-27 14:14:19 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
2019-02-27 18:29:27 +01:00
|
|
|
# Implementation of various source code metrics.
|
|
|
|
# These are currently ad-hoc string operations and regexps.
|
|
|
|
# We might want to use a proper static analysis library in the future, if we want to get more advanced metrics.
|
|
|
|
|
2019-02-27 14:14:19 +01:00
|
|
|
import re
|
|
|
|
|
2019-02-27 18:29:27 +01:00
|
|
|
def get_file_len(f):
|
2019-02-27 14:14:19 +01:00
|
|
|
"""Get file length of file"""
|
2019-09-02 21:33:59 +02:00
|
|
|
i = -1
|
2019-02-27 14:14:19 +01:00
|
|
|
for i, l in enumerate(f):
|
|
|
|
pass
|
|
|
|
return i + 1
|
|
|
|
|
2019-02-27 16:05:00 +01:00
|
|
|
def get_include_count(f):
|
|
|
|
"""Get number of #include statements in the file"""
|
|
|
|
include_count = 0
|
|
|
|
for line in f:
|
2019-07-17 14:02:17 +02:00
|
|
|
if re.match(r'\s*#\s*include', line):
|
2019-02-27 16:05:00 +01:00
|
|
|
include_count += 1
|
|
|
|
return include_count
|
|
|
|
|
2019-02-27 18:29:27 +01:00
|
|
|
def get_function_lines(f):
|
2019-02-27 14:14:19 +01:00
|
|
|
"""
|
|
|
|
Return iterator which iterates over functions and returns (function name, function lines)
|
|
|
|
"""
|
|
|
|
|
2019-03-12 14:22:53 +01:00
|
|
|
# Skip lines that look like they are defining functions with these
|
|
|
|
# names: they aren't real function definitions.
|
2019-08-05 16:31:02 +02:00
|
|
|
REGEXP_CONFUSE_TERMS = {"MOCK_IMPL", "MOCK_DECL", "HANDLE_DECL",
|
|
|
|
"ENABLE_GCC_WARNINGS", "ENABLE_GCC_WARNING",
|
|
|
|
"DUMMY_TYPECHECK_INSTANCE",
|
2019-03-12 13:35:26 +01:00
|
|
|
"DISABLE_GCC_WARNING", "DISABLE_GCC_WARNINGS"}
|
2019-02-27 18:29:27 +01:00
|
|
|
|
2019-02-27 14:14:19 +01:00
|
|
|
in_function = False
|
2019-07-17 14:00:28 +02:00
|
|
|
found_openbrace = False
|
2019-02-27 14:14:19 +01:00
|
|
|
for lineno, line in enumerate(f):
|
|
|
|
if not in_function:
|
|
|
|
# find the start of a function
|
|
|
|
m = re.match(r'^([a-zA-Z_][a-zA-Z_0-9]*),?\(', line)
|
|
|
|
if m:
|
|
|
|
func_name = m.group(1)
|
2019-03-12 14:22:53 +01:00
|
|
|
if func_name in REGEXP_CONFUSE_TERMS:
|
|
|
|
continue
|
2019-02-27 14:14:19 +01:00
|
|
|
func_start = lineno
|
|
|
|
in_function = True
|
2019-07-17 14:00:28 +02:00
|
|
|
elif not found_openbrace and line.startswith("{"):
|
|
|
|
found_openbrace = True
|
|
|
|
func_start = lineno
|
2019-02-27 14:14:19 +01:00
|
|
|
else:
|
2019-03-12 13:35:26 +01:00
|
|
|
# Find the end of a function
|
2019-02-27 14:14:19 +01:00
|
|
|
if line.startswith("}"):
|
2019-07-17 14:00:28 +02:00
|
|
|
n_lines = lineno - func_start + 1
|
2019-02-27 14:14:19 +01:00
|
|
|
in_function = False
|
2019-07-17 14:00:28 +02:00
|
|
|
found_openbrace = False
|
2019-02-27 14:14:19 +01:00
|
|
|
yield (func_name, n_lines)
|