主要内容

MISRA C:2023 Rule 21.1

#define and #undef shall not be used on a reserved identifier or reserved macro name

自 R2024a 起

描述

规则定义

#define and #undef shall not be used on a reserved identifier or reserved macro name 1 .

理由

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

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

  • C 标准库中描述的文件范围中的标识符

  • C 标准库中描述为在标准头文件中定义的宏名称

规则检查项可以根据分析中使用的 C 标准版本标记不同的标识符或宏。请参阅 C 标准版本 (-c-version)。例如,如果您运行 C99 分析,请参阅 ISO®/IEC 9899:1999 标准第 7 节“库”中定义的保留标识符和宏。

故障排除

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

示例

全部展开

#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   */

检查信息

组:标准库
类别:必需
AGC 类别:必需

版本历史记录

在 R2024a 中推出


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.