From: Eli Friedman Date: Wed, 6 Feb 2008 05:33:51 +0000 (+0000) Subject: Fix the codegen of structs with flexible array members. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=75afb585496b8c521672a6a91b8dd1838d36185a;p=clang Fix the codegen of structs with flexible array members. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@46806 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/AST/ASTContext.cpp b/AST/ASTContext.cpp index 535f69271f..6eae4584d5 100644 --- a/AST/ASTContext.cpp +++ b/AST/ASTContext.cpp @@ -323,10 +323,22 @@ const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D, // 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 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 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); diff --git a/test/CodeGen/struct.c b/test/CodeGen/struct.c index fc79f0a2f4..1b476f823d 100644 --- a/test/CodeGen/struct.c +++ b/test/CodeGen/struct.c @@ -138,6 +138,10 @@ void f12() } /* 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;}