Potatoswatter
Potatoswatter

Reputation: 137770

Use pragma to disable a warning if implemented

Clang recently implemented an annoying warning. If I disable it using #pragma clang diagnostic ignored, then older Clang versions will emit an "unknown warning group" warning.

Is there some way to test whether a warning is implemented?

Upvotes: 2

Views: 1227

Answers (1)

Potatoswatter
Potatoswatter

Reputation: 137770

Recent versions of Clang implement the __has_warning feature-check macro. Since Clang emulates GCC (not vice-versa) with only one pool of warning flags, it's reasonable to code against GCC using feature-check introspection:

#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

This is a bit of cumbersome copypasta. It can be avoided using an inclusion file, like this:

#define SUPPRESS_WARNING "-Wwhatever"
#include "suppress_warning.h"

// Code that trips warning

#include "unsuppress_warning.h"

suppress_warning.h is a bit tricky, because __has_warning and #pragma do not accept macros as arguments. So, get it from Github or this Wandbox demo.

Upvotes: 5

Related Questions