aboutsummaryrefslogtreecommitdiff
path: root/gdb/symtab.c
diff options
context:
space:
mode:
authorPedro Alves <palves@redhat.com>2017-02-22 14:43:35 +0000
committerPedro Alves <palves@redhat.com>2017-02-23 16:16:06 +0000
commit14bc53a81471e0b550de1c24d4d5266f676aacc3 (patch)
treeed7da34bc20667f7ee0a430372b2cfb942fda6ee /gdb/symtab.c
parent07e253aa3b7a530f22b84053e661842ccd9da2ea (diff)
downloadgdb-14bc53a81471e0b550de1c24d4d5266f676aacc3.zip
gdb-14bc53a81471e0b550de1c24d4d5266f676aacc3.tar.gz
gdb-14bc53a81471e0b550de1c24d4d5266f676aacc3.tar.bz2
Use gdb::function_view in iterate_over_symtabs & co
I wanted to pass a lambda to iterate_over_symtabs (see following patch), so I converted it to function_view, and then the rest is cascaded from that. This gets rid of a bunch of single-use callback functions and corresponding manually managed callback capture types (add_partial_datum, search_symbols_data, etc.) in favor of letting the compiler generate them for us by using lambdas with a capture. In a couple cases, it was more natural to convert the existing function callbacks to function objects (i.e., operator(), e.g., decode_compound_collector). gdb/ChangeLog: 2017-02-23 Pedro Alves <palves@redhat.com> * ada-lang.c: Include "common/function-view.h". (ada_iterate_over_symbols): Adjust to use function_view as callback type. (struct add_partial_datum, ada_complete_symbol_matcher): Delete. (ada_make_symbol_completion_list): Use a lambda. (ada_exc_search_name_matches): Delete. (name_matches_regex): New. (ada_add_global_exceptions): Use a lambda and name_matches_regex. * compile/compile-c-support.c: Include "common/function-view.h". (print_one_macro): Change prototype to accept a ui_file pointer. (write_macro_definitions): Use a lambda. * dwarf2read.c: Include "common/function-view.h". (dw2_map_expand_apply, dw2_map_symtabs_matching_filename) (dw2_expand_symtabs_matching): Adjust to use function_view as callback type. * language.h: Include "common/function-view.h". (struct language_defn) <la_iterate_over_symbols>: Adjust to use function_view as callback type. (LA_ITERATE_OVER_SYMBOLS): Remove DATA parameter. * linespec.c: Include "common/function-view.h". (collect_info::add_symbol): New method. (struct symbol_and_data_callback, iterate_inline_only, struct symbol_matcher_data, iterate_name_matcher): Delete. (iterate_over_all_matching_symtabs): Adjust to use function_view as callback type and lambdas. (iterate_over_file_blocks): Adjust to use function_view as callback type. (decode_compound_collector): Now a class with private fields. (decode_compound_collector::release_symbols): New method. (collect_one_symbol): Rename to... (decode_compound_collector::operator()): ... this and adjust. (lookup_prefix_sym): decode_compound_collector construction bits move to decode_compound_collector ctor. Pass the decode_compound_collector object directly as callback. Remove cleanups and use decode_compound_collector::release_symbols instead. (symtab_collector): Now a class with private fields. (symtab_collector::release_symtabs): New method. (add_symtabs_to_list): Rename to... (symtab_collector::operator()): ... this and adjust. (collect_symtabs_from_filename): symtab_collector construction bits move to symtab_collector ctor. Pass the symtab_collector object directly as callback. Remove cleanups and use symtab_collector::release_symtabs instead. (collect_symbols): Delete. (add_matching_symbols_to_info): Use lambdas. * macrocmd.c (print_macro_callback): Delete. (info_macro_command): Use a lambda. (info_macros_command): Pass print_macro_definition as callable directly. (print_one_macro): Remove 'ignore' parameter. (macro_list_command): Adjust. * macrotab.c (macro_for_each_data::fn): Now a function_view. (macro_for_each_data::user_data): Delete field. (foreach_macro): Adjust to call the function_view. (macro_for_each): Adjust to use function_view as callback type. (foreach_macro_in_scope): Adjust to call the function_view. (macro_for_each_in_scope): Adjust to use function_view as callback type. * macrotab.h: Include "common/function-view.h". (macro_callback_fn): Declare a prototype instead of a pointer. Remove "user_data" parameter. (macro_for_each, macro_for_each_in_scope): Adjust to use function_view as callback type. * psymtab.c (partial_map_expand_apply) (psym_map_symtabs_matching_filename, recursively_search_psymtabs): Adjust to use function_view as callback type and to return bool. (psym_expand_symtabs_matching): Adjust to use function_view as callback types. * symfile-debug.c (debug_qf_map_symtabs_matching_filename): Adjust to use function_view as callback type and to return bool. (debug_qf_expand_symtabs_matching): Adjust to use function_view as callback types. * symfile.c (expand_symtabs_matching): Adjust to use function_view as callback types. * symfile.h: Include "common/function-view.h". (expand_symtabs_file_matcher_ftype) (expand_symtabs_symbol_matcher_ftype) (expand_symtabs_exp_notify_ftype): Remove "data" parameter and return bool. (quick_symbol_functions::map_symtabs_matching_filename) (quick_symbol_functions::expand_symtabs_matching): Adjust to use function_view as callback type and return bool. (expand_symtabs_matching): Adjust to use function_view as callback type. (maintenance_expand_name_matcher) (maintenance_expand_file_matcher): Delete. (maintenance_expand_symtabs): Use lambdas. * symtab.c (iterate_over_some_symtabs): Adjust to use function_view as callback types and return bool. (iterate_over_symtabs): Likewise. Use unique_xmalloc_ptr instead of a cleanup. (lookup_symtab_callback): Delete. (lookup_symtab): Use a lambda. (iterate_over_symbols): Adjust to use function_view as callback type. (struct search_symbols_data, search_symbols_file_matches) (search_symbols_name_matches): Delete. (search_symbols): Use a pair of lambdas. (struct add_name_data, add_macro_name, symbol_completion_matcher) (symtab_expansion_callback): Delete. (default_make_symbol_completion_list_break_on_1): Use lambdas. * symtab.h: Include "common/function-view.h". (iterate_over_some_symtabs): Adjust to use function_view as callback type and return bool. (iterate_over_symtabs): Adjust to use function_view as callback type. (symbol_found_callback_ftype): Remove 'data' parameter and return bool. (iterate_over_symbols): Adjust to use function_view as callback type.
Diffstat (limited to 'gdb/symtab.c')
-rw-r--r--gdb/symtab.c287
1 files changed, 98 insertions, 189 deletions
diff --git a/gdb/symtab.c b/gdb/symtab.c
index 2c141e5..b9f4f77 100644
--- a/gdb/symtab.c
+++ b/gdb/symtab.c
@@ -382,23 +382,20 @@ compare_glob_filenames_for_search (const char *filename,
If NAME is not absolute, then REAL_PATH is NULL
If NAME is absolute, then REAL_PATH is the gdb_realpath form of NAME.
- The return value, NAME, REAL_PATH, CALLBACK, and DATA
- are identical to the `map_symtabs_matching_filename' method of
- quick_symbol_functions.
+ The return value, NAME, REAL_PATH and CALLBACK are identical to the
+ `map_symtabs_matching_filename' method of quick_symbol_functions.
FIRST and AFTER_LAST indicate the range of compunit symtabs to search.
Each symtab within the specified compunit symtab is also searched.
AFTER_LAST is one past the last compunit symtab to search; NULL means to
search until the end of the list. */
-int
+bool
iterate_over_some_symtabs (const char *name,
const char *real_path,
- int (*callback) (struct symtab *symtab,
- void *data),
- void *data,
struct compunit_symtab *first,
- struct compunit_symtab *after_last)
+ struct compunit_symtab *after_last,
+ gdb::function_view<bool (symtab *)> callback)
{
struct compunit_symtab *cust;
struct symtab *s;
@@ -410,8 +407,8 @@ iterate_over_some_symtabs (const char *name,
{
if (compare_filenames_for_search (s->filename, name))
{
- if (callback (s, data))
- return 1;
+ if (callback (s))
+ return true;
continue;
}
@@ -423,8 +420,8 @@ iterate_over_some_symtabs (const char *name,
if (compare_filenames_for_search (symtab_to_fullname (s), name))
{
- if (callback (s, data))
- return 1;
+ if (callback (s))
+ return true;
continue;
}
@@ -438,82 +435,59 @@ iterate_over_some_symtabs (const char *name,
gdb_assert (IS_ABSOLUTE_PATH (name));
if (FILENAME_CMP (real_path, fullname) == 0)
{
- if (callback (s, data))
- return 1;
+ if (callback (s))
+ return true;
continue;
}
}
}
}
- return 0;
+ return false;
}
/* Check for a symtab of a specific name; first in symtabs, then in
psymtabs. *If* there is no '/' in the name, a match after a '/'
in the symtab filename will also work.
- Calls CALLBACK with each symtab that is found and with the supplied
- DATA. If CALLBACK returns true, the search stops. */
+ Calls CALLBACK with each symtab that is found. If CALLBACK returns
+ true, the search stops. */
void
iterate_over_symtabs (const char *name,
- int (*callback) (struct symtab *symtab,
- void *data),
- void *data)
+ gdb::function_view<bool (symtab *)> callback)
{
struct objfile *objfile;
- char *real_path = NULL;
- struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
+ gdb::unique_xmalloc_ptr<char> real_path;
/* Here we are interested in canonicalizing an absolute path, not
absolutizing a relative path. */
if (IS_ABSOLUTE_PATH (name))
{
- real_path = gdb_realpath (name);
- make_cleanup (xfree, real_path);
- gdb_assert (IS_ABSOLUTE_PATH (real_path));
+ real_path.reset (gdb_realpath (name));
+ gdb_assert (IS_ABSOLUTE_PATH (real_path.get ()));
}
ALL_OBJFILES (objfile)
- {
- if (iterate_over_some_symtabs (name, real_path, callback, data,
- objfile->compunit_symtabs, NULL))
- {
- do_cleanups (cleanups);
+ {
+ if (iterate_over_some_symtabs (name, real_path.get (),
+ objfile->compunit_symtabs, NULL,
+ callback))
return;
- }
- }
+ }
/* Same search rules as above apply here, but now we look thru the
psymtabs. */
ALL_OBJFILES (objfile)
- {
- if (objfile->sf
- && objfile->sf->qf->map_symtabs_matching_filename (objfile,
- name,
- real_path,
- callback,
- data))
- {
- do_cleanups (cleanups);
+ {
+ if (objfile->sf
+ && objfile->sf->qf->map_symtabs_matching_filename (objfile,
+ name,
+ real_path.get (),
+ callback))
return;
- }
- }
-
- do_cleanups (cleanups);
-}
-
-/* The callback function used by lookup_symtab. */
-
-static int
-lookup_symtab_callback (struct symtab *symtab, void *data)
-{
- struct symtab **result_ptr = (struct symtab **) data;
-
- *result_ptr = symtab;
- return 1;
+ }
}
/* A wrapper for iterate_over_symtabs that returns the first matching
@@ -524,7 +498,12 @@ lookup_symtab (const char *name)
{
struct symtab *result = NULL;
- iterate_over_symtabs (name, lookup_symtab_callback, &result);
+ iterate_over_symtabs (name, [&] (symtab *symtab)
+ {
+ result = symtab;
+ return true;
+ });
+
return result;
}
@@ -2777,18 +2756,17 @@ basic_lookup_transparent_type (const char *name)
}
/* Iterate over the symbols named NAME, matching DOMAIN, in BLOCK.
-
- For each symbol that matches, CALLBACK is called. The symbol and
- DATA are passed to the callback.
-
- If CALLBACK returns zero, the iteration ends. Otherwise, the
+
+ For each symbol that matches, CALLBACK is called. The symbol is
+ passed to the callback.
+
+ If CALLBACK returns false, the iteration ends. Otherwise, the
search continues. */
void
iterate_over_symbols (const struct block *block, const char *name,
const domain_enum domain,
- symbol_found_callback_ftype *callback,
- void *data)
+ gdb::function_view<symbol_found_callback_ftype> callback)
{
struct block_iterator iter;
struct symbol *sym;
@@ -2798,7 +2776,7 @@ iterate_over_symbols (const struct block *block, const char *name,
if (symbol_matches_domain (SYMBOL_LANGUAGE (sym),
SYMBOL_DOMAIN (sym), domain))
{
- if (!callback (sym, data))
+ if (!callback (sym))
return;
}
}
@@ -4282,39 +4260,6 @@ sort_search_symbols_remove_dups (struct symbol_search *found, int nfound,
xfree (symbols);
}
-/* An object of this type is passed as the user_data to the
- expand_symtabs_matching method. */
-struct search_symbols_data
-{
- int nfiles;
- const char **files;
-
- /* It is true if PREG contains valid data, false otherwise. */
- unsigned preg_p : 1;
- regex_t preg;
-};
-
-/* A callback for expand_symtabs_matching. */
-
-static int
-search_symbols_file_matches (const char *filename, void *user_data,
- int basenames)
-{
- struct search_symbols_data *data = (struct search_symbols_data *) user_data;
-
- return file_matches (filename, data->files, data->nfiles, basenames);
-}
-
-/* A callback for expand_symtabs_matching. */
-
-static int
-search_symbols_name_matches (const char *symname, void *user_data)
-{
- struct search_symbols_data *data = (struct search_symbols_data *) user_data;
-
- return !data->preg_p || regexec (&data->preg, symname, 0, NULL, 0) == 0;
-}
-
/* Search the symbol table for matches to the regular expression REGEXP,
returning the results in *MATCHES.
@@ -4359,8 +4304,10 @@ search_symbols (const char *regexp, enum search_domain kind,
enum minimal_symbol_type ourtype4;
struct symbol_search *found;
struct symbol_search *tail;
- struct search_symbols_data datum;
int nfound;
+ /* This is true if PREG contains valid data, false otherwise. */
+ bool preg_p;
+ regex_t preg;
/* OLD_CHAIN .. RETVAL_CHAIN is always freed, RETVAL_CHAIN .. current
CLEANUP_CHAIN is freed only in the case of an error. */
@@ -4375,7 +4322,7 @@ search_symbols (const char *regexp, enum search_domain kind,
ourtype4 = types4[kind];
*matches = NULL;
- datum.preg_p = 0;
+ preg_p = false;
if (regexp != NULL)
{
@@ -4414,31 +4361,35 @@ search_symbols (const char *regexp, enum search_domain kind,
}
}
- errcode = regcomp (&datum.preg, regexp,
+ errcode = regcomp (&preg, regexp,
REG_NOSUB | (case_sensitivity == case_sensitive_off
? REG_ICASE : 0));
if (errcode != 0)
{
- char *err = get_regcomp_error (errcode, &datum.preg);
+ char *err = get_regcomp_error (errcode, &preg);
make_cleanup (xfree, err);
error (_("Invalid regexp (%s): %s"), err, regexp);
}
- datum.preg_p = 1;
- make_regfree_cleanup (&datum.preg);
+ preg_p = true;
+ make_regfree_cleanup (&preg);
}
/* Search through the partial symtabs *first* for all symbols
matching the regexp. That way we don't have to reproduce all of
the machinery below. */
-
- datum.nfiles = nfiles;
- datum.files = files;
- expand_symtabs_matching ((nfiles == 0
- ? NULL
- : search_symbols_file_matches),
- search_symbols_name_matches,
- NULL, kind, &datum);
+ expand_symtabs_matching ([&] (const char *filename, bool basenames)
+ {
+ return file_matches (filename, files, nfiles,
+ basenames);
+ },
+ [&] (const char *symname)
+ {
+ return (!preg_p || regexec (&preg, symname,
+ 0, NULL, 0) == 0);
+ },
+ NULL,
+ kind);
/* Here, we search through the minimal symbol tables for functions
and variables that match, and force their symbols to be read.
@@ -4470,8 +4421,8 @@ search_symbols (const char *regexp, enum search_domain kind,
|| MSYMBOL_TYPE (msymbol) == ourtype3
|| MSYMBOL_TYPE (msymbol) == ourtype4)
{
- if (!datum.preg_p
- || regexec (&datum.preg, MSYMBOL_NATURAL_NAME (msymbol), 0,
+ if (!preg_p
+ || regexec (&preg, MSYMBOL_NATURAL_NAME (msymbol), 0,
NULL, 0) == 0)
{
/* Note: An important side-effect of these lookup functions
@@ -4514,8 +4465,8 @@ search_symbols (const char *regexp, enum search_domain kind,
files, nfiles, 1))
&& file_matches (symtab_to_fullname (real_symtab),
files, nfiles, 0)))
- && ((!datum.preg_p
- || regexec (&datum.preg, SYMBOL_NATURAL_NAME (sym), 0,
+ && ((!preg_p
+ || regexec (&preg, SYMBOL_NATURAL_NAME (sym), 0,
NULL, 0) == 0)
&& ((kind == VARIABLES_DOMAIN
&& SYMBOL_CLASS (sym) != LOC_TYPEDEF
@@ -4572,8 +4523,8 @@ search_symbols (const char *regexp, enum search_domain kind,
|| MSYMBOL_TYPE (msymbol) == ourtype3
|| MSYMBOL_TYPE (msymbol) == ourtype4)
{
- if (!datum.preg_p
- || regexec (&datum.preg, MSYMBOL_NATURAL_NAME (msymbol), 0,
+ if (!preg_p
+ || regexec (&preg, MSYMBOL_NATURAL_NAME (msymbol), 0,
NULL, 0) == 0)
{
/* For functions we can do a quick check of whether the
@@ -5110,46 +5061,6 @@ completion_list_add_fields (struct symbol *sym, const char *sym_text,
}
}
-/* Type of the user_data argument passed to add_macro_name,
- symbol_completion_matcher and symtab_expansion_callback. */
-
-struct add_name_data
-{
- /* Arguments required by completion_list_add_name. */
- const char *sym_text;
- int sym_text_len;
- const char *text;
- const char *word;
-
- /* Extra argument required for add_symtab_completions. */
- enum type_code code;
-};
-
-/* A callback used with macro_for_each and macro_for_each_in_scope.
- This adds a macro's name to the current completion list. */
-
-static void
-add_macro_name (const char *name, const struct macro_definition *ignore,
- struct macro_source_file *ignore2, int ignore3,
- void *user_data)
-{
- struct add_name_data *datum = (struct add_name_data *) user_data;
-
- completion_list_add_name (name,
- datum->sym_text, datum->sym_text_len,
- datum->text, datum->word);
-}
-
-/* A callback for expand_symtabs_matching. */
-
-static int
-symbol_completion_matcher (const char *name, void *user_data)
-{
- struct add_name_data *datum = (struct add_name_data *) user_data;
-
- return compare_symbol_name (name, datum->sym_text, datum->sym_text_len);
-}
-
/* Add matching symbols from SYMTAB to the current completion list. */
static void
@@ -5182,21 +5093,6 @@ add_symtab_completions (struct compunit_symtab *cust,
}
}
-/* Callback to add completions to the current list when symbol tables
- are expanded during completion list generation. */
-
-static void
-symtab_expansion_callback (struct compunit_symtab *symtab,
- void *user_data)
-{
- struct add_name_data *datum = (struct add_name_data *) user_data;
-
- add_symtab_completions (symtab,
- datum->sym_text, datum->sym_text_len,
- datum->text, datum->word,
- datum->code);
-}
-
static void
default_make_symbol_completion_list_break_on_1 (const char *text,
const char *word,
@@ -5218,7 +5114,6 @@ default_make_symbol_completion_list_break_on_1 (const char *text,
const char *sym_text;
/* Length of sym_text. */
int sym_text_len;
- struct add_name_data datum;
struct cleanup *cleanups;
/* Now look for the symbol we are supposed to complete on. */
@@ -5292,12 +5187,6 @@ default_make_symbol_completion_list_break_on_1 (const char *text,
completion_tracker = new_completion_tracker ();
cleanups = make_cleanup_free_completion_tracker (&completion_tracker);
- datum.sym_text = sym_text;
- datum.sym_text_len = sym_text_len;
- datum.text = text;
- datum.word = word;
- datum.code = code;
-
/* At this point scan through the misc symbol vectors and add each
symbol you find to the list. Eventually we want to ignore
anything that isn't a text symbol (everything else will be
@@ -5321,13 +5210,22 @@ default_make_symbol_completion_list_break_on_1 (const char *text,
add_symtab_completions (cust, sym_text, sym_text_len, text, word,
code);
- /* Look through the partial symtabs for all symbols which begin
- by matching SYM_TEXT. Expand all CUs that you find to the list.
- symtab_expansion_callback is called for each expanded symtab,
- causing those symtab's completions to be added to the list too. */
- expand_symtabs_matching (NULL, symbol_completion_matcher,
- symtab_expansion_callback, ALL_DOMAIN,
- &datum);
+ /* Look through the partial symtabs for all symbols which begin by
+ matching SYM_TEXT. Expand all CUs that you find to the list. */
+ expand_symtabs_matching (NULL,
+ [&] (const char *name) /* symbol matcher */
+ {
+ return compare_symbol_name (name,
+ sym_text,
+ sym_text_len);
+ },
+ [&] (compunit_symtab *symtab) /* expansion notify */
+ {
+ add_symtab_completions (symtab,
+ sym_text, sym_text_len,
+ text, word, code);
+ },
+ ALL_DOMAIN);
/* Search upwards from currently selected frame (so that we can
complete on local vars). Also catch fields of types defined in
@@ -5385,6 +5283,17 @@ default_make_symbol_completion_list_break_on_1 (const char *text,
{
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 *,
+ macro_source_file *,
+ int)
+ {
+ completion_list_add_name (macro_name,
+ sym_text, sym_text_len,
+ text, word);
+ };
+
/* Add any macros visible in the default scope. Note that this
may yield the occasional wrong result, because an expression
might be evaluated in a scope other than the default. For
@@ -5396,12 +5305,12 @@ default_make_symbol_completion_list_break_on_1 (const char *text,
if (scope)
{
macro_for_each_in_scope (scope->file, scope->line,
- add_macro_name, &datum);
+ add_macro_name);
xfree (scope);
}
/* User-defined macros are always visible. */
- macro_for_each (macro_user_macros, add_macro_name, &datum);
+ macro_for_each (macro_user_macros, add_macro_name);
}
do_cleanups (cleanups);