public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Jason Merrill <jason@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc r12-1405] c-family: don't warn for [[maybe_unused]] on data member
Date: Sat, 12 Jun 2021 16:46:51 +0000 (GMT)	[thread overview]
Message-ID: <20210612164652.016D83857C52@sourceware.org> (raw)

https://gcc.gnu.org/g:c0f769fa3114ea852a26d93f0ee3f9595463de0b

commit r12-1405-gc0f769fa3114ea852a26d93f0ee3f9595463de0b
Author: Jason Merrill <jason@redhat.com>
Date:   Fri Jun 11 16:10:50 2021 -0400

    c-family: don't warn for [[maybe_unused]] on data member
    
    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.

Diff:
---
 gcc/doc/extend.texi                             |  6 +++---
 gcc/c-family/c-attribs.c                        |  1 +
 gcc/testsuite/g++.dg/diagnostic/maybe_unused1.C | 17 +++++++++++++++++
 gcc/testsuite/g++.dg/ext/attrib62.C             |  2 +-
 4 files changed, 22 insertions(+), 4 deletions(-)

diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi
index 408979b78af..8fc66d626d8 100644
--- a/gcc/doc/extend.texi
+++ b/gcc/doc/extend.texi
@@ -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/c-family/c-attribs.c b/gcc/c-family/c-attribs.c
index 42026a811dd..6bf492afcc0 100644
--- a/gcc/c-family/c-attribs.c
+++ b/gcc/c-family/c-attribs.c
@@ -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;
diff --git a/gcc/testsuite/g++.dg/diagnostic/maybe_unused1.C b/gcc/testsuite/g++.dg/diagnostic/maybe_unused1.C
new file mode 100644
index 00000000000..70a8ec937dd
--- /dev/null
+++ b/gcc/testsuite/g++.dg/diagnostic/maybe_unused1.C
@@ -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 };
diff --git a/gcc/testsuite/g++.dg/ext/attrib62.C b/gcc/testsuite/g++.dg/ext/attrib62.C
index 116ee829a2d..d34cd2549de 100644
--- a/gcc/testsuite/g++.dg/ext/attrib62.C
+++ b/gcc/testsuite/g++.dg/ext/attrib62.C
@@ -3,5 +3,5 @@
 
 template<typename T> struct A
 {
-  T a, __attribute((unused)) b; // { dg-warning "attribute ignored" }
+  T a, __attribute((unused)) b;
 };


                 reply	other threads:[~2021-06-12 16:46 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210612164652.016D83857C52@sourceware.org \
    --to=jason@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).