
GNU and MSVC have extensions where flexible array members (or their equivalent) can be in unions or alone in structs. This is already fully supported in Clang through the 0-sized array ("fake flexible array") extension or when C99 flexible array members have been syntactically obfuscated. Clang needs to explicitly allow these extensions directly for C99 flexible arrays, since they are common code patterns in active use by the Linux kernel (and other projects). Such projects have been using either 0-sized arrays (which is considered deprecated in favor of C99 flexible array members) or via obfuscated syntax, both of which complicate their code bases. For example, these do not error by default: ``` union one { int a; int b[0]; }; union two { int a; struct { struct { } __empty; int b[]; }; }; ``` But this does: ``` union three { int a; int b[]; }; ``` Remove the default error diagnostics for this but continue to provide warnings under Microsoft or GNU extensions checks. This will allow for a seamless transition for code bases away from 0-sized arrays without losing existing code patterns. Add explicit checking for the warnings under various constructions. Additionally fixes a CodeGen bug with flexible array members in unions in C++, which was found when adding a testcase for: ``` union { char x[]; } z = {0}; ``` which only had Sema tests originally. Fixes #84565
25 lines
718 B
C++
25 lines
718 B
C++
// RUN: %clang_cc1 -triple i386-unknown-unknown -x c++ -emit-llvm -o - %s | FileCheck %s
|
|
|
|
union _u { char a[]; } u = {};
|
|
union _u0 { char a[]; } u0 = {0};
|
|
|
|
// CHECK: %union._u = type { [0 x i8] }
|
|
|
|
// CHECK: @u = global %union._u zeroinitializer, align 1
|
|
// CHECK: @u0 = global { [1 x i8] } zeroinitializer, align 1
|
|
|
|
union { char a[]; } z = {};
|
|
// CHECK: @z = internal global %union.{{.*}} zeroinitializer, align 1
|
|
union { char a[]; } z0 = {0};
|
|
// CHECK: @z0 = internal global { [1 x i8] } zeroinitializer, align 1
|
|
|
|
/* C++ requires global anonymous unions have static storage, so we have to
|
|
reference them to keep them in the IR output. */
|
|
char keep(int pick)
|
|
{
|
|
if (pick)
|
|
return z.a[0];
|
|
else
|
|
return z0.a[0];
|
|
}
|