|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Regression contract for clang-tidy's `,-warnings-as-errors` diagnostic suffix |
| 3 | +# (#2442). With WarningsAsErrors enabled, clang-tidy prints an analyzer check as |
| 4 | +# [clang-analyzer-<check>,-warnings-as-errors]; a gate that only accepts |
| 5 | +# [<check>] drops every such line and reports a clean gate. Both directions are |
| 6 | +# pinned, and every assertion is reported, so a regression names what broke: |
| 7 | +# 1. a suffixed finding WITH an argued whitelist entry is counted under its |
| 8 | +# plain check name and suppressed (exit 0, "1 argued false positive"); |
| 9 | +# 2. a suffixed finding WITHOUT an entry fails the gate (exit 1) and is |
| 10 | +# reported under its plain check name. This is the negative assertion: a |
| 11 | +# parser that drops the suffixed line reports clean (exit 0) here. |
| 12 | +# Reverting the suffix group in FINDING_RE (scripts/lint-mem-gate.py) turns |
| 13 | +# both red. |
| 14 | +# |
| 15 | +# The gate runs from inside the fixture directory with RELATIVE paths. On the |
| 16 | +# Windows leg python3 is a native MSYS2 build: MSYS converts path ARGUMENTS for |
| 17 | +# it, but never the diagnostic text on stdin, so an absolute /c/... path in a |
| 18 | +# finding would not resolve there, and a C:/... one would not parse (the file |
| 19 | +# field ends at the first colon). |
| 20 | +set -euo pipefail |
| 21 | + |
| 22 | +ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" |
| 23 | +GATE="$ROOT/scripts/lint-mem-gate.py" |
| 24 | +FIX="$(mktemp -d "${TMPDIR:-/tmp}/lint-mem-gate.XXXXXX")" |
| 25 | +trap 'rm -rf "$FIX"' EXIT |
| 26 | + |
| 27 | +failures=0 |
| 28 | +fail() { |
| 29 | + echo "FAIL: $*" >&2 |
| 30 | + failures=$((failures + 1)) |
| 31 | +} |
| 32 | + |
| 33 | +show() { |
| 34 | + sed 's/^/ | /' "$1" >&2 |
| 35 | +} |
| 36 | + |
| 37 | +# A subshell, so the test's own shell never sits inside $FIX (Windows cannot |
| 38 | +# remove a directory that is a process's working directory). |
| 39 | +run_gate() { |
| 40 | + (cd "$FIX" && LINT_MEM_WHITELIST=whitelist.txt python3 "$GATE" "$@") |
| 41 | +} |
| 42 | + |
| 43 | +cat > "$FIX/sample.c" <<'EOF' |
| 44 | +int sample(int *value) { |
| 45 | + return *value; |
| 46 | +} |
| 47 | +
|
| 48 | +int unlisted(int *value) { |
| 49 | + return *value; |
| 50 | +} |
| 51 | +EOF |
| 52 | + |
| 53 | +# A native Windows python3 writes CRLF; strip it before the hash is reused. |
| 54 | +HASH="$(run_gate --hash sample.c sample | tr -d '\r' | awk '/^segment-sha256:/ {print $2}')" |
| 55 | +if [[ ! "$HASH" =~ ^[0-9a-f]{64}$ ]]; then |
| 56 | + echo "FAIL: lint-mem-gate.py --hash sample.c sample printed no segment hash (got '$HASH')" >&2 |
| 57 | + exit 1 |
| 58 | +fi |
| 59 | + |
| 60 | +cat > "$FIX/whitelist.txt" <<EOF |
| 61 | +## sample.c :: sample :: clang-analyzer-core.NullDereference |
| 62 | +segment-sha256: $HASH |
| 63 | +why: | |
| 64 | + The test deliberately models a diagnostic that is suppressed by the fixture whitelist. |
| 65 | + This text is long enough to exercise the gate's normal argument validation path. |
| 66 | +tried: | |
| 67 | + The fixture is intentionally minimal and only verifies diagnostic parsing. |
| 68 | +EOF |
| 69 | + |
| 70 | +SUFFIX='[clang-analyzer-core.NullDereference,-warnings-as-errors]' |
| 71 | + |
| 72 | +# 1. Whitelisted: counted under the plain check name, then suppressed. |
| 73 | +rc=0 |
| 74 | +printf '%s\n' "sample.c:2:12: warning: possible null dereference $SUFFIX" \ |
| 75 | + | run_gate > /dev/null 2> "$FIX/whitelisted.err" || rc=$? |
| 76 | +if [ "$rc" -ne 0 ]; then |
| 77 | + fail "a whitelisted suffixed finding left the gate at exit $rc, expected 0" |
| 78 | + show "$FIX/whitelisted.err" |
| 79 | +fi |
| 80 | +if ! grep -q 'memory gate clean (1 argued false positive' "$FIX/whitelisted.err"; then |
| 81 | + fail "a whitelisted suffixed finding was not counted as 1 argued false positive" |
| 82 | + show "$FIX/whitelisted.err" |
| 83 | +fi |
| 84 | + |
| 85 | +# 2. Not whitelisted: the gate must fail and name the finding. |
| 86 | +rc=0 |
| 87 | +printf '%s\n' "sample.c:6:12: warning: possible null dereference $SUFFIX" \ |
| 88 | + | run_gate > /dev/null 2> "$FIX/unlisted.err" || rc=$? |
| 89 | +if [ "$rc" -ne 1 ]; then |
| 90 | + fail "an unwhitelisted suffixed finding left the gate at exit $rc, expected 1" \ |
| 91 | + "(exit 0 = the suffixed line was parsed as no finding, #2442)" |
| 92 | + show "$FIX/unlisted.err" |
| 93 | +fi |
| 94 | +if ! grep -qF 'sample.c:6: possible null dereference [clang-analyzer-core.NullDereference]' "$FIX/unlisted.err" || |
| 95 | + ! grep -qF 'no whitelist entry for unlisted()' "$FIX/unlisted.err" || |
| 96 | + ! grep -qF '1 finding(s) unaccounted for' "$FIX/unlisted.err"; then |
| 97 | + fail "the gate did not report the unwhitelisted suffixed finding under its plain check name" |
| 98 | + show "$FIX/unlisted.err" |
| 99 | +fi |
| 100 | + |
| 101 | +if [ "$failures" -ne 0 ]; then |
| 102 | + echo "lint-mem-gate warning suffix contract: $failures assertion(s) failed" >&2 |
| 103 | + exit 1 |
| 104 | +fi |
| 105 | +echo 'lint-mem-gate warning suffix contract passed (whitelisted + unwhitelisted suffixed findings)' |
0 commit comments