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

2021-05-18 Thread Marek Polacek via Gcc-patches
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 
> > 
> > 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



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

2021-05-18 Thread Franz Sirl

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 

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.


regards,
Franz


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

2021-05-13 Thread Jason Merrill via Gcc-patches

On 5/13/21 6:08 PM, Marek Polacek wrote:

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?


OK.


-- >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 

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.

gcc/testsuite/ChangeLog:

PR c++/99032
* g++.dg/cpp0x/friend7.C: New test.
* g++.dg/cpp0x/gen-attrs-4.C: Add dg-error.
* g++.dg/cpp0x/gen-attrs-39-1.C: Likewise.
* g++.dg/cpp0x/gen-attrs-74.C: New test.
* g++.dg/ext/attrib63.C: New test.
---
  gcc/cp/cp-tree.h|  1 +
  gcc/cp/decl.c   |  5 +++
  gcc/cp/decl2.c  | 14 
  gcc/cp/parser.c | 23 +++-
  gcc/testsuite/g++.dg/cpp0x/friend7.C| 40 +
  gcc/testsuite/g++.dg/cpp0x/gen-attrs-39-1.C |  3 +-
  gcc/testsuite/g++.dg/cpp0x/gen-attrs-4.C|  3 +-
  gcc/testsuite/g++.dg/cpp0x/gen-attrs-74.C   | 10 ++
  gcc/testsuite/g++.dg/ext/attrib63.C | 16 +
  9 files changed, 112 insertions(+), 3 deletions(-)
  create mode 100644 gcc/testsuite/g++.dg/cpp0x/friend7.C
  create mode 100644 gcc/testsuite/g++.dg/cpp0x/gen-attrs-74.C
  create mode 100644 gcc/testsuite/g++.dg/ext/attrib63.C

diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h
index 122dadf976f..580db914d40 100644
--- a/gcc/cp/cp-tree.h
+++ b/gcc/cp/cp-tree.h
@@ -6763,6 +6763,7 @@ extern tree grokbitfield (const cp_declarator *, 
cp_decl_specifier_seq *,
  tree, tree, tree);
  extern tree splice_template_attributes(tree *, tree);
  extern bool any_dependent_type_attributes_p   (tree);
+extern bool any_non_type_attribute_p   (tree);
  extern tree cp_reconstruct_complex_type   (tree, tree);
  extern bool attributes_naming_typedef_ok  (tree);
  extern void cplus_decl_attributes (tree *, tree, int);
diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c
index bc3928d7f85..17511f09e79 100644
--- a/gcc/cp/decl.c
+++ b/gcc/cp/decl.c
@@ -13741,6 +13741,11 @@ grokdeclarator (const cp_declarator *declarator,
  
  	if (friendp)

  {
+   if (attrlist && !funcdef_flag
+   /* Hack to allow attributes like vector_size on a friend.  */
+   && any_non_type_attribute_p (*attrlist))
+ error_at (id_loc, "attribute appertains to a friend "
+   "declaration that is not a definition");
/* Friends are treated specially.  */
if (ctype == current_class_type)
  ;  /* We already issued a permerror.  */
diff --git a/gcc/cp/decl2.c b/gcc/cp/decl2.c
index 89f874a32cc..8e4dd6b544a 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 does not require a type.  */

+
+bool
+any_non_type_attribute_p (tree 

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

2021-05-13 Thread 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 

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.

gcc/testsuite/ChangeLog:

PR c++/99032
* g++.dg/cpp0x/friend7.C: New test.
* g++.dg/cpp0x/gen-attrs-4.C: Add dg-error.
* g++.dg/cpp0x/gen-attrs-39-1.C: Likewise.
* g++.dg/cpp0x/gen-attrs-74.C: New test.
* g++.dg/ext/attrib63.C: New test.
---
 gcc/cp/cp-tree.h|  1 +
 gcc/cp/decl.c   |  5 +++
 gcc/cp/decl2.c  | 14 
 gcc/cp/parser.c | 23 +++-
 gcc/testsuite/g++.dg/cpp0x/friend7.C| 40 +
 gcc/testsuite/g++.dg/cpp0x/gen-attrs-39-1.C |  3 +-
 gcc/testsuite/g++.dg/cpp0x/gen-attrs-4.C|  3 +-
 gcc/testsuite/g++.dg/cpp0x/gen-attrs-74.C   | 10 ++
 gcc/testsuite/g++.dg/ext/attrib63.C | 16 +
 9 files changed, 112 insertions(+), 3 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/cpp0x/friend7.C
 create mode 100644 gcc/testsuite/g++.dg/cpp0x/gen-attrs-74.C
 create mode 100644 gcc/testsuite/g++.dg/ext/attrib63.C

diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h
index 122dadf976f..580db914d40 100644
--- a/gcc/cp/cp-tree.h
+++ b/gcc/cp/cp-tree.h
@@ -6763,6 +6763,7 @@ extern tree grokbitfield (const cp_declarator *, 
cp_decl_specifier_seq *,
  tree, tree, tree);
 extern tree splice_template_attributes (tree *, tree);
 extern bool any_dependent_type_attributes_p(tree);
+extern bool any_non_type_attribute_p   (tree);
 extern tree cp_reconstruct_complex_type(tree, tree);
 extern bool attributes_naming_typedef_ok   (tree);
 extern void cplus_decl_attributes  (tree *, tree, int);
diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c
index bc3928d7f85..17511f09e79 100644
--- a/gcc/cp/decl.c
+++ b/gcc/cp/decl.c
@@ -13741,6 +13741,11 @@ grokdeclarator (const cp_declarator *declarator,
 
if (friendp)
  {
+   if (attrlist && !funcdef_flag
+   /* Hack to allow attributes like vector_size on a friend.  */
+   && any_non_type_attribute_p (*attrlist))
+ error_at (id_loc, "attribute appertains to a friend "
+   "declaration that is not a definition");
/* Friends are treated specially.  */
if (ctype == current_class_type)
  ;  /* We already issued a permerror.  */
diff --git a/gcc/cp/decl2.c b/gcc/cp/decl2.c
index 89f874a32cc..8e4dd6b544a 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 does not require a type.  */
+
+bool
+any_non_type_attribute_p (tree attrs)
+{
+  for (tree a = attrs; a; a = 

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

2021-05-12 Thread Jason Merrill via Gcc-patches

On 5/12/21 8:03 PM, Marek Polacek wrote:

+  // FIXME: Add dg error when PR100339 is resolved.
+  //[[deprecated]] friend void ::foo(int, int);


This could be an xfailed dg-error.



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

2021-05-12 Thread Jason Merrill via Gcc-patches

On 5/12/21 8:03 PM, Marek Polacek wrote:

On Wed, May 12, 2021 at 12:21:26PM -0400, Jason Merrill wrote:

On 5/12/21 11:03 AM, Marek Polacek wrote:

On Wed, May 12, 2021 at 10:37:50AM -0400, Jason Merrill wrote:

On 5/11/21 10:45 PM, Marek Polacek wrote:

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 only applies to C++11-style attributes.  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.


Hmm, why error in this case rather than the usual warning?


I thought such usage (C++11 attribute on a friend) is so rare (no occurrences
in the testsuite) that it'd be better to go straight for an error.  Though
I do see the argument that generally we ignore unknown/misplaced attributes.
clang gives errors, FWIW.


Up to you.


Also, we should warn about similarly useless GNU attributes on friends.


That's unfortunately more complicated: we should(?) accept

#define vector __attribute__((vector_size(16)))
class A {
friend vector float f();
};

vector float vf;
vector float
f ()
{
return vf;
}


Ah, sure.  Because of the DWIM binding of GNU attributes, that attribute
applies to 'float' rather than to the friend.

I think we could work around this issue in the grokdeclarator change by
allowing attributes that have the type_required flag.


That works (and I need to scan all the attributes, hence the new function
any_type_req_attributes_p).


I don't think this issue affects the other two hunks of the patch, so we
should be able to remove the cxx11_attribute_p checks in those.


Yeah, done.
  

In the cp_parser_member_declaration change, I don't think we need to look at
decl_specifiers.std_attributes, as those appertain to the type.


Right, it only caught the 'friend [[deprecated]] void;' case that's handled
by your patch now.  Dropped.
  

Incidentally, I think one of the lines in your testcase is a syntax error
that we fail to diagnose:

+  friend [[deprecated]] void; // { dg-error "attribute appertains" }

I think a C++11 attribute cannot appear in the middle of the
decl-specifier-seq, only before it (in which case it appertains to the
declaration) or at the end (in which case it appertains to the type). So we
should give an error for this line, but not this error.  With something
like:


Nice, thanks!  I've moved that line into a separate 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 

gcc/cp/ChangeLog:

PR c++/99032
* cp-tree.h (any_type_req_attributes_p): Declare.
* decl.c (grokdeclarator): Diagnose when an attribute appertains to
a friend declaration that is not a definition.
* decl2.c (any_type_req_attributes_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.

gcc/testsuite/ChangeLog:

PR c++/99032
* g++.dg/cpp0x/friend7.C: New test.
* g++.dg/cpp0x/gen-attrs-4.C: Add dg-error.
* g++.dg/cpp0x/gen-attrs-39-1.C: Likewise.
* g++.dg/cpp0x/gen-attrs-74.C: New test.
* g++.dg/ext/attrib63.C: New test.
---
  gcc/cp/cp-tree.h|  1 +
  gcc/cp/decl.c   |  5 +++
  gcc/cp/decl2.c  | 14 
  gcc/cp/parser.c | 23 +++-
  gcc/testsuite/g++.dg/cpp0x/friend7.C| 40 +
  gcc/testsuite/g++.dg/cpp0x/gen-attrs-39-1.C |  3 +-
  

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

2021-05-12 Thread Marek Polacek via Gcc-patches
On Wed, May 12, 2021 at 12:21:26PM -0400, Jason Merrill wrote:
> On 5/12/21 11:03 AM, Marek Polacek wrote:
> > On Wed, May 12, 2021 at 10:37:50AM -0400, Jason Merrill wrote:
> > > On 5/11/21 10:45 PM, Marek Polacek wrote:
> > > > 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 only applies to C++11-style attributes.  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.
> > > 
> > > Hmm, why error in this case rather than the usual warning?
> > 
> > I thought such usage (C++11 attribute on a friend) is so rare (no 
> > occurrences
> > in the testsuite) that it'd be better to go straight for an error.  Though
> > I do see the argument that generally we ignore unknown/misplaced attributes.
> > clang gives errors, FWIW.
> 
> Up to you.
> 
> > > Also, we should warn about similarly useless GNU attributes on friends.
> > 
> > That's unfortunately more complicated: we should(?) accept
> > 
> > #define vector __attribute__((vector_size(16)))
> > class A {
> >friend vector float f();
> > };
> > 
> > vector float vf;
> > vector float
> > f ()
> > {
> >return vf;
> > }
> 
> Ah, sure.  Because of the DWIM binding of GNU attributes, that attribute
> applies to 'float' rather than to the friend.
> 
> I think we could work around this issue in the grokdeclarator change by
> allowing attributes that have the type_required flag.

That works (and I need to scan all the attributes, hence the new function
any_type_req_attributes_p).

> I don't think this issue affects the other two hunks of the patch, so we
> should be able to remove the cxx11_attribute_p checks in those.

Yeah, done.
 
> In the cp_parser_member_declaration change, I don't think we need to look at
> decl_specifiers.std_attributes, as those appertain to the type.

Right, it only caught the 'friend [[deprecated]] void;' case that's handled
by your patch now.  Dropped.
 
> Incidentally, I think one of the lines in your testcase is a syntax error
> that we fail to diagnose:
> 
> +  friend [[deprecated]] void; // { dg-error "attribute appertains" }
> 
> I think a C++11 attribute cannot appear in the middle of the
> decl-specifier-seq, only before it (in which case it appertains to the
> declaration) or at the end (in which case it appertains to the type). So we
> should give an error for this line, but not this error.  With something
> like:

Nice, thanks!  I've moved that line into a separate 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 

gcc/cp/ChangeLog:

PR c++/99032
* cp-tree.h (any_type_req_attributes_p): Declare.
* decl.c (grokdeclarator): Diagnose when an attribute appertains to
a friend declaration that is not a definition.
* decl2.c (any_type_req_attributes_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.

gcc/testsuite/ChangeLog:

PR c++/99032
* g++.dg/cpp0x/friend7.C: New test.
* g++.dg/cpp0x/gen-attrs-4.C: Add dg-error.
* g++.dg/cpp0x/gen-attrs-39-1.C: Likewise.
* g++.dg/cpp0x/gen-attrs-74.C: New test.
* g++.dg/ext/attrib63.C: New test.
---
 gcc/cp/cp-tree.h|  1 +
 gcc/cp/decl.c   |  5 +++
 gcc/cp/decl2.c  | 14 
 gcc/cp/parser.c