Skip to content

Commit b218973

Browse files
committed
Clang-tidy linter tool
1 parent 9e451eb commit b218973

4 files changed

Lines changed: 356 additions & 20 deletions

File tree

.pre-commit-config.yaml

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@ repos:
1717
- id: check-yaml
1818
- id: check-added-large-files
1919
- id: check-merge-conflict
20-
- repo: https://github.com/benjeffery/pre-commit-clang-format
21-
rev: "1.0"
22-
hooks:
23-
- id: clang-format
24-
verbose: true
20+
21+
# - repo: https://github.com/benjeffery/pre-commit-clang-format
22+
# rev: "1.0"
23+
# hooks:
24+
# - id: clang-format
25+
# verbose: true
26+
2527
- repo: local
2628
hooks:
2729
- id: air-format
@@ -37,3 +39,15 @@ repos:
3739
language: system
3840
pass_filenames: false
3941
files: '\.(R|Rmd|rmd|qmd|Qmd)$'
42+
43+
- id: clang-format
44+
name: clang-format
45+
entry: clang-format -i --style=file
46+
language: system
47+
files: '\.(c|cc|cpp|cxx|h|hh|hpp|hxx)$'
48+
49+
- id: clang-tidy
50+
name: clang-tidy for RcppTskit
51+
entry: python RcppTskit/tools/clang-tidy.py
52+
language: python
53+
files: '\.(c|cc|cpp|cxx|h|hh|hpp|hxx)$'

README.md

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,9 @@ git clone https://github.com/HighlanderLab/RcppTskit.git
9292

9393
We use [pre-commit](https://pre-commit.com) hooks to ensure code quality. Specifically, we use:
9494
* [air](https://github.com/posit-dev/air) to format R code,
95-
* [jarl](https://github.com/etiennebacher/jarl) to lint R code, and
96-
* [clang-format](https://clang.llvm.org/docs/ClangFormat.html) to format C/C++ code.
95+
* [jarl](https://github.com/etiennebacher/jarl) to lint R code,
96+
* [clang-format](https://clang.llvm.org/docs/ClangFormat.html) to format C/C++ code, and
97+
* [clang-tidy](https://clang.llvm.org/extra/clang-tidy/) to lint C/C++ code.
9798

9899
To install the hooks, run:
99100

@@ -109,37 +110,37 @@ If you plan to update `tskit`, follow instructions in `extern/README.md`.
109110

110111
Then open `RcppTskit` package directory in your favourite R IDE (Positron, RStudio, text-editor-of-your-choice, etc.) and implement your changes.
111112

112-
You should routinely check the status of the package (in R):
113+
You should routinely check your changes (in R):
113114

114115
```
115116
# Note that the RcppTskit R package is in the RcppTskit sub-directory
116117
setwd("path/to/RcppTskit/RcppTskit")
117118
118-
# Run checks of your changes, documentation, tests, etc.
119+
# Check
119120
devtools::check()
120121
121-
# Install the package
122+
# Install
122123
devtools::install()
123124
124-
# Run just tests
125+
# Test
125126
devtools::test()
126127
127-
# Check code test coverage
128+
# Test coverage
128129
cov <- covr::package_coverage(clean = TRUE)
129130
covr::report(cov)
130131
```
131132

132-
Alternatively you can check from the command line:
133+
Alternatively check your changes from the command line:
133134

134135
```
135136
# Note that the RcppTskit package is in the RcppTskit sub-directory
136137
cd path/to/RcppTskit/RcppTskit
137138
138-
# Run checks of your changes, documentation, tests, etc.
139+
# Check
139140
R CMD build RcppTskit
140141
R CMD check RcppTskit_*.tar.gz
141142
142-
# Install the package
143+
# Install
143144
R CMD INSTALL RcppTskit_*.tar.gz
144145
```
145146

RcppTskit/notes_pkg_dev.Rmd

Lines changed: 70 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Next TODOs
44

5+
* Hide pointer functions and organise docs!?
6+
57
Missing link(s) in Rd file 'TreeSequence.Rd':
68
‘ts_dump’ ‘ts_summary’
79

@@ -119,19 +121,82 @@ To ignore multiple lines use `# nocov start/stop`.
119121

120122
In `Rcpp` code use `// # nocov ...`.
121123

122-
## Air formatter
124+
## Air formatter of R code
123125

124126
https://usethis.r-lib.org/reference/use_air.html
125127
```
126128
usethis::use_air()
127129
```
128130

129-
## Jarl linter and fixer (buil on Air)
131+
Setup editor and `air.toml`.
132+
133+
In comand line, run:
134+
135+
```
136+
air format .
137+
air format --check .
138+
```
139+
140+
## Jarl linter of R code
130141

131142
https://jarl.etiennebacher.com
132143

133-
* Add jarl.toml to ~/.config/jarl and to the package folder
134-
* TODO
144+
Setup editor and `jarl.toml`. Can also do `~/.config/jarl`.
145+
146+
```
147+
jarl check .
148+
jarl check --fix .
149+
```
150+
151+
## clang-format of C/C++ code
152+
153+
```
154+
# All files (run from the RcppTskit directory)
155+
find src -type f \( -name '*.c' -o -name '*.cpp' -o -name '*.h' -o -name '*.hpp' \) \
156+
! -path 'inst/include/tskit/*' \
157+
! -path 'src/tskit/*' \
158+
! -path 'src/RcppExports.cpp' \
159+
-exec clang-format -i {} +
160+
161+
# All files (run from the repo root)
162+
find . -type f \( -name '*.c' -o -name '*.cpp' -o -name '*.h' -o -name '*.hpp' \) \
163+
! -path './extern/*' \
164+
! -path './RcppTskit/inst/include/tskit/*' \
165+
! -path './RcppTskit/src/tskit/*' \
166+
! -path './RcppTskit/src/RcppExports.cpp' \
167+
-exec clang-format -i {} +
168+
169+
# Single file
170+
clang-format -i --style=file src/RcppTskit_minimal.cpp
171+
172+
# Only changes
173+
git diff --name-only main | grep -E '\.(c|cpp|h|hpp)$' | xargs clang-format -i
174+
```
175+
176+
or
177+
178+
```
179+
pre-commit run clang-format src/RcppTskit.cpp
180+
pre-commit run clang-format --all-files
181+
```
182+
183+
## clang-tidy linter of C/C++ code
184+
185+
```
186+
echo $(brew --prefix llvm)/bin/clang-tidy
187+
# /opt/homebrew/opt/llvm/bin/clang-tidy
188+
export CLANG_TIDY="$(brew --prefix llvm)/bin/clang-tidy"
189+
./tools/clang-tidy.py src/RcppTskit.cpp
190+
./RcppTskit/tools/clang-tidy.py RcppTskit/src/test_tsk_abort_stderr.cpp -- -system-headers '-header-filter=.*' -extra-arg=-Wno-unknown-warning-option
191+
./tools/clang-tidy.py src/test_tsk_abort_stderr.cpp -- -system-headers '-header-filter=.*' -extra-arg=-Wno-unknown-warning-option
192+
```
193+
194+
or
195+
196+
```
197+
pre-commit run clang-tidy src/RcppTskit.cpp
198+
pre-commit run clang-tidy --all-files
199+
```
135200

136201
## GitHub actions
137202

@@ -144,7 +209,7 @@ usethis::use_github_action("check-standard")
144209
# Code testing coverage with covr
145210
usethis::use_github_action("test-coverage")
146211
147-
# Air formatting
212+
# Air formatting --> moved to pre-commit
148213
usethis::use_github_action(url = "https://github.com/posit-dev/setup-air/blob/main/examples/format-suggest.yaml")
149214
usethis::use_github_action(url = "https://github.com/posit-dev/setup-air/blob/main/examples/format-check.yaml")
150215
```

0 commit comments

Comments
 (0)