/* Symbol table lookup for the GNU debugger, GDB.
- Copyright (C) 1986-2016 Free Software Foundation, Inc.
+ Copyright (C) 1986-2017 Free Software Foundation, Inc.
This file is part of GDB.
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;
{
if (compare_filenames_for_search (s->filename, name))
{
- if (callback (s, data))
- return 1;
+ if (callback (s))
+ return true;
continue;
}
if (compare_filenames_for_search (symtab_to_fullname (s), name))
{
- if (callback (s, data))
- return 1;
+ if (callback (s))
+ return true;
continue;
}
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
{
struct symtab *result = NULL;
- iterate_over_symtabs (name, lookup_symtab_callback, &result);
+ iterate_over_symtabs (name, [&] (symtab *symtab)
+ {
+ result = symtab;
+ return true;
+ });
+
return result;
}
}
/* Compute the demangled form of NAME as used by the various symbol
- lookup functions. The result is stored in *RESULT_NAME. Returns a
- cleanup which can be used to clean up the result.
+ lookup functions. The result can either be the input NAME
+ directly, or a pointer to a buffer owned by the STORAGE object.
- For Ada, this function just sets *RESULT_NAME to NAME, unmodified.
+ For Ada, this function just returns NAME, unmodified.
Normally, Ada symbol lookups are performed using the encoded name
rather than the demangled name, and so it might seem to make sense
for this function to return an encoded version of NAME.
characters to become lowercase, and thus cause the symbol lookup
to fail. */
-struct cleanup *
+const char *
demangle_for_lookup (const char *name, enum language lang,
- const char **result_name)
+ demangle_result_storage &storage)
{
- char *demangled_name = NULL;
- const char *modified_name = NULL;
- struct cleanup *cleanup = make_cleanup (null_cleanup, 0);
-
- modified_name = name;
-
/* If we are using C++, D, or Go, demangle the name before doing a
lookup, so we can always binary search. */
if (lang == language_cplus)
{
- demangled_name = gdb_demangle (name, DMGL_ANSI | DMGL_PARAMS);
- if (demangled_name)
- {
- modified_name = demangled_name;
- make_cleanup (xfree, demangled_name);
- }
- else
- {
- /* If we were given a non-mangled name, canonicalize it
- according to the language (so far only for C++). */
- demangled_name = cp_canonicalize_string (name);
- if (demangled_name)
- {
- modified_name = demangled_name;
- make_cleanup (xfree, demangled_name);
- }
- }
+ char *demangled_name = gdb_demangle (name, DMGL_ANSI | DMGL_PARAMS);
+ if (demangled_name != NULL)
+ return storage.set_malloc_ptr (demangled_name);
+
+ /* If we were given a non-mangled name, canonicalize it
+ according to the language (so far only for C++). */
+ std::string canon = cp_canonicalize_string (name);
+ if (!canon.empty ())
+ return storage.swap_string (canon);
}
else if (lang == language_d)
{
- demangled_name = d_demangle (name, 0);
- if (demangled_name)
- {
- modified_name = demangled_name;
- make_cleanup (xfree, demangled_name);
- }
+ char *demangled_name = d_demangle (name, 0);
+ if (demangled_name != NULL)
+ return storage.set_malloc_ptr (demangled_name);
}
else if (lang == language_go)
{
- demangled_name = go_demangle (name, 0);
- if (demangled_name)
- {
- modified_name = demangled_name;
- make_cleanup (xfree, demangled_name);
- }
+ char *demangled_name = go_demangle (name, 0);
+ if (demangled_name != NULL)
+ return storage.set_malloc_ptr (demangled_name);
}
- *result_name = modified_name;
- return cleanup;
+ return name;
}
/* See symtab.h.
const domain_enum domain, enum language lang,
struct field_of_this_result *is_a_field_of_this)
{
- const char *modified_name;
- struct block_symbol returnval;
- struct cleanup *cleanup = demangle_for_lookup (name, lang, &modified_name);
+ demangle_result_storage storage;
+ const char *modified_name = demangle_for_lookup (name, lang, storage);
- returnval = lookup_symbol_aux (modified_name, block, domain, lang,
- is_a_field_of_this);
- do_cleanups (cleanup);
-
- return returnval;
+ return lookup_symbol_aux (modified_name, block, domain, lang,
+ is_a_field_of_this);
}
/* See symtab.h. */
/* I'm not really sure that type of this can ever
be typedefed; just be safe. */
t = check_typedef (t);
- if (TYPE_CODE (t) == TYPE_CODE_PTR
- || TYPE_CODE (t) == TYPE_CODE_REF)
+ if (TYPE_CODE (t) == TYPE_CODE_PTR || TYPE_IS_REFERENCE (t))
t = TYPE_TARGET_TYPE (t);
if (TYPE_CODE (t) != TYPE_CODE_STRUCT
domain_enum domain)
{
enum language lang = current_language->la_language;
- const char *modified_name;
- struct cleanup *cleanup = demangle_for_lookup (linkage_name, lang,
- &modified_name);
struct objfile *main_objfile, *cur_objfile;
+ demangle_result_storage storage;
+ const char *modified_name = demangle_for_lookup (linkage_name, lang, storage);
+
if (objfile->separate_debug_objfile_backlink)
main_objfile = objfile->separate_debug_objfile_backlink;
else
result = lookup_symbol_in_objfile_symtabs (cur_objfile, STATIC_BLOCK,
modified_name, domain);
if (result.symbol != NULL)
- {
- do_cleanups (cleanup);
- return result;
- }
+ return result;
}
- do_cleanups (cleanup);
return (struct block_symbol) {NULL, NULL};
}
}
/* 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;
if (symbol_matches_domain (SYMBOL_LANGUAGE (sym),
SYMBOL_DOMAIN (sym), domain))
{
- if (!callback (sym, data))
+ if (!callback (sym))
return;
}
}
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.
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. */
ourtype4 = types4[kind];
*matches = NULL;
- datum.preg_p = 0;
+ preg_p = false;
if (regexp != NULL)
{
}
}
- 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.
|| 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
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
|| 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
}
}
-/* 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
struct block_iterator iter;
int i;
+ if (cust == NULL)
+ return;
+
for (i = GLOBAL_BLOCK; i <= STATIC_BLOCK; i++)
{
QUIT;
}
}
-/* 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,
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. */
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
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
{
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
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);
make_file_symbol_completion_list_1 (const char *text, const char *word,
const char *srcfile)
{
- struct symbol *sym;
struct symtab *s;
- struct block *b;
- struct block_iterator iter;
/* The symbol we are completing on. Points in same buffer as text. */
const char *sym_text;
/* Length of sym_text. */
/* Go through this symtab and check the externs and statics for
symbols which match. */
-
- b = BLOCKVECTOR_BLOCK (SYMTAB_BLOCKVECTOR (s), GLOBAL_BLOCK);
- ALL_BLOCK_SYMBOLS (b, iter, sym)
- {
- COMPLETION_LIST_ADD_SYMBOL (sym, sym_text, sym_text_len, text, word);
- }
-
- b = BLOCKVECTOR_BLOCK (SYMTAB_BLOCKVECTOR (s), STATIC_BLOCK);
- ALL_BLOCK_SYMBOLS (b, iter, sym)
- {
- COMPLETION_LIST_ADD_SYMBOL (sym, sym_text, sym_text_len, text, word);
- }
+ add_symtab_completions (SYMTAB_COMPUNIT (s),
+ sym_text, sym_text_len,
+ text, word, TYPE_CODE_UNDEF);
return (return_val);
}