Skip to content

Commit

Permalink
ci: improve clang-tidy workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
danctorres committed Dec 9, 2024
1 parent 84d91b1 commit 888a028
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 27 deletions.
2 changes: 1 addition & 1 deletion .clang-tidy
Original file line number Diff line number Diff line change
@@ -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/)).*'
78 changes: 52 additions & 26 deletions .github/workflows/clang_tidy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: |-
Expand All @@ -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
======================================================================================"

0 comments on commit 888a028

Please sign in to comment.