Introduce and use make_unique_xstrdup
authorPedro Alves <palves@redhat.com>
Tue, 4 Jun 2019 21:40:54 +0000 (22:40 +0100)
committerPedro Alves <palves@redhat.com>
Tue, 4 Jun 2019 21:48:32 +0000 (22:48 +0100)
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:
2019-06-04  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:
2019-06-04  Pedro Alves  <palves@redhat.com>

* server.c (captured_main): Use make_unique_xstrdup.

23 files changed:
gdb/ChangeLog
gdb/ada-lang.c
gdb/breakpoint.c
gdb/cli/cli-dump.c
gdb/common/filestuff.c
gdb/common/gdb_tilde_expand.c
gdb/common/gdb_unique_ptr.h
gdb/common/pathstuff.c
gdb/compile/compile-cplus-types.c
gdb/completer.c
gdb/cp-support.c
gdb/fbsd-tdep.c
gdb/gdbserver/ChangeLog
gdb/gdbserver/server.c
gdb/guile/scm-safe-call.c
gdb/infcmd.c
gdb/linespec.c
gdb/mi/mi-main.c
gdb/psymtab.c
gdb/symfile.c
gdb/target.c
gdb/tui/tui-regs.c
gdb/value.c

index e36eae9ac16ee8d8296c007cf9bf5706331f09b8..79475684548384762b857ae134971b2a6c64abb5 100644 (file)
@@ -1,3 +1,30 @@
+2019-06-04  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.
+
 2019-06-04  Christian Biesinger  <cbiesinger@google.com>
 
        Add objfile property to gdb.Type.
index 99c099aa07d79292ab1c042e8d46e1c237050f04..22c51fbf082473932192596ff02defe0c67c437a 100644 (file)
@@ -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));
     }
 }
 
index 054bb1b13da485a5ca89cacdc4fd2a51e68d1101..ae3dcc0a9525afdab89f1ad92576618ddbbc39f4 100644 (file)
@@ -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;
index 8f0d8bfa8cb416ba567eba183187e1389f93975b..621bc9373ff76e7d9efef81c0e1b164430d2470a 100644 (file)
@@ -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;
index 1ca62482a7e27a5c9da4a63bba463c0a59d2ba3d..c7b8c694055138cea1c66c89154f5251a70d2d50 100644 (file)
@@ -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;
index fc338ff6ecbdfab9f86aaf711777bc8e75166eef..326df8f6aa9584a4f058e326401bdc773b72201b 100644 (file)
@@ -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]);
 }
index a4be2bb796387678939306235dc2d26afb10fbcf..67a5f265353ffe3b75c960ee141b1220cf8ef351 100644 (file)
@@ -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 */
index 2b1669a5b99e2db9cbb52a4085b0ab7b42e3e3f2..e0e048d66546acea292e63c000d321cbb3767195 100644 (file)
@@ -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>
index 692393085e21d1f0c3060cb6125a5c3c8ce0af33..d7aef265cf820800e6671f9ccef0712f7f4c162f 100644 (file)
@@ -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.  */
index 24c8446630746e2cc700042986bc6d0d65daf992..d4773f2a77b536e145b5c65e74a11b1ebaf6ff46 100644 (file)
@@ -798,9 +798,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] == ' ')
            {
@@ -1723,10 +1721,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));
     }
 }
 
@@ -1765,10 +1760,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));
        }
     }
 
@@ -1782,10 +1774,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));
        }
     }
 }
index 4afb79a4ea9d032b23e9a49e7f5ee2bae90f10cd..9ba6ee3ede537f2383f73ba9473905f9cfefc278 100644 (file)
@@ -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);
index cc7c2b7ab2f49946154d505b18ec073f030f5edf..e81c6c99bff254bf63cb0f4e6ccba9dec3947f54 100644 (file)
@@ -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;
index a769b15baf58d8ae674fecc062ebcc0468eecab9..bc554bd89c29830269d6a7fb337b482767fefb55 100644 (file)
@@ -1,3 +1,7 @@
+2019-06-04  Pedro Alves  <palves@redhat.com>
+
+       * server.c (captured_main): Use make_unique_xstrdup.
+
 2019-06-02  Tom Tromey  <tom@tromey.com>
 
        * gdbreplay.c (fromhex): Remove.
index fae5b2433cf0a43eba5cbb3736da2807d69828c5..5e93a58fd6bf23d1b3fe35d55409ef0a1deadb50 100644 (file)
@@ -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);
index 86e3bbc356bf8f7dd41643220bd8510e9de81992..14eace5f9cdb56db3f1a14566dec62522cdcd732 100644 (file)
@@ -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;
 }
 \f
index 1dfbe2329b7e8649c8c9f4cfdbe7662cb723babe..00b55c828ab83b94141507bfcfa46cf210c39843 100644 (file)
@@ -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
index f418e03b774531a8af0b0c16090f378b4fad3e81..f0afe1b4ca145d7d65c0fd0bacf03b00d9be7bb1 100644 (file)
@@ -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);
index 4921c13528ec305e0dd950f5def69a1193ef42c7..13c310d494c0f348531db91f1c9da73f71c67af0 100644 (file)
@@ -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, ","))
index 78a46997ca8397f6444e591aa9ca15b728a57239..6cc7566580abb78bec918e7a8f4cb25545b881e8 100644 (file)
@@ -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.
index af99da18f7affb8e386cad0894e6efa4302ffeb7..26762d289cae7df0dea1d02b78b549cc8e05648e 100644 (file)
@@ -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);
 
index 752e62b022f81a895cf8672173d580d2dc4127ce..1c04095fd63c329a4d92331154280d2adf0b4e7b 100644 (file)
@@ -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;
 
index 91b40f5d072d69a62a68c65f94bc9d5a008a16f6..e6aeedd923115d3c089044f635f5e6d098e5e7dd 100644 (file)
@@ -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));
     }
 }
 
index dad9f07b68eb030b0ec8136c0dda9bfe632b518c..71030efed072d69a50786812b35d573a0628e35e 100644 (file)
@@ -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.
This page took 0.047823 seconds and 4 git commands to generate.