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]>
150 lines
5.8 KiB
Rust
150 lines
5.8 KiB
Rust
//! `explicit-function-lifetime` — say `automatic` or `static` outside a class.
|
|
|
|
use crate::context::LintContext;
|
|
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 ExplicitFunctionLifetime;
|
|
|
|
const NAME: &str = "explicit-function-lifetime";
|
|
|
|
/// The two words that state a lifetime, wherever a subroutine writes them.
|
|
pub(crate) const LIFETIMES: [&str; 2] = ["automatic", "static"];
|
|
|
|
impl Rule for ExplicitFunctionLifetime {
|
|
fn name(&self) -> &'static str {
|
|
NAME
|
|
}
|
|
|
|
fn summary(&self) -> &'static str {
|
|
"state `automatic` or `static` on a function declared outside a class"
|
|
}
|
|
|
|
fn severity(&self) -> Severity {
|
|
Severity::Style
|
|
}
|
|
|
|
fn doc(&self) -> &'static str {
|
|
"A function with no stated lifetime is `static` in a module and \
|
|
`automatic` in a class (IEEE 1800 §13.4.2), and a static function's \
|
|
locals are shared by every call — so one written without the keyword \
|
|
is a re-entrancy bug waiting for the second caller, and a reader \
|
|
cannot tell whether the author knew. Writing the word costs nine \
|
|
characters and removes the question. Verible's \
|
|
`explicit-function-lifetime` (\"Checks that every function declared \
|
|
outside of a class is declared with an explicit lifetime.\", enabled \
|
|
by default) is the same rule and the same default; a class method is \
|
|
exempt in both, because there the default is already the safe one.\n\n\
|
|
A body-less **prototype** — `extern function f;`, a `pure virtual`, a \
|
|
DPI import — is not judged: it declares no locals, and the definition \
|
|
that follows is where the lifetime belongs. Nor is an **out-of-body \
|
|
definition** (`function cls::f(...); … endfunction`), which is \
|
|
**wider than Verible**: lexically it sits outside the class, and \
|
|
Verible reports it, but it *is* a class method and its lifetime is \
|
|
already automatic — 948 of the 960 opentitan findings this rule and \
|
|
its task twin produced before the exemption were exactly that shape. \
|
|
No automatic fix: \
|
|
`automatic` is the answer a reader expects and `static` is what the \
|
|
language would have picked, so choosing between them changes what \
|
|
the code does."
|
|
}
|
|
|
|
fn check(&self, ctx: &LintContext) -> Vec<Violation> {
|
|
let mut out = Vec::new();
|
|
walk_items_scoped(ctx.file(), &mut |scope, item| {
|
|
let ItemKind::Function(decl) = &item.kind else { return };
|
|
// An out-of-body definition (`function cls::f(...)`) is a class
|
|
// method written outside the class body; `scope` is non-empty for
|
|
// exactly those.
|
|
if scope == ItemScope::Class
|
|
|| !decl.scope.is_empty()
|
|
|| decl.is_prototype
|
|
|| decl.dpi.is_some()
|
|
{
|
|
return;
|
|
}
|
|
if decl.qualifiers.iter().any(|q| LIFETIMES.contains(&q.text.as_str())) {
|
|
return;
|
|
}
|
|
out.push(Violation::new(
|
|
NAME,
|
|
format!(
|
|
"function `{}` has no explicit lifetime; write `automatic` or `static`",
|
|
decl.name
|
|
),
|
|
decl.name_span,
|
|
));
|
|
});
|
|
out
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
fn violations(src: &str) -> Vec<Violation> {
|
|
ExplicitFunctionLifetime.check(&LintContext::new(src, None))
|
|
}
|
|
|
|
#[test]
|
|
fn flags_a_module_function_and_points_at_its_name() {
|
|
let src = "module m;\n function int f(int x);\n return x;\n endfunction\nendmodule\n";
|
|
let found = violations(src);
|
|
assert_eq!(found.len(), 1, "{found:?}");
|
|
assert_eq!(&src[found[0].span.as_range()], "f");
|
|
assert!(found[0].fix.is_none());
|
|
}
|
|
|
|
#[test]
|
|
fn either_keyword_satisfies_the_rule() {
|
|
for kw in ["automatic", "static"] {
|
|
let src = format!(
|
|
"module m;\n function {kw} int f(int x);\n return x;\n endfunction\nendmodule\n"
|
|
);
|
|
assert!(violations(&src).is_empty(), "{kw}: {:?}", violations(&src));
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn a_class_method_is_exempt() {
|
|
let src = "class c;\n function int f(int x);\n return x;\n endfunction\nendclass\n";
|
|
assert!(violations(src).is_empty(), "{:?}", violations(src));
|
|
}
|
|
|
|
#[test]
|
|
fn a_package_or_file_scope_function_is_judged() {
|
|
let pkg = "package p;\n function int f();\n return 0;\n endfunction\nendpackage\n";
|
|
assert_eq!(violations(pkg).len(), 1);
|
|
let top = "function int f();\n return 0;\nendfunction\n";
|
|
assert_eq!(violations(top).len(), 1);
|
|
}
|
|
|
|
#[test]
|
|
fn a_prototype_declares_no_locals() {
|
|
let src = "class c;\n extern function int f();\nendclass\n\
|
|
import \"DPI-C\" function int c_f(input int x);\n";
|
|
assert!(violations(src).is_empty(), "{:?}", violations(src));
|
|
}
|
|
|
|
/// An `extern` method defined outside its class body is still a class
|
|
/// method; Verible reports it, lucisv does not (see the rule's doc).
|
|
#[test]
|
|
fn an_out_of_body_definition_is_a_class_method() {
|
|
let src = "class c;\n extern function int f();\nendclass\n\
|
|
function c::f();\n return 0;\nendfunction\n";
|
|
assert!(violations(src).is_empty(), "{:?}", violations(src));
|
|
}
|
|
|
|
#[test]
|
|
fn a_class_nested_in_a_package_is_still_a_class() {
|
|
let src = "package p;\n class c;\n function int f();\n return 0;\n \
|
|
endfunction\n endclass\nendpackage\n";
|
|
assert!(violations(src).is_empty(), "{:?}", violations(src));
|
|
}
|
|
}
|