uninitialized.cpp revision d40066b0fb883839a9100e5455e33190b9b8abac
1// RUN: %clang_cc1 -fsyntax-only -Wall -Wuninitialized -verify %s 2 3int foo(int x); 4int bar(int* x); 5int boo(int& x); 6int far(const int& x); 7 8// Test self-references within initializers which are guaranteed to be 9// uninitialized. 10int a = a; // FIXME: This doesn't warn!? Seems it doesn't cast 'a' to an RValue. 11int b = b + 1; // expected-warning {{variable 'b' is uninitialized when used within its own initialization}} 12int c = (c + c); // expected-warning 2 {{variable 'c' is uninitialized when used within its own initialization}} 13void test() { 14 int d = ({ d + d ;}); // expected-warning {{variable 'd' is uninitialized when used within its own initialization}} 15} 16int e = static_cast<long>(e) + 1; // expected-warning {{variable 'e' is uninitialized when used within its own initialization}} 17int 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. 20int g = sizeof(g); 21void* ptr = &ptr; 22int h = bar(&h); 23int i = boo(i); 24int j = far(j); 25int k = __alignof__(k); 26 27// Also test similar constructs in a field's initializer. 28struct 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)) {} 45}; 46