1 // RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify %s
9 void test1(int x
, int y
) {
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
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}}
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}}
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}}
32 i
++); // expected-warning{{for loop has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}}
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}}
42 arr
); // expected-warning{{range-based for loop has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}}
45 while (b() == 0); // expected-warning{{while loop has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}}
48 while (b() == 0); { // expected-warning{{while loop has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}}
52 while (b() == 0); // expected-warning{{while loop has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}}
58 c() == 0); // expected-warning{{while loop has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}}
63 do; // expected-note{{to match this 'do'}}
64 b(); // expected-error{{expected 'while' in do/while loop}}
65 while (b()); // no-warning
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}}
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}}
88 /// There should be no warning when null statement is placed on its own line.
89 void test2(int x
, int y
) {
94 for (i
= 0; i
< x
; i
++) // no-warning
102 int arr
[3] = { 1, 2, 3 };
103 for (int j
: arr
) // no-warning
106 while (b() == 0) // no-warning
110 c() == 0) // no-warning
115 switch(y
) // 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.
125 void test3(int x
, int y
) {
126 if (x
) EMPTY(x
); // no-warning
129 for (i
= 0; i
< x
; i
++) EMPTY(i
); // no-warning
133 i
++) EMPTY(i
); // no-warning
135 int arr
[3] = { 1, 2, 3 };
136 for (int j
: arr
) EMPTY(j
); // no-warning
139 arr
) EMPTY(j
); // no-warning
141 while (b() == 0) EMPTY(i
); // no-warning
144 c() == 0) EMPTY(i
); // no-warning
148 EMPTY(i
); // no-warning
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
) {
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}}
168 for (i
= 0; i
< x
; i
++); // no-warning
173 i
++); // expected-warning{{for loop has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}}
181 while (b() == 0); // expected-warning{{while loop has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}}
184 while (b() == 0); // no-warning
188 c() == 0); // expected-warning{{while loop has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}}
192 c() == 0); // no-warning
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
204 for (i
= 0; i
< x
; i
++) // no-warning
207 for (i
= 0; i
< x
; i
++) { // no-warning
216 int arr
[3] = { 1, 2, 3 };
217 for (int j
: arr
) // no-warning
220 for (int j
: arr
) {} // no-warning
222 while (b() == 0) // no-warning
225 while (b() == 0) {} // no-warning
227 switch(x
) // no-warning
229 switch(y
) // no-warning
241 void test_errors(int x
) {
243 aa
; // expected-error{{use of undeclared identifier}}
244 // no empty body warning.
247 for (i
= 0; i
< x
; i
++)
248 bb
; // expected-error{{use of undeclared identifier}}
250 int arr
[3] = { 1, 2, 3 };
252 cc
; // expected-error{{use of undeclared identifier}}
255 dd
; // expected-error{{use of undeclared identifier}}
258 // Warnings for statements in templates shouldn't be duplicated for all
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}}
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}}
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