🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

1 total messages Started by Paul Eggert Thu, 05 Nov 1992 01:53
more consistent warnings for signed integer overflow (GCC 2.3.1)
#82
Author: Paul Eggert
Date: Thu, 05 Nov 1992 01:53
169 lines
6731 bytes
GCC 2.3.1 (sparc, SunOS 4.1.2) warns about signed integer overflow in
expressions that the ANSI standard requires to be constant expressions,
but it does not warn about signed integer overflow in most other contexts.
For example, the program

	long long id(long long x) { return 1LL << 63; }
	main() { static long long a = 1LL<<63;
		long long b = 1LL<<63;
		long long c = id(1LL<<63);
		long long d; d = 1LL<<63;
		return (a|b|c|d) != 1LL<<63; }

contains signed integer overflows on all six lines, but GCC diagnoses only
lines 2 and 3, as follows (only the line 2 diagnostic is required by ANSI C):

	t.c: In function `main':
	t.c:2: warning: overflow in constant expression
	t.c:3: warning: overflow in constant expression

GCC should warn about all six lines; this is more consistent and more useful.
Here is a patch.  This patch causes GCC to detect signed integer overflows in
programmer-specified operations on constants, except for overflows that result
from casts (where programmers are presumed to know what they are doing).

Thu Nov  5 01:27:21 1992  Paul Eggert  (eggert@twinsun.com)

	* c-typeck.c (build_c_cast): Ignore overflows that arise from casts.
	(convert_for_assignment): Warn if signed integer conversion overflows.
	(parser_build_binary_op): Warn if signed integer binary op overflows.
	* c-parse.y (unary_expr): Warn if signed integer unary op overflows.
	* fold-const.c (fold_convert): Do not discard signed integer overflows
	that come from conversions; build_c_cast now discards the right ones.
	* c-common.c (constant_expression_warning): Suppress duplicate
	overflow warnings that would otherwise occur due to the above changes.

===================================================================
RCS file: c-typeck.c,v
retrieving revision 2.3.1.1
retrieving revision 2.3.1.2
diff -c -r2.3.1.1 -r2.3.1.2
*** c-typeck.c	1992/11/04 19:04:56	2.3.1.1
--- c-typeck.c	1992/11/05 01:27:21	2.3.1.2
***************
*** 2136,2141 ****
--- 2136,2143 ----
        && (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<'))
      warning ("comparisons like X<=Y<=Z do not have their mathematical meaning");
  
+   constant_expression_warning (result);
+ 
    class = TREE_CODE_CLASS (TREE_CODE (result));
  
    /* Record the code that was specified in the source,
***************
*** 3769,3774 ****
--- 3771,3780 ----
  	warning ("cast to pointer from integer of different size");
  
        value = convert (type, value);
+ 
+       /* Ignore any integer overflow caused by the cast.  */
+       if (TREE_CODE (value) == INTEGER_CST)
+ 	TREE_CONSTANT_OVERFLOW (value) = 0;
      }
  
    if (value == expr && pedantic)
***************
*** 3991,3997 ****
         &&
        (coder == INTEGER_TYPE || coder == REAL_TYPE || coder == ENUMERAL_TYPE))
      {
!       return convert (type, rhs);
      }
    /* Conversions among pointers */
    else if (codel == POINTER_TYPE && coder == POINTER_TYPE)
--- 3997,4005 ----
         &&
        (coder == INTEGER_TYPE || coder == REAL_TYPE || coder == ENUMERAL_TYPE))
      {
!       tree t = convert (type, rhs);
!       constant_expression_warning (t);
!       return t;
      }
    /* Conversions among pointers */
    else if (codel == POINTER_TYPE && coder == POINTER_TYPE)
===================================================================
RCS file: c-parse.y,v
retrieving revision 2.3
retrieving revision 2.3.1.1
diff -c -r2.3 -r2.3.1.1
*** c-parse.y	1992/11/02 19:59:22	2.3
--- c-parse.y	1992/11/05 01:27:21	2.3.1.1
***************
*** 341,347 ****
  		{ $$ = $3;
  		  pedantic = $<itype>1; }
  	| unop cast_expr  %prec UNARY
! 		{ $$ = build_unary_op ($1, $2, 0); }
  	/* Refer to the address of a label as a pointer.  */
  	| ANDAND identifier
  		{ tree label = lookup_label ($2);
--- 341,348 ----
  		{ $$ = $3;
  		  pedantic = $<itype>1; }
  	| unop cast_expr  %prec UNARY
! 		{ $$ = build_unary_op ($1, $2, 0);
! 		  constant_expression_warning ($$); }
  	/* Refer to the address of a label as a pointer.  */
  	| ANDAND identifier
  		{ tree label = lookup_label ($2);
===================================================================
RCS file: fold-const.c,v
retrieving revision 2.3.1.4
retrieving revision 2.3.1.5
diff -c -r2.3.1.4 -r2.3.1.5
*** fold-const.c	1992/11/04 20:29:55	2.3.1.4
--- fold-const.c	1992/11/05 01:27:21	2.3.1.5
***************
*** 1487,1497 ****
  	     appropriately sign-extended or truncated.  */
  	  t = build_int_2 (TREE_INT_CST_LOW (arg1),
  			   TREE_INT_CST_HIGH (arg1));
- 	  /* Carry forward overflow indication unless truncating.  */
- 	  if (TYPE_PRECISION (type) >= TYPE_PRECISION (TREE_TYPE (t)))
- 	    TREE_CONSTANT_OVERFLOW (t) = TREE_CONSTANT_OVERFLOW (arg1);
  	  TREE_TYPE (t) = type;
! 	  force_fit_type (t, 0);
  	}
  #if !defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
        else if (TREE_CODE (arg1) == REAL_CST)
--- 1487,1496 ----
  	     appropriately sign-extended or truncated.  */
  	  t = build_int_2 (TREE_INT_CST_LOW (arg1),
  			   TREE_INT_CST_HIGH (arg1));
  	  TREE_TYPE (t) = type;
! 	  TREE_CONSTANT_OVERFLOW (t) =
! 	    (TREE_CONSTANT_OVERFLOW (arg1)
! 	     | force_fit_type (t, 0));
  	}
  #if !defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
        else if (TREE_CODE (arg1) == REAL_CST)
===================================================================
RCS file: c-common.c,v
retrieving revision 2.3
retrieving revision 2.3.1.1
diff -c -r2.3 -r2.3.1.1
*** c-common.c	1992/10/01 18:58:42	2.3
--- c-common.c	1992/11/05 01:27:21	2.3.1.1
***************
*** 294,300 ****
       tree value;
  {
    if (TREE_CODE (value) == INTEGER_CST && TREE_CONSTANT_OVERFLOW (value))
!     pedwarn ("overflow in constant expression");
  }
  
  void
--- 294,304 ----
       tree value;
  {
    if (TREE_CODE (value) == INTEGER_CST && TREE_CONSTANT_OVERFLOW (value))
!     {
!       pedwarn ("overflow in constant expression");
!       /* Suppress duplicate warnings.  */
!       TREE_CONSTANT_OVERFLOW (value) = 0;
!     }
  }
  
  void
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads