]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
Simplify typename_concat
authorTom Tromey <tromey@adacore.com>
Mon, 29 Jul 2024 15:38:29 +0000 (09:38 -0600)
committerTom Tromey <tromey@adacore.com>
Mon, 12 Aug 2024 18:16:15 +0000 (12:16 -0600)
This patch simplifies typename_concat, changing the return type and
removing the obstack allocation code.  The latter is possible because
the only caller using this mode uses the name when creating a new
type, and 'new_type' copies the string to the appropriate obstack
anyway.  It also changes typename_concat to use 'concat'.  This change
lets us remove a mildly fragile macro as well.

Approved-By: Simon Marchi <simon.marchi@efficios.com>
gdb/dwarf2/read.c

index a0013f7e475692a46b934b8bb185bbb89ad8b3ed..fde8eee62762ac528181091cc386be9eb24e9b8c 100644 (file)
@@ -863,9 +863,10 @@ static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
 
 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
 
-static char *typename_concat (struct obstack *obs, const char *prefix,
-                             const char *suffix, int physname,
-                             struct dwarf2_cu *cu);
+static gdb::unique_xmalloc_ptr<char> typename_concat (const char *prefix,
+                                                     const char *suffix,
+                                                     int physname,
+                                                     struct dwarf2_cu *cu);
 
 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
 
@@ -6673,7 +6674,7 @@ dwarf2_compute_name (const char *name,
          if (*prefix != '\0')
            {
              gdb::unique_xmalloc_ptr<char> prefixed_name
-               (typename_concat (NULL, prefix, name, physname, cu));
+               = typename_concat (prefix, name, physname, cu);
 
              buf.puts (prefixed_name.get ());
            }
@@ -13986,9 +13987,12 @@ read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
   /* Now build the name of the current namespace.  */
 
   previous_prefix = determine_prefix (die, cu);
+  gdb::unique_xmalloc_ptr<char> name_storage;
   if (previous_prefix[0] != '\0')
-    name = typename_concat (&objfile->objfile_obstack,
-                           previous_prefix, name, 0, cu);
+    {
+      name_storage = typename_concat (previous_prefix, name, 0, cu);
+      name = name_storage.get ();
+    }
 
   /* Create the type.  */
   type = type_allocator (objfile, cu->lang ()).new_type (TYPE_CODE_NAMESPACE,
@@ -19998,17 +20002,15 @@ determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
       }
 }
 
-/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
-   with appropriate separator.  If PREFIX or SUFFIX is NULL or empty, then
-   simply copy the SUFFIX or PREFIX, respectively.  If OBS is non-null, perform
-   an obconcat, otherwise allocate storage for the result.  The CU argument is
-   used to determine the language and hence, the appropriate separator.  */
-
-#define MAX_SEP_LEN 7  /* strlen ("__") + strlen ("_MOD_")  */
+/* Return a newly-allocated string formed by concatenating PREFIX and
+   SUFFIX with appropriate separator.  If PREFIX or SUFFIX is NULL or
+   empty, then simply copy the SUFFIX or PREFIX, respectively.  The CU
+   argument is used to determine the language and hence, the
+   appropriate separator.  */
 
-static char *
-typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
-                int physname, struct dwarf2_cu *cu)
+static gdb::unique_xmalloc_ptr<char>
+typename_concat (const char *prefix, const char *suffix, int physname,
+                struct dwarf2_cu *cu)
 {
   const char *lead = "";
   const char *sep;
@@ -20044,23 +20046,8 @@ typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
   if (suffix == NULL)
     suffix = "";
 
-  if (obs == NULL)
-    {
-      char *retval
-       = ((char *)
-          xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
-
-      strcpy (retval, lead);
-      strcat (retval, prefix);
-      strcat (retval, sep);
-      strcat (retval, suffix);
-      return retval;
-    }
-  else
-    {
-      /* We have an obstack.  */
-      return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
-    }
+  return gdb::unique_xmalloc_ptr<char> (concat (lead, prefix, sep, suffix,
+                                               nullptr));
 }
 
 /* Return a generic name for a DW_TAG_template_type_param or