]> granicus.if.org Git - clang/commitdiff
If the alignment of the chosen field in a union is greater than the alignment of...
authorAnders Carlsson <andersca@mac.com>
Thu, 3 Sep 2009 22:56:02 +0000 (22:56 +0000)
committerAnders Carlsson <andersca@mac.com>
Thu, 3 Sep 2009 22:56:02 +0000 (22:56 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@80964 91177308-0d34-0410-b5e6-96231b3b80d8

lib/CodeGen/CGRecordLayoutBuilder.cpp
test/CodeGen/pragma-pack-3.c [new file with mode: 0644]

index 74ef2eacc987975c9b5b71e83bc3c61118af7a0e..f2fd885ee4d9a1cf17a388ee4727315529d0ee1f 100644 (file)
@@ -199,8 +199,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 (file)
index 0000000..b9166ae
--- /dev/null
@@ -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;