change and rename gmp_string_asprintf to return an std::string
authorJoel Brobecker <brobecker@adacore.com>
Tue, 24 Nov 2020 02:45:35 +0000 (21:45 -0500)
committerJoel Brobecker <brobecker@adacore.com>
Tue, 24 Nov 2020 02:45:35 +0000 (21:45 -0500)
This was suggested by Simon during a code review of this package upstream.
The upside is that this makes the function's API more natural and C++.
The downside is an extra malloc, which might be the reason why we went
for using a unique_xmalloc_ptr in the first place. Since this function
is not expected to be called frequently, the API improvement might be
worth the performance impact.

gdb/ChangeLog:

        * gmp-utils.h (gmp_string_printf): Rename from gmp_string_asprintf.
        Change return type to std::string. Update all callers.
        * gmp-utils.c (gmp_string_printf): Likewise.

gdb/ChangeLog
gdb/gdbtypes.c
gdb/gmp-utils.c
gdb/gmp-utils.h
gdb/typeprint.c
gdb/valprint.c

index bd74aa55e5b34c09afd4497f192392dce8b4d530..2cb64da1fdd335505a5006313dea177f2aea800e 100644 (file)
@@ -1,3 +1,9 @@
+2020-11-24  Joel Brobecker  <brobecker@adacore.com>
+
+       * gmp-utils.h (gmp_string_printf): Rename from gmp_string_asprintf.
+       Change return type to std::string. Update all callers.
+       * gmp-utils.c (gmp_string_printf): Likewise.
+
 2020-11-24  Joel Brobecker  <brobecker@adacore.com>
 
        * unittests/gmp-utils-selftests.c (write_fp_test): Use mpq_set_si
index e6f70bbe2d37094e84b5a7efb5e71b9f21e8f71d..3879eebd894f909815a0807dcb5c462ceb55b067 100644 (file)
@@ -4927,7 +4927,7 @@ static void
 print_fixed_point_type_info (struct type *type, int spaces)
 {
   printfi_filtered (spaces + 2, "scaling factor: %s\n",
-                   fixed_point_scaling_factor (type).str ().get ());
+                   fixed_point_scaling_factor (type).str ().c_str ());
 }
 
 static struct obstack dont_print_type_obstack;
index db92e57316c5b9a958aeb3ddb9c970e0672e810d..44fe156d32dcfdafd5c1494b9cd80622c7e120fd 100644 (file)
 
 /* See gmp-utils.h.  */
 
-gdb::unique_xmalloc_ptr<char>
-gmp_string_asprintf (const char *fmt, ...)
+std::string
+gmp_string_printf (const char *fmt, ...)
 {
   va_list vp;
-  char *buf;
 
   va_start (vp, fmt);
-  gmp_vasprintf (&buf, fmt, vp);
+  int size = gmp_vsnprintf (NULL, 0, fmt, vp);
   va_end (vp);
 
-  return gdb::unique_xmalloc_ptr<char> (buf);
+  std::string str (size, '\0');
+
+  /* C++11 and later guarantee std::string uses contiguous memory and
+     always includes the terminating '\0'.  */
+  va_start (vp, fmt);
+  gmp_vsprintf (&str[0], fmt, vp);
+  va_end (vp);
+
+  return str;
 }
 
 /* See gmp-utils.h.  */
index 1214b645367a1b514dc788257e01374637c5ab4e..59965e504c54cc5c6aad529450c206006eea2770 100644 (file)
@@ -29,9 +29,9 @@
 #include <gmp.h>
 #include "gdbsupport/traits.h"
 
-/* Same as gmp_asprintf, but returning a convenient wrapper type.  */
+/* Same as gmp_asprintf, but returning an std::string.  */
 
-gdb::unique_xmalloc_ptr<char> gmp_string_asprintf (const char *fmt, ...);
+std::string gmp_string_printf (const char *fmt, ...);
 
 /* A class to make it easier to use GMP's mpz_t values within GDB.  */
 
@@ -110,8 +110,7 @@ struct gdb_mpz
              bool unsigned_p) const;
 
   /* Return a string containing VAL.  */
-  gdb::unique_xmalloc_ptr<char> str () const
-  { return gmp_string_asprintf ("%Zd", val); }
+  std::string str () const { return gmp_string_printf ("%Zd", val); }
 
   /* The destructor.  */
   ~gdb_mpz () { mpz_clear (val); }
@@ -163,8 +162,7 @@ struct gdb_mpq
   }
 
   /* Return a string representing VAL as "<numerator> / <denominator>".  */
-  gdb::unique_xmalloc_ptr<char> str () const
-  { return gmp_string_asprintf ("%Qd", val); }
+  std::string str () const { return gmp_string_printf ("%Qd", val); }
 
   /* Return VAL rounded to the nearest integer.  */
   gdb_mpz get_rounded () const;
index f947faf315f6566d74f1b0bb53b49f3ef7ba11e2..0dd3b1c482143a49c8a54c788049d4892b7487c3 100644 (file)
@@ -667,11 +667,10 @@ print_type_scalar (struct type *type, LONGEST val, struct ui_file *stream)
 void
 print_type_fixed_point (struct type *type, struct ui_file *stream)
 {
-  gdb::unique_xmalloc_ptr<char> small_img
-    = fixed_point_scaling_factor (type).str ();
+  std::string small_img = fixed_point_scaling_factor (type).str ();
 
   fprintf_filtered (stream, "%s-byte fixed point (small = %s)",
-                   pulongest (TYPE_LENGTH (type)), small_img.get ());
+                   pulongest (TYPE_LENGTH (type)), small_img.c_str ());
 }
 
 /* Dump details of a type specified either directly or indirectly.
index 38ae0bdf0e25defab21ece9bbce7c07ac651a300..b102ff4b5717b28dea65971c1f66e629374ba827 100644 (file)
@@ -814,8 +814,8 @@ generic_val_print_fixed_point (struct value *val, struct ui_file *stream,
                          fixed_point_scaling_factor (type));
 
       const char *fmt = TYPE_LENGTH (type) < 4 ? "%.11Fg" : "%.17Fg";
-      gdb::unique_xmalloc_ptr<char> str = gmp_string_asprintf (fmt, f.val);
-      fprintf_filtered (stream, "%s", str.get ());
+      std::string str = gmp_string_printf (fmt, f.val);
+      fprintf_filtered (stream, "%s", str.c_str ());
     }
 }
 
This page took 0.036826 seconds and 4 git commands to generate.