Skip to content
This repository has been archived by the owner on Jan 20, 2024. It is now read-only.

Commit

Permalink
[lldb] Make only one function that needs to be implemented when searc…
Browse files Browse the repository at this point in the history
…hing 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
  • Loading branch information
clayborg authored Dec 13, 2023
1 parent 27259f1 commit dd95877
Show file tree
Hide file tree
Showing 52 changed files with 1,135 additions and 907 deletions.
77 changes: 10 additions & 67 deletions lldb/include/lldb/Core/Module.h
Original file line number Diff line number Diff line change
Expand Up @@ -415,70 +415,19 @@ class Module : public std::enable_shared_from_this<Module>,
void FindGlobalVariables(const RegularExpression &regex, size_t max_matches,
VariableList &variable_list);

/// Find types by name.
///
/// Type lookups in modules go through the SymbolFile. The SymbolFile needs to
/// be able to lookup types by basename and not the fully qualified typename.
/// This allows the type accelerator tables to stay small, even with heavily
/// templatized C++. The type search will then narrow down the search
/// results. If "exact_match" is true, then the type search will only match
/// exact type name matches. If "exact_match" is false, the type will match
/// as long as the base typename matches and as long as any immediate
/// containing namespaces/class scopes that are specified match. So to
/// search for a type "d" in "b::c", the name "b::c::d" can be specified and
/// it will match any class/namespace "b" which contains a class/namespace
/// "c" which contains type "d". We do this to allow users to not always
/// have to specify complete scoping on all expressions, but it also allows
/// for exact matching when required.
///
/// \param[in] type_name
/// The name of the type we are looking for that is a fully
/// or partially qualified type name.
///
/// \param[in] exact_match
/// If \b true, \a type_name is fully qualified and must match
/// exactly. If \b false, \a type_name is a partially qualified
/// name where the leading namespaces or classes can be
/// omitted to make finding types that a user may type
/// easier.
///
/// \param[out] types
/// A type list gets populated with any matches.
/// Find types using a type-matching object that contains all search
/// parameters.
///
void
FindTypes(ConstString type_name, bool exact_match, size_t max_matches,
llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files,
TypeList &types);

/// Find types by name.
///
/// This behaves like the other FindTypes method but allows to
/// specify a DeclContext and a language for the type being searched
/// for.
///
/// \param searched_symbol_files
/// Prevents one file from being visited multiple times.
void
FindTypes(llvm::ArrayRef<CompilerContext> pattern, LanguageSet languages,
llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files,
TypeMap &types);

lldb::TypeSP FindFirstType(const SymbolContext &sc, ConstString type_name,
bool exact_match);

/// Find types by name that are in a namespace. This function is used by the
/// expression parser when searches need to happen in an exact namespace
/// scope.
/// \see lldb_private::TypeQuery
///
/// \param[in] type_name
/// The name of a type within a namespace that should not include
/// any qualifying namespaces (just a type basename).
/// \param[in] query
/// A type matching object that contains all of the details of the type
/// search.
///
/// \param[out] type_list
/// A type list gets populated with any matches.
void FindTypesInNamespace(ConstString type_name,
const CompilerDeclContext &parent_decl_ctx,
size_t max_matches, TypeList &type_list);
/// \param[in] results
/// Any matching types will be populated into the \a results object using
/// TypeMap::InsertUnique(...).
void FindTypes(const TypeQuery &query, TypeResults &results);

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

void FindTypes_Impl(
ConstString name, const CompilerDeclContext &parent_decl_ctx,
size_t max_matches,
llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files,
TypeMap &types);

Module(const Module &) = delete;
const Module &operator=(const Module &) = delete;

Expand Down
24 changes: 10 additions & 14 deletions lldb/include/lldb/Core/ModuleList.h
Original file line number Diff line number Diff line change
Expand Up @@ -340,26 +340,22 @@ class ModuleList {
lldb::SymbolType symbol_type,
SymbolContextList &sc_list) const;

/// Find types by name.
/// Find types using a type-matching object that contains all search
/// parameters.
///
/// \param[in] search_first
/// If non-null, this module will be searched before any other
/// modules.
///
/// \param[in] name
/// The name of the type we are looking for.
///
/// \param[in] max_matches
/// Allow the number of matches to be limited to \a
/// max_matches. Specify UINT32_MAX to get all possible matches.
///
/// \param[out] types
/// A type list gets populated with any matches.
/// \param[in] query
/// A type matching object that contains all of the details of the type
/// search.
///
void FindTypes(Module *search_first, ConstString name,
bool name_is_fully_qualified, size_t max_matches,
llvm::DenseSet<SymbolFile *> &searched_symbol_files,
TypeList &types) const;
/// \param[in] results
/// Any matching types will be populated into the \a results object using
/// TypeMap::InsertUnique(...).
void FindTypes(Module *search_first, const TypeQuery &query,
lldb_private::TypeResults &results) const;

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

Expand Down
7 changes: 7 additions & 0 deletions lldb/include/lldb/Symbol/CompilerDecl.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ class CompilerDecl {
// based argument index
CompilerType GetFunctionArgumentType(size_t arg_idx) const;

/// Populate a valid compiler context from the current declaration.
///
/// \returns A valid vector of CompilerContext entries that describes
/// this declaration. The first entry in the vector is the parent of
/// the subsequent entry, so the topmost entry is the global namespace.
std::vector<lldb_private::CompilerContext> GetCompilerContext() const;

private:
TypeSystem *m_type_system = nullptr;
void *m_opaque_decl = nullptr;
Expand Down
8 changes: 8 additions & 0 deletions lldb/include/lldb/Symbol/CompilerDeclContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#include <vector>

#include "lldb/Symbol/Type.h"
#include "lldb/Utility/ConstString.h"
#include "lldb/lldb-private.h"

Expand Down Expand Up @@ -56,6 +57,13 @@ class CompilerDeclContext {
return m_type_system != nullptr && m_opaque_decl_ctx != nullptr;
}

/// Populate a valid compiler context from the current decl context.
///
/// \returns A valid vector of CompilerContext entries that describes
/// this declaration context. The first entry in the vector is the parent of
/// the subsequent entry, so the topmost entry is the global namespace.
std::vector<lldb_private::CompilerContext> GetCompilerContext() const;

std::vector<CompilerDecl> FindDeclByName(ConstString name,
const bool ignore_using_decls);

Expand Down
29 changes: 14 additions & 15 deletions lldb/include/lldb/Symbol/SymbolFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -301,21 +301,20 @@ class SymbolFile : public PluginInterface {
bool include_inlines, SymbolContextList &sc_list);
virtual void FindFunctions(const RegularExpression &regex,
bool include_inlines, SymbolContextList &sc_list);
virtual void
FindTypes(ConstString name, const CompilerDeclContext &parent_decl_ctx,
uint32_t max_matches,
llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files,
TypeMap &types);

/// Find types specified by a CompilerContextPattern.
/// \param languages
/// Only return results in these languages.
/// \param searched_symbol_files
/// Prevents one file from being visited multiple times.
virtual void
FindTypes(llvm::ArrayRef<CompilerContext> pattern, LanguageSet languages,
llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files,
TypeMap &types);

/// Find types using a type-matching object that contains all search
/// parameters.
///
/// \see lldb_private::TypeQuery
///
/// \param[in] query
/// A type matching object that contains all of the details of the type
/// search.
///
/// \param[in] results
/// Any matching types will be populated into the \a results object using
/// TypeMap::InsertUnique(...).
virtual void FindTypes(const TypeQuery &query, TypeResults &results) {}

virtual void
GetMangledNamesForFunction(const std::string &scope_qualified_name,
Expand Down
13 changes: 2 additions & 11 deletions lldb/include/lldb/Symbol/SymbolFileOnDemand.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,17 +152,8 @@ class SymbolFileOnDemand : public lldb_private::SymbolFile {
const std::string &scope_qualified_name,
std::vector<lldb_private::ConstString> &mangled_names) override;

void
FindTypes(lldb_private::ConstString name,
const lldb_private::CompilerDeclContext &parent_decl_ctx,
uint32_t max_matches,
llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files,
lldb_private::TypeMap &types) override;

void FindTypes(llvm::ArrayRef<lldb_private::CompilerContext> pattern,
lldb_private::LanguageSet languages,
llvm::DenseSet<SymbolFile *> &searched_symbol_files,
lldb_private::TypeMap &types) override;
void FindTypes(const lldb_private::TypeQuery &query,
lldb_private::TypeResults &results) override;

void GetTypes(lldb_private::SymbolContextScope *sc_scope,
lldb::TypeClass type_mask,
Expand Down
Loading

0 comments on commit dd95877

Please sign in to comment.