Debian Codegraph @

clang/test/AST/Interp/switch.cpp

raw
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
3
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;
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 }
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), "");
40
41
42 constexpr int withInit() {
43 switch(int a = 2; a) {
44 case 1: return -1;
45 case 2: return 2;
46 }
47 return -1;
48 }
49 static_assert(withInit() == 2, "");
50
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;
59 }
60
61 return -1;
62 }
63 static_assert(FT(1) == 1, "");
64 static_assert(FT(4) == 4, "");
65 static_assert(FT(5) == -1, "");
66
67
68 constexpr int good() { return 1; }
69 constexpr int test(int val) {
70 switch (val) {
71 case good(): return 100;
72 default: return -1;
73 }
74 return 0;
75 }
76 static_assert(test(1) == 100, "");
77
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;
84 }
85 return 0;
86 }
87 static_assert(another_test(1) == 100, ""); // both-error {{static assertion failed}} \
88 // both-note {{evaluates to}}