aboutsummaryrefslogtreecommitdiff
path: root/src/tools/xref_selftest.sh
diff options
context:
space:
mode:
authorGravatar zinc <zinc@downsta.ir> 2026-10-14 15:52:01 +0330
committerGravatar zinc <zinc@downsta.ir> 2026-10-14 15:52:01 +0330
commit4fc8f54a21f7a9d8712665501b40c3a52b3eede9 (patch)
treefcb16d3185c1f80fae8d54cd9062bd68d6c0c88d /src/tools/xref_selftest.sh
parentline tables and backtraces: port agent/line-tables onto main, op 56 on the x8... (diff)
parentmerge spec conformance suite (diff)
merge main into work/line-tables
Diffstat (limited to 'src/tools/xref_selftest.sh')
-rwxr-xr-xsrc/tools/xref_selftest.sh35
1 files changed, 35 insertions, 0 deletions
diff --git a/src/tools/xref_selftest.sh b/src/tools/xref_selftest.sh
new file mode 100755
index 0000000..e8a888b
--- /dev/null
+++ b/src/tools/xref_selftest.sh
@@ -0,0 +1,35 @@
+#!/bin/bash
+# self-test of src/tools/xref.sh --check: the checked-in baseline passes (exit 0); a copy of it
+# with one writer removed from one line fails (exit 1); and a scratch copy of src/compiler with one
+# new write to a global (namecount) fails and names that global. works in a scratch directory; the
+# tree, src/compiler, tests/xref.baseline and build/xref are not touched.
+# usage: src/tools/xref_selftest.sh [SERVER] (default build/bin/zclsp)
+set -u
+cd "$(dirname "$0")/../.."
+usage="usage: src/tools/xref_selftest.sh [SERVER] (default build/bin/zclsp)"
+case "${1:-}" in
+ -h|--help) echo "$usage"; exit 0;;
+ -*) echo "xref_selftest: unknown option $1; $usage" >&2; exit 2;;
+esac
+[ $# -le 1 ] || { echo "xref_selftest: too many arguments; $usage" >&2; exit 2; }
+server=${1:-build/bin/zclsp}
+tmp=$(mktemp -d); trap 'rm -rf "$tmp"' EXIT
+fail=0
+bash src/tools/xref.sh --server "$server" --out "$tmp/out" --check > "$tmp/ok.log" 2>&1; r=$?
+if [ $r = 0 ]; then echo "ok xref --check passes against tests/xref.baseline"
+else echo "FAIL xref --check against tests/xref.baseline: exit $r"; cat "$tmp/ok.log"; fail=1; fi
+# drop the last writer of the first global that has one: --check must report it as a new writer
+line=$(awk 'NF>=2{print NR; exit}' tests/xref.baseline)
+sed "${line}s/ [^ ]*\$//" tests/xref.baseline > "$tmp/baseline"
+bash src/tools/xref.sh --server "$server" --out "$tmp/out" --baseline "$tmp/baseline" --check > "$tmp/bad.log" 2>&1; r=$?
+if [ $r = 1 ] && grep -q 'is now written by' "$tmp/bad.log"; then echo "ok xref --check fails when the baseline misses a writer"
+else echo "FAIL xref --check with a writer removed from the baseline: exit $r (want 1)"; cat "$tmp/bad.log"; fail=1; fi
+# a real new write in a copy of the compiler: a new function that assigns namecount
+g=namecount
+mkdir "$tmp/compiler"; cp src/compiler/* "$tmp/compiler/"
+last=$(tr -d '\r' < "$tmp/compiler/parts" | tail -1)
+printf '\nint xref_selftest_probe() { %s = %s + 1; return 0; }\n' "$g" "$g" >> "$tmp/compiler/$last"
+bash src/tools/xref.sh --server "$server" --out "$tmp/out" --compiler-dir "$tmp/compiler" --check > "$tmp/src.log" 2>&1; r=$?
+if [ $r = 1 ] && grep -q "^ $g is now written by xref_selftest_probe" "$tmp/src.log"; then echo "ok xref --check fails and names $g when a copy of the compiler writes it in a new function"
+else echo "FAIL xref --check with a new write to $g in a copy of the compiler: exit $r (want 1 naming $g)"; cat "$tmp/src.log"; fail=1; fi
+exit $fail