You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[RNE Rewrite] chore: set up clangd for C++ sources (#1285)
## Description
Sets up a committed [clangd](https://clangd.llvm.org/) config for the
core package's C/C++ sources so the rewrite gets code intelligence and a
shared, strict warning set, and enforces that set at commit time. First
step of #1271; clang-tidy CI is a follow-up (#1286).
- `compile_flags.txt` — include roots / `-std=c++20` / defines, with
third-party headers (ExecuTorch/torch/JSI) as `-isystem` so their
warnings don't pollute our diagnostics. Relative paths anchored to the
package dir, so it is portable with no per-machine generation.
- `.clangd` — strict warning set layered on top (incl.
`-Wconversion`/`-Wsign-conversion`), with clangd's include cleaner
disabled (ExecuTorch umbrella headers misreport includes).
- `scripts/check-cpp-warnings.sh` + `lefthook.yml` — a `pre-commit`
command that compiles staged `cpp/` sources with the same warning set
and aborts the commit on any warning, keeping the editor and the commit
gate in sync. It skips gracefully (never blocks) when no compiler or the
provisioned headers are available; bypass with `git commit --no-verify`.
- `.gitignore` — track the shared config while keeping generated
`compile_commands.json` local.
- `CONTRIBUTING.md` — prerequisites (provisioned `third-party/include` +
`yarn install`), editor setup, and the pre-commit behavior.
### Introduces a breaking change?
- [ ] Yes
- [x] No
### Type of change
- [ ] Bug fix (change which fixes an issue)
- [ ] New feature (change which adds functionality)
- [ ] Documentation update (improves or adds clarity to existing
documentation)
- [x] Other (chores, tests, code style improvements etc.)
### Tested on
- [ ] iOS
- [ ] Android
### Testing instructions
1. Provision `packages/react-native-executorch/third-party/include` and
run `yarn install`.
2. Open a file under `packages/react-native-executorch/cpp` in an editor
with the clangd extension (MS C/C++ IntelliSense disabled); confirm the
`<executorch/...>`/`<jsi/jsi.h>` includes resolve and the current
sources are clean.
3. Confirm the strict flags fire by introducing a deliberate violation,
e.g. in `cpp/core/dtype.cpp`:
```cpp
int rne_probe(float f) {
int casted = (int)f; // expect -Wold-style-cast + -Wconversion
int unused = 7; // expect -Wunused-variable
return casted;
}
```
clangd should flag these on our code (third-party headers stay quiet
thanks to `-isystem`).
4. `git add` that change and `git commit` — the pre-commit hook aborts
with the warning printed. Verified end-to-end via lefthook: warning →
`cpp-warnings ❌` (exit 1); clean → `✔️` (exit 0). Revert afterwards.
### Related issues
#1271
### Checklist
- [x] I have performed a self-review of my code
- [x] I have commented my code, particularly in hard-to-understand areas
- [x] I have updated the documentation accordingly
- [x] My changes generate no new warnings
Copy file name to clipboardExpand all lines: CONTRIBUTING.md
+34Lines changed: 34 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -45,6 +45,40 @@ Found a model you would like to use in your app but it is not currently supplied
45
45
Do you have a neat example use case and want to share it with us? You can just drop us a message on Discord server and/or open a PR to `apps` directory here.
46
46
If you found some inconsistencies in our documentation or just something is missing just open a PR with suggested changes (remember to add changes to previous docs versions too, e.g `docs/versioned_docs/version-0.3.x`, `docs/versioned_docs/version-0.4.x`).
47
47
48
+
# C++ tooling (clangd)
49
+
50
+
The core package ships a committed [clangd](https://clangd.llvm.org/) setup so the
51
+
C/C++ sources under `packages/react-native-executorch/cpp` get code intelligence and
52
+
a strict, shared set of compiler warnings:
53
+
54
+
-`packages/react-native-executorch/compile_flags.txt` — the compilation database:
55
+
language standard, preprocessor defines, and include roots (paths are relative to
56
+
the package, so the config is portable). ExecuTorch/torch/JSI headers are added as
57
+
system includes so their internal warnings don't pollute diagnostics for our code.
58
+
-`packages/react-native-executorch/.clangd` — the warning policy layered on top.
59
+
60
+
For clangd to resolve the `<executorch/...>` and `<jsi/jsi.h>` includes you need the
61
+
same prerequisites as a native build:
62
+
63
+
1.`yarn install` at the repo root (provides the JSI headers under `node_modules`).
64
+
2. ExecuTorch headers provisioned under `packages/react-native-executorch/third-party/include`
65
+
(see [third-party/README.md](./packages/react-native-executorch/third-party/README.md)).
66
+
67
+
Without them clangd still lints your own code; the third-party includes simply stay
68
+
unresolved until the headers are present.
69
+
70
+
A `pre-commit` hook (lefthook) compiles staged `cpp/` sources with this same warning set
71
+
and aborts the commit if any warning is introduced — the editor and the commit gate stay
72
+
in sync. It skips automatically when no compiler or the provisioned headers are available,
73
+
so it never blocks contributors who don't build the native code; bypass with
74
+
`git commit --no-verify`.
75
+
76
+
Editor setup: install the official **clangd** extension (e.g. `llvm-vs-code-extensions.vscode-clangd`
77
+
for VS Code) and disable the default Microsoft C/C++ IntelliSense engine so the two
78
+
don't conflict. clangd discovers `compile_flags.txt`/`.clangd` automatically from the
79
+
open file's directory. If you produce a `compile_commands.json` from a native build,
80
+
clangd will prefer it — it's gitignored and takes precedence over `compile_flags.txt`.
81
+
48
82
# Creating a Pull Request
49
83
50
84
Before writing any code reach out to us to make sure no one is currently working on it, you can always open an issue first.
0 commit comments