TODO section 34 (36 -> 54 rules). Options: OptionSpec/OptionKind/OptionValue/RuleOptions in vlog_linter with one validator behind both front ends - [lint.<rule>] tables in lucisv.toml (accepted now; stored raw by lucisv_core::config, resolved per file by the CLI with <config>:<line> on the first bad entry) and the editor's lucisv.lint.options object (bad entries logged and dropped) - precedence editor > toml > default key by key, --rules-doc listing every option. A small anchored regex engine of our own: a Thompson-NFA simulation, linear in pattern x name by construction (QA's first cut found the backtracking matcher exponential on (a*)*b), unknown escapes refused by name through both doors, a 71-row table verified against CPython. The four naming rules take Verible's option spellings with defaults that keep today's verdicts byte-identical. Eighteen Style rules, one file each with true positives and near- misses: line-length (the formatter's width), posix-eof (safe fix), forbid-consecutive-null-statements (safe fix), generate-label and generate-label-prefix, package-filename, one-module-per-file (off), signal/interface/macro/constraint/parameter-type-name-style, port-name-suffix (off), banned-declared-name-patterns (off), typedef-enums, typedef-structs-unions (allow_anonymous_nested defaults true here - opentitan's 3 159 findings were all generated register packages, and its own config disables the rule), always-comb (unsafe fix), explicit-begin (off, Verible's eleven enables). Four rules ask parsed_cleanly() first after a census showed error-recovered UVM files rebound as generate blocks. package.json regenerated (drift test). 3 549 tests / 48 binaries; corpus 814/820 byte-identical; naming-rule finding lists identical to 0.36.0; opentitan check timing unchanged. docs/features/linter-expansion-2/README.md. Co-Authored-By: Claude Fable 5 <[email protected]>
331 lines
12 KiB
Rust
331 lines
12 KiB
Rust
//! `explicit-begin` — a body is a `begin ... end` block, always.
|
|
|
|
use crate::context::LintContext;
|
|
use crate::options::{OptionDefault, OptionKind, OptionSpec};
|
|
use crate::registry::Rule;
|
|
use crate::severity::Severity;
|
|
use crate::violation::{Applicability, Fix, Violation};
|
|
use crate::visit::{walk_all_stmts, walk_items};
|
|
use vlog_parser::ast::{ItemKind, Stmt, StmtKind};
|
|
use vlog_parser::span::TextRange;
|
|
|
|
/// See [`Rule::doc`].
|
|
pub struct ExplicitBegin;
|
|
|
|
const NAME: &str = "explicit-begin";
|
|
|
|
/// The eleven constructs Verible lets a project switch on one at a time, each
|
|
/// with the keyword a diagnostic names it by.
|
|
const ENABLES: [(&str, &str); 11] = [
|
|
("always_comb_enable", "always_comb"),
|
|
("always_enable", "always"),
|
|
("always_ff_enable", "always_ff"),
|
|
("always_latch_enable", "always_latch"),
|
|
("else_enable", "else"),
|
|
("final_enable", "final"),
|
|
("for_enable", "for"),
|
|
("forever_enable", "forever"),
|
|
("if_enable", "if"),
|
|
("initial_enable", "initial"),
|
|
("while_enable", "while"),
|
|
];
|
|
|
|
impl Rule for ExplicitBegin {
|
|
fn name(&self) -> &'static str {
|
|
NAME
|
|
}
|
|
|
|
fn summary(&self) -> &'static str {
|
|
"wrap every body in `begin` and `end`"
|
|
}
|
|
|
|
fn severity(&self) -> Severity {
|
|
Severity::Style
|
|
}
|
|
|
|
fn doc(&self) -> &'static str {
|
|
"The classic C bug transplanted: `if (a) x = 1;` grows a second \
|
|
statement in a later edit, the author indents it, and the compiler \
|
|
reads it as unconditional. Writing `begin ... end` from the start \
|
|
makes that edit impossible to get wrong, which is why several house \
|
|
styles require it. Verible's `explicit-begin` ships **off** — the \
|
|
style is far from universal, and a codebase that does not follow it \
|
|
would light up on the first run — and lucisv keeps it off for the same \
|
|
reason.\n\n\
|
|
Eleven `*_enable` options, exactly Verible's, switch the constructs on \
|
|
and off one at a time; all are on when the rule is. An `else if` is \
|
|
never reported: the `else`'s body is another `if`, which is the \
|
|
chained form every style guide writes. A null body (`if (a) ;`) is \
|
|
left to `suspicious-semicolon`, which has more to say about it. The \
|
|
fix wraps the body in `begin`/`end` and is **safe** — a block around \
|
|
one statement changes nothing — and running the formatter afterwards \
|
|
lays it out."
|
|
}
|
|
|
|
fn default_on(&self) -> bool {
|
|
false
|
|
}
|
|
|
|
fn fix(&self) -> Option<Applicability> {
|
|
Some(Applicability::Safe)
|
|
}
|
|
|
|
fn options(&self) -> &'static [OptionSpec] {
|
|
// Written out rather than generated, because a spec table is a
|
|
// `static` and every entry needs its own `&'static str` doc.
|
|
&[
|
|
OptionSpec {
|
|
name: "always_comb_enable",
|
|
kind: OptionKind::Bool,
|
|
default: OptionDefault::Bool(true),
|
|
doc: "check the body of an `always_comb`",
|
|
},
|
|
OptionSpec {
|
|
name: "always_enable",
|
|
kind: OptionKind::Bool,
|
|
default: OptionDefault::Bool(true),
|
|
doc: "check the body of an `always`",
|
|
},
|
|
OptionSpec {
|
|
name: "always_ff_enable",
|
|
kind: OptionKind::Bool,
|
|
default: OptionDefault::Bool(true),
|
|
doc: "check the body of an `always_ff`",
|
|
},
|
|
OptionSpec {
|
|
name: "always_latch_enable",
|
|
kind: OptionKind::Bool,
|
|
default: OptionDefault::Bool(true),
|
|
doc: "check the body of an `always_latch`",
|
|
},
|
|
OptionSpec {
|
|
name: "else_enable",
|
|
kind: OptionKind::Bool,
|
|
default: OptionDefault::Bool(true),
|
|
doc: "check the body of an `else`",
|
|
},
|
|
OptionSpec {
|
|
name: "final_enable",
|
|
kind: OptionKind::Bool,
|
|
default: OptionDefault::Bool(true),
|
|
doc: "check the body of a `final`",
|
|
},
|
|
OptionSpec {
|
|
name: "for_enable",
|
|
kind: OptionKind::Bool,
|
|
default: OptionDefault::Bool(true),
|
|
doc: "check the body of a `for` loop",
|
|
},
|
|
OptionSpec {
|
|
name: "forever_enable",
|
|
kind: OptionKind::Bool,
|
|
default: OptionDefault::Bool(true),
|
|
doc: "check the body of a `forever` loop",
|
|
},
|
|
OptionSpec {
|
|
name: "if_enable",
|
|
kind: OptionKind::Bool,
|
|
default: OptionDefault::Bool(true),
|
|
doc: "check the body of an `if`",
|
|
},
|
|
OptionSpec {
|
|
name: "initial_enable",
|
|
kind: OptionKind::Bool,
|
|
default: OptionDefault::Bool(true),
|
|
doc: "check the body of an `initial`",
|
|
},
|
|
OptionSpec {
|
|
name: "while_enable",
|
|
kind: OptionKind::Bool,
|
|
default: OptionDefault::Bool(true),
|
|
doc: "check the body of a `while` loop",
|
|
},
|
|
]
|
|
}
|
|
|
|
fn check(&self, ctx: &LintContext) -> Vec<Violation> {
|
|
let mut out = Vec::new();
|
|
let mut report = |keyword: &'static str, body: &Stmt| {
|
|
if !enabled(ctx, keyword) || is_block(body) || matches!(body.kind, StmtKind::Null) {
|
|
return;
|
|
}
|
|
out.push(
|
|
Violation::new(
|
|
NAME,
|
|
format!("`{keyword}` body is not a `begin ... end` block"),
|
|
body.span,
|
|
)
|
|
.with_fix(Fix::safe(vec![
|
|
(TextRange::new(body.span.start, body.span.start), "begin ".to_string()),
|
|
(TextRange::new(body.span.end, body.span.end), " end".to_string()),
|
|
])),
|
|
);
|
|
};
|
|
walk_items(ctx.file(), &mut |item| match &item.kind {
|
|
ItemKind::Always { kw, stmt } => {
|
|
let keyword = ENABLES
|
|
.iter()
|
|
.find_map(|(_, word)| (*word == kw).then_some(*word))
|
|
.unwrap_or("always");
|
|
if let Some(body) = procedural_body(stmt) {
|
|
report(keyword, body);
|
|
}
|
|
}
|
|
ItemKind::Initial(stmt) => {
|
|
if let Some(body) = procedural_body(stmt) {
|
|
report("initial", body);
|
|
}
|
|
}
|
|
ItemKind::Final(stmt) => {
|
|
if let Some(body) = procedural_body(stmt) {
|
|
report("final", body);
|
|
}
|
|
}
|
|
_ => {}
|
|
});
|
|
walk_all_stmts(ctx.file(), &mut |stmt| match &stmt.kind {
|
|
StmtKind::If { then, els, .. } => {
|
|
report("if", then);
|
|
// `else if` is the chained form, not a missing block.
|
|
if let Some(els) = els {
|
|
if !matches!(els.kind, StmtKind::If { .. }) {
|
|
report("else", els);
|
|
}
|
|
}
|
|
}
|
|
StmtKind::For { body, .. } => report("for", body),
|
|
StmtKind::While { body, .. } => report("while", body),
|
|
StmtKind::Forever { body } => report("forever", body),
|
|
_ => {}
|
|
});
|
|
out
|
|
}
|
|
}
|
|
|
|
/// Whether the run wants this construct checked.
|
|
fn enabled(ctx: &LintContext, keyword: &str) -> bool {
|
|
ENABLES
|
|
.iter()
|
|
.find(|(_, word)| *word == keyword)
|
|
.is_none_or(|(option, _)| ctx.option_bool(NAME, option))
|
|
}
|
|
|
|
/// The statement a procedural block really runs: the one under its timing
|
|
/// control, when it has one (`always_ff @(posedge clk) q <= d;`).
|
|
///
|
|
/// `None` for a timing control with no body at all (`always @(posedge clk);`),
|
|
/// which has nothing to wrap.
|
|
fn procedural_body(stmt: &Stmt) -> Option<&Stmt> {
|
|
match &stmt.kind {
|
|
StmtKind::Timing { stmt, .. } => stmt.as_deref(),
|
|
_ => Some(stmt),
|
|
}
|
|
}
|
|
|
|
/// Whether this statement is already a block.
|
|
fn is_block(stmt: &Stmt) -> bool {
|
|
matches!(stmt.kind, StmtKind::Block { .. } | StmtKind::ForkJoin { .. })
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use crate::options::RuleOptions;
|
|
use crate::violation::apply_fixes;
|
|
|
|
fn violations(src: &str) -> Vec<Violation> {
|
|
ExplicitBegin.check(&LintContext::new(src, None))
|
|
}
|
|
|
|
#[test]
|
|
fn it_is_off_by_default_and_takes_veribles_eleven_options() {
|
|
assert!(!ExplicitBegin.default_on());
|
|
let names: Vec<&str> = ExplicitBegin.options().iter().map(|spec| spec.name).collect();
|
|
assert_eq!(names.len(), 11);
|
|
assert!(names.contains(&"if_enable") && names.contains(&"always_ff_enable"));
|
|
}
|
|
|
|
#[test]
|
|
fn flags_an_unblocked_if_body_and_wraps_it() {
|
|
let src = "module m;\n initial begin\n if (a) x = 1;\n end\nendmodule\n";
|
|
let found = violations(src);
|
|
assert_eq!(found.len(), 1, "{found:?}");
|
|
assert_eq!(&src[found[0].span.as_range()], "x = 1;");
|
|
let fixed = apply_fixes(src, &found, false);
|
|
assert_eq!(
|
|
fixed.output,
|
|
"module m;\n initial begin\n if (a) begin x = 1; end\n end\nendmodule\n"
|
|
);
|
|
assert!(violations(&fixed.output).is_empty(), "the fix satisfies the rule");
|
|
}
|
|
|
|
#[test]
|
|
fn a_blocked_body_is_clean() {
|
|
let src = "module m;\n initial begin\n if (a) begin\n x = 1;\n end\n end\n\
|
|
endmodule\n";
|
|
assert!(violations(src).is_empty(), "{:?}", violations(src));
|
|
}
|
|
|
|
#[test]
|
|
fn every_construct_is_covered() {
|
|
let src = "module m;\n\
|
|
\x20 always_comb x = 1;\n\
|
|
\x20 always_ff @(posedge clk) q <= d;\n\
|
|
\x20 always @(a) y = 2;\n\
|
|
\x20 always_latch if (en) z = 3;\n\
|
|
\x20 initial x = 0;\n\
|
|
\x20 final $display(\"bye\");\n\
|
|
endmodule\n";
|
|
let found = violations(src);
|
|
// Six bodies, plus the `if` inside the always_latch.
|
|
assert_eq!(found.len(), 7, "{:#?}", found.iter().map(|v| &v.message).collect::<Vec<_>>());
|
|
let src = "module m;\n initial begin\n\
|
|
\x20 for (int i = 0; i < 2; i++) x = i;\n\
|
|
\x20 while (a) y = 1;\n\
|
|
\x20 forever z = 2;\n\
|
|
\x20 end\nendmodule\n";
|
|
assert_eq!(violations(src).len(), 3, "{:?}", violations(src));
|
|
}
|
|
|
|
#[test]
|
|
fn an_else_if_chain_is_not_a_missing_block() {
|
|
let src = "module m;\n initial begin\n if (a) begin\n x = 1;\n end\n\
|
|
\x20 else if (b) begin\n x = 2;\n end\n end\nendmodule\n";
|
|
assert!(violations(src).is_empty(), "{:?}", violations(src));
|
|
}
|
|
|
|
#[test]
|
|
fn a_bare_else_body_is_reported() {
|
|
let src =
|
|
"module m;\n initial begin\n if (a) begin\n x = 1;\n end else x = 2;\n\
|
|
\x20 end\nendmodule\n";
|
|
let found = violations(src);
|
|
assert_eq!(found.len(), 1, "{found:?}");
|
|
assert!(found[0].message.contains("`else`"), "{}", found[0].message);
|
|
}
|
|
|
|
#[test]
|
|
fn a_null_body_is_left_to_suspicious_semicolon() {
|
|
let src = "module m;\n initial begin\n if (a) ;\n end\nendmodule\n";
|
|
assert!(violations(src).is_empty());
|
|
}
|
|
|
|
#[test]
|
|
fn each_option_switches_its_own_construct_off() {
|
|
let src = "module m;\n initial begin\n if (a) x = 1;\n while (b) y = 2;\n end\n\
|
|
endmodule\n";
|
|
assert_eq!(violations(src).len(), 2);
|
|
let mut options = RuleOptions::new();
|
|
options.set_raw(NAME, "if_enable", "false").expect("accepted");
|
|
let found = ExplicitBegin.check(&LintContext::new(src, None).with_options(&options));
|
|
assert_eq!(found.len(), 1, "{found:?}");
|
|
assert!(found[0].message.contains("`while`"), "{}", found[0].message);
|
|
}
|
|
|
|
#[test]
|
|
fn a_fork_join_body_is_already_a_block() {
|
|
let src = "module m;\n initial fork\n x = 1;\n join\nendmodule\n";
|
|
assert!(violations(src).is_empty(), "{:?}", violations(src));
|
|
}
|
|
}
|