Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Tom Tromey <tom@tromey.com>
To: Pedro Alves <palves@redhat.com>
Cc: Tom Tromey <tom@tromey.com>,  gdb-patches@sourceware.org
Subject: Re: [RFA 01/12] Introduce string_vprintf
Date: Fri, 29 Sep 2017 02:38:00 -0000	[thread overview]
Message-ID: <877ewiw7ou.fsf@tromey.com> (raw)
In-Reply-To: <bc230250-fa6f-76f7-82a6-01a901214768@redhat.com> (Pedro Alves's	message of "Thu, 28 Sep 2017 22:53:53 +0100")

Pedro> This is OK, but IWBN to add some unittest to
Pedro> gdb/unittests/utils-selftests.c.

I didn't see that so I just made a new file.
There isn't much there; I didn't think it made sense to write a
comprehensive printf test suite for this function.  I can add more if
you think this isn't enough.

Tom

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index 2120df6..9c0f6b6 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,3 +1,11 @@
+2017-09-28  Tom Tromey  <tom@tromey.com>
+
+	* Makefile.in (SUBDIR_UNITTESTS_SRCS): Add new file.
+	(SUBDIR_UNITTESTS_OBS): Add new file.
+	* unittests/string-vprintf-selftests.c: New file.
+	* common/common-utils.c (string_vprintf): New function.
+	* common/common-utils.h (string_vprintf): Declare.
+
 2017-09-28  Alexander Shaposhnikov <alexander.v.shaposhnikov@gmail.com> (tiny patch)
 
 	* dwarf2read.c (open_and_init_dwp_file): Protect against dwp_file
diff --git a/gdb/Makefile.in b/gdb/Makefile.in
index 9004b35..ff86ce9 100644
--- a/gdb/Makefile.in
+++ b/gdb/Makefile.in
@@ -533,6 +533,7 @@ SUBDIR_UNITTESTS_SRCS = \
 	unittests/optional-selftests.c \
 	unittests/ptid-selftests.c \
 	unittests/scoped_restore-selftests.c \
+	unittests/string-vprintf-selftests.c \
 	unittests/xml-utils-selftests.c
 
 SUBDIR_UNITTESTS_OBS = \
@@ -543,6 +544,7 @@ SUBDIR_UNITTESTS_OBS = \
 	optional-selftests.o \
 	ptid-selftests.o \
 	scoped_restore-selftests.o \
+	string-vprintf-selftests.o \
 	xml-utils-selftests.o
 
 # Opcodes currently live in one of two places.  Either they are in the
diff --git a/gdb/common/common-utils.c b/gdb/common/common-utils.c
index 6b10d11..d8c546a 100644
--- a/gdb/common/common-utils.c
+++ b/gdb/common/common-utils.c
@@ -174,6 +174,27 @@ string_printf (const char* fmt, ...)
   return str;
 }
 
+/* See documentation in common-utils.h.  */
+
+std::string
+string_vprintf (const char* fmt, va_list args)
+{
+  va_list vp;
+  size_t size;
+
+  va_copy (vp, args);
+  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'.  */
+  vsprintf (&str[0], fmt, args);
+
+  return str;
+}
+
 char *
 savestring (const char *ptr, size_t len)
 {
diff --git a/gdb/common/common-utils.h b/gdb/common/common-utils.h
index 6475c28..19724f9 100644
--- a/gdb/common/common-utils.h
+++ b/gdb/common/common-utils.h
@@ -63,6 +63,10 @@ int xsnprintf (char *str, size_t size, const char *format, ...)
 std::string string_printf (const char* fmt, ...)
   ATTRIBUTE_PRINTF (1, 2);
 
+/* Like string_printf, but takes a va_list.  */
+std::string string_vprintf (const char* fmt, va_list args)
+  ATTRIBUTE_PRINTF (1, 0);
+
 /* 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/unittests/string-vprintf-selftests.c b/gdb/unittests/string-vprintf-selftests.c
new file mode 100644
index 0000000..b84d0a5
--- /dev/null
+++ b/gdb/unittests/string-vprintf-selftests.c
@@ -0,0 +1,57 @@
+/* Self tests for string_vprintf for GDB, the GNU debugger.
+
+   Copyright (C) 2017 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"
+#include "common/common-utils.h"
+
+namespace selftests {
+namespace string_vprintf {
+
+std::string
+format (const char *fmt, ...)
+{
+  va_list vp;
+
+  va_start (vp, fmt);
+  std::string result = ::string_vprintf (fmt, vp);
+  va_end (vp);
+  return result;
+}
+
+void
+run_tests ()
+{
+  /* Basic smoke tests.  */
+  SELF_CHECK (format ("%s", "test") == "test");
+  SELF_CHECK (format ("%d", 23) == "23");
+  SELF_CHECK (format ("%s %d %s", "test", 23, "done")
+	      == "test 23 done");
+  SELF_CHECK (format ("nothing") == "nothing");
+}
+
+}
+}
+
+void
+_initialize_string_vprintf_selftests ()
+{
+  selftests::register_test ("string_vprintf",
+			    selftests::string_vprintf::run_tests);
+}


  reply	other threads:[~2017-09-29  2:38 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-09-28 19:50 [RFA 00/12] some minor cleanup removals Tom Tromey
2017-09-28 19:50 ` [RFA 08/12] Remove cleanup from complaints.c Tom Tromey
2017-09-28 22:09   ` Pedro Alves
2017-09-28 19:50 ` [RFA 01/12] Introduce string_vprintf Tom Tromey
2017-09-28 21:53   ` Pedro Alves
2017-09-29  2:38     ` Tom Tromey [this message]
2017-09-29  3:00       ` Tom Tromey
2017-09-29 10:25         ` Pedro Alves
2017-09-28 19:50 ` [RFA 12/12] Remove some unused declarations Tom Tromey
2017-09-28 19:50 ` [RFA 03/12] Remove cleanups from utils.c Tom Tromey
2017-09-28 22:00   ` Pedro Alves
2017-09-28 19:50 ` [RFA 10/12] Remove cleanup from mt-tdep.c Tom Tromey
2017-09-28 19:50 ` [RFA 05/12] Remove some cleanups from stack.c Tom Tromey
2017-09-28 22:06   ` Pedro Alves
2017-09-28 19:50 ` [RFA 06/12] Remove cleanups from cp-support.c Tom Tromey
2017-09-28 19:50 ` [RFA 09/12] Remove cleanup from xstormy16-tdep.c Tom Tromey
2017-09-28 19:50 ` [RFA 02/12] Remove cleanup from display_gdb_prompt Tom Tromey
2017-09-28 21:56   ` Pedro Alves
2017-09-28 19:50 ` [RFA 11/12] Remove a cleanup from symtab.c Tom Tromey
2017-09-28 19:50 ` [RFA 04/12] Remove cleanup from tilegx-tdep.c Tom Tromey
2017-09-28 22:00   ` Pedro Alves
2017-09-28 20:11 ` [RFA 07/12] Remove some cleanups from tracepoint.c Tom Tromey
2017-09-28 21:04 ` [RFA 00/12] some minor cleanup removals Sergio Durigan Junior
2017-09-28 22:11   ` Pedro Alves

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=877ewiw7ou.fsf@tromey.com \
    --to=tom@tromey.com \
    --cc=gdb-patches@sourceware.org \
    --cc=palves@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox