]> git.ipfire.org Git - thirdparty/gcc.git/commit
Update documentation to clarify a GCC extension [PR c/77650]
authorQing Zhao <qing.zhao@oracle.com>
Thu, 29 Jun 2023 17:07:26 +0000 (17:07 +0000)
committerQing Zhao <qing.zhao@oracle.com>
Thu, 29 Jun 2023 17:15:53 +0000 (17:15 +0000)
commit070a6bf0bdc6761ad77ac97404c98f00a7007d54
tree97c11d69ad70de17b6b42880b5372bcd248c7500
parent3967da96556163d78cbeafe342c16b820aa85799
Update documentation to clarify a GCC extension [PR c/77650]

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

"The 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 containing a C99 flexible array member, or a union
     containing such a structure, 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 containing a C99 flexible array member, or a union
     containing such a structure, is not the last 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, accessing a member of the array 'mid_flex.flex_data.data[]'
     might have undefined behavior.  Compilers do not handle such a case
     consistently, Any code relying on this case should be modified to ensure
     that flexible array members only end up at the ends of structures.

     Please use the warning option '-Wflex-array-member-not-at-end' to
     identify all such cases in the source code and modify them.  This extension
     is now deprecated.
"

PR c/77650

gcc/c-family/ChangeLog:

* c.opt: New option -Wflex-array-member-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
gcc/c/c-decl.cc
gcc/doc/extend.texi
gcc/testsuite/gcc.dg/variable-sized-type-flex-array.c [new file with mode: 0644]