-
Type:
Code Smell Detection
-
Status: Active
-
Resolution: Unresolved
-
Labels:
-
Default Severity:Critical
-
Impact:High
-
Likelihood:Low
-
Default Quality Profiles:Sonar way
-
Legacy Key:PPUndefUsage
-
Covered Languages:C, C++, Objective-C
-
Remediation Function:Constant/Issue
-
Constant Cost:2h
-
MISRA C 2004:19.6
-
MISRA C 2012:20.5
-
MISRA C++ 2008:16-0-3
-
PC-Lint:9021
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
See
- MISRA C:2004, 19.6 - #undef shall not be used.
- MISRA C++:2008, 16-0-3 - #undef shall not be used.
- MISRA C:2012, 20.5 - #undef should not be used
1.
|
C-Family | RSPEC-4372 |
|
Active | Unassigned |