Code that contains many macros becomes hard to understand. This is even worse when the set of defined macros is not stable, and you have to know at
each point what macros are defined. Therefore, #undef
can decrease the readability of macros.
However, well-disciplined use of #undef
can also improve readability, for instance when defining a macro with a limited scope: The
macro is #defined
, used a couple of times to reduce code duplication, then immediately #undefed
.
This rule raises an issue when a #undef
undefines a macro that was defined in another file. It will also raise an issue for an
#undef
directive that tries to undefine a non-existing macro.
Noncompliant code example
#ifndef MY_HDR
#define MY_HDR
#endif
...
#undef MY_HDR /* Noncompliant */
Compliant solution
#define LEVEL(i) int const i = #i
LEVEL(Debug);
LEVEL(Warning);
LEVEL(Error);
#undef LEVEL