Skip to content

[lldb] Resolve Swift-implemented Objective-C classes using Swift runtime #10548

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

Open
wants to merge 3 commits into
base: swift/release/6.2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions lldb/source/Plugins/ExpressionParser/Swift/SwiftREPL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -501,13 +501,27 @@ bool SwiftREPL::PrintOneVariable(Debugger &debugger, StreamFileSP &output_sp,
options.SetRevealEmptyAggregates(false);
options.SetHidePointerValue(true);
options.SetVariableFormatDisplayLanguage(lldb::eLanguageTypeSwift);
options.SetDeclPrintingHelper([](ConstString type_name,
ConstString var_name,
const DumpValueObjectOptions &options,
Stream &stream) -> bool {
options.SetDeclPrintingHelper([&](ConstString type_name,
ConstString var_name,
const DumpValueObjectOptions &options,
Stream &stream) -> bool {
if (!type_name || !var_name)
return false;

// Try to get the SwiftASTContext representation of the type. It
// will hide Objective-C implemention details that are not
// publicly declared in the SDK.
if (valobj_sp)
if (auto dyn_valobj_sp =
valobj_sp->GetDynamicValue(lldb::eDynamicCanRunTarget)) {
CompilerType dyn_type = dyn_valobj_sp->GetCompilerType();
Status error;
if (SwiftASTContext *ast_context = getSwiftASTContext()) {
CompilerType ast_type = ast_context->ImportType(dyn_type, error);
if (ast_type && error.Success())
type_name = ast_type.GetDisplayTypeName();
}
}
std::string type_name_str(type_name ? type_name.GetCString() : "");
for (auto iter = type_name_str.find(" *"); iter != std::string::npos;
iter = type_name_str.find(" *")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1951,15 +1951,30 @@ bool SwiftLanguageRuntime::GetDynamicTypeAndAddress_Class(
if (!error.Success())
return false;

bool is_clang_type = false;
auto tss = class_type.GetTypeSystem().dyn_cast_or_null<TypeSystemSwift>();
if (!tss) {
is_clang_type = true;
if (auto module_sp = in_value.GetModule()) {
auto type_system_or_err =
module_sp->GetTypeSystemForLanguage(lldb::eLanguageTypeSwift);
if (!type_system_or_err) {
llvm::consumeError(type_system_or_err.takeError());
return false;
}
auto ts_sp = *type_system_or_err;
tss =
llvm::cast<TypeSystemSwift>(ts_sp.get())->GetTypeSystemSwiftTypeRef();
}
}
if (!tss)
return false;

address.SetRawAddress(instance_ptr);
auto ts = tss->GetTypeSystemSwiftTypeRef();
if (!ts)
return false;
// Ask the Objective-C runtime about Objective-C types.
if (tss->IsImportedType(class_type.GetOpaqueQualType(), nullptr))
auto resolve_objc = [&]() {
if (auto *objc_runtime =
SwiftLanguageRuntime::GetObjCRuntime(GetProcess())) {
Value::ValueType value_type;
Expand All @@ -1985,11 +2000,12 @@ bool SwiftLanguageRuntime::GetDynamicTypeAndAddress_Class(
});
return found;
}
return false;
}
Log *log(GetLog(LLDBLog::Types));
// Scope reflection_ctx to minimize its lock scope.
{
return false;
};

auto resolve_swift = [&]() {
// Scope reflection_ctx to minimize its lock scope.
ThreadSafeReflectionContext reflection_ctx = GetReflectionContext();
if (!reflection_ctx)
return false;
Expand Down Expand Up @@ -2033,10 +2049,19 @@ bool SwiftLanguageRuntime::GetDynamicTypeAndAddress_Class(
return false;
}
}

LLDB_LOG(log, "dynamic type of instance_ptr {0:x} is {1}", instance_ptr,
LLDB_LOG(GetLog(LLDBLog::Types),
"dynamic type of instance_ptr {0:x} is {1}", instance_ptr,
class_type.GetMangledTypeName());
class_type_or_name.SetCompilerType(dynamic_type);
return true;
};

if (!resolve_swift()) {
// Ask the Objective-C runtime about Objective-C types.
if (is_clang_type || !tss->IsImportedType(class_type.GetOpaqueQualType(), nullptr))
if (resolve_objc())
return true;
return false;
}

#ifndef NDEBUG
Expand Down Expand Up @@ -3017,22 +3042,28 @@ bool SwiftLanguageRuntime::GetDynamicTypeAndAddress(
return false;

LLDB_SCOPED_TIMER();
CompilerType val_type(in_value.GetCompilerType());
Value::ValueType static_value_type = Value::ValueType::Invalid;

// Try to import a Clang type into Swift.
if (in_value.GetObjectRuntimeLanguage() == eLanguageTypeObjC)
return GetDynamicTypeAndAddress_ClangType(in_value, use_dynamic,
class_type_or_name, address,
value_type, local_buffer);
if (in_value.GetObjectRuntimeLanguage() == eLanguageTypeObjC) {
if (GetDynamicTypeAndAddress_ClangType(in_value, use_dynamic,
class_type_or_name, address,
value_type, local_buffer))
return true;
else

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Else after return?

return GetDynamicTypeAndAddress_Class(in_value, val_type, use_dynamic,
class_type_or_name, address,
static_value_type, local_buffer);
}

if (!CouldHaveDynamicValue(in_value))
return false;

CompilerType val_type(in_value.GetCompilerType());
Flags type_info(val_type.GetTypeInfo());
if (!type_info.AnySet(eTypeIsSwift))
return false;

Value::ValueType static_value_type = Value::ValueType::Invalid;
bool success = false;
bool is_indirect_enum_case = IsIndirectEnumCase(in_value);
// Type kinds with instance metadata don't need generic type resolution.
Expand Down