Repository navigation
Commit ffdef70
authored
feat: clang-tidy checks only changed lines and skips without a compilation database (#303)
* feat: clang-tidy checks only changed lines and skips without a compilation database
- Report only diagnostics on the lines a commit adds or changes, using
clang-tidy's --line-filter (so --fix is limited too). The lines come from
the staged changes, or from PRE_COMMIT_FROM_REF...PRE_COMMIT_TO_REF for
pre-push and run --from-ref/--to-ref. Every line is checked when a file
has no changes (run --all-files) or outside git; --all-lines turns the
filter off.
- Without a compilation database, skip clang-tidy locally with a notice and
fail when CI is set. clang-tidy's own compile_commands.json and
compile_flags.txt lookup next to the files, -p and --extra-arg count as a
database.
- Also look in builddir/, cmake-build-*/, build-*/, build/*/ and
out/build/*/ for compile_commands.json.
- Treat all file types pre-commit tags as c/c++ (.mm, .ino, module units)
as source files.
* refactor: name the compile_commands.json literal once
* perf: find compile_commands.json with os.scandir; time out a stuck git diff
* refactor: split the compile_commands.json pattern search
* fix: keep line filters exact and short for large commits
- clang-tidy uses the first --line-filter name that ends the reported path,
so list longer names first: "a.cpp" no longer takes the lines of
"data.cpp" and hides its new warnings.
- Give each parallel run only its own file's filter, and split a serial run
into several commands when the filter would make the command line too
long (Windows: 32,767 characters, Linux: 128 KiB per argument).
- Report any OSError from starting clang-tidy as a failed hook instead of a
traceback.
* fix: check every line when the file on disk is not the diffed version
The staged (or pushed) line numbers only fit the file clang-tidy reads when
the two match. pre-commit hides unstaged changes during a commit, but a
direct run, --files, or a push with uncommitted changes can differ: then
check every line instead of filtering with lines that do not fit.
* fix: do not read an added "++ " line as a diff file header
An added line "++ i;" shows as "+++ i;" in the diff, which the parser took
for the next file's header, so later hunks of the file were lost. Skip each
hunk's lines by their counts. Also say in the docs that --fix applies the
fixes of the reported problems, which can touch a nearby unchanged line.
* refactor: move hunk skipping out of the diff parser
Keeps _parse_added_lines simple (SonarCloud cognitive complexity) and
covers a diff that ends inside a hunk.
---------
Co-authored-by: Xianpeng Shen <3353385+shenxianpeng@users.noreply.github.com>1 parent ae4e8b3 commit ffdef70
10 files changed
Lines changed: 1229 additions & 55 deletions
File tree
- cpp_linter_hooks
- examples
- cmake
- meson
- tests
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
26 | 26 | | |
27 | 27 | | |
28 | 28 | | |
29 | | - | |
| 29 | + | |
| 30 | + | |
30 | 31 | | |
31 | 32 | | |
32 | 33 | | |
| |||
44 | 45 | | |
45 | 46 | | |
46 | 47 | | |
47 | | - | |
| 48 | + | |
| 49 | + | |
48 | 50 | | |
49 | 51 | | |
50 | 52 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
29 | 29 | | |
30 | 30 | | |
31 | 31 | | |
32 | | - | |
33 | | - | |
34 | | - | |
35 | | - | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
36 | 37 | | |
37 | 38 | | |
38 | 39 | | |
| |||
163 | 164 | | |
164 | 165 | | |
165 | 166 | | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
166 | 195 | | |
167 | 196 | | |
168 | 197 | | |
| |||
187 | 216 | | |
188 | 217 | | |
189 | 218 | | |
190 | | - | |
191 | | - | |
192 | | - | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
193 | 226 | | |
194 | 227 | | |
195 | 228 | | |
| |||
207 | 240 | | |
208 | 241 | | |
209 | 242 | | |
210 | | - | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
211 | 256 | | |
212 | 257 | | |
213 | 258 | | |
| |||
264 | 309 | | |
265 | 310 | | |
266 | 311 | | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
267 | 317 | | |
268 | 318 | | |
269 | 319 | | |
| |||
278 | 328 | | |
279 | 329 | | |
280 | 330 | | |
281 | | - | |
| 331 | + | |
282 | 332 | | |
283 | 333 | | |
284 | 334 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
0 commit comments