Skip to content

Verilog preprocessor: `include via macro expansion - #2330

Open
kroening wants to merge 1 commit into
mainfrom
kroening/include-via-define
Open

kroening wants to merge 1 commit into
mainfrom
kroening/include-via-define

Conversation

@kroening

@kroening kroening commented Nov 5, 2026 •

Copy link
Copy Markdown
Collaborator

A `define may expand to an `include directive whose file name is passed as a macro argument (1800-2017 section 22.4), e.g.

`define DO_INCLUDE(FN) `include FN
`DO_INCLUDE("file.svh")

Previously this failed: the file name token (FN) was read literally when scanning the `include argument, producing "expecting either " or < after `include". When the include did resolve, exiting it aborted with an invariant violation because the exit line directive assumed the enclosing context was a file rather than a macro expansion.

Changes:

  • Substitute macro arguments when reading the `include file name.
  • Make emit_line_directive refer to the nearest enclosing file context (new topmost_file_context() helper) so an include triggered from inside a macro expansion emits a correct line directive instead of tripping PRECONDITION(context().is_file()).

Adds a regression test covering multiple expansions on one line and an expansion suppressed inside a false `ifdef. This is the sv-tests 22.4--include_via_define case.

A `define may expand to an `include directive whose file name is
passed as a macro argument (1800-2017 22.4), e.g.

  `define DO_INCLUDE(FN) `include FN
  `DO_INCLUDE("file.svh")

Substitute macro arguments when reading the `include file name, and
make the enclosing line directive refer to the nearest enclosing file
rather than the macro-expansion context, via a new
topmost_file_context() helper.
@kroening
kroening force-pushed the kroening/include-via-define branch from a8e34bd to 5e87252 Compare November 5, 2026 00:09

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant