public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Martin Sebor <msebor@gmail.com>
To: Gcc Patch List <gcc-patches@gcc.gnu.org>,
	Jason Merrill <jason@redhat.com>
Subject: [PATCH] restore -Wunused-variable on a typedef'd variable in a function template (PR 79548)
Date: Sat, 18 Feb 2017 02:29:00 -0000	[thread overview]
Message-ID: <28dd4f25-b208-52af-af1e-7bc5e36db557@gmail.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 649 bytes --]

The attached patch fixes bug 79548 - [5/6/7 Regression] missing
-Wunused-variable on a typedef'd variable in a function template,
most likely broken by the introduction of -Wunused-local-typedefs.

While testing the patch I came across a couple of other bugs:

   79585 - spurious -Wunused-variable on a pointer with attribute
           unused in function template
and

   79586 - missing -Wdeprecated depending on position of attribute

The test I added for 79548 fails two assertions due to the first
of these two so I xfailed them.  The second doesn't have an impact
on it.  Neither of these is a regression so I didn't try to fix them.

Martin

[-- Attachment #2: gcc-79548.diff --]
[-- Type: text/x-patch, Size: 3721 bytes --]

PR c++/79548 - [5/6/7 Regression] missing -Wunused-variable on a typedef'd variable in a function template

gcc/cp/ChangeLog:

	PR c++/79548
	* decl.c (poplevel): Avoid diagnosing entities declared with
	attribute unused.
	(initialize_local_var): Do not consider the type of a variable
	when determining whether or not it's used.
	
gcc/testsuite/ChangeLog:

	PR c++/79548
	* g++.dg/warn/Wunused-var-26.C: New test.

diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c
index 70c44fb..e315ad0 100644
--- a/gcc/cp/decl.c
+++ b/gcc/cp/decl.c
@@ -664,7 +664,8 @@ poplevel (int keep, int reverse, int functionbody)
 	    && (!CLASS_TYPE_P (type)
 		|| !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
 		|| lookup_attribute ("warn_unused",
-				     TYPE_ATTRIBUTES (TREE_TYPE (decl)))))
+				     TYPE_ATTRIBUTES (TREE_TYPE (decl))))
+	    && !lookup_attribute ("unused", TYPE_ATTRIBUTES (TREE_TYPE (decl))))
 	  {
 	    if (! TREE_USED (decl))
 	      warning_at (DECL_SOURCE_LOCATION (decl),
@@ -6546,7 +6547,6 @@ initialize_local_var (tree decl, tree init)
 {
   tree type = TREE_TYPE (decl);
   tree cleanup;
-  int already_used;
 
   gcc_assert (VAR_P (decl)
 	      || TREE_CODE (decl) == RESULT_DECL);
@@ -6564,7 +6564,7 @@ initialize_local_var (tree decl, tree init)
     return;
 
   /* Compute and store the initial value.  */
-  already_used = TREE_USED (decl) || TREE_USED (type);
+  bool already_used = TREE_USED (decl);
   if (TREE_USED (type))
     DECL_READ_P (decl) = 1;
 
diff --git a/gcc/testsuite/g++.dg/warn/Wunused-var-26.C b/gcc/testsuite/g++.dg/warn/Wunused-var-26.C
new file mode 100644
index 0000000..562f25b
--- /dev/null
+++ b/gcc/testsuite/g++.dg/warn/Wunused-var-26.C
@@ -0,0 +1,127 @@
+// PR c++/79548 - missing -Wunused-variable on a typedef'd variable
+// in a function template
+// { dg-do compile }
+// { dg-options "-Wunused" }
+
+
+#define UNUSED __attribute__ ((unused))
+
+template <class T>
+void f_int ()
+{
+  T t;                        // { dg-warning "unused variable" }
+
+  typedef T U;
+  U u;                        // { dg-warning "unused variable" }
+}
+
+template void f_int<int>();
+
+
+template <class T>
+void f_intptr ()
+{
+  T *t = 0;                   // { dg-warning "unused variable" }
+
+  typedef T U;
+  U *u = 0;                   // { dg-warning "unused variable" }
+}
+
+template void f_intptr<int>();
+
+
+template <class T>
+void f_var_unused ()
+{
+  // The variable is marked unused.
+  T t UNUSED;
+
+  typedef T U;
+  U u UNUSED;
+}
+
+template void f_var_unused<int>();
+
+
+template <class T>
+void f_var_type_unused ()
+{
+  // The variable's type is marked unused.
+  T* UNUSED t = new T;   // { dg-bogus "unused variable" "bug 79585" { xfail *-*-* } }
+
+  typedef T U;
+  U* UNUSED u = new U;   // { dg-bogus "unused variable" "bug 79585" { xfail *-*-* } }
+
+  typedef T UNUSED U;
+  U v = U ();   // { dg-bogus "unused variable" "bug 79585" { xfail *-*-* } }
+}
+
+template void f_var_type_unused<int>();
+
+
+struct A { int i; };
+
+template <class T>
+void f_A ()
+{
+  T t;                        // { dg-warning "unused variable" }
+
+  typedef T U;
+  U u;                        // { dg-warning "unused variable" }
+}
+
+template void f_A<A>();
+
+
+template <class T>
+void f_A_unused ()
+{
+  T t UNUSED;
+
+  typedef T U;
+  U u UNUSED;
+}
+
+template void f_A_unused<A>();
+
+
+struct B { B (); };
+
+template <class T>
+void f_B ()
+{
+  T t;
+
+  typedef T U;
+  U u;
+}
+
+template void f_B<B>();
+
+
+struct C { ~C (); };
+
+template <class T>
+void f_C ()
+{
+  T t;
+
+  typedef T U;
+  U u;
+}
+
+template void f_C<C>();
+
+
+struct D { B b; };
+
+template <class T>
+void f_D ()
+{
+  T t;
+
+  typedef T U;
+  U u;
+}
+
+template void f_D<D>();

             reply	other threads:[~2017-02-18  1:07 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-02-18  2:29 Martin Sebor [this message]
2017-02-21 18:10 ` Jason Merrill
2017-02-21 19:09   ` Martin Sebor
2017-02-21 20:35     ` Jason Merrill
2017-02-22  7:48       ` Martin Sebor
2017-02-22 18:10         ` Jason Merrill
2017-02-22 23:52           ` Martin Sebor
2017-02-23  1:11             ` Jason Merrill
2017-02-23 21:06               ` Martin Sebor
2017-02-23 23:48                 ` Jason Merrill
2017-03-20 21:12                   ` Jason Merrill
2017-03-21 18:40                     ` Martin Sebor
2017-03-21 19:28                       ` Jason Merrill
2017-03-21 22:37                         ` Martin Sebor

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=28dd4f25-b208-52af-af1e-7bc5e36db557@gmail.com \
    --to=msebor@gmail.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jason@redhat.com \
    /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).