Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[lldb] Add SBType::FindDirectNestedType() function #68705

Merged
merged 15 commits into from
Oct 14, 2023
Merged
8 changes: 8 additions & 0 deletions lldb/bindings/interface/SBTypeDocstrings.i
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,14 @@ SBType supports the eq/ne operator. For example,::
"
) lldb::SBType::GetTypeFlags;

%feature("docstring",
"Searches for a directly nested type that has the provided name.

Returns the type if it was found.
Returns invalid type if nothing was found.
"
) lldb::SBType::FindDirectNestedType;

%feature("docstring",
"Represents a list of :py:class:`SBType` s.

Expand Down
2 changes: 2 additions & 0 deletions lldb/include/lldb/API/SBType.h
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,8 @@ class SBType {
bool GetDescription(lldb::SBStream &description,
lldb::DescriptionLevel description_level);

lldb::SBType FindDirectNestedType(const char *name);

lldb::SBType &operator=(const lldb::SBType &rhs);

bool operator==(lldb::SBType &rhs);
Expand Down
2 changes: 2 additions & 0 deletions lldb/include/lldb/Symbol/Type.h
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,8 @@ class TypeImpl {
bool GetDescription(lldb_private::Stream &strm,
lldb::DescriptionLevel description_level);

CompilerType FindDirectNestedType(llvm::StringRef name);

private:
bool CheckModule(lldb::ModuleSP &module_sp) const;
bool CheckExeModule(lldb::ModuleSP &module_sp) const;
Expand Down
4 changes: 4 additions & 0 deletions lldb/include/lldb/Symbol/TypeSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@ class TypeSystem : public PluginInterface,

virtual lldb::LanguageType DeclContextGetLanguage(void *opaque_decl_ctx) = 0;

/// Returns the direct parent context of specified type
virtual CompilerDeclContext
GetCompilerDeclContextForType(const CompilerType &type);

// Tests
#ifndef NDEBUG
/// Verify the integrity of the type to catch CompilerTypes that mix
Expand Down
8 changes: 8 additions & 0 deletions lldb/source/API/SBType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,14 @@ lldb::TemplateArgumentKind SBType::GetTemplateArgumentKind(uint32_t idx) {
return eTemplateArgumentKindNull;
}

SBType SBType::FindDirectNestedType(const char *name) {
LLDB_INSTRUMENT_VA(this, name);

if (!IsValid())
return SBType();
return SBType(m_opaque_sp->FindDirectNestedType(name));
}

SBTypeList::SBTypeList() : m_opaque_up(new TypeListImpl()) {
LLDB_INSTRUMENT_VA(this);
}
Expand Down
7 changes: 7 additions & 0 deletions lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2636,6 +2636,13 @@ TypeSystemClang::GetDeclContextForType(const CompilerType &type) {
return GetDeclContextForType(ClangUtil::GetQualType(type));
}

CompilerDeclContext
TypeSystemClang::GetCompilerDeclContextForType(const CompilerType &type) {
if (auto *decl_context = GetDeclContextForType(type))
return CreateDeclContext(decl_context);
return CompilerDeclContext();
}

/// Aggressively desugar the provided type, skipping past various kinds of
/// syntactic sugar and other constructs one typically wants to ignore.
/// The \p mask argument allows one to skip certain kinds of simplifications,
Expand Down
3 changes: 3 additions & 0 deletions lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.h
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,9 @@ class TypeSystemClang : public TypeSystem {

static clang::DeclContext *GetDeclContextForType(const CompilerType &type);

CompilerDeclContext
GetCompilerDeclContextForType(const CompilerType &type) override;

uint32_t GetPointerByteSize() override;

clang::TranslationUnitDecl *GetTranslationUnitDecl() {
Expand Down
17 changes: 17 additions & 0 deletions lldb/source/Symbol/Type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1082,6 +1082,23 @@ bool TypeImpl::GetDescription(lldb_private::Stream &strm,
return true;
}

CompilerType TypeImpl::FindDirectNestedType(llvm::StringRef name) {
if (name.empty())
return CompilerType();
auto type_system = GetTypeSystem(/*prefer_dynamic*/ false);
auto *symbol_file = type_system->GetSymbolFile();
auto decl_context = type_system->GetCompilerDeclContextForType(m_static_type);
if (!decl_context.IsValid())
return CompilerType();
llvm::DenseSet<lldb_private::SymbolFile *> searched_symbol_files;
TypeMap search_result;
symbol_file->FindTypes(ConstString(name), decl_context, /*max_matches*/ 1,
searched_symbol_files, search_result);
if (search_result.Empty())
return CompilerType();
return search_result.GetTypeAtIndex(0)->GetFullCompilerType();
}

bool TypeMemberFunctionImpl::IsValid() {
return m_type.IsValid() && m_kind != lldb::eMemberFunctionKindUnknown;
}
Expand Down
5 changes: 5 additions & 0 deletions lldb/source/Symbol/TypeSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,11 @@ std::optional<llvm::json::Value> TypeSystem::ReportStatistics() {
return std::nullopt;
}

CompilerDeclContext
Endilll marked this conversation as resolved.
Show resolved Hide resolved
TypeSystem::GetCompilerDeclContextForType(const CompilerType &type) {
return CompilerDeclContext();
}

#pragma mark TypeSystemMap

TypeSystemMap::TypeSystemMap() : m_mutex(), m_map() {}
Expand Down
31 changes: 31 additions & 0 deletions lldb/test/API/python_api/type/TestTypeList.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,37 @@ def test(self):

self.assertEqual(task_type, task_head_pointee_type)

# Check whether we can find a directly nested type by name
name_type = task_type.FindDirectNestedType("name")
self.assertTrue(name_type)
self.DebugSBType(name_type)

Michael137 marked this conversation as resolved.
Show resolved Hide resolved
enum_type = task_type.FindDirectNestedType("E")
self.assertTrue(enum_type)
self.DebugSBType(enum_type)

union_type = task_type.FindDirectNestedType("U")
self.assertTrue(union_type)
self.DebugSBType(union_type)

# Check that we don't find indirectly nested types
self.assertTrue(enum_type.size == 1)
Michael137 marked this conversation as resolved.
Show resolved Hide resolved

invalid_type = task_type.FindDirectNestedType("E2")
self.assertFalse(invalid_type)

# Check that FindDirectNestedType handles types without DeclContext
# and other errorneous inputs
task_ptr_type = task_type.GetPointerType()
invalid_type = task_ptr_type.FindDirectNestedType("name")
self.assertFalse(invalid_type)

invalid_type = task_type.FindDirectNestedType("")
self.assertFalse(invalid_type)

invalid_type = task_type.FindDirectNestedType(None)
self.assertFalse(invalid_type)

# We'll now get the child member 'id' from 'task_head'.
id = task_head.GetChildMemberWithName("id")
self.DebugSBValue(id)
Expand Down
5 changes: 5 additions & 0 deletions lldb/test/API/python_api/type/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ class Task {
} my_type_is_nameless;
struct name {
int x;
enum E : int {} e;
enum E2 {} e2;
} my_type_is_named;
enum E : unsigned char {} e;
union U {
} u;
Task(int i, Task *n):
id(i),
next(n),
Expand Down
4 changes: 4 additions & 0 deletions llvm/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,10 @@ Changes to LLDB

* Methods in SBHostOS related to threads have had their implementations
removed. These methods will return a value indicating failure.
* ``SBType::FindDirectNestedType`` function is added. It's useful
for formatters to quickly find directly nested type when it's known
where to search for it, avoiding more expensive global search via
``SBTarget::FindFirstType``.

Changes to Sanitizers
---------------------
Expand Down