[patch 2/2] Perform a namespace lookup at every block level

Sami Wagiaalla
2009-08-18T20:34:57Z

In cases like this

namespace A{
int ax;
}

namespace B{
using namespace A;
}

namespace C{
using namespace B;
}

using namespace A

void foo(){
C::ax;
}

And this:

namespace{
namespace{
in xx;
}
}

void foo(){
xx;
}

A recursive search must be performed. This patch provides that functionality:

2009-08-14 Sami Wagiaalla <swagiaal@redhat.com>

* cp-support.h: Add 'searched' feild to using_direct struct.
* cp-namespace.c (cp_lookup_symbol_imports): Perform recursive search.
(cp_add_using): Initialize 'searched' feild.
(cp_copy_usings): Copy searched feild.

diff --git a/gdb/cp-namespace.c b/gdb/cp-namespace.c
index a0ccd7f..909ec2c 100644
--- a/gdb/cp-namespace.c
+++ b/gdb/cp-namespace.c
@@ -229,6 +229,7 @@ cp_add_using (const char *dest,
retval->import_src = savestring (src, strlen(src));
retval->import_dest = savestring (dest, strlen(dest));
retval->next = next;
+ retval->searched = 0;

return retval;
}
@@ -255,6 +256,8 @@ cp_copy_usings (struct using_direct *using,
obstack);
retval->next = cp_copy_usings (using->next, obstack);

+ retval->searched = using->searched;
+
xfree (using->import_src);
xfree (using->import_dest);
xfree (using);
@@ -408,7 +411,7 @@ cp_lookup_symbol_imports (const char *scope,
const struct block *block,
const domain_enum domain)
{
- const struct using_direct *current;
+ struct using_direct *current;
struct symbol *sym;

/* First, try to find the symbol in the given namespace. */
@@ -428,13 +431,20 @@ cp_lookup_symbol_imports (const char *scope,

/* If the import destination is the current scope or one of its ancestors then
it is applicable. */
- if (strncmp (scope, current->import_dest, strlen(current->import_dest)) == 0)
+ if (strncmp (scope, current->import_dest,
+ strlen(current->import_dest)) == 0 &&
+ !current->searched )
{
- sym = cp_lookup_symbol_in_namespace (current->import_src,
+ /* Mark this import as searched so that the recursive call does not
+ search it again. */
+ current->searched = 1;
+ sym = cp_lookup_symbol_namespace (current->import_src,
name,
linkage_name,
block,
domain);
+
+ current->searched = 0;
if (sym != NULL)
return sym;
}
diff --git a/gdb/cp-support.h b/gdb/cp-support.h
index b5a5c5f..c9d53e5 100644
--- a/gdb/cp-support.h
+++ b/gdb/cp-support.h
@@ -47,6 +47,9 @@ struct using_direct
char *import_src;
char *import_dest;
struct using_direct *next;
+
+ /* Used during import search to temporarly mark this node as searced. */
+ int searched;
};

diff --git a/gdb/testsuite/gdb.cp/namespace-recursive.cc b/gdb/testsuite/gdb.cp/namespace-recursive.cc
new file mode 100644
index 0000000..9368ad8
--- /dev/null
+++ b/gdb/testsuite/gdb.cp/namespace-recursive.cc
@@ -0,0 +1,30 @@
+namespace A{
+ int ax = 9;
+}
+
+namespace B{
+ using namespace A;
+}
+
+namespace C{
+ using namespace B;
+}
+
+//---------------
+namespace D{
+ using namespace D;
+ int dx = 99;
+}
+using namespace C;
+
+//---------------
+namespace{
+ namespace{
+ int xx = 999;
+ }
+}
+
+int main(){
+ using namespace D;
+ return ax + dx + xx;
+}
\ No newline at end of file
diff --git a/gdb/testsuite/gdb.cp/namespace-recursive.exp b/gdb/testsuite/gdb.cp/namespace-recursive.exp
new file mode 100644
index 0000000..ebc898f
--- /dev/null
+++ b/gdb/testsuite/gdb.cp/namespace-recursive.exp
@@ -0,0 +1,66 @@
+# Copyright 2008 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+if $tracelevel then {
+ strace $tracelevel
+}
+
+set prms_id 0
+set bug_id 0
+
+set testfile namespace-recursive
+set srcfile ${testfile}.cc
+set binfile ${objdir}/${subdir}/${testfile}
+if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug c++}] != "" } {
+ untested "Couldn't compile test program"
+ return -1
+}
+
+if [get_compiler_info ${binfile}] {
+ return -1;
+}
+
+
+# Get things started.
+
+gdb_exit
+gdb_start
+gdb_reinitialize_dir $srcdir/$subdir
+gdb_load ${binfile}
+
+if ![runto_main] then {
+ perror "couldn't run to breakpoint main"
+ continue
+}
+
+############################################
+# test printing from namespace imported into
+# imported namespace
+
+gdb_test "print ax" "= 9"
+
+############################################
+# test that gdb can print without falling
+# into search loop
+
+gdb_test "print dx" "= 99"
+
+############################################
+# test printing from namespace imported into
+# imported namespace where imports are implicit
+# anonymous namespace imports.
+
+gdb_test "print xx" "= 999"
+