@@ -246,6 +246,25 @@ static void dtorRecord(Block *B, std::byte *Ptr, const Descriptor *D) {
246
246
destroyBase (B, Ptr, F.Desc , F.Offset );
247
247
}
248
248
249
+ // / Whether a record needs its descriptor dtor function called.
250
+ static bool needsRecordDtor (const Record *R) {
251
+ for (const auto &B : R->bases ()) {
252
+ if (B.Desc ->DtorFn )
253
+ return true ;
254
+ }
255
+
256
+ for (const auto &F : R->fields ()) {
257
+ if (F.Desc ->DtorFn )
258
+ return true ;
259
+ }
260
+
261
+ for (const auto &V : R->virtual_bases ()) {
262
+ if (V.Desc ->DtorFn )
263
+ return true ;
264
+ }
265
+ return false ;
266
+ }
267
+
249
268
static BlockCtorFn getCtorPrim (PrimType Type) {
250
269
// Floating types are special. They are primitives, but need their
251
270
// constructor called.
@@ -336,7 +355,7 @@ Descriptor::Descriptor(const DeclTy &D, const Type *SourceTy,
336
355
AllocSize(std::max<size_t >(alignof (void *), Size) + MDSize),
337
356
ElemDesc(Elem), IsConst(IsConst), IsMutable(IsMutable),
338
357
IsTemporary(IsTemporary), IsArray(true ), CtorFn(ctorArrayDesc),
339
- DtorFn(dtorArrayDesc) {
358
+ DtorFn(Elem->DtorFn ? dtorArrayDesc : nullptr ) {
340
359
assert (Source && " Missing source" );
341
360
}
342
361
@@ -347,7 +366,7 @@ Descriptor::Descriptor(const DeclTy &D, const Descriptor *Elem, MetadataSize MD,
347
366
Size(UnknownSizeMark), MDSize(MD.value_or(0 )),
348
367
AllocSize(MDSize + alignof (void *)), ElemDesc(Elem), IsConst(true ),
349
368
IsMutable(false ), IsTemporary(IsTemporary), IsArray(true ),
350
- CtorFn(ctorArrayDesc), DtorFn(dtorArrayDesc) {
369
+ CtorFn(ctorArrayDesc), DtorFn(Elem->DtorFn ? dtorArrayDesc : nullptr ) {
351
370
assert (Source && " Missing source" );
352
371
}
353
372
@@ -359,7 +378,7 @@ Descriptor::Descriptor(const DeclTy &D, const Record *R, MetadataSize MD,
359
378
Size(ElemSize), MDSize(MD.value_or(0 )), AllocSize(Size + MDSize),
360
379
ElemRecord(R), IsConst(IsConst), IsMutable(IsMutable),
361
380
IsTemporary(IsTemporary), IsVolatile(IsVolatile), CtorFn(ctorRecord),
362
- DtorFn(dtorRecord) {
381
+ DtorFn(needsRecordDtor(R) ? dtorRecord : nullptr ) {
363
382
assert (Source && " Missing source" );
364
383
}
365
384
0 commit comments