Files
Guoliang ChenandClaude Mythos 5.2 71d13b5fbe
ci / checks (push) Successful in 1m0s
ci / corpus (push) Successful in 18s
ci / linux (push) Successful in 6m4s
release / build-and-publish (push) Successful in 5m31s
Release 0.66.2: every source file names its author
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]>
2026-11-24 12:11:09 -08:00

348 lines
16 KiB
Rust

// Author: Guoliang Chen, a Senior Digital Design Engineer
//! TODO §65.4: `find_references` and `rename` through a hierarchical path
//! rooted at a top-level module another file declares (`tb.dut.g_sw[0].q`,
//! the opentitan DV `force` shape; IEEE 1800 §23.6), and `$root.tb.…`.
//! `docs/features/top-rooted-paths/README.md`.
use std::path::{Path, PathBuf};
use vlog_refactor::{find_references, rename, RefactorEdit};
const DUT: &str = "module dut_a;
for (genvar i = 0; i < 2; i++) begin : g_sw
logic buf_q;
end
endmodule
";
const TB: &str = "module tb;
dut_a dut ();
endmodule
";
const PROBE: &str = "module probe (output logic o);
assign o = tb.dut.g_sw[0].buf_q;
initial force tb.dut.g_sw[1].buf_q = 1'b0;
initial release $root.tb.dut.g_sw[1].buf_q;
endmodule
";
fn path(name: &str) -> PathBuf {
PathBuf::from("/rtl").join(name)
}
fn workspace(files: &[(&str, &str)]) -> Vec<(PathBuf, String)> {
files.iter().map(|(name, text)| (path(name), text.to_string())).collect()
}
/// The offset of the first `name` after `prefix`.
fn at(text: &str, prefix: &str, name: &str) -> u32 {
let from = text.find(prefix).unwrap_or_else(|| panic!("`{prefix}` is not in the text"));
let from = from + prefix.len();
(from + text[from..].find(name).expect("the name after the prefix")) as u32
}
fn apply(text: &str, edit: &RefactorEdit, file: &Path) -> String {
let mut out = String::new();
let mut cursor = 0usize;
for e in edit.edits_for(file) {
out.push_str(&text[cursor..e.range.start as usize]);
out.push_str(&e.new_text);
cursor = e.range.end as usize;
}
out.push_str(&text[cursor..]);
out
}
/// Every file's text after renaming the `name` after `prefix` in `file`, and
/// the warnings.
fn renamed(
files: &[(&str, &str)],
file: &str,
prefix: &str,
name: &str,
new: &str,
) -> (Vec<String>, Vec<String>) {
let text = files.iter().find(|(n, _)| *n == file).map(|(_, t)| *t).expect("the file");
let edit = rename(&workspace(files), &path(file), at(text, prefix, name), new)
.unwrap_or_else(|why| panic!("rename refused: {why}"));
let texts = files.iter().map(|(n, t)| apply(t, &edit, &path(n))).collect();
(texts, edit.warnings)
}
/// `file:line` of every reference to the `name` after `prefix`, sorted.
fn references(files: &[(&str, &str)], file: &str, prefix: &str, name: &str) -> Vec<String> {
let text = files.iter().find(|(n, _)| *n == file).map(|(_, t)| *t).expect("the file");
let found = find_references(&workspace(files), &path(file), at(text, prefix, name), true)
.unwrap_or_else(|why| panic!("find_references refused: {why}"));
let mut out: Vec<String> = found
.iter()
.map(|(p, span)| {
let name = p.file_name().and_then(|n| n.to_str()).unwrap_or("?");
let text = files.iter().find(|(n, _)| *n == name).map(|(_, t)| *t).expect("file");
assert_eq!(&text[span.as_range()], ident_at(text, span.start));
format!("{name}:{}", text[..span.start as usize].matches('\n').count() + 1)
})
.collect();
out.sort();
out
}
/// The identifier starting at `start` — what a reference span must slice.
fn ident_at(text: &str, start: u32) -> &str {
let rest = &text[start as usize..];
let end = rest.find(|c: char| !(c.is_ascii_alphanumeric() || c == '_')).unwrap_or(rest.len());
&rest[..end]
}
fn files() -> Vec<(&'static str, &'static str)> {
vec![("dut_a.sv", DUT), ("tb.sv", TB), ("probe.sv", PROBE)]
}
#[test]
fn find_references_lists_every_segment_of_a_top_rooted_path() {
let files = files();
let want = ["dut_a.sv:2", "probe.sv:2", "probe.sv:3", "probe.sv:4"];
assert_eq!(references(&files, "dut_a.sv", "begin : ", "g_sw"), want);
let want = ["dut_a.sv:3", "probe.sv:2", "probe.sv:3", "probe.sv:4"];
assert_eq!(references(&files, "dut_a.sv", "logic ", "buf_q"), want);
let want = ["probe.sv:2", "probe.sv:3", "probe.sv:4", "tb.sv:2"];
assert_eq!(references(&files, "tb.sv", "dut_a ", "dut"), want);
let want = ["probe.sv:2", "probe.sv:3", "probe.sv:4", "tb.sv:1"];
assert_eq!(references(&files, "tb.sv", "module ", "tb"), want, "the root itself");
}
#[test]
fn renaming_a_member_rewrites_the_path_and_warns_about_nothing() {
let files = files();
for (prefix, old, new) in [("begin : ", "g_sw", "g_hw"), ("logic ", "buf_q", "buf_r")] {
let (texts, warnings) = renamed(&files, "dut_a.sv", prefix, old, new);
assert_eq!(texts[0], DUT.replace(old, new));
assert_eq!(texts[1], TB);
assert_eq!(texts[2], PROBE.replace(old, new));
assert!(warnings.is_empty(), "{warnings:?}");
}
}
#[test]
fn renaming_the_instance_or_the_top_module_rewrites_its_segment() {
let files = files();
let (texts, warnings) = renamed(&files, "tb.sv", "dut_a ", "dut", "u_dut");
assert_eq!(texts[1], TB.replace(" dut ", " u_dut "));
assert_eq!(texts[2], PROBE.replace("tb.dut.", "tb.u_dut."));
assert!(warnings.is_empty(), "{warnings:?}");
let (texts, warnings) = renamed(&files, "tb.sv", "module ", "tb", "top_tb");
assert_eq!(texts[1], TB.replace("module tb", "module top_tb"));
assert_eq!(texts[2], PROBE.replace("tb.dut.", "top_tb.dut."));
assert!(warnings.is_empty(), "{warnings:?}");
}
/// The rename can be asked from the path itself, at any segment.
#[test]
fn a_rename_requested_on_a_segment_of_the_path_renames_the_declaration() {
let files = files();
let (texts, warnings) = renamed(&files, "probe.sv", "force tb.dut.", "g_sw", "g_hw");
assert_eq!(texts[0], DUT.replace("g_sw", "g_hw"));
assert_eq!(texts[2], PROBE.replace("g_sw", "g_hw"));
assert!(warnings.is_empty(), "{warnings:?}");
}
/// A local of the head's name wins (IEEE 1800 §23.8's upward search finds it
/// first): the path is that net's, nothing is rewritten, and the segment no
/// declaration answers is named — the §65.1 path warning, as before.
#[test]
fn a_local_that_shadows_the_top_module_is_not_renamed_through() {
let shadow = "module probe2 (output logic o);
logic tb;
assign o = tb.dut.g_sw[0].buf_q;
endmodule
";
let mut files = files();
files.push(("probe2.sv", shadow));
let (texts, warnings) = renamed(&files, "dut_a.sv", "begin : ", "g_sw", "g_hw");
assert_eq!(texts[2], PROBE.replace("g_sw", "g_hw"), "the unshadowed paths move");
assert_eq!(texts[3], shadow, "the shadowed path does not");
let said: Vec<&String> = warnings.iter().filter(|w| w.contains("probe2.sv")).collect();
assert_eq!(said.len(), 1, "{warnings:?}");
assert!(said[0].starts_with("probe2.sv:3:"), "{said:?}");
}
/// A root no file declares: the path is left and named, with its lines.
#[test]
fn a_root_the_workspace_does_not_declare_is_left_and_warned_about() {
let files = [("dut_a.sv", DUT), ("probe.sv", PROBE)];
let (texts, warnings) = renamed(&files, "dut_a.sv", "begin : ", "g_sw", "g_hw");
assert_eq!(texts[1], PROBE, "nothing reaches `dut_a` from an unknown root");
// Exactly one warning names these sites — the §65.1 path line; the
// unresolved-candidate scan leaves them to it (Handshake H1).
let said: Vec<&String> = warnings.iter().filter(|w| w.contains("probe.sv")).collect();
assert_eq!(said.len(), 1, "one line for the file: {warnings:?}");
assert!(said[0].starts_with("probe.sv:2,3,4:") && said[0].contains("`g_sw`"), "{said:?}");
}
/// Two `module tb`: `tb.dut` names two instances, so nothing after it is
/// renamed, and it is said.
#[test]
fn an_ambiguous_root_is_left_and_warned_about() {
let dut_b = "module dut_b;\n logic x;\nendmodule\n";
let tb2 = "module tb;\n dut_b dut ();\nendmodule\n";
let mut files = files();
files.push(("dut_b.sv", dut_b));
files.push(("tb2.sv", tb2));
let (texts, warnings) = renamed(&files, "dut_a.sv", "begin : ", "g_sw", "g_hw");
assert_eq!(texts[2], PROBE);
let said: Vec<&String> = warnings.iter().filter(|w| w.contains("probe.sv")).collect();
assert_eq!(said.len(), 1, "one line for the file: {warnings:?}");
assert!(said[0].starts_with("probe.sv:2,3,4:"), "{warnings:?}");
}
/// With two `module tb`, each `tb.` head could name either, so renaming one
/// of them is refused rather than rewriting heads that may be the other's.
#[test]
fn renaming_one_of_two_same_named_tops_is_refused() {
let tb2 = "module tb;\n dut_a dut ();\nendmodule\n";
let mut files = files();
files.push(("tb2.sv", tb2));
let text = TB;
let why = rename(&workspace(&files), &path("tb.sv"), at(text, "module ", "tb"), "top_tb")
.expect_err("refused");
assert!(why.contains("tb"), "{why}");
}
/// Handshake H1: QA's repro (`probe.sv: assign o = tb.dut.g_sw[0].buf_q;`)
/// with the root undeclared, and with three `module tb` (the opentitan
/// shape): exactly one warning names the probe's line, the §65.1 path line,
/// and the unresolved-candidate scan does not repeat it.
#[test]
fn an_unresolved_top_rooted_path_is_named_by_exactly_one_warning() {
let probe = "module probe (output logic o);\n assign o = tb.dut.g_sw[0].buf_q;\nendmodule\n";
let tb_b = "module tb;\n dut_b dut ();\nendmodule\n";
let tb_c = "module tb;\n dut_c dut ();\nendmodule\n";
let undeclared = vec![("dut_a.sv", DUT), ("probe.sv", probe)];
let mut three = vec![("dut_a.sv", DUT), ("tb.sv", TB), ("probe.sv", probe)];
three.extend([("tb_b.sv", tb_b), ("tb_c.sv", tb_c)]);
for files in [undeclared, three] {
for (prefix, old) in [("begin : ", "g_sw"), ("logic ", "buf_q")] {
let (texts, warnings) = renamed(&files, "dut_a.sv", prefix, old, "renamed_x");
let at_probe = files.iter().position(|(n, _)| *n == "probe.sv").expect("probe");
assert_eq!(texts[at_probe], probe, "left unchanged");
let said: Vec<&String> = warnings.iter().filter(|w| w.contains("probe.sv")).collect();
assert_eq!(said.len(), 1, "{old}: {warnings:?}");
assert!(said[0].starts_with("probe.sv:2:"), "{old}: {said:?}");
}
}
}
// ---------------------------------------------------------------------------
// Rework after QA (2026-10-19): rooting only when the head is provably a top
// module or interface. Each case is QA's repro (`scratchpad/qa654/attack`);
// each asserts the path is left unchanged and named, never silently renamed.
// ---------------------------------------------------------------------------
/// Rename the `name` after `prefix` in `file`; assert `left` is unchanged and
/// a warning names `left`'s `line`.
fn left_and_named(files: &[(&str, &str)], at_: (&str, &str, &str), left: &str, line: u32) {
let (file, prefix, name) = at_;
let (texts, warnings) = renamed(files, file, prefix, name, "renamed_x");
let i = files.iter().position(|(n, _)| *n == left).expect("the left file");
assert_eq!(texts[i], files[i].1, "{name}: `{left}` is not edited");
let named = format!("{left}:{line}:");
assert!(warnings.iter().any(|w| w.starts_with(&named)), "{name}: {warnings:?}");
}
/// QA F1 (B12): a package `tb` beside the module `tb`. Renaming the package's
/// `dut` used to rewrite `tb.dut.x`; renaming the module's instance used to
/// miss it. Neither touches it now, and both name it.
#[test]
fn qa_f1_a_package_sharing_the_roots_name_lends_the_path_nothing() {
let dut = "module dut_a;\n logic x;\nendmodule\n";
let pkg = "package tb;\n logic dut;\nendpackage\n";
let other = "module other;\n initial force tb.dut.x = 0;\nendmodule\n";
let files = [("dut_a.sv", dut), ("tb.sv", TB), ("tb_pkg.sv", pkg), ("other.sv", other)];
left_and_named(&files, ("tb_pkg.sv", "logic ", "dut"), "other.sv", 2);
left_and_named(&files, ("tb.sv", "dut_a ", "dut"), "other.sv", 2);
left_and_named(&files, ("dut_a.sv", "logic ", "x"), "other.sv", 2);
}
/// QA A3/A4: `pk.x` (illegal) with a package `pk`, with and without a module
/// `pk`: renaming the package's `x` leaves it.
#[test]
fn qa_f1_a_package_is_never_a_root() {
let pkg = "package pk;\n logic x;\nendpackage\n";
let module = "module pk;\n logic x;\nendmodule\n";
let user = "module user (output logic o);\n assign o = pk.x;\nendmodule\n";
let alone = [("p.sv", pkg), ("user.sv", user)];
let (texts, _) = renamed(&alone, "p.sv", "logic ", "x", "y");
assert_eq!(texts[1], user);
let both = [("p.sv", pkg), ("m.sv", module), ("user.sv", user)];
left_and_named(&both, ("p.sv", "logic ", "x"), "user.sv", 2);
left_and_named(&both, ("m.sv", "logic ", "x"), "user.sv", 2);
}
/// QA F2 (A1): `import p::*` supplies `cfg`, a package variable, and a module
/// `cfg` exists too. The imported name is the head; renaming the module's
/// `field` used to rewrite `cfg.field`.
#[test]
fn qa_f2_an_imported_head_is_never_rooted_at_a_same_named_module() {
let pkg = "package p;\n typedef struct packed { logic field; } s_t;\n s_t cfg;\nendpackage\n";
let module = "module cfg;\n logic field;\nendmodule\n";
let user =
"module user (output logic o);\n import p::*;\n assign o = cfg.field;\nendmodule\n";
let files = [("p.sv", pkg), ("cfgm.sv", module), ("user.sv", user)];
left_and_named(&files, ("cfgm.sv", "logic ", "field"), "user.sv", 3);
}
/// QA F4: three heads the binder cannot see — a macro-declared member in a
/// class (A11), a macro-declared struct in a module (A12) and a class in a
/// package writing `tb.dut.x` (D2) — each used to be renamed through a
/// same-named module.
#[test]
fn qa_f4_a_head_this_crate_cannot_see_is_never_rooted() {
let cfg = "module cfg;\n logic en;\nendmodule\n";
let class = "class env;\n `DECLARE_CFG\n task run(); if (cfg.en) $display(\"x\"); endtask\nendclass\n";
left_and_named(&[("cm.sv", cfg), ("cls.sv", class)], ("cm.sv", "logic ", "en"), "cls.sv", 3);
let st = "module st;\n logic a;\nendmodule\n";
let user = "module user (output logic o);\n `DECL_STRUCT(st)\n assign o = st.a;\nendmodule\n";
left_and_named(&[("cm.sv", st), ("user.sv", user)], ("cm.sv", "logic ", "a"), "user.sv", 3);
let dut = "module dut_a;\n logic x;\nendmodule\n";
let pkg = "package env_pkg;\n class seq;\n task body(); tb.dut.x = 1; endtask\n endclass\nendpackage\n";
let files = [("dut_a.sv", dut), ("tb.sv", TB), ("pkg.sv", pkg)];
left_and_named(&files, ("dut_a.sv", "logic ", "x"), "pkg.sv", 3);
}
/// QA R1 (E1, E2, E6, E3): a macro statement in a `begin` block, a task or a
/// function body, or a macro in the ANSI port list, may declare `st`/`bus`;
/// renaming the same-named module's member leaves the path and names it.
#[test]
fn qa_r1_a_macro_where_a_declaration_could_stand_blocks_rooting() {
let st = "module st;\n logic a;\nendmodule\n";
for (user, line) in [
("module user;\n initial begin\n `DECL_LOCAL(st)\n st.a = 1;\n end\nendmodule\n", 4),
("module user;\n task automatic t();\n `DECL_LOCAL(st)\n st.a = 1;\n endtask\nendmodule\n", 4),
("module user;\n function automatic void f();\n `DECL_LOCAL(st)\n begin st.a = 1; end\n endfunction\nendmodule\n", 4),
] {
left_and_named(&[("st.sv", st), ("user.sv", user)], ("st.sv", "logic ", "a"), "user.sv", line);
}
let bus = "module bus;\n logic data;\nendmodule\n";
let user = "module user (`MY_PORTS, output logic o);\n assign o = bus.data;\nendmodule\n";
let files = [("bus.sv", bus), ("user.sv", user)];
let (texts, _) = renamed(&files, "bus.sv", "logic ", "data", "d2");
assert_eq!(texts[1], user, "the macro port may declare `bus`");
}
/// The held shapes (E7, E8): a macro in a sibling generate block, or used as
/// a data type, declares no head on the path's scope chain — renamed.
#[test]
fn qa_r1_a_macro_off_the_scope_chain_or_as_a_type_still_renames() {
let st = "module st;\n logic a;\nendmodule\n";
for user in [
"module user;\n if (1) begin : g1\n `DECL_LOCAL(st)\n end\n if (1) begin : g2\n initial st.a = 1;\n end\nendmodule\n",
"module user;\n `MY_STRUCT_T other;\n initial st.a = 1;\nendmodule\n",
] {
let files = [("st.sv", st), ("user.sv", user)];
let (texts, warnings) = renamed(&files, "st.sv", "logic ", "a", "b");
assert_eq!(texts[1], user.replace("st.a", "st.b"), "{warnings:?}");
}
}
// Author: Guoliang Chen. Lucisv is made by Guoliang Chen with love, luciochen.com