[v6,2/3] gdb: Tab complete internalvars in expressions

Message ID 20250406013809.230156-3-nt8r@protonmail.com
State New
Headers
Series Tab complete convenience variables |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gdb_build--master-aarch64 success Build passed
linaro-tcwg-bot/tcwg_gdb_build--master-arm success Build passed

Commit Message

Antonio Rische April 6, 2025, 1:39 a.m. UTC
  For example, 'print $f<tab>' or 'print $fo<tab>+$foo' after running
'set $foo=0' now tab completes.

'print $_siginf<tab>' also now tab completes.

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=7353
---
 gdb/NEWS                              |  3 +++
 gdb/completer.c                       |  9 ++++++++
 gdb/doc/gdb.texinfo                   |  4 ++--
 gdb/testsuite/gdb.base/completion.exp | 33 +++++++++++++++++++++++++++
 4 files changed, 47 insertions(+), 2 deletions(-)
  

Comments

Eli Zaretskii April 6, 2025, 5:21 a.m. UTC | #1
> Date: Sun, 06 Apr 2025 01:39:04 +0000
> From: Antonio Rische <nt8r@protonmail.com>
> Cc: Antonio Rische <nt8r@protonmail.com>
> 
> For example, 'print $f<tab>' or 'print $fo<tab>+$foo' after running
> 'set $foo=0' now tab completes.
> 
> 'print $_siginf<tab>' also now tab completes.
> 
> Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=7353
> ---
>  gdb/NEWS                              |  3 +++
>  gdb/completer.c                       |  9 ++++++++
>  gdb/doc/gdb.texinfo                   |  4 ++--
>  gdb/testsuite/gdb.base/completion.exp | 33 +++++++++++++++++++++++++++
>  4 files changed, 47 insertions(+), 2 deletions(-)

Thanks the documentation parts are okay.

Reviewed-By: Eli Zaretskii <eliz@gnu.org>
  
Keith Seitz April 11, 2025, 8:08 p.m. UTC | #2
Hi,

Thank you for the updated patch!

On 4/5/25 6:39 PM, Antonio Rische wrote:
> diff --git a/gdb/testsuite/gdb.base/completion.exp b/gdb/testsuite/gdb.base/completion.exp
> index 571d714b7..5b04b62ed 100644
> --- a/gdb/testsuite/gdb.base/completion.exp
> +++ b/gdb/testsuite/gdb.base/completion.exp
> @@ -1005,3 +1005,36 @@ foreach_with_prefix spc { " " "" } {
>   	    }
>       }
>   }
> +
> +# Test command completion with convenience variables.
> +test_gdb_complete_unique \
> +    "print \$_sigin" \
> +    "print \$_siginfo"
> +
> +test_gdb_complete_unique \
> +    "print \$_siginfo" \
> +    "print \$_siginfo"
> +
> +test_gdb_complete_unique \
> +    "print \$_gdb_maj" \
> +    "print \$_gdb_major"
> +
> +test_gdb_complete_unique "print \$_gdb_maint_setting_str" "print \$_gdb_maint_setting_str"
> +
> +test_gdb_complete_tab_multiple "print \$_gdb_mai" "nt_setting" {
> +	"_gdb_maint_setting"
> +	"_gdb_maint_setting_str"
> +    }

I have to admit, I am still disappointed that these do not contain the
leading '$', but I can certainly see the pain involved with making this
work, so I'm going to just leave this be. It's still much better than
we have today.

> +
> +test_gdb_complete_cmd_multiple "" "print \$_gdb_maint_setting" {
> +	"print \$_gdb_maint_setting"
> +	"print \$_gdb_maint_setting_str"
> +    }
> +
> +# Check that a nonexisting convenience variable is not completed.

"non-existing" -- no need to repost, just change your local copy.

> +test_gdb_complete_none "print \$unique_variable_1234567"
> +test_gdb_complete_none "set \$unique_variable_1234567"
> +
> +# Check that after defining the convenience variable it is completed.
> +gdb_test_no_output "set \$unique_variable_1234567=4"
> +test_gdb_complete_unique "print \$unique_variable_123456" "print \$unique_variable_1234567"

Reviewed-By: Keith Seitz <keiths@redhat.com>

Keith
  

Patch

diff --git a/gdb/NEWS b/gdb/NEWS
index 07c1193d8..7069f3b4b 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -121,6 +121,9 @@  qXfer:threads:read
   previously treated variables set to void as having never been set (see
   the $_isvoid convenience function for this behavior).
 
+* Tab completion now suggests convenience variables when tab completing after
+  the '$' symbol in expressions.
+
 *** Changes in GDB 16
 
 * Support for Nios II targets has been removed as this architecture
diff --git a/gdb/completer.c b/gdb/completer.c
index 0a8409b44..f94d467ee 100644
--- a/gdb/completer.c
+++ b/gdb/completer.c
@@ -1497,6 +1497,15 @@  complete_expression (completion_tracker &tracker,
       && expr_completer->complete (exp.get (), tracker))
     return;
 
+  /* If the text is non-empty, see if the word is preceded by '$'.  */
+  if (text[0] != '\0' && text[strlen (text) - strlen (word) - 1] == '$')
+    {
+      /* We don't support completion of history indices.  */
+      if (!isdigit (word[0]))
+	complete_internalvar (tracker, word);
+      return;
+    }
+
   complete_files_symbols (tracker, text, word);
 }
 
diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index e034ac532..f18fe049b 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -1908,8 +1908,8 @@  limit of 10 elements to print for arrays and strings.
 @value{GDBN} can fill in the rest of a word in a command for you, if there is
 only one possibility; it can also show you what the valid possibilities
 are for the next word in a command, at any time.  This works for @value{GDBN}
-commands, @value{GDBN} subcommands, command options, and the names of symbols
-in your program.
+commands, @value{GDBN} subcommands, command options, convenience variables, and
+the names of symbols in your program.
 
 Press the @key{TAB} key whenever you want @value{GDBN} to fill out the rest
 of a word.  If there is only one possibility, @value{GDBN} fills in the
diff --git a/gdb/testsuite/gdb.base/completion.exp b/gdb/testsuite/gdb.base/completion.exp
index 571d714b7..5b04b62ed 100644
--- a/gdb/testsuite/gdb.base/completion.exp
+++ b/gdb/testsuite/gdb.base/completion.exp
@@ -1005,3 +1005,36 @@  foreach_with_prefix spc { " " "" } {
 	    }
     }
 }
+
+# Test command completion with convenience variables.
+test_gdb_complete_unique \
+    "print \$_sigin" \
+    "print \$_siginfo"
+
+test_gdb_complete_unique \
+    "print \$_siginfo" \
+    "print \$_siginfo"
+
+test_gdb_complete_unique \
+    "print \$_gdb_maj" \
+    "print \$_gdb_major"
+
+test_gdb_complete_unique "print \$_gdb_maint_setting_str" "print \$_gdb_maint_setting_str"
+
+test_gdb_complete_tab_multiple "print \$_gdb_mai" "nt_setting" {
+	"_gdb_maint_setting"
+	"_gdb_maint_setting_str"
+    }
+
+test_gdb_complete_cmd_multiple "" "print \$_gdb_maint_setting" {
+	"print \$_gdb_maint_setting"
+	"print \$_gdb_maint_setting_str"
+    }
+
+# Check that a nonexisting convenience variable is not completed.
+test_gdb_complete_none "print \$unique_variable_1234567"
+test_gdb_complete_none "set \$unique_variable_1234567"
+
+# Check that after defining the convenience variable it is completed.
+gdb_test_no_output "set \$unique_variable_1234567=4"
+test_gdb_complete_unique "print \$unique_variable_123456" "print \$unique_variable_1234567"