3 # Implementation of various source code metrics.
4 # These are currently ad-hoc string operations and regexps.
5 # We might want to use a proper static analysis library in the future, if we want to get more advanced metrics.
7 # Future imports for Python 2.7, mandatory in 3.0
8 from __future__
import division
9 from __future__
import print_function
10 from __future__
import unicode_literals
15 """Get file length of file"""
17 for i
, l
in enumerate(f
):
21 def get_include_count(f
):
22 """Get number of #include statements in the file"""
25 if re
.match(r
'\s*#\s*include', line
):
29 def get_function_lines(f
):
31 Return iterator which iterates over functions and returns (function name, function lines)
34 # Skip lines that look like they are defining functions with these
35 # names: they aren't real function definitions.
36 REGEXP_CONFUSE_TERMS
= {"MOCK_IMPL", "MOCK_DECL", "HANDLE_DECL",
37 "ENABLE_GCC_WARNINGS", "ENABLE_GCC_WARNING",
38 "DUMMY_TYPECHECK_INSTANCE",
39 "DISABLE_GCC_WARNING", "DISABLE_GCC_WARNINGS"}
42 found_openbrace
= False
43 for lineno
, line
in enumerate(f
):
45 # find the start of a function
46 m
= re
.match(r
'^([a-zA-Z_][a-zA-Z_0-9]*),?\(', line
)
48 func_name
= m
.group(1)
49 if func_name
in REGEXP_CONFUSE_TERMS
:
53 elif not found_openbrace
and line
.startswith("{"):
54 found_openbrace
= True
57 # Find the end of a function
58 if line
.startswith("}"):
59 n_lines
= lineno
- func_start
+ 1
61 found_openbrace
= False
62 yield (func_name
, n_lines
)