From patchwork Wed Apr 4 04:40:47 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 26594 Received: (qmail 86210 invoked by alias); 4 Apr 2018 04:41:00 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 85917 invoked by uid 89); 4 Apr 2018 04:40:58 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-25.1 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_STOCKGEN, RCVD_IN_DNSWL_NONE, SPF_HELO_PASS autolearn=ham version=3.3.2 spammy= X-HELO: gateway32.websitewelcome.com Received: from gateway32.websitewelcome.com (HELO gateway32.websitewelcome.com) (192.185.145.119) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Wed, 04 Apr 2018 04:40:54 +0000 Received: from cm15.websitewelcome.com (cm15.websitewelcome.com [100.42.49.9]) by gateway32.websitewelcome.com (Postfix) with ESMTP id 8A37BF8623D for ; Tue, 3 Apr 2018 23:40:53 -0500 (CDT) Received: from box5379.bluehost.com ([162.241.216.53]) by cmsmtp with SMTP id 3aE9fhf9FntAo3aE9fJpHY; Tue, 03 Apr 2018 23:40:53 -0500 Received: from 75-166-37-45.hlrn.qwest.net ([75.166.37.45]:43166 helo=bapiya.Home) by box5379.bluehost.com with esmtpsa (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.89_1) (envelope-from ) id 1f3aE9-000hxu-AV; Tue, 03 Apr 2018 23:40:53 -0500 From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [RFA v2 08/10] More use of std::vector in linespec.c Date: Tue, 3 Apr 2018 22:40:47 -0600 Message-Id: <20180404044049.31481-9-tom@tromey.com> In-Reply-To: <20180404044049.31481-1-tom@tromey.com> References: <20180404044049.31481-1-tom@tromey.com> X-BWhitelist: no X-Source-L: No X-Exim-ID: 1f3aE9-000hxu-AV X-Source-Sender: 75-166-37-45.hlrn.qwest.net (bapiya.Home) [75.166.37.45]:43166 X-Source-Auth: tom+tromey.com X-Email-Count: 9 X-Source-Cap: ZWx5bnJvYmk7ZWx5bnJvYmk7Ym94NTM3OS5ibHVlaG9zdC5jb20= X-Local-Domain: yes 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. gdb/ChangeLog 2018-04-03 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/linespec.c b/gdb/linespec.c index 1e1ce2a239..b52e0061da 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 4f7dc36660..8c24fde3ee 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 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 *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 d3b8871b58..9a1fb34544 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. */