1

I'm using a GCC #pragma like this:

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
...
#pragma GCC diagnostic pop

This works great and removes the erroneous maybe-uninitialized errors that I'm seeing.

However, in old-ish versions of LLVM/clang++ (seen in clang version 9.0.0) this pragma is unknown and throws a warning. Since we also use -Werror, these warnings are turned to errors and compilation fails. See this output:

/home/path/to/include.hpp:22:32: warning: unknown warning group '-Wmaybe-uninitialized', ignored [-Wunknown-warning-option]
#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
                               ^
In file included from /home/path/to/main.cpp:1:

/home/path/to/include.hpp:22:32: error: unknown warning group '-Wmaybe-uninitialized', ignored [-Werror,-Wunknown-warning-option]

#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
                               ^

1 error generated.

How can I use this GCC #pragma when compiling with LLVM?

jlconlin
  • 14,206
  • 22
  • 72
  • 105
  • 2
    Could you wrap in `#ifdef __GNUC__`? – Nate Eldredge Nov 02 '20 at 21:35
  • 1
    You could also, as the message suggests, use `-Wno-unknown-warning-option` when compiling with clang. – Nate Eldredge Nov 02 '20 at 21:37
  • @NateEldredge Something like that could, and should. Potentially with an equivalent for LLVM in the else branch. After thinking hard about whether the diagnostic may be actually correct, and even if not whether it may be simpler to just bite the apple and initialize the bloody thing, redundantly. A move of an immediate to a register is not *that* expensive ;-). – Peter - Reinstate Monica Nov 02 '20 at 21:37
  • 3
    @NateEldredge clang also defines `__GNUC__`. Better check for `__GNUC__ && !__clang__`. [More info](https://stackoverflow.com/questions/28166565/detect-gcc-as-opposed-to-msvc-clang-with-macro) – rustyx Nov 02 '20 at 21:42
  • Thanks @NateEldredge. It seems that using the `-Wno-unkown-warning-option` is the simplest way to approach this. – jlconlin Nov 02 '20 at 21:58

0 Answers0