Skip to content

Commit 7b9d052

Browse files
committed
Robustifying clang-tidy
1 parent 07a8757 commit 7b9d052

5 files changed

Lines changed: 268 additions & 52 deletions

File tree

.clang-tidy

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
# Stable low-noise gate for pre-commit. Keep this profile focused on
3+
# correctness and a few numerics/performance checks that are useful for this
4+
# codebase without depending heavily on LLVM-version-specific style policies.
5+
Checks: >-
6+
clang-diagnostic-*,
7+
clang-analyzer-*,
8+
bugprone-integer-division,
9+
bugprone-incorrect-roundings,
10+
bugprone-suspicious-memory-comparison,
11+
bugprone-too-small-loop-variable,
12+
bugprone-undefined-memory-manipulation,
13+
performance-inefficient-vector-operation,
14+
performance-move-const-arg
15+
WarningsAsErrors: ""
16+
FormatStyle: none
17+
SystemHeaders: false

README_DEVEL.md

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ The current hook set includes:
9696
* [air](https://github.com/posit-dev/air) to format `R` code
9797
* [jarl](https://github.com/etiennebacher/jarl) to lint `R` code
9898
* [clang-format](https://clang.llvm.org/docs/ClangFormat.html) to format `C/C++` code
99+
* [clang-tidy](https://clang.llvm.org/extra/clang-tidy/) to lint `C/C++` code
99100

100101
Install these tools with your preferred package manager before contributing.
101102
One macOS setup looks like this:
@@ -113,7 +114,10 @@ brew install air
113114
# Install clang-format
114115
brew install clang-format
115116

116-
# Add llvm tools, to PATH
117+
# Install llvm for clang-tidy
118+
brew install llvm
119+
120+
# Add llvm tools to PATH
117121
echo 'export PATH="/opt/homebrew/opt/llvm/bin:$PATH"' >> ~/.zshrc
118122
```
119123

@@ -191,8 +195,11 @@ Relevant tests are in `RcppTskit/tests/testthat`.
191195

192196
During development,
193197
start with focused tests for the area you changed.
194-
Before opening or updating a pull request,
195-
run the full test suite and package checks.
198+
Before opening or updating a pull request:
199+
1) run the full test suite,
200+
2) ensure package checks pass, and
201+
3) check test coverage.
202+
All of these are described below.
196203

197204
### `R CMD check`
198205

@@ -323,6 +330,9 @@ At that point,
323330
the safest recovery path is usually to create a fresh branch from the latest `upstream/main`
324331
and then move your commits across with `git cherry-pick <commit>`,
325332
or simply re-apply the changes manually to the files if that is simpler.
333+
Another option with many local commits
334+
is to squash them (see below) into one
335+
and then rebasing.
326336

327337
After a successful rebase, rerun the relevant tests and checks.
328338
If you already pushed the branch to `origin`, update it with:
@@ -393,3 +403,12 @@ If you plan to update the vendored `tskit` C library,
393403
follow the instructions in `extern/README.md` and
394404
do not edit vendored copies by hand.
395405
Any changes to that code should go to `tskit` upstream.
406+
407+
## More advanced and thorough testing
408+
409+
The repository pins a low-noise `.clang-tidy` profile.
410+
For occasional broader `C/C++` audits,
411+
use `--config-file=RcppTskit/tools/clang_tidy_audit.yaml`
412+
with `clang-tidy` or `RcppTskit/tools/clang_tidy.py`.
413+
See `RcppTskit/notes_pkg_dev.Rmd` for a set of unpolished notes.
414+
The notes also include suggestions on debugging `C/C++` code.

RcppTskit/notes_pkg_dev.Rmd

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -167,36 +167,6 @@ To ignore multiple lines use `# nocov start/stop`.
167167

168168
In `Rcpp` code use `// # nocov ...`.
169169

170-
### Debugging C/C++
171-
172-
In `src/Makevars.in` uncomment debug flags ...
173-
174-
From terminal run:
175-
176-
```
177-
# Go to package folder
178-
R_HOME="/Library/Frameworks/R.framework/Resources"
179-
R_BIN="$R_HOME/bin/exec/R"
180-
ASAN="/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/17/lib/darwin/libclang_rt.asan_osx_dynamic.dylib"
181-
UBSAN="/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/17/lib/darwin/libclang_rt.ubsan_osx_dynamic.dylib"
182-
183-
env R_HOME="$R_HOME" \
184-
DYLD_INSERT_LIBRARIES="$ASAN:$UBSAN" \
185-
ASAN_OPTIONS="abort_on_error=1,detect_leaks=0,verbosity=1" \
186-
UBSAN_OPTIONS="print_stacktrace=1" \
187-
"$R_BIN" --vanilla
188-
```
189-
190-
In the R session (not from Rstudio/Positron!):
191-
192-
```
193-
# Load + compile into the current ASan session (no child R load)
194-
devtools::load_all()
195-
196-
# Run tests in the same process
197-
devtools::test()
198-
```
199-
200170
### Air formatter of R code
201171

202172
https://usethis.r-lib.org/reference/use_air.html
@@ -264,13 +234,21 @@ pre-commit run clang-format --all-files
264234

265235
https://clang.llvm.org/extra/clang-tidy/
266236

237+
The repo uses two `clang-tidy` profiles:
238+
239+
* `.clang-tidy`: stable low-noise pre-commit gate focused on correctness,
240+
numerics, and a few performance checks.
241+
* `tools/clang_tidy_audit.yaml`: broader manual audit profile for occasional
242+
sweeps; do not require this profile to be warning-free on every commit.
243+
267244
```
268245
echo $(brew --prefix llvm)/bin/clang-tidy
269246
# /opt/homebrew/opt/llvm/bin/clang-tidy
270247
export CLANG_TIDY="$(brew --prefix llvm)/bin/clang-tidy"
271248
./tools/clang_tidy.py src/RcppTskit.cpp
272249
./RcppTskit/tools/clang_tidy.py RcppTskit/src/test_tsk_abort_stderr.cpp -- -system-headers '-header-filter=.*' -extra-arg=-Wno-unknown-warning-option
273250
./tools/clang_tidy.py src/test_tsk_abort_stderr.cpp -- -system-headers '-header-filter=.*' -extra-arg=-Wno-unknown-warning-option
251+
./tools/clang_tidy.py src/RcppTskit.cpp -- --config-file=tools/clang_tidy_audit.yaml
274252
```
275253

276254
or
@@ -280,6 +258,36 @@ pre-commit run clang-tidy src/RcppTskit.cpp
280258
pre-commit run clang-tidy --all-files
281259
```
282260

261+
### Debugging C/C++
262+
263+
In `src/Makevars.in` uncomment debug flags ...
264+
265+
From terminal run:
266+
267+
```
268+
# Go to package folder
269+
R_HOME="/Library/Frameworks/R.framework/Resources"
270+
R_BIN="$R_HOME/bin/exec/R"
271+
ASAN="/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/17/lib/darwin/libclang_rt.asan_osx_dynamic.dylib"
272+
UBSAN="/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/17/lib/darwin/libclang_rt.ubsan_osx_dynamic.dylib"
273+
274+
env R_HOME="$R_HOME" \
275+
DYLD_INSERT_LIBRARIES="$ASAN:$UBSAN" \
276+
ASAN_OPTIONS="abort_on_error=1,detect_leaks=0,verbosity=1" \
277+
UBSAN_OPTIONS="print_stacktrace=1" \
278+
"$R_BIN" --vanilla
279+
```
280+
281+
In the R session (not from Rstudio/Positron!):
282+
283+
```
284+
# Load + compile into the current ASan session (no child R load)
285+
devtools::load_all()
286+
287+
# Run tests in the same process
288+
devtools::test()
289+
```
290+
283291
### GitHub actions
284292

285293
```

0 commit comments

Comments
 (0)