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

Kees Cook via cfe-commits cfe-commits at lists.llvm.org
Wed Mar 20 23:30:00 PDT 2024


kees wrote:

> `InitListChecker::CheckStructUnionTypes` never calls `StructuredList->setInitializedFieldInUnion`

Ah-ha, thank you for the pointer. I think I've figured this out: initialization was avoiding flexible arrays because we don't yet support non-zero initialization (as described in commit 5955a0f9375a8c0b134eeb4a8de5155dcce7c94f). However, we _do_ want to allow zero initialization. This fixes the Assert and the sketchy `undef`s in the codegen output. Now we get zero inits:

```diff
-    // If we've hit the flexible array member at the end, we're done.
-    if (Field->getType()->isIncompleteArrayType())
+    // If we've hit a flexible array member, only allow zero initialization.
+    // Other values are not yet supported. See commit 5955a0f9375a.
+    if (Field->getType()->isIncompleteArrayType() && !IsZeroInitializer(Init))
```

> I think things can be simplified a bit further

Ah yes! That's much nicer. Since we're processing the Union, we can just return completely instead of continuing the loop. Thanks!


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


More information about the cfe-commits mailing list