Start here

Home
About Klocwork
What's new
Fixed issues
Release notes
Installation

Reference

C/C++ checkers
Java checkers
C# checkers
MISRA C 2004 checkers
MISRA C++ 2008 checkers
MISRA C 2012 checkers
MISRA C 2012 checkers with Amendment 1
Commands
Metrics
Troubleshooting
Reference

Product components

C/C++ Integration build analysis
Java Integration build analysis
Desktop analysis
Refactoring
Klocwork Static Code Analysis
Klocwork Code Review
Structure101
Tuning
Custom checkers

Coding environments

Visual Studio
Eclipse for C/C++
Eclipse for Java
IntelliJ IDEA
Other

Administration

Project configuration
Build configuration
Administration
Analysis performance
Server performance
Security/permissions
Licensing
Klocwork Static Code Analysis Web API
Klocwork Code Review Web API

Community

View help online
Visit RogueWave.com
Klocwork Support
Rogue Wave Videos

Legal

Legal information

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 related

Category: Required

Analysis: Decidable, Single Translation Unit

Applies to: C90, C99

Rationale

Confusion 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.

Example

file.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.

Rationale

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.

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