Debian Codegraph @

clang/test/SemaCXX/integer-overflow.cpp

raw
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
3
4 // RUN: %clang_cc1 %s -verify -fsyntax-only -std=gnu++98 -triple x86_64-pc-linux-gnu -fexperimental-new-constant-interpreter
5 // RUN: %clang_cc1 %s -verify -fsyntax-only -std=gnu++2a -triple x86_64-pc-linux-gnu -fexperimental-new-constant-interpreter
6
7
8 typedef unsigned long long uint64_t;
9 typedef unsigned int uint32_t;
10
11 // Check integer sizes.
12 int array64[sizeof(uint64_t) == 8 ? 1 : -1];
13 int array32[sizeof(uint32_t) == 4 ? 1 : -1];
14 int arrayint[sizeof(int) < sizeof(uint64_t) ? 1 : -1];
15
16 uint64_t f0(uint64_t);
17 uint64_t f1(uint64_t, uint32_t);
18 uint64_t f2(uint64_t, ...);
19
20 static const uint64_t overflow = 1 * 4608 * 1024 * 1024; // expected-warning {{overflow in expression; result is 536'870'912 with type 'int'}}
21
22 uint64_t check_integer_overflows(int i) { //expected-note 0+{{declared here}}
23 // expected-warning@+1 {{overflow in expression; result is 536'870'912 with type 'int'}}
24 uint64_t overflow = 4608 * 1024 * 1024,
25 // expected-warning@+1 {{overflow in expression; result is 536'870'912 with type 'int'}}
26 overflow2 = (uint64_t)(4608 * 1024 * 1024),
27 // expected-warning@+1 {{overflow in expression; result is 536'870'912 with type 'int'}}
28 overflow3 = (uint64_t)(4608 * 1024 * 1024 * i),
29 // expected-warning@+1 {{overflow in expression; result is 536'870'912 with type 'int'}}
30 overflow4 = (1ULL * ((4608) * ((1024) * (1024))) + 2ULL),
31 // expected-warning@+1 {{overflow in expression; result is 536'870'912 with type 'int'}}
32 overflow5 = static_cast<uint64_t>(4608 * 1024 * 1024),
33 // expected-warning@+1 2{{overflow in expression; result is 536'870'912 with type 'int'}}
34 multi_overflow = (uint64_t)((uint64_t)(4608 * 1024 * 1024) * (uint64_t)(4608 * 1024 * 1024));
35
36 // expected-warning@+1 {{overflow in expression; result is 536'870'912 with type 'int'}}
37 overflow += overflow2 = overflow3 = (uint64_t)(4608 * 1024 * 1024);
38 // expected-warning@+1 {{overflow in expression; result is 536'870'912 with type 'int'}}
39 overflow += overflow2 = overflow3 = 4608 * 1024 * 1024;
40
41 // expected-warning@+1 {{overflow in expression; result is 536'870'912 with type 'int'}}
42 overflow += overflow2 = overflow3 = static_cast<uint64_t>(4608 * 1024 * 1024);
43
44 uint64_t not_overflow = 4608 * 1024 * 1024ULL;
45 uint64_t not_overflow2 = (1ULL * ((uint64_t)(4608) * (1024 * 1024)) + 2ULL);
46
47 // expected-warning@+1 2{{overflow in expression; result is 536'870'912 with type 'int'}}
48 overflow = 4608 * 1024 * 1024 ? 4608 * 1024 * 1024 : 0;
49
50 // expected-warning@+1 {{overflow in expression; result is 536'870'912 with type 'int'}}
51 overflow = 0 ? 0 : 4608 * 1024 * 1024;
52
53 // expected-warning@+1 {{overflow in expression; result is 536'870'912 with type 'int'}}
54 if (4608 * 1024 * 1024)
55 return 0;
56
57 // expected-warning@+1 {{overflow in expression; result is 536'870'912 with type 'int'}}
58 if ((uint64_t)(4608 * 1024 * 1024))
59 return 1;
60
61 // expected-warning@+1 {{overflow in expression; result is 536'870'912 with type 'int'}}
62 if (static_cast<uint64_t>(4608 * 1024 * 1024))
63 return 1;
64
65 // expected-warning@+1 {{overflow in expression; result is 536'870'912 with type 'int'}}
66 if ((uint64_t)(4608 * 1024 * 1024))
67 return 2;
68
69 // expected-warning@+1 {{overflow in expression; result is 536'870'912 with type 'int'}}
70 if ((uint64_t)(4608 * 1024 * 1024 * i))
71 return 3;
72
73 // expected-warning@+1 {{overflow in expression; result is 536'870'912 with type 'int'}}
74 if ((1ULL * ((4608) * ((1024) * (1024))) + 2ULL))
75 return 4;
76
77 // expected-warning@+1 2{{overflow in expression; result is 536'870'912 with type 'int'}}
78 if ((uint64_t)((uint64_t)(4608 * 1024 * 1024) * (uint64_t)(4608 * 1024 * 1024)))
79 return 5;
80
81 #if __cplusplus < 201103L
82 switch (i) {
83 // expected-warning@+1 {{overflow in expression; result is 536'870'912 with type 'int'}}
84 case 4608 * 1024 * 1024:
85 return 6;
86 // expected-warning@+1 {{overflow in expression; result is 537'919'488 with type 'int'}}
87 case (uint64_t)(4609 * 1024 * 1024):
88 return 7;
89 // expected-warning@+1 {{overflow in expression; result is 537'919'488 with type 'int'}}
90 case 1 + static_cast<uint64_t>(4609 * 1024 * 1024):
91 return 7;
92 // expected-error@+1 {{expression is not an integral constant expression}}
93 case ((uint64_t)(4608 * 1024 * 1024 * i)):
94 return 8;
95 // expected-warning@+1 {{overflow in expression; result is 536'870'912 with type 'int'}}
96 case ((1ULL * ((4608) * ((1024) * (1024))) + 2ULL)):
97 return 9;
98 // expected-warning@+2 2{{overflow in expression; result is 536'870'912 with type 'int'}}
99 // expected-warning@+1 {{overflow converting case value to switch condition type (288230376151711744 to 0)}}
100 case ((uint64_t)((uint64_t)(4608 * 1024 * 1024) * (uint64_t)(4608 * 1024 * 1024))):
101 return 10;
102 }
103 #endif
104
105 // expected-warning@+1 {{overflow in expression; result is 536'870'912 with type 'int'}}
106 while (4608 * 1024 * 1024);
107
108 // expected-warning@+1 {{overflow in expression; result is 536'870'912 with type 'int'}}
109 while ((uint64_t)(4608 * 1024 * 1024));
110
111 // expected-warning@+1 {{overflow in expression; result is 536'870'912 with type 'int'}}
112 while (static_cast<uint64_t>(4608 * 1024 * 1024));
113
114 // expected-warning@+1 {{overflow in expression; result is 536'870'912 with type 'int'}}
115 while ((uint64_t)(4608 * 1024 * 1024));
116
117 // expected-warning@+1 {{overflow in expression; result is 536'870'912 with type 'int'}}
118 while ((uint64_t)(4608 * 1024 * 1024 * i));
119
120 // expected-warning@+1 {{overflow in expression; result is 536'870'912 with type 'int'}}
121 while ((1ULL * ((4608) * ((1024) * (1024))) + 2ULL));
122
123 // expected-warning@+1 2{{overflow in expression; result is 536'870'912 with type 'int'}}
124 while ((uint64_t)((uint64_t)(4608 * 1024 * 1024) * (uint64_t)(4608 * 1024 * 1024)));
125
126 // expected-warning@+1 {{overflow in expression; result is 536'870'912 with type 'int'}}
127 do { } while (4608 * 1024 * 1024);
128
129 // expected-warning@+1 {{overflow in expression; result is 536'870'912 with type 'int'}}
130 do { } while ((uint64_t)(4608 * 1024 * 1024));
131
132 // expected-warning@+1 {{overflow in expression; result is 536'870'912 with type 'int'}}
133 do { } while (static_cast<uint64_t>(4608 * 1024 * 1024));
134
135 // expected-warning@+1 {{overflow in expression; result is 536'870'912 with type 'int'}}
136 do { } while ((uint64_t)(4608 * 1024 * 1024));
137
138 // expected-warning@+1 {{overflow in expression; result is 536'870'912 with type 'int'}}
139 do { } while ((uint64_t)(4608 * 1024 * 1024 * i));
140
141 // expected-warning@+1 {{overflow in expression; result is 536'870'912 with type 'int'}}
142 do { } while ((1ULL * ((4608) * ((1024) * (1024))) + 2ULL));
143
144 // expected-warning@+1 2{{overflow in expression; result is 536'870'912 with type 'int'}}
145 do { } while ((uint64_t)((uint64_t)(4608 * 1024 * 1024) * (uint64_t)(4608 * 1024 * 1024)));
146
147 // expected-warning@+3 {{overflow in expression; result is 536'870'912 with type 'int'}}
148 // expected-warning@+3 {{overflow in expression; result is 536'870'912 with type 'int'}}
149 // expected-warning@+3 {{overflow in expression; result is 536'870'912 with type 'int'}}
150 for (uint64_t i = 4608 * 1024 * 1024;
151 (uint64_t)(4608 * 1024 * 1024);
152 i += (uint64_t)(4608 * 1024 * 1024 * i));
153
154 // expected-warning@+3 {{overflow in expression; result is 536'870'912 with type 'int'}}
155 // expected-warning@+3 2{{overflow in expression; result is 536'870'912 with type 'int'}}
156 // expected-warning@+3 2{{overflow in expression; result is 536'870'912 with type 'int'}}
157 for (uint64_t i = (1ULL * ((4608) * ((1024) * (1024))) + 2ULL);
158 ((uint64_t)((uint64_t)(4608 * 1024 * 1024) * (uint64_t)(4608 * 1024 * 1024)));
159 i = ((4608 * 1024 * 1024) + ((uint64_t)(4608 * 1024 * 1024))));
160
161 // expected-warning@+1 {{overflow in expression; result is 536'870'912 with type 'int'}}
162 _Complex long long x = 4608 * 1024 * 1024;
163
164 // expected-warning@+1 {{overflow in expression; result is 536'870'912 with type 'int'}}
165 (__real__ x) = 4608 * 1024 * 1024;
166
167 // expected-warning@+1 {{overflow in expression; result is 536'870'912 with type 'int'}}
168 (__imag__ x) = 4608 * 1024 * 1024;
169
170 // expected-warning@+2 {{overflow in expression; result is 536'870'912 with type 'int'}}
171 uint64_t a[10];
172 a[4608 * 1024 * 1024] = 1;
173 #if __cplusplus < 201103L
174 // expected-warning@-2 {{array index 536'870'912 is past the end of the array (that has type 'uint64_t[10]' (aka 'unsigned long long[10]'))}}
175 // expected-note@-4 {{array 'a' declared here}}
176 #endif
177
178 // expected-warning@+1 2{{overflow in expression; result is 536'870'912 with type 'int'}}
179 return ((4608 * 1024 * 1024) + ((uint64_t)(4608 * 1024 * 1024)));
180 }
181
182 void check_integer_overflows_in_function_calls() {
183 // expected-warning@+1 {{overflow in expression; result is 536'870'912 with type 'int'}}
184 (void)f0(4608 * 1024 * 1024);
185
186 // expected-warning@+1 {{overflow in expression; result is 536'870'912 with type 'int'}}
187 uint64_t x = f0(4608 * 1024 * 1024);
188
189 // expected-warning@+2 {{overflow in expression; result is 536'870'912 with type 'int'}}
190 uint64_t (*f0_ptr)(uint64_t) = &f0;
191 (void)(*f0_ptr)(4608 * 1024 * 1024);
192
193 // expected-warning@+1 {{overflow in expression; result is 536'870'912 with type 'int'}}
194 (void)f2(0, f0(4608 * 1024 * 1024));
195 }
196
197 // Tests that ensure that evaluation-for-overflow of random expressions doesn't
198 // crash.
199 namespace EvaluationCrashes {
200 namespace VirtualCallWithVbase {
201 struct A {};
202 struct B : virtual A {
203 virtual bool f(const void *, int);
204 };
205 struct C : B {
206 bool f(const void *, int);
207 };
208 int d;
209 bool e(C c) {
210 if (c.f(&d, d)) {}
211 return true;
212 }
213 }
214 }
215
216 namespace GH31643 {
217 void f() {
218 int a = -(1<<31); // expected-warning {{overflow in expression; result is -2'147'483'648 with type 'int'}}
219 }
220 }
221
222 #if __cplusplus >= 201103L
223 namespace GH63629 {
224 typedef long long int64_t;
225
226 template<typename T>
227 class u_ptr {
228 T *ptr;
229 public:
230 u_ptr(const u_ptr&) = delete;
231 u_ptr &operator=(const u_ptr&) = delete;
232 u_ptr(u_ptr &&other) : ptr(other.ptr) { other.ptr = 0; }
233 u_ptr(T *ptr) : ptr(ptr) { }
234 ~u_ptr() { delete ptr; }
235 };
236
237 u_ptr<bool> Wrap(int64_t x) {
238 return nullptr;
239 }
240
241 int64_t Pass(int64_t x) { return x; }
242
243 int m() {
244 int64_t x = Pass(30 * 24 * 60 * 59 * 1000); // expected-warning {{overflow in expression; result is -1'746'167'296 with type 'int'}}
245 auto r = Wrap(Pass(30 * 24 * 60 * 59 * 1000)); // expected-warning {{overflow in expression; result is -1'746'167'296 with type 'int'}}
246 return 0;
247 }
248 }
249
250 namespace GH46755 {
251 void f() {
252 struct { int v; } &&r = {512 * 1024 * 1024 * 1024}; // expected-warning {{overflow in expression; result is 0 with type 'int'}}
253 }
254 }
255 #endif