|
| 1 | +# Clang-Tidy |
| 2 | + |
| 3 | +Centralized clang-tidy configuration for Eclipse S-CORE C++ modules. |
| 4 | + |
| 5 | +## What This Provides |
| 6 | + |
| 7 | +| File | Purpose | |
| 8 | +|------|---------| |
| 9 | +| `.clang-tidy` | Canonical S-CORE check set (conservative baseline, tailorable per module) | |
| 10 | +| `clang_tidy.bazelrc` | Importable Bazel flags that activate the `clang-tidy` test config | |
| 11 | + |
| 12 | +This module ships **policy assets**, not API wrappers. Consumers wire up |
| 13 | +`lint_clang_tidy_aspect` / `lint_test` from `@aspect_rules_lint` directly — |
| 14 | +exactly the same pattern used by the `sanitizers` module. |
| 15 | + |
| 16 | +## Setup (8 steps) |
| 17 | + |
| 18 | +### 1 — Add Bazel dependencies |
| 19 | + |
| 20 | +In your `MODULE.bazel`: |
| 21 | + |
| 22 | +```python |
| 23 | +bazel_dep(name = "score_cpp_policies", version = "<version>") |
| 24 | +bazel_dep(name = "aspect_rules_lint", version = "2.5.0") |
| 25 | +bazel_dep(name = "toolchains_llvm", version = "1.7.0") |
| 26 | +``` |
| 27 | + |
| 28 | +Register an LLVM toolchain via the `llvm` extension (see `tests/MODULE.bazel` for a |
| 29 | +minimal example). |
| 30 | + |
| 31 | +### 2 — Import `clang_tidy.bazelrc` |
| 32 | + |
| 33 | +In your workspace `.bazelrc`: |
| 34 | + |
| 35 | +```bazelrc |
| 36 | +import %workspace%/path/to/clang_tidy.bazelrc # if vendored locally |
| 37 | +# — or, once Bazel supports external-repo imports — |
| 38 | +# import %workspace%/../clang_tidy/clang_tidy.bazelrc |
| 39 | +``` |
| 40 | + |
| 41 | +> **Tip**: If your repo layout places `score_cpp_policies` as a sibling (as in the |
| 42 | +> `tests/` self-test workspace here), a relative `import` works. Otherwise vendor |
| 43 | +> the three lines from `clang_tidy.bazelrc` into your own `.bazelrc`. |
| 44 | +
|
| 45 | +### 3 — Create `tools/lint/BUILD.bazel` |
| 46 | + |
| 47 | +```python |
| 48 | +# tools/lint/BUILD.bazel (empty package marker is sufficient) |
| 49 | +``` |
| 50 | + |
| 51 | +### 4 — Create `tools/lint/linters.bzl` |
| 52 | + |
| 53 | +```python |
| 54 | +load("@aspect_rules_lint//lint:clang_tidy.bzl", "lint_clang_tidy_aspect") |
| 55 | +load("@aspect_rules_lint//lint:lint_test.bzl", "lint_test") |
| 56 | + |
| 57 | +clang_tidy_aspect = lint_clang_tidy_aspect( |
| 58 | + binary = Label("@llvm_toolchain//:clang-tidy"), |
| 59 | + configs = [ |
| 60 | + Label("@score_cpp_policies//clang_tidy:.clang-tidy"), # central baseline |
| 61 | + Label("//:.clang-tidy"), # local overrides (optional) |
| 62 | + ], |
| 63 | + lint_target_headers = True, |
| 64 | + angle_includes_are_system = True, |
| 65 | +) |
| 66 | + |
| 67 | +clang_tidy_test = lint_test(aspect = clang_tidy_aspect) |
| 68 | +``` |
| 69 | + |
| 70 | +If you do not need per-module overrides, omit the `Label("//:.clang-tidy")` entry and |
| 71 | +skip step 5. |
| 72 | + |
| 73 | +### 5 — (Optional) Add a local `.clang-tidy` override |
| 74 | + |
| 75 | +Place a `.clang-tidy` at your repo root to extend or tighten the central check set. |
| 76 | +Use [`@score_cpp_policies//clang_tidy:.clang-tidy`](.clang-tidy) as the starting point. |
| 77 | + |
| 78 | +> **Advisory checks**: Checks in the `cppcoreguidelines-*` and `modernize-*` families are |
| 79 | +> advisory (warnings only). Only `clang-analyzer-*` is `WarningsAsErrors` by default. |
| 80 | +> Module owners should tighten `WarningsAsErrors` incrementally as compliance improves. |
| 81 | +
|
| 82 | +### 6 — Expose `clang_tidy_test` in a `BUILD` file |
| 83 | + |
| 84 | +```python |
| 85 | +load("//tools/lint:linters.bzl", "clang_tidy_test") |
| 86 | + |
| 87 | +clang_tidy_test( |
| 88 | + name = "clang_tidy", |
| 89 | + srcs = ["//..."], # or a more targeted glob |
| 90 | +) |
| 91 | +``` |
| 92 | + |
| 93 | +### 7 — Run |
| 94 | + |
| 95 | +```bash |
| 96 | +bazel test --config=clang-tidy //... |
| 97 | +``` |
| 98 | + |
| 99 | +Reports are written to `bazel-out/.../rules_lint_report/` as text files next to each |
| 100 | +linted target. |
| 101 | + |
| 102 | +### 8 — CI integration |
| 103 | + |
| 104 | +Add a workflow job that runs: |
| 105 | + |
| 106 | +```bash |
| 107 | +bazel test --config=clang-tidy //... |
| 108 | +``` |
0 commit comments