r194307 - Don't emit an internal destructor that is identical to an external one.

Rafael Espindola rafael.espindola at gmail.com
Fri Nov 8 17:57:21 PST 2013


Author: rafael
Date: Fri Nov  8 19:57:21 2013
New Revision: 194307

URL: http://llvm.org/viewvc/llvm-project?rev=194307&view=rev
Log:
Don't emit an internal destructor that is identical to an external one.

It is not safe to emit alias to undefined (not supported by ELF or COFF), but
it is safe to rauw when the alias would have been internal or linkonce_odr.

Modified:
    cfe/trunk/lib/CodeGen/CGCXX.cpp
    cfe/trunk/test/CodeGenCXX/ctor-dtor-alias.cpp

Modified: cfe/trunk/lib/CodeGen/CGCXX.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGCXX.cpp?rev=194307&r1=194306&r2=194307&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGCXX.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGCXX.cpp Fri Nov  8 19:57:21 2013
@@ -82,18 +82,12 @@ bool CodeGenModule::TryEmitBaseDestructo
   if (!UniqueBase)
     return true;
 
-  /// If we don't have a definition for the destructor yet, don't
-  /// emit.  We can't emit aliases to declarations; that's just not
-  /// how aliases work.
-  const CXXDestructorDecl *BaseD = UniqueBase->getDestructor();
-  if (!BaseD->isImplicit() && !BaseD->hasBody())
-    return true;
-
   // If the base is at a non-zero offset, give up.
   const ASTRecordLayout &ClassLayout = Context.getASTRecordLayout(Class);
   if (!ClassLayout.getBaseClassOffset(UniqueBase).isZero())
     return true;
 
+  const CXXDestructorDecl *BaseD = UniqueBase->getDestructor();
   return TryEmitDefinitionAsAlias(GlobalDecl(D, Dtor_Base),
                                   GlobalDecl(BaseD, Dtor_Base),
                                   false);
@@ -146,14 +140,20 @@ bool CodeGenModule::TryEmitDefinitionAsA
     return false;
   }
 
-  // Don't create an alias to a linker weak symbol unless we know we can do
-  // that in every TU. This avoids producing different COMDATs in different
-  // TUs.
-  if (llvm::GlobalValue::isWeakForLinker(TargetLinkage)) {
-    if (!InEveryTU)
+  if (!InEveryTU) {
+    /// If we don't have a definition for the destructor yet, don't
+    /// emit.  We can't emit aliases to declarations; that's just not
+    /// how aliases work.
+    if (Ref->isDeclaration())
       return true;
 
-    assert(Linkage == TargetLinkage);
+    // Don't create an alias to a linker weak symbol unless we know we can do
+    // that in every TU. This avoids producing different COMDATs in different
+    // TUs.
+    if (llvm::GlobalValue::isWeakForLinker(TargetLinkage)) {
+      assert(Linkage == TargetLinkage);
+      return true;
+    }
   }
 
   // Create the alias with no name.

Modified: cfe/trunk/test/CodeGenCXX/ctor-dtor-alias.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenCXX/ctor-dtor-alias.cpp?rev=194307&r1=194306&r2=194307&view=diff
==============================================================================
--- cfe/trunk/test/CodeGenCXX/ctor-dtor-alias.cpp (original)
+++ cfe/trunk/test/CodeGenCXX/ctor-dtor-alias.cpp Fri Nov  8 19:57:21 2013
@@ -73,3 +73,18 @@ namespace test5 {
   }
   B X;
 }
+
+namespace test6 {
+  // Test that we use ~A directly, even when ~A is not defined. The symbol for
+  // ~B would have been internal and still contain a reference to ~A.
+  struct A {
+    virtual ~A();
+  };
+  namespace {
+  struct B : public A {
+    ~B() {}
+  };
+  }
+  B X;
+  // CHECK-DAG: call i32 @__cxa_atexit({{.*}}@_ZN5test61AD2Ev
+}





More information about the cfe-commits mailing list