Debian Codegraph @

clang/test/AST/ByteCode/libcxx/allocate-arrays.cpp

raw
1 // RUN: %clang_cc1 -std=c++2c -fexperimental-new-constant-interpreter -verify=expected,both %s
2 // RUN: %clang_cc1 -std=c++2c -verify=ref,both %s
3
4
5 /// This example used to cause an invalid read because allocating
6 /// an array needs to return a pointer to the first element,
7 /// not to the array.
8
9 namespace std {
10 using size_t = decltype(sizeof(0));
11
12 template <class _Tp>
13 class allocator {
14 public:
15 typedef size_t size_type;
16 typedef _Tp value_type;
17 constexpr _Tp *allocate(size_t __n) {
18 return static_cast<_Tp *>(::operator new(__n * sizeof(_Tp)));
19 }
20 };
21 }
22
23 void *operator new(std::size_t, void *p) { return p; }
24 void* operator new[] (std::size_t, void* p) {return p;}
25
26 namespace std {
27 template <class _Ep>
28 class initializer_list {
29 const _Ep *__begin_;
30 __SIZE_TYPE__ __size_;
31
32 public:
33 typedef _Ep value_type;
34 typedef const _Ep &reference;
35 constexpr __SIZE_TYPE__ size() const noexcept { return __size_; }
36 constexpr const _Ep *begin() const noexcept { return __begin_; }
37 constexpr const _Ep *end() const noexcept { return __begin_ + __size_; }
38 };
39 }
40
41 template<typename T>
42 class vector {
43 public:
44 constexpr vector(std::initializer_list<T> Ts) {
45 A = B = std::allocator<T>{}.allocate(Ts.size()); // both-note {{heap allocation performed here}}
46
47 new (A) T(*Ts.begin());
48 }
49 private:
50 T *A = nullptr;
51 T *B = nullptr;
52 };
53
54 constexpr vector<vector<int>> ints = {{3}, {4}}; // both-error {{must be initialized by a constant expression}} \
55 // both-note {{pointer to}}