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]>
305 lines
9.2 KiB
Rust
305 lines
9.2 KiB
Rust
// Author: Guoliang Chen, a Senior Digital Design Engineer
|
|
|
|
//! QA attacks on TODO §65.1's scope rules (a generate label declared in the
|
|
//! enclosing scope, `generate … endgenerate` not a scope, a block's member
|
|
//! scope, an index transparent in a path's spine).
|
|
//! `docs/features/generate-label-rename/README.md`, QA log 2026-09-30.
|
|
|
|
use vlog_parser::parse_source_tolerant;
|
|
use vlog_semantic::{analyze, Resolution, Semantics};
|
|
|
|
fn sem(src: &str) -> Semantics {
|
|
let (file, _) = parse_source_tolerant(src);
|
|
analyze(&file, src)
|
|
}
|
|
|
|
/// Offset of the `n`th (0-based) occurrence of `needle`, plus `skip` bytes.
|
|
fn at(src: &str, needle: &str, n: usize, skip: usize) -> u32 {
|
|
(src.match_indices(needle).nth(n).expect("occurrence must exist").0 + skip) as u32
|
|
}
|
|
|
|
fn resolution(s: &Semantics, offset: u32) -> Option<Resolution> {
|
|
s.reference_at(offset).map(|r| r.resolution.clone())
|
|
}
|
|
|
|
fn declared(s: &Semantics, src: &str, needle: &str, n: usize, skip: usize) -> Option<Resolution> {
|
|
let id = s.symbol_at(at(src, needle, n, skip)).expect("a declaration there");
|
|
Some(Resolution::Resolved(id))
|
|
}
|
|
|
|
#[test]
|
|
fn a_genvar_never_leaks_even_past_a_module_signal_of_its_name() {
|
|
let src = "module g (output logic o, output logic p);
|
|
logic i;
|
|
for (genvar i = 0; i < 2; i++) begin : g_l
|
|
logic q;
|
|
assign q = i[0];
|
|
end
|
|
assign o = i;
|
|
assign p = g_l[1].q;
|
|
endmodule
|
|
";
|
|
let s = sem(src);
|
|
assert_eq!(resolution(&s, at(src, "o = i", 0, 4)), declared(&s, src, "logic i", 0, 6));
|
|
assert_eq!(resolution(&s, at(src, "q = i", 0, 4)), declared(&s, src, "genvar i", 0, 7));
|
|
assert_eq!(resolution(&s, at(src, "g_l[1]", 0, 0)), declared(&s, src, ": g_l", 0, 2));
|
|
}
|
|
|
|
#[test]
|
|
fn a_member_lookup_through_a_block_never_climbs_to_the_enclosing_scope() {
|
|
// `g_l[0].clk` names a `clk` *inside* the block; there is none, so the
|
|
// module's port must never be the answer (a rename of `clk` would then
|
|
// rewrite a path that meant something else).
|
|
let src = "module m (input logic clk, output logic o, output logic p);
|
|
for (genvar i = 0; i < 2; i++) begin : g_l
|
|
logic q;
|
|
end
|
|
if (1) begin : g_b
|
|
logic r;
|
|
end
|
|
assign o = g_l[0].clk;
|
|
assign p = g_b.clk;
|
|
endmodule
|
|
";
|
|
let s = sem(src);
|
|
let port = declared(&s, src, "logic clk", 0, 6);
|
|
for (needle, n) in [(".clk", 0), (".clk", 1)] {
|
|
let got = resolution(&s, at(src, needle, n, 1));
|
|
assert_ne!(got, port, "{needle} #{n} climbed to the module port: {got:?}");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn an_unnamed_generate_if_hides_its_declarations_and_a_region_does_not() {
|
|
let src = "module u (output logic o, output logic p);
|
|
generate
|
|
wire in_region;
|
|
if (1) begin
|
|
wire hidden;
|
|
end
|
|
begin
|
|
wire in_bare;
|
|
end
|
|
endgenerate
|
|
assign o = in_region;
|
|
assign p = hidden | in_bare;
|
|
endmodule
|
|
";
|
|
let s = sem(src);
|
|
assert_eq!(
|
|
resolution(&s, at(src, "o = in_region", 0, 4)),
|
|
declared(&s, src, "wire in_r", 0, 5)
|
|
);
|
|
for (needle, decl) in [("p = hidden", "wire hidden"), ("| in_bare", "wire in_bare")] {
|
|
let got = resolution(&s, at(src, needle, 0, needle.find(' ').unwrap_or(0) + 3));
|
|
assert_ne!(got, declared(&s, src, decl, 0, 5), "{needle} saw into an unnamed block");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn a_block_label_named_like_a_module_is_the_lexical_answer() {
|
|
// IEEE 1800 §23.8: a name is looked up in the enclosing scopes before it
|
|
// is taken as a top-level module name.
|
|
let src = "module sub;
|
|
logic x;
|
|
endmodule
|
|
module m (output logic o);
|
|
if (1) begin : sub
|
|
logic x;
|
|
end
|
|
assign o = sub.x;
|
|
endmodule
|
|
";
|
|
let s = sem(src);
|
|
assert_eq!(resolution(&s, at(src, "sub.x", 0, 0)), declared(&s, src, ": sub", 0, 2));
|
|
assert_eq!(resolution(&s, at(src, "sub.x", 0, 4)), declared(&s, src, "logic x", 1, 6));
|
|
}
|
|
|
|
#[test]
|
|
fn an_element_select_never_resolves_to_a_declaration_of_another_type() {
|
|
let src = "module t (output logic o);
|
|
typedef struct packed { logic f; } pkt_t;
|
|
typedef struct packed { logic f; } other_t;
|
|
typedef pkt_t [1:0] vec_t;
|
|
pkt_t [1:0][1:0] two_d;
|
|
vec_t v;
|
|
logic [3:0] bits;
|
|
other_t f;
|
|
assign o = two_d[0][1].f | v[1].f | bits[0].f;
|
|
endmodule
|
|
";
|
|
let s = sem(src);
|
|
let pkt_f = declared(&s, src, "logic f", 0, 6);
|
|
assert_eq!(resolution(&s, at(src, "[1].f |", 0, 4)), pkt_f, "a 2-D packed array of pkt_t");
|
|
assert_eq!(resolution(&s, at(src, "v[1].f", 0, 5)), pkt_f, "through a packed-array typedef");
|
|
let got = resolution(&s, at(src, "bits[0].f", 0, 8));
|
|
assert!(
|
|
!matches!(got, Some(Resolution::Resolved(_))),
|
|
"`bits[0].f` has no field to name: {got:?}"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn an_index_through_a_class_queue_or_an_instance_array_is_not_a_wrong_answer() {
|
|
let src = "class item;
|
|
int m;
|
|
endclass
|
|
module leaf (output logic sig);
|
|
endmodule
|
|
module t (output logic o);
|
|
int m;
|
|
logic sig;
|
|
item q[$];
|
|
leaf u_arr [2] (.sig());
|
|
initial o = q[0].m == 0;
|
|
assign o = u_arr[1].sig;
|
|
endmodule
|
|
";
|
|
let s = sem(src);
|
|
let got = resolution(&s, at(src, "q[0].m", 0, 5));
|
|
assert_ne!(got, declared(&s, src, "int m", 1, 4), "the module's `m`: {got:?}");
|
|
let got = resolution(&s, at(src, "u_arr[1].sig", 0, 9));
|
|
assert_ne!(got, declared(&s, src, "logic sig", 1, 6), "the module's `sig`: {got:?}");
|
|
}
|
|
|
|
#[test]
|
|
fn a_procedural_block_in_a_function_is_one_label_with_disable_and_end() {
|
|
let src = "module f;
|
|
function automatic int ff(int a);
|
|
begin : fb
|
|
if (a == 0) disable fb;
|
|
ff = a;
|
|
end : fb
|
|
endfunction
|
|
task automatic tt();
|
|
fork : fk
|
|
join_none : fk
|
|
disable fk;
|
|
endtask
|
|
endmodule
|
|
";
|
|
let s = sem(src);
|
|
let fb = declared(&s, src, ": fb", 0, 2);
|
|
assert_eq!(resolution(&s, at(src, "disable fb", 0, 8)), fb);
|
|
assert_eq!(resolution(&s, at(src, "end : fb", 0, 6)), fb);
|
|
let fk = declared(&s, src, ": fk", 0, 2);
|
|
assert_eq!(resolution(&s, at(src, "join_none : fk", 0, 12)), fk);
|
|
assert_eq!(resolution(&s, at(src, "disable fk", 0, 8)), fk);
|
|
}
|
|
|
|
#[test]
|
|
fn sibling_loops_with_one_label_neither_crash_nor_resolve_into_the_wrong_body() {
|
|
let src = "module s (output logic o, output logic p);
|
|
for (genvar i = 0; i < 2; i++) begin : g
|
|
logic a;
|
|
end : g
|
|
for (genvar j = 0; j < 2; j++) begin : g
|
|
logic b;
|
|
end : g
|
|
assign o = g[0].a;
|
|
assign p = g[1].b;
|
|
endmodule
|
|
";
|
|
let s = sem(src);
|
|
let first = declared(&s, src, ": g", 0, 2);
|
|
for (needle, n) in [(": g", 1), (": g", 2), (": g", 3), ("g[0]", 0), ("g[1]", 0)] {
|
|
let skip = if needle.starts_with(':') { 2 } else { 0 };
|
|
assert_eq!(resolution(&s, at(src, needle, n, skip)), first, "{needle} #{n}");
|
|
}
|
|
assert_eq!(resolution(&s, at(src, ".a", 0, 1)), declared(&s, src, "logic a", 0, 6));
|
|
let got = resolution(&s, at(src, ".b", 0, 1));
|
|
assert_ne!(got, declared(&s, src, "logic a", 0, 6), "{got:?}");
|
|
}
|
|
|
|
#[test]
|
|
fn a_label_in_both_arms_of_an_ifdef_is_one_label() {
|
|
let src = "module d (output logic o);
|
|
`ifdef WIDE
|
|
for (genvar i = 0; i < 4; i++) begin : g_w
|
|
logic q;
|
|
end : g_w
|
|
`else
|
|
for (genvar i = 0; i < 2; i++) begin : g_w
|
|
logic q;
|
|
end : g_w
|
|
`endif
|
|
assign o = g_w[0].q;
|
|
endmodule
|
|
";
|
|
let s = sem(src);
|
|
let first = declared(&s, src, ": g_w", 0, 2);
|
|
for n in 1..4 {
|
|
assert_eq!(resolution(&s, at(src, ": g_w", n, 2)), first, "label #{n}");
|
|
}
|
|
assert_eq!(resolution(&s, at(src, "g_w[0]", 0, 0)), first);
|
|
}
|
|
|
|
#[test]
|
|
fn a_blocks_member_key_is_none_or_dotted_never_a_bare_colliding_name() {
|
|
// A named block inside a function: the function's own name is not a key
|
|
// the index files anything under, and `ff.fb` is exactly the key a
|
|
// module `ff`'s block `fb` is filed under.
|
|
let src = "module m;
|
|
function automatic void ff();
|
|
begin : fb
|
|
logic t;
|
|
t = 1'b0;
|
|
end
|
|
endfunction
|
|
endmodule
|
|
";
|
|
let s = sem(src);
|
|
let t = s.symbols_named("t").next().expect("t");
|
|
let key = s.symbol(t).and_then(|sym| s.member_key(sym.scope));
|
|
assert_ne!(key.as_deref(), Some("ff.fb"), "collides with module `ff`'s block `fb`");
|
|
// Fixed (F4): qualified by the enclosing unit, marked as the function's.
|
|
assert_eq!(key.as_deref(), Some("m.ff().fb"));
|
|
}
|
|
|
|
/// Re-check of F4: a named block in a package function, a class method, an
|
|
/// out-of-body class method and an interface task — never a key the index
|
|
/// files a module's block under.
|
|
#[test]
|
|
fn recheck_a_block_in_a_subroutine_anywhere_keys_by_its_unit_and_subroutine() {
|
|
let src = "package pk;
|
|
function automatic void pf();
|
|
begin : pb
|
|
logic pt;
|
|
end
|
|
endfunction
|
|
class cls;
|
|
function void cm();
|
|
begin : cb
|
|
logic ct;
|
|
end
|
|
endfunction
|
|
extern function void ext();
|
|
endclass
|
|
function void cls::ext();
|
|
begin : eb
|
|
logic et;
|
|
end
|
|
endfunction
|
|
endpackage
|
|
interface ifc;
|
|
task automatic it();
|
|
begin : ib
|
|
logic itv;
|
|
end
|
|
endtask
|
|
endinterface
|
|
";
|
|
let s = sem(src);
|
|
let key = |name: &str| {
|
|
let id = s.symbols_named(name).next().expect("declared");
|
|
s.symbol(id).and_then(|sym| s.member_key(sym.scope))
|
|
};
|
|
assert_eq!(key("pt").as_deref(), Some("pk.pf().pb"));
|
|
assert_eq!(key("ct").as_deref(), Some("cls.cm().cb"));
|
|
assert_eq!(key("itv").as_deref(), Some("ifc.it().ib"));
|
|
let et = key("et");
|
|
assert!(et.as_deref().is_none_or(|k| k.contains("()")), "{et:?}");
|
|
}
|
|
|
|
// Author: Guoliang Chen. Lucisv is made by Guoliang Chen with love, luciochen.com
|