16

I know how to use __attribute__((deprecated)) or [[deprecated]] to deprecate a function like this:

int old_fn() __attribute__ ((deprecated));
[[deprecated]] int old_fn2();

But how to deprecate a Macro like this:

#define OLD_MACRO 1

4 Answers 4

17

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;
}
4
  • This doesn't seem to work wth gcc 4.3.3. Nice option though.
    – mjs
    Feb 1, 2017 at 15:56
  • @mjs According to here, _Pragma should have been supported since gcc 3.0 (full C99 support since gcc 4.5). You might have to enable C99 explicitly with -std=c99 previously to 4.5 (possibly even later, don't really know when c99 got standard for gcc...) to make that work.
    – Aconcagua
    Feb 3, 2017 at 8:47
  • @Aconcagua I think _Pragma was recognised, it just didn't understand the content. But i might be wrong.
    – mjs
    Feb 3, 2017 at 9:17
  • 1
    @mjs To see if it understands the content, try #pragma GCC warning "'DEPRECATED_MACRO1' macro is deprecated". This is what the _Pragma stuff is equivalent to...
    – Aconcagua
    Feb 3, 2017 at 10:09
9

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.

3
  • 2
    As a matter of taste, I would prefer to have the user #define USE_DEPRECATED_MACROS 1 in their source before the #include rather than putting the flag in through the command line... but really, same result either way; they have to know what they're doing, and can back it out when possible (and lose the warning thereby). Good call! Apr 21, 2010 at 8:24
  • 7
    I'd do it the other way around: #ifndef NO_DEPRECATED_MACROS. That way, you issue the warning while maintaining source-compatibility for clients of your header file. The user can then add NO_DEPRECATED_MACROS to get rid of the warning, rather than his build breaking when he takes the new header file, and him having to read the release notes to find out why OLD_MACRO is gone. Of course if the user uses -Werror it all amounts to the same thing, but if he doesn't then maybe that's precisely because he doesn't want his build falling over because you're tinkering with warnings ;-) Apr 21, 2010 at 13:18
  • This wouldn't tell the user WHICH deprecated macro is being used / which ones he should replace by what etc.
    – Algoman
    Nov 20, 2017 at 11:58
7

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)
3
  • 1
    And indeed if you had that macro you would need to deprecate it.
    – Joshua
    Apr 22, 2016 at 19:59
  • 1
    @Joshua: In the next release of the software THREE won't be defined, but in the version after that, #define THREE (4).
    – nategoose
    Apr 22, 2016 at 20:02
  • "Implicit declaration of function 'DEPRICATED' is invalid in C99". And if I use DEPRECATE, I get "Expected expression".
    – Cœur
    Jul 27, 2020 at 13:22
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.