1 // RUN: %clang_cc1 -fsyntax-only -Wall -Wuninitialized -Wno-unused-value -std=c++11 -verify %s 2 3 // definitions for std::move 4 namespace std { 5 inline namespace foo { 6 template <class T> struct remove_reference { typedef T type; }; 7 template <class T> struct remove_reference<T&> { typedef T type; }; 8 template <class T> struct remove_reference<T&&> { typedef T type; }; 9 10 template <class T> typename remove_reference<T>::type&& move(T&& t); 11 } 12 } 13 14 int foo(int x); 15 int bar(int* x); 16 int boo(int& x); 17 int far(const int& x); 18 int moved(int&& x); 19 int &ref(int x); 20 21 // Test self-references within initializers which are guaranteed to be 22 // uninitialized. 23 int a = a; // no-warning: used to signal intended lack of initialization. 24 int b = b + 1; // expected-warning {{variable 'b' is uninitialized when used within its own initialization}} 25 int c = (c + c); // expected-warning 2 {{variable 'c' is uninitialized when used within its own initialization}} 26 int e = static_cast<long>(e) + 1; // expected-warning {{variable 'e' is uninitialized when used within its own initialization}} 27 int f = foo(f); // expected-warning {{variable 'f' is uninitialized when used within its own initialization}} 28 29 // Thes don't warn as they don't require the value. 30 int g = sizeof(g); 31 void* ptr = &ptr; 32 int h = bar(&h); 33 int i = boo(i); 34 int j = far(j); 35 int k = __alignof__(k); 36 37 int l = k ? l : l; // expected-warning 2{{variable 'l' is uninitialized when used within its own initialization}} 38 int m = 1 + (k ? m : m); // expected-warning 2{{variable 'm' is uninitialized when used within its own initialization}} 39 int n = -n; // expected-warning {{variable 'n' is uninitialized when used within its own initialization}} 40 int o = std::move(o); // expected-warning {{variable 'o' is uninitialized when used within its own initialization}} 41 const int p = std::move(p); // expected-warning {{variable 'p' is uninitialized when used within its own initialization}} 42 int q = moved(std::move(q)); // expected-warning {{variable 'q' is uninitialized when used within its own initialization}} 43 int r = std::move((p ? q : (18, r))); // expected-warning {{variable 'r' is uninitialized when used within its own initialization}} 44 int s = r ?: s; // expected-warning {{variable 's' is uninitialized when used within its own initialization}} 45 int t = t ?: s; // expected-warning {{variable 't' is uninitialized when used within its own initialization}} 46 int u = (foo(u), s); // expected-warning {{variable 'u' is uninitialized when used within its own initialization}} 47 int v = (u += v); // expected-warning {{variable 'v' is uninitialized when used within its own initialization}} 48 int w = (w += 10); // expected-warning {{variable 'w' is uninitialized when used within its own initialization}} 49 int x = x++; // expected-warning {{variable 'x' is uninitialized when used within its own initialization}} 50 int y = ((s ? (y, v) : (77, y))++, sizeof(y)); // expected-warning {{variable 'y' is uninitialized when used within its own initialization}} 51 int z = ++ref(z); // expected-warning {{variable 'z' is uninitialized when used within its own initialization}} 52 int aa = (ref(aa) += 10); // expected-warning {{variable 'aa' is uninitialized when used within its own initialization}} 53 int bb = bb ? x : y; // expected-warning {{variable 'bb' is uninitialized when used within its own initialization}} 54 55 void test_stuff () { 56 int a = a; // no-warning: used to signal intended lack of initialization. 57 int b = b + 1; // expected-warning {{variable 'b' is uninitialized when used within its own initialization}} 58 int c = (c + c); // expected-warning {{variable 'c' is uninitialized when used within its own initialization}} 59 int d = ({ d + d ;}); // expected-warning {{variable 'd' is uninitialized when used within its own initialization}} 60 int e = static_cast<long>(e) + 1; // expected-warning {{variable 'e' is uninitialized when used within its own initialization}} 61 int f = foo(f); // expected-warning {{variable 'f' is uninitialized when used within its own initialization}} 62 63 // Thes don't warn as they don't require the value. 64 int g = sizeof(g); 65 void* ptr = &ptr; 66 int h = bar(&h); 67 int i = boo(i); 68 int j = far(j); 69 int k = __alignof__(k); 70 71 int l = k ? l : l; // expected-warning {{variable 'l' is uninitialized when used within its own initialization}} 72 int m = 1 + (k ? m : m); // expected-warning {{'m' is uninitialized when used within its own initialization}} 73 int n = -n; // expected-warning {{variable 'n' is uninitialized when used within its own initialization}} 74 int o = std::move(o); // expected-warning {{variable 'o' is uninitialized when used within its own initialization}} 75 const int p = std::move(p); // expected-warning {{variable 'p' is uninitialized when used within its own initialization}} 76 int q = moved(std::move(q)); // expected-warning {{variable 'q' is uninitialized when used within its own initialization}} 77 int r = std::move((p ? q : (18, r))); // expected-warning {{variable 'r' is uninitialized when used within its own initialization}} 78 int s = r ?: s; // expected-warning {{variable 's' is uninitialized when used within its own initialization}} 79 int t = t ?: s; // expected-warning {{variable 't' is uninitialized when used within its own initialization}} 80 int u = (foo(u), s); // expected-warning {{variable 'u' is uninitialized when used within its own initialization}} 81 int v = (u += v); // expected-warning {{variable 'v' is uninitialized when used within its own initialization}} 82 int w = (w += 10); // expected-warning {{variable 'w' is uninitialized when used within its own initialization}} 83 int x = x++; // expected-warning {{variable 'x' is uninitialized when used within its own initialization}} 84 int y = ((s ? (y, v) : (77, y))++, sizeof(y)); // expected-warning {{variable 'y' is uninitialized when used within its own initialization}} 85 int z = ++ref(z); // expected-warning {{variable 'z' is uninitialized when used within its own initialization}} 86 int aa = (ref(aa) += 10); // expected-warning {{variable 'aa' is uninitialized when used within its own initialization}} 87 int bb = bb ? x : y; // expected-warning {{variable 'bb' is uninitialized when used within its own initialization}} 88 89 90 for (;;) { 91 int a = a; // no-warning: used to signal intended lack of initialization. 92 int b = b + 1; // expected-warning {{variable 'b' is uninitialized when used within its own initialization}} 93 int c = (c + c); // expected-warning {{variable 'c' is uninitialized when used within its own initialization}} 94 int d = ({ d + d ;}); // expected-warning {{variable 'd' is uninitialized when used within its own initialization}} 95 int e = static_cast<long>(e) + 1; // expected-warning {{variable 'e' is uninitialized when used within its own initialization}} 96 int f = foo(f); // expected-warning {{variable 'f' is uninitialized when used within its own initialization}} 97 98 // Thes don't warn as they don't require the value. 99 int g = sizeof(g); 100 void* ptr = &ptr; 101 int h = bar(&h); 102 int i = boo(i); 103 int j = far(j); 104 int k = __alignof__(k); 105 106 int l = k ? l : l; // expected-warning {{variable 'l' is uninitialized when used within its own initialization}} 107 int m = 1 + (k ? m : m); // expected-warning {{'m' is uninitialized when used within its own initialization}} 108 int n = -n; // expected-warning {{variable 'n' is uninitialized when used within its own initialization}} 109 int o = std::move(o); // expected-warning {{variable 'o' is uninitialized when used within its own initialization}} 110 const int p = std::move(p); // expected-warning {{variable 'p' is uninitialized when used within its own initialization}} 111 int q = moved(std::move(q)); // expected-warning {{variable 'q' is uninitialized when used within its own initialization}} 112 int r = std::move((p ? q : (18, r))); // expected-warning {{variable 'r' is uninitialized when used within its own initialization}} 113 int s = r ?: s; // expected-warning {{variable 's' is uninitialized when used within its own initialization}} 114 int t = t ?: s; // expected-warning {{variable 't' is uninitialized when used within its own initialization}} 115 int u = (foo(u), s); // expected-warning {{variable 'u' is uninitialized when used within its own initialization}} 116 int v = (u += v); // expected-warning {{variable 'v' is uninitialized when used within its own initialization}} 117 int w = (w += 10); // expected-warning {{variable 'w' is uninitialized when used within its own initialization}} 118 int x = x++; // expected-warning {{variable 'x' is uninitialized when used within its own initialization}} 119 int y = ((s ? (y, v) : (77, y))++, sizeof(y)); // expected-warning {{variable 'y' is uninitialized when used within its own initialization}} 120 int z = ++ref(z); // expected-warning {{variable 'z' is uninitialized when used within its own initialization}} 121 int aa = (ref(aa) += 10); // expected-warning {{variable 'aa' is uninitialized when used within its own initialization}} 122 int bb = bb ? x : y; // expected-warning {{variable 'bb' is uninitialized when used within its own initialization}} 123 124 } 125 } 126 127 // Also test similar constructs in a field's initializer. 128 struct S { 129 int x; 130 int y; 131 const int z = 5; 132 void *ptr; 133 134 S(bool (*)[1]) : x(x) {} // expected-warning {{field 'x' is uninitialized when used here}} 135 S(bool (*)[2]) : x(x + 1) {} // expected-warning {{field 'x' is uninitialized when used here}} 136 S(bool (*)[3]) : x(x + x) {} // expected-warning 2{{field 'x' is uninitialized when used here}} 137 S(bool (*)[4]) : x(static_cast<long>(x) + 1) {} // expected-warning {{field 'x' is uninitialized when used here}} 138 S(bool (*)[5]) : x(foo(x)) {} // expected-warning {{field 'x' is uninitialized when used here}} 139 140 // These don't actually require the value of x and so shouldn't warn. 141 S(char (*)[1]) : x(sizeof(x)) {} // rdar://8610363 142 S(char (*)[2]) : ptr(&ptr) {} 143 S(char (*)[3]) : x(bar(&x)) {} 144 S(char (*)[4]) : x(boo(x)) {} 145 S(char (*)[5]) : x(far(x)) {} 146 S(char (*)[6]) : x(__alignof__(x)) {} 147 148 S(int (*)[1]) : x(0), y(x ? y : y) {} // expected-warning 2{{field 'y' is uninitialized when used here}} 149 S(int (*)[2]) : x(0), y(1 + (x ? y : y)) {} // expected-warning 2{{field 'y' is uninitialized when used here}} 150 S(int (*)[3]) : x(-x) {} // expected-warning {{field 'x' is uninitialized when used here}} 151 S(int (*)[4]) : x(std::move(x)) {} // expected-warning {{field 'x' is uninitialized when used here}} 152 S(int (*)[5]) : z(std::move(z)) {} // expected-warning {{field 'z' is uninitialized when used here}} 153 S(int (*)[6]) : x(moved(std::move(x))) {} // expected-warning {{field 'x' is uninitialized when used here}} 154 S(int (*)[7]) : x(0), y(std::move((x ? x : (18, y)))) {} // expected-warning {{field 'y' is uninitialized when used here}} 155 S(int (*)[8]) : x(0), y(x ?: y) {} // expected-warning {{field 'y' is uninitialized when used here}} 156 S(int (*)[9]) : x(0), y(y ?: x) {} // expected-warning {{field 'y' is uninitialized when used here}} 157 S(int (*)[10]) : x(0), y((foo(y), x)) {} // expected-warning {{field 'y' is uninitialized when used here}} 158 S(int (*)[11]) : x(0), y(x += y) {} // expected-warning {{field 'y' is uninitialized when used here}} 159 S(int (*)[12]) : x(x += 10) {} // expected-warning {{field 'x' is uninitialized when used here}} 160 S(int (*)[13]) : x(x++) {} // expected-warning {{field 'x' is uninitialized when used here}} 161 S(int (*)[14]) : x(0), y(((x ? (y, x) : (77, y))++, sizeof(y))) {} // expected-warning {{field 'y' is uninitialized when used here}} 162 S(int (*)[15]) : x(++ref(x)) {} // expected-warning {{field 'x' is uninitialized when used here}} 163 S(int (*)[16]) : x((ref(x) += 10)) {} // expected-warning {{field 'x' is uninitialized when used here}} 164 S(int (*)[17]) : x(0), y(y ? x : x) {} // expected-warning {{field 'y' is uninitialized when used here}} 165 }; 166 167 // Test self-references with record types. 168 class A { 169 // Non-POD class. 170 public: 171 enum count { ONE, TWO, THREE }; 172 int num; 173 static int count; 174 int get() const { return num; } 175 int get2() { return num; } 176 void set(int x) { num = x; } 177 static int zero() { return 0; } 178 179 A() {} 180 A(A const &a) {} 181 A(int x) {} 182 A(int *x) {} 183 A(A *a) {} 184 A(A &&a) {} 185 ~A(); 186 }; 187 188 A getA() { return A(); } 189 A getA(int x) { return A(); } 190 A getA(A* a) { return A(); } 191 A getA(A a) { return A(); } 192 A moveA(A&& a) { return A(); } 193 A const_refA(const A& a) { return A(); } 194 195 void setupA(bool x) { 196 A a1; 197 a1.set(a1.get()); 198 A a2(a1.get()); 199 A a3(a1); 200 A a4(&a4); 201 A a5(a5.zero()); 202 A a6(a6.ONE); 203 A a7 = getA(); 204 A a8 = getA(a8.TWO); 205 A a9 = getA(&a9); 206 A a10(a10.count); 207 208 A a11(a11); // expected-warning {{variable 'a11' is uninitialized when used within its own initialization}} 209 A a12(a12.get()); // expected-warning {{variable 'a12' is uninitialized when used within its own initialization}} 210 A a13(a13.num); // expected-warning {{variable 'a13' is uninitialized when used within its own initialization}} 211 A a14 = A(a14); // expected-warning {{variable 'a14' is uninitialized when used within its own initialization}} 212 A a15 = getA(a15.num); // expected-warning {{variable 'a15' is uninitialized when used within its own initialization}} 213 A a16(&a16.num); // expected-warning {{variable 'a16' is uninitialized when used within its own initialization}} 214 A a17(a17.get2()); // expected-warning {{variable 'a17' is uninitialized when used within its own initialization}} 215 A a18 = x ? a18 : a17; // expected-warning {{variable 'a18' is uninitialized when used within its own initialization}} 216 A a19 = getA(x ? a19 : a17); // expected-warning {{variable 'a19' is uninitialized when used within its own initialization}} 217 A a20{a20}; // expected-warning {{variable 'a20' is uninitialized when used within its own initialization}} 218 A a21 = {a21}; // expected-warning {{variable 'a21' is uninitialized when used within its own initialization}} 219 220 // FIXME: Make the local uninitialized warning consistent with the global 221 // uninitialized checking. 222 A *a22 = new A(a22->count); // expected-warning {{variable 'a22' is uninitialized when used within its own initialization}} 223 A *a23 = new A(a23->ONE); // expected-warning {{variable 'a23' is uninitialized when used within its own initialization}} 224 A *a24 = new A(a24->TWO); // expected-warning {{variable 'a24' is uninitialized when used within its own initialization}} 225 A *a25 = new A(a25->zero()); // expected-warning {{variable 'a25' is uninitialized when used within its own initialization}} 226 227 A *a26 = new A(a26->get()); // expected-warning {{variable 'a26' is uninitialized when used within its own initialization}} 228 A *a27 = new A(a27->get2()); // expected-warning {{variable 'a27' is uninitialized when used within its own initialization}} 229 A *a28 = new A(a28->num); // expected-warning {{variable 'a28' is uninitialized when used within its own initialization}} 230 231 const A a29(a29); // expected-warning {{variable 'a29' is uninitialized when used within its own initialization}} 232 const A a30 = a30; // expected-warning {{variable 'a30' is uninitialized when used within its own initialization}} 233 234 A a31 = std::move(a31); // expected-warning {{variable 'a31' is uninitialized when used within its own initialization}} 235 A a32 = moveA(std::move(a32)); // expected-warning {{variable 'a32' is uninitialized when used within its own initialization}} 236 A a33 = A(std::move(a33)); // expected-warning {{variable 'a33' is uninitialized when used within its own initialization}} 237 A a34(std::move(a34)); // expected-warning {{variable 'a34' is uninitialized when used within its own initialization}} 238 A a35 = std::move(x ? a34 : (37, a35)); // expected-warning {{variable 'a35' is uninitialized when used within its own initialization}} 239 240 A a36 = const_refA(a36); 241 A a37(const_refA(a37)); 242 243 A a38({a38}); // expected-warning {{variable 'a38' is uninitialized when used within its own initialization}} 244 A a39 = {a39}; // expected-warning {{variable 'a39' is uninitialized when used within its own initialization}} 245 A a40 = A({a40}); // expected-warning {{variable 'a40' is uninitialized when used within its own initialization}} 246 } 247 248 bool cond; 249 250 A a1; 251 A a2(a1.get()); 252 A a3(a1); 253 A a4(&a4); 254 A a5(a5.zero()); 255 A a6(a6.ONE); 256 A a7 = getA(); 257 A a8 = getA(a8.TWO); 258 A a9 = getA(&a9); 259 A a10(a10.count); 260 261 A a11(a11); // expected-warning {{variable 'a11' is uninitialized when used within its own initialization}} 262 A a12(a12.get()); // expected-warning {{variable 'a12' is uninitialized when used within its own initialization}} 263 A a13(a13.num); // expected-warning {{variable 'a13' is uninitialized when used within its own initialization}} 264 A a14 = A(a14); // expected-warning {{variable 'a14' is uninitialized when used within its own initialization}} 265 A a15 = getA(a15.num); // expected-warning {{variable 'a15' is uninitialized when used within its own initialization}} 266 A a16(&a16.num); // expected-warning {{variable 'a16' is uninitialized when used within its own initialization}} 267 A a17(a17.get2()); // expected-warning {{variable 'a17' is uninitialized when used within its own initialization}} 268 A a18 = cond ? a18 : a17; // expected-warning {{variable 'a18' is uninitialized when used within its own initialization}} 269 A a19 = getA(cond ? a19 : a17); // expected-warning {{variable 'a19' is uninitialized when used within its own initialization}} 270 A a20{a20}; // expected-warning {{variable 'a20' is uninitialized when used within its own initialization}} 271 A a21 = {a21}; // expected-warning {{variable 'a21' is uninitialized when used within its own initialization}} 272 273 A *a22 = new A(a22->count); 274 A *a23 = new A(a23->ONE); 275 A *a24 = new A(a24->TWO); 276 A *a25 = new A(a25->zero()); 277 278 A *a26 = new A(a26->get()); // expected-warning {{variable 'a26' is uninitialized when used within its own initialization}} 279 A *a27 = new A(a27->get2()); // expected-warning {{variable 'a27' is uninitialized when used within its own initialization}} 280 A *a28 = new A(a28->num); // expected-warning {{variable 'a28' is uninitialized when used within its own initialization}} 281 282 const A a29(a29); // expected-warning {{variable 'a29' is uninitialized when used within its own initialization}} 283 const A a30 = a30; // expected-warning {{variable 'a30' is uninitialized when used within its own initialization}} 284 285 A a31 = std::move(a31); // expected-warning {{variable 'a31' is uninitialized when used within its own initialization}} 286 A a32 = moveA(std::move(a32)); // expected-warning {{variable 'a32' is uninitialized when used within its own initialization}} 287 A a33 = A(std::move(a33)); // expected-warning {{variable 'a33' is uninitialized when used within its own initialization}} 288 A a34(std::move(a34)); // expected-warning {{variable 'a34' is uninitialized when used within its own initialization}} 289 A a35 = std::move(x ? a34 : (37, a35)); // expected-warning {{variable 'a35' is uninitialized when used within its own initialization}} 290 291 A a36 = const_refA(a36); 292 A a37(const_refA(a37)); 293 294 A a38({a38}); // expected-warning {{variable 'a38' is uninitialized when used within its own initialization}} 295 A a39 = {a39}; // expected-warning {{variable 'a39' is uninitialized when used within its own initialization}} 296 A a40 = A({a40}); // expected-warning {{variable 'a40' is uninitialized when used within its own initialization}} 297 298 class T { 299 A a, a2; 300 const A c_a; 301 A* ptr_a; 302 303 T() {} 304 T(bool (*)[1]) : a() {} 305 T(bool (*)[2]) : a2(a.get()) {} 306 T(bool (*)[3]) : a2(a) {} 307 T(bool (*)[4]) : a(&a) {} 308 T(bool (*)[5]) : a(a.zero()) {} 309 T(bool (*)[6]) : a(a.ONE) {} 310 T(bool (*)[7]) : a(getA()) {} 311 T(bool (*)[8]) : a2(getA(a.TWO)) {} 312 T(bool (*)[9]) : a(getA(&a)) {} 313 T(bool (*)[10]) : a(a.count) {} 314 315 T(bool (*)[11]) : a(a) {} // expected-warning {{field 'a' is uninitialized when used here}} 316 T(bool (*)[12]) : a(a.get()) {} // expected-warning {{field 'a' is uninitialized when used here}} 317 T(bool (*)[13]) : a(a.num) {} // expected-warning {{field 'a' is uninitialized when used here}} 318 T(bool (*)[14]) : a(A(a)) {} // expected-warning {{field 'a' is uninitialized when used here}} 319 T(bool (*)[15]) : a(getA(a.num)) {} // expected-warning {{field 'a' is uninitialized when used here}} 320 T(bool (*)[16]) : a(&a.num) {} // expected-warning {{field 'a' is uninitialized when used here}} 321 T(bool (*)[17]) : a(a.get2()) {} // expected-warning {{field 'a' is uninitialized when used here}} 322 T(bool (*)[18]) : a2(cond ? a2 : a) {} // expected-warning {{field 'a2' is uninitialized when used here}} 323 T(bool (*)[19]) : a2(cond ? a2 : a) {} // expected-warning {{field 'a2' is uninitialized when used here}} 324 T(bool (*)[20]) : a{a} {} // expected-warning {{field 'a' is uninitialized when used here}} 325 T(bool (*)[21]) : a({a}) {} // expected-warning {{field 'a' is uninitialized when used here}} 326 327 T(bool (*)[22]) : ptr_a(new A(ptr_a->count)) {} 328 T(bool (*)[23]) : ptr_a(new A(ptr_a->ONE)) {} 329 T(bool (*)[24]) : ptr_a(new A(ptr_a->TWO)) {} 330 T(bool (*)[25]) : ptr_a(new A(ptr_a->zero())) {} 331 332 T(bool (*)[26]) : ptr_a(new A(ptr_a->get())) {} // expected-warning {{field 'ptr_a' is uninitialized when used here}} 333 T(bool (*)[27]) : ptr_a(new A(ptr_a->get2())) {} // expected-warning {{field 'ptr_a' is uninitialized when used here}} 334 T(bool (*)[28]) : ptr_a(new A(ptr_a->num)) {} // expected-warning {{field 'ptr_a' is uninitialized when used here}} 335 336 T(bool (*)[29]) : c_a(c_a) {} // expected-warning {{field 'c_a' is uninitialized when used here}} 337 T(bool (*)[30]) : c_a(A(c_a)) {} // expected-warning {{field 'c_a' is uninitialized when used here}} 338 339 T(bool (*)[31]) : a(std::move(a)) {} // expected-warning {{field 'a' is uninitialized when used here}} 340 T(bool (*)[32]) : a(moveA(std::move(a))) {} // expected-warning {{field 'a' is uninitialized when used here}} 341 T(bool (*)[33]) : a(A(std::move(a))) {} // expected-warning {{field 'a' is uninitialized when used here}} 342 T(bool (*)[34]) : a(A(std::move(a))) {} // expected-warning {{field 'a' is uninitialized when used here}} 343 T(bool (*)[35]) : a2(std::move(x ? a : (37, a2))) {} // expected-warning {{field 'a2' is uninitialized when used here}} 344 345 T(bool (*)[36]) : a(const_refA(a)) {} 346 T(bool (*)[37]) : a(A(const_refA(a))) {} 347 348 T(bool (*)[38]) : a({a}) {} // expected-warning {{field 'a' is uninitialized when used here}} 349 T(bool (*)[39]) : a{a} {} // expected-warning {{field 'a' is uninitialized when used here}} 350 T(bool (*)[40]) : a({a}) {} // expected-warning {{field 'a' is uninitialized when used here}} 351 }; 352 353 struct B { 354 // POD struct. 355 int x; 356 int *y; 357 }; 358 359 B getB() { return B(); }; 360 B getB(int x) { return B(); }; 361 B getB(int *x) { return B(); }; 362 B getB(B *b) { return B(); }; 363 B moveB(B &&b) { return B(); }; 364 365 B* getPtrB() { return 0; }; 366 B* getPtrB(int x) { return 0; }; 367 B* getPtrB(int *x) { return 0; }; 368 B* getPtrB(B **b) { return 0; }; 369 370 void setupB(bool x) { 371 B b1; 372 B b2(b1); 373 B b3 = { 5, &b3.x }; 374 B b4 = getB(); 375 B b5 = getB(&b5); 376 B b6 = getB(&b6.x); 377 378 // Silence unused warning 379 (void) b2; 380 (void) b4; 381 382 B b7(b7); // expected-warning {{variable 'b7' is uninitialized when used within its own initialization}} 383 B b8 = getB(b8.x); // expected-warning {{variable 'b8' is uninitialized when used within its own initialization}} 384 B b9 = getB(b9.y); // expected-warning {{variable 'b9' is uninitialized when used within its own initialization}} 385 B b10 = getB(-b10.x); // expected-warning {{variable 'b10' is uninitialized when used within its own initialization}} 386 387 B* b11 = 0; 388 B* b12(b11); 389 B* b13 = getPtrB(); 390 B* b14 = getPtrB(&b14); 391 392 (void) b12; 393 (void) b13; 394 395 B* b15 = getPtrB(b15->x); // expected-warning {{variable 'b15' is uninitialized when used within its own initialization}} 396 B* b16 = getPtrB(b16->y); // expected-warning {{variable 'b16' is uninitialized when used within its own initialization}} 397 398 B b17 = { b17.x = 5, b17.y = 0 }; 399 B b18 = { b18.x + 1, b18.y }; // expected-warning 2{{variable 'b18' is uninitialized when used within its own initialization}} 400 401 const B b19 = b19; // expected-warning {{variable 'b19' is uninitialized when used within its own initialization}} 402 const B b20(b20); // expected-warning {{variable 'b20' is uninitialized when used within its own initialization}} 403 404 B b21 = std::move(b21); // expected-warning {{variable 'b21' is uninitialized when used within its own initialization}} 405 B b22 = moveB(std::move(b22)); // expected-warning {{variable 'b22' is uninitialized when used within its own initialization}} 406 B b23 = B(std::move(b23)); // expected-warning {{variable 'b23' is uninitialized when used within its own initialization}} 407 B b24 = std::move(x ? b23 : (18, b24)); // expected-warning {{variable 'b24' is uninitialized when used within its own initialization}} 408 } 409 410 B b1; 411 B b2(b1); 412 B b3 = { 5, &b3.x }; 413 B b4 = getB(); 414 B b5 = getB(&b5); 415 B b6 = getB(&b6.x); 416 417 B b7(b7); // expected-warning {{variable 'b7' is uninitialized when used within its own initialization}} 418 B b8 = getB(b8.x); // expected-warning {{variable 'b8' is uninitialized when used within its own initialization}} 419 B b9 = getB(b9.y); // expected-warning {{variable 'b9' is uninitialized when used within its own initialization}} 420 B b10 = getB(-b10.x); // expected-warning {{variable 'b10' is uninitialized when used within its own initialization}} 421 422 B* b11 = 0; 423 B* b12(b11); 424 B* b13 = getPtrB(); 425 B* b14 = getPtrB(&b14); 426 427 B* b15 = getPtrB(b15->x); // expected-warning {{variable 'b15' is uninitialized when used within its own initialization}} 428 B* b16 = getPtrB(b16->y); // expected-warning {{variable 'b16' is uninitialized when used within its own initialization}} 429 430 B b17 = { b17.x = 5, b17.y = 0 }; 431 B b18 = { b18.x + 1, b18.y }; // expected-warning 2{{variable 'b18' is uninitialized when used within its own initialization}} 432 433 const B b19 = b19; // expected-warning {{variable 'b19' is uninitialized when used within its own initialization}} 434 const B b20(b20); // expected-warning {{variable 'b20' is uninitialized when used within its own initialization}} 435 436 B b21 = std::move(b21); // expected-warning {{variable 'b21' is uninitialized when used within its own initialization}} 437 B b22 = moveB(std::move(b22)); // expected-warning {{variable 'b22' is uninitialized when used within its own initialization}} 438 B b23 = B(std::move(b23)); // expected-warning {{variable 'b23' is uninitialized when used within its own initialization}} 439 B b24 = std::move(x ? b23 : (18, b24)); // expected-warning {{variable 'b24' is uninitialized when used within its own initialization}} 440 441 class U { 442 B b1, b2; 443 B *ptr1, *ptr2; 444 const B constb = {}; 445 446 U() {} 447 U(bool (*)[1]) : b1() {} 448 U(bool (*)[2]) : b2(b1) {} 449 U(bool (*)[3]) : b1{ 5, &b1.x } {} 450 U(bool (*)[4]) : b1(getB()) {} 451 U(bool (*)[5]) : b1(getB(&b1)) {} 452 U(bool (*)[6]) : b1(getB(&b1.x)) {} 453 454 U(bool (*)[7]) : b1(b1) {} // expected-warning {{field 'b1' is uninitialized when used here}} 455 U(bool (*)[8]) : b1(getB(b1.x)) {} // expected-warning {{field 'b1' is uninitialized when used here}} 456 U(bool (*)[9]) : b1(getB(b1.y)) {} // expected-warning {{field 'b1' is uninitialized when used here}} 457 U(bool (*)[10]) : b1(getB(-b1.x)) {} // expected-warning {{field 'b1' is uninitialized when used here}} 458 459 U(bool (*)[11]) : ptr1(0) {} 460 U(bool (*)[12]) : ptr1(0), ptr2(ptr1) {} 461 U(bool (*)[13]) : ptr1(getPtrB()) {} 462 U(bool (*)[14]) : ptr1(getPtrB(&ptr1)) {} 463 464 U(bool (*)[15]) : ptr1(getPtrB(ptr1->x)) {} // expected-warning {{field 'ptr1' is uninitialized when used here}} 465 U(bool (*)[16]) : ptr2(getPtrB(ptr2->y)) {} // expected-warning {{field 'ptr2' is uninitialized when used here}} 466 467 U(bool (*)[17]) : b1 { b1.x = 5, b1.y = 0 } {} 468 U(bool (*)[18]) : b1 { b1.x + 1, b1.y } {} // expected-warning 2{{field 'b1' is uninitialized when used here}} 469 470 U(bool (*)[19]) : constb(constb) {} // expected-warning {{field 'constb' is uninitialized when used here}} 471 U(bool (*)[20]) : constb(B(constb)) {} // expected-warning {{field 'constb' is uninitialized when used here}} 472 473 U(bool (*)[21]) : b1(std::move(b1)) {} // expected-warning {{field 'b1' is uninitialized when used here}} 474 U(bool (*)[22]) : b1(moveB(std::move(b1))) {} // expected-warning {{field 'b1' is uninitialized when used here}} 475 U(bool (*)[23]) : b1(B(std::move(b1))) {} // expected-warning {{field 'b1' is uninitialized when used here}} 476 U(bool (*)[24]) : b2(std::move(x ? b1 : (18, b2))) {} // expected-warning {{field 'b2' is uninitialized when used here}} 477 }; 478 479 struct C { char a[100], *e; } car = { .e = car.a }; 480 481 // <rdar://problem/10398199> 482 namespace rdar10398199 { 483 class FooBase { protected: ~FooBase() {} }; 484 class Foo : public FooBase { 485 public: 486 operator int&() const; 487 }; 488 void stuff(); 489 template <typename T> class FooImpl : public Foo { 490 T val; 491 public: 492 FooImpl(const T &x) : val(x) {} 493 ~FooImpl() { stuff(); } 494 }; 495 496 template <typename T> FooImpl<T> makeFoo(const T& x) { 497 return FooImpl<T>(x); 498 } 499 500 void test() { 501 const Foo &x = makeFoo(42); 502 const int&y = makeFoo(42u); 503 (void)x; 504 (void)y; 505 }; 506 } 507 508 // PR 12325 - this was a false uninitialized value warning due to 509 // a broken CFG. 510 int pr12325(int params) { 511 int x = ({ 512 while (false) 513 ; 514 int _v = params; 515 if (false) 516 ; 517 _v; // no-warning 518 }); 519 return x; 520 } 521 522 // Test lambda expressions with -Wuninitialized 523 int test_lambda() { 524 auto f1 = [] (int x, int y) { int z; return x + y + z; }; // expected-warning{{variable 'z' is uninitialized when used here}} expected-note {{initialize the variable 'z' to silence this warning}} 525 return f1(1, 2); 526 } 527 528 namespace { 529 struct A { 530 enum { A1 }; 531 static int A2() {return 5;} 532 int A3; 533 int A4() { return 5;} 534 }; 535 536 struct B { 537 A a; 538 }; 539 540 struct C { 541 C() {} 542 C(int x) {} 543 static A a; 544 B b; 545 }; 546 A C::a = A(); 547 548 // Accessing non-static members will give a warning. 549 struct D { 550 C c; 551 D(char (*)[1]) : c(c.b.a.A1) {} 552 D(char (*)[2]) : c(c.b.a.A2()) {} 553 D(char (*)[3]) : c(c.b.a.A3) {} // expected-warning {{field 'c' is uninitialized when used here}} 554 D(char (*)[4]) : c(c.b.a.A4()) {} // expected-warning {{field 'c' is uninitialized when used here}} 555 556 // c::a is static, so it is already initialized 557 D(char (*)[5]) : c(c.a.A1) {} 558 D(char (*)[6]) : c(c.a.A2()) {} 559 D(char (*)[7]) : c(c.a.A3) {} 560 D(char (*)[8]) : c(c.a.A4()) {} 561 }; 562 563 struct E { 564 int b = 1; 565 int c = 1; 566 int a; // This field needs to be last to prevent the cross field 567 // uninitialized warning. 568 E(char (*)[1]) : a(a ? b : c) {} // expected-warning {{field 'a' is uninitialized when used here}} 569 E(char (*)[2]) : a(b ? a : a) {} // expected-warning 2{{field 'a' is uninitialized when used here}} 570 E(char (*)[3]) : a(b ? (a) : c) {} // expected-warning {{field 'a' is uninitialized when used here}} 571 E(char (*)[4]) : a(b ? c : (a+c)) {} // expected-warning {{field 'a' is uninitialized when used here}} 572 E(char (*)[5]) : a(b ? c : b) {} 573 574 E(char (*)[6]) : a(a ?: a) {} // expected-warning 2{{field 'a' is uninitialized when used here}} 575 E(char (*)[7]) : a(b ?: a) {} // expected-warning {{field 'a' is uninitialized when used here}} 576 E(char (*)[8]) : a(a ?: c) {} // expected-warning {{field 'a' is uninitialized when used here}} 577 E(char (*)[9]) : a(b ?: c) {} 578 579 E(char (*)[10]) : a((a, a, b)) {} 580 E(char (*)[11]) : a((c + a, a + 1, b)) {} // expected-warning 2{{field 'a' is uninitialized when used here}} 581 E(char (*)[12]) : a((b + c, c, a)) {} // expected-warning {{field 'a' is uninitialized when used here}} 582 E(char (*)[13]) : a((a, a, a, a)) {} // expected-warning {{field 'a' is uninitialized when used here}} 583 E(char (*)[14]) : a((b, c, c)) {} 584 E(char (*)[15]) : a(b ?: a) {} // expected-warning {{field 'a' is uninitialized when used here}} 585 E(char (*)[16]) : a(a ?: b) {} // expected-warning {{field 'a' is uninitialized when used here}} 586 }; 587 588 struct F { 589 int a; 590 F* f; 591 F(int) {} 592 F() {} 593 }; 594 595 int F::*ptr = &F::a; 596 F* F::*f_ptr = &F::f; 597 struct G { 598 F f1, f2; 599 F *f3, *f4; 600 G(char (*)[1]) : f1(f1) {} // expected-warning {{field 'f1' is uninitialized when used here}} 601 G(char (*)[2]) : f2(f1) {} 602 G(char (*)[3]) : f2(F()) {} 603 604 G(char (*)[4]) : f1(f1.*ptr) {} // expected-warning {{field 'f1' is uninitialized when used here}} 605 G(char (*)[5]) : f2(f1.*ptr) {} 606 607 G(char (*)[6]) : f3(f3) {} // expected-warning {{field 'f3' is uninitialized when used here}} 608 G(char (*)[7]) : f3(f3->*f_ptr) {} // expected-warning {{field 'f3' is uninitialized when used here}} 609 G(char (*)[8]) : f3(new F(f3->*ptr)) {} // expected-warning {{field 'f3' is uninitialized when used here}} 610 }; 611 612 struct H { 613 H() : a(a) {} // expected-warning {{field 'a' is uninitialized when used here}} 614 const A a; 615 }; 616 } 617 618 namespace statics { 619 static int a = a; // no-warning: used to signal intended lack of initialization. 620 static int b = b + 1; // expected-warning {{variable 'b' is uninitialized when used within its own initialization}} 621 static int c = (c + c); // expected-warning 2{{variable 'c' is uninitialized when used within its own initialization}} 622 static int e = static_cast<long>(e) + 1; // expected-warning {{variable 'e' is uninitialized when used within its own initialization}} 623 static int f = foo(f); // expected-warning {{variable 'f' is uninitialized when used within its own initialization}} 624 625 // Thes don't warn as they don't require the value. 626 static int g = sizeof(g); 627 int gg = g; // Silence unneeded warning 628 static void* ptr = &ptr; 629 static int h = bar(&h); 630 static int i = boo(i); 631 static int j = far(j); 632 static int k = __alignof__(k); 633 634 static int l = k ? l : l; // expected-warning 2{{variable 'l' is uninitialized when used within its own initialization}} 635 static int m = 1 + (k ? m : m); // expected-warning 2{{variable 'm' is uninitialized when used within its own initialization}} 636 static int n = -n; // expected-warning {{variable 'n' is uninitialized when used within its own initialization}} 637 static int o = std::move(o); // expected-warning {{variable 'o' is uninitialized when used within its own initialization}} 638 static const int p = std::move(p); // expected-warning {{variable 'p' is uninitialized when used within its own initialization}} 639 static int q = moved(std::move(q)); // expected-warning {{variable 'q' is uninitialized when used within its own initialization}} 640 static int r = std::move((p ? q : (18, r))); // expected-warning {{variable 'r' is uninitialized when used within its own initialization}} 641 static int s = r ?: s; // expected-warning {{variable 's' is uninitialized when used within its own initialization}} 642 static int t = t ?: s; // expected-warning {{variable 't' is uninitialized when used within its own initialization}} 643 static int u = (foo(u), s); // expected-warning {{variable 'u' is uninitialized when used within its own initialization}} 644 static int v = (u += v); // expected-warning {{variable 'v' is uninitialized when used within its own initialization}} 645 static int w = (w += 10); // expected-warning {{variable 'w' is uninitialized when used within its own initialization}} 646 static int x = x++; // expected-warning {{variable 'x' is uninitialized when used within its own initialization}} 647 static int y = ((s ? (y, v) : (77, y))++, sizeof(y)); // expected-warning {{variable 'y' is uninitialized when used within its own initialization}} 648 static int z = ++ref(z); // expected-warning {{variable 'z' is uninitialized when used within its own initialization}} 649 static int aa = (ref(aa) += 10); // expected-warning {{variable 'aa' is uninitialized when used within its own initialization}} 650 static int bb = bb ? x : y; // expected-warning {{variable 'bb' is uninitialized when used within its own initialization}} 651 652 653 void test() { 654 static int a = a; // no-warning: used to signal intended lack of initialization. 655 static int b = b + 1; // expected-warning {{static variable 'b' is suspiciously used within its own initialization}} 656 static int c = (c + c); // expected-warning 2{{static variable 'c' is suspiciously used within its own initialization}} 657 static int d = ({ d + d ;}); // expected-warning 2{{static variable 'd' is suspiciously used within its own initialization}} 658 static int e = static_cast<long>(e) + 1; // expected-warning {{static variable 'e' is suspiciously used within its own initialization}} 659 static int f = foo(f); // expected-warning {{static variable 'f' is suspiciously used within its own initialization}} 660 661 // Thes don't warn as they don't require the value. 662 static int g = sizeof(g); 663 static void* ptr = &ptr; 664 static int h = bar(&h); 665 static int i = boo(i); 666 static int j = far(j); 667 static int k = __alignof__(k); 668 669 static int l = k ? l : l; // expected-warning 2{{static variable 'l' is suspiciously used within its own initialization}} 670 static int m = 1 + (k ? m : m); // expected-warning 2{{static variable 'm' is suspiciously used within its own initialization}} 671 static int n = -n; // expected-warning {{static variable 'n' is suspiciously used within its own initialization}} 672 static int o = std::move(o); // expected-warning {{static variable 'o' is suspiciously used within its own initialization}} 673 static const int p = std::move(p); // expected-warning {{static variable 'p' is suspiciously used within its own initialization}} 674 static int q = moved(std::move(q)); // expected-warning {{static variable 'q' is suspiciously used within its own initialization}} 675 static int r = std::move((p ? q : (18, r))); // expected-warning {{static variable 'r' is suspiciously used within its own initialization}} 676 static int s = r ?: s; // expected-warning {{static variable 's' is suspiciously used within its own initialization}} 677 static int t = t ?: s; // expected-warning {{static variable 't' is suspiciously used within its own initialization}} 678 static int u = (foo(u), s); // expected-warning {{static variable 'u' is suspiciously used within its own initialization}} 679 static int v = (u += v); // expected-warning {{static variable 'v' is suspiciously used within its own initialization}} 680 static int w = (w += 10); // expected-warning {{static variable 'w' is suspiciously used within its own initialization}} 681 static int x = x++; // expected-warning {{static variable 'x' is suspiciously used within its own initialization}} 682 static int y = ((s ? (y, v) : (77, y))++, sizeof(y)); // expected-warning {{static variable 'y' is suspiciously used within its own initialization}} 683 static int z = ++ref(z); // expected-warning {{static variable 'z' is suspiciously used within its own initialization}} 684 static int aa = (ref(aa) += 10); // expected-warning {{static variable 'aa' is suspiciously used within its own initialization}} 685 static int bb = bb ? x : y; // expected-warning {{static variable 'bb' is suspiciously used within its own initialization}} 686 687 for (;;) { 688 static int a = a; // no-warning: used to signal intended lack of initialization. 689 static int b = b + 1; // expected-warning {{static variable 'b' is suspiciously used within its own initialization}} 690 static int c = (c + c); // expected-warning 2{{static variable 'c' is suspiciously used within its own initialization}} 691 static int d = ({ d + d ;}); // expected-warning 2{{static variable 'd' is suspiciously used within its own initialization}} 692 static int e = static_cast<long>(e) + 1; // expected-warning {{static variable 'e' is suspiciously used within its own initialization}} 693 static int f = foo(f); // expected-warning {{static variable 'f' is suspiciously used within its own initialization}} 694 695 // Thes don't warn as they don't require the value. 696 static int g = sizeof(g); 697 static void* ptr = &ptr; 698 static int h = bar(&h); 699 static int i = boo(i); 700 static int j = far(j); 701 static int k = __alignof__(k); 702 703 static int l = k ? l : l; // expected-warning 2{{static variable 'l' is suspiciously used within its own initialization}} 704 static int m = 1 + (k ? m : m); // expected-warning 2{{static variable 'm' is suspiciously used within its own initialization}} 705 static int n = -n; // expected-warning {{static variable 'n' is suspiciously used within its own initialization}} 706 static int o = std::move(o); // expected-warning {{static variable 'o' is suspiciously used within its own initialization}} 707 static const int p = std::move(p); // expected-warning {{static variable 'p' is suspiciously used within its own initialization}} 708 static int q = moved(std::move(q)); // expected-warning {{static variable 'q' is suspiciously used within its own initialization}} 709 static int r = std::move((p ? q : (18, r))); // expected-warning {{static variable 'r' is suspiciously used within its own initialization}} 710 static int s = r ?: s; // expected-warning {{static variable 's' is suspiciously used within its own initialization}} 711 static int t = t ?: s; // expected-warning {{static variable 't' is suspiciously used within its own initialization}} 712 static int u = (foo(u), s); // expected-warning {{static variable 'u' is suspiciously used within its own initialization}} 713 static int v = (u += v); // expected-warning {{static variable 'v' is suspiciously used within its own initialization}} 714 static int w = (w += 10); // expected-warning {{static variable 'w' is suspiciously used within its own initialization}} 715 static int x = x++; // expected-warning {{static variable 'x' is suspiciously used within its own initialization}} 716 static int y = ((s ? (y, v) : (77, y))++, sizeof(y)); // expected-warning {{static variable 'y' is suspiciously used within its own initialization}} 717 static int z = ++ref(z); // expected-warning {{static variable 'z' is suspiciously used within its own initialization}} 718 static int aa = (ref(aa) += 10); // expected-warning {{static variable 'aa' is suspiciously used within its own initialization}} 719 static int bb = bb ? x : y; // expected-warning {{static variable 'bb' is suspiciously used within its own initialization}} 720 } 721 } 722 } 723 724 namespace in_class_initializers { 725 struct S { 726 S() : a(a + 1) {} // expected-warning{{field 'a' is uninitialized when used here}} 727 int a = 42; // Note: because a is in a member initializer list, this initialization is ignored. 728 }; 729 730 struct T { 731 T() : b(a + 1) {} // No-warning. 732 int a = 42; 733 int b; 734 }; 735 736 struct U { 737 U() : a(b + 1), b(a + 1) {} // expected-warning{{field 'b' is uninitialized when used here}} 738 int a = 42; // Note: because a and b are in the member initializer list, these initializers are ignored. 739 int b = 1; 740 }; 741 } 742 743 namespace references { 744 int &a = a; // expected-warning{{reference 'a' is not yet bound to a value when used within its own initialization}} 745 int &b(b); // expected-warning{{reference 'b' is not yet bound to a value when used within its own initialization}} 746 int &c = a ? b : c; // expected-warning{{reference 'c' is not yet bound to a value when used within its own initialization}} 747 int &d{d}; // expected-warning{{reference 'd' is not yet bound to a value when used within its own initialization}} 748 int &e = d ?: e; // expected-warning{{reference 'e' is not yet bound to a value when used within its own initialization}} 749 int &f = f ?: d; // expected-warning{{reference 'f' is not yet bound to a value when used within its own initialization}} 750 751 int &return_ref1(int); 752 int &return_ref2(int&); 753 754 int &g = return_ref1(g); // expected-warning{{reference 'g' is not yet bound to a value when used within its own initialization}} 755 int &h = return_ref2(h); // expected-warning{{reference 'h' is not yet bound to a value when used within its own initialization}} 756 757 struct S { 758 S() : a(a) {} // expected-warning{{reference 'a' is not yet bound to a value when used here}} 759 int &a; 760 }; 761 762 void test() { 763 int &a = a; // expected-warning{{reference 'a' is not yet bound to a value when used within its own initialization}} 764 int &b(b); // expected-warning{{reference 'b' is not yet bound to a value when used within its own initialization}} 765 int &c = a ? b : c; // expected-warning{{reference 'c' is not yet bound to a value when used within its own initialization}} 766 int &d{d}; // expected-warning{{reference 'd' is not yet bound to a value when used within its own initialization}} 767 } 768 769 struct T { 770 T() // expected-note{{during field initialization in this constructor}} 771 : a(b), b(a) {} // expected-warning{{reference 'b' is not yet bound to a value when used here}} 772 int &a, &b; 773 int &c = c; // expected-warning{{reference 'c' is not yet bound to a value when used here}} 774 }; 775 776 int x; 777 struct U { 778 U() : b(a) {} // No-warning. 779 int &a = x; 780 int &b; 781 }; 782 } 783 784 namespace operators { 785 struct A { 786 A(bool); 787 bool operator==(A); 788 }; 789 790 A makeA(); 791 792 A a1 = a1 = makeA(); // expected-warning{{variable 'a1' is uninitialized when used within its own initialization}} 793 A a2 = a2 == a1; // expected-warning{{variable 'a2' is uninitialized when used within its own initialization}} 794 A a3 = a2 == a3; // expected-warning{{variable 'a3' is uninitialized when used within its own initialization}} 795 796 int x = x = 5; 797 } 798 799 namespace lambdas { 800 struct A { 801 template<typename T> A(T) {} 802 int x; 803 }; 804 A a0([] { return a0.x; }); // ok 805 void f() { 806 A a1([=] { return a1.x; }); // expected-warning{{variable 'a1' is uninitialized when used within its own initialization}} 807 A a2([&] { return a2.x; }); // ok 808 } 809 } 810 811 namespace record_fields { 812 bool x; 813 struct A { 814 A() {} 815 A get(); 816 static A num(); 817 static A copy(A); 818 static A something(A&); 819 }; 820 821 A ref(A&); 822 A const_ref(const A&); 823 A pointer(A*); 824 A normal(A); 825 A rref(A&&); 826 827 struct B { 828 A a; 829 B(char (*)[1]) : a(a) {} // expected-warning {{uninitialized}} 830 B(char (*)[2]) : a(a.get()) {} // expected-warning {{uninitialized}} 831 B(char (*)[3]) : a(a.num()) {} 832 B(char (*)[4]) : a(a.copy(a)) {} // expected-warning {{uninitialized}} 833 B(char (*)[5]) : a(a.something(a)) {} 834 B(char (*)[6]) : a(ref(a)) {} 835 B(char (*)[7]) : a(const_ref(a)) {} 836 B(char (*)[8]) : a(pointer(&a)) {} 837 B(char (*)[9]) : a(normal(a)) {} // expected-warning {{uninitialized}} 838 B(char (*)[10]) : a(std::move(a)) {} // expected-warning {{uninitialized}} 839 B(char (*)[11]) : a(A(std::move(a))) {} // expected-warning {{uninitialized}} 840 B(char (*)[12]) : a(rref(std::move(a))) {} // expected-warning {{uninitialized}} 841 B(char (*)[13]) : a(std::move(x ? a : (25, a))) {} // expected-warning 2{{uninitialized}} 842 }; 843 struct C { 844 C() {} // expected-note9{{in this constructor}} 845 A a1 = a1; // expected-warning {{uninitialized}} 846 A a2 = a2.get(); // expected-warning {{uninitialized}} 847 A a3 = a3.num(); 848 A a4 = a4.copy(a4); // expected-warning {{uninitialized}} 849 A a5 = a5.something(a5); 850 A a6 = ref(a6); 851 A a7 = const_ref(a7); 852 A a8 = pointer(&a8); 853 A a9 = normal(a9); // expected-warning {{uninitialized}} 854 const A a10 = a10; // expected-warning {{uninitialized}} 855 A a11 = std::move(a11); // expected-warning {{uninitialized}} 856 A a12 = A(std::move(a12)); // expected-warning {{uninitialized}} 857 A a13 = rref(std::move(a13)); // expected-warning {{uninitialized}} 858 A a14 = std::move(x ? a13 : (22, a14)); // expected-warning {{uninitialized}} 859 }; 860 struct D { // expected-note9{{in the implicit default constructor}} 861 A a1 = a1; // expected-warning {{uninitialized}} 862 A a2 = a2.get(); // expected-warning {{uninitialized}} 863 A a3 = a3.num(); 864 A a4 = a4.copy(a4); // expected-warning {{uninitialized}} 865 A a5 = a5.something(a5); 866 A a6 = ref(a6); 867 A a7 = const_ref(a7); 868 A a8 = pointer(&a8); 869 A a9 = normal(a9); // expected-warning {{uninitialized}} 870 const A a10 = a10; // expected-warning {{uninitialized}} 871 A a11 = std::move(a11); // expected-warning {{uninitialized}} 872 A a12 = A(std::move(a12)); // expected-warning {{uninitialized}} 873 A a13 = rref(std::move(a13)); // expected-warning {{uninitialized}} 874 A a14 = std::move(x ? a13 : (22, a14)); // expected-warning {{uninitialized}} 875 }; 876 D d; 877 struct E { 878 A a1 = a1; 879 A a2 = a2.get(); 880 A a3 = a3.num(); 881 A a4 = a4.copy(a4); 882 A a5 = a5.something(a5); 883 A a6 = ref(a6); 884 A a7 = const_ref(a7); 885 A a8 = pointer(&a8); 886 A a9 = normal(a9); 887 const A a10 = a10; 888 A a11 = std::move(a11); 889 A a12 = A(std::move(a12)); 890 A a13 = rref(std::move(a13)); 891 A a14 = std::move(x ? a13 : (22, a14)); 892 }; 893 } 894 895 namespace cross_field_warnings { 896 struct A { 897 int a, b; 898 A() {} 899 A(char (*)[1]) : b(a) {} // expected-warning{{field 'a' is uninitialized when used here}} 900 A(char (*)[2]) : a(b) {} // expected-warning{{field 'b' is uninitialized when used here}} 901 }; 902 903 struct B { 904 int a = b; // expected-warning{{field 'b' is uninitialized when used here}} 905 int b; 906 B() {} // expected-note{{during field initialization in this constructor}} 907 }; 908 909 struct C { 910 int a; 911 int b = a; // expected-warning{{field 'a' is uninitialized when used here}} 912 C(char (*)[1]) : a(5) {} 913 C(char (*)[2]) {} // expected-note{{during field initialization in this constructor}} 914 }; 915 916 struct D { 917 int a; 918 int &b; 919 int &c = a; 920 int d = b; 921 D() : b(a) {} 922 }; 923 924 struct E { 925 int a; 926 int get(); 927 static int num(); 928 E() {} 929 E(int) {} 930 }; 931 932 struct F { 933 int a; 934 E e; 935 int b; 936 F(char (*)[1]) : a(e.get()) {} // expected-warning{{field 'e' is uninitialized when used here}} 937 F(char (*)[2]) : a(e.num()) {} 938 F(char (*)[3]) : e(a) {} // expected-warning{{field 'a' is uninitialized when used here}} 939 F(char (*)[4]) : a(4), e(a) {} 940 F(char (*)[5]) : e(b) {} // expected-warning{{field 'b' is uninitialized when used here}} 941 F(char (*)[6]) : e(b), b(4) {} // expected-warning{{field 'b' is uninitialized when used here}} 942 }; 943 944 struct G { 945 G(const A&) {}; 946 }; 947 948 struct H { 949 A a1; 950 G g; 951 A a2; 952 H() : g(a1) {} 953 H(int) : g(a2) {} 954 }; 955 956 struct I { 957 I(int*) {} 958 }; 959 960 struct J : public I { 961 int *a; 962 int *b; 963 int c; 964 J() : I((a = new int(5))), b(a), c(*a) {} 965 }; 966 967 struct K { 968 int a = (b = 5); 969 int b = b + 5; 970 }; 971 972 struct L { 973 int a = (b = 5); 974 int b = b + 5; // expected-warning{{field 'b' is uninitialized when used here}} 975 L() : a(5) {} // expected-note{{during field initialization in this constructor}} 976 }; 977 978 struct M { }; 979 980 struct N : public M { 981 int a; 982 int b; 983 N() : b(a) { } // expected-warning{{field 'a' is uninitialized when used here}} 984 }; 985 986 struct O { 987 int x = 42; 988 int get() { return x; } 989 }; 990 991 struct P { 992 O o; 993 int x = o.get(); 994 P() : x(o.get()) { } 995 }; 996 997 struct Q { 998 int a; 999 int b; 1000 int &c; 1001 Q() : 1002 a(c = 5), // expected-warning{{reference 'c' is not yet bound to a value when used here}} 1003 b(c), // expected-warning{{reference 'c' is not yet bound to a value when used here}} 1004 c(a) {} 1005 }; 1006 1007 struct R { 1008 int a; 1009 int b; 1010 int c; 1011 int d = a + b + c; 1012 R() : a(c = 5), b(c), c(a) {} 1013 }; 1014 1015 // FIXME: Use the CFG-based analysis to give a sometimes uninitialized 1016 // warning on y. 1017 struct T { 1018 int x; 1019 int y; 1020 T(bool b) 1021 : x(b ? (y = 5) : (1 + y)), // expected-warning{{field 'y' is uninitialized when used here}} 1022 y(y + 1) {} 1023 T(int b) 1024 : x(!b ? (1 + y) : (y = 5)), // expected-warning{{field 'y' is uninitialized when used here}} 1025 y(y + 1) {} 1026 }; 1027 1028 } 1029 1030 namespace base_class { 1031 struct A { 1032 A (int) {} 1033 }; 1034 1035 struct B : public A { 1036 int x; 1037 B() : A(x) {} // expected-warning{{field 'x' is uninitialized when used here}} 1038 }; 1039 1040 struct C : public A { 1041 int x; 1042 int y; 1043 C() : A(y = 4), x(y) {} 1044 }; 1045 } 1046 1047 namespace delegating_constructor { 1048 struct A { 1049 A(int); 1050 A(int&, int); 1051 1052 A(char (*)[1]) : A(x) {} 1053 // expected-warning@-1 {{field 'x' is uninitialized when used here}} 1054 A(char (*)[2]) : A(x, x) {} 1055 // expected-warning@-1 {{field 'x' is uninitialized when used here}} 1056 1057 A(char (*)[3]) : A(x, 0) {} 1058 1059 int x; 1060 }; 1061 } 1062 1063 namespace init_list { 1064 int num = 5; 1065 struct A { int i1, i2; }; 1066 struct B { A a1, a2; }; 1067 1068 A a1{1,2}; 1069 A a2{a2.i1 + 2}; // expected-warning{{uninitialized}} 1070 A a3 = {a3.i1 + 2}; // expected-warning{{uninitialized}} 1071 A a4 = A{a4.i2 + 2}; // expected-warning{{uninitialized}} 1072 1073 B b1 = { {}, {} }; 1074 B b2 = { {}, b2.a1 }; 1075 B b3 = { b3.a1 }; // expected-warning{{uninitialized}} 1076 B b4 = { {}, b4.a2} ; // expected-warning{{uninitialized}} 1077 B b5 = { b5.a2 }; // expected-warning{{uninitialized}} 1078 1079 B b6 = { {b6.a1.i1} }; // expected-warning{{uninitialized}} 1080 B b7 = { {0, b7.a1.i1} }; 1081 B b8 = { {}, {b8.a1.i1} }; 1082 B b9 = { {}, {0, b9.a1.i1} }; 1083 1084 B b10 = { {b10.a1.i2} }; // expected-warning{{uninitialized}} 1085 B b11 = { {0, b11.a1.i2} }; // expected-warning{{uninitialized}} 1086 B b12 = { {}, {b12.a1.i2} }; 1087 B b13 = { {}, {0, b13.a1.i2} }; 1088 1089 B b14 = { {b14.a2.i1} }; // expected-warning{{uninitialized}} 1090 B b15 = { {0, b15.a2.i1} }; // expected-warning{{uninitialized}} 1091 B b16 = { {}, {b16.a2.i1} }; // expected-warning{{uninitialized}} 1092 B b17 = { {}, {0, b17.a2.i1} }; 1093 1094 B b18 = { {b18.a2.i2} }; // expected-warning{{uninitialized}} 1095 B b19 = { {0, b19.a2.i2} }; // expected-warning{{uninitialized}} 1096 B b20 = { {}, {b20.a2.i2} }; // expected-warning{{uninitialized}} 1097 B b21 = { {}, {0, b21.a2.i2} }; // expected-warning{{uninitialized}} 1098 1099 B b22 = { {b18.a2.i2 + 5} }; 1100 1101 struct C {int a; int& b; int c; }; 1102 C c1 = { 0, num, 0 }; 1103 C c2 = { 1, num, c2.b }; 1104 C c3 = { c3.b, num }; // expected-warning{{uninitialized}} 1105 C c4 = { 0, c4.b, 0 }; // expected-warning{{uninitialized}} 1106 C c5 = { 0, c5.c, 0 }; 1107 C c6 = { c6.b, num, 0 }; // expected-warning{{uninitialized}} 1108 C c7 = { 0, c7.a, 0 }; 1109 1110 struct D {int &a; int &b; }; 1111 D d1 = { num, num }; 1112 D d2 = { num, d2.a }; 1113 D d3 = { d3.b, num }; // expected-warning{{uninitialized}} 1114 }; 1115