#define or #undef of a compiler-predefined macro
ID |
objectivec.maintainability.redefine_predefined_macro |
Severity |
low |
Remediation Complexity |
medium |
Remediation Risk |
medium |
Remediation Effort |
low |
Resource |
Preprocessor |
Language |
Objective-C |
Description
Reports a #define or #undef naming a macro the compiler predefines - FILE, LINE, APPLE, clang, VA_ARGS and similar - or the defined operator. Defining a feature-test builtin that the compiler may not provide, as in the usual #ifndef __has_feature shim, is not reported.
Rationale
Redefining or undefining a predefined macro is undefined behavior. Beyond the standard’s verdict, the practical damage is that every header included afterwards - including system and framework headers, which branch on APPLE, clang and the version macros - sees the replacement instead of the real value, so the compiler selects a different set of declarations from the one it was built against. Redefining FILE or LINE additionally corrupts assertion and logging output at the point where it is most needed.
The following code illustrates the pattern detected by this rule:
// FLAGGED: #define or #undef of a compiler-predefined macro
#define __FILE__ "redacted.m"