summaryrefslogtreecommitdiff
path: root/clang/test/AST/ByteCode/switch.cpp
blob: 151b4a5852e8888f9c741c82468783abe2472977 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// RUN: %clang_cc1 -std=c++17 -fexperimental-new-constant-interpreter -verify=expected,both %s
// RUN: %clang_cc1 -std=c++17 -verify=ref,both %s

constexpr bool isEven(int a) {
  bool v = false;
  switch(a) {
  case 2: return true;
  case 4: return true;
  case 6: return true;

  case 8:
  case 10:
  case 12:
  case 14:
  case 16:
    return true;
  case 18:
    v = true;
    break;

  default:
  switch(a) {
  case 1:
    break;
  case 3:
    return false;
  default:
    break;
  }
  }

  return v;
}
static_assert(isEven(2), "");
static_assert(isEven(8), "");
static_assert(isEven(10), "");
static_assert(isEven(18), "");
static_assert(!isEven(1), "");
static_assert(!isEven(3), "");


constexpr int withInit() {
  switch(int a = 2; a) {
    case 1: return -1;
    case 2: return 2;
  }
  return -1;
}
static_assert(withInit() == 2, "");

constexpr int FT(int a) {
  int m = 0;
  switch(a) {
  case 4: m++;
  case 3: m++;
  case 2: m++;
  case 1: m++;
    return m;
  }

  return -1;
}
static_assert(FT(1) == 1, "");
static_assert(FT(4) == 4, "");
static_assert(FT(5) == -1, "");


constexpr int good() { return 1; }
constexpr int test(int val) {
  switch (val) {
  case good(): return 100;
  default: return -1;
  }
  return 0;
}
static_assert(test(1) == 100, "");

constexpr int bad(int val) { return val / 0; } // both-warning {{division by zero}}
constexpr int another_test(int val) { // both-note {{declared here}}
  switch (val) {
  case bad(val): return 100; // both-error {{case value is not a constant expression}} \
                             // both-note {{cannot be used in a constant expression}}
  default: return -1;
  }
  return 0;
}
static_assert(another_test(1) == 100, ""); // both-error {{static assertion failed}} \
                                           // both-note {{evaluates to}}

namespace gnurange {
  constexpr int l(int n) {
    return n + 1;
  }
  constexpr int h(int n) {
    return 2 * n + 1;
  }
  constexpr int f(int x) {
    const int n = 2;
    constexpr struct {
      char lo {'a'};
      char hi {'z'};
    } s;

    switch (x) {
      case l(n) ... h(n):
        return 1;
      case -1 ... 1:
        return 2;
      case 9 ... 14:
        return 3;
      case 15:
        return 4;
      case 16 ... 20:
        return 5;
      case s.lo ... s.hi:
        return 6;
      default:
        return -1;
    }
  }
  static_assert(f(0) == 2);
  static_assert(f(2) == -1);
  static_assert(f(3) == 1);
  static_assert(f(4) == 1);
  static_assert(f(5) == 1);
  static_assert(f(6) == -1);
  static_assert(f(14) == 3);
  static_assert(f(15) == 4);
  static_assert(f(16) == 5);
  static_assert(f(20) == 5);
  static_assert(f('d') == 6);

  template <int Lo, int Hi>
  constexpr bool g(int x) {
    switch (x) {
      case Lo ... Hi:
        break;
      default:
        return false;
    }
    return true;
  }
  static_assert(g<100, 200>(132));

  constexpr bool m(int x) {
    switch (x) {
      case 10 ... 1: // both-warning {{empty case range specified}}
        return true;
      default:
        return false;
    }
  }
  static_assert(m(3)); // both-error {{static assertion failed due to requirement 'm(3)'}}
  static_assert(!m(3));

  constexpr bool j(int x) { // both-note {{declared here}}
    switch (x) {
      case bad(x) ... 100: // both-error {{case value is not a constant expression}} \
                           // both-note {{cannot be used in a constant expression}}
        return true;
      default:
        break;
    }
    return false;
  }
  static_assert(j(1)); // both-error {{static assertion failed}}

  constexpr bool d(int x) { // both-note {{declared here}}
    switch (x) {
      case -100 ... bad(x): // both-error {{case value is not a constant expression}} \
                            // both-note {{cannot be used in a constant expression}}
        return true;
      default:
        break;
    }
    return false;
  }
  static_assert(d(1)); // both-error {{static assertion failed}}
  
  constexpr bool s(int x) { // both-note {{declared here}}
    switch (x) {
      case bad(x) - 100 ... bad(x) + 100: // both-error {{case value is not a constant expression}} \
                                          // both-note {{cannot be used in a constant expression}}
        return true;
      default:
        break;
    }
    return false;
  }
  static_assert(s(1)); // both-error {{static assertion failed}}
}