Files
clang-p2996/lldb/source/Plugins/SymbolFile/DWARF/DWARFDIE.h
Greg Clayton dd95877958 [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
2023-12-12 16:51:49 -08:00

147 lines
4.9 KiB
C++

//===-- DWARFDIE.h ----------------------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#ifndef LLDB_SOURCE_PLUGINS_SYMBOLFILE_DWARF_DWARFDIE_H
#define LLDB_SOURCE_PLUGINS_SYMBOLFILE_DWARF_DWARFDIE_H
#include "DWARFBaseDIE.h"
#include "llvm/ADT/SmallSet.h"
#include "llvm/ADT/iterator_range.h"
namespace lldb_private::plugin {
namespace dwarf {
class DWARFDIE : public DWARFBaseDIE {
public:
class child_iterator;
using DWARFBaseDIE::DWARFBaseDIE;
// Tests
bool IsStructUnionOrClass() const;
bool IsMethod() const;
// Accessors
// Accessing information about a DIE
const char *GetMangledName() const;
const char *GetPubname() const;
using DWARFBaseDIE::GetName;
void GetName(Stream &s) const;
void AppendTypeName(Stream &s) const;
Type *ResolveType() const;
// Resolve a type by UID using this DIE's DWARF file
Type *ResolveTypeUID(const DWARFDIE &die) const;
// Functions for obtaining DIE relations and references
DWARFDIE
GetParent() const;
DWARFDIE
GetFirstChild() const;
DWARFDIE
GetSibling() const;
DWARFDIE
GetReferencedDIE(const dw_attr_t attr) const;
// Get a another DIE from the same DWARF file as this DIE. This will
// check the current DIE's compile unit first to see if "die_offset" is
// in the same compile unit, and fall back to checking the DWARF file.
DWARFDIE
GetDIE(dw_offset_t die_offset) const;
using DWARFBaseDIE::GetDIE;
DWARFDIE
LookupDeepestBlock(lldb::addr_t file_addr) const;
DWARFDIE
GetParentDeclContextDIE() const;
// DeclContext related functions
std::vector<DWARFDIE> GetDeclContextDIEs() const;
/// Return this DIE's decl context as it is needed to look up types
/// in Clang modules. This context will include any modules or functions that
/// the type is declared in so an exact module match can be efficiently made.
std::vector<CompilerContext> GetDeclContext() const;
/// Get a context to a type so it can be looked up.
///
/// This function uses the current DIE to fill in a CompilerContext array
/// that is suitable for type lookup for comparison to a TypeQuery's compiler
/// context (TypeQuery::GetContextRef()). If this DIE represents a named type,
/// it should fill out the compiler context with the type itself as the last
/// entry. The declaration context should be above the type and stop at an
/// appropriate time, like either the translation unit or at a function
/// context. This is designed to allow users to efficiently look for types
/// using a full or partial CompilerContext array.
std::vector<CompilerContext> GetTypeLookupContext() const;
// Getting attribute values from the DIE.
//
// GetAttributeValueAsXXX() functions should only be used if you are
// looking for one or two attributes on a DIE. If you are trying to
// parse all attributes, use GetAttributes (...) instead
DWARFDIE
GetAttributeValueAsReferenceDIE(const dw_attr_t attr) const;
bool GetDIENamesAndRanges(
const char *&name, const char *&mangled, DWARFRangeList &ranges,
std::optional<int> &decl_file, std::optional<int> &decl_line,
std::optional<int> &decl_column, std::optional<int> &call_file,
std::optional<int> &call_line, std::optional<int> &call_column,
DWARFExpressionList *frame_base) const;
/// The range of all the children of this DIE.
llvm::iterator_range<child_iterator> children() const;
};
class DWARFDIE::child_iterator
: public llvm::iterator_facade_base<DWARFDIE::child_iterator,
std::forward_iterator_tag, DWARFDIE> {
/// The current child or an invalid DWARFDie.
DWARFDIE m_die;
public:
child_iterator() = default;
child_iterator(const DWARFDIE &parent) : m_die(parent.GetFirstChild()) {}
bool operator==(const child_iterator &it) const {
// DWARFDIE's operator== differentiates between an invalid DWARFDIE that
// has a CU but no DIE and one that has neither CU nor DIE. The 'end'
// iterator could be default constructed, so explicitly allow
// (CU, (DIE)nullptr) == (nullptr, nullptr) -> true
if (!m_die.IsValid() && !it.m_die.IsValid())
return true;
return m_die == it.m_die;
}
const DWARFDIE &operator*() const {
assert(m_die.IsValid() && "Derefencing invalid iterator?");
return m_die;
}
DWARFDIE &operator*() {
assert(m_die.IsValid() && "Derefencing invalid iterator?");
return m_die;
}
child_iterator &operator++() {
assert(m_die.IsValid() && "Incrementing invalid iterator?");
m_die = m_die.GetSibling();
return *this;
}
};
} // namespace dwarf
} // namespace lldb_private::plugin
#endif // LLDB_SOURCE_PLUGINS_SYMBOLFILE_DWARF_DWARFDIE_H