Do not allocate macro_scope on the heap

I noticed that there's no particular reason to allocate the
macro_scope objects on the heap.  They can be passed around by value
just as easily.

Approved-By: Simon Marchi <simon.marchi@efficios.com>
This commit is contained in:
Tom Tromey
2025-05-29 17:23:04 -06:00
parent 408984ea7b
commit 4aac43f399
6 changed files with 56 additions and 59 deletions

View File

@@ -3392,18 +3392,18 @@ c_parse (struct parser_state *par_state)
c_parse_state cstate; c_parse_state cstate;
scoped_restore cstate_restore = make_scoped_restore (&cpstate, &cstate); scoped_restore cstate_restore = make_scoped_restore (&cpstate, &cstate);
gdb::unique_xmalloc_ptr<struct macro_scope> macro_scope; macro_scope macro_scope;
if (par_state->expression_context_block) if (par_state->expression_context_block)
macro_scope macro_scope
= sal_macro_scope (find_pc_line (par_state->expression_context_pc, 0)); = sal_macro_scope (find_pc_line (par_state->expression_context_pc, 0));
else else
macro_scope = default_macro_scope (); macro_scope = default_macro_scope ();
if (! macro_scope) if (!macro_scope.is_valid ())
macro_scope = user_macro_scope (); macro_scope = user_macro_scope ();
scoped_restore restore_macro_scope scoped_restore restore_macro_scope
= make_scoped_restore (&expression_macro_scope, macro_scope.get ()); = make_scoped_restore (&expression_macro_scope, &macro_scope);
scoped_restore restore_yydebug = make_scoped_restore (&yydebug, scoped_restore restore_yydebug = make_scoped_restore (&yydebug,
par_state->debug); par_state->debug);

View File

@@ -185,18 +185,18 @@ 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)
{ {
gdb::unique_xmalloc_ptr<struct macro_scope> scope; 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));
else else
scope = default_macro_scope (); scope = default_macro_scope ();
if (scope == NULL) if (!scope.is_valid ())
scope = user_macro_scope (); scope = user_macro_scope ();
if (scope != NULL && scope->file != NULL && scope->file->table != NULL) if (scope.is_valid () && scope.file->table != nullptr)
{ {
macro_for_each_in_scope (scope->file, scope->line, macro_for_each_in_scope (scope.file, scope.line,
[&] (const char *name, [&] (const char *name,
const macro_definition *macro, const macro_definition *macro,
macro_source_file *source, macro_source_file *source,

View File

@@ -57,11 +57,11 @@ macro_expand_command (const char *exp, int from_tty)
" expression you\n" " expression you\n"
"want to expand.")); "want to expand."));
gdb::unique_xmalloc_ptr<macro_scope> ms = default_macro_scope (); macro_scope ms = default_macro_scope ();
if (ms != nullptr) if (ms.is_valid ())
{ {
gdb::unique_xmalloc_ptr<char> expanded = macro_expand (exp, *ms); gdb::unique_xmalloc_ptr<char> expanded = macro_expand (exp, ms);
gdb_puts ("expands to: "); gdb_puts ("expands to: ");
gdb_puts (expanded.get ()); gdb_puts (expanded.get ());
@@ -85,11 +85,11 @@ macro_expand_once_command (const char *exp, int from_tty)
" the expression\n" " the expression\n"
"you want to expand.")); "you want to expand."));
gdb::unique_xmalloc_ptr<macro_scope> ms = default_macro_scope (); macro_scope ms = default_macro_scope ();
if (ms != nullptr) if (ms.is_valid ())
{ {
gdb::unique_xmalloc_ptr<char> expanded = macro_expand_once (exp, *ms); gdb::unique_xmalloc_ptr<char> expanded = macro_expand_once (exp, ms);
gdb_puts ("expands to: "); gdb_puts ("expands to: ");
gdb_puts (expanded.get ()); gdb_puts (expanded.get ());
@@ -169,7 +169,6 @@ 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)
{ {
gdb::unique_xmalloc_ptr<struct macro_scope> ms;
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;
@@ -201,15 +200,15 @@ info_macro_command (const char *args, int from_tty)
" of the macro\n" " of the macro\n"
"whose definition you want to see.")); "whose definition you want to see."));
ms = default_macro_scope (); macro_scope ms = default_macro_scope ();
if (! ms) if (!ms.is_valid ())
macro_inform_no_debuginfo (); macro_inform_no_debuginfo ();
else if (show_all_macros_named) else if (show_all_macros_named)
macro_for_each (ms->file->table, [&] (const char *macro_name, macro_for_each (ms.file->table, [&] (const char *macro_name,
const macro_definition *macro, const macro_definition *macro,
macro_source_file *source, macro_source_file *source,
int line) int line)
{ {
if (strcmp (name, macro_name) == 0) if (strcmp (name, macro_name) == 0)
print_macro_definition (name, macro, source, line); print_macro_definition (name, macro, source, line);
@@ -218,12 +217,12 @@ info_macro_command (const char *args, int from_tty)
{ {
struct macro_definition *d; struct macro_definition *d;
d = macro_lookup_definition (ms->file, ms->line, name); d = macro_lookup_definition (ms.file, ms.line, name);
if (d) if (d)
{ {
int line; int line;
struct macro_source_file *file struct macro_source_file *file
= macro_definition_location (ms->file, ms->line, name, &line); = macro_definition_location (ms.file, ms.line, name, &line);
print_macro_definition (name, d, file, line); print_macro_definition (name, d, file, line);
} }
@@ -232,7 +231,7 @@ info_macro_command (const char *args, int from_tty)
gdb_printf ("The symbol `%s' has no definition as a C/C++" gdb_printf ("The symbol `%s' has no definition as a C/C++"
" preprocessor macro\n" " preprocessor macro\n"
"at ", name); "at ", name);
show_pp_source_pos (gdb_stdout, ms->file, ms->line); show_pp_source_pos (gdb_stdout, ms.file, ms.line);
} }
} }
} }
@@ -241,7 +240,7 @@ info_macro_command (const char *args, int from_tty)
static void static void
info_macros_command (const char *args, int from_tty) info_macros_command (const char *args, int from_tty)
{ {
gdb::unique_xmalloc_ptr<struct macro_scope> ms; macro_scope ms;
if (args == NULL) if (args == NULL)
ms = default_macro_scope (); ms = default_macro_scope ();
@@ -254,10 +253,10 @@ info_macros_command (const char *args, int from_tty)
ms = sal_macro_scope (sals[0]); ms = sal_macro_scope (sals[0]);
} }
if (! ms || ! ms->file || ! ms->file->table) if (!ms.is_valid () || ms.file->table == nullptr)
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);
} }

View File

@@ -34,28 +34,29 @@
struct macro_table *macro_user_macros; struct macro_table *macro_user_macros;
gdb::unique_xmalloc_ptr<struct macro_scope> macro_scope
sal_macro_scope (struct symtab_and_line sal) sal_macro_scope (struct symtab_and_line sal)
{ {
macro_scope result;
struct macro_source_file *main_file, *inclusion; struct macro_source_file *main_file, *inclusion;
struct compunit_symtab *cust; struct compunit_symtab *cust;
if (sal.symtab == NULL) if (sal.symtab == NULL)
return NULL; return result;
cust = sal.symtab->compunit (); cust = sal.symtab->compunit ();
if (cust->macro_table () == NULL) if (cust->macro_table () == NULL)
return NULL; return result;
gdb::unique_xmalloc_ptr<struct macro_scope> ms (XNEW (struct macro_scope)); macro_scope ms;
main_file = macro_main (cust->macro_table ()); main_file = macro_main (cust->macro_table ());
inclusion = macro_lookup_inclusion (main_file, sal.symtab->filename_for_id); inclusion = macro_lookup_inclusion (main_file, sal.symtab->filename_for_id);
if (inclusion) if (inclusion)
{ {
ms->file = inclusion; ms.file = inclusion;
ms->line = sal.line; ms.line = sal.line;
} }
else else
{ {
@@ -73,8 +74,8 @@ sal_macro_scope (struct symtab_and_line sal)
For the time being, though, we'll just treat these as For the time being, though, we'll just treat these as
occurring at the end of the main source file. */ occurring at the end of the main source file. */
ms->file = main_file; ms.file = main_file;
ms->line = -1; ms.line = -1;
complaint (_("symtab found for `%s', but that file\n" complaint (_("symtab found for `%s', but that file\n"
"is not covered in the compilation unit's macro information"), "is not covered in the compilation unit's macro information"),
@@ -85,20 +86,16 @@ sal_macro_scope (struct symtab_and_line sal)
} }
gdb::unique_xmalloc_ptr<struct macro_scope> macro_scope
user_macro_scope (void) user_macro_scope ()
{ {
gdb::unique_xmalloc_ptr<struct macro_scope> ms (XNEW (struct macro_scope)); return { macro_main (macro_user_macros), -1 };
ms->file = macro_main (macro_user_macros);
ms->line = -1;
return ms;
} }
gdb::unique_xmalloc_ptr<struct macro_scope> macro_scope
default_macro_scope (void) default_macro_scope ()
{ {
struct symtab_and_line sal; struct symtab_and_line sal;
gdb::unique_xmalloc_ptr<struct macro_scope> ms;
frame_info_ptr frame; frame_info_ptr frame;
CORE_ADDR pc; CORE_ADDR pc;
@@ -128,8 +125,8 @@ default_macro_scope (void)
sal.line = cursal.line; sal.line = cursal.line;
} }
ms = sal_macro_scope (sal); macro_scope ms = sal_macro_scope (sal);
if (! ms) if (!ms.is_valid ())
ms = user_macro_scope (); ms = user_macro_scope ();
return ms; return ms;

View File

@@ -31,21 +31,25 @@ extern struct macro_table *macro_user_macros;
in scope: a source file (either a main source file or an in scope: a source file (either a main source file or an
#inclusion), and a line number in that file. */ #inclusion), and a line number in that file. */
struct macro_scope { struct macro_scope {
struct macro_source_file *file; struct macro_source_file *file = nullptr;
int line; int line = 0;
/* Return true if this scope is valid. */
bool is_valid () const
{
return file != nullptr;
}
}; };
/* 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 an invalid scope. */
gdb::unique_xmalloc_ptr<struct macro_scope> sal_macro_scope macro_scope sal_macro_scope (struct symtab_and_line sal);
(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. */ user-defined macros. */
gdb::unique_xmalloc_ptr<struct macro_scope> user_macro_scope (void); macro_scope user_macro_scope ();
/* 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
@@ -54,7 +58,7 @@ gdb::unique_xmalloc_ptr<struct macro_scope> user_macro_scope (void);
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); macro_scope default_macro_scope ();
/* 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
location given by MS. */ location given by MS. */

View File

@@ -6198,8 +6198,6 @@ default_collect_symbol_completion_matches_break_on
if (current_language->macro_expansion () == macro_expansion_c if (current_language->macro_expansion () == macro_expansion_c
&& code == TYPE_CODE_UNDEF) && code == TYPE_CODE_UNDEF)
{ {
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,
const macro_definition *, const macro_definition *,
@@ -6217,10 +6215,9 @@ default_collect_symbol_completion_matches_break_on
resulting expression will be evaluated at "file:line" -- but resulting expression will be evaluated at "file:line" -- but
at there does not seem to be a way to detect this at at there does not seem to be a way to detect this at
completion time. */ completion time. */
scope = default_macro_scope (); macro_scope scope = default_macro_scope ();
if (scope) if (scope.is_valid ())
macro_for_each_in_scope (scope->file, scope->line, macro_for_each_in_scope (scope.file, scope.line, add_macro_name);
add_macro_name);
/* 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);