Skip to content

Commit dd95877

Browse files
authored
[lldb] Make only one function that needs to be implemented when searching for types (#74786)
This patch revives the effort to get this Phabricator patch into upstream: https://reviews.llvm.org/D137900 This patch was accepted before in Phabricator but I found some -gsimple-template-names issues that are fixed in this patch. A fixed up version of the description from the original patch starts now. This patch started off trying to fix Module::FindFirstType() as it sometimes didn't work. The issue was the SymbolFile plug-ins didn't do any filtering of the matching types they produced, and they only looked up types using the type basename. This means if you have two types with the same basename, your type lookup can fail when only looking up a single type. We would ask the Module::FindFirstType to lookup "Foo::Bar" and it would ask the symbol file to find only 1 type matching the basename "Bar", and then we would filter out any matches that didn't match "Foo::Bar". So if the SymbolFile found "Foo::Bar" first, then it would work, but if it found "Baz::Bar" first, it would return only that type and it would be filtered out. Discovering this issue lead me to think of the patch Alex Langford did a few months ago that was done for finding functions, where he allowed SymbolFile objects to make sure something fully matched before parsing the debug information into an AST type and other LLDB types. So this patch aimed to allow type lookups to also be much more efficient. As LLDB has been developed over the years, we added more ways to to type lookups. These functions have lots of arguments. This patch aims to make one API that needs to be implemented that serves all previous lookups: - Find a single type - Find all types - Find types in a namespace This patch introduces a `TypeQuery` class that contains all of the state needed to perform the lookup which is powerful enough to perform all of the type searches that used to be in our API. It contain a vector of CompilerContext objects that can fully or partially specify the lookup that needs to take place. If you just want to lookup all types with a matching basename, regardless of the containing context, you can specify just a single CompilerContext entry that has a name and a CompilerContextKind mask of CompilerContextKind::AnyType. Or you can fully specify the exact context to use when doing lookups like: CompilerContextKind::Namespace "std" CompilerContextKind::Class "foo" CompilerContextKind::Typedef "size_type" This change expands on the clang modules code that already used a vector<CompilerContext> items, but it modifies it to work with expression type lookups which have contexts, or user lookups where users query for types. The clang modules type lookup is still an option that can be enabled on the `TypeQuery` objects. This mirrors the most recent addition of type lookups that took a vector<CompilerContext> that allowed lookups to happen for the expression parser in certain places. Prior to this we had the following APIs in Module: ``` void Module::FindTypes(ConstString type_name, bool exact_match, size_t max_matches, llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files, TypeList &types); void Module::FindTypes(llvm::ArrayRef<CompilerContext> pattern, LanguageSet languages, llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files, TypeMap &types); void Module::FindTypesInNamespace(ConstString type_name, const CompilerDeclContext &parent_decl_ctx, size_t max_matches, TypeList &type_list); ``` The new Module API is much simpler. It gets rid of all three above functions and replaces them with: ``` void FindTypes(const TypeQuery &query, TypeResults &results); ``` The `TypeQuery` class contains all of the needed settings: - The vector<CompilerContext> that allow efficient lookups in the symbol file classes since they can look at basename matches only realize fully matching types. Before this any basename that matched was fully realized only to be removed later by code outside of the SymbolFile layer which could cause many types to be realized when they didn't need to. - If the lookup is exact or not. If not exact, then the compiler context must match the bottom most items that match the compiler context, otherwise it must match exactly - If the compiler context match is for clang modules or not. Clang modules matches include a Module compiler context kind that allows types to be matched only from certain modules and these matches are not needed when d oing user type lookups. - An optional list of languages to use to limit the search to only certain languages The `TypeResults` object contains all state required to do the lookup and store the results: - The max number of matches - The set of SymbolFile objects that have already been searched - The matching type list for any matches that are found The benefits of this approach are: - Simpler API, and only one API to implement in SymbolFile classes - Replaces the FindTypesInNamespace that used a CompilerDeclContext as a way to limit the search, but this only worked if the TypeSystem matched the current symbol file's type system, so you couldn't use it to lookup a type in another module - Fixes a serious bug in our FindFirstType functions where if we were searching for "foo::bar", and we found a "baz::bar" first, the basename would match and we would only fetch 1 type using the basename, only to drop it from the matching list and returning no results
1 parent 27259f1 commit dd95877

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+1135
-907
lines changed

lldb/include/lldb/Core/Module.h

Lines changed: 10 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -415,70 +415,19 @@ class Module : public std::enable_shared_from_this<Module>,
415415
void FindGlobalVariables(const RegularExpression &regex, size_t max_matches,
416416
VariableList &variable_list);
417417

418-
/// Find types by name.
419-
///
420-
/// Type lookups in modules go through the SymbolFile. The SymbolFile needs to
421-
/// be able to lookup types by basename and not the fully qualified typename.
422-
/// This allows the type accelerator tables to stay small, even with heavily
423-
/// templatized C++. The type search will then narrow down the search
424-
/// results. If "exact_match" is true, then the type search will only match
425-
/// exact type name matches. If "exact_match" is false, the type will match
426-
/// as long as the base typename matches and as long as any immediate
427-
/// containing namespaces/class scopes that are specified match. So to
428-
/// search for a type "d" in "b::c", the name "b::c::d" can be specified and
429-
/// it will match any class/namespace "b" which contains a class/namespace
430-
/// "c" which contains type "d". We do this to allow users to not always
431-
/// have to specify complete scoping on all expressions, but it also allows
432-
/// for exact matching when required.
433-
///
434-
/// \param[in] type_name
435-
/// The name of the type we are looking for that is a fully
436-
/// or partially qualified type name.
437-
///
438-
/// \param[in] exact_match
439-
/// If \b true, \a type_name is fully qualified and must match
440-
/// exactly. If \b false, \a type_name is a partially qualified
441-
/// name where the leading namespaces or classes can be
442-
/// omitted to make finding types that a user may type
443-
/// easier.
444-
///
445-
/// \param[out] types
446-
/// A type list gets populated with any matches.
418+
/// Find types using a type-matching object that contains all search
419+
/// parameters.
447420
///
448-
void
449-
FindTypes(ConstString type_name, bool exact_match, size_t max_matches,
450-
llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files,
451-
TypeList &types);
452-
453-
/// Find types by name.
454-
///
455-
/// This behaves like the other FindTypes method but allows to
456-
/// specify a DeclContext and a language for the type being searched
457-
/// for.
458-
///
459-
/// \param searched_symbol_files
460-
/// Prevents one file from being visited multiple times.
461-
void
462-
FindTypes(llvm::ArrayRef<CompilerContext> pattern, LanguageSet languages,
463-
llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files,
464-
TypeMap &types);
465-
466-
lldb::TypeSP FindFirstType(const SymbolContext &sc, ConstString type_name,
467-
bool exact_match);
468-
469-
/// Find types by name that are in a namespace. This function is used by the
470-
/// expression parser when searches need to happen in an exact namespace
471-
/// scope.
421+
/// \see lldb_private::TypeQuery
472422
///
473-
/// \param[in] type_name
474-
/// The name of a type within a namespace that should not include
475-
/// any qualifying namespaces (just a type basename).
423+
/// \param[in] query
424+
/// A type matching object that contains all of the details of the type
425+
/// search.
476426
///
477-
/// \param[out] type_list
478-
/// A type list gets populated with any matches.
479-
void FindTypesInNamespace(ConstString type_name,
480-
const CompilerDeclContext &parent_decl_ctx,
481-
size_t max_matches, TypeList &type_list);
427+
/// \param[in] results
428+
/// Any matching types will be populated into the \a results object using
429+
/// TypeMap::InsertUnique(...).
430+
void FindTypes(const TypeQuery &query, TypeResults &results);
482431

483432
/// Get const accessor for the module architecture.
484433
///
@@ -1122,12 +1071,6 @@ class Module : public std::enable_shared_from_this<Module>,
11221071
private:
11231072
Module(); // Only used internally by CreateJITModule ()
11241073

1125-
void FindTypes_Impl(
1126-
ConstString name, const CompilerDeclContext &parent_decl_ctx,
1127-
size_t max_matches,
1128-
llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files,
1129-
TypeMap &types);
1130-
11311074
Module(const Module &) = delete;
11321075
const Module &operator=(const Module &) = delete;
11331076

lldb/include/lldb/Core/ModuleList.h

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -340,26 +340,22 @@ class ModuleList {
340340
lldb::SymbolType symbol_type,
341341
SymbolContextList &sc_list) const;
342342

343-
/// Find types by name.
343+
/// Find types using a type-matching object that contains all search
344+
/// parameters.
344345
///
345346
/// \param[in] search_first
346347
/// If non-null, this module will be searched before any other
347348
/// modules.
348349
///
349-
/// \param[in] name
350-
/// The name of the type we are looking for.
351-
///
352-
/// \param[in] max_matches
353-
/// Allow the number of matches to be limited to \a
354-
/// max_matches. Specify UINT32_MAX to get all possible matches.
355-
///
356-
/// \param[out] types
357-
/// A type list gets populated with any matches.
350+
/// \param[in] query
351+
/// A type matching object that contains all of the details of the type
352+
/// search.
358353
///
359-
void FindTypes(Module *search_first, ConstString name,
360-
bool name_is_fully_qualified, size_t max_matches,
361-
llvm::DenseSet<SymbolFile *> &searched_symbol_files,
362-
TypeList &types) const;
354+
/// \param[in] results
355+
/// Any matching types will be populated into the \a results object using
356+
/// TypeMap::InsertUnique(...).
357+
void FindTypes(Module *search_first, const TypeQuery &query,
358+
lldb_private::TypeResults &results) const;
363359

364360
bool FindSourceFile(const FileSpec &orig_spec, FileSpec &new_spec) const;
365361

lldb/include/lldb/Symbol/CompilerDecl.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,13 @@ class CompilerDecl {
8484
// based argument index
8585
CompilerType GetFunctionArgumentType(size_t arg_idx) const;
8686

87+
/// Populate a valid compiler context from the current declaration.
88+
///
89+
/// \returns A valid vector of CompilerContext entries that describes
90+
/// this declaration. The first entry in the vector is the parent of
91+
/// the subsequent entry, so the topmost entry is the global namespace.
92+
std::vector<lldb_private::CompilerContext> GetCompilerContext() const;
93+
8794
private:
8895
TypeSystem *m_type_system = nullptr;
8996
void *m_opaque_decl = nullptr;

lldb/include/lldb/Symbol/CompilerDeclContext.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include <vector>
1313

14+
#include "lldb/Symbol/Type.h"
1415
#include "lldb/Utility/ConstString.h"
1516
#include "lldb/lldb-private.h"
1617

@@ -56,6 +57,13 @@ class CompilerDeclContext {
5657
return m_type_system != nullptr && m_opaque_decl_ctx != nullptr;
5758
}
5859

60+
/// Populate a valid compiler context from the current decl context.
61+
///
62+
/// \returns A valid vector of CompilerContext entries that describes
63+
/// this declaration context. The first entry in the vector is the parent of
64+
/// the subsequent entry, so the topmost entry is the global namespace.
65+
std::vector<lldb_private::CompilerContext> GetCompilerContext() const;
66+
5967
std::vector<CompilerDecl> FindDeclByName(ConstString name,
6068
const bool ignore_using_decls);
6169

lldb/include/lldb/Symbol/SymbolFile.h

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -301,21 +301,20 @@ class SymbolFile : public PluginInterface {
301301
bool include_inlines, SymbolContextList &sc_list);
302302
virtual void FindFunctions(const RegularExpression &regex,
303303
bool include_inlines, SymbolContextList &sc_list);
304-
virtual void
305-
FindTypes(ConstString name, const CompilerDeclContext &parent_decl_ctx,
306-
uint32_t max_matches,
307-
llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files,
308-
TypeMap &types);
309-
310-
/// Find types specified by a CompilerContextPattern.
311-
/// \param languages
312-
/// Only return results in these languages.
313-
/// \param searched_symbol_files
314-
/// Prevents one file from being visited multiple times.
315-
virtual void
316-
FindTypes(llvm::ArrayRef<CompilerContext> pattern, LanguageSet languages,
317-
llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files,
318-
TypeMap &types);
304+
305+
/// Find types using a type-matching object that contains all search
306+
/// parameters.
307+
///
308+
/// \see lldb_private::TypeQuery
309+
///
310+
/// \param[in] query
311+
/// A type matching object that contains all of the details of the type
312+
/// search.
313+
///
314+
/// \param[in] results
315+
/// Any matching types will be populated into the \a results object using
316+
/// TypeMap::InsertUnique(...).
317+
virtual void FindTypes(const TypeQuery &query, TypeResults &results) {}
319318

320319
virtual void
321320
GetMangledNamesForFunction(const std::string &scope_qualified_name,

lldb/include/lldb/Symbol/SymbolFileOnDemand.h

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -152,17 +152,8 @@ class SymbolFileOnDemand : public lldb_private::SymbolFile {
152152
const std::string &scope_qualified_name,
153153
std::vector<lldb_private::ConstString> &mangled_names) override;
154154

155-
void
156-
FindTypes(lldb_private::ConstString name,
157-
const lldb_private::CompilerDeclContext &parent_decl_ctx,
158-
uint32_t max_matches,
159-
llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files,
160-
lldb_private::TypeMap &types) override;
161-
162-
void FindTypes(llvm::ArrayRef<lldb_private::CompilerContext> pattern,
163-
lldb_private::LanguageSet languages,
164-
llvm::DenseSet<SymbolFile *> &searched_symbol_files,
165-
lldb_private::TypeMap &types) override;
155+
void FindTypes(const lldb_private::TypeQuery &query,
156+
lldb_private::TypeResults &results) override;
166157

167158
void GetTypes(lldb_private::SymbolContextScope *sc_scope,
168159
lldb::TypeClass type_mask,

0 commit comments

Comments
 (0)