Why is this an issue?
This is a draft version of a MISRA C++ 202x rule proposed for public review.
MISRA Rule 16.0.1
Analysis Type: Decidable,Single Translation Unit
This rule applies to all the lines within a translation unit, even if they are excluded by preprocessing.
Note: white-space is permitted before and after the
A preprocessor directive may be used to conditionally exclude source code until a corresponding #else, #elif or #endif
directive is encountered. A malformed or invalid preprocessing directive contained within the excluded source code may not be detected by the
compiler, possibly leading to the exclusion of more code than was intended.
Requiring all preprocessor directives to be syntactically valid, even when they occur within an excluded block of code, ensures that this cannot
In the following example all the code between the #ifndef and #endif directives may be excluded if
AAA is defined.
The developer intended that
AAA be assigned to
x, but the #else directive was entered incorrectly and not diagnosed
by the compiler.
#define AAA 2
int32_t x = 0;
x = 1;
#else1 // Non-compliant
x = AAA;
This rule does not apply to the following examples as the
# is not a preprocessing token:
// Not a preprocessing token within a comment \
#not a token
auto s = R"(
#text)"; // Use in a raw string literal is not a preprocessing token
Copyright The MISRA Consortium Limited © 2023
S977 targets the same defect as this rule but for a non-mission-critical context.