1 // RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify %s
9 #define AND(x, y) ((x) && (y))
11 void test1(int x
, int y
) {
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
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.
23 c())); // expected-warning {{if statement has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}}
26 c())); // expected-warning{{while loop has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}}
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}}
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}}
43 i
++); // expected-warning{{for loop has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}}
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}}
53 arr
); // expected-warning{{range-based for loop has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}}
56 while (b() == 0); // expected-warning{{while loop has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}}
59 while (b() == 0); { // expected-warning{{while loop has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}}
63 while (b() == 0); // expected-warning{{while loop has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}}
69 c() == 0); // expected-warning{{while loop has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}}
74 do; // expected-note{{to match this 'do'}}
75 b(); // expected-error{{expected 'while' in do/while loop}}
76 while (b()); // no-warning
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}}
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}}
99 /// There should be no warning when null statement is placed on its own line.
100 void test2(int x
, int y
) {
105 for (i
= 0; i
< x
; i
++) // no-warning
113 int arr
[3] = { 1, 2, 3 };
114 for (int j
: arr
) // no-warning
117 while (b() == 0) // no-warning
121 c() == 0) // no-warning
126 switch(y
) // 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.
136 void test3(int x
, int y
) {
137 if (x
) EMPTY(x
); // no-warning
140 for (i
= 0; i
< x
; i
++) EMPTY(i
); // no-warning
144 i
++) EMPTY(i
); // no-warning
146 int arr
[3] = { 1, 2, 3 };
147 for (int j
: arr
) EMPTY(j
); // no-warning
150 arr
) EMPTY(j
); // no-warning
152 while (b() == 0) EMPTY(i
); // no-warning
155 c() == 0) EMPTY(i
); // no-warning
159 EMPTY(i
); // no-warning
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
) {
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}}
179 for (i
= 0; i
< x
; i
++); // no-warning
184 i
++); // expected-warning{{for loop has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}}
192 while (b() == 0); // expected-warning{{while loop has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}}
195 while (b() == 0); // no-warning
199 c() == 0); // expected-warning{{while loop has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}}
203 c() == 0); // no-warning
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
215 for (i
= 0; i
< x
; i
++) // no-warning
218 for (i
= 0; i
< x
; i
++) { // no-warning
227 int arr
[3] = { 1, 2, 3 };
228 for (int j
: arr
) // no-warning
231 for (int j
: arr
) {} // no-warning
233 while (b() == 0) // no-warning
236 while (b() == 0) {} // no-warning
238 switch(x
) // no-warning
240 switch(y
) // no-warning
252 void test_errors(int x
) {
254 aa
; // expected-error{{use of undeclared identifier}}
255 // no empty body warning.
258 for (i
= 0; i
< x
; i
++)
259 bb
; // expected-error{{use of undeclared identifier}}
261 int arr
[3] = { 1, 2, 3 };
263 cc
; // expected-error{{use of undeclared identifier}}
266 dd
; // expected-error{{use of undeclared identifier}}
269 // Warnings for statements in templates shouldn't be duplicated for all
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}}
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}}
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