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

login
register
mail settings
Submitter Dodji Seketeli
Date Sept. 25, 2012, 11:57 a.m.
Message ID <87d31ab9ig.fsf@redhat.com>
Download mbox | patch
Permalink /patch/186794/
State New
Headers show

Comments

Dodji Seketeli - Sept. 25, 2012, 11:57 a.m.
Jason Merrill <jason@redhat.com> writes:

> 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.

As we discussed on IRC, this will not really help, as what we want is to
be able to take advantage of the attribute at compile time.

Rather, the patch below makes is_late_template_attribute the "unused"
attribute be applied directly, as you suggested.

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

---
 gcc/cp/decl2.c                                     |  5 ++++
 .../c-c++-common/Wunused-local-typedefs-2.c        | 35 ++++++++++++++++++++++
 2 files changed, 40 insertions(+)
 create mode 100644 gcc/testsuite/c-c++-common/Wunused-local-typedefs-2.c
Jason Merrill - Sept. 25, 2012, 6:02 p.m.
OK.

Jason

Patch

diff --git a/gcc/cp/decl2.c b/gcc/cp/decl2.c
index 0df4613..a590d17 100644
--- a/gcc/cp/decl2.c
+++ b/gcc/cp/decl2.c
@@ -1104,6 +1104,11 @@  is_late_template_attribute (tree attr, tree decl)
   if (is_attribute_p ("weak", name))
     return true;
 
+  /* Attribute unused is applied directly, as it appertains to
+     decls. */
+  if (is_attribute_p ("unused", name))
+    return false;
+
   /* If any of the arguments are dependent expressions, we can't evaluate
      the attribute until instantiation time.  */
   for (arg = args; arg; arg = TREE_CHAIN (arg))
diff --git a/gcc/testsuite/c-c++-common/Wunused-local-typedefs-2.c b/gcc/testsuite/c-c++-common/Wunused-local-typedefs-2.c
new file mode 100644
index 0000000..77bacd7
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/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));
+    }
+};