1 // RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify %s
7 void test1(int x, int y) {
9 if (x); // expected-warning {{if statement has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}}
13 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}}
18 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 i++); // expected-warning{{for loop has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}}
30 int arr[3] = { 1, 2, 3 };
31 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}}
35 arr); // expected-warning{{range-based for loop has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}}
38 while (b() == 0); // expected-warning{{while loop has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}}
41 while (b() == 0); { // expected-warning{{while 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}}
51 c() == 0); // expected-warning{{while loop has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}}
56 do; // expected-note{{to match this 'do'}}
57 b(); // expected-error{{expected 'while' in do/while loop}}
58 while (b()); // no-warning
61 do; // expected-note{{to match this 'do'}}
62 b(); // expected-error{{expected 'while' in do/while loop}}
63 while (b()); // expected-warning{{while loop has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}}
66 switch(x) // no-warning
68 switch(y); // expected-warning{{switch statement has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}}
81 /// There should be no warning when null statement is placed on its own line.
82 void test2(int x, int y) {
87 for (i = 0; i < x; i++) // no-warning
95 int arr[3] = { 1, 2, 3 };
96 for (int j : arr) // no-warning
99 while (b() == 0) // no-warning
103 c() == 0) // no-warning
108 switch(y) // no-warning
112 // Last `for' or `while' statement in compound statement shouldn't warn.
113 while(b() == 0); // no-warning
116 /// There should be no warning for a null statement resulting from an empty macro.
118 void test3(int x, int y) {
119 if (x) EMPTY(x); // no-warning
122 for (i = 0; i < x; i++) EMPTY(i); // no-warning
126 i++) EMPTY(i); // no-warning
128 int arr[3] = { 1, 2, 3 };
129 for (int j : arr) EMPTY(j); // no-warning
132 arr) EMPTY(j); // no-warning
134 while (b() == 0) EMPTY(i); // no-warning
137 c() == 0) EMPTY(i); // no-warning
141 EMPTY(i); // no-warning
147 // Idiom used in some metaprogramming constructs.
148 switch (x) default:; // no-warning
150 // Frequent idiom used in macros.
151 do {} while (false); // no-warning
154 /// There should be no warning for a common for/while idiom when it is obvious
155 /// from indentation that next statement wasn't meant to be a body.
156 void test5(int x, int y) {
158 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}}
161 for (i = 0; i < x; i++); // no-warning
166 i++); // expected-warning{{for loop has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}}
174 while (b() == 0); // expected-warning{{while loop has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}}
177 while (b() == 0); // no-warning
181 c() == 0); // expected-warning{{while loop has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}}
185 c() == 0); // no-warning
189 /// There should be no warning for a statement with a non-null body.
190 void test6(int x, int y) {
191 if (x) {} // no-warning
197 for (i = 0; i < x; i++) // no-warning
200 for (i = 0; i < x; i++) { // no-warning
209 int arr[3] = { 1, 2, 3 };
210 for (int j : arr) // no-warning
213 for (int j : arr) {} // no-warning
215 while (b() == 0) // no-warning
218 while (b() == 0) {} // no-warning
220 switch(x) // no-warning
222 switch(y) // no-warning
234 void test_errors(int x) {
236 aa; // expected-error{{use of undeclared identifier}}
237 // no empty body warning.
240 for (i = 0; i < x; i++)
241 bb; // expected-error{{use of undeclared identifier}}
243 int arr[3] = { 1, 2, 3 };
245 cc; // expected-error{{use of undeclared identifier}}
248 dd; // expected-error{{use of undeclared identifier}}
251 // Warnings for statements in templates shouldn't be duplicated for all
253 template <typename T>
254 void test_template(int x) {
255 if (x); // expected-warning{{if statement has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}}
258 EMPTY(x); // no-warning
260 int arr[3] = { 1, 2, 3 };
261 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}}
263 while (b() == 0); // expected-warning{{while loop has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}}
267 void test_template_inst(int x) {
268 test_template<int>(x);
269 test_template<double>(x);
272 #define IDENTITY(a) a
273 void test7(int x, int y) {
274 if (x) IDENTITY(); // no-warning