Skip to content

Commit 22fecba

Browse files
committed
docs: add guide on running clang-tidy on pull requests with GitHub Actions
1 parent 0569232 commit 22fecba

1 file changed

Lines changed: 266 additions & 0 deletions

File tree

Lines changed: 266 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,266 @@
1+
---
2+
date:
3+
created: 2026-09-20
4+
slug: clang-tidy-github-actions-pull-requests
5+
description: >-
6+
Run clang-tidy on every pull request with GitHub Actions: generate compile_commands.json,
7+
report only the changed lines, post findings as review comments, and fail the check.
8+
categories:
9+
- Guides
10+
tags:
11+
- clang-tidy
12+
- cpp-linter-action
13+
- github-actions
14+
authors:
15+
- shenxianpeng
16+
---
17+
18+
# Run clang-tidy on pull requests with GitHub Actions
19+
20+
Running clang-tidy once on a laptop is easy. Running it on every pull request is where projects
21+
get stuck: clang-tidy needs the real compile flags or it reports missing headers, the first run on
22+
an existing code base produces thousands of findings nobody asked for, and the output ends up in a
23+
CI log that contributors do not open.
24+
25+
This guide sets up one GitHub Actions job that solves those three problems. When it is done,
26+
clang-tidy findings appear as review comments on the lines a pull request changed, and the check
27+
turns red when something is left unfixed.
28+
29+
<!-- more -->
30+
31+
## Step 1: start with a small `.clang-tidy`
32+
33+
Put the configuration in the repository, not in the workflow. The same file is then used by CI,
34+
by editors (clangd, CLion, Qt Creator, Visual Studio) and by anyone running clang-tidy by hand.
35+
36+
```yaml title=".clang-tidy"
37+
Checks: >
38+
-*,
39+
bugprone-*,
40+
performance-*,
41+
clang-analyzer-*,
42+
-bugprone-easily-swappable-parameters
43+
HeaderFilterRegex: '(^|/)(src|include)/'
44+
```
45+
46+
Why this set:
47+
48+
- `bugprone-*`, `performance-*` and `clang-analyzer-*` report likely defects rather than style
49+
preferences, so the first review comments contributors see are ones they agree with. Add
50+
`modernize-*`, `readability-*` or `cppcoreguidelines-*` later, one group at a time.
51+
- `bugprone-easily-swappable-parameters` is removed because it fires on nearly every function
52+
that takes two arguments of the same type.
53+
- `HeaderFilterRegex` decides which headers get reported. Up to LLVM 21, clang-tidy hides
54+
findings in headers unless they match this expression. From LLVM 22 it shows findings from
55+
every non-system header, which includes vendored code. Setting it explicitly gives the same
56+
result on every version: your own `src/` and `include/` are reported, `third_party/` is not.
57+
Adjust the directory names to your layout.
58+
59+
Check the file before committing it:
60+
61+
```console
62+
$ clang-tidy --verify-config
63+
No config errors detected.
64+
```
65+
66+
## Step 2: generate `compile_commands.json`
67+
68+
clang-tidy parses each file the way the compiler does, so it needs the include paths, defines and
69+
language standard of the real build. It reads them from a compilation database,
70+
`compile_commands.json`. Without one, the typical result is
71+
`'foo.h' file not found [clang-diagnostic-error]` and most checks never run on that file.
72+
73+
=== "CMake"
74+
75+
```bash
76+
cmake -B build -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
77+
```
78+
79+
Configuring is enough; you do not need to compile. The file is written to
80+
`build/compile_commands.json`. This works with the Makefile and Ninja generators, which is
81+
what `ubuntu-latest` uses.
82+
83+
=== "Meson"
84+
85+
```bash
86+
meson setup build
87+
```
88+
89+
Meson always writes `build/compile_commands.json`.
90+
91+
=== "Make"
92+
93+
```bash
94+
sudo apt-get install -y bear
95+
bear -- make
96+
```
97+
98+
[Bear](https://github.com/rizsotto/Bear) records the compiler calls made by `make` and writes
99+
`compile_commands.json` to the current directory. This one does need a full build.
100+
101+
=== "No build system"
102+
103+
Skip the database and pass the flags directly with the action's `extra-args` input:
104+
105+
```yaml
106+
extra-args: '-std=c++17 -Iinclude'
107+
```
108+
109+
Two things commonly go wrong here:
110+
111+
- **Third-party dependencies.** If the configure step runs `find_package(Foo)`, install
112+
`libfoo-dev` (or restore your vcpkg/Conan cache) before it, exactly as your build job does.
113+
- **Generated headers.** Protobuf output, `config.h` and similar files only exist after the build
114+
step that produces them. Build those targets before running clang-tidy.
115+
116+
## Step 3: add the workflow
117+
118+
```yaml title=".github/workflows/clang-tidy.yml"
119+
name: clang-tidy
120+
on:
121+
pull_request:
122+
branches: [main]
123+
124+
jobs:
125+
clang-tidy:
126+
runs-on: ubuntu-latest
127+
permissions:
128+
contents: read
129+
pull-requests: write
130+
steps:
131+
- uses: actions/checkout@v5
132+
133+
- name: Generate compile_commands.json
134+
run: cmake -B build -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
135+
136+
- uses: cpp-linter/cpp-linter-action@v2
137+
id: linter
138+
env:
139+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
140+
with:
141+
version: '21'
142+
style: ''
143+
tidy-checks: ''
144+
database: build
145+
lines-changed-only: true
146+
tidy-review: true
147+
step-summary: true
148+
149+
- name: Fail if clang-tidy reported anything
150+
if: steps.linter.outputs.clang-tidy-checks-failed > 0
151+
run: exit 1
152+
```
153+
154+
| Input | What it does here |
155+
| --- | --- |
156+
| `version: '21'` | Installs clang-tidy 21 on the runner. Pin it, and use the same major version locally; new LLVM releases add and move checks. |
157+
| `style: ''` | Turns clang-format off, so this job is clang-tidy only. Set it to `file` to check formatting against your `.clang-format` in the same step. |
158+
| `tidy-checks: ''` | Uses your `.clang-tidy` and nothing else. The default value is a broad list of check groups that is **appended** to the `Checks` in your file, which is the usual reason CI reports more than a local run. |
159+
| `database: build` | The directory that contains `compile_commands.json`. |
160+
| `lines-changed-only: true` | Reports findings only on lines the pull request added or modified. |
161+
| `tidy-review: true` | Posts the findings as a pull request review. |
162+
| `step-summary: true` | Writes the same report to the workflow run's summary page. |
163+
164+
## Step 4: report only what the pull request changed
165+
166+
This is the setting that makes clang-tidy acceptable on a code base that has never used it.
167+
168+
- `files-changed-only` defaults to `true`: only files touched by the pull request are analyzed.
169+
- `lines-changed-only` filters what is reported inside those files:
170+
171+
| Value | Reported |
172+
| --- | --- |
173+
| `false` (default) | Every finding in the changed files. |
174+
| `diff` | Findings on any line shown in the diff, including unchanged context lines. |
175+
| `true` | Findings on added or modified lines only. |
176+
177+
With `true`, someone who changes ten lines in a 3,000-line legacy file sees findings for those ten
178+
lines. The existing findings stay where they are until somebody touches that code, so the code
179+
base gets cleaner in the places that are actively worked on, and nobody has to land a
180+
10,000-line cleanup first.
181+
182+
clang-tidy still parses the whole translation unit, so the run time depends on how many files
183+
changed, not on how many lines.
184+
185+
## Step 5: choose where the feedback appears
186+
187+
| Feedback | Input | Default | Token permission |
188+
| --- | --- | --- | --- |
189+
| Annotations in the "Files changed" tab | `file-annotations` | `true` | none |
190+
| Report on the workflow run summary page | `step-summary` | `false` | none |
191+
| One comment in the conversation, updated on each push | `thread-comments: update` | `false` | `pull-requests: write` |
192+
| Review comments on the changed lines | `tidy-review` | `false` | `pull-requests: write` |
193+
194+
A review puts each finding next to the line it is about, with the check name. When clang-tidy has
195+
a fix for the finding, the comment contains a suggestion that can be committed from the browser.
196+
197+
![clang-tidy findings posted as a pull request review](https://raw.githubusercontent.com/cpp-linter/cpp-linter-action/main/docs/images/tidy-review.png)
198+
199+
Things to know about reviews:
200+
201+
- They are skipped for draft and closed pull requests.
202+
- Each push gets a new review, and the previous one is dismissed.
203+
- GitHub only accepts review comments on lines that are part of the diff. Anything that does not
204+
fit is counted in the review summary, which also carries the complete patch.
205+
`lines-changed-only: true` keeps that number small.
206+
- By default the review requests changes when there are findings and approves when there are none.
207+
Approving requires the repository setting "Allow GitHub Actions to create and approve pull
208+
requests". Set `passive-reviews: true` if the bot should only comment.
209+
210+
## Step 6: decide when the check fails
211+
212+
The action reports; it does not fail the job by itself. The last step in the workflow does that,
213+
using the `clang-tidy-checks-failed` output (there is also `clang-format-checks-failed`, and
214+
`checks-failed` for both).
215+
216+
A workable rollout is to leave that step out for the first couple of weeks, so contributors get
217+
used to the comments while nothing blocks a merge, and then add it and make the job a required
218+
status check.
219+
220+
## Pull requests from forks
221+
222+
For a `pull_request` event that comes from a fork, GitHub hands the workflow a read-only
223+
`GITHUB_TOKEN`, whatever the `permissions` block says. Reviews and thread comments cannot be
224+
posted with it. Annotations and the step summary still work, because they do not go through the
225+
API, and so does the failing check.
226+
227+
Do not switch the workflow to `pull_request_target` to get a write token. That event runs with
228+
your repository's secrets, and this job configures a build from the pull request's code, which is
229+
arbitrary code execution for anyone who opens a pull request.
230+
231+
## Troubleshooting
232+
233+
| Symptom | Cause and fix |
234+
| --- | --- |
235+
| `'foo.h' file not found [clang-diagnostic-error]` | No database, wrong `database` directory, a dependency that is not installed on the runner, or a generated header that has not been built. See step 2. |
236+
| CI reports far more than a local run | `tidy-checks` was left at its default and added check groups to yours; set it to `''`. Otherwise the clang-tidy versions differ; pin `version`. |
237+
| Nothing is reported for headers | `HeaderFilterRegex` does not match your header paths. |
238+
| Findings from vendored code | Narrow `HeaderFilterRegex`, and exclude the sources with `ignore: 'third_party|build'`. |
239+
| No review shows up | The pull request is a draft, comes from a fork, or the job lacks `pull-requests: write`. |
240+
| The job is slow | Keep `files-changed-only` on. The analysis already uses every core (`jobs: 0`); the time usually goes into installing dependencies and configuring, so cache those. |
241+
242+
## Run the same checks before the commit
243+
244+
The same clang-tidy version and the same `.clang-tidy` can run as a pre-commit hook, so most
245+
findings never reach the pull request:
246+
247+
```yaml title=".pre-commit-config.yaml"
248+
repos:
249+
- repo: https://github.com/cpp-linter/cpp-linter-hooks
250+
rev: v1.6.0
251+
hooks:
252+
- id: clang-tidy
253+
args: [--checks=.clang-tidy, --version=21]
254+
```
255+
256+
[One clang-format version everywhere](2026-09-12-one-clang-version-everywhere.md) covers keeping
257+
the hook, CI and local installs on one LLVM version.
258+
259+
## Where to go next
260+
261+
- Every input and output is described in the
262+
[cpp-linter-action documentation](https://cpp-linter.github.io/cpp-linter-action/).
263+
- If you already run separate clang-format and clang-tidy actions,
264+
[Moving to cpp-linter](2026-09-12-moving-to-cpp-linter.md) shows how to merge them into this
265+
one step.
266+
- [Why cpp-linter?](../../why-cpp-linter.md) compares it with the other clang-tidy actions.

0 commit comments

Comments
 (0)