switch.c revision 1a2b8e27135ec72f183dd4510612f6d5d7ee088a
1// RUN: %clang_cc1 -fsyntax-only -verify -Wswitch-enum -Wcovered-switch-default %s 2void f (int z) { 3 while (z) { 4 default: z--; // expected-error {{statement not in switch}} 5 } 6} 7 8void foo(int X) { 9 switch (X) { 10 case 42: ; // expected-note {{previous case}} 11 case 5000000000LL: // expected-warning {{overflow}} 12 case 42: // expected-error {{duplicate case value}} 13 ; 14 15 case 100 ... 99: ; // expected-warning {{empty case range}} 16 17 case 43: ; // expected-note {{previous case}} 18 case 43 ... 45: ; // expected-error {{duplicate case value}} 19 20 case 100 ... 20000:; // expected-note {{previous case}} 21 case 15000 ... 40000000:; // expected-error {{duplicate case value}} 22 } 23} 24 25void test3(void) { 26 // empty switch; 27 switch (0); // expected-warning {{no case matching constant switch condition '0'}} 28} 29 30extern int g(); 31 32void test4() 33{ 34 int cond; 35 switch (cond) { 36 case 0 && g(): 37 case 1 || g(): 38 break; 39 } 40 41 switch(cond) { 42 case g(): // expected-error {{expression is not an integer constant expression}} 43 case 0 ... g(): // expected-error {{expression is not an integer constant expression}} 44 break; 45 } 46 47 switch (cond) { 48 case 0 && g() ... 1 || g(): 49 break; 50 } 51 52 switch (cond) { 53 case g() // expected-error {{expression is not an integer constant expression}} 54 && 0: 55 break; 56 } 57 58 switch (cond) { 59 case 0 ... 60 g() // expected-error {{expression is not an integer constant expression}} 61 || 1: 62 break; 63 } 64} 65 66void test5(int z) { 67 switch(z) { 68 default: // expected-note {{previous case defined here}} 69 default: // expected-error {{multiple default labels in one switch}} 70 break; 71 } 72} 73 74void test6() { 75 char ch = 'a'; 76 switch(ch) { 77 case 1234: // expected-warning {{overflow converting case value}} 78 break; 79 } 80} 81 82// PR5606 83int f0(int var) { 84 switch (va) { // expected-error{{use of undeclared identifier 'va'}} 85 case 1: 86 break; 87 case 2: 88 return 1; 89 } 90 return 2; 91} 92 93void test7() { 94 enum { 95 A = 1, 96 B 97 } a; 98 switch(a) { //expected-warning{{enumeration value 'B' not handled in switch}} 99 case A: 100 break; 101 } 102 switch(a) { 103 case B: 104 case A: 105 break; 106 } 107 switch(a) { 108 case A: 109 case B: 110 case 3: // expected-warning{{case value not in enumerated type ''}} 111 break; 112 } 113 switch(a) { 114 case A: 115 case B: 116 case 3 ... //expected-warning{{case value not in enumerated type ''}} 117 4: //expected-warning{{case value not in enumerated type ''}} 118 break; 119 } 120 switch(a) { 121 case 1 ... 2: 122 break; 123 } 124 switch(a) { 125 case 0 ... 2: //expected-warning{{case value not in enumerated type ''}} 126 break; 127 } 128 switch(a) { 129 case 1 ... 3: //expected-warning{{case value not in enumerated type ''}} 130 break; 131 } 132 switch(a) { 133 case 0 ... //expected-warning{{case value not in enumerated type ''}} 134 3: //expected-warning{{case value not in enumerated type ''}} 135 break; 136 } 137 138} 139 140void test8() { 141 enum { 142 A, 143 B, 144 C = 1 145 } a; 146 switch(a) { 147 case A: 148 case B: 149 break; 150 } 151 switch(a) { 152 case A: 153 case C: 154 break; 155 } 156 switch(a) { //expected-warning{{enumeration value 'B' not handled in switch}} 157 case A: 158 break; 159 } 160} 161 162void test9() { 163 enum { 164 A = 3, 165 C = 1 166 } a; 167 switch(a) { 168 case 0: //expected-warning{{case value not in enumerated type ''}} 169 case 1: 170 case 2: //expected-warning{{case value not in enumerated type ''}} 171 case 3: 172 case 4: //expected-warning{{case value not in enumerated type ''}} 173 break; 174 } 175} 176 177void test10() { 178 enum { 179 A = 10, 180 C = 2, 181 B = 4, 182 D = 12 183 } a; 184 switch(a) { 185 case 0 ... //expected-warning{{case value not in enumerated type ''}} 186 1: //expected-warning{{case value not in enumerated type ''}} 187 case 2 ... 4: 188 case 5 ... //expected-warning{{case value not in enumerated type ''}} 189 9: //expected-warning{{case value not in enumerated type ''}} 190 case 10 ... 12: 191 case 13 ... //expected-warning{{case value not in enumerated type ''}} 192 16: //expected-warning{{case value not in enumerated type ''}} 193 break; 194 } 195} 196 197void test11() { 198 enum { 199 A = -1, 200 B, 201 C 202 } a; 203 switch(a) { //expected-warning{{enumeration value 'A' not handled in switch}} 204 case B: 205 case C: 206 break; 207 } 208 209 switch(a) { //expected-warning{{enumeration value 'A' not explicitly handled in switch}} 210 case B: 211 case C: 212 break; 213 214 default: 215 break; 216 } 217} 218 219void test12() { 220 enum { 221 A = -1, 222 B = 4294967286 223 } a; 224 switch(a) { 225 case A: 226 case B: 227 break; 228 } 229} 230 231// <rdar://problem/7643909> 232typedef enum { 233 val1, 234 val2, 235 val3 236} my_type_t; 237 238int test13(my_type_t t) { 239 switch(t) { // expected-warning{{enumeration value 'val3' not handled in switch}} 240 case val1: 241 return 1; 242 case val2: 243 return 2; 244 } 245 return -1; 246} 247 248// <rdar://problem/7658121> 249enum { 250 EC0 = 0xFFFF0000, 251 EC1 = 0xFFFF0001, 252}; 253 254int test14(int a) { 255 switch(a) { 256 case EC0: return 0; 257 case EC1: return 1; 258 } 259 return 0; 260} 261 262void f1(unsigned x) { 263 switch (x) { 264 case -1: break; 265 default: break; 266 } 267} 268 269void test15() { 270 int i = 0; 271 switch (1) { // expected-warning {{no case matching constant switch condition '1'}} 272 case 0: i = 0; break; 273 case 2: i++; break; 274 } 275} 276 277void test16() { 278 const char c = '5'; 279 switch (c) { // expected-warning {{no case matching constant switch condition '53'}} 280 case '6': return; 281 } 282} 283 284// PR7359 285void test17(int x) { 286 switch (x >= 17) { // expected-warning {{switch condition has boolean value}} 287 case 0: return; 288 } 289 290 switch ((int) (x <= 17)) { 291 case 0: return; 292 } 293} 294 295int test18() { 296 enum { A, B } a; 297 switch (a) { 298 case A: return 0; 299 case B: return 1; 300 case 7: return 1; // expected-warning {{case value not in enumerated type}} 301 default: return 2; // expected-warning {{default label in switch which covers all enumeration values}} 302 } 303} 304