Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Oleg Tolmatcev <oleg.tolmatcev@gmail.com>
To: gdb-patches@sourceware.org
Cc: Oleg Tolmatcev <oleg.tolmatcev@gmail.com>
Subject: [PATCH v2] gdb: search local symbols before global symbols in completion
Date: Mon, 14 Sep 2026 00:43:02 +0200	[thread overview]
Message-ID: <20260913224301.357-2-oleg.tolmatcev@gmail.com> (raw)

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


             reply	other threads:[~2026-09-13 22:44 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-13 22:43 Oleg Tolmatcev [this message]
  -- strict thread matches above, loose matches on Subject: below --
2026-08-03 20:25 Oleg Tolmatcev
2026-08-05  7:56 ` Oleg Tolmatcev

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260913224301.357-2-oleg.tolmatcev@gmail.com \
    --to=oleg.tolmatcev@gmail.com \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox