在字符串中引用宏文字字符串参数

Quote macro literal string argument inside string

我有以下宏:

`define check(CONDITION) \
  begin \
    if (!(CONDITION)) \
      $display("'%s' failed.", `"CONDITION`"); \
  end

以及以下扩展:

module test;
  initial begin
    `check(0)
    `check(1 == 0)
  end
endmodule

他们打印以下内容:

'0' failed.
'1 == 0' failed.

但是,如果我对字符串有条件,那么宏展开将无法正常工作。具体来说,添加以下行会导致编译错误:

`check("foo" == "bar")

不过,我想要打印以下内容:

'"foo" == "bar"' failed.

有没有办法编写允许这样做的宏体?我想避免有两个宏的解决方案,一个是条件内不允许使用字符串,另一个是明确用于字符串。

您不能仅使用 SystemVerilog 中的一个宏来完成此操作。它需要像 PERL 中的 qq() 运算符这样的东西才能工作。