[RFA,08/10] More use of std::vector in linespec.c

Message ID 20180401163539.15314-9-tom@tromey.com
State Superseded
Headers show
Series
  • Remove some cleanups from linespec.c
Related show

Commit Message

Tom Tromey April 1, 2018, 4:35 p.m.
This changes some spots in linespec.c to take a std::vector.  This
patch spilled out to objc-lang.c a bit as well.  This change allows
for the removal of some cleanups.

2018-03-31  Tom Tromey  <tom@tromey.com>

	* utils.c (compare_strings): Remove.
	* utils.h (compare_strings): Remove.
	* objc-lang.h (find_imps): Update.
	* objc-lang.c (find_methods): Take a std::vector.
	(uniquify_strings, find_imps): Likewise.
	* linespec.c (find_methods): Take a std::vector.
	(decode_objc): Use std::vector.
	(add_all_symbol_names_from_pspace, find_superclass_methods): Take
	a std::vector.
	(find_method, find_function_symbols): Use std::vector.
---
 gdb/ChangeLog   | 13 +++++++++++++
 gdb/linespec.c  | 47 ++++++++++++++++++-----------------------------
 gdb/objc-lang.c | 44 ++++++++++++--------------------------------
 gdb/objc-lang.h |  4 ++--
 gdb/utils.c     | 11 -----------
 gdb/utils.h     |  1 -
 6 files changed, 45 insertions(+), 75 deletions(-)

-- 
2.13.6

Comments

Simon Marchi April 2, 2018, 2:35 a.m. | #1
On 2018-04-01 12:35 PM, Tom Tromey wrote:
> This changes some spots in linespec.c to take a std::vector.  This

> patch spilled out to objc-lang.c a bit as well.  This change allows

> for the removal of some cleanups.

> 

> 2018-03-31  Tom Tromey  <tom@tromey.com>

> 

> 	* utils.c (compare_strings): Remove.

> 	* utils.h (compare_strings): Remove.

> 	* objc-lang.h (find_imps): Update.

> 	* objc-lang.c (find_methods): Take a std::vector.

> 	(uniquify_strings, find_imps): Likewise.

> 	* linespec.c (find_methods): Take a std::vector.

> 	(decode_objc): Use std::vector.

> 	(add_all_symbol_names_from_pspace, find_superclass_methods): Take

> 	a std::vector.

> 	(find_method, find_function_symbols): Use std::vector.

> ---

>  gdb/ChangeLog   | 13 +++++++++++++

>  gdb/linespec.c  | 47 ++++++++++++++++++-----------------------------

>  gdb/objc-lang.c | 44 ++++++++++++--------------------------------

>  gdb/objc-lang.h |  4 ++--

>  gdb/utils.c     | 11 -----------

>  gdb/utils.h     |  1 -

>  6 files changed, 45 insertions(+), 75 deletions(-)

> 

> diff --git a/gdb/linespec.c b/gdb/linespec.c

> index 48168eaaa6..96a3117293 100644

> --- a/gdb/linespec.c

> +++ b/gdb/linespec.c

> @@ -379,7 +379,7 @@ static void add_matching_symbols_to_info (const char *name,

>  

>  static void add_all_symbol_names_from_pspace (struct collect_info *info,

>  					      struct program_space *pspace,

> -					      VEC (const_char_ptr) *names,

> +					      const std::vector<const char *> &names,


This line is now too long, otherwise LGTM.

Simon

Patch

diff --git a/gdb/linespec.c b/gdb/linespec.c
index 48168eaaa6..96a3117293 100644
--- a/gdb/linespec.c
+++ b/gdb/linespec.c
@@ -379,7 +379,7 @@  static void add_matching_symbols_to_info (const char *name,
 
 static void add_all_symbol_names_from_pspace (struct collect_info *info,
 					      struct program_space *pspace,
-					      VEC (const_char_ptr) *names,
+					      const std::vector<const char *> &names,
 					      enum search_domain search_domain);
 
 static VEC (symtab_ptr) *
@@ -1211,7 +1211,7 @@  iterate_over_file_blocks
 
 static void
 find_methods (struct type *t, enum language t_lang, const char *name,
-	      VEC (const_char_ptr) **result_names,
+	      std::vector<const char *> *result_names,
 	      VEC (typep) **superclasses)
 {
   int ibase;
@@ -1266,7 +1266,7 @@  find_methods (struct type *t, enum language t_lang, const char *name,
 		  if (TYPE_FN_FIELD_STUB (f, field_counter))
 		    continue;
 		  phys_name = TYPE_FN_FIELD_PHYSNAME (f, field_counter);
-		  VEC_safe_push (const_char_ptr, *result_names, phys_name);
+		  result_names->push_back (phys_name);
 		}
 	    }
 	}
@@ -3401,20 +3401,19 @@  static std::vector<symtab_and_line>
 decode_objc (struct linespec_state *self, linespec_p ls, const char *arg)
 {
   struct collect_info info;
-  VEC (const_char_ptr) *symbol_names = NULL;
+  std::vector<const char *> symbol_names;
   const char *new_argptr;
-  struct cleanup *cleanup = make_cleanup (VEC_cleanup (const_char_ptr),
-					  &symbol_names);
 
   info.state = self;
   info.file_symtabs = NULL;
   VEC_safe_push (symtab_ptr, info.file_symtabs, NULL);
-  make_cleanup (VEC_cleanup (symtab_ptr), &info.file_symtabs);
+  struct cleanup *cleanup = make_cleanup (VEC_cleanup (symtab_ptr),
+					  &info.file_symtabs);
   info.result.symbols = NULL;
   info.result.minimal_symbols = NULL;
 
   new_argptr = find_imps (arg, &symbol_names);
-  if (VEC_empty (const_char_ptr, symbol_names))
+  if (symbol_names.empty ())
     {
       do_cleanups (cleanup);
       return {};
@@ -3640,13 +3639,10 @@  compare_msymbols (const void *a, const void *b)
 static void
 add_all_symbol_names_from_pspace (struct collect_info *info,
 				  struct program_space *pspace,
-				  VEC (const_char_ptr) *names,
+				  const std::vector<const char *> &names,
 				  enum search_domain search_domain)
 {
-  int ix;
-  const char *iter;
-
-  for (ix = 0; VEC_iterate (const_char_ptr, names, ix, iter); ++ix)
+  for (const char *iter : names)
     add_matching_symbols_to_info (iter,
 				  symbol_name_match_type::FULL,
 				  search_domain, info, pspace);
@@ -3655,9 +3651,9 @@  add_all_symbol_names_from_pspace (struct collect_info *info,
 static void
 find_superclass_methods (VEC (typep) *superclasses,
 			 const char *name, enum language name_lang,
-			 VEC (const_char_ptr) **result_names)
+			 std::vector<const char *> *result_names)
 {
-  int old_len = VEC_length (const_char_ptr, *result_names);
+  size_t old_len = result_names->size ();
   VEC (typep) *iter_classes;
   struct cleanup *cleanup = make_cleanup (null_cleanup, NULL);
 
@@ -3672,8 +3668,7 @@  find_superclass_methods (VEC (typep) *superclasses,
       for (ix = 0; VEC_iterate (typep, iter_classes, ix, t); ++ix)
 	find_methods (t, name_lang, name, result_names, &new_supers);
 
-      if (VEC_length (const_char_ptr, *result_names) != old_len
-	  || VEC_empty (typep, new_supers))
+      if (result_names->size () != old_len || VEC_empty (typep, new_supers))
 	break;
 
       iter_classes = new_supers;
@@ -3695,9 +3690,9 @@  find_method (struct linespec_state *self, VEC (symtab_ptr) *file_symtabs,
   struct symbol *sym;
   struct cleanup *cleanup = make_cleanup (null_cleanup, NULL);
   int ix;
-  int last_result_len;
+  size_t last_result_len;
   VEC (typep) *superclass_vec;
-  VEC (const_char_ptr) *result_names;
+  std::vector<const char *> result_names;
   struct collect_info info;
 
   /* Sort symbols so that symbols with the same program space are next
@@ -3723,8 +3718,6 @@  find_method (struct linespec_state *self, VEC (symtab_ptr) *file_symtabs,
      what to do.  */
   superclass_vec = NULL;
   make_cleanup (VEC_cleanup (typep), &superclass_vec);
-  result_names = NULL;
-  make_cleanup (VEC_cleanup (const_char_ptr), &result_names);
   last_result_len = 0;
   for (ix = 0; VEC_iterate (symbolp, sym_classes, ix, sym); ++ix)
     {
@@ -3749,7 +3742,7 @@  find_method (struct linespec_state *self, VEC (symtab_ptr) *file_symtabs,
 	{
 	  /* If we did not find a direct implementation anywhere in
 	     this program space, consider superclasses.  */
-	  if (VEC_length (const_char_ptr, result_names) == last_result_len)
+	  if (result_names.size () == last_result_len)
 	    find_superclass_methods (superclass_vec, method_name,
 				     SYMBOL_LANGUAGE (sym), &result_names);
 
@@ -3760,7 +3753,7 @@  find_method (struct linespec_state *self, VEC (symtab_ptr) *file_symtabs,
 					    FUNCTIONS_DOMAIN);
 
 	  VEC_truncate (typep, superclass_vec, 0);
-	  last_result_len = VEC_length (const_char_ptr, result_names);
+	  last_result_len = result_names.size ();
 	}
     }
 
@@ -3905,9 +3898,7 @@  find_function_symbols (struct linespec_state *state,
 		       VEC (bound_minimal_symbol_d) **minsyms)
 {
   struct collect_info info;
-  VEC (const_char_ptr) *symbol_names = NULL;
-  struct cleanup *cleanup = make_cleanup (VEC_cleanup (const_char_ptr),
-					  &symbol_names);
+  std::vector<const char *> symbol_names;
 
   info.state = state;
   info.result.symbols = NULL;
@@ -3916,15 +3907,13 @@  find_function_symbols (struct linespec_state *state,
 
   /* Try NAME as an Objective-C selector.  */
   find_imps (name, &symbol_names);
-  if (!VEC_empty (const_char_ptr, symbol_names))
+  if (!symbol_names.empty ())
     add_all_symbol_names_from_pspace (&info, state->search_pspace,
 				      symbol_names, FUNCTIONS_DOMAIN);
   else
     add_matching_symbols_to_info (name, name_match_type, FUNCTIONS_DOMAIN,
 				  &info, state->search_pspace);
 
-  do_cleanups (cleanup);
-
   if (VEC_empty (symbolp, info.result.symbols))
     {
       VEC_free (symbolp, info.result.symbols);
diff --git a/gdb/objc-lang.c b/gdb/objc-lang.c
index 4f7dc36660..8c24fde3ee 100644
--- a/gdb/objc-lang.c
+++ b/gdb/objc-lang.c
@@ -45,6 +45,7 @@ 
 #include "cli/cli-utils.h"
 
 #include <ctype.h>
+#include <algorithm>
 
 struct objc_object {
   CORE_ADDR isa;
@@ -974,7 +975,7 @@  parse_method (char *method, char *type, char **theclass,
 static void
 find_methods (char type, const char *theclass, const char *category, 
 	      const char *selector,
-	      VEC (const_char_ptr) **symbol_names)
+	      std::vector<const char *> *symbol_names)
 {
   struct objfile *objfile = NULL;
 
@@ -1050,7 +1051,7 @@  find_methods (char type, const char *theclass, const char *category,
 	      ((nselector == NULL) || (strcmp (selector, nselector) != 0)))
 	    continue;
 
-	  VEC_safe_push (const_char_ptr, *symbol_names, symname);
+	  symbol_names->push_back (symname);
 	}
 
       if (objc_csym == NULL)
@@ -1068,33 +1069,14 @@  find_methods (char type, const char *theclass, const char *category,
 /* Uniquify a VEC of strings.  */
 
 static void
-uniquify_strings (VEC (const_char_ptr) **strings)
+uniquify_strings (std::vector<const char *> *strings)
 {
-  int ix;
-  const char *elem, *last = NULL;
-  int out;
-
-  /* If the vector is empty, there's nothing to do.  This explicit
-     check is needed to avoid invoking qsort with NULL. */
-  if (VEC_empty (const_char_ptr, *strings))
+  if (strings->empty ())
     return;
 
-  qsort (VEC_address (const_char_ptr, *strings),
-	 VEC_length (const_char_ptr, *strings),
-	 sizeof (const_char_ptr),
-	 compare_strings);
-  out = 0;
-  for (ix = 0; VEC_iterate (const_char_ptr, *strings, ix, elem); ++ix)
-    {
-      if (last == NULL || strcmp (last, elem) != 0)
-	{
-	  /* Keep ELEM.  */
-	  VEC_replace (const_char_ptr, *strings, out, elem);
-	  ++out;
-	}
-      last = elem;
-    }
-  VEC_truncate (const_char_ptr, *strings, out);
+  std::sort (strings->begin (), strings->end (), compare_cstrings);
+  strings->erase (std::unique (strings->begin (), strings->end (), streq),
+		  strings->end ());
 }
 
 /* 
@@ -1128,7 +1110,7 @@  uniquify_strings (VEC (const_char_ptr) **strings)
  */
 
 const char *
-find_imps (const char *method, VEC (const_char_ptr) **symbol_names)
+find_imps (const char *method, std::vector<const char *> *symbol_names)
 {
   char type = '\0';
   char *theclass = NULL;
@@ -1161,22 +1143,20 @@  find_imps (const char *method, VEC (const_char_ptr) **symbol_names)
 
   /* If we hit the "selector" case, and we found some methods, then
      add the selector itself as a symbol, if it exists.  */
-  if (selector_case && !VEC_empty (const_char_ptr, *symbol_names))
+  if (selector_case && !symbol_names->empty ())
     {
       struct symbol *sym = lookup_symbol (selector, NULL, VAR_DOMAIN,
 					  0).symbol;
 
       if (sym != NULL) 
-	VEC_safe_push (const_char_ptr, *symbol_names,
-		       SYMBOL_NATURAL_NAME (sym));
+	symbol_names->push_back (SYMBOL_NATURAL_NAME (sym));
       else
 	{
 	  struct bound_minimal_symbol msym
 	    = lookup_minimal_symbol (selector, 0, 0);
 
 	  if (msym.minsym != NULL) 
-	    VEC_safe_push (const_char_ptr, *symbol_names,
-			   MSYMBOL_NATURAL_NAME (msym.minsym));
+	    symbol_names->push_back (MSYMBOL_NATURAL_NAME (msym.minsym));
 	}
     }
 
diff --git a/gdb/objc-lang.h b/gdb/objc-lang.h
index 0727dfb3f0..d3ed1d5fa6 100644
--- a/gdb/objc-lang.h
+++ b/gdb/objc-lang.h
@@ -37,8 +37,8 @@  extern char *objc_demangle (const char *mangled, int options);
 
 extern int find_objc_msgcall (CORE_ADDR pc, CORE_ADDR *new_pc);
 
-extern const char *
-  find_imps (const char *method, VEC (const_char_ptr) **symbol_names);
+extern const char *find_imps (const char *method,
+			      std::vector<const char *> *symbol_names);
 
 extern struct value *value_nsstring (struct gdbarch *gdbarch,
 				     char *ptr, int len);
diff --git a/gdb/utils.c b/gdb/utils.c
index bd7553e5f4..df0a2cecc4 100644
--- a/gdb/utils.c
+++ b/gdb/utils.c
@@ -2937,17 +2937,6 @@  compare_positive_ints (const void *ap, const void *bp)
   return * (int *) ap - * (int *) bp;
 }
 
-/* String compare function for qsort.  */
-
-int
-compare_strings (const void *arg1, const void *arg2)
-{
-  const char **s1 = (const char **) arg1;
-  const char **s2 = (const char **) arg2;
-
-  return strcmp (*s1, *s2);
-}
-
 #define AMBIGUOUS_MESS1	".\nMatching formats:"
 #define AMBIGUOUS_MESS2	\
   ".\nUse \"set gnutarget format-name\" to specify the format."
diff --git a/gdb/utils.h b/gdb/utils.h
index 7c211391e0..ee69827558 100644
--- a/gdb/utils.h
+++ b/gdb/utils.h
@@ -102,7 +102,6 @@  extern int streq_hash (const void *, const void *);
 extern int subset_compare (const char *, const char *);
 
 int compare_positive_ints (const void *ap, const void *bp);
-int compare_strings (const void *ap, const void *bp);
 
 /* Compare C strings for std::sort.  */