Skip to content

Commit b561ed1

Browse files
committed
ci: improve clang-tidy workflow
1 parent 84d91b1 commit b561ed1

File tree

2 files changed

+53
-28
lines changed

2 files changed

+53
-28
lines changed

.clang-tidy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
Checks: 'clang-analyzer-*,bugprone-*,modernize-*,readability-*,cppcoreguidelines-*,performance-*,concurrency-*,cert-*,google-*,misc-*'
22
WarningsAsErrors: '*'
3-
HeaderFilterRegex: '^(?!.*(third_party|external)).*'
3+
HeaderFilterRegex: '^(?!.*(third_party|external|/usr/)).*'

.github/workflows/clang_tidy.yml

Lines changed: 52 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,62 @@ name: Clang-Tidy
44
on: # yamllint disable-line rule:truthy
55
pull_request:
66
branches: ['*']
7+
78
jobs:
89
clang-tidy:
910
runs-on: ubuntu-latest
11+
container:
12+
image: dtors/base-cpp:latest
13+
1014
steps:
1115
- name: Checkout code
12-
uses: actions/checkout@v3
13-
- name: Get changed files
14-
uses: jitterbit/get-changed-files@v1
15-
id: changed_files
16-
with:
17-
format: space-delimited
18-
token: ${{ secrets.GITHUB_TOKEN }}
19-
- name: Run Clang-Tidy
20-
run: |-
21-
IFS=' ' read -r -a files <<< "${{ steps.changed_files.outputs.all }}"
22-
cpp_files=()
23-
for file in "${files[@]}"; do
24-
if [[ "$file" == *.cpp || "$file" == *.h || "$file" == *.hpp || "$file" == *.cc || "$file" == *.cxx || "$file" == *.hxx ]]; then
25-
cpp_files+=("$file")
26-
fi
27-
done
28-
29-
if [ ${#cpp_files[@]} -gt 0 ]; then
30-
echo -e "Running clang-tidy"
31-
bazel build //... \
32-
--aspects @bazel_clang_tidy//clang_tidy:clang_tidy.bzl%clang_tidy_aspect \
33-
--output_groups=report \
34-
--@bazel_clang_tidy//:clang_tidy_config=//:clang_tidy_config
35-
else
36-
echo -e "Skipping clang-tidy since no source files were changed."
37-
fi
16+
uses: actions/checkout@v4
17+
18+
- name: Configure Git Safe Directory
19+
run: |
20+
git config --global --add safe.directory $(pwd)
21+
22+
- name: Find affected files
23+
run: |
24+
git fetch origin ${{ github.base_ref }} --depth=1
25+
git diff --name-only origin/${{ github.base_ref }} > affected_files.txt
26+
echo "Affected files:"
27+
cat affected_files.txt
28+
29+
- name: Find affected cpp files
30+
run: |
31+
> affected_cpp_files.txt
32+
33+
while read file; do
34+
if [[ "$file" == *.cpp || "$file" == *.h || "$file" == *.hpp || "$file" == *.cc || "$file" == *.cxx || "$file" == *.hxx ]]; then
35+
echo "$file" >> affected_cpp_files.txt
36+
fi
37+
done < affected_files.txt
38+
39+
sort -u affected_cpp_files.txt -o affected_cpp_files.txt
40+
echo "Affected cpp files:"
41+
cat affected_cpp_files.txt
42+
43+
echo "cpp_files=$(cat affected_cpp_files.txt | tr '\n' ' ')" >> $GITHUB_ENV
44+
45+
- name: Generate Compile Commands
46+
if: env.cpp_files != ''
47+
run: |
48+
echo "Generating compile commands"
49+
bazel run @hedron_compile_commands//:refresh_all
50+
51+
- name: Running Clang-Tidy
52+
if: env.cpp_files != ''
53+
run: |
54+
> clang_tidy_output.txt
55+
56+
echo "Running Clang-Tidy for: ${cpp_files}"
57+
clang-tidy ${cpp_files} -p ./compile_commands.json -header-filter=.* -system-headers > clang_tidy_output.txt
58+
59+
- name: Skip Clang-Tidy
60+
if: env.cpp_files == ''
61+
run: echo "No affected cpp files found. Skipping Clang-Tidy."
62+
3863
- name: Show failure message
3964
if: failure()
4065
run: |-
@@ -43,5 +68,5 @@ jobs:
4368
1. Generate compile commands:
4469
bazel run @hedron_compile_commands//:refresh_all
4570
2. Run clang-tidy:
46-
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
71+
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
4772
======================================================================================"

0 commit comments

Comments
 (0)