Misra规则19.7:功能类似于宏 [英] Misra rule 19.7 : function like macro

查看:177
本文介绍了Misra规则19.7:功能类似于宏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Misra规则19.7发出警告:在下一行中,应优先使用函数而不是类似函数的宏:

I have a warning regarding Misra rule 19.7 : A function should be used in preference to a function-like macro in the below line :

#define gOFFSETOF(type, mem) (gOFFSET)((size_t) ((char *)&((type *) 0)->mem - (char *)((type *) 0)))

我应该如何解决呢?

推荐答案

规则19.7(建议):应优先使用函数而不是类似函数的宏. 尽管宏可以提供比函数更快的速度,但是函数提供了更安全,更健壮的机制.对于参数的类型检查,以及类似函数的宏可能会多次评估参数的问题,尤其如此.

Rule 19.7 (advisory): A function should be used in preference to a function-like macro. While macros can provide a speed advantage over functions, functions provide a safer and more robust mechanism. This is particularly true with respect to the type checking of parameters, and the problem of function-like macros potentially evaluating parameters multiple times.

该规则是建议性的,因此这意味着通常应遵循":

The rule is advisory, so this means it "should normally be followed":

请注意,建议"的状态并不意味着可以忽略这些项目,而应在合理可行的范围内予以遵循.对于咨询规则而言,形式上的偏差不是必需的,但如果认为适当,则可能会引起形式上的偏差.

Note that the status of "advisory" does not mean that these items can be ignored, but that they should be followed as far as is reasonably practical. Formal deviations are not necessary for advisory rules, but may be raised if it is considered appropriate.

因此,您可以选择不作任何规定就违反规则.

So you have the option to break the rule without making a formal deviation.

现在,在回答您的问题我该如何解决?"的情况下,鉴于无法将此宏函数实现为函数,您有两种选择.

Now, in answer to your question, "how should I solve this?", you have two choices given that this macro function can't be implemented as a function.

该通报清楚地表明,函数比宏函数要好,并引用了 C陷阱和陷阱,Andrew Koenig(1988)中有关将宏函数与函数进行比较的部分,但将其描述为偏好,尤其是针对速度优势"而设计的短宏功能.

The advisory makes it clear that functions are nicer than macro functions, and cites the section in C Traps and Pitfalls, Andrew Koenig (1988) on comparing macro functions with functions, but describes it as a preference, and specifically over the short macro functions designed for "speed advantage".

如果您相信此宏可以使代码更简洁明了,并且适当地避免了宏函数的常见陷阱,那么您可以不遵守规则19.7,而无需进行正式的特定偏离,也不会违反法规.

If you believe this macro makes the code clearer, conciser, and you have suitably avoided the common pitfalls of macro functions, then you can deviate from rule 19.7 without making a formal specific deviation, and without failing compliance.

如果在您考虑了建议之后认为适当,则删除宏功能.您可能想编写更多函数来分解多余的内联代码,和/或避免不必要的代码重复.

If after considering the advisory you deem it appropriate then remove the macro function. You may want to write more functions to break up the extra inline code, and/or avoid needless code duplication.

这篇关于Misra规则19.7:功能类似于宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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