[clang] Implement lifetime analysis for lifetime_capture_by(X) (#115921)
[llvm-project.git] / clang / test / SemaCXX / warn-empty-body.cpp
blob08cfb57e90e075e6e5eb7b41b78165baefc1a046
1 // RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify %s
3 void a(int i);
4 int b();
5 int c();
7 #define MACRO_A 0
9 #define AND(x, y) ((x) && (y))
11 void test1(int x, int y) {
12 while(true) {
13 if (x); // expected-warning {{if statement has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}}
15 // Check that we handle conditions that start or end with a macro
16 // correctly.
17 if (x == MACRO_A); // expected-warning {{if statement has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}}
18 if (MACRO_A == x); // expected-warning {{if statement has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}}
20 // Check that we handle the case where the condition comes from a macro
21 // expansion over multiple lines.
22 if (AND(b(),
23 c())); // expected-warning {{if statement has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}}
25 while (AND(b(),
26 c())); // expected-warning{{while loop has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}}
27 a(0);
29 int i;
30 // PR11329
31 for (i = 0; i < x; i++); { // expected-warning{{for loop has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}}
32 a(i);
33 b();
36 for (i = 0; i < x; i++); // expected-warning{{for loop has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}}
38 a(i);
41 for (i = 0;
42 i < x;
43 i++); // expected-warning{{for loop has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}}
45 a(i);
48 int arr[3] = { 1, 2, 3 };
49 for (int j : arr); // expected-warning{{range-based for loop has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}}
50 a(i);
52 for (int j :
53 arr); // expected-warning{{range-based for loop has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}}
54 a(i);
56 while (b() == 0); // expected-warning{{while loop has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}}
57 a(i);
59 while (b() == 0); { // expected-warning{{while loop has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}}
60 a(i);
63 while (b() == 0); // expected-warning{{while loop has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}}
65 a(i);
68 while (b() == 0 ||
69 c() == 0); // expected-warning{{while loop has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}}
71 a(i);
74 do; // expected-note{{to match this 'do'}}
75 b(); // expected-error{{expected 'while' in do/while loop}}
76 while (b()); // no-warning
77 c();
79 do; // expected-note{{to match this 'do'}}
80 b(); // expected-error{{expected 'while' in do/while loop}}
81 while (b()); // expected-warning{{while loop has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}}
82 c();
84 switch(x) // no-warning
86 switch(y); // expected-warning{{switch statement has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}}
88 case 0:
89 a(10);
90 break;
91 default:
92 a(20);
93 break;
99 /// There should be no warning when null statement is placed on its own line.
100 void test2(int x, int y) {
101 if (x) // no-warning
102 ; // no-warning
104 int i;
105 for (i = 0; i < x; i++) // no-warning
106 ; // no-warning
108 for (i = 0;
109 i < x;
110 i++) // no-warning
111 ; // no-warning
113 int arr[3] = { 1, 2, 3 };
114 for (int j : arr) // no-warning
115 ; // no-warning
117 while (b() == 0) // no-warning
118 ; // no-warning
120 while (b() == 0 ||
121 c() == 0) // no-warning
122 ; // no-warning
124 switch(x)
126 switch(y) // no-warning
127 ; // no-warning
130 // Last `for' or `while' statement in compound statement shouldn't warn.
131 while(b() == 0); // no-warning
134 /// There should be no warning for a null statement resulting from an empty macro.
135 #define EMPTY(a)
136 void test3(int x, int y) {
137 if (x) EMPTY(x); // no-warning
139 int i;
140 for (i = 0; i < x; i++) EMPTY(i); // no-warning
142 for (i = 0;
143 i < x;
144 i++) EMPTY(i); // no-warning
146 int arr[3] = { 1, 2, 3 };
147 for (int j : arr) EMPTY(j); // no-warning
149 for (int j :
150 arr) EMPTY(j); // no-warning
152 while (b() == 0) EMPTY(i); // no-warning
154 while (b() == 0 ||
155 c() == 0) EMPTY(i); // no-warning
157 switch (x) {
158 switch (y)
159 EMPTY(i); // no-warning
163 void test4(int x)
165 // Idiom used in some metaprogramming constructs.
166 switch (x) default:; // no-warning
168 // Frequent idiom used in macros.
169 do {} while (false); // no-warning
172 /// There should be no warning for a common for/while idiom when it is obvious
173 /// from indentation that next statement wasn't meant to be a body.
174 void test5(int x, int y) {
175 int i;
176 for (i = 0; i < x; i++); // expected-warning{{for loop has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}}
177 a(i);
179 for (i = 0; i < x; i++); // no-warning
180 a(i);
182 for (i = 0;
183 i < x;
184 i++); // expected-warning{{for loop has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}}
185 a(i);
187 for (i = 0;
188 i < x;
189 i++); // no-warning
190 a(i);
192 while (b() == 0); // expected-warning{{while loop has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}}
193 a(i);
195 while (b() == 0); // no-warning
196 a(i);
198 while (b() == 0 ||
199 c() == 0); // expected-warning{{while loop has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}}
200 a(i);
202 while (b() == 0 ||
203 c() == 0); // no-warning
204 a(i);
207 /// There should be no warning for a statement with a non-null body.
208 void test6(int x, int y) {
209 if (x) {} // no-warning
211 if (x)
212 a(x); // no-warning
214 int i;
215 for (i = 0; i < x; i++) // no-warning
216 a(i); // no-warning
218 for (i = 0; i < x; i++) { // no-warning
219 a(i); // no-warning
222 for (i = 0;
223 i < x;
224 i++) // no-warning
225 a(i); // no-warning
227 int arr[3] = { 1, 2, 3 };
228 for (int j : arr) // no-warning
229 a(j);
231 for (int j : arr) {} // no-warning
233 while (b() == 0) // no-warning
234 a(i); // no-warning
236 while (b() == 0) {} // no-warning
238 switch(x) // no-warning
240 switch(y) // no-warning
242 case 0:
243 a(10);
244 break;
245 default:
246 a(20);
247 break;
252 void test_errors(int x) {
253 if (1)
254 aa; // expected-error{{use of undeclared identifier}}
255 // no empty body warning.
257 int i;
258 for (i = 0; i < x; i++)
259 bb; // expected-error{{use of undeclared identifier}}
261 int arr[3] = { 1, 2, 3 };
262 for (int j : arr)
263 cc; // expected-error{{use of undeclared identifier}}
265 while (b() == 0)
266 dd; // expected-error{{use of undeclared identifier}}
269 // Warnings for statements in templates shouldn't be duplicated for all
270 // instantiations.
271 template <typename T>
272 void test_template(int x) {
273 if (x); // expected-warning{{if statement has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}}
275 if (x)
276 EMPTY(x); // no-warning
278 int arr[3] = { 1, 2, 3 };
279 for (int j : arr); // expected-warning{{range-based for loop has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}}
281 while (b() == 0); // expected-warning{{while loop has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}}
282 a(x);
285 void test_template_inst(int x) {
286 test_template<int>(x);
287 test_template<double>(x);
290 #define IDENTITY(a) a
291 void test7(int x, int y) {
292 if (x) IDENTITY(); // no-warning