106 lines · c
1// RUN: %clang_cc1 -verify=expected,c-expected -std=c2y -Wall -pedantic %s2// RUN: %clang_cc1 -verify=expected,c-expected,ped -std=c23 -Wall -pedantic %s3// RUN: %clang_cc1 -verify=expected,cxx-expected,gnu -Wall -pedantic -x c++ %s4// RUN: %clang_cc1 -verify=expected,c-expected,pre -std=c2y -Wpre-c2y-compat -Wall -pedantic %s5 6/* WG14 N3370: Yes7 * Case range expressions v3.18 *9 * This introduces the ability to specify closed ranges in case statements in a10 * switch statement. This was already a well-supported Clang extension before11 * it was standardized.12 */13 14void correct(int i) {15 constexpr int j = 100, k = 200;16 switch (i) {17 case 12 ... 14: break; /* gnu-warning {{case ranges are a GNU extension}}18 ped-warning {{case ranges are a C2y extension}}19 pre-warning {{case ranges are incompatible with C standards before C2y}}20 */21 // Implementations are encouraged to diagnose empty ranges.22 case 15 ... 11: break; /* expected-warning {{empty case range specified}}23 gnu-warning {{case ranges are a GNU extension}}24 ped-warning {{case ranges are a C2y extension}}25 pre-warning {{case ranges are incompatible with C standards before C2y}}26 */27 // This is not an empty range, it's a range of a single value.28 case 10 ... 10: break; /* gnu-warning {{case ranges are a GNU extension}}29 ped-warning {{case ranges are a C2y extension}}30 pre-warning {{case ranges are incompatible with C standards before C2y}}31 */32 case j ... k: break; /* gnu-warning {{case ranges are a GNU extension}}33 ped-warning {{case ranges are a C2y extension}}34 pre-warning {{case ranges are incompatible with C standards before C2y}}35 */36 }37}38 39void incorrect(int i) { // cxx-expected-note 2 {{declared here}}40 switch (i) {41 // The values have to be integer constant expressions. Note that when the42 // initial value in the range is an error, we don't issue the warnings about43 // extensions or incompatibility.44 case i ... 10: break; /* c-expected-error {{expression is not an integer constant expression}}45 cxx-expected-error {{case value is not a constant expression}}46 cxx-expected-note {{function parameter 'i' with unknown value cannot be used in a constant expression}}47 */48 case 10 ... i: break; /* c-expected-error {{expression is not an integer constant expression}}49 cxx-expected-error {{case value is not a constant expression}}50 cxx-expected-note {{function parameter 'i' with unknown value cannot be used in a constant expression}}51 gnu-warning {{case ranges are a GNU extension}}52 ped-warning {{case ranges are a C2y extension}}53 pre-warning {{case ranges are incompatible with C standards before C2y}}54 */55 case 1.3f ... 10: break; /* c-expected-error {{integer constant expression must have integer type, not 'float'}}56 cxx-expected-error {{conversion from 'float' to 'int' is not allowed in a converted constant expression}}57 */58 case 10 ... "a": break; /* c-expected-error {{integer constant expression must have integer type, not 'char[2]'}}59 cxx-expected-error {{value of type 'const char[2]' is not implicitly convertible to 'int'}}60 gnu-warning {{case ranges are a GNU extension}}61 ped-warning {{case ranges are a C2y extension}}62 pre-warning {{case ranges are incompatible with C standards before C2y}}63 */64 }65 66 switch (i) {67 // Cannot have multiple cases covering the same value.68 // FIXME: diagnostic quality here is poor. The "previous case" note is69 // showing up on a subsequent line (I'd expect the error and note to be70 // reversed), and "duplicate case value 20" is showing up on a line where71 // there is no duplicate value 20 to begin with.72 case 10 ... 20: break; /* expected-error {{duplicate case value '11'}}73 expected-note {{previous case defined here}}74 gnu-warning {{case ranges are a GNU extension}}75 ped-warning {{case ranges are a C2y extension}}76 pre-warning {{case ranges are incompatible with C standards before C2y}}77 */78 case 11: break; /* expected-note {{previous case defined here}}79 */80 case 11 ... 14: break; /* expected-error {{duplicate case value '20'}}81 gnu-warning {{case ranges are a GNU extension}}82 ped-warning {{case ranges are a C2y extension}}83 pre-warning {{case ranges are incompatible with C standards before C2y}}84 */85 }86 87 // The values specified by the range shall not change as a result of88 // conversion to the promoted type of the controlling expression.89 // FIXME: the overflow warnings seem like they probably should also trigger90 // in C++ as they do in C.91 switch ((unsigned char)i) {92 case 254 ... 256: break; /* c-expected-warning {{overflow converting case value to switch condition type (256 to 0)}}93 gnu-warning {{case ranges are a GNU extension}}94 ped-warning {{case ranges are a C2y extension}}95 pre-warning {{case ranges are incompatible with C standards before C2y}}96 */97 case 257 ... 258: break; /* c-expected-warning {{overflow converting case value to switch condition type (257 to 1)}}98 c-expected-warning {{overflow converting case value to switch condition type (258 to 2)}}99 gnu-warning {{case ranges are a GNU extension}}100 ped-warning {{case ranges are a C2y extension}}101 pre-warning {{case ranges are incompatible with C standards before C2y}}102 */103 }104}105 106