MISRA C++:2023 Rule 19.0.3
#include
directives should only be preceded by preprocessor
directives or comments
Since R2024b
Description
Rule Definition
#include
directives should only be preceded by preprocessor
directives or comments. 1
Rationale
Grouping all #include preprocessor directives at the beginning of the source file makes the code more readable. #include directives might include header files where macros are defined. If you use such a macro before including its definition, you might encounter unexpected code behavior.
Polyspace Implementation
Polyspace® reports violations of this rule when an #include
directive comes after any code that is not a comment, preprocessor directive, or
language specification token. Polyspace ignores code that is hidden by using conditional compilation directives
such as #if
or #ifdef
. Polyspace does not report a violation of this rule when an
#include
directive is located within an extern
"C"
block.
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: Advisory |
Version History
Introduced in R2024b
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.