Re: [V5][PATCH 2/2] Update documentation to clarify a GCC extension

2023-03-27 Thread Qing Zhao via Gcc-patches


> On Mar 27, 2023, at 12:48 PM, Qing Zhao via Gcc-patches 
>  wrote:
> 
> 
> 
>> On Mar 27, 2023, at 12:31 PM, Jakub Jelinek  wrote:
>> 
>> On Mon, Mar 27, 2023 at 04:22:25PM +, Qing Zhao via Gcc-patches wrote:
 The latter IMHO.  Having a warning with completely nonsensical name will
 just confuse users.
>>> 
>>> Okay. -:)
>>> How about "-Wstruct-with-fam-not-at-end”?  Or do you have any suggestion on 
>>> the name?
>> 
>> Nobody will know what fam is.
> 
> Yes, I agree -:)
> 
>> -Wflex-array-member-not-at-end ?
> 
> However, Will this name include “a structure with flexible array member is 
> not at end”?  

Looks like no better name than “-Wflex-array-member-not-at-end” as I can think 
of..

I will use this one.

Let me know if you have any further comments on the documentation part.

thanks.

Qing
> Qing
> 
>> 
>>  Jakub
>> 
> 



Re: [V5][PATCH 2/2] Update documentation to clarify a GCC extension

2023-03-27 Thread Qing Zhao via Gcc-patches


> On Mar 27, 2023, at 12:31 PM, Jakub Jelinek  wrote:
> 
> On Mon, Mar 27, 2023 at 04:22:25PM +, Qing Zhao via Gcc-patches wrote:
>>> The latter IMHO.  Having a warning with completely nonsensical name will
>>> just confuse users.
>> 
>> Okay. -:)
>> How about "-Wstruct-with-fam-not-at-end”?  Or do you have any suggestion on 
>> the name?
> 
> Nobody will know what fam is.

Yes, I agree -:)

> -Wflex-array-member-not-at-end ?

However, Will this name include “a structure with flexible array member is not 
at end”?  
Qing

> 
>   Jakub
> 



Re: [V5][PATCH 2/2] Update documentation to clarify a GCC extension

2023-03-27 Thread Jakub Jelinek via Gcc-patches
On Mon, Mar 27, 2023 at 04:22:25PM +, Qing Zhao via Gcc-patches wrote:
> > The latter IMHO.  Having a warning with completely nonsensical name will
> > just confuse users.
> 
> Okay. -:)
> How about "-Wstruct-with-fam-not-at-end”?  Or do you have any suggestion on 
> the name?

Nobody will know what fam is.
-Wflex-array-member-not-at-end ?

Jakub



Re: [V5][PATCH 2/2] Update documentation to clarify a GCC extension

2023-03-27 Thread Qing Zhao via Gcc-patches


> On Mar 27, 2023, at 12:06 PM, Jakub Jelinek  wrote:
> 
> On Mon, Mar 27, 2023 at 03:57:58PM +, Qing Zhao wrote:
 +Please use warning option  @option{-Wgnu-variable-sized-type-not-at-end} 
 to
>>> This is certainly misnamed.
>> 
>> The name “-Wgnu-variable-sized-type-not-at-end” was just used the warning 
>> name from CLANG. -:)
>> 
>> Shall we use the same name as CLANG? Or we invent a new name?
> 
> The latter IMHO.  Having a warning with completely nonsensical name will
> just confuse users.

Okay. -:)
How about "-Wstruct-with-fam-not-at-end”?  Or do you have any suggestion on the 
name?
> 
>>> GNU variable sized type not at the end of a
>>> struct is something like
>>> void bar (void *);
>>> void foo (int n) {
>>> struct S { int a; int b[n]; int c; } s;
>>> s.a = 1;
>>> __builtin_memset (s.b, 0, sizeof (s.b));
>>> s.c = 3;
>>> bar (&s);
>>> }
>>> Certainly not flexible array members in the middle of structure.
>> 
>> Right now, with -Wpedantic, we have the following warning for the above 
>> small case:
>> 
>> t2.c:3:24: warning: a member of a structure or union cannot have a variably 
>> modified type [-Wpedantic]
>>3 |  struct S { int a; int b[n]; int c; } s;
>>  |^
> 
> Sure, it is a GNU C extension (not allowed in C++ BTW).
> It is documented in https://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html
> though just very briefly:
> As an extension, GCC accepts variable-length arrays as a member of a 
> structure or a union. For example: 
> void
> foo (int n)
> {
>  struct S { int x[n]; };
> }

Okay, I see. 
> 
>> Do we have a definition for “GNU variable sized type” now?
> 
> Naturally, variable sized type should have non-constant sizeof, because
> otherwise it is constant sized type.

Oh, for flexible array members, we cannot take sizeof it, So they are 
considered as incomplete type, right?

thanks.

Qing
>  That is not
> the case for flexible array members, there is nothing variable sized on
> them, especially if they are in the middle of a structure.
> 
>   Jakub
> 



Re: [V5][PATCH 2/2] Update documentation to clarify a GCC extension

2023-03-27 Thread Jakub Jelinek via Gcc-patches
On Mon, Mar 27, 2023 at 03:57:58PM +, Qing Zhao wrote:
> >> +Please use warning option  @option{-Wgnu-variable-sized-type-not-at-end} 
> >> to
> > This is certainly misnamed.
> 
> The name “-Wgnu-variable-sized-type-not-at-end” was just used the warning 
> name from CLANG. -:)
> 
> Shall we use the same name as CLANG? Or we invent a new name?

The latter IMHO.  Having a warning with completely nonsensical name will
just confuse users.

> >  GNU variable sized type not at the end of a
> > struct is something like
> > void bar (void *);
> > void foo (int n) {
> >  struct S { int a; int b[n]; int c; } s;
> >  s.a = 1;
> >  __builtin_memset (s.b, 0, sizeof (s.b));
> >  s.c = 3;
> >  bar (&s);
> > }
> > Certainly not flexible array members in the middle of structure.
> 
> Right now, with -Wpedantic, we have the following warning for the above small 
> case:
> 
> t2.c:3:24: warning: a member of a structure or union cannot have a variably 
> modified type [-Wpedantic]
> 3 |  struct S { int a; int b[n]; int c; } s;
>   |^

Sure, it is a GNU C extension (not allowed in C++ BTW).
It is documented in https://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html
though just very briefly:
As an extension, GCC accepts variable-length arrays as a member of a structure 
or a union. For example: 
void
foo (int n)
{
  struct S { int x[n]; };
}

> Do we have a definition for “GNU variable sized type” now?

Naturally, variable sized type should have non-constant sizeof, because
otherwise it is constant sized type.  That is not
the case for flexible array members, there is nothing variable sized on
them, especially if they are in the middle of a structure.

Jakub



Re: [V5][PATCH 2/2] Update documentation to clarify a GCC extension

2023-03-27 Thread Qing Zhao via Gcc-patches


> On Mar 27, 2023, at 11:43 AM, Jakub Jelinek  wrote:
> 
> On Mon, Mar 27, 2023 at 01:38:34PM +, Qing Zhao wrote:
>> 
>> 
>>> On Mar 23, 2023, at 4:14 PM, Joseph Myers  wrote:
>>> 
>>> On Thu, 23 Mar 2023, Qing Zhao via Gcc-patches wrote:
>>> 
 +Wgnu-variable-sized-type-not-at-end
 +C C++ Var(warn_variable_sized_type_not_at_end) Warning
 +Warn about structures or unions with C99 flexible array members are not
 +at the end of a structure.
>>> 
>>> I think there's at least one word missing here, e.g. "that" before "are".
>> 
>> Will fix it.
>>> 
 +Please use warning option  @option{-Wgnu-variable-sized-type-not-at-end} 
 to
 +identify all such cases in the source code and modify them.  This 
 extension
 +will be deprecated from gcc in the next release.
>>> 
>>> We don't generally say "in the next release" in the manual (or "deprecated 
>>> from gcc").  Maybe it *is* deprecated, maybe it will be *removed*, or will 
>>> *start to warn by default*, in some specified version number (giving a 
>>> version number seems better than "next release"), but "will be deprecated" 
>>> is odd.
>> How about the following:
>> 
>> +Please use warning option  @option{-Wgnu-variable-sized-type-not-at-end} to
> This is certainly misnamed.

The name “-Wgnu-variable-sized-type-not-at-end” was just used the warning name 
from CLANG. -:)

Shall we use the same name as CLANG? Or we invent a new name?

>  GNU variable sized type not at the end of a
> struct is something like
> void bar (void *);
> void foo (int n) {
>  struct S { int a; int b[n]; int c; } s;
>  s.a = 1;
>  __builtin_memset (s.b, 0, sizeof (s.b));
>  s.c = 3;
>  bar (&s);
> }
> Certainly not flexible array members in the middle of structure.

Right now, with -Wpedantic, we have the following warning for the above small 
case:

t2.c:3:24: warning: a member of a structure or union cannot have a variably 
modified type [-Wpedantic]
3 |  struct S { int a; int b[n]; int c; } s;
  |^


Do we have a definition for “GNU variable sized type” now?
Shall we include "flexible array members” and" the structure/union with a 
flexible array members at the end" into “GNU variable sized type”?

thanks.

Qing
> 
>> +identify all such cases in the source code and modify them.  This warning 
>> will be 
>> + on by default starting from GCC14.
> 
>   Jakub



Re: [V5][PATCH 2/2] Update documentation to clarify a GCC extension

2023-03-27 Thread Jakub Jelinek via Gcc-patches
On Mon, Mar 27, 2023 at 01:38:34PM +, Qing Zhao wrote:
> 
> 
> > On Mar 23, 2023, at 4:14 PM, Joseph Myers  wrote:
> > 
> > On Thu, 23 Mar 2023, Qing Zhao via Gcc-patches wrote:
> > 
> >> +Wgnu-variable-sized-type-not-at-end
> >> +C C++ Var(warn_variable_sized_type_not_at_end) Warning
> >> +Warn about structures or unions with C99 flexible array members are not
> >> +at the end of a structure.
> > 
> > I think there's at least one word missing here, e.g. "that" before "are".
> 
> Will fix it.
> > 
> >> +Please use warning option  @option{-Wgnu-variable-sized-type-not-at-end} 
> >> to
> >> +identify all such cases in the source code and modify them.  This 
> >> extension
> >> +will be deprecated from gcc in the next release.
> > 
> > We don't generally say "in the next release" in the manual (or "deprecated 
> > from gcc").  Maybe it *is* deprecated, maybe it will be *removed*, or will 
> > *start to warn by default*, in some specified version number (giving a 
> > version number seems better than "next release"), but "will be deprecated" 
> > is odd.
> How about the following:
> 
> +Please use warning option  @option{-Wgnu-variable-sized-type-not-at-end} to

This is certainly misnamed.  GNU variable sized type not at the end of a
struct is something like
void bar (void *);
void foo (int n) {
  struct S { int a; int b[n]; int c; } s;
  s.a = 1;
  __builtin_memset (s.b, 0, sizeof (s.b));
  s.c = 3;
  bar (&s);
}
Certainly not flexible array members in the middle of structure.

> +identify all such cases in the source code and modify them.  This warning 
> will be 
> + on by default starting from GCC14.

Jakub



Re: [V5][PATCH 2/2] Update documentation to clarify a GCC extension

2023-03-27 Thread Qing Zhao via Gcc-patches


> On Mar 27, 2023, at 10:34 AM, Xi Ruoyao  wrote:
> 
> On Mon, 2023-03-27 at 13:38 +, Qing Zhao via Gcc-patches wrote:
>> 
>> 
>>> On Mar 23, 2023, at 4:14 PM, Joseph Myers  wrote:
>>> 
>>> On Thu, 23 Mar 2023, Qing Zhao via Gcc-patches wrote:
>>> 
 +Wgnu-variable-sized-type-not-at-end
 +C C++ Var(warn_variable_sized_type_not_at_end) Warning
 +Warn about structures or unions with C99 flexible array members are not
 +at the end of a structure.
>>> 
>>> I think there's at least one word missing here, e.g. "that" before "are".
>> 
>> Will fix it.
>>> 
 +Please use warning option  @option{-Wgnu-variable-sized-type-not-at-end} 
 to
 +identify all such cases in the source code and modify them.  This 
 extension
 +will be deprecated from gcc in the next release.
>>> 
>>> We don't generally say "in the next release" in the manual (or "deprecated 
>>> from gcc").  Maybe it *is* deprecated, maybe it will be *removed*, or will 
>>> *start to warn by default*, in some specified version number (giving a 
>>> version number seems better than "next release"), but "will be deprecated" 
>>> is odd.
>> How about the following:
>> 
>> +Please use warning option  @option{-Wgnu-variable-sized-type-not-at-end} to
>> +identify all such cases in the source code and modify them.  This warning 
>> will be 
>> + on by default starting from GCC14.
> 
> I'm wondering why it *was" not on by default... 

This is a new warning that will be added to gcc13, since it’s in a very late 
stage before gcc13 release,
So I am not feeling comfortable to turn it on by default now. 
I think it might be safer to turn it on by default in the beginning of gcc14.

Qing
> 
> 
> -- 
> Xi Ruoyao 
> School of Aerospace Science and Technology, Xidian University



Re: [V5][PATCH 2/2] Update documentation to clarify a GCC extension

2023-03-27 Thread Xi Ruoyao via Gcc-patches
On Mon, 2023-03-27 at 13:38 +, Qing Zhao via Gcc-patches wrote:
> 
> 
> > On Mar 23, 2023, at 4:14 PM, Joseph Myers  wrote:
> > 
> > On Thu, 23 Mar 2023, Qing Zhao via Gcc-patches wrote:
> > 
> > > +Wgnu-variable-sized-type-not-at-end
> > > +C C++ Var(warn_variable_sized_type_not_at_end) Warning
> > > +Warn about structures or unions with C99 flexible array members are not
> > > +at the end of a structure.
> > 
> > I think there's at least one word missing here, e.g. "that" before "are".
> 
> Will fix it.
> > 
> > > +Please use warning option  @option{-Wgnu-variable-sized-type-not-at-end} 
> > > to
> > > +identify all such cases in the source code and modify them.  This 
> > > extension
> > > +will be deprecated from gcc in the next release.
> > 
> > We don't generally say "in the next release" in the manual (or "deprecated 
> > from gcc").  Maybe it *is* deprecated, maybe it will be *removed*, or will 
> > *start to warn by default*, in some specified version number (giving a 
> > version number seems better than "next release"), but "will be deprecated" 
> > is odd.
> How about the following:
> 
> +Please use warning option  @option{-Wgnu-variable-sized-type-not-at-end} to
> +identify all such cases in the source code and modify them.  This warning 
> will be 
> + on by default starting from GCC14.

I'm wondering why it *was" not on by default... 


-- 
Xi Ruoyao 
School of Aerospace Science and Technology, Xidian University


Re: [V5][PATCH 2/2] Update documentation to clarify a GCC extension

2023-03-27 Thread Qing Zhao via Gcc-patches



> On Mar 23, 2023, at 4:14 PM, Joseph Myers  wrote:
> 
> On Thu, 23 Mar 2023, Qing Zhao via Gcc-patches wrote:
> 
>> +Wgnu-variable-sized-type-not-at-end
>> +C C++ Var(warn_variable_sized_type_not_at_end) Warning
>> +Warn about structures or unions with C99 flexible array members are not
>> +at the end of a structure.
> 
> I think there's at least one word missing here, e.g. "that" before "are".

Will fix it.
> 
>> +Please use warning option  @option{-Wgnu-variable-sized-type-not-at-end} to
>> +identify all such cases in the source code and modify them.  This extension
>> +will be deprecated from gcc in the next release.
> 
> We don't generally say "in the next release" in the manual (or "deprecated 
> from gcc").  Maybe it *is* deprecated, maybe it will be *removed*, or will 
> *start to warn by default*, in some specified version number (giving a 
> version number seems better than "next release"), but "will be deprecated" 
> is odd.
How about the following:

+Please use warning option  @option{-Wgnu-variable-sized-type-not-at-end} to
+identify all such cases in the source code and modify them.  This warning will 
be 
+ on by default starting from GCC14.

Thanks.

Qing

> 
> -- 
> Joseph S. Myers
> jos...@codesourcery.com



Re: Fwd: [V5][PATCH 2/2] Update documentation to clarify a GCC extension

2023-03-23 Thread Joseph Myers
On Thu, 23 Mar 2023, Qing Zhao via Gcc-patches wrote:

> +Wgnu-variable-sized-type-not-at-end
> +C C++ Var(warn_variable_sized_type_not_at_end) Warning
> +Warn about structures or unions with C99 flexible array members are not
> +at the end of a structure.

I think there's at least one word missing here, e.g. "that" before "are".

> +Please use warning option  @option{-Wgnu-variable-sized-type-not-at-end} to
> +identify all such cases in the source code and modify them.  This extension
> +will be deprecated from gcc in the next release.

We don't generally say "in the next release" in the manual (or "deprecated 
from gcc").  Maybe it *is* deprecated, maybe it will be *removed*, or will 
*start to warn by default*, in some specified version number (giving a 
version number seems better than "next release"), but "will be deprecated" 
is odd.

-- 
Joseph S. Myers
jos...@codesourcery.com


Fwd: [V5][PATCH 2/2] Update documentation to clarify a GCC extension

2023-03-23 Thread Qing Zhao via Gcc-patches
Ping…

Please let me know if you have any further comments on the patch.

thanks.

Qing

Begin forwarded message:

From: Qing Zhao mailto:qing.z...@oracle.com>>
Subject: [V5][PATCH 2/2] Update documentation to clarify a GCC extension
Date: March 16, 2023 at 5:47:15 PM EDT
To: jos...@codesourcery.com<mailto:jos...@codesourcery.com>, 
ja...@redhat.com<mailto:ja...@redhat.com>, 
san...@codesourcery.com<mailto:san...@codesourcery.com>
Cc: rguent...@suse.de<mailto:rguent...@suse.de>, 
siddh...@gotplt.org<mailto:siddh...@gotplt.org>, 
keesc...@chromium.org<mailto:keesc...@chromium.org>, 
gcc-patches@gcc.gnu.org<mailto:gcc-patches@gcc.gnu.org>, Qing Zhao 
mailto:qing.z...@oracle.com>>

on a structure with a C99 flexible array member being nested in
another structure. (PR77650)

"GCC extension accepts a structure containing an ISO C99 "flexible array
member", or a union containing such a structure (possibly recursively)
to be a member of a structure.

There are two situations:

  * A structure or a union with a C99 flexible array member is the last
field of another structure, for example:

 struct flex  { int length; char data[]; };
 union union_flex { int others; struct flex f; };

 struct out_flex_struct { int m; struct flex flex_data; };
 struct out_flex_union { int n; union union_flex flex_data; };

In the above, both 'out_flex_struct.flex_data.data[]' and
'out_flex_union.flex_data.f.data[]' are considered as flexible
arrays too.

  * A structure or a union with a C99 flexible array member is the
middle field of another structure, for example:

 struct flex  { int length; char data[]; };

 struct mid_flex { int m; struct flex flex_data; int n; };

In the above, 'mid_flex.flex_data.data[]' has undefined behavior.
Compilers do not handle such case consistently, Any code relying on
such case should be modified to ensure that flexible array members
only end up at the ends of structures.

Please use warning option '-Wgnu-variable-sized-type-not-at-end' to
identify all such cases in the source code and modify them.  This
extension will be deprecated from gcc in the next release.
"

gcc/c-family/ChangeLog:

* c.opt: New option -Wgnu-variable-sized-type-not-at-end.

gcc/c/ChangeLog:

* c-decl.cc<http://c-decl.cc> (finish_struct): Issue warnings for new option.

gcc/ChangeLog:

* doc/extend.texi: Document GCC extension on a structure containing
a flexible array member to be a member of another structure.

gcc/testsuite/ChangeLog:

* gcc.dg/variable-sized-type-flex-array.c: New test.
---
gcc/c-family/c.opt|  5 +++
gcc/c/c-decl.cc<http://c-decl.cc>   |  8 
gcc/doc/extend.texi   | 45 ++-
.../gcc.dg/variable-sized-type-flex-array.c   | 31 +
4 files changed, 88 insertions(+), 1 deletion(-)
create mode 100644 gcc/testsuite/gcc.dg/variable-sized-type-flex-array.c

diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt
index cddeece..660ac07f3d4 100644
--- a/gcc/c-family/c.opt
+++ b/gcc/c-family/c.opt
@@ -737,6 +737,11 @@ Wformat-truncation=
C ObjC C++ LTO ObjC++ Joined RejectNegative UInteger Var(warn_format_trunc) 
Warning LangEnabledBy(C ObjC C++ LTO ObjC++,Wformat=, warn_format >= 1, 0) 
IntegerRange(0, 2)
Warn about calls to snprintf and similar functions that truncate output.

+Wgnu-variable-sized-type-not-at-end
+C C++ Var(warn_variable_sized_type_not_at_end) Warning
+Warn about structures or unions with C99 flexible array members are not
+at the end of a structure.
+
Wif-not-aligned
C ObjC C++ ObjC++ Var(warn_if_not_aligned) Init(1) Warning
Warn when the field in a struct is not aligned.
diff --git a/gcc/c/c-decl.cc<http://c-decl.cc> 
b/gcc/c/c-decl.cc<http://c-decl.cc>
index 14c54809b9d..1632043a8ff 100644
--- a/gcc/c/c-decl.cc<http://c-decl.cc>
+++ b/gcc/c/c-decl.cc<http://c-decl.cc>
@@ -9269,6 +9269,14 @@ finish_struct (location_t loc, tree t, tree fieldlist, 
tree attributes,
TYPE_INCLUDE_FLEXARRAY (t)
 = is_last_field && TYPE_INCLUDE_FLEXARRAY (TREE_TYPE (x));

+  if (warn_variable_sized_type_not_at_end
+  && !is_last_field
+  && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x))
+  && TYPE_INCLUDE_FLEXARRAY (TREE_TYPE (x)))
+ warning_at (DECL_SOURCE_LOCATION (x),
+OPT_Wgnu_variable_sized_type_not_at_end,
+"variable sized type not at the end of a struct");
+
  if (DECL_NAME (x)
 || RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
saw_named_field = true;
diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi
index fd3745c5608..0928b962a60 100644
--- a/gcc/doc/extend.texi
+++ b/gcc/doc/extend.texi
@@ -1748,7 +1748,50 @@ Flexible array members may only appear as the last 
member of a
A structure containing a flexible a

[V5][PATCH 2/2] Update documentation to clarify a GCC extension

2023-03-16 Thread Qing Zhao via Gcc-patches
on a structure with a C99 flexible array member being nested in
another structure. (PR77650)

"GCC extension accepts a structure containing an ISO C99 "flexible array
member", or a union containing such a structure (possibly recursively)
to be a member of a structure.

 There are two situations:

   * A structure or a union with a C99 flexible array member is the last
 field of another structure, for example:

  struct flex  { int length; char data[]; };
  union union_flex { int others; struct flex f; };

  struct out_flex_struct { int m; struct flex flex_data; };
  struct out_flex_union { int n; union union_flex flex_data; };

 In the above, both 'out_flex_struct.flex_data.data[]' and
 'out_flex_union.flex_data.f.data[]' are considered as flexible
 arrays too.

   * A structure or a union with a C99 flexible array member is the
 middle field of another structure, for example:

  struct flex  { int length; char data[]; };

  struct mid_flex { int m; struct flex flex_data; int n; };

 In the above, 'mid_flex.flex_data.data[]' has undefined behavior.
 Compilers do not handle such case consistently, Any code relying on
 such case should be modified to ensure that flexible array members
 only end up at the ends of structures.

 Please use warning option '-Wgnu-variable-sized-type-not-at-end' to
 identify all such cases in the source code and modify them.  This
 extension will be deprecated from gcc in the next release.
"

gcc/c-family/ChangeLog:

* c.opt: New option -Wgnu-variable-sized-type-not-at-end.

gcc/c/ChangeLog:

* c-decl.cc (finish_struct): Issue warnings for new option.

gcc/ChangeLog:

* doc/extend.texi: Document GCC extension on a structure containing
a flexible array member to be a member of another structure.

gcc/testsuite/ChangeLog:

* gcc.dg/variable-sized-type-flex-array.c: New test.
---
 gcc/c-family/c.opt|  5 +++
 gcc/c/c-decl.cc   |  8 
 gcc/doc/extend.texi   | 45 ++-
 .../gcc.dg/variable-sized-type-flex-array.c   | 31 +
 4 files changed, 88 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/gcc.dg/variable-sized-type-flex-array.c

diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt
index cddeece..660ac07f3d4 100644
--- a/gcc/c-family/c.opt
+++ b/gcc/c-family/c.opt
@@ -737,6 +737,11 @@ Wformat-truncation=
 C ObjC C++ LTO ObjC++ Joined RejectNegative UInteger Var(warn_format_trunc) 
Warning LangEnabledBy(C ObjC C++ LTO ObjC++,Wformat=, warn_format >= 1, 0) 
IntegerRange(0, 2)
 Warn about calls to snprintf and similar functions that truncate output.
 
+Wgnu-variable-sized-type-not-at-end
+C C++ Var(warn_variable_sized_type_not_at_end) Warning
+Warn about structures or unions with C99 flexible array members are not
+at the end of a structure.
+
 Wif-not-aligned
 C ObjC C++ ObjC++ Var(warn_if_not_aligned) Init(1) Warning
 Warn when the field in a struct is not aligned.
diff --git a/gcc/c/c-decl.cc b/gcc/c/c-decl.cc
index 14c54809b9d..1632043a8ff 100644
--- a/gcc/c/c-decl.cc
+++ b/gcc/c/c-decl.cc
@@ -9269,6 +9269,14 @@ finish_struct (location_t loc, tree t, tree fieldlist, 
tree attributes,
TYPE_INCLUDE_FLEXARRAY (t)
  = is_last_field && TYPE_INCLUDE_FLEXARRAY (TREE_TYPE (x));
 
+  if (warn_variable_sized_type_not_at_end
+ && !is_last_field
+ && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x))
+ && TYPE_INCLUDE_FLEXARRAY (TREE_TYPE (x)))
+   warning_at (DECL_SOURCE_LOCATION (x),
+   OPT_Wgnu_variable_sized_type_not_at_end,
+   "variable sized type not at the end of a struct");
+
   if (DECL_NAME (x)
  || RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
saw_named_field = true;
diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi
index fd3745c5608..0928b962a60 100644
--- a/gcc/doc/extend.texi
+++ b/gcc/doc/extend.texi
@@ -1748,7 +1748,50 @@ Flexible array members may only appear as the last 
member of a
 A structure containing a flexible array member, or a union containing
 such a structure (possibly recursively), may not be a member of a
 structure or an element of an array.  (However, these uses are
-permitted by GCC as extensions.)
+permitted by GCC as extensions, see details below.)
+@end itemize
+
+GCC extension accepts a structure containing an ISO C99 @dfn{flexible array
+member}, or a union containing such a structure (possibly recursively)
+to be a member of a structure.
+
+There are two situations:
+
+@itemize @bullet
+@item
+A structure or a union with a C99 flexible array member is the last field
+of another structure, for example:
+
+@smallexample
+struct flex  @{ int length; char data[]; @};
+union union_flex @{ int others; struct flex f; @};
+
+struct out_flex_struct @{ int m; struct flex flex_data; @};
+struct out_flex_union @{ int n; uni