如何禁用特定的未知 #pragma 警告(GCC 和/或 Clang)
我知道如何禁用所有未知的#pragma警告。例如,在 如何禁用 #pragma 警告?。
有没有办法禁用某个特定编译指示的“未知编译指示”警告?例如,如果我禁用 #pragma ugubugu
的警告,则以下代码:
#pragma ugubugu
#pragma untiunti
int main() {return 0;}
当使用以下任一方式编译时:
g++ pragma.cpp -Wall
clang++ pragma.cpp -Wall
应该产生一个警告:
warning: ignoring #pragma untiunti
例如,也许有一种简单的方法来注册自定义编译指示,它将什么也不做?
如果 Visual Studio 也有这样的选项,那就太好了,但这不太重要。
“但为什么他最终要使用自定义编译指示?”
我的源代码由两个编译器解析。其中一个中有一个特殊的#pragma
,而另一个则不知道。当然,我可以将 #ifdef COMPILER_IDENTIFICATION_MACRO ... #endif
放在 #pragma
的每个实例周围,但这会很麻烦。
I know how to disable all unknown #pragma warnings. The answer was given, for example, in How can I disable #pragma warnings?.
Is there a way to disable an 'unknown pragma' warning for one particular pragma? For example, if I disable warning for #pragma ugubugu
the following code:
#pragma ugubugu
#pragma untiunti
int main() {return 0;}
when compiled with either:
g++ pragma.cpp -Wall
clang++ pragma.cpp -Wall
should produce a single warning:
warning: ignoring #pragma untiunti
Maybe, for example, is there a simple way to register a custom pragma which would do nothing?
It would be great to know if there is such an option is Visual Studio too, but that is less important.
"But why ultimately is he playing with custom pragmas?"
My source code is parsed by two compilers. In one of those, there is a special #pragma
that is unknown to the other. Of course, I could probably put #ifdef COMPILER_IDENTIFICATION_MACRO ... #endif
around every instance of the #pragma
, but that would be cumbersome.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我有理由确信没有任何办法可以做到这一点。
GCC 和 Clang 都有内部接口,允许语言前端向预处理器注册
#pragma
处理程序 - 请参阅 GCC 的libcpp/directives.c
和 Clang 的lib/ Lex/Pragma.cpp
- 但是,据我所知,没有任何东西可以让您修改注册的处理程序(超出您正在编译的语言变体所暗示的内容) for) 基于命令行选项。请注意,得票最高的答案比那里接受的答案更好。
-Wno-unknown-pragmas
可以简单地添加到命令行任何打开警告的内容(如-Wall
)之后。从更哲学的角度来看,我认为这确实是正确的解决方案,尽管可能很麻烦!
对我来说,从编译器中隐藏任何
#pragma
似乎是正确的,因为编译器预计不会以您想要的方式理解它,因为#pragma
的全部要点是提供一种在编译器中调用实现定义的行为的机制。(如果您最终这样做了,请注意 Clang 定义了 __clang__,但 GCC 和 Clang 都定义了 __GNUC__。)
I'm reasonably sure that there isn't any way to do this.
Both GCC and Clang do have internal interfaces which allow the language frontend to register
#pragma
handlers with the preprocessor - see GCC'slibcpp/directives.c
and Clang'slib/Lex/Pragma.cpp
- but, as far as I can see, there is nothing which lets you modify which handlers are registered (beyond what is implied by the language variant you're compiling for) based on command line options.Note that the highest voted answer is better than the accepted one there.
-Wno-unknown-pragmas
can simply be added on the command line after anything (like-Wall
) which turns the warning on.From a more philisophical viewpoint, I think this is really the right solution, cumbersome though it may be!
It seems correct to me to hide any
#pragma
from a compiler which is not expected to understand it in the way that you intend, given that the whole point of#pragma
is to provide a mechanism for invoking implementation-defined behaviour in the compiler.(If you do end up doing this, note that Clang defines
__clang__
, but both GCC and Clang define__GNUC__
.)我假设您想要禁用编译指示警告,因为它在一个平台上有效,但在另一个平台上无效。如果是这种情况,您可以使用宏有选择地启用编译指示,从而无需抑制警告。
例如,如果您只想要 Visual C++ 上的编译指示,您可以这样做:
然后,您可以编写
I assume you want to disable the pragma warnings because it's something that is valid on one platform but not another. If that's the case, you can use macros to selectively enable the pragma, eliminating the need to suppress the warning.
For example, if you want the pragma on Visual C++ only, you can do:
And then, you can write
此类处理的示例用于 Qt 库,非与 Qt MOC 编译器交互的标准元对象系统。这用于扩展一些非 C++ 构造(例如,Q_OBJECT、Q_PROPERTY 等),稍后将其提供给 C++ 编译器的有效语法。
An example of this type of processing is used for the Qt library, non-standard metaobject system which interacts with the Qt MOC compiler. This is used to expand some non-C++ constructs (for example, Q_OBJECT, Q_PROPERTY, etc.) that is later fed with a valid syntax to the C++ compiler.