|
| 1 | +#!/usr/bin/env bash |
| 2 | +# |
| 3 | +# warnings-baseline.sh -- advisory non-test build-warning reporter for t27. |
| 4 | +# |
| 5 | +# The #969 dead-code audit removes or annotates build warnings one conservative |
| 6 | +# slice at a time (see docs/NOW.md). This script gives that effort a memorable, |
| 7 | +# read-only progress meter: it builds the binary crate with JSON diagnostics, |
| 8 | +# counts the non-test compiler warnings, compares the count to a recorded |
| 9 | +# baseline, and prints the top offending source files so the next slice is easy |
| 10 | +# to pick. |
| 11 | +# |
| 12 | +# It is ADVISORY ONLY: it never edits code, never reseals anything, and is NOT |
| 13 | +# wired into CI (the required checks stay check-now-freshness / validate / |
| 14 | +# check / check-linked-issue). The baseline is a soft reference, not a gate. |
| 15 | +# |
| 16 | +# Exit codes (informational, never used to fail a required check): |
| 17 | +# 0 = warning count <= baseline (no regression) |
| 18 | +# 1 = warning count > baseline (more warnings than the recorded baseline) |
| 19 | +# 2 = build failed (could not produce diagnostics) |
| 20 | +# |
| 21 | +# Usage: |
| 22 | +# scripts/warnings-baseline.sh # full report + top modules |
| 23 | +# scripts/warnings-baseline.sh --quiet # one-line verdict only |
| 24 | +# |
| 25 | +# Anchor: phi^2 + phi^-2 = 3 |
| 26 | + |
| 27 | +set -uo pipefail |
| 28 | + |
| 29 | +# Recorded baseline of non-test build warnings on master. Update this number |
| 30 | +# (in the same PR) whenever a reviewed slice legitimately lowers it, so the |
| 31 | +# meter keeps tracking real progress. Measured precisely from the structured |
| 32 | +# JSON diagnostics on master after the host/mod.rs facade slice (#1111): 683. |
| 33 | +# (Earlier NOW.md entries quoted ~685/726 from the cargo summary line, which |
| 34 | +# rounds differently; this script's primary-span count is the canonical meter.) |
| 35 | +BASELINE=683 |
| 36 | + |
| 37 | +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 38 | +REPO_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)" |
| 39 | +cd "$REPO_ROOT" |
| 40 | + |
| 41 | +QUIET=0 |
| 42 | +if [ "${1:-}" = "--quiet" ]; then QUIET=1; fi |
| 43 | +log() { if [ "$QUIET" -eq 0 ]; then echo "$@"; fi; } |
| 44 | + |
| 45 | +# Prefer an explicit cargo if present; fall back to PATH. |
| 46 | +CARGO_BIN="cargo" |
| 47 | +if [ -x "$HOME/.cargo/bin/cargo" ]; then CARGO_BIN="$HOME/.cargo/bin/cargo"; fi |
| 48 | + |
| 49 | +# Capture JSON diagnostics. We do not redirect into the build target dir; the |
| 50 | +# caller's CARGO_TARGET_DIR (if any) is respected. stderr carries progress. |
| 51 | +JSON_OUT="$(mktemp)" |
| 52 | +trap 'rm -f "$JSON_OUT"' EXIT |
| 53 | + |
| 54 | +if ! "$CARGO_BIN" build --bin t27c --message-format=json >"$JSON_OUT" 2>/dev/null; then |
| 55 | + log "warnings-baseline: BUILD FAILED -- cannot count warnings." |
| 56 | + exit 2 |
| 57 | +fi |
| 58 | + |
| 59 | +# Count non-test warnings and tally the top files using the structured output. |
| 60 | +# A small python helper keeps the JSON parsing robust and dependency-free. |
| 61 | +REPORT="$(python3 - "$JSON_OUT" <<'PY' |
| 62 | +import json, sys, collections |
| 63 | +path = sys.argv[1] |
| 64 | +counts = collections.Counter() |
| 65 | +total = 0 |
| 66 | +with open(path, "r", encoding="utf-8", errors="replace") as fh: |
| 67 | + for line in fh: |
| 68 | + line = line.strip() |
| 69 | + if not line: |
| 70 | + continue |
| 71 | + try: |
| 72 | + m = json.loads(line) |
| 73 | + except Exception: |
| 74 | + continue |
| 75 | + if m.get("reason") != "compiler-message": |
| 76 | + continue |
| 77 | + msg = m.get("message", {}) |
| 78 | + if msg.get("level") != "warning": |
| 79 | + continue |
| 80 | + text = msg.get("message", "") |
| 81 | + # Skip the cargo summary line ("`t27c` generated N warnings"). |
| 82 | + if text.startswith("`t27c`"): |
| 83 | + continue |
| 84 | + total += 1 |
| 85 | + f = None |
| 86 | + for s in msg.get("spans", []): |
| 87 | + if s.get("is_primary"): |
| 88 | + f = s.get("file_name") |
| 89 | + break |
| 90 | + if f is None: |
| 91 | + spans = msg.get("spans", []) |
| 92 | + if spans: |
| 93 | + f = spans[0].get("file_name") |
| 94 | + counts[f or "<unknown>"] += 1 |
| 95 | +print(total) |
| 96 | +for f, n in counts.most_common(10): |
| 97 | + print(f"{n}\t{f}") |
| 98 | +PY |
| 99 | +)" |
| 100 | + |
| 101 | +TOTAL="$(printf '%s\n' "$REPORT" | head -n 1)" |
| 102 | +TOPS="$(printf '%s\n' "$REPORT" | tail -n +2)" |
| 103 | + |
| 104 | +if ! printf '%s' "$TOTAL" | grep -qE '^[0-9]+$'; then |
| 105 | + log "warnings-baseline: could not parse warning count." |
| 106 | + exit 2 |
| 107 | +fi |
| 108 | + |
| 109 | +if [ "$TOTAL" -gt "$BASELINE" ]; then |
| 110 | + VERDICT="REGRESSED ($TOTAL > baseline $BASELINE)" |
| 111 | + CODE=1 |
| 112 | +else |
| 113 | + VERDICT="OK ($TOTAL <= baseline $BASELINE)" |
| 114 | + CODE=0 |
| 115 | +fi |
| 116 | + |
| 117 | +if [ "$QUIET" -eq 1 ]; then |
| 118 | + echo "warnings-baseline: $VERDICT" |
| 119 | + exit "$CODE" |
| 120 | +fi |
| 121 | + |
| 122 | +echo "================================================================" |
| 123 | +echo " t27 non-test build warnings (advisory; #969 dead-code progress)" |
| 124 | +echo "================================================================" |
| 125 | +echo " count : $TOTAL" |
| 126 | +echo " baseline : $BASELINE" |
| 127 | +echo " verdict : $VERDICT" |
| 128 | +echo "----------------------------------------------------------------" |
| 129 | +echo " top files by warning count:" |
| 130 | +printf '%s\n' "$TOPS" | while IFS=$'\t' read -r n f; do |
| 131 | + [ -n "$n" ] && printf ' %5s %s\n' "$n" "$f" |
| 132 | +done |
| 133 | +echo "----------------------------------------------------------------" |
| 134 | +if [ "$CODE" -eq 0 ] && [ "$TOTAL" -lt "$BASELINE" ]; then |
| 135 | + echo " note: count is BELOW baseline -- if a reviewed slice lowered it," |
| 136 | + echo " update BASELINE in this script (currently $BASELINE) in the same PR." |
| 137 | +fi |
| 138 | +echo " advisory only: never edits code, never gates CI." |
| 139 | +exit "$CODE" |
0 commit comments