Re: [PATCH] PR c++/33255 - Support -Wunused-local-typedefs warning

2011-11-07 Thread Jason Merrill
The bootstrap issues with a recent patch of mine led me to run make 
check-g++-strict-gc, which turned up a GC problem with this code, fixed 
thus.


Tested x86_64-pc-linux-gnu, applying to trunk.
commit f2f82e0908b807a0d339a00b434913fc0886c0cc
Author: Jason Merrill ja...@redhat.com
Date:   Mon Nov 7 09:19:27 2011 -0500

	PR c++/33255
	* decl.c (save_function_data): Clear local_typedefs.

diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c
index 3b283d8..63da51d 100644
--- a/gcc/cp/decl.c
+++ b/gcc/cp/decl.c
@@ -13021,6 +13021,7 @@ save_function_data (tree decl)
   f-base.x_stmt_tree.x_cur_stmt_list = NULL;
   f-bindings = NULL;
   f-x_local_names = NULL;
+  f-base.local_typedefs = NULL;
 }
 
 


Re: [PATCH] PR c++/33255 - Support -Wunused-local-typedefs warning

2011-09-08 Thread H.J. Lu
On Wed, Sep 7, 2011 at 12:57 PM, Dodji Seketeli do...@redhat.com wrote:
 Jason Merrill ja...@redhat.com writes:

 On 09/07/2011 02:01 PM, Dodji Seketeli wrote:
 +
   /* Process declarations and variables for C compiler.

 Blank line at the top of the file?

 Oops, I noticed it and changed it in the aggregated patch I sent, but
 forgot to update the diff against the previous.  Sorry for that.

      Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 
 2000,
      2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
 @@ -8328,7 +8329,8 @@ finish_function (void)
     if (!decl_function_context (fndecl))
       undef_nested_function = false;

 -  cfun-language = NULL;
 +  if (cfun-language != NULL)
 +    ggc_free (cfun-language);

 You'll still want to set it to NULL after freeing it.

 Fixed.

 OK with those changes.

 Thanks.

 Here is the patch I will commit in a short while.

 gcc/

        * c-decl.c (lookup_name): Use the new
        maybe_record_typedef_use.
        (pushdecl): Use the new
        record_locally_defined_typedef.
        (store_parm_decls): Allocate cfun-language.
        (finish_function): Use the new maybe_warn_unused_local_typedefs,
        and free cfun-language.
        (c_push_function_context): Allocate cfun-language here only if
        needed.
        (c_pop_function_context): Likewise, mark cfun-language
        for collection only when it should be done.
        * c-common.c (handle_used_attribute): Don't ignore TYPE_DECL
        nodes.
        * c-typeck.c (c_expr_sizeof_type, c_cast_expr): Use the new
        maybe_record_local_typedef_use.

 gcc/c-family

        * c-common.h (struct c_language_function::local_typedefs): New
        field.
        (record_locally_defined_typedef, maybe_record_typedef_use)
        (maybe_warn_unused_local_typedefs): Declare new functions.
        * c-common.c (record_locally_defined_typedef)
        (maybe_record_typedef_use)
        (maybe_warn_unused_local_typedefs): Define new functions.
        * c.opt: Declare new -Wunused-local-typedefs flag.

 gcc/cp

        * name-lookup.c (pushdecl_maybe_friend_1): Use the new
        record_locally_defined_typedef.
        * decl.c (finish_function): Use the new
        maybe_warn_unused_local_typedefs.
        (grokfield): Use the new record_locally_defined_typedef.
        * parser.c (lookup_name): Use the new maybe_record_typedef_use.

 gcc/doc/

        * invoke.texi: Update documentation for -Wunused-local-typedefs.

 gcc/testsuite/

        * g++.dg/warn/Wunused-local-typedefs.C: New test file.
        * c-c++-common/Wunused-local-typedefs.c: Likewise.

 libstdc++-v3/

        * include/ext/bitmap_allocator.h
        (__detail::__mini_vector::__lower_bound): Remove unused typedef.
        * src/istream.cc (std::operator(basic_istreamchar __in,
        basic_stringchar __str)): Likewise.
        (std::getline): Likewise.
        * src/valarray.cc (__valarray_product): Likewise.

This may have caused:

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=50332

-- 
H.J.


Re: [PATCH] PR c++/33255 - Support -Wunused-local-typedefs warning

2011-09-08 Thread Dodji Seketeli
Jason Merrill ja...@redhat.com writes:

 I think -Wunused and -Wall should imply -Wunused-local-typedefs unless
 the user specifies -Wno-unused-local-typedefs.

Dodji Seketeli do...@redhat.com writes:

 I actually first tried this (actually adding it to -Wall -extra and
 -Wunused) and found out the following issue.
 
 A typedef can be defined in a macro in a system header, be expanded in
 a function and not be used by the function.  In this case we shouldn't
 warn, but PR preprocessor/7263 makes us warn nonetheless.  There are
 many spots of that kind in the libstdc++ test suite.
 

Jason Merrill ja...@redhat.com writes:

 Does your set of linemap patches fix the issue?  In that case, we can
 add it when those go in.  Speaking of which, sorry I haven't found the
 time to review them yet.

So, in prevision of when the patch for PR preprocessor/7263 goes in, I
am proposing this patchlet that turns on -Wunused-local-typedefs
whenever -Wunused (and so -Wall) is turned on.

I have tested it on a synthetic tree made of my current patch series for
PR preprocessor/7263, and trunk that contains -Wunused-local-typedefs
support.

Is this be OK for trunk when PR preprocessor/7263 gets in, assuming it
passes bootstrap and tests on trunk at that moment?

Thanks.


Enable -Wunused-local-typedefs when -Wall or -Wunused

gcc/

* opts.c (finish_options): Activate -Wunused-local-typedefs if
-Wunused is activated.
* doc/invoke.texi: Update blurb of -Wunused-local-typedefs.

diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
index 3aa9611..5f4afe3 100644
--- a/gcc/doc/invoke.texi
+++ b/gcc/doc/invoke.texi
@@ -3505,6 +3505,7 @@ To suppress this warning use the @samp{unused} attribute
 @item -Wunused-local-typedefs @r{(C, Objective-C, C++ and Objective-C++ only)}
 @opindex Wunused-local-typedefs
 Warn when a typedef locally defined in a function is not used.
+This warning is enabled by @option{-Wall}.
 
 @item -Wunused-parameter
 @opindex Wunused-parameter
diff --git a/gcc/opts.c b/gcc/opts.c
index 5d5bcb9..ebb99d0 100644
--- a/gcc/opts.c
+++ b/gcc/opts.c
@@ -830,6 +830,10 @@ finish_options (struct gcc_options *opts, struct 
gcc_options *opts_set,
   if (opts-x_warn_unused_value == -1)
 opts-x_warn_unused_value = opts-x_warn_unused;
 
+  /* Wunused-local-typedefs is enabled by -Wunused or -Wall.  */
+  if (opts-x_warn_unused_local_typedefs == -1)
+opts-x_warn_unused_local_typedefs = opts-x_warn_unused;
+
   /* This replaces set_Wextra.  */
   if (opts-x_warn_uninitialized == -1)
 opts-x_warn_uninitialized = opts-x_extra_warnings;
-- 
Dodji


Re: [PATCH] PR c++/33255 - Support -Wunused-local-typedefs warning

2011-09-08 Thread Jason Merrill

On 09/08/2011 04:50 PM, Dodji Seketeli wrote:

Is this be OK for trunk when PR preprocessor/7263 gets in, assuming it
passes bootstrap and tests on trunk at that moment?


Yes, except...


--- a/gcc/doc/invoke.texi
+++ b/gcc/doc/invoke.texi
@@ -3505,6 +3505,7 @@ To suppress this warning use the @samp{unused} attribute
  @item -Wunused-local-typedefs @r{(C, Objective-C, C++ and Objective-C++ only)}
  @opindex Wunused-local-typedefs
  Warn when a typedef locally defined in a function is not used.
+This warning is enabled by @option{-Wall}.


Don't you want to say This warning is also enabled by 
@option{-Wunused}, which is enabled by @option{-Wall}.?


Jason


Re: [PATCH] PR c++/33255 - Support -Wunused-local-typedefs warning

2011-09-08 Thread Dodji Seketeli
Jason Merrill ja...@redhat.com writes:

 On 09/08/2011 04:50 PM, Dodji Seketeli wrote:
 Is this be OK for trunk when PR preprocessor/7263 gets in, assuming it
 passes bootstrap and tests on trunk at that moment?

 Yes, except...

 --- a/gcc/doc/invoke.texi
 +++ b/gcc/doc/invoke.texi
 @@ -3505,6 +3505,7 @@ To suppress this warning use the @samp{unused} 
 attribute
   @item -Wunused-local-typedefs @r{(C, Objective-C, C++ and Objective-C++ 
 only)}
   @opindex Wunused-local-typedefs
   Warn when a typedef locally defined in a function is not used.
 +This warning is enabled by @option{-Wall}.

 Don't you want to say This warning is also enabled by
 @option{-Wunused}, which is enabled by @option{-Wall}.?

For the sake of consistency, I followed the pattern used for the other
-Wunused-* options in that same file.

I thought I didn't have to mention that -Wunused triggers
-Wunused-local-typedefs because a bit below this, the text for -Wunused
reads:

All the above @option{-Wunused} options combined.

And before that, each relevant -Wunused-* option is said to be triggered
by -Wall, as I did.

I would also find your phrasing more logical, if it wasn't for the
consistency constraint.

-- 
Dodji


Re: [PATCH] PR c++/33255 - Support -Wunused-local-typedefs warning

2011-09-08 Thread Jason Merrill

On 09/08/2011 05:54 PM, Dodji Seketeli wrote:

Jason Merrillja...@redhat.com  writes:


Don't you want to say This warning is also enabled by
@option{-Wunused}, which is enabled by @option{-Wall}.?


For the sake of consistency, I followed the pattern used for the other
-Wunused-* options in that same file.


OK.

Jason



Re: [PATCH] PR c++/33255 - Support -Wunused-local-typedefs warning

2011-09-07 Thread Dodji Seketeli
Jason Merrill ja...@redhat.com writes:

 On 08/08/2011 03:52 PM, Dodji Seketeli wrote:
 +  cfun-language = NULL;

 Might as well ggc_free it first.

Done.


 +  /* We want T to be either a type or a TYPE_DECL.   */

 Comment is out of date.

Removed.


 Does __attribute ((used)) on the typedef prevent the warning?

No it doesn't.  handle_used_attribute ignores the used attribute on
anything that isn't a variable or a function.  I have changed that in
the patch below.

Note that as handle_used_attribute function is called before the
typedef is properly setup (by calling set_underlying_type), I couldn't
use is_typedef_decl in it.  Is that a problem?

To ease the review, here is the changes I made against the previous
patch.

diff --git a/gcc/c-decl.c b/gcc/c-decl.c
index 230f1a7..42730d7 100644
--- a/gcc/c-decl.c
+++ b/gcc/c-decl.c
@@ -1,3 +1,4 @@
+
 /* Process declarations and variables for C compiler.
Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
@@ -8328,7 +8329,8 @@ finish_function (void)
   if (!decl_function_context (fndecl))
 undef_nested_function = false;
 
-  cfun-language = NULL;
+  if (cfun-language != NULL)
+ggc_free (cfun-language);
 
   /* We're leaving the context of this function, so zap cfun.
  It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
diff --git a/gcc/c-family/c-common.c b/gcc/c-family/c-common.c
index 290240a..bb57569 100644
--- a/gcc/c-family/c-common.c
+++ b/gcc/c-family/c-common.c
@@ -6091,7 +6091,8 @@ handle_used_attribute (tree *pnode, tree name, tree 
ARG_UNUSED (args),
   tree node = *pnode;
 
   if (TREE_CODE (node) == FUNCTION_DECL
-  || (TREE_CODE (node) == VAR_DECL  TREE_STATIC (node)))
+  || (TREE_CODE (node) == VAR_DECL  TREE_STATIC (node))
+  || (TREE_CODE (node) == TYPE_DECL))
 {
   TREE_USED (node) = 1;
   DECL_PRESERVE_P (node) = 1;
@@ -9629,7 +9630,6 @@ record_locally_defined_typedef (tree decl)
 void
 maybe_record_typedef_use (tree t)
 {
-  /* We want T to be either a type or a TYPE_DECL.   */
   if (!is_typedef_decl (t))
 return;
 
diff --git a/gcc/testsuite/c-c++-common/Wunused-local-typedefs.c 
b/gcc/testsuite/c-c++-common/Wunused-local-typedefs.c
index 32fb723..683019d 100644
--- a/gcc/testsuite/c-c++-common/Wunused-local-typedefs.c
+++ b/gcc/testsuite/c-c++-common/Wunused-local-typedefs.c
@@ -36,3 +36,9 @@ test7 (void)
   typedef int foo;
   int vec[1] = {sizeof (foo)};
 }
+
+void
+test8 (void)
+{
+  typedef int foo __attribute__((used));
+}


The consolidated patch against trunk is below.  Bootstrapped and
tested against trunk on x86_64-unknown-linux-gnu.

gcc/

* c-decl.c (lookup_name): Use the new
maybe_record_typedef_use.
(pushdecl): Use the new
record_locally_defined_typedef.
(store_parm_decls): Allocate cfun-language.
(finish_function): Use the new maybe_warn_unused_local_typedefs,
and free cfun-language.
(c_push_function_context): Allocate cfun-language here only if
needed.
(c_pop_function_context): Likewise, mark cfun-language
for collection only when it should be done.
* c-common.c (handle_used_attribute): Don't ignore TYPE_DECL
nodes.
* c-typeck.c (c_expr_sizeof_type, c_cast_expr): Use the new
maybe_record_local_typedef_use.

gcc/c-family

* c-common.h (struct c_language_function::local_typedefs): New
field.
(record_locally_defined_typedef, maybe_record_typedef_use)
(maybe_warn_unused_local_typedefs): Declare new functions.
* c-common.c (record_locally_defined_typedef)
(maybe_record_typedef_use)
(maybe_warn_unused_local_typedefs): Define new functions.
* c.opt: Declare new -Wunused-local-typedefs flag.

gcc/cp

* name-lookup.c (pushdecl_maybe_friend_1): Use the new
record_locally_defined_typedef.
* decl.c (finish_function): Use the new
maybe_warn_unused_local_typedefs.
(grokfield): Use the new record_locally_defined_typedef.
* parser.c (lookup_name): Use the new maybe_record_typedef_use.

gcc/doc/

* invoke.texi: Update documentation for -Wunused-local-typedefs.

gcc/testsuite/

* g++.dg/warn/Wunused-local-typedefs.C: New test file.
* c-c++-common/Wunused-local-typedefs.c: Likewise.

libstdc++-v3/

* include/ext/bitmap_allocator.h
(__detail::__mini_vector::__lower_bound): Remove unused typedef.
* src/istream.cc (std::operator(basic_istreamchar __in,
basic_stringchar __str)): Likewise.
(std::getline): Likewise.
* src/valarray.cc (__valarray_product): Likewise.
---
 gcc/c-decl.c   |   39 +-
 gcc/c-family/c-common.c|   73 ++-
 gcc/c-family/c-common.h|7 +
 gcc/c-family/c.opt  

Re: [PATCH] PR c++/33255 - Support -Wunused-local-typedefs warning

2011-09-07 Thread Jason Merrill

On 09/07/2011 02:01 PM, Dodji Seketeli wrote:

+
  /* Process declarations and variables for C compiler.


Blank line at the top of the file?


 Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
@@ -8328,7 +8329,8 @@ finish_function (void)
if (!decl_function_context (fndecl))
  undef_nested_function = false;

-  cfun-language = NULL;
+  if (cfun-language != NULL)
+ggc_free (cfun-language);


You'll still want to set it to NULL after freeing it.

OK with those changes.

Jason


Re: [PATCH] PR c++/33255 - Support -Wunused-local-typedefs warning

2011-09-07 Thread Dodji Seketeli
Jason Merrill ja...@redhat.com writes:

 On 09/07/2011 02:01 PM, Dodji Seketeli wrote:
 +
   /* Process declarations and variables for C compiler.

 Blank line at the top of the file?

Oops, I noticed it and changed it in the aggregated patch I sent, but
forgot to update the diff against the previous.  Sorry for that.

  Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 
 2000,
  2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
 @@ -8328,7 +8329,8 @@ finish_function (void)
 if (!decl_function_context (fndecl))
   undef_nested_function = false;

 -  cfun-language = NULL;
 +  if (cfun-language != NULL)
 +ggc_free (cfun-language);

 You'll still want to set it to NULL after freeing it.

Fixed.

 OK with those changes.

Thanks.

Here is the patch I will commit in a short while.

gcc/

* c-decl.c (lookup_name): Use the new
maybe_record_typedef_use.
(pushdecl): Use the new
record_locally_defined_typedef.
(store_parm_decls): Allocate cfun-language.
(finish_function): Use the new maybe_warn_unused_local_typedefs,
and free cfun-language.
(c_push_function_context): Allocate cfun-language here only if
needed.
(c_pop_function_context): Likewise, mark cfun-language
for collection only when it should be done.
* c-common.c (handle_used_attribute): Don't ignore TYPE_DECL
nodes.
* c-typeck.c (c_expr_sizeof_type, c_cast_expr): Use the new
maybe_record_local_typedef_use.

gcc/c-family

* c-common.h (struct c_language_function::local_typedefs): New
field.
(record_locally_defined_typedef, maybe_record_typedef_use)
(maybe_warn_unused_local_typedefs): Declare new functions.
* c-common.c (record_locally_defined_typedef)
(maybe_record_typedef_use)
(maybe_warn_unused_local_typedefs): Define new functions.
* c.opt: Declare new -Wunused-local-typedefs flag.

gcc/cp

* name-lookup.c (pushdecl_maybe_friend_1): Use the new
record_locally_defined_typedef.
* decl.c (finish_function): Use the new
maybe_warn_unused_local_typedefs.
(grokfield): Use the new record_locally_defined_typedef.
* parser.c (lookup_name): Use the new maybe_record_typedef_use.

gcc/doc/

* invoke.texi: Update documentation for -Wunused-local-typedefs.

gcc/testsuite/

* g++.dg/warn/Wunused-local-typedefs.C: New test file.
* c-c++-common/Wunused-local-typedefs.c: Likewise.

libstdc++-v3/

* include/ext/bitmap_allocator.h
(__detail::__mini_vector::__lower_bound): Remove unused typedef.
* src/istream.cc (std::operator(basic_istreamchar __in,
basic_stringchar __str)): Likewise.
(std::getline): Likewise.
* src/valarray.cc (__valarray_product): Likewise.
---
 gcc/c-decl.c   |   42 +-
 gcc/c-family/c-common.c|   73 ++-
 gcc/c-family/c-common.h|7 +
 gcc/c-family/c.opt |4 +
 gcc/cp/decl.c  |4 +
 gcc/cp/decl2.c |1 +
 gcc/cp/name-lookup.c   |7 +
 gcc/cp/parser.c|2 +
 gcc/doc/invoke.texi|9 +-
 .../c-c++-common/Wunused-local-typedefs.c  |   44 ++
 gcc/testsuite/g++.dg/warn/Wunused-local-typedefs.C |  146 
 libstdc++-v3/include/ext/bitmap_allocator.h|2 -
 libstdc++-v3/src/istream.cc|3 -
 libstdc++-v3/src/valarray.cc   |1 -
 14 files changed, 330 insertions(+), 15 deletions(-)
 create mode 100644 gcc/testsuite/c-c++-common/Wunused-local-typedefs.c
 create mode 100644 gcc/testsuite/g++.dg/warn/Wunused-local-typedefs.C

diff --git a/gcc/c-decl.c b/gcc/c-decl.c
index d683d4e..5d4564a 100644
--- a/gcc/c-decl.c
+++ b/gcc/c-decl.c
@@ -2769,7 +2769,15 @@ pushdecl (tree x)
 
  skip_external_and_shadow_checks:
   if (TREE_CODE (x) == TYPE_DECL)
-set_underlying_type (x);
+{
+  /* So this is a typedef, set its underlying type.  */
+  set_underlying_type (x);
+
+  /* If X is a typedef defined in the current function, record it
+for the purpose of implementing the -Wunused-local-typedefs
+warning.  */
+  record_locally_defined_typedef (x);
+}
 
   bind (name, x, scope, /*invisible=*/false, nested, locus);
 
@@ -3435,7 +3443,10 @@ lookup_name (tree name)
 {
   struct c_binding *b = I_SYMBOL_BINDING (name);
   if (b  !b-invisible)
-return b-decl;
+{
+  maybe_record_typedef_use (b-decl);
+  return b-decl;
+}
   return 0;
 }
 
@@ -8192,6 +8203,9 @@ store_parm_decls (void)
   /* Initialize the RTL code for the function.  */
   allocate_struct_function (fndecl, 

Re: [PATCH] PR c++/33255 - Support -Wunused-local-typedefs warning

2011-09-06 Thread Jason Merrill

On 08/08/2011 03:52 PM, Dodji Seketeli wrote:

+  cfun-language = NULL;


Might as well ggc_free it first.


+  /* We want T to be either a type or a TYPE_DECL.   */


Comment is out of date.

Does __attribute ((used)) on the typedef prevent the warning?

Jason



[PING] [PATCH] PR c++/33255 - Support -Wunused-local-typedefs warning

2011-09-01 Thread Dodji Seketeli
I am friendly pinging this patch.


Dodji Seketeli do...@redhat.com a écrit:

 Hello,

 Jason Merrill ja...@redhat.com writes:

 On 08/06/2011 06:57 AM, Dodji Seketeli wrote:
  @@ -4340,6 +4340,8 @@ c_sizeof_or_alignof_type (location_t loc,
 value = fold_convert_loc (loc, size_type_node, value);
 gcc_assert (!TYPE_IS_SIZETYPE (TREE_TYPE (value)));
 
  +  maybe_record_local_typedef_use (type);
 
 Why is this still needed?

 It is not.  I just forgot to remove it.  Done now.

 
  +/* Return TRUE if DECL is declared in the current function.  */
  +
  +bool
  +local_decl_p (tree decl)
 
 I don't think we need to work so hard to compare contexts to
 current_function_decl; just checking that the decl is in some function
 should be enough, i.e. decl_function_context is non-null.

 Thanks.  I didn't know about decl_function_context.  I grepped for
 functions containing local in their names but I was doomed to
 failure.  I am using that now.

 
  +  static int unused_local_typedefs_warn_count;
 
 This could use a comment.

 Added.

 
  +/* If T is a typedef variant type or a TYPE_DECL declared locally,
  +   mark it as used.  */
  +
  +void
  +maybe_record_local_typedef_use (tree t)
 
 Does this still need to handle types?  And the meaning of TREE_USED
 shouldn't change depending on a warning flag or whether a decl is
 local or not; let's just mark all TYPE_DECLs as used.

 OK, done.

 
 Seems like we don't need local_decl_p at all; at the definition point,
 if we're in a function, a new declaration must be local enough for our
 purposes.
 
  +void
  +cp_maybe_record_local_typedef_use (tree t)
  +{
  +  /*  If the current function is being instantiated, bail out.  */
 
 As above, I think we shouldn't put so many conditions on setting
 TREE_USED.

 I have removed cp_maybe_record_local_typedef_use altogether and just
 use maybe_record_typedef_use now.

 
  @@ -19572,6 +19572,9 @@ cp_parser_lookup_name (cp_parser *parser, tree 
  name,
 if (DECL_P (decl))
   check_accessibility_of_qualified_id (decl, object_type, 
  parser-scope);
 
  +  cp_maybe_record_local_typedef_use (decl);
  +  cp_maybe_record_local_typedef_use (parser-scope);
 
 Why do we need to handle parser-scope?  Wasn't it previously looked up?

 Sigh, my bad..  Fixed.

 Below is the updated patch that passed bootstrap and regression tests
 on x86_64-unknown-linux-gnu against trunk.

 Thanks.

 From: Dodji Seketeli do...@redhat.com
 Date: Mon, 25 Jul 2011 19:02:07 +0200
 Subject: [PATCH] PR c++/33255 - Support -Wunused-local-typedefs warning

 gcc/

   * c-decl.c (lookup_name): Use the new
   maybe_record_typedef_use.
   (pushdecl): Use the new
   record_locally_defined_typedef.
   (store_parm_decls): Allocate cfun-language.
   (finish_function): Use the new maybe_warn_unused_local_typedefs,
   and mark cfun-language to be collected.
   (c_push_function_context): Allocate cfun-language here only if
   needed.
   (c_pop_function_context): Likewise, mark cfun-language
   for collection only when it should be done.
   * c-typeck.c (c_expr_sizeof_type, c_cast_expr): Use the new
   maybe_record_local_typedef_use.

 gcc/c-family

   * c-common.h (struct c_language_function::local_typedefs): New
   field.
   (record_locally_defined_typedef, maybe_record_typedef_use)
   (maybe_warn_unused_local_typedefs): Declare new functions.
   * c-common.c (record_locally_defined_typedef)
   (maybe_record_typedef_use)
   (maybe_warn_unused_local_typedefs): Define new functions.
   * c.opt: Declare new -Wunused-local-typedefs flag.

 gcc/cp

   * name-lookup.c (pushdecl_maybe_friend_1): Use the new
   record_locally_defined_typedef.
   * decl.c (finish_function): Use the new
   maybe_warn_unused_local_typedefs.
   (grokfield): Use the new record_locally_defined_typedef.
   * parser.c (lookup_name): Use the new maybe_record_typedef_use.

 gcc/doc/

   * invoke.texi: Update documentation for -Wunused-local-typedefs.

 gcc/testsuite/

   * g++.dg/warn/Wunused-local-typedefs.C: New test file.
   * c-c++-common/Wunused-local-typedefs.c: Likewise.

 libstdc++-v3/

   * include/ext/bitmap_allocator.h
   (__detail::__mini_vector::__lower_bound): Remove unused typedef.
   * src/istream.cc (std::operator(basic_istreamchar __in,
   basic_stringchar __str)): Likewise.
   (std::getline): Likewise.
   * src/valarray.cc (__valarray_product): Likewise.
 ---
  gcc/c-decl.c   |   38 +-
  gcc/c-family/c-common.c|   71 ++
  gcc/c-family/c-common.h|7 +
  gcc/c-family/c.opt |4 +
  gcc/cp/decl.c  |4 +
  gcc/cp/decl2.c |1 +
  gcc/cp/name-lookup.c   |7 +
  gcc/cp/parser.c

Re: [PATCH] PR c++/33255 - Support -Wunused-local-typedefs warning

2011-08-08 Thread Dodji Seketeli
Hello,

Jason Merrill ja...@redhat.com writes:

 On 08/06/2011 06:57 AM, Dodji Seketeli wrote:
  @@ -4340,6 +4340,8 @@ c_sizeof_or_alignof_type (location_t loc,
 value = fold_convert_loc (loc, size_type_node, value);
 gcc_assert (!TYPE_IS_SIZETYPE (TREE_TYPE (value)));
 
  +  maybe_record_local_typedef_use (type);
 
 Why is this still needed?

It is not.  I just forgot to remove it.  Done now.

 
  +/* Return TRUE if DECL is declared in the current function.  */
  +
  +bool
  +local_decl_p (tree decl)
 
 I don't think we need to work so hard to compare contexts to
 current_function_decl; just checking that the decl is in some function
 should be enough, i.e. decl_function_context is non-null.

Thanks.  I didn't know about decl_function_context.  I grepped for
functions containing local in their names but I was doomed to
failure.  I am using that now.

 
  +  static int unused_local_typedefs_warn_count;
 
 This could use a comment.

Added.

 
  +/* If T is a typedef variant type or a TYPE_DECL declared locally,
  +   mark it as used.  */
  +
  +void
  +maybe_record_local_typedef_use (tree t)
 
 Does this still need to handle types?  And the meaning of TREE_USED
 shouldn't change depending on a warning flag or whether a decl is
 local or not; let's just mark all TYPE_DECLs as used.

OK, done.

 
 Seems like we don't need local_decl_p at all; at the definition point,
 if we're in a function, a new declaration must be local enough for our
 purposes.
 
  +void
  +cp_maybe_record_local_typedef_use (tree t)
  +{
  +  /*  If the current function is being instantiated, bail out.  */
 
 As above, I think we shouldn't put so many conditions on setting
 TREE_USED.

I have removed cp_maybe_record_local_typedef_use altogether and just
use maybe_record_typedef_use now.

 
  @@ -19572,6 +19572,9 @@ cp_parser_lookup_name (cp_parser *parser, tree name,
 if (DECL_P (decl))
   check_accessibility_of_qualified_id (decl, object_type, parser-scope);
 
  +  cp_maybe_record_local_typedef_use (decl);
  +  cp_maybe_record_local_typedef_use (parser-scope);
 
 Why do we need to handle parser-scope?  Wasn't it previously looked up?

Sigh, my bad..  Fixed.

Below is the updated patch that passed bootstrap and regression tests
on x86_64-unknown-linux-gnu against trunk.

Thanks.

From: Dodji Seketeli do...@redhat.com
Date: Mon, 25 Jul 2011 19:02:07 +0200
Subject: [PATCH] PR c++/33255 - Support -Wunused-local-typedefs warning

gcc/

* c-decl.c (lookup_name): Use the new
maybe_record_typedef_use.
(pushdecl): Use the new
record_locally_defined_typedef.
(store_parm_decls): Allocate cfun-language.
(finish_function): Use the new maybe_warn_unused_local_typedefs,
and mark cfun-language to be collected.
(c_push_function_context): Allocate cfun-language here only if
needed.
(c_pop_function_context): Likewise, mark cfun-language
for collection only when it should be done.
* c-typeck.c (c_expr_sizeof_type, c_cast_expr): Use the new
maybe_record_local_typedef_use.

gcc/c-family

* c-common.h (struct c_language_function::local_typedefs): New
field.
(record_locally_defined_typedef, maybe_record_typedef_use)
(maybe_warn_unused_local_typedefs): Declare new functions.
* c-common.c (record_locally_defined_typedef)
(maybe_record_typedef_use)
(maybe_warn_unused_local_typedefs): Define new functions.
* c.opt: Declare new -Wunused-local-typedefs flag.

gcc/cp

* name-lookup.c (pushdecl_maybe_friend_1): Use the new
record_locally_defined_typedef.
* decl.c (finish_function): Use the new
maybe_warn_unused_local_typedefs.
(grokfield): Use the new record_locally_defined_typedef.
* parser.c (lookup_name): Use the new maybe_record_typedef_use.

gcc/doc/

* invoke.texi: Update documentation for -Wunused-local-typedefs.

gcc/testsuite/

* g++.dg/warn/Wunused-local-typedefs.C: New test file.
* c-c++-common/Wunused-local-typedefs.c: Likewise.

libstdc++-v3/

* include/ext/bitmap_allocator.h
(__detail::__mini_vector::__lower_bound): Remove unused typedef.
* src/istream.cc (std::operator(basic_istreamchar __in,
basic_stringchar __str)): Likewise.
(std::getline): Likewise.
* src/valarray.cc (__valarray_product): Likewise.
---
 gcc/c-decl.c   |   38 +-
 gcc/c-family/c-common.c|   71 ++
 gcc/c-family/c-common.h|7 +
 gcc/c-family/c.opt |4 +
 gcc/cp/decl.c  |4 +
 gcc/cp/decl2.c |1 +
 gcc/cp/name-lookup.c   |7 +
 gcc/cp/parser.c|2 +
 gcc/doc/invoke.texi

Re: [PATCH] PR c++/33255 - Support -Wunused-local-typedefs warning

2011-08-06 Thread Dodji Seketeli
So I finally got back to this and updated the patch according to the
comments below.

Jason Merrill ja...@redhat.com writes:

 On 07/27/2011 01:54 PM, Dodji Seketeli wrote:
 +  /*  Set of typedefs that are used in this function.  */
 +  struct pointer_set_t * GTY((skip)) used_local_typedefs;

 Is there a reason not to just use TREE_USED for this?

Done now.

 +  /* Vector of locally defined typedefs, for
 + -Wunused-local-typedefs.  */
 +  VEC(tree,gc) *local_typedefs;

 If the accessors are in c-common, this field should be in
 c_language_function.

Done, see my comment below.


 +  /* We are only interested in a typedef declared locally.  */
 +  if (DECL_CONTEXT (typedef_decl) != current_function_decl)
 +return;

 What if it's used in a nested function/local class/lambda?


Oops.  Good catch.  I have added a local_decl_p predicated and used that
instead.  I have also added a test case for this.

 @@ -4175,6 +4175,9 @@ mark_used (tree decl)

/* Set TREE_USED for the benefit of -Wunused.  */
TREE_USED (decl) = 1;
 +
 +  maybe_record_local_typedef_use (TREE_TYPE (decl));

 Why is this needed?  If the decl has the typedef for a type, we should
 have already marked it as used in grokdeclarator.

 Actually, couldn't we just mark a typedef as used when when lookup
 finds it?  That would avoid having to mark in so many places and avoid
 the need for walk_tree.

I have done this and it's indeed much better.  Thanks.

 I think -Wunused and -Wall should imply -Wunused-local-typedefs unless
 the user specifies -Wno-unused-local-typedefs.

[...]

 Does your set of linemap patches fix the issue?  In that case, we can
 add it when those go in.

OK.

Joseph S. Myers jos...@codesourcery.com writes:

 On Fri, 29 Jul 2011, Jason Merrill wrote:

  Looking a bit further, it looks like the C FE uses cfun-language only
  to store the context of the outer function when faced with a nested
  function.  This is done by c_push_function_context, called by
  c_parser_declaration_or_fndef.  Otherwise, cfun-language is not
  allocated.  Is it appropriate that -Wunused-local-typedefs allocates it
  as well?
 
 I think so.  Joseph?

 Seems reasonable.

So I have done this in the C FE.  -Wunused-local-typedefs allocates
cfun-language when the cfun for the current function is created, and
let it be garbage-collected at the end of the current function.

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

gcc/

* c-decl.c (lookup_name): Use the new
maybe_record_local_typedef_use.
(pushdecl): Use the new
record_locally_defined_typedef.
(store_parm_decls): Allocate cfun-language.
(finish_function): Use the new maybe_warn_unused_local_typedefs,
and mark cfunlanguage to be collected.
(c_push_function_context): Allocate cfun-language here only if
needed.
(c_pop_function_context): Likewise, mark cfun-language
for collection only when it should be done.
* c-typeck.c (c_expr_sizeof_type, c_cast_expr): Use the new
maybe_record_local_typedef_use.

gcc/c-family

* c-common.h (struct c_language_function::local_typedefs): New
field.
(local_decl_p, record_locally_defined_typedef)
(maybe_record_local_typedef_use)
(maybe_warn_unused_local_typedefs): Declare new functions.
* c-common.c (local_decl_p, record_locally_defined_typedef)
(maybe_record_local_typedef_use)
(maybe_warn_unused_local_typedefs): Define new functions.
* c.opt: Declare new -Wunused-local-typedefs flag.

gcc/cp

* name-lookup.c (pushdecl_maybe_friend_1): Use the new
record_locally_defined_typedef.
* cp-tree.h (cp_maybe_record_local_typedef_use): Declare new function.
* decl.c (finish_function): Use the new 
maybe_warn_unused_local_typedefs.
* decl2.c (cp_maybe_record_local_typedef_use): New public
function.
(grokfield): Use the new record_locally_defined_typedef.
* parser.c (lookup_name): Use the new cp_maybe_record_local_typedef_use.
maybe_record_local_typedef_use.

gcc/doc/

* invoke.texi: Update documentation for -Wunused-local-typedefs.

gcc/testsuite/

* g++.dg/warn/Wunused-local-typedefs.C: New test file.
* c-c++-common/Wunused-local-typedefs.c: Likewise.

libstdc++-v3/

* include/ext/bitmap_allocator.h
(__detail::__mini_vector::__lower_bound): Remove unused typedef.
* src/istream.cc (std::operator(basic_istreamchar __in,
basic_stringchar __str)): Likewise.
(std::getline): Likewise.
* src/valarray.cc (__valarray_product): Likewise.
---
 gcc/c-decl.c   |   38 +-
 gcc/c-family/c-common.c|  114 +++
 gcc/c-family/c-common.h|8 +
 gcc/c-family/c.opt |4 +
 gcc/cp/cp-tree.h   | 

Re: [PATCH] PR c++/33255 - Support -Wunused-local-typedefs warning

2011-08-06 Thread Jason Merrill

On 08/06/2011 06:57 AM, Dodji Seketeli wrote:

@@ -4340,6 +4340,8 @@ c_sizeof_or_alignof_type (location_t loc,
   value = fold_convert_loc (loc, size_type_node, value);
   gcc_assert (!TYPE_IS_SIZETYPE (TREE_TYPE (value)));

+  maybe_record_local_typedef_use (type);


Why is this still needed?


+/* Return TRUE if DECL is declared in the current function.  */
+
+bool
+local_decl_p (tree decl)


I don't think we need to work so hard to compare contexts to 
current_function_decl; just checking that the decl is in some function 
should be enough, i.e. decl_function_context is non-null.



+  static int unused_local_typedefs_warn_count;


This could use a comment.


+/* If T is a typedef variant type or a TYPE_DECL declared locally,
+   mark it as used.  */
+
+void
+maybe_record_local_typedef_use (tree t)


Does this still need to handle types?  And the meaning of TREE_USED 
shouldn't change depending on a warning flag or whether a decl is local 
or not; let's just mark all TYPE_DECLs as used.


Seems like we don't need local_decl_p at all; at the definition point, 
if we're in a function, a new declaration must be local enough for our 
purposes.



+void
+cp_maybe_record_local_typedef_use (tree t)
+{
+  /*  If the current function is being instantiated, bail out.  */


As above, I think we shouldn't put so many conditions on setting TREE_USED.


@@ -19572,6 +19572,9 @@ cp_parser_lookup_name (cp_parser *parser, tree name,
   if (DECL_P (decl))
 check_accessibility_of_qualified_id (decl, object_type, parser-scope);

+  cp_maybe_record_local_typedef_use (decl);
+  cp_maybe_record_local_typedef_use (parser-scope);


Why do we need to handle parser-scope?  Wasn't it previously looked up?



Re: [PATCH] PR c++/33255 - Support -Wunused-local-typedefs warning

2011-07-30 Thread Joseph S. Myers
On Fri, 29 Jul 2011, Jason Merrill wrote:

  Looking a bit further, it looks like the C FE uses cfun-language only
  to store the context of the outer function when faced with a nested
  function.  This is done by c_push_function_context, called by
  c_parser_declaration_or_fndef.  Otherwise, cfun-language is not
  allocated.  Is it appropriate that -Wunused-local-typedefs allocates it
  as well?
 
 I think so.  Joseph?

Seems reasonable.

-- 
Joseph S. Myers
jos...@codesourcery.com


Re: [PATCH] PR c++/33255 - Support -Wunused-local-typedefs warning

2011-07-29 Thread Jason Merrill

On 07/27/2011 01:54 PM, Dodji Seketeli wrote:

+  /*  Set of typedefs that are used in this function.  */
+  struct pointer_set_t * GTY((skip)) used_local_typedefs;


Is there a reason not to just use TREE_USED for this?


+  /* Vector of locally defined typedefs, for
+ -Wunused-local-typedefs.  */
+  VEC(tree,gc) *local_typedefs;


If the accessors are in c-common, this field should be in 
c_language_function.



+  /* We are only interested in a typedef declared locally.  */
+  if (DECL_CONTEXT (typedef_decl) != current_function_decl)
+   return;


What if it's used in a nested function/local class/lambda?


@@ -4175,6 +4175,9 @@ mark_used (tree decl)

   /* Set TREE_USED for the benefit of -Wunused.  */
   TREE_USED (decl) = 1;
+
+  maybe_record_local_typedef_use (TREE_TYPE (decl));


Why is this needed?  If the decl has the typedef for a type, we should 
have already marked it as used in grokdeclarator.


Actually, couldn't we just mark a typedef as used when when lookup finds 
it?  That would avoid having to mark in so many places and avoid the 
need for walk_tree.


I think -Wunused and -Wall should imply -Wunused-local-typedefs unless 
the user specifies -Wno-unused-local-typedefs.


Jason


Re: [PATCH] PR c++/33255 - Support -Wunused-local-typedefs warning

2011-07-29 Thread Paolo Carlini

Hi,
I think -Wunused and -Wall should imply -Wunused-local-typedefs unless 
the user specifies -Wno-unused-local-typedefs.
IMHO, this is a very good idea looking forward, but then I think we 
should make sure the warning plays well with system headers either as-is 
or together with some other pending work of Dodji. In particular, as I 
probably mentioned already in the trail, we really want to double check 
that debug-mode does not trigger warnings, I'm a bit of worried because 
many people use and like it.


Paolo.


Re: [PATCH] PR c++/33255 - Support -Wunused-local-typedefs warning

2011-07-29 Thread Dodji Seketeli
Jason Merrill ja...@redhat.com writes:

 On 07/27/2011 01:54 PM, Dodji Seketeli wrote:
 +  /*  Set of typedefs that are used in this function.  */
 +  struct pointer_set_t * GTY((skip)) used_local_typedefs;

 Is there a reason not to just use TREE_USED for this?

I wasn't sure if that flag wasn't (or couldn't be) used for some core
functionality with another meaning for this type of tree, and so would
cause some conflict.


 +  /* Vector of locally defined typedefs, for
 + -Wunused-local-typedefs.  */
 +  VEC(tree,gc) *local_typedefs;

 If the accessors are in c-common, this field should be in
 c_language_function.


Thanks, I didn't realize this existed.

 Actually, couldn't we just mark a typedef as used when when lookup
 finds it?  That would avoid having to mark in so many places and avoid
 the need for walk_tree.

This would indeed simplify things.  I'll try it.

 I think -Wunused and -Wall should imply -Wunused-local-typedefs unless
 the user specifies -Wno-unused-local-typedefs.

I actually first tried this (actually adding it to -Wall -extra and
-Wunused) and found out the following issue.

A typedef can be defined in a macro in a system header, be expanded in a
function and not be used by the function.  In this case we shouldn't
warn, but PR preprocessor/7263 makes us warn nonetheless.  There are
many spots of that kind in the libstdc++ test suite.

Paolo Carlini paolo.carl...@oracle.com writes:

 Hi,
 I think -Wunused and -Wall should imply -Wunused-local-typedefs
 unless the user specifies -Wno-unused-local-typedefs.
 IMHO, this is a very good idea looking forward, but then I think we
 should make sure the warning plays well with system headers either
 as-is or together with some other pending work of Dodji. In
 particular, as I probably mentioned already in the trail, we really
 want to double check that debug-mode does not trigger warnings, I'm a
 bit of worried because many people use and like it.

Exactly.  This would be a side effect of PR preprocessor/7263?

So do you guys think we should add it nonetheless and just add
-Wno-unused-local-typedefs to the tests that exhibit the above issue
before fixing PR preprocessor/7263?

Thanks.

-- 
Dodji


Re: [PATCH] PR c++/33255 - Support -Wunused-local-typedefs warning

2011-07-29 Thread Paolo Carlini

Hi,

I think -Wunused and -Wall should imply -Wunused-local-typedefs unless
the user specifies -Wno-unused-local-typedefs.

I actually first tried this (actually adding it to -Wall -extra and
-Wunused) and found out the following issue.

A typedef can be defined in a macro in a system header, be expanded in a
function and not be used by the function.  In this case we shouldn't
warn, but PR preprocessor/7263 makes us warn nonetheless.  There are
many spots of that kind in the libstdc++ test suite.

Paolo Carlinipaolo.carl...@oracle.com  writes:


Hi,

I think -Wunused and -Wall should imply -Wunused-local-typedefs
unless the user specifies -Wno-unused-local-typedefs.

IMHO, this is a very good idea looking forward, but then I think we
should make sure the warning plays well with system headers either
as-is or together with some other pending work of Dodji. In
particular, as I probably mentioned already in the trail, we really
want to double check that debug-mode does not trigger warnings, I'm a
bit of worried because many people use and like it.

Exactly.  This would be a side effect of PR preprocessor/7263?

So do you guys think we should add it nonetheless and just add
-Wno-unused-local-typedefs to the tests that exhibit the above issue
before fixing PR preprocessor/7263?
Personally, I don't have a strong opinion, but I think it's very 
important to have a solid plan for 4.7.0: we don't want to regress in 
terms of warnings spilled from system headers, we slowly made good 
progress over the years and now the situation is pretty good and much 
less confusing than it used to be to the users. For sure, anyway, I'm 
available to clean up a bit some of the warnings emitted by the library, 
if that can help the process.


Paolo.


Re: [PATCH] PR c++/33255 - Support -Wunused-local-typedefs warning

2011-07-29 Thread Dodji Seketeli
Jason Merrill ja...@redhat.com writes:

 On 07/27/2011 01:54 PM, Dodji Seketeli wrote:
 +  /*  Set of typedefs that are used in this function.  */
 +  struct pointer_set_t * GTY((skip)) used_local_typedefs;

 Is there a reason not to just use TREE_USED for this?

 +  /* Vector of locally defined typedefs, for
 + -Wunused-local-typedefs.  */
 +  VEC(tree,gc) *local_typedefs;

 If the accessors are in c-common, this field should be in
 c_language_function.

Looking into this a bit, it seems to me that I can access
cfun-language-base (of type c_language_function) from inside either
the C or C++ FE only, as the type of cfun-language -- which is of type
struct language_function -- is only defined either in c-lang.h or
cp-tree.h.  I cannot access it from c-common.c.

This is consistent with the comment of the language field of the
function struct:

  /* Language-specific code can use this to store whatever it likes.  */
  struct language_function * language;

What am I missing?

-- 
Dodji


Re: [PATCH] PR c++/33255 - Support -Wunused-local-typedefs warning

2011-07-29 Thread Jason Merrill

On 07/29/2011 08:36 AM, Dodji Seketeli wrote:

Looking into this a bit, it seems to me that I can access
cfun-language-base (of type c_language_function) from inside either
the C or C++ FE only, as the type of cfun-language -- which is of type
struct language_function -- is only defined either in c-lang.h or
cp-tree.h.  I cannot access it from c-common.c.


I think you can use (struct c_language_function *)cfun-language.

Jason


Re: [PATCH] PR c++/33255 - Support -Wunused-local-typedefs warning

2011-07-29 Thread Jason Merrill

On 07/29/2011 03:35 AM, Dodji Seketeli wrote:

So do you guys think we should add it nonetheless and just add
-Wno-unused-local-typedefs to the tests that exhibit the above issue
before fixing PR preprocessor/7263?


Does your set of linemap patches fix the issue?  In that case, we can 
add it when those go in.  Speaking of which, sorry I haven't found the 
time to review them yet.


Jason


Re: [PATCH] PR c++/33255 - Support -Wunused-local-typedefs warning

2011-07-29 Thread Dodji Seketeli
Jason Merrill ja...@redhat.com writes:

 On 07/29/2011 03:35 AM, Dodji Seketeli wrote:
 So do you guys think we should add it nonetheless and just add
 -Wno-unused-local-typedefs to the tests that exhibit the above issue
 before fixing PR preprocessor/7263?

 Does your set of linemap patches fix the issue?

Yes it does.  Particularly this patch
http://gcc.gnu.org/ml/gcc-patches/2011-07/msg01318.html 

  In that case, we can add it when those go in.

OK.

  Speaking of which, sorry I haven't found the time to review them yet.

No problem.

-- 
Dodji


Re: [PATCH] PR c++/33255 - Support -Wunused-local-typedefs warning

2011-07-29 Thread Dodji Seketeli
Jason Merrill ja...@redhat.com writes:

 On 07/29/2011 08:36 AM, Dodji Seketeli wrote:
 Looking into this a bit, it seems to me that I can access
 cfun-language-base (of type c_language_function) from inside either
 the C or C++ FE only, as the type of cfun-language -- which is of type
 struct language_function -- is only defined either in c-lang.h or
 cp-tree.h.  I cannot access it from c-common.c.

 I think you can use (struct c_language_function *)cfun-language.

I see.

Looking a bit further, it looks like the C FE uses cfun-language only
to store the context of the outer function when faced with a nested
function.  This is done by c_push_function_context, called by
c_parser_declaration_or_fndef.  Otherwise, cfun-language is not
allocated.  Is it appropriate that -Wunused-local-typedefs allocates it
as well?

-- 
Dodji


Re: [PATCH] PR c++/33255 - Support -Wunused-local-typedefs warning

2011-07-29 Thread Jason Merrill

On 07/29/2011 10:27 AM, Dodji Seketeli wrote:

Jason Merrillja...@redhat.com  writes:


On 07/29/2011 08:36 AM, Dodji Seketeli wrote:

Looking into this a bit, it seems to me that I can access
cfun-language-base (of type c_language_function) from inside either
the C or C++ FE only, as the type of cfun-language -- which is of type
struct language_function -- is only defined either in c-lang.h or
cp-tree.h.  I cannot access it from c-common.c.


I think you can use (struct c_language_function *)cfun-language.


I see.

Looking a bit further, it looks like the C FE uses cfun-language only
to store the context of the outer function when faced with a nested
function.  This is done by c_push_function_context, called by
c_parser_declaration_or_fndef.  Otherwise, cfun-language is not
allocated.  Is it appropriate that -Wunused-local-typedefs allocates it
as well?


I think so.  Joseph?

Jason


[PATCH] PR c++/33255 - Support -Wunused-local-typedefs warning

2011-07-27 Thread Dodji Seketeli
Hello,

The patch below implements a new flag to warn when a typedef defined
in a function is unused.  The warning is -Wunused-local-typedef, is
active for the C and C++ FEs and is intended for trunk.

With this patch the compiler caught a few spots of unused local
typedefs in libstdc++ that I have fixed thus.

Bootstrapped and tested on x86_64-unknown-linux-gnu against trunk.

-- 
Dodji

From b4612a6dd8a642795fe81398b372746f19c86614 Mon Sep 17 00:00:00 2001
From: Dodji Seketeli do...@redhat.com
Date: Mon, 25 Jul 2011 19:02:07 +0200
Subject: [PATCH] PR c++/33255 - Support -Wunused-local-typedefs warning

gcc/

* Makefile.in: add pointer-set.h dependency to function.h
* function.h (function::{local_typedefs,used_local_typedefs}): New
struct members.
* tree.c (walk_type_fields): Don't forget to walk the underlying
type of a typedef.
* c-decl.c (pushdecl, grokdeclarator): Use the new
record_locally_defined_typedef.
(finish_function): Use the new maybe_warn_unused_local_typedefs.
(maybe_record_local_typedef_use_r)
(c_maybe_record_local_typedef_use): New static functions.
(maybe_record_local_typedef_use): New public function definition.
* c-typeck.c (c_expr_sizeof_type, c_cast_expr): Use the new
maybe_record_local_typedef_use.

gcc/c-family

* c-common.h (record_locally_defined_typedef)
(maybe_record_local_typedef_use)
(maybe_record_local_typedef_use_real)
(maybe_warn_unused_local_typedefs): Declare new functions.
* c-common.c (c_sizeof_or_alignof_type): Use the new
maybe_record_local_typedef_use.
(record_locally_defined_typedef)
(maybe_record_local_typedef_use_real)
(maybe_warn_unused_local_typedefs): Define new functions.
* c.opt: Declare new -Wunused-local-typedefs flag.

gcc/cp

* name-lookup.c (pushdecl_maybe_friend_1): Use the new
record_locally_defined_typedef.
* cp-tree.h (maybe_record_local_typedef_use): Declare new function.
* decl.c (grokdeclarator): Use the new
maybe_record_local_typedef_use.
(finish_function): Use the new maybe_warn_unused_local_typedefs.
* decl2.c (cp_maybe_record_local_typedef_use_r)
(cp_maybe_record_local_typedef_use): New static functions.
(maybe_record_local_typedef_use): New public function.
(mark_used): Use the new maybe_record_local_typedef_use.
* init.c (build_new): Likewise.
* parser.c (cp_parser_qualifying_entity, cp_parser_template_id):
Likewise.
* rtti.c (build_dynamic_cast_1): Use the new
maybe_record_local_typedef_use.
* typeck.c (cxx_sizeof_or_alignof_type, build_static_cast_1)
(build_reinterpret_cast_1)
(build_const_cast_1): Use the new maybe_record_local_typedef_use.
* typeck2.c (build_functional_cast): Likewise.

gcc/doc/

* invoke.texi: Update documentation for -Wunused-local-typedefs.

gcc/testsuite/

* g++.dg/warn/Wunused-local-typedefs.C: New test file.
* c-c++-common/Wunused-local-typedefs.c: Likewise.

libstdc++-v3/

* include/ext/bitmap_allocator.h
(__detail::__mini_vector::__lower_bound): Remove unused typedef.
* src/istream.cc (std::operator(basic_istreamchar __in,
basic_stringchar __str)): Likewise.
(std::getline): Likewise.
* src/valarray.cc (__valarray_product): Likewise.
---
 gcc/Makefile.in|2 +-
 gcc/c-decl.c   |   54 +++-
 gcc/c-family/c-common.c|   95 
 gcc/c-family/c-common.h|4 +
 gcc/c-family/c.opt |4 +
 gcc/c-typeck.c |5 +
 gcc/cp/cp-tree.h   |1 +
 gcc/cp/decl.c  |6 ++
 gcc/cp/decl2.c |   46 ++
 gcc/cp/init.c  |2 +
 gcc/cp/name-lookup.c   |7 ++
 gcc/cp/parser.c|7 ++
 gcc/cp/rtti.c  |2 +
 gcc/cp/typeck.c|7 ++
 gcc/cp/typeck2.c   |2 +
 gcc/doc/invoke.texi|9 ++-
 gcc/function.h |8 ++
 .../c-c++-common/Wunused-local-typedefs.c  |   38 
 gcc/tree.c |5 +
 libstdc++-v3/include/ext/bitmap_allocator.h|2 -
 libstdc++-v3/src/istream.cc|3 -
 libstdc++-v3/src/valarray.cc   |1 -
 22 files changed, 300 insertions(+), 10 deletions(-)
 create mode 100644 gcc/testsuite/c-c