Clang Project

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