简体   繁体   English

类似函数的宏扩展为空

[英]Function-like macro expands to nothing

This is a very strange problem.这是一个很奇怪的问题。 It's a very simple looking function-like macro, and I have seen its examples many times.这是一个非常简单的类似函数的宏,我已经多次看到它的例子。 I have even used it before!我什至以前用过它! However, I just couldn't get this working after 6 hours.但是,我在 6 小时后无法正常工作。 I got exhausted, and decided to ask here.我累了,决定在这里问。 Maybe somebody spots the problem.也许有人发现了问题。 The minimal reproducible case is:最小的可重现情况是:

enum e {
    x, y, z,

#define Func(X, Y, Z) \
  my_##X = Y

Func(x, y, z),
};

It's used in clang source code in a similar way.它以类似的方式用于 clang源代码。 I, too, include a file that's containing both the definition and use of the macro.我也包含一个包含宏的定义和使用的文件。

EDIT:编辑:

Thanks a lot for answers and comments.非常感谢您的回答和评论。 It's true that I have used Godbolt, but after my build failed.我确实使用过 Godbolt,但是在我的构建失败之后。 It turned out that macro has never failed during my build.事实证明,宏在我的构建过程中从未失败过。 That's because I haven't used the macro!那是因为我没用过宏! It's not used, so it never generated an output.它没有被使用,所以它从来没有产生过输出。 That's quite shameful.真是太丢人了。 I have to accept my mistake, and move on.我必须接受我的错误,然后继续前进。 I must have checked it by preprocessing locally.我必须通过本地预处理来检查它。 Getting empty string in Godbolt made me believe that I am doing something wrong.在 Godbolt 中得到空字符串让我相信我做错了什么。

Apparently (as noted by @Shawn in comments) this is because the compiler output in Godbolt "unused labels" (the "funnel" filter icon) is checked and if you run with with pre-processor output -E it will filter out unused things.显然(正如@Shawn 在评论中指出的那样)这是因为 Godbolt“未使用的标签”(“漏斗”过滤器图标)中的编译器输出被检查,如果您使用预处理器输出-E ,它将过滤掉未使用的东西.

You can fix this by simply referring to my_x somewhere in the compiled program or simply uncheck "unused labels" in Godbolt when viewing pre-processor output.您可以通过在编译程序中的某处简单地引用my_x或在查看预处理器输出时在 Godbolt 中取消选中“未使用的标签”来解决此问题。

As a side-note, please note that "trailing comma" in the end of enums was added in C99, so if you use an older C90 compiler you might experience strange compiler hiccupts because of that.作为旁注,请注意枚举末尾的“尾随逗号”是在 C99 中添加的,因此如果您使用较旧的 C90 编译器,您可能会因此遇到奇怪的编译器打嗝。

As per Godbolt open issue (feature request) 1380 , Godbolt does not (yet) support the -E option [ emphasis mine]:根据Godbolt open issue (feature request) 1380 ,Godbolt 不(还)支持-E选项 [强调我的]:

Support -E output in some way #1380以某种方式支持 -E 输出 #1380

[...] [...]

apmorton commented on May 15, 2019 apmorton 于 2019 年 5 月 15 日发表评论

appears to be caused by the combination of label and directive filtering.似乎是由标签和指令过滤的组合引起的。

[...] [...]

you should probably turn off any filters that are meant to parse assembly when you are instructing the compiler to output something other than assembly.当您指示编译器输出程序集以外的内容时,您可能应该关闭任何用于解析程序集的过滤器。

not sure that this is really a bug in the filters, as they are technically doing what they are supposed to - its just that they are running on output they aren't meant to.不确定这是否真的是过滤器中的错误,因为它们在技术上正在做他们应该做的事情 - 只是他们在不打算的输出上运行。

maybe it could detect usage of -E and disable filters meant for assembly output?也许它可以检测到 -E 的使用并禁用用于程序集输出的过滤器?

partouf commented on May 18, 2019 partouf 于 2019 年 5 月 18 日发表评论

The assembly parser isn't really meant to support -E , it's expecting asm and not C++, so there's really nothing wrong.程序集解析器并不是真的要支持-E ,它期待的是 asm 而不是 C++,所以真的没有错。

We can consider disabling the parser on -E , but we would need to detect that on a per-compiler basis.我们可以考虑在-E上禁用解析器,但我们需要在每个编译器的基础上检测它。

Thus, the issue mentioned by OP as well as the more minimal example shown by @Jabberwocky in a comment both produces the expected program if actually compiled, but not the expected preprocessor output when using the -E option (which should do nothing except preprocessing).因此,OP 提到的问题以及@Jabberwocky 在评论中显示的更简单的示例,如果实际编译,都会产生预期的程序,但在使用-E选项时不会产生预期的预处理器输出(除了预处理之外,它什么都不做) .

I bet it is a newline problem in godbolt.我敢打赌这是 Godbolt 中的换行问题。 Workaround here:解决方法在这里:

#define Func(X, Y, Z) ,my_ ## X = Y


enum e {
    x, y, z

    Func(x, y, z),
};

https://godbolt.org/z/Mznf5o https://godbolt.org/z/Mznf5o

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM