From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id uTMdAZ6O5GmndikAWB0awg (envelope-from ) for ; Sun, 19 Apr 2026 04:13:18 -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=mZ8Cd2Fm; dkim=pass header.d=suse.de header.i=@suse.de header.a=ed25519-sha256 header.s=susede2_ed25519 header.b=uG3hHjv9; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.a=rsa-sha256 header.s=susede2_rsa header.b=APEiYrjE; dkim=neutral header.d=suse.de header.i=@suse.de header.a=ed25519-sha256 header.s=susede2_ed25519 header.b=TF+rHs9C; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id ED2B71E0B1; Sun, 19 Apr 2026 04:13:17 -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 129B61E093 for ; Sun, 19 Apr 2026 04:13:15 -0400 (EDT) Received: from vm01.sourceware.org (localhost [127.0.0.1]) by sourceware.org (Postfix) with ESMTP id 611EE4C515FA for ; Sun, 19 Apr 2026 08:13:14 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 611EE4C515FA 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=mZ8Cd2Fm; dkim=pass header.d=suse.de header.i=@suse.de header.a=ed25519-sha256 header.s=susede2_ed25519 header.b=uG3hHjv9; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.a=rsa-sha256 header.s=susede2_rsa header.b=APEiYrjE; dkim=neutral header.d=suse.de header.i=@suse.de header.a=ed25519-sha256 header.s=susede2_ed25519 header.b=TF+rHs9C Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.223.131]) by sourceware.org (Postfix) with ESMTPS id 3DD694BA9000 for ; Sun, 19 Apr 2026 08:12:39 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 3DD694BA9000 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 3DD694BA9000 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=195.135.223.131 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1776586359; cv=none; b=Q2aRGKhyvfNXxhgoUhg7PDqKjzCGB+Lr4gJoJNMb4q2TzbMEroVSw71bBu1G9ls3tOUR1RXU0B3oPvSPuhXYNE/dtzUGE2+liXct97QMP2Jtl61TZEwyH1OMqNxJmivW39pmBdHmoczFfIceKM5E4rrzOlrSCG6UNklNm9AlDzo= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1776586359; c=relaxed/simple; bh=1tc0G4LgGdCgo4YL5P2nBHErOO4wgNAidtki02M59LI=; h=DKIM-Signature:DKIM-Signature:DKIM-Signature:DKIM-Signature:From: To:Subject:Date:Message-ID:MIME-Version; b=df87bFoCZuwMR0oA2w5jr0u2JGppMShqQ085BWtQoSZScih4VAizedKVWgCW+zUqNta9E83HvT1ynhGjwvXqXHpnMyWTW2HioQIRUM/98IG/tVMjWW4dZtl8J6M4OO4G/x1+thfpZoZYzxs3yVfdarqOK2y81YVw9ZuTGt5Nkao= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3DD694BA9000 Received: from imap1.dmz-prg2.suse.org (imap1.dmz-prg2.suse.org [IPv6:2a07:de40:b281:104: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-out2.suse.de (Postfix) with ESMTPS id E89615BD38 for ; Sun, 19 Apr 2026 08:12:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1776586358; 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=hDSgxS0ibUsen9aXCKh/MwM/ECo3Rkc8+dB0LVmR+9M=; b=mZ8Cd2FmH4sAH/KleKlQ6s44lWbEydiaoe5nPGle7EU6aPUAgdkGN1SqzjIthxYHYJDO/K KWvDIurb5KoeTCIQnymalMZ5tp+VZ9+1Ky9CPDCX68tzVilDk3vdbKFrEHk7I703oSJ/h0 hVhFhB441geTRrExDRPH8JL+aAa6fQQ= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1776586358; 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=hDSgxS0ibUsen9aXCKh/MwM/ECo3Rkc8+dB0LVmR+9M=; b=uG3hHjv9J+HBx19IlDO+UiAJyUbV85saGDqlpXkXmAHNKfpC/6V3ahzxY9gTmjJu7IvVny NZ8vMfr6wG4SHgAA== Authentication-Results: smtp-out2.suse.de; dkim=pass header.d=suse.de header.s=susede2_rsa header.b=APEiYrjE; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b=TF+rHs9C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1776586356; 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=hDSgxS0ibUsen9aXCKh/MwM/ECo3Rkc8+dB0LVmR+9M=; b=APEiYrjEwa+rcX6b5CAL1++aKs6EWFuJ13Q5kAEDhU9OFu0GXxSQPC6aW1sOcDun0TT+Zn mFHiVmaVeAdAaK66AcLF2lneawzuQ0qu6JYNQp+cuhg8ZGNZY3+MlJbHUJS+MDKpuH4AYX 5EVxX+MVxcqhY70yis0dJJAltFJzToA= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1776586356; 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=hDSgxS0ibUsen9aXCKh/MwM/ECo3Rkc8+dB0LVmR+9M=; b=TF+rHs9CZNWEGcFUNiE7r+8c1GkVADKtSKAPizzDeJ9dLOjEcImK50nTvyHKWHcl0asJZG W2OxRHMDzH4AlhCw== 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 CF73A593B0 for ; Sun, 19 Apr 2026 08:12:36 +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 YNdpMXSO5GktUAAAD6G6ig (envelope-from ) for ; Sun, 19 Apr 2026 08:12:36 +0000 From: Tom de Vries To: gdb-patches@sourceware.org Subject: [PATCH v5 2/3] [gdb/exp] Fix ignoring of incorrect namespace prefix Date: Sun, 19 Apr 2026 10:12:35 +0200 Message-ID: <20260419081236.570600-3-tdevries@suse.de> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20260419081236.570600-1-tdevries@suse.de> References: <20260419081236.570600-1-tdevries@suse.de> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spamd-Result: default: False [-3.01 / 50.00]; BAYES_HAM(-3.00)[100.00%]; NEURAL_HAM_LONG(-1.00)[-1.000]; MID_CONTAINS_FROM(1.00)[]; R_MISSING_CHARSET(0.50)[]; R_DKIM_ALLOW(-0.20)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; NEURAL_HAM_SHORT(-0.20)[-1.000]; MIME_GOOD(-0.10)[text/plain]; MX_GOOD(-0.01)[]; TO_MATCH_ENVRCPT_ALL(0.00)[]; ARC_NA(0.00)[]; RCVD_VIA_SMTP_AUTH(0.00)[]; FUZZY_RATELIMITED(0.00)[rspamd.com]; RCPT_COUNT_ONE(0.00)[1]; MIME_TRACE(0.00)[0:+]; FROM_HAS_DN(0.00)[]; PREVIOUSLY_DELIVERED(0.00)[gdb-patches@sourceware.org]; FROM_EQ_ENVFROM(0.00)[]; DBL_BLOCKED_OPENRESOLVER(0.00)[sourceware.org:url,suse.de:dkim,suse.de:mid,gnu.org:url,imap1.dmz-prg2.suse.org:helo,imap1.dmz-prg2.suse.org:rdns]; DKIM_SIGNED(0.00)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; RCVD_TLS_ALL(0.00)[]; TO_DN_NONE(0.00)[]; RCVD_COUNT_TWO(0.00)[2]; SPAMHAUS_XBL(0.00)[2a07:de40:b281:104:10:150:64:97:from]; DKIM_TRACE(0.00)[suse.de:+] X-Rspamd-Action: no action X-Rspamd-Server: rspamd1.dmz-prg2.suse.org X-Rspamd-Queue-Id: E89615BD38 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 = true; + { + const char *current_scope = (block->function_block () != nullptr + ? block->scope () + : nullptr /* Don't know. */); + directive_match = (current_scope != nullptr + ? streq (scope, current_scope) + : true /* 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 | 62 ++++++++++++++++++++++++ gdb/testsuite/gdb.cp/nsusing-2.exp | 75 ++++++++++++++++++++++++++++++ 3 files changed, 145 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 1903287770b..d7a960127dc 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 = true; + { + const char *current_scope = (block->function_block () != nullptr + ? block->scope () + : nullptr /* Don't know. */); + directive_match = (current_scope != nullptr + ? streq (scope, current_scope) + : true /* 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..226bda3e101 --- /dev/null +++ b/gdb/testsuite/gdb.cp/nsusing-2.cc @@ -0,0 +1,62 @@ +/* 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. */ + + { /* Variant 1: using block is stop block, using block is not function block. */ + using namespace mod_b; + void (xxx + yyy); + foo (); /* main-1. */ + } + + { /* Variant 2: using block is super block of stop block, using block is not function block. */ + using namespace mod_b; + { + void (xxx + yyy); + foo (); /* main-2. */ + } + } + + using namespace mod_b; + + { /* Variant 3: using block is super block of stop block, using block is function block. */ + void (xxx + yyy); + foo (); /* main-3. */ + } + + /* Variant 4: using block is stop block, using block is function block. */ + void (xxx + yyy); + foo (); /* main-4. */ + + return 0; +} diff --git a/gdb/testsuite/gdb.cp/nsusing-2.exp b/gdb/testsuite/gdb.cp/nsusing-2.exp new file mode 100644 index 00000000000..65e685d0d32 --- /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".}] +} + +# After start of main. Function main is using namespace mod_b. Check 4 variants. +foreach_with_prefix n {1 2 3 4} { + set line_main_n [gdb_get_line_number "main-$n"] + gdb_test "next" \ + [subst_vars {$line_main_n\t[^\r\n]+}] + + # 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