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

Pedro Alves
2017-09-28T09:36:17Z

On 09/12/2017 07:57 PM, Tom Tromey wrote:

> diff --git a/gdb/mi/mi-cmd-var.c b/gdb/mi/mi-cmd-var.c
> index 8b22b2f..b628814 100644
> --- a/gdb/mi/mi-cmd-var.c
> +++ b/gdb/mi/mi-cmd-var.c
> @@ -95,32 +95,21 @@ 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);
> + std::string name = argv[0];
>
> - expr = xstrdup (argv[2]);
> - make_cleanup (xfree, expr);
> + frame = argv[1];
> + expr = argv[2];
>
> - if (strcmp (name, "-") == 0)
> - {
> - xfree (name);
> - name = varobj_gen_name ();
> - }
> - else if (!isalpha (*name))
> + if (name == "-")
> + name = varobj_gen_name ();
> + else if (!isalpha (name[0]))
> error (_("-var-create: name of object must begin with a letter"));

name was a deep copy of argv[0] up to here. That copy
appears unnecessary, if you write it like this:

std::string name;

if (strcmp (argv[0], "-") == 0)
name = varobj_gen_name ();
else if (!isalpha (*argv[0]))
error (_("-var-create: name of object must begin with a letter"));
else
name = argv[0];

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

const char *name;
std::string gen_name;

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

Otherwise OK.

Thanks,
Pedro Alves