TODO section 22, coverage_gaps H4 corrected. Class-body and module-item include fragments already parsed - the file-scope item loop is the class-item loop. The three shapes that did not - a bare statement list, an enum-member list (riscv-dv's CUSTOM_1, file, included between enum members), a covergroup-item list - become SourceFile::fragment, detected by a whole-file rule that cannot misfire on a valid file: the first structural item failed (directives, macros and null items do not count; a tolerant Ok that recorded a diagnostic counts as failed - an existing parity test caught if/else reading as a generate-if to one parser and a statement to the other), the file does not open with a design-unit keyword, and a shape consumes the file to EOF, tried narrowest-first. Each shape re-lexes a fresh token stream - Rewind does not restore the tokens' comment buckets and the first draft lost comments on non-fragment files. A fragment formats at indent zero through the existing printers; its declarations bind; its enum variants are not exported (pkg::CUSTOM_1 must not resolve to a name that does not exist). FACTS stays 12. The other .svh was never a fragment problem: a define whose formal argument list spans two lines with no backslash. The lexer continues such a directive until the list closes - incrementally, after QA found the first cut quadratic on an unclosed list (20000 lines: 2.5 s -> 0.56 ms), pinned by a byte-visit counter. Opentitan 12 -> 10 failing files, exactly the two predicted; corpus 813 -> 814 of 820; every remaining comment-gap failure proven to fail identically outside a fragment. docs/features/fragment-mode/README.md. Co-Authored-By: Claude Fable 5 <[email protected]>
146 lines
6.0 KiB
Rust
146 lines
6.0 KiB
Rust
//! Fragment files: what a whole file that is the middle of some body
|
|
//! declares, and where the names inside it bind. The rules live in
|
|
//! `docs/features/fragment-mode/README.md`.
|
|
|
|
use std::path::PathBuf;
|
|
use vlog_parser::parse_source;
|
|
use vlog_semantic::{analyze, Resolution, ScopeKind, Semantics, SymbolKind, WorkspaceIndex};
|
|
|
|
fn sem(src: &str) -> Semantics {
|
|
let file = parse_source(src).expect("fixture must parse");
|
|
analyze(&file, src)
|
|
}
|
|
|
|
/// Offset of the `n`th (0-based) occurrence of `needle`.
|
|
fn nth(src: &str, needle: &str, n: usize) -> u32 {
|
|
src.match_indices(needle).nth(n).expect("occurrence must exist").0 as u32
|
|
}
|
|
|
|
/// The declaration the use at `offset` points at, by name and kind.
|
|
fn defines(s: &Semantics, offset: u32) -> Option<(String, SymbolKind)> {
|
|
let d = s.definition_of(offset)?;
|
|
Some((d.name.clone(), d.kind))
|
|
}
|
|
|
|
fn kind_of(s: &Semantics, name: &str) -> SymbolKind {
|
|
s.symbols()
|
|
.iter()
|
|
.find(|sym| sym.name == name)
|
|
.unwrap_or_else(|| panic!("no symbol {name}"))
|
|
.kind
|
|
}
|
|
|
|
// ----------------------------------------------------------------------
|
|
// What each shape declares
|
|
// ----------------------------------------------------------------------
|
|
|
|
#[test]
|
|
fn an_enum_member_fragment_declares_variants_at_file_scope() {
|
|
// IEEE 1800 §6.19 binds an enum's labels in the scope *holding* the enum.
|
|
// For a fragment that scope is the file, so hover on `CUSTOM_1` says
|
|
// "enum variant" rather than nothing at all.
|
|
let src = "// TODO: Add custom instruction name enum\nCUSTOM_1,\n";
|
|
let s = sem(src);
|
|
assert_eq!(kind_of(&s, "CUSTOM_1"), SymbolKind::EnumVariant);
|
|
let file = s.scope(s.file_scope()).expect("a file scope");
|
|
assert_eq!(file.symbols.len(), 1);
|
|
}
|
|
|
|
#[test]
|
|
fn an_enum_member_fragments_values_bind_lexically() {
|
|
let src = "A = 1,\nB = A + 1\n";
|
|
let s = sem(src);
|
|
assert_eq!(defines(&s, nth(src, "A", 1)), Some(("A".to_string(), SymbolKind::EnumVariant)));
|
|
}
|
|
|
|
#[test]
|
|
fn a_statement_fragment_declares_its_locals_in_a_block_scope() {
|
|
// A snippet's locals are scoped by whatever body includes it, not by the
|
|
// file — so they get a `Block` of their own and never reach file scope.
|
|
// (The fragment has to *start* with something that is not an item, or the
|
|
// file is an item list and never reaches fragment mode: a leading `int i;`
|
|
// is a perfectly good module-item fragment. See the README's known
|
|
// limits.)
|
|
let src = "do_reset();\nint idx;\nidx = 1;\n";
|
|
let s = sem(src);
|
|
let file = s.scope(s.file_scope()).expect("a file scope");
|
|
assert!(file.symbols.is_empty(), "nothing leaks to file scope");
|
|
assert_eq!(file.children.len(), 1);
|
|
let block = s.scope(file.children[0]).expect("the fragment's block");
|
|
assert_eq!(block.kind, ScopeKind::Block);
|
|
assert_eq!(kind_of(&s, "idx"), SymbolKind::Var);
|
|
assert_eq!(defines(&s, nth(src, "idx", 1)), Some(("idx".to_string(), SymbolKind::Var)));
|
|
}
|
|
|
|
#[test]
|
|
fn a_statement_fragments_free_names_are_recorded_as_references() {
|
|
// Everything a snippet touches comes from the body that includes it, so
|
|
// it cannot resolve here — but it must still be a *reference*, or rename
|
|
// and find-references would silently skip the file.
|
|
let src = "cfg.enable = 1;\nfoo(bar);\n";
|
|
let s = sem(src);
|
|
let names: Vec<&str> = s.references().iter().map(|r| r.name.as_str()).collect();
|
|
assert!(names.contains(&"cfg"), "{names:?}");
|
|
assert!(names.contains(&"bar"), "{names:?}");
|
|
}
|
|
|
|
#[test]
|
|
fn a_cover_item_fragment_declares_its_labels_at_file_scope() {
|
|
let src = "cp_a: coverpoint a;\ncp_b: coverpoint b;\nx_ab: cross cp_a, cp_b;\n";
|
|
let s = sem(src);
|
|
assert_eq!(kind_of(&s, "cp_a"), SymbolKind::Coverpoint);
|
|
assert_eq!(kind_of(&s, "cp_b"), SymbolKind::Coverpoint);
|
|
assert_eq!(kind_of(&s, "x_ab"), SymbolKind::Cross);
|
|
// And a cross names the coverpoints beside it.
|
|
assert_eq!(
|
|
defines(&s, nth(src, "cp_a", 1)),
|
|
Some(("cp_a".to_string(), SymbolKind::Coverpoint))
|
|
);
|
|
}
|
|
|
|
// ----------------------------------------------------------------------
|
|
// What a fragment does *not* do
|
|
// ----------------------------------------------------------------------
|
|
|
|
#[test]
|
|
fn a_fragment_exports_nothing_to_the_workspace() {
|
|
// The census is unchanged (FACTS_FORMAT stays 12): a fragment file is not
|
|
// a package, so `riscv_custom_instr_enum::CUSTOM_1` must not resolve — the
|
|
// only way a user reaches `CUSTOM_1` is through the package that
|
|
// `` `include``s the fragment, which needs the include graph nobody has
|
|
// yet (`docs/features/include-nav/README.md`).
|
|
let ws = WorkspaceIndex::build([
|
|
(PathBuf::from("riscv_custom_instr_enum.sv"), "CUSTOM_1,\n"),
|
|
(PathBuf::from("snippet.svh"), "do_reset();\nint idx;\n"),
|
|
(PathBuf::from("cov.svh"), "cp_a: coverpoint a;\n"),
|
|
]);
|
|
assert!(ws.units("CUSTOM_1").is_empty(), "an enum variant is not a unit");
|
|
assert!(ws.units("idx").is_empty(), "a snippet local is not a unit");
|
|
assert!(ws.units("cp_a").is_empty(), "a coverpoint label is not a unit");
|
|
assert!(ws.member("riscv_custom_instr_enum", "CUSTOM_1").is_empty());
|
|
assert!(ws.workspace_symbols("CUSTOM_1").is_empty(), "and it is not searchable either");
|
|
}
|
|
|
|
#[test]
|
|
fn a_broken_file_is_still_an_opaque_leaf() {
|
|
// A file that fits no shape keeps the error node it always had, and the
|
|
// binder keeps treating it as a leaf that declares nothing.
|
|
let (file, diags) = vlog_parser::parse_source_tolerant("foo bar baz )(\n");
|
|
assert!(!diags.is_empty());
|
|
let s = analyze(&file, "foo bar baz )(\n");
|
|
assert!(s.symbols().is_empty());
|
|
}
|
|
|
|
#[test]
|
|
fn a_fragment_resolves_nothing_it_cannot_see() {
|
|
// The names an enum-member fragment writes in its values are ordinary
|
|
// lexical uses, so an unknown one is a miss rather than a wrong answer.
|
|
let src = "A = WIDTH - 1\n";
|
|
let s = sem(src);
|
|
let r = s.reference_at(nth(src, "WIDTH", 0)).expect("a reference at WIDTH");
|
|
assert!(
|
|
!matches!(r.resolution, Resolution::Resolved(_)),
|
|
"nothing in this file declares WIDTH"
|
|
);
|
|
}
|