MISRAC2012-Rule-20.7
In this section:
Synopsis
(Required) The expansion of macro parameters shall be enclosed in parentheses.
Enabled by default
Yes
Severity/Certainty
High/Medium

Full description
An expansion of macro parameters was found that is not enclosed in parentheses. This check is identical to MISRAC++2023-19.3.4.
Coding standards
- MISRA C:2012 Rule-20.7
(Required) Expressions resulting from the expansion of macro parameters shall be enclosed in parentheses
- MISRA C++ 2023 19.3.4
(Required) Parentheses shall be used to ensure macro arguments are expanded appropriately
Code examples
The following code example fails the check and will give a warning:
void example(void) {
int r;
#define M( x, y ) ( x / y )
r = M ( 1 + 2, 1 - 2 );
}
The following code example passes the check and will not give a warning about this issue:
static struct str {
int val;
} s;
void example(void) {
int r;
int a[10];
/* already enclosed in macro def*/
#define M( x, y ) ( ( x ) << ( y ) )
r = M( 1 + 2, 3 + 4 );
/* no need after ## or # */
#define N( x ) a [ ##x ] = (x)
N ( 0 + 2 );
/* no need after . or ->, member name */
#define MEMBER( S, M ) ( S ).M
r = MEMBER ( s, val );
/* enclosed in inner macro */
#define F( X ) G( X )
#define G( Y ) ( Y )
r = F ( 2 );
/* enclosed at invocation site,
even single literal should have parentheses */
#define M( x, y ) ( x / y )
r = M ( ( 1 ), ( 2 + 3 ) );
}