|
| 1 | +--- |
| 2 | +date: |
| 3 | + created: 2026-09-12 |
| 4 | +categories: |
| 5 | + - Guides |
| 6 | +tags: |
| 7 | + - clang-format |
| 8 | + - clang-tidy |
| 9 | + - cpp-linter-action |
| 10 | + - cpp-linter-hooks |
| 11 | + - clang-tools |
| 12 | +authors: |
| 13 | + - shenxianpeng |
| 14 | +--- |
| 15 | + |
| 16 | +# One clang-format version everywhere: pre-commit, CI and your laptop |
| 17 | + |
| 18 | +A pull request fails the format check. The author runs `clang-format -i` locally, pushes, and it |
| 19 | +fails again with a different diff. Nothing is wrong with the code. The laptop has clang-format 18, |
| 20 | +CI installed 21 from a package repository last week, and the reviewer's editor plugin ships 19. |
| 21 | + |
| 22 | +Every major LLVM release changes clang-format's output in small ways: new style options, changed |
| 23 | +defaults, fixed bugs. `clang-tidy` is worse, because new checks appear and old ones move between |
| 24 | +categories. Pinning the *project* version of a linter wrapper does not help if the wrapper pulls |
| 25 | +whatever clang happens to be around. |
| 26 | + |
| 27 | +<!-- more --> |
| 28 | + |
| 29 | +The cpp-linter tools are built to pick one LLVM major version and use it in every place code gets |
| 30 | +checked. |
| 31 | + |
| 32 | +## Pick the version once |
| 33 | + |
| 34 | +Write it down somewhere visible, for example in `CONTRIBUTING.md`: |
| 35 | + |
| 36 | +```text |
| 37 | +Formatting and static analysis use LLVM 21 (clang-format 21, clang-tidy 21). |
| 38 | +``` |
| 39 | + |
| 40 | +Everything below pins `21`. When you move to 22, change it in all places in one commit, reformat |
| 41 | +the tree in the same commit, and the history stays clean. |
| 42 | + |
| 43 | +## Pre-commit: cpp-linter-hooks |
| 44 | + |
| 45 | +[cpp-linter-hooks](https://github.com/cpp-linter/cpp-linter-hooks) installs `clang-format` and |
| 46 | +`clang-tidy` as Python wheels, so every contributor gets the same binary regardless of what their |
| 47 | +distribution ships. The `rev` is the hooks release; the tool version is a separate `--version` |
| 48 | +argument: |
| 49 | + |
| 50 | +```yaml title=".pre-commit-config.yaml" |
| 51 | +repos: |
| 52 | + - repo: https://github.com/cpp-linter/cpp-linter-hooks |
| 53 | + rev: v1.6.0 |
| 54 | + hooks: |
| 55 | + - id: clang-format |
| 56 | + args: [--style=file, --version=21] |
| 57 | + - id: clang-tidy |
| 58 | + args: [--checks=.clang-tidy, --version=21] |
| 59 | +``` |
| 60 | +
|
| 61 | +`--style=file` and `--checks=.clang-tidy` read the same `.clang-format` and `.clang-tidy` files |
| 62 | +that CI will use, so the rules, like the version, are defined in one place. |
| 63 | + |
| 64 | +## CI: cpp-linter-action |
| 65 | + |
| 66 | +[cpp-linter-action](https://github.com/cpp-linter/cpp-linter-action) installs the requested |
| 67 | +version itself; the workflow does not need `apt-get install clang-format-21` or a matching LLVM |
| 68 | +apt repository. The `version` input takes the LLVM major version. It also accepts a path to tools |
| 69 | +you installed yourself, or an empty string for whatever the runner has; the |
| 70 | +[input reference](https://cpp-linter.github.io/cpp-linter-action/inputs-outputs/#version) has the |
| 71 | +details. |
| 72 | + |
| 73 | +```yaml title=".github/workflows/lint.yml" |
| 74 | +name: cpp-linter |
| 75 | +on: |
| 76 | + pull_request: |
| 77 | + branches: [main] |
| 78 | + push: |
| 79 | + branches: [main] |
| 80 | +
|
| 81 | +jobs: |
| 82 | + cpp-linter: |
| 83 | + runs-on: ubuntu-latest |
| 84 | + permissions: |
| 85 | + contents: read |
| 86 | + pull-requests: write |
| 87 | + steps: |
| 88 | + - uses: actions/checkout@v5 |
| 89 | + - uses: cpp-linter/cpp-linter-action@v2 |
| 90 | + id: linter |
| 91 | + env: |
| 92 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 93 | + with: |
| 94 | + version: '21' |
| 95 | + style: file |
| 96 | + tidy-checks: '' |
| 97 | + thread-comments: ${{ github.event_name == 'pull_request' && 'update' }} |
| 98 | + - name: Fail on lint errors |
| 99 | + if: steps.linter.outputs.checks-failed > 0 |
| 100 | + run: exit 1 |
| 101 | +``` |
| 102 | + |
| 103 | +`style: file` and an empty `tidy-checks` tell the action to use `.clang-format` and |
| 104 | +`.clang-tidy` from the repository, the same files pre-commit used a minute earlier on the |
| 105 | +developer's machine. The result is a thread comment on the pull request that is updated on every |
| 106 | +push instead of a new comment each time, plus file annotations in the diff view. |
| 107 | + |
| 108 | +## Local one-off runs: clang-tools |
| 109 | + |
| 110 | +Sometimes you want the binary itself: a quick `clang-format --dry-run` over a directory, or a |
| 111 | +`clang-tidy` run with a hand-written compile database. [clang-tools](https://github.com/cpp-linter/clang-tools-pip) |
| 112 | +downloads a statically linked binary for the exact major version and falls back to the Python |
| 113 | +wheel when there is no binary for your platform: |
| 114 | + |
| 115 | +```bash |
| 116 | +pip install clang-tools |
| 117 | +clang-tools install clang-format clang-tidy --version 21 |
| 118 | +clang-format-21 --version |
| 119 | +``` |
| 120 | + |
| 121 | +The binaries come from [clang-tools-static-binaries](https://github.com/cpp-linter/clang-tools-static-binaries), |
| 122 | +which publishes a rolling window of recent LLVM majors for Linux, macOS and Windows, on x86-64 and |
| 123 | +ARM64; each release lists its exact versions in |
| 124 | +[`versions.json`](https://github.com/cpp-linter/clang-tools-static-binaries/releases/latest/download/versions.json). |
| 125 | +The same release is what cpp-linter-action downloads in CI, so the bytes match. |
| 126 | + |
| 127 | +## Containers and other package managers |
| 128 | + |
| 129 | +The same versions are available as |
| 130 | +[Docker images](https://github.com/cpp-linter/clang-tools-docker) tagged by major version: |
| 131 | + |
| 132 | +```bash |
| 133 | +docker run -v "$PWD":/src xianpengshen/clang-tools:21 clang-format --dry-run --Werror /src/main.cpp |
| 134 | +``` |
| 135 | + |
| 136 | +The [Homebrew tap](https://github.com/cpp-linter/homebrew-tap) and the |
| 137 | +[asdf plugin](https://github.com/cpp-linter/asdf-clang-tools) are built from the same static |
| 138 | +binaries, for teams that already manage tool versions that way: |
| 139 | + |
| 140 | +```bash |
| 141 | +brew install cpp-linter/tap/clang-format@21 cpp-linter/tap/clang-tidy@21 |
| 142 | +``` |
| 143 | + |
| 144 | +## Upgrading |
| 145 | + |
| 146 | +When LLVM 22 is the version you want: |
| 147 | + |
| 148 | +1. Change `21` to `22` in `.pre-commit-config.yaml`, the workflow and `CONTRIBUTING.md`. |
| 149 | +2. Run `pre-commit run clang-format --all-files` and commit the reformatted tree together with the |
| 150 | + version change. |
| 151 | +3. Open the pull request. cpp-linter-action runs with 22 and should report nothing, because the |
| 152 | + tree was formatted with the same version a moment ago. |
| 153 | + |
| 154 | +If the action reports differences at this point, the two tools are not on the same version, and |
| 155 | +the numbers above are the first thing to check. |
| 156 | + |
| 157 | +## clang-tidy drifts more than clang-format |
| 158 | + |
| 159 | +`clang-format` version drift produces noisy diffs. `clang-tidy` version drift produces different |
| 160 | +findings: checks that were added, renamed or made stricter. A pull request that passes locally and |
| 161 | +fails in CI with a check nobody has heard of is usually a version mismatch. With the hook and the |
| 162 | +action pinned to the same major version, the two numbers to compare are `--version` in |
| 163 | +`.pre-commit-config.yaml` and `version` in the workflow. |
0 commit comments