[cfe-commits] r46856 - in /cfe/trunk: CodeGen/CodeGenTypes.cpp test/CodeGen/2008-02-07-bitfield-bug.c

Lauro Ramos Venancio lauro.venancio at gmail.com
Thu Feb 7 10:18:59 PST 2008


Author: laurov
Date: Thu Feb  7 12:18:58 2008
New Revision: 46856

URL: http://llvm.org/viewvc/llvm-project?rev=46856&view=rev
Log:
Fix codegen of
struct {
  char a[3];
  unsigned char b:1;
};

Fix PR1990.


Added:
    cfe/trunk/test/CodeGen/2008-02-07-bitfield-bug.c
Modified:
    cfe/trunk/CodeGen/CodeGenTypes.cpp

Modified: cfe/trunk/CodeGen/CodeGenTypes.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/CodeGen/CodeGenTypes.cpp?rev=46856&r1=46855&r2=46856&view=diff

==============================================================================
--- cfe/trunk/CodeGen/CodeGenTypes.cpp (original)
+++ cfe/trunk/CodeGen/CodeGenTypes.cpp Thu Feb  7 12:18:58 2008
@@ -477,6 +477,7 @@
 /// addPaddingFields - Current cursor is not suitable place to add next field.
 /// Add required padding fields.
 void RecordOrganizer::addPaddingFields(unsigned WaterMark) {
+  assert(WaterMark >= llvmSize && "Invalid padding Field");
   unsigned RequiredBits = WaterMark - llvmSize;
   unsigned RequiredBytes = (RequiredBits + 7) / 8;
   for (unsigned i = 0; i != RequiredBytes; ++i)
@@ -573,7 +574,7 @@
     uint64_t O = Offsets[i - 1];
     if (O % TySize == 0) {
       FoundPrevField = true;
-      if (TySize - (Cursor - O) >= BitFieldSize) {
+      if (TySize > (Cursor - O) && TySize - (Cursor - O) >= BitFieldSize) {
 	// The bitfield fits in the last aligned field.
 	// This is : struct { char a; int CurrentField:10;};
 	// where 'CurrentField' shares first field with 'a'.
@@ -585,11 +586,15 @@
 	// Place the bitfield in a new LLVM field.
 	// This is : struct { char a; short CurrentField:10;};
 	// where 'CurrentField' needs a new llvm field.
-	addPaddingFields(O + TySize);
+        unsigned Padding = 0;
+        if (Cursor % TySize) {
+          Padding = TySize - (Cursor % TySize);
+          addPaddingFields(Cursor + Padding);
+        }
 	CGT.addFieldInfo(FD, llvmFieldNo);
 	CGT.addBitFieldInfo(FD, 0, BitFieldSize);
-	addPaddingFields(O + TySize +  BitFieldSize);
-	Cursor = O + TySize +  BitFieldSize;
+        Cursor += Padding + BitFieldSize;
+	addPaddingFields(Cursor);
       }
       break;
     }

Added: cfe/trunk/test/CodeGen/2008-02-07-bitfield-bug.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/2008-02-07-bitfield-bug.c?rev=46856&view=auto

==============================================================================
--- cfe/trunk/test/CodeGen/2008-02-07-bitfield-bug.c (added)
+++ cfe/trunk/test/CodeGen/2008-02-07-bitfield-bug.c Thu Feb  7 12:18:58 2008
@@ -0,0 +1,11 @@
+// RUN: clang %s -emit-llvm
+// PR1990
+
+struct test {
+  char a[3];
+  unsigned char b:1;
+};
+
+void f(struct test *t) {
+  t->b = 1;
+}





More information about the cfe-commits mailing list