Ted Kremenek | c75f618 | 2010-10-30 00:43:15 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -fsyntax-only -Wall -Wuninitialized -verify %s |
| 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. |
| 10 | int a = a; // FIXME: This doesn't warn!? Seems it doesn't cast 'a' to an RValue. |
| 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 | d40066b | 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 | 349894e | 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 | |
| 27 | // Also test similar constructs in a field's initializer. |
| 28 | struct S { |
| 29 | int x; |
| 30 | void *ptr; |
| 31 | |
| 32 | S(bool (*)[1]) : x(x) {} // expected-warning {{field is uninitialized when used here}} |
| 33 | S(bool (*)[2]) : x(x + 1) {} // expected-warning {{field is uninitialized when used here}} |
| 34 | S(bool (*)[3]) : x(x + x) {} // expected-warning {{field is uninitialized when used here}} |
| 35 | S(bool (*)[4]) : x(static_cast<long>(x) + 1) {} // expected-warning {{field is uninitialized when used here}} |
| 36 | S(bool (*)[5]) : x(foo(x)) {} // FIXME: This should warn! |
| 37 | |
| 38 | // These don't actually require the value of x and so shouldn't warn. |
| 39 | S(char (*)[1]) : x(sizeof(x)) {} // rdar://8610363 |
| 40 | S(char (*)[2]) : ptr(&ptr) {} |
| 41 | S(char (*)[3]) : x(__alignof__(x)) {} |
| 42 | S(char (*)[4]) : x(bar(&x)) {} |
| 43 | S(char (*)[5]) : x(boo(x)) {} |
| 44 | S(char (*)[6]) : x(far(x)) {} |
Ted Kremenek | c75f618 | 2010-10-30 00:43:15 +0000 | [diff] [blame] | 45 | }; |