From patchwork Fri Feb 16 23:02:12 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 25951 Received: (qmail 43719 invoked by alias); 16 Feb 2018 23:02:20 -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 43709 invoked by uid 89); 16 Feb 2018 23:02:20 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-25.7 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, SPF_HELO_PASS autolearn=ham version=3.3.2 spammy= X-HELO: gateway32.websitewelcome.com Received: from gateway32.websitewelcome.com (HELO gateway32.websitewelcome.com) (192.185.145.171) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Fri, 16 Feb 2018 23:02:17 +0000 Received: from cm16.websitewelcome.com (cm16.websitewelcome.com [100.42.49.19]) by gateway32.websitewelcome.com (Postfix) with ESMTP id 7755D18343 for ; Fri, 16 Feb 2018 17:02:16 -0600 (CST) Received: from box5379.bluehost.com ([162.241.216.53]) by cmsmtp with SMTP id mp1Eex8kfODN4mp1EepdBM; Fri, 16 Feb 2018 17:02:16 -0600 Received: from 174-29-60-18.hlrn.qwest.net ([174.29.60.18]:48332 helo=bapiya.Home) by box5379.bluehost.com with esmtpsa (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.89) (envelope-from ) id 1emp1E-000Msw-5s; Fri, 16 Feb 2018 17:02:16 -0600 From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [RFA] Remove a cleanup from parse_expression_for_completion Date: Fri, 16 Feb 2018 16:02:12 -0700 Message-Id: <20180216230212.18553-1-tom@tromey.com> X-BWhitelist: no X-Source-L: No X-Exim-ID: 1emp1E-000Msw-5s X-Source-Sender: 174-29-60-18.hlrn.qwest.net (bapiya.Home) [174.29.60.18]:48332 X-Source-Auth: tom+tromey.com X-Email-Count: 1 X-Source-Cap: ZWx5bnJvYmk7ZWx5bnJvYmk7Ym94NTM3OS5ibHVlaG9zdC5jb20= X-Local-Domain: yes This removes a cleanup from parse_expression_for_completion, by changing various expression-completion functions to use std::string rather than explicit malloc+free. Regression tested by the buildbot. gdb/ChangeLog 2018-02-16 Tom Tromey * value.h: (extract_field_op): Update. * eval.c (extract_field_op): Return a const char *. * expression.h (parse_expression_for_completion): Update. * completer.c (complete_expression): Update. (add_struct_fields): Make fieldname const. * parse.c (expout_completion_name): Now a std::string. (mark_completion_tag, parse_exp_in_context_1): Update. (parse_expression_for_completion): Change "name" to std::string*. --- gdb/ChangeLog | 11 +++++++++++ gdb/completer.c | 22 ++++++++-------------- gdb/eval.c | 2 +- gdb/expression.h | 3 ++- gdb/parse.c | 20 +++++++------------- gdb/value.h | 2 +- 6 files changed, 30 insertions(+), 30 deletions(-) diff --git a/gdb/completer.c b/gdb/completer.c index a71bd368ff..c0d5778867 100644 --- a/gdb/completer.c +++ b/gdb/completer.c @@ -962,7 +962,7 @@ location_completer_handle_brkchars (struct cmd_list_element *ignore, static void add_struct_fields (struct type *type, completion_list &output, - char *fieldname, int namelen) + const char *fieldname, int namelen) { int i; int computed_type_name = 0; @@ -1016,12 +1016,11 @@ complete_expression (completion_tracker &tracker, const char *text, const char *word) { struct type *type = NULL; - char *fieldname; + std::string fieldname; enum type_code code = TYPE_CODE_UNDEF; /* Perform a tentative parse of the expression, to see whether a field completion is required. */ - fieldname = NULL; TRY { type = parse_expression_for_completion (text, &fieldname, &code); @@ -1032,7 +1031,7 @@ complete_expression (completion_tracker &tracker, } END_CATCH - if (fieldname && type) + if (!fieldname.empty () && type) { for (;;) { @@ -1045,25 +1044,20 @@ complete_expression (completion_tracker &tracker, if (TYPE_CODE (type) == TYPE_CODE_UNION || TYPE_CODE (type) == TYPE_CODE_STRUCT) { - int flen = strlen (fieldname); completion_list result; - add_struct_fields (type, result, fieldname, flen); - xfree (fieldname); + add_struct_fields (type, result, fieldname.c_str (), + fieldname.size ()); tracker.add_completions (std::move (result)); return; } } - else if (fieldname && code != TYPE_CODE_UNDEF) + else if (!fieldname.empty () && code != TYPE_CODE_UNDEF) { - struct cleanup *cleanup = make_cleanup (xfree, fieldname); - - collect_symbol_completion_matches_type (tracker, fieldname, fieldname, - code); - do_cleanups (cleanup); + collect_symbol_completion_matches_type (tracker, fieldname.c_str (), + fieldname.c_str (), code); return; } - xfree (fieldname); complete_files_symbols (tracker, text, word); } diff --git a/gdb/eval.c b/gdb/eval.c index 6f74c41b9f..4899011a58 100644 --- a/gdb/eval.c +++ b/gdb/eval.c @@ -272,7 +272,7 @@ fetch_subexp_value (struct expression *exp, int *pc, struct value **valp, subexpression of the left-hand-side of the dereference. This is used when completing field names. */ -char * +const char * extract_field_op (struct expression *exp, int *subexp) { int tem; diff --git a/gdb/expression.h b/gdb/expression.h index 030f2f08e7..f9b5b1e820 100644 --- a/gdb/expression.h +++ b/gdb/expression.h @@ -101,7 +101,8 @@ extern expression_up parse_expression (const char *); extern expression_up parse_expression_with_language (const char *string, enum language lang); -extern struct type *parse_expression_for_completion (const char *, char **, +extern struct type *parse_expression_for_completion (const char *, + std::string *, enum type_code *); extern expression_up parse_exp_1 (const char **, CORE_ADDR pc, diff --git a/gdb/parse.c b/gdb/parse.c index e3f1306a17..1d1ede4f56 100644 --- a/gdb/parse.c +++ b/gdb/parse.c @@ -88,7 +88,7 @@ static int expout_last_struct = -1; static enum type_code expout_tag_completion_type = TYPE_CODE_UNDEF; /* The token for tagged type name completion. */ -static char *expout_completion_name; +static std::string expout_completion_name; static unsigned int expressiondebug = 0; @@ -569,15 +569,13 @@ mark_completion_tag (enum type_code tag, const char *ptr, int length) { gdb_assert (parse_completion && expout_tag_completion_type == TYPE_CODE_UNDEF - && expout_completion_name == NULL + && expout_completion_name.empty () && expout_last_struct == -1); gdb_assert (tag == TYPE_CODE_UNION || tag == TYPE_CODE_STRUCT || tag == TYPE_CODE_ENUM); expout_tag_completion_type = tag; - expout_completion_name = (char *) xmalloc (length + 1); - memcpy (expout_completion_name, ptr, length); - expout_completion_name[length] = '\0'; + expout_completion_name = std::string (ptr, length); } @@ -1137,8 +1135,7 @@ parse_exp_in_context_1 (const char **stringptr, CORE_ADDR pc, type_stack.depth = 0; expout_last_struct = -1; expout_tag_completion_type = TYPE_CODE_UNDEF; - xfree (expout_completion_name); - expout_completion_name = NULL; + expout_completion_name = ""; comma_terminates = comma; @@ -1281,7 +1278,7 @@ parse_expression_with_language (const char *string, enum language lang) *NAME must be freed by the caller. */ struct type * -parse_expression_for_completion (const char *string, char **name, +parse_expression_for_completion (const char *string, std::string *name, enum type_code *code) { expression_up exp; @@ -1306,8 +1303,7 @@ parse_expression_for_completion (const char *string, char **name, if (expout_tag_completion_type != TYPE_CODE_UNDEF) { *code = expout_tag_completion_type; - *name = expout_completion_name; - expout_completion_name = NULL; + *name = std::move (expout_completion_name); return NULL; } @@ -1315,14 +1311,12 @@ parse_expression_for_completion (const char *string, char **name, return NULL; *name = extract_field_op (exp.get (), &subexp); - if (!*name) + if (name->empty ()) return NULL; /* This might throw an exception. If so, we want to let it propagate. */ val = evaluate_subexpression_type (exp.get (), subexp); - /* (*NAME) is a part of the EXP memory block freed below. */ - *name = xstrdup (*name); return value_type (val); } diff --git a/gdb/value.h b/gdb/value.h index e0ea22d4e5..5676d245b9 100644 --- a/gdb/value.h +++ b/gdb/value.h @@ -889,7 +889,7 @@ extern void fetch_subexp_value (struct expression *exp, int *pc, struct value **val_chain, int preserve_errors); -extern char *extract_field_op (struct expression *exp, int *subexp); +extern const char *extract_field_op (struct expression *exp, int *subexp); extern struct value *evaluate_subexp_with_coercion (struct expression *, int *, enum noside);