Ted Kremenek | 1a2b8e2 | 2012-02-08 05:08:58 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -fsyntax-only -verify -Wswitch-enum -Wcovered-switch-default %s |
Chris Lattner | 8a87e57 | 2007-07-23 17:05:23 +0000 | [diff] [blame] | 2 | void f (int z) { |
| 3 | while (z) { |
Chris Lattner | 0471f5b | 2007-08-23 18:29:20 +0000 | [diff] [blame] | 4 | default: z--; // expected-error {{statement not in switch}} |
Chris Lattner | 8a87e57 | 2007-07-23 17:05:23 +0000 | [diff] [blame] | 5 | } |
| 6 | } |
| 7 | |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 8 | void foo(int X) { |
| 9 | switch (X) { |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 10 | case 42: ; // expected-note {{previous case}} |
Chris Lattner | 0471f5b | 2007-08-23 18:29:20 +0000 | [diff] [blame] | 11 | case 5000000000LL: // expected-warning {{overflow}} |
Douglas Gregor | 3940ce8 | 2012-05-16 05:32:58 +0000 | [diff] [blame] | 12 | case 42: // expected-error {{duplicate case value '42'}} |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 13 | ; |
Chris Lattner | 6efc4d3 | 2007-08-23 17:48:14 +0000 | [diff] [blame] | 14 | |
Chris Lattner | 0471f5b | 2007-08-23 18:29:20 +0000 | [diff] [blame] | 15 | case 100 ... 99: ; // expected-warning {{empty case range}} |
| 16 | |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 17 | case 43: ; // expected-note {{previous case}} |
Chris Lattner | 0471f5b | 2007-08-23 18:29:20 +0000 | [diff] [blame] | 18 | case 43 ... 45: ; // expected-error {{duplicate case value}} |
| 19 | |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 20 | case 100 ... 20000:; // expected-note {{previous case}} |
Chris Lattner | 0471f5b | 2007-08-23 18:29:20 +0000 | [diff] [blame] | 21 | case 15000 ... 40000000:; // expected-error {{duplicate case value}} |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 22 | } |
| 23 | } |
| 24 | |
Chris Lattner | f334850 | 2007-08-23 14:29:07 +0000 | [diff] [blame] | 25 | void test3(void) { |
Chris Lattner | 0471f5b | 2007-08-23 18:29:20 +0000 | [diff] [blame] | 26 | // empty switch; |
Dmitri Gribenko | 625bb56 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 27 | switch (0); // expected-warning {{no case matching constant switch condition '0'}} \ |
| 28 | // expected-warning {{switch statement has empty body}} \ |
| 29 | // expected-note{{put the semicolon on a separate line to silence this warning}} |
Chris Lattner | f334850 | 2007-08-23 14:29:07 +0000 | [diff] [blame] | 30 | } |
| 31 | |
Anders Carlsson | 51fe996 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 32 | extern int g(); |
| 33 | |
| 34 | void test4() |
| 35 | { |
John McCall | 0fb9708 | 2010-05-18 03:19:21 +0000 | [diff] [blame] | 36 | int cond; |
| 37 | switch (cond) { |
Anders Carlsson | 51fe996 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 38 | case 0 && g(): |
| 39 | case 1 || g(): |
| 40 | break; |
| 41 | } |
| 42 | |
John McCall | 0fb9708 | 2010-05-18 03:19:21 +0000 | [diff] [blame] | 43 | switch(cond) { |
Anders Carlsson | d3a61d5 | 2008-12-01 02:13:02 +0000 | [diff] [blame] | 44 | case g(): // expected-error {{expression is not an integer constant expression}} |
| 45 | case 0 ... g(): // expected-error {{expression is not an integer constant expression}} |
Anders Carlsson | 51fe996 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 46 | break; |
| 47 | } |
| 48 | |
John McCall | 0fb9708 | 2010-05-18 03:19:21 +0000 | [diff] [blame] | 49 | switch (cond) { |
Anders Carlsson | 51fe996 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 50 | case 0 && g() ... 1 || g(): |
| 51 | break; |
| 52 | } |
Anders Carlsson | 6dde0d5 | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 53 | |
John McCall | 0fb9708 | 2010-05-18 03:19:21 +0000 | [diff] [blame] | 54 | switch (cond) { |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 55 | case g() // expected-error {{expression is not an integer constant expression}} |
| 56 | && 0: |
Anders Carlsson | 6dde0d5 | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 57 | break; |
| 58 | } |
| 59 | |
John McCall | 0fb9708 | 2010-05-18 03:19:21 +0000 | [diff] [blame] | 60 | switch (cond) { |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 61 | case 0 ... |
| 62 | g() // expected-error {{expression is not an integer constant expression}} |
| 63 | || 1: |
Anders Carlsson | 6dde0d5 | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 64 | break; |
| 65 | } |
Anders Carlsson | 51fe996 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 68 | void test5(int z) { |
| 69 | switch(z) { |
| 70 | default: // expected-note {{previous case defined here}} |
| 71 | default: // expected-error {{multiple default labels in one switch}} |
| 72 | break; |
| 73 | } |
| 74 | } |
| 75 | |
Chris Lattner | 5f04881 | 2009-10-16 16:45:22 +0000 | [diff] [blame] | 76 | void test6() { |
John McCall | 0fb9708 | 2010-05-18 03:19:21 +0000 | [diff] [blame] | 77 | char ch = 'a'; |
Chris Lattner | 5f04881 | 2009-10-16 16:45:22 +0000 | [diff] [blame] | 78 | switch(ch) { |
| 79 | case 1234: // expected-warning {{overflow converting case value}} |
| 80 | break; |
| 81 | } |
| 82 | } |
Douglas Gregor | be724ba | 2009-11-25 06:20:02 +0000 | [diff] [blame] | 83 | |
| 84 | // PR5606 |
Douglas Gregor | e24b575 | 2010-10-14 20:34:08 +0000 | [diff] [blame] | 85 | int f0(int var) { |
Douglas Gregor | be724ba | 2009-11-25 06:20:02 +0000 | [diff] [blame] | 86 | switch (va) { // expected-error{{use of undeclared identifier 'va'}} |
| 87 | case 1: |
| 88 | break; |
| 89 | case 2: |
| 90 | return 1; |
| 91 | } |
| 92 | return 2; |
| 93 | } |
Douglas Gregor | ba915af | 2010-02-08 22:24:16 +0000 | [diff] [blame] | 94 | |
| 95 | void test7() { |
| 96 | enum { |
| 97 | A = 1, |
| 98 | B |
| 99 | } a; |
| 100 | switch(a) { //expected-warning{{enumeration value 'B' not handled in switch}} |
| 101 | case A: |
| 102 | break; |
| 103 | } |
| 104 | switch(a) { |
| 105 | case B: |
| 106 | case A: |
| 107 | break; |
| 108 | } |
| 109 | switch(a) { |
| 110 | case A: |
| 111 | case B: |
Fariborz Jahanian | 54faba4 | 2012-03-21 20:56:29 +0000 | [diff] [blame] | 112 | case 3: // expected-warning{{case value not in enumerated type 'enum <anonymous enum}} |
Douglas Gregor | ba915af | 2010-02-08 22:24:16 +0000 | [diff] [blame] | 113 | break; |
| 114 | } |
| 115 | switch(a) { |
| 116 | case A: |
| 117 | case B: |
Fariborz Jahanian | 54faba4 | 2012-03-21 20:56:29 +0000 | [diff] [blame] | 118 | case 3 ... //expected-warning{{case value not in enumerated type 'enum <anonymous enum}} |
| 119 | 4: //expected-warning{{case value not in enumerated type 'enum <anonymous enum}} |
Douglas Gregor | ba915af | 2010-02-08 22:24:16 +0000 | [diff] [blame] | 120 | break; |
| 121 | } |
| 122 | switch(a) { |
| 123 | case 1 ... 2: |
| 124 | break; |
| 125 | } |
| 126 | switch(a) { |
Fariborz Jahanian | 54faba4 | 2012-03-21 20:56:29 +0000 | [diff] [blame] | 127 | case 0 ... 2: //expected-warning{{case value not in enumerated type 'enum <anonymous enum}} |
Douglas Gregor | ba915af | 2010-02-08 22:24:16 +0000 | [diff] [blame] | 128 | break; |
| 129 | } |
| 130 | switch(a) { |
Fariborz Jahanian | 54faba4 | 2012-03-21 20:56:29 +0000 | [diff] [blame] | 131 | case 1 ... 3: //expected-warning{{case value not in enumerated type 'enum <anonymous enum}} |
Douglas Gregor | ba915af | 2010-02-08 22:24:16 +0000 | [diff] [blame] | 132 | break; |
| 133 | } |
| 134 | switch(a) { |
Fariborz Jahanian | 54faba4 | 2012-03-21 20:56:29 +0000 | [diff] [blame] | 135 | case 0 ... //expected-warning{{case value not in enumerated type 'enum <anonymous enum}} |
| 136 | 3: //expected-warning{{case value not in enumerated type 'enum <anonymous enum}} |
Douglas Gregor | ba915af | 2010-02-08 22:24:16 +0000 | [diff] [blame] | 137 | break; |
| 138 | } |
| 139 | |
| 140 | } |
| 141 | |
| 142 | void test8() { |
| 143 | enum { |
| 144 | A, |
| 145 | B, |
| 146 | C = 1 |
| 147 | } a; |
| 148 | switch(a) { |
| 149 | case A: |
| 150 | case B: |
| 151 | break; |
| 152 | } |
| 153 | switch(a) { |
| 154 | case A: |
| 155 | case C: |
| 156 | break; |
| 157 | } |
| 158 | switch(a) { //expected-warning{{enumeration value 'B' not handled in switch}} |
| 159 | case A: |
| 160 | break; |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | void test9() { |
| 165 | enum { |
| 166 | A = 3, |
| 167 | C = 1 |
| 168 | } a; |
| 169 | switch(a) { |
Fariborz Jahanian | 54faba4 | 2012-03-21 20:56:29 +0000 | [diff] [blame] | 170 | case 0: //expected-warning{{case value not in enumerated type 'enum <anonymous enum}} |
Douglas Gregor | ba915af | 2010-02-08 22:24:16 +0000 | [diff] [blame] | 171 | case 1: |
Fariborz Jahanian | 54faba4 | 2012-03-21 20:56:29 +0000 | [diff] [blame] | 172 | case 2: //expected-warning{{case value not in enumerated type 'enum <anonymous enum}} |
Douglas Gregor | ba915af | 2010-02-08 22:24:16 +0000 | [diff] [blame] | 173 | case 3: |
Fariborz Jahanian | 54faba4 | 2012-03-21 20:56:29 +0000 | [diff] [blame] | 174 | case 4: //expected-warning{{case value not in enumerated type 'enum <anonymous enum}} |
Douglas Gregor | ba915af | 2010-02-08 22:24:16 +0000 | [diff] [blame] | 175 | break; |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | void test10() { |
| 180 | enum { |
| 181 | A = 10, |
| 182 | C = 2, |
| 183 | B = 4, |
| 184 | D = 12 |
| 185 | } a; |
| 186 | switch(a) { |
Fariborz Jahanian | 54faba4 | 2012-03-21 20:56:29 +0000 | [diff] [blame] | 187 | case 0 ... //expected-warning{{case value not in enumerated type 'enum <anonymous enum}} |
| 188 | 1: //expected-warning{{case value not in enumerated type 'enum <anonymous enum}} |
Douglas Gregor | ba915af | 2010-02-08 22:24:16 +0000 | [diff] [blame] | 189 | case 2 ... 4: |
Fariborz Jahanian | 54faba4 | 2012-03-21 20:56:29 +0000 | [diff] [blame] | 190 | case 5 ... //expected-warning{{case value not in enumerated type 'enum <anonymous enum}} |
| 191 | 9: //expected-warning{{case value not in enumerated type 'enum <anonymous enum}} |
Douglas Gregor | ba915af | 2010-02-08 22:24:16 +0000 | [diff] [blame] | 192 | case 10 ... 12: |
Fariborz Jahanian | 54faba4 | 2012-03-21 20:56:29 +0000 | [diff] [blame] | 193 | case 13 ... //expected-warning{{case value not in enumerated type 'enum <anonymous enum}} |
| 194 | 16: //expected-warning{{case value not in enumerated type 'enum <anonymous enum}} |
Douglas Gregor | ba915af | 2010-02-08 22:24:16 +0000 | [diff] [blame] | 195 | break; |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | void test11() { |
| 200 | enum { |
| 201 | A = -1, |
| 202 | B, |
| 203 | C |
| 204 | } a; |
| 205 | switch(a) { //expected-warning{{enumeration value 'A' not handled in switch}} |
| 206 | case B: |
| 207 | case C: |
| 208 | break; |
| 209 | } |
| 210 | |
David Blaikie | e1d82de | 2012-01-24 04:56:25 +0000 | [diff] [blame] | 211 | switch(a) { //expected-warning{{enumeration value 'A' not explicitly handled in switch}} |
Douglas Gregor | ba915af | 2010-02-08 22:24:16 +0000 | [diff] [blame] | 212 | case B: |
| 213 | case C: |
| 214 | break; |
| 215 | |
| 216 | default: |
| 217 | break; |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | void test12() { |
| 222 | enum { |
| 223 | A = -1, |
| 224 | B = 4294967286 |
| 225 | } a; |
| 226 | switch(a) { |
| 227 | case A: |
| 228 | case B: |
| 229 | break; |
| 230 | } |
| 231 | } |
Douglas Gregor | 30ab371 | 2010-02-17 23:29:11 +0000 | [diff] [blame] | 232 | |
| 233 | // <rdar://problem/7643909> |
| 234 | typedef enum { |
| 235 | val1, |
| 236 | val2, |
| 237 | val3 |
| 238 | } my_type_t; |
| 239 | |
| 240 | int test13(my_type_t t) { |
| 241 | switch(t) { // expected-warning{{enumeration value 'val3' not handled in switch}} |
| 242 | case val1: |
| 243 | return 1; |
| 244 | case val2: |
| 245 | return 2; |
| 246 | } |
| 247 | return -1; |
| 248 | } |
Douglas Gregor | 2853eac | 2010-02-18 00:56:01 +0000 | [diff] [blame] | 249 | |
| 250 | // <rdar://problem/7658121> |
| 251 | enum { |
| 252 | EC0 = 0xFFFF0000, |
| 253 | EC1 = 0xFFFF0001, |
| 254 | }; |
| 255 | |
| 256 | int test14(int a) { |
| 257 | switch(a) { |
| 258 | case EC0: return 0; |
| 259 | case EC1: return 1; |
| 260 | } |
| 261 | return 0; |
| 262 | } |
Douglas Gregor | f9f627d | 2010-03-01 01:04:55 +0000 | [diff] [blame] | 263 | |
| 264 | void f1(unsigned x) { |
| 265 | switch (x) { |
| 266 | case -1: break; |
| 267 | default: break; |
| 268 | } |
| 269 | } |
John McCall | 0fb9708 | 2010-05-18 03:19:21 +0000 | [diff] [blame] | 270 | |
| 271 | void test15() { |
| 272 | int i = 0; |
| 273 | switch (1) { // expected-warning {{no case matching constant switch condition '1'}} |
| 274 | case 0: i = 0; break; |
| 275 | case 2: i++; break; |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | void test16() { |
| 280 | const char c = '5'; |
| 281 | switch (c) { // expected-warning {{no case matching constant switch condition '53'}} |
| 282 | case '6': return; |
| 283 | } |
| 284 | } |
John McCall | 6907fbe | 2010-06-12 01:56:02 +0000 | [diff] [blame] | 285 | |
| 286 | // PR7359 |
| 287 | void test17(int x) { |
| 288 | switch (x >= 17) { // expected-warning {{switch condition has boolean value}} |
| 289 | case 0: return; |
| 290 | } |
| 291 | |
| 292 | switch ((int) (x <= 17)) { |
| 293 | case 0: return; |
| 294 | } |
| 295 | } |
David Blaikie | 31ceb61 | 2012-01-21 18:12:07 +0000 | [diff] [blame] | 296 | |
| 297 | int test18() { |
| 298 | enum { A, B } a; |
| 299 | switch (a) { |
| 300 | case A: return 0; |
| 301 | case B: return 1; |
David Blaikie | 9366750 | 2012-01-22 02:31:55 +0000 | [diff] [blame] | 302 | case 7: return 1; // expected-warning {{case value not in enumerated type}} |
David Blaikie | 2dd52e3 | 2012-01-24 05:34:08 +0000 | [diff] [blame] | 303 | default: return 2; // expected-warning {{default label in switch which covers all enumeration values}} |
David Blaikie | 9366750 | 2012-01-22 02:31:55 +0000 | [diff] [blame] | 304 | } |
| 305 | } |
Fariborz Jahanian | 54faba4 | 2012-03-21 20:56:29 +0000 | [diff] [blame] | 306 | |
| 307 | // rdar://110822110 |
| 308 | typedef enum { |
| 309 | kOne = 1, |
| 310 | } Ints; |
| 311 | |
| 312 | void rdar110822110(Ints i) |
| 313 | { |
| 314 | switch (i) { |
| 315 | case kOne: |
| 316 | break; |
| 317 | case 2: // expected-warning {{case value not in enumerated type 'Ints'}} |
| 318 | break; |
| 319 | default: // expected-warning {{default label in switch which covers all enumeration values}} |
| 320 | break; |
| 321 | } |
| 322 | } |
Douglas Gregor | 3940ce8 | 2012-05-16 05:32:58 +0000 | [diff] [blame] | 323 | |
| 324 | // PR9243 |
| 325 | #define TEST19MACRO 5 |
| 326 | void test19(int i) { |
| 327 | enum { |
| 328 | kTest19Enum1 = 7, |
Richard Trieu | 7af7de7 | 2012-05-30 01:01:11 +0000 | [diff] [blame] | 329 | kTest19Enum2 = kTest19Enum1 |
Douglas Gregor | 3940ce8 | 2012-05-16 05:32:58 +0000 | [diff] [blame] | 330 | }; |
| 331 | const int a = 3; |
| 332 | switch (i) { |
| 333 | case 5: // expected-note {{previous case}} |
| 334 | case TEST19MACRO: // expected-error {{duplicate case value '5'}} |
| 335 | |
| 336 | case 7: // expected-note {{previous case}} |
| 337 | case kTest19Enum1: // expected-error {{duplicate case value: '7' and 'kTest19Enum1' both equal '7'}} \ |
| 338 | // expected-note {{previous case}} |
| 339 | case kTest19Enum1: // expected-error {{duplicate case value 'kTest19Enum1'}} \ |
| 340 | // expected-note {{previous case}} |
| 341 | case kTest19Enum2: // expected-error {{duplicate case value: 'kTest19Enum1' and 'kTest19Enum2' both equal '7'}} \ |
| 342 | // expected-note {{previous case}} |
| 343 | case (int)kTest19Enum2: //expected-error {{duplicate case value 'kTest19Enum2'}} |
| 344 | |
| 345 | case 3: // expected-note {{previous case}} |
| 346 | case a: // expected-error {{duplicate case value: '3' and 'a' both equal '3'}} \ |
| 347 | // expected-note {{previous case}} |
| 348 | case a: // expected-error {{duplicate case value 'a'}} |
| 349 | break; |
| 350 | } |
| 351 | } |