无法抑制旧版 GCC 上的警告

Can't suppress warnings on an older GCC

我正在使用一些自动生成的代码,这些代码往往包含

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

这显然会产生如下警告:

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

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

#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

而且我仍然收到该警告。


编辑

看了几眼,好像不行。 #pragma GCC diagnostic 已添加到 gcc-4.2.4 中,并且完全忽略了这些行。没有警告,因为 -Wpragmas 也不存在于 gcc-3.4.6 中。

我将不得不求助于 changing/tricking 生成器来不创建可警告代码。

尝试使用 #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.