Skip to content

Commit

Permalink
feat(HIS): add goto statement checker
Browse files Browse the repository at this point in the history
Signed-off-by: Afonso Santos <[email protected]>
  • Loading branch information
AfonsoSantos96 authored and danielRep committed Feb 16, 2024
1 parent 2c77837 commit 8cee013
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
3 changes: 3 additions & 0 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down Expand Up @@ -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 && \
Expand Down
40 changes: 39 additions & 1 deletion his_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 per file")

# 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")
Expand Down

0 comments on commit 8cee013

Please sign in to comment.