MISRA.ELIF.OTHERFILE#elif in an improper file.
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 relatedCategory: Required Analysis: Decidable, Single Translation Unit Applies to: C90, C99 RationaleConfusion can arise when blocks of code are included or excluded by the use of conditional compilation directives which are spread over multiple files. Requiring that a #if directive be terminated within the same file reduces the visual complexity of the code and the chance that errors will be made during maintenance. Note: #if directives may be used within included files provided they are terminated within the same file. Example/* file1.c */ #ifdef A /* Compliant */ #include "file1.h" #endif /* End of file1.c */ /* file2.c */ #if 1 /* Non-compliant */ #include "file2.h" /* End of file2.c*/ /* file1.h */ #if 1 /* Compliant */ #endif /* End of file1.h */ /* file2.h */ #endif /* End of file1.h */ MISRA-C 2004 Rule 19.17 (required): All #else, #elif and #endif preprocessor directives shall reside in the same file as the #if or #ifdef directive to which they are related.#elif in an improper file. This rule is also covered by MISRA.ELSE.OTHERFILE and MISRA.ENDIF.OTHERFILE. 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 so long as all directives that relate to the same sequence are located in one file. Examplefile.c #define A ... #ifdef A ... #include "file1.h" # #endif ... #if 1 #include "file2.h" ... EOF file1.h #if 1 ... #endif /* Compliant */ EOF file2.h ... #endif /* Not compliant */ MISRA-C++ 2008 Rule 16-1-2 (required): All #else, #elif and #endif preprocessor directives shall reside in the same file as the #if or #ifdef directive to which they are related.This rule is also covered by MISRA.ELSE.OTHERFILE and MISRA.ENDIF.OTHERFILE. RationaleWhen 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. Example// file.cpp #define A ... #ifdef A ... #include "file1.hpp" # #endif ... #if 1 #include "file2.hpp" ... // file1.hpp #if 1 ... #endif // Compliant // file2.hpp ... #endif // Non-compliant |