1*67e74705SXin Li // RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify %s
2*67e74705SXin Li
3*67e74705SXin Li void a(int i);
4*67e74705SXin Li int b();
5*67e74705SXin Li int c();
6*67e74705SXin Li
7*67e74705SXin Li #define MACRO_A 0
8*67e74705SXin Li
test1(int x,int y)9*67e74705SXin Li void test1(int x, int y) {
10*67e74705SXin Li while(true) {
11*67e74705SXin Li if (x); // expected-warning {{if statement has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}}
12*67e74705SXin Li
13*67e74705SXin Li // Check that we handle conditions that start or end with a macro
14*67e74705SXin Li // correctly.
15*67e74705SXin Li 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*67e74705SXin Li if (MACRO_A == x); // expected-warning {{if statement has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}}
17*67e74705SXin Li
18*67e74705SXin Li int i;
19*67e74705SXin Li // PR11329
20*67e74705SXin Li 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*67e74705SXin Li a(i);
22*67e74705SXin Li b();
23*67e74705SXin Li }
24*67e74705SXin Li
25*67e74705SXin Li 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}}
26*67e74705SXin Li {
27*67e74705SXin Li a(i);
28*67e74705SXin Li }
29*67e74705SXin Li
30*67e74705SXin Li for (i = 0;
31*67e74705SXin Li i < x;
32*67e74705SXin Li i++); // expected-warning{{for loop has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}}
33*67e74705SXin Li {
34*67e74705SXin Li a(i);
35*67e74705SXin Li }
36*67e74705SXin Li
37*67e74705SXin Li int arr[3] = { 1, 2, 3 };
38*67e74705SXin Li 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*67e74705SXin Li a(i);
40*67e74705SXin Li
41*67e74705SXin Li for (int j :
42*67e74705SXin Li arr); // expected-warning{{range-based for loop has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}}
43*67e74705SXin Li a(i);
44*67e74705SXin Li
45*67e74705SXin Li while (b() == 0); // expected-warning{{while loop has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}}
46*67e74705SXin Li a(i);
47*67e74705SXin Li
48*67e74705SXin Li while (b() == 0); { // expected-warning{{while loop has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}}
49*67e74705SXin Li a(i);
50*67e74705SXin Li }
51*67e74705SXin Li
52*67e74705SXin Li while (b() == 0); // expected-warning{{while loop has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}}
53*67e74705SXin Li {
54*67e74705SXin Li a(i);
55*67e74705SXin Li }
56*67e74705SXin Li
57*67e74705SXin Li while (b() == 0 ||
58*67e74705SXin Li c() == 0); // expected-warning{{while loop has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}}
59*67e74705SXin Li {
60*67e74705SXin Li a(i);
61*67e74705SXin Li }
62*67e74705SXin Li
63*67e74705SXin Li do; // expected-note{{to match this 'do'}}
64*67e74705SXin Li b(); // expected-error{{expected 'while' in do/while loop}}
65*67e74705SXin Li while (b()); // no-warning
66*67e74705SXin Li c();
67*67e74705SXin Li
68*67e74705SXin Li do; // expected-note{{to match this 'do'}}
69*67e74705SXin Li b(); // expected-error{{expected 'while' in do/while loop}}
70*67e74705SXin Li while (b()); // expected-warning{{while loop has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}}
71*67e74705SXin Li c();
72*67e74705SXin Li
73*67e74705SXin Li switch(x) // no-warning
74*67e74705SXin Li {
75*67e74705SXin Li switch(y); // expected-warning{{switch statement has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}}
76*67e74705SXin Li {
77*67e74705SXin Li case 0:
78*67e74705SXin Li a(10);
79*67e74705SXin Li break;
80*67e74705SXin Li default:
81*67e74705SXin Li a(20);
82*67e74705SXin Li break;
83*67e74705SXin Li }
84*67e74705SXin Li }
85*67e74705SXin Li }
86*67e74705SXin Li }
87*67e74705SXin Li
88*67e74705SXin Li /// There should be no warning when null statement is placed on its own line.
test2(int x,int y)89*67e74705SXin Li void test2(int x, int y) {
90*67e74705SXin Li if (x) // no-warning
91*67e74705SXin Li ; // no-warning
92*67e74705SXin Li
93*67e74705SXin Li int i;
94*67e74705SXin Li for (i = 0; i < x; i++) // no-warning
95*67e74705SXin Li ; // no-warning
96*67e74705SXin Li
97*67e74705SXin Li for (i = 0;
98*67e74705SXin Li i < x;
99*67e74705SXin Li i++) // no-warning
100*67e74705SXin Li ; // no-warning
101*67e74705SXin Li
102*67e74705SXin Li int arr[3] = { 1, 2, 3 };
103*67e74705SXin Li for (int j : arr) // no-warning
104*67e74705SXin Li ; // no-warning
105*67e74705SXin Li
106*67e74705SXin Li while (b() == 0) // no-warning
107*67e74705SXin Li ; // no-warning
108*67e74705SXin Li
109*67e74705SXin Li while (b() == 0 ||
110*67e74705SXin Li c() == 0) // no-warning
111*67e74705SXin Li ; // no-warning
112*67e74705SXin Li
113*67e74705SXin Li switch(x)
114*67e74705SXin Li {
115*67e74705SXin Li switch(y) // no-warning
116*67e74705SXin Li ; // no-warning
117*67e74705SXin Li }
118*67e74705SXin Li
119*67e74705SXin Li // Last `for' or `while' statement in compound statement shouldn't warn.
120*67e74705SXin Li while(b() == 0); // no-warning
121*67e74705SXin Li }
122*67e74705SXin Li
123*67e74705SXin Li /// There should be no warning for a null statement resulting from an empty macro.
124*67e74705SXin Li #define EMPTY(a)
test3(int x,int y)125*67e74705SXin Li void test3(int x, int y) {
126*67e74705SXin Li if (x) EMPTY(x); // no-warning
127*67e74705SXin Li
128*67e74705SXin Li int i;
129*67e74705SXin Li for (i = 0; i < x; i++) EMPTY(i); // no-warning
130*67e74705SXin Li
131*67e74705SXin Li for (i = 0;
132*67e74705SXin Li i < x;
133*67e74705SXin Li i++) EMPTY(i); // no-warning
134*67e74705SXin Li
135*67e74705SXin Li int arr[3] = { 1, 2, 3 };
136*67e74705SXin Li for (int j : arr) EMPTY(j); // no-warning
137*67e74705SXin Li
138*67e74705SXin Li for (int j :
139*67e74705SXin Li arr) EMPTY(j); // no-warning
140*67e74705SXin Li
141*67e74705SXin Li while (b() == 0) EMPTY(i); // no-warning
142*67e74705SXin Li
143*67e74705SXin Li while (b() == 0 ||
144*67e74705SXin Li c() == 0) EMPTY(i); // no-warning
145*67e74705SXin Li
146*67e74705SXin Li switch (x) {
147*67e74705SXin Li switch (y)
148*67e74705SXin Li EMPTY(i); // no-warning
149*67e74705SXin Li }
150*67e74705SXin Li }
151*67e74705SXin Li
test4(int x)152*67e74705SXin Li void test4(int x)
153*67e74705SXin Li {
154*67e74705SXin Li // Idiom used in some metaprogramming constructs.
155*67e74705SXin Li switch (x) default:; // no-warning
156*67e74705SXin Li
157*67e74705SXin Li // Frequent idiom used in macros.
158*67e74705SXin Li do {} while (false); // no-warning
159*67e74705SXin Li }
160*67e74705SXin Li
161*67e74705SXin Li /// There should be no warning for a common for/while idiom when it is obvious
162*67e74705SXin Li /// from indentation that next statement wasn't meant to be a body.
test5(int x,int y)163*67e74705SXin Li void test5(int x, int y) {
164*67e74705SXin Li int i;
165*67e74705SXin Li 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*67e74705SXin Li a(i);
167*67e74705SXin Li
168*67e74705SXin Li for (i = 0; i < x; i++); // no-warning
169*67e74705SXin Li a(i);
170*67e74705SXin Li
171*67e74705SXin Li for (i = 0;
172*67e74705SXin Li i < x;
173*67e74705SXin Li i++); // expected-warning{{for loop has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}}
174*67e74705SXin Li a(i);
175*67e74705SXin Li
176*67e74705SXin Li for (i = 0;
177*67e74705SXin Li i < x;
178*67e74705SXin Li i++); // no-warning
179*67e74705SXin Li a(i);
180*67e74705SXin Li
181*67e74705SXin Li while (b() == 0); // expected-warning{{while loop has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}}
182*67e74705SXin Li a(i);
183*67e74705SXin Li
184*67e74705SXin Li while (b() == 0); // no-warning
185*67e74705SXin Li a(i);
186*67e74705SXin Li
187*67e74705SXin Li while (b() == 0 ||
188*67e74705SXin Li c() == 0); // expected-warning{{while loop has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}}
189*67e74705SXin Li a(i);
190*67e74705SXin Li
191*67e74705SXin Li while (b() == 0 ||
192*67e74705SXin Li c() == 0); // no-warning
193*67e74705SXin Li a(i);
194*67e74705SXin Li }
195*67e74705SXin Li
196*67e74705SXin Li /// There should be no warning for a statement with a non-null body.
test6(int x,int y)197*67e74705SXin Li void test6(int x, int y) {
198*67e74705SXin Li if (x) {} // no-warning
199*67e74705SXin Li
200*67e74705SXin Li if (x)
201*67e74705SXin Li a(x); // no-warning
202*67e74705SXin Li
203*67e74705SXin Li int i;
204*67e74705SXin Li for (i = 0; i < x; i++) // no-warning
205*67e74705SXin Li a(i); // no-warning
206*67e74705SXin Li
207*67e74705SXin Li for (i = 0; i < x; i++) { // no-warning
208*67e74705SXin Li a(i); // no-warning
209*67e74705SXin Li }
210*67e74705SXin Li
211*67e74705SXin Li for (i = 0;
212*67e74705SXin Li i < x;
213*67e74705SXin Li i++) // no-warning
214*67e74705SXin Li a(i); // no-warning
215*67e74705SXin Li
216*67e74705SXin Li int arr[3] = { 1, 2, 3 };
217*67e74705SXin Li for (int j : arr) // no-warning
218*67e74705SXin Li a(j);
219*67e74705SXin Li
220*67e74705SXin Li for (int j : arr) {} // no-warning
221*67e74705SXin Li
222*67e74705SXin Li while (b() == 0) // no-warning
223*67e74705SXin Li a(i); // no-warning
224*67e74705SXin Li
225*67e74705SXin Li while (b() == 0) {} // no-warning
226*67e74705SXin Li
227*67e74705SXin Li switch(x) // no-warning
228*67e74705SXin Li {
229*67e74705SXin Li switch(y) // no-warning
230*67e74705SXin Li {
231*67e74705SXin Li case 0:
232*67e74705SXin Li a(10);
233*67e74705SXin Li break;
234*67e74705SXin Li default:
235*67e74705SXin Li a(20);
236*67e74705SXin Li break;
237*67e74705SXin Li }
238*67e74705SXin Li }
239*67e74705SXin Li }
240*67e74705SXin Li
test_errors(int x)241*67e74705SXin Li void test_errors(int x) {
242*67e74705SXin Li if (1)
243*67e74705SXin Li aa; // expected-error{{use of undeclared identifier}}
244*67e74705SXin Li // no empty body warning.
245*67e74705SXin Li
246*67e74705SXin Li int i;
247*67e74705SXin Li for (i = 0; i < x; i++)
248*67e74705SXin Li bb; // expected-error{{use of undeclared identifier}}
249*67e74705SXin Li
250*67e74705SXin Li int arr[3] = { 1, 2, 3 };
251*67e74705SXin Li for (int j : arr)
252*67e74705SXin Li cc; // expected-error{{use of undeclared identifier}}
253*67e74705SXin Li
254*67e74705SXin Li while (b() == 0)
255*67e74705SXin Li dd; // expected-error{{use of undeclared identifier}}
256*67e74705SXin Li }
257*67e74705SXin Li
258*67e74705SXin Li // Warnings for statements in templates shouldn't be duplicated for all
259*67e74705SXin Li // instantiations.
260*67e74705SXin Li template <typename T>
test_template(int x)261*67e74705SXin Li void test_template(int x) {
262*67e74705SXin Li if (x); // expected-warning{{if statement has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}}
263*67e74705SXin Li
264*67e74705SXin Li if (x)
265*67e74705SXin Li EMPTY(x); // no-warning
266*67e74705SXin Li
267*67e74705SXin Li int arr[3] = { 1, 2, 3 };
268*67e74705SXin Li 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}}
269*67e74705SXin Li
270*67e74705SXin Li while (b() == 0); // expected-warning{{while loop has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}}
271*67e74705SXin Li a(x);
272*67e74705SXin Li }
273*67e74705SXin Li
test_template_inst(int x)274*67e74705SXin Li void test_template_inst(int x) {
275*67e74705SXin Li test_template<int>(x);
276*67e74705SXin Li test_template<double>(x);
277*67e74705SXin Li }
278*67e74705SXin Li
279*67e74705SXin Li #define IDENTITY(a) a
test7(int x,int y)280*67e74705SXin Li void test7(int x, int y) {
281*67e74705SXin Li if (x) IDENTITY(); // no-warning
282*67e74705SXin Li }
283*67e74705SXin Li
284