V2638. MISRA. Generic association should list an appropriate type.

This diagnostic rule is based on the MISRA (Motor Industry Software Reliability Association) software development guidelines.

This diagnostic rule is relevant only for C.

The _Generic construct (C11) used with certain types from the association list can lead to unexpected results.

Before a controlling expression is matched to the association list, it undergoes a chain of implicit conversions (lvalue conversion):

The C standard does not impose any restrictions on the types specified in the association list, therefore a certain branch may never be selected. So, the association list must not contain any of the following types:

The example:

#define builtin_typename(expr)                      \
  (_Generic( (expr)                                 \
     , char: "char"                                 \
     , const char: "const char"                     \
     , volatile char: "volatile char"               \
     , const volatile char: "const volatile char"   \
     , short: "short"                               \
     , const short: "const short"                   \
     , volatile short: "volatile short"             \
     , const volatile short: "const volatile short" \
     , ....) )

In the code, the builtin_typename macro is declared, converting the passed expression to a string literal that contains the expression type. To do this, all built-in types with combinations of const and volatile qualifiers are enumerated. However, the controlling expression will never be cv-qualified after implicit conversions. As a result, such a _Generic construct includes associations that will never be selected.

The fixed code:

#define builtin_typename(expr) \
  (_Generic( (expr) \
     , char: "char" \
     , short: "short" \
     , ....) )

This diagnostic is classified as:

  • MISRA-C-23.4