xref: /minix3/external/bsd/llvm/dist/clang/test/SemaCXX/uninit-variables.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc // RUN: %clang_cc1 -fsyntax-only -Wuninitialized -fsyntax-only -fcxx-exceptions %s -verify -std=c++1y
2f4a2713aSLionel Sambuc 
3f4a2713aSLionel Sambuc // Stub out types for 'typeid' to work.
4f4a2713aSLionel Sambuc namespace std { class type_info {}; }
5f4a2713aSLionel Sambuc 
6f4a2713aSLionel Sambuc int test1_aux(int &x);
test1()7f4a2713aSLionel Sambuc int test1() {
8f4a2713aSLionel Sambuc   int x;
9f4a2713aSLionel Sambuc   test1_aux(x);
10f4a2713aSLionel Sambuc   return x; // no-warning
11f4a2713aSLionel Sambuc }
12f4a2713aSLionel Sambuc 
test2_aux()13f4a2713aSLionel Sambuc int test2_aux() {
14f4a2713aSLionel Sambuc   int x;
15f4a2713aSLionel Sambuc   int &y = x;
16f4a2713aSLionel Sambuc   return x; // no-warning
17f4a2713aSLionel Sambuc }
18f4a2713aSLionel Sambuc 
19f4a2713aSLionel Sambuc // Don't warn on unevaluated contexts.
unevaluated_tests()20f4a2713aSLionel Sambuc void unevaluated_tests() {
21f4a2713aSLionel Sambuc   int x;
22f4a2713aSLionel Sambuc   (void)sizeof(x);
23f4a2713aSLionel Sambuc   (void)typeid(x);
24f4a2713aSLionel Sambuc }
25f4a2713aSLionel Sambuc 
26f4a2713aSLionel Sambuc // Warn for glvalue arguments to typeid whose type is polymorphic.
~AA27f4a2713aSLionel Sambuc struct A { virtual ~A() {} };
polymorphic_test()28f4a2713aSLionel Sambuc void polymorphic_test() {
29f4a2713aSLionel Sambuc   A *a; // expected-note{{initialize the variable 'a' to silence this warning}}
30f4a2713aSLionel Sambuc   (void)typeid(*a); // expected-warning{{variable 'a' is uninitialized when used here}}
31f4a2713aSLionel Sambuc }
32f4a2713aSLionel Sambuc 
33f4a2713aSLionel Sambuc // Handle cases where the CFG may constant fold some branches, thus
34f4a2713aSLionel Sambuc // mitigating the need for some path-sensitivity in the analysis.
35f4a2713aSLionel Sambuc unsigned test3_aux();
test3()36f4a2713aSLionel Sambuc unsigned test3() {
37f4a2713aSLionel Sambuc   unsigned x = 0;
38f4a2713aSLionel Sambuc   const bool flag = true;
39f4a2713aSLionel Sambuc   if (flag && (x = test3_aux()) == 0) {
40f4a2713aSLionel Sambuc     return x;
41f4a2713aSLionel Sambuc   }
42f4a2713aSLionel Sambuc   return x;
43f4a2713aSLionel Sambuc }
test3_b()44f4a2713aSLionel Sambuc unsigned test3_b() {
45f4a2713aSLionel Sambuc   unsigned x ;
46f4a2713aSLionel Sambuc   const bool flag = true;
47f4a2713aSLionel Sambuc   if (flag && (x = test3_aux()) == 0) {
48f4a2713aSLionel Sambuc     x = 1;
49f4a2713aSLionel Sambuc   }
50f4a2713aSLionel Sambuc   return x; // no-warning
51f4a2713aSLionel Sambuc }
test3_c()52f4a2713aSLionel Sambuc unsigned test3_c() {
53f4a2713aSLionel Sambuc   unsigned x; // expected-note{{initialize the variable 'x' to silence this warning}}
54f4a2713aSLionel Sambuc   const bool flag = false;
55f4a2713aSLionel Sambuc   if (flag && (x = test3_aux()) == 0) {
56f4a2713aSLionel Sambuc     x = 1;
57f4a2713aSLionel Sambuc   }
58f4a2713aSLionel Sambuc   return x; // expected-warning{{variable 'x' is uninitialized when used here}}
59f4a2713aSLionel Sambuc }
60f4a2713aSLionel Sambuc 
61f4a2713aSLionel Sambuc enum test4_A {
62f4a2713aSLionel Sambuc  test4_A_a, test_4_A_b
63f4a2713aSLionel Sambuc };
test4()64f4a2713aSLionel Sambuc test4_A test4() {
65f4a2713aSLionel Sambuc  test4_A a; // expected-note{{variable 'a' is declared here}}
66f4a2713aSLionel Sambuc  return a; // expected-warning{{variable 'a' is uninitialized when used here}}
67f4a2713aSLionel Sambuc }
68f4a2713aSLionel Sambuc 
69f4a2713aSLionel Sambuc // Test variables getting invalidated by function calls with reference arguments
70f4a2713aSLionel Sambuc // *AND* there are multiple invalidated arguments.
71f4a2713aSLionel Sambuc void test5_aux(int &, int &);
72f4a2713aSLionel Sambuc 
test5()73f4a2713aSLionel Sambuc int test5() {
74f4a2713aSLionel Sambuc   int x, y;
75f4a2713aSLionel Sambuc   test5_aux(x, y);
76f4a2713aSLionel Sambuc   return x + y; // no-warning
77f4a2713aSLionel Sambuc }
78f4a2713aSLionel Sambuc 
79f4a2713aSLionel Sambuc // This test previously crashed Sema.
80f4a2713aSLionel Sambuc class Rdar9188004A {
81f4a2713aSLionel Sambuc public:
82f4a2713aSLionel Sambuc   virtual ~Rdar9188004A();
83f4a2713aSLionel Sambuc };
84f4a2713aSLionel Sambuc 
85f4a2713aSLionel Sambuc template< typename T > class Rdar9188004B : public Rdar9188004A {
foo(Rdar9188004B * next) const86f4a2713aSLionel Sambuc virtual double *foo(Rdar9188004B *next) const  {
87f4a2713aSLionel Sambuc     double *values = next->foo(0);
88f4a2713aSLionel Sambuc     try {
89f4a2713aSLionel Sambuc     }
90f4a2713aSLionel Sambuc     catch(double e) {
91f4a2713aSLionel Sambuc       values[0] = e;
92f4a2713aSLionel Sambuc     }
93f4a2713aSLionel Sambuc     return 0;
94f4a2713aSLionel Sambuc   }
95f4a2713aSLionel Sambuc };
96f4a2713aSLionel Sambuc class Rdar9188004C : public Rdar9188004B<Rdar9188004A> {
97f4a2713aSLionel Sambuc   virtual void bar(void) const;
98f4a2713aSLionel Sambuc };
bar(void) const99f4a2713aSLionel Sambuc void Rdar9188004C::bar(void) const {}
100f4a2713aSLionel Sambuc 
101f4a2713aSLionel Sambuc // Don't warn about uninitialized variables in unreachable code.
PR9625()102f4a2713aSLionel Sambuc void PR9625() {
103f4a2713aSLionel Sambuc   if (false) {
104f4a2713aSLionel Sambuc     int x;
105f4a2713aSLionel Sambuc     (void)static_cast<float>(x); // no-warning
106f4a2713aSLionel Sambuc   }
107f4a2713aSLionel Sambuc }
108f4a2713aSLionel Sambuc 
109f4a2713aSLionel Sambuc // Don't warn about variables declared in "catch"
110f4a2713aSLionel Sambuc void RDar9251392_bar(const char *msg);
111f4a2713aSLionel Sambuc 
RDar9251392()112f4a2713aSLionel Sambuc void RDar9251392() {
113f4a2713aSLionel Sambuc   try {
114f4a2713aSLionel Sambuc     throw "hi";
115f4a2713aSLionel Sambuc   }
116f4a2713aSLionel Sambuc   catch (const char* msg) {
117f4a2713aSLionel Sambuc     RDar9251392_bar(msg); // no-warning
118f4a2713aSLionel Sambuc   }
119f4a2713aSLionel Sambuc }
120f4a2713aSLionel Sambuc 
121f4a2713aSLionel Sambuc // Test handling of "no-op" casts.
test_noop_cast()122f4a2713aSLionel Sambuc void test_noop_cast()
123f4a2713aSLionel Sambuc {
124f4a2713aSLionel Sambuc     int x = 1;
125f4a2713aSLionel Sambuc     int y = (int&)x; // no-warning
126f4a2713aSLionel Sambuc }
127f4a2713aSLionel Sambuc 
test_noop_cast2()128f4a2713aSLionel Sambuc void test_noop_cast2() {
129f4a2713aSLionel Sambuc     int x; // expected-note {{initialize the variable 'x' to silence this warning}}
130f4a2713aSLionel Sambuc     int y = (int&)x; // expected-warning {{uninitialized when used here}}
131f4a2713aSLionel Sambuc }
132f4a2713aSLionel Sambuc 
133f4a2713aSLionel Sambuc // Test handling of bit casts.
test_bitcasts()134f4a2713aSLionel Sambuc void test_bitcasts() {
135f4a2713aSLionel Sambuc   int x = 1;
136f4a2713aSLionel Sambuc   int y = (float &)x; // no-warning
137f4a2713aSLionel Sambuc }
138f4a2713aSLionel Sambuc 
test_bitcasts_2()139f4a2713aSLionel Sambuc void test_bitcasts_2() {
140f4a2713aSLionel Sambuc   int x;  // expected-note {{initialize the variable 'x' to silence this warning}}
141f4a2713aSLionel Sambuc   int y = (float &)x; // expected-warning {{uninitialized when used here}}
142f4a2713aSLionel Sambuc }
143f4a2713aSLionel Sambuc 
144f4a2713aSLionel Sambuc void consume_const_ref(const int &n);
test_const_ref()145f4a2713aSLionel Sambuc int test_const_ref() {
146f4a2713aSLionel Sambuc   int n; // expected-note {{variable}}
147f4a2713aSLionel Sambuc   consume_const_ref(n);
148f4a2713aSLionel Sambuc   return n; // expected-warning {{uninitialized when used here}}
149f4a2713aSLionel Sambuc }
150*0a6a1f1dSLionel Sambuc 
151*0a6a1f1dSLionel Sambuc // Don't crash here.
__anonc9d2b03b0102null152*0a6a1f1dSLionel Sambuc auto PR19996 = [a=0]{int t; return a;};
153