如何禁用特定的未知 #pragma 警告(GCC 和/或 Clang)

发布于 2024-11-18 07:48:39 字数 966 浏览 3 评论 0原文

我知道如何禁用所有未知的#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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

罪#恶を代价 2024-11-25 07:48:39

我有理由确信没有任何办法可以做到这一点。

GCC 和 Clang 都有内部接口,允许语言前端向预处理器注册 #pragma 处理程序 - 请参阅 GCC 的 libcpp/directives.c 和 Clang 的 lib/ Lex/Pragma.cpp - 但是,据我所知,没有任何东西可以让您修改注册的处理程序(超出您正在编译的语言变体所暗示的内容) for) 基于命令行选项。

我知道如何禁用所有未知的#pragma警告。例如,此处给出了答案:SO:如何禁用#pragma警告?

请注意,得票最高的答案比那里接受的答案更好。 -Wno-unknown-pragmas 可以简单地添加到命令行任何打开警告的内容(如-Wall)之后。

我的源代码由两个编译器解析。其中一个中有一个特殊的#pragma,另一个不知道。当然,我可以将 #ifdef COMPILER_IDENTIFICATION_MACRO ... #endif 放在 #pragma 的每个实例周围,但这会很麻烦。

从更哲学的角度来看,我认为这确实是正确的解决方案,尽管可能很麻烦!

对我来说,从编译器中隐藏任何 #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's libcpp/directives.c and Clang's lib/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.

I know how to disable all unknown #pragma warnings. The answer was given, for example, here: SO: How to disable #pragma warnings?

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.

My source 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.

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

深府石板幽径 2024-11-25 07:48:39

我假设您想要禁用编译指示警告,因为它在一个平台上有效,但在另一个平台上无效。如果是这种情况,您可以使用宏有选择地启用编译指示,从而无需抑制警告。

例如,如果您只想要 Visual C++ 上的编译指示,您可以这样做:

#if defined(_MSC_VER)
#    define SAFE_PRAGMA_UGUBUGU __pragma(ugubugu)
#else
#    define SAFE_PRAGMA_UGUBUGU 
#endif

然后,您可以编写

SAFE_PRAGMA_UGUBUGU
#pragma untiunti   
int main() {return 0;}

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:

#if defined(_MSC_VER)
#    define SAFE_PRAGMA_UGUBUGU __pragma(ugubugu)
#else
#    define SAFE_PRAGMA_UGUBUGU 
#endif

And then, you can write

SAFE_PRAGMA_UGUBUGU
#pragma untiunti   
int main() {return 0;}
岁月静好 2024-11-25 07:48:39
  • 编译器不允许自定义编译指示,因为编译指示(大部分)是编译器和/或链接器控制指令。由于这非常接近特定的编译器实现和功能,那么为用户“定义新的编译指示”有何用途?事实上,在特定编译器上实现的可用 pragma 指令完全独立于供应商(没有 C++ 标准化规则)。
  • 可能您想使用编译指示来标记代码的特殊部分(例如,为您自己的预处理器提供数据),因为您要求无操作指令。这可以使用预处理器(#defines)来完成。
  • C/C++ 代码中自定义“标记”的另一种可能性(例如#MY_PRAGMA)是在C/C++ 预处理器之前使用您自己的预处理器。

此类处理的示例用于 Qt 库,非与 Qt MOC 编译器交互的标准元对象系统。这用于扩展一些非 C++ 构造(例如,Q_OBJECT、Q_PROPERTY 等),稍后将其提供给 C++ 编译器的有效语法。

  • Compilers do not allow custom pragmas because pragmas are (mostly) compiler and/or linker controlling directives. Since this is very close to a particular compiler implementation and features, what would be the application of "defining new pragmas" for the user? In fact, what available pragma directives are implemented on a particular compiler is totally vendor independent (there is no C++ standardization rule).
  • May be you want to use pragmas for marking up special sections of your code (e.g., to feed your own preprocessor) since you are asking for no-op directives. This can be done using the preprocessor (#defines).
  • Another possibility for custom "markup" in C/C++ code, e.g., #MY_PRAGMA, is to use your own preprocessor before the C/C++ one.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文