proposed patch for constant signed/unsigned comparison warning
Paul Eggert
eggert@twinsun.com
Wed Sep 24 14:14:00 GMT 1997
Date: Wed, 24 Sep 97 08:35:32 EDT
From: kenner@vlsi1.ultra.nyu.edu (Richard Kenner)
Mixing signed and unsigned like that is a very bad idea and it's quite
proper for the compiler to warn about it.
It's true that mixing signed and unsigned is sometimes a mistake, but
sometimes it's not. Unfortunately, the latter happens often enough so
that these warnings are very often bogus in practice.
Here's an example of the problem. I just compiled GNU diffutils 2.7
using -Wall with testgcc-970912, and carefully analyzed the resulting
diagnostics. On this example, GCC emits 29 warnings about `comparison
between signed and unsigned' in the diffutils code proper -- and all
29 warnings are false alarms. The warned-about comparisons all
operate correctly, either because the signed quantity cannot possibly
be negative, or because the code is comparing an unsigned quantity to
-1 and it expects the -1 to be converted to unsigned as part of the
comparison.
I realize that the signed-vs-unsigned warning is useful in some
applications, but with a 100% false-alarm ratio on a real example,
I think GCC needs to be improved.
The GCC documentation says the following about -Wall:
The following `-W...' options are not implied by `-Wall'. Some of
them warn about constructions that users generally do not consider
questionable, but which occasionally you might wish to check for;
others warn about constructions that are necessary or hard to avoid in
some cases, and there is no simple way to modify the code to suppress
the warning.
and this suggests that the signed-vs-unsigned comparison warning,
as it's currently implemented, should not be implied by -Wall.
Linus Torvalds originally brought up this problem in May 1996 with
respect to the Linux kernel. I recall that rms showed some sympathy
for his complaint at the time. I also recall that kenner wrote that
he was thinking of making GCC smarter so that it didn't generate these
warnings when they were bogus. That would be nice, but it hasn't been
done yet and it's not trivial to do. Until GCC is made smarter, -Wall
should not generate these warnings.
Here's a proposed patch.
1997-09-24 Paul Eggert <eggert@twinsun.com>
* c-decl.c (c_decode_option): -Wall no longer implies
-Wsign-compare, since -Wsign-compare cries wolf too often.
===================================================================
RCS file: c-decl.c,v
retrieving revision 1.1
retrieving revision 1.2
diff -c -r1.1 -r1.2
*** c-decl.c 1997/09/10 17:41:05 1.1
--- c-decl.c 1997/09/24 19:15:08 1.2
***************
*** 758,764 ****
warn_char_subscripts = 1;
warn_parentheses = 1;
warn_missing_braces = 1;
- warn_sign_compare = 1;
/* We set this to 2 here, but 1 in -Wmain, so -ffreestanding can turn
it off only if it's not explicit. */
warn_main = 2;
--- 758,763 ----
===================================================================
RCS file: RCS/invoke.texi,v
retrieving revision 1.1
retrieving revision 1.2
diff -c -r1.1 -r1.2
*** invoke.texi 1997/09/12 18:29:28 1.1
--- invoke.texi 1997/09/24 19:15:08 1.2
***************
*** 1338,1350 ****
and @samp{j} will be rearranged to match the declaration order of the
members.
- @item -Wsign-compare
- @cindex warning for comparison of signed and unsigned values
- @cindex comparison of signed and unsigned values, warning
- @cindex signed and unsigned values, comparison warning
- Warn when a comparison between signed and unsigned values could produce
- an incorrect result when the signed value is converted to unsigned.
-
@item -Wtemplate-debugging
@cindex template debugging
When using templates in a C++ program, warn if debugging is not yet
--- 1338,1343 ----
***************
*** 1506,1511 ****
--- 1499,1511 ----
@code{x = -1} if @code{x} is unsigned. But do not warn about explicit
casts like @code{(unsigned) -1}.
+ @item -Wsign-compare
+ @cindex warning for comparison of signed and unsigned values
+ @cindex comparison of signed and unsigned values, warning
+ @cindex signed and unsigned values, comparison warning
+ Warn when a comparison between signed and unsigned values could produce
+ an incorrect result when the signed value is converted to unsigned.
+
@item -Waggregate-return
Warn if any functions that return structures or unions are defined or
called. (In languages where you can return an array, this also elicits
More information about the Gcc
mailing list