From 888a028e9bf516ef8ac69c070a736bc174cacae6 Mon Sep 17 00:00:00 2001 From: dtor Date: Sun, 8 Dec 2024 19:08:11 +0000 Subject: [PATCH] ci: improve clang-tidy workflow --- .clang-tidy | 2 +- .github/workflows/clang_tidy.yml | 78 +++++++++++++++++++++----------- 2 files changed, 53 insertions(+), 27 deletions(-) diff --git a/.clang-tidy b/.clang-tidy index 15648c6..592a716 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -1,3 +1,3 @@ Checks: 'clang-analyzer-*,bugprone-*,modernize-*,readability-*,cppcoreguidelines-*,performance-*,concurrency-*,cert-*,google-*,misc-*' WarningsAsErrors: '*' -HeaderFilterRegex: '^(?!.*(third_party|external)).*' +HeaderFilterRegex: '^(?!.*(third_party|external|/usr/)).*' diff --git a/.github/workflows/clang_tidy.yml b/.github/workflows/clang_tidy.yml index 8ddf992..7b51930 100644 --- a/.github/workflows/clang_tidy.yml +++ b/.github/workflows/clang_tidy.yml @@ -4,37 +4,63 @@ name: Clang-Tidy on: # yamllint disable-line rule:truthy pull_request: branches: ['*'] + jobs: clang-tidy: runs-on: ubuntu-latest + steps: - name: Checkout code - uses: actions/checkout@v3 - - name: Get changed files - uses: jitterbit/get-changed-files@v1 - id: changed_files + uses: actions/checkout@v4 + + - name: Find affected files + id: affected_files + run: | + git fetch origin ${{ github.base_ref }} --depth=1 + git diff --name-only origin/${{ github.base_ref }} > affected_files.txt + echo "Affected files:" + cat affected_files.txt + + - name: Find affected cpp files + run: | + > affected_cpp_files.txt + + while read file; do + if [[ "$file" == *.cpp || "$file" == *.h || "$file" == *.hpp || "$file" == *.cc || "$file" == *.cxx || "$file" == *.hxx ]]; then + echo "$file" >> affected_cpp_files.txt + fi + done < affected_files.txt + + sort -u affected_cpp_files.txt -o affected_cpp_files.txt + echo "Affected cpp files:" + cat affected_cpp_files.txt + + echo "cpp_files=$(cat affected_cpp_files.txt | tr '\n' ' ')" >> $GITHUB_ENV + + - name: Generate Compile Commands + if: env.cpp_files != '' + uses: addnab/docker-run-action@v3 with: - format: space-delimited - token: ${{ secrets.GITHUB_TOKEN }} - - name: Run Clang-Tidy - run: |- - IFS=' ' read -r -a files <<< "${{ steps.changed_files.outputs.all }}" - cpp_files=() - for file in "${files[@]}"; do - if [[ "$file" == *.cpp || "$file" == *.h || "$file" == *.hpp || "$file" == *.cc || "$file" == *.cxx || "$file" == *.hxx ]]; then - cpp_files+=("$file") - fi - done - - if [ ${#cpp_files[@]} -gt 0 ]; then - echo -e "Running clang-tidy" - bazel build //... \ - --aspects @bazel_clang_tidy//clang_tidy:clang_tidy.bzl%clang_tidy_aspect \ - --output_groups=report \ - --@bazel_clang_tidy//:clang_tidy_config=//:clang_tidy_config - else - echo -e "Skipping clang-tidy since no source files were changed." - fi + image: dtors/build-cpp:latest + run: | + echo "Generating compile commands" + bazel run @hedron_compile_commands//:refresh_all + + - name: Running Clang-Tidy + if: env.cpp_files != '' + uses: addnab/docker-run-action@v3 + with: + image: dtors/build-cpp:latest + run: | + > clang_tidy_output.txt + + echo "Running Clang-Tidy for: ${cpp_files}" + clang-tidy ${cpp_files} -p ./compile_commands.json -header-filter=.* -system-headers > clang_tidy_output.txt + + - name: Skip Clang-Tidy + if: env.cpp_files == '' + run: echo "No affected cpp files found. Skipping Clang-Tidy." + - name: Show failure message if: failure() run: |- @@ -43,5 +69,5 @@ jobs: 1. Generate compile commands: bazel run @hedron_compile_commands//:refresh_all 2. Run clang-tidy: - find . -name \"*.cpp\" -o -name \"*.h\" -o -name \"*.hpp\" -o -name \"*.cc\" -o -name \"*.cxx\" -o -name \"*.hxx\" | xargs clang-tidy -fix -fix-errors -p ./compile_commands.json + find . -name \"*.cpp\" -o -name \"*.h\" -o -name \"*.hpp\" -o -name \"*.cc\" -o -name \"*.cxx\" -o -name \"*.hxx\" | xargs clang-tidy -fix -fix-errors -p ./compile_commands.json --system-headers=0 ======================================================================================"