Change gdb_abspath to return a unique_xmalloc_ptr

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-22  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.
This commit is contained in:
Tom Tromey 2017-08-03 16:32:14 -06:00
parent 0d999a6ef0
commit e3e41d588a
7 changed files with 33 additions and 28 deletions

View File

@ -1,3 +1,13 @@
2017-08-22 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-22 Zhouyi Zhou <zhouzhouyi@gmail.com>
* cli-cmds.c (list_commands): List actual code around more than

View File

@ -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

View File

@ -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 ();
}
}

View File

@ -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

View File

@ -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;

View File

@ -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

View File

@ -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);