proposed patch for constant signed/unsigned comparison warning

Paul Eggert eggert@twinsun.com
Thu Sep 25 03:26:00 GMT 1997


   Date: Wed, 24 Sep 1997 23:16:40 -0700
   From: Jim Wilson <wilson@cygnus.com>

   Since this particular change was not intentional, there does not seem to be
   any reason why it must be retained.  I think we could make everyone happy
   here if we changed this back so that -Wsign-compare is enabled by -W.

That sounds reasonable.  This behavior would preserve compatibility
with GCC 2.0 through 2.7.2.3, and so it would be less likely to annoy
existing users (and this includes the users who like the warning and
are turning it on now with -W).  Thanks for tracking this down.

Here's a revised patch that implements this suggestion.
Any objections if I install it?

1997-09-25  Paul Eggert  <eggert@twinsun.com>

	Go back to GCC 2.0 through 2.7.2.x behavior for warning about
	signed vs unsigned comparisons, except that -Wsign-compare now
	controls this warning separately.
	* c-typeck.c (build_binary_op): Warn about comparing signed vs
	unsigned if -W is specified and -Wno-sign-compare is not.
	* c-decl.c (warn_sign_compare): Initialize to -1, indicating
	that the flag hasn't been set either positively or negatively.
	(c_decode_option): -Wall no longer implies -Wsign-compare.

===================================================================
RCS file: c-decl.c,v
retrieving revision 1.1
retrieving revision 1.3
diff -c -r1.1 -r1.3
*** c-decl.c	1997/09/10 17:41:05	1.1
--- c-decl.c	1997/09/25 07:23:15	1.3
***************
*** 553,561 ****
  
  int warn_main;
  
! /* Warn about comparison of signed and unsigned values.  */
  
! int warn_sign_compare;
  
  /* Nonzero means `$' can be in an identifier.  */
  
--- 553,562 ----
  
  int warn_main;
  
! /* Warn about comparison of signed and unsigned values.
!    If -1, neither -Wsign-compare nor -Wno-sign-compare has been specified.  */
  
! int warn_sign_compare = -1;
  
  /* Nonzero means `$' can be in an identifier.  */
  
***************
*** 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;
--- 759,764 ----
===================================================================
RCS file: c-typeck.c,v
retrieving revision 2.7.2.11
retrieving revision 2.7.2.12
diff -c -r2.7.2.11 -r2.7.2.12
*** c-typeck.c	1997/09/10 10:58:25	2.7.2.11
--- c-typeck.c	1997/09/25 07:30:28	2.7.2.12
***************
*** 2495,2501 ****
  	  converted = 1;
  	  resultcode = xresultcode;
  
! 	  if (warn_sign_compare && skip_evaluation == 0)
  	    {
  	      int op0_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op0));
  	      int op1_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op1));
--- 2495,2502 ----
  	  converted = 1;
  	  resultcode = xresultcode;
  
! 	  if ((warn_sign_compare < 0 ? extra_warnings : warn_sign_compare != 0)
! 	      && skip_evaluation == 0)
  	    {
  	      int op0_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op0));
  	      int op1_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op1));
===================================================================
RCS file: invoke.texi,v
retrieving revision 1.1
retrieving revision 1.3
diff -c -r1.1 -r1.3
*** invoke.texi	1997/09/12 18:29:28	1.1
--- invoke.texi	1997/09/25 07:23:15	1.3
***************
*** 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 ----
***************
*** 1421,1426 ****
--- 1414,1424 ----
  arguments.
  
  @item
+ A comparison between signed and unsigned values could produce an
+ incorrect result when the signed value is converted to unsigned.
+ (But do not warn if @samp{-Wno-sign-compare} is also specified.)
+ 
+ @item
  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 @code{x.h}:
***************
*** 1506,1511 ****
--- 1504,1518 ----
  @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.
+ This warning is also enabled by @samp{-W}; to get the other warnings
+ of @samp{-W} without this warning, use @samp{-W -Wno-sign-compare}.
+ 
  @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