Files
lucisv/crates/vlog_refactor/src/scope.rs
T
lucioandClaude Fable 5 40a7b478c5 Rename warns about macro arguments it did not read; Scope at the crate root; lucisv check --rules-doc
TODO section 29; closes docs/topo.md 2.9-18 (server half) and 2.9-5;
the second half of coverage_gaps M12.
FileFacts::macro_words - the identifier-shaped words seen inside macro
invocations, directive lines and attributes (words::harvest_raw, which
shares its raw-token match with harvest so the two cannot drift) -
tag MW, FACTS_FORMAT 16 -> 17 (+3.1 percent corpus / +1.7 percent
opentitan store, measured). WorkspaceIndex::files_naming_in_macros is
files_mentioning's twin, and lucisv_server::rename appends one warning
per such file that lies outside the set rename_sources built: no new
rung, nothing read, 58 files in 186 microseconds for clk on opentitan.
Pinned by a three-file case and its no-double-warning control, run by
QA against the real server; QA also found an organic instance in
opentitan (cipher_crypt inside a DV_CHECK_FATAL) that 0.33.0 never
mentioned. vlog_refactor's Scope moves to the crate root (pure move,
counts identical). lucisv check --rules-doc prints rules_markdown(),
takes no files, is lint-only; the example binary is gone.
3 226 tests / 48 binaries, corpus 814/820, opentitan check byte-
identical to 0.33.0.

docs/features/macro-word-census/README.md.

Co-Authored-By: Claude Fable 5 <[email protected]>
2026-09-03 02:41:15 -07:00

107 lines
4.0 KiB
Rust

//! The in-scope files, the index built from them, and the analyses computed
//! so far — what every refactoring in this crate reads the workspace through.
//! Nothing here is specific to a rename: `ports`, `references` and
//! `instantiate` read the same workspace, which is why it sits at the crate
//! root rather than under one of them.
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use vlog_parser::parse_source_tolerant;
use vlog_semantic::{
analyze, resolve_external, ExternalResolutions, FileId, Semantics, WorkspaceIndex,
};
/// One file's two analysis layers, as `textDocument/definition` uses them.
pub(crate) struct Analysis {
pub(crate) semantics: Semantics,
pub(crate) external: ExternalResolutions,
}
/// The in-scope files, the index built from them, and the analyses computed so
/// far. Analyses are built on demand: a rename usually needs a handful of the
/// workspace's files, and analyzing the rest would be pure cost.
pub(crate) struct Scope<'a> {
/// Path and text, sorted by path and deduplicated the way
/// `WorkspaceIndex::build` does it, so index `i` and `FileId(i)`'s texts
/// agree.
sources: Vec<(&'a Path, &'a str)>,
pub(crate) index: WorkspaceIndex,
/// Source index → the index's own file id.
file_ids: Vec<Option<FileId>>,
cache: HashMap<usize, Analysis>,
}
impl<'a> Scope<'a> {
pub(crate) fn build(files: &'a [(PathBuf, String)]) -> Scope<'a> {
let mut sources: Vec<(&Path, &str)> =
files.iter().map(|(p, t)| (p.as_path(), t.as_str())).collect();
// The same normalization `WorkspaceIndex::build` applies: a stable
// sort by path, then the first copy of a repeated path wins.
sources.sort_by(|a, b| a.0.cmp(b.0));
sources.dedup_by(|a, b| a.0 == b.0);
let index = WorkspaceIndex::build(sources.iter().map(|(p, t)| (p.to_path_buf(), *t)));
let ids: HashMap<&Path, FileId> = index
.files()
.iter()
.enumerate()
.map(|(i, path)| (path.as_path(), FileId(i as u32)))
.collect();
let file_ids = sources.iter().map(|(path, _)| ids.get(path).copied()).collect();
Scope { sources, index, file_ids, cache: HashMap::new() }
}
pub(crate) fn len(&self) -> usize {
self.sources.len()
}
pub(crate) fn path(&self, i: usize) -> Option<&'a Path> {
self.sources.get(i).map(|(path, _)| *path)
}
pub(crate) fn text(&self, i: usize) -> Option<&'a str> {
self.sources.get(i).map(|(_, text)| *text)
}
pub(crate) fn sources(&self) -> &[(&'a Path, &'a str)] {
&self.sources
}
pub(crate) fn file_id(&self, i: usize) -> Option<FileId> {
self.file_ids.get(i).copied().flatten()
}
pub(crate) fn index_of(&self, path: &Path) -> Option<usize> {
self.sources.iter().position(|(p, _)| *p == path)
}
/// The two analysis layers for one file, computed on first use.
pub(crate) fn analysis(&mut self, i: usize) -> Option<&Analysis> {
let Scope { sources, index, cache, .. } = self;
let text = sources.get(i).map(|(_, text)| *text)?;
Some(cache.entry(i).or_insert_with(|| {
let semantics = analyze(&parse_source_tolerant(text).0, text);
let external = resolve_external(&semantics, index);
Analysis { semantics, external }
}))
}
/// One file's analysis, but only if it has already been computed — what
/// the verification pass reads while it also holds the index.
pub(crate) fn cached(&self, i: usize) -> Option<&Analysis> {
self.cache.get(&i)
}
/// The workspace index the edits were computed against.
pub(crate) fn index(&self) -> &WorkspaceIndex {
&self.index
}
}
/// How a file is named in a message: its file name, because an absolute path
/// in a rename box is unreadable.
pub(crate) fn display(path: &Path) -> String {
path.file_name().and_then(|n| n.to_str()).unwrap_or("?").to_string()
}