From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id 8cbWMGXp2GnGhBAAWB0awg (envelope-from ) for ; Fri, 10 Apr 2026 08:13:25 -0400 Authentication-Results: simark.ca; dkim=pass (1024-bit key; unprotected) header.d=suse.de header.i=@suse.de header.a=rsa-sha256 header.s=susede2_rsa header.b=hFhD7eRP; dkim=pass header.d=suse.de header.i=@suse.de header.a=ed25519-sha256 header.s=susede2_ed25519 header.b=oDJPuV/R; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.a=rsa-sha256 header.s=susede2_rsa header.b=hFhD7eRP; dkim=neutral header.d=suse.de header.i=@suse.de header.a=ed25519-sha256 header.s=susede2_ed25519 header.b=oDJPuV/R; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id C2DDF1E0BC; Fri, 10 Apr 2026 08:13:25 -0400 (EDT) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-2.4 required=5.0 tests=ARC_SIGNED,ARC_VALID,BAYES_00, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED,RCVD_IN_VALIDITY_CERTIFIED_BLOCKED, RCVD_IN_VALIDITY_RPBL_BLOCKED,RCVD_IN_VALIDITY_SAFE_BLOCKED autolearn=ham autolearn_force=no version=4.0.1 Received: from vm01.sourceware.org (vm01.sourceware.org [38.145.34.32]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id E2D581E04F for ; Fri, 10 Apr 2026 08:13:24 -0400 (EDT) Received: from vm01.sourceware.org (localhost [127.0.0.1]) by sourceware.org (Postfix) with ESMTP id 123344BA23FC for ; Fri, 10 Apr 2026 12:13:24 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 123344BA23FC Authentication-Results: sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=suse.de header.i=@suse.de header.a=rsa-sha256 header.s=susede2_rsa header.b=hFhD7eRP; dkim=pass header.d=suse.de header.i=@suse.de header.a=ed25519-sha256 header.s=susede2_ed25519 header.b=oDJPuV/R; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.a=rsa-sha256 header.s=susede2_rsa header.b=hFhD7eRP; dkim=neutral header.d=suse.de header.i=@suse.de header.a=ed25519-sha256 header.s=susede2_ed25519 header.b=oDJPuV/R Received: from smtp-out1.suse.de (smtp-out1.suse.de [IPv6:2a07:de40:b251:101:10:150:64:1]) by sourceware.org (Postfix) with ESMTPS id 6456E4BA2E27 for ; Fri, 10 Apr 2026 12:11:46 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 6456E4BA2E27 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=suse.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=suse.de ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 6456E4BA2E27 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a07:de40:b251:101:10:150:64:1 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1775823106; cv=none; b=V5d11vFrZ+L7CipsNKN5ggV1gJaV6OdrCVPfx/C1GaRJzi9tOGOmuwoGgghOsg/fEkT5S/n/JhtJl5bl2xIyUPa6bUjVDi8vcvtSark8hlRtP7Rox2onOneXjXpuNyw5b3ZBetiXkNXcWh+GA+s5QLNIjQx+3kqhb07Hj1sbi7w= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1775823106; c=relaxed/simple; bh=bHZMA6D6Ws727x7jx+2q/AcliQlod+uKicQQyr1y5x0=; h=DKIM-Signature:DKIM-Signature:DKIM-Signature:DKIM-Signature:From: To:Subject:Date:Message-ID:MIME-Version; b=fGj2uNhPHMAQeyrjatfJWzgY7bTOUKB21AFAdrGdT/vlLCExCyTKkY7gVMV7WB/P/kyut+X70z1gMVQa/pT3r9F6JrejZ6ccgp+ka9ASrlbpggF2MwxmuOhfT/fERHCLy2Pk3s2pXEGnHndjuSqb/Uck2mWjeZmGr9catPTAbzE= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6456E4BA2E27 Received: from imap1.dmz-prg2.suse.org (unknown [10.150.64.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 5F6916A7ED for ; Fri, 10 Apr 2026 12:11:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1775823105; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=+f9qN2N15B3Pcqvrgoava2HCAJI/7NZOMgVCweskURY=; b=hFhD7eRP96iuaKEpATZYVrxLWEMNob/prAhCAKcTij6kEy/kunfHC2fltNMqej+5jk/tne DmgfY/tNQc5011mnW43AaaJ749TRTlDfkK6CqhIVEC1r6kdGL3/njcVFJeaIMeJIzFuFBw C1r2J0v3LPkp/ZlXa6WoTFATnW50fO8= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1775823105; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=+f9qN2N15B3Pcqvrgoava2HCAJI/7NZOMgVCweskURY=; b=oDJPuV/RSou2SCtkPKGoXpxQsp6ebNj35EIG5+sFmCmvhTDq4+0bIYsguv/pUwMDAn8Rfr ow4jWFqKGJSD+wCw== Authentication-Results: smtp-out1.suse.de; none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1775823105; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=+f9qN2N15B3Pcqvrgoava2HCAJI/7NZOMgVCweskURY=; b=hFhD7eRP96iuaKEpATZYVrxLWEMNob/prAhCAKcTij6kEy/kunfHC2fltNMqej+5jk/tne DmgfY/tNQc5011mnW43AaaJ749TRTlDfkK6CqhIVEC1r6kdGL3/njcVFJeaIMeJIzFuFBw C1r2J0v3LPkp/ZlXa6WoTFATnW50fO8= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1775823105; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=+f9qN2N15B3Pcqvrgoava2HCAJI/7NZOMgVCweskURY=; b=oDJPuV/RSou2SCtkPKGoXpxQsp6ebNj35EIG5+sFmCmvhTDq4+0bIYsguv/pUwMDAn8Rfr ow4jWFqKGJSD+wCw== Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id 46ED14A0B4 for ; Fri, 10 Apr 2026 12:11:45 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id CKH7DwHp2GnGXwAAD6G6ig (envelope-from ) for ; Fri, 10 Apr 2026 12:11:45 +0000 From: Tom de Vries To: gdb-patches@sourceware.org Subject: [PATCH v3 2/2] [gdb/exp] Fix ignoring of incorrect namespace prefix Date: Fri, 10 Apr 2026 14:11:44 +0200 Message-ID: <20260410121144.1836026-3-tdevries@suse.de> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20260410121144.1836026-1-tdevries@suse.de> References: <20260410121144.1836026-1-tdevries@suse.de> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spamd-Result: default: False [-2.80 / 50.00]; BAYES_HAM(-3.00)[100.00%]; MID_CONTAINS_FROM(1.00)[]; NEURAL_HAM_LONG(-1.00)[-1.000]; R_MISSING_CHARSET(0.50)[]; NEURAL_HAM_SHORT(-0.20)[-1.000]; MIME_GOOD(-0.10)[text/plain]; TO_DN_NONE(0.00)[]; DKIM_SIGNED(0.00)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; FUZZY_RATELIMITED(0.00)[rspamd.com]; ARC_NA(0.00)[]; RCPT_COUNT_ONE(0.00)[1]; TO_MATCH_ENVRCPT_ALL(0.00)[]; RCVD_COUNT_TWO(0.00)[2]; FROM_HAS_DN(0.00)[]; RCVD_TLS_ALL(0.00)[]; PREVIOUSLY_DELIVERED(0.00)[gdb-patches@sourceware.org]; FROM_EQ_ENVFROM(0.00)[]; RCVD_VIA_SMTP_AUTH(0.00)[]; MIME_TRACE(0.00)[0:+]; DBL_BLOCKED_OPENRESOLVER(0.00)[sourceware.org:url, imap1.dmz-prg2.suse.org:helo, gnu.org:url, suse.de:mid] X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces~public-inbox=simark.ca@sourceware.org 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 non-existent variable mod_a::yyy, we get: ... $ gdb -q -batch a.out -ex start -ex "print mod_a::yyy" ... Temporary breakpoint 1, main () at test.c:7 7 return 0; $1 = 20 ... The problem is in cp_lookup_symbol_via_imports, where we decide that the "using namespace mod_b" from main is applicable in scope mod_a. Fix this by being more strict in the calculation of directive_match: ... if (len == 0) - directive_match = 1; + { + const char *current_scope = (block->function () != nullptr + ? block->scope () + : nullptr /* Don't know. */); + directive_match = (current_scope != nullptr + ? streq (scope, current_scope) + : 1 /* Assume there's a match. */); + } ... As is clear from the code, in case we don't know the current scope, we assume there's a match. This may be harmless, or this may describe a cornercase we haven't run into yet. If so, it's a pre-existing issue. The new test-case contains regression tests for: - PR34051, and - PR34034 for which it contains a kfail. Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=34051 --- gdb/cp-namespace.c | 9 +++- gdb/testsuite/gdb.cp/nsusing-2.cc | 39 ++++++++++++++++ gdb/testsuite/gdb.cp/nsusing-2.exp | 75 ++++++++++++++++++++++++++++++ 3 files changed, 122 insertions(+), 1 deletion(-) create mode 100644 gdb/testsuite/gdb.cp/nsusing-2.cc create mode 100644 gdb/testsuite/gdb.cp/nsusing-2.exp diff --git a/gdb/cp-namespace.c b/gdb/cp-namespace.c index f8b71be3ff1..45cde597674 100644 --- a/gdb/cp-namespace.c +++ b/gdb/cp-namespace.c @@ -431,7 +431,14 @@ cp_lookup_symbol_via_imports (const char *scope, if (search_parents) { if (len == 0) - directive_match = 1; + { + const char *current_scope = (block->function () != nullptr + ? block->scope () + : nullptr /* Don't know. */); + directive_match = (current_scope != nullptr + ? streq (scope, current_scope) + : 1 /* Assume there's a match. */); + } else directive_match = (startswith (scope, current->import_dest) && (scope[len] == ':' diff --git a/gdb/testsuite/gdb.cp/nsusing-2.cc b/gdb/testsuite/gdb.cp/nsusing-2.cc new file mode 100644 index 00000000000..72f98add1b9 --- /dev/null +++ b/gdb/testsuite/gdb.cp/nsusing-2.cc @@ -0,0 +1,39 @@ +/* This testcase is part of GDB, the GNU debugger. + + Copyright 2026 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 . */ + +/* C++ variant of the Fortran example from PR34034. */ + +namespace mod_a { + int xxx = 10; +} + +namespace mod_b { + using namespace mod_a; + int yyy = 20; +} + +static void foo () {} + +int +main (void) +{ + foo (); /* main-entry. */ + using namespace mod_b; + (void)xxx; + (void)yyy; + return 0; /* main-return. */ +} diff --git a/gdb/testsuite/gdb.cp/nsusing-2.exp b/gdb/testsuite/gdb.cp/nsusing-2.exp new file mode 100644 index 00000000000..647a8600955 --- /dev/null +++ b/gdb/testsuite/gdb.cp/nsusing-2.exp @@ -0,0 +1,75 @@ +# Copyright 2026 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 . + +# Test recursive "using namespace". Regression test for PR34034 and PR34051. + +standard_testfile .cc + +if {[prepare_for_testing "failed to prepare" $testfile $srcfile \ + {debug c++}]} { + return +} + +with_test_prefix pre-main { + gdb_test "print mod_a::xxx" " = 10" + gdb_test "print mod_b::yyy" " = 20" + + # Namespace mod_b is using namespace mod_a, so mod_a::xxx is available as + # mod_b::xxx. This is not available here though, but later, at + # start-of-main. I wonder if this should also be available here. + gdb_test "print mod_b::xxx" \ + [string_to_regexp {No symbol "xxx" in namespace "mod_b".}] +} + +set line_main_entry [gdb_get_line_number main-entry] +if {![runto $srcfile:$line_main_entry]} { + return +} + +# Start of main. Function main is not yet using namespace mod_b. +with_test_prefix start-of-main { + # Namespace mod_b is using namespace mod_a, so mod_a::xxx is available as + # mod_b::xxx. See also the note at the identical command in pre-main. + gdb_test "print mod_b::xxx" " = 10" + + # Same command as in end-of-main, but not a regression test for PR34034. + gdb_test "print xxx" \ + [string_to_regexp {No symbol "xxx" in current context.}] + + # Same test as in end-of-main, but not a regression test for PR34051. + gdb_test "print mod_a::yyy" \ + [string_to_regexp {No symbol "yyy" in namespace "mod_a".}] +} + +set line_main_return [gdb_get_line_number "main-return"] +gdb_test "next" \ + [subst_vars {$line_main_return\t[^\r\n]+}] + +# End of main. Function main is using namespace mod_b. +with_test_prefix end-of-main { + # Function main is using namespace mod_b, so mod_b::yyy is available as + # yyy. + gdb_test "print yyy" " = 20" + + # 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 " $ = 20". Regression test for PR34051. + gdb_test "print mod_a::yyy" \ + [string_to_regexp {No symbol "yyy" in namespace "mod_a".}] +} -- 2.51.0