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]>
246 lines
10 KiB
Rust
246 lines
10 KiB
Rust
//! Fragment files (TODO.md §22): a whole file that is the bare middle of some
|
|
//! larger construct's body. See `docs/features/fragment-mode/README.md`.
|
|
|
|
use vlog_parser::ast::{CoverItemKind, Fragment, ItemKind, SourceFile, StmtKind};
|
|
use vlog_parser::{parse, parse_tolerant, tokenize};
|
|
|
|
fn strict(src: &str) -> SourceFile {
|
|
parse(tokenize(src).expect("lex error"), src)
|
|
.unwrap_or_else(|e| panic!("strict parse failed on {src:?}: {e}"))
|
|
}
|
|
|
|
fn tolerant(src: &str) -> (SourceFile, usize) {
|
|
let (file, diags) = parse_tolerant(tokenize(src).expect("lex error"), src);
|
|
(file, diags.len())
|
|
}
|
|
|
|
/// The three shapes, as a word, for the tests that only care which one won.
|
|
fn shape(file: &SourceFile) -> &'static str {
|
|
match &file.fragment {
|
|
None => "items",
|
|
Some(Fragment::Statements(_)) => "statements",
|
|
Some(Fragment::EnumMembers { .. }) => "enum members",
|
|
Some(Fragment::CoverItems(_)) => "cover items",
|
|
}
|
|
}
|
|
|
|
// ----------------------------------------------------------------------
|
|
// The three shapes
|
|
// ----------------------------------------------------------------------
|
|
|
|
#[test]
|
|
fn the_riscv_enum_fragment_parses_in_both_modes() {
|
|
// `riscv_instr_pkg.sv:651` includes this file *between two enum members*.
|
|
let src = "// TODO: Add custom instruction name enum\nCUSTOM_1,\n";
|
|
for file in [strict(src), tolerant(src).0] {
|
|
let Some(Fragment::EnumMembers { members, directives }) = &file.fragment else {
|
|
panic!("expected an enum-member fragment, got {}", shape(&file));
|
|
};
|
|
assert!(file.items.is_empty(), "a fragment file has no items");
|
|
assert_eq!(members.len(), 1);
|
|
assert_eq!(members[0].name, "CUSTOM_1");
|
|
assert!(members[0].comma, "the trailing comma is what splices it in");
|
|
assert!(directives.is_empty());
|
|
}
|
|
assert_eq!(tolerant(src).1, 0, "a fragment is not a broken file");
|
|
}
|
|
|
|
#[test]
|
|
fn an_enum_member_fragment_keeps_values_ranges_and_the_missing_comma() {
|
|
let file = strict("A, B = 2, ReqPeriResetIdx[0:1], C\n");
|
|
let Some(Fragment::EnumMembers { members, .. }) = &file.fragment else {
|
|
panic!("expected an enum-member fragment, got {}", shape(&file));
|
|
};
|
|
assert_eq!(members.len(), 4);
|
|
assert!(members[1].value.is_some());
|
|
assert_eq!(members[2].range.len(), 1);
|
|
assert!(!members[3].comma, "the last member wrote no comma");
|
|
}
|
|
|
|
#[test]
|
|
fn ifdef_arms_between_enum_members_are_list_directives() {
|
|
let file = strict("`ifdef ADD_CUSTOM\nCUSTOM_1,\n`endif\nLAST\n");
|
|
let Some(Fragment::EnumMembers { members, directives }) = &file.fragment else {
|
|
panic!("expected an enum-member fragment, got {}", shape(&file));
|
|
};
|
|
assert_eq!(members.len(), 2);
|
|
assert_eq!(directives.len(), 2, "the `ifdef and the `endif");
|
|
assert_eq!(directives[0].before, 0);
|
|
assert_eq!(directives[1].before, 1);
|
|
}
|
|
|
|
#[test]
|
|
fn a_statement_fragment_parses_as_statements() {
|
|
let file = strict("a = 1;\nif (b) c <= 2;\nbegin\n d = 3;\nend\n");
|
|
let Some(Fragment::Statements(stmts)) = &file.fragment else {
|
|
panic!("expected a statement fragment, got {}", shape(&file));
|
|
};
|
|
assert_eq!(stmts.len(), 3);
|
|
assert!(matches!(stmts[1].kind, StmtKind::If { .. }));
|
|
assert!(matches!(stmts[2].kind, StmtKind::Block { .. }));
|
|
}
|
|
|
|
#[test]
|
|
fn a_cover_item_fragment_parses_as_cover_items() {
|
|
let file = strict("cp_a: coverpoint a;\nx: cross cp_a, cp_b;\noption.weight = 5;\n");
|
|
let Some(Fragment::CoverItems(items)) = &file.fragment else {
|
|
panic!("expected a cover-item fragment, got {}", shape(&file));
|
|
};
|
|
assert_eq!(items.len(), 3);
|
|
assert!(matches!(items[0].kind, CoverItemKind::Coverpoint(_)));
|
|
assert!(matches!(items[1].kind, CoverItemKind::Cross(_)));
|
|
assert!(matches!(items[2].kind, CoverItemKind::Option(_)));
|
|
}
|
|
|
|
// ----------------------------------------------------------------------
|
|
// What is *not* a fragment
|
|
// ----------------------------------------------------------------------
|
|
|
|
#[test]
|
|
fn a_file_whose_first_item_parses_is_never_re_read() {
|
|
// The rule that keeps every file that works today working: a class-body
|
|
// fragment and a module-item fragment are ordinary item lists and must
|
|
// stay item lists, whatever else could be read out of the same bytes.
|
|
for src in [
|
|
"module m;\nendmodule\n",
|
|
"rand bit [31:0] addr;\nfunction void f();\n x = 1;\nendfunction\n",
|
|
"assign y = a & b;\nalways_comb z = y;\n",
|
|
"package p;\n typedef enum {A, B} e_t;\nendpackage\n",
|
|
] {
|
|
let file = strict(src);
|
|
assert_eq!(shape(&file), "items", "{src:?} is an item list");
|
|
assert!(file.fragment.is_none());
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn a_design_unit_is_never_re_read_however_broken_it_is() {
|
|
// The cheap guard in front of the retry: a file that opens with a
|
|
// design-unit keyword is a unit being written, and a mid-edit one must
|
|
// keep the error node it has always had rather than pay for three retries.
|
|
let (file, diags) = tolerant("module m;\n x <= 1;\n");
|
|
assert!(file.fragment.is_none());
|
|
assert!(diags > 0);
|
|
}
|
|
|
|
#[test]
|
|
fn a_file_that_fits_no_shape_keeps_one_error_node_and_one_diagnostic() {
|
|
let (file, diags) = tolerant("foo bar baz )(\n");
|
|
assert!(file.fragment.is_none(), "garbage is not a fragment");
|
|
assert_eq!(file.items.len(), 1);
|
|
assert!(matches!(file.items[0].kind, ItemKind::Error(_)));
|
|
assert_eq!(diags, 1, "one message, no cascade");
|
|
}
|
|
|
|
#[test]
|
|
fn a_shape_that_does_not_reach_end_of_file_does_not_win() {
|
|
// The whole-file rule: `A, B` reads as enum members, but the `endmodule`
|
|
// after it does not, so the file stays what it was — broken.
|
|
let (file, diags) = tolerant("A, B\nendmodule\n");
|
|
assert!(file.fragment.is_none());
|
|
assert!(diags > 0);
|
|
}
|
|
|
|
#[test]
|
|
fn an_empty_file_is_not_a_fragment() {
|
|
for src in ["", "\n", "// just a comment\n"] {
|
|
let file = strict(src);
|
|
assert!(file.fragment.is_none(), "{src:?}");
|
|
}
|
|
}
|
|
|
|
// ----------------------------------------------------------------------
|
|
// Order, spans and comments
|
|
// ----------------------------------------------------------------------
|
|
|
|
#[test]
|
|
fn the_narrowest_shape_wins_the_same_bytes() {
|
|
// `cp: coverpoint a;` is a labelled variable declaration to the statement
|
|
// grammar and `option.weight = 5;` is an assignment, so the cover-item
|
|
// reading has to be tried first or the wrong symbols bind.
|
|
assert_eq!(shape(&strict("cp: coverpoint a;\n")), "cover items");
|
|
assert_eq!(shape(&strict("option.weight = 5;\n")), "cover items");
|
|
// An enum-member list is never a statement list — a statement ends at a
|
|
// `;` and a member list stops in front of one — so these are unambiguous.
|
|
assert_eq!(shape(&strict("A, B\n")), "enum members");
|
|
assert_eq!(shape(&strict("a = 1;\n")), "statements");
|
|
}
|
|
|
|
#[test]
|
|
fn a_fragments_span_covers_the_whole_file() {
|
|
let src = "a = 1;\nb = 2;\n";
|
|
let file = strict(src);
|
|
assert_eq!(file.span.start, 0);
|
|
assert_eq!(file.span.end as usize, src.len());
|
|
}
|
|
|
|
#[test]
|
|
fn a_trailing_comment_lands_in_the_files_dangling_slot() {
|
|
let file = strict("A,\nB\n// after the last member\n");
|
|
assert_eq!(file.dangling.len(), 1);
|
|
assert_eq!(file.dangling[0].text, "// after the last member");
|
|
}
|
|
|
|
#[test]
|
|
fn a_comment_in_every_slot_of_a_fragment_survives_the_parse() {
|
|
// The parse is only half of hard invariant #3; the formatter half is
|
|
// `crates/vlog_formatter/tests/verification.rs`'s fragment gap fuzzer.
|
|
let src = "// before\nA, // after A\n/* mid */ B\n// tail\n";
|
|
let file = strict(src);
|
|
let Some(Fragment::EnumMembers { members, .. }) = &file.fragment else {
|
|
panic!("expected an enum-member fragment, got {}", shape(&file));
|
|
};
|
|
assert_eq!(members[0].meta.leading.len(), 1);
|
|
assert!(members[0].meta.trailing.is_some());
|
|
assert_eq!(members[1].meta.leading.len(), 1);
|
|
assert_eq!(file.dangling.len(), 1);
|
|
}
|
|
|
|
#[test]
|
|
fn a_fragment_that_opens_with_a_directive_is_still_a_fragment() {
|
|
// A directive, a macro invocation and a null item are legal in *every*
|
|
// body, so they do not settle what kind of file this is.
|
|
assert_eq!(shape(&strict("`ifdef X\nA,\n`endif\nB\n")), "enum members");
|
|
assert_eq!(shape(&strict("`uvm_info(a, b, c)\nx = 1;\n")), "statements");
|
|
}
|
|
|
|
#[test]
|
|
fn the_two_modes_ask_the_fragment_question_at_the_same_point() {
|
|
// `if (a) begin x = 1; end` is a *generate-if* to the item grammar, and a
|
|
// tolerant parse will happily recover the statement inside it into an
|
|
// error node — while the strict parse of the same bytes fails and becomes
|
|
// a statement fragment. If the two disagreed the editor would report
|
|
// errors on a file the formatter formats, so an item the tolerant parse
|
|
// had to *patch* counts as a failure for this question.
|
|
let src = "if (a) begin\n x = 1;\nend else begin\n x = 2;\nend\n";
|
|
assert_eq!(shape(&strict(src)), "statements");
|
|
let (file, diags) = tolerant(src);
|
|
assert_eq!(shape(&file), "statements");
|
|
assert_eq!(diags, 0, "and no diagnostic survives the retry");
|
|
}
|
|
|
|
#[test]
|
|
fn a_generate_if_the_item_grammar_really_reads_is_left_alone() {
|
|
// The other side of the same coin: a body the item grammar reads *without*
|
|
// patching anything is a generate-if and stays one.
|
|
let src = "if (a) begin\n wire w;\nend\n";
|
|
let (file, diags) = tolerant(src);
|
|
assert_eq!(shape(&file), "items");
|
|
assert_eq!(diags, 0);
|
|
assert!(matches!(file.items[0].kind, ItemKind::GenIf(_)));
|
|
}
|
|
|
|
// ----------------------------------------------------------------------
|
|
// The known limits, pinned so a change to them is deliberate
|
|
// ----------------------------------------------------------------------
|
|
|
|
#[test]
|
|
fn an_unlabelled_coverpoint_first_reads_as_a_declaration() {
|
|
// `coverpoint a;` is a legal variable declaration, so the file's first
|
|
// item *parses* and the retry never runs. Labelled coverpoints — which is
|
|
// how real covergroup fragments are written, because a bare one cannot be
|
|
// crossed — are unaffected. Recorded so the day this changes is a choice.
|
|
let file = strict("coverpoint a;\ncross a, b;\n");
|
|
assert_eq!(shape(&file), "items");
|
|
}
|