无法抑制对旧版 GCC 的警告

标签 c gcc compiler-warnings

我正在使用一些自动生成的代码,这些代码往往有类似的行

void f(int16_t a)
{
    if (a < INT32_MAX)
       ...
}

这显然会产生警告,例如:

warning: comparison is always true due to limited range of data type

我无法更改 GCC 命令行选项,我只能在自动生成的内容之前/之后添加代码,如下所示:

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wtype-limits"
#pragma GCC diagnostic ignored "-Wtautological-constant-out-of-range-compare"

#include "autogenerated.h"

#pragma GCC diagnostic pop

它在较新的 GCC 版本上运行良好,但我需要支持回 gcc-3.4.6。我可以处理丢失的#pragma GCC Diagnostic Push,但似乎此警告在以前的版本中与-Wextra捆绑在一起。因此,我尝试将其全部禁用:

// GCC 4.6+ needed for push/pop
#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)
#pragma GCC diagnostic push
#endif
// Disable warnings about unknown pragmas in case some of the options
// aren't present in the current version
#pragma GCC diagnostic ignored "-Wpragmas"
// Disable the problematic warnings
#pragma GCC diagnostic ignored "-Wtautological-constant-out-of-range-compare"
#pragma GCC diagnostic ignored "-Wsign-compare"
#pragma GCC diagnostic ignored "-Wtype-limits"
// Sometimes it is bundled in -Wextra without a specific one, so disable that too
#pragma GCC diagnostic ignored "-Wextra"
// Disable everything else as well!
#pragma GCC diagnostic ignored "-Wall"

#include "autogenerated.h"

#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)
#pragma GCC diagnostic pop
#else
#pragma GCC diagnostic warning "-Wtautological-constant-out-of-range-compare"
#pragma GCC diagnostic warning "-Wsign-compare"
#pragma GCC diagnostic warning "-Wtype-limits"
#pragma GCC diagnostic warning "-Wextra"
#pragma GCC diagnostic warning "-Wall"
#pragma GCC diagnostic warning "-Wpragmas"
#endif

我仍然收到该警告。


编辑

经过一番研究,似乎这是不可能的。 gcc-4.2.4 中添加了#pragma GCC Diagnostic,并且这些行被完全忽略。没有警告,因为 -Wpragmas 也不存在于 gcc-3.4.6 中。

我将不得不改变/欺骗生成器以不创建可警告的代码。

最佳答案

尝试使用#pragma GCC system_header

来自手册:

The header files declaring interfaces to the operating system and runtime libraries often cannot be written in strictly conforming C. Therefore, GCC gives code found in system headers special treatment. All warnings, other than those generated by ‘#warning’ (see Diagnostics), are suppressed while GCC is processing a system header.

关于无法抑制对旧版 GCC 的警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30350551/

相关文章:

c++ - 如何为类的内置类型成员变量获取/实现 "Uninitialized uses warning"消息?

c - C 中与网络相关的低级软件的读物、工具和库

c++ - 不同于按值传递和按 const ref 传递的程序集

c - 什么时候在 C 中使用 #define 定义静态函数有效?

gcc - 在 Code::Blocks 中应用 --enable-shared 或 -fPIC

c++ - 让编译器检查数组初始值设定项的数量

c++ - gcc 内部符号装饰有什么问题?

c++ - 快速图像处理

c++ - 在溢出的情况下变量的值是否总是负数

c - 为什么 argc 和 argv 的地址相差 12 个字节?