[clang] Implement lifetime analysis for lifetime_capture_by(X) (#115921)
[llvm-project.git] / clang / test / AST / ByteCode / switch.cpp
blob8136f92f5dfd3f1799b70b8ed1506e94b90d9378
1 // RUN: %clang_cc1 -std=c++17 -fexperimental-new-constant-interpreter -verify=expected,both %s
2 // RUN: %clang_cc1 -std=c++17 -verify=ref,both %s
4 constexpr 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;
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;
21 default:
22 switch(a) {
23 case 1:
24 break;
25 case 3:
26 return false;
27 default:
28 break;
32 return v;
34 static_assert(isEven(2), "");
35 static_assert(isEven(8), "");
36 static_assert(isEven(10), "");
37 static_assert(isEven(18), "");
38 static_assert(!isEven(1), "");
39 static_assert(!isEven(3), "");
42 constexpr int withInit() {
43 switch(int a = 2; a) {
44 case 1: return -1;
45 case 2: return 2;
47 return -1;
49 static_assert(withInit() == 2, "");
51 constexpr 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;
61 return -1;
63 static_assert(FT(1) == 1, "");
64 static_assert(FT(4) == 4, "");
65 static_assert(FT(5) == -1, "");
68 constexpr int good() { return 1; }
69 constexpr int test(int val) {
70 switch (val) {
71 case good(): return 100;
72 default: return -1;
74 return 0;
76 static_assert(test(1) == 100, "");
78 constexpr int bad(int val) { return val / 0; } // both-warning {{division by zero}}
79 constexpr 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;
85 return 0;
87 static_assert(another_test(1) == 100, ""); // both-error {{static assertion failed}} \
88 // both-note {{evaluates to}}