105 lines
3.7 KiB
Rust
105 lines
3.7 KiB
Rust
//! `struct-union-name-style` — struct/union typedef names are
|
|
//! `lower_snake_case` ending `_t`.
|
|
|
|
use crate::context::LintContext;
|
|
use crate::naming::{is_escaped, is_lower_snake_case};
|
|
use crate::registry::Rule;
|
|
use crate::violation::Violation;
|
|
use crate::visit::walk_items;
|
|
use vlog_parser::ast::{ItemKind, TypedefKind};
|
|
|
|
/// See [`Rule::doc`].
|
|
pub struct StructUnionNameStyle;
|
|
|
|
const NAME: &str = "struct-union-name-style";
|
|
|
|
impl Rule for StructUnionNameStyle {
|
|
fn name(&self) -> &'static str {
|
|
NAME
|
|
}
|
|
|
|
fn summary(&self) -> &'static str {
|
|
"name struct and union typedefs in lower_snake_case ending in _t"
|
|
}
|
|
|
|
fn doc(&self) -> &'static str {
|
|
"The `_t` suffix is the one piece of Verilog naming convention that is \
|
|
nearly universal: it marks an identifier as a type, which the language \
|
|
itself gives no syntax for at a use site. Verible's \
|
|
`struct-union-name-style` (enabled by default) requires \
|
|
lower_snake_case ending in `_t`, and lucisv checks the same shape. Only \
|
|
a `typedef struct` / `typedef union` is judged — an anonymous struct \
|
|
has no name, and requiring it to have one is Verible's separate \
|
|
`typedef-structs-unions` idea. Escaped identifiers are exempt, and \
|
|
there is no fix: a type rename reaches every use."
|
|
}
|
|
|
|
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 };
|
|
let TypedefKind::Struct(structure) = &typedef.kind else { return };
|
|
let name = &typedef.name;
|
|
if is_escaped(name) || (is_lower_snake_case(name) && name.ends_with("_t")) {
|
|
return;
|
|
}
|
|
out.push(Violation::new(
|
|
NAME,
|
|
format!("{} `{name}` is not lower_snake_case ending in `_t`", structure.kw),
|
|
typedef.name_span,
|
|
));
|
|
});
|
|
out
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
fn violations(src: &str) -> Vec<Violation> {
|
|
StructUnionNameStyle.check(&LintContext::new(src, None))
|
|
}
|
|
|
|
fn typedef(kw: &str, name: &str) -> String {
|
|
format!("package p;\n typedef {kw} packed {{ logic a; }} {name};\nendpackage\n")
|
|
}
|
|
|
|
#[test]
|
|
fn flags_a_missing_suffix_and_a_wrong_case() {
|
|
let found = violations(&typedef("struct", "addr"));
|
|
assert_eq!(found.len(), 1);
|
|
assert!(found[0].message.contains("struct"), "{}", found[0].message);
|
|
assert!(found[0].fix.is_none(), "a type rename reaches every use");
|
|
assert_eq!(violations(&typedef("struct", "AddrT")).len(), 1);
|
|
assert_eq!(violations(&typedef("struct", "addr_e")).len(), 1, "_e is for enums");
|
|
}
|
|
|
|
#[test]
|
|
fn a_union_is_named_in_the_message() {
|
|
let found = violations(&typedef("union", "addr"));
|
|
assert_eq!(found.len(), 1);
|
|
assert!(found[0].message.contains("union"), "{}", found[0].message);
|
|
}
|
|
|
|
#[test]
|
|
fn a_conventional_name_is_clean() {
|
|
assert!(violations(&typedef("struct", "addr_t")).is_empty());
|
|
assert!(violations(&typedef("union", "word2_t")).is_empty());
|
|
}
|
|
|
|
#[test]
|
|
fn a_non_struct_typedef_is_none_of_its_business() {
|
|
let src = "package p;\n typedef enum logic { A } state;\nendpackage\n";
|
|
assert!(violations(src).is_empty());
|
|
let src = "package p;\n typedef logic [7:0] byteish;\nendpackage\n";
|
|
assert!(violations(src).is_empty());
|
|
}
|
|
|
|
#[test]
|
|
fn a_struct_typedef_inside_a_class_body_is_covered() {
|
|
let src = "class c;\n typedef struct packed { logic a; } addr;\nendclass\n";
|
|
assert_eq!(violations(src).len(), 1, "{:?}", violations(src));
|
|
}
|
|
}
|