Introduce string_printf
authorPedro Alves <palves@redhat.com>
Tue, 8 Nov 2016 15:26:42 +0000 (15:26 +0000)
committerPedro Alves <palves@redhat.com>
Tue, 8 Nov 2016 15:26:42 +0000 (15:26 +0000)
This introduces the string_printf function.  Like asprintf, but
returns a std::string.

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

* Makefile.in (COMMON_OBS): Add utils-selftests.o.
* common/common-utils.c (string_printf): New function.
* common/common-utils.h: Include <string>.
(string_printf): Declare.
* utils-selftests.c: New file.

gdb/ChangeLog
gdb/Makefile.in
gdb/common/common-utils.c
gdb/common/common-utils.h
gdb/utils-selftests.c [new file with mode: 0644]

index cc7f9a034d575563c898d29aa595a28e90baaa64..1b647bb38ed42449e3c0f79b4b8e41323eaf0552 100644 (file)
@@ -1,3 +1,11 @@
+2016-11-08  Pedro Alves  <palves@redhat.com>
+
+       * Makefile.in (COMMON_OBS): Add utils-selftests.o.
+       * common/common-utils.c (string_printf): New function.
+       * common/common-utils.h: Include <string>.
+       (string_printf): Declare.
+       * utils-selftests.c: New file.
+
 2016-11-08  Yao Qi  <yao.qi@linaro.org>
 
        * aarch64-tdep.c (aarch64_software_single_step): Return
index 6db63c7e90d4e6db3567ec1a11942fb28f6ee707..3876cd92ac54cb3cd7fed1cc557cae55012eba9e 100644 (file)
@@ -1066,7 +1066,7 @@ COMMON_OBS = $(DEPFILES) $(CONFIG_OBS) $(YYOBJ) \
        ada-typeprint.o c-typeprint.o f-typeprint.o m2-typeprint.o \
        ada-valprint.o c-valprint.o cp-valprint.o d-valprint.o f-valprint.o \
        m2-valprint.o \
-       ser-event.o serial.o mdebugread.o top.o utils.o \
+       ser-event.o serial.o mdebugread.o top.o utils.o utils-selftests.o \
        ui-file.o \
        user-regs.o \
        frame.o frame-unwind.o doublest.o \
index 5a346ec37d7ced7be762f38093492eebadbe2e1e..e112b62846033c9d5d6532e925419a88d9285569 100644 (file)
@@ -150,6 +150,29 @@ xsnprintf (char *str, size_t size, const char *format, ...)
   return ret;
 }
 
+/* See documentation in common-utils.h.  */
+
+std::string
+string_printf (const char* fmt, ...)
+{
+  va_list vp;
+  int size;
+
+  va_start (vp, fmt);
+  size = vsnprintf (NULL, 0, fmt, vp);
+  va_end (vp);
+
+  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);
+  vsprintf (&str[0], fmt, vp);
+  va_end (vp);
+
+  return str;
+}
+
 char *
 savestring (const char *ptr, size_t len)
 {
index 47def11a67576df04c49e1d0687b962809d444f9..a9053ffa48d6764fda52a30522109eaa53e2c1c4 100644 (file)
@@ -20,6 +20,8 @@
 #ifndef COMMON_UTILS_H
 #define COMMON_UTILS_H
 
+#include <string>
+
 /* If possible, define FUNCTION_NAME, a macro containing the name of
    the function being defined.  Since this macro may not always be
    defined, all uses must be protected by appropriate macro definition
@@ -56,6 +58,10 @@ char *xstrvprintf (const char *format, va_list ap)
 int xsnprintf (char *str, size_t size, const char *format, ...)
      ATTRIBUTE_PRINTF (3, 4);
 
+/* Returns a std::string built from a printf-style format string.  */
+std::string string_printf (const char* fmt, ...)
+  ATTRIBUTE_PRINTF (1, 2);
+
 /* Make a copy of the string at PTR with LEN characters
    (and add a null character at the end in the copy).
    Uses malloc to get the space.  Returns the address of the copy.  */
diff --git a/gdb/utils-selftests.c b/gdb/utils-selftests.c
new file mode 100644 (file)
index 0000000..f424655
--- /dev/null
@@ -0,0 +1,60 @@
+/* Self tests for general utility routines for GDB, the GNU debugger.
+
+   Copyright (C) 2016 Free Software Foundation, Inc.
+
+   This file is part of GDB.
+
+   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
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+#include "defs.h"
+#include "selftest.h"
+
+#if GDB_SELF_TEST
+
+namespace selftests {
+
+/* common-utils self tests.  Defined here instead of in
+   common/common-utils.c because that file is shared with
+   gdbserver.  */
+
+static void
+common_utils_tests (void)
+{
+  SELF_CHECK (string_printf ("%s", "") == "");
+  SELF_CHECK (string_printf ("%d comes before 2", 1) == "1 comes before 2");
+  SELF_CHECK (string_printf ("hello %s", "world") == "hello world");
+
+#define X10 "0123456789"
+#define X100 X10 X10 X10 X10 X10 X10 X10 X10 X10 X10
+#define X1000 X100 X100 X100 X100 X100 X100 X100 X100 X100 X100
+#define X10000 X1000 X1000 X1000 X1000 X1000 X1000 X1000 X1000 X1000 X1000
+#define X100000 X10000 X10000 X10000 X10000 X10000 X10000 X10000 X10000 X10000 X10000
+  SELF_CHECK (string_printf ("%s", X10) == X10);
+  SELF_CHECK (string_printf ("%s", X100) == X100);
+  SELF_CHECK (string_printf ("%s", X1000) == X1000);
+  SELF_CHECK (string_printf ("%s", X10000) == X10000);
+  SELF_CHECK (string_printf ("%s", X100000) == X100000);
+}
+
+} /* namespace selftests */
+
+#endif
+
+void
+_initialize_utils_selftests (void)
+{
+#if GDB_SELF_TEST
+  register_self_test (selftests::common_utils_tests);
+#endif
+}
This page took 0.040092 seconds and 4 git commands to generate.