re PR c++/56100 (spurious -Wshadow warning with local variable in template class)
authorPaolo Carlini <paolo.carlini@oracle.com>
Wed, 1 Apr 2015 21:27:55 +0000 (21:27 +0000)
committerPaolo Carlini <paolo@gcc.gnu.org>
Wed, 1 Apr 2015 21:27:55 +0000 (21:27 +0000)
/cp
2015-04-01  Paolo Carlini  <paolo.carlini@oracle.com>

PR c++/56100
* pt.c (instantiating_current_function_p): New.
* name-lookup.c (pushdecl_maybe_friend_1): Use it.
* cp-tree.h (instantiating_current_function_p): Declare.

/testsuite
2015-04-01  Paolo Carlini  <paolo.carlini@oracle.com>

PR c++/56100
* g++.dg/warn/Wshadow-8.C: New.
* g++.dg/warn/Wshadow-9.C: Likewise.
* g++.dg/warn/Wshadow-10.C: Likewise.
* g++.dg/warn/Wshadow-11.C: Likewise.

From-SVN: r221814

gcc/cp/cp-tree.h
gcc/cp/name-lookup.c
gcc/cp/pt.c
gcc/testsuite/ChangeLog
gcc/testsuite/g++.dg/warn/Wshadow-10.C [new file with mode: 0644]
gcc/testsuite/g++.dg/warn/Wshadow-11.C [new file with mode: 0644]
gcc/testsuite/g++.dg/warn/Wshadow-8.C [new file with mode: 0644]
gcc/testsuite/g++.dg/warn/Wshadow-9.C [new file with mode: 0644]

index 48d2aa8..2a904a5 100644 (file)
@@ -5732,6 +5732,7 @@ extern tree get_mostly_instantiated_function_type (tree);
 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 4303ed5..e3f7cca 100644 (file)
@@ -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 c649cad..28a85eb 100644 (file)
@@ -20773,6 +20773,16 @@ current_instantiation (void)
   return current_tinst_level;
 }
 
+/* Return TRUE if current_function_decl is being instantiated, false
+   otherwise.  */
+
+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 9f8e0b7..d024bc9 100644 (file)
@@ -1,3 +1,11 @@
+2015-04-01  Paolo Carlini  <paolo.carlini@oracle.com>
+
+       PR c++/56100
+       * g++.dg/warn/Wshadow-8.C: New.
+       * g++.dg/warn/Wshadow-9.C: Likewise.
+       * g++.dg/warn/Wshadow-10.C: Likewise.
+       * g++.dg/warn/Wshadow-11.C: Likewise.
+
 2015-04-01  Bernd Edlinger  <bernd.edlinger@hotmail.de>
 
        * gcc.dg/pr23623.c: Added aligned attribute.
diff --git a/gcc/testsuite/g++.dg/warn/Wshadow-10.C b/gcc/testsuite/g++.dg/warn/Wshadow-10.C
new file mode 100644 (file)
index 0000000..21d5002
--- /dev/null
@@ -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> ();
+}
diff --git a/gcc/testsuite/g++.dg/warn/Wshadow-11.C b/gcc/testsuite/g++.dg/warn/Wshadow-11.C
new file mode 100644 (file)
index 0000000..d3b70c3
--- /dev/null
@@ -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> ();
+}
diff --git a/gcc/testsuite/g++.dg/warn/Wshadow-8.C b/gcc/testsuite/g++.dg/warn/Wshadow-8.C
new file mode 100644 (file)
index 0000000..4f1ed02
--- /dev/null
@@ -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 ();
+}
diff --git a/gcc/testsuite/g++.dg/warn/Wshadow-9.C b/gcc/testsuite/g++.dg/warn/Wshadow-9.C
new file mode 100644 (file)
index 0000000..836bce6
--- /dev/null
@@ -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 ();
+}