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]>
195 lines
7.2 KiB
Rust
195 lines
7.2 KiB
Rust
//! `endif-comment` — say which `` `ifdef `` an `` `endif `` closes.
|
|
|
|
use crate::context::LintContext;
|
|
use crate::preproc::{directive_operand, directive_word};
|
|
use crate::registry::Rule;
|
|
use crate::severity::Severity;
|
|
use crate::violation::{Applicability, Fix, Violation};
|
|
use vlog_parser::span::TextRange;
|
|
use vlog_parser::token::Token;
|
|
|
|
/// See [`Rule::doc`].
|
|
pub struct EndifComment;
|
|
|
|
const NAME: &str = "endif-comment";
|
|
|
|
impl Rule for EndifComment {
|
|
fn name(&self) -> &'static str {
|
|
NAME
|
|
}
|
|
|
|
fn summary(&self) -> &'static str {
|
|
"name the condition an `` `endif `` closes in a comment beside it"
|
|
}
|
|
|
|
fn severity(&self) -> Severity {
|
|
Severity::Style
|
|
}
|
|
|
|
fn default_on(&self) -> bool {
|
|
false
|
|
}
|
|
|
|
fn fix(&self) -> Option<Applicability> {
|
|
Some(Applicability::Safe)
|
|
}
|
|
|
|
fn doc(&self) -> &'static str {
|
|
"A bare `` `endif `` two hundred lines below its `` `ifdef `` tells \
|
|
the reader nothing: which condition just ended, and whether the \
|
|
nesting is what they think it is, both take a scroll to answer. \
|
|
`` `endif // SYNTHESIS `` answers them in place, which is the same \
|
|
argument `mismatched-labels` makes for `end : name`. Verible's \
|
|
`endif-comment` (\"Checks that a Verilog `` `endif `` directive is \
|
|
followed by a matching comment.\", **disabled** by default) is the \
|
|
same rule and the same default.\n\n\
|
|
The comment must **name the condition** — any comment on the \
|
|
`` `endif ``'s own line that writes the macro name as a word will \
|
|
do, so `// SYNTHESIS`, `` // `ifdef SYNTHESIS `` and `/* SYNTHESIS \
|
|
*/` all pass. An `` `endif `` whose `` `ifdef `` lucisv cannot see \
|
|
(an unbalanced file, a condition opened in an included header) is \
|
|
left alone: there is no name to ask for.\n\n\
|
|
The fix is **safe** and only offered where there is no comment at \
|
|
all: it appends `` // <name> `` after the directive, adding text and \
|
|
removing none. A comment that is there but names something else is \
|
|
reported without a fix — rewriting somebody's words is not a repair."
|
|
}
|
|
|
|
fn check(&self, ctx: &LintContext) -> Vec<Violation> {
|
|
let mut open: Vec<Option<String>> = Vec::new();
|
|
let mut out = Vec::new();
|
|
for token in ctx.tokens() {
|
|
match directive_word(token) {
|
|
Some("ifdef") | Some("ifndef") => {
|
|
open.push(directive_operand(token).map(str::to_string))
|
|
}
|
|
Some("endif") => {
|
|
// An `endif` with nothing open is an unbalanced file, or a
|
|
// condition opened in an included header: no name to ask
|
|
// for, so nothing to say.
|
|
let Some(Some(name)) = open.pop() else { continue };
|
|
if let Some(violation) = judge(ctx, token, &name) {
|
|
out.push(violation);
|
|
}
|
|
}
|
|
_ => {}
|
|
}
|
|
}
|
|
out
|
|
}
|
|
}
|
|
|
|
/// The finding this `` `endif `` earns, or `None` when its comment names the
|
|
/// condition.
|
|
fn judge(ctx: &LintContext, token: &Token, name: &str) -> Option<Violation> {
|
|
let beside: Vec<&str> = ctx
|
|
.comments()
|
|
.into_iter()
|
|
.filter(|comment| comment.line == token.line && comment.span.start >= token.span.end)
|
|
.map(|comment| comment.text.as_str())
|
|
.collect();
|
|
if beside.iter().any(|text| names(text, name)) {
|
|
return None;
|
|
}
|
|
let violation = Violation::new(
|
|
NAME,
|
|
format!("`endif` does not say what it closes; write `// {name}`"),
|
|
token.span,
|
|
);
|
|
match beside.is_empty() {
|
|
true => Some(violation.with_fix(Fix::safe(vec![(
|
|
TextRange::new(token.span.end, token.span.end),
|
|
format!(" // {name}"),
|
|
)]))),
|
|
// There is a comment; it just names something else. Replacing it would
|
|
// throw away what somebody wrote.
|
|
false => Some(violation),
|
|
}
|
|
}
|
|
|
|
/// True when `text` writes `name` as a whole word.
|
|
fn names(text: &str, name: &str) -> bool {
|
|
let is_part = |c: char| c == '_' || c == '$' || c.is_ascii_alphanumeric();
|
|
let mut rest = text;
|
|
while let Some(at) = rest.find(name) {
|
|
let before = rest[..at].chars().next_back();
|
|
let after = rest[at + name.len()..].chars().next();
|
|
if !before.is_some_and(is_part) && !after.is_some_and(is_part) {
|
|
return true;
|
|
}
|
|
rest = &rest[at + name.len()..];
|
|
}
|
|
false
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use crate::violation::apply_fixes;
|
|
|
|
fn violations(src: &str) -> Vec<Violation> {
|
|
EndifComment.check(&LintContext::new(src, None))
|
|
}
|
|
|
|
#[test]
|
|
fn flags_a_bare_endif_and_offers_the_comment() {
|
|
let src = "`ifdef SYNTHESIS\nmodule m;\nendmodule\n`endif\n";
|
|
let found = violations(src);
|
|
assert_eq!(found.len(), 1, "{found:?}");
|
|
assert_eq!(&src[found[0].span.as_range()], "`endif");
|
|
let fixed = apply_fixes(src, &found, false);
|
|
assert_eq!(fixed.output, "`ifdef SYNTHESIS\nmodule m;\nendmodule\n`endif // SYNTHESIS\n");
|
|
assert!(violations(&fixed.output).is_empty(), "the fix settles");
|
|
}
|
|
|
|
#[test]
|
|
fn a_naming_comment_of_any_shape_passes() {
|
|
for comment in ["// SYNTHESIS", "// `ifdef SYNTHESIS", "/* SYNTHESIS */", "//SYNTHESIS"] {
|
|
let src = format!("`ifdef SYNTHESIS\nmodule m;\nendmodule\n`endif {comment}\n");
|
|
assert!(violations(&src).is_empty(), "{comment}: {:?}", violations(&src));
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn a_comment_naming_something_else_is_reported_without_a_fix() {
|
|
let src = "`ifdef SYNTHESIS\nmodule m;\nendmodule\n`endif // done\n";
|
|
let found = violations(src);
|
|
assert_eq!(found.len(), 1, "{found:?}");
|
|
assert!(found[0].fix.is_none(), "somebody's comment is not ours to rewrite");
|
|
}
|
|
|
|
#[test]
|
|
fn a_name_that_only_contains_the_condition_is_not_a_match() {
|
|
let src = "`ifdef SYN\nmodule m;\nendmodule\n`endif // SYNTHESIS\n";
|
|
assert_eq!(violations(src).len(), 1, "{:?}", violations(src));
|
|
}
|
|
|
|
#[test]
|
|
fn nesting_pairs_each_endif_with_its_own_condition() {
|
|
let src = "`ifdef A\n`ifdef B\nmodule m;\nendmodule\n`endif\n`endif\n";
|
|
let found = violations(src);
|
|
assert_eq!(found.len(), 2, "{found:?}");
|
|
assert!(found[0].message.contains("// B"), "{}", found[0].message);
|
|
assert!(found[1].message.contains("// A"), "{}", found[1].message);
|
|
}
|
|
|
|
#[test]
|
|
fn an_ifndef_and_an_else_are_handled_like_the_ifdef() {
|
|
let src = "`ifndef A\nmodule m;\nendmodule\n`else\nmodule n;\nendmodule\n`endif\n";
|
|
let found = violations(src);
|
|
assert_eq!(found.len(), 1, "{found:?}");
|
|
assert!(found[0].message.contains("// A"), "{}", found[0].message);
|
|
}
|
|
|
|
#[test]
|
|
fn an_unbalanced_endif_has_no_name_to_ask_for() {
|
|
assert!(violations("`endif\nmodule m;\nendmodule\n").is_empty());
|
|
}
|
|
|
|
#[test]
|
|
fn a_comment_on_the_next_line_is_not_beside_the_endif() {
|
|
let src = "`ifdef A\nmodule m;\nendmodule\n`endif\n// A\n";
|
|
assert_eq!(violations(src).len(), 1);
|
|
}
|
|
}
|