Why is this an issue?
This is a draft version of a MISRA C++ 202x rule proposed for public review.
MISRA Rule 16.1.3
Analysis Type: Decidable,Single Translation Unit
As well as using a #define preprocessor directive, macro names may effectively be defined in other implementation-defined, ways.
For example some implementations support:
- Using a compiler command-line option, such as
-D, to allow macros to be defined prior to translation;
- Pre-defined macros provided by the compiler.
If an attempt is made to use an identifier in a preprocessor directive, and that identifier has not been defined as a macro name, then the
preprocessor will assume that it has a value of zero. This may not meet developer expectations.
The following example assumes that the identifier
M is not defined as a macro name.
#if M == 0 // Non-compliant
#if defined( N ) // Compliant - N is not evaluated, even if not a macro
#if N == 0 // Compliant - N is known to be defined at this point
// Compliant - B is only evaluated in ( B == 0 ) if it is defined
#if defined( B ) && ( B == 0 )
Copyright The MISRA Consortium Limited © 2023
S966 targets the same defect as this rule but for a non-mission-critical context.