如何在源文件中为特定宏定义的零参数抑制GCC可变参数宏参数警告

gnz*_*lbg 10 c++ macros gcc

我想为零参数抑制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
Run Code Online (Sandbox Code Playgroud)

对于使用GCC 5.3.0时源文件中的特定宏定义.

在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
Run Code Online (Sandbox Code Playgroud)

在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
Run Code Online (Sandbox Code Playgroud)

为了清楚起见,除了这个特定的代码片段外,应该在整个程序中启用警告.这是关于细粒度控制.只需不传递-pedantic给编译器就可以在GCC中为整个程序禁用警告.

Zul*_*lan 7

您应该能够使用

#pragma GCC system_header

但这适用于文件的其余部分,您不能只在包含的文件中使用它。所以不提供完美的范围,可能需要一些重新组合/间接包含头文件。

(但坦率地说,如果您无法将头文件修复为符合标准,您不妨将整个头文件视为一个 system_header,从而使其不会产生大多数警告。)

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