r195158 - Microsoft Record Layout: zero sized base after base with vbtbl fix
Warren Hunt
whunt at google.com
Tue Nov 19 14:11:10 PST 2013
Author: whunt
Date: Tue Nov 19 16:11:09 2013
New Revision: 195158
URL: http://llvm.org/viewvc/llvm-project?rev=195158&view=rev
Log:
Microsoft Record Layout: zero sized base after base with vbtbl fix
Microsoft adds an extra byte of padding before laying out zero sized
non-virtual bases if the non-virtual base before it contains a vbptr.
This patch adds the same behavior to clang.
Differential Revision: http://llvm-reviews.chandlerc.com/D2106
Added:
cfe/trunk/test/Layout/ms-x86-empty-base-after-base-with-vbptr.cpp
Modified:
cfe/trunk/lib/AST/RecordLayoutBuilder.cpp
Modified: cfe/trunk/lib/AST/RecordLayoutBuilder.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/RecordLayoutBuilder.cpp?rev=195158&r1=195157&r2=195158&view=diff
==============================================================================
--- cfe/trunk/lib/AST/RecordLayoutBuilder.cpp (original)
+++ cfe/trunk/lib/AST/RecordLayoutBuilder.cpp Tue Nov 19 16:11:09 2013
@@ -2017,6 +2017,8 @@ static bool isMsLayout(const RecordDecl*
// * If the last field is a non-zero length bitfield and we have any virtual
// bases then some extra padding is added before the virtual bases for no
// obvious reason.
+// * When laying out empty non-virtual bases, an extra byte of padding is added
+// if the non-virtual base before the empty non-virtual base has a vbptr.
namespace {
@@ -2141,6 +2143,8 @@ public:
bool LastBaseWasEmpty;
/// \brief Lets us know if we're in 64-bit mode
bool Is64BitMode;
+ /// \brief True if the last non-virtual base has a vbptr.
+ bool LastNonVirtualBaseHasVBPtr;
};
} // namespace
@@ -2304,6 +2308,7 @@ void
MicrosoftRecordLayoutBuilder::layoutNonVirtualBases(const CXXRecordDecl *RD) {
LazyEmptyBase = 0;
LastBaseWasEmpty = false;
+ LastNonVirtualBaseHasVBPtr = false;
// Lay out the primary base first.
if (PrimaryBase)
@@ -2331,6 +2336,10 @@ MicrosoftRecordLayoutBuilder::layoutNonV
const ASTRecordLayout &LazyLayout =
Context.getASTRecordLayout(LazyEmptyBase);
Size = Size.RoundUpToAlignment(LazyLayout.getAlignment());
+ // If the last non-virtual base has a vbptr we add a byte of padding for no
+ // obvious reason.
+ if (LastNonVirtualBaseHasVBPtr)
+ Size++;
Bases.insert(std::make_pair(LazyEmptyBase, Size));
// Empty bases only consume space when followed by another empty base.
if (RD && Layout->getNonVirtualSize().isZero()) {
@@ -2338,6 +2347,7 @@ MicrosoftRecordLayoutBuilder::layoutNonV
Size++;
}
LazyEmptyBase = 0;
+ LastNonVirtualBaseHasVBPtr = false;
}
// RD is null when flushing the final lazy base.
@@ -2356,6 +2366,7 @@ MicrosoftRecordLayoutBuilder::layoutNonV
// Note: we don't update alignment here because it was accounted
// for during initalization.
LastBaseWasEmpty = false;
+ LastNonVirtualBaseHasVBPtr = Layout->hasVBPtr();
}
void MicrosoftRecordLayoutBuilder::layoutVBPtr(const CXXRecordDecl *RD) {
Added: cfe/trunk/test/Layout/ms-x86-empty-base-after-base-with-vbptr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Layout/ms-x86-empty-base-after-base-with-vbptr.cpp?rev=195158&view=auto
==============================================================================
--- cfe/trunk/test/Layout/ms-x86-empty-base-after-base-with-vbptr.cpp (added)
+++ cfe/trunk/test/Layout/ms-x86-empty-base-after-base-with-vbptr.cpp Tue Nov 19 16:11:09 2013
@@ -0,0 +1,216 @@
+// RUN: %clang_cc1 -fno-rtti -emit-llvm-only -triple i686-pc-win32 -fdump-record-layouts -cxx-abi microsoft %s 2>/dev/null \
+// RUN: | FileCheck %s
+// RUN: %clang_cc1 -fno-rtti -emit-llvm-only -triple x86_64-pc-win32 -fdump-record-layouts -cxx-abi microsoft %s 2>/dev/null \
+// RUN: | FileCheck %s -check-prefix CHECK-X64
+
+
+struct U { char a; };
+struct V { };
+struct W { };
+struct X : virtual V { char a; };
+struct Y : virtual V { char a; };
+struct Z : Y { };
+
+struct A : X, W { char a; };
+
+// CHECK: *** Dumping AST Record Layout
+// CHECK: 0 | struct A
+// CHECK: 0 | struct X (base)
+// CHECK: 0 | (X vbtable pointer)
+// CHECK: 4 | char a
+// CHECK: 9 | struct W (base) (empty)
+// CHECK: 9 | char a
+// CHECK: 12 | struct V (virtual base) (empty)
+// CHECK: | [sizeof=12, align=4
+// CHECK: | nvsize=12, nvalign=4]
+// CHECK-X64: *** Dumping AST Record Layout
+// CHECK-X64: 0 | struct A
+// CHECK-X64: 0 | struct X (base)
+// CHECK-X64: 0 | (X vbtable pointer)
+// CHECK-X64: 8 | char a
+// CHECK-X64: 17 | struct W (base) (empty)
+// CHECK-X64: 17 | char a
+// CHECK-X64: 24 | struct V (virtual base) (empty)
+// CHECK-X64: | [sizeof=24, align=8
+// CHECK-X64: | nvsize=24, nvalign=8]
+
+struct B : X, U, W { char a; };
+
+// CHECK: *** Dumping AST Record Layout
+// CHECK: 0 | struct B
+// CHECK: 0 | struct X (base)
+// CHECK: 0 | (X vbtable pointer)
+// CHECK: 4 | char a
+// CHECK: 8 | struct U (base)
+// CHECK: 8 | char a
+// CHECK: 9 | struct W (base) (empty)
+// CHECK: 9 | char a
+// CHECK: 12 | struct V (virtual base) (empty)
+// CHECK: | [sizeof=12, align=4
+// CHECK: | nvsize=12, nvalign=4]
+// CHECK-X64: *** Dumping AST Record Layout
+// CHECK-X64: 0 | struct B
+// CHECK-X64: 0 | struct X (base)
+// CHECK-X64: 0 | (X vbtable pointer)
+// CHECK-X64: 8 | char a
+// CHECK-X64: 16 | struct U (base)
+// CHECK-X64: 16 | char a
+// CHECK-X64: 17 | struct W (base) (empty)
+// CHECK-X64: 17 | char a
+// CHECK-X64: 24 | struct V (virtual base) (empty)
+// CHECK-X64: | [sizeof=24, align=8
+// CHECK-X64: | nvsize=24, nvalign=8]
+
+struct C : X, V, W { char a; };
+
+// CHECK: *** Dumping AST Record Layout
+// CHECK: 0 | struct C
+// CHECK: 0 | struct X (base)
+// CHECK: 0 | (X vbtable pointer)
+// CHECK: 4 | char a
+// CHECK: 9 | struct V (base) (empty)
+// CHECK: 10 | struct W (base) (empty)
+// CHECK: 10 | char a
+// CHECK: 12 | struct V (virtual base) (empty)
+// CHECK: | [sizeof=12, align=4
+// CHECK: | nvsize=12, nvalign=4]
+// CHECK-X64: *** Dumping AST Record Layout
+// CHECK-X64: 0 | struct C
+// CHECK-X64: 0 | struct X (base)
+// CHECK-X64: 0 | (X vbtable pointer)
+// CHECK-X64: 8 | char a
+// CHECK-X64: 17 | struct V (base) (empty)
+// CHECK-X64: 18 | struct W (base) (empty)
+// CHECK-X64: 18 | char a
+// CHECK-X64: 24 | struct V (virtual base) (empty)
+// CHECK-X64: | [sizeof=24, align=8
+// CHECK-X64: | nvsize=24, nvalign=8]
+
+struct D : X, U, V, W { char a; };
+
+// CHECK: *** Dumping AST Record Layout
+// CHECK: 0 | struct D
+// CHECK: 0 | struct X (base)
+// CHECK: 0 | (X vbtable pointer)
+// CHECK: 4 | char a
+// CHECK: 8 | struct U (base)
+// CHECK: 8 | char a
+// CHECK: 9 | struct V (base) (empty)
+// CHECK: 10 | struct W (base) (empty)
+// CHECK: 10 | char a
+// CHECK: 12 | struct V (virtual base) (empty)
+// CHECK: | [sizeof=12, align=4
+// CHECK: | nvsize=12, nvalign=4]
+// CHECK-X64: *** Dumping AST Record Layout
+// CHECK-X64: 0 | struct D
+// CHECK-X64: 0 | struct X (base)
+// CHECK-X64: 0 | (X vbtable pointer)
+// CHECK-X64: 8 | char a
+// CHECK-X64: 16 | struct U (base)
+// CHECK-X64: 16 | char a
+// CHECK-X64: 17 | struct V (base) (empty)
+// CHECK-X64: 18 | struct W (base) (empty)
+// CHECK-X64: 18 | char a
+// CHECK-X64: 24 | struct V (virtual base) (empty)
+// CHECK-X64: | [sizeof=24, align=8
+// CHECK-X64: | nvsize=24, nvalign=8]
+
+struct E : X, U, Y, V, W { char a; };
+
+// CHECK: *** Dumping AST Record Layout
+// CHECK: 0 | struct E
+// CHECK: 0 | struct X (base)
+// CHECK: 0 | (X vbtable pointer)
+// CHECK: 4 | char a
+// CHECK: 8 | struct U (base)
+// CHECK: 8 | char a
+// CHECK: 12 | struct Y (base)
+// CHECK: 12 | (Y vbtable pointer)
+// CHECK: 16 | char a
+// CHECK: 21 | struct V (base) (empty)
+// CHECK: 22 | struct W (base) (empty)
+// CHECK: 22 | char a
+// CHECK: 24 | struct V (virtual base) (empty)
+// CHECK: | [sizeof=24, align=4
+// CHECK: | nvsize=24, nvalign=4]
+// CHECK-X64: *** Dumping AST Record Layout
+// CHECK-X64: 0 | struct E
+// CHECK-X64: 0 | struct X (base)
+// CHECK-X64: 0 | (X vbtable pointer)
+// CHECK-X64: 8 | char a
+// CHECK-X64: 16 | struct U (base)
+// CHECK-X64: 16 | char a
+// CHECK-X64: 24 | struct Y (base)
+// CHECK-X64: 24 | (Y vbtable pointer)
+// CHECK-X64: 32 | char a
+// CHECK-X64: 41 | struct V (base) (empty)
+// CHECK-X64: 42 | struct W (base) (empty)
+// CHECK-X64: 42 | char a
+// CHECK-X64: 48 | struct V (virtual base) (empty)
+// CHECK-X64: | [sizeof=48, align=8
+// CHECK-X64: | nvsize=48, nvalign=8]
+
+struct F : Z, W { char a; };
+
+// CHECK: *** Dumping AST Record Layout
+// CHECK: 0 | struct F
+// CHECK: 0 | struct Z (base)
+// CHECK: 0 | struct Y (base)
+// CHECK: 0 | (Y vbtable pointer)
+// CHECK: 4 | char a
+// CHECK: 9 | struct W (base) (empty)
+// CHECK: 9 | char a
+// CHECK: 12 | struct V (virtual base) (empty)
+// CHECK: | [sizeof=12, align=4
+// CHECK: | nvsize=12, nvalign=4]
+// CHECK-X64: *** Dumping AST Record Layout
+// CHECK-X64: 0 | struct F
+// CHECK-X64: 0 | struct Z (base)
+// CHECK-X64: 0 | struct Y (base)
+// CHECK-X64: 0 | (Y vbtable pointer)
+// CHECK-X64: 8 | char a
+// CHECK-X64: 17 | struct W (base) (empty)
+// CHECK-X64: 17 | char a
+// CHECK-X64: 24 | struct V (virtual base) (empty)
+// CHECK-X64: | [sizeof=24, align=8
+// CHECK-X64: | nvsize=24, nvalign=8]
+
+struct G : X, W, Y, V { char a; };
+
+// CHECK: *** Dumping AST Record Layout
+// CHECK: 0 | struct G
+// CHECK: 0 | struct X (base)
+// CHECK: 0 | (X vbtable pointer)
+// CHECK: 4 | char a
+// CHECK: 9 | struct W (base) (empty)
+// CHECK: 12 | struct Y (base)
+// CHECK: 12 | (Y vbtable pointer)
+// CHECK: 16 | char a
+// CHECK: 21 | struct V (base) (empty)
+// CHECK: 21 | char a
+// CHECK: 24 | struct V (virtual base) (empty)
+// CHECK: | [sizeof=24, align=4
+// CHECK: | nvsize=24, nvalign=4]
+// CHECK-X64: *** Dumping AST Record Layout
+// CHECK-X64: 0 | struct G
+// CHECK-X64: 0 | struct X (base)
+// CHECK-X64: 0 | (X vbtable pointer)
+// CHECK-X64: 8 | char a
+// CHECK-X64: 17 | struct W (base) (empty)
+// CHECK-X64: 24 | struct Y (base)
+// CHECK-X64: 24 | (Y vbtable pointer)
+// CHECK-X64: 32 | char a
+// CHECK-X64: 41 | struct V (base) (empty)
+// CHECK-X64: 41 | char a
+// CHECK-X64: 48 | struct V (virtual base) (empty)
+// CHECK-X64: | [sizeof=48, align=8
+// CHECK-X64: | nvsize=48, nvalign=8]
+
+int a[
+sizeof(A)+
+sizeof(B)+
+sizeof(C)+
+sizeof(D)+
+sizeof(E)+
+sizeof(F)+
+sizeof(G)];
More information about the cfe-commits
mailing list