主要内容

CERT C:Rule DCL37-C

Do not declare or define a reserved identifier

描述

Do not declare or define a reserved identifier. 1

Polyspace 实现

规则检查项会检查以下问题:

  • 定义或取消定义保留标识符或宏

  • 声明保留标识符或宏名称

如果您的代码声明或定义了保留标识符:

  • 如果有显式定义,Polyspace® 会将其标记出来。

  • 如果没有显式定义,则当您的代码使用该符号时,编译器会进行隐式定义。Polyspace 会将这种使用方式标记出来。

每次使用不合规的标识符时,都会报告此违规。

示例

全部展开

问题

当您对保留标识符或宏名称使用 #define#undef 时,会出现此问题。

风险

保留标识符和保留宏名称是供实现使用的。删除或更改保留宏的含义可能会导致未定义行为。此规则适用于下列项:

  • 以下划线开头的标识符或宏名称

  • C 标准库 (ISO®/IEC 9899:1999, Section 7, "Library") 中描述的文件范围中的标识符

  • C 标准库 (ISO/IEC 9899:1999, Section 7, "Library") 中描述为在标准头文件中定义的宏名称。

示例 - 定义或取消定义保留标识符
#undef __LINE__               /* Non-compliant - begins with _ */
#define _Guard_H 1            /* Non-compliant - begins with _ */
#undef _ BUILTIN_sqrt        /* Non-compliant - implementation may
                               * use _BUILTIN_sqrt for other purposes,
                               * e.g. generating a sqrt instruction */
#define defined               /* Non-compliant - reserved identifier */
#define errno my_errno        /* Non-compliant - library identifier */
#define isneg(x) ( (x) < 0 )  /* Compliant - rule doesn't include  
                               * future library directions   */
问题

当声明了保留标识符或宏名称时,会出现此问题。

如果您定义与标准库宏、对象或函数对应的宏名称,则 Polyspace 会认为这违反了此规则。

该规则将暂定的定义也视为定义。

风险

此标准允许实现对保留标识符进行特殊处理。如果重复使用保留标识符,可能会导致未定义的行为。

检查信息

组:Rule 02. 声明和初始化 (DCL)
PQL 名称:std.cert.DCL37_C

版本历史记录

在 R2019a 中推出


1 This software has been created by MathWorks incorporating portions of: the “SEI CERT-C Website,” © 2017 Carnegie Mellon University, the SEI CERT-C++ Web site © 2017 Carnegie Mellon University, ”SEI CERT C Coding Standard – Rules for Developing safe, Reliable and Secure systems – 2016 Edition,” © 2016 Carnegie Mellon University, and “SEI CERT C++ Coding Standard – Rules for Developing safe, Reliable and Secure systems in C++ – 2016 Edition” © 2016 Carnegie Mellon University, with special permission from its Software Engineering Institute.

ANY MATERIAL OF CARNEGIE MELLON UNIVERSITY AND/OR ITS SOFTWARE ENGINEERING INSTITUTE CONTAINED HEREIN IS FURNISHED ON AN "AS-IS" BASIS. CARNEGIE MELLON UNIVERSITY MAKES NO WARRANTIES OF ANY KIND, EITHER EXPRESSED OR IMPLIED, AS TO ANY MATTER INCLUDING, BUT NOT LIMITED TO, WARRANTY OF FITNESS FOR PURPOSE OR MERCHANTABILITY, EXCLUSIVITY, OR RESULTS OBTAINED FROM USE OF THE MATERIAL. CARNEGIE MELLON UNIVERSITY DOES NOT MAKE ANY WARRANTY OF ANY KIND WITH RESPECT TO FREEDOM FROM PATENT, TRADEMARK, OR COPYRIGHT INFRINGEMENT.

This software and associated documentation has not been reviewed nor is it endorsed by Carnegie Mellon University or its Software Engineering Institute.