From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 36517 invoked by alias); 8 Nov 2016 15:35:44 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Received: (qmail 36507 invoked by uid 89); 8 Nov 2016 15:35:43 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-4.8 required=5.0 tests=BAYES_00,RP_MATCHES_RCVD,SPF_HELO_PASS autolearn=ham version=3.3.2 spammy=x100, X100 X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Tue, 08 Nov 2016 15:35:33 +0000 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id F11F17AE9B; Tue, 8 Nov 2016 15:35:31 +0000 (UTC) Received: from [127.0.0.1] (ovpn01.gateway.prod.ext.ams2.redhat.com [10.39.146.11]) by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id uA8FZT9w023980; Tue, 8 Nov 2016 10:35:30 -0500 Subject: Re: [PATCH v2 01/31] Introduce string_printf To: Simon Marchi References: <1476839539-8374-1-git-send-email-palves@redhat.com> <1476839539-8374-2-git-send-email-palves@redhat.com> <8d8aa4e4f950dad9474f3fe17a14eee7@simark.ca> <9079091d-b838-b687-6971-9d49ced34a59@redhat.com> Cc: gdb-patches@sourceware.org From: Pedro Alves Message-ID: <22b8d24b-ad2a-89d2-e6df-b4f098c79269@redhat.com> Date: Tue, 08 Nov 2016 15:35:00 -0000 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.2.0 MIME-Version: 1.0 In-Reply-To: <9079091d-b838-b687-6971-9d49ced34a59@redhat.com> Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: 7bit X-SW-Source: 2016-11/txt/msg00172.txt.bz2 On 10/19/2016 10:02 PM, Pedro Alves wrote: > And then thinking that people might dislike the hack, I changed > it to consider the \0 as part of the initial string (std::string > can contain embedded NULLs). > > But I shouldn't have. The "hack" is really not a problem: > > https://herbsutter.com/2008/04/07/cringe-not-vectors-are-guaranteed-to-be-contiguous/#comment-483 > > I.e., "everyone" does it like that. Now that we assume C++11, it's no longer a hack. Below's the version that I pushed in. This version also tweaks the selftests a bit: - put them in a namespace. - use string comparison instead of just checking the size. >From d4081a383e28db26c65298f7405554d4312b1342 Mon Sep 17 00:00:00 2001 From: Pedro Alves Date: Tue, 8 Nov 2016 15:26:42 +0000 Subject: [PATCH] Introduce string_printf This introduces the string_printf function. Like asprintf, but returns a std::string. gdb/ChangeLog: 2016-11-08 Pedro Alves * Makefile.in (COMMON_OBS): Add utils-selftests.o. * common/common-utils.c (string_printf): New function. * common/common-utils.h: Include . (string_printf): Declare. * utils-selftests.c: New file. --- gdb/ChangeLog | 8 +++++++ gdb/Makefile.in | 2 +- gdb/common/common-utils.c | 23 ++++++++++++++++++ gdb/common/common-utils.h | 6 +++++ gdb/utils-selftests.c | 60 +++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 gdb/utils-selftests.c diff --git a/gdb/ChangeLog b/gdb/ChangeLog index cc7f9a0..1b647bb 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,3 +1,11 @@ +2016-11-08 Pedro Alves + + * Makefile.in (COMMON_OBS): Add utils-selftests.o. + * common/common-utils.c (string_printf): New function. + * common/common-utils.h: Include . + (string_printf): Declare. + * utils-selftests.c: New file. + 2016-11-08 Yao Qi * aarch64-tdep.c (aarch64_software_single_step): Return diff --git a/gdb/Makefile.in b/gdb/Makefile.in index 6db63c7..3876cd9 100644 --- a/gdb/Makefile.in +++ b/gdb/Makefile.in @@ -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 \ diff --git a/gdb/common/common-utils.c b/gdb/common/common-utils.c index 5a346ec..e112b62 100644 --- a/gdb/common/common-utils.c +++ b/gdb/common/common-utils.c @@ -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) { diff --git a/gdb/common/common-utils.h b/gdb/common/common-utils.h index 47def11..a9053ff 100644 --- a/gdb/common/common-utils.h +++ b/gdb/common/common-utils.h @@ -20,6 +20,8 @@ #ifndef COMMON_UTILS_H #define COMMON_UTILS_H +#include + /* 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 index 0000000..f424655 --- /dev/null +++ b/gdb/utils-selftests.c @@ -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 . */ + +#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 +} -- 2.5.5