如何禁止显示源文件中特定宏定义的零参数的 GCC 可变参数宏参数警告
How to suppress GCC variadic macro argument warning for zero arguments for a particular macro definition within a source file
我想抑制 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
使用 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
在 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
需要明确的是,除了这个特定的代码片段之外,应该在整个程序中启用警告。这是关于细粒度控制。在GCC中禁用整个程序的警告可以通过不将-pedantic
传递给编译器来实现。
您应该能够使用
#pragma GCC system_header
但这适用于文件的其余部分,您不能只在包含的文件中使用它。因此,它不能提供完美的范围,可能需要对头文件进行一些重新洗牌/间接包含。
(但坦率地说,如果您无法将头文件修复为符合标准,则不妨将整个标头视为system_header,这会将其排除在生成大多数警告之外。
见 https://gcc.gnu.org/onlinedocs/cpp/System-Headers.html
你可以尝试使用它:
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wvariadic-macros"
#define FOO(A, ...) foo(A, ##__VA_ARGS__)
#pragma GCC diagnostic pop
相关文章:
- 类模板参数推导-clang和gcc不同
- 带有自动参数的函数使用 GCC 编译,但不使用 Visual C++ 编译
- GCC/CLang不同意模板模板参数的部分特化
- 调试模板时出现问题.专门针对 Linux GCC 7、GCC 6、GCC 5、GCC 4.9 错误构建失败:模板参数 1
- 使用具有默认参数的函数模板进行 decltype 会使结果混乱(一个有趣的问题或 gcc 的错误)
- constexpr 函数的常量引用参数:gcc/msvc vs clang/icc
- 隐蔽的 gcc 参数到 CMake 文件
- typedef名称与可变模板参数名称一致时出现GCC错误
- 将带有模板化方法的类作为带有GCC的模板参数传递
- 在if constexpr中使用带参数包的概念时,升级到gcc 9后出现编译错误
- 模板模板参数导致Clang下的编译器错误,而不是GCC
- GCC 仪器 - 是否可以自动输出函数的参数?
- 带有 gcc 和 clang 的可变参数宏扩展的奇怪行为
- 修改嵌套 lambda 中捕获的参数:gcc 与 clang?
- 使用函数参数作为常量表达式的一部分 - gcc vs clang
- 使用 GCC 5 进行模板参数阴影
- 类数据成员指针的非类型模板参数包无法使用 gcc 编译
- 在 mingw64-gcc 上可能存在可变参数的错误
- 在折叠表达式中扩展参数包作为 lambda 捕获的一部分 - gcc 与 clang
- 打开模板参数:GCC 是否删除开关