From 9b2f85815c57d2eb4322a3d87464b5686cdbb391 Mon Sep 17 00:00:00 2001 From: Tom Tromey Date: Sat, 31 Mar 2018 12:43:56 -0600 Subject: [PATCH] More use of std::vector in linespec.c 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. ChangeLog 2018-04-05 Tom Tromey * 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 | 52 +++++++++++++++++++------------------------------ gdb/objc-lang.c | 44 ++++++++++++----------------------------- gdb/objc-lang.h | 4 ++-- gdb/utils.c | 11 ----------- gdb/utils.h | 1 - 6 files changed, 47 insertions(+), 78 deletions(-) diff --git a/gdb/ChangeLog b/gdb/ChangeLog index f670b9daffd..7e8b307d540 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,3 +1,16 @@ +2018-04-05 Tom Tromey + + * 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. + 2018-04-05 Tom Tromey * completer.c (completion_tracker::completion_tracker): Remove diff --git a/gdb/linespec.c b/gdb/linespec.c index 1e1ce2a2397..b52e0061da7 100644 --- a/gdb/linespec.c +++ b/gdb/linespec.c @@ -377,10 +377,9 @@ static void add_matching_symbols_to_info (const char *name, struct collect_info *info, struct program_space *pspace); -static void add_all_symbol_names_from_pspace (struct collect_info *info, - struct program_space *pspace, - VEC (const_char_ptr) *names, - enum search_domain search_domain); +static void add_all_symbol_names_from_pspace + (struct collect_info *info, struct program_space *pspace, + const std::vector &names, enum search_domain search_domain); static VEC (symtab_ptr) * collect_symtabs_from_filename (const char *file, @@ -1211,7 +1210,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 *result_names, VEC (typep) **superclasses) { int ibase; @@ -1266,7 +1265,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); } } } @@ -3398,20 +3397,19 @@ static std::vector 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 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 {}; @@ -3637,13 +3635,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 &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); @@ -3652,9 +3647,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 *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); @@ -3669,8 +3664,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; @@ -3692,9 +3686,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 result_names; struct collect_info info; /* Sort symbols so that symbols with the same program space are next @@ -3720,8 +3714,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) { @@ -3746,7 +3738,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); @@ -3757,7 +3749,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 (); } } @@ -3902,9 +3894,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 symbol_names; info.state = state; info.result.symbols = NULL; @@ -3913,15 +3903,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 4f7dc366602..8c24fde3eed 100644 --- a/gdb/objc-lang.c +++ b/gdb/objc-lang.c @@ -45,6 +45,7 @@ #include "cli/cli-utils.h" #include +#include 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 *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 *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 *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 0727dfb3f08..d3ed1d5fa63 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 *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 bd7553e5f46..df0a2cecc4f 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 d3b8871b582..9a1fb345442 100644 --- a/gdb/utils.h +++ b/gdb/utils.h @@ -101,7 +101,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. */ -- 2.30.2