[2/7] gdb: option completion for 'save gdb-index' command

Message ID 523e35f23ec81debf21af6a42b148968c73e2ecd.1701107594.git.aburgess@redhat.com
State New
Headers
Series Changes to gdb-index creation |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gdb_build--master-aarch64 fail Patch failed to apply
linaro-tcwg-bot/tcwg_gdb_build--master-arm fail Patch failed to apply

Commit Message

Andrew Burgess Nov. 27, 2023, 5:55 p.m. UTC
  Add proper support for option completion to the 'save gdb-index'
command.  Update save_gdb_index_command function to make use of the
new option_def data structures for parsing the '-dwarf-5' option.
---
 gdb/dwarf2/index-write.c | 69 ++++++++++++++++++++++++++++++----------
 1 file changed, 52 insertions(+), 17 deletions(-)
  

Comments

Tom Tromey Nov. 27, 2023, 6:58 p.m. UTC | #1
>>>>> "Andrew" == Andrew Burgess <aburgess@redhat.com> writes:

Andrew> Add proper support for option completion to the 'save gdb-index'
Andrew> command.  Update save_gdb_index_command function to make use of the
Andrew> new option_def data structures for parsing the '-dwarf-5' option.

I thought there was some reason this was difficult... the reason the
'palves/filename-options' branch exists (maybe only in Pedro's github).

However if it works then it seems like a good improvement to me.

Tom
  
Andrew Burgess Nov. 27, 2023, 10:20 p.m. UTC | #2
Tom Tromey <tom@tromey.com> writes:

>>>>>> "Andrew" == Andrew Burgess <aburgess@redhat.com> writes:
>
> Andrew> Add proper support for option completion to the 'save gdb-index'
> Andrew> command.  Update save_gdb_index_command function to make use of the
> Andrew> new option_def data structures for parsing the '-dwarf-5' option.
>
> I thought there was some reason this was difficult... the reason the
> 'palves/filename-options' branch exists (maybe only in Pedro's github).

I've only skimmed that branch, but I think that branch is adding options
that are themselves filenames, e.g. '-option FILENAME', but that's not
the case here.  The '-dwarf-5' is the option, and that's separate from
FILENAME.

We already have two other commands that follow the same pattern as I'm
proposing here 'maint print c-tdesc' and 'compile file', admittedly,
neither of these are likely high usage commands, so maybe they are
broken in some subtle way that has never been spotted ...

... anyway, at least for the basic usage I've tested, everything still
seems fine with this change in place.

>
> However if it works then it seems like a good improvement to me.

Thanks,
Andrew
  

Patch

diff --git a/gdb/dwarf2/index-write.c b/gdb/dwarf2/index-write.c
index 8ee5e420936..c0867799f6d 100644
--- a/gdb/dwarf2/index-write.c
+++ b/gdb/dwarf2/index-write.c
@@ -1523,6 +1523,48 @@  write_dwarf_index (dwarf2_per_bfd *per_bfd, const char *dir,
     dwz_index_wip->finalize ();
 }
 
+/* Options structure for the 'save gdb-index' command.  */
+
+struct save_gdb_index_options
+{
+  bool dwarf_5 = false;
+};
+
+/* The option_def list for the 'save gdb-index' command.  */
+
+static const gdb::option::option_def save_gdb_index_options_defs[] = {
+  gdb::option::boolean_option_def<save_gdb_index_options> {
+    "dwarf-5",
+    [] (save_gdb_index_options *opt) { return &opt->dwarf_5; },
+    nullptr, /* show_cmd_cb */
+    nullptr /* set_doc */
+  }
+};
+
+/* Create an options_def_group for the 'save gdb-index' command.  */
+
+static gdb::option::option_def_group
+make_gdb_save_index_options_def_group (save_gdb_index_options *opts)
+{
+  return {{save_gdb_index_options_defs}, opts};
+}
+
+/* Completer for the "save gdb-index" command.  */
+
+static void
+gdb_save_index_cmd_completer (struct cmd_list_element *ignore,
+			      completion_tracker &tracker,
+			      const char *text, const char *word)
+{
+  auto grp = make_gdb_save_index_options_def_group (nullptr);
+  if (gdb::option::complete_options
+      (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, grp))
+    return;
+
+  word = advance_to_filename_complete_word_point (tracker, text);
+  filename_completer (ignore, tracker, text, word);
+}
+
 /* Implementation of the `save gdb-index' command.
 
    Note that the .gdb_index file format used by this command is
@@ -1530,26 +1572,19 @@  write_dwarf_index (dwarf2_per_bfd *per_bfd, const char *dir,
    there.  */
 
 static void
-save_gdb_index_command (const char *arg, int from_tty)
+save_gdb_index_command (const char *args, int from_tty)
 {
-  const char dwarf5space[] = "-dwarf-5 ";
-  dw_index_kind index_kind = dw_index_kind::GDB_INDEX;
-
-  if (!arg)
-    arg = "";
-
-  arg = skip_spaces (arg);
-  if (strncmp (arg, dwarf5space, strlen (dwarf5space)) == 0)
-    {
-      index_kind = dw_index_kind::DEBUG_NAMES;
-      arg += strlen (dwarf5space);
-      arg = skip_spaces (arg);
-    }
+  save_gdb_index_options opts;
+  const auto group = make_gdb_save_index_options_def_group (&opts);
+  gdb::option::process_options
+    (&args, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, group);
 
-  if (!*arg)
+  if (args == nullptr || *args == '\0')
     error (_("usage: save gdb-index [-dwarf-5] DIRECTORY"));
 
-  std::string directory (gdb_tilde_expand (arg));
+  std::string directory (gdb_tilde_expand (args));
+  dw_index_kind index_kind
+    = (opts.dwarf_5 ? dw_index_kind::DEBUG_NAMES : dw_index_kind::GDB_INDEX);
 
   for (objfile *objfile : current_program_space->objfiles ())
     {
@@ -1675,5 +1710,5 @@  No options create one file with .gdb-index extension for pre-DWARF-5\n\
 compatible .gdb_index section.  With -dwarf-5 creates two files with\n\
 extension .debug_names and .debug_str for DWARF-5 .debug_names section."),
 	       &save_cmdlist);
-  set_cmd_completer (c, filename_completer);
+  set_cmd_completer_handle_brkchars (c, gdb_save_index_cmd_completer);
 }