C and C++


LANG.STRUCT.CGEN.EMPE : Excessive Macro Parameter Evaluation in C Generic

Summary

A C generic selection expression for which both of the following are true.

Properties

Class Name Excessive Macro Parameter Evaluation in C Generic
Significance style
Mnemonic LANG.STRUCT.CGEN.EMPE
Categories
MisraC2023 MisraC2023:23.7 A generic selection that is expanded from a macro should evaluate its argument only once
Misra2012 Misra2012:23.7 A generic selection that is expanded from a macro should evaluate its argument only once
Availability Available for C only (not C++).
Enabling Checks for this warning class are disabled by default, and require the unnormalized C ASTs for the project. To enable them, add the following WARNING_FILTER rule and RETAIN_UNNORMALIZED_C_AST specification to the project configuration file.
RETAIN_UNNORMALIZED_C_AST = Yes
WARNING_FILTER += allow class="Excessive Macro Parameter Evaluation in C Generic"
Note that retaining the unnormalized ASTs will increase the disk space used to store the project representation, and may make the analysis take longer.

Example

/* 'Excessive Macro Parameter Evaluation in C Generic' warning issued on following line
 * ('Inconsistent Macro Parameter Evaluation in C Generic' warning also issued when enabled)
 */
#define EMPE_ONE(x) _Generic(x, \
                             int:x+x, /* two expansions of x */ \
                             default:x)

#define EMPE_OK1(x) _Generic(x, int:x, default:x)  /* ok: at most one expansion of x per association */
#define EMPE_OK2(x) _Generic(x, int:1, default:0)  /* ok: no expansions of x */
#define EMPE_OK3(x) _Generic(x, int:x, default:0)  /* ok: at most one expansion of x per association
                                                    * ('Inconsistent Macro Parameter Evaluation in C Generic'
                                                    *   warning issued when enabled)
                                                    */

Relevant Configuration File Parameters

The following configuration file parameters affect checks for this warning class.