Skip to content

Commit

Permalink
Pass 3 to destructor for delete[]
Browse files Browse the repository at this point in the history
  • Loading branch information
Fznamznon committed Jan 7, 2025
1 parent 8cac3a7 commit 2f0bba9
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 17 deletions.
7 changes: 4 additions & 3 deletions clang/lib/CodeGen/CGCXXABI.h
Original file line number Diff line number Diff line change
Expand Up @@ -251,9 +251,10 @@ class CGCXXABI {

public:
virtual void emitVirtualObjectDelete(CodeGenFunction &CGF,
const CXXDeleteExpr *DE,
Address Ptr, QualType ElementType,
const CXXDestructorDecl *Dtor) = 0;
const CXXDeleteExpr *DE, Address Ptr,
QualType ElementType,
const CXXDestructorDecl *Dtor,
bool VectorDeleting) = 0;
virtual void emitRethrow(CodeGenFunction &CGF, bool isNoReturn) = 0;
virtual void emitThrow(CodeGenFunction &CGF, const CXXThrowExpr *E) = 0;
virtual llvm::GlobalVariable *getThrowInfo(QualType T) { return nullptr; }
Expand Down
21 changes: 16 additions & 5 deletions clang/lib/CodeGen/CGExprCXX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1903,7 +1903,7 @@ static void EmitDestroyingObjectDelete(CodeGenFunction &CGF,
auto *Dtor = ElementType->getAsCXXRecordDecl()->getDestructor();
if (Dtor && Dtor->isVirtual())
CGF.CGM.getCXXABI().emitVirtualObjectDelete(CGF, DE, Ptr, ElementType,
Dtor);
Dtor, false);
else
CGF.EmitDeleteCall(DE->getOperatorDelete(), Ptr.emitRawPointer(CGF),
ElementType);
Expand All @@ -1916,7 +1916,8 @@ static bool EmitObjectDelete(CodeGenFunction &CGF,
const CXXDeleteExpr *DE,
Address Ptr,
QualType ElementType,
llvm::BasicBlock *UnconditionalDeleteBlock) {
llvm::BasicBlock *UnconditionalDeleteBlock,
bool VectorDeleting) {
// C++11 [expr.delete]p3:
// If the static type of the object to be deleted is different from its
// dynamic type, the static type shall be a base class of the dynamic type
Expand Down Expand Up @@ -1961,7 +1962,7 @@ static bool EmitObjectDelete(CodeGenFunction &CGF,
}
if (UseVirtualCall) {
CGF.CGM.getCXXABI().emitVirtualObjectDelete(CGF, DE, Ptr, ElementType,
Dtor);
Dtor, VectorDeleting);
return false;
}
}
Expand Down Expand Up @@ -2130,12 +2131,22 @@ void CodeGenFunction::EmitCXXDeleteExpr(const CXXDeleteExpr *E) {
}

assert(ConvertTypeForMem(DeleteTy) == Ptr.getElementType());
bool RequiresVectorDestructor = false;
// FIXME check that ABI requires to call "vector" destructors or check MSVC?
if (const RecordType *RT = DeleteTy->getAs<RecordType>()) {
CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
if (RD->hasDefinition() && !RD->hasTrivialDestructor()) {
// FIXME: check devirtualization?
const CXXDestructorDecl *Dtor = RD->getDestructor();
RequiresVectorDestructor = Dtor->isVirtual();
}
}

if (E->isArrayForm()) {
if (E->isArrayForm() && !RequiresVectorDestructor) {
EmitArrayDelete(*this, E, Ptr, DeleteTy);
EmitBlock(DeleteEnd);
} else {
if (!EmitObjectDelete(*this, E, Ptr, DeleteTy, DeleteEnd))
if (!EmitObjectDelete(*this, E, Ptr, DeleteTy, DeleteEnd, E->isArrayForm()))
EmitBlock(DeleteEnd);
}
}
Expand Down
6 changes: 4 additions & 2 deletions clang/lib/CodeGen/ItaniumCXXABI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,8 @@ class ItaniumCXXABI : public CodeGen::CGCXXABI {

void emitVirtualObjectDelete(CodeGenFunction &CGF, const CXXDeleteExpr *DE,
Address Ptr, QualType ElementType,
const CXXDestructorDecl *Dtor) override;
const CXXDestructorDecl *Dtor,
bool VectorDeleting = false) override;

void emitRethrow(CodeGenFunction &CGF, bool isNoReturn) override;
void emitThrow(CodeGenFunction &CGF, const CXXThrowExpr *E) override;
Expand Down Expand Up @@ -1368,7 +1369,8 @@ void ItaniumCXXABI::emitVirtualObjectDelete(CodeGenFunction &CGF,
const CXXDeleteExpr *DE,
Address Ptr,
QualType ElementType,
const CXXDestructorDecl *Dtor) {
const CXXDestructorDecl *Dtor,
bool VectorDeleting) {
bool UseGlobalDelete = DE->isGlobalDelete();
if (UseGlobalDelete) {
// Derive the complete-object pointer, which is what we need
Expand Down
17 changes: 10 additions & 7 deletions clang/lib/CodeGen/MicrosoftCXXABI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ class MicrosoftCXXABI : public CGCXXABI {

void emitVirtualObjectDelete(CodeGenFunction &CGF, const CXXDeleteExpr *DE,
Address Ptr, QualType ElementType,
const CXXDestructorDecl *Dtor) override;
const CXXDestructorDecl *Dtor,
bool VectorDeleting) override;

void emitRethrow(CodeGenFunction &CGF, bool isNoReturn) override;
void emitThrow(CodeGenFunction &CGF, const CXXThrowExpr *E) override;
Expand Down Expand Up @@ -888,14 +889,15 @@ MicrosoftCXXABI::getRecordArgABI(const CXXRecordDecl *RD) const {

void MicrosoftCXXABI::emitVirtualObjectDelete(CodeGenFunction &CGF,
const CXXDeleteExpr *DE,
Address Ptr,
QualType ElementType,
const CXXDestructorDecl *Dtor) {
Address Ptr, QualType ElementType,
const CXXDestructorDecl *Dtor,
bool VectorDeleting) {
// FIXME: Provide a source location here even though there's no
// CXXMemberCallExpr for dtor call.
bool UseGlobalDelete = DE->isGlobalDelete();
// FIXME check that vector deletion is actually required.
CXXDtorType DtorType = UseGlobalDelete ? Dtor_Complete : Dtor_VectorDeleting;
CXXDtorType DtorType = UseGlobalDelete ? Dtor_Complete
: (VectorDeleting) ? Dtor_VectorDeleting
: Dtor_Deleting;
llvm::Value *MDThis = EmitVirtualDestructorCall(CGF, Dtor, DtorType, Ptr, DE,
/*CallOrInvoke=*/nullptr);
if (UseGlobalDelete)
Expand Down Expand Up @@ -2005,7 +2007,8 @@ llvm::Value *MicrosoftCXXABI::EmitVirtualDestructorCall(
auto *D = E.dyn_cast<const CXXDeleteExpr *>();
assert((CE != nullptr) ^ (D != nullptr));
assert(CE == nullptr || CE->arg_begin() == CE->arg_end());
assert(DtorType == Dtor_VectorDeleting || DtorType == Dtor_Complete);
assert(DtorType == Dtor_VectorDeleting || DtorType == Dtor_Complete ||
DtorType == Dtor_Deleting);

// We have only one destructor in the vftable but can get both behaviors
// by passing an implicit int parameter.
Expand Down

0 comments on commit 2f0bba9

Please sign in to comment.