[clang] [Clang][Sema]: Allow flexible arrays in unions and alone in structs (PR #84428)

Aaron Ballman via cfe-commits cfe-commits at lists.llvm.org
Mon Mar 11 10:32:57 PDT 2024


AaronBallman wrote:

> > C23 6.7.3.2p20: "As a special case, the last member of a structure with more than one named member may have an incomplete array type; this is called a flexible array member. ..."
> 
> Thanks! Yeah, I wonder if that could have been "of a structure _or union_ " (as in was that an intentional or unintentional omission)?

C99 added flexible array members, and the C99 rationale says the feature was added specifically as a replacement for the common idiom known as the "struct hack" for creating a structure containing a variable-size array. I think the specification is intentional, given that the rationale document says "There are a few restrictions on flexible array members that ensure that code using them makes sense. For example, there must be at least one other member, and the flexible array must occur last." and a union has no such notion because all the members overlap (so there's not really "one other member").

https://github.com/llvm/llvm-project/pull/84428


More information about the cfe-commits mailing list