MISRA.DEFINE.NOPARSMacro 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 ##.RationaleIf 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. ExampleDefine 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 |