Files
lucisv/tools/corpus_check.sh

145 lines
5.3 KiB
Bash
Executable File

#!/usr/bin/env bash
# Run lucisv over a corpus of real-world RTL and report parse/format health.
#
# This is lucisv's answer to ruff's "ecosystem check": point it at a tree of
# third-party Verilog and it says how much of it we can parse, how much is
# already format-clean, and which errors dominate. It never writes to the
# corpus -- only --check is used.
#
# Usage: tools/corpus_check.sh [corpus-dir] [-o results.tsv] [-t TOP] [-b lucisv]
#
# Exit status: 0 if no verification errors and no crashes were seen,
# 1 otherwise (those two outcomes are always formatter bugs).
set -eu
corpus_dir=corpus
lucisv=target/release/lucisv
results=corpus-check-results.tsv
top=20
while [ $# -gt 0 ]; do
case $1 in
-o) results=$2; shift 2 ;;
-t) top=$2; shift 2 ;;
-b) lucisv=$2; shift 2 ;;
-*) echo "usage: $0 [corpus-dir] [-o out.tsv] [-t top-n] [-b lucisv-binary]" >&2; exit 2 ;;
*) corpus_dir=$1; shift ;;
esac
done
[ -x "$lucisv" ] || { echo "no lucisv binary at $lucisv (cargo build --release?)" >&2; exit 2; }
[ -d "$corpus_dir" ] || { echo "no corpus at $corpus_dir (tools/corpus_fetch.sh?)" >&2; exit 2; }
# Classify one file. lucisv --check exit codes (frozen contract, see README):
# 0 clean, 1 would reformat, 2 error. Anything else is a crash -- a signal
# death (128+n) or an abort the per-file catch_unwind could not contain.
check_one() {
file=$1
status=0
out=$("$lucisv" --check "$file" 2>&1) || status=$?
msg=$(printf '%s\n' "$out" | grep '^error: ' | head -1)
msg=${msg#error: }
msg=${msg#"$file": }
# A diagnostic can embed raw source text (e.g. a verification-error's
# comment payload) that itself contains a literal tab or newline -- see
# QA log 2026-08-19. Squash those before the TSV write below, or a stray
# tab silently splits this row into 4+ fields for every downstream
# awk -F'\t'/cut -f consumer.
msg=$(printf '%s' "$msg" | tr '\t\n' ' ')
case $status in
0) outcome=ok-unchanged; msg= ;;
1) outcome=would-reformat; msg= ;;
2)
case $msg in
*"verification failed"*) outcome=verification-error ;;
"parse error:"*|"lex error:"*) outcome=parse-error ;;
*) outcome=other-error ;;
esac
;;
*) outcome=crash; msg="exit status $status ${msg:-(no diagnostic)}" ;;
esac
printf '%s\t%s\t%s\n' "$outcome" "$file" "$msg"
}
echo "lucisv: $lucisv"
echo "corpus: $corpus_dir"
echo
: >"$results"
find "$corpus_dir" -type f \( -name '*.v' -o -name '*.sv' \) -not -path '*/.git/*' |
sort |
while IFS= read -r f; do
check_one "$f" >>"$results"
# A crash kills the child, not us; keep going so the report is complete.
done
total=$(wc -l <"$results" | tr -d ' ')
[ "$total" -gt 0 ] || { echo "no .v/.sv files found under $corpus_dir" >&2; exit 2; }
count() { grep -c "^$1 " "$results" || true; }
pct() { awk -v n="$1" -v d="$2" 'BEGIN { printf "%.1f", d ? 100 * n / d : 0 }'; }
ok=$(count ok-unchanged)
reformat=$(count would-reformat)
parse=$(count parse-error)
verify=$(count verification-error)
other=$(count other-error)
crash=$(count crash)
formatted=$((ok + reformat))
echo "outcome files share"
echo "-------------------- ----- -------"
for pair in "ok-unchanged $ok" "would-reformat $reformat" "parse-error $parse" \
"verification-error $verify" "other-error $other" "crash $crash"; do
# shellcheck disable=SC2086 # deliberate split of "name count" pairs
set -- $pair
printf '%-20s %5d %5s%%\n' "$1" "$2" "$(pct "$2" "$total")"
done
echo "-------------------- ----- -------"
printf '%-20s %5d %5s%%\n' "total" "$total" 100.0
echo
echo "parsed & formattable: $formatted/$total ($(pct "$formatted" "$total")%)"
echo "already format-clean: $ok/$total ($(pct "$ok" "$total")%)"
echo " (of formattable: $(pct "$ok" "$formatted")%)"
# Error breakdown. Source positions are stripped so the same diagnostic from
# different files collapses into one bucket; double-quoted literals are
# collapsed for the same reason (backticked keywords are kept -- they are
# what names the unsupported construct).
echo
echo "top $top error messages (positions and quoted literals normalized):"
awk -F'\t' '$3 != "" { print $3 }' "$results" |
sed -e 's/[0-9][0-9]*:[0-9][0-9]*: /L:C: /' -e 's/"[^"]*"/"…"/g' |
sort | uniq -c | sort -rn | head -"$top" |
awk '{ n = $1; $1 = ""; sub(/^ /, ""); printf "%6d %s\n", n, $0 }'
# The parser stops at the first error, so each file votes exactly once: this
# is "which token blocks the most files", i.e. the unsupported-construct
# ranking to feed back into README.md's roadmap.
echo
echo "top $top blocking tokens (the \"found\" token of each parse error):"
awk -F'\t' '$3 != "" { print $3 }' "$results" |
sed -n 's/.*(found "\(.*\)").*/\1/p' |
cut -c1-60 | sort | uniq -c | sort -rn | head -"$top" |
awk '{ n = $1; $1 = ""; sub(/^ /, ""); printf "%6d %s\n", n, $0 }'
# Verification errors and crashes are never "unsupported syntax" -- the
# runtime safety net guarantees equivalence and convergence for anything
# that formats, so each of these is a formatter bug. List them all.
for kind in verification-error crash; do
n=$(count "$kind")
echo
if [ "$n" -eq 0 ]; then
echo "$kind: none"
else
echo "$kind ($n) -- these are formatter bugs, not unsupported syntax:"
awk -F'\t' -v k="$kind" '$1 == k { printf " %s\n %s\n", $2, $3 }' "$results"
fi
done
echo
echo "per-file results: $results"
[ "$verify" -eq 0 ] && [ "$crash" -eq 0 ]