> On Nov 10, 2025, at 16:56, Joseph Myers <[email protected]> wrote:
>
> On Mon, 10 Nov 2025, Qing Zhao wrote:
>
>>> struct { int a; char b[] __attribute__ ((counted_by (a))); } *x;
>>
>> For such case, usually how to initialize the above x?
>> Since the anonymous structure has a FAM field, usually we need to use malloc
>> to initialize it, is this correct?
>> Or is there other way to initialize such variable?
>
> Yes, use malloc
>
>> If we have to use malloc to initialize, how to do it? (There is no structure
>> name, how can we get the size of
>> the structure?) one way to do it is to manually calculate the size of the
>> anonymous structure with use
>> malloc to initialize.
>
> The type can be accessed as typeof (*x).
>
Okay, I see.
>>> is a legitimate use of an unnamed structure type, where the counted_by
>>> checks should take place at the level of that type rather than expecting
>>> it to appear in a larger type. Similarly, if you name the field using a
>>> type without a tag,
>>>
>>> struct s { struct { int a; char b[] __attribute__ ((counted_by (a))); } x;
>>> };
>>
>> Again, for this case, how to initialize a variable with “stuct s”?
>
> If you need the type of the member x, again, it can be accessed with
> typeof.
Yeah, I see now. -:)
>
>>> This means the patch should include testcases (both valid, and invalid
>>> ones being diagnosed) with unnamed structure or union types used in
>>> contexts that *don't* insert their members into the namespace of some
>>> outer structure or union, and verify that the compiler behaves as expected
>>> on such cases (and I suspect the code based on TYPE_NAME will need
>>> changing to make it behave correctly).
>>
>> Does the above two cases you listed cover all such cases?
>
> There are the various extensions (-fms-extensions / -fplan9-extensions) to
> consider as well, but it might be reasonable to say that only genuinely
> anonymous struct / union members get the special counted_by handling,
So, in C FE, what’s the best way to determine whether the anonymous
struct/union is
genuine or not? (The DECL_CONTEXT of this anonymous structure/union is
NULL_TREE?)
> not
> members with no name but using a previously named type (only in the
> genuinely anonymous case is there no way to access the anonymous type of
> the anonymous member directly).
>
> --
> Joseph S. Myers
> [email protected]