From 38057224ce89dfd9a961752f05b4edd7cf4df33b 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 --- docker/Dockerfile | 3 +++ his_checker.py | 40 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/docker/Dockerfile b/docker/Dockerfile index 530ebf4..c859d8a 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -10,6 +10,8 @@ ARG CLANG_VERSION=14 # use this repo temporarily while the patches for misra fps are not in a new official version ARG CPPCHECK_REPO=https://github.com/danmar/cppcheck.git ARG CPPCHECK_VERSION=2.9 +# repo for qmcalc tool +ARG QMCALC_REPO=https://github.com/dspinellis/cqmetrics.git # install dependencies RUN apt-get update && apt-get install -y \ @@ -40,6 +42,7 @@ RUN apt-get update && apt-get install -y \ pip3 install doorstop && \ pip3 install pyspellchecker && \ npm install -g cspell@latest && \ + mkdir /opt/qmcalc && git clone $QMCALC_REPO --depth 1 /opt/qmcalc && make -C /opt/qmcalc/src && \ mkdir /opt/cppcheck && git clone $CPPCHECK_REPO --depth 1 --branch $CPPCHECK_VERSION /opt/cppcheck && make -C /opt/cppcheck FILESDIR=/usr/share/cppcheck && make -C /opt/cppcheck install FILESDIR=/usr/share/cppcheck && \ mkdir /opt/aarch64-toolchain && curl $AARCH64_TOOLCHAIN_LINK | tar xJ -C /opt/aarch64-toolchain --strip-components=1 && \ mkdir /opt/riscv-toolchain && curl $RISCV_TOOLCHAIN_LINK | tar xz -C /opt/riscv-toolchain --strip-components=1 && \ diff --git a/his_checker.py b/his_checker.py index 2ec03e9..e3a095b 100755 --- a/his_checker.py +++ b/his_checker.py @@ -9,9 +9,47 @@ import sys import argparse +import os + +# Thresholds are derived from the HIS metrics specification (v1.3.1) +GOTO_THRESHOLD = 0 + +def process_goto(files): + + """ + 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. + + Returns: + The number of functions that exceed the 'goto' statement threshold. + """ + + metric_fail = 0 + qmcalc_goto_index = 18 + goto_tool = "qmcalc " + + print("Checking the number of goto statements in each function (Limit: " + str(GOTO_THRESHOLD) + ")") + + # Process each file + for file in files.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]) > GOTO_THRESHOLD: + print("At " + file + " there is " + fields[qmcalc_goto_index] + " goto statements") + metric_fail += 1 + + print("Check done with " + str(metric_fail) + " error(s)\n") + + return metric_fail if __name__ == "__main__": - METRICS_LIST = [] + METRICS_LIST = [process_goto] CHECK_FAIL = 0 PARSER = argparse.ArgumentParser() PARSER.add_argument("files", nargs="+", help="The files to process")