//! `invalid-system-task-function` — system tasks nothing should still call. 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 InvalidSystemTaskFunction; const NAME: &str = "invalid-system-task-function"; /// The forbidden names and what to write instead — Verible's list /// (`invalid-system-task-function`), name for name. /// /// The `$dist_*` family and `$random` all draw from the *global* seed that /// `$random` shares, so one extra call anywhere in a testbench reshuffles /// every other stream; `$urandom` and `$urandom_range` are per-thread and /// reproducible. `$psprintf` is a vendor extension for the standard /// `$sformatf`, and `$srandom` seeds that same global generator rather than /// the calling process. const FORBIDDEN: &[(&str, &str)] = &[ ("$psprintf", "$sformatf"), ("$random", "$urandom"), ("$srandom", "process::self().srandom()"), ("$dist_chi_square", "$urandom"), ("$dist_erlang", "$urandom"), ("$dist_exponential", "$urandom"), ("$dist_normal", "$urandom"), ("$dist_poisson", "$urandom"), ("$dist_t", "$urandom"), ("$dist_uniform", "$urandom"), ]; impl Rule for InvalidSystemTaskFunction { fn name(&self) -> &'static str { NAME } fn summary(&self) -> &'static str { "do not call a deprecated or non-reproducible system task" } fn severity(&self) -> Severity { Severity::Warning } fn doc(&self) -> &'static str { "`$random`, `$srandom` and the `$dist_*` family all draw from one \ global generator, so adding a call anywhere in a testbench reshuffles \ every other random stream in it and a failing seed stops reproducing \ — the reason IEEE 1800 added the per-process `$urandom` / \ `$urandom_range` and `process::self().srandom()`. `$psprintf` is a \ vendor spelling of the standard `$sformatf` and is not portable. \ Verible's `invalid-system-task-function` (\"Checks that no forbidden \ system tasks or functions are used.\", enabled by default) forbids \ exactly this list, and lucisv names the replacement in the message. \ No automatic fix: `$random` and `$urandom` return different value \ streams, so swapping them changes what the test does." } fn check(&self, ctx: &LintContext) -> Vec { ctx.tokens() .iter() .filter(|token| token.kind == TokenKind::SystemIdent) .filter_map(|token| { let (name, instead) = FORBIDDEN.iter().find(|(name, _)| *name == token.text.as_str())?; Some(Violation::new( NAME, format!("`{name}` is forbidden; use `{instead}`"), token.span, )) }) .collect() } } #[cfg(test)] mod tests { use super::*; fn violations(src: &str) -> Vec { InvalidSystemTaskFunction.check(&LintContext::new(src, None)) } #[test] fn flags_every_forbidden_name_and_says_what_to_use() { for (name, instead) in FORBIDDEN { let src = format!("module m;\n initial x = {name}();\nendmodule\n"); let found = violations(&src); assert_eq!(found.len(), 1, "{name}: {found:?}"); assert!(found[0].message.contains(name), "{}", found[0].message); assert!(found[0].message.contains(instead), "{}", found[0].message); assert_eq!(&src[found[0].span.as_range()], *name, "the call, underlined"); } } #[test] fn the_replacements_and_their_neighbours_are_left_alone() { let src = "module m;\n\ initial begin\n\ a = $urandom();\n\ b = $urandom_range(0, 7);\n\ c = $sformatf(\"%0d\", a);\n\ $display(\"%0d\", b);\n\ end\n\ endmodule\n"; assert!(violations(src).is_empty(), "{:?}", violations(src)); } #[test] fn a_near_miss_name_is_not_a_hit() { // Matched whole, not by prefix: `$random_stable` and a signal called // `random` are neither of them `$random`. let src = "module m;\n\ logic random;\n\ initial begin\n\ a = $randomize_helper();\n\ b = random;\n\ end\n\ endmodule\n"; assert!(violations(src).is_empty(), "{:?}", violations(src)); } #[test] fn a_forbidden_name_inside_a_string_is_data() { let src = "module m;\n initial $display(\"call $random here\");\nendmodule\n"; assert!(violations(src).is_empty()); } }