Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
* [PATCH] gdb: search local symbols before global symbols in completion
@ 2026-08-01 16:01 Oleg Tolmatcev
  2026-08-03 11:03 ` Andrew Burgess
  0 siblings, 1 reply; 4+ messages in thread
From: Oleg Tolmatcev @ 2026-08-01 16:01 UTC (permalink / raw)
  To: gdb-patches; +Cc: Oleg Tolmatcev

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.
---
 gdb/symtab.c | 72 ++++++++++++++++++++++++++--------------------------
 1 file changed, 36 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)
-- 
2.55.0.windows.1


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] gdb: search local symbols before global symbols in completion
  2026-08-01 16:01 [PATCH] gdb: search local symbols before global symbols in completion Oleg Tolmatcev
@ 2026-08-03 11:03 ` Andrew Burgess
  2026-08-10 18:02   ` Oleg Tolmatcev
  0 siblings, 1 reply; 4+ messages in thread
From: Andrew Burgess @ 2026-08-03 11:03 UTC (permalink / raw)
  To: Oleg Tolmatcev, gdb-patches; +Cc: Oleg Tolmatcev

Oleg Tolmatcev <oleg.tolmatcev@gmail.com> writes:

> 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.
> ---
>  gdb/symtab.c | 72 ++++++++++++++++++++++++++--------------------------

It feels like this really needs a test in the gdb.dap/ directory.

Thanks,
Andrew


>  1 file changed, 36 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)
> -- 
> 2.55.0.windows.1


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] gdb: search local symbols before global symbols in completion
  2026-08-03 11:03 ` Andrew Burgess
@ 2026-08-10 18:02   ` Oleg Tolmatcev
  2026-08-21 15:51     ` Tom Tromey
  0 siblings, 1 reply; 4+ messages in thread
From: Oleg Tolmatcev @ 2026-08-10 18:02 UTC (permalink / raw)
  To: Andrew Burgess; +Cc: gdb-patches

пн, 3 авг. 2026 г. в 13:03, Andrew Burgess <aburgess@redhat.com>:
>
> Oleg Tolmatcev <oleg.tolmatcev@gmail.com> writes:
>
> > 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.
> > ---
> >  gdb/symtab.c | 72 ++++++++++++++++++++++++++--------------------------
>
> It feels like this really needs a test in the gdb.dap/ directory.
>
> Thanks,
> Andrew
>
>
> >  1 file changed, 36 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)
> > --
> > 2.55.0.windows.1
>

I tried 3 times, but every time my test causes regressions on ARM. I
don't know how to write a passing test.

Is a test really necessary for this trivial patch that only changes the
order of completion results?

Oleg

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] gdb: search local symbols before global symbols in completion
  2026-08-10 18:02   ` Oleg Tolmatcev
@ 2026-08-21 15:51     ` Tom Tromey
  0 siblings, 0 replies; 4+ messages in thread
From: Tom Tromey @ 2026-08-21 15:51 UTC (permalink / raw)
  To: Oleg Tolmatcev; +Cc: Andrew Burgess, gdb-patches

>>>>> "Oleg" == Oleg Tolmatcev <oleg.tolmatcev@gmail.com> writes:

Oleg> Is a test really necessary for this trivial patch that only changes the
Oleg> order of completion results?

To be clear, yeah, this would be desirable so it does not regress.
What would be really good is a test for the DAP completions request.

Tom

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-08-21 15:52 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-08-01 16:01 [PATCH] gdb: search local symbols before global symbols in completion Oleg Tolmatcev
2026-08-03 11:03 ` Andrew Burgess
2026-08-10 18:02   ` Oleg Tolmatcev
2026-08-21 15:51     ` Tom Tromey

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox