Ted Kremenek | c02af35 | 2012-04-12 20:03:47 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -fsyntax-only -Wall -Wuninitialized -std=c++11 -verify %s |
Ted Kremenek | c75f618 | 2010-10-30 00:43:15 +0000 | [diff] [blame] | 2 | |
Chandler Carruth | 349894e | 2011-03-27 20:35:59 +0000 | [diff] [blame] | 3 | int foo(int x); |
| 4 | int bar(int* x); |
| 5 | int boo(int& x); |
| 6 | int far(const int& x); |
| 7 | |
| 8 | // Test self-references within initializers which are guaranteed to be |
| 9 | // uninitialized. |
Chandler Carruth | b414c4f | 2011-04-05 17:41:31 +0000 | [diff] [blame] | 10 | int a = a; // no-warning: used to signal intended lack of initialization. |
Chandler Carruth | 349894e | 2011-03-27 20:35:59 +0000 | [diff] [blame] | 11 | int b = b + 1; // expected-warning {{variable 'b' is uninitialized when used within its own initialization}} |
| 12 | int c = (c + c); // expected-warning 2 {{variable 'c' is uninitialized when used within its own initialization}} |
Chandler Carruth | 349894e | 2011-03-27 20:35:59 +0000 | [diff] [blame] | 13 | int e = static_cast<long>(e) + 1; // expected-warning {{variable 'e' is uninitialized when used within its own initialization}} |
| 14 | int f = foo(f); // expected-warning {{variable 'f' is uninitialized when used within its own initialization}} |
| 15 | |
| 16 | // Thes don't warn as they don't require the value. |
| 17 | int g = sizeof(g); |
| 18 | void* ptr = &ptr; |
| 19 | int h = bar(&h); |
| 20 | int i = boo(i); |
| 21 | int j = far(j); |
| 22 | int k = __alignof__(k); |
| 23 | |
Richard Trieu | 7e9f8af | 2012-05-09 00:21:34 +0000 | [diff] [blame] | 24 | int l = k ? l : l; // expected-warning 2{{variable 'l' is uninitialized when used within its own initialization}} |
| 25 | int m = 1 + (k ? m : m); // expected-warning 2{{variable 'm' is uninitialized when used within its own initialization}} |
| 26 | int n = -n; // expected-warning {{variable 'n' is uninitialized when used within its own initialization}} |
Richard Trieu | 898267f | 2011-09-01 21:44:13 +0000 | [diff] [blame] | 27 | |
Richard Trieu | f6278e5 | 2012-05-09 21:08:22 +0000 | [diff] [blame] | 28 | void test () { |
| 29 | int a = a; // no-warning: used to signal intended lack of initialization. |
| 30 | int b = b + 1; // expected-warning {{variable 'b' is uninitialized when used within its own initialization}} |
| 31 | int c = (c + c); // expected-warning {{variable 'c' is uninitialized when used within its own initialization}} |
| 32 | int d = ({ d + d ;}); // expected-warning {{variable 'd' is uninitialized when used within its own initialization}} |
| 33 | int e = static_cast<long>(e) + 1; // expected-warning {{variable 'e' is uninitialized when used within its own initialization}} |
| 34 | int f = foo(f); // expected-warning {{variable 'f' is uninitialized when used within its own initialization}} |
| 35 | |
| 36 | // Thes don't warn as they don't require the value. |
| 37 | int g = sizeof(g); |
| 38 | void* ptr = &ptr; |
| 39 | int h = bar(&h); |
| 40 | int i = boo(i); |
| 41 | int j = far(j); |
| 42 | int k = __alignof__(k); |
| 43 | |
| 44 | int l = k ? l : l; // FIXME: warn here |
| 45 | int m = 1 + (k ? m : m); // FIXME: warn here |
| 46 | int n = -n; // expected-warning {{variable 'n' is uninitialized when used within its own initialization}} |
| 47 | |
| 48 | for (;;) { |
| 49 | int a = a; // no-warning: used to signal intended lack of initialization. |
| 50 | int b = b + 1; // expected-warning {{variable 'b' is uninitialized when used within its own initialization}} |
| 51 | int c = (c + c); // expected-warning {{variable 'c' is uninitialized when used within its own initialization}} |
| 52 | int d = ({ d + d ;}); // expected-warning {{variable 'd' is uninitialized when used within its own initialization}} |
| 53 | int e = static_cast<long>(e) + 1; // expected-warning {{variable 'e' is uninitialized when used within its own initialization}} |
| 54 | int f = foo(f); // expected-warning {{variable 'f' is uninitialized when used within its own initialization}} |
| 55 | |
| 56 | // Thes don't warn as they don't require the value. |
| 57 | int g = sizeof(g); |
| 58 | void* ptr = &ptr; |
| 59 | int h = bar(&h); |
| 60 | int i = boo(i); |
| 61 | int j = far(j); |
| 62 | int k = __alignof__(k); |
| 63 | |
| 64 | int l = k ? l : l; // FIXME: warn here |
| 65 | int m = 1 + (k ? m : m); // FIXME: warn here |
| 66 | int n = -n; // expected-warning {{variable 'n' is uninitialized when used within its own initialization}} |
| 67 | } |
| 68 | } |
| 69 | |
Richard Trieu | 898267f | 2011-09-01 21:44:13 +0000 | [diff] [blame] | 70 | // Test self-references with record types. |
| 71 | class A { |
| 72 | // Non-POD class. |
| 73 | public: |
| 74 | enum count { ONE, TWO, THREE }; |
| 75 | int num; |
| 76 | static int count; |
| 77 | int get() const { return num; } |
Richard Trieu | ffea6b4 | 2012-03-08 01:15:31 +0000 | [diff] [blame] | 78 | int get2() { return num; } |
Richard Trieu | 898267f | 2011-09-01 21:44:13 +0000 | [diff] [blame] | 79 | void set(int x) { num = x; } |
| 80 | static int zero() { return 0; } |
| 81 | |
| 82 | A() {} |
| 83 | A(A const &a) {} |
| 84 | A(int x) {} |
| 85 | A(int *x) {} |
| 86 | A(A *a) {} |
Rafael Espindola | 1a5d355 | 2012-01-06 04:54:01 +0000 | [diff] [blame] | 87 | ~A(); |
Richard Trieu | 898267f | 2011-09-01 21:44:13 +0000 | [diff] [blame] | 88 | }; |
| 89 | |
| 90 | A getA() { return A(); } |
| 91 | A getA(int x) { return A(); } |
| 92 | A getA(A* a) { return A(); } |
Richard Trieu | 7e9f8af | 2012-05-09 00:21:34 +0000 | [diff] [blame] | 93 | A getA(A a) { return A(); } |
Richard Trieu | 898267f | 2011-09-01 21:44:13 +0000 | [diff] [blame] | 94 | |
Richard Trieu | 7e9f8af | 2012-05-09 00:21:34 +0000 | [diff] [blame] | 95 | void setupA(bool x) { |
Richard Trieu | 898267f | 2011-09-01 21:44:13 +0000 | [diff] [blame] | 96 | A a1; |
| 97 | a1.set(a1.get()); |
| 98 | A a2(a1.get()); |
| 99 | A a3(a1); |
| 100 | A a4(&a4); |
| 101 | A a5(a5.zero()); |
| 102 | A a6(a6.ONE); |
| 103 | A a7 = getA(); |
| 104 | A a8 = getA(a8.TWO); |
| 105 | A a9 = getA(&a9); |
| 106 | A a10(a10.count); |
| 107 | |
| 108 | A a11(a11); // expected-warning {{variable 'a11' is uninitialized when used within its own initialization}} |
| 109 | A a12(a12.get()); // expected-warning {{variable 'a12' is uninitialized when used within its own initialization}} |
| 110 | A a13(a13.num); // expected-warning {{variable 'a13' is uninitialized when used within its own initialization}} |
| 111 | A a14 = A(a14); // expected-warning {{variable 'a14' is uninitialized when used within its own initialization}} |
| 112 | A a15 = getA(a15.num); // expected-warning {{variable 'a15' is uninitialized when used within its own initialization}} |
| 113 | A a16(&a16.num); // expected-warning {{variable 'a16' is uninitialized when used within its own initialization}} |
Richard Trieu | ffea6b4 | 2012-03-08 01:15:31 +0000 | [diff] [blame] | 114 | A a17(a17.get2()); // expected-warning {{variable 'a17' is uninitialized when used within its own initialization}} |
Richard Trieu | 7e9f8af | 2012-05-09 00:21:34 +0000 | [diff] [blame] | 115 | A a18 = x ? a18 : a17; // expected-warning {{variable 'a18' is uninitialized when used within its own initialization}} |
| 116 | A a19 = getA(x ? a19 : a17); // expected-warning {{variable 'a19' is uninitialized when used within its own initialization}} |
Richard Trieu | 898267f | 2011-09-01 21:44:13 +0000 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | struct B { |
| 120 | // POD struct. |
| 121 | int x; |
| 122 | int *y; |
| 123 | }; |
| 124 | |
| 125 | B getB() { return B(); }; |
| 126 | B getB(int x) { return B(); }; |
| 127 | B getB(int *x) { return B(); }; |
| 128 | B getB(B *b) { return B(); }; |
| 129 | |
| 130 | void setupB() { |
| 131 | B b1; |
| 132 | B b2(b1); |
| 133 | B b3 = { 5, &b3.x }; |
| 134 | B b4 = getB(); |
| 135 | B b5 = getB(&b5); |
| 136 | B b6 = getB(&b6.x); |
| 137 | |
| 138 | // Silence unused warning |
| 139 | (void) b2; |
| 140 | (void) b4; |
| 141 | |
| 142 | B b7(b7); // expected-warning {{variable 'b7' is uninitialized when used within its own initialization}} |
| 143 | B b8 = getB(b8.x); // expected-warning {{variable 'b8' is uninitialized when used within its own initialization}} |
| 144 | B b9 = getB(b9.y); // expected-warning {{variable 'b9' is uninitialized when used within its own initialization}} |
Richard Trieu | 7e9f8af | 2012-05-09 00:21:34 +0000 | [diff] [blame] | 145 | B b10 = getB(-b10.x); // expected-warning {{variable 'b10' is uninitialized when used within its own initialization}} |
Richard Trieu | 898267f | 2011-09-01 21:44:13 +0000 | [diff] [blame] | 146 | } |
| 147 | |
Chandler Carruth | 349894e | 2011-03-27 20:35:59 +0000 | [diff] [blame] | 148 | // Also test similar constructs in a field's initializer. |
| 149 | struct S { |
| 150 | int x; |
| 151 | void *ptr; |
| 152 | |
| 153 | S(bool (*)[1]) : x(x) {} // expected-warning {{field is uninitialized when used here}} |
| 154 | S(bool (*)[2]) : x(x + 1) {} // expected-warning {{field is uninitialized when used here}} |
| 155 | S(bool (*)[3]) : x(x + x) {} // expected-warning {{field is uninitialized when used here}} |
| 156 | S(bool (*)[4]) : x(static_cast<long>(x) + 1) {} // expected-warning {{field is uninitialized when used here}} |
| 157 | S(bool (*)[5]) : x(foo(x)) {} // FIXME: This should warn! |
| 158 | |
| 159 | // These don't actually require the value of x and so shouldn't warn. |
| 160 | S(char (*)[1]) : x(sizeof(x)) {} // rdar://8610363 |
| 161 | S(char (*)[2]) : ptr(&ptr) {} |
| 162 | S(char (*)[3]) : x(__alignof__(x)) {} |
| 163 | S(char (*)[4]) : x(bar(&x)) {} |
| 164 | S(char (*)[5]) : x(boo(x)) {} |
| 165 | S(char (*)[6]) : x(far(x)) {} |
Ted Kremenek | c75f618 | 2010-10-30 00:43:15 +0000 | [diff] [blame] | 166 | }; |
Richard Trieu | 47eb898 | 2011-09-07 00:58:53 +0000 | [diff] [blame] | 167 | |
| 168 | struct C { char a[100], *e; } car = { .e = car.a }; |
Douglas Gregor | 2d9eb21 | 2011-11-15 15:29:30 +0000 | [diff] [blame] | 169 | |
| 170 | // <rdar://problem/10398199> |
| 171 | namespace rdar10398199 { |
| 172 | class FooBase { protected: ~FooBase() {} }; |
| 173 | class Foo : public FooBase { |
| 174 | public: |
| 175 | operator int&() const; |
| 176 | }; |
| 177 | void stuff(); |
| 178 | template <typename T> class FooImpl : public Foo { |
| 179 | T val; |
| 180 | public: |
| 181 | FooImpl(const T &x) : val(x) {} |
| 182 | ~FooImpl() { stuff(); } |
| 183 | }; |
| 184 | |
| 185 | template <typename T> FooImpl<T> makeFoo(const T& x) { |
| 186 | return FooImpl<T>(x); |
| 187 | } |
| 188 | |
| 189 | void test() { |
| 190 | const Foo &x = makeFoo(42); |
| 191 | const int&y = makeFoo(42u); |
| 192 | (void)x; |
| 193 | (void)y; |
| 194 | }; |
| 195 | } |
Ted Kremenek | 550f223 | 2012-03-22 05:57:43 +0000 | [diff] [blame] | 196 | |
| 197 | // PR 12325 - this was a false uninitialized value warning due to |
| 198 | // a broken CFG. |
| 199 | int pr12325(int params) { |
| 200 | int x = ({ |
| 201 | while (false) |
| 202 | ; |
| 203 | int _v = params; |
| 204 | if (false) |
| 205 | ; |
| 206 | _v; // no-warning |
| 207 | }); |
| 208 | return x; |
| 209 | } |
| 210 | |
Ted Kremenek | c02af35 | 2012-04-12 20:03:47 +0000 | [diff] [blame] | 211 | // Test lambda expressions with -Wuninitialized |
| 212 | int test_lambda() { |
| 213 | auto f1 = [] (int x, int y) { int z; return x + y + z; }; // expected-warning {{C++11 requires lambda with omitted result type to consist of a single return statement}} expected-warning{{variable 'z' is uninitialized when used here}} expected-note {{initialize the variable 'z' to silence this warning}} |
| 214 | return f1(1, 2); |
| 215 | } |