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