Use ui_file_as_string throughout more

This replaces most of the remaining ui_file_xstrdup calls with
ui_file_as_string calls.  Whenever a call was replaced, that led to a
cascade of other necessary adjustments throughout, to make the code
use std::string instead of raw pointers.  And then whenever I added a
std::string as member of a struct, I needed to adjust
allocation/destruction of said struct to use new/delete instead of
xmalloc/xfree.

The stopping point was once gdb built again.  These doesn't seem to be
a way to reasonably split this out further.

Maybe-not-obvious changes:

 - demangle_for_lookup returns a cleanup today.  To get rid of that,
   and avoid unnecessary string dupping/copying, this introduces a
   demangle_result_storage type that the caller instantiates and
   passes to demangle_for_lookup.

 - Many methods returned a "char *" to indicate that the caller owns
   the memory and must free it.  Those are switched to return a
   std::string instead.  Methods that return a "view" into some
   internal string return a "const char *" instead.  I.e., we only
   copy/allocate when necessary.

gdb/ChangeLog:
2016-11-08  Pedro Alves  <palves@redhat.com>

	* ada-lang.c (ada_name_for_lookup, type_as_string): Use and return
	std::string.
	(type_as_string_and_cleanup): Delete.
	(ada_lookup_struct_elt_type): Use type_as_string.
	* ada-lang.h (ada_name_for_lookup): Now returns std::string.
	* ada-varobj.c (ada_varobj_scalar_image): Return a std::string.
	(ada_varobj_describe_child): Make 'child_name' and
	'child_path_expr' parameters std::string pointers.
	(ada_varobj_describe_struct_child, ada_varobj_describe_ptr_child):
	Likewise, and use string_printf.
	(ada_varobj_describe_simple_array_child)
	(ada_varobj_describe_child): Likewise.
	(ada_varobj_get_name_of_child, ada_varobj_get_path_expr_of_child)
	(ada_varobj_get_value_image)
	(ada_varobj_get_value_of_array_variable)
	(ada_varobj_get_value_of_variable, ada_name_of_variable)
	(ada_name_of_child, ada_path_expr_of_child)
	(ada_value_of_variable): Now returns std::string.  Use
	string_printf.
	(ada_value_of_child): Adjust.
	* break-catch-throw.c (check_status_exception_catchpoint): Adjust
	to use std::string.
	* breakpoint.c (watch_command_1): Adjust to use std::string.
	* c-lang.c (c_get_string): Adjust to use std::string.
	* c-typeprint.c (print_name_maybe_canonical): Use std::string.
	* c-varobj.c (varobj_is_anonymous_child): Use ==/!= std::string
	operators.
	(c_name_of_variable): Now returns a std::string.
	(c_describe_child): The 'cname' and 'cfull_expression' output
	parameters are now std::string pointers.  Adjust.
	(c_name_of_child, c_path_expr_of_child, c_value_of_variable)
	(cplus_number_of_children): Adjust to use std::string and
	string_printf.
	(cplus_name_of_variable): Now returns a std::string.
	(cplus_describe_child): The 'cname' and 'cfull_expression' output
	parameters are now std::string pointers.  Adjust.
	(cplus_name_of_child, cplus_path_expr_of_child)
	(cplus_value_of_variable): Now returns a std::string.
	* cp-abi.c (cplus_typename_from_type_info): Return std::string.
	* cp-abi.h (cplus_typename_from_type_info): Return std::string.
	(struct cp_abi_ops) <get_typename_from_type_info>: Return
	std::string.
	* cp-support.c (inspect_type): Use std::string.
	(cp_canonicalize_string_full, cp_canonicalize_string_no_typedefs)
	(cp_canonicalize_string): Return std::string and adjust.
	* cp-support.h (cp_canonicalize_string)
	(cp_canonicalize_string_no_typedefs, cp_canonicalize_string_full):
	Return std::string.
	* dbxread.c (read_dbx_symtab): Use std::string.
	* dwarf2read.c (dwarf2_canonicalize_name): Adjust to use std::string.
	* gdbcmd.h (lookup_struct_elt_type): Adjust to use std::string.
	* gnu-v3-abi.c (gnuv3_get_typeid): Use std::string.
	(gnuv3_get_typename_from_type_info): Return a std::string and
	adjust.
	(gnuv3_get_type_from_type_info): Adjust to use std::string.
	* guile/guile.c (gdbscm_execute_gdb_command): Adjust to use
	std::string.
	* infcmd.c (print_return_value_1): Adjust to use std::string.
	* linespec.c (find_linespec_symbols): Adjust to
	demangle_for_lookup API change.  Use std::string.
	* mi/mi-cmd-var.c (print_varobj, mi_cmd_var_set_format)
	(mi_cmd_var_info_type, mi_cmd_var_info_path_expression)
	(mi_cmd_var_info_expression, mi_cmd_var_evaluate_expression)
	(mi_cmd_var_assign, varobj_update_one): Adjust to use std::string.
	* minsyms.c (lookup_minimal_symbol): Use std::string.
	* python/py-varobj.c (py_varobj_iter_next): Use new instead of
	XNEW.  vitem->name is a std::string now, adjust.
	* rust-exp.y (convert_ast_to_type, convert_name): Adjust to use
	std::string.
	* stabsread.c (define_symbol): Adjust to use std::string.
	* symtab.c (demangle_for_lookup): Now returns 'const char *'.  Add
	a demangle_result_storage parameter.  Use it for storage.
	(lookup_symbol_in_language)
	(lookup_symbol_in_objfile_from_linkage_name): Adjust to new
	demangle_for_lookup API.
	* symtab.h (struct demangle_result_storage): New type.
	(demangle_for_lookup): Now returns 'const char *'.  Add a
	demangle_result_storage parameter.
	* typeprint.c (type_to_string): Return std::string and use
	ui_file_as_string.
	* value.h (type_to_string): Change return type to std::string.
	* varobj-iter.h (struct varobj_item) <name>: Now a std::string.
	(varobj_iter_delete): Use delete instead of xfree.
	* varobj.c (create_child): Return std::string instead of char * in
	output parameter.
	(name_of_variable, name_of_child, my_value_of_variable): Return
	std::string instead of char *.
	(varobj_create, varobj_get_handle): Constify 'objname' parameter.
	Adjust to std::string fields.
	(varobj_get_objname): Return a const char * instead of a char *.
	(varobj_get_expression): Return a std::string.
	(varobj_list_children): Adjust to use std::string.
	(varobj_get_type): Return a std::string.
	(varobj_get_path_expr): Return a const char * instead of a char *.
	Adjust to std::string fields.
	(varobj_get_formatted_value, varobj_get_value): Return a
	std::string.
	(varobj_set_value): Change type of 'expression' parameter to
	std::string.  Use std::string.
	(install_new_value): Use std::string.
	(delete_variable_1): Adjust to use std::string.
	(create_child): Change the 'name' parameter to a std::string
	reference.  Swap it into the new item's name.
	(create_child_with_value): Swap item's name into the new child's
	name.  Use string_printf.
	(new_variable): Use new instead of XNEW.
	(free_variable): Don't xfree fields that are now std::string.
	(name_of_variable, name_of_child): Now returns std::string.
	(value_of_root): Adjust to use std::string.
	(my_value_of_variable, varobj_value_get_print_value): Return
	and use std::string.
	(varobj_value_get_print_value): Adjust to use ui_file_as_string
	and std::string.
	* varobj.h (struct varobj) <name, path_expr, obj_name,
	print_value>: Now std::string's.
	<name_of_variable, name_of_child, path_expr_of_child,
	value_of_variable>: Return std::string.
	(varobj_create, varobj_get_handle): Constify 'objname' parameter.
	(varobj_get_objname): Return a const char * instead of a char *.
	(varobj_get_expression, varobj_get_type): Return a std::string.
	(varobj_get_path_expr): Return a const char * instead of a char *.
	(varobj_get_formatted_value, varobj_get_value): Return a
	std::string.
	(varobj_set_value): Constify 'expression' parameter.
	(varobj_value_get_print_value): Return a std::string.
This commit is contained in:
Pedro Alves
2016-11-08 15:26:47 +00:00
parent d2af8993a7
commit 2f408ecb92
31 changed files with 608 additions and 615 deletions

View File

@@ -37,8 +37,8 @@ static void cplus_class_num_children (struct type *type, int children[3]);
int
varobj_is_anonymous_child (const struct varobj *child)
{
return (strcmp (child->name, ANONYMOUS_STRUCT_NAME) == 0
|| strcmp (child->name, ANONYMOUS_UNION_NAME) == 0);
return (child->name == ANONYMOUS_STRUCT_NAME
|| child->name == ANONYMOUS_UNION_NAME);
}
/* Given the value and the type of a variable object,
@@ -231,10 +231,10 @@ c_number_of_children (const struct varobj *var)
return children;
}
static char *
static std::string
c_name_of_variable (const struct varobj *parent)
{
return xstrdup (parent->name);
return parent->name;
}
/* Return the value of element TYPE_INDEX of a structure
@@ -279,40 +279,39 @@ value_struct_element_index (struct value *value, int type_index)
If any of CNAME, CVALUE, or CTYPE is not null, but the corresponding
information cannot be determined, set *CNAME, *CVALUE, or *CTYPE
to NULL. */
to empty. */
static void
c_describe_child (const struct varobj *parent, int index,
char **cname, struct value **cvalue, struct type **ctype,
char **cfull_expression)
std::string *cname, struct value **cvalue,
struct type **ctype, std::string *cfull_expression)
{
struct value *value = parent->value;
struct type *type = varobj_get_value_type (parent);
char *parent_expression = NULL;
std::string parent_expression;
int was_ptr;
if (cname)
*cname = NULL;
*cname = std::string ();
if (cvalue)
*cvalue = NULL;
if (ctype)
*ctype = NULL;
if (cfull_expression)
{
*cfull_expression = NULL;
*cfull_expression = std::string ();
parent_expression
= varobj_get_path_expr (varobj_get_path_expr_parent (parent));
}
adjust_value_for_child_access (&value, &type, &was_ptr, 0);
switch (TYPE_CODE (type))
{
case TYPE_CODE_ARRAY:
if (cname)
*cname
= xstrdup (int_string (index
+ TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type)),
10, 1, 0, 0));
*cname = int_string (index
+ TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type)),
10, 1, 0, 0);
if (cvalue && value)
{
@@ -333,10 +332,10 @@ c_describe_child (const struct varobj *parent, int index,
if (cfull_expression)
*cfull_expression =
xstrprintf ("(%s)[%s]", parent_expression,
int_string (index
+ TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type)),
10, 1, 0, 0));
string_printf ("(%s)[%s]", parent_expression.c_str (),
int_string (index
+ TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type)),
10, 1, 0, 0));
break;
@@ -355,25 +354,26 @@ c_describe_child (const struct varobj *parent, int index,
{
if (TYPE_CODE (TYPE_FIELD_TYPE (type, index))
== TYPE_CODE_STRUCT)
*cname = xstrdup (ANONYMOUS_STRUCT_NAME);
*cname = ANONYMOUS_STRUCT_NAME;
else
*cname = xstrdup (ANONYMOUS_UNION_NAME);
*cname = ANONYMOUS_UNION_NAME;
}
if (cfull_expression)
*cfull_expression = xstrdup ("");
*cfull_expression = "";
}
else
{
if (cname)
*cname = xstrdup (field_name);
*cname = field_name;
if (cfull_expression)
{
const char *join = was_ptr ? "->" : ".";
*cfull_expression = xstrprintf ("(%s)%s%s", parent_expression,
join, field_name);
*cfull_expression = string_printf ("(%s)%s%s",
parent_expression.c_str (),
join, field_name);
}
}
@@ -390,7 +390,7 @@ c_describe_child (const struct varobj *parent, int index,
case TYPE_CODE_PTR:
if (cname)
*cname = xstrprintf ("*%s", parent->name);
*cname = string_printf ("*%s", parent->name.c_str ());
if (cvalue && value)
{
@@ -413,33 +413,32 @@ c_describe_child (const struct varobj *parent, int index,
*ctype = TYPE_TARGET_TYPE (type);
if (cfull_expression)
*cfull_expression = xstrprintf ("*(%s)", parent_expression);
*cfull_expression = string_printf ("*(%s)", parent_expression.c_str ());
break;
default:
/* This should not happen. */
if (cname)
*cname = xstrdup ("???");
*cname = "???";
if (cfull_expression)
*cfull_expression = xstrdup ("???");
*cfull_expression = "???";
/* Don't set value and type, we don't know then. */
}
}
static char *
static std::string
c_name_of_child (const struct varobj *parent, int index)
{
char *name;
std::string name;
c_describe_child (parent, index, &name, NULL, NULL, NULL);
return name;
}
static char *
static std::string
c_path_expr_of_child (const struct varobj *child)
{
char *path_expr;
std::string path_expr;
c_describe_child (child->parent, child->index, NULL, NULL, NULL,
&path_expr);
@@ -479,7 +478,7 @@ get_type (const struct varobj *var)
return type;
}
static char *
static std::string
c_value_of_variable (const struct varobj *var,
enum varobj_display_formats format)
{
@@ -496,16 +495,11 @@ c_value_of_variable (const struct varobj *var,
{
case TYPE_CODE_STRUCT:
case TYPE_CODE_UNION:
return xstrdup ("{...}");
return "{...}";
/* break; */
case TYPE_CODE_ARRAY:
{
char *number;
number = xstrprintf ("[%d]", var->num_children);
return (number);
}
return string_printf ("[%d]", var->num_children);
/* break; */
default:
@@ -515,7 +509,7 @@ c_value_of_variable (const struct varobj *var,
/* This can happen if we attempt to get the value of a struct
member when the parent is an invalid pointer. This is an
error condition, so we should tell the caller. */
return NULL;
return std::string ();
}
else
{
@@ -523,7 +517,7 @@ c_value_of_variable (const struct varobj *var,
/* Frozen variable and no value yet. We don't
implicitly fetch the value. MI response will
use empty string for the value, which is OK. */
return NULL;
return std::string ();
gdb_assert (varobj_value_is_changeable_p (var));
gdb_assert (!value_lazy (var->value));
@@ -531,7 +525,7 @@ c_value_of_variable (const struct varobj *var,
/* If the specified format is the current one,
we can reuse print_value. */
if (format == var->format)
return xstrdup (var->print_value);
return var->print_value;
else
return varobj_value_get_print_value (var->value, format, var);
}
@@ -629,9 +623,9 @@ cplus_number_of_children (const struct varobj *var)
adjust_value_for_child_access (&value, &type, NULL, lookup_actual_type);
cplus_class_num_children (type, kids);
if (strcmp (var->name, "public") == 0)
if (var->name == "public")
children = kids[v_public];
else if (strcmp (var->name, "private") == 0)
else if (var->name == "private")
children = kids[v_private];
else
children = kids[v_protected];
@@ -677,7 +671,7 @@ cplus_class_num_children (struct type *type, int children[3])
}
}
static char *
static std::string
cplus_name_of_variable (const struct varobj *parent)
{
return c_name_of_variable (parent);
@@ -704,25 +698,25 @@ match_accessibility (struct type *type, int index, enum accessibility acc)
static void
cplus_describe_child (const struct varobj *parent, int index,
char **cname, struct value **cvalue, struct type **ctype,
char **cfull_expression)
std::string *cname, struct value **cvalue, struct type **ctype,
std::string *cfull_expression)
{
struct value *value;
struct type *type;
int was_ptr;
int lookup_actual_type = 0;
char *parent_expression = NULL;
const char *parent_expression = NULL;
const struct varobj *var;
struct value_print_options opts;
if (cname)
*cname = NULL;
*cname = std::string ();
if (cvalue)
*cvalue = NULL;
if (ctype)
*ctype = NULL;
if (cfull_expression)
*cfull_expression = NULL;
*cfull_expression = std::string ();
get_user_print_options (&opts);
@@ -758,9 +752,9 @@ cplus_describe_child (const struct varobj *parent, int index,
const char *field_name;
vptr_fieldno = get_vptr_fieldno (type, &basetype);
if (strcmp (parent->name, "private") == 0)
if (parent->name == "private")
acc = private_field;
else if (strcmp (parent->name, "protected") == 0)
else if (parent->name == "protected")
acc = protected_field;
while (index >= 0)
@@ -783,24 +777,24 @@ cplus_describe_child (const struct varobj *parent, int index,
{
if (TYPE_CODE (TYPE_FIELD_TYPE (type, type_index))
== TYPE_CODE_STRUCT)
*cname = xstrdup (ANONYMOUS_STRUCT_NAME);
*cname = ANONYMOUS_STRUCT_NAME;
else if (TYPE_CODE (TYPE_FIELD_TYPE (type, type_index))
== TYPE_CODE_UNION)
*cname = xstrdup (ANONYMOUS_UNION_NAME);
*cname = ANONYMOUS_UNION_NAME;
}
if (cfull_expression)
*cfull_expression = xstrdup ("");
*cfull_expression = std::string ();
}
else
{
if (cname)
*cname = xstrdup (TYPE_FIELD_NAME (type, type_index));
*cname = TYPE_FIELD_NAME (type, type_index);
if (cfull_expression)
*cfull_expression
= xstrprintf ("((%s)%s%s)", parent_expression, join,
field_name);
= string_printf ("((%s)%s%s)", parent_expression, join,
field_name);
}
if (cvalue && value)
@@ -813,7 +807,7 @@ cplus_describe_child (const struct varobj *parent, int index,
{
/* This is a baseclass. */
if (cname)
*cname = xstrdup (TYPE_FIELD_NAME (type, index));
*cname = TYPE_FIELD_NAME (type, index);
if (cvalue && value)
*cvalue = value_cast (TYPE_FIELD_TYPE (type, index), value);
@@ -840,11 +834,11 @@ cplus_describe_child (const struct varobj *parent, int index,
as a constructor, if it exists. Therefore, we must
indicate that the name is a class name by using the
'class' keyword. See PR mi/11912 */
*cfull_expression = xstrprintf ("(%s(class %s%s) %s)",
ptr,
TYPE_FIELD_NAME (type, index),
ptr,
parent_expression);
*cfull_expression = string_printf ("(%s(class %s%s) %s)",
ptr,
TYPE_FIELD_NAME (type, index),
ptr,
parent_expression);
}
}
else
@@ -892,7 +886,7 @@ cplus_describe_child (const struct varobj *parent, int index,
gdb_assert (access);
if (cname)
*cname = xstrdup (access);
*cname = access;
/* Value and type and full expression are null here. */
}
@@ -903,19 +897,19 @@ cplus_describe_child (const struct varobj *parent, int index,
}
}
static char *
static std::string
cplus_name_of_child (const struct varobj *parent, int index)
{
char *name = NULL;
std::string name;
cplus_describe_child (parent, index, &name, NULL, NULL, NULL);
return name;
}
static char *
static std::string
cplus_path_expr_of_child (const struct varobj *child)
{
char *path_expr;
std::string path_expr;
cplus_describe_child (child->parent, child->index, NULL, NULL, NULL,
&path_expr);
@@ -940,7 +934,7 @@ cplus_type_of_child (const struct varobj *parent, int index)
return type;
}
static char *
static std::string
cplus_value_of_variable (const struct varobj *var,
enum varobj_display_formats format)
{
@@ -948,7 +942,7 @@ cplus_value_of_variable (const struct varobj *var,
/* If we have one of our special types, don't print out
any value. */
if (CPLUS_FAKE_CHILD (var))
return xstrdup ("");
return std::string ();
return c_value_of_variable (var, format);
}