メインコンテンツ

このページの内容は最新ではありません。最新版の英語を参照するには、ここをクリックします。

MISRA C++:2008 Rule 9-6-4

Named bit-fields with signed integer type shall have a length of more than one bit

説明

ルール定義

Named bit-fields with signed integer type shall have a length of more than one bit. 1

根拠

長さ 1 の符号付き整数ビットフィールド型の変数は、開発者の期待に沿わない値を保持する可能性があります。たとえば、std::int16_t (cstdint から) などの固定幅の符号付き整数型は 2 の補数表現をもちます。この表現では、単一ビットが単なる符号ビットで、値が 0 または -1 です。

Polyspace 実装

チェッカーは、長さが 1 に等しい符号付き整数ビットフィールド型の名前付き変数の宣言にフラグを設定します。

長さ 0 のビットフィールド型にはフラグは設定されません。

トラブルシューティング

ルール違反が想定されるものの、Polyspace® から報告されない場合は、コーディング規約違反が想定どおりに表示されない理由の診断を参照してください。

すべて展開する

#include <cstdint>

typedef struct
{
   std::uint16_t IOFlag :1;    //Compliant - unsigned type
   std::int16_t InterruptFlag :1; //Noncompliant
   std::int16_t Register1Flag :2; //Compliant - Length more than one bit
   std::int16_t : 1; //Compliant - Unnamed
   std::int16_t : 0; //Compliant - Unnamed
   std::uint16_t SetupFlag :1; //Compliant - unsigned type
} InterruptConfigbits_t;

この例では、2 番目のビットフィールド宣言のみが非準拠です。名前付き変数が 1 ビット長符号付き型で宣言されています。

チェック情報

グループ: Classes
カテゴリ: 必要

バージョン履歴

R2013b で導入


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.