Files
clang-p2996/lldb/source/Plugins/SymbolFile/DWARF/DWARFDIE.cpp
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

507 lines
14 KiB
C++

//===-- DWARFDIE.cpp ------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
#include "DWARFDIE.h"
#include "DWARFASTParser.h"
#include "DWARFDebugInfo.h"
#include "DWARFDebugInfoEntry.h"
#include "DWARFDeclContext.h"
#include "DWARFUnit.h"
#include "llvm/ADT/iterator.h"
using namespace lldb_private;
using namespace lldb_private::dwarf;
using namespace lldb_private::plugin::dwarf;
namespace {
/// Iterate through all DIEs elaborating (i.e. reachable by a chain of
/// DW_AT_specification and DW_AT_abstract_origin attributes) a given DIE. For
/// convenience, the starting die is included in the sequence as the first
/// item.
class ElaboratingDIEIterator
: public llvm::iterator_facade_base<
ElaboratingDIEIterator, std::input_iterator_tag, DWARFDIE,
std::ptrdiff_t, DWARFDIE *, DWARFDIE *> {
// The operating invariant is: top of m_worklist contains the "current" item
// and the rest of the list are items yet to be visited. An empty worklist
// means we've reached the end.
// Infinite recursion is prevented by maintaining a list of seen DIEs.
// Container sizes are optimized for the case of following DW_AT_specification
// and DW_AT_abstract_origin just once.
llvm::SmallVector<DWARFDIE, 2> m_worklist;
llvm::SmallSet<DWARFDebugInfoEntry *, 3> m_seen;
void Next() {
assert(!m_worklist.empty() && "Incrementing end iterator?");
// Pop the current item from the list.
DWARFDIE die = m_worklist.back();
m_worklist.pop_back();
// And add back any items that elaborate it.
for (dw_attr_t attr : {DW_AT_specification, DW_AT_abstract_origin}) {
if (DWARFDIE d = die.GetReferencedDIE(attr))
if (m_seen.insert(die.GetDIE()).second)
m_worklist.push_back(d);
}
}
public:
/// An iterator starting at die d.
explicit ElaboratingDIEIterator(DWARFDIE d) : m_worklist(1, d) {}
/// End marker
ElaboratingDIEIterator() = default;
const DWARFDIE &operator*() const { return m_worklist.back(); }
ElaboratingDIEIterator &operator++() {
Next();
return *this;
}
friend bool operator==(const ElaboratingDIEIterator &a,
const ElaboratingDIEIterator &b) {
if (a.m_worklist.empty() || b.m_worklist.empty())
return a.m_worklist.empty() == b.m_worklist.empty();
return a.m_worklist.back() == b.m_worklist.back();
}
};
llvm::iterator_range<ElaboratingDIEIterator>
elaborating_dies(const DWARFDIE &die) {
return llvm::make_range(ElaboratingDIEIterator(die),
ElaboratingDIEIterator());
}
} // namespace
DWARFDIE
DWARFDIE::GetParent() const {
if (IsValid())
return DWARFDIE(m_cu, m_die->GetParent());
else
return DWARFDIE();
}
DWARFDIE
DWARFDIE::GetFirstChild() const {
if (IsValid())
return DWARFDIE(m_cu, m_die->GetFirstChild());
else
return DWARFDIE();
}
DWARFDIE
DWARFDIE::GetSibling() const {
if (IsValid())
return DWARFDIE(m_cu, m_die->GetSibling());
else
return DWARFDIE();
}
DWARFDIE
DWARFDIE::GetReferencedDIE(const dw_attr_t attr) const {
if (IsValid())
return m_die->GetAttributeValueAsReference(GetCU(), attr);
else
return {};
}
DWARFDIE
DWARFDIE::GetDIE(dw_offset_t die_offset) const {
if (IsValid())
return m_cu->GetDIE(die_offset);
else
return DWARFDIE();
}
DWARFDIE
DWARFDIE::GetAttributeValueAsReferenceDIE(const dw_attr_t attr) const {
if (IsValid()) {
DWARFUnit *cu = GetCU();
const bool check_specification_or_abstract_origin = true;
DWARFFormValue form_value;
if (m_die->GetAttributeValue(cu, attr, form_value, nullptr,
check_specification_or_abstract_origin))
return form_value.Reference();
}
return DWARFDIE();
}
DWARFDIE
DWARFDIE::LookupDeepestBlock(lldb::addr_t address) const {
if (!IsValid())
return DWARFDIE();
DWARFDIE result;
bool check_children = false;
bool match_addr_range = false;
switch (Tag()) {
case DW_TAG_class_type:
case DW_TAG_namespace:
case DW_TAG_structure_type:
case DW_TAG_common_block:
check_children = true;
break;
case DW_TAG_compile_unit:
case DW_TAG_module:
case DW_TAG_catch_block:
case DW_TAG_subprogram:
case DW_TAG_try_block:
case DW_TAG_partial_unit:
match_addr_range = true;
break;
case DW_TAG_lexical_block:
case DW_TAG_inlined_subroutine:
check_children = true;
match_addr_range = true;
break;
default:
break;
}
if (match_addr_range) {
DWARFRangeList ranges =
m_die->GetAttributeAddressRanges(m_cu, /*check_hi_lo_pc=*/true);
if (ranges.FindEntryThatContains(address)) {
check_children = true;
switch (Tag()) {
default:
break;
case DW_TAG_inlined_subroutine: // Inlined Function
case DW_TAG_lexical_block: // Block { } in code
result = *this;
break;
}
} else {
check_children = false;
}
}
if (check_children) {
for (DWARFDIE child : children()) {
if (DWARFDIE child_result = child.LookupDeepestBlock(address))
return child_result;
}
}
return result;
}
const char *DWARFDIE::GetMangledName() const {
if (IsValid())
return m_die->GetMangledName(m_cu);
else
return nullptr;
}
const char *DWARFDIE::GetPubname() const {
if (IsValid())
return m_die->GetPubname(m_cu);
else
return nullptr;
}
// GetName
//
// Get value of the DW_AT_name attribute and place that value into the supplied
// stream object. If the DIE is a NULL object "NULL" is placed into the stream,
// and if no DW_AT_name attribute exists for the DIE then nothing is printed.
void DWARFDIE::GetName(Stream &s) const {
if (!IsValid())
return;
if (GetDIE()->IsNULL()) {
s.PutCString("NULL");
return;
}
const char *name = GetDIE()->GetAttributeValueAsString(GetCU(), DW_AT_name, nullptr, true);
if (!name)
return;
s.PutCString(name);
}
// AppendTypeName
//
// Follows the type name definition down through all needed tags to end up with
// a fully qualified type name and dump the results to the supplied stream.
// This is used to show the name of types given a type identifier.
void DWARFDIE::AppendTypeName(Stream &s) const {
if (!IsValid())
return;
if (GetDIE()->IsNULL()) {
s.PutCString("NULL");
return;
}
if (const char *name = GetPubname()) {
s.PutCString(name);
return;
}
switch (Tag()) {
case DW_TAG_array_type:
break; // print out a "[]" after printing the full type of the element
// below
case DW_TAG_base_type:
s.PutCString("base ");
break;
case DW_TAG_class_type:
s.PutCString("class ");
break;
case DW_TAG_const_type:
s.PutCString("const ");
break;
case DW_TAG_enumeration_type:
s.PutCString("enum ");
break;
case DW_TAG_file_type:
s.PutCString("file ");
break;
case DW_TAG_interface_type:
s.PutCString("interface ");
break;
case DW_TAG_packed_type:
s.PutCString("packed ");
break;
case DW_TAG_pointer_type:
break; // print out a '*' after printing the full type below
case DW_TAG_ptr_to_member_type:
break; // print out a '*' after printing the full type below
case DW_TAG_reference_type:
break; // print out a '&' after printing the full type below
case DW_TAG_restrict_type:
s.PutCString("restrict ");
break;
case DW_TAG_set_type:
s.PutCString("set ");
break;
case DW_TAG_shared_type:
s.PutCString("shared ");
break;
case DW_TAG_string_type:
s.PutCString("string ");
break;
case DW_TAG_structure_type:
s.PutCString("struct ");
break;
case DW_TAG_subrange_type:
s.PutCString("subrange ");
break;
case DW_TAG_subroutine_type:
s.PutCString("function ");
break;
case DW_TAG_thrown_type:
s.PutCString("thrown ");
break;
case DW_TAG_union_type:
s.PutCString("union ");
break;
case DW_TAG_unspecified_type:
s.PutCString("unspecified ");
break;
case DW_TAG_volatile_type:
s.PutCString("volatile ");
break;
case DW_TAG_LLVM_ptrauth_type: {
unsigned key = GetAttributeValueAsUnsigned(DW_AT_LLVM_ptrauth_key, 0);
bool isAddressDiscriminated = GetAttributeValueAsUnsigned(
DW_AT_LLVM_ptrauth_address_discriminated, 0);
unsigned extraDiscriminator =
GetAttributeValueAsUnsigned(DW_AT_LLVM_ptrauth_extra_discriminator, 0);
bool isaPointer =
GetAttributeValueAsUnsigned(DW_AT_LLVM_ptrauth_isa_pointer, 0);
s.Printf("__ptrauth(%d, %d, 0x0%x, %d)", key, isAddressDiscriminated,
extraDiscriminator, isaPointer);
break;
}
default:
return;
}
// Follow the DW_AT_type if possible
if (DWARFDIE next_die = GetAttributeValueAsReferenceDIE(DW_AT_type))
next_die.AppendTypeName(s);
switch (Tag()) {
case DW_TAG_array_type:
s.PutCString("[]");
break;
case DW_TAG_pointer_type:
s.PutChar('*');
break;
case DW_TAG_ptr_to_member_type:
s.PutChar('*');
break;
case DW_TAG_reference_type:
s.PutChar('&');
break;
default:
break;
}
}
lldb_private::Type *DWARFDIE::ResolveType() const {
if (IsValid())
return GetDWARF()->ResolveType(*this, true);
else
return nullptr;
}
lldb_private::Type *DWARFDIE::ResolveTypeUID(const DWARFDIE &die) const {
if (SymbolFileDWARF *dwarf = GetDWARF())
return dwarf->ResolveTypeUID(die, true);
return nullptr;
}
std::vector<DWARFDIE> DWARFDIE::GetDeclContextDIEs() const {
if (!IsValid())
return {};
std::vector<DWARFDIE> result;
DWARFDIE parent = GetParentDeclContextDIE();
while (parent.IsValid() && parent.GetDIE() != GetDIE()) {
result.push_back(std::move(parent));
parent = parent.GetParentDeclContextDIE();
}
return result;
}
std::vector<lldb_private::CompilerContext> DWARFDIE::GetDeclContext() const {
std::vector<lldb_private::CompilerContext> context;
const dw_tag_t tag = Tag();
if (tag == DW_TAG_compile_unit || tag == DW_TAG_partial_unit)
return context;
DWARFDIE parent = GetParent();
if (parent)
context = parent.GetDeclContext();
auto push_ctx = [&](CompilerContextKind kind, llvm::StringRef name) {
context.push_back({kind, ConstString(name)});
};
switch (tag) {
case DW_TAG_module:
push_ctx(CompilerContextKind::Module, GetName());
break;
case DW_TAG_namespace:
push_ctx(CompilerContextKind::Namespace, GetName());
break;
case DW_TAG_structure_type:
push_ctx(CompilerContextKind::Struct, GetName());
break;
case DW_TAG_union_type:
push_ctx(CompilerContextKind::Union, GetName());
break;
case DW_TAG_class_type:
push_ctx(CompilerContextKind::Class, GetName());
break;
case DW_TAG_enumeration_type:
push_ctx(CompilerContextKind::Enum, GetName());
break;
case DW_TAG_subprogram:
push_ctx(CompilerContextKind::Function, GetPubname());
break;
case DW_TAG_variable:
push_ctx(CompilerContextKind::Variable, GetPubname());
break;
case DW_TAG_typedef:
push_ctx(CompilerContextKind::Typedef, GetName());
break;
default:
break;
}
return context;
}
std::vector<lldb_private::CompilerContext>
DWARFDIE::GetTypeLookupContext() const {
std::vector<lldb_private::CompilerContext> context;
// If there is no name, then there is no need to look anything up for this
// DIE.
const char *name = GetName();
if (!name || !name[0])
return context;
const dw_tag_t tag = Tag();
if (tag == DW_TAG_compile_unit || tag == DW_TAG_partial_unit)
return context;
DWARFDIE parent = GetParent();
if (parent)
context = parent.GetTypeLookupContext();
auto push_ctx = [&](CompilerContextKind kind, llvm::StringRef name) {
context.push_back({kind, ConstString(name)});
};
switch (tag) {
case DW_TAG_namespace:
push_ctx(CompilerContextKind::Namespace, name);
break;
case DW_TAG_structure_type:
push_ctx(CompilerContextKind::Struct, name);
break;
case DW_TAG_union_type:
push_ctx(CompilerContextKind::Union, name);
break;
case DW_TAG_class_type:
push_ctx(CompilerContextKind::Class, name);
break;
case DW_TAG_enumeration_type:
push_ctx(CompilerContextKind::Enum, name);
break;
case DW_TAG_variable:
push_ctx(CompilerContextKind::Variable, GetPubname());
break;
case DW_TAG_typedef:
push_ctx(CompilerContextKind::Typedef, name);
break;
case DW_TAG_base_type:
push_ctx(CompilerContextKind::Builtin, name);
break;
default:
break;
}
return context;
}
DWARFDIE
DWARFDIE::GetParentDeclContextDIE() const {
if (IsValid())
return m_die->GetParentDeclContextDIE(m_cu);
else
return DWARFDIE();
}
bool DWARFDIE::IsStructUnionOrClass() const {
const dw_tag_t tag = Tag();
return tag == DW_TAG_class_type || tag == DW_TAG_structure_type ||
tag == DW_TAG_union_type;
}
bool DWARFDIE::IsMethod() const {
for (DWARFDIE d : elaborating_dies(*this))
if (d.GetParent().IsStructUnionOrClass())
return true;
return false;
}
bool DWARFDIE::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,
lldb_private::DWARFExpressionList *frame_base) const {
if (IsValid()) {
return m_die->GetDIENamesAndRanges(
GetCU(), name, mangled, ranges, decl_file, decl_line, decl_column,
call_file, call_line, call_column, frame_base);
} else
return false;
}
llvm::iterator_range<DWARFDIE::child_iterator> DWARFDIE::children() const {
return llvm::make_range(child_iterator(*this), child_iterator());
}