CygnusX1
CygnusX1

Reputation: 21818

How to disable a particular unknown #pragma warning (GCC and/or Clang)

I know how to disable all unknown #pragma warnings. The answer was given, for example, in How can I disable #pragma warnings?.

Is there a way to disable an 'unknown pragma' warning for one particular pragma? For example, if I disable warning for #pragma ugubugu the following code:

#pragma ugubugu
#pragma untiunti

int main() {return 0;}

when compiled with either:

g++ pragma.cpp -Wall
clang++ pragma.cpp -Wall

should produce a single warning:

warning: ignoring #pragma untiunti

Maybe, for example, is there a simple way to register a custom pragma which would do nothing?

It would be great to know if there is such an option is Visual Studio too, but that is less important.


"But why ultimately is he playing with custom pragmas?"

My source code is parsed by two compilers. In one of those, there is a special #pragma that is unknown to the other. Of course, I could probably put #ifdef COMPILER_IDENTIFICATION_MACRO ... #endif around every instance of the #pragma, but that would be cumbersome.

Upvotes: 36

Views: 42131

Answers (3)

Hernán
Hernán

Reputation: 4587

  • Compilers do not allow custom pragmas because pragmas are (mostly) compiler and/or linker controlling directives. Since this is very close to a particular compiler implementation and features, what would be the application of "defining new pragmas" for the user? In fact, what available pragma directives are implemented on a particular compiler is totally vendor independent (there is no C++ standardization rule).
  • May be you want to use pragmas for marking up special sections of your code (e.g., to feed your own preprocessor) since you are asking for no-op directives. This can be done using the preprocessor (#defines).
  • Another possibility for custom "markup" in C/C++ code, e.g., #MY_PRAGMA, is to use your own preprocessor before the C/C++ one.

An example of this type of processing is used for the Qt library, non-standard metaobject system which interacts with the Qt MOC compiler. This is used to expand some non-C++ constructs (for example, Q_OBJECT, Q_PROPERTY, etc.) that is later fed with a valid syntax to the C++ compiler.

Upvotes: -6

sffc
sffc

Reputation: 6424

I assume you want to disable the pragma warnings because it's something that is valid on one platform but not another. If that's the case, you can use macros to selectively enable the pragma, eliminating the need to suppress the warning.

For example, if you want the pragma on Visual C++ only, you can do:

#if defined(_MSC_VER)
#    define SAFE_PRAGMA_UGUBUGU __pragma(ugubugu)
#else
#    define SAFE_PRAGMA_UGUBUGU 
#endif

And then, you can write

SAFE_PRAGMA_UGUBUGU
#pragma untiunti   
int main() {return 0;}

Upvotes: 10

Matthew Slattery
Matthew Slattery

Reputation: 47058

I'm reasonably sure that there isn't any way to do this.

Both GCC and Clang do have internal interfaces which allow the language frontend to register #pragma handlers with the preprocessor - see GCC's libcpp/directives.c and Clang's lib/Lex/Pragma.cpp - but, as far as I can see, there is nothing which lets you modify which handlers are registered (beyond what is implied by the language variant you're compiling for) based on command line options.

I know how to disable all unknown #pragma warnings. The answer was given, for example, here: SO: How to disable #pragma warnings?

Note that the highest voted answer is better than the accepted one there. -Wno-unknown-pragmas can simply be added on the command line after anything (like -Wall) which turns the warning on.

My source is parsed by two compilers. In one of those, there is a special #pragma, that is unknown to the other. Of course, I could probably put #ifdef COMPILER_IDENTIFICATION_MACRO ... #endif around every instance of the #pragma but that would be cumbersome.

From a more philisophical viewpoint, I think this is really the right solution, cumbersome though it may be!

It seems correct to me to hide any #pragma from a compiler which is not expected to understand it in the way that you intend, given that the whole point of #pragma is to provide a mechanism for invoking implementation-defined behaviour in the compiler.

(If you do end up doing this, note that Clang defines __clang__, but both GCC and Clang define __GNUC__.)

Upvotes: 28

Related Questions