Wtype-limits and functional range checks

Jan Smets jan.smets@alcatel-lucent.com
Mon Jul 23 10:35:00 GMT 2012


Hi

We have a lot of typicalrange checks in our code that are 'incompatible' 
with GCC's -Wtype-limits.
(We've just upgraded from 3.4 to 4.6 and some 'type-limit' checks were 
enabled by default in GCC 3, but have become too noisy in GCC4)

Take this example :

#define MIN 0
#define MAX 100

typedef unsigned short tSomeType;

int doSomething(tSomeType value)
{
        if ((value < tSomeType_MIN) || (value > tSomeType_MAX)) {
            return 1;
        }
        return 0;
}

Would give: warning: comparison is always false due to limited range of 
data type [-Wtype-limits]
or, if you change the typedef short to int: warning: comparison of 
unsigned expression < 0 is always false [-Wtype-limits]

This is 100% valid... but it's not very practical. The reason the range 
check is there because MIN and MAX can change flexible somewhere in the 
future.

I'd really like to use this warning because it can improve code quality 
a lot.
I tried to think of various ways to work around it (macro's , #pragma's) 
but I couldn't find any that satisfied me.

Is there a way to disable this warning for "range" checks (ie, when 
there is both a > and < compare of the same variable) ?

Can anyone help me hacking this in or at least give me some hints how to 
do this.
(e.g, how do I know if value is used twice in the if()?)  I had a look 
at c-family/c-common.c but I don't see how this can be done.

Any help is appreciated.

Thanks

  - Jan






More information about the Gcc-help mailing list