Reputation: 8115
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 thatMACRO
, 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?
Upvotes: 1
Views: 1076
Reputation: 26134
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=Pe193
or --diag_error=Pe193
.
Upvotes: 2
Reputation: 61575
You say cpp -WUndef
check is impractical at build time. Assuming your code is under SCM control, there's also commit time to the earliest quality-controlled repo. You could add a commit-hook for this check or extend an existing one.
If this would be an innovation for you, consider what other code-quality checks it would be nice to automate this way.
Upvotes: 1