File: type-aware-new-constexpr.cpp

package info (click to toggle)
llvm-toolchain-21 1%3A21.1.6-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,245,028 kB
  • sloc: cpp: 7,619,726; ansic: 1,434,018; asm: 1,058,748; python: 252,740; f90: 94,671; objc: 70,685; lisp: 42,813; pascal: 18,401; sh: 8,601; ml: 5,111; perl: 4,720; makefile: 3,675; awk: 3,523; javascript: 2,409; xml: 892; fortran: 770
file content (127 lines) | stat: -rw-r--r-- 5,549 bytes parent folder | download | duplicates (3)
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
// RUN: %clang_cc1 -fsyntax-only -verify %s -std=c++26 -Wno-ext-cxx-type-aware-allocators -fexceptions    -fsized-deallocation    -faligned-allocation
// RUN: %clang_cc1 -fsyntax-only -verify %s -std=c++26 -Wno-ext-cxx-type-aware-allocators -fexceptions -fno-sized-deallocation    -faligned-allocation
// RUN: %clang_cc1 -fsyntax-only -verify %s -std=c++26 -Wno-ext-cxx-type-aware-allocators -fexceptions    -fsized-deallocation -fno-aligned-allocation
// RUN: %clang_cc1 -fsyntax-only -verify %s -std=c++26 -Wno-ext-cxx-type-aware-allocators -fexceptions -fno-sized-deallocation -fno-aligned-allocation
// RUN: %clang_cc1 -fsyntax-only -verify %s -std=c++26 -Wno-ext-cxx-type-aware-allocators -fexceptions    -fsized-deallocation    -faligned-allocation -fexperimental-new-constant-interpreter
// RUN: %clang_cc1 -fsyntax-only -verify %s -std=c++26 -Wno-ext-cxx-type-aware-allocators -fexceptions -fno-sized-deallocation    -faligned-allocation -fexperimental-new-constant-interpreter
// RUN: %clang_cc1 -fsyntax-only -verify %s -std=c++26 -Wno-ext-cxx-type-aware-allocators -fexceptions    -fsized-deallocation -fno-aligned-allocation -fexperimental-new-constant-interpreter
// RUN: %clang_cc1 -fsyntax-only -verify %s -std=c++26 -Wno-ext-cxx-type-aware-allocators -fexceptions -fno-sized-deallocation -fno-aligned-allocation -fexperimental-new-constant-interpreter


namespace std {
  template <class T> struct type_identity {};
  enum class align_val_t : __SIZE_TYPE__ {};
  struct destroying_delete_t { explicit destroying_delete_t() = default; };
}

using size_t = __SIZE_TYPE__;

struct S1 {
  constexpr explicit S1() : i(5) {  }
  const int i;
};

void *operator new(std::type_identity<S1>, size_t sz, std::align_val_t); // #1
void operator delete(std::type_identity<S1>, void* ptr, size_t sz, std::align_val_t); // #2

constexpr int ensure_consteval_skips_typed_allocators() {
  // Verify we dont resolve typed allocators in const contexts
  auto * s = new S1();
  auto result = s->i;
  delete s;
  return result;
};

struct S2 {
  constexpr explicit S2() : i(5) {  }
  const int i;
};

void *operator new(std::type_identity<S2>, size_t sz, std::align_val_t) = delete; // #3
void operator delete(std::type_identity<S2>, void* ptr, size_t sz, std::align_val_t) = delete; // #4

constexpr int ensure_constexpr_retains_types_at_runtime() {
  // Verify we dont resolve typed allocators in const contexts
  S2 *s = new S2();
  // expected-error@-1 {{call to deleted function 'operator new'}}
  // expected-note@#1 {{candidate function not viable: no known conversion from 'type_identity<S2>' to 'type_identity<S1>' for 1st argument}}
  // expected-note@#3 {{candidate function has been explicitly deleted}}
  auto result = s->i;
  delete s;
  // expected-error@-1 {{attempt to use a deleted function}}
  // expected-note@#4 {{'operator delete' has been explicitly marked deleted here}}
  return result;
};


struct S3 {
  constexpr explicit S3() : i(5) {  }
  const int i;
  template <typename T> void* operator new(std::type_identity<T>, size_t sz, std::align_val_t) = delete; // #5
  template <typename T> void operator delete(std::type_identity<T>, void *, size_t sz, std::align_val_t) = delete; // #6
};

template <typename T> void* operator new(std::type_identity<T>, size_t sz, std::align_val_t) = delete; // #7
template <typename T> void operator delete(std::type_identity<T>, void *, size_t sz, std::align_val_t) = delete; // #8

constexpr int constexpr_vs_inclass_operators() {
  S3 *s;
  if consteval {
    s = ::new S3();
    // expected-error@-1 {{call to deleted function 'operator new'}}
    // expected-note@#1 {{candidate function not viable: no known conversion from 'type_identity<S3>' to 'type_identity<S1>' for 1st argument}}
    // expected-note@#3 {{candidate function not viable: no known conversion from 'type_identity<S3>' to 'type_identity<S2>' for 1st argument}}
    // expected-note@#7 {{candidate function [with T = S3] has been explicitly deleted}}
  } else {
    s = new S3();
    // expected-error@-1 {{call to deleted function 'operator new'}}
    // expected-note@#5 {{candidate function [with T = S3] has been explicitly deleted}}
  }
  auto result = s->i;
  if consteval {
    ::delete s;
    // expected-error@-1 {{attempt to use a deleted function}}
    // expected-note@#8 {{'operator delete<S3>' has been explicitly marked deleted here}}
  } else {
    delete s;
    // expected-error@-1 {{attempt to use a deleted function}}
    // expected-note@#6 {{'operator delete<S3>' has been explicitly marked deleted here}}
  }
  return result;
};

// Test a variety of valid constant evaluation paths
struct S4 {
  int i = 1;
  constexpr S4() __attribute__((noinline)) {}
};

void* operator new(std::type_identity<S4>, size_t sz, std::align_val_t);
void operator delete(std::type_identity<S4>, void *, size_t sz, std::align_val_t);

constexpr int do_dynamic_alloc(int n) {
  S4* s = new S4;
  int result = n * s->i;
  delete s;
  return result;
}

template <int N> struct Tag {
};

static constexpr int force_do_dynamic_alloc = do_dynamic_alloc(5);

constexpr int test_consteval_calling_constexpr(int i) {
  if consteval {
    return do_dynamic_alloc(2 * i);
  }
  return do_dynamic_alloc(3 * i);
}

int test_consteval(int n, Tag<test_consteval_calling_constexpr(2)>, Tag<do_dynamic_alloc(3)>) {
  static const int t1 = test_consteval_calling_constexpr(4);
  static const int t2 = do_dynamic_alloc(5);
  int t3 = test_consteval_calling_constexpr(6);
  int t4 = do_dynamic_alloc(7);
  return t1 * t2 * t3 * t4;
}