1 // RUN: %clang_cc1 %s -verify -fsyntax-only -std=gnu++98 -triple x86_64-pc-linux-gnu
2 // RUN: %clang_cc1 %s -verify -fsyntax-only -std=gnu++2a -triple x86_64-pc-linux-gnu
4 typedef unsigned long long uint64_t;
5 typedef unsigned int uint32_t;
7 // Check integer sizes.
8 int array64
[sizeof(uint64_t) == 8 ? 1 : -1];
9 int array32
[sizeof(uint32_t) == 4 ? 1 : -1];
10 int arrayint
[sizeof(int) < sizeof(uint64_t) ? 1 : -1];
12 uint64_t f0(uint64_t);
13 uint64_t f1(uint64_t, uint32_t);
14 uint64_t f2(uint64_t, ...);
16 static const uint64_t overflow
= 1 * 4608 * 1024 * 1024; // expected-warning {{overflow in expression; result is 536870912 with type 'int'}}
18 uint64_t check_integer_overflows(int i
) { //expected-note 0+{{declared here}}
19 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
20 uint64_t overflow
= 4608 * 1024 * 1024,
21 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
22 overflow2
= (uint64_t)(4608 * 1024 * 1024),
23 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
24 overflow3
= (uint64_t)(4608 * 1024 * 1024 * i
),
25 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
26 overflow4
= (1ULL * ((4608) * ((1024) * (1024))) + 2ULL),
27 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
28 overflow5
= static_cast<uint64_t>(4608 * 1024 * 1024),
29 // expected-warning@+1 2{{overflow in expression; result is 536870912 with type 'int'}}
30 multi_overflow
= (uint64_t)((uint64_t)(4608 * 1024 * 1024) * (uint64_t)(4608 * 1024 * 1024));
32 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
33 overflow
+= overflow2
= overflow3
= (uint64_t)(4608 * 1024 * 1024);
34 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
35 overflow
+= overflow2
= overflow3
= 4608 * 1024 * 1024;
37 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
38 overflow
+= overflow2
= overflow3
= static_cast<uint64_t>(4608 * 1024 * 1024);
40 uint64_t not_overflow
= 4608 * 1024 * 1024ULL;
41 uint64_t not_overflow2
= (1ULL * ((uint64_t)(4608) * (1024 * 1024)) + 2ULL);
43 // expected-warning@+1 2{{overflow in expression; result is 536870912 with type 'int'}}
44 overflow
= 4608 * 1024 * 1024 ? 4608 * 1024 * 1024 : 0;
46 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
47 overflow
= 0 ? 0 : 4608 * 1024 * 1024;
49 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
50 if (4608 * 1024 * 1024)
53 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
54 if ((uint64_t)(4608 * 1024 * 1024))
57 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
58 if (static_cast<uint64_t>(4608 * 1024 * 1024))
61 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
62 if ((uint64_t)(4608 * 1024 * 1024))
65 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
66 if ((uint64_t)(4608 * 1024 * 1024 * i
))
69 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
70 if ((1ULL * ((4608) * ((1024) * (1024))) + 2ULL))
73 // expected-warning@+1 2{{overflow in expression; result is 536870912 with type 'int'}}
74 if ((uint64_t)((uint64_t)(4608 * 1024 * 1024) * (uint64_t)(4608 * 1024 * 1024)))
77 #if __cplusplus < 201103L
79 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
80 case 4608 * 1024 * 1024:
82 // expected-warning@+1 {{overflow in expression; result is 537919488 with type 'int'}}
83 case (uint64_t)(4609 * 1024 * 1024):
85 // expected-warning@+1 {{overflow in expression; result is 537919488 with type 'int'}}
86 case 1 + static_cast<uint64_t>(4609 * 1024 * 1024):
88 // expected-error@+1 {{expression is not an integral constant expression}}
89 case ((uint64_t)(4608 * 1024 * 1024 * i
)):
91 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
92 case ((1ULL * ((4608) * ((1024) * (1024))) + 2ULL)):
94 // expected-warning@+2 2{{overflow in expression; result is 536870912 with type 'int'}}
95 // expected-warning@+1 {{overflow converting case value to switch condition type (288230376151711744 to 0)}}
96 case ((uint64_t)((uint64_t)(4608 * 1024 * 1024) * (uint64_t)(4608 * 1024 * 1024))):
101 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
102 while (4608 * 1024 * 1024);
104 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
105 while ((uint64_t)(4608 * 1024 * 1024));
107 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
108 while (static_cast<uint64_t>(4608 * 1024 * 1024));
110 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
111 while ((uint64_t)(4608 * 1024 * 1024));
113 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
114 while ((uint64_t)(4608 * 1024 * 1024 * i
));
116 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
117 while ((1ULL * ((4608) * ((1024) * (1024))) + 2ULL));
119 // expected-warning@+1 2{{overflow in expression; result is 536870912 with type 'int'}}
120 while ((uint64_t)((uint64_t)(4608 * 1024 * 1024) * (uint64_t)(4608 * 1024 * 1024)));
122 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
123 do { } while (4608 * 1024 * 1024);
125 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
126 do { } while ((uint64_t)(4608 * 1024 * 1024));
128 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
129 do { } while (static_cast<uint64_t>(4608 * 1024 * 1024));
131 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
132 do { } while ((uint64_t)(4608 * 1024 * 1024));
134 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
135 do { } while ((uint64_t)(4608 * 1024 * 1024 * i
));
137 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
138 do { } while ((1ULL * ((4608) * ((1024) * (1024))) + 2ULL));
140 // expected-warning@+1 2{{overflow in expression; result is 536870912 with type 'int'}}
141 do { } while ((uint64_t)((uint64_t)(4608 * 1024 * 1024) * (uint64_t)(4608 * 1024 * 1024)));
143 // expected-warning@+3 {{overflow in expression; result is 536870912 with type 'int'}}
144 // expected-warning@+3 {{overflow in expression; result is 536870912 with type 'int'}}
145 // expected-warning@+3 {{overflow in expression; result is 536870912 with type 'int'}}
146 for (uint64_t i
= 4608 * 1024 * 1024;
147 (uint64_t)(4608 * 1024 * 1024);
148 i
+= (uint64_t)(4608 * 1024 * 1024 * i
));
150 // expected-warning@+3 {{overflow in expression; result is 536870912 with type 'int'}}
151 // expected-warning@+3 2{{overflow in expression; result is 536870912 with type 'int'}}
152 // expected-warning@+3 2{{overflow in expression; result is 536870912 with type 'int'}}
153 for (uint64_t i
= (1ULL * ((4608) * ((1024) * (1024))) + 2ULL);
154 ((uint64_t)((uint64_t)(4608 * 1024 * 1024) * (uint64_t)(4608 * 1024 * 1024)));
155 i
= ((4608 * 1024 * 1024) + ((uint64_t)(4608 * 1024 * 1024))));
157 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
158 _Complex
long long x
= 4608 * 1024 * 1024;
160 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
161 (__real__ x
) = 4608 * 1024 * 1024;
163 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
164 (__imag__ x
) = 4608 * 1024 * 1024;
166 // expected-warning@+2 {{overflow in expression; result is 536870912 with type 'int'}}
168 a
[4608 * 1024 * 1024] = 1;
169 #if __cplusplus < 201103L
170 // expected-warning@-2 {{array index 536870912 is past the end of the array (that has type 'uint64_t[10]' (aka 'unsigned long long[10]'))}}
171 // expected-note@-4 {{array 'a' declared here}}
174 // expected-warning@+1 2{{overflow in expression; result is 536870912 with type 'int'}}
175 return ((4608 * 1024 * 1024) + ((uint64_t)(4608 * 1024 * 1024)));
178 void check_integer_overflows_in_function_calls() {
179 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
180 (void)f0(4608 * 1024 * 1024);
182 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
183 uint64_t x
= f0(4608 * 1024 * 1024);
185 // expected-warning@+2 {{overflow in expression; result is 536870912 with type 'int'}}
186 uint64_t (*f0_ptr
)(uint64_t) = &f0
;
187 (void)(*f0_ptr
)(4608 * 1024 * 1024);
189 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
190 (void)f2(0, f0(4608 * 1024 * 1024));
193 // Tests that ensure that evaluation-for-overflow of random expressions doesn't
195 namespace EvaluationCrashes
{
196 namespace VirtualCallWithVbase
{
198 struct B
: virtual A
{
199 virtual bool f(const void *, int);
202 bool f(const void *, int);
214 int a
= -(1<<31); // expected-warning {{overflow in expression; result is -2147483648 with type 'int'}}
218 #if __cplusplus >= 201103L
220 typedef long long int64_t;
226 u_ptr(const u_ptr
&) = delete;
227 u_ptr
&operator=(const u_ptr
&) = delete;
228 u_ptr(u_ptr
&&other
) : ptr(other
.ptr
) { other
.ptr
= 0; }
229 u_ptr(T
*ptr
) : ptr(ptr
) { }
230 ~u_ptr() { delete ptr
; }
233 u_ptr
<bool> Wrap(int64_t x
) {
237 int64_t Pass(int64_t x
) { return x
; }
240 int64_t x
= Pass(30 * 24 * 60 * 59 * 1000); // expected-warning {{overflow in expression; result is -1746167296 with type 'int'}}
241 auto r
= Wrap(Pass(30 * 24 * 60 * 59 * 1000)); // expected-warning {{overflow in expression; result is -1746167296 with type 'int'}}