Skip to content

Commit a104953

Browse files
authored
docs: add guide on running clang-tidy on pull requests with GitHub Actions (#62)
## Why People search for "clang-tidy github actions" and "clang-tidy pull request comments", and for those queries platisd/clang-tidy-pr-comments and ZedThree/clang-tidy-review rank ahead of cpp-linter-action. The site has no page that answers that question from start to finish; the two existing posts assume the reader already has a lint workflow. This is the first of a few how-to posts written around what people actually search for. ## What's in this PR One new post in the Guides category: **Run clang-tidy on pull requests with GitHub Actions** (`docs/blog/posts/2026-09-20-clang-tidy-pull-requests-github-actions.md`), published at `/blog/2026/09/20/clang-tidy-github-actions-pull-requests/` (explicit `slug`, plus a meta `description`). It follows the places where people get stuck: 1. A small starter `.clang-tidy` (`bugprone-*`, `performance-*`, `clang-analyzer-*`, explicit `HeaderFilterRegex`). 2. Generating `compile_commands.json` with CMake, Meson, Make + Bear, or `extra-args` when there is no build system. 3. The full workflow, with a table explaining each input. 4. `files-changed-only` / `lines-changed-only` and what each value reports. 5. Where feedback appears (annotations, step summary, thread comment, review) and which token permission each needs, plus the review caveats. 6. Failing the check with the `clang-tidy-checks-failed` output, and rolling that out gradually. 7. Fork pull requests: what still works with a read-only token, and why not to switch to `pull_request_target`. 8. A troubleshooting table. Two details worth a look from someone who knows the action well: - The post says the default `tidy-checks` value is appended to the `Checks` in `.clang-tidy`, and recommends `tidy-checks: ''`. That is taken from the input description in `action.yml`. - The post says clang-tidy 22 reports findings from all non-system headers by default, while 21 and earlier hide header findings unless `HeaderFilterRegex` matches. That is from the LLVM 22 release notes, and is the reason the starter config sets `HeaderFilterRegex` explicitly. I left out the note about thread comments being disabled on private repositories, because I could not find the matching behaviour in the cpp-linter source. ## Checks - Inputs, outputs, permissions and review caveats were checked against `action.yml` at v2.22.0, `docs/permissions.md` and `docs/pr-review-caveats.md`; the hook arguments against the cpp-linter-hooks README. - The starter `.clang-tidy` was run with clang-tidy 22.1.0 on a small demo project: `--verify-config` reports no errors, a finding in `include/` is reported and the same finding in `third_party/` is filtered out. - `mkdocs build --strict` passes locally, and the repository's pre-commit hooks pass on the new file. - The workflow in the post has not been run on GitHub as written. It has the same structure as the one in the migration post; the differences are that the push trigger and `format-review` are gone, clang-format is off (`style: ''`), `step-summary` is on, and the fail step uses `clang-tidy-checks-failed`.
1 parent b7045cd commit a104953

1 file changed

Lines changed: 263 additions & 0 deletions

File tree

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

0 commit comments

Comments
 (0)