Skip to content

Commit 1fc7300

Browse files
authored
Merge pull request DeusData#2499 from ankitbuilds99/fix/lint-mem-gate-warning-suffix
fix(ci): parse clang-tidy analyzer warning suffix
2 parents f031c30 + 714a0cf commit 1fc7300

3 files changed

Lines changed: 117 additions & 1 deletion

File tree

‎scripts/lint-mem-gate.py‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,15 @@
4747
MIN_WHY = 120
4848
MIN_TRIED = 40
4949

50-
FINDING_RE = re.compile(r"^(?P<file>[^:]+):(?P<line>\d+):\d+:\s+(?:error|warning):\s+(?P<msg>.*?)\s*\[(?P<check>[\w.-]+)\]\s*$")
50+
# With WarningsAsErrors enabled, clang-tidy appends the escalation reason to
51+
# analyzer checks, for example:
52+
# [clang-analyzer-core.NullDereference,-warnings-as-errors]
53+
# Keep the check name separate from that suffix so the gate still matches the
54+
# same whitelist key instead of silently dropping the finding.
55+
FINDING_RE = re.compile(
56+
r"^(?P<file>[^:]+):(?P<line>\d+):\d+:\s+(?:error|warning):\s+"
57+
r"(?P<msg>.*?)\s*\[(?P<check>[\w.-]+)(?:,-warnings-as-errors)?\]\s*$"
58+
)
5159

5260

5361
def strip_noise(text):

‎scripts/test.sh‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,9 @@ bash "$ROOT/tests/test_activation_diagnostic_contract.sh"
295295
echo "=== Step 0e3: security gate fail-closed contract ==="
296296
bash "$ROOT/tests/test_security_gate_fail_closed.sh"
297297

298+
echo "=== Step 0e4: memory-analyzer gate warning-suffix contract (#2442) ==="
299+
bash "$ROOT/tests/test_lint_mem_gate.sh"
300+
298301
echo "=== Step 0f: tree-sitter runtime Makefile dependencies ==="
299302
bash "$ROOT/tests/test_makefile_ts_runtime_dependencies.sh"
300303

‎tests/test_lint_mem_gate.sh‎

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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

Comments
 (0)