41 lines
1.4 KiB
Plaintext
41 lines
1.4 KiB
Plaintext
When the inclusion and exclusion of blocks of statements is controlled by a series of preprocessor directives, confusion can arise if all of the relevant directives do not occur within one file. This rule requires that all preprocessor directives in a sequence of the form ``++#if++`` / ``++#ifdef++`` ... ``++#elif++`` ... ``++#else++`` ... ``++#endif++`` shall reside in the same file. Observance of this rule preserves good code structure and avoids maintenance problems.
|
|
|
|
|
|
Notice that this does not preclude the possibility that such directives may exist within included files provided that all directives that relate to the same sequence are located in one file.
|
|
|
|
|
|
== Noncompliant Code Example
|
|
|
|
----
|
|
// file1.hpp
|
|
...
|
|
#endif // Noncompliant
|
|
|
|
// file.cpp
|
|
#if 1 // Noncompliant
|
|
#include "file1.hpp"
|
|
----
|
|
|
|
|
|
== Compliant Solution
|
|
|
|
----
|
|
// file1.hpp
|
|
#if 1 // Compliant
|
|
...
|
|
#endif // Compliant
|
|
|
|
// file.cpp
|
|
#ifdef 1 // Compliant
|
|
#include "file1.hpp"
|
|
#endif // Compliant
|
|
----
|
|
|
|
|
|
== See
|
|
|
|
* MISRA C:2004, 19.17 - All #else, #elif and #endif preprocessor directives shall reside in the same file as the #if or #ifdef directive to which they are related.
|
|
* MISRA {cpp}:2008, 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.
|
|
* MISRA C:2012, 20.14 - All #else, #elif and #endif preprocessor directives shall reside same file as the #if, #ifdef or #ifndef directive to which they are related
|
|
|