[cfe-commits] r96351 - in /cfe/trunk: lib/CodeGen/CGVtable.cpp test/CodeGenCXX/vtable-layout.cpp

Anders Carlsson andersca at mac.com
Tue Feb 16 08:02:57 PST 2010


Author: andersca
Date: Tue Feb 16 10:02:57 2010
New Revision: 96351

URL: http://llvm.org/viewvc/llvm-project?rev=96351&view=rev
Log:
Fix a bug where we would not emit secondary vtables for bases of a primary base.

Modified:
    cfe/trunk/lib/CodeGen/CGVtable.cpp
    cfe/trunk/test/CodeGenCXX/vtable-layout.cpp

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

==============================================================================
--- cfe/trunk/lib/CodeGen/CGVtable.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGVtable.cpp Tue Feb 16 10:02:57 2010
@@ -822,6 +822,10 @@
   /// LayoutVtable - Layout a vtable and all its secondary vtables.
   void LayoutVtable(BaseSubobject Base);
   
+  /// LayoutSecondaryVtables - Layout the secondary vtables for the given base
+  /// subobject.
+  void LayoutSecondaryVtables(BaseSubobject Base);
+  
 public:
   VtableBuilder(CGVtableInfo &VtableInfo, const CXXRecordDecl *MostDerivedClass)
     : VtableInfo(VtableInfo), MostDerivedClass(MostDerivedClass), 
@@ -1070,36 +1074,49 @@
     AddressPoints.insert(std::make_pair(PrimaryBase, AddressPoint));
   }
 
+  // Layout secondary vtables.
+  LayoutSecondaryVtables(Base);
+  
+  // FIXME: Emit vtables for virtual bases here.
+}
+
+void VtableBuilder::LayoutSecondaryVtables(BaseSubobject Base) {
+  // Itanium C++ ABI 2.5.2:
+  //   Following the primary virtual table of a derived class are secondary 
+  //   virtual tables for each of its proper base classes, except any primary
+  //   base(s) with which it shares its primary virtual table.
+
+  const CXXRecordDecl *RD = Base.getBase();
   const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
   const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
-
-  // Layout secondary vtables.
+  
   for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
        E = RD->bases_end(); I != E; ++I) {
+    // Ignore virtual bases, we'll emit them later.
+    if (I->isVirtual())
+      continue;
+    
     const CXXRecordDecl *BaseDecl = 
       cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
 
     // Ignore bases that don't have a vtable.
     if (!BaseDecl->isDynamicClass())
       continue;
-    
-    // Ignore the primary base.
-    if (BaseDecl == PrimaryBase)
-      continue;
 
-    // Ignore virtual bases, we'll emit them later.
-    if (I->isVirtual())
-      continue;
-    
     // Get the base offset of this base.
     uint64_t BaseOffset = Base.getBaseOffset() + 
       Layout.getBaseClassOffset(BaseDecl);
+    
+    // Don't emit a secondary vtable for a primary base. We might however want 
+    // to emit secondary vtables for other bases of this base.
+    if (BaseDecl == PrimaryBase) {
+      LayoutSecondaryVtables(BaseSubobject(BaseDecl, BaseOffset));
+      continue;
+    }
 
     // Layout this secondary vtable.
     LayoutVtable(BaseSubobject(BaseDecl, BaseOffset));
   }
-  
-  // FIXME: Emit vtables for virtual bases here.
 }
 
 /// dumpLayout - Dump the vtable layout.

Modified: cfe/trunk/test/CodeGenCXX/vtable-layout.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenCXX/vtable-layout.cpp?rev=96351&r1=96350&r2=96351&view=diff

==============================================================================
--- cfe/trunk/test/CodeGenCXX/vtable-layout.cpp (original)
+++ cfe/trunk/test/CodeGenCXX/vtable-layout.cpp Tue Feb 16 10:02:57 2010
@@ -365,3 +365,33 @@
 void B::f() { }
 
 }
+
+namespace Test10 {
+
+// Test for a bug where we would not emit secondary vtables for bases
+// of a primary base.
+struct A1 { virtual void a1(); };
+struct A2 { virtual void a2(); };
+
+// CHECK:     Vtable for 'Test10::C' (7 entries).
+// CHECK-NEXT:   0 | offset_to_top (0)
+// CHECK-NEXT:   1 | Test10::C RTTI
+// CHECK-NEXT:       -- (Test10::A1, 0) vtable address --
+// CHECK-NEXT:       -- (Test10::B, 0) vtable address --
+// CHECK-NEXT:       -- (Test10::C, 0) vtable address --
+// CHECK-NEXT:   2 | void Test10::A1::a1()
+// CHECK-NEXT:   3 | void Test10::C::f()
+// CHECK-NEXT:   4 | offset_to_top (-8)
+// CHECK-NEXT:   5 | Test10::C RTTI
+// CHECK-NEXT:       -- (Test10::A2, 8) vtable address --
+// CHECK-NEXT:   6 | void Test10::A2::a2()
+struct B : A1, A2 {
+  int b;
+};
+
+struct C : B {
+  virtual void f();
+};
+void C::f() { }
+
+}





More information about the cfe-commits mailing list