Hi, Joseph,

I added the following testing cases for this purpose:

/* Support an untagged type as its own top-level type.  */

/* A. Variable declaration.  */
struct { int a; char b[] __attribute__ ((counted_by (a))); } *x;
struct { char b1[] __attribute__ ((counted_by (a1))); } *x1; /* { dg-error 
"attribute is not a field declaration in the same structure as" } */

struct { char *e __attribute__ ((counted_by (f))); int f; } *x2;
struct { char *e1 __attribute__ ((counted_by (f1))); } *x3; /* { dg-error 
"attribute is not a field declaration in the same structure as" } */

/* B. Function declaration.  */
struct { int c; char d[] __attribute__ ((counted_by (c))); } *func (int a, int 
b);
struct { char d1[] __attribute__ ((counted_by (c1))); } *func1 (int a, int b); 
/* { dg-error "attribute is not a field declaration in the same structure as" } 
*/

/* C. Parameter declaration.  */
//void func2 (struct { float *f __attribute__ ((counted_by (g))); int g; } *pa, 
int count);
//void func3 (struct { float *f1 __attribute__ ((counted_by (g1)));} *pa, int 
count);  

/* D. Typename.  */
int foo ()
{
  int res = sizeof (struct {int count; char *p __attribute__ ((counted_by 
(count))); });

  res += alignof (struct {char *p1 __attribute__ ((counted_by (count1))); }); 
/* { dg-error "attribute is not a field declaration in the same structure as" } 
*/
  return res;
}

/* Support an unnamed field with a named struct/union.  */
struct s { struct { int a; char b[] __attribute__ ((counted_by (a))); } *x; } 
*y;
struct s1 { struct { char c[] __attribute__ ((counted_by (d))); } *x; } *yy; /* 
{ dg-error "attribute is not a field declaration in the same structure as" } */

struct s2 { struct { char *b __attribute__ ((counted_by (a))); int a; } *x; } 
*y2;
struct s3 { struct { char *c __attribute__ ((counted_by (d))); } *x; } *y3; /* 
{ dg-error "attribute is not a field declaration in the same structure as" } */


I have the following questions need your help:

1.  I cannot come up with a valid testing case for “c_parser_enum_specifier” 
when calling “finish_declspecs”, 
    I am wondering whether such testing cases existing? 
    Shall I skip checking counted_by after “finish_declspecs” inside 
“c_parser_enum_specifer”?


2. In the above C. Parameter declaration, I got warnings:
 warning: anonymous struct declared inside parameter list will not be visible 
outside of this definition or declaration
   My question, is such usage valid in GNU C?

3. Do you think that the above testing case is enough for this purpose?

Thanks a lot for your help.

Qing



> On Dec 8, 2025, at 18:18, Joseph Myers <[email protected]> wrote:
> 
> On Fri, 5 Dec 2025, Qing Zhao wrote:
> 
>>> On Dec 4, 2025, at 18:43, Joseph Myers <[email protected]> wrote:
>>> 
>>> On Thu, 4 Dec 2025, Qing Zhao wrote:
>>> 
>>>> However, where should we call the “verify_counted_by_attribute” for the 
>>>> above cases?
>>>> 
>>>> Looks like that inside “finish_struct” is not the correct place to do 
>>>> this check since at that time we don’t know whether this structure will 
>>>> be used on its own.
>>>> 
>>>> Only when we are sure that this anonymous structure is used on its own, 
>>>> we should call “verify_counted_by_attribute” for it.
>>>> 
>>>> So, where in C FE we should check for this? Any suggestions?
>>> 
>>> Maybe go through all the places that process declaration specifiers.
>> 
>> Inside the routine “grokdeclarator”, when call “build_decl” for 
>> VAR_DECL, checking whether the type of the decl is an anonymous 
>> struct/union, if so, call “verify_counted_by_attribute” for this type.
> 
> I'm thinking in terms of: declaration specifiers should always go through 
> finish_declspecs.  In most places that go through finish_declspecs, we 
> know from the syntactic context that, if the declaration specifiers define 
> a structure or union (the ctsk_tagdef case), it must be valid on its own 
> (it's not a case of the anonymous struct/union feature) - or, in some 
> cases, a struct/union definition isn't actually valid in that context.  
> In the case of the call from c_parser_struct_declaration, we don't yet 
> know whether it's a case of the anonymous struct/union feature; if it is, 
> that will be determined inside the grokfield call.
> 
> That doesn't mean the checking logic needs to be tied to the exactly the 
> places that call finish_declspecs - but each finish_declspecs call ought 
> to end up being followed, in the ctsk_tagdef case, by a check for validity 
> of counted_by attributes in the struct/union being defined, except in the 
> anonymous struct/union case.  And there ought to be testcases that ensure 
> these checks do get carried out for syntax leading to each of those 
> finish_declspecs calls.
> 
> It's also OK to carry out some of the checks earlier if you know earlier 
> that they are needed (for example, in the case of a named structure or 
> union).
> 
> -- 
> Joseph S. Myers
> [email protected]


Reply via email to