diff mbox

[v2] Add completer for skip numbers

Message ID 20181111165446.7069-1-simon.marchi@polymtl.ca
State New
Headers show

Commit Message

Simon Marchi Nov. 11, 2018, 4:54 p.m. UTC
Add completer to various commands that accept skip numbers:

  - skip enable
  - skip disable
  - skip delete
  - info skip

These commands also accept ranges, the completer works for that but is
not very smart.  It will suggest invalid ranges, for example when doing
"2-<TAB>" it will suggest "1", which would not result in a valid range.
Also, it will keep suggesting when doing "1-2-<TAB>", even though it's
an invalid syntax.

A future idea would be to make a re-usable and well-tested completer for
numbers and ranges.  I think it could at least be re-used for breakpoint
and thread numbers (for example with the "enable breakpoints" command).

gdb/ChangeLog:

	* skip.c (complete_skip_number): New function.
	(_initialize_step_skip): Add completers to some skip commands.

gdb/testsuite/ChangeLog:

	* gdb.base/skip.exp: Add standard_testfile.  Add "skip delete"
	completer tests.
---
 gdb/skip.c                      | 35 +++++++++++++++++++++++++-------
 gdb/testsuite/gdb.base/skip.exp | 36 +++++++++++++++++++++++++++++++++
 2 files changed, 64 insertions(+), 7 deletions(-)

Comments

Pedro Alves Nov. 12, 2018, 12:41 p.m. UTC | #1
On 11/11/2018 04:54 PM, Simon Marchi wrote:
> Add completer to various commands that accept skip numbers:
> 
>   - skip enable
>   - skip disable
>   - skip delete
>   - info skip
> 
> These commands also accept ranges, the completer works for that but is
> not very smart.  It will suggest invalid ranges, for example when doing
> "2-<TAB>" it will suggest "1", which would not result in a valid range.
> Also, it will keep suggesting when doing "1-2-<TAB>", even though it's
> an invalid syntax.
> 
> A future idea would be to make a re-usable and well-tested completer for
> numbers and ranges.  I think it could at least be re-used for breakpoint
> and thread numbers (for example with the "enable breakpoints" command).
> 
> gdb/ChangeLog:
> 
> 	* skip.c (complete_skip_number): New function.
> 	(_initialize_step_skip): Add completers to some skip commands.
> 
> gdb/testsuite/ChangeLog:
> 
> 	* gdb.base/skip.exp: Add standard_testfile.  Add "skip delete"
> 	completer tests.

OK.

Thanks,
Pedro Alves
Simon Marchi Nov. 12, 2018, 3:40 p.m. UTC | #2
On 2018-11-12 07:41, Pedro Alves wrote:
> On 11/11/2018 04:54 PM, Simon Marchi wrote:
>> Add completer to various commands that accept skip numbers:
>> 
>>   - skip enable
>>   - skip disable
>>   - skip delete
>>   - info skip
>> 
>> These commands also accept ranges, the completer works for that but is
>> not very smart.  It will suggest invalid ranges, for example when 
>> doing
>> "2-<TAB>" it will suggest "1", which would not result in a valid 
>> range.
>> Also, it will keep suggesting when doing "1-2-<TAB>", even though it's
>> an invalid syntax.
>> 
>> A future idea would be to make a re-usable and well-tested completer 
>> for
>> numbers and ranges.  I think it could at least be re-used for 
>> breakpoint
>> and thread numbers (for example with the "enable breakpoints" 
>> command).
>> 
>> gdb/ChangeLog:
>> 
>> 	* skip.c (complete_skip_number): New function.
>> 	(_initialize_step_skip): Add completers to some skip commands.
>> 
>> gdb/testsuite/ChangeLog:
>> 
>> 	* gdb.base/skip.exp: Add standard_testfile.  Add "skip delete"
>> 	completer tests.
> 
> OK.
> 
> Thanks,
> Pedro Alves

Thanks, pushed.

Simon
diff mbox

Patch

diff --git a/gdb/skip.c b/gdb/skip.c
index 13db0f6b43c6..f2a1df79e8bf 100644
--- a/gdb/skip.c
+++ b/gdb/skip.c
@@ -641,6 +641,23 @@  function_name_is_marked_for_skip (const char *function_name,
   return false;
 }
 
+/* Completer for skip numbers.  */
+
+static void
+complete_skip_number (cmd_list_element *cmd,
+		      completion_tracker &completer,
+		      const char *text, const char *word)
+{
+  size_t word_len = strlen (word);
+
+  for (const skiplist_entry &entry : skiplist_entries)
+    {
+      gdb::unique_xmalloc_ptr<char> name (xstrprintf ("%d", entry.number ()));
+      if (strncmp (word, name.get (), word_len) == 0)
+	completer.add_completion (std::move (name));
+    }
+}
+
 void
 _initialize_step_skip (void)
 {
@@ -676,28 +693,31 @@  If no function name is given, skip the current function."),
 	       &skiplist);
   set_cmd_completer (c, location_completer);
 
-  add_cmd ("enable", class_breakpoint, skip_enable_command, _("\
+  c = add_cmd ("enable", class_breakpoint, skip_enable_command, _("\
 Enable skip entries.  You can specify numbers (e.g. \"skip enable 1 3\"), \
 ranges (e.g. \"skip enable 4-8\"), or both (e.g. \"skip enable 1 3 4-8\").\n\n\
 If you don't specify any numbers or ranges, we'll enable all skip entries.\n\n\
 Usage: skip enable [NUMBER | RANGE]..."),
-	   &skiplist);
+	       &skiplist);
+  set_cmd_completer (c, complete_skip_number);
 
-  add_cmd ("disable", class_breakpoint, skip_disable_command, _("\
+  c = add_cmd ("disable", class_breakpoint, skip_disable_command, _("\
 Disable skip entries.  You can specify numbers (e.g. \"skip disable 1 3\"), \
 ranges (e.g. \"skip disable 4-8\"), or both (e.g. \"skip disable 1 3 4-8\").\n\n\
 If you don't specify any numbers or ranges, we'll disable all skip entries.\n\n\
 Usage: skip disable [NUMBER | RANGE]..."),
-	   &skiplist);
+	       &skiplist);
+  set_cmd_completer (c, complete_skip_number);
 
-  add_cmd ("delete", class_breakpoint, skip_delete_command, _("\
+  c = add_cmd ("delete", class_breakpoint, skip_delete_command, _("\
 Delete skip entries.  You can specify numbers (e.g. \"skip delete 1 3\"), \
 ranges (e.g. \"skip delete 4-8\"), or both (e.g. \"skip delete 1 3 4-8\").\n\n\
 If you don't specify any numbers or ranges, we'll delete all skip entries.\n\n\
 Usage: skip delete [NUMBER | RANGES]..."),
-           &skiplist);
+	       &skiplist);
+  set_cmd_completer (c, complete_skip_number);
 
-  add_info ("skip", info_skip_command, _("\
+  c = add_info ("skip", info_skip_command, _("\
 Display the status of skips.  You can specify numbers (e.g. \"skip info 1 3\"), \
 ranges (e.g. \"skip info 4-8\"), or both (e.g. \"skip info 1 3 4-8\").\n\n\
 If you don't specify any numbers or ranges, we'll show all skips.\n\n\
@@ -705,6 +725,7 @@  Usage: skip info [NUMBER | RANGES]...\n\
 The \"Type\" column indicates one of:\n\
 \tfile        - ignored file\n\
 \tfunction    - ignored function"));
+  set_cmd_completer (c, complete_skip_number);
 
   add_setshow_boolean_cmd ("skip", class_maintenance,
 			   &debug_skip, _("\
diff --git a/gdb/testsuite/gdb.base/skip.exp b/gdb/testsuite/gdb.base/skip.exp
index 223c93d0d9b6..a7ea0b52c47b 100644
--- a/gdb/testsuite/gdb.base/skip.exp
+++ b/gdb/testsuite/gdb.base/skip.exp
@@ -16,6 +16,10 @@ 
 # This file was written by Justin Lebar. (justin.lebar@gmail.com)
 # And further hacked on by Doug Evans. (dje@google.com)
 
+load_lib completion-support.exp
+
+standard_testfile
+
 if { [prepare_for_testing "failed to prepare" "skip" \
                           {skip.c skip1.c } \
                           {debug nowarnings}] } {
@@ -297,3 +301,35 @@  with_test_prefix "step using -fi + -fu" {
     gdb_test "step" ".*" "step 4"; # Skip over test_skip()
     gdb_test "step" "test_skip_file_and_function \\(\\) at.*" "step 5"; # Return from skip1_test_skip_file_and_function()
 }
+
+with_test_prefix "skip delete completion" {
+    global binfile
+    clean_restart "${binfile}"
+    if ![runto_main] {
+	fail "can't run to main"
+	return
+    }
+
+    # Create a bunch of skips, don't care what they are.
+    for {set i 0} {$i < 12} {incr i} {
+	gdb_test "skip" ".*" "add skip $i"
+    }
+
+    set all_numbers { "1" "10" "11" "12" "2" "3" "4" "5" "6" "7" "8" "9" }
+
+    # Test completing single numbers.
+    test_gdb_complete_multiple "skip delete " "" "" $all_numbers
+    test_gdb_complete_multiple "skip delete " "1" "" { "1" "10" "11" "12" }
+    test_gdb_complete_multiple "skip delete 2 " "" "" $all_numbers
+    test_gdb_complete_unique "skip delete 11" "skip delete 11"
+
+    # Test completing ranges.
+    test_gdb_complete_multiple "skip delete 2-" "" "" $all_numbers
+    test_gdb_complete_unique "skip delete 2-5" "skip delete 2-5"
+
+    # Test cases with no completion.
+    test_gdb_complete_none "skip delete 123"
+    test_gdb_complete_none "skip delete a1"
+    test_gdb_complete_none "skip delete 2-33"
+}
+