c - MISRA C 2012 - 规则 21.1 - 以下划线开头的宏

标签 c lint misra

MISRA C 2012 中的规则 21.1 指出

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

This rule applies to identifier or macro beginning with an underscore

Rationale:

Removing or changing the meaning of a reserved macro may result in undefined bahaviour

我不明白为什么宏的名称不能以 unerscore 开头,即使它不是保留宏?例如在我的头文件中:

#ifndef __MY_HEADER_
#define __MY_HEADER_

或者在我正在使用的库中:

#define __I volatile const

我应该更改我所有的代码和我正在使用的库(这是一个大库)以符合此规则还是有更简单的解决方案?

最佳答案

根据 C 标准(第 7.1.3 节),所有_[A_Z]__ 开头的标识符都是保留的。由于它们是保留的,常识和规则 21 禁止您修改(重新定义或取消定义)它们(或创建您自己的)。

因此,您应该将您的代码更改为使用前导下划线,即使在 include guards 更不用说您的宏了。

可以找到一些进一步的阅读,例如这里:Include guard conventions in C

关于c - MISRA C 2012 - 规则 21.1 - 以下划线开头的宏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50696794/

相关文章:

C语言枚举数据类型?

c - C 的污点分析

c - 在 c 的 if 语句中声明变量是个坏习惯吗?

c - 在 C 中创建时初始化数组的指向地址

c - 如何使用 resolve MISRA C error for assembly language inline function?

c - 静态变量在不应该的时候出现在 Doxygen 文档中

c - 为函数中结构内部的指针分配内存

c - 进程运行顺利,没有触及 "gets"函数,不知道为什么

xcode - OCLint 生成空的compile_commands.json

facebook - 错误 Linting URL : An internal error occurred while linting the URL