From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id cVhLCmAnp2oDtwkAWB0awg (envelope-from ) for ; Sun, 13 Sep 2026 18:44:48 -0400 Authentication-Results: simark.ca; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20251104 header.b=GGUaJdU2; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id 26F231E051; Sun, 13 Sep 2026 18:44:48 -0400 (EDT) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-5.4 required=5.0 tests=ARC_SIGNED,ARC_VALID,BAYES_00, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED autolearn=ham autolearn_force=no version=4.0.1 Received: from vm01.sourceware.org (vm01.sourceware.org [IPv6:2620:52:6:3111::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 266BC1E033 for ; Sun, 13 Sep 2026 18:44:47 -0400 (EDT) Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 17CFA4BB1C0D for ; Sun, 13 Sep 2026 22:44:46 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 17CFA4BB1C0D Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20251104 header.b=GGUaJdU2 Received: from mail-wm1-x333.google.com (mail-wm1-x333.google.com [IPv6:2a00:1450:4864:20::333]) by sourceware.org (Postfix) with ESMTPS id DE5C14B9DB6E for ; Sun, 13 Sep 2026 22:44:20 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org DE5C14B9DB6E Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org DE5C14B9DB6E Authentication-Results: sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::333 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1789339461; cv=none; b=NC66WtxbJo4qwFSRm8FwCidAtLXyv0xr5viGyIp3G0FxNE/SkPqMe2oVuiQihTmO473mJiFPAcbg0dettoHudS8sIdaFr7RoJg9bTAvXDb1vwTTGm8JIA6i0Ttu+SJSNbK6YPv95e2aNIUEfpocisoaFKRS5QINgITPqase23Tk= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1789339461; c=relaxed/simple; bh=lCVjPTBMKJlmbWZWJSETrIEwVzdza+fjHTQ3+pUjyR4=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=pWqN5ODf5C87VwnI2i0r22Tf8fQrMWbck4iaOy0TnBS+6e/6NFXR9/m5PY3cLj9ssAOnCTEDrEvNkUUnKErJkGpnG4fiscJXQQd71hzSzJ44Yu5Acl6HdbyGzejG1s7rXQgAujMeamOLQarvGHEbIpGLzbqFe/Lddf1ZQ7WSu50= ARC-Authentication-Results: i=1; sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20251104 header.b=GGUaJdU2 DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org DE5C14B9DB6E Received: by mail-wm1-x333.google.com with SMTP id 5b1f17b1804b1-49e6bad7b79so12722995e9.1 for ; Sun, 13 Sep 2026 15:44:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1789339460; x=1789944260; darn=sourceware.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=+WE3cXdZSSFR3dw1VanPoBaVXHrAhMqAuZWUYsC+EfM=; b=GGUaJdU2MyvPk7od1tJKabPjDJny6phPPqWx9AhhNiLucKuTCTc2vjoszeCGVpZr46 6E2MrGCUtMT0NhJ980q8tXpWptztXl3rZGXMUC6PepSXx73tC4LsRsn/vXcWOpn4wXOt xTwXFQWBkgUJBIK4pzzN0Nil0FBn1Led8YfGaGlykRSvHCiVH8vsFjSXWS5XCAvdhmyG rOR/hxdUGVEjqK8DGSMsMwfHjjtVxSy0e2KRwDzJ4qy+YsqFulWdcG0EGGQe0zd/x43v ltTictBevknlrycVh1Zj2jJEcOPMsAQZ+qoV7uofneiksavofKFEadY91hAWY6VeHGiw 1hew== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1789339460; x=1789944260; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=+WE3cXdZSSFR3dw1VanPoBaVXHrAhMqAuZWUYsC+EfM=; b=q3vbNSUOQLxeaj/P9dBEykJfrdw30IS6MGKoEKYfhbQDErvZwBQ+Po0iNiOjboP2sr NA4BDyXG9+NdSyiMrLXeiwLU08CypFDBV6YbEPkDeF6ER/YLHO4WsRUcpJFSMwlkHOka Efr41puAcnB/NB1nAu5LuWqNVJIBcqDDOPZfMGzW17WUaRAs3tV8NUDi+201PUkke3ri 3nHeUXU81HEOC4X3P+qLidx4aXY5HqylGb65sG0aeutWEJ11Su5f4QouKPM3/JjgS5Ee GnpNTh8aBbITZ6V/kiVoTJzBt6zAlPeweT8dePiQLFgQphCnK3F/2zcDj0AD9Xazy6H1 pPXA== X-Gm-Message-State: AFuF++noVMijtuZDZkE8iFo6LKVzRXCk2F6rr1JSt9zgrkvjteEjCrfR LKCBycO0egvAbZMv+j6gA9Zmg1tflkeMv7qEyY1Irv38A9hEzpHsJHTQiydSxg== X-Gm-Gg: AYBFou0mLHuYu9yxISLBMenRH6++1Q1Xl5trN84Prp5O0ewSgkakUVdl6TBlEcp78t4 lczyhljMDayc7wdotUVvnxFjZSC7e7Ztkn1N5F6DNSCLUe+X3XV6ywtTEd2rPLpuVN4ridb06n3 2aRrWIyolEVXAyuxJygiHSRn4OQJOum7Jxm1HD/jT9WBVgQh5aDSLD025wUbuIr936rTPwPiPjt XEiUVgW6Tk0OrI/bQq+Mv5R9+kIXGTvyIObBUogCJEwISIfjRXThaPB/qkZ5EN8JlU4FSKkT7kn kVcSL39ePAWJapGRLmBfJjjYoWaQvdp6EpdznMtasZgq7mVm/j//299FC8b4McEHHJ20K5wp3bn tyAFS80PIgvMK5HaKCXMZ28Q5OR5a6rt70hs9Az9qUscJQ63C0dx4AQ2No21UKgDhKLUDTsv7J+ 960ZjuFjc0kB7x55b5LWITJw96pKBcJjM2u+9PW3EMQO3hQB7OY26/ALf2GlkXNiJfm1shkCGQI UF3JSm2QaxTWExltua3Ier7PISMcZIU9vqs7uP1J6piCtT52ixW89fRr0WE8LcVRclA X-Received: by 2002:a05:600c:3150:b0:49e:747e:c00e with SMTP id 5b1f17b1804b1-49e747ec158mr62178535e9.9.1789339459730; Sun, 13 Sep 2026 15:44:19 -0700 (PDT) Received: from Mein-PC (p200300ed8f441a0024c1028e800bc45b.dip0.t-ipconnect.de. [2003:ed:8f44:1a00:24c1:28e:800b:c45b]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-49d26b7332asm351049035e9.0.2026.09.13.15.44.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 13 Sep 2026 15:44:19 -0700 (PDT) From: Oleg Tolmatcev To: gdb-patches@sourceware.org Cc: Oleg Tolmatcev Subject: [PATCH v2] gdb: search local symbols before global symbols in completion Date: Mon, 14 Sep 2026 00:43:02 +0200 Message-ID: <20260913224301.357-2-oleg.tolmatcev@gmail.com> X-Mailer: git-send-email 2.55.0.windows.5 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 When symbol completion is performed from a selected frame, search the local blocks before scanning minimal symbols and global/static symbols. This makes completion prefer names that are visible in the current context, including local variables. Add a regression test that checks completion prefers a local symbol over a global symbol with the same name. --- gdb/symtab.c | 72 +++++++++++++-------------- gdb/testsuite/gdb.dap/completions.exp | 15 ++++++ 2 files changed, 51 insertions(+), 36 deletions(-) This patch is also based on the patch in https://sourceware.org/pipermail/gdb-patches/2026-September/230035.html. diff --git a/gdb/symtab.c b/gdb/symtab.c index 4c27f3951fd..c2e6f91b247 100644 --- a/gdb/symtab.c +++ b/gdb/symtab.c @@ -5931,6 +5931,42 @@ default_collect_symbol_completion_matches_break_on lookup_name_info lookup_name (sym_text, name_match_type, true); + /* Search upwards from currently selected frame (so that we can + complete on local vars). Also catch fields of types defined in + this places which match our text string. Only complete on types + visible from current context. */ + + b = get_selected_block (); + surrounding_static_block = b == nullptr ? nullptr : b->static_block (); + surrounding_global_block = b == nullptr ? nullptr : b->global_block (); + if (surrounding_static_block != NULL) + while (b != surrounding_static_block) + { + QUIT; + + for (struct symbol *sym : block_iterator_range (b)) + { + if (code == TYPE_CODE_UNDEF) + { + completion_list_add_symbol (tracker, sym, lookup_name, + sym_text, word); + completion_list_add_fields (tracker, sym, lookup_name, + sym_text, word); + } + else if (sym->domain () == STRUCT_DOMAIN + && sym->type ()->code () == code) + completion_list_add_symbol (tracker, sym, lookup_name, + sym_text, word); + } + + /* Stop when we encounter an enclosing function. Do not stop for + non-inlined functions - the locals of the enclosing function + are in scope for a nested function. */ + if (b->function () != NULL && b->inlined_p ()) + break; + b = b->superblock (); + } + /* At this point scan through the misc symbol vectors and add each symbol you find to the list. Eventually we want to ignore anything that isn't a text symbol (everything else will be @@ -5974,42 +6010,6 @@ default_collect_symbol_completion_matches_break_on SEARCH_ALL_DOMAINS); } - /* Search upwards from currently selected frame (so that we can - complete on local vars). Also catch fields of types defined in - this places which match our text string. Only complete on types - visible from current context. */ - - b = get_selected_block (); - surrounding_static_block = b == nullptr ? nullptr : b->static_block (); - surrounding_global_block = b == nullptr ? nullptr : b->global_block (); - if (surrounding_static_block != NULL) - while (b != surrounding_static_block) - { - QUIT; - - for (struct symbol *sym : block_iterator_range (b)) - { - if (code == TYPE_CODE_UNDEF) - { - completion_list_add_symbol (tracker, sym, lookup_name, - sym_text, word); - completion_list_add_fields (tracker, sym, lookup_name, - sym_text, word); - } - else if (sym->domain () == STRUCT_DOMAIN - && sym->type ()->code () == code) - completion_list_add_symbol (tracker, sym, lookup_name, - sym_text, word); - } - - /* Stop when we encounter an enclosing function. Do not stop for - non-inlined functions - the locals of the enclosing function - are in scope for a nested function. */ - if (b->function () != NULL && b->inlined_p ()) - break; - b = b->superblock (); - } - /* Add fields from the file's types; symbols will be added below. */ if (code == TYPE_CODE_UNDEF) diff --git a/gdb/testsuite/gdb.dap/completions.exp b/gdb/testsuite/gdb.dap/completions.exp index b5b0ad840b7..1c3ca4c54b5 100644 --- a/gdb/testsuite/gdb.dap/completions.exp +++ b/gdb/testsuite/gdb.dap/completions.exp @@ -104,6 +104,21 @@ with_test_prefix "no frame" { check_results [dict get $result body targets] 1 2 3 } +# Check that local symbols are searched before global symbols. Limiting +# the number of results makes the search order observable. +dap_check_request_and_response "limit completions" evaluate \ + {o expression [s "set max-completions 1"] context [s repl]} + +set result [lindex [dap_check_request_and_response \ + "completion with limit" completions \ + [format {o frameId [i %s] text [s {%s}] column [i %s]} \ + $inner_frame_id $pfx $col]] \ + 0] + +with_test_prefix local_symbol { + check_results [dict get $result body targets] 4 +} + # The result here isn't important, this is just checking that the # request doesn't fail. dap_check_request_and_response "completion of empty string" completions \ -- 2.55.0.windows.5