I'd like to add some macros to ease (un)setting a specific warning around routines that we are deprecating internally.
I'd like to turn this:
#pragma clang diagnostic push #pragma clang diagnostic warning "-Wdeprecated-declarations" void Foo() __attribute__((deprecated("Warning: deprecated routine"))) #pragma clang diagnostic pop
into this:
MY_DEPRECATED_BEGIN void Foo() MY_DEPRECATED MY_DEPRECATED_END
The MY_DEPRECATED_BEGIN
macro is giving me trouble as I have to specify two pragmas in a single macro. Can this be done?
(Bonus points for a solution that achieves the same effect using only the MY_DEPRECATED macro!)
void Foo() __attribute__((deprecated)); #define MY_DEPRECATED_BEGIN \ _Pragma("clang diagnostic push") \ _Pragma("clang diagnostic warning \"-Wdeprecated-declarations\"") int main() { MY_DEPRECATED_BEGIN Foo(); #pragma clang diagnostic pop }
The short answer to your technical question is that C99 provides the _Pragma("foo")
construct, which is equivalent to #pragma foo
but is processed slightly later, and doesn't need to be on a line by itself.
Your other problem is that -Wdeprecated-declarations
doesn't do what you think it does. Simply declaring a function as deprecated will never give you a diagnostic, because __attribute__((deprecated))
is supposed to be used (generally in header files). What causes the diagnostic is if you use a deprecated function — and it's at that point that the setting of -Wdeprecated
becomes relevant.
If you really just want to deprecate Foo
iff MY_DEPRECATED
is set, then the right way to do that is
#ifdef MY_DEPRECATED #define ATTRIBUTE_DEPRECATED __attribute__((deprecated)) #else #define ATTRIBUTE_DEPRECATED #endif void Foo() ATTRIBUTE_DEPRECATED;
You can use this solution:
#define NS_SUPPRESS_DIRECT_USE(expr) _Pragma("clang diagnostic push") \ _Pragma("clang diagnostic ignored \"-Wdeprecated-declarations\"")\ expr\ _Pragma("clang diagnostic pop")
Then just add it:
NS_SUPPRESS_DIRECT_USE( Foo(); );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With