]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
Do not allocate macro_scope on the heap
authorTom Tromey <tom@tromey.com>
Thu, 29 May 2025 23:23:04 +0000 (17:23 -0600)
committerTom Tromey <tom@tromey.com>
Sat, 31 May 2025 14:50:34 +0000 (08:50 -0600)
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>
gdb/c-exp.y
gdb/compile/compile-c-support.c
gdb/macrocmd.c
gdb/macroscope.c
gdb/macroscope.h
gdb/symtab.c

index bdf76744817860f8409e314a6253aa0b0de31b9a..14d4b704ff1f5731804b1d3bab4c00649dac40ea 100644 (file)
@@ -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<struct macro_scope> 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, &macro_scope);
 
   scoped_restore restore_yydebug = make_scoped_restore (&yydebug,
                                                        par_state->debug);
index 0d32350795527775c2dfc3df79d2715de65ef914..ac8888fb16bbcc70a025e3dc517411dcfb3f0907 100644 (file)
@@ -185,18 +185,18 @@ static void
 write_macro_definitions (const struct block *block, CORE_ADDR pc,
                         struct ui_file *file)
 {
-  gdb::unique_xmalloc_ptr<struct macro_scope> 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,
index 0192a0077a180b7e99f3341af3d7397a11b62015..86b6839867e9d9287468f62039b90a8427097482 100644 (file)
@@ -57,11 +57,11 @@ macro_expand_command (const char *exp, int from_tty)
           " expression you\n"
           "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 (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<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 (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<struct macro_scope> 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<struct macro_scope> 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);
 }
 
 \f
index 97b41b62876328c9f6f58ef6ea3efb6f20884fd4..633ead19501a0c286f2d4152b510f469ca674677 100644 (file)
 struct macro_table *macro_user_macros;
 
 
-gdb::unique_xmalloc_ptr<struct macro_scope>
+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<struct macro_scope> 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<struct macro_scope>
-user_macro_scope (void)
+macro_scope
+user_macro_scope ()
 {
-  gdb::unique_xmalloc_ptr<struct macro_scope> 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<struct macro_scope>
-default_macro_scope (void)
+macro_scope
+default_macro_scope ()
 {
   struct symtab_and_line sal;
-  gdb::unique_xmalloc_ptr<struct macro_scope> 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;
index de3186de5034b5822427955eedd4550b22fee73d..2cb6a0f3db4274c27a08f3fd9e567b8097dfe82f 100644 (file)
@@ -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<struct macro_scope> 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<struct macro_scope> 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<struct macro_scope> user_macro_scope (void);
 
    If we have no macro information for the current location, return
    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
    location given by MS.  */
index 241c32973aaa05c89cdcd2e88bcff96e4bad7c84..b2de990143db468eb98a9d9eb24c42ab2519cb4a 100644 (file)
@@ -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<struct macro_scope> 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);