]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c-family: don't warn for [[maybe_unused]] on data member
authorJason Merrill <jason@redhat.com>
Fri, 11 Jun 2021 20:10:50 +0000 (16:10 -0400)
committerJason Merrill <jason@redhat.com>
Sat, 12 Jun 2021 16:46:28 +0000 (12:46 -0400)
The C++17 standard (and C2x) says that [[maybe_unused]] may be applied to a
non-static data member, so we shouldn't warn about it.  And I don't see a
reason not to handle a FIELD_DECL the same as any other decl, by setting
TREE_USED on it.  It doesn't look like anything yet cares about that flag on
a FIELD_DECL, but setting it shouldn't hurt.

gcc/c-family/ChangeLog:

* c-attribs.c (handle_unused_attribute): Handle FIELD_DECL.

gcc/testsuite/ChangeLog:

* g++.dg/ext/attrib62.C: No longer warn.
* g++.dg/diagnostic/maybe_unused1.C: New test.

gcc/ChangeLog:

* doc/extend.texi (unused variable attribute): Applies to
structure fields as well.

gcc/c-family/c-attribs.c
gcc/doc/extend.texi
gcc/testsuite/g++.dg/diagnostic/maybe_unused1.C [new file with mode: 0644]
gcc/testsuite/g++.dg/ext/attrib62.C

index 42026a811dd2e4469ed9811c75711347613d02d3..6bf492afcc0146f0dbb04206588c97f7bb187966 100644 (file)
@@ -1568,6 +1568,7 @@ handle_unused_attribute (tree *node, tree name, tree ARG_UNUSED (args),
          || VAR_OR_FUNCTION_DECL_P (decl)
          || TREE_CODE (decl) == LABEL_DECL
          || TREE_CODE (decl) == CONST_DECL
+         || TREE_CODE (decl) == FIELD_DECL
          || TREE_CODE (decl) == TYPE_DECL)
        {
          TREE_USED (decl) = 1;
index 408979b78af9ec3a3c7e925f9b7af83357593555..8fc66d626d83cfd80579bd26576222704c81ed60 100644 (file)
@@ -7516,9 +7516,9 @@ Not all targets support this attribute.
 
 @item unused
 @cindex @code{unused} variable attribute
-This attribute, attached to a variable, means that the variable is meant
-to be possibly unused.  GCC does not produce a warning for this
-variable.
+This attribute, attached to a variable or structure field, means that
+the variable or field is meant to be possibly unused.  GCC does not
+produce a warning for this variable or field.
 
 @item used
 @cindex @code{used} variable attribute
diff --git a/gcc/testsuite/g++.dg/diagnostic/maybe_unused1.C b/gcc/testsuite/g++.dg/diagnostic/maybe_unused1.C
new file mode 100644 (file)
index 0000000..70a8ec9
--- /dev/null
@@ -0,0 +1,17 @@
+/* [dcl.attr.unused] The attribute may be applied to the declaration of a
+   class, a typedef-name, a variable (including a structured binding
+   declaration), a non-static data member, a function, an enumeration, or an
+   enumerator.  */
+// { dg-do compile { target c++11 } }
+// { dg-additional-options "-Wunused -Wextra" }
+
+class [[maybe_unused]] Test {
+  [[maybe_unused]] int a_;
+  void b() {};
+};
+
+[[maybe_unused]] typedef Test Test2;
+
+[[maybe_unused]] int i;
+[[maybe_unused]] void f();
+enum [[maybe_unused]] E { e [[maybe_unused]] = 42 };
index 116ee829a2d01e0744021f3a9d3416fd2b39548f..d34cd2549de512574230053f922655c92375dd79 100644 (file)
@@ -3,5 +3,5 @@
 
 template<typename T> struct A
 {
-  T a, __attribute((unused)) b; // { dg-warning "attribute ignored" }
+  T a, __attribute((unused)) b;
 };