[cfe-commits] r64239 - in /cfe/trunk: include/clang/Basic/DiagnosticSemaKinds.def lib/Sema/SemaType.cpp test/Sema/array-constraint.c test/Sema/flexible-array-init.c

Daniel Dunbar daniel at zuster.org
Tue Feb 10 14:47:01 PST 2009


Agh! Do we really need to accept this!?

What does f0 compute:
--
struct S0 {
  int a;
  int b[];
};

struct S1 {
  struct S0 x;
  struct S0 y;
};

int f0(struct S1 a) {
  return a.y.b[0];
}
--

I haven't studied this issue closely, but this seems to be a GNU
extension which defies having any reasonable semantics.

 - Daniel

On Tue, Feb 10, 2009 at 1:49 PM, Douglas Gregor <dgregor at apple.com> wrote:
> Author: dgregor
> Date: Tue Feb 10 15:49:46 2009
> New Revision: 64239
>
> URL: http://llvm.org/viewvc/llvm-project?rev=64239&view=rev
> Log:
> 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.
>
> Modified:
>    cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.def
>    cfe/trunk/lib/Sema/SemaType.cpp
>    cfe/trunk/test/Sema/array-constraint.c
>    cfe/trunk/test/Sema/flexible-array-init.c
>
> Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.def
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.def?rev=64239&r1=64238&r2=64239&view=diff
>
> ==============================================================================
> --- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.def (original)
> +++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.def Tue Feb 10 15:49:46 2009
> @@ -651,7 +651,7 @@
>      "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")
>
> Modified: cfe/trunk/lib/Sema/SemaType.cpp
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaType.cpp?rev=64239&r1=64238&r2=64239&view=diff
>
> ==============================================================================
> --- cfe/trunk/lib/Sema/SemaType.cpp (original)
> +++ cfe/trunk/lib/Sema/SemaType.cpp Tue Feb 10 15:49:46 2009
> @@ -399,12 +399,9 @@
>         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;
>       }
>
> Modified: cfe/trunk/test/Sema/array-constraint.c
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/array-constraint.c?rev=64239&r1=64238&r2=64239&view=diff
>
> ==============================================================================
> --- cfe/trunk/test/Sema/array-constraint.c (original)
> +++ cfe/trunk/test/Sema/array-constraint.c Tue Feb 10 15:49:46 2009
> @@ -20,7 +20,7 @@
>   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;
>  }
>
>
> Modified: cfe/trunk/test/Sema/flexible-array-init.c
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/flexible-array-init.c?rev=64239&r1=64238&r2=64239&view=diff
>
> ==============================================================================
> --- cfe/trunk/test/Sema/flexible-array-init.c (original)
> +++ cfe/trunk/test/Sema/flexible-array-init.c Tue Feb 10 15:49:46 2009
> @@ -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 @@
>
>  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 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}}
> +};
>
>
> _______________________________________________
> cfe-commits mailing list
> cfe-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits
>



More information about the cfe-commits mailing list