aboutsummaryrefslogtreecommitdiff
path: root/gcc/cp/name-lookup.c
diff options
context:
space:
mode:
authorJanis Johnson <janis187@us.ibm.com>2011-05-26 20:15:22 +0000
committerNathan Froyd <froydnj@gcc.gnu.org>2011-05-26 20:15:22 +0000
commit1eb2a14d1670a17b084e4fb33757a5fb6d7eefbd (patch)
treed67de4f1d33d599df36d693a0e3561ed970648d6 /gcc/cp/name-lookup.c
parent6a1a787e415ffbb0023dd66c7cf4ebd3475b8c7f (diff)
downloadgcc-1eb2a14d1670a17b084e4fb33757a5fb6d7eefbd.zip
gcc-1eb2a14d1670a17b084e4fb33757a5fb6d7eefbd.tar.gz
gcc-1eb2a14d1670a17b084e4fb33757a5fb6d7eefbd.tar.bz2
detect C++ errors to fix 2288 and 18770
detect C++ errors to fix 2288 and 18770 gcc/cp/ PR c++/2288 PR c++/18770 * name-lookup.h (enum scope_kind): Add sk_cond. * name-lookup.c (pushdecl_maybe_friend): Get scope of shadowed local. Detect and report error for redeclaration from for-init or if or switch condition. (begin_scope): Handle sk_cond. * semantics.c (begin_if_stmt): Use sk_cond. (begin switch_stmt): Ditto. gcc/testsuite/ PR c++/2288 PR c++/18770 * g++.old-deja/g++.jason/cond.C: Remove xfails. * g++.dg/parse/pr18770.C: New test. * g++.dg/cpp0x/range-for5.C: Add dg-error marker. Co-Authored-By: Nathan Froyd <froydnj@codesourcery.com> From-SVN: r174307
Diffstat (limited to 'gcc/cp/name-lookup.c')
-rw-r--r--gcc/cp/name-lookup.c24
1 files changed, 23 insertions, 1 deletions
diff --git a/gcc/cp/name-lookup.c b/gcc/cp/name-lookup.c
index 935dd2a..3d07ff6 100644
--- a/gcc/cp/name-lookup.c
+++ b/gcc/cp/name-lookup.c
@@ -935,8 +935,15 @@ pushdecl_maybe_friend_1 (tree x, bool is_friend)
else
{
/* Here to install a non-global value. */
- tree oldlocal = innermost_non_namespace_value (name);
tree oldglobal = IDENTIFIER_NAMESPACE_VALUE (name);
+ tree oldlocal = NULL_TREE;
+ cxx_scope *oldscope = NULL;
+ cxx_binding *oldbinding = outer_binding (name, NULL, true);
+ if (oldbinding)
+ {
+ oldlocal = oldbinding->value;
+ oldscope = oldbinding->scope;
+ }
if (need_new_binding)
{
@@ -1065,6 +1072,20 @@ pushdecl_maybe_friend_1 (tree x, bool is_friend)
}
}
}
+ /* Error if redeclaring a local declared in a
+ for-init-statement or in the condition of an if or
+ switch statement when the new declaration is in the
+ outermost block of the controlled statement.
+ Redeclaring a variable from a for or while condition is
+ detected elsewhere. */
+ else if (TREE_CODE (oldlocal) == VAR_DECL
+ && oldscope == current_binding_level->level_chain
+ && (oldscope->kind == sk_cond
+ || oldscope->kind == sk_for))
+ {
+ error ("redeclaration of %q#D", x);
+ error ("%q+#D previously declared here", oldlocal);
+ }
if (warn_shadow && !nowarn)
{
@@ -1424,6 +1445,7 @@ begin_scope (scope_kind kind, tree entity)
case sk_try:
case sk_catch:
case sk_for:
+ case sk_cond:
case sk_class:
case sk_scoped_enum:
case sk_function_parms: