r201288 - Fixing a compiler assertion with zero-width bit-fields in packed structs.

Yunzhong Gao Yunzhong_Gao at playstation.sony.com
Wed Feb 12 18:48:03 PST 2014


Author: ygao
Date: Wed Feb 12 20:45:10 2014
New Revision: 201288

URL: http://llvm.org/viewvc/llvm-project?rev=201288&view=rev
Log:
Fixing a compiler assertion with zero-width bit-fields in packed structs.
According to the GNU docs, zero-sized bitfields should not be affected by the
packed attribute.

Differential Revision: http://llvm-reviews.chandlerc.com/D2693


Modified:
    cfe/trunk/lib/AST/RecordLayoutBuilder.cpp
    cfe/trunk/test/Sema/bitfield-layout.c

Modified: cfe/trunk/lib/AST/RecordLayoutBuilder.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/RecordLayoutBuilder.cpp?rev=201288&r1=201287&r2=201288&view=diff
==============================================================================
--- cfe/trunk/lib/AST/RecordLayoutBuilder.cpp (original)
+++ cfe/trunk/lib/AST/RecordLayoutBuilder.cpp Wed Feb 12 20:45:10 2014
@@ -1552,8 +1552,8 @@ void RecordLayoutBuilder::LayoutBitField
   // Remember the alignment we would have used if the field were not packed.
   unsigned UnpackedFieldAlign = FieldAlign;
 
-  // Ignore the field alignment if the field is packed.
-  if (!IsMsStruct && FieldPacked)
+  // Ignore the field alignment if the field is packed unless it has zero-size.
+  if (!IsMsStruct && FieldPacked && FieldSize != 0)
     FieldAlign = 1;
 
   // But, if there's an 'aligned' attribute on the field, honor that.

Modified: cfe/trunk/test/Sema/bitfield-layout.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/bitfield-layout.c?rev=201288&r1=201287&r2=201288&view=diff
==============================================================================
--- cfe/trunk/test/Sema/bitfield-layout.c (original)
+++ cfe/trunk/test/Sema/bitfield-layout.c Wed Feb 12 20:45:10 2014
@@ -9,6 +9,21 @@ struct a {char x; int : 0; char y;};
 CHECK_SIZE(struct, a, 5)
 CHECK_ALIGN(struct, a, 1)
 
+// Zero-width bit-fields with packed
+struct __attribute__((packed)) a2 { short x : 9; char : 0; int y : 17; };
+CHECK_SIZE(struct, a2, 5)
+CHECK_ALIGN(struct, a2, 1)
+
+// Zero-width bit-fields at the end of packed struct
+struct __attribute__((packed)) a3 { short x : 9; int : 0; };
+CHECK_SIZE(struct, a3, 4)
+CHECK_ALIGN(struct, a3, 1)
+
+// For comparison, non-zero-width bit-fields at the end of packed struct
+struct __attribute__((packed)) a4 { short x : 9; int : 1; };
+CHECK_SIZE(struct, a4, 2)
+CHECK_ALIGN(struct, a4, 1)
+
 union b {char x; int : 0; char y;};
 CHECK_SIZE(union, b, 1)
 CHECK_ALIGN(union, b, 1)





More information about the cfe-commits mailing list