From 717daf451de1bcd3416de69cb487ac2a73539583 Mon Sep 17 00:00:00 2001 From: erwann lesech Date: Sat, 13 Jan 2024 10:44:32 +0100 Subject: [PATCH 1/4] 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"); From e203991deaaf4684074cc18c30af8f8a44dc7432 Mon Sep 17 00:00:00 2001 From: erwann lesech Date: Sat, 13 Jan 2024 10:46:57 +0100 Subject: [PATCH 2/4] fix: fix the path of the script --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 07dcdf00..67a95636 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -67,7 +67,7 @@ jobs: 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 + run: ./.github/workflows/clang_tidy.sh > clang_tidy_output.txt - name: Check for changes run: | From 5dacb9f69d733bb9986fa3a702b6b7945be6915c Mon Sep 17 00:00:00 2001 From: erwann lesech Date: Sat, 13 Jan 2024 10:49:28 +0100 Subject: [PATCH 3/4] fix: fix function name in tests --- src/io_backend/tests/io_backend_tests.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/io_backend/tests/io_backend_tests.c b/src/io_backend/tests/io_backend_tests.c index b03b42d0..24630d53 100644 --- a/src/io_backend/tests/io_backend_tests.c +++ b/src/io_backend/tests/io_backend_tests.c @@ -17,7 +17,7 @@ TestSuite(io_backend, .timeout = 1); Test(io_backend, io_backend_direct) { char *argv[] = { "./42sh", "-c", "echo test" }; - char *input = io_backend(3, argv); + char *input = io_backend_manager(3, argv); cr_assert_str_eq(input, "echo test", "wrong input: %s", input); free(input); @@ -26,7 +26,7 @@ Test(io_backend, io_backend_direct) Test(io_backend, io_backend_direct_multiple) { char *argv[] = { "./42sh", "-c", "echo test ; echo test2" }; - char *input = io_backend(3, argv); + char *input = io_backend_manager(3, argv); cr_assert_str_eq(input, "echo test ; echo test2", "wrong input: %s", input); free(input); @@ -35,7 +35,7 @@ Test(io_backend, io_backend_direct_multiple) Test(io_backend, io_backend_direct_backslash) { char *argv[] = { "./42sh", "-c", "echo test \\" }; - char *input = io_backend(3, argv); + char *input = io_backend_manager(3, argv); cr_assert_str_eq(input, "echo test \\", "wrong input: %s", input); free(input); @@ -44,7 +44,7 @@ Test(io_backend, io_backend_direct_backslash) Test(io_backend, io_backend_file) { char *argv[] = { "./42sh", "../src/io_backend/tests/test.txt" }; - char *input = io_backend(2, argv); + char *input = io_backend_manager(2, argv); cr_assert_str_eq(input, "echo test", "wrong input: %s", input); free(input); @@ -59,7 +59,7 @@ Test(io_backend, io_backend_stdin) if (pid == 0) { cr_redirect_stdin(); - char *input = io_backend(1, argv); + char *input = io_backend_manager(1, argv); cr_assert_str_eq(input, "echo test", "wrong input: %s", input); free(input); exit(0); @@ -83,7 +83,7 @@ Test(io_backend, io_backend_stdin_eof) if (pid == 0) { cr_redirect_stdin(); - char *input = io_backend(1, argv); + char *input = io_backend_manager(1, argv); cr_assert_str_eq(input, "echo test", "wrong input: %s", input); free(input); exit(0); From 55670574e7318a82934eabcab9f2f8e2c0a17c5f Mon Sep 17 00:00:00 2001 From: erwann lesech Date: Sat, 13 Jan 2024 10:51:37 +0100 Subject: [PATCH 4/4] fix: fix function name in tests --- src/execute/tests/exec_tests.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/execute/tests/exec_tests.c b/src/execute/tests/exec_tests.c index de95f235..56a171e6 100644 --- a/src/execute/tests/exec_tests.c +++ b/src/execute/tests/exec_tests.c @@ -22,7 +22,7 @@ Test(exec, test_exec_simple_command) char *argv[] = { "./42sh", "-c", "echo test", NULL }; int argc = 3; - char *input = io_backend(argc, argv); + char *input = io_backend_manager(argc, argv); struct lexer *lexer = lexer_new(input);