]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
Remove some cleanups from search_minsyms_for_name
authorTom Tromey <tom@tromey.com>
Sat, 31 Mar 2018 16:32:00 +0000 (10:32 -0600)
committerTom Tromey <tom@tromey.com>
Thu, 5 Apr 2018 13:39:33 +0000 (07:39 -0600)
This changes struct collect_minsyms to use a std::vector, which
enables the removal of a cleanup from search_minsyms_for_name.  This
also changes iterate_over_minimal_symbols to take a
gdb::function_view, which makes a function in linespec.c more
type-safe.

ChangeLog
2018-04-05  Tom Tromey  <tom@tromey.com>

* minsyms.h (iterate_over_minimal_symbols): Update.
* minsyms.c (iterate_over_minimal_symbols): Take a
gdb::function_view.
* linespec.c (struct collect_minsyms): Remove.
(compare_msyms): Now a std::sort comparator.
(add_minsym): Add parameters.
(search_minsyms_for_name): Update.  Use std::vector.

gdb/ChangeLog
gdb/linespec.c
gdb/minsyms.c
gdb/minsyms.h

index 6dfae4eded67108de7dcd02a82a18b66bc12395f..e0efcb58cad2f412debd75128a017a94247accc8 100644 (file)
@@ -1,3 +1,13 @@
+2018-04-05  Tom Tromey  <tom@tromey.com>
+
+       * minsyms.h (iterate_over_minimal_symbols): Update.
+       * minsyms.c (iterate_over_minimal_symbols): Take a
+       gdb::function_view.
+       * linespec.c (struct collect_minsyms): Remove.
+       (compare_msyms): Now a std::sort comparator.
+       (add_minsym): Add parameters.
+       (search_minsyms_for_name): Update.  Use std::vector.
+
 2018-04-03  Tom Tromey  <tom@tromey.com>
 
        * mipsread.c (read_alphacoff_dynamic_symtab): Use
index 1236b3f4754af8a90ecbb1190a7de6f359c60211..31ebca37c42134c28125c26f5f1c25bcef2db279 100644 (file)
@@ -46,6 +46,7 @@
 #include "location.h"
 #include "common/function-view.h"
 #include "common/def-vector.h"
+#include <algorithm>
 
 /* An enumeration of the various things a user might attempt to
    complete for a linespec location.  */
@@ -4370,27 +4371,6 @@ minsym_found (struct linespec_state *self, struct objfile *objfile,
     add_sal_to_sals (self, result, &sal, MSYMBOL_NATURAL_NAME (msymbol), 0);
 }
 
-/* A helper struct to pass some data through
-   iterate_over_minimal_symbols.  */
-
-struct collect_minsyms
-{
-  /* The objfile we're examining.  */
-  struct objfile *objfile;
-
-  /* Only search the given symtab, or NULL to search for all symbols.  */
-  struct symtab *symtab;
-
-  /* The funfirstline setting from the initial call.  */
-  int funfirstline;
-
-  /* The list_mode setting from the initial call.  */
-  int list_mode;
-
-  /* The resulting symbols.  */
-  VEC (bound_minimal_symbol_d) *msyms;
-};
-
 /* A helper function to classify a minimal_symbol_type according to
    priority.  */
 
@@ -4415,47 +4395,45 @@ classify_mtype (enum minimal_symbol_type t)
     }
 }
 
-/* Callback for qsort that sorts symbols by priority.  */
+/* Callback for std::sort that sorts symbols by priority.  */
 
-static int
-compare_msyms (const void *a, const void *b)
+static bool
+compare_msyms (const bound_minimal_symbol &a, const bound_minimal_symbol &b)
 {
-  const bound_minimal_symbol_d *moa = (const bound_minimal_symbol_d *) a;
-  const bound_minimal_symbol_d *mob = (const bound_minimal_symbol_d *) b;
-  enum minimal_symbol_type ta = MSYMBOL_TYPE (moa->minsym);
-  enum minimal_symbol_type tb = MSYMBOL_TYPE (mob->minsym);
+  enum minimal_symbol_type ta = MSYMBOL_TYPE (a.minsym);
+  enum minimal_symbol_type tb = MSYMBOL_TYPE (b.minsym);
 
-  return classify_mtype (ta) - classify_mtype (tb);
+  return classify_mtype (ta) < classify_mtype (tb);
 }
 
-/* Callback for iterate_over_minimal_symbols that adds the symbol to
-   the result.  */
+/* Helper for search_minsyms_for_name that adds the symbol to the
+   result.  */
 
 static void
-add_minsym (struct minimal_symbol *minsym, void *d)
+add_minsym (struct minimal_symbol *minsym, struct objfile *objfile,
+           struct symtab *symtab, int list_mode,
+           std::vector<struct bound_minimal_symbol> *msyms)
 {
-  struct collect_minsyms *info = (struct collect_minsyms *) d;
-
-  if (info->symtab != NULL)
+  if (symtab != NULL)
     {
       /* We're looking for a label for which we don't have debug
         info.  */
       CORE_ADDR func_addr;
-      if (msymbol_is_function (info->objfile, minsym, &func_addr))
+      if (msymbol_is_function (objfile, minsym, &func_addr))
        {
          symtab_and_line sal = find_pc_sect_line (func_addr, NULL, 0);
 
-         if (info->symtab != sal.symtab)
+         if (symtab != sal.symtab)
            return;
        }
     }
 
   /* Exclude data symbols when looking for breakpoint locations.  */
-  if (!info->list_mode && !msymbol_is_function (info->objfile, minsym))
+  if (!list_mode && !msymbol_is_function (objfile, minsym))
     return;
 
-  bound_minimal_symbol_d mo = {minsym, info->objfile};
-  VEC_safe_push (bound_minimal_symbol_d, info->msyms, &mo);
+  struct bound_minimal_symbol mo = {minsym, objfile};
+  msyms->push_back (mo);
 }
 
 /* Search for minimal symbols called NAME.  If SEARCH_PSPACE
@@ -4471,15 +4449,7 @@ search_minsyms_for_name (struct collect_info *info,
                         struct program_space *search_pspace,
                         struct symtab *symtab)
 {
-  struct collect_minsyms local;
-  struct cleanup *cleanup;
-
-  memset (&local, 0, sizeof (local));
-  local.funfirstline = info->state->funfirstline;
-  local.list_mode = info->state->list_mode;
-  local.symtab = symtab;
-
-  cleanup = make_cleanup (VEC_cleanup (bound_minimal_symbol_d), &local.msyms);
+  std::vector<struct bound_minimal_symbol> minsyms;
 
   if (symtab == NULL)
     {
@@ -4498,8 +4468,13 @@ search_minsyms_for_name (struct collect_info *info,
 
        ALL_OBJFILES (objfile)
        {
-         local.objfile = objfile;
-         iterate_over_minimal_symbols (objfile, name, add_minsym, &local);
+         iterate_over_minimal_symbols (objfile, name,
+                                       [&] (struct minimal_symbol *msym)
+                                         {
+                                           add_minsym (msym, objfile, nullptr,
+                                                       info->state->list_mode,
+                                                       &minsyms);
+                                         });
        }
       }
     }
@@ -4508,41 +4483,36 @@ search_minsyms_for_name (struct collect_info *info,
       if (search_pspace == NULL || SYMTAB_PSPACE (symtab) == search_pspace)
        {
          set_current_program_space (SYMTAB_PSPACE (symtab));
-         local.objfile = SYMTAB_OBJFILE(symtab);
-         iterate_over_minimal_symbols (local.objfile, name, add_minsym, &local);
+         iterate_over_minimal_symbols
+           (SYMTAB_OBJFILE (symtab), name,
+            [&] (struct minimal_symbol *msym)
+              {
+                add_minsym (msym, SYMTAB_OBJFILE (symtab), symtab,
+                            info->state->list_mode, &minsyms);
+              });
        }
     }
 
-    if (!VEC_empty (bound_minimal_symbol_d, local.msyms))
-      {
-       int classification;
-       int ix;
-       bound_minimal_symbol_d *item;
-
-       qsort (VEC_address (bound_minimal_symbol_d, local.msyms),
-              VEC_length (bound_minimal_symbol_d, local.msyms),
-              sizeof (bound_minimal_symbol_d),
-              compare_msyms);
-
-       /* Now the minsyms are in classification order.  So, we walk
-          over them and process just the minsyms with the same
-          classification as the very first minsym in the list.  */
-       item = VEC_index (bound_minimal_symbol_d, local.msyms, 0);
-       classification = classify_mtype (MSYMBOL_TYPE (item->minsym));
-
-       for (ix = 0;
-            VEC_iterate (bound_minimal_symbol_d, local.msyms, ix, item);
-            ++ix)
-         {
-           if (classify_mtype (MSYMBOL_TYPE (item->minsym)) != classification)
-             break;
+  if (!minsyms.empty ())
+    {
+      int classification;
 
-           VEC_safe_push (bound_minimal_symbol_d,
-                          info->result.minimal_symbols, item);
-         }
-      }
+      std::sort (minsyms.begin (), minsyms.end (), compare_msyms);
 
-    do_cleanups (cleanup);
+      /* Now the minsyms are in classification order.  So, we walk
+        over them and process just the minsyms with the same
+        classification as the very first minsym in the list.  */
+      classification = classify_mtype (MSYMBOL_TYPE (minsyms[0].minsym));
+
+      for (const struct bound_minimal_symbol &item : minsyms)
+       {
+         if (classify_mtype (MSYMBOL_TYPE (item.minsym)) != classification)
+           break;
+
+         VEC_safe_push (bound_minimal_symbol_d,
+                        info->result.minimal_symbols, &item);
+       }
+    }
 }
 
 /* A helper function to add all symbols matching NAME to INFO.  If
index 72969b77787f9ce1a327b475ff5005e10affe588..9d23c4fd4fd269f8cbd34b14c1fe0c921a3c0458 100644 (file)
@@ -469,13 +469,10 @@ linkage_name_str (const lookup_name_info &lookup_name)
 /* See minsyms.h.  */
 
 void
-iterate_over_minimal_symbols (struct objfile *objf,
-                             const lookup_name_info &lookup_name,
-                             void (*callback) (struct minimal_symbol *,
-                                               void *),
-                             void *user_data)
+iterate_over_minimal_symbols
+    (struct objfile *objf, const lookup_name_info &lookup_name,
+     gdb::function_view<void (struct minimal_symbol *)> callback)
 {
-
   /* The first pass is over the ordinary hash table.  */
     {
       const char *name = linkage_name_str (lookup_name);
@@ -490,7 +487,7 @@ iterate_over_minimal_symbols (struct objfile *objf,
           iter = iter->hash_next)
        {
          if (mangled_cmp (MSYMBOL_LINKAGE_NAME (iter), name) == 0)
-           (*callback) (iter, user_data);
+           callback (iter);
        }
     }
 
@@ -509,7 +506,7 @@ iterate_over_minimal_symbols (struct objfile *objf,
           iter != NULL;
           iter = iter->demangled_hash_next)
        if (name_match (MSYMBOL_SEARCH_NAME (iter), lookup_name, NULL))
-         (*callback) (iter, user_data);
+         callback (iter);
     }
 }
 
index 11a202025d349cbef2393d6d035defa1ecf67cec..a2b7ddd7035e0d00c65c8ff0184993dbc26d269a 100644 (file)
@@ -265,14 +265,11 @@ struct bound_minimal_symbol lookup_minimal_symbol_by_pc (CORE_ADDR);
    are considered.  The caller is responsible for canonicalizing NAME,
    should that need to be done.
 
-   For each matching symbol, CALLBACK is called with the symbol and
-   USER_DATA as arguments.  */
-
-void iterate_over_minimal_symbols (struct objfile *objf,
-                                  const lookup_name_info &name,
-                                  void (*callback) (struct minimal_symbol *,
-                                                    void *),
-                                  void *user_data);
+   For each matching symbol, CALLBACK is called with the symbol.  */
+
+void iterate_over_minimal_symbols
+    (struct objfile *objf, const lookup_name_info &name,
+     gdb::function_view<void (struct minimal_symbol *)> callback);
 
 /* Compute the upper bound of MINSYM.  The upper bound is the last
    address thought to be part of the symbol.  If the symbol has a