[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 13 15:01:59 PDT 2024


================
@@ -1,13 +1,58 @@
-// RUN: %clang_cc1 %s -verify=c -fsyntax-only
+// RUN: %clang_cc1 %s -verify -fsyntax-only
 // RUN: %clang_cc1 %s -verify -fsyntax-only -x c++
-// RUN: %clang_cc1 %s -verify -fsyntax-only -fms-compatibility
 // RUN: %clang_cc1 %s -verify -fsyntax-only -fms-compatibility -x c++
+// RUN: %clang_cc1 %s -verify=gnu -fsyntax-only -Wgnu-flexible-array-union-member -Wgnu-empty-struct
+// RUN: %clang_cc1 %s -verify=microsoft -fsyntax-only -fms-compatibility -Wmicrosoft
 
 // The test checks that an attempt to initialize union with flexible array
 // member with an initializer list doesn't crash clang.
 
 
-union { char x[]; } r = {0}; // c-error {{flexible array member 'x' in a union is not allowed}}
+union { char x[]; } r = {0}; /* gnu-warning {{flexible array member 'x' in a union is a GNU extension}}
+                                microsoft-warning {{flexible array member 'x' in a union is a Microsoft extension}}
+                              */
----------------
kees wrote:

Clang's initialization for flexible arrays works for all the cases I've checked. Part of Linux's migration to C99 flex arrays uncovered a `__builtin_object_size()` bug that has was fixed in Clang 17 via commit 804af933f7310f78d91e13ad8a13b64b00249614 which includes several initialization tests beyond the ones that are touched in this PR. As a result I have confidence in this having appropriate test coverage, though if more is needed, it should be trivial to add.

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


More information about the cfe-commits mailing list