Skip to content

Commit

Permalink
feat<clang_tidy>: add clang_tidy to CI
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ErwannLesech committed Jan 13, 2024
1 parent d013ae1 commit 717daf4
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 3 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
44 changes: 44 additions & 0 deletions .github/workflows/clang_tidy.sh
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion src/io_backend/io_backend.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
2 changes: 1 addition & 1 deletion src/io_backend/io_backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
2 changes: 1 addition & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down

0 comments on commit 717daf4

Please sign in to comment.