Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Put multiple pragma directives into one preprocessor define [duplicate]

I need to push/pop several gcc diagnostics in my code. If that was needed in a single file I would do the following:

#pragma GCC diagnostic push
#pragma GCC diagnostic error "-Wformat"
#pragma GCC diagnostic error "-Wuninitialized"

...some code...

#pragma GCC diagnostic push

But I need this in multiple locations. So I want to have a #define or something similar. I thought about the following but c preprocessor does not allow #pragmas in #define.

#define PushWarnings \
    #pragma GCC diagnostic push \
    #pragma GCC diagnostic error "-Wformat" \
    #pragma GCC diagnostic error "-Wuninitialized"

Is there a way of achieving this?

like image 981
Paul Avatar asked Jul 23 '26 12:07

Paul


1 Answers

Yes, there is a way to do this. C99 introduced the _Pragma operator (also available in C++ since C++11).

A pragma such as

#pragma GCC diagnostic error "-Wformat"

can also be written as

_Pragma("GCC diagnostic error \"-Wformat\"")

The latter is not a # preprocessor directive, so it can be generated from macros:

#define PushWarnings \
    _Pragma("GCC diagnostic push") \
    _Pragma("GCC diagnostic error \"-Wformat\"") \
    _Pragma("GCC diagnostic error \"-Wuninitialized\"")
like image 153
melpomene Avatar answered Jul 25 '26 04:07

melpomene



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!