From 717daf451de1bcd3416de69cb487ac2a73539583 Mon Sep 17 00:00:00 2001 From: erwann lesech Date: Sat, 13 Jan 2024 10:44:32 +0100 Subject: [PATCH] feat: add clang_tidy to CI This commit should add the clang_tidy rule into the CI. Plus it renames a function in io_backend to io_backen_manager so it is compliant to the clang_tidy script. --- .github/workflows/ci.yml | 22 +++++++++++++++++ .github/workflows/clang_tidy.sh | 44 +++++++++++++++++++++++++++++++++ src/io_backend/io_backend.c | 2 +- src/io_backend/io_backend.h | 2 +- src/main.c | 2 +- 5 files changed, 69 insertions(+), 3 deletions(-) create mode 100755 .github/workflows/clang_tidy.sh diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index fd108984..07dcdf00 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -54,6 +54,28 @@ jobs: echo "Code is not clang-format compliant. Please format your code." exit 1 fi + + clang-tidy: + + runs-on: ubuntu-latest + + steps: + - name: Checkout repository + uses: actions/checkout@v2 + + - name: create clang-tidy output file and witness file + run: touch clang_tidy_output.txt clang_tidy_witness.txt + + - name: Run clang-tidy script + run: ./github/workflows/clang_tidy.sh > clang_tidy_output.txt + + - name: Check for changes + run: | + diff clang_tidy_output.txt clang_tidy_witness.txt + if [ $? -eq 1 ]; then + echo "Code is not clang-tidy compliant. Please fix your code." + exit 1 + fi doxygen: diff --git a/.github/workflows/clang_tidy.sh b/.github/workflows/clang_tidy.sh new file mode 100755 index 00000000..cebb7456 --- /dev/null +++ b/.github/workflows/clang_tidy.sh @@ -0,0 +1,44 @@ +#!/bin/bash + +root_dir=$(git rev-parse --show-toplevel) + +for file in $(find "$root_dir/src" -type f -name '*.c'); do + function_count=$(grep -E '^(bool|int|char|double|void|float|struct [a-zA-Z][a-zA-Z_]*|unsigned|long)[[:space:]]+[*]*[a-zA-Z_][a-zA-Z0-9_]*[[:space:]]*\([^)]*\)[[:space:]]*' "$file" | wc -l) + # echo "Processing file: $file" + + # Extract function prototypes and count parameters and lines + grep -E '^(bool|int|char|double|void|float|struct [a-zA-Z][a-zA-Z_]*|unsigned|long)[[:space:]]+[*]*[a-zA-Z_][a-zA-Z0-9_]*[[:space:]]*\([^)]*\)[[:space:]]*' "$file" | while IFS= read -r line; do + return_type=$(echo "$line" | awk '{print $1}') + function_name=$(echo "$line" | awk '{print $2}' | sed 's/([^)]*//g') + + # If the type of the function is a struct, then we need to remove the 'struct' keyword + if [[ "$return_type" == "struct" ]]; then + return_type=$(echo "$line" | awk '{print $1 " " $2}') + function_name=$(echo "$line" | awk '{print $3}' | sed 's/([^)]*)//g') + fi + parameters=$(echo "$line" | sed 's/^[^(]*(//;s/)[^{]*$//;s/,/\n/g' | wc -l) + + # Count the number of lines in the function (excluding blank and '{', '}' lines) + lines_in_function=$(sed -n "/$function_name/,/^}/p" "$file" | sed '/^$/d' | sed '/^[[:space:]]*{$/d' | sed '/^[[:space:]]*}$/d' | wc -l) + lines_in_function=$((lines_in_function-1)) + + if [[ "$parameters" -gt 4 ]]; then + echo "Too many parameters in function: $function_name" + fi + + if [[ "$lines_in_function" -gt 40 ]]; then + echo "Too many lines in function: $function_name" + fi + + # echo "Function: $function_name" + # echo "Return type: $return_type" + # echo "Parameters: $parameters" + # echo "Lines in function: $lines_in_function" + # echo "---------------------" + + done + # echo "Total functions: $function_count" + if [[ "$function_count" -gt 10 ]]; then + echo "Too many functions in file: $file" + fi +done \ No newline at end of file diff --git a/src/io_backend/io_backend.c b/src/io_backend/io_backend.c index 57b2b331..54b73b33 100644 --- a/src/io_backend/io_backend.c +++ b/src/io_backend/io_backend.c @@ -102,7 +102,7 @@ char *io_backend_file(char **argv) return res; } -char *io_backend(int argc, char **argv) +char *io_backend_manager(int argc, char **argv) { if (argc < 2) { diff --git a/src/io_backend/io_backend.h b/src/io_backend/io_backend.h index 679d5094..8ae999aa 100644 --- a/src/io_backend/io_backend.h +++ b/src/io_backend/io_backend.h @@ -21,6 +21,6 @@ * \param argv Arguments * \return the input as a string */ -char *io_backend(int argc, char **argv); +char *io_backend_manager(int argc, char **argv); #endif /* ! IO_BACKEND_H */ diff --git a/src/main.c b/src/main.c index 249f505b..dd3dd407 100644 --- a/src/main.c +++ b/src/main.c @@ -28,7 +28,7 @@ int main(int argc, char **argv) bool logger_enabled = check_logger(&argc, argv); bool pretty_print_enabled = check_pretty_print(&argc, argv); - char *input = io_backend(argc, argv); + char *input = io_backend_manager(argc, argv); if (input == NULL) { errx(127, "Error while reading input");