TODO section 31 (coverage_gaps M9, docs/topo.md 2.9-3). fence::collect enters class bodies, functions, tasks and always/ initial/final blocks, and statement lists - begin/end, fork/join and every compound body - through scan_stmt_scope/scan_nested_stmt on the one scan_units both scope kinds share; stmt_docs/stmt_seq obey the two swallowed-unit rules item_docs/item_seq had; fence regions merge into range.rs's plan, which was load-bearing (a fenced statement has no verbatim run behind it; proved by disabling it). A region opens only at a line start (opens_line) - the rule the format-fences README has stated since round 1 - so a fence after code on the same line is an ordinary comment everywhere, which narrows one accidental item-level shape (module m; // lucisv: off) and is ruled a drift fix. Deferred with reasons: port lists, case-item label lists, same-line fences, a property's locals. verify.rs, lucisv/src/lint.rs and lucisv_server/src/lint.rs untouched. Proof: 3 244 tests / 48 binaries, fences.cases 25 -> 38 with the statement-level entry inverted, corpus 814/820 and 860 files byte- identical to 0.34.0, opentitan: 4 of its 9 fenced files now keep their fenced bytes and are fixed points, --check counts identical; 3 348 fence-injection trials with no new refusals. docs/features/statement-fences/README.md. Co-Authored-By: Claude Fable 5 <[email protected]>
248 lines
13 KiB
Rust
248 lines
13 KiB
Rust
//! `// verilog_format: off` fences: the properties the `.cases` table cannot
|
|
//! state directly — byte-exactness of the spliced range, idempotency of a
|
|
//! second pass, and that the safety net still rejects a *corrupted* region.
|
|
//! Spec: `docs/features/format-fences/README.md`.
|
|
|
|
use vlog_formatter::fence::{fence_of, Fence};
|
|
use vlog_formatter::{format_source, verify_equivalence, Config};
|
|
|
|
fn fmt(src: &str) -> String {
|
|
format_source(src, &Config::default()).expect("format")
|
|
}
|
|
|
|
/// Format, then assert the three properties every fenced input must have:
|
|
/// the region survives byte-for-byte, the output re-lexes to the input, and
|
|
/// formatting again is a fixed point.
|
|
fn fenced(src: &str, region: &str) -> String {
|
|
let out = fmt(src);
|
|
assert!(src.contains(region), "the region is not in the input:\n{region}");
|
|
assert!(out.contains(region), "region not emitted byte-exact:\nwant\n{region}\ngot\n{out}");
|
|
verify_equivalence(src, &out).expect("output is token-equivalent to the input");
|
|
assert_eq!(fmt(&out), out, "second pass changed the output");
|
|
out
|
|
}
|
|
|
|
#[test]
|
|
fn a_region_is_emitted_byte_for_byte() {
|
|
// Interior spacing, alignment, trailing comments and a blank line inside
|
|
// the region all survive; the item after it is formatted normally.
|
|
let src = "module m;\n // lucisv: off\n wire [3:0] a = 4'h1; // lo\n\n wire [31:0] b= 32'h2;\n // lucisv: on\n wire c;\nendmodule\n";
|
|
let region =
|
|
" // lucisv: off\n wire [3:0] a = 4'h1; // lo\n\n wire [31:0] b= 32'h2;\n // lucisv: on";
|
|
let out = fenced(src, region);
|
|
assert!(out.ends_with(" wire c;\nendmodule\n"), "{out}");
|
|
}
|
|
|
|
#[test]
|
|
fn a_region_keeps_its_own_indentation_on_every_line() {
|
|
// The source is indented four spaces inside a module the formatter
|
|
// indents by two: the region's first line is not re-indented either.
|
|
let src =
|
|
"module m;\n // lucisv: off\n wire a;\n // lucisv: on\n wire b;\nendmodule\n";
|
|
let out = fenced(src, " // lucisv: off\n wire a;\n // lucisv: on");
|
|
assert!(
|
|
out.contains("\n wire b;\n"),
|
|
"formatting did not resume at the module indent:\n{out}"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn an_unmatched_off_runs_to_the_end_of_its_scope_only() {
|
|
// It must stop at the module's last item — the comment trailing that item
|
|
// included, since `Meta.trailing` has no span of its own to splice from —
|
|
// and the next module must still be formatted.
|
|
let src = "module m;\n // lucisv: off\n wire a; // note\nendmodule\n\nmodule n;\n wire b;\nendmodule\n";
|
|
let out = fenced(src, " // lucisv: off\n wire a; // note");
|
|
assert!(out.contains("module n;\n wire b;\n"), "the next module was not formatted:\n{out}");
|
|
}
|
|
|
|
#[test]
|
|
fn comments_before_the_off_fence_are_still_formatted_and_never_dropped() {
|
|
let src =
|
|
"module m;\n// doc for the table\n// lucisv: off\nwire a;\n// lucisv: on\nendmodule\n";
|
|
let out = fenced(src, "// lucisv: off\nwire a;\n// lucisv: on");
|
|
assert!(out.contains(" // doc for the table\n"), "leading comment lost or unindented:\n{out}");
|
|
}
|
|
|
|
#[test]
|
|
fn a_corrupted_fenced_region_is_still_rejected_by_the_safety_net() {
|
|
// The fence is not a licence to change bytes: a hand-built "output" that
|
|
// reformats, drops or adds anything inside the region must fail
|
|
// equivalence exactly as it would outside one.
|
|
let src = "module m;\n // lucisv: off\n wire [3:0] a;\n wire b; // note\n // lucisv: on\nendmodule\n";
|
|
let corrupted = [
|
|
// a token dropped from inside the region
|
|
"module m;\n // lucisv: off\n wire a;\n wire b; // note\n // lucisv: on\nendmodule\n",
|
|
// the region's trailing comment dropped
|
|
"module m;\n // lucisv: off\n wire [3:0] a;\n wire b;\n // lucisv: on\nendmodule\n",
|
|
// a fence comment itself dropped
|
|
"module m;\n wire [3:0] a;\n wire b; // note\n // lucisv: on\nendmodule\n",
|
|
// a token added inside the region
|
|
"module m;\n // lucisv: off\n wire [3:0] a, z;\n wire b; // note\n // lucisv: on\nendmodule\n",
|
|
// a fence turned into an ordinary comment
|
|
"module m;\n // lucisv: off\n wire [3:0] a;\n wire b; // note\n // lucisv: onward\nendmodule\n",
|
|
];
|
|
// The real formatter gets it right...
|
|
verify_equivalence(src, &fmt(src)).expect("the real output is equivalent");
|
|
// ...and every corruption of it is caught.
|
|
for bad in corrupted {
|
|
assert!(
|
|
verify_equivalence(src, bad).is_err(),
|
|
"a corrupted fenced region was accepted:\n{bad}"
|
|
);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn a_statement_region_is_emitted_byte_for_byte() {
|
|
// A fence between two statements of an `always` block: the hand-aligned
|
|
// interior, the blank line and the trailing comment all survive, and the
|
|
// statement after the `on` is formatted normally.
|
|
let src = "module m;\n always_ff @(posedge clk) begin\n // lucisv: off\n q0 <= d0; // lo\n\n q1 <= d1;\n // lucisv: on\n q2 <= d2;\n end\nendmodule\n";
|
|
let region =
|
|
" // lucisv: off\n q0 <= d0; // lo\n\n q1 <= d1;\n // lucisv: on";
|
|
let out = fenced(src, region);
|
|
assert!(out.contains("\n q2 <= d2;\n"), "formatting did not resume in the block:\n{out}");
|
|
}
|
|
|
|
#[test]
|
|
fn an_unmatched_off_in_a_statement_list_stops_at_the_end_of_that_list() {
|
|
// It must stop at the block's last statement — its trailing comment
|
|
// included — and never swallow the `end`, the `endfunction` or anything
|
|
// after them.
|
|
let src = "module m;\n initial begin\n // lucisv: off\n x = 1;\n y = 2; // note\n end\n\n function int f();\n return 1;\n endfunction\nendmodule\n";
|
|
let out = fenced(src, " // lucisv: off\n x = 1;\n y = 2; // note");
|
|
assert!(out.contains("\n end\n"), "the block's `end` was swallowed:\n{out}");
|
|
assert!(out.contains("function int f();"), "the next item was not formatted:\n{out}");
|
|
assert!(out.contains(" return 1;"), "the next body was not formatted:\n{out}");
|
|
}
|
|
|
|
#[test]
|
|
fn a_fork_body_and_a_task_body_are_each_their_own_scope() {
|
|
let src = "module m;\n task automatic t();\n fork\n // lucisv: off\n p1 ();\n p2 ();\n // lucisv: on\n p3 ();\n join\n z = 1;\n endtask\nendmodule\n";
|
|
let out =
|
|
fenced(src, " // lucisv: off\n p1 ();\n p2 ();\n // lucisv: on");
|
|
assert!(out.contains(" p3();"), "the statement after the `on` was not formatted:\n{out}");
|
|
assert!(out.contains(" z = 1;"), "the task body after the fork was not formatted:\n{out}");
|
|
}
|
|
|
|
#[test]
|
|
fn a_class_body_is_a_fenceable_scope() {
|
|
let src = "class c;\n int a;\n // lucisv: off\n int b = 2;\n int cc =3;\n // lucisv: on\n int d;\nendclass\n";
|
|
let out = fenced(src, " // lucisv: off\n int b = 2;\n int cc =3;\n // lucisv: on");
|
|
assert!(out.contains(" int a;\n"), "{out}");
|
|
assert!(out.contains(" int d;\n"), "{out}");
|
|
}
|
|
|
|
#[test]
|
|
fn an_unmatched_off_in_a_class_body_stops_at_endclass() {
|
|
let src = "class c;\n // lucisv: off\n int a; // note\nendclass\n\nclass d;\n int b;\nendclass\n";
|
|
let out = fenced(src, " // lucisv: off\n int a; // note");
|
|
assert!(out.contains("class d;\n int b;\n"), "the next class was not formatted:\n{out}");
|
|
}
|
|
|
|
#[test]
|
|
fn a_fence_sharing_its_line_with_code_is_an_ordinary_comment() {
|
|
// `begin // lucisv: off` reaches the scan as the *leading* comment of the
|
|
// first statement, mid-line. Honouring it would open a region whose first
|
|
// line came from the wrong column; it is an ordinary comment instead.
|
|
let src = "module m;\n always @(*) begin // lucisv: off\n x = 1;\n y = 2;\n end\nendmodule\n";
|
|
let out = fmt(src);
|
|
verify_equivalence(src, &out).expect("equivalent");
|
|
assert_eq!(fmt(&out), out, "second pass changed the output");
|
|
assert!(out.contains(" x = 1;"), "the fence fired mid-line:\n{out}");
|
|
// The same rule at item level: a header's own line is not a fence line.
|
|
let src = "module m; // lucisv: off\n wire a;\nendmodule\n";
|
|
let out = fmt(src);
|
|
verify_equivalence(src, &out).expect("equivalent");
|
|
assert_eq!(fmt(&out), out, "second pass changed the output");
|
|
assert!(out.contains(" wire a;"), "the fence fired mid-line:\n{out}");
|
|
}
|
|
|
|
#[test]
|
|
fn the_three_deferred_places_are_inert_and_never_corrupt() {
|
|
// A port list, a case-item label list, and the single-statement body of a
|
|
// bodyless `if`: a fence in each is an ordinary comment, and the code
|
|
// around it is laid out exactly as it would be without one.
|
|
let cases = [
|
|
"module m (\n // lucisv: off\n input logic a,\n output logic b\n);\nendmodule\n",
|
|
"module m;\n always @(*) case (s)\n // lucisv: off\n 2'b00: a = 1;\n 2'b01: a = 2;\n endcase\nendmodule\n",
|
|
"module m;\n always @(*)\n // lucisv: off\n if (c) x = 1;\nendmodule\n",
|
|
];
|
|
for src in cases {
|
|
let out = fmt(src);
|
|
verify_equivalence(src, &out).expect("output is token-equivalent to the input");
|
|
assert_eq!(fmt(&out), out, "second pass changed the output:\n{out}");
|
|
assert!(out.contains("lucisv: off"), "the fence comment was dropped:\n{out}");
|
|
assert!(!out.contains(" a = 1"), "the fence fenced:\n{out}");
|
|
assert!(!out.contains("input logic"), "the fence fenced:\n{out}");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn a_corrupted_statement_or_class_region_is_still_rejected_by_the_safety_net() {
|
|
// The same attack as at item level, aimed at the two new scopes: a fence
|
|
// is not a licence to change bytes, and a hand-built "output" that
|
|
// reformats, drops or adds anything inside one must fail equivalence.
|
|
// Every pair changes the *token stream*, which is the question
|
|
// `verify_equivalence` answers; that a region's bytes are also reproduced
|
|
// exactly — spacing included, which no token check can see — is what the
|
|
// `fenced` helper asserts in the tests above.
|
|
let stmt = "module m;\n initial begin\n // lucisv: off\n x = 1;\n y = 2; // note\n // lucisv: on\n end\nendmodule\n";
|
|
let stmt_corrupted = [
|
|
// a literal rewritten inside the region
|
|
"module m;\n initial begin\n // lucisv: off\n x = 7;\n y = 2; // note\n // lucisv: on\n end\nendmodule\n",
|
|
// the region's trailing comment dropped
|
|
"module m;\n initial begin\n // lucisv: off\n x = 1;\n y = 2;\n // lucisv: on\n end\nendmodule\n",
|
|
// the `off` fence itself dropped
|
|
"module m;\n initial begin\n x = 1;\n y = 2; // note\n // lucisv: on\n end\nendmodule\n",
|
|
// a statement added inside the region
|
|
"module m;\n initial begin\n // lucisv: off\n x = 1;\n z = 0;\n y = 2; // note\n // lucisv: on\n end\nendmodule\n",
|
|
// a statement dropped from inside the region
|
|
"module m;\n initial begin\n // lucisv: off\n x = 1;\n // lucisv: on\n end\nendmodule\n",
|
|
// the `on` fence turned into an ordinary comment
|
|
"module m;\n initial begin\n // lucisv: off\n x = 1;\n y = 2; // note\n // lucisv: onward\n end\nendmodule\n",
|
|
// the block's `end` swallowed into the region's last line
|
|
"module m;\n initial begin\n // lucisv: off\n x = 1;\n y = 2; // note\n // lucisv: on\nendmodule\n",
|
|
];
|
|
let class = "class c;\n // lucisv: off\n int b = 2;\n int cc =3;\n // lucisv: on\n int d;\nendclass\n";
|
|
let class_corrupted = [
|
|
// a type rewritten inside the region
|
|
"class c;\n // lucisv: off\n bit b = 2;\n int cc =3;\n // lucisv: on\n int d;\nendclass\n",
|
|
// a member dropped from inside the region
|
|
"class c;\n // lucisv: off\n int b = 2;\n // lucisv: on\n int d;\nendclass\n",
|
|
// a member added inside the region
|
|
"class c;\n // lucisv: off\n int b = 2;\n int cc =3;\n int e;\n // lucisv: on\n int d;\nendclass\n",
|
|
// `endclass` swallowed
|
|
"class c;\n // lucisv: off\n int b = 2;\n int cc =3;\n // lucisv: on\n int d;\n",
|
|
// a fence comment dropped
|
|
"class c;\n int b = 2;\n int cc =3;\n // lucisv: on\n int d;\nendclass\n",
|
|
];
|
|
for (src, corrupted) in [(stmt, &stmt_corrupted[..]), (class, &class_corrupted[..])] {
|
|
verify_equivalence(src, &fmt(src)).expect("the real output is equivalent");
|
|
for bad in corrupted {
|
|
assert!(
|
|
verify_equivalence(src, bad).is_err(),
|
|
"a corrupted fenced region was accepted:\n{bad}"
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn the_matcher_agrees_with_what_the_formatter_honours() {
|
|
// The unit tests in `fence.rs` pin the matcher; this pins that the
|
|
// formatter is driven by it, in both directions.
|
|
let body = "module m;\n {}\n wire a;\nendmodule\n";
|
|
for spelling in ["// verilog_format: off", "//lucisv: off", "//\tverilog_format: off"] {
|
|
assert_eq!(fence_of(spelling), Some(Fence::Off));
|
|
let out = fmt(&body.replace("{}", spelling));
|
|
assert!(out.contains("wire a;"), "{spelling} did not fence:\n{out}");
|
|
}
|
|
for spelling in ["// lucisv: off now", "// lucisv:off", "/* lucisv: off */", "// LUCISV: OFF"] {
|
|
assert_eq!(fence_of(spelling), None);
|
|
let out = fmt(&body.replace("{}", spelling));
|
|
assert!(out.contains("wire a;"), "{spelling} fenced but should not have:\n{out}");
|
|
}
|
|
}
|