Skip to content

Commit 7abb084

Browse files
Harden AGENTS.md around clang usage, cleanup clang-tidy.sh wrapper (#160)
* AGENTS, readme, script cleanup
1 parent 38456f9 commit 7abb084

3 files changed

Lines changed: 50 additions & 39 deletions

File tree

‎AGENTS.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -326,11 +326,11 @@ malformed table, missing `@param` on a documented function, …) fails the build
326326

327327
Code should be easy to read and understand. If a sacrifice is made for performance or readability, it should be documented.
328328

329-
Adhere to clang-format checks configured in `.clang-format`. Run `./scripts/clang-format.sh` if needed to confirm code styling, and `./scripts/clang-format.sh --fix` to auto-format generated code created.
329+
Adhere to clang-format checks configured in `.clang-format`. After C++ code changes, run `./scripts/clang-format.sh` to confirm styling, or `./scripts/clang-format.sh --fix` to auto-format.
330330

331331
### Static Analysis
332332

333-
Adhere to clang-tidy checks configured in `.clang-tidy`. Run `./scripts/clang-tidy.sh` if needed to confirm code quality.
333+
Adhere to clang-tidy checks configured in `.clang-tidy`. After C++ code changes, run `./scripts/clang-tidy.sh` to confirm code quality.
334334

335335
## Dependencies
336336

‎docs/tools.md‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,13 @@ sudo apt-get install clang-format clang-tidy clang-tools
4949
./scripts/clang-tidy.sh
5050
```
5151

52-
The wrapper forwards extra arguments to `run-clang-tidy`:
52+
With no arguments, runs against every relevant file in the repository against
53+
the checks in `.clang-tidy`.
5354

5455
```bash
55-
./scripts/clang-tidy.sh -j 4 # Number of cores
56-
./scripts/clang-tidy.sh -checks='-*,misc-const-correctness' # Only specific checks
57-
./scripts/clang-tidy.sh -fix # Apply fixes
56+
./scripts/clang-tidy.sh src/ffi_client.cpp # Check just this file
57+
./scripts/clang-tidy.sh -j 4 # Override worker count
58+
./scripts/clang-tidy.sh --fix # Apply fixes
5859
```
5960

6061
Output is captured to `clang-tidy.log` at the repo root, since the terminal

‎scripts/clang-tidy.sh‎

Lines changed: 43 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ cd "${script_dir}/.."
6161
# users who hit a pre-flight error or a bad argument get pointed at this.
6262
usage() {
6363
cat <<'EOF'
64-
Usage: ./scripts/clang-tidy.sh [OPTIONS] [FILE...] [run-clang-tidy args...]
64+
Usage: ./scripts/clang-tidy.sh [OPTIONS] [FILE...]
6565
6666
Run clang-tidy locally or in CI using the same file set and config that the
6767
repo's CI workflow uses. Picks up checks from the repo-root .clang-tidy.
@@ -70,14 +70,16 @@ Options:
7070
-h, --help, -?
7171
Show this help and exit.
7272
--fix
73-
Apply fixes in place (forwarded to run-clang-tidy as -fix).
73+
Apply fixes in place.
7474
--github-actions, --gh
7575
Force GitHub Actions annotation + step-summary mode.
7676
Auto-detected when GITHUB_ACTIONS=true.
7777
--fail-on-warning, --strict
7878
Exit non-zero when any warning is emitted (errors already exit
7979
non-zero on their own). Off by default for local edit/run cycles;
8080
CI opts in via the workflow file.
81+
-j N
82+
Override the clang-tidy worker count. Defaults to nproc / hw.ncpu.
8183
8284
Positional arguments:
8385
FILE...
@@ -86,10 +88,6 @@ Positional arguments:
8688
to scope the run to a single file or a curated set, e.g.:
8789
./scripts/clang-tidy.sh <path-to-file.cpp>
8890
89-
Any other arguments are forwarded verbatim to run-clang-tidy. Common ones:
90-
-j N worker count (defaults to nproc / sysctl hw.ncpu)
91-
-checks=... override the .clang-tidy check list for this run
92-
9391
Examples:
9492
./scripts/clang-tidy.sh # full src/ tree
9593
./scripts/clang-tidy.sh <path-to-file.cpp> # single file
@@ -135,8 +133,8 @@ fi
135133
# not just on errors. Off by default so local edit/run cycles aren't blocked
136134
# by in-progress code; CI opts in via the workflow file.
137135
FAIL_ON_WARNING=0
136+
FIX_MODE=0
138137

139-
forward_args=()
140138
explicit_files=()
141139
while (($#)); do
142140
case "$1" in
@@ -146,10 +144,7 @@ while (($#)); do
146144
exit 0
147145
;;
148146
--fix)
149-
# Unify with clang-format.sh's --fix spelling. run-clang-tidy only
150-
# understands the single-dash -fix, so normalize every accepted form
151-
# to that when forwarding.
152-
forward_args+=("-fix")
147+
FIX_MODE=1
153148
shift
154149
;;
155150
--github-actions|--gh)
@@ -160,37 +155,45 @@ while (($#)); do
160155
FAIL_ON_WARNING=1
161156
shift
162157
;;
158+
-j)
159+
if [[ $# -lt 2 || ! "$2" =~ ^[1-9][0-9]*$ ]]; then
160+
echo "ERROR: -j requires a positive integer worker count." >&2
161+
usage >&2
162+
exit 2
163+
fi
164+
jobs="$2"
165+
shift 2
166+
;;
163167
--)
164-
# Explicit forwarding separator: everything after `--` goes straight
165-
# to run-clang-tidy without further interpretation.
168+
# Explicit positional separator: everything after `--` is a file path.
166169
shift
167-
forward_args+=("$@")
170+
explicit_files+=("$@")
168171
break
169172
;;
170-
--*)
171-
# Long options we don't recognize are user typos far more often than
172-
# genuine run-clang-tidy long options (run-clang-tidy uses single-dash
173-
# long options like -fix, -j, -checks=...). Reject and surface usage.
173+
--*|-*)
174174
echo "ERROR: unknown option: $1" >&2
175175
usage >&2
176176
exit 2
177177
;;
178178
*)
179-
# A positional arg naming an existing file is an explicit target to
180-
# analyze; anything else (e.g. the value after -j, a -checks=... flag)
181-
# is forwarded verbatim to run-clang-tidy. When explicit files are
182-
# given they replace the default src/ FILE_REGEX below so the run is
183-
# scoped to exactly those files.
184-
if [[ -f "$1" ]]; then
185-
explicit_files+=("$1")
186-
else
187-
forward_args+=("$1")
188-
fi
179+
explicit_files+=("$1")
189180
shift
190181
;;
191182
esac
192183
done
193184

185+
for path in "${explicit_files[@]}"; do
186+
if [[ -f "${path}" ]]; then
187+
continue
188+
fi
189+
if [[ -e "${path}" ]]; then
190+
echo "ERROR: clang-tidy target is not a regular file: ${path}" >&2
191+
else
192+
echo "ERROR: clang-tidy target does not exist: ${path}" >&2
193+
fi
194+
exit 2
195+
done
196+
194197
if [[ ! -f "${BUILD_DIR}/compile_commands.json" ]]; then
195198
echo "ERROR: ${BUILD_DIR}/compile_commands.json not found." >&2
196199
echo "Run: ./build.sh release (configures + builds, generates protobuf headers)" >&2
@@ -248,10 +251,17 @@ fi
248251
# coreutils tool; macOS doesn't ship it, so fall back to `sysctl hw.ncpu`,
249252
# and finally to a conservative 4 if neither is available (e.g. a minimal
250253
# container).
251-
if command -v nproc >/dev/null 2>&1; then
252-
jobs=$(nproc)
253-
else
254-
jobs=$(sysctl -n hw.ncpu 2>/dev/null || echo 4)
254+
if [[ -z "${jobs:-}" ]]; then
255+
if command -v nproc >/dev/null 2>&1; then
256+
jobs=$(nproc)
257+
else
258+
jobs=$(sysctl -n hw.ncpu 2>/dev/null || echo 4)
259+
fi
260+
fi
261+
262+
tidy_args=()
263+
if (( FIX_MODE == 1 )); then
264+
tidy_args+=("-fix")
255265
fi
256266

257267
# -------- Begin GitHub Actions annotations --------
@@ -536,7 +546,7 @@ PYTHONUNBUFFERED=1 run-clang-tidy \
536546
-quiet \
537547
-j "${jobs}" \
538548
${extra_args[@]+"${extra_args[@]}"} \
539-
${forward_args[@]+"${forward_args[@]}"} \
549+
${tidy_args[@]+"${tidy_args[@]}"} \
540550
"${tidy_targets[@]}" \
541551
2>&1 | tee "${log}"
542552
rc="${PIPESTATUS[0]}"

0 commit comments

Comments
 (0)