Catching Extra Semicolons After UVM Macros with UVMLint
A solution‑first guide to preventing the classic dangling else
error caused by UVM reporting macros.
UVMLint Rule: No Trailing Semicolon After UVM Reporting Macros
Define a lint rule that flags any UVM reporting macro invocation immediately followed by a semicolon.
Implementation detail: This rule should be parser‑aware. Avoid pure regex; require an AST/token stream so comments, strings, and macro line breaks are handled correctly.
Why This Rule Exists
Consider a simple user code as below:
// Buggy code
if (debug_on)
`uvm_info("DEBUG", "state info", UVM_LOW); // semicolon here
else
$display("Doing something else");
The UVM BCL defines `uvm_info
as a complete begin...end
block. Adding a semicolon after the macro turns into a null statement that terminates the if
prematurely, leaving the else
orphaned.
// UVM BCL (abridged)
`define uvm_info(ID,MSG,VERBOSITY) \
begin \
if (uvm_report_enabled(VERBOSITY,UVM_INFO,ID)) \
uvm_report_info (ID, MSG, VERBOSITY, `uvm_file, `uvm_line); \
end
So what's wrong? Let's expand the macro in-place.
// What the compiler effectively sees
if (debug_on)
begin
if (uvm_report_enabled(UVM_LOW,UVM_INFO,"DEBUG"))
uvm_report_info("DEBUG","state info",UVM_LOW,`uvm_file,`uvm_line);
end; // null statement ends the 'if'
else
$display("Doing something else"); // ← now orphaned
Fix: drop the trailing semicolon after the macro invocation.
if (debug_on)
`uvm_info("DEBUG", "state info", UVM_LOW)
else
$display("Doing something else");
Why Grep Isn’t Enough
Text search won’t reliably detect this class of errors:
- Multiline calls: macros can be split across lines; naive patterns miss them.
- Comments & strings: false positives from commented code or string literals.
- Macro families: the issue applies to multiple report macros, aliasing, and wrappers.
- Language context: only a parser can tell if a semicolon actually terminates an
if
branch.
Use a syntax‑aware linter (UVMLint or similar) that understands SystemVerilog tokens, comments, and macro expansion boundaries.
Takeaways
- UVM reporting macros expand into self‑contained
begin...end
blocks. - Do not add a trailing semicolon when used under
if/else
. - Enforce with a UVMLint rule; avoid brittle grep‑based checks.
Comments
Post a Comment