#!/usr/bin/env bash # Score lucisv against the corpus: one JSON line on stdout. # # {"passing":N,"total":N,"pct":P,"failures_by_category":{"":N,...}} # # A file *passes* when the formatter accepts it (`lucisv --check` exits 0 or 1 -- # i.e. it parsed, and the runtime safety net verified the output) AND the # linter runs over it without an internal error (`lucisv check` exits 0 or 1). # Exit 2 or a signal death on either side is a failure. The corpus is never # written to (only --check / check are used, with --no-cache). # # Failures are bucketed into the categories the coverage loop works on; the # classifier is message-driven, with a look at the offending source line for # the ambiguous "expected `;`" / "expected expression" diagnostics. A category # name is stable even when its count is zero -- zero-count categories are # simply absent from the JSON. # # Usage: scripts/score.sh [corpus-dir] [-b lucisv-binary] [-j jobs] [-o details.tsv] # details.tsv: outcomecategoryfilemessage, one row per file # (default .score-results.tsv, gitignored). set -eu corpus_dir=corpus lucisv=target/release/lucisv jobs=$( (sysctl -n hw.ncpu 2>/dev/null || nproc 2>/dev/null || echo 4) ) details=.score-results.tsv while [ $# -gt 0 ]; do case $1 in -b) lucisv=$2; shift 2 ;; -j) jobs=$2; shift 2 ;; -o) details=$2; shift 2 ;; -*) echo "usage: $0 [corpus-dir] [-b lucisv] [-j jobs] [-o details.tsv]" >&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; } # --- per-file classification -------------------------------------------------- # classify FILE LINE MESSAGE -> category name on stdout. classify() { file=$1 line=$2 msg=$3 src= if [ -n "$line" ] && [ "$line" -gt 0 ] 2>/dev/null; then src=$(sed -n "${line}p" "$file" 2>/dev/null || true) fi # Context: the source line plus the one before (some constructs put the # blocking token on a continuation line, e.g. `bind X\n mod inst (`). ctx=$src if [ -n "$line" ] && [ "$line" -gt 1 ] 2>/dev/null; then ctx="$(sed -n "$((line - 1))p" "$file" 2>/dev/null || true) $src" fi case $msg in *"verification failed"*) echo verification-error; return ;; esac case $file in *.tpl.sv) echo not-verilog; return ;; esac case $msg in *"keyword \`interface\`"*|*"keyword \`default\`"*|*"keyword \`clocking\`"*) echo interface; return ;; *"(found \"virtual\")"*) echo interface; return ;; *"keyword \`assert\`"*|*"keyword \`assume\`"*|*"keyword \`cover\`"*|*"keyword \`restrict\`"*) echo assertions; return ;; *"keyword \`sequence\`"*|*"keyword \`property\`"*) echo assertions; return ;; *"(found \"restrict\")"*) echo assertions; return ;; *'\"DPI-C\"'*|*"keyword \`export\`"*) echo dpi-export; return ;; *"unsupported cast base"*|*"keyword \`weak0\`"*|*"keyword \`weak1\`"*|*"keyword \`pull0\`"*|*"keyword \`pull1\`"*) echo expressions; return ;; *"(found \"(* "*) echo expressions; return ;; *"keyword \`enum\`"*|*"(found \"struct\")"*|*"(found \"type\")"*|*"(found \"union\")"*) echo types-decls; return ;; *"(found \"\$error\")"*|*"(found \"\$fatal\")"*|*"(found \"\$warning\")"*|*"(found \"\$info\")"*) echo elab-tasks; return ;; *"(found \"\`uvm_field_"*) echo macro-items; return ;; *"(found \"\`else\")"*|*"(found \"\`elsif\")"*|*"(found \"\`endif\")"*|*"(found \"\`ifdef\")"*|*"(found \"\`ifndef\")"*) echo directive-split; return ;; *"(found \"with\")"*|*"keyword \`randcase\`"*|*"keyword \`randsequence\`"*|*"keyword \`let\`"*) echo dv-statements; return ;; *"keyword \`import\`"*|*"keyword \`fork\`"*|*"keyword \`wait\`"*) echo dv-statements; return ;; "lex error:"*) case $ctx in *"'h\`"*|*"'d\`"*|*"'b\`"*|*"'o\`"*) echo lex-macro-number; return ;; esac echo lex-error; return ;; esac # Ambiguous diagnostics: decide from the source context. case $msg in *"keyword \`end\`"*) echo invalid-or-fragment; return ;; esac case $ctx in *"module \$"*|*"CUSTOM_"*) echo invalid-or-fragment; return ;; *"bind "*) echo bind; return ;; *"assert property"*|*"assume property"*|*"cover property"*|*"restrict property"*) echo assertions; return ;; *"assert ("*|*"assert("*) echo dv-statements; return ;; *"import \"DPI"*|*"export \"DPI"*) echo dpi-export; return ;; *"\`error"*|*"\`warning"*) echo macro-items; return ;; *"\`uvm_"*|*"\`"[A-Za-z_]*" ("*) echo macro-items; return ;; *"<<"*"{"*|*">>"*"{"*) echo expressions; return ;; *"->"*) echo expressions; return ;; *"inside \`"*) echo expressions; return ;; *"'{"*"{"*) echo expressions; return ;; *"::"*"["*) echo dv-statements; return ;; *"foreach"*|*"randcase"*|*"wait fork"*|*"let "*|*", ,"*|*"endtask"*|*"endfunction"*) echo dv-statements; return ;; *"clocking"*) echo interface; return ;; *"struct"*|*"enum"*|*"localparam type"*|*"parameter type"*) echo types-decls; return ;; *"_t ["*|*"_t ["*|*"for (int"*|*"for(int"*) echo types-decls; return ;; *"cross "*|*"coverpoint"*) echo directive-split; return ;; esac # A bare macro on its own line, followed by a declaration, is the # macro-as-type misparse (`uvm_component_new` then `foo_t x;`). prev= k=1 while [ "$k" -le 4 ] && [ -n "$line" ] && [ "$line" -gt "$k" ] 2>/dev/null; do cand=$(sed -n "$((line - k))p" "$file" 2>/dev/null | sed 's/^[[:space:]]*//') case $cand in ""|"//"*) k=$((k + 1)); continue ;; esac prev=$cand break done case $prev in "\`"[A-Za-z_]*) echo macro-items; return ;; esac echo other } # check_one FILE -> one TSV row: outcomecategoryfilemessage check_one() { file=$1 status=0 out=$("$lucisv" --no-cache --check "$file" 2>&1) || status=$? case $status in 0|1) lstatus=0 lout=$("$lucisv" check --no-cache "$file" 2>&1 >/dev/null) || lstatus=$? case $lstatus in 0|1) printf 'pass\t-\t%s\t\n' "$file" ;; 2) msg=$(printf '%s\n' "$lout" | grep '^error: ' | head -1 | tr '\t\n' ' ') printf 'fail\tlint-error\t%s\t%s\n' "$file" "${msg#error: }" ;; *) printf 'fail\tcrash\t%s\tlint exit status %s\n' "$file" "$lstatus" ;; esac ;; 2) msg=$(printf '%s\n' "$out" | grep '^error: ' | head -1) msg=${msg#error: } msg=${msg#"$file": } msg=$(printf '%s' "$msg" | tr '\t\n' ' ') line=$(printf '%s' "$msg" | sed -n 's/^[a-z]* error: \([0-9][0-9]*\):.*/\1/p') cat=$(classify "$file" "${line:-0}" "$msg") printf 'fail\t%s\t%s\t%s\n' "$cat" "$file" "$msg" ;; *) printf 'fail\tcrash\t%s\texit status %s\n' "$file" "$status" ;; esac } export -f classify check_one export lucisv tmp=$(mktemp) trap 'rm -f "$tmp"' EXIT find "$corpus_dir" -type f \( -name '*.v' -o -name '*.sv' \) -not -path '*/.git/*' | sort | xargs -P "$jobs" -I{} bash -c 'check_one "$1"' _ {} >"$tmp" sort -t"$(printf '\t')" -k3,3 "$tmp" >"$details" total=$(wc -l <"$details" | tr -d ' ') [ "$total" -gt 0 ] || { echo "no .v/.sv files found under $corpus_dir" >&2; exit 2; } passing=$(awk -F'\t' '$1=="pass"' "$details" | wc -l | tr -d ' ') awk -F'\t' -v passing="$passing" -v total="$total" ' $1 == "fail" { n[$2]++ } END { printf "{\"passing\":%d,\"total\":%d,\"pct\":%.1f,\"failures_by_category\":{", passing, total, 100 * passing / total # largest bucket first, name as tie-break, so diffs between runs are readable m = 0 for (k in n) { keys[m++] = k } for (i = 0; i < m; i++) for (j = i + 1; j < m; j++) if (n[keys[j]] > n[keys[i]] || (n[keys[j]] == n[keys[i]] && keys[j] < keys[i])) { t = keys[i]; keys[i] = keys[j]; keys[j] = t } for (i = 0; i < m; i++) printf "%s\"%s\":%d", (i ? "," : ""), keys[i], n[keys[i]] print "}}" }' "$details"