From patchwork Wed Feb 7 22:04:28 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 25865 Received: (qmail 3827 invoked by alias); 7 Feb 2018 22:04:44 -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 3630 invoked by uid 89); 7 Feb 2018 22:04:42 -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, RCVD_IN_DNSWL_NONE, SPF_HELO_PASS autolearn=ham version=3.3.2 spammy=sal X-HELO: gateway34.websitewelcome.com Received: from gateway34.websitewelcome.com (HELO gateway34.websitewelcome.com) (192.185.148.214) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Wed, 07 Feb 2018 22:04:40 +0000 Received: from cm16.websitewelcome.com (cm16.websitewelcome.com [100.42.49.19]) by gateway34.websitewelcome.com (Postfix) with ESMTP id D236D520BA for ; Wed, 7 Feb 2018 16:04:38 -0600 (CST) Received: from box5379.bluehost.com ([162.241.216.53]) by cmsmtp with SMTP id jXpWecTEkODN4jXpWeWWd1; Wed, 07 Feb 2018 16:04:38 -0600 Received: from 174-29-33-254.hlrn.qwest.net ([174.29.33.254]:59168 helo=pokyo.Home) by box5379.bluehost.com with esmtpsa (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.89) (envelope-from ) id 1ejXpW-002nlZ-JE; Wed, 07 Feb 2018 16:04:38 -0600 From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [RFA 3/9] Return unique_xmalloc_ptr from macro scope functions Date: Wed, 7 Feb 2018 15:04:28 -0700 Message-Id: <20180207220434.6045-4-tom@tromey.com> In-Reply-To: <20180207220434.6045-1-tom@tromey.com> References: <20180207220434.6045-1-tom@tromey.com> X-BWhitelist: no X-Source-L: No X-Exim-ID: 1ejXpW-002nlZ-JE X-Source-Sender: 174-29-33-254.hlrn.qwest.net (pokyo.Home) [174.29.33.254]:59168 X-Source-Auth: tom+tromey.com X-Email-Count: 4 X-Source-Cap: ZWx5bnJvYmk7ZWx5bnJvYmk7Ym94NTM3OS5ibHVlaG9zdC5jb20= X-Local-Domain: yes This changes the macro scope functions (sal_macro_scope, user_macro_scope, and default_macro_scope) to return a unique_xmalloc_ptr, then fixes up the users. This allowed for the removal of several cleanups. 2018-02-07 Tom Tromey * symtab.c (default_collect_symbol_completion_matches_break_on): Use unique_xmalloc_ptr. * macroscope.h: (sal_macro_scope, user_macro_scope) (default_macro_scope): Return unique_xmalloc_ptr. * macroscope.c (sal_macro_scope, user_macro_scope) (default_macro_scope): Return unique_xmalloc_ptr. * macroexp.h (macro_expand, macro_expand_once): Return unique_xmalloc_ptr. * macroexp.c (macro_expand, macro_expand_once): Return unique_xmalloc_ptr. * macrocmd.c (macro_expand_command, macro_expand_once_command) (info_macro_command, info_macros_command): Use unique_xmalloc_ptr. * compile/compile-c-support.c (write_macro_definitions): Use unique_xmalloc_ptr. * c-exp.y (c_parse): Use unique_xmalloc_ptr. --- gdb/ChangeLog | 19 +++++++++++++++++++ gdb/c-exp.y | 24 +++++++++++------------- gdb/compile/compile-c-support.c | 2 +- gdb/macrocmd.c | 38 ++++++++++---------------------------- gdb/macroexp.c | 6 +++--- gdb/macroexp.h | 12 ++++++------ gdb/macroscope.c | 15 ++++++--------- gdb/macroscope.h | 15 ++++++--------- gdb/symtab.c | 9 +++------ 9 files changed, 65 insertions(+), 75 deletions(-) diff --git a/gdb/ChangeLog b/gdb/ChangeLog index 00ed7e78c2..abb6126864 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,3 +1,22 @@ +2018-02-07 Tom Tromey + + * symtab.c (default_collect_symbol_completion_matches_break_on): + Use unique_xmalloc_ptr. + * macroscope.h: (sal_macro_scope, user_macro_scope) + (default_macro_scope): Return unique_xmalloc_ptr. + * macroscope.c (sal_macro_scope, user_macro_scope) + (default_macro_scope): Return unique_xmalloc_ptr. + * macroexp.h (macro_expand, macro_expand_once): Return + unique_xmalloc_ptr. + * macroexp.c (macro_expand, macro_expand_once): Return + unique_xmalloc_ptr. + * macrocmd.c (macro_expand_command, macro_expand_once_command) + (info_macro_command, info_macros_command): Use + unique_xmalloc_ptr. + * compile/compile-c-support.c (write_macro_definitions): Use + unique_xmalloc_ptr. + * c-exp.y (c_parse): Use unique_xmalloc_ptr. + 2018-02-07 Simon Marchi * value.c (value_static_field): Assign field type instead of diff --git a/gdb/c-exp.y b/gdb/c-exp.y index 8f0aa00f52..8dc3c068a5 100644 --- a/gdb/c-exp.y +++ b/gdb/c-exp.y @@ -3220,26 +3220,24 @@ c_parse (struct parser_state *par_state) gdb_assert (par_state != NULL); pstate = par_state; - /* Note that parsing (within yyparse) freely installs cleanups - assuming they'll be run here (below). */ - - back_to = make_cleanup (free_current_contents, &expression_macro_scope); - - /* Set up the scope for macro expansion. */ - expression_macro_scope = NULL; + gdb::unique_xmalloc_ptr macro_scope; if (expression_context_block) - expression_macro_scope - = sal_macro_scope (find_pc_line (expression_context_pc, 0)); + macro_scope = sal_macro_scope (find_pc_line (expression_context_pc, 0)); else - expression_macro_scope = default_macro_scope (); - if (! expression_macro_scope) - expression_macro_scope = user_macro_scope (); + macro_scope = default_macro_scope (); + if (! macro_scope) + macro_scope = user_macro_scope (); + + scoped_restore restore_macro_scope + = make_scoped_restore (&expression_macro_scope, macro_scope.get ()); /* Initialize macro expansion code. */ obstack_init (&expansion_obstack); gdb_assert (! macro_original_text); - make_cleanup (scan_macro_cleanup, 0); + /* Note that parsing (within yyparse) freely installs cleanups + assuming they'll be run here (below). */ + back_to = make_cleanup (scan_macro_cleanup, 0); scoped_restore restore_yydebug = make_scoped_restore (&yydebug, parser_debug); diff --git a/gdb/compile/compile-c-support.c b/gdb/compile/compile-c-support.c index 006d3f811c..e694648288 100644 --- a/gdb/compile/compile-c-support.c +++ b/gdb/compile/compile-c-support.c @@ -157,7 +157,7 @@ static void write_macro_definitions (const struct block *block, CORE_ADDR pc, struct ui_file *file) { - struct macro_scope *scope; + gdb::unique_xmalloc_ptr scope; if (block != NULL) scope = sal_macro_scope (find_pc_line (pc, 0)); diff --git a/gdb/macrocmd.c b/gdb/macrocmd.c index e90d78724d..13fd95d7da 100644 --- a/gdb/macrocmd.c +++ b/gdb/macrocmd.c @@ -55,11 +55,8 @@ macro_inform_no_debuginfo (void) static void macro_expand_command (const char *exp, int from_tty) { - struct macro_scope *ms = NULL; - char *expanded = NULL; - struct cleanup *cleanup_chain = make_cleanup (free_current_contents, &ms); - - make_cleanup (free_current_contents, &expanded); + gdb::unique_xmalloc_ptr ms; + gdb::unique_xmalloc_ptr expanded; /* You know, when the user doesn't specify any expression, it would be really cool if this defaulted to the last expression evaluated. @@ -74,26 +71,21 @@ macro_expand_command (const char *exp, int from_tty) ms = default_macro_scope (); if (ms) { - expanded = macro_expand (exp, standard_macro_lookup, ms); + expanded = macro_expand (exp, standard_macro_lookup, ms.get ()); fputs_filtered ("expands to: ", gdb_stdout); - fputs_filtered (expanded, gdb_stdout); + fputs_filtered (expanded.get (), gdb_stdout); fputs_filtered ("\n", gdb_stdout); } else macro_inform_no_debuginfo (); - - do_cleanups (cleanup_chain); - return; } static void macro_expand_once_command (const char *exp, int from_tty) { - struct macro_scope *ms = NULL; - char *expanded = NULL; - struct cleanup *cleanup_chain = make_cleanup (free_current_contents, &ms); - make_cleanup (free_current_contents, &expanded); + gdb::unique_xmalloc_ptr ms; + gdb::unique_xmalloc_ptr expanded; /* You know, when the user doesn't specify any expression, it would be really cool if this defaulted to the last expression evaluated. @@ -108,16 +100,13 @@ macro_expand_once_command (const char *exp, int from_tty) ms = default_macro_scope (); if (ms) { - expanded = macro_expand_once (exp, standard_macro_lookup, ms); + expanded = macro_expand_once (exp, standard_macro_lookup, ms.get ()); fputs_filtered ("expands to: ", gdb_stdout); - fputs_filtered (expanded, gdb_stdout); + fputs_filtered (expanded.get (), gdb_stdout); fputs_filtered ("\n", gdb_stdout); } else macro_inform_no_debuginfo (); - - do_cleanups (cleanup_chain); - return; } /* Outputs the include path of a macro starting at FILE and LINE to STREAM. @@ -190,8 +179,7 @@ print_macro_definition (const char *name, static void info_macro_command (const char *args, int from_tty) { - struct macro_scope *ms = NULL; - struct cleanup *cleanup_chain; + gdb::unique_xmalloc_ptr ms; const char *name; int show_all_macros_named = 0; const char *arg_start = args; @@ -228,7 +216,6 @@ info_macro_command (const char *args, int from_tty) "whose definition you want to see.")); ms = default_macro_scope (); - cleanup_chain = make_cleanup (free_current_contents, &ms); if (! ms) macro_inform_no_debuginfo (); @@ -263,16 +250,13 @@ info_macro_command (const char *args, int from_tty) show_pp_source_pos (gdb_stdout, ms->file, ms->line); } } - - do_cleanups (cleanup_chain); } /* Implementation of the "info macros" command. */ static void info_macros_command (const char *args, int from_tty) { - struct macro_scope *ms = NULL; - struct cleanup *cleanup_chain = make_cleanup (free_current_contents, &ms); + gdb::unique_xmalloc_ptr ms; if (args == NULL) ms = default_macro_scope (); @@ -289,8 +273,6 @@ info_macros_command (const char *args, int from_tty) macro_inform_no_debuginfo (); else macro_for_each_in_scope (ms->file, ms->line, print_macro_definition); - - do_cleanups (cleanup_chain); } diff --git a/gdb/macroexp.c b/gdb/macroexp.c index 8a2df1edff..aefb13a9d6 100644 --- a/gdb/macroexp.c +++ b/gdb/macroexp.c @@ -1485,7 +1485,7 @@ scan (struct macro_buffer *dest, } -char * +gdb::unique_xmalloc_ptr macro_expand (const char *source, macro_lookup_ftype *lookup_func, void *lookup_func_baton) @@ -1504,11 +1504,11 @@ macro_expand (const char *source, appendc (&dest, '\0'); discard_cleanups (back_to); - return dest.text; + return gdb::unique_xmalloc_ptr (dest.text); } -char * +gdb::unique_xmalloc_ptr macro_expand_once (const char *source, macro_lookup_ftype *lookup_func, void *lookup_func_baton) diff --git a/gdb/macroexp.h b/gdb/macroexp.h index f7c74116ce..8d3e3f010f 100644 --- a/gdb/macroexp.h +++ b/gdb/macroexp.h @@ -37,9 +37,9 @@ typedef struct macro_definition *(macro_lookup_ftype) (const char *name, preprocessor definitions. SOURCE is a null-terminated string. The result is a null-terminated string, allocated using xmalloc; it is the caller's responsibility to free it. */ -char *macro_expand (const char *source, - macro_lookup_ftype *lookup_func, - void *lookup_func_baton); +gdb::unique_xmalloc_ptr macro_expand (const char *source, + macro_lookup_ftype *lookup_func, + void *lookup_func_baton); /* Expand all preprocessor macro references that appear explicitly in @@ -49,9 +49,9 @@ char *macro_expand (const char *source, SOURCE is a null-terminated string. The result is a null-terminated string, allocated using xmalloc; it is the caller's responsibility to free it. */ -char *macro_expand_once (const char *source, - macro_lookup_ftype *lookup_func, - void *lookup_func_baton); +gdb::unique_xmalloc_ptr macro_expand_once (const char *source, + macro_lookup_ftype *lookup_func, + void *lookup_func_baton); /* If the null-terminated string pointed to by *LEXPTR begins with a diff --git a/gdb/macroscope.c b/gdb/macroscope.c index a94492011d..23d2e50fdd 100644 --- a/gdb/macroscope.c +++ b/gdb/macroscope.c @@ -35,11 +35,10 @@ struct macro_table *macro_user_macros; -struct macro_scope * +gdb::unique_xmalloc_ptr sal_macro_scope (struct symtab_and_line sal) { struct macro_source_file *main_file, *inclusion; - struct macro_scope *ms; struct compunit_symtab *cust; if (sal.symtab == NULL) @@ -48,7 +47,7 @@ sal_macro_scope (struct symtab_and_line sal) if (COMPUNIT_MACRO_TABLE (cust) == NULL) return NULL; - ms = XNEW (struct macro_scope); + gdb::unique_xmalloc_ptr ms (XNEW (struct macro_scope)); main_file = macro_main (COMPUNIT_MACRO_TABLE (cust)); inclusion = macro_lookup_inclusion (main_file, sal.symtab->filename); @@ -87,22 +86,20 @@ sal_macro_scope (struct symtab_and_line sal) } -struct macro_scope * +gdb::unique_xmalloc_ptr user_macro_scope (void) { - struct macro_scope *ms; - - ms = XNEW (struct macro_scope); + gdb::unique_xmalloc_ptr ms (XNEW (struct macro_scope)); ms->file = macro_main (macro_user_macros); ms->line = -1; return ms; } -struct macro_scope * +gdb::unique_xmalloc_ptr default_macro_scope (void) { struct symtab_and_line sal; - struct macro_scope *ms; + gdb::unique_xmalloc_ptr ms; struct frame_info *frame; CORE_ADDR pc; diff --git a/gdb/macroscope.h b/gdb/macroscope.h index 3bd1c9e817..00fa98ab6e 100644 --- a/gdb/macroscope.h +++ b/gdb/macroscope.h @@ -39,13 +39,13 @@ struct macro_scope { /* Return a `struct macro_scope' object corresponding to the symtab and line given in SAL. If we have no macro information for that location, or if SAL's pc is zero, return zero. */ -struct macro_scope *sal_macro_scope (struct symtab_and_line sal); +gdb::unique_xmalloc_ptr sal_macro_scope + (struct symtab_and_line sal); /* Return a `struct macro_scope' object representing just the - user-defined macros. The result is allocated using xmalloc; the - caller is responsible for freeing it. */ -struct macro_scope *user_macro_scope (void); + user-defined macros. */ +gdb::unique_xmalloc_ptr user_macro_scope (void); /* Return a `struct macro_scope' object describing the scope the `macro expand' and `macro expand-once' commands should use for looking up @@ -53,11 +53,8 @@ struct macro_scope *user_macro_scope (void); its PC; otherwise, this is the last listing position. If we have no macro information for the current location, return - the user macro scope. - - The object returned is allocated using xmalloc; the caller is - responsible for freeing it. */ -struct macro_scope *default_macro_scope (void); + the user macro scope. */ +gdb::unique_xmalloc_ptr default_macro_scope (void); /* Look up the definition of the macro named NAME in scope at the source diff --git a/gdb/symtab.c b/gdb/symtab.c index 81f4fc9ec5..0fd3f3a30f 100644 --- a/gdb/symtab.c +++ b/gdb/symtab.c @@ -5154,7 +5154,7 @@ default_collect_symbol_completion_matches_break_on if (current_language->la_macro_expansion == macro_expansion_c && code == TYPE_CODE_UNDEF) { - struct macro_scope *scope; + gdb::unique_xmalloc_ptr scope; /* This adds a macro's name to the current completion list. */ auto add_macro_name = [&] (const char *macro_name, @@ -5175,11 +5175,8 @@ default_collect_symbol_completion_matches_break_on completion time. */ scope = default_macro_scope (); if (scope) - { - macro_for_each_in_scope (scope->file, scope->line, - add_macro_name); - xfree (scope); - } + macro_for_each_in_scope (scope->file, scope->line, + add_macro_name); /* User-defined macros are always visible. */ macro_for_each (macro_user_macros, add_macro_name);