[v2,5/9] Introduce "static constructor" for mi_parse

Message ID 20230404-dap-loaded-sources-v2-5-93f229095e03@adacore.com
State New
Headers
Series Implement the DAP "loadedSources" request |

Commit Message

Tom Tromey May 18, 2023, 8:18 p.m. UTC
  Change the mi_parse function to be a static method of mi_parse.  This
lets us remove the 'set_args' setter function.
---
 gdb/mi/mi-main.c  |  2 +-
 gdb/mi/mi-parse.c |  6 +++---
 gdb/mi/mi-parse.h | 28 +++++++++++++---------------
 3 files changed, 17 insertions(+), 19 deletions(-)
  

Comments

Andrew Burgess Aug. 11, 2023, 11:02 a.m. UTC | #1
Tom Tromey via Gdb-patches <gdb-patches@sourceware.org> writes:

> Change the mi_parse function to be a static method of mi_parse.  This
> lets us remove the 'set_args' setter function.

Hi Tom,

Sorry to respond to such an old thread, but I ran into the
mi_parse::make code today.  From your commit message I don't see the
connection for why we need a static mi_parse::make function in order to
get rid of the set_args method.

For reference, I switched the code back to using constructors (but
didn't add back set_args), and I'm not seeing any regressions in
gdb.mi/* and gdb.python/*.

Could you expand on why this change is needed?

Thanks,
Andrew


> ---
>  gdb/mi/mi-main.c  |  2 +-
>  gdb/mi/mi-parse.c |  6 +++---
>  gdb/mi/mi-parse.h | 28 +++++++++++++---------------
>  3 files changed, 17 insertions(+), 19 deletions(-)
>
> diff --git a/gdb/mi/mi-main.c b/gdb/mi/mi-main.c
> index aaebce40fac..b430115daea 100644
> --- a/gdb/mi/mi-main.c
> +++ b/gdb/mi/mi-main.c
> @@ -1919,7 +1919,7 @@ mi_execute_command (const char *cmd, int from_tty)
>  
>    try
>      {
> -      command = mi_parse (cmd, &token);
> +      command = mi_parse::make (cmd, &token);
>      }
>    catch (const gdb_exception &exception)
>      {
> diff --git a/gdb/mi/mi-parse.c b/gdb/mi/mi-parse.c
> index f077eb36a7c..b7c5a8cecdf 100644
> --- a/gdb/mi/mi-parse.c
> +++ b/gdb/mi/mi-parse.c
> @@ -109,7 +109,7 @@ mi_parse_escape (const char **string_ptr)
>  void
>  mi_parse::parse_argv ()
>  {
> -  const char *chp = m_args.get ();
> +  const char *chp = m_args.c_str ();
>    int argc = 0;
>    char **argv = XNEWVEC (char *, argc + 1);
>  
> @@ -216,7 +216,7 @@ mi_parse::~mi_parse ()
>  }
>  
>  std::unique_ptr<struct mi_parse>
> -mi_parse (const char *cmd, char **token)
> +mi_parse::make (const char *cmd, char **token)
>  {
>    const char *chp;
>  
> @@ -345,7 +345,7 @@ mi_parse (const char *cmd, char **token)
>      }
>  
>    /* Save the rest of the arguments for the command.  */
> -  parse->set_args (chp);
> +  parse->m_args = 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 edb61547354..6f1da6e6eb5 100644
> --- a/gdb/mi/mi-parse.h
> +++ b/gdb/mi/mi-parse.h
> @@ -41,7 +41,17 @@ enum mi_command_type
>  
>  struct mi_parse
>    {
> -    mi_parse () = default;
> +    /* Attempts to parse CMD returning a ``struct mi_parse''.  If CMD is
> +       invalid, an exception is thrown.  For an MI_COMMAND COMMAND, ARGS
> +       and OP are initialized.  Un-initialized fields are zero.  *TOKEN is
> +       set to the token, even if an exception is thrown.  It is allocated
> +       with xmalloc; it must either be freed with xfree, or assigned to
> +       the TOKEN field of the resultant mi_parse object, to be freed by
> +       mi_parse_free.  */
> +
> +    static std::unique_ptr<struct mi_parse> make (const char *cmd,
> +						  char **token);
> +
>      ~mi_parse ();
>  
>      DISABLE_COPY_AND_ASSIGN (mi_parse);
> @@ -54,9 +64,6 @@ struct mi_parse
>      const char *args () const
>      { return m_args.c_str (); }
>  
> -    void set_args (const char *args)
> -    { m_args = args; }
> -
>      enum mi_command_type op = MI_COMMAND;
>      char *command = nullptr;
>      char *token = nullptr;
> @@ -75,20 +82,11 @@ struct mi_parse
>  
>    private:
>  
> +    mi_parse () = default;
> +
>      std::string m_args;
>    };
>  
> -/* Attempts to parse CMD returning a ``struct mi_parse''.  If CMD is
> -   invalid, an exception is thrown.  For an MI_COMMAND COMMAND, ARGS
> -   and OP are initialized.  Un-initialized fields are zero.  *TOKEN is
> -   set to the token, even if an exception is thrown.  It is allocated
> -   with xmalloc; it must either be freed with xfree, or assigned to
> -   the TOKEN field of the resultant mi_parse object, to be freed by
> -   mi_parse_free.  */
> -
> -extern std::unique_ptr<struct mi_parse> mi_parse (const char *cmd,
> -						  char **token);
> -
>  /* Parse a string argument into a print_values value.  */
>  
>  enum print_values mi_parse_print_values (const char *name);
>
> -- 
> 2.40.0
  
Tom Tromey Aug. 11, 2023, 1:10 p.m. UTC | #2
>>>>> "Andrew" == Andrew Burgess <aburgess@redhat.com> writes:

Andrew> Sorry to respond to such an old thread, but I ran into the
Andrew> mi_parse::make code today.  From your commit message I don't see the
Andrew> connection for why we need a static mi_parse::make function in order to
Andrew> get rid of the set_args method.

Andrew> For reference, I switched the code back to using constructors (but
Andrew> didn't add back set_args), and I'm not seeing any regressions in
Andrew> gdb.mi/* and gdb.python/*.

Andrew> Could you expand on why this change is needed?

I don't recall what exactly I was thinking when I wrote it.  It might
have been a leftover from some other attempt to use unique_ptr to ensure
there isn't a possibility of a leak.

I think it's fine to switch it to constructors if that is convenient for
you.

Tom
  
Andrew Burgess Aug. 11, 2023, 3:06 p.m. UTC | #3
Tom Tromey <tromey@adacore.com> writes:

>>>>>> "Andrew" == Andrew Burgess <aburgess@redhat.com> writes:
>
> Andrew> Sorry to respond to such an old thread, but I ran into the
> Andrew> mi_parse::make code today.  From your commit message I don't see the
> Andrew> connection for why we need a static mi_parse::make function in order to
> Andrew> get rid of the set_args method.
>
> Andrew> For reference, I switched the code back to using constructors (but
> Andrew> didn't add back set_args), and I'm not seeing any regressions in
> Andrew> gdb.mi/* and gdb.python/*.
>
> Andrew> Could you expand on why this change is needed?
>
> I don't recall what exactly I was thinking when I wrote it.  It might
> have been a leftover from some other attempt to use unique_ptr to ensure
> there isn't a possibility of a leak.
>
> I think it's fine to switch it to constructors if that is convenient for
> you.

Thanks.  I'll CC you when I post the patch.  Just thought I'd ask first,
didn't want to look like I was just trying to revert your work :)

Thanks,
Andrew
  

Patch

diff --git a/gdb/mi/mi-main.c b/gdb/mi/mi-main.c
index aaebce40fac..b430115daea 100644
--- a/gdb/mi/mi-main.c
+++ b/gdb/mi/mi-main.c
@@ -1919,7 +1919,7 @@  mi_execute_command (const char *cmd, int from_tty)
 
   try
     {
-      command = mi_parse (cmd, &token);
+      command = mi_parse::make (cmd, &token);
     }
   catch (const gdb_exception &exception)
     {
diff --git a/gdb/mi/mi-parse.c b/gdb/mi/mi-parse.c
index f077eb36a7c..b7c5a8cecdf 100644
--- a/gdb/mi/mi-parse.c
+++ b/gdb/mi/mi-parse.c
@@ -109,7 +109,7 @@  mi_parse_escape (const char **string_ptr)
 void
 mi_parse::parse_argv ()
 {
-  const char *chp = m_args.get ();
+  const char *chp = m_args.c_str ();
   int argc = 0;
   char **argv = XNEWVEC (char *, argc + 1);
 
@@ -216,7 +216,7 @@  mi_parse::~mi_parse ()
 }
 
 std::unique_ptr<struct mi_parse>
-mi_parse (const char *cmd, char **token)
+mi_parse::make (const char *cmd, char **token)
 {
   const char *chp;
 
@@ -345,7 +345,7 @@  mi_parse (const char *cmd, char **token)
     }
 
   /* Save the rest of the arguments for the command.  */
-  parse->set_args (chp);
+  parse->m_args = 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 edb61547354..6f1da6e6eb5 100644
--- a/gdb/mi/mi-parse.h
+++ b/gdb/mi/mi-parse.h
@@ -41,7 +41,17 @@  enum mi_command_type
 
 struct mi_parse
   {
-    mi_parse () = default;
+    /* Attempts to parse CMD returning a ``struct mi_parse''.  If CMD is
+       invalid, an exception is thrown.  For an MI_COMMAND COMMAND, ARGS
+       and OP are initialized.  Un-initialized fields are zero.  *TOKEN is
+       set to the token, even if an exception is thrown.  It is allocated
+       with xmalloc; it must either be freed with xfree, or assigned to
+       the TOKEN field of the resultant mi_parse object, to be freed by
+       mi_parse_free.  */
+
+    static std::unique_ptr<struct mi_parse> make (const char *cmd,
+						  char **token);
+
     ~mi_parse ();
 
     DISABLE_COPY_AND_ASSIGN (mi_parse);
@@ -54,9 +64,6 @@  struct mi_parse
     const char *args () const
     { return m_args.c_str (); }
 
-    void set_args (const char *args)
-    { m_args = args; }
-
     enum mi_command_type op = MI_COMMAND;
     char *command = nullptr;
     char *token = nullptr;
@@ -75,20 +82,11 @@  struct mi_parse
 
   private:
 
+    mi_parse () = default;
+
     std::string m_args;
   };
 
-/* Attempts to parse CMD returning a ``struct mi_parse''.  If CMD is
-   invalid, an exception is thrown.  For an MI_COMMAND COMMAND, ARGS
-   and OP are initialized.  Un-initialized fields are zero.  *TOKEN is
-   set to the token, even if an exception is thrown.  It is allocated
-   with xmalloc; it must either be freed with xfree, or assigned to
-   the TOKEN field of the resultant mi_parse object, to be freed by
-   mi_parse_free.  */
-
-extern std::unique_ptr<struct mi_parse> mi_parse (const char *cmd,
-						  char **token);
-
 /* Parse a string argument into a print_values value.  */
 
 enum print_values mi_parse_print_values (const char *name);