Every tracked file that can carry a comment (880 of them) now opens with "Author: Guoliang Chen, a Senior Digital Design Engineer" and closes with "Author: Guoliang Chen. Lucisv is made by Guoliang Chen with love, luciochen.com", in the file's own comment syntax. No behaviour change: the diff outside the version bump and changelogs is comment lines only. - tools/stamp_author.py: writes the lines; idempotent, `--check` lists what is missing. Headers go under a shebang and under Markdown front matter. - Left alone: test fixtures, cases and snapshots (byte-exact inputs), JSON and JSONL, logs, images, lockfiles, docs/site/dist (regenerated) and the LICENSE files (already carry the copyright line). - tools/linux_check.sh: --help reads its header from line 3, now that the author line sits under the shebang. - tools/gates.sh green on this tree: 5 545 tests, corpus 814/6 as before. Co-Authored-By: Claude Mythos 5.2 <[email protected]>
456 lines
16 KiB
Rust
456 lines
16 KiB
Rust
// Author: Guoliang Chen, a Senior Digital Design Engineer
|
|
|
|
//! Selection formatting (`format_selection`): the smallest run of sibling
|
|
//! items or statements a selection covers is laid out at the indentation it
|
|
//! was written at, and every other byte of the file stays as written.
|
|
//!
|
|
//! Spec: `docs/features/selection-formatting/README.md`.
|
|
|
|
use vlog_formatter::{format_range, format_selection, verify_equivalence, Config, LineRanges};
|
|
|
|
fn select_with(src: &str, spec: &str, config: &Config) -> Result<String, String> {
|
|
let lines = LineRanges::parse(spec).expect("line spec");
|
|
format_selection(src, config, &lines).map_err(|e| e.to_string())
|
|
}
|
|
|
|
fn select(src: &str, spec: &str) -> String {
|
|
select_with(src, spec, &Config::default()).expect("formats")
|
|
}
|
|
|
|
/// Format, then format the result again with the same lines: the second run
|
|
/// must change nothing.
|
|
fn fixed(src: &str, spec: &str) -> String {
|
|
let out = select(src, spec);
|
|
assert_eq!(select(&out, spec), out, "a second run must change nothing");
|
|
out
|
|
}
|
|
|
|
/// Lines `first..=last` (1-based) of `src` are the target: everything before
|
|
/// and after must come back byte for byte, and the returned middle is what
|
|
/// replaced them.
|
|
fn middle<'a>(src: &str, out: &'a str, first: usize, last: usize) -> &'a str {
|
|
let lines: Vec<&str> = src.split_inclusive('\n').collect();
|
|
let head: String = lines[..first - 1].concat();
|
|
let tail: String = lines[last..].concat();
|
|
assert!(out.starts_with(&head), "bytes before the target changed:\n{out}");
|
|
assert!(out.ends_with(&tail), "bytes after the target changed:\n{out}");
|
|
assert!(out.len() >= head.len() + tail.len(), "target overlaps the kept bytes:\n{out}");
|
|
&out[head.len()..out.len() - tail.len()]
|
|
}
|
|
|
|
// ------------------------------------------------------------------
|
|
// The owner's shapes
|
|
// ------------------------------------------------------------------
|
|
|
|
/// A file indented four spaces, as many hand-written files are.
|
|
const FOUR: &str = "\
|
|
module m;
|
|
reg [3:0] q;
|
|
always @(posedge clk) begin
|
|
if (rst) q<=0;
|
|
else begin
|
|
case (sel)
|
|
2'b00: q<=a;
|
|
2'b01 : q <= b;
|
|
default:q<=c;
|
|
endcase
|
|
end
|
|
end
|
|
assign y=q;
|
|
endmodule
|
|
";
|
|
|
|
#[test]
|
|
fn a_whole_always_keeps_its_indentation_in_a_four_space_file() {
|
|
let out = fixed(FOUR, "3-12");
|
|
let mid = middle(FOUR, &out, 3, 12);
|
|
assert_eq!(
|
|
mid,
|
|
" always @(posedge clk) begin
|
|
if (rst) q <= 0;
|
|
else begin
|
|
case (sel)
|
|
2'b00: q <= a;
|
|
2'b01: q <= b;
|
|
default: q <= c;
|
|
endcase
|
|
end
|
|
end
|
|
"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn a_whole_case_inside_an_always_is_the_only_thing_laid_out() {
|
|
let out = fixed(FOUR, "6-10");
|
|
let mid = middle(FOUR, &out, 6, 10);
|
|
assert_eq!(
|
|
mid,
|
|
" case (sel)
|
|
2'b00: q <= a;
|
|
2'b01: q <= b;
|
|
default: q <= c;
|
|
endcase
|
|
"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn an_always_inside_a_generate_for_is_laid_out_alone() {
|
|
let src = "\
|
|
module m;
|
|
generate
|
|
for (genvar i=0;i<4;i++) begin : g
|
|
always @(posedge clk) q[i]<=d[i];
|
|
assign w[i]=q[i];
|
|
end
|
|
endgenerate
|
|
endmodule
|
|
";
|
|
let out = fixed(src, "4");
|
|
let mid = middle(src, &out, 4, 4);
|
|
assert_eq!(mid, " always @(posedge clk) q[i] <= d[i];\n");
|
|
}
|
|
|
|
#[test]
|
|
fn statements_inside_a_function_are_laid_out_without_the_function() {
|
|
let src = "\
|
|
module m;
|
|
function automatic int f(input int a);
|
|
int r;
|
|
r=a+1;
|
|
return r;
|
|
endfunction
|
|
endmodule
|
|
";
|
|
let out = fixed(src, "4-5");
|
|
let mid = middle(src, &out, 4, 5);
|
|
assert_eq!(mid, " r = a + 1;\n return r;\n");
|
|
assert!(out.contains(" int r;\n"), "the unselected statement stays: {out}");
|
|
}
|
|
|
|
#[test]
|
|
fn a_block_inside_an_else_branch_is_laid_out_alone() {
|
|
let src = "\
|
|
module m;
|
|
always @* begin
|
|
if (a) x = 1;
|
|
else begin
|
|
y=2;
|
|
z = 3;
|
|
end
|
|
end
|
|
endmodule
|
|
";
|
|
let out = fixed(src, "5-6");
|
|
let mid = middle(src, &out, 5, 6);
|
|
assert_eq!(mid, " y = 2;\n z = 3;\n");
|
|
}
|
|
|
|
// ------------------------------------------------------------------
|
|
// Indentation
|
|
// ------------------------------------------------------------------
|
|
|
|
#[test]
|
|
fn tab_indentation_is_reproduced_as_written() {
|
|
let src = "module m;\n\talways @* begin\n\t\tx=1;\n\t\tif (a) begin\n\t\t\ty=2;\n\t\tend\n\tend\nendmodule\n";
|
|
let out = fixed(src, "2-7");
|
|
let mid = middle(src, &out, 2, 7);
|
|
assert_eq!(
|
|
mid,
|
|
"\talways @(*) begin\n\t x = 1;\n\t if (a) begin\n\t y = 2;\n\t end\n\tend\n"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn off_grid_indentation_is_kept() {
|
|
let src = "module m;\n always @* begin\n x=1;\n end\nendmodule\n";
|
|
let out = fixed(src, "2-4");
|
|
assert_eq!(middle(src, &out, 2, 4), " always @(*) begin\n x = 1;\n end\n");
|
|
}
|
|
|
|
#[test]
|
|
fn a_misindented_parent_does_not_move_the_selection() {
|
|
let src = "module m;\nalways @* begin\n x=1;\n y = 2;\n end\nendmodule\n";
|
|
let out = fixed(src, "3");
|
|
assert_eq!(middle(src, &out, 3, 3), " x = 1;\n");
|
|
assert!(out.contains("\n y = 2;\n"), "the sibling is untouched: {out}");
|
|
}
|
|
|
|
// ------------------------------------------------------------------
|
|
// Partial selections
|
|
// ------------------------------------------------------------------
|
|
|
|
#[test]
|
|
fn one_line_inside_a_case_lays_out_the_case() {
|
|
let out = fixed(FOUR, "8");
|
|
let mid = middle(FOUR, &out, 6, 10);
|
|
assert!(mid.starts_with(" case (sel)\n"), "{mid}");
|
|
assert!(mid.contains(" 2'b01: q <= b;\n"), "{mid}");
|
|
}
|
|
|
|
#[test]
|
|
fn a_cursor_only_range_formats_the_statement_under_it() {
|
|
let src = "module m;\n always @* begin\n x=1;\n y=2;\n end\nendmodule\n";
|
|
let out = fixed(src, "4");
|
|
assert_eq!(out, "module m;\n always @* begin\n x=1;\n y = 2;\n end\nendmodule\n");
|
|
}
|
|
|
|
#[test]
|
|
fn a_selection_ending_on_the_end_line_takes_only_the_statements_it_covers() {
|
|
let src = "module m;\n always @* begin\n x=1;\n y=2;\n end\n assign a=b;\nendmodule\n";
|
|
let out = fixed(src, "4-5");
|
|
assert_eq!(middle(src, &out, 4, 4), " y = 2;\n");
|
|
}
|
|
|
|
#[test]
|
|
fn the_end_line_alone_takes_the_unit_it_closes() {
|
|
let src = "module m;\n always @* begin\n x=1;\n y=2;\n end\n assign a=b;\nendmodule\n";
|
|
let out = fixed(src, "5");
|
|
assert_eq!(middle(src, &out, 2, 5), " always @(*) begin\n x = 1;\n y = 2;\n end\n");
|
|
}
|
|
|
|
#[test]
|
|
fn a_selection_starting_on_the_first_line_takes_the_whole_unit() {
|
|
let src = "module m;\n always @* begin\n x=1;\n y=2;\n end\n assign a=b;\nendmodule\n";
|
|
let out = fixed(src, "2-3");
|
|
assert_eq!(middle(src, &out, 2, 5), " always @(*) begin\n x = 1;\n y = 2;\n end\n");
|
|
}
|
|
|
|
#[test]
|
|
fn blank_lines_alone_change_nothing() {
|
|
let src = "module m;\n wire a;\n\n\n wire b;\nendmodule\n";
|
|
assert_eq!(select(src, "3-4"), src);
|
|
}
|
|
|
|
#[test]
|
|
fn leading_and_trailing_comments_travel_with_their_unit() {
|
|
let src = "module m;\n always @* begin\n // why\n x=1; // what\n y=2;\n end\nendmodule\n";
|
|
let out = fixed(src, "4");
|
|
assert_eq!(middle(src, &out, 3, 4), " // why\n x = 1; // what\n");
|
|
}
|
|
|
|
#[test]
|
|
fn a_comment_after_begin_is_not_line_clean_and_the_parent_is_taken() {
|
|
let src = "module m;\n always @* begin // note\n x=1;\n end\nendmodule\n";
|
|
let out = fixed(src, "3");
|
|
assert_eq!(middle(src, &out, 2, 4), " always @(*) begin\n // note\n x = 1;\n end\n");
|
|
}
|
|
|
|
#[test]
|
|
fn two_statements_on_one_line_are_taken_together() {
|
|
let src = "module m;\n always @* begin\n x=1; y=2;\n z=3;\n end\nendmodule\n";
|
|
let out = fixed(src, "3");
|
|
assert_eq!(middle(src, &out, 3, 3), " x = 1;\n y = 2;\n");
|
|
assert!(out.contains(" z=3;\n"), "{out}");
|
|
}
|
|
|
|
#[test]
|
|
fn a_selection_across_two_branches_takes_the_whole_if() {
|
|
let src = "module m;\n always @* begin\n if (a) begin\n x=1;\n end else begin\n y=2;\n end\n end\nendmodule\n";
|
|
let out = fixed(src, "4-6");
|
|
let mid = middle(src, &out, 3, 7);
|
|
assert_eq!(mid, " if (a) begin\n x = 1;\n end else begin\n y = 2;\n end\n");
|
|
}
|
|
|
|
// ------------------------------------------------------------------
|
|
// Fences
|
|
// ------------------------------------------------------------------
|
|
|
|
#[test]
|
|
fn a_fence_inside_the_target_is_emitted_as_written() {
|
|
let src = "module m;\n always @* begin\n x=1;\n // lucisv: off\n y = 2;\n // lucisv: on\n z=3;\n end\nendmodule\n";
|
|
let out = fixed(src, "2-8");
|
|
assert!(out.contains(" y = 2;\n"), "{out}");
|
|
assert!(out.contains(" x = 1;\n") && out.contains(" z = 3;\n"), "{out}");
|
|
}
|
|
|
|
#[test]
|
|
fn a_fence_around_the_target_wins() {
|
|
let src = "module m;\n // lucisv: off\n always @* begin\n x=1;\n end\n // lucisv: on\nendmodule\n";
|
|
assert_eq!(select(src, "4"), src);
|
|
assert_eq!(select(src, "3-5"), src);
|
|
}
|
|
|
|
#[test]
|
|
fn a_fence_straddling_the_target_trims_the_fenced_units() {
|
|
let src = "module m;\n always @* begin\n a=1;\n b=2;\n // lucisv: off\n c = 3;\n // lucisv: on\n d=4;\n end\nendmodule\n";
|
|
// Lines 3-6 meet a, b, and c (fenced): c is trimmed off the end.
|
|
let out = fixed(src, "3-6");
|
|
assert_eq!(middle(src, &out, 3, 4), " a = 1;\n b = 2;\n");
|
|
assert!(out.ends_with(
|
|
" // lucisv: off\n c = 3;\n // lucisv: on\n d=4;\n end\nendmodule\n"
|
|
));
|
|
// Lines 6-8 meet c (fenced) and d — whose extent starts at `on`: both go.
|
|
assert_eq!(select(src, "6-8"), src);
|
|
}
|
|
|
|
// ------------------------------------------------------------------
|
|
// Deferral to `format_range`
|
|
// ------------------------------------------------------------------
|
|
|
|
#[test]
|
|
fn a_module_header_or_whole_module_selection_is_format_range() {
|
|
let src = "module m(input a,output b);\n wire x;\n assign x=a;\nendmodule\nmodule n;\nendmodule\n";
|
|
for spec in ["1", "1-4", "1-6", "4", "2-5"] {
|
|
let lines = LineRanges::parse(spec).expect("spec");
|
|
let range = format_range(src, &Config::default(), &lines).expect("range formats");
|
|
assert_eq!(select(src, spec), range, "spec {spec}");
|
|
}
|
|
}
|
|
|
|
// ------------------------------------------------------------------
|
|
// Files that do not parse
|
|
// ------------------------------------------------------------------
|
|
|
|
const BROKEN: &str = "\
|
|
module m;
|
|
always @* begin
|
|
x=1;
|
|
end
|
|
assign y = ;
|
|
endmodule
|
|
";
|
|
|
|
#[test]
|
|
fn an_error_outside_the_target_does_not_stop_it() {
|
|
let out = fixed(BROKEN, "3");
|
|
assert_eq!(middle(BROKEN, &out, 3, 3), " x = 1;\n");
|
|
}
|
|
|
|
#[test]
|
|
fn an_error_inside_the_target_refuses() {
|
|
let err = select_with(BROKEN, "5", &Config::default()).expect_err("refused");
|
|
assert!(err.contains("syntax error") || err.contains("parse"), "{err}");
|
|
}
|
|
|
|
// ------------------------------------------------------------------
|
|
// Idempotence and multiple ranges
|
|
// ------------------------------------------------------------------
|
|
|
|
#[test]
|
|
fn formatting_the_same_target_twice_changes_nothing() {
|
|
for spec in ["3-12", "6-10", "4", "8", "13"] {
|
|
fixed(FOUR, spec);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn two_ranges_lay_out_two_targets_and_nothing_between() {
|
|
let src = "module m;\n always @* begin\n x=1;\n y=2;\n z=3;\n end\nendmodule\n";
|
|
let out = fixed(src, "3,5");
|
|
assert_eq!(
|
|
out,
|
|
"module m;\n always @* begin\n x = 1;\n y=2;\n z = 3;\n end\nendmodule\n"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn overlapping_targets_are_reselected_from_their_hull() {
|
|
// `4` selects the inner statement, `2` the always holding it.
|
|
let src = "module m;\n always @* begin\n x=1;\n y=2;\n end\nendmodule\n";
|
|
let out = fixed(src, "2,4");
|
|
assert_eq!(out, "module m;\n always @(*) begin\n x = 1;\n y = 2;\n end\nendmodule\n");
|
|
}
|
|
|
|
// ------------------------------------------------------------------
|
|
// Masked-corruption attacks on the splice
|
|
// ------------------------------------------------------------------
|
|
|
|
/// Either a correct result (equivalent to the input, a fixed point) or a
|
|
/// refusal — never silently wrong.
|
|
fn correct_or_refused(src: &str, spec: &str) -> Option<String> {
|
|
let out = select_with(src, spec, &Config::default()).ok()?;
|
|
verify_equivalence(src, &out).expect("the result is equivalent to the input");
|
|
assert_eq!(select(&out, spec), out, "and a fixed point");
|
|
Some(out)
|
|
}
|
|
|
|
#[test]
|
|
fn a_trailing_comment_on_the_last_line_survives() {
|
|
let src = "module m;\n always @* begin\n x=1; /* a */ /* b */\n end\nendmodule\n";
|
|
let out = correct_or_refused(src, "3").expect("formats");
|
|
assert!(out.contains("/* a */") && out.contains("/* b */"), "{out}");
|
|
}
|
|
|
|
#[test]
|
|
fn crlf_line_endings_are_kept() {
|
|
let src = "module m;\r\n always @* begin\r\n x=1;\r\n y=2;\r\n end\r\nendmodule\r\n";
|
|
let out = correct_or_refused(src, "3-4").expect("formats");
|
|
assert_eq!(
|
|
out,
|
|
"module m;\r\n always @* begin\r\n x = 1;\r\n y = 2;\r\n end\r\nendmodule\r\n"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn crlf_with_a_trailing_comment_on_the_last_line_keeps_every_line_ending() {
|
|
// QA finding 1: the `\r` after a trailing `//` comment is the line
|
|
// ending, not part of the unit, so the splice leaves it in place.
|
|
let src = "module m;\r\n initial begin\r\n a = 1; // c\r\n b = 2; // d\r\n end\r\nendmodule\r\n";
|
|
let out = correct_or_refused(src, "3-4").expect("formats");
|
|
assert_eq!(out, src.replace("b = 2", "b = 2"));
|
|
let out = correct_or_refused(src, "4").expect("formats");
|
|
assert_eq!(out, src.replace("b = 2", "b = 2"));
|
|
}
|
|
|
|
#[test]
|
|
fn a_multi_line_block_comment_inside_the_target_survives() {
|
|
let src =
|
|
"module m;\n always @* begin\n /* one\n two */\n x=1;\n end\nendmodule\n";
|
|
let out = correct_or_refused(src, "2-6").expect("formats");
|
|
assert!(out.contains("/* one\n two */"), "{out}");
|
|
}
|
|
|
|
#[test]
|
|
fn a_multi_line_string_inside_the_target_is_kept_or_refused() {
|
|
let src =
|
|
"module m;\n initial begin\n $display(\"a\\\n b\");\n x=1;\n end\nendmodule\n";
|
|
if let Some(out) = correct_or_refused(src, "2-6") {
|
|
assert!(out.contains("\"a\\\n b\""), "{out}");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn a_target_at_the_end_of_a_file_without_a_final_newline() {
|
|
let src = "always @* begin\n x=1;\nend";
|
|
let out = correct_or_refused(src, "2").expect("formats");
|
|
assert_eq!(out, "always @* begin\n x = 1;\nend");
|
|
let src = "wire a;\nassign b=a;";
|
|
let out = correct_or_refused(src, "2").expect("formats");
|
|
assert_eq!(out, "wire a;\nassign b = a;");
|
|
}
|
|
|
|
#[test]
|
|
fn multi_byte_characters_before_the_target_do_not_shift_it() {
|
|
let src = "module m;\n // café — ünïcode\n wire a; // ß\n assign b=a;\nendmodule\n";
|
|
let out = correct_or_refused(src, "4").expect("formats");
|
|
assert_eq!(middle(src, &out, 4, 4), " assign b = a;\n");
|
|
}
|
|
|
|
#[test]
|
|
fn the_safety_net_rejects_a_mis_spliced_output() {
|
|
// An off-by-one at the end of the splice duplicates the `;`; at the
|
|
// start it drops the trailing comment's first byte. Both must be caught.
|
|
let src = "module m;\n always @* begin\n x=1; // c\n end\nendmodule\n";
|
|
let duplicated = "module m;\n always @* begin\n x = 1;; // c\n end\nendmodule\n";
|
|
assert!(verify_equivalence(src, duplicated).is_err());
|
|
let dropped = "module m;\n always @* begin\n x = 1; c\n end\nendmodule\n";
|
|
assert!(verify_equivalence(src, dropped).is_err());
|
|
let crlf = "module m;\r\n always @* begin\r\n /* one\r\n two */\r\n x=1;\r\n end\r\nendmodule\r\n";
|
|
let lost_comment_line =
|
|
"module m;\r\n always @* begin\r\n /* one */\r\n x = 1;\r\n end\r\nendmodule\r\n";
|
|
assert!(verify_equivalence(crlf, lost_comment_line).is_err());
|
|
}
|
|
|
|
#[test]
|
|
fn a_column_zero_directive_does_not_set_the_indentation_of_the_code_it_guards() {
|
|
let src = "module m;\n`ifdef A\n wire a;\n`endif\n wire b;\nendmodule\n";
|
|
let out = fixed(src, "2-4");
|
|
// The code says where the block stands; the directives are indented with
|
|
// it, as a whole-file run indents them.
|
|
assert_eq!(middle(src, &out, 2, 4), " `ifdef A\n wire a;\n `endif\n");
|
|
}
|
|
|
|
// Author: Guoliang Chen. Lucisv is made by Guoliang Chen with love, luciochen.com
|