]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: Fix up maybe_unused attribute handling [PR110345]
authorJakub Jelinek <jakub@redhat.com>
Fri, 20 Dec 2024 09:17:56 +0000 (10:17 +0100)
committerJakub Jelinek <jakub@gcc.gnu.org>
Fri, 20 Dec 2024 09:17:56 +0000 (10:17 +0100)
When adding test coverage for maybe_unused attribute, I've run into
several things:
1) similarly to deprecated attribute, the attribute shouldn't pedantically
   appertain to types other than class/enumeration definitions
2) similarly to deprecated attribute, the attribute shouldn't pedantically
   appertain to unnamed bit-fields
3) the standard says that it can appertain to identifier labels, but
   we handled it silently also on case and default labels
4) I've run into a weird spurious error on
   int f [[maybe_unused]];
   int & [[maybe_unused]] i = f;
   int && [[maybe_unused]] j = 0;
   The problem was that we create an attribute variant for the int &
   type, then create an attribute variant for the int && type, and
   the type_canon_hash hashing just thought those 2 are the same,
   so used int & [[maybe_unused]] type for j rather than
   int && [[maybe_unused]].  As TYPE_REF_IS_RVALUE is a flag in the
   generic code, it was easily possible to hash that flag and compare
   it

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

PR c++/110345
gcc/
* tree.cc (type_hash_canon_hash): Hash TYPE_REF_IS_RVALUE for
REFERENCE_TYPE.
(type_cache_hasher::equal): Compare TYPE_REF_IS_RVALUE for
REFERENCE_TYPE.
gcc/cp/
* tree.cc (handle_maybe_unused_attribute): New function.
(std_attributes): Use handle_maybe_unused_attribute instead
of handle_unused_attribute for maybe_unused attribute.
gcc/testsuite/
* g++.dg/cpp0x/attr-maybe_unused1.C: New test.
* g++.dg/cpp0x/alignas21.C: Add test for int && alignas (int).

gcc/cp/tree.cc
gcc/testsuite/g++.dg/cpp0x/alignas21.C
gcc/testsuite/g++.dg/cpp0x/attr-maybe_unused1.C [new file with mode: 0644]
gcc/tree.cc

index 12d0831e4c4cb764ae7a4a2c4482122c1147aee3..9b181be6237f64df58231a8fa042b4dce250a1a6 100644 (file)
@@ -5162,6 +5162,27 @@ handle_std_deprecated_attribute (tree *node, tree name, tree args, int flags,
   return ret;
 }
 
+/* The C++17 [[maybe_unused]] attribute mostly maps to the GNU unused
+   attribute.  */
+
+static tree
+handle_maybe_unused_attribute (tree *node, tree name, tree args, int flags,
+                              bool *no_add_attrs)
+{
+  tree t = *node;
+  tree ret = handle_unused_attribute (node, name, args, flags, no_add_attrs);
+  if (TYPE_P (*node) && t != *node)
+    pedwarn (input_location, OPT_Wattributes,
+            "%qE on a type other than class or enumeration definition", name);
+  else if (TREE_CODE (*node) == FIELD_DECL && DECL_UNNAMED_BIT_FIELD (*node))
+    pedwarn (input_location, OPT_Wattributes, "%qE on unnamed bit-field",
+            name);
+  else if (TREE_CODE (*node) == LABEL_DECL && DECL_NAME (*node) == NULL_TREE)
+    pedwarn (input_location, OPT_Wattributes,
+            "%qE on %<case%> or %<default%> label", name);
+  return ret;
+}
+
 /* Table of valid C++ attributes.  */
 static const attribute_spec cxx_gnu_attributes[] =
 {
@@ -5188,7 +5209,7 @@ static const attribute_spec std_attributes[] =
   { "deprecated", 0, 1, false, false, false, false,
     handle_std_deprecated_attribute, NULL },
   { "maybe_unused", 0, 0, false, false, false, false,
-    handle_unused_attribute, NULL },
+    handle_maybe_unused_attribute, NULL },
   { "nodiscard", 0, 1, false, false, false, false,
     handle_nodiscard_attribute, NULL },
   { "no_unique_address", 0, 0, true, false, false, false,
index b7be7c5ce926e1201704254b6dd40c80a9ad1e12..3d3059a4e02c1b46456b3a3bee737f2bcec1d1b2 100644 (file)
@@ -81,6 +81,7 @@ int g2 alignas (int) [2];
 int corge () alignas (int);                    // { dg-error "'alignas' on a type other than class" }
 int *alignas (int) h;                          // { dg-error "'alignas' on a type other than class" }
 int & alignas (int) i = f;                     // { dg-error "'alignas' on a type other than class" }
+int && alignas (int) j = 0;                    // { dg-error "'alignas' on a type other than class" }
 int S::* alignas (int) k;                      // { dg-error "'alignas' on a type other than class" }
 auto l = sizeof (int [2] alignas (int));       // { dg-error "'alignas' on a type other than class" }
 int freddy (alignas (int) int a,               // { dg-error "alignment may not be specified for 'a'" }
diff --git a/gcc/testsuite/g++.dg/cpp0x/attr-maybe_unused1.C b/gcc/testsuite/g++.dg/cpp0x/attr-maybe_unused1.C
new file mode 100644 (file)
index 0000000..c2c2d36
--- /dev/null
@@ -0,0 +1,148 @@
+// C++ 26 P2552R3 - On the ignorability of standard attributes
+// { dg-do compile { target c++11 } }
+
+int arr[2];
+struct S { int a, b; };
+S arr2[2];
+
+void
+foo (int n)
+{
+  [[maybe_unused]] int x1;
+  [[maybe_unused ("foobar")]] int x2;          // { dg-error "'maybe_unused' attribute does not take any arguments" }
+                                               // { dg-error "expected primary-expression before 'int'" "" { target *-*-* } .-1 }
+  [[maybe_unused (0)]] int x3;                 // { dg-error "'maybe_unused' attribute does not take any arguments" }
+                                               // { dg-error "expected primary-expression before 'int'" "" { target *-*-* } .-1 }
+
+  auto a = [] [[maybe_unused]] () {};
+  auto b = [] constexpr [[maybe_unused]] {};   // { dg-error "'maybe_unused' on a type other than class or enumeration definition" }
+                                               // { dg-error "parameter declaration before lambda declaration specifiers only optional with" "" { target c++20_down } .-1 }
+                                               // { dg-error "'constexpr' lambda only available with" "" { target c++14_down } .-2 }
+  auto c = [] noexcept [[maybe_unused]] {};    // { dg-error "'maybe_unused' on a type other than class or enumeration definition" }
+                                               // { dg-error "parameter declaration before lambda exception specification only optional with" "" { target c++20_down } .-1 }
+  auto d = [] () [[maybe_unused]] {};          // { dg-error "'maybe_unused' on a type other than class or enumeration definition" }
+  auto e = new int [n] [[maybe_unused]];       // { dg-warning "attributes ignored on outermost array type in new expression" }
+  auto e2 = new int [n] [[maybe_unused]] [42]; // { dg-warning "attributes ignored on outermost array type in new expression" }
+  auto f = new int [n][42] [[maybe_unused]];   // { dg-error "'maybe_unused' on a type other than class or enumeration definition" }
+  [[maybe_unused]];                            // { dg-warning "attributes at the beginning of statement are ignored" }
+  [[maybe_unused]] {}                          // { dg-warning "attributes at the beginning of statement are ignored" }
+  [[maybe_unused]] if (true) {}                        // { dg-warning "attributes at the beginning of statement are ignored" }
+  [[maybe_unused]] while (false) {}            // { dg-warning "attributes at the beginning of statement are ignored" }
+  [[maybe_unused]] goto lab;                   // { dg-warning "attributes at the beginning of statement are ignored" }
+  [[maybe_unused]] lab:;
+  [[maybe_unused]] try {} catch (int) {}       // { dg-warning "attributes at the beginning of statement are ignored" }
+  if ([[maybe_unused]] int x = 0) {}
+  switch (n)
+    {
+    [[maybe_unused]] case 1:                   // { dg-error "'maybe_unused' on 'case' or 'default' label" }
+    [[maybe_unused]] break;                    // { dg-warning "attributes at the beginning of statement are ignored" }
+    [[maybe_unused]] default:                  // { dg-error "'maybe_unused' on 'case' or 'default' label" }
+        break;
+    }
+  for ([[maybe_unused]] auto a : arr) {}
+  for ([[maybe_unused]] auto [a, b] : arr2) {} // { dg-error "structured bindings only available with" "" { target c++14_down } }
+  [[maybe_unused]] asm ("");                   // { dg-warning "attributes ignored on 'asm' declaration" }
+  try {} catch ([[maybe_unused]] int x) {}
+  try {} catch ([[maybe_unused]] int) {}
+  try {} catch (int [[maybe_unused]] x) {}     // { dg-warning "attribute ignored" }
+  try {} catch (int [[maybe_unused]]) {}       // { dg-warning "attribute ignored" }
+  try {} catch (int x [[maybe_unused]]) {}
+}
+
+[[maybe_unused]] int bar ();
+using foobar [[maybe_unused]] = int;
+[[maybe_unused]] int a;
+[[maybe_unused]] auto [b, c] = arr;            // { dg-error "structured bindings only available with" "" { target c++14_down } }
+[[maybe_unused]];                              // { dg-warning "attribute ignored" }
+inline [[maybe_unused]] void baz () {}         // { dg-warning "attribute ignored" }
+                                               // { dg-error "standard attributes in middle of decl-specifiers" "" { target *-*-* } .-1 }
+constexpr [[maybe_unused]] int qux () { return 0; }    // { dg-warning "attribute ignored" }
+                                               // { dg-error "standard attributes in middle of decl-specifiers" "" { target *-*-* } .-1 }
+int [[maybe_unused]] d;                                // { dg-warning "attribute ignored" }
+int const [[maybe_unused]] e = 1;              // { dg-warning "attribute ignored" }
+struct A {} [[maybe_unused]];                  // { dg-warning "attribute ignored in declaration of 'struct A'" }
+struct A [[maybe_unused]];                     // { dg-warning "attribute ignored" }
+struct A [[maybe_unused]] a1;                  // { dg-warning "attribute ignored" }
+A [[maybe_unused]] a2;                         // { dg-warning "attribute ignored" }
+enum B { B0 } [[maybe_unused]];                        // { dg-warning "attribute ignored in declaration of 'enum B'" }
+enum B [[maybe_unused]];                       // { dg-warning "attribute ignored" }
+enum B [[maybe_unused]] b1;                    // { dg-warning "attribute ignored" }
+B [[maybe_unused]] b2;                         // { dg-warning "attribute ignored" }
+struct [[maybe_unused]] C {};
+int f [[maybe_unused]];
+int g[2] [[maybe_unused]];                     // { dg-error "'maybe_unused' on a type other than class or enumeration definition" }
+int g2 [[maybe_unused]] [2];
+int corge () [[maybe_unused]];                 // { dg-error "'maybe_unused' on a type other than class or enumeration definition" }
+int *[[maybe_unused]] h;                       // { dg-error "'maybe_unused' on a type other than class or enumeration definition" }
+int & [[maybe_unused]] i = f;                  // { dg-error "'maybe_unused' on a type other than class or enumeration definition" }
+int && [[maybe_unused]] j = 0;                 // { dg-error "'maybe_unused' on a type other than class or enumeration definition" }
+int S::* [[maybe_unused]] k;                   // { dg-error "'maybe_unused' on a type other than class or enumeration definition" }
+auto l = sizeof (int [2] [[maybe_unused]]);    // { dg-error "'maybe_unused' on a type other than class or enumeration definition" }
+int freddy ([[maybe_unused]] int a,
+           [[maybe_unused]] int,
+           [[maybe_unused]] int c = 0,
+           [[maybe_unused]] int = 0);
+void
+corge ([[maybe_unused]] int a,
+       [[maybe_unused]] int,
+       [[maybe_unused]] int c = 0,
+       [[maybe_unused]] int = 0)
+{
+}
+[[maybe_unused]] void
+garply ()
+{
+}
+int grault (int [[maybe_unused]] a,            // { dg-warning "attribute ignored" }
+           int [[maybe_unused]],               // { dg-warning "attribute ignored" }
+           int [[maybe_unused]] c = 0,         // { dg-warning "attribute ignored" }
+           int [[maybe_unused]] = 0);          // { dg-warning "attribute ignored" }
+void
+waldo (int [[maybe_unused]] a,                 // { dg-warning "attribute ignored" }
+       int [[maybe_unused]],                   // { dg-warning "attribute ignored" }
+       int [[maybe_unused]] c = 0,             // { dg-warning "attribute ignored" }
+       int [[maybe_unused]] = 0)               // { dg-warning "attribute ignored" }
+{
+}
+int plugh (int a [[maybe_unused]],
+           int b [[maybe_unused]] = 0);
+void
+thud (int a [[maybe_unused]],
+      int b [[maybe_unused]] = 0)
+{
+}
+enum [[maybe_unused]] D { D0 };
+enum class [[maybe_unused]] E { E0 };
+enum F {};
+enum [[maybe_unused]] F;                       // { dg-warning "type attributes ignored after type is already defined" }
+enum G {
+  G0 [[maybe_unused]],
+  G1 [[maybe_unused]] = 2
+};
+namespace [[maybe_unused]] H { using H0 = int; }// { dg-warning "'maybe_unused' attribute directive ignored" }
+namespace [[maybe_unused]] {}                  // { dg-warning "'maybe_unused' attribute directive ignored" }
+[[maybe_unused]] using namespace H;            // { dg-warning "'maybe_unused' attribute directive ignored" }
+struct [[maybe_unused]] I
+{
+  [[maybe_unused]];                            // { dg-error "declaration does not declare anything" }
+  [[maybe_unused]] int i;
+  [[maybe_unused]] int foo ();
+  [[maybe_unused]] int bar () { return 1; }
+  [[maybe_unused]] int : 0;                    // { dg-error "'maybe_unused' on unnamed bit-field" }
+  [[maybe_unused]] int i2 : 5;
+  [[maybe_unused]] static int i3;
+  static int i4;
+};
+[[maybe_unused]] int I::i4 = 0;
+struct J : [[maybe_unused]] C {};              // { dg-warning "attributes on base specifiers are ignored" }
+#if __cpp_concepts >= 201907L
+template <typename T>
+concept K [[maybe_unused]] = requires { true; };// { dg-warning "'maybe_unused' attribute ignored" "" { target c++20 } }
+#endif
+typedef int L [[maybe_unused]];
+template <typename T>
+struct M {};
+template <>
+struct [[maybe_unused]] M<int> { int m; };
+typedef int N[2] [[maybe_unused]];             // { dg-error "'maybe_unused' on a type other than class or enumeration definition" }
+typedef int O [[maybe_unused]] [2];
index 3224511391623066de4e67da9b1cc442bd42adcc..e3b5e56b828c908b6af5e7b485c4767c3722b31e 100644 (file)
@@ -6120,6 +6120,10 @@ type_hash_canon_hash (tree type)
       hstate.add_poly_int (TYPE_VECTOR_SUBPARTS (type));
       break;
 
+    case REFERENCE_TYPE:
+      hstate.add_flag (TYPE_REF_IS_RVALUE (type));
+      break;
+
     default:
       break;
     }
@@ -6162,7 +6166,6 @@ type_cache_hasher::equal (type_hash *a, type_hash *b)
     case OPAQUE_TYPE:
     case COMPLEX_TYPE:
     case POINTER_TYPE:
-    case REFERENCE_TYPE:
     case NULLPTR_TYPE:
       return true;
 
@@ -6252,6 +6255,9 @@ type_cache_hasher::equal (type_hash *a, type_hash *b)
        break;
       return false;
 
+    case REFERENCE_TYPE:
+      return TYPE_REF_IS_RVALUE (a->type) == TYPE_REF_IS_RVALUE (b->type);
+
     default:
       return false;
     }