aboutsummaryrefslogtreecommitdiff
path: root/gcc/cp
diff options
context:
space:
mode:
authorMarek Polacek <polacek@redhat.com>2021-05-18 16:11:16 -0400
committerMarek Polacek <polacek@redhat.com>2021-05-19 13:10:15 -0400
commitadcb497bdba499d161d2e5e8de782bdd6f75d62c (patch)
treea942fb6dd1ff181d3f39e0507b46db9045f88a9d /gcc/cp
parenta911287e13d1a1f95259cb60c57293eabc2a27b9 (diff)
downloadgcc-adcb497bdba499d161d2e5e8de782bdd6f75d62c.zip
gcc-adcb497bdba499d161d2e5e8de782bdd6f75d62c.tar.gz
gcc-adcb497bdba499d161d2e5e8de782bdd6f75d62c.tar.bz2
c++: Relax attribute on friend declaration checking [PR100596]
It turned out that there are codebases that profusely use GNU attributes on friend declarations, so we have to dial back our checking and allow them. And for C++11 attributes let's just warn instead of giving errors. PR c++/100596 gcc/cp/ChangeLog: * cp-tree.h (any_non_type_attribute_p): Remove. * decl.c (grokdeclarator): Turn an error into a warning and only warn for standard attributes. * decl2.c (any_non_type_attribute_p): Remove. * parser.c (cp_parser_elaborated_type_specifier): Turn an error into a warning and only warn for standard attributes. (cp_parser_member_declaration): Likewise. gcc/testsuite/ChangeLog: * g++.dg/cpp0x/friend7.C: Turn a few dg-warnings into dg-errors. Remove dg-errors for GNU attributes. * g++.dg/ext/attrib63.C: Remove dg-error. * g++.dg/cpp0x/friend8.C: New test.
Diffstat (limited to 'gcc/cp')
-rw-r--r--gcc/cp/cp-tree.h1
-rw-r--r--gcc/cp/decl.c14
-rw-r--r--gcc/cp/decl2.c14
-rw-r--r--gcc/cp/parser.c29
4 files changed, 28 insertions, 30 deletions
diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h
index 3c900dc..860ed79 100644
--- a/gcc/cp/cp-tree.h
+++ b/gcc/cp/cp-tree.h
@@ -6761,7 +6761,6 @@ 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 4f2fc2e..28052df 100644
--- a/gcc/cp/decl.c
+++ b/gcc/cp/decl.c
@@ -13731,11 +13731,15 @@ 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");
+ /* Packages tend to use GNU attributes on friends, so we only
+ warn for standard attributes. */
+ if (attrlist && !funcdef_flag && cxx11_attribute_p (*attrlist))
+ {
+ *attrlist = NULL_TREE;
+ if (warning_at (id_loc, OPT_Wattributes, "attribute ignored"))
+ inform (id_loc, "an attribute that appertains to a friend "
+ "declaration that is not a definition is ignored");
+ }
/* 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 8e4dd6b..89f874a 100644
--- a/gcc/cp/decl2.c
+++ b/gcc/cp/decl2.c
@@ -1331,20 +1331,6 @@ 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 = TREE_CHAIN (a))
- {
- const attribute_spec *as = lookup_attribute_spec (get_attribute_name (a));
- if (as && !as->type_required)
- return true;
- }
- return false;
-}
-
/* Return true iff ATTRS are acceptable attributes to be applied in-place
to a typedef which gives a previously unnamed class or enum a name for
linkage purposes. */
diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c
index f3503b1..bc0505d 100644
--- a/gcc/cp/parser.c
+++ b/gcc/cp/parser.c
@@ -19774,9 +19774,12 @@ cp_parser_elaborated_type_specifier (cp_parser* parser,
&& ! processing_explicit_instantiation)
warning (OPT_Wattributes,
"attributes ignored on template instantiation");
- else if (is_friend && attributes)
- error ("attribute appertains to a friend declaration that is not "
- "a definition");
+ else if (is_friend && cxx11_attribute_p (attributes))
+ {
+ if (warning (OPT_Wattributes, "attribute ignored"))
+ inform (input_location, "an attribute that appertains to a friend "
+ "declaration that is not a definition is ignored");
+ }
else if (is_declaration && cp_parser_declares_only_class_p (parser))
cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
else
@@ -26064,17 +26067,23 @@ cp_parser_member_declaration (cp_parser* parser)
if (type && TREE_CODE (type) == TYPE_DECL)
type = TREE_TYPE (type);
}
+ /* Warn if an attribute cannot appear here, as per
+ [dcl.attr.grammar]/5. But not when declares_class_or_enum:
+ we ignore attributes in elaborated-type-specifiers. */
+ if (!declares_class_or_enum
+ && cxx11_attribute_p (decl_specifiers.attributes))
+ {
+ decl_specifiers.attributes = NULL_TREE;
+ if (warning_at (decl_spec_token_start->location,
+ OPT_Wattributes, "attribute ignored"))
+ inform (decl_spec_token_start->location, "an attribute "
+ "that appertains to a friend declaration that "
+ "is not a definition is ignored");
+ }
if (!type || !TYPE_P (type))
error_at (decl_spec_token_start->location,
"friend declaration does not name a class or "
"function");
- /* Give an error if an attribute cannot appear here, as per
- [dcl.attr.grammar]/5. But not when declares_class_or_enum:
- we ignore attributes in elaborated-type-specifiers. */
- else if (!declares_class_or_enum && decl_specifiers.attributes)
- error_at (decl_spec_token_start->location,
- "attribute appertains to a friend declaration "
- "that is not a definition");
else
make_friend_class (current_class_type, type,
/*complain=*/true);