This is the mail archive of the gdb-patches@sourceware.org mailing list for the GDB project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH] Introduce and use make_unique_xstrdup (was: Re: [PATCH v2 12/24] Introduce generic command options framework)


On 6/3/19 8:16 PM, Tom Tromey wrote:

> Pedro> +/* Dup STR and return a unique_xmalloc_ptr for the result.  */
> Pedro> +
> Pedro> +static gdb::unique_xmalloc_ptr<char>
> Pedro> +make_unique_xstrdup (const char *str)
> Pedro> +{
> Pedro> +  return gdb::unique_xmalloc_ptr<char> (xstrdup (str));
> Pedro> +}
> 
> I could 25 spots that could use this, so how about putting it into a
> header somewhere?  I am happy to convert all the existing uses.

Yeah.  I added a bunch of those spots myself, and been meaning to do
this.

How about this?

>From 366c62a676e47c7303622c811c646ea1b24eae31 Mon Sep 17 00:00:00 2001
From: Pedro Alves <palves@redhat.com>
Date: Tue, 4 Jun 2019 18:38:55 +0100
Subject: [PATCH] Introduce and use make_unique_xstrdup

Adds an utility function to make it shorter to write the common case
of wrapping an xstrdup with a unique_xmalloc_ptr, and uses it
throughout.

Note: I tried to put this in common/common-utils.h near skip_spaces,
etc. but that is included in common/common-defs.h before
common/gdb_unique_ptr.h is included, so it would fail to compile
because gdb::unique_xmalloc_ptr isn't defined at that point yet.  I
tried moving the gdb_unique_ptr.h inclusion before common-utils.h, but
that doesn't work because gdb_unique_ptr.h depends on common-utils.h
for xfree.

gdb/ChangeLog:
yyyy-mm-dd  Pedro Alves  <palves@redhat.com>

	* common/gdb_unique_ptr.h (make_unique_xstrdup): New.

	* ada-lang.c (catch_ada_completer): Use make_unique_xstrdup.
	* breakpoint.c (condition_completer): Likewise.
	* cli/cli-dump.c (scan_expression): Likewise.
	* common/filestuff.c (mkdir_recursive): Likewise.
	* common/gdb_tilde_expand.c (gdb_tilde_expand_up)
	* common/pathstuff.c (gdb_realpath, gdb_realpath_keepfile)
	(gdb_abspath): Likewise.
	* compile/compile-cplus-types.c
	(compile_cplus_instance::decl_name): Likewise.
	* completer.c (complete_explicit_location):
	(signal_completer, reg_or_group_completer_1): Likewise.
	* cp-support.c (cp_remove_params_if_any): Likewise.
	* fbsd-tdep.c (fbsd_core_vnode_path): Likewise.
	* guile/scm-safe-call.c (gdbscm_safe_eval_string): Likewise.
	* infcmd.c (strip_bg_char): Likewise.
	* linespec.c (copy_token_string): Likewise.
	* mi/mi-main.c (output_cores): Likewise.
	* psymtab.c (psymtab_search_name):
	* symfile.c (test_set_ext_lang_command): Likewise.
	* target.c (target_fileio_read_stralloc): Likewise.
	* tui/tui-regs.c (tui_reggroup_completer): Likewise.
	* value.c (complete_internalvar): Likewise.

gdb/gdbserver/ChangeLog:
yyyy-mm-dd  Pedro Alves  <palves@redhat.com>

	* server.c (captured_main): Use make_unique_xstrdup.
---
 gdb/ada-lang.c                    |  3 +--
 gdb/breakpoint.c                  |  5 +----
 gdb/cli/cli-dump.c                |  2 +-
 gdb/common/filestuff.c            |  2 +-
 gdb/common/gdb_tilde_expand.c     |  2 +-
 gdb/common/gdb_unique_ptr.h       |  8 ++++++++
 gdb/common/pathstuff.c            |  8 ++++----
 gdb/compile/compile-cplus-types.c |  2 +-
 gdb/completer.c                   | 19 ++++---------------
 gdb/cp-support.c                  |  2 +-
 gdb/fbsd-tdep.c                   |  2 +-
 gdb/gdbserver/server.c            |  2 +-
 gdb/guile/scm-safe-call.c         |  2 +-
 gdb/infcmd.c                      |  2 +-
 gdb/linespec.c                    |  2 +-
 gdb/mi/mi-main.c                  |  2 +-
 gdb/psymtab.c                     |  2 +-
 gdb/symfile.c                     |  2 +-
 gdb/target.c                      |  2 +-
 gdb/tui/tui-regs.c                |  2 +-
 gdb/value.c                       |  6 +-----
 21 files changed, 34 insertions(+), 45 deletions(-)

diff --git a/gdb/ada-lang.c b/gdb/ada-lang.c
index 99c099aa07d..22c51fbf082 100644
--- a/gdb/ada-lang.c
+++ b/gdb/ada-lang.c
@@ -13195,8 +13195,7 @@ catch_ada_completer (struct cmd_list_element *cmd, completion_tracker &tracker,
   for (const ada_exc_info &info : exceptions)
     {
       if (startswith (info.name, word))
-	tracker.add_completion
-	  (gdb::unique_xmalloc_ptr<char> (xstrdup (info.name)));
+	tracker.add_completion (make_unique_xstrdup (info.name));
     }
 }
 
diff --git a/gdb/breakpoint.c b/gdb/breakpoint.c
index b85f7eedac8..08083645e1e 100644
--- a/gdb/breakpoint.c
+++ b/gdb/breakpoint.c
@@ -941,10 +941,7 @@ condition_completer (struct cmd_list_element *cmd,
 	  xsnprintf (number, sizeof (number), "%d", b->number);
 
 	  if (strncmp (number, text, len) == 0)
-	    {
-	      gdb::unique_xmalloc_ptr<char> copy (xstrdup (number));
-	      tracker.add_completion (std::move (copy));
-	    }
+	    tracker.add_completion (make_unique_xstrdup (number));
 	}
 
       return;
diff --git a/gdb/cli/cli-dump.c b/gdb/cli/cli-dump.c
index 8f0d8bfa8cb..621bc9373ff 100644
--- a/gdb/cli/cli-dump.c
+++ b/gdb/cli/cli-dump.c
@@ -37,7 +37,7 @@ static gdb::unique_xmalloc_ptr<char>
 scan_expression (const char **cmd, const char *def)
 {
   if ((*cmd) == NULL || (**cmd) == '\0')
-    return gdb::unique_xmalloc_ptr<char> (xstrdup (def));
+    return make_unique_xstrdup (def);
   else
     {
       char *exp;
diff --git a/gdb/common/filestuff.c b/gdb/common/filestuff.c
index 1ca62482a7e..c7b8c694055 100644
--- a/gdb/common/filestuff.c
+++ b/gdb/common/filestuff.c
@@ -462,7 +462,7 @@ is_regular_file (const char *name, int *errno_ptr)
 bool
 mkdir_recursive (const char *dir)
 {
-  gdb::unique_xmalloc_ptr<char> holder (xstrdup (dir));
+  auto holder = make_unique_xstrdup (dir);
   char * const start = holder.get ();
   char *component_start = start;
   char *component_end = start;
diff --git a/gdb/common/gdb_tilde_expand.c b/gdb/common/gdb_tilde_expand.c
index fc338ff6ecb..326df8f6aa9 100644
--- a/gdb/common/gdb_tilde_expand.c
+++ b/gdb/common/gdb_tilde_expand.c
@@ -91,5 +91,5 @@ gdb_tilde_expand_up (const char *dir)
   gdb_assert (glob.pathc () > 0);
   /* "glob" may return more than one match to the path provided by the
      user, but we are only interested in the first match.  */
-  return gdb::unique_xmalloc_ptr<char> (xstrdup (glob.pathv ()[0]));
+  return make_unique_xstrdup (glob.pathv ()[0]);
 }
diff --git a/gdb/common/gdb_unique_ptr.h b/gdb/common/gdb_unique_ptr.h
index a4be2bb7963..67a5f265353 100644
--- a/gdb/common/gdb_unique_ptr.h
+++ b/gdb/common/gdb_unique_ptr.h
@@ -56,4 +56,12 @@ struct noop_deleter
 
 } /* namespace gdb */
 
+/* Dup STR and return a unique_xmalloc_ptr for the result.  */
+
+static inline gdb::unique_xmalloc_ptr<char>
+make_unique_xstrdup (const char *str)
+{
+  return gdb::unique_xmalloc_ptr<char> (xstrdup (str));
+}
+
 #endif /* COMMON_GDB_UNIQUE_PTR_H */
diff --git a/gdb/common/pathstuff.c b/gdb/common/pathstuff.c
index 2b1669a5b99..e0e048d6654 100644
--- a/gdb/common/pathstuff.c
+++ b/gdb/common/pathstuff.c
@@ -65,7 +65,7 @@ gdb_realpath (const char *filename)
        we might not be able to display the original casing in a given
        path.  */
     if (len > 0 && len < MAX_PATH)
-      return gdb::unique_xmalloc_ptr<char> (xstrdup (buf));
+      return make_unique_xstrdup (buf);
   }
 #else
   {
@@ -77,7 +77,7 @@ gdb_realpath (const char *filename)
 #endif
 
   /* This system is a lost cause, just dup the buffer.  */
-  return gdb::unique_xmalloc_ptr<char> (xstrdup (filename));
+  return make_unique_xstrdup (filename);
 }
 
 /* See common/pathstuff.h.  */
@@ -92,7 +92,7 @@ gdb_realpath_keepfile (const char *filename)
   /* Extract the basename of filename, and return immediately
      a copy of filename if it does not contain any directory prefix.  */
   if (base_name == filename)
-    return gdb::unique_xmalloc_ptr<char> (xstrdup (filename));
+    return make_unique_xstrdup (filename);
 
   dir_name = (char *) alloca ((size_t) (base_name - filename + 2));
   /* Allocate enough space to store the dir_name + plus one extra
@@ -135,7 +135,7 @@ gdb_abspath (const char *path)
     return gdb_tilde_expand_up (path);
 
   if (IS_ABSOLUTE_PATH (path))
-    return gdb::unique_xmalloc_ptr<char> (xstrdup (path));
+    return make_unique_xstrdup (path);
 
   /* Beware the // my son, the Emacs barfs, the botch that catch...  */
   return gdb::unique_xmalloc_ptr<char>
diff --git a/gdb/compile/compile-cplus-types.c b/gdb/compile/compile-cplus-types.c
index 692393085e2..d7aef265cf8 100644
--- a/gdb/compile/compile-cplus-types.c
+++ b/gdb/compile/compile-cplus-types.c
@@ -65,7 +65,7 @@ compile_cplus_instance::decl_name (const char *natural)
   if (name != nullptr)
     return name;
 
-  return gdb::unique_xmalloc_ptr<char> (xstrdup (natural));
+  return make_unique_xstrdup (natural);
 }
 
 /* Get the access flag for the NUM'th field of TYPE.  */
diff --git a/gdb/completer.c b/gdb/completer.c
index b2768ede769..5dd9a99f2a2 100644
--- a/gdb/completer.c
+++ b/gdb/completer.c
@@ -805,9 +805,7 @@ complete_explicit_location (completion_tracker &tracker,
 		   before: "b -function 'not_loaded_function_yet()'"
 		   after:  "b -function 'not_loaded_function_yet()' "
 	      */
-	      gdb::unique_xmalloc_ptr<char> text_copy
-		(xstrdup (text));
-	      tracker.add_completion (std::move (text_copy));
+	      tracker.add_completion (make_unique_xstrdup (text));
 	    }
 	  else if (quoted_arg_end[1] == ' ')
 	    {
@@ -1733,10 +1731,7 @@ signal_completer (struct cmd_list_element *ignore,
 	continue;
 
       if (strncasecmp (signame, word, len) == 0)
-	{
-	  gdb::unique_xmalloc_ptr<char> copy (xstrdup (signame));
-	  tracker.add_completion (std::move (copy));
-	}
+	tracker.add_completion (make_unique_xstrdup (signame));
     }
 }
 
@@ -1775,10 +1770,7 @@ reg_or_group_completer_1 (completion_tracker &tracker,
 	   i++)
 	{
 	  if (*name != '\0' && strncmp (word, name, len) == 0)
-	    {
-	      gdb::unique_xmalloc_ptr<char> copy (xstrdup (name));
-	      tracker.add_completion (std::move (copy));
-	    }
+	    tracker.add_completion (make_unique_xstrdup (name));
 	}
     }
 
@@ -1792,10 +1784,7 @@ reg_or_group_completer_1 (completion_tracker &tracker,
 	{
 	  name = reggroup_name (group);
 	  if (strncmp (word, name, len) == 0)
-	    {
-	      gdb::unique_xmalloc_ptr<char> copy (xstrdup (name));
-	      tracker.add_completion (std::move (copy));
-	    }
+	    tracker.add_completion (make_unique_xstrdup (name));
 	}
     }
 }
diff --git a/gdb/cp-support.c b/gdb/cp-support.c
index 4afb79a4ea9..9ba6ee3ede5 100644
--- a/gdb/cp-support.c
+++ b/gdb/cp-support.c
@@ -908,7 +908,7 @@ cp_remove_params_if_any (const char *demangled_name, bool completion_mode)
      we're completing / matching everything, avoid returning NULL
      which would make callers interpret the result as an error.  */
   if (demangled_name[0] == '\0' && completion_mode)
-    return gdb::unique_xmalloc_ptr<char> (xstrdup (""));
+    return make_unique_xstrdup ("");
 
   gdb::unique_xmalloc_ptr<char> without_params
     = cp_remove_params_1 (demangled_name, false);
diff --git a/gdb/fbsd-tdep.c b/gdb/fbsd-tdep.c
index cc7c2b7ab2f..e81c6c99bff 100644
--- a/gdb/fbsd-tdep.c
+++ b/gdb/fbsd-tdep.c
@@ -1266,7 +1266,7 @@ fbsd_core_vnode_path (struct gdbarch *gdbarch, int fd)
 	  && bfd_get_signed_32 (core_bfd, descdata + KF_FD) == fd)
 	{
 	  char *path = (char *) descdata + KF_PATH;
-	  return gdb::unique_xmalloc_ptr<char> (xstrdup (path));
+	  return make_unique_xstrdup (path);
 	}
 
       descdata += structsize;
diff --git a/gdb/gdbserver/server.c b/gdb/gdbserver/server.c
index fae5b2433cf..5e93a58fd6b 100644
--- a/gdb/gdbserver/server.c
+++ b/gdb/gdbserver/server.c
@@ -3802,7 +3802,7 @@ captured_main (int argc, char *argv[])
       int i, n;
 
       n = argc - (next_arg - argv);
-      program_path.set (gdb::unique_xmalloc_ptr<char> (xstrdup (next_arg[0])));
+      program_path.set (make_unique_xstrdup (next_arg[0]));
       for (i = 1; i < n; i++)
 	program_args.push_back (xstrdup (next_arg[i]));
       program_args.push_back (NULL);
diff --git a/gdb/guile/scm-safe-call.c b/gdb/guile/scm-safe-call.c
index 86e3bbc356b..14eace5f9cd 100644
--- a/gdb/guile/scm-safe-call.c
+++ b/gdb/guile/scm-safe-call.c
@@ -404,7 +404,7 @@ gdbscm_safe_eval_string (const char *string, int display_result)
   result = gdbscm_with_guile (scscm_eval_scheme_string, (void *) &data);
 
   if (result != NULL)
-    return gdb::unique_xmalloc_ptr<char> (xstrdup (result));
+    return make_unique_xstrdup (result);
   return NULL;
 }
 
diff --git a/gdb/infcmd.c b/gdb/infcmd.c
index 1dfbe2329b7..00b55c828ab 100644
--- a/gdb/infcmd.c
+++ b/gdb/infcmd.c
@@ -421,7 +421,7 @@ strip_bg_char (const char *args, int *bg_char_p)
     }
 
   *bg_char_p = 0;
-  return gdb::unique_xmalloc_ptr<char> (xstrdup (args));
+  return make_unique_xstrdup (args);
 }
 
 /* Common actions to take after creating any sort of inferior, by any
diff --git a/gdb/linespec.c b/gdb/linespec.c
index f418e03b774..f0afe1b4ca1 100644
--- a/gdb/linespec.c
+++ b/gdb/linespec.c
@@ -555,7 +555,7 @@ copy_token_string (linespec_token token)
   const char *str, *s;
 
   if (token.type == LSTOKEN_KEYWORD)
-    return gdb::unique_xmalloc_ptr<char> (xstrdup (LS_TOKEN_KEYWORD (token)));
+    return make_unique_xstrdup (LS_TOKEN_KEYWORD (token));
 
   str = LS_TOKEN_STOKEN (token).ptr;
   s = remove_trailing_whitespace (str, str + LS_TOKEN_STOKEN (token).length);
diff --git a/gdb/mi/mi-main.c b/gdb/mi/mi-main.c
index 4921c13528e..13c310d494c 100644
--- a/gdb/mi/mi-main.c
+++ b/gdb/mi/mi-main.c
@@ -695,7 +695,7 @@ static void
 output_cores (struct ui_out *uiout, const char *field_name, const char *xcores)
 {
   ui_out_emit_list list_emitter (uiout, field_name);
-  gdb::unique_xmalloc_ptr<char> cores (xstrdup (xcores));
+  auto cores = make_unique_xstrdup (xcores);
   char *p = cores.get ();
 
   for (p = strtok (p, ","); p;  p = strtok (NULL, ","))
diff --git a/gdb/psymtab.c b/gdb/psymtab.c
index 78a46997ca8..6cc7566580a 100644
--- a/gdb/psymtab.c
+++ b/gdb/psymtab.c
@@ -647,7 +647,7 @@ psymtab_search_name (const char *name)
       break;
     }
 
-  return gdb::unique_xmalloc_ptr<char> (xstrdup (name));
+  return make_unique_xstrdup (name);
 }
 
 /* Look, in partial_symtab PST, for symbol whose natural name is NAME.
diff --git a/gdb/symfile.c b/gdb/symfile.c
index af99da18f7a..26762d289ca 100644
--- a/gdb/symfile.c
+++ b/gdb/symfile.c
@@ -3877,7 +3877,7 @@ test_set_ext_lang_command ()
   SELF_CHECK (lang == language_unknown);
 
   /* Test adding a new extension using the CLI command.  */
-  gdb::unique_xmalloc_ptr<char> args_holder (xstrdup (".hello rust"));
+  auto args_holder = make_unique_xstrdup (".hello rust");
   ext_args = args_holder.get ();
   set_ext_lang_command (NULL, 1, NULL);
 
diff --git a/gdb/target.c b/gdb/target.c
index 752e62b022f..1c04095fd63 100644
--- a/gdb/target.c
+++ b/gdb/target.c
@@ -3106,7 +3106,7 @@ target_fileio_read_stralloc (struct inferior *inf, const char *filename)
     return gdb::unique_xmalloc_ptr<char> (nullptr);
 
   if (transferred == 0)
-    return gdb::unique_xmalloc_ptr<char> (xstrdup (""));
+    return make_unique_xstrdup ("");
 
   bufstr[transferred] = 0;
 
diff --git a/gdb/tui/tui-regs.c b/gdb/tui/tui-regs.c
index 91b40f5d072..e6aeedd9231 100644
--- a/gdb/tui/tui-regs.c
+++ b/gdb/tui/tui-regs.c
@@ -676,7 +676,7 @@ tui_reggroup_completer (struct cmd_list_element *ignore,
   for (tmp = extra; *tmp != NULL; ++tmp)
     {
       if (strncmp (word, *tmp, len) == 0)
-	tracker.add_completion (gdb::unique_xmalloc_ptr<char> (xstrdup (*tmp)));
+	tracker.add_completion (make_unique_xstrdup (*tmp));
     }
 }
 
diff --git a/gdb/value.c b/gdb/value.c
index dad9f07b68e..71030efed07 100644
--- a/gdb/value.c
+++ b/gdb/value.c
@@ -2014,11 +2014,7 @@ complete_internalvar (completion_tracker &tracker, const char *name)
 
   for (var = internalvars; var; var = var->next)
     if (strncmp (var->name, name, len) == 0)
-      {
-	gdb::unique_xmalloc_ptr<char> copy (xstrdup (var->name));
-
-	tracker.add_completion (std::move (copy));
-      }
+      tracker.add_completion (make_unique_xstrdup (var->name));
 }
 
 /* Create an internal variable with name NAME and with a void value.
-- 
2.14.5


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]