[RFC,2/8] Use classes to represent MI Command instead of structures

Message ID 20190418152337.6376-3-jan.vrany@fit.cvut.cz
State New, archived
Headers

Commit Message

Jan Vrany April 18, 2019, 3:23 p.m. UTC
  From: Didier Nadeau <didier.nadeau@gmail.com>

This commit changes the infrastructure of mi-cmds.h and associated
files to use classes instead of structure to populate the hashmap
containing the commands.

The base class is virtual and there is one subclass MI commands
implemented with pure MI implementation and another for MI commands
implemented over a CLI command.

Logic for suppress_notification and parsing of ARGV/ARGC has been moved
to the classes implementation.

gdb/ChangeLog:
2017-02-03  Didier Nadeau  <didier.nadeau@gmail.com>

    * mi/mi-cmds.c (create_mi_cmd): Remove.
    (mi_command::mi_command): New function.
    (mi_command::do_suppress_notification): New function.
    (mi_command_mi::mi_command_mi): New function.
    (mi_command_mi::invoke): New function.
    (mi_command_cli::mi_command_cli): New function.
    (mi_command_cli::invoke): New function.
    (mi_cmd_lookup): Change return type.
    * mi/mi-cmds.h (struct mi_cli): Remove.
    (struct mi_cmd): Remove.
    (class mi_command): New class.
    (class mi_command_mi): New class.
    (class mi_command_cli): New class.
    (mi_cmd_loopkup): Change return type.
    * mi/mi-main.c (mi_execute_cli_command): Remove declaration.
    (mi_execute_command): Remove suppress_notification handling.
    (mi_cmd_execute): Remove call to argv_func.
    (mi_cmd_execute): Remove call to mi_execute_cli_command.
    (mi_cmd_execute): New call to mi_command::invoke.
    * mi/mi-main.h (mi_execute_cli_command): New declaration.
    * mi/mi-parse.c (mi_parse): Remove call to mi_parse_argv.
    * mi/mi-parse.h (struct mi_parse): Remove field struct mi_cmd.
    (struct mi_parse): New field class mi_command.
    (mi_parse_argv): New declaration.
---
 gdb/mi/mi-cmd-info.c |  2 +-
 gdb/mi/mi-cmds.c     | 97 +++++++++++++++++++++++++++++++-------------
 gdb/mi/mi-cmds.h     | 70 ++++++++++++++++++++++----------
 gdb/mi/mi-main.c     | 18 +-------
 gdb/mi/mi-main.h     |  1 +
 gdb/mi/mi-parse.c    | 18 ++------
 gdb/mi/mi-parse.h    |  6 ++-
 7 files changed, 128 insertions(+), 84 deletions(-)
  

Comments

Tom Tromey April 25, 2019, 7:25 p.m. UTC | #1
>>>>> "Jan" == Jan Vrany <jan.vrany@fit.cvut.cz> writes:

Jan> +std::unique_ptr<scoped_restore_tmpl<int>>
Jan> +mi_command::do_suppress_notification ()
Jan> +{
Jan> +  if (m_suppress_notification != NULL)
Jan> +    return std::unique_ptr<scoped_restore_tmpl <int>> (
Jan> +	new scoped_restore_tmpl <int> (m_suppress_notification, 1));
Jan> +
Jan> +  return std::unique_ptr<scoped_restore_tmpl <int>> ();

Rather than returning a unique_ptr, I think this could directly return a
scoped_restore_tmpl<int>.  Then, if m_suppress_notification is NULL,
just use the address of a dummy variable instead.

Jan> +void
Jan> +mi_command_cli::invoke (struct mi_parse *parse)
Jan> +{
Jan> +  std::unique_ptr<scoped_restore_tmpl <int>> restore
Jan> +    = do_suppress_notification ();

Alternatively, if all invoke methods have to start this way, make invoke
non-virtual and then have it do the setup and then call a virtual
do_invoke function.  Then invoke can use a gdb::optional<...>.

Tom
  
Simon Marchi May 3, 2019, 10:49 p.m. UTC | #2
On 2019-04-25 3:25 p.m., Tom Tromey wrote:
> Rather than returning a unique_ptr, I think this could directly return a
> scoped_restore_tmpl<int>.  Then, if m_suppress_notification is NULL,
> just use the address of a dummy variable instead.

Or a gdb::optional?

Simon
  
Simon Marchi May 3, 2019, 10:57 p.m. UTC | #3
On 2019-05-03 6:49 p.m., Simon Marchi wrote:
> On 2019-04-25 3:25 p.m., Tom Tromey wrote:
>> Rather than returning a unique_ptr, I think this could directly return a
>> scoped_restore_tmpl<int>.  Then, if m_suppress_notification is NULL,
>> just use the address of a dummy variable instead.
> 
> Or a gdb::optional?

Oh, just saw that Tom suggested that too:

> Alternatively, if all invoke methods have to start this way, make invoke
> non-virtual and then have it do the setup and then call a virtual
> do_invoke function.  Then invoke can use a gdb::optional<...>.

I think that's a good idea, but do_suppress_notification could return an
optional in any case.

Simon
  

Patch

diff --git a/gdb/mi/mi-cmd-info.c b/gdb/mi/mi-cmd-info.c
index 8645fac294..3d8bd68264 100644
--- a/gdb/mi/mi-cmd-info.c
+++ b/gdb/mi/mi-cmd-info.c
@@ -67,7 +67,7 @@  void
 mi_cmd_info_gdb_mi_command (const char *command, char **argv, int argc)
 {
   const char *cmd_name;
-  struct mi_cmd *cmd;
+  mi_command *cmd;
   struct ui_out *uiout = current_uiout;
 
   /* This command takes exactly one argument.  */
diff --git a/gdb/mi/mi-cmds.c b/gdb/mi/mi-cmds.c
index 2b3cf297cf..566d6c1885 100644
--- a/gdb/mi/mi-cmds.c
+++ b/gdb/mi/mi-cmds.c
@@ -22,6 +22,7 @@ 
 #include "top.h"
 #include "mi-cmds.h"
 #include "mi-main.h"
+#include "mi-parse.h"
 #include <map>
 #include <string>
 #include <memory>
@@ -37,9 +38,9 @@  static bool
 insert_mi_cmd_entry (mi_cmd_up command)
 {
   gdb_assert (command != NULL);
-  gdb_assert (command->name != NULL);
+  gdb_assert (! command->name ().empty ());
 
-  std::string name (command->name);
+  const std::string &name = command->name ();
 
   if (mi_cmd_table.find (name) != mi_cmd_table.end ())
     return false;
@@ -49,32 +50,16 @@  insert_mi_cmd_entry (mi_cmd_up command)
   return true;
 }
 
-/* Create an mi_cmd structure with name NAME.  */
-
-static mi_cmd_up
-create_mi_cmd (const char *name)
-{
-  mi_cmd_up cmd (new mi_cmd ());
-
-  cmd->name = name;
-
-  return cmd;
-}
-
 /* Create and register a new MI command with a pure MI implementation.  */
 
 static void
 add_mi_cmd_mi (const char *name, mi_cmd_argv_ftype function,
 	       int *suppress_notification = NULL)
 {
-  mi_cmd_up cmd_up = create_mi_cmd (name);
+  mi_command *micommand = new mi_command_mi (name, function,
+					     suppress_notification);
 
-  cmd_up->cli.cmd = NULL;
-  cmd_up->cli.args_p = 0;
-  cmd_up->argv_func = function;
-  cmd_up->suppress_notification = suppress_notification;
-
-  bool success = insert_mi_cmd_entry (std::move (cmd_up));
+  bool success = insert_mi_cmd_entry (std::move (mi_cmd_up (micommand)));
   gdb_assert (success);
 }
 
@@ -84,17 +69,70 @@  static void
 add_mi_cmd_cli (const char *name, const char *cli_name, int args_p,
 		int *suppress_notification = NULL)
 {
-  mi_cmd_up cmd_up = create_mi_cmd (name);
-
-  cmd_up->cli.cmd = cli_name;
-  cmd_up->cli.args_p = args_p;
-  cmd_up->argv_func = NULL;
-  cmd_up->suppress_notification = suppress_notification;
+  mi_command *micommand = new mi_command_cli (name, cli_name, args_p,
+					      suppress_notification);
 
-  bool success = insert_mi_cmd_entry (std::move (cmd_up));
+  bool success = insert_mi_cmd_entry (std::move (mi_cmd_up (micommand)));
   gdb_assert (success);
 }
 
+/* See mi-cmds.h  */
+mi_command::mi_command (const char *name, int *suppress_notification)
+  : m_name (name),
+    m_suppress_notification (suppress_notification)
+{}
+
+std::unique_ptr<scoped_restore_tmpl<int>>
+mi_command::do_suppress_notification ()
+{
+  if (m_suppress_notification != NULL)
+    return std::unique_ptr<scoped_restore_tmpl <int>> (
+	new scoped_restore_tmpl <int> (m_suppress_notification, 1));
+
+  return std::unique_ptr<scoped_restore_tmpl <int>> ();
+}
+
+mi_command_mi::mi_command_mi (const char *name, mi_cmd_argv_ftype func,
+			      int *suppress_notification)
+  : mi_command (name, suppress_notification),
+    m_argv_function (func)
+{
+  gdb_assert (func != NULL);
+}
+
+void
+mi_command_mi::invoke (struct mi_parse *parse)
+{
+  std::unique_ptr<scoped_restore_tmpl <int>> restore
+    = do_suppress_notification ();
+
+  mi_parse_argv (parse->args, parse);
+
+  if (parse->argv == NULL)
+    error (_("Problem parsing arguments: %s %s"), parse->command, parse->args);
+
+  this->m_argv_function (parse->command, parse->argv, parse->argc);
+}
+
+mi_command_cli::mi_command_cli (const char *name, const char *cli_name,
+				int args_p, int *suppress_notification)
+  : mi_command (name, suppress_notification),
+    m_cli_name (cli_name),
+    m_args_p (args_p)
+{}
+
+void
+mi_command_cli::invoke (struct mi_parse *parse)
+{
+  std::unique_ptr<scoped_restore_tmpl <int>> restore
+    = do_suppress_notification ();
+
+  mi_execute_cli_command (this->m_cli_name.c_str (), this->m_args_p,
+			  parse->args);
+}
+
+/* Initialize the available MI commands.  */
+
 static void
 build_table ()
 {
@@ -244,7 +282,7 @@  build_table ()
 
 /* See mi-cmds.h.  */
 
-struct mi_cmd *
+mi_command *
 mi_cmd_lookup (const char *command)
 {
   gdb_assert (command != NULL);
@@ -262,3 +300,4 @@  _initialize_mi_cmds (void)
 {
   build_table ();
 }
+
diff --git a/gdb/mi/mi-cmds.h b/gdb/mi/mi-cmds.h
index 97e1be819f..eae023c5da 100644
--- a/gdb/mi/mi-cmds.h
+++ b/gdb/mi/mi-cmds.h
@@ -126,38 +126,64 @@  extern mi_cmd_argv_ftype mi_cmd_enable_pretty_printing;
 extern mi_cmd_argv_ftype mi_cmd_enable_frame_filters;
 extern mi_cmd_argv_ftype mi_cmd_var_set_update_range;
 
-/* Description of a single command.  */
+/* mi_command base virtual class.  */
 
-struct mi_cli
+class mi_command
 {
-  /* Corresponding CLI command.  If ARGS_P is non-zero, the MI
-     command's argument list is appended to the CLI command.  */
-  const char *cmd;
-  int args_p;
+  public:
+    mi_command (const char *name, int *suppress_notification);
+    virtual ~mi_command () {};
+
+    const std::string &name ()
+    { return m_name; }
+
+    /* Execute the MI command.  */
+    virtual void invoke (struct mi_parse *parse) = 0;
+
+  protected:
+    std::unique_ptr<scoped_restore_tmpl<int>> do_suppress_notification ();
+
+  private:
+
+    /* The name of the command.  */
+    std::string m_name;
+
+    /* Pointer to integer to set during command's invocation.  */
+    int *m_suppress_notification;
 };
 
-struct mi_cmd
+/* MI command with a pure MI implementation.  */
+
+class mi_command_mi : public mi_command
+{
+  public:
+    mi_command_mi (const char *name, mi_cmd_argv_ftype func,
+		   int *suppress_notification);
+    void invoke (struct mi_parse *parse) override;
+
+  private:
+    mi_cmd_argv_ftype *m_argv_function;
+};
+
+/* MI command implemented on top of a CLI command.  */
+
+class mi_command_cli : public mi_command
 {
-  /* Official name of the command.  */
-  const char *name;
-  /* The corresponding CLI command that can be used to implement this
-     MI command (if cli.lhs is non NULL).  */
-  struct mi_cli cli;
-  /* If non-null, the function implementing the MI command.  */
-  mi_cmd_argv_ftype *argv_func;
-  /* If non-null, the pointer to a field in
-     'struct mi_suppress_notification', which will be set to true by MI
-     command processor (mi-main.c:mi_cmd_execute) when this command is
-     being executed.  It will be set back to false when command has been
-     executed.  */
-  int *suppress_notification;
+  public:
+    mi_command_cli (const char *name, const char *cli_name, int args_p,
+		  int *suppress_notification);
+    void invoke (struct mi_parse *parse) override;
+
+  private:
+    std::string m_cli_name;
+    int m_args_p;
 };
 
-typedef std::unique_ptr<struct mi_cmd> mi_cmd_up;
+typedef std::unique_ptr<mi_command> mi_cmd_up;
 
 /* Lookup a command in the MI command table.  */
 
-extern struct mi_cmd *mi_cmd_lookup (const char *command);
+extern mi_command *mi_cmd_lookup (const char *command);
 
 /* Debug flag */
 extern int mi_debug_p;
diff --git a/gdb/mi/mi-main.c b/gdb/mi/mi-main.c
index 17ca807471..481f09f799 100644
--- a/gdb/mi/mi-main.c
+++ b/gdb/mi/mi-main.c
@@ -90,9 +90,6 @@  int running_result_record_printed = 1;
 int mi_proceeded;
 
 static void mi_cmd_execute (struct mi_parse *parse);
-
-static void mi_execute_cli_command (const char *cmd, int args_p,
-				    const char *args);
 static void mi_execute_async_cli_command (const char *cli_command,
 					  char **argv, int argc);
 static bool register_changed_p (int regnum, readonly_detached_regcache *,
@@ -1954,9 +1951,6 @@  mi_execute_command (const char *cmd, int from_tty)
 
       gdb::optional<scoped_restore_tmpl<int>> restore_suppress;
 
-      if (command->cmd != NULL && command->cmd->suppress_notification != NULL)
-	restore_suppress.emplace (command->cmd->suppress_notification, 1);
-
       command->token = token;
 
       if (do_timings)
@@ -2095,17 +2089,9 @@  mi_cmd_execute (struct mi_parse *parse)
 
   current_context = parse;
 
-  if (parse->cmd->argv_func != NULL)
-    {
-      parse->cmd->argv_func (parse->command, parse->argv, parse->argc);
-    }
-  else if (parse->cmd->cli.cmd != 0)
+  if (parse->cmd != NULL)
     {
-      /* FIXME: DELETE THIS. */
-      /* The operation is still implemented by a cli command.  */
-      /* Must be a synchronous one.  */
-      mi_execute_cli_command (parse->cmd->cli.cmd, parse->cmd->cli.args_p,
-			      parse->args);
+      parse->cmd->invoke (parse);
     }
   else
     {
diff --git a/gdb/mi/mi-main.h b/gdb/mi/mi-main.h
index 72c4e59428..3733bc37ac 100644
--- a/gdb/mi/mi-main.h
+++ b/gdb/mi/mi-main.h
@@ -54,6 +54,7 @@  struct mi_suppress_notification
 };
 extern struct mi_suppress_notification mi_suppress_notification;
 
+void mi_execute_cli_command (const char *cmd, int args_p, const char *args);
 /* Implementation of -fix-multi-location-breakpoint-output.  */
 
 extern void mi_cmd_fix_multi_location_breakpoint_output (const char *command,
diff --git a/gdb/mi/mi-parse.c b/gdb/mi/mi-parse.c
index 6c8d922d66..7ed2b2bbc6 100644
--- a/gdb/mi/mi-parse.c
+++ b/gdb/mi/mi-parse.c
@@ -106,7 +106,7 @@  mi_parse_escape (const char **string_ptr)
   return c;
 }
 
-static void
+void
 mi_parse_argv (const char *args, struct mi_parse *parse)
 {
   const char *chp = args;
@@ -363,20 +363,8 @@  mi_parse (const char *cmd, char **token)
       chp = skip_spaces (chp);
     }
 
-  /* For new argv commands, attempt to return the parsed argument
-     list.  */
-  if (parse->cmd->argv_func != NULL)
-    {
-      mi_parse_argv (chp, parse.get ());
-      if (parse->argv == NULL)
-	error (_("Problem parsing arguments: %s %s"), parse->command, chp);
-    }
-
-  /* FIXME: DELETE THIS */
-  /* For CLI commands, also return the remainder of the
-     command line as a single string. */
-  if (parse->cmd->cli.cmd != NULL)
-    parse->args = xstrdup (chp);
+  /* Save the rest of the arguments for the command.  */
+  parse->args = xstrdup (chp);
 
   /* Fully parsed, flag as an MI command.  */
   parse->op = MI_COMMAND;
diff --git a/gdb/mi/mi-parse.h b/gdb/mi/mi-parse.h
index 2262ff56f9..94bd48219c 100644
--- a/gdb/mi/mi-parse.h
+++ b/gdb/mi/mi-parse.h
@@ -49,7 +49,7 @@  struct mi_parse
     enum mi_command_type op;
     char *command;
     char *token;
-    const struct mi_cmd *cmd;
+    mi_command *cmd;
     struct mi_timestamp *cmd_start;
     char *args;
     char **argv;
@@ -79,4 +79,8 @@  extern std::unique_ptr<struct mi_parse> mi_parse (const char *cmd,
 
 enum print_values mi_parse_print_values (const char *name);
 
+/* Split ARGS into argc/argv and store the result in PARSE.  */
+
+void mi_parse_argv (const char *args, struct mi_parse *parse);
+
 #endif /* MI_MI_PARSE_H */