Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to suppress GCC variadic macro argument warning for zero arguments for a particular macro definition within a source file [duplicate]

Tags:

c++

gcc

macros

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.

like image 838
gnzlbg Avatar asked Feb 23 '16 20:02

gnzlbg


People also ask

How do I eliminate the warning-wparentheses in GCC for IF statements?

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 .

How to avoid -Werror=main warnings in GCC?

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.

Can you leave the variable argument out of C++?

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.

Can a macro accept a variable number of arguments?

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.


2 Answers

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

like image 156
Zulan Avatar answered Nov 15 '22 20:11

Zulan


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
like image 29
Michael S. Avatar answered Nov 15 '22 20:11

Michael S.