//! 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, {:.1}% unresolved, \ {} names unplaced", self.files, self.symbols, self.references, pct(self.resolved), pct(self.external), pct(self.inherited), pct(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) { 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); } } }