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

寵の児 提交于 2019-12-04 00:12:07
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();
                      );
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!