主要内容

本页翻译不是最新的。点击此处可查看最新英文版本。

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 实现

如果使用函数标识符时没有满足以下任一条件,此规则检查项会报告违规:

  • 前面有 &

  • 后面跟着带括号的参数列表

故障排除

如果您预计存在违规,但未看到该违规,请参阅Diagnose Why Coding Standard Violations Do Not Appear as Expected

示例

全部展开

在此示例中,函数标识符 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.