Files
lucisv/crates/vlog_linter/src/rules/proper_parameter_declaration.rs
T
lucioandClaude Fable 5 8adb6c8ac1 Linter expansion, wave 3: the remaining nineteen Verible rules, forbidden-macro, void-cast's forbidden calls, and the e2e options case
TODO section 35 (54 -> 73 rules: every rule in Verible's published
list but forbid-implicit-declarations).
module-begin-block, legacy-generate-region, legacy-genvar-declaration,
v2001-generate-begin, explicit-function-lifetime,
explicit-task-lifetime (both exempt out-of-body definitions after a
census showed 948 of 960 findings were exactly that shape),
positive-meaning-parameter-name, suggest-parentheses (safe fix,
truth-tabled), unpacked-dimensions-range-ordering, uvm-macro-semicolon,
create-object-name-match, numeric-format-string-style,
macro-string-concatenation, disable-statement, forbidden-macro,
endif-comment (safe fix), forbid-line-continuations, dff-name-style,
proper-parameter-declaration. Verible's forbidden-symbol is the
source-file name of invalid-system-task-function (read in its
rule .cc), so it is that rule's symbols option, nothing fires twice;
void-cast gains forbidden_calls. Shared readers: walk_items_scoped,
exprs.rs, preproc.rs. A checked-in e2e case drives lucisv.lint.options
over the wire. Two rules fire on an error leaf because the parser
refuses a module-scope begin/end and a bare generate begin - filed as
docs/topo.md 2.9-25.
3 695 tests / 48 binaries; corpus 814/820 byte-identical to 0.38.0;
opentitan default-set census 1 053 -> 1 169, the arithmetic balancing
to the six default-on additions; timing unchanged.

docs/features/linter-expansion-3/README.md.

Co-Authored-By: Claude Fable 5 <[email protected]>
2026-09-04 04:55:10 -05:00

197 lines
7.4 KiB
Rust

//! `proper-parameter-declaration` — `parameter` in a header, `localparam` in a body.
use crate::context::LintContext;
use crate::options::{OptionDefault, OptionKind, OptionSpec};
use crate::registry::Rule;
use crate::severity::Severity;
use crate::violation::Violation;
use crate::visit::{walk_items_scoped, ItemScope};
use vlog_parser::ast::ItemKind;
/// See [`Rule::doc`].
pub struct ProperParameterDeclaration;
const NAME: &str = "proper-parameter-declaration";
/// Verible's two options, spelled and defaulted as Verible spells them.
const OPTIONS: &[OptionSpec] = &[
OptionSpec {
name: "package_allow_parameter",
kind: OptionKind::Bool,
default: OptionDefault::Bool(false),
doc: "accept `parameter` in a package body",
},
OptionSpec {
name: "package_allow_localparam",
kind: OptionKind::Bool,
default: OptionDefault::Bool(true),
doc: "accept `localparam` in a package body",
},
];
impl Rule for ProperParameterDeclaration {
fn name(&self) -> &'static str {
NAME
}
fn summary(&self) -> &'static str {
"declare `parameter` only in a formal parameter list and `localparam` only in a body"
}
fn severity(&self) -> Severity {
Severity::Style
}
fn default_on(&self) -> bool {
false
}
fn options(&self) -> &'static [OptionSpec] {
OPTIONS
}
fn doc(&self) -> &'static str {
"A `parameter` is what an instantiation may override, so it belongs \
in the `#( … )` a caller can see. Written in a module *body* it is \
still overridable — by `defparam`, or by a hierarchical override — \
which means a value the reader believes is fixed can change from \
outside with nothing at the declaration to say so. `localparam` is \
the spelling for a constant that is nobody's business but the \
module's. Verible's `proper-parameter-declaration` (\"Checks that \
`parameter` declarations are in formal parameter lists and \
`localparam` in modules/classes/packages.\", **disabled** by \
default) is the same rule and the same default, with the same two \
options: `package_allow_parameter` (false) and \
`package_allow_localparam` (true).\n\n\
A **formal parameter list is never judged** — `#(parameter int W = \
8)` and `#(localparam int X = 2*W)` are both fine there. What is \
judged is a body declaration: `parameter` anywhere is wrong (in a \
package, unless `package_allow_parameter`), and `localparam` is \
right in a module, interface, program or class body, wrong at file \
scope, and in a package answers to `package_allow_localparam`. Only \
item-scope declarations are read; a `parameter` written among a \
subroutine's statements is left to the language. No automatic fix: \
`parameter` → `localparam` removes an override an instantiation may \
be relying on."
}
fn check(&self, ctx: &LintContext) -> Vec<Violation> {
let allow_parameter = ctx.option_bool(NAME, "package_allow_parameter");
let allow_localparam = ctx.option_bool(NAME, "package_allow_localparam");
let mut out = Vec::new();
walk_items_scoped(ctx.file(), &mut |scope, item| {
let ItemKind::ParamDecl(decl) = &item.kind else { return };
let Some(first) = decl.declarators.first() else { return };
let Some(message) = complaint(&decl.kw, scope, allow_parameter, allow_localparam)
else {
return;
};
out.push(Violation::new(NAME, message, first.name_span));
});
out
}
}
/// What is wrong with this keyword in this scope, or `None` when nothing is.
fn complaint(
kw: &str,
scope: ItemScope,
allow_parameter: bool,
allow_localparam: bool,
) -> Option<String> {
match (kw, scope) {
("parameter", ItemScope::Package) if allow_parameter => None,
("parameter", _) => Some(
"`parameter` belongs in a formal parameter list; write `localparam` for a \
body constant"
.to_string(),
),
("localparam", ItemScope::Package) if allow_localparam => None,
("localparam", ItemScope::Package) => Some(
"`localparam` is not accepted in a package body by this project's settings".to_string(),
),
("localparam", ItemScope::File) => {
Some("`localparam` belongs in a module, interface, program or class body".to_string())
}
_ => None,
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::options::RuleOptions;
fn violations(src: &str) -> Vec<Violation> {
ProperParameterDeclaration.check(&LintContext::new(src, None))
}
fn with(src: &str, key: &str, raw: &str) -> Vec<Violation> {
let mut options = RuleOptions::new();
options.set_raw(NAME, key, raw).expect("accepted");
ProperParameterDeclaration.check(&LintContext::new(src, None).with_options(&options))
}
#[test]
fn flags_a_parameter_in_a_module_body_and_points_at_its_name() {
let src = "module m;\n parameter int Bar = 1;\nendmodule\n";
let found = violations(src);
assert_eq!(found.len(), 1, "{found:?}");
assert_eq!(&src[found[0].span.as_range()], "Bar");
assert!(found[0].fix.is_none());
}
/// Verible's own accepted list, snippet for snippet.
#[test]
fn the_shapes_verible_accepts_are_accepted() {
for src in [
"module foo; localparam int Bar = 1; endmodule\n",
"class foo; localparam int Bar = 1; endclass\n",
"package foo; localparam int Bar = 1; endpackage\n",
"module foo #(localparam int Bar = 1); endmodule\n",
"module foo #(parameter int Bar = 1); endmodule\n",
] {
assert!(violations(src).is_empty(), "{src}: {:?}", violations(src));
}
}
/// Verible's own rejected list, snippet for snippet.
#[test]
fn the_shapes_verible_rejects_are_reported() {
for src in [
"parameter int Foo = 1;\n",
"package foo; parameter int Bar = 1; endpackage\n",
"module foo; parameter int Bar = 1; endmodule\n",
"class foo; parameter int Bar = 1; endclass\n",
"package foo; class bar; parameter int HelloWorld = 1; endclass endpackage\n",
] {
assert_eq!(violations(src).len(), 1, "{src}: {:?}", violations(src));
}
}
#[test]
fn package_allow_parameter_moves_the_line() {
let src = "package foo; parameter int Bar = 1; endpackage\n";
assert_eq!(violations(src).len(), 1);
assert!(with(src, "package_allow_parameter", "true").is_empty());
}
#[test]
fn package_allow_localparam_moves_it_the_other_way() {
let src = "package foo; localparam int Bar = 1; endpackage\n";
assert!(violations(src).is_empty());
assert_eq!(with(src, "package_allow_localparam", "false").len(), 1);
}
#[test]
fn a_localparam_at_file_scope_has_no_body_to_belong_to() {
assert_eq!(violations("localparam int Foo = 1;\n").len(), 1);
}
#[test]
fn a_declaration_inside_a_generate_block_keeps_its_modules_scope() {
let src = "module m;\n if (A) begin : g\n localparam int X = 1;\n end\nendmodule\n";
assert!(violations(src).is_empty(), "{:?}", violations(src));
}
}