Files
lucisv/crates/vlog_semantic/tests/corpus.rs
T
Guoliang ChenandClaude Mythos 5.2 71d13b5fbe
ci / checks (push) Successful in 1m0s
ci / corpus (push) Successful in 18s
ci / linux (push) Successful in 6m4s
release / build-and-publish (push) Successful in 5m31s
Release 0.66.2: every source file names its author
Every tracked file that can carry a comment (880 of them) now opens with
"Author: Guoliang Chen, a Senior Digital Design Engineer" and closes with
"Author: Guoliang Chen. Lucisv is made by Guoliang Chen with love,
luciochen.com", in the file's own comment syntax. No behaviour change: the
diff outside the version bump and changelogs is comment lines only.

- tools/stamp_author.py: writes the lines; idempotent, `--check` lists
  what is missing. Headers go under a shebang and under Markdown front
  matter.
- Left alone: test fixtures, cases and snapshots (byte-exact inputs), JSON
  and JSONL, logs, images, lockfiles, docs/site/dist (regenerated) and the
  LICENSE files (already carry the copyright line).
- tools/linux_check.sh: --help reads its header from line 3, now that the
  author line sits under the shebang.
- tools/gates.sh green on this tree: 5 545 tests, corpus 814/6 as before.

Co-Authored-By: Claude Mythos 5.2 <[email protected]>
2026-11-24 12:11:09 -08:00

139 lines
4.6 KiB
Rust

// Author: Guoliang Chen, a Senior Digital Design Engineer
//! Smoke test over the real-RTL corpus (`corpus/`, fetched by
//! `tools/corpus_fetch.sh`): analyze every file, panic nowhere, check that
//! every recorded span really covers the name it claims, and report an honest
//! resolution rate. Skipped when the corpus is not checked out.
use std::fs;
use std::path::{Path, PathBuf};
use vlog_parser::parse_source_tolerant;
use vlog_semantic::{analyze, Semantics};
#[derive(Default)]
struct Totals {
files: usize,
symbols: usize,
references: usize,
resolved: usize,
external: usize,
/// Uses inside a class that only its `extends` (or an import) could
/// supply — the honest third bucket class-semantics added.
inherited: usize,
/// Names the source scan could not place. Expected to stay at zero — a
/// nonzero count means a construct whose name this crate cannot locate.
unplaced: usize,
}
impl Totals {
fn add(&mut self, sem: &Semantics, src: &str, path: &Path) {
self.files += 1;
self.symbols += sem.symbols().len();
for sym in sem.symbols() {
self.unplaced +=
usize::from(!covers(src, sym.name_span, &sym.name, path, "declaration"));
}
for r in sem.references() {
self.unplaced += usize::from(!covers(src, r.name_span, &r.name, path, "use"));
}
let (resolved, total) = sem.resolution_counts();
self.references += total;
self.resolved += resolved;
self.external += sem.references().iter().filter(|r| r.is_external_candidate()).count();
self.inherited += sem.references().iter().filter(|r| r.is_inherited_candidate()).count();
}
fn unresolved(&self) -> usize {
self.references - self.resolved - self.external - self.inherited
}
fn report(&self, label: &str) {
let pct = |n: usize| 100.0 * n as f64 / self.references.max(1) as f64;
println!(
"{label}: {} files, {} symbols, {} references — {:.1}% resolved in-file, \
{:.1}% external candidates, {:.1}% inherited candidates, {:.2}% unresolved ({}), \
{} names unplaced",
self.files,
self.symbols,
self.references,
pct(self.resolved),
pct(self.external),
pct(self.inherited),
pct(self.unresolved()),
self.unresolved(),
self.unplaced,
);
}
}
/// True when `span` really is where `name` is written. An empty span is the
/// scan's "not found" answer and is counted, not asserted away.
fn covers(src: &str, span: vlog_parser::TextRange, name: &str, path: &Path, what: &str) -> bool {
if span.is_empty() {
return false;
}
assert_eq!(
&src[span.as_range()],
name,
"{}: the {what} span of {name} covers other bytes",
path.display()
);
true
}
#[test]
fn every_corpus_file_analyzes_without_panicking() {
let root = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../corpus");
if !root.is_dir() {
eprintln!("corpus/ not checked out — skipping");
return;
}
let mut files = Vec::new();
collect(&root, &mut files);
files.sort();
let mut all = Totals::default();
// The same numbers over the files that parse without a single error node,
// which is the population the LSP's resolution quality is really about.
let mut clean = Totals::default();
for path in &files {
let Ok(src) = fs::read_to_string(path) else {
continue;
};
let (file, diags) = parse_source_tolerant(&src);
let sem = analyze(&file, &src);
all.add(&sem, &src, path);
if diags.is_empty() {
clean.add(&sem, &src, path);
}
}
all.report("corpus (every file)");
clean.report("corpus (files that parse cleanly)");
assert!(all.files > 0, "corpus/ exists but holds no Verilog");
assert_eq!(all.unplaced, 0, "every name must be locatable in the source");
assert!(
100 * clean.unresolved() < 3 * clean.references,
"unresolved rate on cleanly-parsed files regressed: {} of {}",
clean.unresolved(),
clean.references
);
}
fn collect(dir: &Path, out: &mut Vec<PathBuf>) {
let Ok(entries) = fs::read_dir(dir) else {
return;
};
for entry in entries.flatten() {
let path = entry.path();
if path.is_dir() {
collect(&path, out);
} else if matches!(path.extension().and_then(|e| e.to_str()), Some("sv" | "v" | "svh")) {
out.push(path);
}
}
}
// Author: Guoliang Chen. Lucisv is made by Guoliang Chen with love, luciochen.com