annotate clang/test/SemaCXX/uninit-variables.cpp @ 221:79ff65ed7e25

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