主要内容

本页采用了机器翻译。点击此处可查看最新英文版本。

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)

版本历史记录

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