From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 102359 invoked by alias); 11 Sep 2017 19:52:18 -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 101627 invoked by uid 89); 11 Sep 2017 19:52:17 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-24.9 required=5.0 tests=AWL,BAYES_00,GIT_PATCH_0,GIT_PATCH_1,GIT_PATCH_2,GIT_PATCH_3,RP_MATCHES_RCVD,SPF_HELO_PASS,SPF_PASS autolearn=ham version=3.3.2 spammy=sentence X-HELO: smtp.polymtl.ca Received: from smtp.polymtl.ca (HELO smtp.polymtl.ca) (132.207.4.11) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Mon, 11 Sep 2017 19:52:16 +0000 Received: from simark.ca (simark.ca [158.69.221.121]) (authenticated bits=0) by smtp.polymtl.ca (8.14.7/8.14.7) with ESMTP id v8BJq9pL020468 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Mon, 11 Sep 2017 15:52:14 -0400 Received: by simark.ca (Postfix, from userid 112) id 588181ECDF; Mon, 11 Sep 2017 15:52:09 -0400 (EDT) Received: from simark.ca (localhost [127.0.0.1]) by simark.ca (Postfix) with ESMTP id DB8421EA19; Mon, 11 Sep 2017 15:51:57 -0400 (EDT) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII; format=flowed Content-Transfer-Encoding: 7bit Date: Mon, 11 Sep 2017 19:52:00 -0000 From: Simon Marchi To: Tom Tromey Cc: gdb-patches@sourceware.org Subject: Re: [RFA 3/3] Make extract_arg return a std::string In-Reply-To: <20170911003325.3765-4-tom@tromey.com> References: <20170911003325.3765-1-tom@tromey.com> <20170911003325.3765-4-tom@tromey.com> Message-ID: <31794437bc5e56865f80cc72691899a4@polymtl.ca> X-Sender: simon.marchi@polymtl.ca User-Agent: Roundcube Webmail/1.3.0 X-Poly-FromMTA: (simark.ca [158.69.221.121]) at Mon, 11 Sep 2017 19:52:09 +0000 X-IsSubscribed: yes X-SW-Source: 2017-09/txt/msg00311.txt.bz2 On 2017-09-11 02:33, Tom Tromey wrote: > Change extract_arg to return a std::string and fix up all the users. > I think string is mildly better than unique_xmalloc_ptr, when > possible, because it provides a more robust API. I agree, for strings we allocate dynamically ourselves, it makes sense to use std::string. For dynamically allocated strings coming from external APIs we use, then unique_xmalloc_ptr make sense, since it doesn't require doing a copy. For things that are always constant/literal strings, the const char *. > I changed the error messages emitted from find_location_by_number to > avoid either writing to a string or an extra allocation; this can be > changed but I thought that the new message was not any less clear. > You can see an example in the testsuite patch. I made almost the same patch (although unpolished): https://github.com/simark/binutils-gdb/commit/afac80f165a016ca2089169efe7dd266aa8a0ddc and made pretty much the same choices, so that's fine with me. I just have some small comments, the patch is ok to me with those fixed. > diff --git a/gdb/break-catch-sig.c b/gdb/break-catch-sig.c > index 805084f..237dbaa 100644 > --- a/gdb/break-catch-sig.c > +++ b/gdb/break-catch-sig.c > @@ -343,12 +343,12 @@ catch_signal_split_args (char *arg, bool > *catch_all) > gdb_signal signal_number; > char *endptr; > > - gdb::unique_xmalloc_ptr one_arg (extract_arg (&arg)); > - if (one_arg == NULL) > + std::string one_arg = extract_arg (&arg); > + if (one_arg.empty ()) > break; > > /* Check for the special flag "all". */ > - if (strcmp (one_arg.get (), "all") == 0) > + if (strcmp (one_arg.c_str (), "all") == 0) one_arg == "all" > diff --git a/gdb/cli/cli-utils.h b/gdb/cli/cli-utils.h > index 038ddad..c17b4dd 100644 > --- a/gdb/cli/cli-utils.h > +++ b/gdb/cli/cli-utils.h > @@ -149,17 +149,14 @@ remove_trailing_whitespace (const char *start, > char *s) > } > > /* A helper function to extract an argument from *ARG. An argument is > - delimited by whitespace. The return value is either NULL if no > - argument was found, or an xmalloc'd string. */ > + delimited by whitespace. The return value is either empty if no "either" does not make sense in the new sentence. > + argument was found. */ > > -extern char *extract_arg (char **arg); > +extern std::string extract_arg (char **arg); > > -/* A const-correct version of "extract_arg". > +/* A const-correct version of "extract_arg". */ It's a bit funny to refer to extract_arg when this function is also named extract_arg. What about: "A const-correct version of the above." ? > @@ -553,16 +556,14 @@ exists_probe_with_pops (VEC (bound_probe_s) > *probes, > [PROBE [OBJNAME]]] from the provided string STR. */ > > static void > -parse_probe_linespec (const char *str, char **provider, > - char **probe_name, char **objname) > +parse_probe_linespec (const char *str, std::string *provider, > + std::string *probe_name, std::string *objname) > { > - *probe_name = *objname = NULL; Here, I would keep the clearing of probe_name and objname (calling .clear ()), just in case a user of parse_probe_linespec decides to re-use string objects. It then couldn't know if parse_probe_linespec set the value, or if it's the old value. Simon