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]>
106 lines
3.8 KiB
Rust
106 lines
3.8 KiB
Rust
//! What a preprocessor token says: the name a macro invocation calls, and the
|
|
//! word and operand a compiler directive carries.
|
|
//!
|
|
//! The lexer keeps both shapes as single tokens — a macro invocation
|
|
//! (`` `uvm_info("A", "B", UVM_LOW) ``) is one `Ident` whose text starts with
|
|
//! a backtick and swallows its balanced arguments, and a directive
|
|
//! (`` `ifdef FOO ``) is one `Directive` running to the end of its (possibly
|
|
//! continued) line. Four rules read those texts; reading them in one place is
|
|
//! what stops the four from disagreeing about where a name starts and ends.
|
|
|
|
use vlog_parser::token::{Token, TokenKind};
|
|
|
|
/// The macro this token invokes, without its backtick or arguments, or `None`
|
|
/// for a token that is not a macro invocation.
|
|
pub(crate) fn macro_name(token: &Token) -> Option<&str> {
|
|
if token.kind != TokenKind::Ident {
|
|
return None;
|
|
}
|
|
let rest = token.text.strip_prefix('`')?;
|
|
let end = rest.find(|c: char| !is_name_char(c)).unwrap_or(rest.len());
|
|
match end {
|
|
0 => None,
|
|
_ => Some(&rest[..end]),
|
|
}
|
|
}
|
|
|
|
/// The keyword of a compiler directive (`ifdef`, `endif`, `define`), or `None`
|
|
/// for a token that is not one.
|
|
pub(crate) fn directive_word(token: &Token) -> Option<&str> {
|
|
if token.kind != TokenKind::Directive {
|
|
return None;
|
|
}
|
|
let rest = token.text.strip_prefix('`')?;
|
|
let end = rest.find(|c: char| !is_name_char(c)).unwrap_or(rest.len());
|
|
match end {
|
|
0 => None,
|
|
_ => Some(&rest[..end]),
|
|
}
|
|
}
|
|
|
|
/// The name a directive names — the `FOO` of `` `ifdef FOO `` — or `None` when
|
|
/// it names nothing.
|
|
pub(crate) fn directive_operand(token: &Token) -> Option<&str> {
|
|
let word = directive_word(token)?;
|
|
let rest = token.text.get(1 + word.len()..)?.trim_start();
|
|
let end = rest.find(|c: char| !is_name_char(c)).unwrap_or(rest.len());
|
|
match end {
|
|
0 => None,
|
|
_ => Some(&rest[..end]),
|
|
}
|
|
}
|
|
|
|
/// The characters an identifier is made of, matching the lexer's own rule.
|
|
fn is_name_char(c: char) -> bool {
|
|
c == '_' || c == '$' || c.is_ascii_alphanumeric()
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use vlog_parser::tokenize;
|
|
|
|
fn tokens(src: &str) -> Vec<Token> {
|
|
tokenize(src).expect("lexes")
|
|
}
|
|
|
|
#[test]
|
|
fn a_macro_invocation_gives_its_name() {
|
|
let toks = tokens("module m;\n initial `uvm_info(\"A\", \"B\", UVM_LOW);\nendmodule\n");
|
|
let names: Vec<&str> = toks.iter().filter_map(macro_name).collect();
|
|
assert_eq!(names, vec!["uvm_info"]);
|
|
}
|
|
|
|
#[test]
|
|
fn a_macro_without_arguments_gives_its_name_too() {
|
|
let toks = tokens("module m;\n `ASSERT_INIT\nendmodule\n");
|
|
assert_eq!(toks.iter().filter_map(macro_name).collect::<Vec<_>>(), vec!["ASSERT_INIT"]);
|
|
}
|
|
|
|
#[test]
|
|
fn an_ordinary_identifier_is_not_a_macro() {
|
|
let toks = tokens("module m;\n wire uvm_info;\nendmodule\n");
|
|
assert!(toks.iter().filter_map(macro_name).next().is_none());
|
|
}
|
|
|
|
#[test]
|
|
fn a_directive_gives_its_word_and_operand() {
|
|
let toks = tokens("`ifdef FOO\n`endif\n`define BAR 1\nmodule m;\nendmodule\n");
|
|
let words: Vec<&str> = toks.iter().filter_map(directive_word).collect();
|
|
assert_eq!(words, vec!["ifdef", "endif", "define"]);
|
|
let operands: Vec<Option<&str>> =
|
|
toks.iter().filter(|t| t.kind == TokenKind::Directive).map(directive_operand).collect();
|
|
assert_eq!(operands, vec![Some("FOO"), None, Some("BAR")]);
|
|
}
|
|
|
|
#[test]
|
|
fn a_directive_is_not_a_macro_invocation_and_the_other_way_round() {
|
|
let toks = tokens("`ifdef FOO\nmodule m;\n `ASSERT\nendmodule\n`endif\n");
|
|
assert_eq!(toks.iter().filter_map(macro_name).collect::<Vec<_>>(), vec!["ASSERT"]);
|
|
assert!(toks
|
|
.iter()
|
|
.filter(|t| t.kind == TokenKind::Ident)
|
|
.all(|t| directive_word(t).is_none()));
|
|
}
|
|
}
|