> On Nov 12, 2025, at 17:07, Joseph Myers <[email protected]> wrote:
> 
> On Wed, 12 Nov 2025, Qing Zhao wrote:
> 
>>> 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?)
> 
> You can't tell until some time after the definition is complete, because 
> you need to see whether the type is used as the type of an unnamed field 
> (possibly with qualifiers).
> 
> Maybe this suggests that, as in other cases, an anonymous structure or 
> union involving counted_by should actually be required to be valid without 
> reference to the containing struct or union after all -

So, you mean, we should not support the following case: (requested by PR122495 
and PR122496):

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=122495
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=122496

struct fam_in_union {
  int count;
  union {
    char a;
    char fam[] __counted_by(count);
  };
};

Since the “counted_by” field, “count”,  that is referenced inside the anonymous 
union is declared
in the outer containing structure.
??

I am okay with this. not sure whether Linux Kernel need to support such case or 
not?
Kees and Guetavo, what’s your opinion on this?

> that's how things 
> generally work with anonymous structs and unions.  That is, it would be OK 
> for an outer struct or union to have counted_by pointing to a count in an 
> inner anonymous struct or union, but not vice versa.

And at the same time, we should support the following case:

struct nested_annotated {
  struct {
    union {
      int b;
      float f;
    };
    int n;
  };
  char c[] __attribute__ ((counted_by (b)));
};

Yes, currently, we support such case without any issue.

Thanks a lot.

Qing
> 
> -- 
> Joseph S. Myers
> [email protected]


Reply via email to