Patchwork PR c++/54372 - unused attribute inactive on dependant entities

login
register
mail settings
Submitter Dodji Seketeli
Date Sept. 20, 2012, 2:01 p.m.
Message ID <m3boh0oksx.fsf@redhat.com>
Download mbox | patch
Permalink /patch/185395/
State New
Headers show

Comments

Dodji Seketeli - Sept. 20, 2012, 2:01 p.m.
Hello,

In the example of this patch, gcc/g++ invoked with
-Wunused-local-typedefs warns on dependant entities even when those
are decorated with the 'unused' attribute.

This is because in cplus_decl_attributes, save_template_attributes
makes so that the 'unused' attribute is applied to its appertaining
entity only at instantiation time.  But then at parsing time
maybe_warn_unused_local_typedefs checks for TREE_USED before warning.

This patch makes maybe_warn_unused_local_typedefs check for the
syntactic presence of the 'unused' attribute.

Tested on x86_64-unknown-linux-gnu against trunk.

gcc/c-family/

	* c-common.c (maybe_warn_unused_local_typedefs): Check for the
	presence of the attribute 'unused'.

gcc/testsuite/

	* g++.dg/warn/Wunused-local-typedefs-2.C: New test.
---
 gcc/c-family/c-common.c                            |    3 +-
 .../g++.dg/warn/Wunused-local-typedefs-2.C         |   35 ++++++++++++++++++++
 2 files changed, 37 insertions(+), 1 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/warn/Wunused-local-typedefs-2.C
Jason Merrill - Sept. 24, 2012, 7:36 p.m.
On 09/20/2012 10:01 AM, Dodji Seketeli wrote:
> This is because in cplus_decl_attributes, save_template_attributes
> makes so that the 'unused' attribute is applied to its appertaining
> entity only at instantiation time.  But then at parsing time
> maybe_warn_unused_local_typedefs checks for TREE_USED before warning.

I guess we should propagate TREE_USED when instantiating a typedef.

Jason

Patch

diff --git a/gcc/c-family/c-common.c b/gcc/c-family/c-common.c
index 6de2f1c..cf8b1e1 100644
--- a/gcc/c-family/c-common.c
+++ b/gcc/c-family/c-common.c
@@ -10899,7 +10899,8 @@  maybe_warn_unused_local_typedefs (void)
       && errorcount == unused_local_typedefs_warn_count)
     {
       FOR_EACH_VEC_ELT (tree, l->local_typedefs, i, decl)
-	if (!TREE_USED (decl))
+	if (!TREE_USED (decl)
+	    && !lookup_attribute ("unused", DECL_ATTRIBUTES (decl)))
 	  warning_at (DECL_SOURCE_LOCATION (decl),
 		      OPT_Wunused_local_typedefs,
 		      "typedef %qD locally defined but not used", decl);
diff --git a/gcc/testsuite/g++.dg/warn/Wunused-local-typedefs-2.C b/gcc/testsuite/g++.dg/warn/Wunused-local-typedefs-2.C
new file mode 100644
index 0000000..77bacd7
--- /dev/null
+++ b/gcc/testsuite/g++.dg/warn/Wunused-local-typedefs-2.C
@@ -0,0 +1,35 @@ 
+/*  Origin PR c++/54372
+    { dg-options "-Wunused-local-typedefs" }
+    { dg-do compile }
+*/
+
+template <typename T>
+void f2()
+{
+    typedef T t __attribute__((unused));
+}
+
+class S
+{
+    template <typename T>
+    void f4()
+    {
+	typedef T t __attribute__((unused));
+    }
+};
+
+template <typename T>
+class tS
+{
+    void f()
+    {
+	typedef T t2 __attribute__((unused));
+    }
+
+    template <typename U>
+    void f2()
+    {
+	typedef T t1 __attribute__((unused));
+	typedef U t2 __attribute__((unused));
+    }
+};