diff mbox series

c++: Handle enum attributes like class attributes [PR110345]

Message ID ZtIBXMrXQiwwZ2h3@tucnak
State New
Headers show
Series c++: Handle enum attributes like class attributes [PR110345] | expand

Commit Message

Jakub Jelinek Aug. 30, 2024, 5:29 p.m. UTC
Hi!

As the following testcase shows, cp_parser_decl_specifier_seq
was calling warn_misplaced_attr_for_class_type only for class types
and not for enum types, while check_tag_decl calls them for both
class and enum types.
Enum types are really the same case here, the attribute needs to go
before the type name to apply to all instances of the type.
Additionally, when warn_misplaced_attr_for_class_type is called, it
diagnoses something and so it is fine to drop the attributes then
on the floor, but in case it wasn't a type decision, it silently
discarded the attributes, which is invalid for the ignorability of
standard attributes paper.  This patch in that case adds them to
decl_specs->std_attributes and let it be diagnosed later (e.g.
in grokdeclarator).

Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?

2024-08-30  Jakub Jelinek  <jakub@redhat.com>

	PR c++/110345
	* parser.cc (cp_parser_decl_specifier_seq): Call
	warn_misplaced_attr_for_class_type for all OVERLOAD_TYPE_P
	types, not just CLASS_TYPE_P.  When not calling
	warn_misplaced_attr_for_class_type, don't clear attrs and
	add it to decl_specs->std_attributes instead.

	* g++.dg/cpp0x/gen-attrs-85.C: New test.


	Jakub
diff mbox series

Patch

--- gcc/cp/parser.cc.jj	2024-08-30 12:55:17.875362909 +0200
+++ gcc/cp/parser.cc	2024-08-30 13:44:42.485290076 +0200
@@ -16565,13 +16565,13 @@  cp_parser_decl_specifier_seq (cp_parser*
 		      diagnose_misapplied_contracts (attrs);
 		      attrs = NULL_TREE;
 		    }
-		  else if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
+		  else if (decl_specs->type
+			   && decl_specs->type_definition_p
+			   && OVERLOAD_TYPE_P (decl_specs->type))
 		    {
-		      /*  This is an attribute following a
-			  class-specifier.  */
-		      if (decl_specs->type_definition_p)
-			warn_misplaced_attr_for_class_type (token->location,
-							    decl_specs->type);
+		      /* This is an attribute following a class-specifier.  */
+		      warn_misplaced_attr_for_class_type (token->location,
+							  decl_specs->type);
 		      attrs = NULL_TREE;
 		    }
 		  else
--- gcc/testsuite/g++.dg/cpp0x/gen-attrs-85.C.jj	2024-08-30 13:54:12.356968839 +0200
+++ gcc/testsuite/g++.dg/cpp0x/gen-attrs-85.C	2024-08-30 13:54:05.846052485 +0200
@@ -0,0 +1,7 @@ 
+// { dg-do compile { target c++11 } }
+
+struct S {};
+struct S [[gnu::deprecated]] s;	// { dg-warning "attribute ignored" }
+// { dg-message "an attribute that appertains to a type-specifier is ignored" "" { target *-*-* } .-1 }
+enum E {} [[gnu::deprecated]];	// { dg-warning "attribute ignored in declaration of 'enum E'" }
+// { dg-message "attribute for 'enum E' must follow the 'enum' keyword" "" { target *-*-* } .-1 }