Menu

#977 preprocessor macros substitute text inside token

devel
closed-fixed
5
2015-05-17
2015-05-05
x127
No

when expanding preprocessor macros, iverilog 0.9.7 does a sed-style text substition of macro parameter names to values instead of replacing the tokens only.

This is a problem when other tokens contain macro parameter names. Consider
define DECLAREINT(name, i) integer name=iDECLAREINT(bar, 2);

When substituting tokens, (as verilator does) this expands to:
integer bar=2;

When substituting text, as iverilog does, it expands to:
2nteger bar= 2;
(Note that both compilers be called with -E to just precompile.)

Unsurprisingly, this does not compile.

While it is easy to work around this bug if one is aware of it (just rename i to something more unique), there may also exist cases where the bug will cause code to be compiled wrong, possibly leading to debugging headaches.

The bug also only seems to appear if the macro parameter is the prefix of another token, if the other token just contains the macro parameter (such as 'r' in 'integer', no substitution appears.)

Discussion

  • x127

    x127 - 2015-05-05

    Sorry, the bugtracker ate my backticks. Sample file attached.

     
  • Martin Whitaker

    Martin Whitaker - 2015-05-17
    • labels: preprocessor, tokenizer --> preprocessor, tokenizer
    • assigned_to: Martin Whitaker
    • Group: v0.9 --> devel
     
  • Martin Whitaker

    Martin Whitaker - 2015-05-17

    Actually no, the preprocessor does attempt to do token replacement (try replacing "i" with "n" in your example) - you've just found the corner case where it doesn't work!

    Also present in development, where it will be fixed first.

     
  • Martin Whitaker

    Martin Whitaker - 2015-05-17
    • status: open --> closed-fixed
     
  • Martin Whitaker

    Martin Whitaker - 2015-05-17

    I've pushed a fix for this to github in both the development and v0.9 branches.

    Thanks for reporting this.

     

Log in to post a comment.

MongoDB Logo MongoDB