メインコンテンツ

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

MISRA C++:2023 Rule 19.1.2

All #else, #elif and #endif preprocessor directives shall reside in the same file as the #if, #ifdef or #ifndef directive to which they are related

R2024b 以降

説明

ルール定義

All #else, #elif and #endif preprocessor directives shall reside in the same file as the #if, #ifdef or #ifndef directive to which they are related. 1

根拠

条件付きでコードのブロックを含めたり除外したりするには、#if...#elif...#else...#endif などのプリプロセッサ命令を使用します。このような命令のさまざまな分岐が別々のソース ファイルに存在する場合、混乱を招くコードになる可能性があります。すべての分岐が 1 つのプロジェクトに含まれていないと、コードが予期せぬ方法で動作する可能性があります。混乱と予期せぬ動作を回避するには、条件付きプリプロセッサ命令のすべての分岐を同じソース ファイルで保持します。

Polyspace 実装

以下のいずれかの条件に該当する場合、Polyspace® はこのルールの違反を報告します。

  • #else#elif、または #endif 命令のそれぞれに対応する #if 命令がソース ファイル内に見つからない。

  • #if 命令のそれぞれに対応する #endif 命令がソース ファイル内に見つからない。

トラブルシューティング

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

すべて展開する

//file1.h
#if !defined (FILE)
//.....
#elif               // Noncompliant
//...///
//file2.h
#else              // Noncompliant
//...
#endif             // Noncompliant
///

この例では、条件付き命令が 2 つのソース ファイルに分割されています。

  • file1.h では、#if 命令に対応する #endif 命令がありません。Polyspace はこのブロックにフラグを設定します。

  • file2.h では、#else#endif 命令に対応する #if 命令がありません。Polyspace は両方の命令にフラグを設定します。

チェック情報

グループ: Preprocessing Directives
カテゴリ: Required

バージョン履歴

R2024b で導入


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.