aboutsummaryrefslogtreecommitdiff
path: root/clang/test/AST/ByteCode/cxx2a.cpp
blob: 533173d84792fe0c0c0d987d0d11477aa63c9c83 (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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
// RUN: %clang_cc1 -std=c++2a -fsyntax-only -fcxx-exceptions -verify=ref,both %s
// RUN: %clang_cc1 -std=c++2a -fsyntax-only -fcxx-exceptions -verify=expected,both %s -fexperimental-new-constant-interpreter


namespace std {
  struct type_info;
  struct destroying_delete_t {
    explicit destroying_delete_t() = default;
  } inline constexpr destroying_delete{};
  struct nothrow_t {
    explicit nothrow_t() = default;
  } inline constexpr nothrow{};
  using size_t = decltype(sizeof(0));
  enum class align_val_t : size_t {};
};

constexpr void *operator new(std::size_t, void *p) { return p; }
namespace std {
  template<typename T> constexpr T *construct(T *p) { return new (p) T; }
  template<typename T> constexpr void destroy(T *p) { p->~T(); }
}

template <unsigned N>
struct S {
  S() requires (N==1) = default;
  S() requires (N==2) {} // both-note {{declared here}}
  consteval S() requires (N==3) = default;
};

consteval int aConstevalFunction() { // both-error {{consteval function never produces a constant expression}}
  S<2> s4; // both-note {{non-constexpr constructor 'S' cannot be used in a constant expression}}
  return 0;
}
/// We're NOT calling the above function. The diagnostics should appear anyway.

namespace Covariant {
  struct A {
    virtual constexpr char f() const { return 'Z'; }
    char a = f();
  };

  struct D : A {};
  struct Covariant1 {
    D d;
    virtual const A *f() const;
  };

  struct Covariant3 : Covariant1 {
    constexpr virtual const D *f() const { return &this->d; }
  };

  constexpr Covariant3 cb;
  constexpr const Covariant1 *cb1 = &cb;
  static_assert(cb1->f()->a == 'Z');
}

namespace DtorOrder {
  struct Buf {
    char buf[64];
    int n = 0;
    constexpr void operator+=(char c) { buf[n++] = c; }
    constexpr bool operator==(const char *str) const {
      if (str[n] != 0)
        return false;

      for (int i = 0; i < n; ++i) {
        if (buf[n] != str[n])
          return false;
      }
      return true;

      return __builtin_memcmp(str, buf, n) == 0;
    }
    constexpr bool operator!=(const char *str) const { return !operator==(str); }
  };

  struct A {
    constexpr A(Buf &buf, char c) : buf(buf), c(c) { buf += c; }
    constexpr ~A() { buf += (c - 32);}
    constexpr operator bool() const { return true; }
    Buf &buf;
    char c;
  };

  constexpr void abnormal_termination(Buf &buf) {
    struct Indestructible {
      constexpr ~Indestructible(); // not defined
    };
    A a(buf, 'a');
    A(buf, 'b');
    int n = 0;

    for (A &&c = A(buf, 'c'); A d = A(buf, 'd'); A(buf, 'e')) {
      switch (A f(buf, 'f'); A g = A(buf, 'g')) { // both-warning {{boolean}}
      case false: {
        A x(buf, 'x');
      }

      case true: {
        A h(buf, 'h');
        switch (n++) {
        case 0:
          break;
        case 1:
          continue;
        case 2:
          return;
        }
        break;
      }

      default:
        Indestructible indest;
      }

      A j = (A(buf, 'i'), A(buf, 'j'));
    }
  }

  constexpr bool check_abnormal_termination() {
    Buf buf = {};
    abnormal_termination(buf);
    return buf ==
      "abBc"
        "dfgh" /*break*/ "HGFijIJeED"
        "dfgh" /*continue*/ "HGFeED"
        "dfgh" /*return*/ "HGFD"
      "CA";
  }
  static_assert(check_abnormal_termination());
}

namespace std {
  struct type_info;
}

namespace TypeId {
  struct A {
    const std::type_info &ti = typeid(*this);
  };
  struct A2 : A {};
  static_assert(&A().ti == &typeid(A));
  static_assert(&typeid((A2())) == &typeid(A2));
  extern A2 extern_a2;
  static_assert(&typeid(extern_a2) == &typeid(A2));

  constexpr A2 a2;
  constexpr const A &a1 = a2;
  static_assert(&typeid(a1) == &typeid(A));

  struct B {
    virtual void f();
    const std::type_info &ti1 = typeid(*this);
  };
  struct B2 : B {
    const std::type_info &ti2 = typeid(*this);
  };
  static_assert(&B2().ti1 == &typeid(B));
  static_assert(&B2().ti2 == &typeid(B2));
  extern B2 extern_b2;
  static_assert(&typeid(extern_b2) == &typeid(B2));

  constexpr B2 b2;
  constexpr const B &b1 = b2;
  static_assert(&typeid(b1) == &typeid(B2));

  constexpr bool side_effects() {
    // Not polymorphic nor a glvalue.
    bool OK = true;
    (void)typeid(OK = false, A2()); // both-warning {{has no effect}}
    if (!OK) return false;

    // Not polymorphic.
    A2 a2;
    (void)typeid(OK = false, a2); // both-warning {{has no effect}}
    if (!OK) return false;

    // Not a glvalue.
    (void)typeid(OK = false, B2()); // both-warning {{has no effect}}
    if (!OK) return false;

    // Polymorphic glvalue: operand evaluated.
    OK = false;
    B2 b2;
    (void)typeid(OK = true, b2); // both-warning {{will be evaluated}}
    return OK;
  }
  static_assert(side_effects());
}

consteval int f(int i);
constexpr bool test(auto i) {
    return f(0) == 0;
}
consteval int f(int i) {
    return 2 * i;
}
static_assert(test(42));

namespace PureVirtual {
  struct Abstract {
    constexpr virtual void f() = 0; // both-note {{declared here}}
    constexpr Abstract() { do_it(); } // both-note {{in call to}}
    constexpr void do_it() { f(); } // both-note {{pure virtual function 'PureVirtual::Abstract::f' called}}
  };
  struct PureVirtualCall : Abstract { void f(); }; // both-note {{in call to 'Abstract}}
  constexpr PureVirtualCall pure_virtual_call; // both-error {{constant expression}} both-note {{in call to 'PureVirtualCall}}
}

namespace Dtor {
  constexpr bool pseudo(bool read, bool recreate) {
    using T = bool;
    bool b = false; // both-note {{lifetime has already ended}}
    // This evaluates the store to 'b'...
    (b = true).~T();
    // ... and ends the lifetime of the object.
    return (read
            ? b // both-note {{read of object outside its lifetime}}
            : true) +
           (recreate
            ? (std::construct(&b), true)
            : true);
  }
  static_assert(pseudo(false, false)); // both-error {{constant expression}} both-note {{in call}}
  static_assert(pseudo(true, false)); // both-error {{constant expression}} both-note {{in call}}
  static_assert(pseudo(false, true));
}

namespace GH150705 {
  struct A { };
  struct B : A { };
  struct C : A {
    constexpr virtual int foo() const { return 0; }
  };

  constexpr auto p = &C::foo;
  constexpr auto q = static_cast<int (A::*)() const>(p);
  constexpr B b;
  constexpr const A& a = b;
  constexpr auto x = (a.*q)(); // both-error {{constant expression}}
}

namespace DependentRequiresExpr {
  template <class T,
            bool = []() -> bool { // both-error {{not a constant expression}}
              if (requires { T::type; })
                return true;
              return false;
            }()>
  struct p {
    using type = void;
  };

  template <class T> using P = p<T>::type; // both-note {{while checking a default template argument}}
}