From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 516 invoked by alias); 14 Nov 2013 10:55:46 -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 460 invoked by uid 89); 14 Nov 2013 10:55:45 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=0.6 required=5.0 tests=AWL,BAYES_50,RDNS_NONE,URIBL_BLOCKED autolearn=no version=3.3.2 X-HELO: rock.gnat.com Received: from Unknown (HELO rock.gnat.com) (205.232.38.15) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES256-SHA encrypted) ESMTPS; Thu, 14 Nov 2013 10:55:16 +0000 Received: from localhost (localhost.localdomain [127.0.0.1]) by filtered-rock.gnat.com (Postfix) with ESMTP id A931911648A for ; Thu, 14 Nov 2013 05:55:40 -0500 (EST) Received: from rock.gnat.com ([127.0.0.1]) by localhost (rock.gnat.com [127.0.0.1]) (amavisd-new, port 10024) with LMTP id 1QMopVnVEW1c for ; Thu, 14 Nov 2013 05:55:40 -0500 (EST) Received: from joel.gnat.com (localhost.localdomain [127.0.0.1]) by rock.gnat.com (Postfix) with ESMTP id 50ACB116473 for ; Thu, 14 Nov 2013 05:55:39 -0500 (EST) Received: by joel.gnat.com (Postfix, from userid 1000) id D972DE1804; Thu, 14 Nov 2013 14:55:04 +0400 (RET) Date: Thu, 14 Nov 2013 10:55:00 -0000 From: Joel Brobecker To: gdb-patches@sourceware.org Subject: pushed: [RFA 2/3] New function cli-utils.c:extract_arg_const Message-ID: <20131114105504.GQ3481@adacore.com> References: <1384151855-12926-1-git-send-email-brobecker@adacore.com> <1384151855-12926-2-git-send-email-brobecker@adacore.com> <5283D42D.8000909@redhat.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="UoPmpPX/dBe4BELn" Content-Disposition: inline In-Reply-To: <5283D42D.8000909@redhat.com> User-Agent: Mutt/1.5.21 (2010-09-15) X-SW-Source: 2013-11/txt/msg00367.txt.bz2 --UoPmpPX/dBe4BELn Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-length: 698 > Instead of essentially duplicating the code, how about we reimplement > one on top of the other? See below. It compiles, but it's otherwise > untested. Indeed! I actually had started with something very similar, and backtracked, because other units failed to compile. But that was because I had also made the return value a "const" which, in hindsight, was no reason for me to backtrack the whole way! Attached is what I ended up pushing. gdb/ChangeLog: * cli/cli-utils.h (extract_arg_const): Add declaration. * cli/cli-utils.c (extract_arg_const): New function. (extract_arg): Reimplement using extract_arg_const. Tested on x86_64-linux. Thanks, Pedro. -- Joel --UoPmpPX/dBe4BELn Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="0001-New-function-cli-utils.c-extract_arg_const.patch" Content-length: 3329 >From b5be8ce022f894831b133b3b424238d8058eb29e Mon Sep 17 00:00:00 2001 From: Joel Brobecker Date: Mon, 11 Nov 2013 09:19:32 +0400 Subject: [PATCH 1/2] New function cli-utils.c:extract_arg_const This function provides the exact same functionality as extract_arg, except that it takes a "const char**" instead of a "char **". It allows us also to re-implement extract_arg almost as a simple wrapper around the new function. gdb/ChangeLog: Pedro Alves Joel Brobecker * cli/cli-utils.h (extract_arg_const): Add declaration. * cli/cli-utils.c (extract_arg_const): New function. (extract_arg): Reimplement using extract_arg_const. --- gdb/ChangeLog | 7 +++++++ gdb/cli/cli-utils.c | 25 +++++++++++++++++-------- gdb/cli/cli-utils.h | 7 +++++++ 3 files changed, 31 insertions(+), 8 deletions(-) diff --git a/gdb/ChangeLog b/gdb/ChangeLog index 64adfd2..2dc6cca 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,3 +1,10 @@ +2013-11-14 Pedro Alves + Joel Brobecker + + * cli/cli-utils.h (extract_arg_const): Add declaration. + * cli/cli-utils.c (extract_arg_const): New function. + (extract_arg): Reimplement using extract_arg_const. + 2013-11-14 Joel Brobecker * language.h: Add "symtab.h" #include. diff --git a/gdb/cli/cli-utils.c b/gdb/cli/cli-utils.c index f74e6b1..316cf4f 100644 --- a/gdb/cli/cli-utils.c +++ b/gdb/cli/cli-utils.c @@ -261,30 +261,39 @@ remove_trailing_whitespace (const char *start, char *s) /* See documentation in cli-utils.h. */ char * -extract_arg (char **arg) +extract_arg_const (const char **arg) { - char *result, *copy; + const char *result; if (!*arg) return NULL; /* Find the start of the argument. */ - *arg = skip_spaces (*arg); + *arg = skip_spaces_const (*arg); if (!**arg) return NULL; result = *arg; /* Find the end of the argument. */ - *arg = skip_to_space (*arg + 1); + *arg = skip_to_space_const (*arg + 1); if (result == *arg) return NULL; - copy = xmalloc (*arg - result + 1); - memcpy (copy, result, *arg - result); - copy[*arg - result] = '\0'; + return savestring (result, *arg - result); +} + +/* See documentation in cli-utils.h. */ + +char * +extract_arg (char **arg) +{ + const char *arg_const = *arg; + char *result; - return copy; + result = extract_arg_const (&arg_const); + *arg += arg_const - *arg; + return result; } /* See documentation in cli-utils.h. */ diff --git a/gdb/cli/cli-utils.h b/gdb/cli/cli-utils.h index 152fb89..ebae2d2 100644 --- a/gdb/cli/cli-utils.h +++ b/gdb/cli/cli-utils.h @@ -118,6 +118,13 @@ extern char *remove_trailing_whitespace (const char *start, char *s); extern char *extract_arg (char **arg); +/* A const-correct version of "extract_arg". + + Since the returned value is xmalloc'd, it eventually needs to be + xfree'ed, which prevents us from making it const as well. */ + +extern char *extract_arg_const (const char **arg); + /* A helper function that looks for an argument at the start of a string. The argument must also either be at the end of the string, or be followed by whitespace. Returns 1 if it finds the argument, -- 1.8.1.2 --UoPmpPX/dBe4BELn--