Rule Definition
When a section of source code is excluded by preprocessor directives, the content of each excluded statement is ignored until a #else, #elif or #endif directive is encountered (depending on the context). If one of these excluded directives is badly formed, it may be ignored without warning by a compiler with unexpected consequences.
The requirement of this rule is that all preprocessor directives shall be syntactically valid even
when they occur within an excluded block of code.
In particular, ensure that #else and #endif directives are not followed by any characters other than
white-space. Compilers are not always consistent in enforcing this requirement.
Remediation
Ensure that a pre-processing token follows the #else, #elif or #endif directives.
Make sure they are syntactically correct.
Violation Code Sample
#define AAA 2
int32_t foo(void)
{
int32_t x = 0;
...
#ifndef AAA
x = 1;
#else1 // Non-compliant
x = AAA;
#endif
...
return x;
}
Fixed Code Sample
#define AAA 2
int32_t foo(void)
{
int32_t x = 0;
...
#ifndef AAA
x = 1;
#else // Compliant
x = AAA;
#endif
...
return x;
}
Reference
MISRA C++ 2008, 16-0-8: If the # token appears as the first token on a line, then it shall be immediately followed by a preprocessing token.
AUTOSAR, C++, 2014, Rule M16-0-8: If the # token appears as the first token on a line, then it shall be immediately followed by a pre-processing token.
Related Technologies
Technical Criterion
Programming Practices - Unexpected Behavior
About CAST Appmarq
CAST Appmarq is by far the biggest repository of data about real IT systems. It's built on thousands of analyzed applications, made of 35 different technologies, by over 300 business organizations across major verticals. It provides IT Leaders with factual key analytics to let them know if their applications are on track.