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.USE.UNKNOWNDIR

Unknown preprocessor directive is used.

MISRA C 2012 Rule 20.13 A line whose first token is # shall be a valid preprocessing directive

Category: Required

Analysis: Decidable, Single Translation Unit

Applies to: C90, C99

Amplification

White-space is permitted between the # and preprocessing tokens.

Rationale

A preprocessor directive may be used to conditionally exclude source code until a corresponding #else, #elif or #endif directive is encountered. A malformed or invalid preprocessing directive contained within the excluded source code may not be detected by the compiler, possibly leading to the exclusion of more code than was intended.

Requiring all preprocessor directives to be syntactically valid, even when they occur within an excluded block of code, ensures that this cannot happen.

Example

In the following example all the code between the #ifndef and #endif directives may be excluded if AAA is defined. The developer intended that AAA be assigned to x, but the #else directive was entered incorrectly and not diagnosed by the compiler.

#define AAA 2

int32_t foo ( void ) 
{
  int32_t x = 0;

#ifndef AAA
  x = 1; 
#else1      /* Non-compliant */
  x = AAA; 
#endif

  return x; 
}

The following example is compliant because the text #start appearing in a comment is not a token.

/* 
#start is not a token in a comment
*/

MISRA-C++ 2008 Rule 16—0—8 (required): If the # token appears as the first token on a line, then it shall be immediately followed by a preprocessing token

Unknown preprocessor directive is used.

Rationale

When a section of source code is excluded by preprocessor directives, the content of each excluded statement is ignored until a '#else', '#elif' or '#endif' directive is encountered (depending on the context). If one of these excluded directives is badly formed, it may be ignored without warning by a compiler with unexpected consequences.

The requirement of this rule is that all preprocessor directives shall be syntactically valid even when they occur within an excluded block of code.

In particular, ensure that '#else' and '#endif' directives are not followed by any characters other than white-space. Compilers are not always consistent in enforcing this requirement.

Example

#define AAA 2
int32_t foo(void)
{
   int32_t x = 0;
   ...
#ifndef AAA
   x = 1;
#else1           // Non-compliant
   x = AAA;
#endif
   ...
   return x;
}