Skip to content

Commit

Permalink
feat(HIS): add function calls metric check to script
Browse files Browse the repository at this point in the history
Signed-off-by: Afonso Santos <[email protected]>
  • Loading branch information
AfonsoSantos96 committed Oct 23, 2023
1 parent b95729d commit d4d12b3
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
1 change: 1 addition & 0 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ RUN apt-get update && apt-get install -y \
nodejs \
npm \
pmccabe \
cflow \
enchant-2 && \
pip3 install gitlint && \
pip3 install license-expression && \
Expand Down
46 changes: 46 additions & 0 deletions his_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
import sys
import argparse
import os
import re

CYCLOMATIC_THRESHOLD = 10
CALL_THRESHOLD = 7

def process_complexity(files):

Expand All @@ -31,13 +33,57 @@ def process_complexity(files):
print("== Check done with " + str(metric_fail) + " error(s) ==\n")
return metric_fail

def process_calls(files):

"""Process the number of calls inside a function."""

metric_fail = 0
cflow = "cflow -l --depth=2"
print("== Checking the number of called functions ==")
for file in files.files:
if file.endswith(".c"):
lines = os.popen(f"{cflow} {file}").read().split('\n')
call_counter = 0
for line in lines:
if not line:
continue
depth = int(line.split('}')[0].strip('{ ').strip())
if depth == 0:
if call_counter > CALL_THRESHOLD:
print(f"{function_info} calls {call_counter} functions."
f" The maximum are {CALL_THRESHOLD} calls")
metric_fail += 1
match = re.match(
r'\{\s*(\d+)\}\s+(.*?)\(\) <.*?at (.*?):(\d+)>', line)
if match:
depth, function_name, file_path, line_number = \
match.groups()
function_info = (
f"At {file_path.split('src', 1)[1]} ({line_number})"
f": {function_name}")
call_counter = 0
elif depth >= 1:
call_counter += 1
if call_counter > CALL_THRESHOLD:
print(f"{function_info} calls {call_counter} functions. "
f"The maximum are {CALL_THRESHOLD} calls")
metric_fail += 1
print(f"== Check done with {metric_fail} error(s) ==\n")
return metric_fail

if __name__ == "__main__":
CHECK_FAIL = 0
PARSER = argparse.ArgumentParser()
PARSER.add_argument("files", nargs="+", help="The files to process")
ARGS = PARSER.parse_args()

NULL_FD = os.open('/dev/null', os.O_WRONLY)
os.dup2(NULL_FD, 2) # Redirect tools' stderr messages

CHECK_FAIL += process_complexity(ARGS)
CHECK_FAIL += process_calls(ARGS)

os.close(NULL_FD)

if CHECK_FAIL:
sys.exit(-1)

0 comments on commit d4d12b3

Please sign in to comment.