|
| 1 | +#!/usr/bin/env bash |
| 2 | +# |
| 3 | +# check-warning-suppressions.sh — guard against file-scoped GCC warning |
| 4 | +# suppressions in the library sources flagged by TASK-060. |
| 5 | +# |
| 6 | +# The audit in specs/tasks/v2-branch-gap-audit.md §1 ("HIGH — Unscoped |
| 7 | +# warning suppressions") singled out two `#pragma GCC diagnostic |
| 8 | +# ignored "-Warray-bounds"` directives that sat at file scope and |
| 9 | +# silenced the warning for entire translation units. TASK-060 either |
| 10 | +# removes them or scopes them to a `push`/`pop` block; this gate makes |
| 11 | +# the regression visible by failing if any TU in src/ grows a new |
| 12 | +# file-scoped suppression. |
| 13 | +# |
| 14 | +# Watched files: all .cpp files under src/, discovered at runtime via |
| 15 | +# `find`. This is safe because at the time TASK-060 landed no TU in |
| 16 | +# src/ carried an unscoped -Warray-bounds pragma; the broad scan ensures |
| 17 | +# that future work which introduces new TUs is also guarded without |
| 18 | +# needing to remember to update a static list. |
| 19 | +# |
| 20 | +# Detection logic: |
| 21 | +# 1. Any `#pragma GCC diagnostic ignored "-Warray-bounds"` at the |
| 22 | +# beginning of a line is a candidate violation. |
| 23 | +# 2. A candidate is allowed iff it sits between a matching |
| 24 | +# `#pragma GCC diagnostic push` and `#pragma GCC diagnostic pop` |
| 25 | +# pair (push must appear earlier in the file, pop must appear |
| 26 | +# later). Conditional compilation around the push/pop is fine — |
| 27 | +# we only care about the textual ordering. |
| 28 | +# 3. Any candidate that fails the push/pop bracketing check is |
| 29 | +# reported and the script exits 1. |
| 30 | +# |
| 31 | +# Known limitation: the push/pop check uses "nearest push before" / |
| 32 | +# "nearest pop after" heuristics. An interleaved pattern such as |
| 33 | +# push@5, pop@8, pragma@10, pop@15 would be incorrectly allowed because |
| 34 | +# push_before=5 (non-zero) and pop_after=15 (non-zero). This shape is |
| 35 | +# not present in the codebase and is extremely unlikely in practice; if |
| 36 | +# the project ever adopts nested or interleaved push/pop patterns, |
| 37 | +# upgrade the detection to track bracket depth with a counter. |
| 38 | +# |
| 39 | +# Exit codes: |
| 40 | +# 0 no violations |
| 41 | +# 1 one or more watched files carries a file-scoped suppression |
| 42 | +set -euo pipefail |
| 43 | + |
| 44 | +REPO_ROOT="$(cd "$(dirname "$0")/.." && pwd)" |
| 45 | +cd "$REPO_ROOT" |
| 46 | + |
| 47 | +# Discover all .cpp files under src/ at runtime so new TUs are |
| 48 | +# automatically included without requiring a manual list update. |
| 49 | +# Use a while-read loop for bash 3.x / macOS compatibility (no mapfile). |
| 50 | +WATCHED_FILES=() |
| 51 | +while IFS= read -r f; do |
| 52 | + WATCHED_FILES+=("$f") |
| 53 | +done < <(find src -name '*.cpp' | sort) |
| 54 | + |
| 55 | +echo "check-warning-suppressions: scanning ${#WATCHED_FILES[@]} file(s)" |
| 56 | + |
| 57 | +violations=0 |
| 58 | +for file in "${WATCHED_FILES[@]}"; do |
| 59 | + if [ ! -f "$file" ]; then |
| 60 | + echo " $file: missing — watched file no longer present" >&2 |
| 61 | + violations=$((violations + 1)) |
| 62 | + continue |
| 63 | + fi |
| 64 | + |
| 65 | + # Each line that begins with the warning-suppression pragma is a |
| 66 | + # candidate. We then verify it is bracketed by push/pop. |
| 67 | + while IFS=: read -r lineno _; do |
| 68 | + # Single-pass awk: find the nearest push before and pop after |
| 69 | + # the candidate line in one read of the file. |
| 70 | + read -r push_before pop_after < <(awk -v target="$lineno" ' |
| 71 | + /^#pragma[[:space:]]+GCC[[:space:]]+diagnostic[[:space:]]+push/ { |
| 72 | + if (NR < target) last_push = NR |
| 73 | + } |
| 74 | + /^#pragma[[:space:]]+GCC[[:space:]]+diagnostic[[:space:]]+pop/ { |
| 75 | + if (NR > target && first_pop == 0) first_pop = NR |
| 76 | + } |
| 77 | + END { print (last_push ? last_push : 0), (first_pop ? first_pop : 0) } |
| 78 | + ' "$file") |
| 79 | + |
| 80 | + if [ "$push_before" = "0" ] || [ "$pop_after" = "0" ]; then |
| 81 | + echo " $file:$lineno: file-scoped #pragma GCC diagnostic ignored \"-Warray-bounds\" (not bracketed by push/pop)" >&2 |
| 82 | + violations=$((violations + 1)) |
| 83 | + fi |
| 84 | + done < <(grep -nE '^#pragma GCC diagnostic ignored "-Warray-bounds"' "$file" || true) |
| 85 | +done |
| 86 | + |
| 87 | +if [ "$violations" -gt 0 ]; then |
| 88 | + echo "check-warning-suppressions: FAIL — $violations file-scoped suppression(s) found" >&2 |
| 89 | + echo " scope each pragma with #pragma GCC diagnostic push / pop and a comment naming the GCC version range" >&2 |
| 90 | + exit 1 |
| 91 | +fi |
| 92 | + |
| 93 | +echo "check-warning-suppressions: PASS — no file-scoped -Warray-bounds suppressions in watched files" |
0 commit comments