From patchwork Sun May 27 15:20:09 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 27521 Received: (qmail 59765 invoked by alias); 27 May 2018 15:20:18 -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 59670 invoked by uid 89); 27 May 2018 15:20:17 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-26.1 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=showing, strategy, Space X-HELO: gateway34.websitewelcome.com Received: from gateway34.websitewelcome.com (HELO gateway34.websitewelcome.com) (192.185.148.204) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Sun, 27 May 2018 15:20:15 +0000 Received: from cm13.websitewelcome.com (cm13.websitewelcome.com [100.42.49.6]) by gateway34.websitewelcome.com (Postfix) with ESMTP id 488598D047 for ; Sun, 27 May 2018 10:20:14 -0500 (CDT) Received: from box5379.bluehost.com ([162.241.216.53]) by cmsmtp with SMTP id MxSwfymYm79N3MxSwfUbVY; Sun, 27 May 2018 10:20:14 -0500 X-Authority-Reason: nr=8 Received: from 174-29-44-154.hlrn.qwest.net ([174.29.44.154]:51012 helo=bapiya.Home) by box5379.bluehost.com with esmtpsa (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.91) (envelope-from ) id 1fMxSv-004Ahx-RA; Sun, 27 May 2018 10:20:14 -0500 From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [RFA 4/4] Return unique_xmalloc_ptr from gdbscm_scm_to_string Date: Sun, 27 May 2018 09:20:09 -0600 Message-Id: <20180527152009.4228-5-tom@tromey.com> In-Reply-To: <20180527152009.4228-1-tom@tromey.com> References: <20180527152009.4228-1-tom@tromey.com> X-BWhitelist: no X-Source-L: No X-Exim-ID: 1fMxSv-004Ahx-RA X-Source-Sender: 174-29-44-154.hlrn.qwest.net (bapiya.Home) [174.29.44.154]:51012 X-Source-Auth: tom+tromey.com X-Email-Count: 5 X-Source-Cap: ZWx5bnJvYmk7ZWx5bnJvYmk7Ym94NTM3OS5ibHVlaG9zdC5jb20= X-Local-Domain: yes This changes gdbscm_scm_to_string to return a unique_xmalloc_ptr and then fixes all the callers. This allows for the removal of some cleanups. ChangeLog 2018-05-26 Tom Tromey * guile/scm-param.c (pascm_set_func, pascm_show_func) (compute_enum_list, pascm_set_param_value_x) (gdbscm_parameter_value): Update. * guile/guile-internal.h (gdbscm_scm_to_string): Update. (gdbscm_scm_to_host_string): Update. * guile/scm-math.c (vlscm_convert_typed_value_from_scheme): Update. * guile/scm-cmd.c (cmdscm_add_completion): Update. * guile/scm-pretty-print.c (ppscm_print_string_repr): Update. * guile/scm-string.c (gdbscm_scm_to_string): Return unique_xmalloc_ptr. (gdbscm_scm_to_host_string): Likewise. --- gdb/ChangeLog | 15 +++++++++++++++ gdb/guile/guile-internal.h | 8 ++++---- gdb/guile/scm-cmd.c | 4 ++-- gdb/guile/scm-math.c | 15 +++++---------- gdb/guile/scm-param.c | 43 ++++++++++++++++++------------------------- gdb/guile/scm-pretty-print.c | 11 ++++------- gdb/guile/scm-string.c | 8 +++----- 7 files changed, 51 insertions(+), 53 deletions(-) diff --git a/gdb/guile/guile-internal.h b/gdb/guile/guile-internal.h index 2bf0cf72b2..7289d25cf1 100644 --- a/gdb/guile/guile-internal.h +++ b/gdb/guile/guile-internal.h @@ -526,14 +526,14 @@ extern SCM gdbscm_scm_from_c_string (const char *string); extern SCM gdbscm_scm_from_printf (const char *format, ...) ATTRIBUTE_PRINTF (1, 2); -extern char *gdbscm_scm_to_string (SCM string, size_t *lenp, - const char *charset, - int strict, SCM *except_scmp); +extern gdb::unique_xmalloc_ptr gdbscm_scm_to_string + (SCM string, size_t *lenp, const char *charset, int strict, SCM *except_scmp); extern SCM gdbscm_scm_from_string (const char *string, size_t len, const char *charset, int strict); -extern char *gdbscm_scm_to_host_string (SCM string, size_t *lenp, SCM *except); +extern gdb::unique_xmalloc_ptr gdbscm_scm_to_host_string + (SCM string, size_t *lenp, SCM *except); extern SCM gdbscm_scm_from_host_string (const char *string, size_t len); diff --git a/gdb/guile/scm-cmd.c b/gdb/guile/scm-cmd.c index 8bb46622a9..88a98643a9 100644 --- a/gdb/guile/scm-cmd.c +++ b/gdb/guile/scm-cmd.c @@ -362,8 +362,8 @@ cmdscm_add_completion (SCM completion, completion_tracker &tracker) } gdb::unique_xmalloc_ptr item - (gdbscm_scm_to_string (completion, NULL, host_charset (), 1, - &except_scm)); + = gdbscm_scm_to_string (completion, NULL, host_charset (), 1, + &except_scm); if (item == NULL) { /* Inform the user, but otherwise ignore the entire result. */ diff --git a/gdb/guile/scm-math.c b/gdb/guile/scm-math.c index 750b6bb415..5507dd7d89 100644 --- a/gdb/guile/scm-math.c +++ b/gdb/guile/scm-math.c @@ -826,7 +826,6 @@ vlscm_convert_typed_value_from_scheme (const char *func_name, } else if (scm_is_string (obj)) { - char *s; size_t len; struct cleanup *cleanup; @@ -840,19 +839,15 @@ vlscm_convert_typed_value_from_scheme (const char *func_name, else { /* TODO: Provide option to specify conversion strategy. */ - s = gdbscm_scm_to_string (obj, &len, + gdb::unique_xmalloc_ptr s + = gdbscm_scm_to_string (obj, &len, target_charset (gdbarch), 0 /*non-strict*/, &except_scm); if (s != NULL) - { - cleanup = make_cleanup (xfree, s); - value - = value_cstring (s, len, - language_string_char_type (language, - gdbarch)); - do_cleanups (cleanup); - } + value = value_cstring (s.get (), len, + language_string_char_type (language, + gdbarch)); else value = NULL; } diff --git a/gdb/guile/scm-param.c b/gdb/guile/scm-param.c index 7ff4af9501..29ebf0ec69 100644 --- a/gdb/guile/scm-param.c +++ b/gdb/guile/scm-param.c @@ -273,8 +273,6 @@ pascm_set_func (const char *args, int from_tty, struct cmd_list_element *c) { param_smob *p_smob = (param_smob *) get_cmd_context (c); SCM self, result, exception; - char *msg; - struct cleanup *cleanups; gdb_assert (gdbscm_is_procedure (p_smob->set_func)); @@ -291,18 +289,17 @@ pascm_set_func (const char *args, int from_tty, struct cmd_list_element *c) if (!scm_is_string (result)) error (_("Result of %s set-func is not a string."), p_smob->name); - msg = gdbscm_scm_to_host_string (result, NULL, &exception); + gdb::unique_xmalloc_ptr msg = gdbscm_scm_to_host_string (result, NULL, + &exception); if (msg == NULL) { gdbscm_print_gdb_exception (SCM_BOOL_F, exception); error (_("Error converting show text to host string.")); } - cleanups = make_cleanup (xfree, msg); /* GDB is usually silent when a parameter is set. */ - if (*msg != '\0') - fprintf_filtered (gdb_stdout, "%s\n", msg); - do_cleanups (cleanups); + if (*msg.get () != '\0') + fprintf_filtered (gdb_stdout, "%s\n", msg.get ()); } /* A callback function that is registered against the respective @@ -316,8 +313,6 @@ pascm_show_func (struct ui_file *file, int from_tty, { param_smob *p_smob = (param_smob *) get_cmd_context (c); SCM value_scm, self, result, exception; - char *msg; - struct cleanup *cleanups; gdb_assert (gdbscm_is_procedure (p_smob->show_func)); @@ -338,16 +333,15 @@ pascm_show_func (struct ui_file *file, int from_tty, _("Error occurred showing parameter.")); } - msg = gdbscm_scm_to_host_string (result, NULL, &exception); + gdb::unique_xmalloc_ptr msg = gdbscm_scm_to_host_string (result, NULL, + &exception); if (msg == NULL) { gdbscm_print_gdb_exception (SCM_BOOL_F, exception); error (_("Error converting show text to host string.")); } - cleanups = make_cleanup (xfree, msg); - fprintf_filtered (file, "%s\n", msg); - do_cleanups (cleanups); + fprintf_filtered (file, "%s\n", msg.get ()); } /* A helper function that dispatches to the appropriate add_setshow @@ -516,7 +510,8 @@ compute_enum_list (SCM enum_values_scm, int arg_pos, const char *func_name) freeargv (enum_values); SCM_ASSERT_TYPE (0, value, arg_pos, func_name, _("string")); } - enum_values[i] = gdbscm_scm_to_host_string (value, NULL, &exception); + enum_values[i] = gdbscm_scm_to_host_string (value, NULL, + &exception).release (); if (enum_values[i] == NULL) { freeargv (enum_values); @@ -683,34 +678,33 @@ pascm_set_param_value_x (enum var_types type, union pascm_variable *var, } else { - char *string; SCM exception; - string = gdbscm_scm_to_host_string (value, NULL, &exception); + gdb::unique_xmalloc_ptr string + = gdbscm_scm_to_host_string (value, NULL, &exception); if (string == NULL) gdbscm_throw (exception); xfree (var->stringval); - var->stringval = string; + var->stringval = string.release (); } break; case var_enum: { int i; - char *str; SCM exception; SCM_ASSERT_TYPE (scm_is_string (value), value, arg_pos, func_name, _("string")); - str = gdbscm_scm_to_host_string (value, NULL, &exception); + gdb::unique_xmalloc_ptr str + = gdbscm_scm_to_host_string (value, NULL, &exception); if (str == NULL) gdbscm_throw (exception); for (i = 0; enumeration[i]; ++i) { - if (strcmp (enumeration[i], str) == 0) + if (strcmp (enumeration[i], str.get ()) == 0) break; } - xfree (str); if (enumeration[i] == NULL) { gdbscm_out_of_range_error (func_name, arg_pos, value, @@ -1059,17 +1053,17 @@ gdbscm_parameter_value (SCM self) } else { - char *name; SCM except_scm; struct cmd_list_element *alias, *prefix, *cmd; char *newarg; int found = -1; struct gdb_exception except = exception_none; - name = gdbscm_scm_to_host_string (self, NULL, &except_scm); + gdb::unique_xmalloc_ptr name + = gdbscm_scm_to_host_string (self, NULL, &except_scm); if (name == NULL) gdbscm_throw (except_scm); - newarg = concat ("show ", name, (char *) NULL); + newarg = concat ("show ", name.get (), (char *) NULL); TRY { found = lookup_cmd_composition (newarg, &alias, &prefix, &cmd); @@ -1080,7 +1074,6 @@ gdbscm_parameter_value (SCM self) } END_CATCH - xfree (name); xfree (newarg); GDBSCM_HANDLE_GDB_EXCEPTION (except); if (!found) diff --git a/gdb/guile/scm-pretty-print.c b/gdb/guile/scm-pretty-print.c index eea524b104..f406c1f812 100644 --- a/gdb/guile/scm-pretty-print.c +++ b/gdb/guile/scm-pretty-print.c @@ -668,18 +668,16 @@ ppscm_print_string_repr (SCM printer, enum display_hint hint, } else if (scm_is_string (str_scm)) { - struct cleanup *cleanup; size_t length; - char *string + gdb::unique_xmalloc_ptr string = gdbscm_scm_to_string (str_scm, &length, target_charset (gdbarch), 0 /*!strict*/, NULL); - cleanup = make_cleanup (xfree, string); if (hint == HINT_STRING) { struct type *type = builtin_type (gdbarch)->builtin_char; - LA_PRINT_STRING (stream, type, (gdb_byte *) string, + LA_PRINT_STRING (stream, type, (gdb_byte *) string.get (), length, NULL, 0, options); } else @@ -690,14 +688,13 @@ ppscm_print_string_repr (SCM printer, enum display_hint hint, for (i = 0; i < length; ++i) { - if (string[i] == '\0') + if (string.get ()[i] == '\0') fputs_filtered ("\\000", stream); else - fputc_filtered (string[i], stream); + fputc_filtered (string.get ()[i], stream); } } result = STRING_REPR_OK; - do_cleanups (cleanup); } else if (lsscm_is_lazy_string (str_scm)) { diff --git a/gdb/guile/scm-string.c b/gdb/guile/scm-string.c index 63c60f068a..56e14c3320 100644 --- a/gdb/guile/scm-string.c +++ b/gdb/guile/scm-string.c @@ -113,10 +113,9 @@ gdbscm_call_scm_to_stringn (void *datap) If STRICT is zero, then escape sequences are used for characters that can't be converted, and EXCEPT_SCMP may be passed as NULL. - Space for the result is allocated with malloc, caller must free. It is an error to call this if STRING is not a string. */ -char * +gdb::unique_xmalloc_ptr gdbscm_scm_to_string (SCM string, size_t *lenp, const char *charset, int strict, SCM *except_scmp) { @@ -136,7 +135,7 @@ gdbscm_scm_to_string (SCM string, size_t *lenp, if (gdbscm_is_false (scm_result)) { gdb_assert (data.result != NULL); - return data.result; + return gdb::unique_xmalloc_ptr (data.result); } gdb_assert (gdbscm_is_exception (scm_result)); *except_scmp = scm_result; @@ -214,10 +213,9 @@ gdbscm_scm_from_string (const char *string, size_t len, Returns NULL if there is a conversion error, with the exception object stored in *EXCEPT_SCMP. - Space for the result is allocated with malloc, caller must free. It is an error to call this if STRING is not a string. */ -char * +gdb::unique_xmalloc_ptr gdbscm_scm_to_host_string (SCM string, size_t *lenp, SCM *except_scmp) { return gdbscm_scm_to_string (string, lenp, host_charset (), 1, except_scmp);