• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // RUN: %clang_cc1 -fsyntax-only -Wall -Wuninitialized -std=c++11 -verify %s
2 
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; // no-warning: used to signal intended lack of initialization.
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}}
test()13 void test() {
14   int d = ({ d + d ;}); // expected-warning {{variable 'd' is uninitialized when used within its own initialization}}
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 
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;
get() const35     int get() const { return num; }
get2()36     int get2() { return num; }
set(int x)37     void set(int x) { num = x; }
zero()38     static int zero() { return 0; }
39 
A()40     A() {}
A(A const & a)41     A(A const &a) {}
A(int x)42     A(int x) {}
A(int * x)43     A(int *x) {}
A(A * a)44     A(A *a) {}
45     ~A();
46 };
47 
getA()48 A getA() { return A(); }
getA(int x)49 A getA(int x) { return A(); }
getA(A * a)50 A getA(A* a) { return A(); }
51 
setupA()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}}
71   A a17(a17.get2());  // expected-warning {{variable 'a17' is uninitialized when used within its own initialization}}
72 }
73 
74 struct B {
75   // POD struct.
76   int x;
77   int *y;
78 };
79 
getB()80 B getB() { return B(); };
getB(int x)81 B getB(int x) { return B(); };
getB(int * x)82 B getB(int *x) { return B(); };
getB(B * b)83 B getB(B *b) { return B(); };
84 
setupB()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 
102 // Also test similar constructs in a field's initializer.
103 struct S {
104   int x;
105   void *ptr;
106 
SS107   S(bool (*)[1]) : x(x) {} // expected-warning {{field is uninitialized when used here}}
SS108   S(bool (*)[2]) : x(x + 1) {} // expected-warning {{field is uninitialized when used here}}
SS109   S(bool (*)[3]) : x(x + x) {} // expected-warning {{field is uninitialized when used here}}
SS110   S(bool (*)[4]) : x(static_cast<long>(x) + 1) {} // expected-warning {{field is uninitialized when used here}}
SS111   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.
SS114   S(char (*)[1]) : x(sizeof(x)) {} // rdar://8610363
SS115   S(char (*)[2]) : ptr(&ptr) {}
SS116   S(char (*)[3]) : x(__alignof__(x)) {}
SS117   S(char (*)[4]) : x(bar(&x)) {}
SS118   S(char (*)[5]) : x(boo(x)) {}
SS119   S(char (*)[6]) : x(far(x)) {}
120 };
121 
122 struct C { char a[100], *e; } car = { .e = car.a };
123 
124 // <rdar://problem/10398199>
125 namespace rdar10398199 {
~FooBase()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:
FooImpl(const T & x)135     FooImpl(const T &x) : val(x) {}
~FooImpl()136     ~FooImpl() { stuff(); }
137   };
138 
makeFoo(const T & x)139   template <typename T> FooImpl<T> makeFoo(const T& x) {
140     return FooImpl<T>(x);
141   }
142 
test()143   void test() {
144     const Foo &x = makeFoo(42);
145     const int&y = makeFoo(42u);
146     (void)x;
147     (void)y;
148   };
149 }
150 
151 // PR 12325 - this was a false uninitialized value warning due to
152 // a broken CFG.
pr12325(int params)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 
165 // Test lambda expressions with -Wuninitialized
test_lambda()166 int test_lambda() {
167   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}}
168   return f1(1, 2);
169 }
170