//! `plusarg-assignment` — read a plusarg's value, do not test for its name. use crate::context::LintContext; use crate::registry::Rule; use crate::severity::Severity; use crate::violation::Violation; use vlog_parser::token::TokenKind; /// See [`Rule::doc`]. pub struct PlusargAssignment; const NAME: &str = "plusarg-assignment"; /// The system task this rule is about. const TEST_PLUSARGS: &str = "$test$plusargs"; impl Rule for PlusargAssignment { fn name(&self) -> &'static str { NAME } fn summary(&self) -> &'static str { "read a plusarg with `$value$plusargs`, not `$test$plusargs`" } fn severity(&self) -> Severity { Severity::Warning } fn doc(&self) -> &'static str { "`$test$plusargs(\"foo\")` answers only whether `+foo` was on the \ command line, and it matches by *prefix*: `+foobar` makes it true \ too, so two switches that share a prefix silently turn each other on. \ `$value$plusargs(\"foo=%d\", v)` matches the whole name, says whether \ it was there, and hands back the value — everything the other one \ does, without the trap, which is why a plusarg should always be \ given and read as a value. Verible's `plusarg-assignment` (\"Checks \ that plusargs are always assigned a value, by ensuring that plusargs \ are never accessed using the `$test$plusargs` system task.\", enabled \ by default) is the same rule and the same default. No automatic fix: \ the replacement needs a format string and a variable to read into, \ neither of which the call being replaced has." } fn check(&self, ctx: &LintContext) -> Vec { ctx.tokens() .iter() .filter(|token| token.kind == TokenKind::SystemIdent && token.text == TEST_PLUSARGS) .map(|token| { Violation::new( NAME, "`$test$plusargs` matches by prefix; use `$value$plusargs` and read the value", token.span, ) }) .collect() } } #[cfg(test)] mod tests { use super::*; fn violations(src: &str) -> Vec { PlusargAssignment.check(&LintContext::new(src, None)) } #[test] fn flags_a_test_plusargs_call() { let src = "module m;\n initial if ($test$plusargs(\"fast\")) go = 1;\nendmodule\n"; let found = violations(src); assert_eq!(found.len(), 1, "{found:?}"); assert_eq!(&src[found[0].span.as_range()], "$test$plusargs"); assert!(found[0].fix.is_none()); } #[test] fn the_value_form_is_what_the_rule_wants() { let src = "module m;\n\ initial if ($value$plusargs(\"speed=%0d\", speed)) go = 1;\n\ endmodule\n"; assert!(violations(src).is_empty()); // And a name that merely contains the words is not the call. assert!(violations("module m;\n logic test_plusargs;\nendmodule\n").is_empty()); assert!(violations("module m;\n initial $display(\"$test$plusargs\");\nendmodule\n") .is_empty()); } }