Having the following code (greatly simplified):
#define DO_SOME_STUFF(val,x,y) \
if (x>y) \
{ \
val = val >> (x-y); \
} else { \
val = val >> (y-x); \
}
An example of calling the macro with expansion will be:
int val = 5;
DO_SOME_STUFF(val,5,10);
=>
if (5>10)
{
// dead code
val = val >> (5-10); // negative shift warning here
} else {
// live code
val = val >> (10-5); // valid code
}
Now, since there is dead code, it will be removed by the compiler and everyone will be happy since I will never negative shift - so I'm doing legal stuff here.
Unfortunately, I receive the warnings before the code gets removed (or so it seems).
In the project I'm working the function will be called 100+ times, and the only solution I came up so far is to do the following:
#pragma GCC diagnostic ignored "-Wshift-count-negative"
DO_SOME_STUFF(val,300,20);
#pragma GCC diagnostic pop
This isn't very nice, since i will add a ton of these which will result in hard-to-read blue code, and I would rather avoid it if possible.
Is there an elegant way to remove the warning just for my macro expansion or to ask the compiler to ignore dead code ? (unfortunately i cannot add the #pragma options to my macro definition).