Search code examples
cmisra

MISRA C 2012 Rule 20.5 #undef should not be used


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 ?


Solution

  • 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.