From 5fe61c6cb09a5a2d2ecddfb74f8b253d9d462165 Mon Sep 17 00:00:00 2001 From: Eli Friedman Date: Fri, 30 Mar 2012 03:55:31 +0000 Subject: [PATCH] ConstStructBuilder: fix offset math for base classes so it works correctly in general. Found by inspection. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@153720 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/CodeGen/CGExprConstant.cpp | 10 +++------- test/CodeGenCXX/const-init-cxx11.cpp | 5 +++++ 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/lib/CodeGen/CGExprConstant.cpp b/lib/CodeGen/CGExprConstant.cpp index fb019f4d00..60cb4c5c23 100644 --- a/lib/CodeGen/CGExprConstant.cpp +++ b/lib/CodeGen/CGExprConstant.cpp @@ -469,21 +469,17 @@ void ConstStructBuilder::Build(const APValue &Val, const RecordDecl *RD, for (unsigned I = 0, N = Bases.size(); I != N; ++I) { BaseInfo &Base = Bases[I]; - // Build the base class subobject at the appropriately-offset location - // within this object. - NextFieldOffsetInChars -= Base.Offset; bool IsPrimaryBase = Layout.getPrimaryBase() == Base.Decl; Build(Val.getStructBase(Base.Index), Base.Decl, IsPrimaryBase, VTable, VTableClass, Offset + Base.Offset); - - NextFieldOffsetInChars += Base.Offset; } } unsigned FieldNo = 0; const FieldDecl *LastFD = 0; bool IsMsStruct = RD->hasAttr(); + uint64_t OffsetBits = CGM.getContext().toBits(Offset); for (RecordDecl::field_iterator Field = RD->field_begin(), FieldEnd = RD->field_end(); Field != FieldEnd; ++Field, ++FieldNo) { @@ -516,10 +512,10 @@ void ConstStructBuilder::Build(const APValue &Val, const RecordDecl *RD, if (!Field->isBitField()) { // Handle non-bitfield members. - AppendField(*Field, Layout.getFieldOffset(FieldNo), EltInit); + AppendField(*Field, Layout.getFieldOffset(FieldNo) + OffsetBits, EltInit); } else { // Otherwise we have a bitfield. - AppendBitField(*Field, Layout.getFieldOffset(FieldNo), + AppendBitField(*Field, Layout.getFieldOffset(FieldNo) + OffsetBits, cast(EltInit)); } } diff --git a/test/CodeGenCXX/const-init-cxx11.cpp b/test/CodeGenCXX/const-init-cxx11.cpp index 8d92b81feb..c6250c6d96 100644 --- a/test/CodeGenCXX/const-init-cxx11.cpp +++ b/test/CodeGenCXX/const-init-cxx11.cpp @@ -78,6 +78,11 @@ namespace BaseClass { struct Test2 : X, X, X, X {}; // CHECK: @_ZN9BaseClass2t2E = constant {{.*}} undef extern constexpr Test2 t2 = Test2(); + + struct __attribute((packed)) PackedD { double y = 2; }; + struct Test3 : C, PackedD { constexpr Test3() {} }; + // CHECK: @_ZN9BaseClass2t3E = constant <{ i8, double }> <{ i8 1, double 2.000000e+00 }> + extern constexpr Test3 t3 = Test3(); } namespace Array { -- 2.40.0