147 lines
5.3 KiB
Rust
147 lines
5.3 KiB
Rust
//! `unknown-parameter` — a `#(.NAME(…))` the module does not declare.
|
|
|
|
use crate::context::LintContext;
|
|
use crate::registry::Rule;
|
|
use crate::severity::Severity;
|
|
use crate::violation::Violation;
|
|
use crate::visit::walk_instantiations;
|
|
use vlog_parser::ast::ListElem;
|
|
|
|
/// See [`Rule::doc`].
|
|
pub struct UnknownParameter;
|
|
|
|
const NAME: &str = "unknown-parameter";
|
|
|
|
impl Rule for UnknownParameter {
|
|
fn name(&self) -> &'static str {
|
|
NAME
|
|
}
|
|
|
|
fn summary(&self) -> &'static str {
|
|
"override only parameters the instantiated module declares"
|
|
}
|
|
|
|
fn severity(&self) -> Severity {
|
|
Severity::Error
|
|
}
|
|
|
|
fn doc(&self) -> &'static str {
|
|
"`#(.Width(8))` on a module whose parameter is called `DataWidth` is \
|
|
an elaboration error, and — worse than the port version — the \
|
|
*silent* failure mode is real: a tool that accepts the override \
|
|
anyway elaborates the module at its declared default, so the design \
|
|
builds and is the wrong width. A `localparam` is not overridable at \
|
|
all (IEEE 1800 §6.20.4), so naming one here is the same error wearing \
|
|
a different hat, and lucisv reports it: the parameter list this rule \
|
|
checks against holds every `parameter` of the header and the body and \
|
|
no `localparam` of either. Verible has no equivalent; this is TODO \
|
|
§33.2's own rule.\n\n\
|
|
It keeps `unknown-port`'s silences exactly: no project, an unindexed \
|
|
module, one declared twice with different headers, one whose header \
|
|
carries a compiler directive, and a scope-qualified instantiation are \
|
|
all cases where the run cannot answer and the rule says nothing. A \
|
|
positional override names nothing to check — that is `module-\
|
|
parameter`'s subject. No automatic fix."
|
|
}
|
|
|
|
fn default_on(&self) -> bool {
|
|
// Off without a project: with no interface table to ask, every named
|
|
// override would be unanswerable and the rule would be a no-op that
|
|
// still cost a walk.
|
|
false
|
|
}
|
|
|
|
fn needs_workspace(&self) -> bool {
|
|
true
|
|
}
|
|
|
|
fn check(&self, ctx: &LintContext) -> Vec<Violation> {
|
|
let mut out = Vec::new();
|
|
walk_instantiations(ctx.file(), &mut |inst| {
|
|
if inst.module.contains(':') {
|
|
return;
|
|
}
|
|
let Some(params) = &inst.params else { return };
|
|
let Some(interface) = ctx.module_interface(&inst.module) else { return };
|
|
for param in params.iter().filter_map(ListElem::ok) {
|
|
if param.name == "*" || param.name_span.is_empty() {
|
|
continue;
|
|
}
|
|
if interface.has_parameter(¶m.name) {
|
|
continue;
|
|
}
|
|
out.push(Violation::new(
|
|
NAME,
|
|
format!("`{}` declares no overridable parameter `{}`", inst.module, param.name),
|
|
param.name_span,
|
|
));
|
|
}
|
|
});
|
|
out
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use crate::context::{MentionOracle, ModuleInterface};
|
|
|
|
/// A project holding one module with these overridable parameters.
|
|
struct Project(&'static str, &'static [&'static str]);
|
|
|
|
impl MentionOracle for Project {
|
|
fn mentioned_elsewhere(&self, _name: &str) -> bool {
|
|
false
|
|
}
|
|
|
|
fn module_interface(&self, name: &str) -> Option<ModuleInterface> {
|
|
(name == self.0).then(|| ModuleInterface {
|
|
ports: Vec::new(),
|
|
parameters: self.1.iter().map(|p| p.to_string()).collect(),
|
|
})
|
|
}
|
|
}
|
|
|
|
fn violations(src: &str, project: &'static Project) -> Vec<Violation> {
|
|
let ctx = LintContext::new(src, None);
|
|
UnknownParameter.check(&ctx.with_oracle(project))
|
|
}
|
|
|
|
static SUB: Project = Project("sub", &["Width", "Depth"]);
|
|
|
|
#[test]
|
|
fn flags_an_override_the_module_does_not_declare() {
|
|
let src = "module m;\n sub #(.Width(8), .Deth(4)) u ();\nendmodule\n";
|
|
let found = violations(src, &SUB);
|
|
assert_eq!(found.len(), 1, "{found:?}");
|
|
assert!(found[0].message.contains("`Deth`"), "{}", found[0].message);
|
|
assert_eq!(&src[found[0].span.as_range()], "Deth");
|
|
assert!(found[0].fix.is_none());
|
|
}
|
|
|
|
#[test]
|
|
fn a_declared_parameter_is_never_flagged() {
|
|
let src = "module m;\n sub #(.Width(8), .Depth(4)) u ();\nendmodule\n";
|
|
assert!(violations(src, &SUB).is_empty(), "{:?}", violations(src, &SUB));
|
|
}
|
|
|
|
#[test]
|
|
fn a_positional_override_names_nothing_to_check() {
|
|
assert!(violations("module m;\n sub #(8, 4) u ();\nendmodule\n", &SUB).is_empty());
|
|
assert!(violations("module m;\n sub u ();\nendmodule\n", &SUB).is_empty());
|
|
}
|
|
|
|
#[test]
|
|
fn an_unindexed_module_and_a_run_with_no_project_are_both_silent() {
|
|
assert!(violations("module m;\n other #(.Nope(1)) u ();\nendmodule\n", &SUB).is_empty());
|
|
let src = "module m;\n sub #(.Nope(1)) u ();\nendmodule\n";
|
|
assert!(UnknownParameter.check(&LintContext::new(src, None)).is_empty());
|
|
}
|
|
|
|
#[test]
|
|
fn a_scope_qualified_instantiation_is_left_to_the_workspace() {
|
|
let src = "module m;\n pkg::sub #(.Nope(1)) u ();\nendmodule\n";
|
|
assert!(violations(src, &SUB).is_empty());
|
|
}
|
|
}
|