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

Erich Keane via cfe-commits cfe-commits at lists.llvm.org
Mon Mar 11 11:15:01 PDT 2024


erichkeane wrote:

> But then we (and GCC and MSVC) support the `[0]` extension syntax. So it's not like codegen is impossible.
> 
> https://godbolt.org/z/PGa9KWGxq
> 
> ```c
> union foo {
> 	int a;
> 	int b[]; // error: flexible array member 'b' in a union is not allowed
> };
> union bar {
> 	int a;
> 	int b[0]; // works just fine!
> };
> ```
> 
> So to me, it seems like the standard can be amended to add the "or unions too" and already 3 implementations could generate valid code (GCC and clang would both need their semantic analyses relaxed here, but their codegen seems to work for the `[0]` extension already).
> 
> I assume this would be useful for type-punning via a union where one type is a flexible array.

Note that there is a HUGE difference (usually) between "works well enough" and "doesn't have huge gotchas" when it comes to standardization.  Over the years, we've run into TONS of features that are supported on compilers, but not to high enough quality for standardization (see #pragma once).

Also note, "works on 3 of the hundreds of C implementations" is a minor set.  

Finally, note I mentioned the 'abstract machine'.  We already had some serious discussions with `memset_explicit` that were the result of only minor considerations not being expressible as a part of the abstract machine, this ends up being a whole different can of worms.

All that to say: Papers welcome :) 

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


More information about the cfe-commits mailing list