MISRA C 2012 Rule 20.5 #undef should not be used

自闭症网瘾萝莉.ら 提交于 2019-12-12 20:53:32

问题


I am trying to get rid of violation of Rule 20.5

Sample code:

#define VAL 2
int32_t func(void)
 {
    int32_t n1 = VAL;
    #undef VAL
    #define VAL(x) (x*x)
    return VAL(n1);
 }

Is there any work around for undef here without changing any other lines ?


回答1:


No, there is no work-around. The code is badly written, there is no justification for using the pre-processor like this. It is just obfuscation - get rid of it. Use plain variables instead.

There exists almost no scenario where the use of #undef is justified. The only valid case I can think of is "X macros", and even those should be used sparsely.



来源:https://stackoverflow.com/questions/47769783/misra-c-2012-rule-20-5-undef-should-not-be-used

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!