Ted Kremenek | c00d5b9 | 2010-10-30 00:43:15 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -fsyntax-only -Wall -Wuninitialized -verify %s |
| 2 | |
Chandler Carruth | edcc04e | 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 | b5d4831 | 2011-04-05 17:41:31 +0000 | [diff] [blame] | 10 | int a = a; // no-warning: used to signal intended lack of initialization. |
Chandler Carruth | edcc04e | 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}} |
| 13 | void test() { |
Ted Kremenek | 3788193 | 2011-04-04 23:29:12 +0000 | [diff] [blame] | 14 | int d = ({ d + d ;}); // expected-warning {{variable 'd' is uninitialized when used within its own initialization}} |
Chandler Carruth | edcc04e | 2011-03-27 20:35:59 +0000 | [diff] [blame] | 15 | } |
| 16 | int e = static_cast<long>(e) + 1; // expected-warning {{variable 'e' is uninitialized when used within its own initialization}} |
| 17 | int f = foo(f); // expected-warning {{variable 'f' is uninitialized when used within its own initialization}} |
| 18 | |
| 19 | // Thes don't warn as they don't require the value. |
| 20 | int g = sizeof(g); |
| 21 | void* ptr = &ptr; |
| 22 | int h = bar(&h); |
| 23 | int i = boo(i); |
| 24 | int j = far(j); |
| 25 | int k = __alignof__(k); |
| 26 | |
Richard Trieu | a04ad1a | 2011-09-01 21:44:13 +0000 | [diff] [blame] | 27 | |
| 28 | // Test self-references with record types. |
| 29 | class A { |
| 30 | // Non-POD class. |
| 31 | public: |
| 32 | enum count { ONE, TWO, THREE }; |
| 33 | int num; |
| 34 | static int count; |
| 35 | int get() const { return num; } |
Richard Trieu | 978dfc0 | 2012-03-08 01:15:31 +0000 | [diff] [blame] | 36 | int get2() { return num; } |
Richard Trieu | a04ad1a | 2011-09-01 21:44:13 +0000 | [diff] [blame] | 37 | void set(int x) { num = x; } |
| 38 | static int zero() { return 0; } |
| 39 | |
| 40 | A() {} |
| 41 | A(A const &a) {} |
| 42 | A(int x) {} |
| 43 | A(int *x) {} |
| 44 | A(A *a) {} |
Rafael Espindola | 7c23b08 | 2012-01-06 04:54:01 +0000 | [diff] [blame] | 45 | ~A(); |
Richard Trieu | a04ad1a | 2011-09-01 21:44:13 +0000 | [diff] [blame] | 46 | }; |
| 47 | |
| 48 | A getA() { return A(); } |
| 49 | A getA(int x) { return A(); } |
| 50 | A getA(A* a) { return A(); } |
| 51 | |
| 52 | void setupA() { |
| 53 | A a1; |
| 54 | a1.set(a1.get()); |
| 55 | A a2(a1.get()); |
| 56 | A a3(a1); |
| 57 | A a4(&a4); |
| 58 | A a5(a5.zero()); |
| 59 | A a6(a6.ONE); |
| 60 | A a7 = getA(); |
| 61 | A a8 = getA(a8.TWO); |
| 62 | A a9 = getA(&a9); |
| 63 | A a10(a10.count); |
| 64 | |
| 65 | A a11(a11); // expected-warning {{variable 'a11' is uninitialized when used within its own initialization}} |
| 66 | A a12(a12.get()); // expected-warning {{variable 'a12' is uninitialized when used within its own initialization}} |
| 67 | A a13(a13.num); // expected-warning {{variable 'a13' is uninitialized when used within its own initialization}} |
| 68 | A a14 = A(a14); // expected-warning {{variable 'a14' is uninitialized when used within its own initialization}} |
| 69 | A a15 = getA(a15.num); // expected-warning {{variable 'a15' is uninitialized when used within its own initialization}} |
| 70 | A a16(&a16.num); // expected-warning {{variable 'a16' is uninitialized when used within its own initialization}} |
Richard Trieu | 978dfc0 | 2012-03-08 01:15:31 +0000 | [diff] [blame] | 71 | A a17(a17.get2()); // expected-warning {{variable 'a17' is uninitialized when used within its own initialization}} |
Richard Trieu | a04ad1a | 2011-09-01 21:44:13 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | struct B { |
| 75 | // POD struct. |
| 76 | int x; |
| 77 | int *y; |
| 78 | }; |
| 79 | |
| 80 | B getB() { return B(); }; |
| 81 | B getB(int x) { return B(); }; |
| 82 | B getB(int *x) { return B(); }; |
| 83 | B getB(B *b) { return B(); }; |
| 84 | |
| 85 | void setupB() { |
| 86 | B b1; |
| 87 | B b2(b1); |
| 88 | B b3 = { 5, &b3.x }; |
| 89 | B b4 = getB(); |
| 90 | B b5 = getB(&b5); |
| 91 | B b6 = getB(&b6.x); |
| 92 | |
| 93 | // Silence unused warning |
| 94 | (void) b2; |
| 95 | (void) b4; |
| 96 | |
| 97 | B b7(b7); // expected-warning {{variable 'b7' is uninitialized when used within its own initialization}} |
| 98 | B b8 = getB(b8.x); // expected-warning {{variable 'b8' is uninitialized when used within its own initialization}} |
| 99 | B b9 = getB(b9.y); // expected-warning {{variable 'b9' is uninitialized when used within its own initialization}} |
| 100 | } |
| 101 | |
Chandler Carruth | edcc04e | 2011-03-27 20:35:59 +0000 | [diff] [blame] | 102 | // Also test similar constructs in a field's initializer. |
| 103 | struct S { |
| 104 | int x; |
| 105 | void *ptr; |
| 106 | |
| 107 | S(bool (*)[1]) : x(x) {} // expected-warning {{field is uninitialized when used here}} |
| 108 | S(bool (*)[2]) : x(x + 1) {} // expected-warning {{field is uninitialized when used here}} |
| 109 | S(bool (*)[3]) : x(x + x) {} // expected-warning {{field is uninitialized when used here}} |
| 110 | S(bool (*)[4]) : x(static_cast<long>(x) + 1) {} // expected-warning {{field is uninitialized when used here}} |
| 111 | S(bool (*)[5]) : x(foo(x)) {} // FIXME: This should warn! |
| 112 | |
| 113 | // These don't actually require the value of x and so shouldn't warn. |
| 114 | S(char (*)[1]) : x(sizeof(x)) {} // rdar://8610363 |
| 115 | S(char (*)[2]) : ptr(&ptr) {} |
| 116 | S(char (*)[3]) : x(__alignof__(x)) {} |
| 117 | S(char (*)[4]) : x(bar(&x)) {} |
| 118 | S(char (*)[5]) : x(boo(x)) {} |
| 119 | S(char (*)[6]) : x(far(x)) {} |
Ted Kremenek | c00d5b9 | 2010-10-30 00:43:15 +0000 | [diff] [blame] | 120 | }; |
Richard Trieu | aa5e256 | 2011-09-07 00:58:53 +0000 | [diff] [blame] | 121 | |
| 122 | struct C { char a[100], *e; } car = { .e = car.a }; |
Douglas Gregor | 6c8f07f | 2011-11-15 15:29:30 +0000 | [diff] [blame] | 123 | |
| 124 | // <rdar://problem/10398199> |
| 125 | namespace rdar10398199 { |
| 126 | class FooBase { protected: ~FooBase() {} }; |
| 127 | class Foo : public FooBase { |
| 128 | public: |
| 129 | operator int&() const; |
| 130 | }; |
| 131 | void stuff(); |
| 132 | template <typename T> class FooImpl : public Foo { |
| 133 | T val; |
| 134 | public: |
| 135 | FooImpl(const T &x) : val(x) {} |
| 136 | ~FooImpl() { stuff(); } |
| 137 | }; |
| 138 | |
| 139 | template <typename T> FooImpl<T> makeFoo(const T& x) { |
| 140 | return FooImpl<T>(x); |
| 141 | } |
| 142 | |
| 143 | void test() { |
| 144 | const Foo &x = makeFoo(42); |
| 145 | const int&y = makeFoo(42u); |
| 146 | (void)x; |
| 147 | (void)y; |
| 148 | }; |
| 149 | } |
Ted Kremenek | 213d053 | 2012-03-22 05:57:43 +0000 | [diff] [blame] | 150 | |
| 151 | // PR 12325 - this was a false uninitialized value warning due to |
| 152 | // a broken CFG. |
| 153 | int pr12325(int params) { |
| 154 | int x = ({ |
| 155 | while (false) |
| 156 | ; |
| 157 | int _v = params; |
| 158 | if (false) |
| 159 | ; |
| 160 | _v; // no-warning |
| 161 | }); |
| 162 | return x; |
| 163 | } |
| 164 | |