Skip to content

Commit e333f20

Browse files
authored
chore: add project setup examples (#225)
Add examples/ with two self-contained templates (CMake, large-project) and quick-reference snippets for Meson, monorepo, clang-format-only, CI, and compile_commands.json setups. Remove examples/* from .gitignore.
1 parent fccce34 commit e333f20

5 files changed

Lines changed: 147 additions & 1 deletion

File tree

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ result.txt
1313
testing/main.c
1414
*/*compile_commands.json
1515
testing/benchmark_results.txt
16-
examples/*
1716

1817
# Ignore Python wheel packages (clang-format, clang-tidy)
1918
clang-tidy-1*
@@ -23,3 +22,6 @@ clang-format-2*
2322

2423
# Ignore CodeSpeed folder
2524
.codspeed/
25+
26+
# Others
27+
plan.md

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ comparison.
4242
- [Troubleshooting](#troubleshooting)
4343
- [Performance Optimization](#performance-optimization)
4444
- [Verbose Output](#verbose-output)
45+
- [Examples](#examples)
4546
- [FAQ](#faq)
4647
- [What's the difference between `cpp-linter-hooks` and `mirrors-clang-format`?](#whats-the-difference-between-cpp-linter-hooks-and-mirrors-clang-format)
4748
- [Contributing](#contributing)
@@ -297,6 +298,14 @@ repos:
297298
args: [--checks=.clang-tidy, --verbose] # Shows which compile_commands.json is used
298299
```
299300

301+
## Examples
302+
303+
Two self-contained templates plus quick snippets for other common setups.
304+
305+
- [CMake minimal config](examples/cmake/) — covers ~80% of C++ projects
306+
- [Large project `files:` regex](examples/large-project/) — scoping hooks for speed
307+
- [Quick snippets](examples/README.md) — Meson, clang-format-only, monorepo, CI, `compile_commands.json`
308+
300309
## FAQ
301310

302311
### What's the difference between [`cpp-linter-hooks`](https://github.com/cpp-linter/cpp-linter-hooks) and [`mirrors-clang-format`](https://github.com/pre-commit/mirrors-clang-format)?

examples/README.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Examples
2+
3+
- [CMake project](cmake/) — the default for ~80% of C++ projects
4+
- [Large project `files:` regex](large-project/) — scoping hooks for speed
5+
6+
## Quick snippets
7+
8+
### clang-format only (no build system)
9+
10+
```yaml
11+
- id: clang-format
12+
args: [--style=file, --version=21]
13+
files: \.(cpp|cc|cxx|c|h|hpp)$
14+
```
15+
16+
### Meson
17+
18+
```bash
19+
meson setup build # auto-detect works with build/
20+
```
21+
22+
Or point explicitly: `args: [--compile-commands=builddir, --checks=.clang-tidy]`.
23+
24+
### clang-tidy + compile_commands.json
25+
26+
```yaml
27+
- id: clang-tidy
28+
args: [--compile-commands=build, --checks=.clang-tidy, --version=21, --jobs=4]
29+
files: ^src/.*\.cpp$
30+
```
31+
32+
Add `--fix` to auto-apply fixes. Add `-v` to see which database is used.
33+
34+
### Monorepo
35+
36+
```yaml
37+
- id: clang-format
38+
name: clang-format (backend)
39+
args: [--style=file:backend/.clang-format, --version=21]
40+
files: ^backend/.*\.(cpp|hpp)$
41+
42+
- id: clang-format
43+
name: clang-format (frontend)
44+
args: [--style=file:frontend/.clang-format, --version=21]
45+
files: ^frontend/.*\.(cpp|hpp)$
46+
```
47+
48+
### CI (GitHub Actions)
49+
50+
```yaml
51+
# .github/workflows/lint.yml
52+
name: Lint
53+
on: [push, pull_request]
54+
jobs:
55+
pre-commit:
56+
runs-on: ubuntu-latest
57+
steps:
58+
- uses: actions/checkout@v4
59+
- uses: actions/setup-python@v5
60+
with: { python-version: '3.11' }
61+
- uses: pre-commit/action@v3.0.1
62+
```

examples/cmake/README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# CMake Project
2+
3+
If you already have a CMake project, you need **two files**:
4+
5+
## .pre-commit-config.yaml
6+
7+
```yaml
8+
repos:
9+
- repo: https://github.com/cpp-linter/cpp-linter-hooks
10+
rev: v1.4.0
11+
hooks:
12+
- id: clang-format
13+
args: [--style=file, --version=21]
14+
files: ^(src|include)/.*\.(cpp|cc|cxx|h|hpp)$
15+
16+
- id: clang-tidy
17+
args: [--checks=.clang-tidy, --version=21]
18+
files: ^(src|include)/.*\.(cpp|cc|cxx)$
19+
```
20+
21+
## CMakeLists.txt
22+
23+
Add one line so `clang-tidy` can see your include paths:
24+
25+
```cmake
26+
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
27+
```
28+
29+
Then `cmake -B build` and `pre-commit install`. Done.
30+
31+
---
32+
33+
`compile_commands.json` is auto-detected from `build/`, `out/`, `cmake-build-debug/`,
34+
or `_build/`. If your build dir has a different name, pass it explicitly:
35+
`args: [--compile-commands=mybuild, ...]`.

examples/large-project/README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Large Project — `files:` Regex
2+
3+
Use `files:` to limit hooks to relevant directories. This is the #1 perf lever.
4+
5+
## One-size-fits-most
6+
7+
```yaml
8+
- id: clang-format
9+
files: ^(src|include)/.*\.(cpp|cc|cxx|h|hpp)$
10+
```
11+
12+
## Common patterns
13+
14+
```yaml
15+
# Exclude generated code
16+
files: ^src/(?!generated/|proto/).*\.(cpp|hpp)$
17+
18+
# Library (public headers + private src)
19+
files: ^(include|src)/.*\.(cpp|cc|cxx|h|hpp|hxx)$
20+
21+
# Application (no public headers)
22+
files: ^src/.*\.(cpp|cc|cxx|h|hpp)$
23+
24+
# Qt (skip moc_)
25+
files: ^src/(?!moc_|ui_).*\.(cpp|h)$
26+
27+
# CUDA
28+
files: ^src/.*\.(cpp|cc|cxx|cu|cuh|h|hpp)$
29+
30+
# Embedded / bare-metal C
31+
files: \.(c|h|s|S)$
32+
```
33+
34+
## Perf tips
35+
36+
- **Don't lint headers directly with `clang-tidy`** — they're processed when a `.cpp` includes them. Restrict to `files: ^src/.*\.cpp$`.
37+
- **Use `--jobs=N` for `clang-tidy`** — start with `N=2`, go up to CPU core count.
38+
- **`--dry-run` for `clang-format` in CI** — fail with a readable diff instead of auto-committing formatting.

0 commit comments

Comments
 (0)