Re: [RFA 03/11] Remove cleanups from mi-cmd-var.c

Tom Tromey
2017-09-29T01:40:57Z

>>>>> "Pedro" == Pedro Alves <palves@redhat.com> writes:

Pedro> There's still a deep copy in that last else above.
Pedro> We should be able to get rid of that like this:
[...]

Here's my latest.

Tom

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index 1a58d82..5a6791e 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,3 +1,10 @@
+2017-09-12 Tom Tromey <tom@tromey.com>
+
+ * varobj.h (varobj_gen_name): Return std::string.
+ * varobj.c (varobj_gen_name): Return std::string.
+ * mi/mi-cmd-var.c (mi_cmd_var_create): Use std::string.
+ (mi_cmd_var_delete): Don't copy "name".
+
2017-09-28 Tom Tromey <tom@tromey.com>

* mi/mi-cmd-break.c (mi_argv_to_format): Return std::string.
diff --git a/gdb/mi/mi-cmd-var.c b/gdb/mi/mi-cmd-var.c
index 8b22b2f..0215b1a 100644
--- a/gdb/mi/mi-cmd-var.c
+++ b/gdb/mi/mi-cmd-var.c
@@ -95,32 +95,24 @@ mi_cmd_var_create (const char *command, char **argv, int argc)
struct ui_out *uiout = current_uiout;
CORE_ADDR frameaddr = 0;
struct varobj *var;
- char *name;
char *frame;
char *expr;
- struct cleanup *old_cleanups;
enum varobj_type var_type;

if (argc != 3)
error (_("-var-create: Usage: NAME FRAME EXPRESSION."));

- name = xstrdup (argv[0]);
- /* Add cleanup for name. Must be free_current_contents as name can
- be reallocated. */
- old_cleanups = make_cleanup (free_current_contents, &name);
-
- frame = xstrdup (argv[1]);
- make_cleanup (xfree, frame);
-
- expr = xstrdup (argv[2]);
- make_cleanup (xfree, expr);
+ frame = argv[1];
+ expr = argv[2];

+ const char *name = argv[0];
+ std::string gen_name;
if (strcmp (name, "-") == 0)
{
- xfree (name);
- name = varobj_gen_name ();
+ gen_name = varobj_gen_name ();
+ name = gen_name.c_str ();
}
- else if (!isalpha (*name))
+ else if (!isalpha (name[0]))
error (_("-var-create: name of object must begin with a letter"));

if (strcmp (frame, "*") == 0)
@@ -135,7 +127,7 @@ mi_cmd_var_create (const char *command, char **argv, int argc)

if (varobjdebug)
fprintf_unfiltered (gdb_stdlog,
- "Name=\"%s\", Frame=\"%s\" (%s), Expression=\"%s\"\n",
+ "Name=\"%s\", Frame=\"%s\" (%s), Expression=\"%s\"\n",
name, frame, hex_string (frameaddr), expr);

var = varobj_create (name, expr, frameaddr, var_type);
@@ -146,8 +138,6 @@ mi_cmd_var_create (const char *command, char **argv, int argc)
print_varobj (var, PRINT_ALL_VALUES, 0 /* don't print expression */);

uiout->field_int ("has_more", varobj_has_more (var, 0));
-
- do_cleanups (old_cleanups);
}

void
@@ -157,16 +147,12 @@ mi_cmd_var_delete (const char *command, char **argv, int argc)
struct varobj *var;
int numdel;
int children_only_p = 0;
- struct cleanup *old_cleanups;
struct ui_out *uiout = current_uiout;

if (argc < 1 || argc > 2)
error (_("-var-delete: Usage: [-c] EXPRESSION."));

- name = xstrdup (argv[0]);
- /* Add cleanup for name. Must be free_current_contents as name can
- be reallocated. */
- old_cleanups = make_cleanup (free_current_contents, &name);
+ name = argv[0];

/* If we have one single argument it cannot be '-c' or any string
starting with '-'. */
@@ -186,9 +172,7 @@ mi_cmd_var_delete (const char *command, char **argv, int argc)
if (strcmp (name, "-c") != 0)
error (_("-var-delete: Invalid option."));
children_only_p = 1;
- do_cleanups (old_cleanups);
- name = xstrdup (argv[1]);
- old_cleanups = make_cleanup (free_current_contents, &name);
+ name = argv[1];
}

/* If we didn't error out, now NAME contains the name of the
@@ -199,8 +183,6 @@ mi_cmd_var_delete (const char *command, char **argv, int argc)
numdel = varobj_delete (var, children_only_p);

uiout->field_int ("ndeleted", numdel);
-
- do_cleanups (old_cleanups);
}

/* Parse a string argument into a format value. */
diff --git a/gdb/varobj.c b/gdb/varobj.c
index f669180..2d850fb 100644
--- a/gdb/varobj.c
+++ b/gdb/varobj.c
@@ -435,17 +435,14 @@ varobj_create (const char *objname,

/* Generates an unique name that can be used for a varobj. */

-char *
+std::string
varobj_gen_name (void)
{
static int id = 0;
- char *obj_name;

/* Generate a name for this object. */
id++;
- obj_name = xstrprintf ("var%d", id);
-
- return obj_name;
+ return string_printf ("var%d", id);
}

/* Given an OBJNAME, returns the pointer to the corresponding varobj. Call
diff --git a/gdb/varobj.h b/gdb/varobj.h
index e35c1b8..0d4a537 100644
--- a/gdb/varobj.h
+++ b/gdb/varobj.h
@@ -233,7 +233,7 @@ extern struct varobj *varobj_create (const char *objname,
const char *expression, CORE_ADDR frame,
enum varobj_type type);

-extern char *varobj_gen_name (void);
+extern std::string varobj_gen_name (void);

extern struct varobj *varobj_get_handle (const char *name);