Remove regcache_xmalloc
[deliverable/binutils-gdb.git] / gdb / c-varobj.c
index 48e16f9d56739887b79c30681d19d083e2210239..e3b097b227578d208f6a5098a147a01f39281662 100644 (file)
@@ -1,6 +1,6 @@
 /* varobj support for C and C++.
 
-   Copyright (C) 1999-2016 Free Software Foundation, Inc.
+   Copyright (C) 1999-2017 Free Software Foundation, Inc.
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -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,
@@ -78,7 +78,7 @@ adjust_value_for_child_access (struct value **value,
      to us, is already supposed to be
      reference-stripped.  */
 
-  gdb_assert (TYPE_CODE (*type) != TYPE_CODE_REF);
+  gdb_assert (!TYPE_IS_REFERENCE (*type));
 
   /* Pointers to structures are treated just like
      structures when accessing children.  Don't
@@ -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)
 {
@@ -489,23 +488,18 @@ c_value_of_variable (const struct varobj *var,
   struct type *type = get_type (var);
 
   /* Strip top-level references.  */
-  while (TYPE_CODE (type) == TYPE_CODE_REF)
+  while (TYPE_IS_REFERENCE (type))
     type = check_typedef (TYPE_TARGET_TYPE (type));
 
   switch (TYPE_CODE (type))
     {
     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);
          }
@@ -556,7 +550,7 @@ const struct lang_varobj_ops c_varobj_ops =
    c_is_path_expr_parent  /* is_path_expr_parent */
 };
 
-/* A little convenience enum for dealing with C++/Java.  */
+/* A little convenience enum for dealing with C++.  */
 enum vsections
 {
   v_public = 0, v_private, v_protected
@@ -586,7 +580,7 @@ cplus_number_of_children (const struct varobj *var)
       if (opts.objectprint)
         {
           value = var->value;
-          lookup_actual_type = (TYPE_CODE (var->type) == TYPE_CODE_REF
+          lookup_actual_type = (TYPE_IS_REFERENCE (var->type)
                                || TYPE_CODE (var->type) == TYPE_CODE_PTR);
         }
       adjust_value_for_child_access (&value, &type, NULL, lookup_actual_type);
@@ -623,15 +617,15 @@ cplus_number_of_children (const struct varobj *var)
          const struct varobj *parent = var->parent;
 
          value = parent->value;
-         lookup_actual_type = (TYPE_CODE (parent->type) == TYPE_CODE_REF
+         lookup_actual_type = (TYPE_IS_REFERENCE (parent->type)
                                || TYPE_CODE (parent->type) == TYPE_CODE_PTR);
         }
       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,31 +698,31 @@ 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);
 
   var = (CPLUS_FAKE_CHILD (parent)) ? parent->parent : parent;
   if (opts.objectprint)
-    lookup_actual_type = (TYPE_CODE (var->type) == TYPE_CODE_REF
+    lookup_actual_type = (TYPE_IS_REFERENCE (var->type)
                          || TYPE_CODE (var->type) == TYPE_CODE_PTR);
   value = var->value;
   type = varobj_get_value_type (var);
@@ -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,16 +834,16 @@ 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
        {
-         char *access = NULL;
+         const char *access = NULL;
          int children[3];
 
          cplus_class_num_children (type, children);
@@ -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);
 }
This page took 0.031198 seconds and 4 git commands to generate.