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 48d2aa8e70d4f7a7ce811140541b0f00f5088e38..2a904a5f4f4d66e7647edfaacb3539b0987018c2 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 4303ed5fb1fbb50d227e448fa18a8ea889d0a92c..e3f7cca4436bf017b49a9f8371c4837ccf214f29 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 c649cad75bd61973b9daeda7dba0726b396b23d7..28a85ebeeb7efb1e245d23b0bcb663b265c8dd4a 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 9f8e0b75babcd3c9672edc03b1998d8a05b3dc33..d024bc938e423b07eb46267558a318625bbd4eda 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 ();
+}