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 typedef unsigned long long uint64_t;
5 typedef unsigned int uint32_t;
6 
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];
11 
12 uint64_t f0(uint64_t);
13 uint64_t f1(uint64_t, uint32_t);
14 uint64_t f2(uint64_t, ...);
15 
16 static const uint64_t overflow = 1 * 4608 * 1024 * 1024; // expected-warning {{overflow in expression; result is 536870912 with type 'int'}}
17 
check_integer_overflows(int i)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));
31 
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;
36 
37 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
38   overflow += overflow2 = overflow3 = static_cast<uint64_t>(4608 * 1024 * 1024);
39 
40   uint64_t not_overflow = 4608 * 1024 * 1024ULL;
41   uint64_t not_overflow2 = (1ULL * ((uint64_t)(4608) * (1024 * 1024)) + 2ULL);
42 
43 // expected-warning@+1 2{{overflow in expression; result is 536870912 with type 'int'}}
44   overflow = 4608 * 1024 * 1024 ?  4608 * 1024 * 1024 : 0;
45 
46 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
47   overflow =  0 ? 0 : 4608 * 1024 * 1024;
48 
49 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
50   if (4608 * 1024 * 1024)
51     return 0;
52 
53 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
54   if ((uint64_t)(4608 * 1024 * 1024))
55     return 1;
56 
57 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
58   if (static_cast<uint64_t>(4608 * 1024 * 1024))
59     return 1;
60 
61 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
62   if ((uint64_t)(4608 * 1024 * 1024))
63     return 2;
64 
65 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
66   if ((uint64_t)(4608 * 1024 * 1024 * i))
67     return 3;
68 
69 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
70   if ((1ULL * ((4608) * ((1024) * (1024))) + 2ULL))
71     return 4;
72 
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)))
75     return 5;
76 
77 #if __cplusplus < 201103L
78   switch (i) {
79 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
80   case 4608 * 1024 * 1024:
81     return 6;
82 // expected-warning@+1 {{overflow in expression; result is 537919488 with type 'int'}}
83   case (uint64_t)(4609 * 1024 * 1024):
84     return 7;
85 // expected-warning@+1 {{overflow in expression; result is 537919488 with type 'int'}}
86   case 1 + static_cast<uint64_t>(4609 * 1024 * 1024):
87     return 7;
88 // expected-error@+2 {{expression is not an integral constant expression}}
89 // expected-note@+1 {{read of non-const variable 'i' is not allowed in a constant expression}}
90   case ((uint64_t)(4608 * 1024 * 1024 * i)):
91     return 8;
92 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
93   case ((1ULL * ((4608) * ((1024) * (1024))) + 2ULL)):
94     return 9;
95 // expected-warning@+2 2{{overflow in expression; result is 536870912 with type 'int'}}
96 // expected-warning@+1 {{overflow converting case value to switch condition type (288230376151711744 to 0)}}
97   case ((uint64_t)((uint64_t)(4608 * 1024 * 1024) * (uint64_t)(4608 * 1024 * 1024))):
98     return 10;
99   }
100 #endif
101 
102 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
103   while (4608 * 1024 * 1024);
104 
105 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
106   while ((uint64_t)(4608 * 1024 * 1024));
107 
108 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
109   while (static_cast<uint64_t>(4608 * 1024 * 1024));
110 
111 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
112   while ((uint64_t)(4608 * 1024 * 1024));
113 
114 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
115   while ((uint64_t)(4608 * 1024 * 1024 * i));
116 
117 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
118   while ((1ULL * ((4608) * ((1024) * (1024))) + 2ULL));
119 
120 // expected-warning@+1 2{{overflow in expression; result is 536870912 with type 'int'}}
121   while ((uint64_t)((uint64_t)(4608 * 1024 * 1024) * (uint64_t)(4608 * 1024 * 1024)));
122 
123 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
124   do { } while (4608 * 1024 * 1024);
125 
126 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
127   do { } while ((uint64_t)(4608 * 1024 * 1024));
128 
129 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
130   do { } while (static_cast<uint64_t>(4608 * 1024 * 1024));
131 
132 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
133   do { } while ((uint64_t)(4608 * 1024 * 1024));
134 
135 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
136   do { } while ((uint64_t)(4608 * 1024 * 1024 * i));
137 
138 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
139   do { } while ((1ULL * ((4608) * ((1024) * (1024))) + 2ULL));
140 
141 // expected-warning@+1 2{{overflow in expression; result is 536870912 with type 'int'}}
142   do { } while ((uint64_t)((uint64_t)(4608 * 1024 * 1024) * (uint64_t)(4608 * 1024 * 1024)));
143 
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 // expected-warning@+3 {{overflow in expression; result is 536870912 with type 'int'}}
147   for (uint64_t i = 4608 * 1024 * 1024;
148        (uint64_t)(4608 * 1024 * 1024);
149        i += (uint64_t)(4608 * 1024 * 1024 * i));
150 
151 // expected-warning@+3 {{overflow in expression; result is 536870912 with type 'int'}}
152 // expected-warning@+3 2{{overflow in expression; result is 536870912 with type 'int'}}
153 // expected-warning@+3 2{{overflow in expression; result is 536870912 with type 'int'}}
154   for (uint64_t i = (1ULL * ((4608) * ((1024) * (1024))) + 2ULL);
155        ((uint64_t)((uint64_t)(4608 * 1024 * 1024) * (uint64_t)(4608 * 1024 * 1024)));
156        i = ((4608 * 1024 * 1024) + ((uint64_t)(4608 * 1024 * 1024))));
157 
158 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
159   _Complex long long x = 4608 * 1024 * 1024;
160 
161 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
162   (__real__ x) = 4608 * 1024 * 1024;
163 
164 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
165   (__imag__ x) = 4608 * 1024 * 1024;
166 
167 // expected-warning@+2 {{overflow in expression; result is 536870912 with type 'int'}}
168   uint64_t a[10];
169   a[4608 * 1024 * 1024] = 1;
170 #if __cplusplus < 201103L
171 // expected-warning@-2 {{array index 536870912 is past the end of the array (which contains 10 elements)}}
172 // expected-note@-4 {{array 'a' declared here}}
173 #endif
174 
175 // expected-warning@+1 2{{overflow in expression; result is 536870912 with type 'int'}}
176   return ((4608 * 1024 * 1024) + ((uint64_t)(4608 * 1024 * 1024)));
177 }
178 
check_integer_overflows_in_function_calls()179 void check_integer_overflows_in_function_calls() {
180 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
181   (void)f0(4608 * 1024 * 1024);
182 
183 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
184   uint64_t x = f0(4608 * 1024 * 1024);
185 
186 // expected-warning@+2 {{overflow in expression; result is 536870912 with type 'int'}}
187   uint64_t (*f0_ptr)(uint64_t) = &f0;
188   (void)(*f0_ptr)(4608 * 1024 * 1024);
189 
190 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
191   (void)f2(0, f0(4608 * 1024 * 1024));
192 }
193 
194 // Tests that ensure that evaluation-for-overflow of random expressions doesn't
195 // crash.
196 namespace EvaluationCrashes {
197   namespace VirtualCallWithVbase {
198     struct A {};
199     struct B : virtual A {
200       virtual bool f(const void *, int);
201     };
202     struct C : B {
203       bool f(const void *, int);
204     };
205     int d;
e(C c)206     bool e(C c) {
207       if (c.f(&d, d)) {}
208       return true;
209     }
210   }
211 }
212