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

后端 未结 2 586
伪装坚强ぢ
伪装坚强ぢ 2021-02-18 17:54

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 c         


        
2条回答
  •  粉色の甜心
    2021-02-18 18:35

    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;
    

提交回复
热议问题