307 lines
16 KiB
Rust
307 lines
16 KiB
Rust
// Author: Guoliang Chen, a Senior Digital Design Engineer
|
|
|
|
//! `rename_preflight` against the road it short-circuits, refusal by refusal.
|
|
//!
|
|
//! The preflight answers, from the request file alone, a question `rename`
|
|
//! would otherwise answer after parsing and analyzing the whole in-scope
|
|
//! workspace. That is only sound if the two answers are the *same sentence*,
|
|
//! so this file is the equality oracle: at **every byte** of seven hostile
|
|
//! buffers, and for every new name worth trying, the fast road's refusal must
|
|
//! be the slow road's refusal, word for word.
|
|
//!
|
|
//! Two directions, and only one of them is allowed:
|
|
//!
|
|
//! * the preflight refuses and the whole road refuses **differently**, or
|
|
//! accepts — an *invented* refusal, which is a rename the user cannot get.
|
|
//! Always a failure.
|
|
//! * the preflight says `Ok(())` and the whole road refuses — a *deferral*,
|
|
//! which is the normal case for everything the request file cannot settle.
|
|
//! Counted, never asserted away, and asserted to be non-zero where the
|
|
//! fixture is built to produce one.
|
|
|
|
use std::path::PathBuf;
|
|
|
|
use vlog_parser::parse_source_tolerant;
|
|
use vlog_refactor::{rename, rename_name_at, rename_preflight};
|
|
use vlog_semantic::analyze;
|
|
|
|
// ---------------------------------------------------------------------
|
|
// Fixtures
|
|
// ---------------------------------------------------------------------
|
|
|
|
const SUB: &str = "module sub #(parameter int W = 1) (input logic clk, output logic q);\n wire local_net;\n assign q = clk & local_net;\nendmodule : sub\n";
|
|
const TOP: &str = "module top;\n logic c, d;\n sub #(.W(2)) u_sub (.clk(c), .q(d));\n sub u_two (.clk(c), .q(d));\nendmodule\n";
|
|
const PKG: &str =
|
|
"package cfg_pkg;\n parameter int DEPTH = 4;\n typedef logic [1:0] mode_t;\nendpackage\n";
|
|
const USER: &str = "module user;\n import cfg_pkg::*;\n cfg_pkg::mode_t m;\n localparam int D = cfg_pkg::DEPTH;\nendmodule\n";
|
|
/// A name that resolves to nothing, and one that could only come from a base
|
|
/// class nothing in the workspace declares.
|
|
const STRAY: &str = "class kid extends absent_base;\n function void go();\n inherited_thing = 1;\n endfunction\nendclass\nmodule holder;\n missing u_missing (.a(b));\nendmodule\n";
|
|
/// Macro invocations, a generate block and a genvar.
|
|
const GEN: &str = "`define BUMP(x) x + 1\n`define WIDTH 8\nmodule gen_holder;\n genvar i;\n generate\n for (i = 0; i < 2; i++) begin : lane\n sub u_lane (.clk(1'b0), .q());\n end\n endgenerate\n wire [`WIDTH-1:0] w;\n assign w = `BUMP(sub);\nendmodule\n";
|
|
/// Every byte a cursor can sit on that is *not* a symbol: line comments,
|
|
/// block comments, a string literal, a system task, an attribute, and — for
|
|
/// the offsets in between — punctuation and whitespace.
|
|
const PROSE: &str = "// a leading line comment naming clk and sub\n/* a block comment\n naming local_net too */\nmodule prose;\n // clk here is prose\n logic clk;\n initial begin\n $display(\"clk and sub and local_net, all in a string\");\n end\n (* keep = \"true\" *)\n wire tagged_net;\nendmodule\n";
|
|
const BROKEN: &str = "module broken (input logic clk;\n wire a\n assign a = clk\nendmodule\n";
|
|
|
|
fn workspace(files: &[(&str, &str)]) -> Vec<(PathBuf, String)> {
|
|
files.iter().map(|(name, text)| (PathBuf::from(name), (*text).to_string())).collect()
|
|
}
|
|
|
|
// ---------------------------------------------------------------------
|
|
// The rename oracle
|
|
// ---------------------------------------------------------------------
|
|
|
|
/// Ask both roads the same rename at every byte of `file`, for every new name
|
|
/// worth asking about there, and return `(refused, deferred)`.
|
|
///
|
|
/// The new names are the four `check_new_name` shapes plus two that can only
|
|
/// be judged after the cursor is resolved: a novel valid name, and **the name
|
|
/// the cursor already spells**, which is the one way to reach the "is the name
|
|
/// it already has" refusal at an arbitrary offset.
|
|
fn rename_oracle(files: &[(&str, &str)], file: &str) -> (usize, usize) {
|
|
let sources = workspace(files);
|
|
let path = PathBuf::from(file);
|
|
let text = files
|
|
.iter()
|
|
.find(|(name, _)| *name == file)
|
|
.map(|(_, text)| *text)
|
|
.expect("the file is in the workspace");
|
|
let semantics = analyze(&parse_source_tolerant(text).0, text);
|
|
|
|
let (mut refused, mut deferred) = (0usize, 0usize);
|
|
for offset in 0..=text.len() as u32 {
|
|
let here = rename_name_at(&semantics, offset);
|
|
let mut names: Vec<&str> = vec!["", "module", "1bad", "has space", "renamed_thing"];
|
|
if let Some(name) = &here {
|
|
names.push(name.as_str());
|
|
}
|
|
for new_name in names {
|
|
let fast = rename_preflight(&semantics, offset, new_name);
|
|
let slow = rename(&sources, &path, offset, new_name);
|
|
match (&fast, &slow) {
|
|
(Err(fast), Err(slow)) => {
|
|
assert_eq!(
|
|
fast, slow,
|
|
"byte {offset} of {file} renamed to {new_name:?}: \
|
|
the preflight and the workspace refuse in different words"
|
|
);
|
|
refused += 1;
|
|
}
|
|
(Err(fast), Ok(_)) => panic!(
|
|
"byte {offset} of {file} renamed to {new_name:?}: the preflight \
|
|
invented the refusal {fast:?} for a rename the workspace performs"
|
|
),
|
|
// The preflight stood aside. Whatever the workspace then says
|
|
// is today's answer, unchanged — which is the whole contract.
|
|
(Ok(()), Err(_)) => deferred += 1,
|
|
(Ok(()), Ok(_)) => {}
|
|
}
|
|
}
|
|
}
|
|
(refused, deferred)
|
|
}
|
|
|
|
#[test]
|
|
fn the_preflight_refuses_a_rename_exactly_as_the_workspace_does() {
|
|
let files = [("sub.sv", SUB), ("top.sv", TOP)];
|
|
let (refused, _) = rename_oracle(&files, "top.sv");
|
|
assert!(refused > 0, "the sweep must actually reach the preflight's refusals");
|
|
rename_oracle(&files, "sub.sv");
|
|
}
|
|
|
|
#[test]
|
|
fn a_cursor_on_prose_is_refused_from_the_buffer_alone() {
|
|
// Comments, a string literal, a system task and an attribute: the whole
|
|
// "there is no symbol to rename at this position" family, which is the
|
|
// 2,192 ms refusal this preflight exists for.
|
|
let files = [("prose.sv", PROSE), ("sub.sv", SUB), ("top.sv", TOP)];
|
|
let (refused, _) = rename_oracle(&files, "prose.sv");
|
|
assert!(refused > 0);
|
|
|
|
// And named exactly, so a reworded refusal cannot hide inside the sweep.
|
|
let sources = workspace(&files);
|
|
let path = PathBuf::from("prose.sv");
|
|
let semantics = analyze(&parse_source_tolerant(PROSE).0, PROSE);
|
|
for (what, at) in [
|
|
("a line comment", PROSE.find("a leading line").expect("the comment")),
|
|
("a block comment", PROSE.find("naming local_net").expect("the comment")),
|
|
("a string literal", PROSE.find("all in a string").expect("the string")),
|
|
] {
|
|
let at = at as u32;
|
|
let fast = rename_preflight(&semantics, at, "renamed_thing").expect_err(what);
|
|
assert_eq!(fast, "there is no symbol to rename at this position.", "{what}");
|
|
assert_eq!(fast, rename(&sources, &path, at, "renamed_thing").expect_err(what), "{what}");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn an_unresolved_and_an_inherited_reference_keep_their_own_sentences() {
|
|
let files = [("stray.sv", STRAY), ("sub.sv", SUB)];
|
|
rename_oracle(&files, "stray.sv");
|
|
|
|
// Which byte carries which refusal is the binder's business, so the
|
|
// fixture is swept for the two sentences rather than told where they are —
|
|
// and both must be found, or the fixture has stopped exercising them.
|
|
let sources = workspace(&files);
|
|
let path = PathBuf::from("stray.sv");
|
|
let semantics = analyze(&parse_source_tolerant(STRAY).0, STRAY);
|
|
let (mut unresolved, mut inherited) = (0usize, 0usize);
|
|
for at in 0..=STRAY.len() as u32 {
|
|
let Err(fast) = rename_preflight(&semantics, at, "renamed_thing") else { continue };
|
|
if fast.contains("is not resolved to any declaration") {
|
|
unresolved += 1;
|
|
} else if fast.contains("could only come from the base class") {
|
|
inherited += 1;
|
|
} else {
|
|
continue;
|
|
}
|
|
let slow = rename(&sources, &path, at, "renamed_thing").expect_err("the same refusal");
|
|
assert_eq!(fast, slow, "at byte {at} of stray.sv");
|
|
}
|
|
assert!(unresolved > 0, "the fixture no longer holds a reference that resolves to nothing");
|
|
assert!(inherited > 0, "the fixture no longer holds an inherited candidate");
|
|
}
|
|
|
|
#[test]
|
|
fn a_macro_invocation_and_a_generate_block_keep_parity() {
|
|
rename_oracle(&[("gen.sv", GEN), ("sub.sv", SUB)], "gen.sv");
|
|
}
|
|
|
|
#[test]
|
|
fn a_buffer_that_does_not_parse_keeps_parity() {
|
|
rename_oracle(&[("broken.sv", BROKEN), ("sub.sv", SUB)], "broken.sv");
|
|
}
|
|
|
|
#[test]
|
|
fn a_package_and_its_qualified_users_keep_parity() {
|
|
let files = [("pkg.sv", PKG), ("user.sv", USER)];
|
|
rename_oracle(&files, "user.sv");
|
|
rename_oracle(&files, "pkg.sv");
|
|
}
|
|
|
|
#[test]
|
|
fn a_name_declared_in_two_files_stays_on_the_slow_road() {
|
|
// Two modules called `sub`. The ambiguity is the *index's* answer, and the
|
|
// index is rebuilt on save, never on a keystroke — so the preflight must
|
|
// not take this refusal, and the sweep must show it deferring instead.
|
|
let files = [("a.sv", SUB), ("b.sv", SUB), ("top.sv", TOP)];
|
|
let (_, deferred) = rename_oracle(&files, "top.sv");
|
|
assert!(deferred > 0, "the ambiguous declaration must reach the workspace to be refused");
|
|
|
|
let sources = workspace(&files);
|
|
let path = PathBuf::from("top.sv");
|
|
let semantics = analyze(&parse_source_tolerant(TOP).0, TOP);
|
|
let at = TOP.find("sub #").expect("the instantiation") as u32;
|
|
assert!(
|
|
rename_preflight(&semantics, at, "renamed_thing").is_ok(),
|
|
"the preflight must stand aside for a name the index answers ambiguously"
|
|
);
|
|
assert!(rename(&sources, &path, at, "renamed_thing").is_err(), "and the workspace refuses it");
|
|
}
|
|
|
|
/// TODO §63.1 QA. A `bind`'s connection expressions resolve in the *target's*
|
|
/// scope, which a single buffer never holds — so both shapes must defer, and
|
|
/// the fixture is swept to prove neither is ever an *invented* refusal.
|
|
const AES_FOR_BIND: &str = "module aes (input logic clk_i, output logic idle_o);\n \
|
|
logic entropy_masking_req;\n aes_core u_aes_core (.clk_i(clk_i));\n \
|
|
assign idle_o = entropy_masking_req;\nendmodule : aes\n";
|
|
const AES_CORE_FOR_BIND: &str =
|
|
"module aes_core (input logic clk_i);\n logic seed_en;\n assign seed_en = clk_i;\nendmodule : aes_core\n";
|
|
const RESEED_IF_FOR_BIND: &str =
|
|
"module aes_reseed_if (input logic entropy_masking_req, input logic seed_en);\nendmodule : aes_reseed_if\n";
|
|
const AES_BIND: &str = "module aes_bind;\n bind aes aes_reseed_if u_aes_reseed_if (\n \
|
|
.entropy_masking_req(entropy_masking_req),\n .seed_en(u_aes_core.seed_en)\n );\nendmodule\n";
|
|
|
|
#[test]
|
|
fn a_binds_connection_expressions_defer_rather_than_invent_a_refusal() {
|
|
// Before this lane a bare-name connection carried a one-segment external
|
|
// candidate (deferred already) but a hierarchical tail carried *no*
|
|
// reference at all — `symbol_at` finds no declaration either, so the
|
|
// preflight fell to `NOTHING_HERE`, a refusal the workspace does not
|
|
// agree with (it resolves and edits the tail). The oracle's own
|
|
// `(Err, Ok)` branch panics on exactly that shape, so a plain sweep is the
|
|
// proof: nothing here may ever be an invented refusal.
|
|
let files = [
|
|
("aes.sv", AES_FOR_BIND),
|
|
("aes_core.sv", AES_CORE_FOR_BIND),
|
|
("aes_reseed_if.sv", RESEED_IF_FOR_BIND),
|
|
("aes_bind.sv", AES_BIND),
|
|
];
|
|
let (_, deferred) = rename_oracle(&files, "aes_bind.sv");
|
|
assert!(deferred > 0, "every connection expression in a bind file defers to the workspace");
|
|
|
|
// And named exactly, at the hierarchical tail's own byte: this is the one
|
|
// Handshakes calls out as changed — `NOTHING_HERE` before, `Ok(())` now.
|
|
let semantics = analyze(&parse_source_tolerant(AES_BIND).0, AES_BIND);
|
|
let at =
|
|
AES_BIND.find("u_aes_core.seed_en").expect("the tail") as u32 + "u_aes_core.".len() as u32;
|
|
assert!(
|
|
rename_preflight(&semantics, at, "renamed_thing").is_ok(),
|
|
"the preflight must stand aside for a bind's hierarchical tail, which only the \
|
|
workspace can root at the bind target"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn renaming_a_symbol_to_the_name_it_already_has_is_refused_from_the_buffer() {
|
|
// A declaration outside any file-scope unit (here inside a `$unit`-level
|
|
// function) has no enclosing unit ambiguity check standing between the
|
|
// cursor and `already_named`, so the buffer-only preflight takes it.
|
|
let unit_fn = "function automatic int helper(int x);\n int local_var;\n local_var = x;\n return local_var;\nendfunction\n";
|
|
let fn_files = [("helper.sv", unit_fn)];
|
|
let fn_sources = workspace(&fn_files);
|
|
let fn_path = PathBuf::from("helper.sv");
|
|
let fn_semantics = analyze(&parse_source_tolerant(unit_fn).0, unit_fn);
|
|
let at_fn = unit_fn.find("local_var").expect("the local var") as u32;
|
|
let fast = rename_preflight(&fn_semantics, at_fn, "local_var").expect_err("the same name");
|
|
assert_eq!(fast, "`local_var` is the name it already has.");
|
|
assert_eq!(fast, rename(&fn_sources, &fn_path, at_fn, "local_var").expect_err("the same name"));
|
|
|
|
// Both a file-scope unit and a declaration inside one (`local_net` in
|
|
// `module sub`) check `enclosing_unit` ambiguity against the workspace
|
|
// index before `already_named`, so the buffer-only preflight defers both.
|
|
let semantics = analyze(&parse_source_tolerant(SUB).0, SUB);
|
|
let at_net = SUB.find("local_net").expect("the declaration") as u32;
|
|
assert!(rename_preflight(&semantics, at_net, "local_net").is_ok(), "deferred, not taken");
|
|
|
|
let at_unit = SUB.find("sub").expect("the module name") as u32;
|
|
assert!(rename_preflight(&semantics, at_unit, "sub").is_ok(), "deferred, not taken");
|
|
}
|
|
|
|
#[test]
|
|
fn an_invalid_new_name_outranks_every_cursor_refusal() {
|
|
// Precedence: `renaming` checks the new name before it builds a scope and
|
|
// before it looks at the cursor, so a bad name on a cursor that sits on
|
|
// nothing must produce the *name's* sentence, not the cursor's.
|
|
let files = [("prose.sv", PROSE), ("sub.sv", SUB)];
|
|
let sources = workspace(&files);
|
|
let path = PathBuf::from("prose.sv");
|
|
let semantics = analyze(&parse_source_tolerant(PROSE).0, PROSE);
|
|
let at = PROSE.find("a leading line").expect("the comment") as u32;
|
|
for (name, expected) in [
|
|
("", "a new name cannot be empty."),
|
|
("module", "`module` is a Verilog keyword."),
|
|
("1bad", "`1bad` is not a valid Verilog identifier."),
|
|
] {
|
|
let fast = rename_preflight(&semantics, at, name).expect_err(name);
|
|
assert_eq!(fast, expected, "{name}");
|
|
assert_eq!(fast, rename(&sources, &path, at, name).expect_err(name), "{name}");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn the_preflight_reads_only_the_buffer_it_was_given() {
|
|
// The proof behind the gate: resolving a cursor that sits on nothing costs
|
|
// no whole-file parse at all, however large the workspace around it.
|
|
let semantics = analyze(&parse_source_tolerant(PROSE).0, PROSE);
|
|
let at = PROSE.find("a leading line").expect("the comment") as u32;
|
|
let before = vlog_parser::stats::parses();
|
|
let refusal = rename_preflight(&semantics, at, "renamed_thing").expect_err("no symbol");
|
|
assert_eq!(vlog_parser::stats::parses() - before, 0, "the preflight parsed something");
|
|
assert_eq!(refusal, "there is no symbol to rename at this position.");
|
|
}
|
|
|
|
// Author: Guoliang Chen. Lucisv is made by Guoliang Chen with love, luciochen.com
|