summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTanya Lattner <tonic@nondot.org>2009-09-04 20:02:07 +0000
committerTanya Lattner <tonic@nondot.org>2009-09-04 20:02:07 +0000
commit9304ff3568d9d1a95316aaa18e1ae3283044f313 (patch)
treea4c496448f152e7f1f694cb89c2e3dae27ed609e
parent617e4548d0bb791fbfd580e2d0074baf8b9a8b8a (diff)
Merge 80964 from mainline.
If the alignment of the chosen field in a union is greater than the alignment of the union, we need to use a packed LLVM struct. Fixes <rdar://problem/7184250>. git-svn-id: https://llvm.org/svn/llvm-project/cfe/branches/release_26@81035 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r--lib/CodeGen/CGRecordLayoutBuilder.cpp9
-rw-r--r--test/CodeGen/pragma-pack-3.c19
2 files changed, 27 insertions, 1 deletions
diff --git a/lib/CodeGen/CGRecordLayoutBuilder.cpp b/lib/CodeGen/CGRecordLayoutBuilder.cpp
index 508a997b0a..79d66209e8 100644
--- a/lib/CodeGen/CGRecordLayoutBuilder.cpp
+++ b/lib/CodeGen/CGRecordLayoutBuilder.cpp
@@ -200,8 +200,15 @@ void CGRecordLayoutBuilder::LayoutUnion(const RecordDecl *D) {
}
// Now add our field.
- if (Ty)
+ if (Ty) {
AppendField(0, Ty);
+
+ if (getTypeAlignment(Ty) > Layout.getAlignment() / 8) {
+ // We need a packed struct.
+ Packed = true;
+ Align = 1;
+ }
+ }
// Append tail padding.
if (Layout.getSize() / 8 > Size)
diff --git a/test/CodeGen/pragma-pack-3.c b/test/CodeGen/pragma-pack-3.c
new file mode 100644
index 0000000000..b9166ae5d3
--- /dev/null
+++ b/test/CodeGen/pragma-pack-3.c
@@ -0,0 +1,19 @@
+// RUN: clang-cc -triple i386-apple-darwin9 %s -emit-llvm -o - | FileCheck -check-prefix X32 %s &&
+// CHECK-X32: %struct.menu = type <{ i8*, i8, i8 }>
+// CHECK-X32: %union.command = type <{ i8*, [2 x i8] }>
+
+// RUN: clang-cc -triple x86_64-apple-darwin9 %s -emit-llvm -o - | FileCheck -check-prefix X64 %s
+// CHECK-X64: %struct.menu = type <{ i8*, i8, i8 }>
+// CHECK-X64: %union.command = type <{ i8*, [2 x i8] }>
+
+// <rdar://problem/7184250>
+#pragma pack(push, 2)
+typedef union command {
+ void *windowRef;
+ struct menu {
+ void *menuRef;
+ unsigned char menuItemIndex;
+ } menu;
+} command;
+
+command c;