[cfe-commits] r79205 - in /cfe/trunk: lib/CodeGen/CGCXX.cpp lib/CodeGen/CGExprScalar.cpp lib/CodeGen/CodeGenFunction.h test/CodeGenCXX/delete.cpp

Anders Carlsson andersca at mac.com
Sun Aug 16 14:13:42 PDT 2009


Author: andersca
Date: Sun Aug 16 16:13:42 2009
New Revision: 79205

URL: http://llvm.org/viewvc/llvm-project?rev=79205&view=rev
Log:
Improve handling of delete expressions.

Added:
    cfe/trunk/test/CodeGenCXX/delete.cpp
Modified:
    cfe/trunk/lib/CodeGen/CGCXX.cpp
    cfe/trunk/lib/CodeGen/CGExprScalar.cpp
    cfe/trunk/lib/CodeGen/CodeGenFunction.h

Modified: cfe/trunk/lib/CodeGen/CGCXX.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGCXX.cpp?rev=79205&r1=79204&r2=79205&view=diff

==============================================================================
--- cfe/trunk/lib/CodeGen/CGCXX.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGCXX.cpp Sun Aug 16 16:13:42 2009
@@ -514,6 +514,63 @@
   return NewPtr;
 }
 
+void CodeGenFunction::EmitCXXDeleteExpr(const CXXDeleteExpr *E) {
+  if (E->isArrayForm()) {
+    ErrorUnsupported(E, "delete[] expression");
+    return;
+  };
+
+  QualType DeleteTy = 
+    E->getArgument()->getType()->getAs<PointerType>()->getPointeeType();
+  
+  llvm::Value *Ptr = EmitScalarExpr(E->getArgument());
+  
+  // Null check the pointer.
+  llvm::BasicBlock *DeleteNotNull = createBasicBlock("delete.notnull");
+  llvm::BasicBlock *DeleteEnd = createBasicBlock("delete.end");
+
+  llvm::Value *IsNull = 
+    Builder.CreateICmpEQ(Ptr, llvm::Constant::getNullValue(Ptr->getType()),
+                         "isnull");
+    
+  Builder.CreateCondBr(IsNull, DeleteEnd, DeleteNotNull);
+  EmitBlock(DeleteNotNull);
+    
+  // Call the destructor if necessary.
+  if (const RecordType *RT = DeleteTy->getAs<RecordType>()) {
+    if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
+      if (!RD->hasTrivialDestructor()) {
+        const CXXDestructorDecl *Dtor = RD->getDestructor(getContext());
+        if (Dtor->isVirtual()) {
+          ErrorUnsupported(E, "delete expression with virtual destructor");
+          return;
+        }
+        
+        EmitCXXDestructorCall(Dtor, Dtor_Complete, Ptr);
+      }
+    }
+  }
+  
+  // Call delete.
+  FunctionDecl *DeleteFD = E->getOperatorDelete();
+  const FunctionProtoType *DeleteFTy = 
+    DeleteFD->getType()->getAsFunctionProtoType();
+  
+  CallArgList DeleteArgs;
+
+  QualType ArgTy = DeleteFTy->getArgType(0);
+  llvm::Value *DeletePtr = Builder.CreateBitCast(Ptr, ConvertType(ArgTy));
+  DeleteArgs.push_back(std::make_pair(RValue::get(DeletePtr), ArgTy));
+  
+  // Emit the call to delete.
+  EmitCall(CGM.getTypes().getFunctionInfo(DeleteFTy->getResultType(), 
+                                          DeleteArgs),
+           CGM.GetAddrOfFunction(GlobalDecl(DeleteFD)),
+           DeleteArgs, DeleteFD);
+  
+  EmitBlock(DeleteEnd);
+}
+
 static bool canGenerateCXXstructor(const CXXRecordDecl *RD, 
                                    ASTContext &Context) {
   // The class has base classes - we don't support that right now.

Modified: cfe/trunk/lib/CodeGen/CGExprScalar.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGExprScalar.cpp?rev=79205&r1=79204&r2=79205&view=diff

==============================================================================
--- cfe/trunk/lib/CodeGen/CGExprScalar.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGExprScalar.cpp Sun Aug 16 16:13:42 2009
@@ -293,6 +293,10 @@
   Value *VisitCXXNewExpr(const CXXNewExpr *E) {
     return CGF.EmitCXXNewExpr(E);
   }
+  Value *VisitCXXDeleteExpr(const CXXDeleteExpr *E) {
+    CGF.EmitCXXDeleteExpr(E);
+    return 0;
+  }
       
   // Binary Operators.
   Value *EmitMul(const BinOpInfo &Ops) {

Modified: cfe/trunk/lib/CodeGen/CodeGenFunction.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenFunction.h?rev=79205&r1=79204&r2=79205&view=diff

==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenFunction.h (original)
+++ cfe/trunk/lib/CodeGen/CodeGenFunction.h Sun Aug 16 16:13:42 2009
@@ -607,6 +607,7 @@
   void PopCXXTemporary();
   
   llvm::Value *EmitCXXNewExpr(const CXXNewExpr *E);
+  void EmitCXXDeleteExpr(const CXXDeleteExpr *E);
   
   //===--------------------------------------------------------------------===//
   //                            Declaration Emission

Added: cfe/trunk/test/CodeGenCXX/delete.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenCXX/delete.cpp?rev=79205&view=auto

==============================================================================
--- cfe/trunk/test/CodeGenCXX/delete.cpp (added)
+++ cfe/trunk/test/CodeGenCXX/delete.cpp Sun Aug 16 16:13:42 2009
@@ -0,0 +1,25 @@
+// RUN: clang-cc %s -emit-llvm -o %t &&
+
+void t1(int *a) {
+  delete a;
+}
+
+struct S {
+  int a;
+};
+
+// POD types.
+void t3(S *s) {
+  delete s;
+}
+
+// Non-POD
+struct T {
+  ~T();
+  int a;
+};
+
+void t4(T *t) {
+  // RUN: grep "call void @_ZN1TD1Ev" %t | count 1
+  delete t;
+}





More information about the cfe-commits mailing list