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]
