简体   繁体   English

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

[英]MISRA C 2012 - Rule 21.1 - Macros starting with underscore

Rule 21.1 in MISRA C 2012 states that MISRA C 2012中的规则21.1规定:

#define and #undef shall not be used on a reserved identifier or reserved macro name #define和#undef不得用于保留的标识符或保留的宏名称

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 删除或更改保留宏的含义可能会导致不确定的行为

I don't understand why macro's name shall not start with an unerscore, even if it is not a reserved macro? 我不明白为什么宏名称不是以保留号开头,即使它不是保留宏也是如此? For example in my header files: 例如在我的头文件中:

#ifndef __MY_HEADER_
#define __MY_HEADER_

or in a library I'm using: 或在我正在使用的图书馆中:

#define __I volatile const

Should I change all my code and the library I'm using (which is a big library) in order to conform to this rule or is there a simpler solution? 我是否应该更改所有代码和正在使用的库(这是一个大库)以符合该规则,还是有一个更简单的解决方案?

According to C standard (section 7.1.3), all identifiers starting with _[A_Z] or __ are reserved. 根据C标准(第7.1.3节),保留以_[A_Z]__开头的所有标识符。 As they are reserved, common sense and rule 21 forbid you to modify (redefine or undefine) them (or create your own). 由于保留它们,常识和规则21禁止您修改(重新定义或取消定义)它们(或创建自己的)。

Thus, you should change your code to not using leading underscores even in include guards not to mention your macros. 因此,您应该将代码更改为使用前导下划线,即使在包含防护的情况下也不要使用宏。

Some further reading can be found eg here: Include guard conventions in C 可以找到一些进一步的阅读,例如,在这里: 在C中包含保护约定

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM