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]>
163 lines
6.0 KiB
Rust
163 lines
6.0 KiB
Rust
//! `enum-name-style` — enum typedef names are `lower_snake_case` ending `_e`
|
|
//! or `_t`.
|
|
|
|
use crate::context::LintContext;
|
|
use crate::naming::{fits_pattern, is_escaped};
|
|
use crate::options::{OptionDefault, OptionKind, OptionSpec};
|
|
use crate::registry::Rule;
|
|
use crate::severity::Severity;
|
|
use crate::violation::Violation;
|
|
use crate::visit::walk_items;
|
|
use vlog_parser::ast::{ItemKind, TypedefKind};
|
|
|
|
/// See [`Rule::doc`].
|
|
pub struct EnumNameStyle;
|
|
|
|
const NAME: &str = "enum-name-style";
|
|
|
|
impl Rule for EnumNameStyle {
|
|
fn name(&self) -> &'static str {
|
|
NAME
|
|
}
|
|
|
|
fn summary(&self) -> &'static str {
|
|
"name enum typedefs in lower_snake_case ending in _e or _t"
|
|
}
|
|
|
|
fn severity(&self) -> Severity {
|
|
Severity::Style
|
|
}
|
|
|
|
fn doc(&self) -> &'static str {
|
|
"A type name that looks like a type name is what makes a declaration \
|
|
readable at a glance: `state_e cur_state` says which identifier is the \
|
|
type without knowing the codebase. Verible's `enum-name-style` \
|
|
(enabled by default) spells this as the pattern `[a-z_0-9]+(_t|_e)` — \
|
|
lower_snake_case with an `_e` or `_t` suffix — and lucisv checks the \
|
|
same shape. Only a `typedef enum` is judged: an anonymous enum has no \
|
|
name to check, and giving it one is Verible's separate \
|
|
`typedef-enums` idea. Escaped identifiers are exempt, and there is no \
|
|
fix: a type rename reaches every use.\n\n\
|
|
The shape is the `style_regex` option, and its default is \
|
|
`[a-z][a-z_0-9]*(_t|_e)` — Verible's pattern with the first character \
|
|
pinned to a letter, which is what lucisv's preset checked before the \
|
|
option existed and what keeps a leading `_` reported."
|
|
}
|
|
|
|
fn options(&self) -> &'static [OptionSpec] {
|
|
&[OptionSpec {
|
|
name: "style_regex",
|
|
kind: OptionKind::Regex,
|
|
default: OptionDefault::Str(DEFAULT_STYLE),
|
|
doc: "the pattern an enum typedef's whole name must match",
|
|
}]
|
|
}
|
|
|
|
fn check(&self, ctx: &LintContext) -> Vec<Violation> {
|
|
let mut out = Vec::new();
|
|
walk_items(ctx.file(), &mut |item| {
|
|
let ItemKind::Typedef(typedef) = &item.kind else { return };
|
|
if !matches!(typedef.kind, TypedefKind::Enum(_)) || is_escaped(&typedef.name) {
|
|
return;
|
|
}
|
|
let name = &typedef.name;
|
|
if fits_pattern(ctx, NAME, "style_regex", name) {
|
|
return;
|
|
}
|
|
out.push(Violation::new(
|
|
NAME,
|
|
crate::naming::style_message(
|
|
ctx,
|
|
NAME,
|
|
"style_regex",
|
|
&format!("enum `{name}`"),
|
|
"is not lower_snake_case ending in `_e` or `_t`",
|
|
),
|
|
typedef.name_span,
|
|
));
|
|
});
|
|
out
|
|
}
|
|
}
|
|
|
|
/// Today's preset, written as a pattern: `lower_snake_case` (a letter first)
|
|
/// ending in `_e` or `_t`.
|
|
const DEFAULT_STYLE: &str = "[a-z][a-z_0-9]*(_t|_e)";
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
fn violations(src: &str) -> Vec<Violation> {
|
|
EnumNameStyle.check(&LintContext::new(src, None))
|
|
}
|
|
|
|
fn typedef(name: &str) -> String {
|
|
format!("package p;\n typedef enum logic {{ A, B }} {name};\nendpackage\n")
|
|
}
|
|
|
|
#[test]
|
|
fn flags_a_missing_suffix_and_a_wrong_case() {
|
|
assert_eq!(violations(&typedef("state")).len(), 1);
|
|
assert_eq!(violations(&typedef("StateE")).len(), 1);
|
|
assert_eq!(violations(&typedef("STATE_E")).len(), 1);
|
|
let found = violations(&typedef("state"));
|
|
assert!(found[0].message.contains("state"), "{}", found[0].message);
|
|
assert!(found[0].fix.is_none(), "a type rename reaches every use");
|
|
}
|
|
|
|
#[test]
|
|
fn both_accepted_suffixes_are_clean() {
|
|
assert!(violations(&typedef("state_e")).is_empty());
|
|
assert!(violations(&typedef("state_t")).is_empty());
|
|
assert!(violations(&typedef("fifo2_state_e")).is_empty());
|
|
}
|
|
|
|
#[test]
|
|
fn a_non_enum_typedef_is_none_of_its_business() {
|
|
let src = "package p;\n typedef logic [7:0] byteish;\nendpackage\n";
|
|
assert!(violations(src).is_empty());
|
|
let src = "package p;\n typedef struct packed { logic a; } thing;\nendpackage\n";
|
|
assert!(violations(src).is_empty());
|
|
}
|
|
|
|
#[test]
|
|
fn an_enum_typedef_inside_a_class_body_is_covered() {
|
|
let src = "class c;\n typedef enum logic { A, B } state;\nendclass\n";
|
|
assert_eq!(violations(src).len(), 1, "{:?}", violations(src));
|
|
}
|
|
|
|
#[test]
|
|
fn it_points_at_the_name() {
|
|
let src = typedef("state");
|
|
assert_eq!(&src[violations(&src)[0].span.as_range()], "state");
|
|
}
|
|
|
|
/// The default pattern is today's preset, character for character: a
|
|
/// leading `_` or digit is still rejected, which Verible's own
|
|
/// `[a-z_0-9]+(_t|_e)` would accept.
|
|
#[test]
|
|
fn the_default_pattern_is_the_preset_it_replaced() {
|
|
for bad in ["_state_t", "State_t", "state"] {
|
|
assert_eq!(violations(&typedef(bad)).len(), 1, "{bad}");
|
|
}
|
|
for good in ["state_t", "state_e", "s_t", "fifo2_state_e"] {
|
|
assert!(violations(&typedef(good)).is_empty(), "{good}");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn a_configured_pattern_replaces_it_and_is_named_in_the_message() {
|
|
let mut options = crate::options::RuleOptions::new();
|
|
options.set_raw(NAME, "style_regex", "\"[a-z_0-9]+_e\"").expect("accepted");
|
|
let src = typedef("state_t");
|
|
let ctx = LintContext::new(&src, None).with_options(&options);
|
|
let found = EnumNameStyle.check(&ctx);
|
|
assert_eq!(found.len(), 1, "`_t` is no longer accepted");
|
|
assert!(found[0].message.contains("[a-z_0-9]+_e"), "{}", found[0].message);
|
|
let src = typedef("_state_e");
|
|
let ctx = LintContext::new(&src, None).with_options(&options);
|
|
assert!(EnumNameStyle.check(&ctx).is_empty(), "and a leading `_` now passes");
|
|
}
|
|
}
|