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]>
140 lines
5.2 KiB
Rust
140 lines
5.2 KiB
Rust
//! `banned-declared-name-patterns` — names a project has forbidden.
|
|
|
|
use crate::context::LintContext;
|
|
use crate::options::{OptionDefault, OptionKind, OptionSpec};
|
|
use crate::regex::Regex;
|
|
use crate::registry::Rule;
|
|
use crate::severity::Severity;
|
|
use crate::violation::Violation;
|
|
|
|
/// See [`Rule::doc`].
|
|
pub struct BannedDeclaredNamePatterns;
|
|
|
|
const NAME: &str = "banned-declared-name-patterns";
|
|
|
|
impl Rule for BannedDeclaredNamePatterns {
|
|
fn name(&self) -> &'static str {
|
|
NAME
|
|
}
|
|
|
|
fn summary(&self) -> &'static str {
|
|
"do not declare a name the project has banned"
|
|
}
|
|
|
|
fn severity(&self) -> Severity {
|
|
Severity::Style
|
|
}
|
|
|
|
fn doc(&self) -> &'static str {
|
|
"Every codebase has words it has decided against — a deprecated \
|
|
prefix, a vendor's reserved namespace, the `tmp`/`foo` a review keeps \
|
|
catching — and none of them are anybody else's business. Verible's \
|
|
`banned-declared-name-patterns` is the rule a project points at its \
|
|
own list, and it ships **off** with an empty list, which is exactly \
|
|
what lucisv does: with no `patterns` set the rule reports nothing and \
|
|
costs nothing.\n\n\
|
|
It judges **declared** names, not uses: every name the file's symbol \
|
|
table holds — modules, packages, types, parameters, signals, ports, \
|
|
instances, block labels, class members. A pattern must match the whole \
|
|
name, so `tmp.*` bans `tmp_addr` and `.*_tmp` bans `addr_tmp`. There \
|
|
is no fix: only the author knows what the name should have been."
|
|
}
|
|
|
|
fn default_on(&self) -> bool {
|
|
false
|
|
}
|
|
|
|
fn options(&self) -> &'static [OptionSpec] {
|
|
&[OptionSpec {
|
|
name: "patterns",
|
|
kind: OptionKind::RegexList,
|
|
default: OptionDefault::List(&[]),
|
|
doc: "the patterns a declared name may not match",
|
|
}]
|
|
}
|
|
|
|
fn check(&self, ctx: &LintContext) -> Vec<Violation> {
|
|
let patterns: Vec<(String, Regex)> = ctx
|
|
.option_list(NAME, "patterns")
|
|
.into_iter()
|
|
// A pattern that does not compile cannot be configured — the
|
|
// option is validated when it is read — so filtering here only
|
|
// ever drops what could not have arrived.
|
|
.filter_map(|text| Regex::compile(&text).ok().map(|regex| (text, regex)))
|
|
.collect();
|
|
if patterns.is_empty() {
|
|
return Vec::new();
|
|
}
|
|
let mut out = Vec::new();
|
|
for symbol in ctx.semantics().symbols() {
|
|
let Some((pattern, _)) =
|
|
patterns.iter().find(|(_, regex)| regex.is_match(&symbol.name))
|
|
else {
|
|
continue;
|
|
};
|
|
out.push(Violation::new(
|
|
NAME,
|
|
format!("`{}` matches the banned pattern `{pattern}`", symbol.name),
|
|
symbol.name_span,
|
|
));
|
|
}
|
|
out.sort_by_key(|violation| (violation.span.start, violation.span.end));
|
|
out
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use crate::options::RuleOptions;
|
|
|
|
fn banning(patterns: &str, src: &str) -> Vec<Violation> {
|
|
let mut options = RuleOptions::new();
|
|
options.set_raw(NAME, "patterns", patterns).expect("accepted");
|
|
BannedDeclaredNamePatterns.check(&LintContext::new(src, None).with_options(&options))
|
|
}
|
|
|
|
#[test]
|
|
fn nothing_is_banned_by_default() {
|
|
let src = "module m;\n logic tmp_addr;\nendmodule\n";
|
|
assert!(BannedDeclaredNamePatterns.check(&LintContext::new(src, None)).is_empty());
|
|
assert!(!BannedDeclaredNamePatterns.default_on());
|
|
}
|
|
|
|
#[test]
|
|
fn a_banned_name_is_reported_where_it_is_declared() {
|
|
let src = "module m;\n logic tmp_addr;\n logic keep_me;\nendmodule\n";
|
|
let found = banning("[\"tmp.*\"]", src);
|
|
assert_eq!(found.len(), 1, "{found:?}");
|
|
assert!(found[0].message.contains("banned pattern `tmp.*`"), "{}", found[0].message);
|
|
assert_eq!(&src[found[0].span.as_range()], "tmp_addr");
|
|
assert!(found[0].fix.is_none());
|
|
}
|
|
|
|
#[test]
|
|
fn the_match_is_the_whole_name() {
|
|
let src = "module m;\n logic addr_tmp;\nendmodule\n";
|
|
assert!(banning("[\"tmp\"]", src).is_empty(), "a substring is not a match");
|
|
assert_eq!(banning("[\".*_tmp\"]", src).len(), 1);
|
|
}
|
|
|
|
#[test]
|
|
fn every_kind_of_declared_name_is_covered() {
|
|
let src = "package foo_pkg;\n typedef logic foo_t;\nendpackage\n\
|
|
module foo_mod (input logic foo_port);\n logic foo_sig;\nendmodule\n";
|
|
let found = banning("[\"foo.*\"]", src);
|
|
let names: Vec<&str> = found.iter().map(|v| &src[v.span.as_range()]).collect();
|
|
for wanted in ["foo_pkg", "foo_t", "foo_mod", "foo_port", "foo_sig"] {
|
|
assert!(names.contains(&wanted), "{wanted} missing from {names:?}");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn several_patterns_are_all_applied_and_findings_come_back_in_order() {
|
|
let src = "module m;\n logic tmp_a;\n logic bad_b;\nendmodule\n";
|
|
let found = banning("[\"tmp.*\", \"bad.*\"]", src);
|
|
assert_eq!(found.len(), 2);
|
|
assert!(found[0].span.start < found[1].span.start);
|
|
}
|
|
}
|