From 4b66c696e30ec9080d87cb9be607609c0c09ad2c Mon Sep 17 00:00:00 2001 From: AfonsoSantos96 Date: Wed, 22 Nov 2023 09:42:52 +0000 Subject: [PATCH] feat(HIS): add goto statement checker Signed-off-by: Afonso Santos --- his_checker.py | 35 ++++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/his_checker.py b/his_checker.py index ecd4eca..0a07393 100755 --- a/his_checker.py +++ b/his_checker.py @@ -9,6 +9,7 @@ import sys import argparse +import os def process_calling(files, threshold): """ @@ -39,12 +40,40 @@ def process_comf(files, threshold): def process_goto(files, threshold): """ - Process the goto metric + Process number of 'goto' statements. This function checks each function in the provided files + for 'goto' statements. If the number of 'goto' statements in a function exceeds the defined + threshold, an error message is printed and the error count is incremented. + + Args: + files: A list of file paths to check for 'goto' statements. + threshold: The maximum number of 'goto' statements allowed in a function. + + Returns: + The number of files that exceed the 'goto' statement threshold. """ - print(f"Processing GOTO metric with threshold {threshold} for files: {', '.join(files)}") + metric_fail = 0 + qmcalc_goto_index = 18 + goto_tool = "qmcalc " + + print("--------------------------------------------") + print(f"Processing GOTO metric with threshold >{threshold}") + print("--------------------------------------------") + + # Process each file + for file in files: + # Run 'qmcalc' on the file and split the output into lines + sline = os.popen(goto_tool + str(file)).read().split('\n') + + for fields in [line.split('\t') for line in sline[:-1]]: + if int(fields[qmcalc_goto_index]) > threshold: + print("At " + file + " there is " + fields[qmcalc_goto_index] + " goto statements") + metric_fail += 1 + + print("GOTO metric finish with " + str(metric_fail) + " error(s)\n") + + return metric_fail - return 0 def process_level(files, threshold): """