192 lines · cpp
1// RUN: %clang_cc1 -std=c++17 -fexperimental-new-constant-interpreter -verify=expected,both %s2// RUN: %clang_cc1 -std=c++17 -verify=ref,both %s3 4constexpr bool isEven(int a) {5 bool v = false;6 switch(a) {7 case 2: return true;8 case 4: return true;9 case 6: return true;10 11 case 8:12 case 10:13 case 12:14 case 14:15 case 16:16 return true;17 case 18:18 v = true;19 break;20 21 default:22 switch(a) {23 case 1:24 break;25 case 3:26 return false;27 default:28 break;29 }30 }31 32 return v;33}34static_assert(isEven(2), "");35static_assert(isEven(8), "");36static_assert(isEven(10), "");37static_assert(isEven(18), "");38static_assert(!isEven(1), "");39static_assert(!isEven(3), "");40 41 42constexpr int withInit() {43 switch(int a = 2; a) {44 case 1: return -1;45 case 2: return 2;46 }47 return -1;48}49static_assert(withInit() == 2, "");50 51constexpr int FT(int a) {52 int m = 0;53 switch(a) {54 case 4: m++;55 case 3: m++;56 case 2: m++;57 case 1: m++;58 return m;59 }60 61 return -1;62}63static_assert(FT(1) == 1, "");64static_assert(FT(4) == 4, "");65static_assert(FT(5) == -1, "");66 67 68constexpr int good() { return 1; }69constexpr int test(int val) {70 switch (val) {71 case good(): return 100;72 default: return -1;73 }74 return 0;75}76static_assert(test(1) == 100, "");77 78constexpr int bad(int val) { return val / 0; } // both-warning {{division by zero}}79constexpr int another_test(int val) { // both-note {{declared here}}80 switch (val) {81 case bad(val): return 100; // both-error {{case value is not a constant expression}} \82 // both-note {{cannot be used in a constant expression}}83 default: return -1;84 }85 return 0;86}87static_assert(another_test(1) == 100, ""); // both-error {{static assertion failed}} \88 // both-note {{evaluates to}}89 90namespace gnurange {91 constexpr int l(int n) {92 return n + 1;93 }94 constexpr int h(int n) {95 return 2 * n + 1;96 }97 constexpr int f(int x) {98 const int n = 2;99 constexpr struct {100 char lo {'a'};101 char hi {'z'};102 } s;103 104 switch (x) {105 case l(n) ... h(n):106 return 1;107 case -1 ... 1:108 return 2;109 case 9 ... 14:110 return 3;111 case 15:112 return 4;113 case 16 ... 20:114 return 5;115 case s.lo ... s.hi:116 return 6;117 default:118 return -1;119 }120 }121 static_assert(f(0) == 2);122 static_assert(f(2) == -1);123 static_assert(f(3) == 1);124 static_assert(f(4) == 1);125 static_assert(f(5) == 1);126 static_assert(f(6) == -1);127 static_assert(f(14) == 3);128 static_assert(f(15) == 4);129 static_assert(f(16) == 5);130 static_assert(f(20) == 5);131 static_assert(f('d') == 6);132 133 template <int Lo, int Hi>134 constexpr bool g(int x) {135 switch (x) {136 case Lo ... Hi:137 break;138 default:139 return false;140 }141 return true;142 }143 static_assert(g<100, 200>(132));144 145 constexpr bool m(int x) {146 switch (x) {147 case 10 ... 1: // both-warning {{empty case range specified}}148 return true;149 default:150 return false;151 }152 }153 static_assert(m(3)); // both-error {{static assertion failed due to requirement 'm(3)'}}154 static_assert(!m(3));155 156 constexpr bool j(int x) { // both-note {{declared here}}157 switch (x) {158 case bad(x) ... 100: // both-error {{case value is not a constant expression}} \159 // both-note {{cannot be used in a constant expression}}160 return true;161 default:162 break;163 }164 return false;165 }166 static_assert(j(1)); // both-error {{static assertion failed}}167 168 constexpr bool d(int x) { // both-note {{declared here}}169 switch (x) {170 case -100 ... bad(x): // both-error {{case value is not a constant expression}} \171 // both-note {{cannot be used in a constant expression}}172 return true;173 default:174 break;175 }176 return false;177 }178 static_assert(d(1)); // both-error {{static assertion failed}}179 180 constexpr bool s(int x) { // both-note {{declared here}}181 switch (x) {182 case bad(x) - 100 ... bad(x) + 100: // both-error {{case value is not a constant expression}} \183 // both-note {{cannot be used in a constant expression}}184 return true;185 default:186 break;187 }188 return false;189 }190 static_assert(s(1)); // both-error {{static assertion failed}}191}192