Basically, since MISRA is too paranoid and does not trust programmers to know what they are doing :-) Seriously, MISRA tries to prevent certain errors and is guided by the belief that if you prohibit potentially problematic code constructs, the reliability of the software suddenly increases. Is this a matter of debate? In the case of #undef
probable reason is that, once a macro is defined, its extension remains positive and always matches its definition. If you enable #undef
, the identifier can be reused as a variable, function name, typedef or member of a structure / union, or even as a macro with a, gasp, a different extension. This is a way to prevent identifier shadowing if you like. Scary, isn't it ?! You decide!
To answer the second question, the only way to limit the scope of the macro if #undef
cannot be used is to use the end of the file, which is the only other standard way that allows you to complete the scope of the macro. In other words, you need to split the source files into smaller ones and define the macro only when compiling a specific file where necessary.
source share