From: Tom de Vries <tdevries@suse.de>
To: gdb-patches@sourceware.org
Subject: [PATCH v6 3/3] [gdb/exp] Handle recursive namespace import
Date: Tue, 12 May 2026 12:21:24 +0200 [thread overview]
Message-ID: <20260512102124.517642-4-tdevries@suse.de> (raw)
In-Reply-To: <20260512102124.517642-1-tdevries@suse.de>
Consider test.c, compiled to a.out using "g++ -g test.c":
...
1 namespace mod_a { int xxx = 10; }
2 namespace mod_b { using namespace mod_a;
3 int yyy = 20; }
4 int main (void) {
5 using namespace mod_b;
6 void (xxx + yyy);
7 return 0;
8 }
...
When trying to print the value of variable xxx we get:
...
$ gdb -q -batch a.out -ex start -ex "print xxx"
...
Temporary breakpoint 1, main () at test.c:7
7 return 0;
No symbol "xxx" in current context.
...
The symbol xxx is defined in namespace mod_a, so it's available as:
...
(gdb) p mod_a::xxx
$1 = 10
...
and namespace mod_b uses namespace mod_a, so it's available as:
...
(gdb) p mod_b::xxx
$2 = 10
...
Then main uses namespace mod_b so xxx should also be available in main, but
it's not.
The problem happens here in cp_lookup_symbol_via_imports:
...
Thread 1 "gdb" hit Breakpoint 1, cp_lookup_symbol_via_imports (scope=0x5f43d0 "",
name=0xfffffffface0 "xxx", block=0x2fba5a0, domain=..., search_scope_first=0,
declaration_only=0, search_parents=1, found_symbols=...)
at /home/vries/gdb/src/gdb/cp-namespace.c:505
505 cp_lookup_symbol_via_imports (current->import_src, name,
...
We're about to follow the "using namespace mod_b" statement:
...
(gdb) p *current
$1 = {import_src = 0x2ed2140 "mod_b", import_dest = 0x66e910 "", alias = 0x0,
declaration = 0x0, next = 0x0, decl_line = 5, searched = 1,
excludes = {0x0}}
...
But it does so using the current block, which is the function block for main:
...
(gdb) p block->function ().m_name
$7 = 0x2f8bc20 "main()"
...
and the block containing the "using namespace mod_a" statement is the static
block.
Fix this by additionally iterating over the static and global blocks instead
of only using the current block.
Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=34034
---
gdb/cp-namespace.c | 10 +++++++---
gdb/testsuite/gdb.cp/nsusing-2.exp | 1 -
2 files changed, 7 insertions(+), 4 deletions(-)
diff --git a/gdb/cp-namespace.c b/gdb/cp-namespace.c
index d7a960127dc..9e37265b27d 100644
--- a/gdb/cp-namespace.c
+++ b/gdb/cp-namespace.c
@@ -502,9 +502,13 @@ cp_lookup_symbol_via_imports (const char *scope,
/* If this import statement creates no alias, pass
current->inner as NAMESPACE to direct the search
towards the imported namespace. */
- cp_lookup_symbol_via_imports (current->import_src, name,
- block, domain, 1, 0, 0,
- found_symbols);
+ for (const struct block *b = block; b != nullptr;
+ b = ((b->is_static_block () || b->is_global_block ())
+ ? b->superblock ()
+ : b->static_block ()))
+ cp_lookup_symbol_via_imports (current->import_src, name,
+ b, domain, 1, 0, 0,
+ found_symbols);
}
}
diff --git a/gdb/testsuite/gdb.cp/nsusing-2.exp b/gdb/testsuite/gdb.cp/nsusing-2.exp
index 65e685d0d32..5a2eac5ce67 100644
--- a/gdb/testsuite/gdb.cp/nsusing-2.exp
+++ b/gdb/testsuite/gdb.cp/nsusing-2.exp
@@ -66,7 +66,6 @@ foreach_with_prefix n {1 2 3 4} {
# Function main is using namespace mod_b, and namespace mod_b is using
# namespace mod_a, so mod_a::xxx is available as xxx. Regression test for
# PR34034.
- setup_kfail exp/34034 *-*-*
gdb_test "print xxx" " = 10"
# This used to print " $<n> = 20". Regression test for PR34051.
--
2.51.0
next prev parent reply other threads:[~2026-05-12 10:22 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-05-12 10:21 [PATCH v6 0/3] [gdb/exp] Fix some namespace issues Tom de Vries
2026-05-12 10:21 ` [PATCH v6 1/3] [gdb] Break up complex assignment in cp_lookup_symbol_via_imports Tom de Vries
2026-05-12 10:21 ` [PATCH v6 2/3] [gdb/exp] Fix ignoring of incorrect namespace prefix Tom de Vries
2026-05-12 10:21 ` Tom de Vries [this message]
2026-05-26 10:01 ` [PING][PATCH v6 0/3] [gdb/exp] Fix some namespace issues Tom de Vries
2026-07-14 8:43 ` Tom de Vries
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260512102124.517642-4-tdevries@suse.de \
--to=tdevries@suse.de \
--cc=gdb-patches@sourceware.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox