Debian Codegraph @

clang/test/SemaCXX/cxx2c-trivially-relocatable.cpp

raw
1 // RUN: %clang_cc1 -std=c++2c -verify %s
2 // RUN: %clang_cc1 -triple aarch64-linux-gnu -fptrauth-intrinsics -fptrauth-calls -std=c++2c -verify %s
3
4 class Trivial {};
5 static_assert(__builtin_is_cpp_trivially_relocatable(Trivial));
6 struct NonRelocatable {
7 ~NonRelocatable();
8 };
9 static NonRelocatable NonRelocatable_g;
10
11 struct Incomplete; // expected-note {{forward declaration of 'Incomplete'}}
12 static_assert(__builtin_is_cpp_trivially_relocatable(Incomplete)); // expected-error {{incomplete type 'Incomplete' used in type trait expression}}
13 static_assert(__builtin_is_cpp_trivially_relocatable(int));
14 static_assert(__builtin_is_cpp_trivially_relocatable(void*));
15 static_assert(!__builtin_is_cpp_trivially_relocatable(int&));
16 static_assert(!__builtin_is_cpp_trivially_relocatable(Trivial&));
17 static_assert(__builtin_is_cpp_trivially_relocatable(const Trivial));
18 static_assert(__builtin_is_cpp_trivially_relocatable(Trivial[1]));
19 static_assert(__builtin_is_cpp_trivially_relocatable(Trivial[]));
20
21 struct WithConst {
22 const int i;
23 };
24 static_assert(!__builtin_is_cpp_trivially_relocatable(WithConst));
25
26 struct UserDtr {
27 ~UserDtr();
28 };
29
30 struct DefaultedDtr {
31 ~DefaultedDtr() = default;
32 };
33 struct UserMoveWithDefaultCopy {
34 UserMoveWithDefaultCopy(UserMoveWithDefaultCopy&&);
35 UserMoveWithDefaultCopy(const UserMoveWithDefaultCopy&) = default;
36 };
37
38 struct UserMove{
39 UserMove(UserMove&&);
40 };
41
42 struct UserMoveDefault{
43 UserMoveDefault(UserMoveDefault&&) = default;
44 };
45
46 struct UserMoveAssignDefault {
47 UserMoveAssignDefault(UserMoveAssignDefault&&) = default;
48 UserMoveAssignDefault& operator=(UserMoveAssignDefault&&) = default;
49 };
50
51 struct UserCopy{
52 UserCopy(const UserCopy&);
53 };
54
55 struct UserCopyDefault{
56 UserCopyDefault(const UserCopyDefault&) = default;
57 };
58
59
60 struct UserDeletedMove{
61 UserDeletedMove(UserDeletedMove&&) = delete;
62 UserDeletedMove(const UserDeletedMove&) = default;
63 };
64
65 static_assert(!__builtin_is_cpp_trivially_relocatable(UserDtr));
66 static_assert(__builtin_is_cpp_trivially_relocatable(DefaultedDtr));
67 static_assert(!__builtin_is_cpp_trivially_relocatable(UserMoveWithDefaultCopy));
68 static_assert(!__builtin_is_cpp_trivially_relocatable(UserMove));
69 static_assert(!__builtin_is_cpp_trivially_relocatable(UserCopy));
70 static_assert(!__builtin_is_cpp_trivially_relocatable(UserMoveDefault));
71 static_assert(__builtin_is_cpp_trivially_relocatable(UserMoveAssignDefault));
72 static_assert(__builtin_is_cpp_trivially_relocatable(UserCopyDefault));
73 static_assert(!__builtin_is_cpp_trivially_relocatable(UserDeletedMove));
74
75 struct DeletedMove {
76 DeletedMove(DeletedMove&&) = delete;
77 };
78 struct DeletedCopy {
79 DeletedCopy(const DeletedCopy&) = delete;
80 };
81 struct DeletedMoveAssign {
82 DeletedMoveAssign& operator=(DeletedMoveAssign&&) = delete;
83 };
84
85 struct DeletedDtr {
86 ~DeletedDtr() = delete;
87 };
88
89 static_assert(!__builtin_is_cpp_trivially_relocatable(DeletedMove));
90 static_assert(!__builtin_is_cpp_trivially_relocatable(DeletedCopy));
91 static_assert(!__builtin_is_cpp_trivially_relocatable(DeletedMoveAssign));
92 static_assert(!__builtin_is_cpp_trivially_relocatable(DeletedDtr));
93
94
95 template <typename T>
96 struct S {
97 T t;
98 };
99 static_assert(__builtin_is_cpp_trivially_relocatable(S<int>));
100 static_assert(__builtin_is_cpp_trivially_relocatable(S<volatile int>));
101 static_assert(!__builtin_is_cpp_trivially_relocatable(S<const int>));
102 static_assert(!__builtin_is_cpp_trivially_relocatable(S<const int&>));
103 static_assert(!__builtin_is_cpp_trivially_relocatable(S<int&>));
104 static_assert(__builtin_is_cpp_trivially_relocatable(S<int[2]>));
105 static_assert(!__builtin_is_cpp_trivially_relocatable(S<const int[2]>));
106 static_assert(__builtin_is_cpp_trivially_relocatable(S<int[]>));
107
108 namespace replaceable {
109
110 struct DeletedMove {
111 DeletedMove(DeletedMove&&) = delete;
112 };
113 struct DeletedCopy {
114 DeletedCopy(const DeletedCopy&) = delete;
115 };
116 struct DeletedMoveAssign {
117 DeletedMoveAssign& operator=(DeletedMoveAssign&&) = delete;
118 };
119
120 struct DefaultedMove {
121 DefaultedMove(DefaultedMove&&) = default;
122 DefaultedMove& operator=(DefaultedMove&&) = default;
123 };
124 struct DefaultedCopy {
125 DefaultedCopy(const DefaultedCopy&) = default;
126 DefaultedCopy(DefaultedCopy&&) = default;
127 DefaultedCopy& operator=(DefaultedCopy&&) = default;
128 };
129 struct DefaultedMoveAssign {
130 DefaultedMoveAssign(DefaultedMoveAssign&&) = default;
131 DefaultedMoveAssign& operator=(DefaultedMoveAssign&&) = default;
132 };
133
134 struct UserProvidedMove {
135 UserProvidedMove(UserProvidedMove&&){};
136 };
137 struct UserProvidedCopy {
138 UserProvidedCopy(const UserProvidedCopy&) {};
139 };
140 struct UserProvidedMoveAssign {
141 UserProvidedMoveAssign& operator=(const UserProvidedMoveAssign&){return *this;};
142 };
143
144 struct Empty{};
145
146 struct DeletedCopyTpl {
147 template <typename U>
148 DeletedCopyTpl(const U&) = delete;
149 };
150
151
152 template <typename T>
153 struct WithBase : T{};
154
155 template <typename T>
156 struct WithVBase : virtual T{};
157
158 struct WithVirtual {
159 virtual ~WithVirtual() = default;
160 WithVirtual(WithVirtual&&) = default;
161 WithVirtual& operator=(WithVirtual&&) = default;
162 };
163
164 int n = 4; // expected-note {{declared here}}
165 static_assert(!__builtin_is_cpp_trivially_relocatable(int[n]));
166 // expected-warning@-1 {{variable length arrays in C++ are a Clang extension}}
167 // expected-note@-2 {{read of non-const variable 'n' is not allowed in a constant expression}}
168
169
170
171 struct Copyable1Explicit {
172 Copyable1Explicit(Copyable1Explicit const &) = default;
173 };
174
175 struct Copyable1 {
176 Copyable1(Copyable1 const &) = default;
177 };
178
179
180 struct CopyAssign1Explicit {
181 CopyAssign1Explicit & operator=(const CopyAssign1Explicit&) = default;
182 };
183
184 struct CopyAssign1 {
185 CopyAssign1 & operator=(CopyAssign1 const &) = default;
186 };
187
188 struct UserDeleted1 {
189 UserDeleted1(const UserDeleted1&) = delete;
190 };
191 static_assert(!__builtin_is_cpp_trivially_relocatable(UserDeleted1));
192
193 struct UserDeleted2 {
194 UserDeleted2(UserDeleted2&&) = delete;
195 };
196 static_assert(!__builtin_is_cpp_trivially_relocatable(UserDeleted2));
197
198
199 struct UserDeleted3 {
200 UserDeleted3 operator=(UserDeleted3);
201 };
202 static_assert(!__builtin_is_cpp_trivially_relocatable(UserDeleted3));
203
204 struct UserDeleted4 {
205 UserDeleted4 operator=(UserDeleted4&&);
206 };
207 static_assert(!__builtin_is_cpp_trivially_relocatable(UserDeleted4));
208
209 }
210
211
212 void test__builtin_trivially_relocate() {
213 struct S{ ~S();};
214 struct R {};
215 __builtin_trivially_relocate(); //expected-error {{too few arguments to function call, expected 3, have 0}}
216 __builtin_trivially_relocate(0, 0, 0, 0); //expected-error {{too many arguments to function call, expected 3, have 4}}
217 __builtin_trivially_relocate(0, 0, 0); //expected-error {{argument to '__builtin_trivially_relocate' must be a pointer}}
218 __builtin_trivially_relocate((const int*)0, 0, 0); //expected-error {{argument to '__builtin_trivially_relocate' must be non-const}}
219 __builtin_trivially_relocate((S*)0, 0, 0); //expected-error {{argument to '__builtin_trivially_relocate' must be relocatable}}
220 __builtin_trivially_relocate((int*)0, 0, 0); //expected-error {{first and second arguments to '__builtin_trivially_relocate' must be of the same type}}
221
222 __builtin_trivially_relocate((int*)0, (int*)0, (int*)0); // expected-error-re {{cannot initialize a value of type '__size_t' (aka '{{.*}}') with an rvalue of type 'int *'}}
223 __builtin_trivially_relocate((int*)0, (int*)0, 0);
224 __builtin_trivially_relocate((R*)0, (R*)0, 0);
225 }
226
227 void test__builtin_trivially_relocate(auto&& src, auto&&dest, auto size) {
228 __builtin_trivially_relocate(src, dest, size); // #reloc1
229 }
230
231 void do_test__builtin_trivially_relocate() {
232 struct S{ ~S();};
233 struct R {};
234 test__builtin_trivially_relocate((R*)0, (R*)0, 0);
235 test__builtin_trivially_relocate((S*)0, (S*)0, 0);
236 // expected-note@-1 {{'test__builtin_trivially_relocate<S *, S *, int>' requested here}}
237 // expected-error@#reloc1 {{first argument to '__builtin_trivially_relocate' must be relocatable}}
238 }
239
240
241 namespace GH143599 {
242 struct A { ~A (); };
243 A::~A () = default;
244
245 static_assert (!__builtin_is_cpp_trivially_relocatable(A));
246
247 struct B { B(const B&); };
248 B::B (const B&) = default;
249
250 static_assert (!__builtin_is_cpp_trivially_relocatable(B));
251
252 struct C { C& operator=(const C&); };
253 C& C::operator=(const C&) = default;
254
255 static_assert (!__builtin_is_cpp_trivially_relocatable(C));
256 }