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 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@+1 {{expression is not an integral constant expression}} 89 case ((uint64_t)(4608 * 1024 * 1024 * i)): 90 return 8; 91 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}} 92 case ((1ULL * ((4608) * ((1024) * (1024))) + 2ULL)): 93 return 9; 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))): 97 return 10; 98 } 99 #endif 100 101 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}} 102 while (4608 * 1024 * 1024); 103 104 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}} 105 while ((uint64_t)(4608 * 1024 * 1024)); 106 107 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}} 108 while (static_cast<uint64_t>(4608 * 1024 * 1024)); 109 110 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}} 111 while ((uint64_t)(4608 * 1024 * 1024)); 112 113 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}} 114 while ((uint64_t)(4608 * 1024 * 1024 * i)); 115 116 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}} 117 while ((1ULL * ((4608) * ((1024) * (1024))) + 2ULL)); 118 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))); 121 122 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}} 123 do { } while (4608 * 1024 * 1024); 124 125 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}} 126 do { } while ((uint64_t)(4608 * 1024 * 1024)); 127 128 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}} 129 do { } while (static_cast<uint64_t>(4608 * 1024 * 1024)); 130 131 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}} 132 do { } while ((uint64_t)(4608 * 1024 * 1024)); 133 134 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}} 135 do { } while ((uint64_t)(4608 * 1024 * 1024 * i)); 136 137 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}} 138 do { } while ((1ULL * ((4608) * ((1024) * (1024))) + 2ULL)); 139 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))); 142 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)); 149 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)))); 156 157 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}} 158 _Complex long long x = 4608 * 1024 * 1024; 159 160 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}} 161 (__real__ x) = 4608 * 1024 * 1024; 162 163 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}} 164 (__imag__ x) = 4608 * 1024 * 1024; 165 166 // expected-warning@+2 {{overflow in expression; result is 536870912 with type 'int'}} 167 uint64_t a[10]; 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}} 172 #endif 173 174 // expected-warning@+1 2{{overflow in expression; result is 536870912 with type 'int'}} 175 return ((4608 * 1024 * 1024) + ((uint64_t)(4608 * 1024 * 1024))); 176 } 177 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); 181 182 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}} 183 uint64_t x = f0(4608 * 1024 * 1024); 184 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); 188 189 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}} 190 (void)f2(0, f0(4608 * 1024 * 1024)); 191 } 192 193 // Tests that ensure that evaluation-for-overflow of random expressions doesn't 194 // crash. 195 namespace EvaluationCrashes { 196 namespace VirtualCallWithVbase { 197 struct A {}; 198 struct B : virtual A { 199 virtual bool f(const void *, int); 200 }; 201 struct C : B { 202 bool f(const void *, int); 203 }; 204 int d; 205 bool e(C c) { 206 if (c.f(&d, d)) {} 207 return true; 208 } 209 } 210 } 211 212 namespace GH31643 { 213 void f() { 214 int a = -(1<<31); // expected-warning {{overflow in expression; result is -2147483648 with type 'int'}} 215 } 216 } 217 218 #if __cplusplus >= 201103L 219 namespace GH63629 { 220 typedef long long int64_t; 221 222 template<typename T> 223 class u_ptr { 224 T *ptr; 225 public: 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; } 231 }; 232 233 u_ptr<bool> Wrap(int64_t x) { 234 return nullptr; 235 } 236 237 int64_t Pass(int64_t x) { return x; } 238 239 int m() { 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'}} 242 return 0; 243 } 244 } 245 #endif 246