1 // RUN: %clang_cc1 -fsyntax-only -Wall -Wc++20-compat -Wuninitialized -Wno-unused-value -Wno-unused-lambda-capture -Wno-uninitialized-const-reference -std=c++1z -verify %s 2 // RUN: %clang_cc1 -fsyntax-only -Wall -Wc++20-compat -Wuninitialized -Wno-unused-value -Wno-unused-lambda-capture -Wno-uninitialized-const-reference -std=c++1z -verify %s -fexperimental-new-constant-interpreter 3 // RUN: %clang_cc1 -fsyntax-only -Wall -Wc++20-compat -Wuninitialized -Wno-unused-value -Wno-unused-lambda-capture -Wno-uninitialized-const-reference -std=c++20 -verify %s 4 5 // definitions for std::move 6 namespace std { 7 inline namespace foo { 8 template <class T> struct remove_reference { typedef T type; }; 9 template <class T> struct remove_reference<T&> { typedef T type; }; 10 template <class T> struct remove_reference<T&&> { typedef T type; }; 11 12 template <class T> typename remove_reference<T>::type&& move(T&& t); 13 } 14 } 15 16 int foo(int x); 17 int bar(int* x); 18 int boo(int& x); 19 int far(const int& x); 20 int moved(int&& x); 21 int &ref(int x); 22 23 // Test self-references within initializers which are guaranteed to be 24 // uninitialized. 25 int a = a; // no-warning: used to signal intended lack of initialization. 26 int b = b + 1; // expected-warning {{variable 'b' is uninitialized when used within its own initialization}} 27 int c = (c + c); // expected-warning 2 {{variable 'c' is uninitialized when used within its own initialization}} 28 int e = static_cast<long>(e) + 1; // expected-warning {{variable 'e' is uninitialized when used within its own initialization}} 29 int f = foo(f); // expected-warning {{variable 'f' is uninitialized when used within its own initialization}} 30 31 // These don't warn as they don't require the value. 32 int g = sizeof(g); 33 void* ptr = &ptr; 34 int h = bar(&h); 35 int i = boo(i); 36 int j = far(j); 37 int k = __alignof__(k); 38 39 int l = k ? l : l; // expected-warning 2{{variable 'l' is uninitialized when used within its own initialization}} 40 int m = 1 + (k ? m : m); // expected-warning 2{{variable 'm' is uninitialized when used within its own initialization}} 41 int n = -n; // expected-warning {{variable 'n' is uninitialized when used within its own initialization}} 42 int o = std::move(o); // expected-warning {{variable 'o' is uninitialized when used within its own initialization}} 43 const int p = std::move(p); // expected-warning {{variable 'p' is uninitialized when used within its own initialization}} 44 int q = moved(std::move(q)); // expected-warning {{variable 'q' is uninitialized when used within its own initialization}} 45 int r = std::move((p ? q : (18, r))); // expected-warning {{variable 'r' is uninitialized when used within its own initialization}} 46 int s = r ?: s; // expected-warning {{variable 's' is uninitialized when used within its own initialization}} 47 int t = t ?: s; // expected-warning {{variable 't' is uninitialized when used within its own initialization}} 48 int u = (foo(u), s); // expected-warning {{variable 'u' is uninitialized when used within its own initialization}} 49 int v = (u += v); // expected-warning {{variable 'v' is uninitialized when used within its own initialization}} 50 int w = (w += 10); // expected-warning {{variable 'w' is uninitialized when used within its own initialization}} 51 int x = x++; // expected-warning {{variable 'x' is uninitialized when used within its own initialization}} 52 int y = ((s ? (y, v) : (77, y))++, sizeof(y)); // expected-warning {{variable 'y' is uninitialized when used within its own initialization}} 53 int z = ++ref(z); // expected-warning {{variable 'z' is uninitialized when used within its own initialization}} 54 int aa = (ref(aa) += 10); // expected-warning {{variable 'aa' is uninitialized when used within its own initialization}} 55 int bb = bb ? x : y; // expected-warning {{variable 'bb' is uninitialized when used within its own initialization}} 56 57 void test_stuff () { 58 int a = a; // no-warning: used to signal intended lack of initialization. 59 int b = b + 1; // expected-warning {{variable 'b' is uninitialized when used within its own initialization}} 60 int c = (c + c); // expected-warning {{variable 'c' is uninitialized when used within its own initialization}} 61 int d = ({ d + d ;}); // expected-warning {{variable 'd' is uninitialized when used within its own initialization}} 62 int e = static_cast<long>(e) + 1; // expected-warning {{variable 'e' is uninitialized when used within its own initialization}} 63 int f = foo(f); // expected-warning {{variable 'f' is uninitialized when used within its own initialization}} 64 65 // These don't warn as they don't require the value. 66 int g = sizeof(g); 67 void* ptr = &ptr; 68 int h = bar(&h); 69 int i = boo(i); 70 int j = far(j); 71 int k = __alignof__(k); 72 73 int l = k ? l : l; // expected-warning {{variable 'l' is uninitialized when used within its own initialization}} 74 int m = 1 + (k ? m : m); // expected-warning {{'m' is uninitialized when used within its own initialization}} 75 int n = -n; // expected-warning {{variable 'n' is uninitialized when used within its own initialization}} 76 int o = std::move(o); // expected-warning {{variable 'o' is uninitialized when used within its own initialization}} 77 const int p = std::move(p); // expected-warning {{variable 'p' is uninitialized when used within its own initialization}} 78 int q = moved(std::move(q)); // expected-warning {{variable 'q' is uninitialized when used within its own initialization}} 79 int r = std::move((p ? q : (18, r))); // expected-warning {{variable 'r' is uninitialized when used within its own initialization}} 80 int s = r ?: s; // expected-warning {{variable 's' is uninitialized when used within its own initialization}} 81 int t = t ?: s; // expected-warning {{variable 't' is uninitialized when used within its own initialization}} 82 int u = (foo(u), s); // expected-warning {{variable 'u' is uninitialized when used within its own initialization}} 83 int v = (u += v); // expected-warning {{variable 'v' is uninitialized when used within its own initialization}} 84 int w = (w += 10); // expected-warning {{variable 'w' is uninitialized when used within its own initialization}} 85 int x = x++; // expected-warning {{variable 'x' is uninitialized when used within its own initialization}} 86 int y = ((s ? (y, v) : (77, y))++, sizeof(y)); // expected-warning {{variable 'y' is uninitialized when used within its own initialization}} 87 int z = ++ref(z); // expected-warning {{variable 'z' is uninitialized when used within its own initialization}} 88 int aa = (ref(aa) += 10); // expected-warning {{variable 'aa' is uninitialized when used within its own initialization}} 89 int bb = bb ? x : y; // expected-warning {{variable 'bb' is uninitialized when used within its own initialization}} 90 91 for (;;) { 92 int a = a; // no-warning: used to signal intended lack of initialization. 93 int b = b + 1; // expected-warning {{variable 'b' is uninitialized when used within its own initialization}} 94 int c = (c + c); // expected-warning {{variable 'c' is uninitialized when used within its own initialization}} 95 int d = ({ d + d ;}); // expected-warning {{variable 'd' is uninitialized when used within its own initialization}} 96 int e = static_cast<long>(e) + 1; // expected-warning {{variable 'e' is uninitialized when used within its own initialization}} 97 int f = foo(f); // expected-warning {{variable 'f' is uninitialized when used within its own initialization}} 98 99 // These don't warn as they don't require the value. 100 int g = sizeof(g); 101 void* ptr = &ptr; 102 int h = bar(&h); 103 int i = boo(i); 104 int j = far(j); 105 int k = __alignof__(k); 106 107 int l = k ? l : l; // expected-warning {{variable 'l' is uninitialized when used within its own initialization}} 108 int m = 1 + (k ? m : m); // expected-warning {{'m' is uninitialized when used within its own initialization}} 109 int n = -n; // expected-warning {{variable 'n' is uninitialized when used within its own initialization}} 110 int o = std::move(o); // expected-warning {{variable 'o' is uninitialized when used within its own initialization}} 111 const int p = std::move(p); // expected-warning {{variable 'p' is uninitialized when used within its own initialization}} 112 int q = moved(std::move(q)); // expected-warning {{variable 'q' is uninitialized when used within its own initialization}} 113 int r = std::move((p ? q : (18, r))); // expected-warning {{variable 'r' is uninitialized when used within its own initialization}} 114 int s = r ?: s; // expected-warning {{variable 's' is uninitialized when used within its own initialization}} 115 int t = t ?: s; // expected-warning {{variable 't' is uninitialized when used within its own initialization}} 116 int u = (foo(u), s); // expected-warning {{variable 'u' is uninitialized when used within its own initialization}} 117 int v = (u += v); // expected-warning {{variable 'v' is uninitialized when used within its own initialization}} 118 int w = (w += 10); // expected-warning {{variable 'w' is uninitialized when used within its own initialization}} 119 int x = x++; // expected-warning {{variable 'x' is uninitialized when used within its own initialization}} 120 int y = ((s ? (y, v) : (77, y))++, sizeof(y)); // expected-warning {{variable 'y' is uninitialized when used within its own initialization}} 121 int z = ++ref(z); // expected-warning {{variable 'z' is uninitialized when used within its own initialization}} 122 int aa = (ref(aa) += 10); // expected-warning {{variable 'aa' is uninitialized when used within its own initialization}} 123 int bb = bb ? x : y; // expected-warning {{variable 'bb' is uninitialized when used within its own initialization}} 124 125 } 126 } 127 128 void test_comma() { 129 int a; // expected-note {{initialize the variable 'a' to silence this warning}} 130 int b = (a, a ?: 2); // expected-warning {{variable 'a' is uninitialized when used here}} 131 int c = (a, a, b, c); // expected-warning {{variable 'c' is uninitialized when used within its own initialization}} 132 int d; // expected-note {{initialize the variable 'd' to silence this warning}} 133 int e = (foo(d), e, b); // expected-warning {{variable 'd' is uninitialized when used here}} 134 int f; // expected-note {{initialize the variable 'f' to silence this warning}} 135 f = f + 1, 2; // expected-warning {{variable 'f' is uninitialized when used here}} 136 int h; 137 int g = (h, g, 2); // no-warning: h, g are evaluated but not used. 138 } 139 140 namespace member_ptr { 141 struct A { 142 int x; 143 int y; 144 A(int x) : x{x} {} 145 }; 146 147 void test_member_ptr() { 148 int A::* px = &A::x; 149 A a{a.*px}; // expected-warning {{variable 'a' is uninitialized when used within its own initialization}} 150 A b = b; // expected-warning {{variable 'b' is uninitialized when used within its own initialization}} 151 } 152 } 153 154 namespace const_ptr { 155 void foo(int *a); 156 void bar(const int *a); 157 void foobar(const int **a); 158 159 void test_const_ptr() { 160 int a; 161 int b; // expected-note {{initialize the variable 'b' to silence this warning}} 162 foo(&a); 163 bar(&b); 164 b = a + b; // expected-warning {{variable 'b' is uninitialized when used here}} 165 int *ptr; //expected-note {{initialize the variable 'ptr' to silence this warning}} 166 const int *ptr2; 167 foo(ptr); // expected-warning {{variable 'ptr' is uninitialized when used here}} 168 foobar(&ptr2); 169 } 170 } 171 172 // Also test similar constructs in a field's initializer. 173 struct S { 174 int x; 175 int y; 176 const int z = 5; 177 void *ptr; 178 179 S(bool (*)[1]) : x(x) {} // expected-warning {{field 'x' is uninitialized when used here}} 180 S(bool (*)[2]) : x(x + 1) {} // expected-warning {{field 'x' is uninitialized when used here}} 181 S(bool (*)[3]) : x(x + x) {} // expected-warning 2{{field 'x' is uninitialized when used here}} 182 S(bool (*)[4]) : x(static_cast<long>(x) + 1) {} // expected-warning {{field 'x' is uninitialized when used here}} 183 S(bool (*)[5]) : x(foo(x)) {} // expected-warning {{field 'x' is uninitialized when used here}} 184 185 // These don't actually require the value of x and so shouldn't warn. 186 S(char (*)[1]) : x(sizeof(x)) {} 187 S(char (*)[2]) : ptr(&ptr) {} 188 S(char (*)[3]) : x(bar(&x)) {} 189 S(char (*)[4]) : x(boo(x)) {} 190 S(char (*)[5]) : x(far(x)) {} 191 S(char (*)[6]) : x(__alignof__(x)) {} 192 193 S(int (*)[1]) : x(0), y(x ? y : y) {} // expected-warning 2{{field 'y' is uninitialized when used here}} 194 S(int (*)[2]) : x(0), y(1 + (x ? y : y)) {} // expected-warning 2{{field 'y' is uninitialized when used here}} 195 S(int (*)[3]) : x(-x) {} // expected-warning {{field 'x' is uninitialized when used here}} 196 S(int (*)[4]) : x(std::move(x)) {} // expected-warning {{field 'x' is uninitialized when used here}} 197 S(int (*)[5]) : z(std::move(z)) {} // expected-warning {{field 'z' is uninitialized when used here}} 198 S(int (*)[6]) : x(moved(std::move(x))) {} // expected-warning {{field 'x' is uninitialized when used here}} 199 S(int (*)[7]) : x(0), y(std::move((x ? x : (18, y)))) {} // expected-warning {{field 'y' is uninitialized when used here}} 200 S(int (*)[8]) : x(0), y(x ?: y) {} // expected-warning {{field 'y' is uninitialized when used here}} 201 S(int (*)[9]) : x(0), y(y ?: x) {} // expected-warning {{field 'y' is uninitialized when used here}} 202 S(int (*)[10]) : x(0), y((foo(y), x)) {} // expected-warning {{field 'y' is uninitialized when used here}} 203 S(int (*)[11]) : x(0), y(x += y) {} // expected-warning {{field 'y' is uninitialized when used here}} 204 S(int (*)[12]) : x(x += 10) {} // expected-warning {{field 'x' is uninitialized when used here}} 205 S(int (*)[13]) : x(x++) {} // expected-warning {{field 'x' is uninitialized when used here}} 206 S(int (*)[14]) : x(0), y(((x ? (y, x) : (77, y))++, sizeof(y))) {} // expected-warning {{field 'y' is uninitialized when used here}} 207 S(int (*)[15]) : x(++ref(x)) {} // expected-warning {{field 'x' is uninitialized when used here}} 208 S(int (*)[16]) : x((ref(x) += 10)) {} // expected-warning {{field 'x' is uninitialized when used here}} 209 S(int (*)[17]) : x(0), y(y ? x : x) {} // expected-warning {{field 'y' is uninitialized when used here}} 210 }; 211 212 // Test self-references with record types. 213 class A { 214 // Non-POD class. 215 public: 216 enum count { ONE, TWO, THREE }; 217 int num; 218 static int count; 219 int get() const { return num; } 220 int get2() { return num; } 221 int set(int x) { num = x; return num; } 222 static int zero() { return 0; } 223 224 A() {} 225 A(A const &a) {} 226 A(int x) {} 227 A(int *x) {} 228 A(A *a) {} 229 A(A &&a) {} 230 ~A(); 231 bool operator!(); 232 bool operator!=(const A&); 233 }; 234 235 bool operator!=(int, const A&); 236 237 A getA() { return A(); } 238 A getA(int x) { return A(); } 239 A getA(A* a) { return A(); } 240 A getA(A a) { return A(); } 241 A moveA(A&& a) { return A(); } 242 A const_refA(const A& a) { return A(); } 243 244 void setupA(bool x) { 245 A a1; 246 a1.set(a1.get()); 247 A a2(a1.get()); 248 A a3(a1); 249 A a4(&a4); 250 A a5(a5.zero()); 251 A a6(a6.ONE); 252 A a7 = getA(); 253 A a8 = getA(a8.TWO); 254 A a9 = getA(&a9); 255 A a10(a10.count); 256 257 A a11(a11); // expected-warning {{variable 'a11' is uninitialized when used within its own initialization}} 258 A a12(a12.get()); // expected-warning {{variable 'a12' is uninitialized when used within its own initialization}} 259 A a13(a13.num); // expected-warning {{variable 'a13' is uninitialized when used within its own initialization}} 260 A a14 = A(a14); // expected-warning {{variable 'a14' is uninitialized when used within its own initialization}} 261 A a15 = getA(a15.num); // expected-warning {{variable 'a15' is uninitialized when used within its own initialization}} 262 A a16(&a16.num); // expected-warning {{variable 'a16' is uninitialized when used within its own initialization}} 263 A a17(a17.get2()); // expected-warning {{variable 'a17' is uninitialized when used within its own initialization}} 264 A a18 = x ? a18 : a17; // expected-warning {{variable 'a18' is uninitialized when used within its own initialization}} 265 A a19 = getA(x ? a19 : a17); // expected-warning {{variable 'a19' is uninitialized when used within its own initialization}} 266 A a20{a20}; // expected-warning {{variable 'a20' is uninitialized when used within its own initialization}} 267 A a21 = {a21}; // expected-warning {{variable 'a21' is uninitialized when used within its own initialization}} 268 269 // FIXME: Make the local uninitialized warning consistent with the global 270 // uninitialized checking. 271 A *a22 = new A(a22->count); // expected-warning {{variable 'a22' is uninitialized when used within its own initialization}} 272 A *a23 = new A(a23->ONE); // expected-warning {{variable 'a23' is uninitialized when used within its own initialization}} 273 A *a24 = new A(a24->TWO); // expected-warning {{variable 'a24' is uninitialized when used within its own initialization}} 274 A *a25 = new A(a25->zero()); // expected-warning {{variable 'a25' is uninitialized when used within its own initialization}} 275 276 A *a26 = new A(a26->get()); // expected-warning {{variable 'a26' is uninitialized when used within its own initialization}} 277 A *a27 = new A(a27->get2()); // expected-warning {{variable 'a27' is uninitialized when used within its own initialization}} 278 A *a28 = new A(a28->num); // expected-warning {{variable 'a28' is uninitialized when used within its own initialization}} 279 280 const A a29(a29); // expected-warning {{variable 'a29' is uninitialized when used within its own initialization}} 281 const A a30 = a30; // expected-warning {{variable 'a30' is uninitialized when used within its own initialization}} 282 283 A a31 = std::move(a31); // expected-warning {{variable 'a31' is uninitialized when used within its own initialization}} 284 A a32 = moveA(std::move(a32)); // expected-warning {{variable 'a32' is uninitialized when used within its own initialization}} 285 A a33 = A(std::move(a33)); // expected-warning {{variable 'a33' is uninitialized when used within its own initialization}} 286 A a34(std::move(a34)); // expected-warning {{variable 'a34' is uninitialized when used within its own initialization}} 287 A a35 = std::move(x ? a34 : (37, a35)); // expected-warning {{variable 'a35' is uninitialized when used within its own initialization}} 288 289 A a36 = const_refA(a36); 290 A a37(const_refA(a37)); 291 292 A a38({a38}); // expected-warning {{variable 'a38' is uninitialized when used within its own initialization}} 293 A a39 = {a39}; // expected-warning {{variable 'a39' is uninitialized when used within its own initialization}} 294 A a40 = A({a40}); // expected-warning {{variable 'a40' is uninitialized when used within its own initialization}} 295 296 A a41 = !a41; // expected-warning {{variable 'a41' is uninitialized when used within its own initialization}} 297 A a42 = !(a42); // expected-warning {{variable 'a42' is uninitialized when used within its own initialization}} 298 A a43 = a43 != a42; // expected-warning {{variable 'a43' is uninitialized when used within its own initialization}} 299 A a44 = a43 != a44; // expected-warning {{variable 'a44' is uninitialized when used within its own initialization}} 300 A a45 = a45 != a45; // expected-warning 2{{variable 'a45' is uninitialized when used within its own initialization}} 301 A a46 = 0 != a46; // expected-warning {{variable 'a46' is uninitialized when used within its own initialization}} 302 303 A a47(a47.set(a47.num)); // expected-warning 2{{variable 'a47' is uninitialized when used within its own initialization}} 304 A a48(a47.set(a48.num)); // expected-warning {{variable 'a48' is uninitialized when used within its own initialization}} 305 A a49(a47.set(a48.num)); 306 } 307 308 bool cond; 309 310 A a1; 311 A a2(a1.get()); 312 A a3(a1); 313 A a4(&a4); 314 A a5(a5.zero()); 315 A a6(a6.ONE); 316 A a7 = getA(); 317 A a8 = getA(a8.TWO); 318 A a9 = getA(&a9); 319 A a10(a10.count); 320 321 A a11(a11); // expected-warning {{variable 'a11' is uninitialized when used within its own initialization}} 322 A a12(a12.get()); // expected-warning {{variable 'a12' is uninitialized when used within its own initialization}} 323 A a13(a13.num); // expected-warning {{variable 'a13' is uninitialized when used within its own initialization}} 324 A a14 = A(a14); // expected-warning {{variable 'a14' is uninitialized when used within its own initialization}} 325 A a15 = getA(a15.num); // expected-warning {{variable 'a15' is uninitialized when used within its own initialization}} 326 A a16(&a16.num); // expected-warning {{variable 'a16' is uninitialized when used within its own initialization}} 327 A a17(a17.get2()); // expected-warning {{variable 'a17' is uninitialized when used within its own initialization}} 328 A a18 = cond ? a18 : a17; // expected-warning {{variable 'a18' is uninitialized when used within its own initialization}} 329 A a19 = getA(cond ? a19 : a17); // expected-warning {{variable 'a19' is uninitialized when used within its own initialization}} 330 A a20{a20}; // expected-warning {{variable 'a20' is uninitialized when used within its own initialization}} 331 A a21 = {a21}; // expected-warning {{variable 'a21' is uninitialized when used within its own initialization}} 332 333 A *a22 = new A(a22->count); 334 A *a23 = new A(a23->ONE); 335 A *a24 = new A(a24->TWO); 336 A *a25 = new A(a25->zero()); 337 338 A *a26 = new A(a26->get()); // expected-warning {{variable 'a26' is uninitialized when used within its own initialization}} 339 A *a27 = new A(a27->get2()); // expected-warning {{variable 'a27' is uninitialized when used within its own initialization}} 340 A *a28 = new A(a28->num); // expected-warning {{variable 'a28' is uninitialized when used within its own initialization}} 341 342 const A a29(a29); // expected-warning {{variable 'a29' is uninitialized when used within its own initialization}} 343 const A a30 = a30; // expected-warning {{variable 'a30' is uninitialized when used within its own initialization}} 344 345 A a31 = std::move(a31); // expected-warning {{variable 'a31' is uninitialized when used within its own initialization}} 346 A a32 = moveA(std::move(a32)); // expected-warning {{variable 'a32' is uninitialized when used within its own initialization}} 347 A a33 = A(std::move(a33)); // expected-warning {{variable 'a33' is uninitialized when used within its own initialization}} 348 A a34(std::move(a34)); // expected-warning {{variable 'a34' is uninitialized when used within its own initialization}} 349 A a35 = std::move(x ? a34 : (37, a35)); // expected-warning {{variable 'a35' is uninitialized when used within its own initialization}} 350 351 A a36 = const_refA(a36); 352 A a37(const_refA(a37)); 353 354 A a38({a38}); // expected-warning {{variable 'a38' is uninitialized when used within its own initialization}} 355 A a39 = {a39}; // expected-warning {{variable 'a39' is uninitialized when used within its own initialization}} 356 A a40 = A({a40}); // expected-warning {{variable 'a40' is uninitialized when used within its own initialization}} 357 358 A a41 = !a41; // expected-warning {{variable 'a41' is uninitialized when used within its own initialization}} 359 A a42 = !(a42); // expected-warning {{variable 'a42' is uninitialized when used within its own initialization}} 360 A a43 = a43 != a42; // expected-warning {{variable 'a43' is uninitialized when used within its own initialization}} 361 A a44 = a43 != a44; // expected-warning {{variable 'a44' is uninitialized when used within its own initialization}} 362 A a45 = a45 != a45; // expected-warning 2{{variable 'a45' is uninitialized when used within its own initialization}} 363 364 A a46 = 0 != a46; // expected-warning {{variable 'a46' is uninitialized when used within its own initialization}} 365 366 A a47(a47.set(a47.num)); // expected-warning 2{{variable 'a47' is uninitialized when used within its own initialization}} 367 A a48(a47.set(a48.num)); // expected-warning {{variable 'a48' is uninitialized when used within its own initialization}} 368 A a49(a47.set(a48.num)); 369 370 class T { 371 A a, a2; 372 const A c_a; 373 A* ptr_a; 374 375 T() {} 376 T(bool (*)[1]) : a() {} 377 T(bool (*)[2]) : a2(a.get()) {} 378 T(bool (*)[3]) : a2(a) {} 379 T(bool (*)[4]) : a(&a) {} 380 T(bool (*)[5]) : a(a.zero()) {} 381 T(bool (*)[6]) : a(a.ONE) {} 382 T(bool (*)[7]) : a(getA()) {} 383 T(bool (*)[8]) : a2(getA(a.TWO)) {} 384 T(bool (*)[9]) : a(getA(&a)) {} 385 T(bool (*)[10]) : a(a.count) {} 386 387 T(bool (*)[11]) : a(a) {} // expected-warning {{field 'a' is uninitialized when used here}} 388 T(bool (*)[12]) : a(a.get()) {} // expected-warning {{field 'a' is uninitialized when used here}} 389 T(bool (*)[13]) : a(a.num) {} // expected-warning {{field 'a' is uninitialized when used here}} 390 T(bool (*)[14]) : a(A(a)) {} // expected-warning {{field 'a' is uninitialized when used here}} 391 T(bool (*)[15]) : a(getA(a.num)) {} // expected-warning {{field 'a' is uninitialized when used here}} 392 T(bool (*)[16]) : a(&a.num) {} // expected-warning {{field 'a' is uninitialized when used here}} 393 T(bool (*)[17]) : a(a.get2()) {} // expected-warning {{field 'a' is uninitialized when used here}} 394 T(bool (*)[18]) : a2(cond ? a2 : a) {} // expected-warning {{field 'a2' is uninitialized when used here}} 395 T(bool (*)[19]) : a2(cond ? a2 : a) {} // expected-warning {{field 'a2' is uninitialized when used here}} 396 T(bool (*)[20]) : a{a} {} // expected-warning {{field 'a' is uninitialized when used here}} 397 T(bool (*)[21]) : a({a}) {} // expected-warning {{field 'a' is uninitialized when used here}} 398 399 T(bool (*)[22]) : ptr_a(new A(ptr_a->count)) {} 400 T(bool (*)[23]) : ptr_a(new A(ptr_a->ONE)) {} 401 T(bool (*)[24]) : ptr_a(new A(ptr_a->TWO)) {} 402 T(bool (*)[25]) : ptr_a(new A(ptr_a->zero())) {} 403 404 T(bool (*)[26]) : ptr_a(new A(ptr_a->get())) {} // expected-warning {{field 'ptr_a' is uninitialized when used here}} 405 T(bool (*)[27]) : ptr_a(new A(ptr_a->get2())) {} // expected-warning {{field 'ptr_a' is uninitialized when used here}} 406 T(bool (*)[28]) : ptr_a(new A(ptr_a->num)) {} // expected-warning {{field 'ptr_a' is uninitialized when used here}} 407 408 T(bool (*)[29]) : c_a(c_a) {} // expected-warning {{field 'c_a' is uninitialized when used here}} 409 T(bool (*)[30]) : c_a(A(c_a)) {} // expected-warning {{field 'c_a' is uninitialized when used here}} 410 411 T(bool (*)[31]) : a(std::move(a)) {} // expected-warning {{field 'a' is uninitialized when used here}} 412 T(bool (*)[32]) : a(moveA(std::move(a))) {} // expected-warning {{field 'a' is uninitialized when used here}} 413 T(bool (*)[33]) : a(A(std::move(a))) {} // expected-warning {{field 'a' is uninitialized when used here}} 414 T(bool (*)[34]) : a(A(std::move(a))) {} // expected-warning {{field 'a' is uninitialized when used here}} 415 T(bool (*)[35]) : a2(std::move(x ? a : (37, a2))) {} // expected-warning {{field 'a2' is uninitialized when used here}} 416 417 T(bool (*)[36]) : a(const_refA(a)) {} 418 T(bool (*)[37]) : a(A(const_refA(a))) {} 419 420 T(bool (*)[38]) : a({a}) {} // expected-warning {{field 'a' is uninitialized when used here}} 421 T(bool (*)[39]) : a{a} {} // expected-warning {{field 'a' is uninitialized when used here}} 422 T(bool (*)[40]) : a({a}) {} // expected-warning {{field 'a' is uninitialized when used here}} 423 424 T(bool (*)[41]) : a(!a) {} // expected-warning {{field 'a' is uninitialized when used here}} 425 T(bool (*)[42]) : a(!(a)) {} // expected-warning {{field 'a' is uninitialized when used here}} 426 T(bool (*)[43]) : a(), a2(a2 != a) {} // expected-warning {{field 'a2' is uninitialized when used here}} 427 T(bool (*)[44]) : a(), a2(a != a2) {} // expected-warning {{field 'a2' is uninitialized when used here}} 428 T(bool (*)[45]) : a(a != a) {} // expected-warning 2{{field 'a' is uninitialized when used here}} 429 T(bool (*)[46]) : a(0 != a) {} // expected-warning {{field 'a' is uninitialized when used here}} 430 431 T(bool (*)[47]) : a2(a2.set(a2.num)) {} // expected-warning 2{{field 'a2' is uninitialized when used here}} 432 T(bool (*)[48]) : a2(a.set(a2.num)) {} // expected-warning {{field 'a2' is uninitialized when used here}} 433 T(bool (*)[49]) : a2(a.set(a.num)) {} 434 435 }; 436 437 struct B { 438 // POD struct. 439 int x; 440 int *y; 441 }; 442 443 B getB() { return B(); }; 444 B getB(int x) { return B(); }; 445 B getB(int *x) { return B(); }; 446 B getB(B *b) { return B(); }; 447 B moveB(B &&b) { return B(); }; 448 449 B* getPtrB() { return 0; }; 450 B* getPtrB(int x) { return 0; }; 451 B* getPtrB(int *x) { return 0; }; 452 B* getPtrB(B **b) { return 0; }; 453 454 void setupB(bool x) { 455 B b1; 456 B b2(b1); 457 B b3 = { 5, &b3.x }; 458 B b4 = getB(); 459 B b5 = getB(&b5); 460 B b6 = getB(&b6.x); 461 462 // Silence unused warning 463 (void) b2; 464 (void) b4; 465 466 B b7(b7); // expected-warning {{variable 'b7' is uninitialized when used within its own initialization}} 467 B b8 = getB(b8.x); // expected-warning {{variable 'b8' is uninitialized when used within its own initialization}} 468 B b9 = getB(b9.y); // expected-warning {{variable 'b9' is uninitialized when used within its own initialization}} 469 B b10 = getB(-b10.x); // expected-warning {{variable 'b10' is uninitialized when used within its own initialization}} 470 471 B* b11 = 0; 472 B* b12(b11); 473 B* b13 = getPtrB(); 474 B* b14 = getPtrB(&b14); 475 476 (void) b12; 477 (void) b13; 478 479 B* b15 = getPtrB(b15->x); // expected-warning {{variable 'b15' is uninitialized when used within its own initialization}} 480 B* b16 = getPtrB(b16->y); // expected-warning {{variable 'b16' is uninitialized when used within its own initialization}} 481 482 B b17 = { b17.x = 5, b17.y = 0 }; 483 B b18 = { b18.x + 1, b18.y }; // expected-warning 2{{variable 'b18' is uninitialized when used within its own initialization}} 484 485 const B b19 = b19; // expected-warning {{variable 'b19' is uninitialized when used within its own initialization}} 486 const B b20(b20); // expected-warning {{variable 'b20' is uninitialized when used within its own initialization}} 487 488 B b21 = std::move(b21); // expected-warning {{variable 'b21' is uninitialized when used within its own initialization}} 489 B b22 = moveB(std::move(b22)); // expected-warning {{variable 'b22' is uninitialized when used within its own initialization}} 490 B b23 = B(std::move(b23)); // expected-warning {{variable 'b23' is uninitialized when used within its own initialization}} 491 B b24 = std::move(x ? b23 : (18, b24)); // expected-warning {{variable 'b24' is uninitialized when used within its own initialization}} 492 } 493 494 B b1; 495 B b2(b1); 496 B b3 = { 5, &b3.x }; 497 B b4 = getB(); 498 B b5 = getB(&b5); 499 B b6 = getB(&b6.x); 500 501 B b7(b7); // expected-warning {{variable 'b7' is uninitialized when used within its own initialization}} 502 B b8 = getB(b8.x); // expected-warning {{variable 'b8' is uninitialized when used within its own initialization}} 503 B b9 = getB(b9.y); // expected-warning {{variable 'b9' is uninitialized when used within its own initialization}} 504 B b10 = getB(-b10.x); // expected-warning {{variable 'b10' is uninitialized when used within its own initialization}} 505 506 B* b11 = 0; 507 B* b12(b11); 508 B* b13 = getPtrB(); 509 B* b14 = getPtrB(&b14); 510 511 B* b15 = getPtrB(b15->x); // expected-warning {{variable 'b15' is uninitialized when used within its own initialization}} 512 B* b16 = getPtrB(b16->y); // expected-warning {{variable 'b16' is uninitialized when used within its own initialization}} 513 514 B b17 = { b17.x = 5, b17.y = 0 }; 515 B b18 = { b18.x + 1, b18.y }; // expected-warning 2{{variable 'b18' is uninitialized when used within its own initialization}} 516 517 const B b19 = b19; // expected-warning {{variable 'b19' is uninitialized when used within its own initialization}} 518 const B b20(b20); // expected-warning {{variable 'b20' is uninitialized when used within its own initialization}} 519 520 B b21 = std::move(b21); // expected-warning {{variable 'b21' is uninitialized when used within its own initialization}} 521 B b22 = moveB(std::move(b22)); // expected-warning {{variable 'b22' is uninitialized when used within its own initialization}} 522 B b23 = B(std::move(b23)); // expected-warning {{variable 'b23' is uninitialized when used within its own initialization}} 523 B b24 = std::move(x ? b23 : (18, b24)); // expected-warning {{variable 'b24' is uninitialized when used within its own initialization}} 524 525 class U { 526 B b1, b2; 527 B *ptr1, *ptr2; 528 const B constb = {}; 529 530 U() {} 531 U(bool (*)[1]) : b1() {} 532 U(bool (*)[2]) : b2(b1) {} 533 U(bool (*)[3]) : b1{ 5, &b1.x } {} 534 U(bool (*)[4]) : b1(getB()) {} 535 U(bool (*)[5]) : b1(getB(&b1)) {} 536 U(bool (*)[6]) : b1(getB(&b1.x)) {} 537 538 U(bool (*)[7]) : b1(b1) {} // expected-warning {{field 'b1' is uninitialized when used here}} 539 U(bool (*)[8]) : b1(getB(b1.x)) {} // expected-warning {{field 'b1' is uninitialized when used here}} 540 U(bool (*)[9]) : b1(getB(b1.y)) {} // expected-warning {{field 'b1' is uninitialized when used here}} 541 U(bool (*)[10]) : b1(getB(-b1.x)) {} // expected-warning {{field 'b1' is uninitialized when used here}} 542 543 U(bool (*)[11]) : ptr1(0) {} 544 U(bool (*)[12]) : ptr1(0), ptr2(ptr1) {} 545 U(bool (*)[13]) : ptr1(getPtrB()) {} 546 U(bool (*)[14]) : ptr1(getPtrB(&ptr1)) {} 547 548 U(bool (*)[15]) : ptr1(getPtrB(ptr1->x)) {} // expected-warning {{field 'ptr1' is uninitialized when used here}} 549 U(bool (*)[16]) : ptr2(getPtrB(ptr2->y)) {} // expected-warning {{field 'ptr2' is uninitialized when used here}} 550 551 U(bool (*)[17]) : b1 { b1.x = 5, b1.y = 0 } {} 552 U(bool (*)[18]) : b1 { b1.x + 1, b1.y } {} // expected-warning 2{{field 'b1' is uninitialized when used here}} 553 554 U(bool (*)[19]) : constb(constb) {} // expected-warning {{field 'constb' is uninitialized when used here}} 555 U(bool (*)[20]) : constb(B(constb)) {} // expected-warning {{field 'constb' is uninitialized when used here}} 556 557 U(bool (*)[21]) : b1(std::move(b1)) {} // expected-warning {{field 'b1' is uninitialized when used here}} 558 U(bool (*)[22]) : b1(moveB(std::move(b1))) {} // expected-warning {{field 'b1' is uninitialized when used here}} 559 U(bool (*)[23]) : b1(B(std::move(b1))) {} // expected-warning {{field 'b1' is uninitialized when used here}} 560 U(bool (*)[24]) : b2(std::move(x ? b1 : (18, b2))) {} // expected-warning {{field 'b2' is uninitialized when used here}} 561 }; 562 563 struct C { char a[100], *e; } car = { .e = car.a }; 564 565 namespace rdar10398199 { 566 class FooBase { protected: ~FooBase() {} }; 567 class Foo : public FooBase { 568 public: 569 operator int&() const; 570 }; 571 void stuff(); 572 template <typename T> class FooImpl : public Foo { 573 T val; 574 public: 575 FooImpl(const T &x) : val(x) {} 576 ~FooImpl() { stuff(); } 577 }; 578 579 template <typename T> FooImpl<T> makeFoo(const T& x) { 580 return FooImpl<T>(x); 581 } 582 583 void test() { 584 const Foo &x = makeFoo(42); 585 const int&y = makeFoo(42u); 586 (void)x; 587 (void)y; 588 }; 589 } 590 591 // PR 12325 - this was a false uninitialized value warning due to 592 // a broken CFG. 593 int pr12325(int params) { 594 int x = ({ 595 while (false) 596 ; 597 int _v = params; 598 if (false) 599 ; 600 _v; // no-warning 601 }); 602 return x; 603 } 604 605 // Test lambda expressions with -Wuninitialized 606 int test_lambda() { 607 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}} 608 return f1(1, 2); 609 } 610 611 namespace { 612 struct A { 613 enum { A1 }; 614 static int A2() {return 5;} 615 int A3; 616 int A4() { return 5;} 617 }; 618 619 struct B { 620 A a; 621 }; 622 623 struct C { 624 C() {} 625 C(int x) {} 626 static A a; 627 B b; 628 }; 629 A C::a = A(); 630 631 // Accessing non-static members will give a warning. 632 struct D { 633 C c; 634 D(char (*)[1]) : c(c.b.a.A1) {} 635 D(char (*)[2]) : c(c.b.a.A2()) {} 636 D(char (*)[3]) : c(c.b.a.A3) {} // expected-warning {{field 'c' is uninitialized when used here}} 637 D(char (*)[4]) : c(c.b.a.A4()) {} // expected-warning {{field 'c' is uninitialized when used here}} 638 639 // c::a is static, so it is already initialized 640 D(char (*)[5]) : c(c.a.A1) {} 641 D(char (*)[6]) : c(c.a.A2()) {} 642 D(char (*)[7]) : c(c.a.A3) {} 643 D(char (*)[8]) : c(c.a.A4()) {} 644 }; 645 646 struct E { 647 int b = 1; 648 int c = 1; 649 int a; // This field needs to be last to prevent the cross field 650 // uninitialized warning. 651 E(char (*)[1]) : a(a ? b : c) {} // expected-warning {{field 'a' is uninitialized when used here}} 652 E(char (*)[2]) : a(b ? a : a) {} // expected-warning 2{{field 'a' is uninitialized when used here}} 653 E(char (*)[3]) : a(b ? (a) : c) {} // expected-warning {{field 'a' is uninitialized when used here}} 654 E(char (*)[4]) : a(b ? c : (a+c)) {} // expected-warning {{field 'a' is uninitialized when used here}} 655 E(char (*)[5]) : a(b ? c : b) {} 656 657 E(char (*)[6]) : a(a ?: a) {} // expected-warning 2{{field 'a' is uninitialized when used here}} 658 E(char (*)[7]) : a(b ?: a) {} // expected-warning {{field 'a' is uninitialized when used here}} 659 E(char (*)[8]) : a(a ?: c) {} // expected-warning {{field 'a' is uninitialized when used here}} 660 E(char (*)[9]) : a(b ?: c) {} 661 662 E(char (*)[10]) : a((a, a, b)) {} 663 E(char (*)[11]) : a((c + a, a + 1, b)) {} // expected-warning 2{{field 'a' is uninitialized when used here}} 664 E(char (*)[12]) : a((b + c, c, a)) {} // expected-warning {{field 'a' is uninitialized when used here}} 665 E(char (*)[13]) : a((a, a, a, a)) {} // expected-warning {{field 'a' is uninitialized when used here}} 666 E(char (*)[14]) : a((b, c, c)) {} 667 E(char (*)[15]) : a(b ?: a) {} // expected-warning {{field 'a' is uninitialized when used here}} 668 E(char (*)[16]) : a(a ?: b) {} // expected-warning {{field 'a' is uninitialized when used here}} 669 }; 670 671 struct F { 672 int a; 673 F* f; 674 F(int) {} 675 F() {} 676 }; 677 678 int F::*ptr = &F::a; 679 F* F::*f_ptr = &F::f; 680 struct G { 681 F f1, f2; 682 F *f3, *f4; 683 G(char (*)[1]) : f1(f1) {} // expected-warning {{field 'f1' is uninitialized when used here}} 684 G(char (*)[2]) : f2(f1) {} 685 G(char (*)[3]) : f2(F()) {} 686 687 G(char (*)[4]) : f1(f1.*ptr) {} // expected-warning {{field 'f1' is uninitialized when used here}} 688 G(char (*)[5]) : f2(f1.*ptr) {} 689 690 G(char (*)[6]) : f3(f3) {} // expected-warning {{field 'f3' is uninitialized when used here}} 691 G(char (*)[7]) : f3(f3->*f_ptr) {} // expected-warning {{field 'f3' is uninitialized when used here}} 692 G(char (*)[8]) : f3(new F(f3->*ptr)) {} // expected-warning {{field 'f3' is uninitialized when used here}} 693 }; 694 695 struct H { 696 H() : a(a) {} // expected-warning {{field 'a' is uninitialized when used here}} 697 const A a; 698 }; 699 } 700 701 namespace statics { 702 static int a = a; // no-warning: used to signal intended lack of initialization. 703 static int b = b + 1; // expected-warning {{variable 'b' is uninitialized when used within its own initialization}} 704 static int c = (c + c); // expected-warning 2{{variable 'c' is uninitialized when used within its own initialization}} 705 static int e = static_cast<long>(e) + 1; // expected-warning {{variable 'e' is uninitialized when used within its own initialization}} 706 static int f = foo(f); // expected-warning {{variable 'f' is uninitialized when used within its own initialization}} 707 708 // These don't warn as they don't require the value. 709 static int g = sizeof(g); 710 int gg = g; // Silence unneeded warning 711 static void* ptr = &ptr; 712 static int h = bar(&h); 713 static int i = boo(i); 714 static int j = far(j); 715 static int k = __alignof__(k); 716 717 static int l = k ? l : l; // expected-warning 2{{variable 'l' is uninitialized when used within its own initialization}} 718 static int m = 1 + (k ? m : m); // expected-warning 2{{variable 'm' is uninitialized when used within its own initialization}} 719 static int n = -n; // expected-warning {{variable 'n' is uninitialized when used within its own initialization}} 720 static int o = std::move(o); // expected-warning {{variable 'o' is uninitialized when used within its own initialization}} 721 static const int p = std::move(p); // expected-warning {{variable 'p' is uninitialized when used within its own initialization}} 722 static int q = moved(std::move(q)); // expected-warning {{variable 'q' is uninitialized when used within its own initialization}} 723 static int r = std::move((p ? q : (18, r))); // expected-warning {{variable 'r' is uninitialized when used within its own initialization}} 724 static int s = r ?: s; // expected-warning {{variable 's' is uninitialized when used within its own initialization}} 725 static int t = t ?: s; // expected-warning {{variable 't' is uninitialized when used within its own initialization}} 726 static int u = (foo(u), s); // expected-warning {{variable 'u' is uninitialized when used within its own initialization}} 727 static int v = (u += v); // expected-warning {{variable 'v' is uninitialized when used within its own initialization}} 728 static int w = (w += 10); // expected-warning {{variable 'w' is uninitialized when used within its own initialization}} 729 static int x = x++; // expected-warning {{variable 'x' is uninitialized when used within its own initialization}} 730 static int y = ((s ? (y, v) : (77, y))++, sizeof(y)); // expected-warning {{variable 'y' is uninitialized when used within its own initialization}} 731 static int z = ++ref(z); // expected-warning {{variable 'z' is uninitialized when used within its own initialization}} 732 static int aa = (ref(aa) += 10); // expected-warning {{variable 'aa' is uninitialized when used within its own initialization}} 733 static int bb = bb ? x : y; // expected-warning {{variable 'bb' is uninitialized when used within its own initialization}} 734 735 736 void test() { 737 static int a = a; // no-warning: used to signal intended lack of initialization. 738 static int b = b + 1; // expected-warning {{static variable 'b' is suspiciously used within its own initialization}} 739 static int c = (c + c); // expected-warning 2{{static variable 'c' is suspiciously used within its own initialization}} 740 static int d = ({ d + d ;}); // expected-warning 2{{static variable 'd' is suspiciously used within its own initialization}} 741 static int e = static_cast<long>(e) + 1; // expected-warning {{static variable 'e' is suspiciously used within its own initialization}} 742 static int f = foo(f); // expected-warning {{static variable 'f' is suspiciously used within its own initialization}} 743 744 // These don't warn as they don't require the value. 745 static int g = sizeof(g); 746 static void* ptr = &ptr; 747 static int h = bar(&h); 748 static int i = boo(i); 749 static int j = far(j); 750 static int k = __alignof__(k); 751 752 static int l = k ? l : l; // expected-warning 2{{static variable 'l' is suspiciously used within its own initialization}} 753 static int m = 1 + (k ? m : m); // expected-warning 2{{static variable 'm' is suspiciously used within its own initialization}} 754 static int n = -n; // expected-warning {{static variable 'n' is suspiciously used within its own initialization}} 755 static int o = std::move(o); // expected-warning {{static variable 'o' is suspiciously used within its own initialization}} 756 static const int p = std::move(p); // expected-warning {{static variable 'p' is suspiciously used within its own initialization}} 757 static int q = moved(std::move(q)); // expected-warning {{static variable 'q' is suspiciously used within its own initialization}} 758 static int r = std::move((p ? q : (18, r))); // expected-warning {{static variable 'r' is suspiciously used within its own initialization}} 759 static int s = r ?: s; // expected-warning {{static variable 's' is suspiciously used within its own initialization}} 760 static int t = t ?: s; // expected-warning {{static variable 't' is suspiciously used within its own initialization}} 761 static int u = (foo(u), s); // expected-warning {{static variable 'u' is suspiciously used within its own initialization}} 762 static int v = (u += v); // expected-warning {{static variable 'v' is suspiciously used within its own initialization}} 763 static int w = (w += 10); // expected-warning {{static variable 'w' is suspiciously used within its own initialization}} 764 static int x = x++; // expected-warning {{static variable 'x' is suspiciously used within its own initialization}} 765 static int y = ((s ? (y, v) : (77, y))++, sizeof(y)); // expected-warning {{static variable 'y' is suspiciously used within its own initialization}} 766 static int z = ++ref(z); // expected-warning {{static variable 'z' is suspiciously used within its own initialization}} 767 static int aa = (ref(aa) += 10); // expected-warning {{static variable 'aa' is suspiciously used within its own initialization}} 768 static int bb = bb ? x : y; // expected-warning {{static variable 'bb' is suspiciously used within its own initialization}} 769 770 for (;;) { 771 static int a = a; // no-warning: used to signal intended lack of initialization. 772 static int b = b + 1; // expected-warning {{static variable 'b' is suspiciously used within its own initialization}} 773 static int c = (c + c); // expected-warning 2{{static variable 'c' is suspiciously used within its own initialization}} 774 static int d = ({ d + d ;}); // expected-warning 2{{static variable 'd' is suspiciously used within its own initialization}} 775 static int e = static_cast<long>(e) + 1; // expected-warning {{static variable 'e' is suspiciously used within its own initialization}} 776 static int f = foo(f); // expected-warning {{static variable 'f' is suspiciously used within its own initialization}} 777 778 // These don't warn as they don't require the value. 779 static int g = sizeof(g); 780 static void* ptr = &ptr; 781 static int h = bar(&h); 782 static int i = boo(i); 783 static int j = far(j); 784 static int k = __alignof__(k); 785 786 static int l = k ? l : l; // expected-warning 2{{static variable 'l' is suspiciously used within its own initialization}} 787 static int m = 1 + (k ? m : m); // expected-warning 2{{static variable 'm' is suspiciously used within its own initialization}} 788 static int n = -n; // expected-warning {{static variable 'n' is suspiciously used within its own initialization}} 789 static int o = std::move(o); // expected-warning {{static variable 'o' is suspiciously used within its own initialization}} 790 static const int p = std::move(p); // expected-warning {{static variable 'p' is suspiciously used within its own initialization}} 791 static int q = moved(std::move(q)); // expected-warning {{static variable 'q' is suspiciously used within its own initialization}} 792 static int r = std::move((p ? q : (18, r))); // expected-warning {{static variable 'r' is suspiciously used within its own initialization}} 793 static int s = r ?: s; // expected-warning {{static variable 's' is suspiciously used within its own initialization}} 794 static int t = t ?: s; // expected-warning {{static variable 't' is suspiciously used within its own initialization}} 795 static int u = (foo(u), s); // expected-warning {{static variable 'u' is suspiciously used within its own initialization}} 796 static int v = (u += v); // expected-warning {{static variable 'v' is suspiciously used within its own initialization}} 797 static int w = (w += 10); // expected-warning {{static variable 'w' is suspiciously used within its own initialization}} 798 static int x = x++; // expected-warning {{static variable 'x' is suspiciously used within its own initialization}} 799 static int y = ((s ? (y, v) : (77, y))++, sizeof(y)); // expected-warning {{static variable 'y' is suspiciously used within its own initialization}} 800 static int z = ++ref(z); // expected-warning {{static variable 'z' is suspiciously used within its own initialization}} 801 static int aa = (ref(aa) += 10); // expected-warning {{static variable 'aa' is suspiciously used within its own initialization}} 802 static int bb = bb ? x : y; // expected-warning {{static variable 'bb' is suspiciously used within its own initialization}} 803 } 804 } 805 } 806 807 namespace in_class_initializers { 808 struct S { 809 S() : a(a + 1) {} // expected-warning{{field 'a' is uninitialized when used here}} 810 int a = 42; // Note: because a is in a member initializer list, this initialization is ignored. 811 }; 812 813 struct T { 814 T() : b(a + 1) {} // No-warning. 815 int a = 42; 816 int b; 817 }; 818 819 struct U { 820 U() : a(b + 1), b(a + 1) {} // expected-warning{{field 'b' is uninitialized when used here}} 821 int a = 42; // Note: because a and b are in the member initializer list, these initializers are ignored. 822 int b = 1; 823 }; 824 } 825 826 namespace references { 827 int &a = a; // expected-warning{{reference 'a' is not yet bound to a value when used within its own initialization}} 828 int &b(b); // expected-warning{{reference 'b' is not yet bound to a value when used within its own initialization}} 829 int &c = a ? b : c; // expected-warning{{reference 'c' is not yet bound to a value when used within its own initialization}} 830 int &d{d}; // expected-warning{{reference 'd' is not yet bound to a value when used within its own initialization}} 831 int &e = d ?: e; // expected-warning{{reference 'e' is not yet bound to a value when used within its own initialization}} 832 int &f = f ?: d; // expected-warning{{reference 'f' is not yet bound to a value when used within its own initialization}} 833 834 int &return_ref1(int); 835 int &return_ref2(int&); 836 837 int &g = return_ref1(g); // expected-warning{{reference 'g' is not yet bound to a value when used within its own initialization}} 838 int &h = return_ref2(h); // expected-warning{{reference 'h' is not yet bound to a value when used within its own initialization}} 839 840 struct S { 841 S() : a(a) {} // expected-warning{{reference 'a' is not yet bound to a value when used here}} 842 int &a; 843 }; 844 845 void test() { 846 int &a = a; // expected-warning{{reference 'a' is not yet bound to a value when used within its own initialization}} 847 int &b(b); // expected-warning{{reference 'b' is not yet bound to a value when used within its own initialization}} 848 int &c = a ? b : c; // expected-warning{{reference 'c' is not yet bound to a value when used within its own initialization}} 849 int &d{d}; // expected-warning{{reference 'd' is not yet bound to a value when used within its own initialization}} 850 } 851 852 struct T { 853 T() // expected-note{{during field initialization in this constructor}} 854 : a(b), b(a) {} // expected-warning{{reference 'b' is not yet bound to a value when used here}} 855 int &a, &b; 856 int &c = c; // expected-warning{{reference 'c' is not yet bound to a value when used here}} 857 }; 858 859 int x; 860 struct U { 861 U() : b(a) {} // No-warning. 862 int &a = x; 863 int &b; 864 }; 865 } 866 867 namespace operators { 868 struct A { 869 A(bool); 870 bool operator==(A); 871 }; 872 873 A makeA(); 874 875 A a1 = a1 = makeA(); // expected-warning{{variable 'a1' is uninitialized when used within its own initialization}} 876 A a2 = a2 == a1; // expected-warning{{variable 'a2' is uninitialized when used within its own initialization}} 877 A a3 = a2 == a3; // expected-warning{{variable 'a3' is uninitialized when used within its own initialization}} 878 879 int x = x = 5; 880 } 881 882 namespace lambdas { 883 struct A { 884 template<typename T> A(T) {} 885 int x; 886 }; 887 A a0([] { return a0.x; }); // ok 888 void f() { 889 A a1([=] { // expected-warning{{variable 'a1' is uninitialized when used within its own initialization}} 890 return a1.x; 891 }); 892 A a2([&] { return a2.x; }); // ok 893 } 894 } 895 896 namespace record_fields { 897 bool x; 898 struct A { 899 A() {} 900 A get(); 901 static A num(); 902 static A copy(A); 903 static A something(A&); 904 }; 905 906 A ref(A&); 907 A const_ref(const A&); 908 A pointer(A*); 909 A normal(A); 910 A rref(A&&); 911 912 struct B { 913 A a; 914 B(char (*)[1]) : a(a) {} // expected-warning {{uninitialized}} 915 B(char (*)[2]) : a(a.get()) {} // expected-warning {{uninitialized}} 916 B(char (*)[3]) : a(a.num()) {} 917 B(char (*)[4]) : a(a.copy(a)) {} // expected-warning {{uninitialized}} 918 B(char (*)[5]) : a(a.something(a)) {} 919 B(char (*)[6]) : a(ref(a)) {} 920 B(char (*)[7]) : a(const_ref(a)) {} 921 B(char (*)[8]) : a(pointer(&a)) {} 922 B(char (*)[9]) : a(normal(a)) {} // expected-warning {{uninitialized}} 923 B(char (*)[10]) : a(std::move(a)) {} // expected-warning {{uninitialized}} 924 B(char (*)[11]) : a(A(std::move(a))) {} // expected-warning {{uninitialized}} 925 B(char (*)[12]) : a(rref(std::move(a))) {} // expected-warning {{uninitialized}} 926 B(char (*)[13]) : a(std::move(x ? a : (25, a))) {} // expected-warning 2{{uninitialized}} 927 }; 928 struct C { 929 C() {} // expected-note9{{in this constructor}} 930 A a1 = a1; // expected-warning {{uninitialized}} 931 A a2 = a2.get(); // expected-warning {{uninitialized}} 932 A a3 = a3.num(); 933 A a4 = a4.copy(a4); // expected-warning {{uninitialized}} 934 A a5 = a5.something(a5); 935 A a6 = ref(a6); 936 A a7 = const_ref(a7); 937 A a8 = pointer(&a8); 938 A a9 = normal(a9); // expected-warning {{uninitialized}} 939 const A a10 = a10; // expected-warning {{uninitialized}} 940 A a11 = std::move(a11); // expected-warning {{uninitialized}} 941 A a12 = A(std::move(a12)); // expected-warning {{uninitialized}} 942 A a13 = rref(std::move(a13)); // expected-warning {{uninitialized}} 943 A a14 = std::move(x ? a13 : (22, a14)); // expected-warning {{uninitialized}} 944 }; 945 struct D { // expected-note9{{in the implicit default constructor}} 946 A a1 = a1; // expected-warning {{uninitialized}} 947 A a2 = a2.get(); // expected-warning {{uninitialized}} 948 A a3 = a3.num(); 949 A a4 = a4.copy(a4); // expected-warning {{uninitialized}} 950 A a5 = a5.something(a5); 951 A a6 = ref(a6); 952 A a7 = const_ref(a7); 953 A a8 = pointer(&a8); 954 A a9 = normal(a9); // expected-warning {{uninitialized}} 955 const A a10 = a10; // expected-warning {{uninitialized}} 956 A a11 = std::move(a11); // expected-warning {{uninitialized}} 957 A a12 = A(std::move(a12)); // expected-warning {{uninitialized}} 958 A a13 = rref(std::move(a13)); // expected-warning {{uninitialized}} 959 A a14 = std::move(x ? a13 : (22, a14)); // expected-warning {{uninitialized}} 960 }; 961 D d; // expected-note {{in implicit default constructor for 'record_fields::D' first required here}} 962 struct E { 963 A a1 = a1; 964 A a2 = a2.get(); 965 A a3 = a3.num(); 966 A a4 = a4.copy(a4); 967 A a5 = a5.something(a5); 968 A a6 = ref(a6); 969 A a7 = const_ref(a7); 970 A a8 = pointer(&a8); 971 A a9 = normal(a9); 972 const A a10 = a10; 973 A a11 = std::move(a11); 974 A a12 = A(std::move(a12)); 975 A a13 = rref(std::move(a13)); 976 A a14 = std::move(x ? a13 : (22, a14)); 977 }; 978 } 979 980 namespace cross_field_warnings { 981 struct A { 982 int a, b; 983 A() {} 984 A(char (*)[1]) : b(a) {} // expected-warning{{field 'a' is uninitialized when used here}} 985 A(char (*)[2]) : a(b) {} // expected-warning{{field 'b' is uninitialized when used here}} 986 }; 987 988 struct B { 989 int a = b; // expected-warning{{field 'b' is uninitialized when used here}} 990 int b; 991 B() {} // expected-note{{during field initialization in this constructor}} 992 }; 993 994 struct C { 995 int a; 996 int b = a; // expected-warning{{field 'a' is uninitialized when used here}} 997 C(char (*)[1]) : a(5) {} 998 C(char (*)[2]) {} // expected-note{{during field initialization in this constructor}} 999 }; 1000 1001 struct D { 1002 int a; 1003 int &b; 1004 int &c = a; 1005 int d = b; 1006 D() : b(a) {} 1007 }; 1008 1009 struct E { 1010 int a; 1011 int get(); 1012 static int num(); 1013 E() {} 1014 E(int) {} 1015 }; 1016 1017 struct F { 1018 int a; 1019 E e; 1020 int b; 1021 F(char (*)[1]) : a(e.get()) {} // expected-warning{{field 'e' is uninitialized when used here}} 1022 F(char (*)[2]) : a(e.num()) {} 1023 F(char (*)[3]) : e(a) {} // expected-warning{{field 'a' is uninitialized when used here}} 1024 F(char (*)[4]) : a(4), e(a) {} 1025 F(char (*)[5]) : e(b) {} // expected-warning{{field 'b' is uninitialized when used here}} 1026 F(char (*)[6]) : e(b), b(4) {} // expected-warning{{field 'b' is uninitialized when used here}} 1027 }; 1028 1029 struct G { 1030 G(const A&) {}; 1031 }; 1032 1033 struct H { 1034 A a1; 1035 G g; 1036 A a2; 1037 H() : g(a1) {} 1038 H(int) : g(a2) {} 1039 }; 1040 1041 struct I { 1042 I(int*) {} 1043 }; 1044 1045 struct J : public I { 1046 int *a; 1047 int *b; 1048 int c; 1049 J() : I((a = new int(5))), b(a), c(*a) {} 1050 }; 1051 1052 struct K { 1053 int a = (b = 5); 1054 int b = b + 5; 1055 }; 1056 1057 struct L { 1058 int a = (b = 5); 1059 int b = b + 5; // expected-warning{{field 'b' is uninitialized when used here}} 1060 L() : a(5) {} // expected-note{{during field initialization in this constructor}} 1061 }; 1062 1063 struct M { }; 1064 1065 struct N : public M { 1066 int a; 1067 int b; 1068 N() : b(a) { } // expected-warning{{field 'a' is uninitialized when used here}} 1069 }; 1070 1071 struct O { 1072 int x = 42; 1073 int get() { return x; } 1074 }; 1075 1076 struct P { 1077 O o; 1078 int x = o.get(); 1079 P() : x(o.get()) { } 1080 }; 1081 1082 struct Q { 1083 int a; 1084 int b; 1085 int &c; 1086 Q() : 1087 a(c = 5), // expected-warning{{reference 'c' is not yet bound to a value when used here}} 1088 b(c), // expected-warning{{reference 'c' is not yet bound to a value when used here}} 1089 c(a) {} 1090 }; 1091 1092 struct R { 1093 int a; 1094 int b; 1095 int c; 1096 int d = a + b + c; 1097 R() : a(c = 5), b(c), c(a) {} 1098 }; 1099 1100 // FIXME: Use the CFG-based analysis to give a sometimes uninitialized 1101 // warning on y. 1102 struct T { 1103 int x; 1104 int y; 1105 T(bool b) 1106 : x(b ? (y = 5) : (1 + y)), // expected-warning{{field 'y' is uninitialized when used here}} 1107 y(y + 1) {} 1108 T(int b) 1109 : x(!b ? (1 + y) : (y = 5)), // expected-warning{{field 'y' is uninitialized when used here}} 1110 y(y + 1) {} 1111 }; 1112 1113 } 1114 1115 namespace base_class { 1116 struct A { 1117 A (int) {} 1118 }; 1119 1120 struct B : public A { 1121 int x; 1122 B() : A(x) {} // expected-warning{{field 'x' is uninitialized when used here}} 1123 }; 1124 1125 struct C : public A { 1126 int x; 1127 int y; 1128 C() : A(y = 4), x(y) {} 1129 }; 1130 } 1131 1132 namespace delegating_constructor { 1133 struct A { 1134 A(int); 1135 A(int&, int); 1136 1137 A(char (*)[1]) : A(x) {} 1138 // expected-warning@-1 {{field 'x' is uninitialized when used here}} 1139 A(char (*)[2]) : A(x, x) {} 1140 // expected-warning@-1 {{field 'x' is uninitialized when used here}} 1141 1142 A(char (*)[3]) : A(x, 0) {} 1143 1144 int x; 1145 }; 1146 } 1147 1148 namespace init_list { 1149 int num = 5; 1150 struct A { int i1, i2; }; 1151 struct B { A a1, a2; }; 1152 1153 A a1{1,2}; 1154 A a2{a2.i1 + 2}; // expected-warning{{uninitialized}} 1155 A a3 = {a3.i1 + 2}; // expected-warning{{uninitialized}} 1156 A a4 = A{a4.i2 + 2}; // expected-warning{{uninitialized}} 1157 1158 B b1 = { {}, {} }; 1159 B b2 = { {}, b2.a1 }; 1160 B b3 = { b3.a1 }; // expected-warning{{uninitialized}} 1161 B b4 = { {}, b4.a2} ; // expected-warning{{uninitialized}} 1162 B b5 = { b5.a2 }; // expected-warning{{uninitialized}} 1163 1164 B b6 = { {b6.a1.i1} }; // expected-warning{{uninitialized}} 1165 B b7 = { {0, b7.a1.i1} }; 1166 B b8 = { {}, {b8.a1.i1} }; 1167 B b9 = { {}, {0, b9.a1.i1} }; 1168 1169 B b10 = { {b10.a1.i2} }; // expected-warning{{uninitialized}} 1170 B b11 = { {0, b11.a1.i2} }; // expected-warning{{uninitialized}} 1171 B b12 = { {}, {b12.a1.i2} }; 1172 B b13 = { {}, {0, b13.a1.i2} }; 1173 1174 B b14 = { {b14.a2.i1} }; // expected-warning{{uninitialized}} 1175 B b15 = { {0, b15.a2.i1} }; // expected-warning{{uninitialized}} 1176 B b16 = { {}, {b16.a2.i1} }; // expected-warning{{uninitialized}} 1177 B b17 = { {}, {0, b17.a2.i1} }; 1178 1179 B b18 = { {b18.a2.i2} }; // expected-warning{{uninitialized}} 1180 B b19 = { {0, b19.a2.i2} }; // expected-warning{{uninitialized}} 1181 B b20 = { {}, {b20.a2.i2} }; // expected-warning{{uninitialized}} 1182 B b21 = { {}, {0, b21.a2.i2} }; // expected-warning{{uninitialized}} 1183 1184 B b22 = { {b18.a2.i2 + 5} }; 1185 1186 struct C {int a; int& b; int c; }; 1187 C c1 = { 0, num, 0 }; 1188 C c2 = { 1, num, c2.b }; 1189 C c3 = { c3.b, num }; // expected-warning{{uninitialized}} 1190 C c4 = { 0, c4.b, 0 }; // expected-warning{{uninitialized}} 1191 C c5 = { 0, c5.c, 0 }; 1192 C c6 = { c6.b, num, 0 }; // expected-warning{{uninitialized}} 1193 C c7 = { 0, c7.a, 0 }; 1194 1195 struct D {int &a; int &b; }; 1196 D d1 = { num, num }; 1197 D d2 = { num, d2.a }; 1198 D d3 = { d3.b, num }; // expected-warning{{uninitialized}} 1199 1200 // Same as above in member initializer form. 1201 struct Awrapper { 1202 A a1{1,2}; 1203 A a2{a2.i1 + 2}; // expected-warning{{uninitialized}} 1204 A a3 = {a3.i1 + 2}; // expected-warning{{uninitialized}} 1205 A a4 = A{a4.i2 + 2}; // expected-warning{{uninitialized}} 1206 Awrapper() {} // expected-note 3{{in this constructor}} 1207 Awrapper(int) : 1208 a1{1,2}, 1209 a2{a2.i1 + 2}, // expected-warning{{uninitialized}} 1210 a3{a3.i1 + 2}, // expected-warning{{uninitialized}} 1211 a4{a4.i2 + 2} // expected-warning{{uninitialized}} 1212 {} 1213 }; 1214 1215 struct Bwrapper { 1216 B b1 = { {}, {} }; 1217 B b2 = { {}, b2.a1 }; 1218 B b3 = { b3.a1 }; // expected-warning{{uninitialized}} 1219 B b4 = { {}, b4.a2} ; // expected-warning{{uninitialized}} 1220 B b5 = { b5.a2 }; // expected-warning{{uninitialized}} 1221 1222 B b6 = { {b6.a1.i1} }; // expected-warning{{uninitialized}} 1223 B b7 = { {0, b7.a1.i1} }; 1224 B b8 = { {}, {b8.a1.i1} }; 1225 B b9 = { {}, {0, b9.a1.i1} }; 1226 1227 B b10 = { {b10.a1.i2} }; // expected-warning{{uninitialized}} 1228 B b11 = { {0, b11.a1.i2} }; // expected-warning{{uninitialized}} 1229 B b12 = { {}, {b12.a1.i2} }; 1230 B b13 = { {}, {0, b13.a1.i2} }; 1231 1232 B b14 = { {b14.a2.i1} }; // expected-warning{{uninitialized}} 1233 B b15 = { {0, b15.a2.i1} }; // expected-warning{{uninitialized}} 1234 B b16 = { {}, {b16.a2.i1} }; // expected-warning{{uninitialized}} 1235 B b17 = { {}, {0, b17.a2.i1} }; 1236 1237 B b18 = { {b18.a2.i2} }; // expected-warning{{uninitialized}} 1238 B b19 = { {0, b19.a2.i2} }; // expected-warning{{uninitialized}} 1239 B b20 = { {}, {b20.a2.i2} }; // expected-warning{{uninitialized}} 1240 B b21 = { {}, {0, b21.a2.i2} }; // expected-warning{{uninitialized}} 1241 1242 B b22 = { {b18.a2.i2 + 5} }; 1243 Bwrapper() {} // expected-note 13{{in this constructor}} 1244 Bwrapper(int) : 1245 b1{ {}, {} }, 1246 b2{ {}, b2.a1 }, 1247 b3{ b3.a1 }, // expected-warning{{uninitialized}} 1248 b4{ {}, b4.a2}, // expected-warning{{uninitialized}} 1249 b5{ b5.a2 }, // expected-warning{{uninitialized}} 1250 1251 b6{ {b6.a1.i1} }, // expected-warning{{uninitialized}} 1252 b7{ {0, b7.a1.i1} }, 1253 b8{ {}, {b8.a1.i1} }, 1254 b9{ {}, {0, b9.a1.i1} }, 1255 1256 b10{ {b10.a1.i2} }, // expected-warning{{uninitialized}} 1257 b11{ {0, b11.a1.i2} }, // expected-warning{{uninitialized}} 1258 b12{ {}, {b12.a1.i2} }, 1259 b13{ {}, {0, b13.a1.i2} }, 1260 1261 b14{ {b14.a2.i1} }, // expected-warning{{uninitialized}} 1262 b15{ {0, b15.a2.i1} }, // expected-warning{{uninitialized}} 1263 b16{ {}, {b16.a2.i1} }, // expected-warning{{uninitialized}} 1264 b17{ {}, {0, b17.a2.i1} }, 1265 1266 b18{ {b18.a2.i2} }, // expected-warning{{uninitialized}} 1267 b19{ {0, b19.a2.i2} }, // expected-warning{{uninitialized}} 1268 b20{ {}, {b20.a2.i2} }, // expected-warning{{uninitialized}} 1269 b21{ {}, {0, b21.a2.i2} }, // expected-warning{{uninitialized}} 1270 1271 b22{ {b18.a2.i2 + 5} } 1272 {} 1273 }; 1274 1275 struct Cwrapper { 1276 C c1 = { 0, num, 0 }; 1277 C c2 = { 1, num, c2.b }; 1278 C c3 = { c3.b, num }; // expected-warning{{uninitialized}} 1279 C c4 = { 0, c4.b, 0 }; // expected-warning{{uninitialized}} 1280 C c5 = { 0, c5.c, 0 }; 1281 C c6 = { c6.b, num, 0 }; // expected-warning{{uninitialized}} 1282 C c7 = { 0, c7.a, 0 }; 1283 1284 Cwrapper() {} // expected-note 3{{in this constructor}} 1285 Cwrapper(int) : 1286 c1{ 0, num, 0 }, 1287 c2{ 1, num, c2.b }, 1288 c3{ c3.b, num }, // expected-warning{{uninitialized}} 1289 c4{ 0, c4.b, 0 }, // expected-warning{{uninitialized}} 1290 c5{ 0, c5.c, 0 }, 1291 c6{ c6.b, num, 0 }, // expected-warning{{uninitialized}} 1292 c7{ 0, c7.a, 0 } 1293 {} 1294 }; 1295 1296 struct Dwrapper { 1297 D d1 = { num, num }; 1298 D d2 = { num, d2.a }; 1299 D d3 = { d3.b, num }; // expected-warning{{uninitialized}} 1300 Dwrapper() {} // expected-note{{in this constructor}} 1301 Dwrapper(int) : 1302 d1{ num, num }, 1303 d2{ num, d2.a }, 1304 d3{ d3.b, num } // expected-warning{{uninitialized}} 1305 {} 1306 }; 1307 1308 struct E { 1309 E(); 1310 E foo(); 1311 E* operator->(); 1312 }; 1313 1314 struct F { F(E); }; 1315 1316 struct EFComposed { 1317 F f; 1318 E e; 1319 EFComposed() : f{ e->foo() }, e() {} // expected-warning{{uninitialized}} 1320 }; 1321 } 1322 1323 namespace template_class { 1324 class Foo { 1325 public: 1326 int *Create() { return nullptr; } 1327 }; 1328 1329 template <typename T> 1330 class A { 1331 public: 1332 // Don't warn on foo here. 1333 A() : ptr(foo->Create()) {} 1334 1335 private: 1336 Foo *foo = new Foo; 1337 int *ptr; 1338 }; 1339 1340 template <typename T> 1341 class B { 1342 public: 1343 // foo is uninitialized here, but class B is never instantiated. 1344 B() : ptr(foo->Create()) {} 1345 1346 private: 1347 Foo *foo; 1348 int *ptr; 1349 }; 1350 1351 template <typename T> 1352 class C { 1353 public: 1354 C() : ptr(foo->Create()) {} 1355 // expected-warning@-1 {{field 'foo' is uninitialized when used here}} 1356 private: 1357 Foo *foo; 1358 int *ptr; 1359 }; 1360 1361 C<int> c; 1362 // expected-note@-1 {{in instantiation of member function 'template_class::C<int>::C' requested here}} 1363 1364 } 1365 1366 namespace base_class_access { 1367 struct A { 1368 A(); 1369 A(int); 1370 1371 int i; 1372 int foo(); 1373 1374 static int bar(); 1375 }; 1376 1377 struct B : public A { 1378 B(int (*)[1]) : A() {} 1379 B(int (*)[2]) : A(bar()) {} 1380 1381 B(int (*)[3]) : A(i) {} 1382 // expected-warning@-1 {{base class 'base_class_access::A' is uninitialized when used here to access 'base_class_access::A::i'}} 1383 1384 B(int (*)[4]) : A(foo()) {} 1385 // expected-warning@-1 {{base_class_access::A' is uninitialized when used here to access 'base_class_access::A::foo'}} 1386 }; 1387 1388 struct C { 1389 C(int) {} 1390 }; 1391 1392 struct D : public C, public A { 1393 D(int (*)[1]) : C(0) {} 1394 D(int (*)[2]) : C(bar()) {} 1395 1396 D(int (*)[3]) : C(i) {} 1397 // expected-warning@-1 {{base class 'base_class_access::A' is uninitialized when used here to access 'base_class_access::A::i'}} 1398 1399 D(int (*)[4]) : C(foo()) {} 1400 // expected-warning@-1 {{base_class_access::A' is uninitialized when used here to access 'base_class_access::A::foo'}} 1401 }; 1402 1403 } 1404 1405 namespace value { 1406 template <class T> T move(T t); 1407 template <class T> T notmove(T t); 1408 } 1409 namespace lvalueref { 1410 template <class T> T move(T& t); 1411 template <class T> T notmove(T& t); 1412 } 1413 namespace rvalueref { 1414 template <class T> T move(T&& t); 1415 template <class T> T notmove(T&& t); 1416 } 1417 1418 namespace move_test { 1419 int a1 = std::move(a1); // expected-warning {{uninitialized}} 1420 int a2 = value::move(a2); // expected-warning {{uninitialized}} 1421 int a3 = value::notmove(a3); // expected-warning {{uninitialized}} 1422 int a4 = lvalueref::move(a4); 1423 int a5 = lvalueref::notmove(a5); 1424 int a6 = rvalueref::move(a6); 1425 int a7 = rvalueref::notmove(a7); 1426 1427 void test() { 1428 int a1 = std::move(a1); // expected-warning {{uninitialized}} 1429 int a2 = value::move(a2); // expected-warning {{uninitialized}} 1430 int a3 = value::notmove(a3); // expected-warning {{uninitialized}} 1431 int a4 = lvalueref::move(a4); 1432 int a5 = lvalueref::notmove(a5); 1433 int a6 = rvalueref::move(a6); 1434 int a7 = rvalueref::notmove(a7); 1435 } 1436 1437 class A { 1438 int a; 1439 A(int (*) [1]) : a(std::move(a)) {} // expected-warning {{uninitialized}} 1440 A(int (*) [2]) : a(value::move(a)) {} // expected-warning {{uninitialized}} 1441 A(int (*) [3]) : a(value::notmove(a)) {} // expected-warning {{uninitialized}} 1442 A(int (*) [4]) : a(lvalueref::move(a)) {} 1443 A(int (*) [5]) : a(lvalueref::notmove(a)) {} 1444 A(int (*) [6]) : a(rvalueref::move(a)) {} 1445 A(int (*) [7]) : a(rvalueref::notmove(a)) {} 1446 }; 1447 } 1448 1449 void array_capture(bool b) { 1450 const char fname[] = "array_capture"; 1451 if (b) { 1452 int unused; // expected-warning {{unused variable}} 1453 } else { 1454 [fname]{}; 1455 } 1456 } 1457 1458 void if_switch_init_stmt(int k) { 1459 if (int n = 0; (n == k || k > 5)) {} 1460 1461 if (int n; (n == k || k > 5)) {} // expected-warning {{uninitialized}} expected-note {{initialize}} 1462 1463 switch (int n = 0; (n == k || k > 5)) {} // expected-warning {{boolean}} 1464 1465 switch (int n; (n == k || k > 5)) {} // expected-warning {{uninitialized}} expected-note {{initialize}} expected-warning {{boolean}} 1466 } 1467 1468 template<typename T> struct Outer { 1469 struct Inner { 1470 int a = 1; 1471 int b; 1472 Inner() : b(a) {} 1473 }; 1474 }; 1475 Outer<int>::Inner outerinner; 1476 1477 struct Polymorphic { virtual ~Polymorphic() { } }; 1478 1479 template<class... Bases> 1480 struct Inherit : Bases... { // #TYPE_INHERIT 1481 int g1; // #FIELD_G1 1482 }; 1483 1484 template<class... Bases> 1485 struct InheritWithExplicit : Bases... { // #TYPE_INHERIT_WITH_EXPLICIT 1486 int g2 [[clang::require_explicit_initialization]]; // #FIELD_G2 1487 }; 1488 1489 struct Special {}; 1490 1491 template<> 1492 struct Inherit<Special> { 1493 int g3 [[clang::require_explicit_initialization]]; // #FIELD_G3 1494 }; 1495 1496 template<> 1497 struct InheritWithExplicit<Special> { 1498 int g4; // #FIELD_G4 1499 }; 1500 1501 void aggregate() { 1502 struct NonAgg { 1503 NonAgg() { } 1504 [[clang::require_explicit_initialization]] int na; // expected-warning {{'require_explicit_initialization' attribute is ignored in non-aggregate type 'NonAgg'}} 1505 }; 1506 NonAgg nonagg; // no-warning 1507 (void)nonagg; 1508 1509 struct S { 1510 [[clang::require_explicit_initialization]] int s1; // #FIELD_S1 1511 int s2; 1512 int s3 = 12; 1513 [[clang::require_explicit_initialization]] int s4 = 100; // #FIELD_S4 1514 static void foo(S) { } 1515 }; 1516 1517 struct C { 1518 #if __cplusplus < 202002L 1519 // expected-warning@+1 {{explicit initialization of field 'c1' will not be enforced in C++20 and later because 'C' has a user-declared constructor, making the type no longer an aggregate}} 1520 [[clang::require_explicit_initialization]] 1521 #endif 1522 int c1; // #FIELD_C1 1523 C() = default; // Test pre-C++20 aggregates 1524 }; 1525 1526 struct D : S { // #TYPE_D 1527 int d1; 1528 int d2 [[clang::require_explicit_initialization]]; // #FIELD_D2 1529 }; 1530 1531 struct D2 : D { // #TYPE_D2 1532 }; 1533 1534 struct E { // #TYPE_E 1535 int e1; 1536 D e2 [[clang::require_explicit_initialization]]; // #FIELD_E2 1537 struct { 1538 [[clang::require_explicit_initialization]] D e3; 1539 D2 e4 [[clang::require_explicit_initialization]]; 1540 }; 1541 }; 1542 1543 S::foo(S{1, 2, 3, 4}); 1544 S::foo(S{.s1 = 100, .s4 = 100}); 1545 S::foo(S{.s1 = 100}); // expected-warning {{field 's4' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_S4 {{'s4' declared here}} 1546 1547 S s{.s1 = 100, .s4 = 100}; 1548 (void)s; 1549 1550 S t{.s4 = 100}; // expected-warning {{field 's1' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_S1 {{'s1' declared here}} 1551 (void)t; 1552 1553 S *ptr1 = new S; // expected-warning {{field in 'S' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_S4 {{'s4' declared here}} expected-note@#FIELD_S1 {{'s1' declared here}} 1554 delete ptr1; 1555 1556 S *ptr2 = new S{.s1 = 100, .s4 = 100}; 1557 delete ptr2; 1558 1559 #if __cplusplus >= 202002L 1560 // expected-warning@+3 {{field 's1' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_S1 {{'s1' declared here}} 1561 // expected-warning@+2 {{field 's4' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_S4 {{'s4' declared here}} 1562 // expected-warning@+1 {{field 'd2' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_D2 {{'d2' declared here}} 1563 D a1({}, 0); 1564 (void)a1; 1565 1566 // expected-warning@+3 {{field 's1' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_S1 {{'s1' declared here}} 1567 // expected-warning@+2 {{field 's4' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_S4 {{'s4' declared here}} 1568 // expected-warning@+1 {{field 'd2' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_D2 {{'d2' declared here}} 1569 D a2(S{}, 0); 1570 (void)a2; 1571 1572 // expected-warning@+2 {{field 's4' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_S4 {{'s4' declared here}} 1573 // expected-warning@+1 {{field 'd2' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_D2 {{'d2' declared here}} 1574 D a3(S{.s1 = 0}, 0); 1575 (void)a3; 1576 1577 // expected-warning@+3 {{field 's1' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_S1 {{'s1' declared here}} 1578 // expected-warning@+2 {{field 's4' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_S4 {{'s4' declared here}} 1579 // expected-warning@+1 {{field 'd2' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_D2 {{'d2' declared here}} 1580 D a4(S(), 0); 1581 (void)a4; 1582 1583 // expected-warning@+2 {{field 's4' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_S4 {{'s4' declared here}} 1584 // expected-warning@+1 {{field 'd2' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_D2 {{'d2' declared here}} 1585 D a5(S(0), 0); 1586 (void)a5; 1587 1588 // expected-warning@+2 {{field 's4' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_S4 {{'s4' declared here}} 1589 // expected-warning@+1 {{field 'd2' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_D2 {{'d2' declared here}} 1590 D a6 = {S(0), 0}; 1591 (void)a6; 1592 #endif 1593 1594 #if 201103L <= __cplusplus && __cplusplus < 202002L 1595 C a; // expected-warning {{field in 'C' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_C1 {{'c1' declared here}} 1596 (void)a; 1597 #endif 1598 1599 // expected-warning@+2 {{field 's4' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_S4 {{'s4' declared here}} 1600 // expected-warning@+1 {{field 's1' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_S1 {{'s1' declared here}} 1601 D b{.d2 = 1}; 1602 (void)b; 1603 1604 // expected-warning@+3 {{field 's4' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_S4 {{'s4' declared here}} 1605 // expected-warning@+2 {{field 'd2' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_D2 {{'d2' declared here}} 1606 // expected-warning@+1 {{field 's1' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_S1 {{'s1' declared here}} 1607 D c{.d1 = 5}; 1608 1609 // expected-warning@+3 {{field 's4' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_S4 {{'s4' declared here}} 1610 // expected-warning@+2 {{field 'd2' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_D2 {{'d2' declared here}} 1611 // expected-warning@+1 {{field 's1' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_S1 {{'s1' declared here}} 1612 c = {{}, 0}; 1613 (void)c; 1614 1615 // expected-note@+3 {{in implicit default constructor for 'D' first required here}} 1616 // expected-warning@#TYPE_D {{field in 'S' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_S1 {{'s1' declared here}} expected-note@#FIELD_S4 {{'s4' declared here}} 1617 // expected-warning@+1 {{field in 'D' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_D2 {{'d2' declared here}} 1618 D d; 1619 (void)d; 1620 1621 // expected-warning@+12 {{field in 'E' requires explicit initialization but is not explicitly initialized}} 1622 // expected-note@#FIELD_E2 {{'e2' declared here}} 1623 // expected-warning@#TYPE_E {{field in 'D' requires explicit initialization but is not explicitly initialized}} 1624 // expected-note@+9 {{in implicit default constructor for 'E' first required here}} 1625 // expected-note@#FIELD_D2 {{'d2' declared here}} 1626 // expected-warning@#TYPE_E {{field in 'D' requires explicit initialization but is not explicitly initialized}} 1627 // expected-note@#FIELD_D2 {{'d2' declared here}} 1628 // expected-warning@#TYPE_E {{field in 'D2' requires explicit initialization but is not explicitly initialized}} 1629 // expected-note@#TYPE_E {{in implicit default constructor for 'D2' first required here}} 1630 // expected-warning@#TYPE_D2 {{field in 'D' requires explicit initialization but is not explicitly initialized}} 1631 // expected-note@+2 {{in implicit default constructor for 'E' first required here}} 1632 // expected-note@#FIELD_D2 {{'d2' declared here}} 1633 E e; 1634 (void)e; 1635 1636 InheritWithExplicit<> agg; // expected-warning {{field in 'InheritWithExplicit<>' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_G2 {{'g2' declared here}} 1637 (void)agg; 1638 1639 InheritWithExplicit<Polymorphic> polymorphic; // expected-warning@#FIELD_G2 {{'require_explicit_initialization' attribute is ignored in non-aggregate type 'InheritWithExplicit<Polymorphic>'}} 1640 (void)polymorphic; 1641 1642 Inherit<Special> specialized_explicit; // expected-warning {{field in 'Inherit<Special>' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_G3 {{'g3' declared here}} 1643 (void)specialized_explicit; 1644 1645 InheritWithExplicit<Special> specialized_implicit; // no-warning 1646 (void)specialized_implicit; 1647 } 1648