MISRA C:2012 Rule 20.14
All #else, #elif and #endif preprocessor directives shall reside in the same file as the #if, #ifdef or #ifndef 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, #ifdef or #ifndef directive to which they are related1 .
Rationale
When conditional compilation directives include or exclude blocks
of code and are spread over multiple files, confusion arises. If you
terminate an #if
directive within the same file,
you reduce the visual complexity of the code and the chances of an
error.
If you terminate #if
directives within the
same file, you can use #if
directives in included
files
Additional Message in Report
'#else' not within a conditional.
'#elseif' not within a conditional.
'#endif' not within a conditional.
Unterminated conditional directive.
Troubleshooting
If you expect a rule violation but do not see it, refer to Diagnose Why Coding Standard Violations Do Not Appear as Expected.
Check Information
Group: Preprocessing Directives |
Category: Required |
AGC Category: Required |
1 All MISRA coding rules and directives are © Copyright The MISRA Consortium Limited 2021.
The MISRA coding standards referenced in the Polyspace® Bug Finder™ documentation are from the following MISRA standards:
MISRA C:2004
MISRA C:2012
MISRA C:2023
MISRA C++:2008
MISRA C++:2023
MISRA and MISRA C are registered trademarks of The MISRA Consortium Limited 2021.