diff options
author | Anders Carlsson <andersca@mac.com> | 2009-11-22 19:13:51 +0000 |
---|---|---|
committer | Anders Carlsson <andersca@mac.com> | 2009-11-22 19:13:51 +0000 |
commit | ba958400a28412d02a0a6ba8af7920e24f70e87b (patch) | |
tree | 97f621647ff1bcc4608b975f8b2c8e37d37a4e35 /clang/lib/AST | |
parent | 1299f36af18a566f092ac15e76030a4512ea64b0 (diff) | |
download | bcm5719-llvm-ba958400a28412d02a0a6ba8af7920e24f70e87b.tar.gz bcm5719-llvm-ba958400a28412d02a0a6ba8af7920e24f70e87b.zip |
When laying out bitfields, make sure that the data size is always aligned to a byte. This fixes PR5580.
llvm-svn: 89611
Diffstat (limited to 'clang/lib/AST')
-rw-r--r-- | clang/lib/AST/RecordLayoutBuilder.cpp | 30 | ||||
-rw-r--r-- | clang/lib/AST/RecordLayoutBuilder.h | 9 |
2 files changed, 28 insertions, 11 deletions
diff --git a/clang/lib/AST/RecordLayoutBuilder.cpp b/clang/lib/AST/RecordLayoutBuilder.cpp index 8e4e160746e..5bc27b9d06f 100644 --- a/clang/lib/AST/RecordLayoutBuilder.cpp +++ b/clang/lib/AST/RecordLayoutBuilder.cpp @@ -22,9 +22,9 @@ using namespace clang; ASTRecordLayoutBuilder::ASTRecordLayoutBuilder(ASTContext &Ctx) - : Ctx(Ctx), Size(0), Alignment(8), Packed(false), MaxFieldAlignment(0), - DataSize(0), IsUnion(false), NonVirtualSize(0), NonVirtualAlignment(8), - PrimaryBase(0), PrimaryBaseWasVirtual(false) {} + : Ctx(Ctx), Size(0), Alignment(8), Packed(false), UnfilledBitsInLastByte(0), + MaxFieldAlignment(0), DataSize(0), IsUnion(false), NonVirtualSize(0), + NonVirtualAlignment(8), PrimaryBase(0), PrimaryBaseWasVirtual(false) {} /// LayoutVtable - Lay out the vtable and set PrimaryBase. void ASTRecordLayoutBuilder::LayoutVtable(const CXXRecordDecl *RD) { @@ -521,7 +521,7 @@ void ASTRecordLayoutBuilder::LayoutFields(const RecordDecl *D) { void ASTRecordLayoutBuilder::LayoutBitField(const FieldDecl *D) { bool FieldPacked = Packed || D->hasAttr<PackedAttr>(); - uint64_t FieldOffset = IsUnion ? 0 : DataSize; + uint64_t FieldOffset = IsUnion ? 0 : (DataSize - UnfilledBitsInLastByte); uint64_t FieldSize = D->getBitWidth()->EvaluateAsInt(Ctx).getZExtValue(); std::pair<uint64_t, unsigned> FieldInfo = Ctx.getTypeInfo(D->getType()); @@ -549,14 +549,19 @@ void ASTRecordLayoutBuilder::LayoutBitField(const FieldDecl *D) { // Place this field at the current location. FieldOffsets.push_back(FieldOffset); - // Reserve space for this field. - if (IsUnion) - Size = std::max(Size, FieldSize); - else - Size = FieldOffset + FieldSize; + // Update DataSize to include the last byte containing (part of) the bitfield. + if (IsUnion) { + // FIXME: I think FieldSize should be TypeSize here. + DataSize = std::max(DataSize, FieldSize); + } else { + uint64_t NewSizeInBits = FieldOffset + FieldSize; + + DataSize = llvm::RoundUpToAlignment(NewSizeInBits, 8); + UnfilledBitsInLastByte = DataSize - NewSizeInBits; + } - // Update the data size. - DataSize = Size; + // Update the size. + Size = std::max(Size, DataSize); // Remember max struct/class alignment. UpdateAlignment(FieldAlign); @@ -568,6 +573,9 @@ void ASTRecordLayoutBuilder::LayoutField(const FieldDecl *D) { return; } + // Reset the unfilled bits. + UnfilledBitsInLastByte = 0; + bool FieldPacked = Packed || D->hasAttr<PackedAttr>(); uint64_t FieldOffset = IsUnion ? 0 : DataSize; uint64_t FieldSize; diff --git a/clang/lib/AST/RecordLayoutBuilder.h b/clang/lib/AST/RecordLayoutBuilder.h index c738e31beb4..fe02d882cec 100644 --- a/clang/lib/AST/RecordLayoutBuilder.h +++ b/clang/lib/AST/RecordLayoutBuilder.h @@ -27,12 +27,21 @@ namespace clang { class ASTRecordLayoutBuilder { ASTContext &Ctx; + /// Size - The current size of the record layout. uint64_t Size; + + /// Alignment - The current alignment of the record layout. unsigned Alignment; + llvm::SmallVector<uint64_t, 16> FieldOffsets; /// Packed - Whether the record is packed or not. bool Packed; + + /// UnfilledBitsInLastByte - If the last field laid out was a bitfield, + /// this contains the number of bits in the last byte that can be used for + /// an adjacent bitfield if necessary. + unsigned char UnfilledBitsInLastByte; /// MaxFieldAlignment - The maximum allowed field alignment. This is set by /// #pragma pack. |