From patchwork Fri Sep 29 02:59:54 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 23221 Received: (qmail 21569 invoked by alias); 29 Sep 2017 03:00:31 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 2275 invoked by uid 89); 29 Sep 2017 03:00:17 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-25.5 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, RCVD_IN_SORBS_SPAM, SPF_PASS autolearn=ham version=3.3.2 spammy= X-HELO: outbound-ss-1812.hostmonster.com Received: from gproxy1-pub.mail.unifiedlayer.com (HELO outbound-ss-1812.hostmonster.com) (69.89.25.95) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Fri, 29 Sep 2017 02:59:59 +0000 Received: from cmgw3 (cmgw4 [10.0.90.84]) by gproxy1.mail.unifiedlayer.com (Postfix) with ESMTP id E31E1175B89 for ; Thu, 28 Sep 2017 20:59:57 -0600 (MDT) Received: from box522.bluehost.com ([74.220.219.122]) by cmgw3 with id FEzu1w0092f2jeq01EzxSi; Thu, 28 Sep 2017 20:59:57 -0600 X-Authority-Analysis: v=2.2 cv=K/VSJ2eI c=1 sm=1 tr=0 a=GsOEXm/OWkKvwdLVJsfwcA==:117 a=GsOEXm/OWkKvwdLVJsfwcA==:17 a=2JCJgTwv5E4A:10 a=zstS-IiYAAAA:8 a=20KFwNOVAAAA:8 a=lCu_t_UPvTo87BH1uuoA:9 a=4G6NA9xxw8l3yy4pmD5M:22 Received: from 75-166-0-208.hlrn.qwest.net ([75.166.0.208]:35782 helo=bapiya) by box522.bluehost.com with esmtpsa (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.87) (envelope-from ) id 1dxlWs-001Ei7-QQ; Thu, 28 Sep 2017 20:59:54 -0600 From: Tom Tromey To: Tom Tromey Cc: Pedro Alves , gdb-patches@sourceware.org Subject: Re: [RFA 01/12] Introduce string_vprintf References: <20170928195011.27382-1-tom@tromey.com> <20170928195011.27382-2-tom@tromey.com> <877ewiw7ou.fsf@tromey.com> Date: Thu, 28 Sep 2017 20:59:54 -0600 In-Reply-To: <877ewiw7ou.fsf@tromey.com> (Tom Tromey's message of "Thu, 28 Sep 2017 20:38:09 -0600") Message-ID: <873776w6ol.fsf@tromey.com> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (gnu/linux) MIME-Version: 1.0 X-BWhitelist: no X-Exim-ID: 1dxlWs-001Ei7-QQ X-Source-Sender: 75-166-0-208.hlrn.qwest.net (bapiya) [75.166.0.208]:35782 X-Source-Auth: tom+tromey.com X-Email-Count: 4 X-Source-Cap: ZWx5bnJvYmk7ZWx5bnJvYmk7Ym94NTIyLmJsdWVob3N0LmNvbQ== X-Local-Domain: yes Tom> I didn't see that so I just made a new file. I pulled and saw the file, so I moved the new tests there. Tom diff --git a/gdb/ChangeLog b/gdb/ChangeLog index ab9250c..4cdae07 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,3 +1,11 @@ +2017-09-28 Tom Tromey + + * unittests/common-utils-selftests.c (format): New function. + (string_vprintf_tests): New function. + (_initialize_common_utils_selftests): Register new tests. + * common/common-utils.c (string_vprintf): New function. + * common/common-utils.h (string_vprintf): Declare. + 2017-09-28 Pedro Alves * Makefile.in (SUBDIR_UNITTESTS_SRCS): Add 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/common-utils-selftests.c b/gdb/unittests/common-utils-selftests.c index 71bc2df..0f26b21 100644 --- a/gdb/unittests/common-utils-selftests.c +++ b/gdb/unittests/common-utils-selftests.c @@ -41,10 +41,33 @@ string_printf_tests () SELF_CHECK (string_printf ("%s", X100000) == X100000); } +static 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; +} + +static void +string_vprintf_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"); +} + } /* namespace selftests */ void _initialize_common_utils_selftests () { selftests::register_test ("string_printf", selftests::string_printf_tests); + selftests::register_test ("string_vprintf", selftests::string_vprintf_tests); }