[lld][WebAssembly] Reinstate mistakenly disabled test. NFC
[llvm-project.git] / clang / test / SemaCXX / warn-empty-body.cpp
bloba248c4251d5250c7047534ec1e8872b10978a259
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 void test1(int x, int y) {
10 while(true) {
11 if (x); // expected-warning {{if statement has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}}
13 // Check that we handle conditions that start or end with a macro
14 // correctly.
15 if (x == MACRO_A); // expected-warning {{if statement has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}}
16 if (MACRO_A == x); // expected-warning {{if statement has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}}
18 int i;
19 // PR11329
20 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}}
21 a(i);
22 b();
25 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}}
27 a(i);
30 for (i = 0;
31 i < x;
32 i++); // expected-warning{{for loop has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}}
34 a(i);
37 int arr[3] = { 1, 2, 3 };
38 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}}
39 a(i);
41 for (int j :
42 arr); // expected-warning{{range-based for loop has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}}
43 a(i);
45 while (b() == 0); // expected-warning{{while loop has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}}
46 a(i);
48 while (b() == 0); { // expected-warning{{while loop has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}}
49 a(i);
52 while (b() == 0); // expected-warning{{while loop has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}}
54 a(i);
57 while (b() == 0 ||
58 c() == 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 do; // expected-note{{to match this 'do'}}
64 b(); // expected-error{{expected 'while' in do/while loop}}
65 while (b()); // no-warning
66 c();
68 do; // expected-note{{to match this 'do'}}
69 b(); // expected-error{{expected 'while' in do/while loop}}
70 while (b()); // expected-warning{{while loop has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}}
71 c();
73 switch(x) // no-warning
75 switch(y); // expected-warning{{switch statement has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}}
77 case 0:
78 a(10);
79 break;
80 default:
81 a(20);
82 break;
88 /// There should be no warning when null statement is placed on its own line.
89 void test2(int x, int y) {
90 if (x) // no-warning
91 ; // no-warning
93 int i;
94 for (i = 0; i < x; i++) // no-warning
95 ; // no-warning
97 for (i = 0;
98 i < x;
99 i++) // no-warning
100 ; // no-warning
102 int arr[3] = { 1, 2, 3 };
103 for (int j : arr) // no-warning
104 ; // no-warning
106 while (b() == 0) // no-warning
107 ; // no-warning
109 while (b() == 0 ||
110 c() == 0) // no-warning
111 ; // no-warning
113 switch(x)
115 switch(y) // no-warning
116 ; // no-warning
119 // Last `for' or `while' statement in compound statement shouldn't warn.
120 while(b() == 0); // no-warning
123 /// There should be no warning for a null statement resulting from an empty macro.
124 #define EMPTY(a)
125 void test3(int x, int y) {
126 if (x) EMPTY(x); // no-warning
128 int i;
129 for (i = 0; i < x; i++) EMPTY(i); // no-warning
131 for (i = 0;
132 i < x;
133 i++) EMPTY(i); // no-warning
135 int arr[3] = { 1, 2, 3 };
136 for (int j : arr) EMPTY(j); // no-warning
138 for (int j :
139 arr) EMPTY(j); // no-warning
141 while (b() == 0) EMPTY(i); // no-warning
143 while (b() == 0 ||
144 c() == 0) EMPTY(i); // no-warning
146 switch (x) {
147 switch (y)
148 EMPTY(i); // no-warning
152 void test4(int x)
154 // Idiom used in some metaprogramming constructs.
155 switch (x) default:; // no-warning
157 // Frequent idiom used in macros.
158 do {} while (false); // no-warning
161 /// There should be no warning for a common for/while idiom when it is obvious
162 /// from indentation that next statement wasn't meant to be a body.
163 void test5(int x, int y) {
164 int i;
165 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}}
166 a(i);
168 for (i = 0; i < x; i++); // no-warning
169 a(i);
171 for (i = 0;
172 i < x;
173 i++); // expected-warning{{for loop has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}}
174 a(i);
176 for (i = 0;
177 i < x;
178 i++); // no-warning
179 a(i);
181 while (b() == 0); // expected-warning{{while loop has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}}
182 a(i);
184 while (b() == 0); // no-warning
185 a(i);
187 while (b() == 0 ||
188 c() == 0); // expected-warning{{while loop has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}}
189 a(i);
191 while (b() == 0 ||
192 c() == 0); // no-warning
193 a(i);
196 /// There should be no warning for a statement with a non-null body.
197 void test6(int x, int y) {
198 if (x) {} // no-warning
200 if (x)
201 a(x); // no-warning
203 int i;
204 for (i = 0; i < x; i++) // no-warning
205 a(i); // no-warning
207 for (i = 0; i < x; i++) { // no-warning
208 a(i); // no-warning
211 for (i = 0;
212 i < x;
213 i++) // no-warning
214 a(i); // no-warning
216 int arr[3] = { 1, 2, 3 };
217 for (int j : arr) // no-warning
218 a(j);
220 for (int j : arr) {} // no-warning
222 while (b() == 0) // no-warning
223 a(i); // no-warning
225 while (b() == 0) {} // no-warning
227 switch(x) // no-warning
229 switch(y) // no-warning
231 case 0:
232 a(10);
233 break;
234 default:
235 a(20);
236 break;
241 void test_errors(int x) {
242 if (1)
243 aa; // expected-error{{use of undeclared identifier}}
244 // no empty body warning.
246 int i;
247 for (i = 0; i < x; i++)
248 bb; // expected-error{{use of undeclared identifier}}
250 int arr[3] = { 1, 2, 3 };
251 for (int j : arr)
252 cc; // expected-error{{use of undeclared identifier}}
254 while (b() == 0)
255 dd; // expected-error{{use of undeclared identifier}}
258 // Warnings for statements in templates shouldn't be duplicated for all
259 // instantiations.
260 template <typename T>
261 void test_template(int x) {
262 if (x); // expected-warning{{if statement has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}}
264 if (x)
265 EMPTY(x); // no-warning
267 int arr[3] = { 1, 2, 3 };
268 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}}
270 while (b() == 0); // expected-warning{{while loop has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}}
271 a(x);
274 void test_template_inst(int x) {
275 test_template<int>(x);
276 test_template<double>(x);
279 #define IDENTITY(a) a
280 void test7(int x, int y) {
281 if (x) IDENTITY(); // no-warning