How do I define a macro with multiple pragmas for Clang?

我只是一个虾纸丫 提交于 2019-12-09 14:09:37

问题


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!)


回答1:


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;



回答2:


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();
                      );


来源:https://stackoverflow.com/questions/13826722/how-do-i-define-a-macro-with-multiple-pragmas-for-clang

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!