aboutsummaryrefslogtreecommitdiff
path: root/gcc/cp/name-lookup.c
diff options
context:
space:
mode:
authorShujing Zhao <pearly.zhao@oracle.com>2010-05-14 03:19:32 +0000
committerShujing Zhao <pzhao@gcc.gnu.org>2010-05-14 03:19:32 +0000
commit74b80262bc639e23b4a94175b1ce1926dfa3477b (patch)
treef4ce482422a000b01c2eb294d32765fb926b0447 /gcc/cp/name-lookup.c
parente15a8cbe1a7d51072a1b998ccfa6f1977b8688fc (diff)
downloadgcc-74b80262bc639e23b4a94175b1ce1926dfa3477b.zip
gcc-74b80262bc639e23b4a94175b1ce1926dfa3477b.tar.gz
gcc-74b80262bc639e23b4a94175b1ce1926dfa3477b.tar.bz2
re PR c++/30566 (-Wshadow warns about clashes between nested function parameters in C++)
gcc/cp/ 2010-05-14 Shujing Zhao <pearly.zhao@oracle.com> PR c++/30566 * name-lookup.c (pushdecl_maybe_friend): Avoid the warnings about shadowing the outer parameter or variables by the declaration of nested function in nested structure or class. Warn the shadowing by the declaration of nested lambda expression. gcc/testsuite/ 2010-05-14 Shujing Zhao <pearly.zhao@oracle.com> PR c++/30566 * testsuite/g++.dg/warn/Wshadow-4.C: Adjust. * testsuite/g++.dg/warn/Wshadow-5.C: New test. * testsuite/g++.dg/warn/Wshadow-6.C: New test. From-SVN: r159383
Diffstat (limited to 'gcc/cp/name-lookup.c')
-rw-r--r--gcc/cp/name-lookup.c56
1 files changed, 38 insertions, 18 deletions
diff --git a/gcc/cp/name-lookup.c b/gcc/cp/name-lookup.c
index 88a3e39..405bf16 100644
--- a/gcc/cp/name-lookup.c
+++ b/gcc/cp/name-lookup.c
@@ -1016,15 +1016,18 @@ pushdecl_maybe_friend (tree x, bool is_friend)
else if (oldlocal != NULL_TREE && !DECL_EXTERNAL (x)
/* Inline decls shadow nothing. */
&& !DECL_FROM_INLINE (x)
- && TREE_CODE (oldlocal) == PARM_DECL
+ && (TREE_CODE (oldlocal) == PARM_DECL
+ || TREE_CODE (oldlocal) == VAR_DECL)
/* Don't check the `this' parameter. */
- && !DECL_ARTIFICIAL (oldlocal))
+ && !DECL_ARTIFICIAL (oldlocal)
+ && !DECL_ARTIFICIAL (x))
{
- bool err = false;
+ bool nowarn = false;
/* Don't complain if it's from an enclosing function. */
if (DECL_CONTEXT (oldlocal) == current_function_decl
- && TREE_CODE (x) != PARM_DECL)
+ && TREE_CODE (x) != PARM_DECL
+ && TREE_CODE (oldlocal) == PARM_DECL)
{
/* Go to where the parms should be and see if we find
them there. */
@@ -1038,16 +1041,41 @@ pushdecl_maybe_friend (tree x, bool is_friend)
if (b->kind == sk_function_parms)
{
error ("declaration of %q#D shadows a parameter", x);
- err = true;
+ nowarn = true;
}
}
- if (warn_shadow && !err)
+ /* The local structure or class can't use parameters of
+ the containing function anyway. */
+ if (DECL_CONTEXT (oldlocal) != current_function_decl)
{
- warning_at (input_location, OPT_Wshadow,
- "declaration of %q#D shadows a parameter", x);
- warning_at (DECL_SOURCE_LOCATION (oldlocal), OPT_Wshadow,
- "shadowed declaration is here");
+ cxx_scope *scope = current_binding_level;
+ tree context = DECL_CONTEXT (oldlocal);
+ for (; scope; scope = scope->level_chain)
+ {
+ if (scope->kind == sk_function_parms
+ && scope->this_entity == context)
+ break;
+ if (scope->kind == sk_class
+ && !LAMBDA_TYPE_P (scope->this_entity))
+ {
+ nowarn = true;
+ break;
+ }
+ }
+ }
+
+ if (warn_shadow && !nowarn)
+ {
+ if (TREE_CODE (oldlocal) == PARM_DECL)
+ warning_at (input_location, OPT_Wshadow,
+ "declaration of %q#D shadows a parameter", x);
+ else
+ warning_at (input_location, OPT_Wshadow,
+ "declaration of %qD shadows a previous local",
+ x);
+ warning_at (DECL_SOURCE_LOCATION (oldlocal), OPT_Wshadow,
+ "shadowed declaration is here");
}
}
@@ -1074,14 +1102,6 @@ pushdecl_maybe_friend (tree x, bool is_friend)
warning (OPT_Wshadow, "declaration of %qD shadows a member of 'this'",
x);
}
- else if (oldlocal != NULL_TREE
- && TREE_CODE (oldlocal) == VAR_DECL)
- {
- warning_at (input_location, OPT_Wshadow,
- "declaration of %qD shadows a previous local", x);
- warning_at (DECL_SOURCE_LOCATION (oldlocal), OPT_Wshadow,
- "shadowed declaration is here");
- }
else if (oldglobal != NULL_TREE
&& TREE_CODE (oldglobal) == VAR_DECL)
/* XXX shadow warnings in outer-more namespaces */