summaryrefslogtreecommitdiff
path: root/gcc/testsuite/gcc.dg/overflow-warn-4.c
diff options
context:
space:
mode:
authorupstream source tree <ports@midipix.org>2015-03-15 20:14:05 -0400
committerupstream source tree <ports@midipix.org>2015-03-15 20:14:05 -0400
commit554fd8c5195424bdbcabf5de30fdc183aba391bd (patch)
tree976dc5ab7fddf506dadce60ae936f43f58787092 /gcc/testsuite/gcc.dg/overflow-warn-4.c
downloadcbb-gcc-4.6.4-554fd8c5195424bdbcabf5de30fdc183aba391bd.tar.bz2
cbb-gcc-4.6.4-554fd8c5195424bdbcabf5de30fdc183aba391bd.tar.xz
obtained gcc-4.6.4.tar.bz2 from upstream website;upstream
verified gcc-4.6.4.tar.bz2.sig; imported gcc-4.6.4 source tree from verified upstream tarball. downloading a git-generated archive based on the 'upstream' tag should provide you with a source tree that is binary identical to the one extracted from the above tarball. if you have obtained the source via the command 'git clone', however, do note that line-endings of files in your working directory might differ from line-endings of the respective files in the upstream repository.
Diffstat (limited to 'gcc/testsuite/gcc.dg/overflow-warn-4.c')
-rw-r--r--gcc/testsuite/gcc.dg/overflow-warn-4.c131
1 files changed, 131 insertions, 0 deletions
diff --git a/gcc/testsuite/gcc.dg/overflow-warn-4.c b/gcc/testsuite/gcc.dg/overflow-warn-4.c
new file mode 100644
index 000000000..464533a36
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/overflow-warn-4.c
@@ -0,0 +1,131 @@
+/* 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" } */
+
+#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 "division by zero" } */
+ /* { dg-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 "division by zero" } */
+ /* { dg-error "enumerator value for 'E4' is not an integer constant" "enum error" { target *-*-* } 19 } */
+ E5 = INT_MAX + 1, /* { dg-warning "integer overflow in expression" } */
+ /* { dg-error "overflow in constant expression" "constant" { target *-*-* } 21 } */
+ /* Again, overflow in evaluated subexpression. */
+ E6 = 0 * (INT_MAX + 1), /* { dg-warning "integer overflow in expression" } */
+ /* { dg-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 "division by zero" } */
+ /* { dg-error "not an integer constant" "integer constant" { target *-*-* } 32 } */
+ int : 0 * (INT_MAX + 1); /* { dg-warning "integer overflow in expression" } */
+ /* { dg-error "overflow in constant expression" "constant" { target *-*-* } 34 } */
+};
+
+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 "integer overflow in expression" } */
+
+}
+
+/* But this expression does need to be constant. */
+static int sc = INT_MAX + 1; /* { dg-warning "integer overflow in expression" } */
+/* { dg-error "overflow in constant expression" "constant" { target *-*-* } 48 } */
+
+/* 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 "integer overflow in expression" } */
+/* { dg-error "overflow in constant expression" "constant" { target *-*-* } 54 } */
+/* { dg-error "initialization makes pointer from integer without a cast" "null" { target *-*-* } 54 } */
+void *q = 0 * (1 / 0); /* { dg-warning "division by zero" } */
+/* { dg-error "initializer element is not computable at load time" "constant" { target *-*-* } 57 } */
+/* { dg-error "initialization makes pointer from integer without a cast" "null" { target *-*-* } 57 } */
+void *r = (1 ? 0 : INT_MAX+1);
+
+void
+g (int i)
+{
+ switch (i)
+ {
+ case 0 * (1/0): /* { dg-warning "division by zero" } */
+ /* { dg-error "case label does not reduce to an integer constant" "constant" { target *-*-* } 67 } */
+ ;
+ case 1 + 0 * (INT_MAX + 1): /* { dg-warning "integer overflow in expression" } */
+ /* { dg-error "overflow in constant expression" "constant" { target *-*-* } 70 } */
+ ;
+ }
+}
+
+int
+h (void)
+{
+ return INT_MAX + 1; /* { dg-warning "integer overflow in expression" } */
+}
+
+int
+h1 (void)
+{
+ return INT_MAX + 1 - INT_MAX; /* { dg-warning "integer overflow in expression" } */
+}
+
+void fuc (unsigned char);
+void fsc (signed char);
+
+void
+h2 (void)
+{
+ fsc (SCHAR_MAX + 1); /* { dg-warning "overflow in implicit constant conversion" } */
+ fsc (SCHAR_MIN - 1); /* { dg-warning "overflow in implicit constant conversion" } */
+ fsc (UCHAR_MAX); /* { dg-warning "overflow in implicit constant conversion" } */
+ fsc (UCHAR_MAX + 1); /* { dg-warning "overflow in implicit constant conversion" } */
+ fuc (-1);
+ fuc (UCHAR_MAX + 1); /* { dg-warning "large integer implicitly truncated to unsigned type" } */
+ fuc (SCHAR_MIN);
+ fuc (SCHAR_MIN - 1); /* { dg-warning "large integer implicitly truncated to unsigned type" } */
+ fuc (-UCHAR_MAX); /* { dg-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;
+}