Say that we have the following for-loop:
#define UPPER 0U
int i;
for(i = 0; i < UPPER; i++) {
/* foo */
}
This will produce a W549: condition is always true
warning, obviously because we get for(i = 0; i < 0; i++)
after macro expansion. In the actual code, UPPER
is a pre-compile time parameter (i.e., it is set by some build scripts depending on the target platform etc.) that can take any value from 0 to 255 and thus the loop is not just dead code.
How can I elegantly avoid this warning when UPPER == 0
?
Obviously, one can wrap the for-loop in an if-statement:
#define UPPER 0U
if(UPPER != 0U) {
int i;
for(i = 0; i < UPPER; i++) {
/* foo */
}
}
But that's not what I'd call elegant.
If you don't want to wrap it in code, wrap the code using conditional compilation:
#if UPPER > 0
int i;
for(i = 0; i < UPPER; i++) {
/* foo */
}
#endif
The elegance stems from:
UPPER
is 0.