SV preprocessor mis-interprets directives at end of file
#335 opened on Mar 16, 2021
Description
In testsuite/bsc.interra/preprocessorTestcases/undef/Undef_NoMacroName.bsv there is a undef preprocessor directive that appears at the end of the file and is missing its arguments. Prior to commit 26ffcb09, it was followed by a space and then EOF, with no newline at the end of the file. That commit removed the trailing space, which caused BSC to stop printing this message:
Error: "Undef_NoMacroName.bsv", line 16, column 1: (P0222)
missing macro identifier after ``undef'
and start printing this message:
Error: "Undef_NoMacroName.bsv", line 16, column 1: (P0145)
Preprocessor macro `undef' is not defined
BSC is treating it as an ordinary macro rather than a directive, because there is no whitespace following it. This is because of how the preprocessor is written. Many directives are identified with a pattern like this (in SystemVerilogPreprocess.lhs):
> prescanMain state@(PreState (outp, pos, ('`':'i':'f':'d':'e':'f':c:restOfInput),
> env, errh, flgs))
> | isWhitespace c = prescanIfDefDirective True pos (eatChars 7 state)
This pattern will only match if there is a whitespace character following the directive. When the directive is at the end of the file, the pattern is not matched, and the function falls through to a final clause that treats it as an ordinary macro.
Instead, the patterns should probably just match restOfInput (no c) and the guard should include a check for EOF. And testcases need to be added.
For Undef_NoMacroName.bsv, I will add a newline, so that the test passes again. But it also means that we should add tests for directives followed by a space and then EOF (and followed by a space and then newline). The CI will complain about these files having trailing whitespace -- so we may want to disable the CI's whitespace checking on the testsuite (or parts of the testsuite).