如果已实施,请使用 pragma 禁用警告
Use pragma to disable a warning if implemented
Clang 最近实施了一个烦人的警告。如果我使用 #pragma clang diagnostic ignored
禁用它,那么旧的 Clang 版本将发出 "unknown warning group" 警告。
是否有某种方法可以测试是否实施了警告?
最新版本的 Clang 实现了 __has_warning
功能检查宏。由于 Clang 仅使用一个警告标志池来模拟 GCC(反之亦然),因此使用功能检查内省来针对 GCC 进行编码是合理的:
#if __GNUC__ && defined( __has_warning )
# if __has_warning( "-Wwhatever" )
# define SUPPRESSING
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wwhatever"
# endif
#endif
// Code that trips warning
#ifdef SUPPRESSING
# undef SUPPRESSING
# pragma GCC diagnostic pop
#endif
这是一个有点麻烦的copypasta。可以使用包含文件来避免,如下所示:
#define SUPPRESS_WARNING "-Wwhatever"
#include "suppress_warning.h"
// Code that trips warning
#include "unsuppress_warning.h"
suppress_warning.h
有点棘手,因为 __has_warning
和 #pragma
不接受宏作为参数。所以,从 Github or this Wandbox demo.
获取
Clang 最近实施了一个烦人的警告。如果我使用 #pragma clang diagnostic ignored
禁用它,那么旧的 Clang 版本将发出 "unknown warning group" 警告。
是否有某种方法可以测试是否实施了警告?
最新版本的 Clang 实现了 __has_warning
功能检查宏。由于 Clang 仅使用一个警告标志池来模拟 GCC(反之亦然),因此使用功能检查内省来针对 GCC 进行编码是合理的:
#if __GNUC__ && defined( __has_warning )
# if __has_warning( "-Wwhatever" )
# define SUPPRESSING
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wwhatever"
# endif
#endif
// Code that trips warning
#ifdef SUPPRESSING
# undef SUPPRESSING
# pragma GCC diagnostic pop
#endif
这是一个有点麻烦的copypasta。可以使用包含文件来避免,如下所示:
#define SUPPRESS_WARNING "-Wwhatever"
#include "suppress_warning.h"
// Code that trips warning
#include "unsuppress_warning.h"
suppress_warning.h
有点棘手,因为 __has_warning
和 #pragma
不接受宏作为参数。所以,从 Github or this Wandbox demo.