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.EXPANSION.DIRECTIVE

Directive-like tokens within a macro argument.

MISRA C 2012 Rule 20.6: Tokens that look like a preprocessing directive shall not occur within a macro argument

C90 [Undefined 50], C99 [Undefined 87]

Category: Required

Analysis: Decidable, Single Translation Unit

Applies to: C90, C99

Rationale

An argument containing sequences of tokens that would otherwise act as preprocessing directives leads to undefined behaviour.

Example

#define M( A ) printf ( #A )

#include <stdio.h>

void main ( void ) 
{
M ( 
#ifdef SW            /* Non-compliant */
       "Message 1" 
#else                /* Non-compliant */
       "Message 2" 
#endif               /* Non-compliant */
    ); 
}

The above may print

#ifdef SW "Message 1" #else "Message 2" #endif

or

"Message 2"

or exhibit some other behaviour.

MISRA-C 2004 Rule 19.9 (required): Arguments to a function-like macro shall not contain tokens that look like preprocessing directives

Directive-like tokens within a macro argument.

[Undefined 50]

If any of the arguments act like preprocessor directives, the behaviour when macro substitution is made can be unpredictable.

MISRA-C++ 2008 Rule 16-0-5 (required): Arguments to a function-like macro shall not contain tokens that look like preprocessing directives.

[Undefined 16.3(10)]

Rationale

If any of the arguments act like preprocessor directives, the behaviour when macro substitution is made can be unpredictable.

Example

#define M(A) printf ( #A )
void main ( )
{
   M(
#ifdef SW       // Non-compliant
   "Message 1"
#else           // Non-compliant
   "Message 2"
#endif          // Non-compliant
   );
}

The above may print

#ifdef SW "Message 1" #else "Message 2" #endif

or

Message 2