I want to suppress GCC variadic macro argument warning for zero arguments, produced for example by:
// for illustration purposes only:
int foo(int i) { return 0; };
#define FOO(A, ...) foo(A, ##__VA_ARGS__)
FOO(1);
^ warning: ISO C++11 requires at least one argument for the "..." in a variadic macro
for a particular macro definition within a source file when using GCC 5.3.0.
In clang this is done as follows:
// ... large file
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wgnu-zero-variadic-macro-arguments"
#define FOO(A, ...) foo(A, ##__VA_ARGS__)
#pragma clang diagnostic pop
// ... large file
// not necessary on the same file
FOO(1); // doesnt trigger the warning
In gcc it looks like -pedantic
is a magical kind of warning type so the following just doesn't work:
// ... large file
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpedantic"
#define FOO(A, ...) foo(A, ##__VA_ARGS__)
#pragma GCC diagnostic pop
// ... large file
Just to be clear, the warning should be enabled in the whole program except in this particular snippet of code. This is about fine grained control. Disabling the warning in GCC for the whole program can be achieved by just not passing -pedantic
to the compiler.
When there is the potential for this confusion, GCC issues a warning when this flag is specified. To eliminate the warning, add explicit braces around the innermost if statement so there is no way the else can belong to the enclosing if. The resulting code looks like this: This warning is enabled by -Wparentheses .
And both, GCC and Clang, know that. You would get -Werror=main warnings if you try to remove argc from the signature. One way to avoid warnings is to write different source code. In general, avoiding warnings by writing better source code is the preferable way. But note the subtle difference between the words different and better.
First, in GNU CPP, and in C++ beginning in C++20, you are allowed to leave the variable argument out entirely: Second, C++20 introduces the __VA_OPT__ function macro. This macro may only appear in the definition of a variadic macro.
A macro can be declared to accept a variable number of arguments much as a function can. The syntax for defining the macro is similar to that of a function. Here is an example: This kind of macro is called variadic.
You should be able to suppress this using
#pragma GCC system_header
But that applies for the rest of the file, and you cannot only use it in included files. So doesn't provide perfect scoping and may require some reshuffling / indirect inclusion of header files.
(But quite frankly, if you can't fix the header file to be standards-conforming, you might as well consider the whole header a system_header, which excludes it from producing most warnings.)
See https://gcc.gnu.org/onlinedocs/cpp/System-Headers.html
You can try to use this:
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wvariadic-macros"
#define FOO(A, ...) foo(A, ##__VA_ARGS__)
#pragma GCC diagnostic pop
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With