//! A read-only sweep probe: set CENSUS to a directory and this prints the //! covergroup and SVA structured/fallback counts and the tolerant diagnostics //! over every `.sv`/`.svh` under it. Ignored; run by hand for the feature //! README. use std::path::{Path, PathBuf}; fn collect(dir: &Path, out: &mut Vec) { let Ok(entries) = std::fs::read_dir(dir) else { return }; for e in entries.flatten() { let p = e.path(); if p.is_dir() { collect(&p, out); } else if matches!(p.extension().and_then(|s| s.to_str()), Some("sv") | Some("svh")) { out.push(p); } } } /// The 1-based line ranges of every SVA construct in one file, printed as /// `SVA_LINES - …`. /// /// What the sweep's "the output moved only inside assertion text" claim is /// checked against: a diff hunk that falls outside every one of these ranges /// is a construct this round had no business touching. Set `SVA_LINES` to a /// file list (one path per line) rather than `CENSUS` to a directory. #[test] #[ignore] fn sva_lines() { std::thread::Builder::new() .stack_size(vlog_parser::PARSE_STACK_SIZE) .spawn(run_sva_lines) .expect("spawn") .join() .expect("join"); } fn run_sva_lines() { let list = std::env::var("SVA_LINES").expect("SVA_LINES"); let paths = std::fs::read_to_string(&list).expect("the file list"); for path in paths.lines().filter(|l| !l.trim().is_empty()) { let Ok(src) = std::fs::read_to_string(path) else { continue }; let (file, _) = vlog_parser::parse_source_tolerant(&src); let index = vlog_parser::LineIndex::new(&src); let mut spans: Vec<(u32, u32)> = Vec::new(); collect_sva(&file.items, &mut spans); let ranges: Vec = spans .iter() .map(|(a, b)| { format!("{}-{}", index.line_col(*a).0, index.line_col(b.saturating_sub(1)).0) }) .collect(); println!("SVA_LINES {path} {}", ranges.join(" ")); } } /// Every assertion-ish item's span, at any item depth. Statement-position /// assertions are inside a construct whose own span already covers them. fn collect_sva(items: &[vlog_parser::ast::Item], out: &mut Vec<(u32, u32)>) { use vlog_parser::ast::ItemKind; for item in items { match &item.kind { ItemKind::Assertion(_) | ItemKind::PropSeqDecl(_) | ItemKind::DefaultDisable(_) | ItemKind::Verbatim(_) => out.push((item.span.start, item.span.end)), ItemKind::Module(m) => collect_sva(&m.items, out), ItemKind::Package { items, .. } => collect_sva(items, out), ItemKind::Class(c) => collect_sva(&c.items, out), ItemKind::GenerateRegion { items, .. } => collect_sva(items, out), ItemKind::GenBlock(b) => collect_sva(&b.items, out), ItemKind::GenIf(g) => { collect_sva(&g.then.items, out); match g.els.as_deref() { Some(vlog_parser::ast::GenElse::Block(b)) => collect_sva(&b.items, out), Some(vlog_parser::ast::GenElse::If(_, inner)) => { collect_sva(&inner.then.items, out) } None => {} } } ItemKind::GenFor(g) => collect_sva(&g.body.items, out), ItemKind::GenCase(g) => { for it in &g.items { collect_sva(&it.body.items, out); } } // A procedural block may hold an assertion *statement*. Pushing // the whole block would make the check vacuous for a 250-line // `always`, so the statements are walked and only the assertion // ones are pushed. ItemKind::Always { stmt, .. } | ItemKind::Initial(stmt) | ItemKind::Final(stmt) => { collect_sva_stmt(stmt, out) } ItemKind::Function(f) => { for st in &f.stmts { collect_sva_stmt(st, out); } } ItemKind::Task(t) => { for st in &t.stmts { collect_sva_stmt(st, out); } } _ => {} } } } /// The assertion statements inside one statement, at any nesting depth. fn collect_sva_stmt(stmt: &vlog_parser::ast::Stmt, out: &mut Vec<(u32, u32)>) { use vlog_parser::ast::StmtKind; if matches!(stmt.kind, StmtKind::Assertion(_) | StmtKind::Verbatim(_)) { out.push((stmt.span.start, stmt.span.end)); return; } vlog_linter_visit(stmt, out); } /// A hand-rolled statement walk — `vlog_linter::visit` is another crate's. fn vlog_linter_visit(stmt: &vlog_parser::ast::Stmt, out: &mut Vec<(u32, u32)>) { use vlog_parser::ast::StmtKind; match &stmt.kind { StmtKind::Block { stmts, .. } | StmtKind::ForkJoin { stmts, .. } => { for st in stmts { collect_sva_stmt(st, out); } } StmtKind::If { then, els, .. } => { collect_sva_stmt(then, out); if let Some(e) = els { collect_sva_stmt(e, out); } } StmtKind::Case { items, .. } => { for it in items { collect_sva_stmt(&it.stmt, out); } } StmtKind::For { body, .. } => collect_sva_stmt(body, out), StmtKind::Timing { stmt: Some(inner), .. } => collect_sva_stmt(inner, out), StmtKind::Labeled { stmt: inner, .. } => collect_sva_stmt(inner, out), _ => {} } } #[test] #[ignore] fn census() { std::thread::Builder::new() .stack_size(vlog_parser::PARSE_STACK_SIZE) .spawn(run) .expect("spawn") .join() .expect("join"); } // `CENSUS` is read relative to the crate directory — cargo runs a test // binary with the crate as its working directory — so a relative path // typed at the workspace root (`CENSUS=corpus`) finds nothing and reports // `files=0`. Pass an absolute path. fn run() { let root = std::env::var("CENSUS").expect("CENSUS"); let mut files = Vec::new(); collect(Path::new(&root), &mut files); files.sort(); let (mut cg, mut fb, mut cf, mut diags, mut with_diags) = (0u64, 0u64, 0u64, 0u64, 0u64); let (mut pd, mut pdf, mut asrt, mut asrtf) = (0u64, 0u64, 0u64, 0u64); for p in &files { let Ok(src) = std::fs::read_to_string(p) else { continue }; let (file, ds) = vlog_parser::parse_source_tolerant(&src); cg += u64::from(file.covergroups); fb += u64::from(file.covergroup_fallbacks); cf += u64::from(file.constraint_fallbacks); pd += u64::from(file.prop_seq_decls); pdf += u64::from(file.prop_seq_fallbacks); asrt += u64::from(file.assertions); asrtf += u64::from(file.assertion_fallbacks); if file.prop_seq_fallbacks > 0 || file.assertion_fallbacks > 0 { println!( "SVA_FALLBACK {} decls={} assertions={}", p.display(), file.prop_seq_fallbacks, file.assertion_fallbacks ); } if file.covergroup_fallbacks > 0 { println!("FALLBACK {} {}", p.display(), file.covergroup_fallbacks); } if !ds.is_empty() { with_diags += 1; println!("DIAGS {} {}", p.display(), ds.len()); } diags += ds.len() as u64; } println!( "files={} covergroups={cg} covergroup_fallbacks={fb} constraint_fallbacks={cf} diagnostics={diags} files_with_diagnostics={with_diags}", files.len() ); println!( "prop_seq_decls={pd} prop_seq_fallbacks={pdf} assertions={asrt} assertion_fallbacks={asrtf}" ); }