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