strawberry/scripts/analyze_changed.sh
trvon 09e6b441ae
Some checks failed
Build and Test / build (debug, clang) (push) Has been cancelled
Build and Test / build (debug, gcc) (push) Has been cancelled
Build and Test / build (release, clang) (push) Has been cancelled
Build and Test / build (release, gcc) (push) Has been cancelled
Build and Test / macos (push) Has been cancelled
Build and Test / docker (push) Has been cancelled
Audit flow: add the clang static analyzer as a strict per-change gate
- scripts/analyze_changed.sh + `make analyze`: clang-check --analyze over the
  changed TUs via builddir/compile_commands.json (host build may be gcc; clang
  tolerates those flags), nice -n 19, 2 at a time. Strict by default (any
  analyzer diagnostic fails), ANALYZE_STRICT=0 reports only; ANALYZE_FILES/--all
  override selection. Results in build/audit/out/analyze.log.
- AGENTS.md "Per-change checks": new analyzer row, positioned as path-sensitive
  intra-TU analysis that complements fold's bounded symbolic cross-call pass and
  the syntactic invariants (run all three); YAMS index-at-checkpoint reminder.
- CLAUDE.md: analyzer added to the checks pointer + a Performance & C++ bullet
  (perf-kpi + Tracy/PerfStats).
- docs/prompts/PROMPT-strawberry-audit.md: "Clang static analyzer" subsection
  under Symbolic SAST.

No C++ changed; unit suite 50/50 green.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-10-28 09:55:03 -06:00

75 lines
3.2 KiB
Bash
Executable file

#!/usr/bin/env bash
# Run the clang static analyzer (clang-check --analyze, the Clang Static Analyzer
# engine) on the C++ translation units changed relative to a git base
# (default: HEAD, i.e. uncommitted changes; pass a ref e.g. `main`, or `--all`
# for the whole tree). Part of the per-change gate alongside clang-tidy and fold
# (see AGENTS.md "Per-change checks" and docs/prompts/PROMPT-strawberry-audit.md).
#
# It reads builddir's compile_commands.json, so the host build may be gcc; clang
# tolerates those flags. Strict by default: any analyzer diagnostic fails the run
# (set ANALYZE_STRICT=0 to report only). Capped for this workstation (nice 19,
# 2 translation units at a time). Every finding is verified in source before it
# is fixed or reported, and the triage is stored in YAMS (source=evidence).
#
# scripts/analyze_changed.sh # uncommitted changes
# scripts/analyze_changed.sh main # changes since main
# scripts/analyze_changed.sh --all # every TU (slow)
# ANALYZE_FILES="src/foo.cpp src/bar.cpp" scripts/analyze_changed.sh
# ANALYZE_STRICT=0 scripts/analyze_changed.sh # report, do not fail
# Results: build/audit/out/analyze.log
set -euo pipefail
ROOT="$(cd "$(dirname "$0")/.." && pwd)"
cd "$ROOT"
BUILD="${BUILDDIR:-builddir}"
OUT="$ROOT/build/audit/out"
mkdir -p "$OUT"
[ -f "$BUILD/compile_commands.json" ] || {
echo "analyze: $BUILD/compile_commands.json missing; run 'meson setup $BUILD' first" >&2
exit 2
}
command -v clang-check >/dev/null 2>&1 || {
echo "analyze: clang-check not found (install clang / clang-tools-extra)" >&2
exit 2
}
if [ -n "${ANALYZE_FILES:-}" ]; then
FILES="$ANALYZE_FILES"
elif [ "${1:-}" = "--all" ]; then
FILES=$(git ls-files -- 'src/*.cpp' 'mcp/*.cpp' 'app/*.cpp' | tr '\n' ' ')
else
BASE="${1:-HEAD}"
FILES=$({ git diff --name-only "$BASE" -- 'src/*.cpp' 'mcp/*.cpp' 'app/*.cpp'; \
git ls-files --others --exclude-standard -- 'src/*.cpp' 'mcp/*.cpp' 'app/*.cpp'; } \
| sort -u | tr '\n' ' ')
fi
# normalize + drop files that no longer exist (deletions); a for-loop so an
# empty list or a missing file can never trip `set -e`/pipefail.
_filtered=""
for f in $FILES; do [ -f "$f" ] && _filtered="$_filtered $f"; done
FILES=$(printf '%s\n' $_filtered | sort -u | tr '\n' ' ')
if [ -z "${FILES// }" ]; then
echo "analyze: no changed C++ translation units vs ${BASE:-HEAD}"
exit 0
fi
echo "analyze: $(printf '%s\n' $FILES | grep -c .) translation unit(s), strict=${ANALYZE_STRICT:-1}"
log="$OUT/analyze.log"; : > "$log"
run_one() {
local tu="$1" build="$2"
# Drop clang-check's "[n/n] Processing file" progress noise; keep diagnostics.
nice -n 19 clang-check -p "$build" --analyze "$tu" 2>&1 | grep -v 'Processing file' || true
}
export -f run_one
# -P2: workstation cap (AGENTS.md: -j2, nice 19).
printf '%s\n' $FILES | xargs -P2 -I{} bash -c 'run_one "$@"' _ {} "$BUILD" | tee "$log"
findings=$(grep -cE ': (warning|error): ' "$log" 2>/dev/null || true)
findings=${findings:-0}
echo "analyze: ${findings} analyzer diagnostic line(s) -> $log"
if [ "$findings" -gt 0 ]; then
echo "verify each in source before fixing or reporting; store triage in YAMS (source=evidence)"
[ "${ANALYZE_STRICT:-1}" = "0" ] || exit 1
fi