MISRA C++:2008 Rule 16-1-2
All #else, #elif and #endif preprocessor directives shall reside in the same file as the #if or #ifdef directive to which they are related
Description
Rule Definition
All #else, #elif and #endif preprocessor directives shall reside in the same file as the #if or #ifdef directive to which they are related.
Rationale
You use preprocessor directives, such as
#if...#elif...#else...#endif
, to conditionally include or exclude blocks
of code. If the different branches of such a directive reside in different source files, the
code can be confusing. If all the branches are not included in a project, the code might
behave in unexpected ways. To avoid confusion and unexpected behavior, keep the branches of
a conditional preprocessor directive within the same source file.
Polyspace Implementation
Polyspace® raises a violation of this rule if either of these conditions are true:
A corresponding
#if
directive cannot be found within a source file for every#else
,#elif
, or#endif
directive.A corresponding
#endif
directive cannot be found within a source file for every#if
directive.
Troubleshooting
If you expect a rule violation but Polyspace does not report it, see Diagnose Why Coding Standard Violations Do Not Appear as Expected.
Examples
Check Information
Group: Preprocessing Directives |
Category: Required |
Version History
Introduced in R2013b