[cfe-commits] r134942 - in /cfe/trunk/lib/CodeGen: CGCleanup.cpp CGException.cpp CGObjC.cpp CGTemporaries.cpp CodeGenFunction.h

John McCall rjmccall at apple.com
Mon Jul 11 17:15:30 PDT 2011


Author: rjmccall
Date: Mon Jul 11 19:15:30 2011
New Revision: 134942

URL: http://llvm.org/viewvc/llvm-project?rev=134942&view=rev
Log:
Do full-expression cleanups in a much more sensible way that still lets
people write useful cleanup classes.


Modified:
    cfe/trunk/lib/CodeGen/CGCleanup.cpp
    cfe/trunk/lib/CodeGen/CGException.cpp
    cfe/trunk/lib/CodeGen/CGObjC.cpp
    cfe/trunk/lib/CodeGen/CGTemporaries.cpp
    cfe/trunk/lib/CodeGen/CodeGenFunction.h

Modified: cfe/trunk/lib/CodeGen/CGCleanup.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGCleanup.cpp?rev=134942&r1=134941&r2=134942&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGCleanup.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGCleanup.cpp Mon Jul 11 19:15:30 2011
@@ -256,9 +256,7 @@
   if (cleanup.isEHCleanup()) cleanup.setTestFlagInEHCleanup();
 }
 
-EHScopeStack::Cleanup::~Cleanup() {
-  llvm_unreachable("Cleanup is indestructable");
-}
+void EHScopeStack::Cleanup::anchor() {}
 
 /// All the branch fixups on the EH stack have propagated out past the
 /// outermost normal cleanup; resolve them all by adding cases to the

Modified: cfe/trunk/lib/CodeGen/CGException.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGException.cpp?rev=134942&r1=134941&r2=134942&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGException.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGException.cpp Mon Jul 11 19:15:30 2011
@@ -323,9 +323,10 @@
 namespace {
   /// A cleanup to free the exception object if its initialization
   /// throws.
-  struct FreeException {
-    static void Emit(CodeGenFunction &CGF, bool forEH,
-                     llvm::Value *exn) {
+  struct FreeException : EHScopeStack::Cleanup {
+    llvm::Value *exn;
+    FreeException(llvm::Value *exn) : exn(exn) {}
+    void Emit(CodeGenFunction &CGF, bool forEH) {
       CGF.Builder.CreateCall(getFreeExceptionFn(CGF), exn)
         ->setDoesNotThrow();
     }

Modified: cfe/trunk/lib/CodeGen/CGObjC.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGObjC.cpp?rev=134942&r1=134941&r2=134942&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGObjC.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGObjC.cpp Mon Jul 11 19:15:30 2011
@@ -1902,23 +1902,6 @@
     CallReleaseForObject(QualType type, llvm::Value *addr,
                          CodeGenFunction::Destroyer *destroyer)
       : ObjCReleasingCleanup(type, addr, destroyer) {}
-
-    using ObjCReleasingCleanup::Emit;
-    static void Emit(CodeGenFunction &CGF, bool IsForEH,
-                     QualType type, llvm::Value *addr,
-                     CodeGenFunction::Destroyer *destroyer) {
-      // EHScopeStack::Cleanup objects can never have their destructors called,
-      // so use placement new to construct our temporary object.
-      union {
-        void* align;
-        char data[sizeof(CallReleaseForObject)];
-      };
-      
-      CallReleaseForObject *Object
-        = new (&align) CallReleaseForObject(type, addr, destroyer);
-      Object->Emit(CGF, IsForEH);
-      (void)data[0];
-    }
   };
 
   /// A cleanup that calls @objc_storeStrong(nil) on all the objects to
@@ -1965,22 +1948,6 @@
   struct CallWeakReleaseForObject : ObjCReleasingCleanup {
     CallWeakReleaseForObject(QualType type, llvm::Value *addr)
       : ObjCReleasingCleanup(type, addr, CodeGenFunction::destroyARCWeak) {}
-
-    using ObjCReleasingCleanup::Emit;
-    static void Emit(CodeGenFunction &CGF, bool IsForEH,
-                     QualType type, llvm::Value *addr) {
-      // EHScopeStack::Cleanup objects can never have their destructors called,
-      // so use placement new to construct our temporary object.
-      union {
-        void* align;
-        char data[sizeof(CallWeakReleaseForObject)];
-      };
-      
-      CallWeakReleaseForObject *Object
-        = new (&align) CallWeakReleaseForObject(type, addr);
-      Object->Emit(CGF, IsForEH);
-      (void)data[0];
-    }
   };
 
   

Modified: cfe/trunk/lib/CodeGen/CGTemporaries.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGTemporaries.cpp?rev=134942&r1=134941&r2=134942&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGTemporaries.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGTemporaries.cpp Mon Jul 11 19:15:30 2011
@@ -16,9 +16,12 @@
 using namespace CodeGen;
 
 namespace {
-  struct DestroyTemporary {
-    static void Emit(CodeGenFunction &CGF, bool forEH,
-                     const CXXDestructorDecl *dtor, llvm::Value *addr) {
+  struct DestroyTemporary : EHScopeStack::Cleanup {
+    const CXXDestructorDecl *dtor;
+    llvm::Value *addr;
+    DestroyTemporary(const CXXDestructorDecl *dtor, llvm::Value *addr)
+      : dtor(dtor), addr(addr) {}
+    void Emit(CodeGenFunction &CGF, bool forEH) {
       CGF.EmitCXXDestructorCall(dtor, Dtor_Complete, /*ForVirtualBase=*/false,
                                 addr);
     }

Modified: cfe/trunk/lib/CodeGen/CodeGenFunction.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenFunction.h?rev=134942&r1=134941&r2=134942&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenFunction.h (original)
+++ cfe/trunk/lib/CodeGen/CodeGenFunction.h Mon Jul 11 19:15:30 2011
@@ -181,15 +181,13 @@
   /// Cleanup implementations should generally be declared in an
   /// anonymous namespace.
   class Cleanup {
+    // Anchor the construction vtable.
+    virtual void anchor();
   public:
-    // Anchor the construction vtable.  We use the destructor because
-    // gcc gives an obnoxious warning if there are virtual methods
-    // with an accessible non-virtual destructor.  Unfortunately,
-    // declaring this destructor makes it non-trivial, but there
-    // doesn't seem to be any other way around this warning.
-    //
-    // This destructor will never be called.
-    virtual ~Cleanup();
+    // Provide a virtual destructor to suppress a very common warning
+    // that unfortunately cannot be suppressed without this.  Cleanups
+    // should not rely on this destructor ever being called.
+    virtual ~Cleanup() {}
 
     /// Emit the cleanup.  For normal cleanups, this is run in the
     /// same EH context as when the cleanup was pushed, i.e. the
@@ -201,38 +199,6 @@
     virtual void Emit(CodeGenFunction &CGF, bool IsForEHCleanup) = 0;
   };
 
-  /// UnconditionalCleanupN stores its N parameters and just passes
-  /// them to the real cleanup function.
-  template <class T, class A0>
-  class UnconditionalCleanup1 : public Cleanup {
-    A0 a0;
-  public:
-    UnconditionalCleanup1(A0 a0) : a0(a0) {}
-    void Emit(CodeGenFunction &CGF, bool IsForEHCleanup) {
-      T::Emit(CGF, IsForEHCleanup, a0);
-    }
-  };
-
-  template <class T, class A0, class A1>
-  class UnconditionalCleanup2 : public Cleanup {
-    A0 a0; A1 a1;
-  public:
-    UnconditionalCleanup2(A0 a0, A1 a1) : a0(a0), a1(a1) {}
-    void Emit(CodeGenFunction &CGF, bool IsForEHCleanup) {
-      T::Emit(CGF, IsForEHCleanup, a0, a1);
-    }
-  };
-
-  template <class T, class A0, class A1, class A2>
-  class UnconditionalCleanup3 : public Cleanup {
-    A0 a0; A1 a1; A2 a2;
-  public:
-    UnconditionalCleanup3(A0 a0, A1 a1, A2 a2) : a0(a0), a1(a1), a2(a2) {}
-    void Emit(CodeGenFunction &CGF, bool IsForEHCleanup) {
-      T::Emit(CGF, IsForEHCleanup, a0, a1, a2);
-    }
-  };
-
   /// ConditionalCleanupN stores the saved form of its N parameters,
   /// then restores them and performs the cleanup.
   template <class T, class A0>
@@ -242,7 +208,7 @@
 
     void Emit(CodeGenFunction &CGF, bool IsForEHCleanup) {
       A0 a0 = DominatingValue<A0>::restore(CGF, a0_saved);
-      T::Emit(CGF, IsForEHCleanup, a0);
+      T(a0).Emit(CGF, IsForEHCleanup);
     }
 
   public:
@@ -260,7 +226,7 @@
     void Emit(CodeGenFunction &CGF, bool IsForEHCleanup) {
       A0 a0 = DominatingValue<A0>::restore(CGF, a0_saved);
       A1 a1 = DominatingValue<A1>::restore(CGF, a1_saved);
-      T::Emit(CGF, IsForEHCleanup, a0, a1);
+      T(a0, a1).Emit(CGF, IsForEHCleanup);
     }
 
   public:
@@ -281,12 +247,12 @@
       A0 a0 = DominatingValue<A0>::restore(CGF, a0_saved);
       A1 a1 = DominatingValue<A1>::restore(CGF, a1_saved);
       A2 a2 = DominatingValue<A2>::restore(CGF, a2_saved);
-      T::Emit(CGF, IsForEHCleanup, a0, a1, a2);
+      T(a0, a1, a2).Emit(CGF, IsForEHCleanup);
     }
     
   public:
     ConditionalCleanup3(A0_saved a0, A1_saved a1, A2_saved a2)
-    : a0_saved(a0), a1_saved(a1), a2_saved(a2) {}
+      : a0_saved(a0), a1_saved(a1), a2_saved(a2) {}
   };
 
 private:
@@ -696,10 +662,8 @@
   void pushFullExprCleanup(CleanupKind kind, A0 a0) {
     // If we're not in a conditional branch, or if none of the
     // arguments requires saving, then use the unconditional cleanup.
-    if (!isInConditionalBranch()) {
-      typedef EHScopeStack::UnconditionalCleanup1<T, A0> CleanupType;
-      return EHStack.pushCleanup<CleanupType>(kind, a0);
-    }
+    if (!isInConditionalBranch())
+      return EHStack.pushCleanup<T>(kind, a0);
 
     typename DominatingValue<A0>::saved_type a0_saved = saveValueInCond(a0);
 
@@ -715,10 +679,8 @@
   void pushFullExprCleanup(CleanupKind kind, A0 a0, A1 a1) {
     // If we're not in a conditional branch, or if none of the
     // arguments requires saving, then use the unconditional cleanup.
-    if (!isInConditionalBranch()) {
-      typedef EHScopeStack::UnconditionalCleanup2<T, A0, A1> CleanupType;
-      return EHStack.pushCleanup<CleanupType>(kind, a0, a1);
-    }
+    if (!isInConditionalBranch())
+      return EHStack.pushCleanup<T>(kind, a0, a1);
 
     typename DominatingValue<A0>::saved_type a0_saved = saveValueInCond(a0);
     typename DominatingValue<A1>::saved_type a1_saved = saveValueInCond(a1);
@@ -736,8 +698,7 @@
     // If we're not in a conditional branch, or if none of the
     // arguments requires saving, then use the unconditional cleanup.
     if (!isInConditionalBranch()) {
-      typedef EHScopeStack::UnconditionalCleanup3<T, A0, A1, A2> CleanupType;
-      return EHStack.pushCleanup<CleanupType>(kind, a0, a1, a2);
+      return EHStack.pushCleanup<T>(kind, a0, a1, a2);
     }
     
     typename DominatingValue<A0>::saved_type a0_saved = saveValueInCond(a0);





More information about the cfe-commits mailing list