1

I am defining some macro - #define temporary inside code, which I have #undef after being used.

Is it not allowed to #undef MACRO?

void c_cmd(unsinged char *com)
{ 

int abc = com[0];
int res = 0;

switch(abc)
{
    case 1:
             #define ssCmd                 com[2]  /* SRT or STP*/
             res = abc + ssCmd;     
             /* part of some functionality */          

             #undef ssCmd 
             break;

    default:
             break;

}

}

Observed warning:

use of '#undef' is discouraged: 'ssCmd' [MISRA 2012 Rule 20.5, advisory]

Lundin
  • 174,148
  • 38
  • 234
  • 367
kapilddit
  • 1,675
  • 4
  • 25
  • 43
  • 1
    Simply read rule 20.5, it's self-explanatory. – Lundin Aug 27 '19 at 09:22
  • 2
    possible duplicate of [Why do the MISRA rules prohibit the use of '#undef'?](https://stackoverflow.com/questions/11665031/why-do-the-misra-rules-prohibit-the-use-of-undef) – P.W Aug 27 '19 at 09:23
  • 1
    @P.W Nah that post is outdated since MISRA-C:2012 had not been released. The rule about `#undef` was relaxed in 2012. – Lundin Aug 27 '19 at 09:24
  • related question - https://stackoverflow.com/questions/47769783/misra-c-2012-rule-20-5-undef-should-not-be-used – kapilddit Aug 27 '19 at 09:51
  • Use int ssCmd = com[2]; instead, and enclose the whole thing in {} to limit the scope of the variable. – Lars Ljung Aug 27 '19 at 16:59

1 Answers1

2

Yes, MISRA Rule 20.5 says that #undef should not be used. It's an advisory rule.

#undef should not normally be needed. Its use can lead to confusion with respect to the existence or meaning of a macro when it is used in the code.

Mike
  • 3,738
  • 5
  • 18
  • 36