主要内容

MISRA C:2012 Rule 8.3

All declarations of an object or function shall use the same names and type qualifiers

描述

All declarations of an object or function shall use the same names and type qualifiers 1 .

理由

在同一对象或函数的所有声明中使用一致的参数名称和参数类型有助于增强类型检查。这样,可以更轻松地检查是否所有声明中都使用了相同的函数接口。

例外情况:

  • 使用相同基本类型的兼容版本并不违反此规则。例如,您可以互换使用 signedintsigned int

  • 使用未命名的函数参数并不违反此规则。例如,互换使用 void foo(int a) 声明和 void foo(int) 声明。

Polyspace 实现

规则检查项会检测多个声明之间或声明与定义之间的参数名称或数据类型存在差异的情况。检查项会检查所有翻译单元中的声明,并标记出编译器不太可能检测到的问题。

故障排除

如果您预期会出现违规,但未看到该违规,请参阅诊断为何编码规范违规未按预期显示

示例

全部展开

extern int div (int num, int den);

int div(int den, int num) { /* Non compliant */
    return(num/den);
}

在此示例中,由于声明和定义中的参数名称发生了对调,因此违反了规则。

typedef unsigned short width;
typedef unsigned short height;
typedef unsigned int area;

extern area calculate(width w, height h);

area calculate(width w, width h) { /* Non compliant */
    return w*h;
}

在此示例中,由于 calculate 函数的第二个参量为以下数据类型,因此违反了规则:

  • 在声明中为 height

  • 在定义中为 width

即使 heightwidth 的基础类型相同,仍然违反了规则。

检查信息

组:声明和定义
类别:必需
AGC 类别:必需
PQL 名称:std.misra_c_2012.R8_3

版本历史记录

在 R2014b 中推出

全部展开


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.