From: Paolo Carlini Date: Wed, 1 Apr 2015 21:27:55 +0000 (+0000) Subject: re PR c++/56100 (spurious -Wshadow warning with local variable in template class) X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=b4d5e41f1dd32de1f23a1897440f6fb149820430;p=gcc.git re PR c++/56100 (spurious -Wshadow warning with local variable in template class) /cp 2015-04-01 Paolo Carlini 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 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 --- diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h index 48d2aa8e70d..2a904a5f4f4 100644 --- a/gcc/cp/cp-tree.h +++ b/gcc/cp/cp-tree.h @@ -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); diff --git a/gcc/cp/name-lookup.c b/gcc/cp/name-lookup.c index 4303ed5fb1f..e3f7cca4436 100644 --- a/gcc/cp/name-lookup.c +++ b/gcc/cp/name-lookup.c @@ -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, diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c index c649cad75bd..28a85ebeeb7 100644 --- a/gcc/cp/pt.c +++ b/gcc/cp/pt.c @@ -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. */ diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 9f8e0b75bab..d024bc938e4 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,11 @@ +2015-04-01 Paolo Carlini + + 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 * 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 index 00000000000..21d50020059 --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/Wshadow-10.C @@ -0,0 +1,15 @@ +// PR c++/56100 +// { dg-options "-Wshadow" } + +struct bar +{ + template + void baz () { int foo; } +}; + +int foo; + +int main () +{ + bar ().baz (); +} diff --git a/gcc/testsuite/g++.dg/warn/Wshadow-11.C b/gcc/testsuite/g++.dg/warn/Wshadow-11.C new file mode 100644 index 00000000000..d3b70c39ace --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/Wshadow-11.C @@ -0,0 +1,15 @@ +// PR c++/56100 +// { dg-options "-Wshadow" } + +int foo; // { dg-message "shadowed declaration" } + +struct bar +{ + template + void baz () { int foo; } // { dg-warning "shadows a global" } +}; + +int main () +{ + bar ().baz (); +} diff --git a/gcc/testsuite/g++.dg/warn/Wshadow-8.C b/gcc/testsuite/g++.dg/warn/Wshadow-8.C new file mode 100644 index 00000000000..4f1ed02f31a --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/Wshadow-8.C @@ -0,0 +1,15 @@ +// PR c++/56100 +// { dg-options "-Wshadow" } + +template +struct bar +{ + void baz () { int foo; } +}; + +int foo; + +int main () +{ + bar ().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 index 00000000000..836bce62cf0 --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/Wshadow-9.C @@ -0,0 +1,15 @@ +// PR c++/56100 +// { dg-options "-Wshadow" } + +int foo; // { dg-message "shadowed declaration" } + +template +struct bar +{ + void baz () { int foo; } // { dg-warning "shadows a global" } +}; + +int main () +{ + bar ().baz (); +}