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