From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id YaZwC5//Amp9YTMAWB0awg (envelope-from ) for ; Tue, 12 May 2026 06:23:27 -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=UxTEsmRV; dkim=pass header.d=suse.de header.i=@suse.de header.a=ed25519-sha256 header.s=susede2_ed25519 header.b=bc/q64Cx; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.a=rsa-sha256 header.s=susede2_rsa header.b=HjApivSB; dkim=neutral header.d=suse.de header.i=@suse.de header.a=ed25519-sha256 header.s=susede2_ed25519 header.b=S8mbwkrr; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id 23C061E093; Tue, 12 May 2026 06:23:27 -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 A16F31E093 for ; Tue, 12 May 2026 06:23:24 -0400 (EDT) Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 4FB104BAE7D6 for ; Tue, 12 May 2026 10:23:23 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4FB104BAE7D6 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=UxTEsmRV; dkim=pass header.d=suse.de header.i=@suse.de header.a=ed25519-sha256 header.s=susede2_ed25519 header.b=bc/q64Cx; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.a=rsa-sha256 header.s=susede2_rsa header.b=HjApivSB; dkim=neutral header.d=suse.de header.i=@suse.de header.a=ed25519-sha256 header.s=susede2_ed25519 header.b=S8mbwkrr Received: from smtp-out2.suse.de (smtp-out2.suse.de [IPv6:2a07:de40:b251:101:10:150:64:2]) by sourceware.org (Postfix) with ESMTPS id D6A2A4BAD17C for ; Tue, 12 May 2026 10:22:00 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D6A2A4BAD17C 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 D6A2A4BAD17C Authentication-Results: sourceware.org; arc=none smtp.remote-ip=2a07:de40:b251:101:10:150:64:2 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1778581321; cv=none; b=eJHq0+oC9xVUQ8WlEM8dkSe9nNqKaQ9xLyujCm5Q8mC2BqW3VyGIRpWo6I3IRzlO/N55gcZkFkUyaY3R/iyeUvgABJMQI7pEjjRJDio/FLlU+nInQr9TjhuGH1EfNPKYFbF1OC+vd1kWUv14PfqniX+0lbTgQk5FoiVocD10dr4= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1778581321; c=relaxed/simple; bh=WQegjOvOV0ydbCL7e6/UnS1umdUFcuLObxMlr13sOvI=; h=DKIM-Signature:DKIM-Signature:DKIM-Signature:DKIM-Signature:From: To:Subject:Date:Message-ID:MIME-Version; b=Al93cuRFkiKQehNJduBbZxlKU7GLrWd+zp1YK/x8IOieT9mTXFPXZKek+ijVpgC1wVFQFfj9dwiLdXl4yBeTNT2ZsegAJCdo0jO5/m+pzZFTlNGp2nw9K7PqIQHFoHYYKTUtslvHNCEiWbS5KWlZ3tTxdA0fhmvht4L0UYIkj74= ARC-Authentication-Results: i=1; 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=UxTEsmRV; dkim=pass header.d=suse.de header.i=@suse.de header.a=ed25519-sha256 header.s=susede2_ed25519 header.b=bc/q64Cx; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.a=rsa-sha256 header.s=susede2_rsa header.b=HjApivSB; dkim=neutral header.d=suse.de header.i=@suse.de header.a=ed25519-sha256 header.s=susede2_ed25519 header.b=S8mbwkrr DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D6A2A4BAD17C 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-out2.suse.de (Postfix) with ESMTPS id AF5E075AFD for ; Tue, 12 May 2026 10:21:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1778581289; 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=9VkBUAGM4p845ZjFzKgwv/e3YiU4MSvSc5s3RNTXuqg=; b=UxTEsmRVPPC3cqFu5NmzHfWoxNZeENydJaDpaRepN/P60VCh1nBJM1pYKUoLz3y4PzkPbE DCIqMulse91Q9WHYZk2829eSoTf4NaZKhm1thTv3AmIviJ6RAsuY8OaCEmoB7KD2DSrg0/ poTTaxxplxZYIcXsAbDndGmXrZ9tB3w= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1778581289; 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=9VkBUAGM4p845ZjFzKgwv/e3YiU4MSvSc5s3RNTXuqg=; b=bc/q64Cxf/81vNcVbCz8pJ7ipala2tfzRz1ZKUhH73Fvy/Ap+Y1Dkk2ckfuv5h6ioqT4KO +dMw/wa5pgoOI0AA== Authentication-Results: smtp-out2.suse.de; none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1778581285; 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=9VkBUAGM4p845ZjFzKgwv/e3YiU4MSvSc5s3RNTXuqg=; b=HjApivSBDCidaiU/4dEwJDqYVtN9gGHZureO65LzOV+y4PbloYtubR/pC1O3ETDRWcUtOr uYJEIhg+v1kAbYzT5UiTNLvO/kG31P7HWGvFHuDajc8Xb1HUmf4InADJRjzBUitQpG/bdT 6DkbovMddt3Xm3mjobFvTTsBMeXWsfY= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1778581285; 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=9VkBUAGM4p845ZjFzKgwv/e3YiU4MSvSc5s3RNTXuqg=; b=S8mbwkrrerFKpGWGP5Eqg5cFFZmgyBvZZaRPvxjGqsVmdyQUrjMB/g2/zJ84Fks/lqPh6r H4s6HPJZBEqx1rBA== 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 94BCF593AB for ; Tue, 12 May 2026 10:21:25 +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 yDT+IiX/AmpjawAAD6G6ig (envelope-from ) for ; Tue, 12 May 2026 10:21:25 +0000 From: Tom de Vries To: gdb-patches@sourceware.org Subject: [PATCH v6 2/3] [gdb/exp] Fix ignoring of incorrect namespace prefix Date: Tue, 12 May 2026 12:21:23 +0200 Message-ID: <20260512102124.517642-3-tdevries@suse.de> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20260512102124.517642-1-tdevries@suse.de> References: <20260512102124.517642-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%]; NEURAL_HAM_LONG(-1.00)[-1.000]; MID_CONTAINS_FROM(1.00)[]; R_MISSING_CHARSET(0.50)[]; NEURAL_HAM_SHORT(-0.20)[-1.000]; MIME_GOOD(-0.10)[text/plain]; FUZZY_RATELIMITED(0.00)[rspamd.com]; RCVD_VIA_SMTP_AUTH(0.00)[]; RCPT_COUNT_ONE(0.00)[1]; ARC_NA(0.00)[]; DKIM_SIGNED(0.00)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; DBL_BLOCKED_OPENRESOLVER(0.00)[sourceware.org:url,gnu.org:url,imap1.dmz-prg2.suse.org:helo,suse.de:mid]; FROM_EQ_ENVFROM(0.00)[]; FROM_HAS_DN(0.00)[]; MIME_TRACE(0.00)[0:+]; RCVD_COUNT_TWO(0.00)[2]; TO_MATCH_ENVRCPT_ALL(0.00)[]; TO_DN_NONE(0.00)[]; PREVIOUSLY_DELIVERED(0.00)[gdb-patches@sourceware.org]; RCVD_TLS_ALL(0.00)[] 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. More concretely, cp_lookup_symbol_via_imports is called with: - scope == "mod_a", - name == "yyy", and - block.m_function.m_name == "main()", and when looking at "using namespace mod_b": ... (gdb) p *current $12 = {import_src = 0x344018c "mod_b", import_dest = 0x1b477a0 "", alias = 0x0, declaration = 0x0, next = 0x0, decl_line = 5, searched = 0, excludes = {0x0}} ... we hit "directive_match = true" because strlen (current->import_dest) == 0. 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. */); + } ... which gets us: - current_scope == "", and - directive_match == false, because scope == "mod_a", so streq (scope, current_scope) == false. 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