[cfe-commits] r101673 - in /cfe/trunk: lib/CodeGen/CGRecordLayoutBuilder.cpp test/CodeGenCXX/bitfield-layout.cpp

Anders Carlsson andersca at mac.com
Sat Apr 17 14:04:52 PDT 2010


Author: andersca
Date: Sat Apr 17 16:04:52 2010
New Revision: 101673

URL: http://llvm.org/viewvc/llvm-project?rev=101673&view=rev
Log:
Unnamed bit-fields in a union should be laid out with a type that doesn't affect alignment.

Modified:
    cfe/trunk/lib/CodeGen/CGRecordLayoutBuilder.cpp
    cfe/trunk/test/CodeGenCXX/bitfield-layout.cpp

Modified: cfe/trunk/lib/CodeGen/CGRecordLayoutBuilder.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGRecordLayoutBuilder.cpp?rev=101673&r1=101672&r2=101673&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGRecordLayoutBuilder.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGRecordLayoutBuilder.cpp Sat Apr 17 16:04:52 2010
@@ -332,10 +332,26 @@
     if (FieldSize == 0)
       return 0;
 
+    const llvm::Type *FieldTy;
+
+    if (!Field->getDeclName()) {
+      // This is an unnamed bit-field, which shouldn't affect alignment on the
+      // struct so we use an array of bytes for it.
+
+      FieldTy = llvm::Type::getInt8Ty(Types.getLLVMContext());
+
+      unsigned NumBytesToAppend =
+        llvm::RoundUpToAlignment(FieldSize, 8) / 8;
+
+      if (NumBytesToAppend > 1)
+        FieldTy = llvm::ArrayType::get(FieldTy, NumBytesToAppend);
+    } else 
+      FieldTy = Types.ConvertTypeForMemRecursive(Field->getType());
+    
     // Add the bit field info.
     LLVMBitFields.push_back(
       LLVMBitFieldInfo(Field, ComputeBitFieldInfo(Types, Field, 0, FieldSize)));
-    return Types.ConvertTypeForMemRecursive(Field->getType());
+    return FieldTy;
   }
   
   // This is a regular union field.

Modified: cfe/trunk/test/CodeGenCXX/bitfield-layout.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenCXX/bitfield-layout.cpp?rev=101673&r1=101672&r2=101673&view=diff
==============================================================================
--- cfe/trunk/test/CodeGenCXX/bitfield-layout.cpp (original)
+++ cfe/trunk/test/CodeGenCXX/bitfield-layout.cpp Sat Apr 17 16:04:52 2010
@@ -1,9 +1,17 @@
 // RUN: %clang_cc1 %s -triple=x86_64-apple-darwin10 -emit-llvm -o - | FileCheck %s
 
-// CHECK: = type { i32, [4 x i8] }
+// CHECK: %union.Test1 = type { i32, [4 x i8] }
 union Test1 {
   int a;
   int b: 39;
-};
+} t1;
 
-Test1 t1;
+// CHECK: %union.Test2 = type { i8 }
+union Test2 {
+  int : 6;
+} t2;
+
+// CHECK: %union.Test3 = type { [2 x i8] }
+union Test3 {
+  int : 9;
+} t3;





More information about the cfe-commits mailing list