Skip to content
systemcoding.dev
Hardware

Lint Verilog With verilator --lint-only and Keep the Codebase Warning-Clean

Run verilator --lint-only -Wall on every Verilog file, fix the common warnings, waive the rest in a .vlt file with a reason and make lint a Makefile target.

7 min read
Summarize in AI
Flat illustration of logic gates wired into a small circuit, with the Haskell, KiCad and SparkFun logos.

Most hardware bugs a simulator finds, a linter finds first, without a testbench and in a fraction of a second. A truncated assignment, a case that forgets a value, a register nothing drives, a latch you did not mean to write: each of these is visible in the source, and each costs hours when it surfaces as a wrong waveform. Verilator's --lint-only mode parses and elaborates your design, runs its checks and stops, with no C++ output and no compile. This post uses Verilator 5.032 to lint a small module, explains the warnings you will meet most, shows how to waive a warning correctly, and turns lint into a build target that keeps the codebase at zero warnings. It assumes you can already write synthesizable Verilog and build a Verilator model.

The basic command

Verilator's checks come in two groups. The default set covers code that is wrong or non-portable and always runs. -Wall adds the style and quality checks: unused signals, missing declarations, incomplete cases and width mismatches. Lint with -Wall, because the extra checks are where the bugs are.

Shell
verilator --lint-only -Wall rtl/lint_demo.v

If the design has several files, list them all, and name the top with --top-module when there is more than one candidate. --lint-only does not need a testbench; it needs only the files that make up the design.

A module with four problems

rtl/lint_demo.vverilog
module lint_demo (
    input  wire       clk,
    input  wire [7:0] a,
    input  wire [3:0] sel,
    output reg  [3:0] y
);
    reg [7:0] spare;

    always @(posedge clk) begin
        case (sel)
            4'd0: y <= a;
            4'd1: y <= a[3:0];
        endcase
    end
endmodule

Running the command above prints:

Text
%Warning-UNUSEDSIGNAL: rtl/lint_demo.v:7:15: Signal is not used: 'spare'
                                           : ... note: In instance 'lint_demo'
                                           : ... For warning description see https://verilator.org/warn/UNUSEDSIGNAL?v=5.032
                                           : ... Use "/* verilator lint_off UNUSEDSIGNAL */" and lint_on around source to disable this message.
%Warning-WIDTHTRUNC: rtl/lint_demo.v:12:23: Operator ASSIGNDLY expects 4 bits on the Assign RHS, but Assign RHS's VARREF 'a' generates 8 bits.
                                           : ... note: In instance 'lint_demo'
%Warning-CASEINCOMPLETE: rtl/lint_demo.v:11:9: Case values incompletely covered (example pattern 0x2)
%Warning-UNUSEDSIGNAL: rtl/lint_demo.v:2:23: Bits of signal are not used: 'a'[7:4]
%Error: Exiting due to 4 warning(s)

The exact wording, columns and the order of messages differ between releases, so read this as the shape of the output. Each warning has a name in capitals, and that name is what you use to waive it or look it up on the Verilator warnings page.

What each warning means

WIDTHTRUNC says the right-hand side is wider than the target and bits will be cut. On line 12, y <= a moves eight bits into four. In hardware the upper bits simply vanish, which is almost never what the author wanted. The fix is explicit: y <= a[3:0], which states the intent and silences the warning for the right reason. Its sibling WIDTHEXPAND fires when the right side is narrower and will be zero-extended, which matters when the value is signed.

CASEINCOMPLETE says some values of sel match no branch. For a clocked process that is not dangerous, because y just holds its value, but for a combinational block the same omission infers a latch, which is a different problem and a common one. Verilator reports it either way. Add a default branch that states what should happen.

UNUSEDSIGNAL names a signal, or a range of bits of one, that nothing reads. spare is dead code. The second occurrence is more instructive: bits 7 to 4 of the input a are never used because every branch truncates. That can be correct for a port you are reserving, or it can mean the logic ignores data it should handle. Treat each as a question for the author.

UNDRIVEN and MULTIDRIVEN are the dangerous pair. A signal read but never assigned is a constant x or z in hardware, and a signal assigned from two processes is a driver fight. Verilator catches both at lint time, long before a simulation shows an x on a waveform.

Others you will see often: LATCH (a combinational block that does not assign on every path), BLKSEQ (a blocking assignment inside a clocked block, which risks races), PINMISSING (an instance port left unconnected), DECLFILENAME (a module whose name does not match its file, which breaks tools that find modules by file name) and SYNCASYNCNET (a signal used as both a synchronous and an asynchronous reset).

The fixed module

rtl/lint_demo.vverilog
module lint_demo (
    input  wire       clk,
    input  wire [3:0] a,
    input  wire [3:0] sel,
    output reg  [3:0] y
);
    always @(posedge clk) begin
        case (sel)
            4'd0:    y <= a;
            4'd1:    y <= ~a;
            default: y <= 4'd0;
        endcase
    end
endmodule
Shell
verilator --lint-only -Wall rtl/lint_demo.v
Text
(no output, exit status 0)

The silence is the point. Verilator prints nothing and exits with status 0 when a design is clean. A clean lint run is a statement you can put in CI, which a simulation pass is not: a testbench passes on the cases it tried, and lint passes on the structure.

The latch warning in a combinational block

LATCH deserves a worked example, because it is the warning that most often points at a real bug in code that simulates fine. A combinational always block must assign its outputs on every path. Miss one path and the hardware has to remember the old value, which means a latch.

rtl/pick.vverilog
module pick (
    input  wire       en,
    input  wire [3:0] a,
    output reg  [3:0] y
);
    always @* begin
        if (en)
            y = a;
    end
endmodule
Text
%Warning-LATCH: rtl/pick.v:6:5: Latch inferred for signal 'pick.y' (not all control paths of combinational always assign a value)
                              : ... Suggest use of always_latch for intentional latches

The message names the signal and the cause. Add an else y = 4'd0; or a default assignment at the top of the block, and the warning goes away. If the latch is intentional, which is rare on an FPGA, write always_latch in SystemVerilog so the intent is in the source and the checker agrees.

Warning flags you will actually use

Individual warnings can be switched without touching the source. -Wno-UNUSEDSIGNAL disables one by name for the whole run, and -Wwarn-UNUSEDSIGNAL turns it back into a non-fatal warning. -Wno-lint disables all lint-class checks and leaves the style checks, which is a reasonable setting for a third-party directory you pass in a separate invocation. A quick way to track progress on a dirty codebase is to count the warnings:

Shell
verilator --lint-only -Wall -Wno-fatal rtl/*.v 2>&1 | grep -c '^%Warning'
Text
37

That number is an example. Record it, fix a module, run it again and watch it fall. Here -Wno-fatal is correct because you are measuring, not building.

Waive with a reason, in one place

Sometimes a warning is right and the code is also right. A bus has 32 data bits and a peripheral uses only the low 8; a vendor-generated file has hundreds of style hits you will never edit. There are two ways to silence a warning, and they have different costs.

An inline comment silences it at the spot:

verilog
/* verilator lint_off UNUSEDSIGNAL */
wire [31:0] bus_wdata_unused_high = bus_wdata;
/* verilator lint_on  UNUSEDSIGNAL */

The comment is visible next to the code, but it spreads across the codebase, and nobody reviews all of them together. A central waiver file keeps every exception in one reviewable list. Verilator reads a file with the .vlt extension when it appears on the command line:

lint.vltText
`verilator_config

lint_off -rule UNUSEDSIGNAL -file "rtl/vendor/*" 
lint_off -rule DECLFILENAME -file "rtl/vendor/*"
lint_off -rule UNUSEDSIGNAL -file "rtl/uart_regs.v" -match "*bus_wdata*"

Name it before the sources:

Shell
verilator --lint-only -Wall lint.vlt rtl/lint_demo.v rtl/uart_regs.v

The -file pattern limits a waiver to the files that need it, and -match narrows it to messages containing a string. Add a comment above each line saying why. A waiver without a reason is a bug someone has hidden.

Make lint a build target

Lint that runs only when someone remembers will rot. Put it in the Makefile and make the test target depend on it, so a warning blocks the simulation that would have wasted your time:

Makefilemakefile
RTL = rtl/lint_demo.v rtl/uart_regs.v

lint:
	verilator --lint-only -Wall lint.vlt --top-module lint_demo $(RTL)

test: lint
	$(MAKE) -C sim run

Verilator's warnings are fatal by default when they are lint-class, so the command already returns a non-zero exit code. Do not add -Wno-fatal to make a red build green; that is the quickest way to start a codebase on the road to a hundred warnings nobody reads. If you adopt lint on an existing codebase that has hundreds, record the current set as a baseline, fix by module and tighten as you go. The rule that matters is that new code does not add warnings.

What lint cannot do

Lint reads structure. It cannot tell you that the counter wraps at 15 when the specification says 9, or that a state machine skips a state. It does not replace the testbench. It does replace the first hour of every debugging session, which is spent discovering something a one-second command already knew.

Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.