On Thu, 11 Dec 2025, Qing Zhao wrote:

> diff --git a/gcc/c/c-decl.cc b/gcc/c/c-decl.cc
> index 0a368e410e5..35a70414cff 100644
> --- a/gcc/c/c-decl.cc
> +++ b/gcc/c/c-decl.cc
> @@ -9083,6 +9083,11 @@ grokfield (location_t loc,
>                         width ? &width : NULL, decl_attrs, expr, NULL,
>                         DEPRECATED_NORMAL);
>  
> +  /* When this field has name, its type is a top level type, we should
> +     call verify_counted_by_for_top_anonymous_type.  */
> +  if (DECL_NAME (value) != NULL_TREE)
> +    verify_counted_by_for_top_anonymous_type (TREE_TYPE (value));

So this is using the type of the field, as opposed to the type from the 
declaration specifiers.

> +/* Caller should make sure the TYPE is a top-level type (i.e. not being
> +   nested in other structure/uniona). For such type, verify its counted_by
> +   if it (or its pointee type) is an anonymous structure/union.  */
> +
> +void
> +verify_counted_by_for_top_anonymous_type (tree type)
> +{
> +  tree checked_type = NULL_TREE;
> +  if (POINTER_TYPE_P (type) && RECORD_OR_UNION_TYPE_P (TREE_TYPE (type)))
> +    checked_type = TREE_TYPE (type);
> +  else if (RECORD_OR_UNION_TYPE_P (type))
> +    checked_type = type;

And this is expecting the type passed to be either a structure or union 
type, or a pointer thereto.

I'd expect this to miss cases where the named field is e.g. a pointer to 
pointer to structure or union without a tag.  Or pointer to function 
returning pointer to array of pointers to structure or union without a 
tag, etc.

On the other hand, it might duplicate checks in cases where the check has 
already occurred - for example, because the anonymous structure or union 
comes from typeof rather than a direct definition in the declaration 
specifiers.

I think you could make grokfield use declspecs->type rather than TREE_TYPE 
(value) to avoid the issue with pointers to pointers and similar, at which 
point verify_counted_by_for_top_anonymous_type should no longer need to 
special-case pointers itself.  And if the check in grokfield is 
conditional on declspecs->typespec_kind == ctsk_tagdef, that should avoid 
duplicate checks.  (Of course testcases need to be added for uses such as 
pointers to pointers and pointers to functions and arrays of pointers to 
structures.)

-- 
Joseph S. Myers
[email protected]

Reply via email to