Hi, Jakub and Joseph:

> On Jun 28, 2022, at 12:43 PM, Jakub Jelinek <ja...@redhat.com> wrote:
> 
> On Tue, Jun 28, 2022 at 03:59:22PM +0000, Qing Zhao via Gcc-patches wrote:
>>> On Jun 28, 2022, at 11:08 AM, Jakub Jelinek <ja...@redhat.com> wrote:
>>> 
>>> On Tue, Jun 28, 2022 at 03:03:12PM +0000, Qing Zhao wrote:
>>>> 2. Then replace all “array_at_struct_end_p” with using DECL_NOT_FLEXARRAY 
>>>> in GCC, adding new testing cases
>>> 
>>> No, IMHO array_at_struct_end_p should stay as is, just test this extra flag
>>> too.
>> 
>> Could you please explain why we still need “array_at_struct_end_p” after we 
>> have the DECL_NOT_FLEXARRAY flag in FIELD_DECL?
> 
> Because the flag just tells whether some array shouldn't be treated as (poor 
> man's)
> flexible array member.  We still need to find out if some FIELD_DECL is to
> be treated like a flexible array member, which is a minority of
> COMPONENT_REFs.
> struct S { int a; char b[0]; int c; } s;
> struct T { int d; char e[]; };
> struct U { int f; struct T g; int h; } u;
> Neither s.b nor u.g.e is to be treated like flexible array member,
> no matter what -fstrict-flex-array= option is used.

I studied the above a little bit today with the following small testing cases:
[opc@qinzhao-ol8u3-x86 trailing_array]$ cat t1.c
struct AX
{
  int n;
  short ax[];
  int m;
};

void warn_ax_local (struct AX *p)
{
  p->ax[2] = 0;   
}
[opc@qinzhao-ol8u3-x86 trailing_array]$ /home/opc/Install/latest/bin/gcc -O2 
-Wall  t1.c -S
t4.c:4:9: error: flexible array member not at end of struct
    4 |   short ax[];
      |         ^~

Looks like that it’s an error when the flexible array member is not at end of a 
struct.

However, for

[opc@qinzhao-ol8u3-x86 trailing_array]$ cat t2.c
struct AX
{
  int n;
  short ax[];
};

struct UX
{
  struct AX b;
  int m;
};

void warn_ax_local (struct AX *p, struct UX *q)
{
  p->ax[2] = 0;   
  q->b.ax[2] = 0;
}

[opc@qinzhao-ol8u3-x86 trailing_array]$  /home/opc/Install/latest/bin/gcc -O2 
-Wall  t2.c -S
[opc@qinzhao-ol8u3-x86 trailing_array]$ 


My impression is:

C frontend is able to check whether the field is at the end of the structure 
and report error when it detect a flexible array member is not at the end of 
struct.

My question is:

1. Is the usage of flexible array member in struct UX of t2.c correct?
2. Why it’s correct?
3. If not correct, is this a bug in FE?

Thanks.

Qing


Reply via email to