c++: Check attributes on friend declarations [PR99032]
commit149061188c7c6ddf27663c8c00b7574fc8d0fd23
authorMarek Polacek <polacek@redhat.com>
Fri, 30 Apr 2021 01:38:14 +0000 (29 21:38 -0400)
committerMarek Polacek <polacek@redhat.com>
Fri, 14 May 2021 01:13:32 +0000 (13 21:13 -0400)
tree73ad0e8a10cdf67cb96a97cd2909cdaacf2dbe44
parentf9af11c7f156bede9aa28410073acbab7b0fa0fe
c++: Check attributes on friend declarations [PR99032]

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.

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
gcc/cp/decl.c
gcc/cp/decl2.c
gcc/cp/parser.c
gcc/testsuite/g++.dg/cpp0x/friend7.C [new file with mode: 0644]
gcc/testsuite/g++.dg/cpp0x/gen-attrs-39-1.C
gcc/testsuite/g++.dg/cpp0x/gen-attrs-4.C
gcc/testsuite/g++.dg/cpp0x/gen-attrs-74.C [new file with mode: 0644]
gcc/testsuite/g++.dg/ext/attrib63.C [new file with mode: 0644]