为什么MISRA规则禁止使用'#undef'? [英] Why do the MISRA rules prohibit the use of '#undef'?

查看:117
本文介绍了为什么MISRA规则禁止使用'#undef'?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么MISRA规则禁止在程序中使用#undef?如果我想限制任何宏的范围,如何在不使用#undef的情况下做到这一点?

Why do the MISRA rules prohibit the use of #undef in a program? If I want to limit the scope of any macro, how to do it without using #undef?

推荐答案

基本上,由于MISRA过于偏执并且不信任程序员不知道自己在做什么:-)认真地说,MISRA试图防止某些错误并受到指导相信如果您禁止潜在的有问题的代码构造,软件的可靠性就会突然提高.这是否成立尚有争议.在#undef的情况下,可能的原因是,一旦定义了宏,它的扩展将保持不变,并且始终是从其定义开始的.如果允许#undef,则标识符可以重用为变量,函数名称,typedef或struct/union成员,甚至可以用作具有不同扩展名的宏.如果可以的话,这是防止标识符遮挡的某种方法.吓人,不是吗?您决定!

Basically, because MISRA is too paranoid and doesn't trust programmers to know what they are doing :-) Seriously, MISRA tries to prevent certain errors and is guided by the belief that if you forbid potentially problematic code constructs, reliability of software suddenly increases. Whether that is true is a matter of debate. In the case of #undef, the likely reason is that once a macro is defined, its expansion stays put and is always that from its definiton. If you allow #undef, the identifier could be reused as a variable, function name, typedef or struct/union member, or even as a macro with a, gasp, different expansion. It's some way to prevent identifier shadowing, if you will. Scary, isn't it?! You decide!

要回答第二个问题,如果不能使用#undef,则限制宏范围的唯一方法是使用文件结尾,这是结束范围的唯一标准定义方式宏换句话说,您必须将源文件拆分为较小的文件,并仅在需要编译特定文件时定义宏.

To answer your second question, the only way to limit the scope of a macro if #undef can't be used is to use the end-of-file, which is the only other standard defined way to end the scope of a macro. In other words, you have to split up your source files into smaller ones and define the macro only when compiling the particular file where it is needed.

这篇关于为什么MISRA规则禁止使用'#undef'?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆