Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Tom Tromey <tom@tromey.com>
To: gdb-patches@sourceware.org
Cc: Tom Tromey <tom@tromey.com>
Subject: [RFA 1/5] Change gdb_abspath to return a unique_xmalloc_ptr
Date: Fri, 18 Aug 2017 20:01:00 -0000	[thread overview]
Message-ID: <20170818200024.4948-2-tom@tromey.com> (raw)
In-Reply-To: <20170818200024.4948-1-tom@tromey.com>

This changes gdb_abspath to return a unique_xmalloc_ptr, and fixes up
the callers.  This allows the removal of a cleanup, and also puts
ownership rules into the API, where they belong.

ChangeLog
2017-08-18  Tom Tromey  <tom@tromey.com>

	* compile/compile.c (compile_file_command): Use
	gdb::unique_xmalloc_ptr, std::string.
	* utils.c (gdb_abspath): Change return type.
	* source.c (openp): Update.
	* objfiles.c (allocate_objfile): Update.
	* main.c (set_gdb_data_directory): Update.
	* utils.h (gdb_abspath): Return a gdb::unique_xmalloc_ptr.
---
 gdb/ChangeLog         | 10 ++++++++++
 gdb/compile/compile.c | 11 +++--------
 gdb/main.c            |  4 ++--
 gdb/objfiles.c        | 11 +++++++----
 gdb/source.c          |  2 +-
 gdb/utils.c           | 21 +++++++++------------
 gdb/utils.h           |  2 +-
 7 files changed, 33 insertions(+), 28 deletions(-)

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index 7ad3f4c..e75d1b7 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,4 +1,14 @@
 2017-08-18  Tom Tromey  <tom@tromey.com>
+
+	* compile/compile.c (compile_file_command): Use
+	gdb::unique_xmalloc_ptr, std::string.
+	* utils.c (gdb_abspath): Change return type.
+	* source.c (openp): Update.
+	* objfiles.c (allocate_objfile): Update.
+	* main.c (set_gdb_data_directory): Update.
+	* utils.h (gdb_abspath): Return a gdb::unique_xmalloc_ptr.
+
+2017-08-18  Tom Tromey  <tom@tromey.com>
 	    Pedro Alves  <palves@redhat.com>
 
 	* spu-multiarch.c (parse_spufs_run): Use scoped_restore.
diff --git a/gdb/compile/compile.c b/gdb/compile/compile.c
index bca7b57..91e084f 100644
--- a/gdb/compile/compile.c
+++ b/gdb/compile/compile.c
@@ -90,8 +90,6 @@ static void
 compile_file_command (char *arg, int from_tty)
 {
   enum compile_i_scope_types scope = COMPILE_I_SIMPLE_SCOPE;
-  char *buffer;
-  struct cleanup *cleanup;
 
   scoped_restore save_async = make_scoped_restore (&current_ui->async, 0);
 
@@ -115,12 +113,9 @@ compile_file_command (char *arg, int from_tty)
     error (_("Unknown argument specified."));
 
   arg = skip_spaces (arg);
-  arg = gdb_abspath (arg);
-  cleanup = make_cleanup (xfree, arg);
-  buffer = xstrprintf ("#include \"%s\"\n", arg);
-  make_cleanup (xfree, buffer);
-  eval_compile_command (NULL, buffer, scope, NULL);
-  do_cleanups (cleanup);
+  gdb::unique_xmalloc_ptr<char> abspath = gdb_abspath (arg);
+  std::string buffer = string_printf ("#include \"%s\"\n", abspath.get ());
+  eval_compile_command (NULL, buffer.c_str (), scope, NULL);
 }
 
 /* Handle the input from the 'compile code' command.  The
diff --git a/gdb/main.c b/gdb/main.c
index 9813041..886e17f 100644
--- a/gdb/main.c
+++ b/gdb/main.c
@@ -128,10 +128,10 @@ set_gdb_data_directory (const char *new_datadir)
      isn't canonical, but that's ok.  */
   if (!IS_ABSOLUTE_PATH (gdb_datadir))
     {
-      char *abs_datadir = gdb_abspath (gdb_datadir);
+      gdb::unique_xmalloc_ptr<char> abs_datadir = gdb_abspath (gdb_datadir);
 
       xfree (gdb_datadir);
-      gdb_datadir = abs_datadir;
+      gdb_datadir = abs_datadir.release ();
     }
 }
 
diff --git a/gdb/objfiles.c b/gdb/objfiles.c
index d261c87..ff99ca6 100644
--- a/gdb/objfiles.c
+++ b/gdb/objfiles.c
@@ -386,22 +386,25 @@ allocate_objfile (bfd *abfd, const char *name, objfile_flags flags)
 
   objfile_alloc_data (objfile);
 
+  gdb::unique_xmalloc_ptr<char> name_holder;
   if (name == NULL)
     {
       gdb_assert (abfd == NULL);
       gdb_assert ((flags & OBJF_NOT_FILENAME) != 0);
-      expanded_name = xstrdup ("<<anonymous objfile>>");
+      expanded_name = "<<anonymous objfile>>";
     }
   else if ((flags & OBJF_NOT_FILENAME) != 0
 	   || is_target_filename (name))
-    expanded_name = xstrdup (name);
+    expanded_name = name;
   else
-    expanded_name = gdb_abspath (name);
+    {
+      name_holder = gdb_abspath (name);
+      expanded_name = name_holder.get ();
+    }
   objfile->original_name
     = (char *) obstack_copy0 (&objfile->objfile_obstack,
 			      expanded_name,
 			      strlen (expanded_name));
-  xfree (expanded_name);
 
   /* Update the per-objfile information that comes from the bfd, ensuring
      that any data that is reference is saved in the per-objfile data
diff --git a/gdb/source.c b/gdb/source.c
index 769d9ef..e2a507d 100644
--- a/gdb/source.c
+++ b/gdb/source.c
@@ -913,7 +913,7 @@ done:
       else if ((opts & OPF_RETURN_REALPATH) != 0)
 	*filename_opened = gdb_realpath (filename);
       else
-	*filename_opened = gdb_abspath (filename);
+	*filename_opened = gdb_abspath (filename).release ();
     }
 
   errno = last_errno;
diff --git a/gdb/utils.c b/gdb/utils.c
index 96ae709..9959c01 100644
--- a/gdb/utils.c
+++ b/gdb/utils.c
@@ -2761,28 +2761,25 @@ gdb_realpath_keepfile (const char *filename)
 
 /* Return PATH in absolute form, performing tilde-expansion if necessary.
    PATH cannot be NULL or the empty string.
-   This does not resolve symlinks however, use gdb_realpath for that.
-   Space for the result is allocated with malloc.
-   If the path is already absolute, it is strdup'd.
-   If there is a problem computing the absolute path, the path is returned
-   unchanged (still strdup'd).  */
+   This does not resolve symlinks however, use gdb_realpath for that.  */
 
-char *
+gdb::unique_xmalloc_ptr<char>
 gdb_abspath (const char *path)
 {
   gdb_assert (path != NULL && path[0] != '\0');
 
   if (path[0] == '~')
-    return tilde_expand (path);
+    return gdb::unique_xmalloc_ptr<char> (tilde_expand (path));
 
   if (IS_ABSOLUTE_PATH (path))
-    return xstrdup (path);
+    return gdb::unique_xmalloc_ptr<char> (xstrdup (path));
 
   /* Beware the // my son, the Emacs barfs, the botch that catch...  */
-  return concat (current_directory,
-	    IS_DIR_SEPARATOR (current_directory[strlen (current_directory) - 1])
-		 ? "" : SLASH_STRING,
-		 path, (char *) NULL);
+  return gdb::unique_xmalloc_ptr<char>
+    (concat (current_directory,
+	     IS_DIR_SEPARATOR (current_directory[strlen (current_directory) - 1])
+	     ? "" : SLASH_STRING,
+	     path, (char *) NULL));
 }
 
 ULONGEST
diff --git a/gdb/utils.h b/gdb/utils.h
index bb5fadc..a2a959f 100644
--- a/gdb/utils.h
+++ b/gdb/utils.h
@@ -256,7 +256,7 @@ extern char *gdb_realpath (const char *);
 
 extern char *gdb_realpath_keepfile (const char *);
 
-extern char *gdb_abspath (const char *);
+extern gdb::unique_xmalloc_ptr<char> gdb_abspath (const char *);
 
 extern int gdb_filename_fnmatch (const char *pattern, const char *string,
 				 int flags);
-- 
2.9.4


  parent reply	other threads:[~2017-08-18 20:01 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-08-18 20:01 [RFA 0/5] simple string changes Tom Tromey
2017-08-18 20:01 ` [RFA 5/5] Change psymtab_search_name to return a unique_xmalloc_ptr Tom Tromey
2017-08-18 20:01 ` [RFA 2/5] Change gdb_realpath_keepfile " Tom Tromey
2017-08-18 20:01 ` Tom Tromey [this message]
2017-08-18 20:01 ` [RFA 4/5] Change rewrite_source_path " Tom Tromey
2017-08-18 20:01 ` [RFA 3/5] Change gdb_realpath " Tom Tromey
2017-08-21  9:57 ` [RFA 0/5] simple string changes Pedro Alves

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170818200024.4948-2-tom@tromey.com \
    --to=tom@tromey.com \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox