[PATCH v2] c++: Check attributes on friend declarations [PR99032]

Marek Polacek polacek@redhat.com
Tue May 18 17:46:04 GMT 2021


On Tue, May 18, 2021 at 07:35:52PM +0200, Franz Sirl wrote:
> Am 2021-05-14 um 00:08 schrieb Marek Polacek via Gcc-patches:
> > On Wed, May 12, 2021 at 08:27:18PM -0400, Jason Merrill wrote:
> > > On 5/12/21 8:03 PM, Marek Polacek wrote:
> > > > diff --git a/gcc/cp/decl2.c b/gcc/cp/decl2.c
> > > > index 89f874a32cc..2bcefb619aa 100644
> > > > --- a/gcc/cp/decl2.c
> > > > +++ b/gcc/cp/decl2.c
> > > > @@ -1331,6 +1331,20 @@ any_dependent_type_attributes_p (tree attrs)
> > > >      return false;
> > > >    }
> > > > +/* True if ATTRS contains any attribute that requires a type.  */
> > > 
> > > Let's invert this to check if ATTRS contains any attribute that does *not*
> > > require a type, and would therefore apply to the decl.
> > 
> > Sounds good, done.  Now I don't need to check *attrlist.
> > I've also fixed up the xfail thing in my new test.
> > 
> > Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?
> > 
> > -- >8 --
> > This patch implements [dcl.attr.grammar]/5: "If an attribute-specifier-seq
> > appertains to a friend declaration ([class.friend]), that declaration shall
> > be a definition."
> > 
> > This restriction applies to C++11-style attributes as well as GNU
> > attributes with the exception that we allow GNU attributes that require
> > a type, such as vector_size to continue accepting code as in attrib63.C.
> > There are various forms of friend declarations, we have friend
> > templates, C++11 extended friend declarations, and so on.  In some cases
> > we already ignore the attribute and warn that it was ignored.  But
> > certain cases weren't diagnosed, and with this patch we'll give a hard
> > error.  I tried hard not to emit both a warning and error and I think it
> > worked out.
> > 
> > Jason provided the cp_parser_decl_specifier_seq hunk to detect using
> > standard attributes in the middle of decl-specifiers, which is invalid.
> > 
> > Co-authored-by: Jason Merrill <jason@redhat.com>
> > 
> > gcc/cp/ChangeLog:
> > 
> > 	PR c++/99032
> > 	* cp-tree.h (any_non_type_attribute_p): Declare.
> > 	* decl.c (grokdeclarator): Diagnose when an attribute appertains to
> > 	a friend declaration that is not a definition.
> > 	* decl2.c (any_non_type_attribute_p): New.
> > 	* parser.c (cp_parser_decl_specifier_seq): Diagnose standard attributes
> > 	in the middle of decl-specifiers.
> > 	(cp_parser_elaborated_type_specifier): Diagnose when an attribute
> > 	appertains to a friend declaration that is not a definition.
> > 	(cp_parser_member_declaration): Likewise.
> > 
> 
> Hi,
> 
> I haven't investigated it in detail yet, but it seems this change breaks
> building Qt-based (tested with Qt-5.12.7) applications. Sample error output
> with trunk@r12+876 -std=gnu++14:
> 
> /usr/include/qt5/QtCore/qvariant.h:470:33: error: attribute appertains to a
> friend declaration that is not a definition
>      friend Q_CORE_EXPORT QDebug operator<<(QDebug, const QVariant &);
>                                  ^~~~~~~~
> 
> For GCC Q_CORE_EXPORT is defined (via Q_DECL_EXPORT) to
> __attribute__((visibility("default"))) AFAICS.
> 
> As this error seemingly cannot be turned into a warning, it's probably quite
> a problem for many people.

Yes, I'm working on a patch that will fix this (hoping to submit it today).

Sorry about the breakage.

Marek



More information about the Gcc-patches mailing list