From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id L+d6DYj5cGrPigMAWB0awg (envelope-from ) for ; Mon, 03 Aug 2026 16:26: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=VWo8jCF2; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id 2554F1E099; Mon, 03 Aug 2026 16:26: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 [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 514521E099 for ; Mon, 03 Aug 2026 16:26:47 -0400 (EDT) Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id B9C044BB24CD for ; Mon, 3 Aug 2026 20:26:46 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B9C044BB24CD 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=VWo8jCF2 Received: from mail-wm1-x32d.google.com (mail-wm1-x32d.google.com [IPv6:2a00:1450:4864:20::32d]) by sourceware.org (Postfix) with ESMTPS id 3CFA54BA2E08 for ; Mon, 3 Aug 2026 20:26:23 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 3CFA54BA2E08 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 3CFA54BA2E08 Authentication-Results: sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::32d ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1785788783; cv=none; b=eUzrVWOrs2fdTXECFr5MDT9iVexbjYXkrOPEMqgomphKWEIpNL7yH0N3uUBScqt3m/ZphDNfF3SixTjoqWMyI3z1aAedzO+ZQ+l0uOD23EzRfgdFnt4uhsNhWi+Ll9JAT8uHW+38u0bSAVLqWn1mq6RG/1MHusvw4l3zL2okTVQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1785788783; c=relaxed/simple; bh=mD5BA12yCqpWOEgdViuPQUeHxTk+2OcIY8Q4xJ3WGtI=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=jrgytc87dVgxIAgigei+v32TvjNcZCMWEz1LzpasYm3E2luQGAHYOujyLNxNwiRYZ/3p4rJqvlsV5fcLj/ncGia5YNS+wJTwq4JN6htYJY35UxWUuw02DuRNAHYJUA3czDEsX567emIhqEImKZXANWZSlY1elxsPsaSjjX7h50Y= 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=VWo8jCF2 DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3CFA54BA2E08 Received: by mail-wm1-x32d.google.com with SMTP id 5b1f17b1804b1-4921eed3fa2so14090715e9.0 for ; Mon, 03 Aug 2026 13:26:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1785788782; x=1786393582; 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=1lvYhhKgshfeyjeCQtvosV1HABeIuR1pW70Ez0zoOM0=; b=VWo8jCF2cr6scV1Fo9xkGtcjYTcUv9WAKlnIlxBBHqBsWGA60jvcuz57xptvBBN8W9 J7MPPG/QAEqPeCZUyEBF/NjQlBjWJFADc5I0MrbmRYm+9jWG2wcAhX/DK7aqJDFaVBmP sqqZqtyszcMUQeadj4ZsJ0hLS6y1JiQJLWPbmISMQmvEiYd1sEcKCnS0eIscP2D0kwPQ l8zGsENJ7Pi/YrE8Foz1nnCk2+a/hBV6m2yJkIoUrY5DUFOKyJ15M+sxf7S2A3rvlmnq zpp4Jj2BlL6mNTICG2frv5q7inggpM4P7BlpFuCT473zgCzs/fvzr4C/5DUVyw9cvGlJ OEuQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1785788782; x=1786393582; 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=1lvYhhKgshfeyjeCQtvosV1HABeIuR1pW70Ez0zoOM0=; b=PJkvLvhVUVzD765Vj855FbO61Rl7QmI5pr57vxFRHAQYrl4SB5mn7gZmRyQtEATinJ oPQ/S4RufDqaJXTrYgjcYLh/IOX4eMDgosiXOXlxK7v/YsoTVWy+f8vAQxycA5HM5Tan ttGNMmYMEZ0wYymyQrLpU8p4BgqwBNTxuZhywndYXKz9fPiuM1P9OWJpnDIIfOBJXiCZ H+UzyiBvuMBwOxVbDLm/Bm/XFF0mCJxMRoai4bd12rwzfmxwkCLr+oGVSbwokhTZx6qd 4pjWxhhVZ1njcod/XgGarO5gPxng44PQ+z/szpRXOVbQ/avHGxJGRdAFiCAup8fhWXVC Hb6g== X-Gm-Message-State: AOJu0YxptwHrOuX/bXNhVVTVBNdbZFBvmAgRhJ/uaK1KQ1/+UwOCpfEu zDWYuAbNVRj1Bab9wthET0llHFf3ByPUZhMcnRWxSGBQXSj6KjznqplMCAY9eg== X-Gm-Gg: AR+sD13FcxKcw/J8A+n7FEebko9eL3KyqgeKDektEm8wXrn0LUjLcaJFFBs/whkk696 H5cKVbg62LBndF0aCL7Hb+vyqzDUCfVZQB/KpuXzB0m5lIfQokNwpKcYTBf+fOJ2/8gWGy9Haqx juoL+J3HiF2f/jWPB2H8XgXJoAZh3B78JvQOoC3w1DnglE0pKvcrU/Lpip8qA25svdlDfqo0JqC dlMLUQRkeReGNGwr7l/on+ciCspMjhl5FM+E/Eju1ofJqAaczuirhQwAfsb3J5lb8NowhGBBug0 AvGfmc1moPGwZ6elt9LtMtZEEwwj4VVa8iN2BjmCdPxqNiGl2ll0yHIkTLEQzs094haw7VQUihA lD2S+1SE7TBn1od6QAIjxNQSy58XuEvAj5IbXOPEQYIEicYuFXUd03KqFeSrXePl7BSwsvLo/GJ 9Ak3IhS8Br1eeXkokR2XpqX4Mso1iSUz4kJ58GSv2zsO4DQsZE3LyXwIUsXLYyQLOcn8e/qFOcf XjVJMKBAS4EKEvADIQabAuwriRPQt9S/RmEuAaUCnUDgJgcfa6VBw== X-Received: by 2002:a05:600c:871a:b0:498:952:e276 with SMTP id 5b1f17b1804b1-4980c66c991mr305854945e9.8.1785788781383; Mon, 03 Aug 2026 13:26:21 -0700 (PDT) Received: from Mein-PC (p200300ed8f468f0000b202c053620649.dip0.t-ipconnect.de. [2003:ed:8f46:8f00:b2:2c0:5362:649]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4994a100e14sm20182385e9.14.2026.08.03.13.26.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 03 Aug 2026 13:26:20 -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, 3 Aug 2026 22:25:30 +0200 Message-ID: <20260803202529.506-2-oleg.tolmatcev@gmail.com> X-Mailer: git-send-email 2.55.0.windows.1 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.base/break.c | 21 ++++++++ gdb/testsuite/gdb.base/completion.exp | 6 +++ 3 files changed, 63 insertions(+), 36 deletions(-) diff --git a/gdb/symtab.c b/gdb/symtab.c index 5d5076f2e77..85665bebdf4 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.base/break.c b/gdb/testsuite/gdb.base/break.c index 26085d40ca2..c9b74c6a567 100644 --- a/gdb/testsuite/gdb.base/break.c +++ b/gdb/testsuite/gdb.base/break.c @@ -23,6 +23,26 @@ extern int marker2 (int a); extern void marker3 (char *a, char *b); extern void marker4 (long d); +struct completion_global_struct +{ + int global_field; +}; + +struct completion_local_struct +{ + int local_field; +}; + +struct completion_global_struct completion_var; + +static void +completion_local_over_global (void) +{ + struct completion_local_struct completion_var = { 23 }; + + completion_var.local_field++; /* local-completion-breakpoint */ +} + /* We're used by a test that requires malloc, so make sure it is in the executable. */ void *need_malloc () @@ -50,6 +70,7 @@ main (int argc, char **argv, char **envp) marker2 (43); /* set breakpoint 20 here */ marker3 ("stack", "trace"); /* set breakpoint 21 here */ marker4 (177601976L); + completion_local_over_global (); /* We're used by a test that requires malloc, so make sure it is in the executable. */ (void)malloc (1); diff --git a/gdb/testsuite/gdb.base/completion.exp b/gdb/testsuite/gdb.base/completion.exp index 44cf649d5b6..9848f21d951 100644 --- a/gdb/testsuite/gdb.base/completion.exp +++ b/gdb/testsuite/gdb.base/completion.exp @@ -87,6 +87,12 @@ gdb_test "complete set gnutarget aut" "set gnutarget auto" gdb_test "complete set cp-abi aut" "set cp-abi auto" +# Check that symbol completion in a selected frame prefers a local +# symbol over a global symbol with the same name. +gdb_breakpoint [gdb_get_line_number "local-completion-breakpoint"] +gdb_continue_to_breakpoint "local-completion-breakpoint" +gdb_test "complete p completion_var.l" "p completion_var.local_field" + # Test that completion of commands 'target FOO' works well. set targets [list "core" "tfile" "exec"] -- 2.55.0.windows.1