メインコンテンツ

MISRA C:2023 Rule 17.12

A function identifier should only be used with either a preceding &, or with a parenthesized parameter list

R2024a 以降

説明

ルール定義

A function identifier should only be used with either a preceding &, or with a parenthesized parameter list 1 .

根拠

関数識別子を、その前に & を付けずに、あるいはその後に小かっこで囲まれたパラメーター リストを指定せずに使用すると、混乱が生じます。このような場合、目的が関数の呼び出しなのか、関数のアドレスの取得なのかが明確ではありません。

Polyspace 実装

以下のいずれも指定せずに関数識別子を使用した場合、ルール チェッカーは違反を報告します。

  • 先行する &

  • 後に続く小かっこで囲まれたパラメーター リスト

トラブルシューティング

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

すべて展開する

この例で使用されている関数識別子 func には、先行する & も、後に続く小かっこ一式もありません。ルール チェッカーは、こうしたあいまいな使用に対して違反を報告します。

#include <stdint.h>

typedef int32_t (*func_ptr_i)(void);
extern int32_t func1(void);        /* Note: A function */
extern int32_t (*func2)(void);     /* Note: A function pointer */
void func(void)
{
	func_ptr_i pfn1 = &func1;       /*Compliant*/
	func_ptr_i pfn2 = func1;        /*Noncompliant*/
	pfn1();                    /*Compliant*/

	if(func1 == func2)         /*Noncompliant*/
	{
		/* ... */
	}
	if(func1() == func2())           /*Compliant*/
	{
		/* ... */
	}

}

チェック情報

グループ: 関数
カテゴリ: 推奨
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.