简体   繁体   English

如何取消对源文件中特定宏定义的零参数的GCC可变参数宏参数警告

[英]How to suppress GCC variadic macro argument warning for zero arguments for a particular macro definition within a source file

I want to suppress GCC variadic macro argument warning for zero arguments, produced for example by: 我想抑制零参数产生的GCC可变参数宏参数警告,例如:

// for illustration purposes only:
int foo(int i) { return 0; };
#define FOO(A, ...) foo(A, ##__VA_ARGS__)
FOO(1);
     ^  warning: ISO C++11 requires at least one argument for the "..." in a variadic macro

for a particular macro definition within a source file when using GCC 5.3.0. 使用GCC 5.3.0时针对源文件中的特定宏定义。

In clang this is done as follows: 在clang中,此操作如下:

// ... large file
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wgnu-zero-variadic-macro-arguments"
#define FOO(A, ...) foo(A, ##__VA_ARGS__)
#pragma clang diagnostic pop
// ... large file

// not necessary on the same file
FOO(1);  // doesnt trigger the warning

In gcc it looks like -pedantic is a magical kind of warning type so the following just doesn't work: 在gcc中, -pedantic是一种神奇的警告类型,因此以下操作无效:

// ... large file
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpedantic"
#define FOO(A, ...) foo(A, ##__VA_ARGS__)
#pragma GCC diagnostic pop
// ... large file

Just to be clear, the warning should be enabled in the whole program except in this particular snippet of code. 只是为了清楚起见,应该在整个程序中启用警告,但此特定代码段除外。 This is about fine grained control. 这是关于细粒度的控制。 Disabling the warning in GCC for the whole program can be achieved by just not passing -pedantic to the compiler. 只需不将-pedantic传递给编译器,就可以在整个程序的GCC中禁用警告。

You should be able to suppress this using 您应该可以抑制使用

#pragma GCC system_header

But that applies for the rest of the file, and you cannot only use it in included files. 但这适用于文件的其余部分,您不能仅在包含的文件中使用它。 So doesn't provide perfect scoping and may require some reshuffling / indirect inclusion of header files. 因此,它不能提供完美的作用域,并且可能需要重新组合/间接包含头文件。

(But quite frankly, if you can't fix the header file to be standards-conforming, you might as well consider the whole header a system_header, which excludes it from producing most warnings.) (但坦率地说,如果您不能将头文件固定为符合标准,则最好将整个头文件视为system_header,以免产生大多数警告。)

See https://gcc.gnu.org/onlinedocs/cpp/System-Headers.html 参见https://gcc.gnu.org/onlinedocs/cpp/System-Headers.html

You can try to use this: 您可以尝试使用此方法:

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wvariadic-macros"
#define FOO(A, ...) foo(A, ##__VA_ARGS__)
#pragma GCC diagnostic pop

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

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