From mboxrd@z Thu Jan 1 00:00:00 1970 From: Michael Meissner To: Andrew Morton Cc: gcc Subject: Re: comparisons.. Date: Wed, 12 Jul 2000 23:35:00 -0000 Message-id: <20000713023538.08807@cse.cygnus.com> References: <396D3B72.8A9426EA@uow.edu.au> X-SW-Source: 2000-07/msg00381.html On Thu, Jul 13, 2000 at 03:45:54AM +0000, Andrew Morton wrote: > unsigned long x; > > int y() > { > return (x < 0); > } > > This is usually a bug. Is there a way of getting gcc to warn about it? The friendly manual says: `-W' Print extra warning messages for these events: * A nonvolatile automatic variable might be changed by a call to `longjmp'. These warnings as well are possible only in optimizing compilation. The compiler sees only the calls to `setjmp'. It cannot know where `longjmp' will be called; in fact, a signal handler could call it at any point in the code. As a result, you may get a warning even when there is in fact no problem because `longjmp' cannot in fact be called at the place which would cause a problem. * A function can return either with or without a value. (Falling off the end of the function body is considered returning without a value.) For example, this function would evoke such a warning: foo (a) { if (a > 0) return a; } * An expression-statement or the left-hand side of a comma expression contains no side effects. To suppress the warning, cast the unused expression to void. For example, an expression such as `x[i,j]' will cause a warning, but `x[(void)i,j]' will not. * An unsigned value is compared against zero with `<' or `<='. * A comparison like `x<=y<=z' appears; this is equivalent to `(x<=y ? 1 : 0) <= z', which is a different interpretation from that of ordinary mathematical notation. * Storage-class specifiers like `static' are not the first things in a declaration. According to the C Standard, this usage is obsolescent. * If `-Wall' or `-Wunused' is also specified, warn about unused arguments. * A comparison between signed and unsigned values could produce an incorrect result when the signed value is converted to unsigned. (But don't warn if `-Wno-sign-compare' is also specified.) * An aggregate has a partly bracketed initializer. For example, the following code would evoke such a warning, because braces are missing around the initializer for `x.h': struct s { int f, g; }; struct t { struct s h; int i; }; struct t x = { 1, 2, 3 }; * An aggregate has an initializer which does not initialize all members. For example, the following code would cause such a warning, because `x.h' would be implicitly initialized to zero: struct s { int f, g, h; }; struct s x = { 3, 4 }; --> gcc -W -S foo.c foo.c: In function `y': foo.c:5: warning: comparison of unsigned expression < 0 is always false -- Michael Meissner, Red Hat, Inc. PMB 198, 174 Littleton Road #3, Westford, Massachusetts 01886, USA Work: meissner@redhat.com phone: +1 978-486-9304 Non-work: meissner@spectacle-pond.org fax: +1 978-692-4482