主要内容

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

MISRA C:2012 Rule 5.1

External identifiers shall be distinct

描述

默认 Polyspace® as You Code 分析中反激活了此检查项。请参阅Checkers Deactivated in Polyspace as You Code Analysis (Polyspace Access)

规则定义

External identifiers shall be distinct 1 .

理由

外部标识符是用全局作用域或存储类 extern 声明的标识符。

如果两个长名称几乎完全相同,差异仅出现在靠后的部分,则这两个名称很有可能被混用。这降低了代码的可读性。

Polyspace 实现

如果两个名称的前 31 个字符之间存在差异,则 Polyspace 会将它们视为不同的名称。对于 C90,前 6 个字符之间必须存在差异。要使用 C90 规则检查,请为 C 标准版本 (-c-version) 选项使用 c90 值。您可以使用 -code-behavior-specifications 选项更改比较的字符数。请参阅 -code-behavior-specifications

报告中的补充消息

文件 YY 中的外部 %s %s 与外部标识符 XX 冲突。

故障排除

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

示例

全部展开


int engine_temperature_raw;
int engine_temperature_scaled;   /* Noncompliant */					
int engin2_temperature;          /* Compliant */	

在此示例中,标识符 engine_temperature_scaled 与上一个标识符 engine_temperature_raw 具有相同的前六个字符。


int engine_exhaust_gas_temperature_raw;
int engine_exhaust_gas_temperature_scaled; /* Noncompliant */					

int eng_exhaust_gas_temp_raw;
int eng_exhaust_gas_temp_scaled;           /* Compliant */						

在此示例中,标识符 engine_exhaust_gas_temperature_scaled 与上一个标识符 engine_exhaust_gas_temperature_raw 的前 31 个字符相同。


/* file1.c */
int abc = 0; /* Noncompliant */

/* file2.c */
int ABC = 0; 

在此示例中,此实现在外部标识符中支持 6 个不区分大小写的重要字符。这两个编译单元中的标识符不同,但其重要字符并非截然不同。

检查信息

组:标识符
类别:必需
AGC 类别:必需

版本历史记录

在 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.