295 lines · cpp
1// RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify %s2 3void a(int i);4int b();5int c();6 7#define MACRO_A 08 9#define AND(x, y) ((x) && (y))10 11void 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 macro16 // 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 macro21 // 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 // PR1132931 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-warning77 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-warning85 {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.100void test2(int x, int y) {101 if (x) // no-warning102 ; // no-warning103 104 int i;105 for (i = 0; i < x; i++) // no-warning106 ; // no-warning107 108 for (i = 0;109 i < x;110 i++) // no-warning111 ; // no-warning112 113 int arr[3] = { 1, 2, 3 };114 for (int j : arr) // no-warning115 ; // no-warning116 117 while (b() == 0) // no-warning118 ; // no-warning119 120 while (b() == 0 ||121 c() == 0) // no-warning122 ; // no-warning123 124 switch(x)125 {126 switch(y) // no-warning127 ; // no-warning128 }129 130 // Last `for' or `while' statement in compound statement shouldn't warn.131 while(b() == 0); // no-warning132}133 134/// There should be no warning for a null statement resulting from an empty macro.135#define EMPTY(a)136void test3(int x, int y) {137 if (x) EMPTY(x); // no-warning138 139 int i;140 for (i = 0; i < x; i++) EMPTY(i); // no-warning141 142 for (i = 0;143 i < x;144 i++) EMPTY(i); // no-warning145 146 int arr[3] = { 1, 2, 3 };147 for (int j : arr) EMPTY(j); // no-warning148 149 for (int j :150 arr) EMPTY(j); // no-warning151 152 while (b() == 0) EMPTY(i); // no-warning153 154 while (b() == 0 ||155 c() == 0) EMPTY(i); // no-warning156 157 switch (x) {158 switch (y)159 EMPTY(i); // no-warning160 }161}162 163void test4(int x)164{165 // Idiom used in some metaprogramming constructs.166 switch (x) default:; // no-warning167 168 // Frequent idiom used in macros.169 do {} while (false); // no-warning170}171 172/// There should be no warning for a common for/while idiom when it is obvious173/// from indentation that next statement wasn't meant to be a body.174void 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-warning180 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-warning190 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-warning196 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-warning204 a(i);205}206 207/// There should be no warning for a statement with a non-null body.208void test6(int x, int y) {209 if (x) {} // no-warning210 211 if (x)212 a(x); // no-warning213 214 int i;215 for (i = 0; i < x; i++) // no-warning216 a(i); // no-warning217 218 for (i = 0; i < x; i++) { // no-warning219 a(i); // no-warning220 }221 222 for (i = 0;223 i < x;224 i++) // no-warning225 a(i); // no-warning226 227 int arr[3] = { 1, 2, 3 };228 for (int j : arr) // no-warning229 a(j);230 231 for (int j : arr) {} // no-warning232 233 while (b() == 0) // no-warning234 a(i); // no-warning235 236 while (b() == 0) {} // no-warning237 238 switch(x) // no-warning239 {240 switch(y) // no-warning241 {242 case 0:243 a(10);244 break;245 default:246 a(20);247 break;248 }249 }250}251 252void 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 all270// instantiations.271template <typename T>272void 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-warning277 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 285void test_template_inst(int x) {286 test_template<int>(x);287 test_template<double>(x);288}289 290#define IDENTITY(a) a291void test7(int x, int y) {292 if (x) IDENTITY(); // no-warning293}294 295