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