Return unique_xmalloc_ptr from macro scope functions

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-08  Tom Tromey  <tom@tromey.com>

	* 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.
This commit is contained in:
Tom Tromey 2018-02-06 01:02:00 +01:00
parent 8ce47547b3
commit f6c2623eb8
9 changed files with 65 additions and 75 deletions

View File

@ -1,3 +1,22 @@
2018-02-08 Tom Tromey <tom@tromey.com>
* 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 <simon.marchi@ericsson.com> 2018-02-07 Simon Marchi <simon.marchi@ericsson.com>
* value.c (value_static_field): Assign field type instead of * value.c (value_static_field): Assign field type instead of

View File

@ -3220,26 +3220,24 @@ c_parse (struct parser_state *par_state)
gdb_assert (par_state != NULL); gdb_assert (par_state != NULL);
pstate = par_state; pstate = par_state;
/* Note that parsing (within yyparse) freely installs cleanups gdb::unique_xmalloc_ptr<struct macro_scope> macro_scope;
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;
if (expression_context_block) if (expression_context_block)
expression_macro_scope macro_scope = sal_macro_scope (find_pc_line (expression_context_pc, 0));
= sal_macro_scope (find_pc_line (expression_context_pc, 0));
else else
expression_macro_scope = default_macro_scope (); macro_scope = default_macro_scope ();
if (! expression_macro_scope) if (! macro_scope)
expression_macro_scope = user_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. */ /* Initialize macro expansion code. */
obstack_init (&expansion_obstack); obstack_init (&expansion_obstack);
gdb_assert (! macro_original_text); 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, scoped_restore restore_yydebug = make_scoped_restore (&yydebug,
parser_debug); parser_debug);

View File

@ -157,7 +157,7 @@ static void
write_macro_definitions (const struct block *block, CORE_ADDR pc, write_macro_definitions (const struct block *block, CORE_ADDR pc,
struct ui_file *file) struct ui_file *file)
{ {
struct macro_scope *scope; gdb::unique_xmalloc_ptr<struct macro_scope> scope;
if (block != NULL) if (block != NULL)
scope = sal_macro_scope (find_pc_line (pc, 0)); scope = sal_macro_scope (find_pc_line (pc, 0));

View File

@ -55,11 +55,8 @@ macro_inform_no_debuginfo (void)
static void static void
macro_expand_command (const char *exp, int from_tty) macro_expand_command (const char *exp, int from_tty)
{ {
struct macro_scope *ms = NULL; gdb::unique_xmalloc_ptr<struct macro_scope> ms;
char *expanded = NULL; gdb::unique_xmalloc_ptr<char> expanded;
struct cleanup *cleanup_chain = make_cleanup (free_current_contents, &ms);
make_cleanup (free_current_contents, &expanded);
/* You know, when the user doesn't specify any expression, it would be /* You know, when the user doesn't specify any expression, it would be
really cool if this defaulted to the last expression evaluated. 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 (); ms = default_macro_scope ();
if (ms) 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 ("expands to: ", gdb_stdout);
fputs_filtered (expanded, gdb_stdout); fputs_filtered (expanded.get (), gdb_stdout);
fputs_filtered ("\n", gdb_stdout); fputs_filtered ("\n", gdb_stdout);
} }
else else
macro_inform_no_debuginfo (); macro_inform_no_debuginfo ();
do_cleanups (cleanup_chain);
return;
} }
static void static void
macro_expand_once_command (const char *exp, int from_tty) macro_expand_once_command (const char *exp, int from_tty)
{ {
struct macro_scope *ms = NULL; gdb::unique_xmalloc_ptr<struct macro_scope> ms;
char *expanded = NULL; gdb::unique_xmalloc_ptr<char> expanded;
struct cleanup *cleanup_chain = make_cleanup (free_current_contents, &ms);
make_cleanup (free_current_contents, &expanded);
/* You know, when the user doesn't specify any expression, it would be /* You know, when the user doesn't specify any expression, it would be
really cool if this defaulted to the last expression evaluated. 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 (); ms = default_macro_scope ();
if (ms) 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 ("expands to: ", gdb_stdout);
fputs_filtered (expanded, gdb_stdout); fputs_filtered (expanded.get (), gdb_stdout);
fputs_filtered ("\n", gdb_stdout); fputs_filtered ("\n", gdb_stdout);
} }
else else
macro_inform_no_debuginfo (); macro_inform_no_debuginfo ();
do_cleanups (cleanup_chain);
return;
} }
/* Outputs the include path of a macro starting at FILE and LINE to STREAM. /* 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 static void
info_macro_command (const char *args, int from_tty) info_macro_command (const char *args, int from_tty)
{ {
struct macro_scope *ms = NULL; gdb::unique_xmalloc_ptr<struct macro_scope> ms;
struct cleanup *cleanup_chain;
const char *name; const char *name;
int show_all_macros_named = 0; int show_all_macros_named = 0;
const char *arg_start = args; const char *arg_start = args;
@ -228,7 +216,6 @@ info_macro_command (const char *args, int from_tty)
"whose definition you want to see.")); "whose definition you want to see."));
ms = default_macro_scope (); ms = default_macro_scope ();
cleanup_chain = make_cleanup (free_current_contents, &ms);
if (! ms) if (! ms)
macro_inform_no_debuginfo (); 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); show_pp_source_pos (gdb_stdout, ms->file, ms->line);
} }
} }
do_cleanups (cleanup_chain);
} }
/* Implementation of the "info macros" command. */ /* Implementation of the "info macros" command. */
static void static void
info_macros_command (const char *args, int from_tty) info_macros_command (const char *args, int from_tty)
{ {
struct macro_scope *ms = NULL; gdb::unique_xmalloc_ptr<struct macro_scope> ms;
struct cleanup *cleanup_chain = make_cleanup (free_current_contents, &ms);
if (args == NULL) if (args == NULL)
ms = default_macro_scope (); ms = default_macro_scope ();
@ -289,8 +273,6 @@ info_macros_command (const char *args, int from_tty)
macro_inform_no_debuginfo (); macro_inform_no_debuginfo ();
else else
macro_for_each_in_scope (ms->file, ms->line, print_macro_definition); macro_for_each_in_scope (ms->file, ms->line, print_macro_definition);
do_cleanups (cleanup_chain);
} }

View File

@ -1485,7 +1485,7 @@ scan (struct macro_buffer *dest,
} }
char * gdb::unique_xmalloc_ptr<char>
macro_expand (const char *source, macro_expand (const char *source,
macro_lookup_ftype *lookup_func, macro_lookup_ftype *lookup_func,
void *lookup_func_baton) void *lookup_func_baton)
@ -1504,11 +1504,11 @@ macro_expand (const char *source,
appendc (&dest, '\0'); appendc (&dest, '\0');
discard_cleanups (back_to); discard_cleanups (back_to);
return dest.text; return gdb::unique_xmalloc_ptr<char> (dest.text);
} }
char * gdb::unique_xmalloc_ptr<char>
macro_expand_once (const char *source, macro_expand_once (const char *source,
macro_lookup_ftype *lookup_func, macro_lookup_ftype *lookup_func,
void *lookup_func_baton) void *lookup_func_baton)

View File

@ -37,9 +37,9 @@ typedef struct macro_definition *(macro_lookup_ftype) (const char *name,
preprocessor definitions. SOURCE is a null-terminated string. The preprocessor definitions. SOURCE is a null-terminated string. The
result is a null-terminated string, allocated using xmalloc; it is result is a null-terminated string, allocated using xmalloc; it is
the caller's responsibility to free it. */ the caller's responsibility to free it. */
char *macro_expand (const char *source, gdb::unique_xmalloc_ptr<char> macro_expand (const char *source,
macro_lookup_ftype *lookup_func, macro_lookup_ftype *lookup_func,
void *lookup_func_baton); void *lookup_func_baton);
/* Expand all preprocessor macro references that appear explicitly in /* 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 SOURCE is a null-terminated string. The result is a
null-terminated string, allocated using xmalloc; it is the caller's null-terminated string, allocated using xmalloc; it is the caller's
responsibility to free it. */ responsibility to free it. */
char *macro_expand_once (const char *source, gdb::unique_xmalloc_ptr<char> macro_expand_once (const char *source,
macro_lookup_ftype *lookup_func, macro_lookup_ftype *lookup_func,
void *lookup_func_baton); void *lookup_func_baton);
/* If the null-terminated string pointed to by *LEXPTR begins with a /* If the null-terminated string pointed to by *LEXPTR begins with a

View File

@ -35,11 +35,10 @@
struct macro_table *macro_user_macros; struct macro_table *macro_user_macros;
struct macro_scope * gdb::unique_xmalloc_ptr<struct macro_scope>
sal_macro_scope (struct symtab_and_line sal) sal_macro_scope (struct symtab_and_line sal)
{ {
struct macro_source_file *main_file, *inclusion; struct macro_source_file *main_file, *inclusion;
struct macro_scope *ms;
struct compunit_symtab *cust; struct compunit_symtab *cust;
if (sal.symtab == NULL) if (sal.symtab == NULL)
@ -48,7 +47,7 @@ sal_macro_scope (struct symtab_and_line sal)
if (COMPUNIT_MACRO_TABLE (cust) == NULL) if (COMPUNIT_MACRO_TABLE (cust) == NULL)
return NULL; return NULL;
ms = XNEW (struct macro_scope); gdb::unique_xmalloc_ptr<struct macro_scope> ms (XNEW (struct macro_scope));
main_file = macro_main (COMPUNIT_MACRO_TABLE (cust)); main_file = macro_main (COMPUNIT_MACRO_TABLE (cust));
inclusion = macro_lookup_inclusion (main_file, sal.symtab->filename); 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<struct macro_scope>
user_macro_scope (void) user_macro_scope (void)
{ {
struct macro_scope *ms; gdb::unique_xmalloc_ptr<struct macro_scope> ms (XNEW (struct macro_scope));
ms = XNEW (struct macro_scope);
ms->file = macro_main (macro_user_macros); ms->file = macro_main (macro_user_macros);
ms->line = -1; ms->line = -1;
return ms; return ms;
} }
struct macro_scope * gdb::unique_xmalloc_ptr<struct macro_scope>
default_macro_scope (void) default_macro_scope (void)
{ {
struct symtab_and_line sal; struct symtab_and_line sal;
struct macro_scope *ms; gdb::unique_xmalloc_ptr<struct macro_scope> ms;
struct frame_info *frame; struct frame_info *frame;
CORE_ADDR pc; CORE_ADDR pc;

View File

@ -39,13 +39,13 @@ struct macro_scope {
/* Return a `struct macro_scope' object corresponding to the symtab /* Return a `struct macro_scope' object corresponding to the symtab
and line given in SAL. If we have no macro information for that and line given in SAL. If we have no macro information for that
location, or if SAL's pc is zero, return zero. */ 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<struct macro_scope> sal_macro_scope
(struct symtab_and_line sal);
/* Return a `struct macro_scope' object representing just the /* Return a `struct macro_scope' object representing just the
user-defined macros. The result is allocated using xmalloc; the user-defined macros. */
caller is responsible for freeing it. */ gdb::unique_xmalloc_ptr<struct macro_scope> user_macro_scope (void);
struct macro_scope *user_macro_scope (void);
/* Return a `struct macro_scope' object describing the scope the `macro /* Return a `struct macro_scope' object describing the scope the `macro
expand' and `macro expand-once' commands should use for looking up 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. its PC; otherwise, this is the last listing position.
If we have no macro information for the current location, return If we have no macro information for the current location, return
the user macro scope. the user macro scope. */
gdb::unique_xmalloc_ptr<struct macro_scope> default_macro_scope (void);
The object returned is allocated using xmalloc; the caller is
responsible for freeing it. */
struct macro_scope *default_macro_scope (void);
/* Look up the definition of the macro named NAME in scope at the source /* Look up the definition of the macro named NAME in scope at the source

View File

@ -5154,7 +5154,7 @@ default_collect_symbol_completion_matches_break_on
if (current_language->la_macro_expansion == macro_expansion_c if (current_language->la_macro_expansion == macro_expansion_c
&& code == TYPE_CODE_UNDEF) && code == TYPE_CODE_UNDEF)
{ {
struct macro_scope *scope; gdb::unique_xmalloc_ptr<struct macro_scope> scope;
/* This adds a macro's name to the current completion list. */ /* This adds a macro's name to the current completion list. */
auto add_macro_name = [&] (const char *macro_name, auto add_macro_name = [&] (const char *macro_name,
@ -5175,11 +5175,8 @@ default_collect_symbol_completion_matches_break_on
completion time. */ completion time. */
scope = default_macro_scope (); scope = default_macro_scope ();
if (scope) if (scope)
{ macro_for_each_in_scope (scope->file, scope->line,
macro_for_each_in_scope (scope->file, scope->line, add_macro_name);
add_macro_name);
xfree (scope);
}
/* User-defined macros are always visible. */ /* User-defined macros are always visible. */
macro_for_each (macro_user_macros, add_macro_name); macro_for_each (macro_user_macros, add_macro_name);