This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Warning for non-portable integer operations?
- From: Mark Dickinson <dickinsm at gmail dot com>
- To: gcc-help at gcc dot gnu dot org
- Date: Sat, 9 May 2009 19:43:39 +0100
- Subject: Warning for non-portable integer operations?
Is there any gcc warning available that would
warn about the following C code, which gives
different results depending on the platform:
#include <stdio.h>
int main(void) {
unsigned int x = 2;
long y = -3;
if (x + y < 10)
printf("Foo\n");
else
printf("Bar\n");
return 0;
}
On OS X 10.5/Core 2 Duo, I get the following
results. Here gcc-mp-4.4 is gcc version 4.4.0.
Macintosh-4:~ dickinsm$ gcc-mp-4.4 -Wall -Wextra test.c && ./a.out
Bar
Macintosh-4:~ dickinsm$ gcc-mp-4.4 -m64 -Wall -Wextra test.c && ./a.out
Foo
There are no warnings produced in either case.
This is all perfectly legitimate, of course: the
behaviour stems from the last couple of clauses
of C99 6.3.1.8 paragraph 1 (and the corresponding
bit of C89), on the 'usual arithmetic conversions' .
Those clauses imply that the type of x+y above depends
on whether sizeof(int) < sizeof(long) or not. If
sizeof(int) == sizeof(long) then x+y has type unsigned
long, otherwise it has type long:
It seems to me that this is a potential source of
bugs, and that a careful programmer who's worried
about portability might want to know when these two
clauses are being invoked to determine the type of
the result of an arithmetic operation.
So does such a warning already exist?
Mark