メインコンテンツ

MISRA C:2012 Rule 23.6

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

R2024a 以降

説明

ルール定義

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

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

根拠

MISRA ガイドラインに沿ってコードを開発するときには、実質的な型のシステムに従う必要があります。総称選択の制御式に、実質的な型とは異なる標準の型が含まれている場合、総称選択は実質的な型のシステムに違反します。

例外として、次の両方の条件を満たす整数定数式にはこのルールは適用されません。

  • 整数定数式が、int よりも下位のランクの実質的な符号付き型または符号なし型を持つ。

  • 文字定数と boolean のいずれでもない。

Polyspace 実装

式の実質的な型がその標準の型と異なる場合、Polyspace® はこのルールの違反を報告します。int よりも下位のランクで、charbool のいずれでもない整数定数は、このルールに違反しません。

トラブルシューティング

ルール違反を想定していてもその違反が表示されない場合、コーディング規約違反が想定どおりに表示されない理由の診断を参照します。

すべて展開する

この例では、総称選択 dispatcher() が制御式の型に基づいて関数を選択します。実質的な型と標準の型が一致しない場合、Polyspace は欠陥を報告します。


#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 */
}

  • (sc1 + sc2) の実質的な型は signed char ですが、標準の型は signed int です。

  • (us1 + us2) の実質的な型は unsigned short ですが、標準の型は signed int です。

  • 列挙値 p1 の実質的な型は enum ですが、標準の型は signed int です。

  • (sc1 + ss1) の実質的な型は signed short ですが、標準の型は signed int です。

  • (ss1 + ss2) の実質的な型は signed short ですが、標準の型は signed int です。

  • '@' の実質的な型は signed char ですが、標準の型は signed int です。

チェック情報

グループ: 総称選択
カテゴリ: 必要
AGC カテゴリ: 必要

バージョン履歴

R2024a で導入


1 All MISRA coding rules and directives are © Copyright The MISRA Consortium Limited 2021.

The MISRA coding standards referenced in the Polyspace Bug Finder™ documentation are from the following MISRA standards:

  • MISRA C:2004

  • MISRA C:2012

  • MISRA C:2023

  • MISRA C++:2008

  • MISRA C++:2023

MISRA and MISRA C are registered trademarks of The MISRA Consortium Limited 2021.