11

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.

gnzlbg
  • 7,135
  • 5
  • 53
  • 106
  • 1
    @Zulan that question asks for the name of the warning flag that enables disables this warning. This question asks for how exactly use that warning flag to disable this warning in a macro definition within a source file (your answer fits better here). – gnzlbg Feb 23 '16 at 20:09
  • I would suggest to just edit the original question to clarify your intent. – Zulan Feb 23 '16 at 20:11
  • I could do that but then basically don't make much sense to me anymore (feels like changing the question too much). For somebody interested in turning of the warning for the whole program just knowing the flag is enough. This one is a bit... more about fine grained control since `-Wpedantic` looks more magical than other warnings in GCC (but not in clang). – gnzlbg Feb 23 '16 at 20:13
  • I'm going with your argument and moving my answer here. – Zulan Feb 23 '16 at 20:42
  • An alternative solution for the case presented would be `#define FOO(...) foo(__VA_ARGS__)` – M.M Feb 23 '16 at 20:49

2 Answers2

8

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

Zulan
  • 21,896
  • 6
  • 49
  • 109
1

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
Michael S.
  • 51
  • 3