Patchwork Fwd: [PATCH] Attribute for unused warning for variables of non-trivial types

login
register
mail settings
Submitter Jason Merrill
Date July 13, 2013, 11:15 p.m.
Message ID <51E1DFAB.20103@redhat.com>
Download mbox | patch
Permalink /patch/258872/
State New
Headers show

Comments

Jason Merrill - July 13, 2013, 11:15 p.m.
On 07/08/2013 10:32 AM, Jason Merrill wrote:
> Was it a deliberate decision to put this in the c-common attributes
> rather than the C++-specific ones?  I'm not saying it's wrong, just
> interested in your thinking.

I think it makes sense to leave it in c-common so that types shared 
between C and C++ can use it without a warning.

I've fixed up the other issues and am applying this patch:
Lubos Lunak - July 14, 2013, 8:53 p.m.
Sorry, I haven't found time for this until now.

On Sunday 14 of July 2013, Jason Merrill wrote:
> On 07/08/2013 10:32 AM, Jason Merrill wrote:
> > Was it a deliberate decision to put this in the c-common attributes
> > rather than the C++-specific ones?  I'm not saying it's wrong, just
> > interested in your thinking.
>
> I think it makes sense to leave it in c-common so that types shared
> between C and C++ can use it without a warning.

 Given the types need to have a ctor/dtor in order for this warning to make 
sense, this should not be needed. I actually didn't put it in c-common 
attributes on purpuse, I just based it on another attribute without realizing 
there was place elsewhere for C++-specific ones. I can write a patch to move 
this if wanted.

> I've fixed up the other issues and am applying this patch:

 Thanks a lot.

 As for the copyright assignment, AFAIK there is a generic SUSE one that 
should cover this as well.
Jason Merrill - July 15, 2013, 12:59 a.m.
On 07/14/2013 01:53 PM, Lubos Lunak wrote:
>   As for the copyright assignment, AFAIK there is a generic SUSE one that
> should cover this as well.

Ah, yes.

Jason

Patch

commit d52bd579a6f2fc0cfa762553013fb161301018fe
Author: Jason Merrill <jason@redhat.com>
Date:   Thu Jul 4 09:03:53 2013 -0700

    	PR c++/55203
    c-family/
    	* c-common.c (c_common_attribute_table): Add warn_unused.
    	(handle_warn_unused_attribute): New.
    cp/
    	* init.c (build_aggr_init): Check for warn_unused attribute.
    	* decl.c (poplevel): Likewise.

diff --git a/gcc/c-family/c-common.c b/gcc/c-family/c-common.c
index 61300cd..970f9f2 100644
--- a/gcc/c-family/c-common.c
+++ b/gcc/c-family/c-common.c
@@ -368,6 +368,7 @@  static tree handle_optimize_attribute (tree *, tree, tree, int, bool *);
 static tree ignore_attribute (tree *, tree, tree, int, bool *);
 static tree handle_no_split_stack_attribute (tree *, tree, tree, int, bool *);
 static tree handle_fnspec_attribute (tree *, tree, tree, int, bool *);
+static tree handle_warn_unused_attribute (tree *, tree, tree, int, bool *);
 
 static void check_function_nonnull (tree, int, tree *);
 static void check_nonnull_arg (void *, tree, unsigned HOST_WIDE_INT);
@@ -738,6 +739,8 @@  const struct attribute_spec c_common_attribute_table[] =
      The name contains space to prevent its usage in source code.  */
   { "fn spec",	 	      1, 1, false, true, true,
 			      handle_fnspec_attribute, false },
+  { "warn_unused",            0, 0, false, false, false,
+			      handle_warn_unused_attribute, false },
   { NULL,                     0, 0, false, false, false, NULL, false }
 };
 
@@ -7950,6 +7953,27 @@  handle_fnspec_attribute (tree *node ATTRIBUTE_UNUSED, tree ARG_UNUSED (name),
   return NULL_TREE;
 }
 
+/* Handle a "warn_unused" attribute; arguments as in
+   struct attribute_spec.handler.  */
+
+static tree
+handle_warn_unused_attribute (tree *node, tree name,
+			      tree args ATTRIBUTE_UNUSED,
+			      int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
+{
+  if (TYPE_P (*node))
+    /* Do nothing else, just set the attribute.  We'll get at
+       it later with lookup_attribute.  */
+    ;
+  else
+    {
+      warning (OPT_Wattributes, "%qE attribute ignored", name);
+      *no_add_attrs = true;
+    }
+
+  return NULL_TREE;
+}
+
 /* Handle a "returns_twice" attribute; arguments as in
    struct attribute_spec.handler.  */
 
diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c
index 54bede0..c97134c 100644
--- a/gcc/cp/decl.c
+++ b/gcc/cp/decl.c
@@ -630,7 +630,9 @@  poplevel (int keep, int reverse, int functionbody)
 	    && DECL_NAME (decl) && ! DECL_ARTIFICIAL (decl)
 	    && type != error_mark_node
 	    && (!CLASS_TYPE_P (type)
-		|| !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)))
+		|| !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
+		|| lookup_attribute ("warn_unused",
+				     TYPE_ATTRIBUTES (TREE_TYPE (decl)))))
 	  {
 	    if (! TREE_USED (decl))
 	      warning (OPT_Wunused_variable, "unused variable %q+D", decl);
diff --git a/gcc/cp/init.c b/gcc/cp/init.c
index 808803d..3bff509 100644
--- a/gcc/cp/init.c
+++ b/gcc/cp/init.c
@@ -1504,7 +1504,8 @@  build_aggr_init (tree exp, tree init, int flags, tsubst_flags_t complain)
       return stmt_expr;
     }
 
-  if (VAR_P (exp) || TREE_CODE (exp) == PARM_DECL)
+  if ((VAR_P (exp) || TREE_CODE (exp) == PARM_DECL)
+      && !lookup_attribute ("warn_unused", TYPE_ATTRIBUTES (type)))
     /* Just know that we've seen something for this node.  */
     TREE_USED (exp) = 1;
 
diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi
index 1c85a3e..721c9b1 100644
--- a/gcc/doc/extend.texi
+++ b/gcc/doc/extend.texi
@@ -16377,6 +16377,23 @@  only be applied to classes declared within an @code{extern "Java"} block.
 Calls to methods declared in this interface are dispatched using GCJ's
 interface table mechanism, instead of regular virtual table dispatch.
 
+@item warn_unused
+@cindex @code{warn_unused} attribute
+
+For C++ types with non-trivial constructors and/or destructors it is
+impossible for the compiler to determine whether a variable of this
+type is truly unused if it is not referenced. This type attribute
+informs the compiler that variables of this type should be warned
+about if they appear to be unused, just like variables of fundamental
+types.
+
+This attribute is appropriate for types which just represent a value,
+such as @code{std::string}; it is not appropriate for types which
+control a resource, such as @code{std::mutex}.
+
+This attribute is also accepted in C, but it is unnecessary because C
+does not have constructors or destructors.
+
 @end table
 
 See also @ref{Namespace Association}.
diff --git a/gcc/testsuite/g++.dg/warn/warn_unused.C b/gcc/testsuite/g++.dg/warn/warn_unused.C
new file mode 100644
index 0000000..af687fc
--- /dev/null
+++ b/gcc/testsuite/g++.dg/warn/warn_unused.C
@@ -0,0 +1,22 @@ 
+// { dg-do compile }
+// { dg-options -Wunused }
+
+struct __attribute__((warn_unused)) Test
+{
+    Test();
+    ~Test();
+    void use();
+};
+
+struct TestNormal
+{
+    TestNormal();
+};
+
+int main()
+{
+   Test unused;         // { dg-warning "unused variable" }
+   Test used;           // { dg-bogus "unused variable" }
+   TestNormal normal;   // { dg-bogus "unused variable" }
+   used.use();
+}