How to deprecate a macro in GCC?

前端 未结 4 695
醉话见心
醉话见心 2021-01-04 05:20

i Know how to use attribute deprecated to deprecate a function like this:

int old_fn () __attribute__ ((deprecated));

But

相关标签:
4条回答
  • 2021-01-04 05:43

    Nice, elegant solution, however depending on C99 being enabled (works with gcc 4.8.2 or later, not tested on earlier versions):

    #define DEPRECATED_MACRO1 _Pragma ("GCC warning \"'DEPRECATED_MACRO1' macro is deprecated\"") 7
    #define DEPRECATED_MACRO2(...) _Pragma ("GCC warning \"'DEPRECATED_MACRO2' macro is deprecated\"") printf(__VA_ARGS__)
    
    int main(int argc, char*argv[])
    {
        int n = DEPRECATED_MACRO1;
        DEPRECATED_MACRO2("%d\n", n);
        return 0;
    }
    
    0 讨论(0)
  • 2021-01-04 06:00

    Since Macros are not part of the compiler (they are a pre-processor function), there is no clean way of doing this. At best, place your deprecated macros in a new header file filled with #warnings. This will of course break existing code, but its one way to guarantee drawing attention to the deprecation.

    0 讨论(0)
  • 2021-01-04 06:03

    You could make sure that these macros would expand to something that would include a expression that would trigger the __attribute__((deprecated)) warning.

    For function-like macros this is pretty easy (especially with the comma operator), but for constant defines or nonstandard macros this may be more complicated, since the context in which these expand is different. I think you could do:

    #define DEPRECATE(name) static inline void __attribute__((deprecated)) deprecate_ ## name (void) { ; }
    ...
    #define MAX(x, y) (DEPRECATE(MAX), x>y?x:y)
    // yeah, yeah, it repeats args in the body, but it's just an example
    

    For a constant define you probably want to assume that the body has to evaluate without having to generate code, such as outside of a function body, in the targets of a switch/case, or as the initial value of a static variable within a function.

    This is tricky, but you may be able to do it for many things.

    I wish that C had a __builtin_warn(const char *) that would work at the compiler level (not-preprocessor) and make things like this easier.

    For constant defines you can do:

    #define THREE (DEPRICATED(THREE),3)
    
    0 讨论(0)
  • 2021-01-04 06:06

    I think the best you can do is something like this:

    #ifdef USE_DEPRECATED_MACROS
    #warning using deprecated macros
    #define OLD_MACRO 1
    ...
    #endif
    

    This way you force the user to e.g. add -DUSE_DEPRECATED_MACROS to their compiler options and they get a warning.

    0 讨论(0)
提交回复
热议问题