メインコンテンツ

AUTOSAR C++14 Rule A2-10-5

An identifier name of a function with static storage duration or a non-member object with external or internal linkage should not be reused

説明

ルール定義

An identifier name of a function with static storage duration or a non-member object with external or internal linkage should not be reused.

根拠

静的ストレージ存続期間が設定されたオブジェクトは、プログラムが実行されている間は使用可能なままになります。これらには次のものが含まれます。

  • プロジェクトの翻訳単位のいずれかから参照可能な外部リンケージを持つ非メンバー オブジェクト。

  • static クラス指定子を使用して宣言されたオブジェクト。これらのオブジェクトは、内部リンケージを持ち、翻訳単位内の任意のスコープから参照できます。

識別子の名前を再利用すると、元の識別子と区別できなくなる可能性があります。

このルールは、関数ローカルな静的オブジェクトのような、リンケージのないオブジェクトには適用されません。このようなオブジェクトの識別子をスコープの外部から参照できないためです。

Polyspace 実装

  • 識別子を再利用すると、Polyspace® が、同じ翻訳単位内にある識別子の最後の使用にフラグを設定します。識別子が異なるファイルに入っている場合は、アルファベット順で最後のファイル パス内の識別子にフラグが設定されます。

  • static クラス指定子を使用して名前空間で関数を宣言し、その関数識別子を再利用して別の名前空間で非静的関数を宣言した場合は、Polyspace が静的関数の識別子にフラグを設定します。たとえば、このコード スニペットでは、識別子 func が名前空間 NS_2 で再利用されますが、名前空間 NS_1 でフラグが設定されます。

    namespace NS_1 {
        static void func(void); // Polyspace flags this use of "func".
    };
    
    namespace NS_2 {
        void func(void); //"func" identifier reused but this is not a static function.
    }
    
    

  • Polyspace は、ローカル変数の識別子が再利用された場合に、グローバル変数の識別子にフラグを設定します。

  • Polyspace は、static クラス指定子を使用せずに宣言されたグローバル関数の識別子とその引数の再利用にはフラグを設定しません。

このチェッカーは、次のような未使用のコードでは起動されません。

  • インスタンス化されていないテンプレート

  • 呼び出されない関数 static または extern

  • 呼び出されない未定義のローカル関数

  • 未使用の型および変数

トラブルシューティング

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

すべて展開する

file1.cpp


#include <cstdint>

namespace first_namespace
{
	static std::int32_t global_var = 0; //Noncompliant - identifier reused

}
static std::int32_t file_var = 10; //Compliant - identifier not reused

file2.cpp

;

#include <cstdint>

namespace first_namespace
{
	static std::int32_t global_var = 0;  // identifier reused
	static std::int16_t module_var = 20; // Compliant - identifier not reused
}




namespace second_namespace
{

	void globalfunc(int argument) // non-static global function and arguments do not raise violation
	{
		int local_var; // local variable
		static std::int16_t local_static; // Object with no linkage
	}
	std::int16_t globalvar_reusedinlocal; 
	std::int16_t globalvar_notreused; // Compliant, identifier not reused
	void foo(){
		++globalvar_reusedinlocal;
		++globalvar_notreused;
	}
};

namespace third_namespace
{

	void globalfunc(int argument) // non-static global function and arguments do not raise violation
	{
		static std::int16_t local_static; // Object with no linkage
		int local_var; // local variable
		int globalvar_reusedinlocal; // Non-compliant, identifier reused in local variable
		++globalvar_reusedinlocal;
	}

};

この例では、global_var が、ソース ファイル file1.cpp 内の static クラス指定子を使用して宣言されます。この識別子は、ソース ファイル file2.cpp で再利用されます。同じファイルで、globalvar_reusedinlocalsecond_namespace で宣言され、外部リンケージを持ちます。識別子が globalfunc 内のローカル変数に再利用されているため、この宣言は非準拠です。

チェック情報

グループ: 構文規則
カテゴリ: Advisory、Automated

バージョン履歴

R2020b で導入