From 0bfe54fdc83b7b4e37c40e652d86d15aa89885b2 Mon Sep 17 00:00:00 2001 From: Douglas Gregor Date: Tue, 10 Feb 2009 21:49:46 +0000 Subject: [PATCH] GNU allows structs with flexible array members to be placed inside arrays and other structs/unions as an extension. Downgrade our error to a warning. Fixes PR3540. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@64239 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Basic/DiagnosticSemaKinds.def | 2 +- lib/Sema/SemaType.cpp | 9 ++---- test/Sema/array-constraint.c | 2 +- test/Sema/flexible-array-init.c | 32 +++++++++++++++++---- 4 files changed, 31 insertions(+), 14 deletions(-) diff --git a/include/clang/Basic/DiagnosticSemaKinds.def b/include/clang/Basic/DiagnosticSemaKinds.def index 9c879f740a..5dfe9d6f35 100644 --- a/include/clang/Basic/DiagnosticSemaKinds.def +++ b/include/clang/Basic/DiagnosticSemaKinds.def @@ -651,7 +651,7 @@ DIAG(err_flexible_array_empty_struct, ERROR, "flexible array %0 not allowed in otherwise empty struct") DIAG(ext_flexible_array_in_struct, EXTENSION, "%0 may not be nested in a struct due to flexible array member") -DIAG(err_flexible_array_in_array, ERROR, +DIAG(ext_flexible_array_in_array, EXTENSION, "%0 may not be used as an array element due to flexible array member") DIAG(err_flexible_array_init_nonempty, ERROR, "non-empty initialization of flexible array member inside subobject") diff --git a/lib/Sema/SemaType.cpp b/lib/Sema/SemaType.cpp index 25fd5c39fe..e898782f31 100644 --- a/lib/Sema/SemaType.cpp +++ b/lib/Sema/SemaType.cpp @@ -399,12 +399,9 @@ QualType Sema::GetTypeForDeclarator(Declarator &D, Scope *S, unsigned Skip) { D.setInvalidType(true); } else if (const RecordType *EltTy = T->getAsRecordType()) { // If the element type is a struct or union that contains a variadic - // array, reject it: C99 6.7.2.1p2. - if (EltTy->getDecl()->hasFlexibleArrayMember()) { - Diag(DeclType.Loc, diag::err_flexible_array_in_array) << T; - T = Context.IntTy; - D.setInvalidType(true); - } + // array, accept it as a GNU extension: C99 6.7.2.1p2. + if (EltTy->getDecl()->hasFlexibleArrayMember()) + Diag(DeclType.Loc, diag::ext_flexible_array_in_array) << T; } else if (T->isObjCInterfaceType()) { Diag(DeclType.Loc, diag::warn_objc_array_of_interfaces) << T; } diff --git a/test/Sema/array-constraint.c b/test/Sema/array-constraint.c index d35b0acdfe..06b5d747ab 100644 --- a/test/Sema/array-constraint.c +++ b/test/Sema/array-constraint.c @@ -20,7 +20,7 @@ struct vari { int b[]; }; -struct vari *func(struct vari a[]) { // expected-error {{'struct vari' may not be used as an array element due to flexible array member}} +struct vari *func(struct vari a[]) { // expected-warning {{'struct vari' may not be used as an array element due to flexible array member}} return a; } diff --git a/test/Sema/flexible-array-init.c b/test/Sema/flexible-array-init.c index 99ef66abe9..909b856d17 100644 --- a/test/Sema/flexible-array-init.c +++ b/test/Sema/flexible-array-init.c @@ -1,4 +1,4 @@ -// RUN: clang -fsyntax-only -verify %s +// RUN: clang -fsyntax-only -pedantic -verify %s struct one { int a; int values[]; @@ -12,17 +12,21 @@ void test() { struct foo { int x; - int y[]; // expected-note 3 {{initialized flexible array member 'y' is here}} + int y[]; // expected-note 4 {{initialized flexible array member 'y' is here}} }; -struct bar { struct foo z; }; +struct bar { struct foo z; }; // expected-warning {{'z' may not be nested in a struct due to flexible array member}} struct foo a = { 1, { 2, 3, 4 } }; // Valid. struct bar b = { { 1, { 2, 3, 4 } } }; // expected-error{{non-empty initialization of flexible array member inside subobject}} -struct bar c = { { 1, { } } }; // Valid. -struct foo d[1] = { { 1, { 2, 3, 4 } } }; // expected-error{{'struct foo' may not be used as an array element due to flexible array member}} +struct bar c = { { 1, { } } }; // Valid. \ + // expected-warning{{use of GNU empty initializer extension}} \ + // expected-warning{{zero size arrays are an extension}} +struct foo d[1] = { { 1, { 2, 3, 4 } } }; // expected-warning{{'struct foo' may not be used as an array element due to flexible array member}} \ + // expected-error{{non-empty initialization of flexible array member inside subobject}} struct foo desig_foo = { .y = {2, 3, 4} }; -struct bar desig_bar = { .z.y = { } }; +struct bar desig_bar = { .z.y = { } }; // expected-warning{{use of GNU empty initializer extension}} \ + // expected-warning{{zero size arrays are an extension}} struct bar desig_bar2 = { .z.y = { 2, 3, 4} }; // expected-error{{non-empty initialization of flexible array member inside subobject}} struct foo design_foo2 = { .y = 2 }; // expected-error{{flexible array requires brace-enclosed initializer}} @@ -36,3 +40,19 @@ struct polygon { }; struct polygon poly = { .points[2] = { 1, 2} }; // expected-error{{designator into flexible array member subobject}} + +// PR3540 +struct X { + int a; + int b; + char data[]; +}; + +struct Y { + int a:4; + int b:4; + int c; + int d; + int e; + struct X xs[]; // expected-warning{{'struct X' may not be used as an array element due to flexible array member}} +}; -- 2.40.0