]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: Diagnose attributes on class/enum declarations [PR110345]
authorJakub Jelinek <jakub@redhat.com>
Wed, 18 Dec 2024 10:54:57 +0000 (11:54 +0100)
committerJakub Jelinek <jakub@gcc.gnu.org>
Wed, 18 Dec 2024 10:54:57 +0000 (11:54 +0100)
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.

gcc/cp/decl.cc
gcc/testsuite/g++.dg/cpp0x/gen-attrs-86.C [new file with mode: 0644]

index 5bd0e2195615e0e1e950f1b99a53d820a6170bb5..135747b75e2ba7ae7a2665dde4b0186d7984bd60 100644 (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);
diff --git a/gcc/testsuite/g++.dg/cpp0x/gen-attrs-86.C b/gcc/testsuite/g++.dg/cpp0x/gen-attrs-86.C
new file mode 100644 (file)
index 0000000..bc33846
--- /dev/null
@@ -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 }