Main Content

MISRA C:2012 Rule 23.6

The controlling expression of a generic selection shall have an essential type that matches its standard type

Since R2024a

Description

Rule Definition

The controlling expression of a generic selection shall have an essential type that matches its standard type.

This rule comes from MISRA C™: 2012 Amendment 3.

Rationale

When you develop your code under the MISRA guidelines, it must follow the essential type system. If the controlling expression of a generic selection has standard type that is different than its essential type, then the generic selection violates the essential type system.

As an exception, this rule does not apply to integer constant expression that satisfy both these conditions:

  • The integer constant expression has an essentially signed or unsigned type of lower rank than int.

  • It is neither character constant nor Boolean.

Polyspace Implementation

Polyspace® reports a violation of this rule if the essential type of an expression is different from its standard type. Integer constants that are of lower rank than int and are neither char nor bool are not a violation of this rule.

Troubleshooting

If you expect a rule violation but do not see it, refer to Diagnose Why Coding Standard Violations Do Not Appear as Expected.

Examples

expand all

In this example, the generic selection dispatcher() selects a function based on the type of the controlling expression. Polyspace reports a defect when the essential types and the standard types do not match.

#include <stdint.h>
#include <stdbool.h>

void handle_ss(signed short);
void handle_us(unsigned short);
void handle_si(signed int);
void handle_ui(unsigned int);

typedef unsigned char uchar;
typedef int int32_t;

#define dispatcher(X) ( _Generic((X) \
                                 , signed short: handle_ss \
                                 , unsigned short: handle_ss \
                                 , signed int: handle_si \
                                 , unsigned int: handle_us \
                                 , default: handle_si) (X) )

enum polyspace { b, u, g, f, i, n, d, e, r };
void NonCompliant() {
	char c1;
	signed char sc1, sc2;
	unsigned char uc1, uc2;
	signed short ss1, ss2;
	unsigned short us1, us2;

	enum polyspace p1;

	uchar u1, u2;

	dispatcher(sc1 + sc2);               /* Noncompliant */
	dispatcher(uc1 + uc2);               /* Noncompliant */

	dispatcher(ss1 + ss2);               /* Noncompliant */
	dispatcher(us1 + us2);               /* Noncompliant */

	dispatcher(sc1 + ss1);               /* Noncompliant */

	dispatcher('@');                     /* Noncompliant */


	// Enums
	dispatcher(p1);                      /* Noncompliant */

	//Exception
	dispatcher(10u);                     /* Compliant */
	dispatcher(250 + 350);               /* compliant */
}

  • The expression (sc1 + sc2) has essential type signed char but standard type signed int.

  • The expression (us1 + us2) has essential type unsigned short but standard type signed int.

  • The enumeration p1 has essential type enum but standard type signed int.

  • The expression (sc1 + ss1) has essential type signed short but standard type signed int.

  • The expression (ss1 + ss2) has essential type signed short but standard type signed int.

  • The expression '@' has essential type signed char but standard type signed int.

Check Information

Group: Generic Selections
Category: Required
AGC Category: Required

Version History

Introduced in R2024a