From: Tom Tromey Date: Thu, 29 May 2025 23:23:04 +0000 (-0600) Subject: Do not allocate macro_scope on the heap X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=4aac43f399a8e55c70bfbc025f33e154d65f7b69;p=thirdparty%2Fbinutils-gdb.git 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 --- diff --git a/gdb/c-exp.y b/gdb/c-exp.y index bdf76744817..14d4b704ff1 100644 --- a/gdb/c-exp.y +++ b/gdb/c-exp.y @@ -3392,18 +3392,18 @@ c_parse (struct parser_state *par_state) c_parse_state cstate; scoped_restore cstate_restore = make_scoped_restore (&cpstate, &cstate); - gdb::unique_xmalloc_ptr macro_scope; + macro_scope macro_scope; if (par_state->expression_context_block) macro_scope = sal_macro_scope (find_pc_line (par_state->expression_context_pc, 0)); else macro_scope = default_macro_scope (); - if (! macro_scope) + if (!macro_scope.is_valid ()) macro_scope = user_macro_scope (); scoped_restore restore_macro_scope - = make_scoped_restore (&expression_macro_scope, macro_scope.get ()); + = make_scoped_restore (&expression_macro_scope, ¯o_scope); scoped_restore restore_yydebug = make_scoped_restore (&yydebug, par_state->debug); diff --git a/gdb/compile/compile-c-support.c b/gdb/compile/compile-c-support.c index 0d323507955..ac8888fb16b 100644 --- a/gdb/compile/compile-c-support.c +++ b/gdb/compile/compile-c-support.c @@ -185,18 +185,18 @@ static void write_macro_definitions (const struct block *block, CORE_ADDR pc, struct ui_file *file) { - gdb::unique_xmalloc_ptr scope; + macro_scope scope; if (block != NULL) scope = sal_macro_scope (find_pc_line (pc, 0)); else scope = default_macro_scope (); - if (scope == NULL) + if (!scope.is_valid ()) 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 macro_definition *macro, macro_source_file *source, diff --git a/gdb/macrocmd.c b/gdb/macrocmd.c index 0192a0077a1..86b6839867e 100644 --- a/gdb/macrocmd.c +++ b/gdb/macrocmd.c @@ -57,11 +57,11 @@ macro_expand_command (const char *exp, int from_tty) " expression you\n" "want to expand.")); - gdb::unique_xmalloc_ptr ms = default_macro_scope (); + macro_scope ms = default_macro_scope (); - if (ms != nullptr) + if (ms.is_valid ()) { - gdb::unique_xmalloc_ptr expanded = macro_expand (exp, *ms); + gdb::unique_xmalloc_ptr expanded = macro_expand (exp, ms); gdb_puts ("expands to: "); gdb_puts (expanded.get ()); @@ -85,11 +85,11 @@ macro_expand_once_command (const char *exp, int from_tty) " the expression\n" "you want to expand.")); - gdb::unique_xmalloc_ptr ms = default_macro_scope (); + macro_scope ms = default_macro_scope (); - if (ms != nullptr) + if (ms.is_valid ()) { - gdb::unique_xmalloc_ptr expanded = macro_expand_once (exp, *ms); + gdb::unique_xmalloc_ptr expanded = macro_expand_once (exp, ms); gdb_puts ("expands to: "); gdb_puts (expanded.get ()); @@ -169,7 +169,6 @@ print_macro_definition (const char *name, static void info_macro_command (const char *args, int from_tty) { - gdb::unique_xmalloc_ptr ms; const char *name; int show_all_macros_named = 0; const char *arg_start = args; @@ -201,15 +200,15 @@ info_macro_command (const char *args, int from_tty) " of the macro\n" "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 (); else if (show_all_macros_named) - macro_for_each (ms->file->table, [&] (const char *macro_name, - const macro_definition *macro, - macro_source_file *source, - int line) + macro_for_each (ms.file->table, [&] (const char *macro_name, + const macro_definition *macro, + macro_source_file *source, + int line) { if (strcmp (name, macro_name) == 0) print_macro_definition (name, macro, source, line); @@ -218,12 +217,12 @@ info_macro_command (const char *args, int from_tty) { struct macro_definition *d; - d = macro_lookup_definition (ms->file, ms->line, name); + d = macro_lookup_definition (ms.file, ms.line, name); if (d) { int line; 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); } @@ -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++" " preprocessor macro\n" "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 info_macros_command (const char *args, int from_tty) { - gdb::unique_xmalloc_ptr ms; + macro_scope ms; if (args == NULL) ms = default_macro_scope (); @@ -254,10 +253,10 @@ info_macros_command (const char *args, int from_tty) 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 (); 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); } diff --git a/gdb/macroscope.c b/gdb/macroscope.c index 97b41b62876..633ead19501 100644 --- a/gdb/macroscope.c +++ b/gdb/macroscope.c @@ -34,28 +34,29 @@ struct macro_table *macro_user_macros; -gdb::unique_xmalloc_ptr +macro_scope sal_macro_scope (struct symtab_and_line sal) { + macro_scope result; struct macro_source_file *main_file, *inclusion; struct compunit_symtab *cust; if (sal.symtab == NULL) - return NULL; + return result; cust = sal.symtab->compunit (); if (cust->macro_table () == NULL) - return NULL; + return result; - gdb::unique_xmalloc_ptr ms (XNEW (struct macro_scope)); + macro_scope ms; main_file = macro_main (cust->macro_table ()); inclusion = macro_lookup_inclusion (main_file, sal.symtab->filename_for_id); if (inclusion) { - ms->file = inclusion; - ms->line = sal.line; + ms.file = inclusion; + ms.line = sal.line; } else { @@ -73,8 +74,8 @@ sal_macro_scope (struct symtab_and_line sal) For the time being, though, we'll just treat these as occurring at the end of the main source file. */ - ms->file = main_file; - ms->line = -1; + ms.file = main_file; + ms.line = -1; complaint (_("symtab found for `%s', but that file\n" "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 -user_macro_scope (void) +macro_scope +user_macro_scope () { - gdb::unique_xmalloc_ptr ms (XNEW (struct macro_scope)); - ms->file = macro_main (macro_user_macros); - ms->line = -1; - return ms; + return { macro_main (macro_user_macros), -1 }; } -gdb::unique_xmalloc_ptr -default_macro_scope (void) +macro_scope +default_macro_scope () { struct symtab_and_line sal; - gdb::unique_xmalloc_ptr ms; frame_info_ptr frame; CORE_ADDR pc; @@ -128,8 +125,8 @@ default_macro_scope (void) sal.line = cursal.line; } - ms = sal_macro_scope (sal); - if (! ms) + macro_scope ms = sal_macro_scope (sal); + if (!ms.is_valid ()) ms = user_macro_scope (); return ms; diff --git a/gdb/macroscope.h b/gdb/macroscope.h index de3186de503..2cb6a0f3db4 100644 --- a/gdb/macroscope.h +++ b/gdb/macroscope.h @@ -31,21 +31,25 @@ extern struct macro_table *macro_user_macros; in scope: a source file (either a main source file or an #inclusion), and a line number in that file. */ struct macro_scope { - struct macro_source_file *file; - int line; + struct macro_source_file *file = nullptr; + 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 and line given in SAL. If we have no macro information for that - location, or if SAL's pc is zero, return zero. */ -gdb::unique_xmalloc_ptr sal_macro_scope - (struct symtab_and_line sal); - + location, or if SAL's pc is zero, return an invalid scope. */ +macro_scope sal_macro_scope (struct symtab_and_line sal); /* Return a `struct macro_scope' object representing just the user-defined macros. */ -gdb::unique_xmalloc_ptr user_macro_scope (void); +macro_scope user_macro_scope (); /* Return a `struct macro_scope' object describing the scope the `macro expand' and `macro expand-once' commands should use for looking up @@ -54,7 +58,7 @@ gdb::unique_xmalloc_ptr user_macro_scope (void); If we have no macro information for the current location, return the user macro scope. */ -gdb::unique_xmalloc_ptr default_macro_scope (void); +macro_scope default_macro_scope (); /* Look up the definition of the macro named NAME in scope at the source location given by MS. */ diff --git a/gdb/symtab.c b/gdb/symtab.c index 241c32973aa..b2de990143d 100644 --- a/gdb/symtab.c +++ b/gdb/symtab.c @@ -6198,8 +6198,6 @@ default_collect_symbol_completion_matches_break_on if (current_language->macro_expansion () == macro_expansion_c && code == TYPE_CODE_UNDEF) { - gdb::unique_xmalloc_ptr scope; - /* This adds a macro's name to the current completion list. */ auto add_macro_name = [&] (const char *macro_name, const macro_definition *, @@ -6217,10 +6215,9 @@ default_collect_symbol_completion_matches_break_on resulting expression will be evaluated at "file:line" -- but at there does not seem to be a way to detect this at completion time. */ - scope = default_macro_scope (); - if (scope) - macro_for_each_in_scope (scope->file, scope->line, - add_macro_name); + macro_scope scope = default_macro_scope (); + if (scope.is_valid ()) + 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);