主要内容

MISRA C:2012 Dir 4.5

Identifiers in the same name space with overlapping visibility should be typographically unambiguous

描述

指令定义

Identifiers in the same name space with overlapping visibility should be typographically unambiguous 1 .

理由

“明确易辨”的含义取决于编写源代码时所用的字母表和语言。当您使用在排字上很接近的标识符时,您可能会产生混淆。

对于英语单词中使用的拉丁字母,至少要避免标识符之间只有以下区别:

  • 小写字母与其对应的大写字母互换。

  • 是否存在下划线字符。

  • 字母 O 与数字 0 互换。

  • 字母 I 与数字 1 互换。

  • 字母 I 与字母 l 互换。

  • 字母 S 与数字 5 互换。

  • 字母 Z 与数字 2 互换。

  • 字母 n 与字母 h 互换。

  • 字母 B 与数字 8 互换。

  • 字母 rn 与字母 m 互换。

Polyspace 实现

该检查项会标记在同一作用域内且仅存在上述字符差异的标识符。

此外,该检查项还遵循以下规则:

  • 检查项会忽略以 doforswitchwhile 开头的宏中定义的标识符。

  • 如果两个标识符的作用域不同,它们的使用就不会产生歧义。对于作用域不同的标识符,检查项不会报告违反此规则。

  • 检查项会单独检查每个转换单元(即每个源文件及其包含的头文件),然后再合并检查结果,以确保给定位置的每一项违规仅报告一次。

    • 如果转换单元同时包含声明和定义,则违规与定义相关联。

    • 如果转换单元仅包含声明,则违规与声明相关联。

    此规则有一个副作用,那就是如果一个头文件包含两个互相冲突的声明,且仅在包含相应定义的转换单元中包含该头文件,那么您只会看到定义处的违规。如果此头文件包含在其他转换单元中,那么您会在声明和定义处都看到违规。

故障排除

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

示例

全部展开

void func(void) {
	int id1_numval;
	int id1_num_val;  /* Non-compliant */

	int id2_numval;
	int id2_numVal;   /* Non-compliant */

	int id3_lvalue;
	int id3_Ivalue;   /* Non-compliant */

	int id4_xyZ;
	int id4_xy2;      /* Non-compliant */

	int id5_zerO;
	int id5_zer0;     /* Non-compliant */

	int id6_rn;
	int id6_m;        /* Non-compliant */

	int numVal;
}

void foo() {
	int numval;
}

在此示例中,当使用可能会相互混淆的标识符时,便会违反规则。在不同作用域中声明的类似标识符不会被报告为违反此规则。

检查信息

组:代码设计
类别:建议
AGC 类别:可读性

版本历史记录

在 R2015b 中推出


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.