Rename warnings: a macro argument the macro carries to the declaration is said wherever it is written (review H-1/H-2)
The home-set scoping dropped a bare macro argument outside the home set whatever the macro did with it, and never saw a `bind` produced by a macro — silent missed renames, regressions against 0.59.0 (`CONN(state_q)` building `.state_q(state_q)` broke the instantiation without a word). New rename/carry.rs decides per macro and formal position whether an argument is carried: after a `.` (past blanks and pastes), into a carried position of another macro, or by a macro that expands to a `bind` into the unit (every position; its body is news and a file invoking it is home). An undefined macro's arguments, one past the formals and a macro cycle are doubt and said. Tests: tests/rename_macro_carry.rs (8), carry.rs unit tests (4). Workspace lib/bins/tests: 80 suites, 5 076 passed, 0 failed, 51 ignored (guard exit 0). OpenTitan engine probe against carrying disabled: state_q 47 = 47, StIdle 0 = 0, cnt_q 13 = 13 warnings, timing unchanged. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
7794d3938b
commit
e8e22c2364
8 files changed
+580
-37
No files matched your search
+6
-1
@@ -69,7 +69,12 @@ TODO §65.1, topo §2.9 row 43 (MCP-14). (`docs/features/generate-label-rename`.
|
||||
as `uvm_macros.svh`), is warned about everywhere as before; an
|
||||
instantiating module's own same-named signal in a file the rename edits is
|
||||
not reported; a macro whose parameter is named like the declaration, and a
|
||||
backtick inside a string, are not.
|
||||
backtick inside a string, are not. A bare macro argument the macro carries
|
||||
to the declaration — after a `.` (`` `CONN(state_q)`` building
|
||||
`.state_q(state_q)`, `` `FORCE(state_q)`` building `force u_m.state_q`),
|
||||
into a `bind` into the declaring module, or into a macro lucisv has no
|
||||
definition of — is reported wherever it is written, and so is the body of
|
||||
a macro that expands to such a `bind`.
|
||||
- `duplicate-declaration` also reports a block label beside a port, genvar,
|
||||
typedef, function or task, and two named procedural blocks of one label in
|
||||
one scope.
|
||||
|
||||
@@ -0,0 +1,324 @@
|
||||
//! Which macro arguments can reach a module member from any file (TODO
|
||||
//! §65.1, independent review H-1/H-2, 2026-10-06).
|
||||
//!
|
||||
//! A bare spelling in another file's macro argument is that file's own name
|
||||
//! — unless the macro *carries* the argument somewhere a bare name is not:
|
||||
//! after a `.` (`force u_m.sig`, a named connection `.x(x)`), after a `.`
|
||||
//! through a `` `` `` paste (`` u_m.``sig ``), into a `bind` into the
|
||||
//! declaring unit (whose connections are evaluated in that unit), or into
|
||||
//! another macro's argument that is carried. Such an argument is news
|
||||
//! wherever it is written. So is an argument of a macro the set does not
|
||||
//! define, or one past the macro's formals: nothing shows it cannot reach.
|
||||
//!
|
||||
//! A `bind` is recognised where its keyword is spelled in a `` `define``
|
||||
//! body of the set (directly, or in a macro that body invokes); a macro that
|
||||
//! expands to one is a *binder*: every argument it takes is carried, its body
|
||||
//! is news (as a `bind` statement's would be), and a file invoking it is home
|
||||
//! (K2).
|
||||
|
||||
use std::collections::{HashMap, HashSet};
|
||||
|
||||
use super::contains_word;
|
||||
use super::home::{macro_names, skip_string};
|
||||
use super::scoped::{binds_into, spellings};
|
||||
|
||||
/// One `` `define`` of a macro: its formal names as written and its body.
|
||||
pub(crate) struct MacroDef {
|
||||
/// The formal arguments as written (`"a"`, `"b = 8"`); empty for an
|
||||
/// object-like macro.
|
||||
pub(crate) params: Vec<String>,
|
||||
/// The replacement text, line continuations joined.
|
||||
pub(crate) body: String,
|
||||
}
|
||||
|
||||
/// Macro name → every `` `define`` of it in the set.
|
||||
pub(crate) type Defs = HashMap<String, Vec<MacroDef>>;
|
||||
|
||||
/// Which arguments of which macros are carried, for one declaring unit.
|
||||
#[derive(Default)]
|
||||
pub(super) struct Carriers {
|
||||
/// Macro → per formal position, whether it is carried; `None` when every
|
||||
/// position is (a binder).
|
||||
formals: HashMap<String, Option<Vec<bool>>>,
|
||||
/// The macros that expand to a `bind` into the unit.
|
||||
binders: HashSet<String>,
|
||||
}
|
||||
|
||||
impl Carriers {
|
||||
/// The carried arguments of every macro in `defs`, for members of `unit`.
|
||||
pub(super) fn of(defs: &Defs, unit: &str) -> Carriers {
|
||||
let mut walk = Walk { defs, unit, binder: HashMap::new(), formals: HashMap::new() };
|
||||
let names: Vec<&String> = defs.keys().collect();
|
||||
for name in names {
|
||||
walk.formals_of(name, &mut HashSet::new());
|
||||
}
|
||||
let binders = walk.binder.into_iter().filter(|(_, b)| *b).map(|(n, _)| n).collect();
|
||||
Carriers { formals: walk.formals, binders }
|
||||
}
|
||||
|
||||
/// Whether argument `position` (0-based) of an invocation of
|
||||
/// `macro_name` may reach the declaration from any file.
|
||||
pub(super) fn carries(&self, macro_name: &str, position: usize) -> bool {
|
||||
match self.formals.get(macro_name) {
|
||||
Some(Some(formals)) => formals.get(position).copied().unwrap_or(true),
|
||||
Some(None) | None => true,
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether `macro_name` expands to a `bind` into the unit.
|
||||
pub(super) fn binds(&self, macro_name: &str) -> bool {
|
||||
self.binders.contains(macro_name)
|
||||
}
|
||||
|
||||
/// Whether any macro of the set is a binder.
|
||||
pub(super) fn any_binder(&self) -> bool {
|
||||
!self.binders.is_empty()
|
||||
}
|
||||
}
|
||||
|
||||
/// The memoized recursion over the set's macros.
|
||||
struct Walk<'a> {
|
||||
defs: &'a Defs,
|
||||
unit: &'a str,
|
||||
binder: HashMap<String, bool>,
|
||||
formals: HashMap<String, Option<Vec<bool>>>,
|
||||
}
|
||||
|
||||
impl Walk<'_> {
|
||||
/// Whether `name` expands to a `bind` into the unit, directly or through
|
||||
/// a macro its body invokes. A cycle (an illegal macro) answers `false`.
|
||||
fn is_binder(&mut self, name: &str, active: &mut HashSet<String>) -> bool {
|
||||
if let Some(&known) = self.binder.get(name) {
|
||||
return known;
|
||||
}
|
||||
let Some(defs) = self.defs.get(name) else { return false };
|
||||
if !active.insert(name.to_string()) {
|
||||
return false;
|
||||
}
|
||||
let mut found = defs.iter().any(|d| body_binds(&d.body, self.unit));
|
||||
if !found {
|
||||
let inner: Vec<String> = defs.iter().flat_map(|d| macro_names(&d.body)).collect();
|
||||
found = inner.iter().any(|n| self.is_binder(n, active));
|
||||
}
|
||||
active.remove(name);
|
||||
self.binder.insert(name.to_string(), found);
|
||||
found
|
||||
}
|
||||
|
||||
/// The carried formal positions of `name`; `None` when every position is
|
||||
/// (a binder, an unknown macro, or a cycle — nothing shows otherwise).
|
||||
fn formals_of(&mut self, name: &str, active: &mut HashSet<String>) -> Option<Vec<bool>> {
|
||||
if let Some(known) = self.formals.get(name) {
|
||||
return known.clone();
|
||||
}
|
||||
let defs = self.defs.get(name)?;
|
||||
if active.contains(name) {
|
||||
return None;
|
||||
}
|
||||
if self.is_binder(name, &mut HashSet::new()) {
|
||||
self.formals.insert(name.to_string(), None);
|
||||
return None;
|
||||
}
|
||||
active.insert(name.to_string());
|
||||
let mut carried: Vec<bool> = Vec::new();
|
||||
for def in defs {
|
||||
for (position, formal) in def.params.iter().enumerate() {
|
||||
let formal = formal.split('=').next().unwrap_or_default().trim();
|
||||
if carried.len() <= position {
|
||||
carried.resize(position + 1, false);
|
||||
}
|
||||
carried[position] |= self.formal_is_carried(&def.body, formal, active);
|
||||
}
|
||||
}
|
||||
active.remove(name);
|
||||
self.formals.insert(name.to_string(), Some(carried.clone()));
|
||||
Some(carried)
|
||||
}
|
||||
|
||||
/// Whether `body` spells `formal` after a `.`, or passes it to a macro
|
||||
/// argument that is carried.
|
||||
fn formal_is_carried(
|
||||
&mut self,
|
||||
body: &str,
|
||||
formal: &str,
|
||||
active: &mut HashSet<String>,
|
||||
) -> bool {
|
||||
if formal.is_empty() || !contains_word(body, formal) {
|
||||
return false;
|
||||
}
|
||||
if spellings(body, formal).1 {
|
||||
return true;
|
||||
}
|
||||
for (inner, arguments) in invocations(body) {
|
||||
for (position, argument) in arguments.iter().enumerate() {
|
||||
if !contains_word(argument, formal) {
|
||||
continue;
|
||||
}
|
||||
let carried = match self.formals_of(&inner, active) {
|
||||
Some(formals) => formals.get(position).copied().unwrap_or(true),
|
||||
None => true,
|
||||
};
|
||||
if carried {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether a `` `define`` body holds a `bind` into `unit`; a body that
|
||||
/// spells `bind` but does not lex is counted (doubt).
|
||||
fn body_binds(body: &str, unit: &str) -> bool {
|
||||
if !contains_word(body, "bind") {
|
||||
return false;
|
||||
}
|
||||
match vlog_parser::tokenize(body) {
|
||||
Ok(tokens) => !binds_into(&tokens, unit).is_empty(),
|
||||
Err(_) => true,
|
||||
}
|
||||
}
|
||||
|
||||
/// Every function-like macro invocation in `text` (strings and comments
|
||||
/// skipped): its name and its top-level argument texts. Invocations nested
|
||||
/// in another's arguments are listed too.
|
||||
fn invocations(text: &str) -> Vec<(String, Vec<&str>)> {
|
||||
let bytes = text.as_bytes();
|
||||
let mut out = Vec::new();
|
||||
let mut i = 0;
|
||||
while i < bytes.len() {
|
||||
match bytes[i] {
|
||||
b'"' => i = skip_string(bytes, i + 1),
|
||||
b'/' if bytes.get(i + 1) == Some(&b'/') => {
|
||||
i = bytes[i..].iter().position(|&b| b == b'\n').map_or(bytes.len(), |n| i + n)
|
||||
}
|
||||
b'/' if bytes.get(i + 1) == Some(&b'*') => {
|
||||
i = text[i + 2..].find("*/").map_or(bytes.len(), |n| i + 2 + n + 2)
|
||||
}
|
||||
// `` `" `` (stringify) and `` `` `` (paste) open nothing.
|
||||
b'`' if matches!(bytes.get(i + 1), Some(b'"' | b'`')) => i += 2,
|
||||
b'`' => {
|
||||
let len = text[i + 1..]
|
||||
.bytes()
|
||||
.take_while(|b| b.is_ascii_alphanumeric() || *b == b'_' || *b == b'$')
|
||||
.count();
|
||||
let name = &text[i + 1..i + 1 + len];
|
||||
i += 1 + len;
|
||||
let open = i + text[i..].len() - text[i..].trim_start().len();
|
||||
if !name.is_empty() && bytes.get(open) == Some(&b'(') {
|
||||
out.push((name.to_string(), split_arguments(&text[open + 1..]).0));
|
||||
}
|
||||
}
|
||||
_ => i += 1,
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// The top-level, comma-separated arguments of the list that starts right
|
||||
/// after its `(`, up to the matching `)` (or the end of `text`), with the
|
||||
/// byte offset of each argument's start.
|
||||
fn split_arguments(text: &str) -> (Vec<&str>, Vec<usize>) {
|
||||
let bytes = text.as_bytes();
|
||||
let (mut arguments, mut starts) = (Vec::new(), vec![0]);
|
||||
let (mut depth, mut start, mut i) = (0usize, 0usize, 0usize);
|
||||
while i < bytes.len() {
|
||||
match bytes[i] {
|
||||
b'"' => {
|
||||
i = skip_string(bytes, i + 1);
|
||||
continue;
|
||||
}
|
||||
b'(' | b'[' | b'{' => depth += 1,
|
||||
b')' | b']' | b'}' if depth == 0 => break,
|
||||
b')' | b']' | b'}' => depth -= 1,
|
||||
b',' if depth == 0 => {
|
||||
arguments.push(&text[start..i]);
|
||||
start = i + 1;
|
||||
starts.push(start);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
i += 1;
|
||||
}
|
||||
arguments.push(&text[start..i.min(text.len())]);
|
||||
(arguments, starts)
|
||||
}
|
||||
|
||||
/// The 0-based position of the argument holding byte `offset` of an
|
||||
/// invocation's argument text (the text right after its `(`).
|
||||
pub(super) fn argument_position(arguments: &str, offset: usize) -> usize {
|
||||
let (_, starts) = split_arguments(arguments);
|
||||
starts.iter().filter(|&&start| start <= offset).count().saturating_sub(1)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn defs(entries: &[(&str, &[&str], &str)]) -> Defs {
|
||||
let mut out = Defs::new();
|
||||
for (name, params, body) in entries {
|
||||
let params = params.iter().map(|p| p.to_string()).collect();
|
||||
out.entry(name.to_string())
|
||||
.or_default()
|
||||
.push(MacroDef { params, body: body.to_string() });
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_formal_after_a_dot_or_a_pasted_dot_is_carried_and_a_plain_one_is_not() {
|
||||
let set = defs(&[
|
||||
("CONN", &["x"], ".x(x)"),
|
||||
("FORCE", &["sig"], "force u_m.sig = 1'b0;"),
|
||||
("PEEK", &["sig"], "u_m.``sig"),
|
||||
("ASSERT", &["n", "e"], "n: assert property (e);"),
|
||||
("STR", &["s"], "`\"s`\""),
|
||||
]);
|
||||
let carriers = Carriers::of(&set, "m");
|
||||
assert!(carriers.carries("CONN", 0));
|
||||
assert!(carriers.carries("FORCE", 0));
|
||||
assert!(carriers.carries("PEEK", 0));
|
||||
assert!(!carriers.carries("ASSERT", 0) && !carriers.carries("ASSERT", 1));
|
||||
assert!(!carriers.carries("STR", 0));
|
||||
assert!(carriers.carries("ASSERT", 2), "past the formals");
|
||||
assert!(carriers.carries("UNKNOWN", 0), "a macro the set does not define");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_formal_passed_on_is_carried_only_when_the_inner_position_is() {
|
||||
let set = defs(&[
|
||||
("OUTER", &["a", "b"], "`INNER(a, b)"),
|
||||
("INNER", &["p", "q"], "p + top.q"),
|
||||
("ELSEWHERE", &["a"], "`NOT_IN_SET(a)"),
|
||||
("LOOP", &["a"], "`LOOP(a)"),
|
||||
]);
|
||||
let carriers = Carriers::of(&set, "m");
|
||||
assert!(!carriers.carries("OUTER", 0));
|
||||
assert!(carriers.carries("OUTER", 1));
|
||||
assert!(carriers.carries("ELSEWHERE", 0));
|
||||
assert!(carriers.carries("LOOP", 0), "a cycle is doubt");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_macro_expanding_to_a_bind_into_the_unit_is_a_binder_through_other_macros() {
|
||||
let set = defs(&[
|
||||
("BIND_CHK", &[], "bind m chk u_chk (.s(state_q));"),
|
||||
("WRAP", &["x"], "`BIND_CHK x"),
|
||||
("OTHER", &["x"], "bind n chk u (.s(x));"),
|
||||
]);
|
||||
let carriers = Carriers::of(&set, "m");
|
||||
assert!(carriers.binds("BIND_CHK") && carriers.binds("WRAP"));
|
||||
assert!(carriers.carries("WRAP", 0));
|
||||
assert!(!carriers.binds("OTHER") && !carriers.carries("OTHER", 0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_argument_position_counts_top_level_commas_only() {
|
||||
let args = "a, f(b, c), {d, e}, \"x,y\", g)";
|
||||
assert_eq!(argument_position(args, 0), 0);
|
||||
assert_eq!(argument_position(args, args.find('c').unwrap_or(0)), 1);
|
||||
assert_eq!(argument_position(args, args.find('e').unwrap_or(0)), 2);
|
||||
assert_eq!(argument_position(args, args.find('g').unwrap_or(0)), 4);
|
||||
}
|
||||
}
|
||||
@@ -15,6 +15,8 @@
|
||||
use std::collections::{BTreeSet, HashMap, HashSet};
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use super::carry::Defs;
|
||||
|
||||
/// The home set of the file at `declaring` in `files`, plus `edited`, as
|
||||
/// paths — what `lucisv-mcp` scopes a member's mentions with. `None` when the
|
||||
/// set cannot be known: some include of it names no file of the set, or is
|
||||
@@ -189,21 +191,17 @@ fn resolve(
|
||||
}
|
||||
}
|
||||
|
||||
/// Every macro name invoked in `texts`, and — through the bodies in `bodies`
|
||||
/// (macro name → its `` `define`` bodies) — every macro those invoke, and so
|
||||
/// on: what can expand inside the home set.
|
||||
pub(crate) fn invoked_from(
|
||||
texts: &[&str],
|
||||
bodies: &HashMap<String, Vec<String>>,
|
||||
) -> HashSet<String> {
|
||||
/// Every macro name invoked in `texts`, and — through the bodies in `defs` —
|
||||
/// every macro those invoke, and so on: what can expand inside the home set.
|
||||
pub(crate) fn invoked_from(texts: &[&str], defs: &Defs) -> HashSet<String> {
|
||||
let mut invoked: HashSet<String> = HashSet::new();
|
||||
let mut todo: Vec<String> = texts.iter().flat_map(|t| macro_names(t)).collect();
|
||||
while let Some(name) = todo.pop() {
|
||||
if !invoked.insert(name.clone()) {
|
||||
continue;
|
||||
}
|
||||
for body in bodies.get(&name).into_iter().flatten() {
|
||||
todo.extend(macro_names(body));
|
||||
for def in defs.get(&name).into_iter().flatten() {
|
||||
todo.extend(macro_names(&def.body));
|
||||
}
|
||||
}
|
||||
invoked
|
||||
@@ -212,7 +210,7 @@ pub(crate) fn invoked_from(
|
||||
/// The names after every backtick in `text` that stands in code — not inside
|
||||
/// a string literal or a comment, where a backtick invokes nothing (QA N2).
|
||||
/// Directives are included; no `` `define`` can be named like one.
|
||||
fn macro_names(text: &str) -> Vec<String> {
|
||||
pub(super) fn macro_names(text: &str) -> Vec<String> {
|
||||
let mut out = Vec::new();
|
||||
let bytes = text.as_bytes();
|
||||
let mut i = 0;
|
||||
@@ -242,7 +240,7 @@ fn macro_names(text: &str) -> Vec<String> {
|
||||
}
|
||||
|
||||
/// The offset just past the string literal whose body starts at `i`.
|
||||
fn skip_string(bytes: &[u8], mut i: usize) -> usize {
|
||||
pub(super) fn skip_string(bytes: &[u8], mut i: usize) -> usize {
|
||||
while i < bytes.len() {
|
||||
match bytes[i] {
|
||||
b'\\' => i += 2,
|
||||
@@ -255,6 +253,7 @@ fn skip_string(bytes: &[u8], mut i: usize) -> usize {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::super::carry::MacroDef;
|
||||
use super::*;
|
||||
|
||||
fn set(files: &[&str]) -> Vec<(PathBuf, String)> {
|
||||
@@ -317,13 +316,11 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn invocations_are_followed_through_define_bodies() {
|
||||
let bodies: HashMap<String, Vec<String>> = [
|
||||
("A".to_string(), vec!["`B(x)".to_string()]),
|
||||
("B".to_string(), vec!["clk_i".to_string()]),
|
||||
]
|
||||
.into_iter()
|
||||
.collect();
|
||||
let invoked = invoked_from(&[" `A\n"], &bodies);
|
||||
let def = |body: &str| vec![MacroDef { params: Vec::new(), body: body.to_string() }];
|
||||
let defs: Defs = [("A".to_string(), def("`B(x)")), ("B".to_string(), def("clk_i"))]
|
||||
.into_iter()
|
||||
.collect();
|
||||
let invoked = invoked_from(&[" `A\n"], &defs);
|
||||
assert!(invoked.contains("A") && invoked.contains("B"));
|
||||
}
|
||||
}
|
||||
@@ -19,6 +19,7 @@
|
||||
//! over `home`'s include-derived home set.
|
||||
|
||||
mod arms;
|
||||
mod carry;
|
||||
mod connections;
|
||||
mod home;
|
||||
use connections::{combine, fuse};
|
||||
|
||||
@@ -21,14 +21,17 @@
|
||||
//! declaration (N1 — another module's own `clk_i` is not); in a file pasted
|
||||
//! into or around the declaring file always (no scope to look in — H1);
|
||||
//! inside a `bind` into the declaring unit wherever it is written (H3);
|
||||
//! elsewhere never;
|
||||
//! in an argument the macro carries after a `.`, into a `bind` into the
|
||||
//! unit or into a macro the set does not define, wherever it is written
|
||||
//! (`carry.rs`, review H-1/H-2); elsewhere never;
|
||||
//! * a **dotted** spelling (`… .state_q`) may be a hierarchical path and is
|
||||
//! always news;
|
||||
//! * a `` `define`` body is news when it is written in the home set, when its
|
||||
//! macro is invoked from the home set — a file with a `bind` into the unit
|
||||
//! counts (K2) — through other bodies too (H2), or when it spells the name
|
||||
//! dotted; a macro with a **parameter** named like the declaration never
|
||||
//! is (N2: the body's spellings are the parameter);
|
||||
//! macro is invoked from the home set — a file with a `bind` into the unit,
|
||||
//! or invoking a macro that expands to one, counts (K2) — through other
|
||||
//! bodies too (H2), when the macro itself expands to such a `bind`, or when
|
||||
//! it spells the name dotted; a macro with a **parameter** named like the
|
||||
//! declaration never is (N2: the body's spellings are the parameter);
|
||||
//! * a bare use in the plain code of a pasted file is a use the engine
|
||||
//! cannot edit, and is said (H4, `Scope::fragment_use_warnings`) — for
|
||||
//! every renamed declaration, scoped or not.
|
||||
@@ -37,13 +40,14 @@
|
||||
//! class's member — can be named bare from any file (an import, a `pkg::`, a
|
||||
//! handle), and every spelling is reported as before.
|
||||
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::collections::HashSet;
|
||||
|
||||
use vlog_parser::span::TextRange;
|
||||
use vlog_parser::token::{Token, TokenKind};
|
||||
use vlog_semantic::{ScopeId, Semantics, SymbolId};
|
||||
|
||||
use super::home::{home_of, invoked_from, Home};
|
||||
use super::carry::{Carriers, Defs, MacroDef};
|
||||
use super::home::{home_of, invoked_from, macro_names, Home};
|
||||
use super::sites::nearest;
|
||||
use super::Target;
|
||||
use crate::scope::Scope;
|
||||
@@ -63,6 +67,9 @@ pub(super) struct Scoping {
|
||||
home: Home,
|
||||
/// Every macro a home-set file invokes, through define bodies.
|
||||
invoked: HashSet<String>,
|
||||
/// The macros whose arguments reach the unit from any file, and those
|
||||
/// expanding to a `bind` into it (`carry.rs`).
|
||||
carriers: Carriers,
|
||||
}
|
||||
|
||||
impl Scoping {
|
||||
@@ -77,10 +84,13 @@ impl Scoping {
|
||||
let mut home = home_of(&sources, target.file, &edited);
|
||||
let scoped = unit.is_some() && !home.unknown;
|
||||
let mut invoked = HashSet::new();
|
||||
let mut carriers = Carriers::default();
|
||||
if let (Some(unit), true) = (&unit, scoped) {
|
||||
home.all.extend(files_binding_into(scope, unit));
|
||||
let defs = define_defs(scope);
|
||||
carriers = Carriers::of(&defs, unit);
|
||||
home.all.extend(files_binding_into(scope, unit, &carriers));
|
||||
let texts: Vec<&str> = home.all.iter().filter_map(|&i| scope.text(i)).collect();
|
||||
invoked = invoked_from(&texts, &define_bodies(scope));
|
||||
invoked = invoked_from(&texts, &defs);
|
||||
}
|
||||
Scoping {
|
||||
unit,
|
||||
@@ -90,6 +100,7 @@ impl Scoping {
|
||||
name: target.name.clone(),
|
||||
home,
|
||||
invoked,
|
||||
carriers,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -135,7 +146,15 @@ impl Scoping {
|
||||
if shadowed && !default_bare {
|
||||
return false;
|
||||
}
|
||||
self.home.all.contains(&file) || self.invoked.contains(macro_name)
|
||||
self.home.all.contains(&file)
|
||||
|| self.invoked.contains(macro_name)
|
||||
|| self.carriers.binds(macro_name)
|
||||
}
|
||||
|
||||
/// Whether argument `position` of an invocation of `macro_name` may
|
||||
/// reach the declaration wherever it is written (`carry.rs`).
|
||||
pub(super) fn carries(&self, macro_name: &str, position: usize) -> bool {
|
||||
!self.scoped || self.carriers.carries(macro_name, position)
|
||||
}
|
||||
|
||||
/// Whether a spelling in `file` — dotted or bare, at `offset` — is news.
|
||||
@@ -160,11 +179,18 @@ impl Scoping {
|
||||
}
|
||||
|
||||
/// K2: every file holding a `bind` into `unit` (by name or by an instance
|
||||
/// path) — home, so the macros it invokes are followed.
|
||||
fn files_binding_into(scope: &Scope, unit: &str) -> Vec<usize> {
|
||||
/// path), or invoking a macro that expands to one — home, so the macros it
|
||||
/// invokes are followed.
|
||||
fn files_binding_into(scope: &Scope, unit: &str, carriers: &Carriers) -> Vec<usize> {
|
||||
(0..scope.len())
|
||||
.filter(|&i| {
|
||||
let Some(text) = scope.text(i) else { return false };
|
||||
let invokes_binder = carriers.any_binder()
|
||||
&& text.contains('`')
|
||||
&& macro_names(text).iter().any(|m| carriers.binds(m));
|
||||
if invokes_binder {
|
||||
return true;
|
||||
}
|
||||
if !super::contains_word(text, "bind") {
|
||||
return false;
|
||||
}
|
||||
@@ -252,15 +278,17 @@ pub(super) fn binds_into(tokens: &[Token], unit: &str) -> Vec<TextRange> {
|
||||
out
|
||||
}
|
||||
|
||||
/// Macro name → every `` `define`` body of it in the set.
|
||||
fn define_bodies(scope: &Scope) -> HashMap<String, Vec<String>> {
|
||||
let mut out: HashMap<String, Vec<String>> = HashMap::new();
|
||||
/// Macro name → every `` `define`` of it in the set.
|
||||
fn define_defs(scope: &Scope) -> Defs {
|
||||
let mut out = Defs::new();
|
||||
for i in 0..scope.len() {
|
||||
let Some(defines) = scope.file_id(i).and_then(|id| scope.index().defines(id)) else {
|
||||
continue;
|
||||
};
|
||||
for define in defines.entries() {
|
||||
out.entry(define.name.clone()).or_default().push(define.body.clone());
|
||||
let params = define.params.clone().unwrap_or_default();
|
||||
let def = MacroDef { params, body: define.body.clone() };
|
||||
out.entry(define.name.clone()).or_default().push(def);
|
||||
}
|
||||
}
|
||||
out
|
||||
@@ -306,7 +334,7 @@ pub(super) fn spellings(text: &str, name: &str) -> (Vec<usize>, bool) {
|
||||
if glued {
|
||||
continue;
|
||||
}
|
||||
match text[..start].trim_end().ends_with('.') {
|
||||
match after_dot(&text[..start]) {
|
||||
true => dotted = true,
|
||||
false => bare.push(start),
|
||||
}
|
||||
@@ -314,6 +342,16 @@ pub(super) fn spellings(text: &str, name: &str) -> (Vec<usize>, bool) {
|
||||
(bare, dotted)
|
||||
}
|
||||
|
||||
/// Whether `before` ends in a `.`, past blanks and `` `` `` pastes
|
||||
/// (`` u_m.``sig `` pastes a path).
|
||||
fn after_dot(before: &str) -> bool {
|
||||
let mut rest = before.trim_end();
|
||||
while let Some(shorter) = rest.strip_suffix("``") {
|
||||
rest = shorter.trim_end();
|
||||
}
|
||||
rest.ends_with('.')
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
@@ -323,5 +361,7 @@ mod tests {
|
||||
assert_eq!(spellings("x(state_q, a.state_q)", "state_q"), (vec![2], true));
|
||||
assert_eq!(spellings("x(u . state_q)", "state_q"), (vec![], true));
|
||||
assert_eq!(spellings("x(state_qq, my_state_q)", "state_q"), (vec![], false));
|
||||
assert_eq!(spellings("u_m.``state_q", "state_q"), (vec![], true));
|
||||
assert_eq!(spellings("pre``state_q", "state_q"), (vec![5], false));
|
||||
}
|
||||
}
|
||||
@@ -25,6 +25,7 @@ use vlog_semantic::{ExternalTarget, ReferenceId, Resolution};
|
||||
|
||||
use crate::TextEdit;
|
||||
|
||||
use super::carry::argument_position;
|
||||
use super::scoped::{binds_into, spellings, Scoping};
|
||||
use super::Target;
|
||||
use crate::scope::Scope;
|
||||
@@ -251,11 +252,19 @@ impl<'a> Scope<'a> {
|
||||
_ => Vec::new(),
|
||||
};
|
||||
let bound = |at: u32| binds.iter().any(|r| r.start <= at && at < r.end);
|
||||
// A bare argument the macro carries after a `.`, into a `bind` or into
|
||||
// a macro the set does not define may reach the declaration from here
|
||||
// (`carry.rs`, review H-1/H-2).
|
||||
let news = |site: &ArgumentSite, at: u32, position: usize| {
|
||||
bound(at)
|
||||
|| scoping.carries(&site.macro_name, position)
|
||||
|| scoping.keeps(self, file, false, at)
|
||||
};
|
||||
sites
|
||||
.into_iter()
|
||||
.filter(|site| {
|
||||
site.dotted
|
||||
|| site.bare.iter().any(|&at| bound(at) || scoping.keeps(self, file, false, at))
|
||||
|| site.bare.iter().zip(&site.positions).any(|(&at, &k)| news(site, at, k))
|
||||
})
|
||||
.map(|site| {
|
||||
format!(
|
||||
@@ -431,6 +440,7 @@ impl<'a> Scope<'a> {
|
||||
macro_name: macro_name_of(&token.text).to_string(),
|
||||
line: lines.line_col(token.span.start).0,
|
||||
dotted,
|
||||
positions: bare.iter().map(|&b| argument_position(arguments, b)).collect(),
|
||||
bare: bare.into_iter().map(at).collect(),
|
||||
});
|
||||
}
|
||||
@@ -595,6 +605,8 @@ pub(crate) struct ArgumentSite {
|
||||
pub(crate) dotted: bool,
|
||||
/// The file offsets of the argument spellings with no `.` before them.
|
||||
pub(crate) bare: Vec<u32>,
|
||||
/// For each of `bare`, the 0-based position of the argument holding it.
|
||||
pub(crate) positions: Vec<usize>,
|
||||
}
|
||||
|
||||
/// Why an identifier spelling the name is not a reference lucisv can edit,
|
||||
|
||||
@@ -0,0 +1,138 @@
|
||||
//! A bare macro argument in another module is that module's own name —
|
||||
//! unless the macro carries it to the renamed declaration: after a `.`
|
||||
//! (`force u_m.sig`, a named connection `.x(x)`), through a pasted path
|
||||
//! (`` u_m.``sig ``), into a `bind` into the declaring module, or into a
|
||||
//! macro the set does not define (TODO §65.1, independent review H-1/H-2,
|
||||
//! 2026-10-06). Each of these was warned about in 0.59.0, silent after the
|
||||
//! scoping rework, and is warned about again.
|
||||
|
||||
use std::path::PathBuf;
|
||||
|
||||
use vlog_refactor::rename;
|
||||
|
||||
/// `m`'s `state_q` is an input port; every test renames it.
|
||||
const M: &str = "module m (input logic clk, input logic state_q);
|
||||
logic r;
|
||||
always_ff @(posedge clk) r <= state_q;
|
||||
endmodule
|
||||
";
|
||||
|
||||
/// The warnings of renaming `m.state_q` to `state_r` among `files` (plus
|
||||
/// `m.sv`).
|
||||
fn warnings_of(files: &[(&str, &str)]) -> Vec<String> {
|
||||
let mut sources = vec![(PathBuf::from("/rtl/m.sv"), M.to_string())];
|
||||
sources.extend(files.iter().map(|(n, t)| (PathBuf::from("/rtl").join(n), t.to_string())));
|
||||
let at = M.find("logic state_q").expect("declaration") as u32 + 6;
|
||||
rename(&sources, &PathBuf::from("/rtl/m.sv"), at, "state_r").expect("renames").warnings
|
||||
}
|
||||
|
||||
/// Whether some warning names `file:line`.
|
||||
fn said(warnings: &[String], file: &str, line: u32) -> bool {
|
||||
warnings.iter().any(|w| w.contains(&format!("{file}:{line}")))
|
||||
}
|
||||
|
||||
/// PC: the instantiation is broken by the rename and must not be silent.
|
||||
#[test]
|
||||
fn a_named_connection_a_macro_builds_from_a_bare_argument_is_said() {
|
||||
let top = "module top (input logic clk, input logic state_q);
|
||||
m u_m (.clk(clk), `CONN(state_q));
|
||||
endmodule
|
||||
";
|
||||
let warnings = warnings_of(&[("defs.svh", "`define CONN(x) .x(x)\n"), ("top.sv", top)]);
|
||||
assert!(said(&warnings, "top.sv", 2), "{warnings:#?}");
|
||||
}
|
||||
|
||||
/// PA: a testbench forcing a path the macro builds from its argument.
|
||||
#[test]
|
||||
fn a_bare_argument_the_macro_puts_after_a_dot_is_said() {
|
||||
let tb = "module tb;
|
||||
logic clk;
|
||||
m u_m (.clk(clk), .state_q(1'b0));
|
||||
initial begin
|
||||
`FORCE(state_q)
|
||||
end
|
||||
endmodule
|
||||
";
|
||||
let defs = "`define FORCE(sig) force u_m.sig = 1'b0;\n";
|
||||
let warnings = warnings_of(&[("defs.svh", defs), ("tb.sv", tb)]);
|
||||
assert!(said(&warnings, "tb.sv", 5), "{warnings:#?}");
|
||||
}
|
||||
|
||||
/// PA2: the same path through a `` `` `` paste.
|
||||
#[test]
|
||||
fn a_bare_argument_pasted_after_a_dot_is_said() {
|
||||
let tb = "module tb;
|
||||
logic clk, o;
|
||||
m u_m (.clk(clk), .state_q(1'b0));
|
||||
assign o = `PEEK(state_q);
|
||||
endmodule
|
||||
";
|
||||
let warnings = warnings_of(&[("defs.svh", "`define PEEK(sig) u_m.``sig\n"), ("tb.sv", tb)]);
|
||||
assert!(said(&warnings, "tb.sv", 4), "{warnings:#?}");
|
||||
}
|
||||
|
||||
/// A macro passing its argument on to one that carries it carries it too;
|
||||
/// the position it passes on is the one that counts.
|
||||
#[test]
|
||||
fn an_argument_passed_on_to_a_carrying_macro_is_said_and_another_is_not() {
|
||||
let defs =
|
||||
"`define FORCE(sig) force u_m.sig = 1'b0;\n`define BOTH(v, sig) v = 1'b1; `FORCE(sig)\n";
|
||||
let tb = "module tb;
|
||||
logic clk, state_q;
|
||||
m u_m (.clk(clk), .state_q(1'b0));
|
||||
initial `BOTH(state_q, r);
|
||||
initial `BOTH(x, state_q);
|
||||
endmodule
|
||||
";
|
||||
let warnings = warnings_of(&[("defs.svh", defs), ("tb.sv", tb)]);
|
||||
assert!(!said(&warnings, "tb.sv", 4), "tb's own state_q: {warnings:#?}");
|
||||
assert!(said(&warnings, "tb.sv", 5), "{warnings:#?}");
|
||||
}
|
||||
|
||||
/// PD: a macro whose body is a `bind` into `m`, invoked from a file that is
|
||||
/// not home — the body's `state_q` is evaluated in `m`.
|
||||
#[test]
|
||||
fn a_define_body_that_binds_into_the_module_is_said_wherever_it_is_invoked() {
|
||||
let defs = "`define BIND_CHK bind m chk u_chk (.s(state_q));\n";
|
||||
let chk = "module chk (input logic s);\nendmodule\n";
|
||||
let binds = "module binds;\n `BIND_CHK\nendmodule\n";
|
||||
let warnings = warnings_of(&[("defs.svh", defs), ("chk.sv", chk), ("binds.sv", binds)]);
|
||||
assert!(said(&warnings, "defs.svh", 1), "{warnings:#?}");
|
||||
}
|
||||
|
||||
/// PD with the name passed in: every argument of a binder is carried.
|
||||
#[test]
|
||||
fn a_bare_argument_of_a_macro_that_binds_into_the_module_is_said() {
|
||||
let defs = "`define BIND_S(sig) bind m chk u_chk (.s(sig));\n";
|
||||
let chk = "module chk (input logic s);\nendmodule\n";
|
||||
let binds = "module binds;\n `BIND_S(state_q)\nendmodule\n";
|
||||
let warnings = warnings_of(&[("defs.svh", defs), ("chk.sv", chk), ("binds.sv", binds)]);
|
||||
assert!(said(&warnings, "binds.sv", 2), "{warnings:#?}");
|
||||
}
|
||||
|
||||
/// Nothing shows what an undefined macro does with its argument.
|
||||
#[test]
|
||||
fn a_bare_argument_of_a_macro_the_set_does_not_define_is_said() {
|
||||
let tb = "module tb;
|
||||
logic state_q;
|
||||
initial `NOWHERE(state_q)
|
||||
endmodule
|
||||
";
|
||||
let warnings = warnings_of(&[("tb.sv", tb)]);
|
||||
assert!(said(&warnings, "tb.sv", 3), "{warnings:#?}");
|
||||
}
|
||||
|
||||
/// The scoping is kept where it is provable: a macro that uses its argument
|
||||
/// as a plain expression, in a module with its own `state_q`, is silent.
|
||||
#[test]
|
||||
fn a_plain_expression_macro_in_another_module_stays_silent() {
|
||||
let defs =
|
||||
"`define ASSERT(n, e) n: assert property (@(posedge clk) e);\n`define CONN(x) .x(x)\n";
|
||||
let other = "module other (input logic clk);
|
||||
logic state_q;
|
||||
`ASSERT(Own, state_q)
|
||||
endmodule
|
||||
";
|
||||
let warnings = warnings_of(&[("defs.svh", defs), ("other.sv", other)]);
|
||||
assert!(!said(&warnings, "other.sv", 3), "{warnings:#?}");
|
||||
}
|
||||
@@ -525,6 +525,32 @@ run was **cut by the guard** at 1 700 s while starting
|
||||
stall); the one re-run: 78 suites, 5 059 passed, 0 failed, 51 ignored;
|
||||
doc-tests 6 suites, 1 passed; corpus 814/820.
|
||||
|
||||
**2026-10-06 — fix for the independent review's H-1/H-2 (`oct_review.md`).**
|
||||
A bare macro argument outside the home set was dropped whatever the macro
|
||||
did with it, and a `bind` produced by a macro was invisible to K2/H3 — both
|
||||
silent missed renames, regressions against 0.59.0 (`.x(x)` built by
|
||||
`` `CONN(state_q)`` broke the instantiation without a word). New
|
||||
`vlog_refactor/src/rename/carry.rs` decides, per macro and formal position,
|
||||
whether an argument is *carried*: the body spells the formal after a `.`
|
||||
(past blanks and `` `` `` pastes), passes it to a carried position of another
|
||||
macro, or the macro expands to a `bind` into the declaring unit (a
|
||||
*binder*, directly or through a macro its body invokes; every position
|
||||
carried). A carried bare argument is news wherever it is written; so is an
|
||||
argument of a macro the set does not define, one past the formals, and one
|
||||
in a macro cycle (doubt). A binder's body is news (`keeps_define_body`) and
|
||||
a file invoking a binder is home (K2). `scoped::spellings` now also sees a
|
||||
`.` through a paste (`` u_m.``state_q``), which only adds warnings.
|
||||
Assumption: a `bind` is recognised only where `bind` is spelled in a
|
||||
`` `define`` body of the set; an undefined macro's arguments are carried, but
|
||||
the macro is not taken to expand to a `bind`. Tests:
|
||||
`vlog_refactor/tests/rename_macro_carry.rs` (8: PC, PA, PA2, a passed-on
|
||||
position and a non-carried one, PD body and argument, an undefined macro,
|
||||
and a plain-expression macro that stays silent) and 4 unit tests in
|
||||
`carry.rs`. OpenTitan (engine probe, whole `hw`, against the same tree with
|
||||
carrying disabled): `entropy_src_main_sm.state_q` 47 = 47 warnings,
|
||||
`hmac_core.StIdle` 0 = 0, `prim_count.cnt_q` 13 = 13, timing unchanged (~2.9 s)
|
||||
— no noise added there.
|
||||
|
||||
## QA log
|
||||
|
||||
**2026-09-30 — QA agent (Opus).** Verdict: **PASS with findings** — no
|
||||
|
||||
Reference in new issue
Block a user