Skip to content

Commit

Permalink
Merge pull request #39 from ErwannLesech/ci_clang_tidy
Browse files Browse the repository at this point in the history
feat<clang_tidy>: add clang_tidy to CI
  • Loading branch information
ErwannLesech authored Jan 13, 2024
2 parents c7521e7 + 5567057 commit 1bdd040
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 10 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/execute/tests/exec_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
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 */
12 changes: 6 additions & 6 deletions src/io_backend/tests/io_backend_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down
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 1bdd040

Please sign in to comment.