如何禁用特定的未知#pragma警告?(GCC和/或叮当声)

时间:2021-10-11 06:59:13

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

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

My question is - 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 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, there is a simple way to register a custom pragma which would do nothing?

例如,可能有一种简单的方法来注册一个不做任何事情的自定义实用程序?

Would be great to know if there is such an option is Visual Studio too, but that is less important.

如果Visual Studio也有这样的选项,那就太好了,但这并不重要。

Thank you!

谢谢你!


"but why ultimately he's playing with custom pragmas?"

“但是,他为什么要和传统的实用主义者一起玩呢?”

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.

我的源代码由两个编译器解析。其中有一个特殊的#pragma,另一个则不知道。当然,我可以写上#ifdef compiler er_identification_macro…#endif围绕#pragma的每个实例,但是这样做会很麻烦。

3 个解决方案

#1


13  

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.

GCC和Clang都有内部接口,允许语言前端向预处理器注册#pragma处理程序——请参阅GCC的libcpp/指令。c和叮当声的lib / Lex /编译指示。cpp——但是,就我所见,没有什么可以让您根据命令行选项修改注册了哪些处理程序(除了您正在编译的语言变体所暗示的)。

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

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

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.

请注意,投票最高的答案比公认的答案要好。-Wno-unknown-pragmas可以简单地在命令行中添加在打开警告的任何东西之后(比如-Wall)。

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.

我的源代码由两个编译器解析。其中有一个特殊的#pragma,另一个则不知道。当然,我可以写上#ifdef compiler er_identification_macro…#endif围绕#pragma的每个实例,但是这样做会很麻烦。

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.

对我来说,向编译器隐藏任何#pragma似乎是正确的,因为编译器并不期望以您所希望的方式理解它,因为#pragma的目的是提供一种机制来调用编译器中实现定义的行为。

(If you do end up doing this, note that Clang defines __clang__, but both GCC and Clang define __GNUC__.)

(如果您最终这样做了,请注意Clang定义了__clang__,但是GCC和Clang都定义了__GNUC__。)

#2


1  

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.

我假设您想要禁用pragma警告,因为它在一个平台上是有效的,而在另一个平台上是无效的。如果是这样,您可以使用宏来选择性地启用pragma,从而消除了抑制警告的需要。

For example, if you want the pragma on Visual C++ only, you can do:

例如,如果您只想要Visual c++的pragma,您可以做到:

#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;}

#3


-2  

  • 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++ standarization rule).
  • 编译器不允许自定义实用程序,因为实用程序(大部分)是编译器和/或链接器控制指令。由于这与特定的编译器实现和特性非常接近,所以对于用户来说,“定义新实用程序”的应用是什么?实际上,在特定的编译器上实现的可用的pragma指令是完全独立于供应商的(没有c++标准化规则)。
  • 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).
  • 可能您希望使用实用程序来标记代码的特殊部分(e)。提供您自己的预处理器)因为您正在要求无操作指令。这可以使用预处理器(#define)完成。
  • Another possibility for custom "markup" in C/C++ code e.g: #MY_PRAGMA is to use your own preprocessor before the C/C++ one.
  • C/ c++代码e中自定义“标记”的另一种可能性。g: #MY_PRAGMA是在C/ 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.

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

#1


13  

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.

GCC和Clang都有内部接口,允许语言前端向预处理器注册#pragma处理程序——请参阅GCC的libcpp/指令。c和叮当声的lib / Lex /编译指示。cpp——但是,就我所见,没有什么可以让您根据命令行选项修改注册了哪些处理程序(除了您正在编译的语言变体所暗示的)。

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

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

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.

请注意,投票最高的答案比公认的答案要好。-Wno-unknown-pragmas可以简单地在命令行中添加在打开警告的任何东西之后(比如-Wall)。

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.

我的源代码由两个编译器解析。其中有一个特殊的#pragma,另一个则不知道。当然,我可以写上#ifdef compiler er_identification_macro…#endif围绕#pragma的每个实例,但是这样做会很麻烦。

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.

对我来说,向编译器隐藏任何#pragma似乎是正确的,因为编译器并不期望以您所希望的方式理解它,因为#pragma的目的是提供一种机制来调用编译器中实现定义的行为。

(If you do end up doing this, note that Clang defines __clang__, but both GCC and Clang define __GNUC__.)

(如果您最终这样做了,请注意Clang定义了__clang__,但是GCC和Clang都定义了__GNUC__。)

#2


1  

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.

我假设您想要禁用pragma警告,因为它在一个平台上是有效的,而在另一个平台上是无效的。如果是这样,您可以使用宏来选择性地启用pragma,从而消除了抑制警告的需要。

For example, if you want the pragma on Visual C++ only, you can do:

例如,如果您只想要Visual c++的pragma,您可以做到:

#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;}

#3


-2  

  • 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++ standarization rule).
  • 编译器不允许自定义实用程序,因为实用程序(大部分)是编译器和/或链接器控制指令。由于这与特定的编译器实现和特性非常接近,所以对于用户来说,“定义新实用程序”的应用是什么?实际上,在特定的编译器上实现的可用的pragma指令是完全独立于供应商的(没有c++标准化规则)。
  • 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).
  • 可能您希望使用实用程序来标记代码的特殊部分(e)。提供您自己的预处理器)因为您正在要求无操作指令。这可以使用预处理器(#define)完成。
  • Another possibility for custom "markup" in C/C++ code e.g: #MY_PRAGMA is to use your own preprocessor before the C/C++ one.
  • C/ c++代码e中自定义“标记”的另一种可能性。g: #MY_PRAGMA是在C/ 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.

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