> On Dec 18, 2025, at 14:33, Joseph Myers <[email protected]> wrote:
>
> 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.
So, for the following:
struct s { struct { int a; char b[] __attribute__ ((counted_by (a))); } *x; }
*y;
When the routine “grokfield” is handling the field “x”, the declspecs->type
points to the anonymous structure,
But the TREE_TYPE(value) is the "pointer to the anonymous structure"?
>
>> +/* 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.
Okay, I just read the routine “grokdeclarator” in more details, and see that
the final type for the declarator is build from
declspecs->type + declarator->kind (which might be one of cdk_attrs,
cdk_array, cdk_function, or cdk_pointer)
So, checking “declspecs->type” directly will avoid to track POINTER_TYPE_P,
FUNCTION_TYPE, ARRAY_TYPE, etc.
Is this understanding correct?
> 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,
Okay, I see.
> at which
> point verify_counted_by_for_top_anonymous_type should no longer need to
> special-case pointers itself.
Yes, that’s right.
> And if the check in grokfield is
> conditional on declspecs->typespec_kind == ctsk_tagdef, that should avoid
> duplicate checks.
Thanks.
> (Of course testcases need to be added for uses such as
> pointers to pointers and pointers to functions and arrays of pointers to
> structures.)
Okay, will do.
Thanks a lot for your help on this.
Qing
>
> --
> Joseph S. Myers
> [email protected]
>