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.
This commit is contained in:
Qing Zhao 2022-10-07 14:58:20 +00:00
parent 21e51a55e9
commit 1879e48f3d
14 changed files with 350 additions and 28 deletions

View file

@ -101,6 +101,8 @@ static tree handle_special_var_sec_attribute (tree *, tree, tree, int, bool *);
static tree handle_aligned_attribute (tree *, tree, tree, int, bool *);
static tree handle_warn_if_not_aligned_attribute (tree *, tree, tree,
int, bool *);
static tree handle_strict_flex_array_attribute (tree *, tree, tree,
int, bool *);
static tree handle_weak_attribute (tree *, tree, tree, int, bool *) ;
static tree handle_noplt_attribute (tree *, tree, tree, int, bool *) ;
static tree handle_alias_ifunc_attribute (bool, tree *, tree, tree, bool *);
@ -369,6 +371,8 @@ const struct attribute_spec c_common_attribute_table[] =
attr_aligned_exclusions },
{ "warn_if_not_aligned", 0, 1, false, false, false, false,
handle_warn_if_not_aligned_attribute, NULL },
{ "strict_flex_array", 1, 1, true, false, false, false,
handle_strict_flex_array_attribute, NULL },
{ "weak", 0, 0, true, false, false, false,
handle_weak_attribute, NULL },
{ "noplt", 0, 0, true, false, false, false,
@ -2508,6 +2512,49 @@ handle_warn_if_not_aligned_attribute (tree *node, tree name,
no_add_attrs, true);
}
/* Handle a "strict_flex_array" attribute; arguments as in
struct attribute_spec.handler. */
static tree
handle_strict_flex_array_attribute (tree *node, tree name,
tree args, int ARG_UNUSED (flags),
bool *no_add_attrs)
{
tree decl = *node;
tree argval = TREE_VALUE (args);
/* This attribute only applies to field decls of a structure. */
if (TREE_CODE (decl) != FIELD_DECL)
{
error_at (DECL_SOURCE_LOCATION (decl),
"%qE attribute may not be specified for %q+D", name, decl);
*no_add_attrs = true;
}
/* This attribute only applies to field with array type. */
else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
{
error_at (DECL_SOURCE_LOCATION (decl),
"%qE attribute may not be specified for a non-array field",
name);
*no_add_attrs = true;
}
else if (TREE_CODE (argval) != INTEGER_CST)
{
error_at (DECL_SOURCE_LOCATION (decl),
"%qE attribute argument not an integer", name);
*no_add_attrs = true;
}
else if (!tree_fits_uhwi_p (argval) || tree_to_uhwi (argval) > 3)
{
error_at (DECL_SOURCE_LOCATION (decl),
"%qE attribute argument %qE is not an integer constant"
" between 0 and 3", name, argval);
*no_add_attrs = true;
}
return NULL_TREE;
}
/* Handle a "weak" attribute; arguments as in
struct attribute_spec.handler. */

View file

@ -2076,6 +2076,13 @@ fsized-deallocation
C++ ObjC++ Var(flag_sized_deallocation) Init(-1)
Enable C++14 sized deallocation support.
fstrict-flex-arrays
C C++ Common Alias(fstrict-flex-arrays=,3,0)
fstrict-flex-arrays=
C C++ Common Joined RejectNegative UInteger Var(flag_strict_flex_arrays) Init(0) IntegerRange(0,3)
-fstrict-flex-arrays=<level> Control when to treat the trailing array of a structure as a flexible array member for the purposes of accessing the elements of such an array. The default is treating all trailing arrays of structures as flexible array members.
fsquangle
C++ ObjC++ WarnRemoved

View file

@ -5034,6 +5034,41 @@ set_array_declarator_inner (struct c_declarator *decl,
return decl;
}
/* Determine whether TYPE is a ISO C99 flexible array memeber type "[]". */
static bool
flexible_array_member_type_p (const_tree type)
{
if (TREE_CODE (type) == ARRAY_TYPE
&& TYPE_SIZE (type) == NULL_TREE
&& TYPE_DOMAIN (type) != NULL_TREE
&& TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE)
return true;
return false;
}
/* Determine whether TYPE is a one-element array type "[1]". */
static bool
one_element_array_type_p (const_tree type)
{
if (TREE_CODE (type) != ARRAY_TYPE)
return false;
return integer_zerop (array_type_nelts (type));
}
/* Determine whether TYPE is a zero-length array type "[0]". */
static bool
zero_length_array_type_p (const_tree type)
{
if (TREE_CODE (type) == ARRAY_TYPE)
if (tree type_size = TYPE_SIZE_UNIT (type))
if ((integer_zerop (type_size))
&& TYPE_DOMAIN (type) != NULL_TREE
&& TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE)
return true;
return false;
}
/* INIT is a constructor that forms DECL's initializer. If the final
element initializes a flexible array field, add the size of that
initializer to DECL's size. */
@ -5048,10 +5083,7 @@ add_flexible_array_elts_to_size (tree decl, tree init)
elt = CONSTRUCTOR_ELTS (init)->last ().value;
type = TREE_TYPE (elt);
if (TREE_CODE (type) == ARRAY_TYPE
&& TYPE_SIZE (type) == NULL_TREE
&& TYPE_DOMAIN (type) != NULL_TREE
&& TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE)
if (flexible_array_member_type_p (type))
{
complete_array_type (&type, elt, false);
DECL_SIZE (decl)
@ -8755,6 +8787,81 @@ finish_incomplete_vars (tree incomplete_vars, bool toplevel)
}
}
/* Determine whether the FIELD_DECL X is a flexible array member according to
the following info:
A. whether the FIELD_DECL X is the last field of the DECL_CONTEXT;
B. whether the FIELD_DECL is an array that is declared as "[]", "[0]",
or "[1]";
C. flag_strict_flex_arrays;
D. the attribute strict_flex_array that is attached to the field
if presenting.
Return TRUE when it's a flexible array member, FALSE otherwise. */
static bool
is_flexible_array_member_p (bool is_last_field,
tree x)
{
/* If not the last field, return false. */
if (!is_last_field)
return false;
/* If not an array field, return false. */
if (TREE_CODE (TREE_TYPE (x)) != ARRAY_TYPE)
return false;
bool is_zero_length_array = zero_length_array_type_p (TREE_TYPE (x));
bool is_one_element_array = one_element_array_type_p (TREE_TYPE (x));
bool is_flexible_array = flexible_array_member_type_p (TREE_TYPE (x));
unsigned int strict_flex_array_level = flag_strict_flex_arrays;
tree attr_strict_flex_array = lookup_attribute ("strict_flex_array",
DECL_ATTRIBUTES (x));
/* If there is a strict_flex_array attribute attached to the field,
override the flag_strict_flex_arrays. */
if (attr_strict_flex_array)
{
/* Get the value of the level first from the attribute. */
unsigned HOST_WIDE_INT attr_strict_flex_array_level = 0;
gcc_assert (TREE_VALUE (attr_strict_flex_array) != NULL_TREE);
attr_strict_flex_array = TREE_VALUE (attr_strict_flex_array);
gcc_assert (TREE_VALUE (attr_strict_flex_array) != NULL_TREE);
attr_strict_flex_array = TREE_VALUE (attr_strict_flex_array);
gcc_assert (tree_fits_uhwi_p (attr_strict_flex_array));
attr_strict_flex_array_level = tree_to_uhwi (attr_strict_flex_array);
/* The attribute has higher priority than flag_struct_flex_array. */
strict_flex_array_level = attr_strict_flex_array_level;
}
switch (strict_flex_array_level)
{
case 0:
/* Default, all trailing arrays are flexible array members. */
return true;
case 1:
/* Level 1: all "[1]", "[0]", and "[]" are flexible array members. */
if (is_one_element_array)
return true;
/* FALLTHROUGH. */
case 2:
/* Level 2: all "[0]", and "[]" are flexible array members. */
if (is_zero_length_array)
return true;
/* FALLTHROUGH. */
case 3:
/* Level 3: Only "[]" are flexible array members. */
if (is_flexible_array)
return true;
break;
default:
gcc_unreachable ();
}
return false;
}
/* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
LOC is the location of the RECORD_TYPE or UNION_TYPE's definition.
FIELDLIST is a chain of FIELD_DECL nodes for the fields.
@ -8816,6 +8923,11 @@ finish_struct (location_t loc, tree t, tree fieldlist, tree attributes,
bool saw_named_field = false;
for (x = fieldlist; x; x = DECL_CHAIN (x))
{
/* Whether this field is the last field of the structure or union.
for UNION, any field is the last field of it. */
bool is_last_field = (DECL_CHAIN (x) == NULL_TREE)
|| (TREE_CODE (t) == UNION_TYPE);
if (TREE_TYPE (x) == error_mark_node)
continue;
@ -8854,10 +8966,7 @@ finish_struct (location_t loc, tree t, tree fieldlist, tree attributes,
DECL_PACKED (x) = 1;
/* Detect flexible array member in an invalid context. */
if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
&& TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
&& TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
&& TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
if (flexible_array_member_type_p (TREE_TYPE (x)))
{
if (TREE_CODE (t) == UNION_TYPE)
{
@ -8865,7 +8974,7 @@ finish_struct (location_t loc, tree t, tree fieldlist, tree attributes,
"flexible array member in union");
TREE_TYPE (x) = error_mark_node;
}
else if (DECL_CHAIN (x) != NULL_TREE)
else if (!is_last_field)
{
error_at (DECL_SOURCE_LOCATION (x),
"flexible array member not at end of struct");
@ -8885,6 +8994,9 @@ finish_struct (location_t loc, tree t, tree fieldlist, tree attributes,
pedwarn (DECL_SOURCE_LOCATION (x), OPT_Wpedantic,
"invalid use of structure with flexible array member");
/* Set DECL_NOT_FLEXARRAY flag for FIELD_DECL x. */
DECL_NOT_FLEXARRAY (x) = !is_flexible_array_member_p (is_last_field, x);
if (DECL_NAME (x)
|| RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
saw_named_field = true;

View file

@ -5433,6 +5433,7 @@ trees_out::core_bools (tree t)
WB (t->decl_common.decl_by_reference_flag);
WB (t->decl_common.decl_read_flag);
WB (t->decl_common.decl_nonshareable_flag);
WB (t->decl_common.decl_not_flexarray);
}
if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
@ -5577,6 +5578,7 @@ trees_in::core_bools (tree t)
RB (t->decl_common.decl_by_reference_flag);
RB (t->decl_common.decl_read_flag);
RB (t->decl_common.decl_nonshareable_flag);
RB (t->decl_common.decl_not_flexarray);
}
if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))

View file

@ -7459,6 +7459,32 @@ This warning can be disabled by @option{-Wno-if-not-aligned}.
The @code{warn_if_not_aligned} attribute can also be used for types
(@pxref{Common Type Attributes}.)
@cindex @code{strict_flex_array} variable attribute
@item strict_flex_array (@var{level})
The @code{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.
@var{level} must be an integer betwen 0 to 3.
@var{level}=0 is the least strict level, all trailing arrays of structures
are treated as flexible array members. @var{level}=3 is the strictest level,
only when the trailing array is declared as a flexible array member per C99
standard onwards (@samp{[]}), 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 (@samp{[0]}) or one-element
array as flexible array members (@samp{[1]}):
When @var{level} is 1, the trailing array is treated as a flexible array member
when it is declared as either @samp{[]}, @samp{[0]}, or @samp{[1]};
When @var{level} is 2, the trailing array is treated as a flexible array member
when it is declared as either @samp{[]}, or @samp{[0]}.
This attribute can be used with or without the @option{-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.
@item alloc_size (@var{position})
@itemx alloc_size (@var{position-1}, @var{position-2})
@cindex @code{alloc_size} variable attribute

View file

@ -207,7 +207,8 @@ in the following sections.
-fopenmp -fopenmp-simd @gol
-fpermitted-flt-eval-methods=@var{standard} @gol
-fplan9-extensions -fsigned-bitfields -funsigned-bitfields @gol
-fsigned-char -funsigned-char -fsso-struct=@var{endianness}}
-fsigned-char -funsigned-char -fstrict-flex-arrays[=@var{n}] @gol
-fsso-struct=@var{endianness}}
@item C++ Language Options
@xref{C++ Dialect Options,,Options Controlling C++ Dialect}.
@ -2835,6 +2836,31 @@ The type @code{char} is always a distinct type from each of
@code{signed char} or @code{unsigned char}, even though its behavior
is always just like one of those two.
@item -fstrict-flex-arrays
@opindex fstrict-flex-arrays
@opindex fno-strict-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 @option{-fstrict-flex-arrays=3}, which is the
strictest. A trailing array is treated as a flexible array member only when it
is declared as a flexible array member per C99 standard onwards.
The negative form is equivalent to @option{-fstrict-flex-arrays=0}, which is the
least strict. All trailing arrays of structures are treated as flexible array
members.
@item -fstrict-flex-arrays=@var{level}
@opindex fstrict-flex-arrays=@var{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 @var{level} controls the level of strictness.
The possible values of @var{level} are the same as for the
@code{strict_flex_array} attribute (@pxref{Variable Attributes}).
You can control this behavior for a specific trailing array field of a
structure by using the variable attribute @code{strict_flex_array} attribute
(@pxref{Variable Attributes}).
@item -fsso-struct=@var{endianness}
@opindex fsso-struct
Set the default scalar storage order of structures and unions to the

View file

@ -517,8 +517,12 @@ print_node (FILE *file, const char *prefix, tree node, int indent,
fprintf (file, " align:%d warn_if_not_align:%d",
DECL_ALIGN (node), DECL_WARN_IF_NOT_ALIGN (node));
if (code == FIELD_DECL)
fprintf (file, " offset_align " HOST_WIDE_INT_PRINT_UNSIGNED,
DECL_OFFSET_ALIGN (node));
{
fprintf (file, " offset_align " HOST_WIDE_INT_PRINT_UNSIGNED,
DECL_OFFSET_ALIGN (node));
fprintf (file, " decl_not_flexarray: %d",
DECL_NOT_FLEXARRAY (node));
}
if (code == FUNCTION_DECL && fndecl_built_in_p (node))
{

View file

@ -0,0 +1,31 @@
/* testing the correct usage of attribute strict_flex_array. */
/* { dg-do compile } */
/* { dg-options "-O2" } */
int x __attribute__ ((strict_flex_array (1))); /* { dg-error "'strict_flex_array' attribute may not be specified for 'x'" } */
struct trailing {
int a;
int c __attribute ((strict_flex_array)); /* { dg-error "wrong number of arguments specified for 'strict_flex_array' attribute" } */
};
struct trailing_1 {
int a;
int b;
int c __attribute ((strict_flex_array (2))); /* { dg-error "'strict_flex_array' attribute may not be specified for a non-array field" } */
};
extern int d;
struct trailing_array_2 {
int a;
int b;
int c[1] __attribute ((strict_flex_array (d))); /* { dg-error "'strict_flex_array' attribute argument not an integer" } */
};
struct trailing_array_3 {
int a;
int b;
int c[0] __attribute ((strict_flex_array (5))); /* { dg-error "'strict_flex_array' attribute argument '5' is not an integer constant between 0 and 3" } */
};

View file

@ -0,0 +1,33 @@
/* testing the correct usage of attribute strict_flex_array. */
/* { dg-do compile } */
/* { dg-options "-O2" } */
int x __attribute__ ((strict_flex_array (1))); /* { dg-error "'strict_flex_array' attribute may not be specified for 'x'" } */
int [[gnu::strict_flex_array(1)]] x; /* { dg-warning "'strict_flex_array' attribute does not apply to types" } */
struct trailing {
int a;
int c __attribute ((strict_flex_array)); /* { dg-error "wrong number of arguments specified for 'strict_flex_array' attribute" } */
};
struct trailing_1 {
int a;
int b;
int c __attribute ((strict_flex_array (2))); /* { dg-error "'strict_flex_array' attribute may not be specified for a non-array field" } */
};
extern int d;
struct trailing_array_2 {
int a;
int b;
int c[1] __attribute ((strict_flex_array (d))); /* { dg-error "'strict_flex_array' attribute argument not an integer" } */
};
struct trailing_array_3 {
int a;
int b;
int c[0] __attribute ((strict_flex_array (5))); /* { dg-error "'strict_flex_array' attribute argument '5' is not an integer constant between 0 and 3" } */
};

View file

@ -1827,7 +1827,10 @@ struct GTY(()) tree_decl_common {
TYPE_WARN_IF_NOT_ALIGN. */
unsigned int warn_if_not_align : 6;
/* 14 bits unused. */
/* In FIELD_DECL, this is DECL_NOT_FLEXARRAY. */
unsigned int decl_not_flexarray : 1;
/* 13 bits unused. */
/* UID for points-to sets, stable over copying from inlining. */
unsigned int pt_uid;

View file

@ -261,6 +261,7 @@ unpack_ts_decl_common_value_fields (struct bitpack_d *bp, tree expr)
else
SET_DECL_FIELD_ABI_IGNORED (expr, val);
expr->decl_common.off_align = bp_unpack_value (bp, 8);
DECL_NOT_FLEXARRAY (expr) = (unsigned) bp_unpack_value (bp, 1);
}
else if (VAR_P (expr))

View file

@ -229,6 +229,7 @@ pack_ts_decl_common_value_fields (struct bitpack_d *bp, tree expr)
else
bp_pack_value (bp, DECL_FIELD_ABI_IGNORED (expr), 1);
bp_pack_value (bp, expr->decl_common.off_align, 8);
bp_pack_value (bp, DECL_NOT_FLEXARRAY (expr), 1);
}
else if (VAR_P (expr))

View file

@ -12691,14 +12691,30 @@ array_ref_up_bound (tree exp)
}
/* Returns true if REF is an array reference, component reference,
or memory reference to an array at the end of a structure.
If this is the case, the array may be allocated larger
than its upper bound implies. */
or memory reference to an array whose actual size might be larger
than its upper bound implies, there are multiple cases:
A. a ref to a flexible array member at the end of a structure;
B. a ref to an array with a different type against the original decl;
for example:
short a[16] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
(*((char(*)[16])&a[0]))[i+8]
C. a ref to an array that was passed as a parameter;
for example:
int test (uint8_t *p, uint32_t t[1][1], int n) {
for (int i = 0; i < 4; i++, p++)
t[i][0] = ...;
FIXME, the name of this routine need to be changed to be more accurate. */
bool
array_at_struct_end_p (tree ref)
{
tree atype;
/* the TYPE for this array referece. */
tree atype = NULL_TREE;
/* the FIELD_DECL for the array field in the containing structure. */
tree afield_decl = NULL_TREE;
if (TREE_CODE (ref) == ARRAY_REF
|| TREE_CODE (ref) == ARRAY_RANGE_REF)
@ -12708,7 +12724,10 @@ array_at_struct_end_p (tree ref)
}
else if (TREE_CODE (ref) == COMPONENT_REF
&& TREE_CODE (TREE_TYPE (TREE_OPERAND (ref, 1))) == ARRAY_TYPE)
atype = TREE_TYPE (TREE_OPERAND (ref, 1));
{
atype = TREE_TYPE (TREE_OPERAND (ref, 1));
afield_decl = TREE_OPERAND (ref, 1);
}
else if (TREE_CODE (ref) == MEM_REF)
{
tree arg = TREE_OPERAND (ref, 0);
@ -12720,6 +12739,7 @@ array_at_struct_end_p (tree ref)
if (tree fld = last_field (argtype))
{
atype = TREE_TYPE (fld);
afield_decl = fld;
if (TREE_CODE (atype) != ARRAY_TYPE)
return false;
if (VAR_P (arg) && DECL_SIZE (fld))
@ -12773,13 +12793,16 @@ array_at_struct_end_p (tree ref)
ref = TREE_OPERAND (ref, 0);
}
/* The array now is at struct end. Treat flexible arrays as
gcc_assert (!afield_decl
|| (afield_decl && TREE_CODE (afield_decl) == FIELD_DECL));
/* The array now is at struct end. Treat flexible array member as
always subject to extend, even into just padding constrained by
an underlying decl. */
if (! TYPE_SIZE (atype)
|| ! TYPE_DOMAIN (atype)
|| ! TYPE_MAX_VALUE (TYPE_DOMAIN (atype)))
return true;
return afield_decl ? !DECL_NOT_FLEXARRAY (afield_decl) : true;
/* If the reference is based on a declared entity, the size of the array
is constrained by its given domain. (Do not trust commons PR/69368). */
@ -12801,9 +12824,9 @@ array_at_struct_end_p (tree ref)
if (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (atype))) != INTEGER_CST
|| TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (atype))) != INTEGER_CST
|| TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (atype))) != INTEGER_CST)
return true;
return afield_decl ? !DECL_NOT_FLEXARRAY (afield_decl) : true;
if (! get_addr_base_and_unit_offset (ref_to_array, &offset))
return true;
return afield_decl ? !DECL_NOT_FLEXARRAY (afield_decl) : true;
/* If at least one extra element fits it is a flexarray. */
if (known_le ((wi::to_offset (TYPE_MAX_VALUE (TYPE_DOMAIN (atype)))
@ -12811,12 +12834,12 @@ array_at_struct_end_p (tree ref)
+ 2)
* wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (atype))),
wi::to_offset (DECL_SIZE_UNIT (ref)) - offset))
return true;
return afield_decl ? !DECL_NOT_FLEXARRAY (afield_decl) : true;
return false;
}
return true;
return afield_decl ? !DECL_NOT_FLEXARRAY (afield_decl) : true;
}
/* Return a tree representing the offset, in bytes, of the field referenced

View file

@ -3005,6 +3005,12 @@ extern void decl_value_expr_insert (tree, tree);
#define DECL_PADDING_P(NODE) \
(FIELD_DECL_CHECK (NODE)->decl_common.decl_flag_3)
/* Used in a FIELD_DECL to indicate whether this field is not a flexible
array member. This is only valid for the last array type field of a
structure. */
#define DECL_NOT_FLEXARRAY(NODE) \
(FIELD_DECL_CHECK (NODE)->decl_common.decl_not_flexarray)
/* A numeric unique identifier for a LABEL_DECL. The UID allocation is
dense, unique within any one function, and may be used to index arrays.
If the value is -1, then no UID has been assigned. */
@ -5547,10 +5553,10 @@ extern tree component_ref_field_offset (tree);
returns null. */
enum struct special_array_member
{
none, /* Not a special array member. */
int_0, /* Interior array member with size zero. */
trail_0, /* Trailing array member with size zero. */
trail_1 /* Trailing array member with one element. */
none, /* Not a special array member. */
int_0, /* Interior array member with size zero. */
trail_0, /* Trailing array member with size zero. */
trail_1 /* Trailing array member with one element. */
};
/* Return the size of the member referenced by the COMPONENT_REF, using