public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] PR c++/28986 failure to diagnose overflow for binary operators
@ 2007-01-06 17:49 Manuel López-Ibáñez
  2007-01-06 18:11 ` Gabriel Dos Reis
  0 siblings, 1 reply; 4+ messages in thread
From: Manuel López-Ibáñez @ 2007-01-06 17:49 UTC (permalink / raw)
  To: gcc-patches

[-- Attachment #1: Type: text/plain, Size: 1837 bytes --]

:ADDPATCH c++:

Somehow the C++ front-end is not able to diagnose overflow for the
result of binary operators. The fix is trivial (I can hardly believe
that this is not a regression). In addition, I have adapted the
overflow testcases from the C front-end to the C++ testsuite with some
minor modifications. For reference, I also provide a diff showing
those modifications.

One particular issue that I am not sure about is whether initialisers
for static variables need to be constant in C++ as they do in C. The
relevant hunk in the diff between C and C++ testcases is:

-/* But this expression does need to be constant.  */
+/* But this expression does need to be constant (in C++ ???).  */
 static int sc = INT_MAX + 1; /* { dg-warning "warning: integer
overflow in expression" } */
-/* { dg-warning "warning: overflow in constant expression" "constant"
{ target *-*-* } 47 } */
+/* { dg-warning "warning: overflow in constant expression" "constant"
{ xfail *-*-* } 47 } */


Bootstrapped and tested with --enable-languages=all on
i686-pc-linux-gnu for revision 120511.

cp/
2007-01-06  Manuel Lopez-Ibanez <manu@gcc.gnu.org>

    PR c++/28986
    * typeck.c (build_binary_op): Call overflow_warning if
TREE_OVERFLOW_P is true for the result and not for any of the
operands.

testsuite/
2007-01-06  Manuel Lopez-Ibanez <manu@gcc.gnu.org>

    PR c++/28986
    * g++.dg/conversion/nullptr1.C: Added overflow warning.
    * g++.dg/warn/multiple-overflow-warn-2.C: New.
    * g++.dg/warn/overflow-warn-1.C: New.
    * g++.dg/warn/overflow-warn-2.C: New.
    * g++.dg/warn/overflow-warn-3.C: New.
    * g++.dg/warn/overflow-warn-4.C: New.
    * g++.dg/warn/overflow-warn-5.C: New.
    * g++.dg/warn/overflow-warn-6.C: New.
    * g++.dg/warn/Woverflow-1.C: New.
    * g++.dg/warn/Woverflow-2.C: New.
    * g++.dg/warn/Woverflow-3.C: New.

[-- Attachment #2: c-c++-overflow-testsuite.diff --]
[-- Type: text/plain, Size: 14180 bytes --]

--- trunk/gcc/testsuite/gcc.dg/multiple-overflow-warn-1.c	2007-01-06 00:20:24.000000000 +0100
+++ trunk/gcc/testsuite/g++.dg/warn/multiple-overflow-warn-1.C	2007-01-06 00:25:06.000000000 +0100
@@ -1,6 +1,6 @@
 /* PR c/19978 : Test for duplicated warnings (unary operators).  */
 /* { dg-do compile } */
-/* { dg-options "-std=c99 -Woverflow" } */
+/* { dg-options "-Woverflow" } */
 
 #include <limits.h>
 
--- trunk/gcc/testsuite/gcc.dg/multiple-overflow-warn-2.c	2007-01-06 00:21:02.000000000 +0100
+++ trunk/gcc/testsuite/g++.dg/warn/multiple-overflow-warn-2.C	2007-01-06 09:40:55.000000000 +0100
@@ -1,6 +1,6 @@
 /* PR c/19978 : Test for duplicated warnings (binary operators).  */
 /* { dg-do compile } */
-/* { dg-options "-std=c99 -Woverflow" } */
+/* { dg-options "-Woverflow" } */
 
 #include <limits.h>
 
--- trunk/gcc/testsuite/gcc.dg/overflow-warn-1.c	2006-12-02 03:16:46.000000000 +0100
+++ trunk/gcc/testsuite/g++.dg/warn/overflow-warn-1.C	2007-01-06 09:40:55.000000000 +0100
@@ -1,7 +1,7 @@
 /* Test for diagnostics for constant overflow.  */
 /* Origin: Joseph Myers <joseph@codesourcery.com> */
 /* { dg-do compile } */
-/* { dg-options "-std=c99" } */
+/* { dg-options "" } */
 
 #include <limits.h>
 
@@ -11,7 +11,7 @@ enum e {
   E1 = UINT_MAX + 1,
   /* Overflow in an unevaluated part of an expression is OK (example
      in the standard).  */
-  E2 = 2 || 1 / 0,
+  E2 = 2 || 1 / 0, /* { dg-bogus "warning: division by zero" "" { xfail *-*-* } 14 } */
   E3 = 1 / 0, /* { dg-warning "warning: division by zero" } */
   /* { dg-error "error: enumerator value for 'E3' is not an integer constant" "enum error" { target *-*-* } 15 } */
   /* But as in DR#031, the 1/0 in an evaluated subexpression means the
@@ -46,10 +46,10 @@ static int sc = INT_MAX + 1; /* { dg-war
    constants.  The third has the overflow in an unevaluated
    subexpression, so is a null pointer constant.  */
 void *p = 0 * (INT_MAX + 1); /* { dg-warning "warning: integer overflow in expression" } */
-/* { dg-warning "warning: initialization makes pointer from integer without a cast" "null" { target *-*-* } 48 } */
+/* { dg-error "invalid conversion from 'int' to 'void" "null" { target *-*-* } 48 } */
 void *q = 0 * (1 / 0); /* { dg-warning "warning: division by zero" } */
-/* { dg-warning "warning: initialization makes pointer from integer without a cast" "null" { xfail *-*-* } 50 } */
-void *r = (1 ? 0 : INT_MAX+1);
+/* { dg-error "invalid conversion from 'int' to 'void*'" "null" { xfail *-*-* } 50 } */
+void *r = (1 ? 0 : INT_MAX+1); /* { dg-bogus "integer overflow in expression" "" { xfail *-*-* } 52 } */
 
 void
 g (int i)
--- trunk/gcc/testsuite/gcc.dg/overflow-warn-2.c	2006-12-02 03:17:12.000000000 +0100
+++ trunk/gcc/testsuite/g++.dg/warn/overflow-warn-2.C	1970-01-01 01:00:00.000000000 +0100
@@ -1,132 +0,0 @@
-/* Test for diagnostics for constant overflow.  Test with -Wtraditional-conversion.  */
-/* Origin: Joseph Myers <joseph@codesourcery.com> */
-/* { dg-do compile } */
-/* { dg-options "-std=c99 -Wtraditional-conversion" } */
-
-#include <limits.h>
-
-enum e {
-  E0 = INT_MAX,
-  /* Unsigned overflow wraps around.  */
-  E1 = UINT_MAX + 1,
-  /* Overflow in an unevaluated part of an expression is OK (example
-     in the standard).  */
-  E2 = 2 || 1 / 0,
-  E3 = 1 / 0, /* { dg-warning "warning: division by zero" } */
-  /* { dg-error "error: enumerator value for 'E3' is not an integer constant" "enum error" { target *-*-* } 15 } */
-  /* But as in DR#031, the 1/0 in an evaluated subexpression means the
-     whole expression violates the constraints.  */
-  E4 = 0 * (1 / 0), /* { dg-warning "warning: division by zero" } */
-  /* { dg-error "error: enumerator value for 'E4' is not an integer constant" "enum error" { xfail *-*-* } 19 } */
-  E5 = INT_MAX + 1, /* { dg-warning "warning: integer overflow in expression" } */
-  /* Again, overflow in evaluated subexpression.  */
-  E6 = 0 * (INT_MAX + 1), /* { dg-warning "warning: integer overflow in expression" } */
-  /* A cast does not constitute overflow in conversion.  */
-  E7 = (char) INT_MAX
-};
-
-struct s {
-  int a;
-  int : 0 * (1 / 0); /* { dg-warning "warning: division by zero" } */
-  int : 0 * (INT_MAX + 1); /* { dg-warning "warning: integer overflow in expression" } */
-};
-
-void
-f (void)
-{
-  /* This expression is not required to be a constant expression, so
-     it should just involve undefined behavior at runtime.  */
-  int c = INT_MAX + 1; /* { dg-warning "warning: integer overflow in expression" } */
-}
-
-/* But this expression does need to be constant.  */
-static int sc = INT_MAX + 1; /* { dg-warning "warning: integer overflow in expression" } */
-
-/* The first two of these involve overflow, so are not null pointer
-   constants.  The third has the overflow in an unevaluated
-   subexpression, so is a null pointer constant.  */
-void *p = 0 * (INT_MAX + 1); /* { dg-warning "warning: integer overflow in expression" } */
-/* { dg-warning "warning: initialization makes pointer from integer without a cast" "null" { target *-*-* } 48 } */
-void *q = 0 * (1 / 0); /* { dg-warning "warning: division by zero" } */
-/* { dg-warning "warning: initialization makes pointer from integer without a cast" "null" { xfail *-*-* } 50 } */
-void *r = (1 ? 0 : INT_MAX+1);
-
-void
-g (int i)
-{
-  switch (i)
-    {
-    case 0 * (1/0): /* { dg-warning "warning: division by zero" } */
-      ;
-    case 1 + 0 * (INT_MAX + 1): /* { dg-warning "warning: integer overflow in expression" } */
-      ;
-    }
-}
-
-int
-h (void)
-{
-  return INT_MAX + 1; /* { dg-warning "warning: integer overflow in expression" } */
-}
-
-int
-h1 (void)
-{
-  return INT_MAX + 1 - INT_MAX; /* { dg-warning "warning: integer overflow in expression" } */
-}
-
-void fuc (unsigned char);
-void fsc (signed char);
-
-void
-h2 (void)
-{
-  fsc (SCHAR_MAX + 1);
-  /* { dg-warning "warning: passing argument 1 of 'fsc' with different width due to prototype" "-Wtraditional-conversion" { target *-*-* } 84 } */
-  fsc (SCHAR_MIN - 1); /* { dg-warning "warning: overflow in implicit constant conversion" } */
-  /* { dg-warning "warning: passing argument 1 of 'fsc' with different width due to prototype" "-Wtraditional-conversion" { target *-*-* } 86 } */
-  fsc (UCHAR_MAX);
-  /* { dg-warning "warning: passing argument 1 of 'fsc' with different width due to prototype" "-Wtraditional-conversion" { target *-*-* } 88 } */
-  fsc (UCHAR_MAX + 1); /* { dg-warning "warning: overflow in implicit constant conversion" } */
-  /* { dg-warning "warning: passing argument 1 of 'fsc' with different width due to prototype" "-Wtraditional-conversion" { target *-*-* } 90 } */
-  fuc (-1);
-  /* { dg-warning "warning: passing argument 1 of 'fuc' with different width due to prototype" "-Wtraditional-conversion" { target *-*-* } 92 } */
-  fuc (UCHAR_MAX + 1); /* { dg-warning "warning: large integer implicitly truncated to unsigned type" } */
-  /* { dg-warning "warning: passing argument 1 of 'fuc' with different width due to prototype" "-Wtraditional-conversion" { target *-*-* } 94 } */
-  fuc (SCHAR_MIN);
-  /* { dg-warning "warning: passing argument 1 of 'fuc' with different width due to prototype" "-Wtraditional-conversion" { target *-*-* } 96 } */
-  fuc (SCHAR_MIN - 1); /* { dg-warning "warning: large integer implicitly truncated to unsigned type" } */
-  /* { dg-warning "warning: passing argument 1 of 'fuc' with different width due to prototype" "-Wtraditional-conversion" { target *-*-* } 98 } */
-  fuc (-UCHAR_MAX); /* { dg-warning "warning: large integer implicitly truncated to unsigned type" } */
-  /* { dg-warning "warning: passing argument 1 of 'fuc' with different width due to prototype" "-Wtraditional-conversion" { target *-*-* } 100 } */
-}
-
-void fui (unsigned int);
-void fsi (signed int);
-
-int si;
-unsigned ui;
-
-void
-h2i (int x)
-{
-  /* For some reason, we only give certain warnings for implicit
-     conversions among values of the same precision with -Wtraditional-conversion,
-     while we don't give others at all.  */
-  fsi ((unsigned)INT_MAX + 1); /* { dg-warning "warning: passing argument 1 of 'fsi' as signed due to prototype" } */
-  si = (unsigned)INT_MAX + 1;
-  si = x ? (unsigned)INT_MAX + 1 : 1;
-  fsi ((unsigned)INT_MAX + 2); /* { dg-warning "warning: passing argument 1 of 'fsi' as signed due to prototype" } */
-  si = (unsigned)INT_MAX + 2;
-  si = x ? (unsigned)INT_MAX + 2 : 1;
-  fsi (UINT_MAX); /* { dg-warning "warning: passing argument 1 of 'fsi' as signed due to prototype" } */
-  si = UINT_MAX;
-  fui (-1);
-  /* { dg-warning "warning: passing argument 1 of 'fui' as unsigned due to prototype" "-Wtraditional-conversion" { target *-*-* } 124 } */
-  ui = -1;
-  ui = x ? -1 : 1U;
-  fui (INT_MIN);
-  /* { dg-warning "warning: passing argument 1 of 'fui' as unsigned due to prototype" "-Wtraditional-conversion" { target *-*-* } 128 } */
-  ui = INT_MIN;
-  ui = x ? INT_MIN : 1U;
-}
--- trunk/gcc/testsuite/gcc.dg/overflow-warn-3.c	2007-01-06 00:20:24.000000000 +0100
+++ trunk/gcc/testsuite/g++.dg/warn/overflow-warn-3.C	2007-01-06 09:40:55.000000000 +0100
@@ -1,7 +1,7 @@
 /* Test for diagnostics for constant overflow.  Test with -pedantic.  */
 /* Origin: Joseph Myers <joseph@codesourcery.com> */
 /* { dg-do compile } */
-/* { dg-options "-std=c99 -pedantic" } */
+/* { dg-options "-fpermissive -pedantic" } */
 
 #include <limits.h>
 
@@ -11,7 +11,7 @@ enum e {
   E1 = UINT_MAX + 1,
   /* Overflow in an unevaluated part of an expression is OK (example
      in the standard).  */
-  E2 = 2 || 1 / 0,
+  E2 = 2 || 1 / 0, /* { dg-bogus "warning: division by zero" "" { xfail *-*-* } 14 } */
   E3 = 1 / 0, /* { dg-warning "warning: division by zero" } */
   /* { dg-error "error: enumerator value for 'E3' is not an integer constant" "enum error" { target *-*-* } 15 } */
   /* But as in DR#031, the 1/0 in an evaluated subexpression means the
@@ -43,19 +43,19 @@ f (void)
 
 }
 
-/* But this expression does need to be constant.  */
+/* But this expression does need to be constant (in C++ ???).  */
 static int sc = INT_MAX + 1; /* { dg-warning "warning: integer overflow in expression" } */
-/* { dg-warning "warning: overflow in constant expression" "constant" { target *-*-* } 47 } */
+/* { dg-warning "warning: overflow in constant expression" "constant" { xfail *-*-* } 47 } */
 
 /* The first two of these involve overflow, so are not null pointer
    constants.  The third has the overflow in an unevaluated
    subexpression, so is a null pointer constant.  */
 void *p = 0 * (INT_MAX + 1); /* { dg-warning "warning: integer overflow in expression" } */
-/* { dg-warning "warning: overflow in constant expression" "constant" { target *-*-* } 53 } */
-/* { dg-warning "warning: initialization makes pointer from integer without a cast" "null" { target *-*-* } 53 } */
+/* { dg-error "invalid conversion from 'int' to 'void" "null" { target *-*-* } 53 } */
+
 void *q = 0 * (1 / 0); /* { dg-warning "warning: division by zero" } */
-/* { dg-warning "warning: initialization makes pointer from integer without a cast" "null" { xfail *-*-* } 56 } */
-void *r = (1 ? 0 : INT_MAX+1);
+/* { dg-error "invalid conversion from 'int' to 'void*'" "null" { xfail *-*-* } 56 } */
+void *r = (1 ? 0 : INT_MAX+1); /* { dg-bogus "integer overflow in expression" "" { xfail *-*-* } 58 } */
 
 void
 g (int i)
--- trunk/gcc/testsuite/gcc.dg/overflow-warn-4.c	2007-01-06 00:20:24.000000000 +0100
+++ trunk/gcc/testsuite/g++.dg/warn/overflow-warn-4.C	2007-01-06 09:40:55.000000000 +0100
@@ -1,7 +1,7 @@
 /* Test for diagnostics for constant overflow.  Test with -pedantic-errors.  */
 /* Origin: Joseph Myers <joseph@codesourcery.com> */
 /* { dg-do compile } */
-/* { dg-options "-std=c99 -pedantic-errors" } */
+/* { dg-options "-pedantic-errors" } */
 
 #include <limits.h>
 
@@ -11,7 +11,7 @@ enum e {
   E1 = UINT_MAX + 1,
   /* Overflow in an unevaluated part of an expression is OK (example
      in the standard).  */
-  E2 = 2 || 1 / 0,
+  E2 = 2 || 1 / 0, /* { dg-bogus "warning: division by zero" "" { xfail *-*-* } 14 } */
   E3 = 1 / 0, /* { dg-warning "warning: division by zero" } */
   /* { dg-error "error: enumerator value for 'E3' is not an integer constant" "enum error" { target *-*-* } 15 } */
   /* But as in DR#031, the 1/0 in an evaluated subexpression means the
@@ -43,19 +43,19 @@ f (void)
 
 }
 
-/* But this expression does need to be constant.  */
+/* But this expression does need to be constant (in C++ ???).  */
 static int sc = INT_MAX + 1; /* { dg-warning "warning: integer overflow in expression" } */
-/* { dg-error "error: overflow in constant expression" "constant" { target *-*-* } 47 } */
+/* { dg-error "error: overflow in constant expression" "constant" { xfail *-*-* } 47 } */
 
 /* The first two of these involve overflow, so are not null pointer
    constants.  The third has the overflow in an unevaluated
    subexpression, so is a null pointer constant.  */
 void *p = 0 * (INT_MAX + 1); /* { dg-warning "warning: integer overflow in expression" } */
-/* { dg-error "error: overflow in constant expression" "constant" { target *-*-* } 53 } */
-/* { dg-error "error: initialization makes pointer from integer without a cast" "null" { target *-*-* } 53 } */
+/* { dg-error "invalid conversion from 'int' to 'void" "null" { target *-*-* } 53 } */
+
 void *q = 0 * (1 / 0); /* { dg-warning "warning: division by zero" } */
-/* { dg-error "error: initialization makes pointer from integer without a cast" "null" { xfail *-*-* } 56 } */
-void *r = (1 ? 0 : INT_MAX+1);
+/* { dg-error "invalid conversion from 'int' to 'void*'" "null" { xfail *-*-* } 56 } */
+void *r = (1 ? 0 : INT_MAX+1); /* { dg-bogus "integer overflow in expression" "" { xfail *-*-* } 58 } */
 
 void
 g (int i)
--- trunk/gcc/testsuite/gcc.dg/overflow-warn-6.c	2007-01-06 00:22:02.000000000 +0100
+++ trunk/gcc/testsuite/g++.dg/warn/overflow-warn-6.C	2007-01-06 09:40:55.000000000 +0100
@@ -1,6 +1,6 @@
 /* Test non-constant operands in overflowed expressions.  */
 /* { dg-do compile } */
-/* { dg-options "-std=c99 -Woverflow" } */
+/* { dg-options "-Woverflow" } */
 
 #include <limits.h>
 

[-- Attachment #3: c++-overflow-warning.diff --]
[-- Type: text/plain, Size: 18792 bytes --]

Index: gcc/testsuite/g++.dg/conversion/nullptr1.C
===================================================================
--- gcc/testsuite/g++.dg/conversion/nullptr1.C	(revision 120511)
+++ gcc/testsuite/g++.dg/conversion/nullptr1.C	(working copy)
@@ -6,5 +6,5 @@
 void *p = 0;
 
 void *q = 0 * (INT_MAX + 1);  // { dg-error "invalid conversion" }
-
+// { dg-warning "integer overflow in expression" "" { target *-*-* } 8 }
 
Index: gcc/testsuite/g++.dg/warn/overflow-warn-3.C
===================================================================
--- gcc/testsuite/g++.dg/warn/overflow-warn-3.C	(revision 0)
+++ gcc/testsuite/g++.dg/warn/overflow-warn-3.C	(revision 0)
@@ -0,0 +1,128 @@
+/* Test for diagnostics for constant overflow.  Test with -pedantic.  */
+/* Origin: Joseph Myers <joseph@codesourcery.com> */
+/* { dg-do compile } */
+/* { dg-options "-fpermissive -pedantic" } */
+
+#include <limits.h>
+
+enum e {
+  E0 = INT_MAX,
+  /* Unsigned overflow wraps around.  */
+  E1 = UINT_MAX + 1,
+  /* Overflow in an unevaluated part of an expression is OK (example
+     in the standard).  */
+  E2 = 2 || 1 / 0, /* { dg-bogus "warning: division by zero" "" { xfail *-*-* } 14 } */
+  E3 = 1 / 0, /* { dg-warning "warning: division by zero" } */
+  /* { dg-error "error: enumerator value for 'E3' is not an integer constant" "enum error" { target *-*-* } 15 } */
+  /* But as in DR#031, the 1/0 in an evaluated subexpression means the
+     whole expression violates the constraints.  */
+  E4 = 0 * (1 / 0), /* { dg-warning "warning: division by zero" } */
+  /* { dg-error "error: enumerator value for 'E4' is not an integer constant" "enum error" { xfail *-*-* } 19 } */
+  E5 = INT_MAX + 1, /* { dg-warning "warning: integer overflow in expression" } */
+  /* { dg-warning "warning: overflow in constant expression" "constant" { target *-*-* } 21 } */
+  /* Again, overflow in evaluated subexpression.  */
+  E6 = 0 * (INT_MAX + 1), /* { dg-warning "warning: integer overflow in expression" } */
+  /* { dg-warning "warning: overflow in constant expression" "constant" { target *-*-* } 24 } */
+  /* A cast does not constitute overflow in conversion.  */
+  E7 = (char) INT_MAX
+};
+
+struct s {
+  int a;
+  int : 0 * (1 / 0); /* { dg-warning "warning: division by zero" } */
+  int : 0 * (INT_MAX + 1); /* { dg-warning "warning: integer overflow in expression" } */
+  /* { dg-warning "warning: overflow in constant expression" "constant" { target *-*-* } 33 } */
+};
+
+void
+f (void)
+{
+  /* This expression is not required to be a constant expression, so
+     it should just involve undefined behavior at runtime.  */
+  int c = INT_MAX + 1; /* { dg-warning "warning: integer overflow in expression" } */
+
+}
+
+/* But this expression does need to be constant (in C++ ???).  */
+static int sc = INT_MAX + 1; /* { dg-warning "warning: integer overflow in expression" } */
+/* { dg-warning "warning: overflow in constant expression" "constant" { xfail *-*-* } 47 } */
+
+/* The first two of these involve overflow, so are not null pointer
+   constants.  The third has the overflow in an unevaluated
+   subexpression, so is a null pointer constant.  */
+void *p = 0 * (INT_MAX + 1); /* { dg-warning "warning: integer overflow in expression" } */
+/* { dg-error "invalid conversion from 'int' to 'void" "null" { target *-*-* } 53 } */
+
+void *q = 0 * (1 / 0); /* { dg-warning "warning: division by zero" } */
+/* { dg-error "invalid conversion from 'int' to 'void*'" "null" { xfail *-*-* } 56 } */
+void *r = (1 ? 0 : INT_MAX+1); /* { dg-bogus "integer overflow in expression" "" { xfail *-*-* } 58 } */
+
+void
+g (int i)
+{
+  switch (i)
+    {
+    case 0 * (1/0): /* { dg-warning "warning: division by zero" } */
+      ;
+    case 1 + 0 * (INT_MAX + 1): /* { dg-warning "warning: integer overflow in expression" } */
+      /* { dg-warning "warning: overflow in constant expression" "constant" { target *-*-* } 67 } */
+      ;
+    }
+}
+
+int
+h (void)
+{
+  return INT_MAX + 1; /* { dg-warning "warning: integer overflow in expression" } */
+}
+
+int
+h1 (void)
+{
+  return INT_MAX + 1 - INT_MAX; /* { dg-warning "warning: integer overflow in expression" } */
+}
+
+void fuc (unsigned char);
+void fsc (signed char);
+
+void
+h2 (void)
+{
+  fsc (SCHAR_MAX + 1); /* { dg-warning "warning: overflow in implicit constant conversion" } */
+  fsc (SCHAR_MIN - 1); /* { dg-warning "warning: overflow in implicit constant conversion" } */
+  fsc (UCHAR_MAX); /* { dg-warning "warning: overflow in implicit constant conversion" } */
+  fsc (UCHAR_MAX + 1); /* { dg-warning "warning: overflow in implicit constant conversion" } */
+  fuc (-1);
+  fuc (UCHAR_MAX + 1); /* { dg-warning "warning: large integer implicitly truncated to unsigned type" } */
+  fuc (SCHAR_MIN);
+  fuc (SCHAR_MIN - 1); /* { dg-warning "warning: large integer implicitly truncated to unsigned type" } */
+  fuc (-UCHAR_MAX); /* { dg-warning "warning: large integer implicitly truncated to unsigned type" } */
+}
+
+void fui (unsigned int);
+void fsi (signed int);
+
+int si;
+unsigned ui;
+
+void
+h2i (int x)
+{
+  /* For some reason, we only give certain warnings for implicit
+     conversions among values of the same precision with -Wconversion,
+     while we don't give others at all.  */
+  fsi ((unsigned)INT_MAX + 1);
+  si = (unsigned)INT_MAX + 1;
+  si = x ? (unsigned)INT_MAX + 1 : 1;
+  fsi ((unsigned)INT_MAX + 2);
+  si = (unsigned)INT_MAX + 2;
+  si = x ? (unsigned)INT_MAX + 2 : 1;
+  fsi (UINT_MAX);
+  si = UINT_MAX;
+  fui (-1);
+  ui = -1;
+  ui = x ? -1 : 1U;
+  fui (INT_MIN);
+  ui = INT_MIN;
+  ui = x ? INT_MIN : 1U;
+}
Index: gcc/testsuite/g++.dg/warn/Woverflow-2.C
===================================================================
--- gcc/testsuite/g++.dg/warn/Woverflow-2.C	(revision 0)
+++ gcc/testsuite/g++.dg/warn/Woverflow-2.C	(revision 0)
@@ -0,0 +1,7 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -Woverflow" } */
+
+#include <limits.h>
+
+int foo = INT_MAX + 1;  /* { dg-warning "integer overflow" } */
+
Index: gcc/testsuite/g++.dg/warn/overflow-warn-4.C
===================================================================
--- gcc/testsuite/g++.dg/warn/overflow-warn-4.C	(revision 0)
+++ gcc/testsuite/g++.dg/warn/overflow-warn-4.C	(revision 0)
@@ -0,0 +1,128 @@
+/* Test for diagnostics for constant overflow.  Test with -pedantic-errors.  */
+/* Origin: Joseph Myers <joseph@codesourcery.com> */
+/* { dg-do compile } */
+/* { dg-options "-pedantic-errors" } */
+
+#include <limits.h>
+
+enum e {
+  E0 = INT_MAX,
+  /* Unsigned overflow wraps around.  */
+  E1 = UINT_MAX + 1,
+  /* Overflow in an unevaluated part of an expression is OK (example
+     in the standard).  */
+  E2 = 2 || 1 / 0, /* { dg-bogus "warning: division by zero" "" { xfail *-*-* } 14 } */
+  E3 = 1 / 0, /* { dg-warning "warning: division by zero" } */
+  /* { dg-error "error: enumerator value for 'E3' is not an integer constant" "enum error" { target *-*-* } 15 } */
+  /* But as in DR#031, the 1/0 in an evaluated subexpression means the
+     whole expression violates the constraints.  */
+  E4 = 0 * (1 / 0), /* { dg-warning "warning: division by zero" } */
+  /* { dg-error "error: enumerator value for 'E4' is not an integer constant" "enum error" { xfail *-*-* } 19 } */
+  E5 = INT_MAX + 1, /* { dg-warning "warning: integer overflow in expression" } */
+  /* { dg-error "error: overflow in constant expression" "constant" { target *-*-* } 21 } */
+  /* Again, overflow in evaluated subexpression.  */
+  E6 = 0 * (INT_MAX + 1), /* { dg-warning "warning: integer overflow in expression" } */
+  /* { dg-error "error: overflow in constant expression" "constant" { target *-*-* } 24 } */
+  /* A cast does not constitute overflow in conversion.  */
+  E7 = (char) INT_MAX
+};
+
+struct s {
+  int a;
+  int : 0 * (1 / 0); /* { dg-warning "warning: division by zero" } */
+  int : 0 * (INT_MAX + 1); /* { dg-warning "warning: integer overflow in expression" } */
+  /* { dg-error "error: overflow in constant expression" "constant" { target *-*-* } 33 } */
+};
+
+void
+f (void)
+{
+  /* This expression is not required to be a constant expression, so
+     it should just involve undefined behavior at runtime.  */
+  int c = INT_MAX + 1; /* { dg-warning "warning: integer overflow in expression" } */
+
+}
+
+/* But this expression does need to be constant (in C++ ???).  */
+static int sc = INT_MAX + 1; /* { dg-warning "warning: integer overflow in expression" } */
+/* { dg-error "error: overflow in constant expression" "constant" { xfail *-*-* } 47 } */
+
+/* The first two of these involve overflow, so are not null pointer
+   constants.  The third has the overflow in an unevaluated
+   subexpression, so is a null pointer constant.  */
+void *p = 0 * (INT_MAX + 1); /* { dg-warning "warning: integer overflow in expression" } */
+/* { dg-error "invalid conversion from 'int' to 'void" "null" { target *-*-* } 53 } */
+
+void *q = 0 * (1 / 0); /* { dg-warning "warning: division by zero" } */
+/* { dg-error "invalid conversion from 'int' to 'void*'" "null" { xfail *-*-* } 56 } */
+void *r = (1 ? 0 : INT_MAX+1); /* { dg-bogus "integer overflow in expression" "" { xfail *-*-* } 58 } */
+
+void
+g (int i)
+{
+  switch (i)
+    {
+    case 0 * (1/0): /* { dg-warning "warning: division by zero" } */
+      ;
+    case 1 + 0 * (INT_MAX + 1): /* { dg-warning "warning: integer overflow in expression" } */
+      /* { dg-error "error: overflow in constant expression" "constant" { target *-*-* } 67 } */
+      ;
+    }
+}
+
+int
+h (void)
+{
+  return INT_MAX + 1; /* { dg-warning "warning: integer overflow in expression" } */
+}
+
+int
+h1 (void)
+{
+  return INT_MAX + 1 - INT_MAX; /* { dg-warning "warning: integer overflow in expression" } */
+}
+
+void fuc (unsigned char);
+void fsc (signed char);
+
+void
+h2 (void)
+{
+  fsc (SCHAR_MAX + 1); /* { dg-warning "warning: overflow in implicit constant conversion" } */
+  fsc (SCHAR_MIN - 1); /* { dg-warning "warning: overflow in implicit constant conversion" } */
+  fsc (UCHAR_MAX); /* { dg-warning "warning: overflow in implicit constant conversion" } */
+  fsc (UCHAR_MAX + 1); /* { dg-warning "warning: overflow in implicit constant conversion" } */
+  fuc (-1);
+  fuc (UCHAR_MAX + 1); /* { dg-warning "warning: large integer implicitly truncated to unsigned type" } */
+  fuc (SCHAR_MIN);
+  fuc (SCHAR_MIN - 1); /* { dg-warning "warning: large integer implicitly truncated to unsigned type" } */
+  fuc (-UCHAR_MAX); /* { dg-warning "warning: large integer implicitly truncated to unsigned type" } */
+}
+
+void fui (unsigned int);
+void fsi (signed int);
+
+int si;
+unsigned ui;
+
+void
+h2i (int x)
+{
+  /* For some reason, we only give certain warnings for implicit
+     conversions among values of the same precision with -Wconversion,
+     while we don't give others at all.  */
+  fsi ((unsigned)INT_MAX + 1);
+  si = (unsigned)INT_MAX + 1;
+  si = x ? (unsigned)INT_MAX + 1 : 1;
+  fsi ((unsigned)INT_MAX + 2);
+  si = (unsigned)INT_MAX + 2;
+  si = x ? (unsigned)INT_MAX + 2 : 1;
+  fsi (UINT_MAX);
+  si = UINT_MAX;
+  fui (-1);
+  ui = -1;
+  ui = x ? -1 : 1U;
+  fui (INT_MIN);
+  ui = INT_MIN;
+  ui = x ? INT_MIN : 1U;
+}
Index: gcc/testsuite/g++.dg/warn/Woverflow-3.C
===================================================================
--- gcc/testsuite/g++.dg/warn/Woverflow-3.C	(revision 0)
+++ gcc/testsuite/g++.dg/warn/Woverflow-3.C	(revision 0)
@@ -0,0 +1,7 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -Wno-overflow" } */
+
+#include <limits.h>
+
+int foo = INT_MAX + 1;
+
Index: gcc/testsuite/g++.dg/warn/multiple-overflow-warn-2.C
===================================================================
--- gcc/testsuite/g++.dg/warn/multiple-overflow-warn-2.C	(revision 0)
+++ gcc/testsuite/g++.dg/warn/multiple-overflow-warn-2.C	(revision 0)
@@ -0,0 +1,12 @@
+/* PR c/19978 : Test for duplicated warnings (binary operators).  */
+/* { dg-do compile } */
+/* { dg-options "-Woverflow" } */
+
+#include <limits.h>
+
+int 
+g1 (void)
+{
+  return INT_MAX + 1 - INT_MAX; /* { dg-bogus "integer overflow in expression.*integer overflow in expression" } */
+  /* { dg-warning "integer overflow in expression" "" { target *-*-* } 10 } */
+}
Index: gcc/testsuite/g++.dg/warn/overflow-warn-1.C
===================================================================
--- gcc/testsuite/g++.dg/warn/overflow-warn-1.C	(revision 0)
+++ gcc/testsuite/g++.dg/warn/overflow-warn-1.C	(revision 0)
@@ -0,0 +1,121 @@
+/* Test for diagnostics for constant overflow.  */
+/* Origin: Joseph Myers <joseph@codesourcery.com> */
+/* { dg-do compile } */
+/* { dg-options "" } */
+
+#include <limits.h>
+
+enum e {
+  E0 = INT_MAX,
+  /* Unsigned overflow wraps around.  */
+  E1 = UINT_MAX + 1,
+  /* Overflow in an unevaluated part of an expression is OK (example
+     in the standard).  */
+  E2 = 2 || 1 / 0, /* { dg-bogus "warning: division by zero" "" { xfail *-*-* } 14 } */
+  E3 = 1 / 0, /* { dg-warning "warning: division by zero" } */
+  /* { dg-error "error: enumerator value for 'E3' is not an integer constant" "enum error" { target *-*-* } 15 } */
+  /* But as in DR#031, the 1/0 in an evaluated subexpression means the
+     whole expression violates the constraints.  */
+  E4 = 0 * (1 / 0), /* { dg-warning "warning: division by zero" } */
+  /* { dg-error "error: enumerator value for 'E4' is not an integer constant" "enum error" { xfail *-*-* } 19 } */
+  E5 = INT_MAX + 1, /* { dg-warning "warning: integer overflow in expression" } */
+  /* Again, overflow in evaluated subexpression.  */
+  E6 = 0 * (INT_MAX + 1), /* { dg-warning "warning: integer overflow in expression" } */
+  /* A cast does not constitute overflow in conversion.  */
+  E7 = (char) INT_MAX
+};
+
+struct s {
+  int a;
+  int : 0 * (1 / 0); /* { dg-warning "warning: division by zero" } */
+  int : 0 * (INT_MAX + 1); /* { dg-warning "warning: integer overflow in expression" } */
+};
+
+void
+f (void)
+{
+  /* This expression is not required to be a constant expression, so
+     it should just involve undefined behavior at runtime.  */
+  int c = INT_MAX + 1; /* { dg-warning "warning: integer overflow in expression" } */
+}
+
+/* But this expression does need to be constant.  */
+static int sc = INT_MAX + 1; /* { dg-warning "warning: integer overflow in expression" } */
+
+/* The first two of these involve overflow, so are not null pointer
+   constants.  The third has the overflow in an unevaluated
+   subexpression, so is a null pointer constant.  */
+void *p = 0 * (INT_MAX + 1); /* { dg-warning "warning: integer overflow in expression" } */
+/* { dg-error "invalid conversion from 'int' to 'void" "null" { target *-*-* } 48 } */
+void *q = 0 * (1 / 0); /* { dg-warning "warning: division by zero" } */
+/* { dg-error "invalid conversion from 'int' to 'void*'" "null" { xfail *-*-* } 50 } */
+void *r = (1 ? 0 : INT_MAX+1); /* { dg-bogus "integer overflow in expression" "" { xfail *-*-* } 52 } */
+
+void
+g (int i)
+{
+  switch (i)
+    {
+    case 0 * (1/0): /* { dg-warning "warning: division by zero" } */
+      ;
+    case 1 + 0 * (INT_MAX + 1): /* { dg-warning "warning: integer overflow in expression" } */
+      ;
+    }
+}
+
+int
+h (void)
+{
+  return INT_MAX + 1; /* { dg-warning "warning: integer overflow in expression" } */
+}
+
+int
+h1 (void)
+{
+  return INT_MAX + 1 - INT_MAX; /* { dg-warning "warning: integer overflow in expression" } */
+}
+
+void fuc (unsigned char);
+void fsc (signed char);
+
+void
+h2 (void)
+{
+  fsc (SCHAR_MAX + 1);
+  fsc (SCHAR_MIN - 1); /* { dg-warning "warning: overflow in implicit constant conversion" } */
+  fsc (UCHAR_MAX);
+  fsc (UCHAR_MAX + 1); /* { dg-warning "warning: overflow in implicit constant conversion" } */
+  fuc (-1);
+  fuc (UCHAR_MAX + 1); /* { dg-warning "warning: large integer implicitly truncated to unsigned type" } */
+  fuc (SCHAR_MIN);
+  fuc (SCHAR_MIN - 1); /* { dg-warning "warning: large integer implicitly truncated to unsigned type" } */
+  fuc (-UCHAR_MAX); /* { dg-warning "warning: large integer implicitly truncated to unsigned type" } */
+}
+
+void fui (unsigned int);
+void fsi (signed int);
+
+int si;
+unsigned ui;
+
+void
+h2i (int x)
+{
+  /* For some reason, we only give certain warnings for implicit
+     conversions among values of the same precision with -Wconversion,
+     while we don't give others at all.  */
+  fsi ((unsigned)INT_MAX + 1);
+  si = (unsigned)INT_MAX + 1;
+  si = x ? (unsigned)INT_MAX + 1 : 1;
+  fsi ((unsigned)INT_MAX + 2);
+  si = (unsigned)INT_MAX + 2;
+  si = x ? (unsigned)INT_MAX + 2 : 1;
+  fsi (UINT_MAX);
+  si = UINT_MAX;
+  fui (-1);
+  ui = -1;
+  ui = x ? -1 : 1U;
+  fui (INT_MIN);
+  ui = INT_MIN;
+  ui = x ? INT_MIN : 1U;
+}
Index: gcc/testsuite/g++.dg/warn/overflow-warn-5.C
===================================================================
--- gcc/testsuite/g++.dg/warn/overflow-warn-5.C	(revision 0)
+++ gcc/testsuite/g++.dg/warn/overflow-warn-5.C	(revision 0)
@@ -0,0 +1,7 @@
+/* PR c/27273 */
+/* { dg-do compile } */
+/* { dg-options "-Woverflow" } */
+
+unsigned char rx_async(unsigned char p) {
+    return p & 512; /* { dg-warning "overflow in implicit constant conversion" } */
+}
Index: gcc/testsuite/g++.dg/warn/overflow-warn-6.C
===================================================================
--- gcc/testsuite/g++.dg/warn/overflow-warn-6.C	(revision 0)
+++ gcc/testsuite/g++.dg/warn/overflow-warn-6.C	(revision 0)
@@ -0,0 +1,18 @@
+/* Test non-constant operands in overflowed expressions.  */
+/* { dg-do compile } */
+/* { dg-options "-Woverflow" } */
+
+#include <limits.h>
+
+int 
+h1 (int x)
+{
+  return x * (0 * (INT_MAX + 1)); /* { dg-warning "warning: integer overflow in expression" } */
+}
+
+int 
+h2 (int x)
+{
+  return ((INT_MAX + 1) * 0) * x; /* { dg-warning "warning: integer overflow in expression" } */
+}
+
Index: gcc/testsuite/g++.dg/warn/Woverflow-1.C
===================================================================
--- gcc/testsuite/g++.dg/warn/Woverflow-1.C	(revision 0)
+++ gcc/testsuite/g++.dg/warn/Woverflow-1.C	(revision 0)
@@ -0,0 +1,7 @@
+/* { dg-do compile } */
+/* { dg-options "-O2" } */
+
+#include <limits.h>
+
+int foo = INT_MAX + 1;  /* { dg-warning "integer overflow" } */
+
Index: gcc/cp/typeck.c
===================================================================
--- gcc/cp/typeck.c	(revision 120511)
+++ gcc/cp/typeck.c	(working copy)
@@ -3870,6 +3870,12 @@ build_binary_op (enum tree_code code, tr
   result = fold_if_not_in_template (result);
   if (final_type != 0)
     result = cp_convert (final_type, result);
+
+  if (TREE_OVERFLOW_P (result) 
+      && !TREE_OVERFLOW_P (op0) 
+      && !TREE_OVERFLOW_P (op1))
+    overflow_warning (result);
+
   return result;
 }
 \f

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2007-01-07 22:15 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-01-06 17:49 [PATCH] PR c++/28986 failure to diagnose overflow for binary operators Manuel López-Ibáñez
2007-01-06 18:11 ` Gabriel Dos Reis
2007-01-07 21:48   ` Manuel López-Ibáñez
2007-01-07 22:15     ` Gabriel Dos Reis

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).