B-Lang-org/bsc

SV preprocessor mis-interprets directives at end of file

開放

#335 建立於 2021年3月16日

 (0 則留言) (0 個反應) (0 位負責人)Haskell (178 個分叉)batch import
buggood first issue

倉庫指標

星標
 (1,108 顆星)
PR 合併指標
 (平均合併 17天 1小時) (30 天內合併 18 個 PR)

描述

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).

貢獻者指南