メインコンテンツ

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

MISRA C++:2008 Rule 16-0-1

#include directives in a file shall only be preceded by other preprocessor directives or comments.

説明

ルール定義

#include directives in a file shall only be preceded by other preprocessor directives or comments. 1

根拠

すべての #include プリプロセッサ命令をソース ファイルの先頭でグループにまとめると、コードが読みやすくなります。#include 命令によって、マクロが定義されているヘッダー ファイルをインクルードすることがあります。マクロの定義をインクルードする前にマクロを使用すると、予期せぬコードの動作が発生する可能性があります。

Polyspace 実装

Polyspace® は、#include 命令がコメントまたはプリプロセッサ命令以外のコードの後に配置されている場合に、この欠陥を報告します。Polyspace は、#if または #ifdef などの条件付きコンパイル命令を使用して非表示にされるコードを無視します。Polyspace は、#include 命令が extern "C" ブロック内に配置されている場合には、このルールに対する違反を報告しません。

トラブルシューティング

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

すべて展開する

//this comment is compliant     //Compliant
/*
    This comment is compliant
*/

#ifndef TESTING_H               //Compliant
#define TESTING_H               //Compliant

#include <iostream>             //Compliant	
using namespace std;            //Compliant
#include <exception>            //Noncompliant

#endif

プリプロセッサ命令でもコメントでもないコード ステートメントの後にインクルード命令が配置されているために、Polyspace はこのインクルード命令にフラグを設定します。

チェック情報

グループ: Preprocessing Directives
カテゴリ: 必要

バージョン履歴

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.