Add a new option -fstrict-flex-arrays[=n] and new attribute strict_flex_array
authorQing Zhao <qing.zhao@oracle.com>
Fri, 7 Oct 2022 14:58:20 +0000 (14:58 +0000)
committerQing Zhao <qing.zhao@oracle.com>
Fri, 7 Oct 2022 17:44:08 +0000 (17:44 +0000)
commit1879e48f3d8595bc9e7f583bbd12df3c6f5c42dc
tree981a0bf7f72cf12425310915f8def6350def1a77
parent21e51a55e9714d14a6df0e8e83ae005eb19f0f02
Add a new option -fstrict-flex-arrays[=n] and new attribute strict_flex_array

Add the following new option -fstrict-flex-arrays[=n] and a corresponding
attribute strict_flex_array to GCC:

'-fstrict-flex-arrays'
     Control when to treat the trailing array of a structure as a flexible array
     member for the purpose of accessing the elements of such an array.
     The positive form is equivalent to '-fstrict-flex-arrays=3', which is the
     strictest.  A trailing array is treated as a flexible array member only when
     it declared as a flexible array member per C99 standard onwards.
     The negative form is equivalent to '-fstrict-flex-arrays=0', which is the
     least strict.  All trailing arrays of structures are treated as flexible
     array members.

'-fstrict-flex-arrays=LEVEL'
     Control when to treat the trailing array of a structure as a flexible array
     member for the purpose of accessing the elements of such an array.  The value
     of LEVEL controls the level of strictness

     The possible values of LEVEL are the same as for the
     'strict_flex_array' attribute (*note Variable Attributes::).

     You can control this behavior for a specific trailing array field
     of a structure by using the variable attribute 'strict_flex_array'
     attribute (*note Variable Attributes::).

'strict_flex_array (LEVEL)'
     The 'strict_flex_array' attribute should be attached to the trailing
     array field of a structure. It controls when to treat the trailing array
     field of a structure as a flexible array member for the purposes of accessing
     the elements of such an array. LEVEL must be an integer betwen 0 to 3.

     LEVEL=0 is the least strict level, all trailing arrays of
     structures are treated as flexible array members.  LEVEL=3 is the
     strictest level, only when the trailing array is declared as a
     flexible array member per C99 standard onwards ('[]'), it is
     treated as a flexible array member.

     There are two more levels in between 0 and 3, which are provided to
     support older codes that use GCC zero-length array extension
     ('[0]') or one-element array as flexible array members('[1]'): When
     LEVEL is 1, the trailing array is treated as a flexible array member
     when it is declared as either '[]', '[0]', or '[1]'; When
     LEVEL is 2, the trailing array is treated as a flexible array member
     when it is declared as either '[]', or '[0]'.

     This attribute can be used with or without the
     '-fstrict-flex-arrays'.  When both the attribute and the option
     present at the same time, the level of the strictness for the
     specific trailing array field is determined by the attribute.

gcc/c-family/ChangeLog:

* c-attribs.cc (handle_strict_flex_array_attribute): New function.
(c_common_attribute_table): New item for strict_flex_array.
* c.opt: (fstrict-flex-arrays): New option.
(fstrict-flex-arrays=): New option.

gcc/c/ChangeLog:

* c-decl.cc (flexible_array_member_type_p): New function.
(one_element_array_type_p): Likewise.
(zero_length_array_type_p): Likewise.
(add_flexible_array_elts_to_size): Call new utility
routine flexible_array_member_type_p.
(is_flexible_array_member_p): New function.
(finish_struct): Set the new DECL_NOT_FLEXARRAY flag.

gcc/cp/ChangeLog:

* module.cc (trees_out::core_bools): Stream out new bit
decl_not_flexarray.
(trees_in::core_bools): Stream in new bit decl_not_flexarray.

gcc/ChangeLog:

* doc/extend.texi: Document strict_flex_array attribute.
* doc/invoke.texi: Document -fstrict-flex-arrays[=n] option.
* print-tree.cc (print_node): Print new bit decl_not_flexarray.
* tree-core.h (struct tree_decl_common): New bit field
decl_not_flexarray.
* tree-streamer-in.cc (unpack_ts_decl_common_value_fields): Stream
in new bit decl_not_flexarray.
* tree-streamer-out.cc (pack_ts_decl_common_value_fields): Stream
out new bit decl_not_flexarray.
* tree.cc (array_at_struct_end_p): Update it with the new bit field
decl_not_flexarray.
* tree.h (DECL_NOT_FLEXARRAY): New flag.

gcc/testsuite/ChangeLog:

* g++.dg/strict-flex-array-1.C: New test.
* gcc.dg/strict-flex-array-1.c: New test.
14 files changed:
gcc/c-family/c-attribs.cc
gcc/c-family/c.opt
gcc/c/c-decl.cc
gcc/cp/module.cc
gcc/doc/extend.texi
gcc/doc/invoke.texi
gcc/print-tree.cc
gcc/testsuite/g++.dg/strict-flex-array-1.C [new file with mode: 0644]
gcc/testsuite/gcc.dg/strict-flex-array-1.c [new file with mode: 0644]
gcc/tree-core.h
gcc/tree-streamer-in.cc
gcc/tree-streamer-out.cc
gcc/tree.cc
gcc/tree.h