2

I cannot make the method to temporarily disable warnings in GCC (see How to disable GCC warnings for a few lines of code) work, at least not for the "unknown-pragmas" warning.

Compiling this code ...

#pragma comment(user,"This should depend on the command line options")

#pragma GCC diagnostic warning "-Wunknown-pragmas"
#pragma comment(user,"This should cause a warning")

#pragma GCC diagnostic error "-Wunknown-pragmas"
#pragma comment(user,"This should cause an error")

#pragma GCC diagnostic ignored "-Wunknown-pragmas"
#pragma comment(user,"This should be ignored")

... produces either no warning/error (except that the linker complais about a missing main), or when using -Wall or just -Wunknown-pragmas it produces one warning for each of the comment pragmas.

The behaviour that I would have expected is that each comment should have caused exactly what the comment says.

I think I can back my expectation with the documentation:

At the moment only warnings (normally controlled by ‘-W...’) can be controlled, and not all of them. Use -fdiagnostics-show-option to determine which diagnostics are controllable and which option controls them.

The warnings I get show as

warning: ignoring #pragma comment  [-Wunknown-pragmas]

and as the part in brackets tells us,

  • this diagnostic is controllable
  • and the option -Wunknown-pragmas controls it

Hence my code should work.

So what am I doing wrong?


version info:

$ gcc --version
gcc (Ubuntu 5.4.0-6ubuntu1~16.04.11) 5.4.0 20160609
Florian Weimer
  • 32,022
  • 3
  • 48
  • 92
Hagen von Eitzen
  • 2,109
  • 21
  • 25
  • Possible duplicate of [Suppress -Wunknown-pragmas warning in GCC](https://stackoverflow.com/questions/12842306/suppress-wunknown-pragmas-warning-in-gcc) – Florian Weimer Mar 09 '19 at 15:40

1 Answers1

2

This is a long-standing missing feature in the GCC C++ front end:

Warnings generated by preprocessing cannot be controlled using programs in g++. Unlike the C front end, pragmas are processed only after the preprocessing phase in the C++ front end.

Florian Weimer
  • 32,022
  • 3
  • 48
  • 92