c++: Diagnose attributes on class/enum declarations [PR110345]

The following testcase shows another issue where we just ignored
attributes without telling user we did that.

If there are any declarators, the ignoring of the attribute
are diagnosed in grokdeclarator etc., but if there is none
(and we don't error such as on
int;
), the following patch emits diagnostics.

2024-12-18  Jakub Jelinek  <jakub@redhat.com>

	PR c++/110345
	* decl.cc (check_tag_decl): Diagnose std_attributes.

	* g++.dg/cpp0x/gen-attrs-86.C: New test.
This commit is contained in:
Jakub Jelinek 2024-12-18 11:54:57 +01:00 committed by Jakub Jelinek
parent 49b142f2ef
commit d003a3862a
2 changed files with 19 additions and 0 deletions

View file

@ -5820,6 +5820,17 @@ check_tag_decl (cp_decl_specifier_seq *declspecs,
warn_misplaced_attr_for_class_type (loc, declared_type);
}
if (declspecs->std_attributes
&& declared_type
&& any_nonignored_attribute_p (declspecs->std_attributes))
{
auto_diagnostic_group d;
if (warning_at (declspecs->locations[ds_std_attribute], OPT_Wattributes,
"attribute ignored"))
inform (declspecs->locations[ds_std_attribute],
"an attribute that appertains to a type-specifier is ignored");
}
/* Diagnose invalid application of contracts, if any. */
if (find_contract (declspecs->attributes))
diagnose_misapplied_contracts (declspecs->attributes);

View file

@ -0,0 +1,8 @@
// { dg-do compile { target c++11 } }
struct S {};
struct S [[gnu::deprecated]]; // { dg-warning "attribute ignored" }
// { dg-message "an attribute that appertains to a type-specifier is ignored" "" { target *-*-* } .-1 }
enum E {};
enum E [[gnu::deprecated]]; // { dg-warning "attribute ignored" }
// { dg-message "an attribute that appertains to a type-specifier is ignored" "" { target *-*-* } .-1 }