[cfe-commits] r46806 - in /cfe/trunk: AST/ASTContext.cpp test/CodeGen/struct.c

Eli Friedman eli.friedman at gmail.com
Tue Feb 5 21:33:52 PST 2008


Author: efriedma
Date: Tue Feb  5 23:33:51 2008
New Revision: 46806

URL: http://llvm.org/viewvc/llvm-project?rev=46806&view=rev
Log:
Fix the codegen of structs with flexible array members.


Modified:
    cfe/trunk/AST/ASTContext.cpp
    cfe/trunk/test/CodeGen/struct.c

Modified: cfe/trunk/AST/ASTContext.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/AST/ASTContext.cpp?rev=46806&r1=46805&r2=46806&view=diff

==============================================================================
--- cfe/trunk/AST/ASTContext.cpp (original)
+++ cfe/trunk/AST/ASTContext.cpp Tue Feb  5 23:33:51 2008
@@ -323,10 +323,22 @@
     // the future, this will need to be tweakable by targets.
     for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) {
       const FieldDecl *FD = D->getMember(i);
-      std::pair<uint64_t, unsigned> FieldInfo = getTypeInfo(FD->getType(), L);
-      uint64_t FieldSize = FieldInfo.first;
-      unsigned FieldAlign = FieldInfo.second;
-      
+      uint64_t FieldSize;
+      unsigned FieldAlign;
+      if (FD->getType()->isIncompleteType()) {
+        // This must be a flexible array member; we can't directly
+        // query getTypeInfo about these, so we figure it out here.
+        // Flexible array members don't have any size, but they
+        // have to be aligned appropriately for their element type.
+        const ArrayType* ATy = FD->getType()->getAsArrayType();
+        FieldAlign = getTypeAlign(ATy->getElementType(), L);
+        FieldSize = 0;
+      } else {
+        std::pair<uint64_t, unsigned> FieldInfo = getTypeInfo(FD->getType(), L);
+        FieldSize = FieldInfo.first;
+        FieldAlign = FieldInfo.second;
+      }
+
       // Round up the current record size to the field's alignment boundary.
       RecordSize = (RecordSize+FieldAlign-1) & ~(FieldAlign-1);
       

Modified: cfe/trunk/test/CodeGen/struct.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/struct.c?rev=46806&r1=46805&r2=46806&view=diff

==============================================================================
--- cfe/trunk/test/CodeGen/struct.c (original)
+++ cfe/trunk/test/CodeGen/struct.c Tue Feb  5 23:33:51 2008
@@ -138,6 +138,10 @@
 }
 
 /* struct initialization */
-struct a13 {int b; int c};
+struct a13 {int b; int c;};
 struct a13 c13 = {5};
 struct a14 { short a; int b; } x = {1, 1};
+
+/* flexible array members */
+struct a15 {char a; int b[];} c15;
+int a16(void) {c15.a = 1;}





More information about the cfe-commits mailing list