Search code examples
cmacrosc-preprocessorconditional-compilationiar

Force macro definition before #if directive


I was surprised to learn that undefined macros are automatically assigned with the value of 0 when used in a preprocessor expression. For example:

//#define A
#if A == 0
// do something
#endif

This code will compile in the "do something" part, while I expected it to give an error. I found out that GCC has an option to warn against this case:

  • Identifiers that are not macros, which are all considered to be the number zero. This allows you to write #if MACRO instead of #ifdef MACRO, if you know that MACRO, when defined, will always have a nonzero value. Function-like macros used without their function call parentheses are also treated as zero.

    In some contexts this shortcut is undesirable. The -Wundef option causes GCC to warn whenever it encounters an identifier which is not a macro in an #if.

So, I am looking for the equivalent option to -Wundef for the IAR Embedded Workbench IDE. Is there such an option?


Solution

  • The IAR C/C++ compiler in IAR Embedded Workbench can detect this. However, by default it's configured as a remark, which isn't shown.

    You can either use the command line option --remarks, or raise the severity if it to a warning or error using --diag_warning=Pe193or --diag_error=Pe193.