如何抑制源文件中特定宏定义的零参数的GCC可变宏宏参数警告 [英] How to suppress GCC variadic macro argument warning for zero arguments for a particular macro definition within a source file

查看:623
本文介绍了如何抑制源文件中特定宏定义的零参数的GCC可变宏宏参数警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想禁止零参数的GCC可变参数宏参数警告,例如:

I want to suppress GCC variadic macro argument warning for zero arguments, produced for example by:

// 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

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

for a particular macro definition within a source file when using GCC 5.3.0.

在clang中,操作如下:

In clang this is done as follows:

// ... 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

在gcc中,它看起来像 code>是一种神奇的警告类型,所以以下只是不工作:

In gcc it looks like -pedantic is a magical kind of warning type so the following just doesn't work:

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

该警告应该在整个程序中启用,除了在这个特定的代码片段。这是关于细粒度控制。通过不将 -pedantic 传递给编译器,可以在GCC中禁用整个程序的警告。

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.

推荐答案

您应该可以使用

#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.

(但坦率地说,如果你不能头文件是符合标准的,你可以考虑整个头一个system_header,它排除了它产生大多数警告。)

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

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

这篇关于如何抑制源文件中特定宏定义的零参数的GCC可变宏宏参数警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆