Debian Codegraph @

gcc/testsuite/g++.dg/warn/overflow-warn-1.C

raw
1 /* Test for diagnostics for constant overflow. */
2 /* Origin: Joseph Myers <[email protected]> */
3 /* { dg-do compile } */
4 /* { dg-options "-fpermissive" } */
5
6 #include <limits.h>
7
8 enum e {
9 E0 = INT_MAX,
10 /* Unsigned overflow wraps around. */
11 E1 = UINT_MAX + 1,
12 /* Overflow in an unevaluated part of an expression is OK (example
13 in the standard). */
14 E2 = 2 || 1 / 0, /* { dg-bogus "warning: division by zero" "" { xfail *-*-* } } */
15 E3 = 1 / 0, /* { dg-warning "division by zero" } */
16 /* { dg-error "enumerator value for 'E3' is not an integer constant|not a constant.expression" "enum error" { target *-*-* } .-1 } */
17 /* But as in DR#031, the 1/0 in an evaluated subexpression means the
18 whole expression violates the constraints. */
19 E4 = 0 * (1 / 0), /* { dg-warning "division by zero" } */
20 /* { dg-error "enumerator value for 'E4' is not an integer constant" "enum error" { target c++ } .-1 } */
21 /* { dg-error "division by zero is not a constant.expression" "division" { target c++11 } .-2 } */
22 E5 = INT_MAX + 1, /* { dg-warning "integer overflow in expression" } */
23 /* { dg-warning "overflow in constant expression" "constant" { target *-*-* } .-1 } */
24 /* Again, overflow in evaluated subexpression. */
25 E6 = 0 * (INT_MAX + 1), /* { dg-warning "integer overflow in expression" } */
26 /* { dg-warning "overflow in constant expression" "constant" { target *-*-* } .-1 } */
27 /* A cast does not constitute overflow in conversion. */
28 E7 = (char) INT_MAX
29 };
30
31 struct s {
32 int a;
33 int : 0 * (1 / 0); /* { dg-warning "division by zero" } */
34 /* { dg-error "division by zero is not a constant.expression" "division" { target c++11 } .-1 } */
35 /* { dg-error "width not an integer constant" "bit.field" { target c++ } .-2 } */
36 /* { dg-error "is not a constant expression" "division" { target c++ } .-3 } */
37 int : 0 * (INT_MAX + 1); /* { dg-warning "integer overflow in expression" } */
38 /* { dg-warning "overflow in constant expression" "constant" { target *-*-* } .-1 } */
39 };
40
41 void
42 f (void)
43 {
44 /* This expression is not required to be a constant expression, so
45 it should just involve undefined behavior at runtime. */
46 int c = INT_MAX + 1; /* { dg-warning "integer overflow in expression" } */
47 }
48
49 /* This expression is neither required to be constant. */
50 static int sc = INT_MAX + 1; /* { dg-warning "integer overflow in expression" } */
51
52
53 // Test for overflow in null pointer constant.
54 void *n = 0;
55 /* The first two of these involve overflow, so are not null pointer
56 constants. The third has the overflow in an unevaluated
57 subexpression, so is a null pointer constant. */
58 void *p = 0 * (INT_MAX + 1); /* { dg-warning "integer overflow in expression" } */
59 /* { dg-warning "invalid conversion from 'int' to 'void" "null" { target *-*-* } .-1 } */
60
61 void *q = 0 * (1 / 0);
62 /* { dg-warning "division by zero" "" { target *-*-* } .-1 } */
63 /* { dg-error "invalid conversion from 'int' to 'void*'" "null" { xfail *-*-* } .-2 } */
64 /* { dg-warning "invalid conversion from" "convert" { target *-*-* } .-3 } */
65
66 void *r = (1 ? 0 : INT_MAX+1);
67 /* { dg-bogus "integer overflow in expression" "" { xfail *-*-* } .-1 } */
68 /* { dg-warning "invalid conversion from" "convert" { target c++11 } .-2 } */
69
70 void
71 g (int i)
72 {
73 switch (i)
74 {
75 case 0 * (1/0):
76 /* { dg-warning "division by zero" "" { target *-*-* } .-1 } */
77 /* { dg-error "division by zero is not a constant expression" "division" { target c++11 } .-2 } */
78 /* { dg-error "is not a constant expression" "const" { target *-*-* } .-3 } */
79 ;
80 case 1 + 0 * (INT_MAX + 1): /* { dg-warning "integer overflow in expression" } */
81 /* { dg-warning "overflow in constant expression" "constant" { target *-*-* } .-1 } */
82 ;
83 }
84 }
85
86 int
87 h (void)
88 {
89 return INT_MAX + 1; /* { dg-warning "integer overflow in expression" } */
90 }
91
92 int
93 h1 (void)
94 {
95 return INT_MAX + 1 - INT_MAX; /* { dg-warning "integer overflow in expression" } */
96 }
97
98 void fuc (unsigned char);
99 void fsc (signed char);
100
101 void
102 h2 (void)
103 {
104 fsc (SCHAR_MAX + 1);
105 fsc (SCHAR_MIN - 1); /* { dg-warning "overflow in conversion from .int. to .signed char. changes value" } */
106 fsc (UCHAR_MAX);
107 fsc (UCHAR_MAX + 1); /* { dg-warning "overflow in conversion from .int. to .signed char. changes value" } */
108 fuc (-1);
109 fuc (UCHAR_MAX + 1); /* { dg-warning "unsigned conversion from .int. to .unsigned char. changes value" } */
110 fuc (SCHAR_MIN);
111 fuc (SCHAR_MIN - 1); /* { dg-warning "unsigned conversion from .int. to .unsigned char. changes value" } */
112 fuc (-UCHAR_MAX); /* { dg-warning "unsigned conversion from .int. to .unsigned char. changes value" } */
113 }
114
115 void fui (unsigned int);
116 void fsi (signed int);
117
118 int si;
119 unsigned ui;
120
121 void
122 h2i (int x)
123 {
124 /* For some reason, we only give certain warnings for implicit
125 conversions among values of the same precision with -Wconversion,
126 while we don't give others at all. */
127 fsi ((unsigned)INT_MAX + 1);
128 si = (unsigned)INT_MAX + 1;
129 si = x ? (unsigned)INT_MAX + 1 : 1;
130 fsi ((unsigned)INT_MAX + 2);
131 si = (unsigned)INT_MAX + 2;
132 si = x ? (unsigned)INT_MAX + 2 : 1;
133 fsi (UINT_MAX);
134 si = UINT_MAX;
135 fui (-1);
136 ui = -1;
137 ui = x ? -1 : 1U;
138 fui (INT_MIN);
139 ui = INT_MIN;
140 ui = x ? INT_MIN : 1U;
141 }