From de9f153b2348f590151504888c22cb937134cd27 Mon Sep 17 00:00:00 2001 From: Anders Carlsson Date: Sat, 17 Apr 2010 20:21:41 +0000 Subject: [PATCH] If a wide bit-field is inside a union its offset should always be 0. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@101668 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/AST/RecordLayoutBuilder.cpp | 11 +++++++---- test/CodeGenCXX/bitfield-layout.cpp | 9 +++++++++ 2 files changed, 16 insertions(+), 4 deletions(-) create mode 100644 test/CodeGenCXX/bitfield-layout.cpp diff --git a/lib/AST/RecordLayoutBuilder.cpp b/lib/AST/RecordLayoutBuilder.cpp index d1de70a7ab..a674ad7970 100644 --- a/lib/AST/RecordLayoutBuilder.cpp +++ b/lib/AST/RecordLayoutBuilder.cpp @@ -640,13 +640,16 @@ void ASTRecordLayoutBuilder::LayoutWideBitField(uint64_t FieldSize, // We're not going to use any of the unfilled bits in the last byte. UnfilledBitsInLastByte = 0; - // The bitfield is allocated starting at the next offset aligned appropriately - // for T', with length n bits. - uint64_t FieldOffset = llvm::RoundUpToAlignment(DataSize, TypeAlign); - + uint64_t FieldOffset; + if (IsUnion) { DataSize = std::max(DataSize, FieldSize); + FieldOffset = 0; } else { + // The bitfield is allocated starting at the next offset aligned appropriately + // for T', with length n bits. + FieldOffset = llvm::RoundUpToAlignment(DataSize, TypeAlign); + uint64_t NewSizeInBits = FieldOffset + FieldSize; DataSize = llvm::RoundUpToAlignment(NewSizeInBits, 8); diff --git a/test/CodeGenCXX/bitfield-layout.cpp b/test/CodeGenCXX/bitfield-layout.cpp new file mode 100644 index 0000000000..c77c925d87 --- /dev/null +++ b/test/CodeGenCXX/bitfield-layout.cpp @@ -0,0 +1,9 @@ +// RUN: %clang_cc1 %s -triple=x86_64-apple-darwin10 -emit-llvm -o - | FileCheck %s + +// CHECK: = type { i32, [4 x i8] } +union Test1 { + int a; + int b: 39; +}; + +Test1 t1; -- 2.40.0