[C++ Patch/RFC] PR 56100

Paolo Carlini paolo.carlini@oracle.com
Wed Apr 1 10:02:00 GMT 2015


Hi,

On 03/31/2015 09:13 PM, Jason Merrill wrote:
> On 03/31/2015 01:14 PM, Paolo Carlini wrote:
>> Note, I took the idea of allowing for current_instantiation ()->decl !=
>> current_function_decl from some code prepared by Dodji for
>> -Wunused-local-typedefs
>
> Let's make this a predicate function.
Indeed. Thus I'm finishing testing the below.

Thanks!
Paolo.

//////////////////
-------------- next part --------------
Index: cp/cp-tree.h
===================================================================
--- cp/cp-tree.h	(revision 221795)
+++ cp/cp-tree.h	(working copy)
@@ -5732,6 +5732,7 @@ extern tree get_mostly_instantiated_function_type
 extern bool problematic_instantiation_changed	(void);
 extern void record_last_problematic_instantiation (void);
 extern struct tinst_level *current_instantiation(void);
+extern bool instantiating_current_function_p    (void);
 extern tree maybe_get_template_decl_from_type_decl (tree);
 extern int processing_template_parmlist;
 extern bool dependent_type_p			(tree);
Index: cp/name-lookup.c
===================================================================
--- cp/name-lookup.c	(revision 221795)
+++ cp/name-lookup.c	(working copy)
@@ -973,9 +973,8 @@ pushdecl_maybe_friend_1 (tree x, bool is_friend)
 	  /* If this is a locally defined typedef in a function that
 	     is not a template instantation, record it to implement
 	     -Wunused-local-typedefs.  */
-	  if (current_instantiation () == NULL
-	      || (current_instantiation ()->decl != current_function_decl))
-	  record_locally_defined_typedef (x);
+	  if (!instantiating_current_function_p ())
+	    record_locally_defined_typedef (x);
 	}
 
       /* Multiple external decls of the same identifier ought to match.
@@ -1277,7 +1276,8 @@ pushdecl_maybe_friend_1 (tree x, bool is_friend)
                               old and new decls are type decls.  */
                            || (TREE_CODE (oldglobal) == TYPE_DECL
                                && (!DECL_ARTIFICIAL (oldglobal)
-                                   || TREE_CODE (x) == TYPE_DECL))))
+                                   || TREE_CODE (x) == TYPE_DECL)))
+		       && !instantiating_current_function_p ())
 		/* XXX shadow warnings in outer-more namespaces */
 		{
 		  if (warning_at (input_location, OPT_Wshadow,
Index: cp/pt.c
===================================================================
--- cp/pt.c	(revision 221795)
+++ cp/pt.c	(working copy)
@@ -20773,6 +20773,13 @@ current_instantiation (void)
   return current_tinst_level;
 }
 
+bool
+instantiating_current_function_p (void)
+{
+  return (current_instantiation ()
+	  && current_instantiation ()->decl == current_function_decl);
+}
+
 /* [temp.param] Check that template non-type parm TYPE is of an allowable
    type. Return zero for ok, nonzero for disallowed. Issue error and
    warning messages under control of COMPLAIN.  */
Index: testsuite/g++.dg/warn/Wshadow-10.C
===================================================================
--- testsuite/g++.dg/warn/Wshadow-10.C	(revision 0)
+++ testsuite/g++.dg/warn/Wshadow-10.C	(working copy)
@@ -0,0 +1,15 @@
+// PR c++/56100
+// { dg-options "-Wshadow" }
+
+struct bar
+{
+  template <typename T>
+  void baz () { int foo; }
+};
+
+int foo;
+
+int main ()
+{
+  bar ().baz <int> ();
+}
Index: testsuite/g++.dg/warn/Wshadow-11.C
===================================================================
--- testsuite/g++.dg/warn/Wshadow-11.C	(revision 0)
+++ testsuite/g++.dg/warn/Wshadow-11.C	(working copy)
@@ -0,0 +1,15 @@
+// PR c++/56100
+// { dg-options "-Wshadow" }
+
+int foo;  // { dg-message "shadowed declaration" }
+
+struct bar
+{
+  template <typename T>
+  void baz () { int foo; }  // { dg-warning "shadows a global" }
+};
+
+int main ()
+{
+  bar ().baz <int> ();
+}
Index: testsuite/g++.dg/warn/Wshadow-8.C
===================================================================
--- testsuite/g++.dg/warn/Wshadow-8.C	(revision 0)
+++ testsuite/g++.dg/warn/Wshadow-8.C	(working copy)
@@ -0,0 +1,15 @@
+// PR c++/56100
+// { dg-options "-Wshadow" }
+
+template <typename T>
+struct bar
+{
+  void baz () { int foo; }
+};
+
+int foo;
+
+int main ()
+{
+  bar <int> ().baz ();
+}
Index: testsuite/g++.dg/warn/Wshadow-9.C
===================================================================
--- testsuite/g++.dg/warn/Wshadow-9.C	(revision 0)
+++ testsuite/g++.dg/warn/Wshadow-9.C	(working copy)
@@ -0,0 +1,15 @@
+// PR c++/56100
+// { dg-options "-Wshadow" }
+
+int foo;  // { dg-message "shadowed declaration" }
+
+template <typename T>
+struct bar
+{
+  void baz () { int foo; }  // { dg-warning "shadows a global" }
+};
+
+int main ()
+{
+  bar <int> ().baz ();
+}


More information about the Gcc-patches mailing list