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.DEFINE.NOPARS

Macro parameter with no parentheses.

MISRA-C Rule 19.10 (required): In the definition of a function-like macro each instance of a parameter shall be enclosed in parentheses unless it is used as the operand of # or ##.

[Koenig 78—81]

Within a definition of a function-like macro, the arguments shall be enclosed in parentheses. For example define an 'abs' function using:

#define abs(x) (((x) >= 0) ? (x) : -(x))

and not:

#define abs(x) ((x >= 0) ? x : -x)

If this rule is not adhered to then when the preprocessor substitutes the macro into the code the operator precedence may not give the desired results.

Consider what happens if the second, incorrect, definition is substituted into the expression:

z = abs( a - b );

giving:

z = ((a - b >= 0) ? a - b : -a - b);

The sub-expression '-a - b' is equivalent to '(-a)-b' rather than '-(a-b)' as intended. Putting all the parameters in parentheses in the macro definition avoids this problem.

MISRA-C++ Rule 16-0-6 (required): In the definition of a function-like macro, each instance of a parameter shall be enclosed in parentheses, unless it is used as the operand of # or ##.

Rationale

If parentheses are not used, then the operator precedence may not give the desired results when the preprocessor substitutes the macro into the code. Within a definition of a function-like macro, the arguments shall be enclosed in parentheses.

Example

Define an 'abs' function using:

#define abs(x) (((x) >= 0) ? (x) : -(x)) // Compliant

and not:

#define abs(x) ((x >= 0) ? x : -x) // Non-compliant

Consider what happens if the second, incorrect, definition is substituted into the expression:

z = abs( a - b );

giving:

z = ((a - b >= 0) ? a - b : -a — b);

The sub-expression '-a - b' is equivalent to '(-a)-b' rather than '-(a-b)' as intended. Putting all the parameters in parentheses in the macro definition avoids this problem.

#define subs(x) a ## x // Compliant