Skip to content

[next] Cherry-pick Swift-specific LLDB adjustments #10618

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

Merged
merged 17 commits into from
May 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
18227a6
[stable/20250402] Cherry-pick "[lldb] Upgrade CompilerType::GetBitSiz…
adrian-prantl Mar 6, 2025
b250056
[LLDB] Fix faulty merge
AnthonyLatsis Apr 30, 2025
dd2b5a4
[LLDB] Swift: Fix faulty cherry-pick
AnthonyLatsis Apr 30, 2025
9d4915e
[LLDB] Swift: Adjust use of `llvm::Module::setTargetTriple` (paramete…
AnthonyLatsis Apr 30, 2025
5e9fb22
[LLDB] Swift: Refactor method override (parameter type changed)
AnthonyLatsis Apr 30, 2025
425218e
[LLDB] Swift: Adjust method override (parameter type changed)
AnthonyLatsis Apr 30, 2025
813e73f
[LLDB] Swift: Adjust method override (parameter type changed)
AnthonyLatsis Apr 30, 2025
12b19a4
[LLDB] Swift: Switch from `UnwindPlan::RowSP` to `UnwindPlan::Row`
AnthonyLatsis Apr 30, 2025
a5fa504
[LLDB] Swift: Adjust call to `TypeSystemClang::GetMetadata` (return t…
AnthonyLatsis Apr 30, 2025
68e65f8
[LLDB] Swift: Add missing `TypeSummaryImpl::GetName` implementations
AnthonyLatsis Apr 30, 2025
c30cc8f
[LLDB] Swift: Adjust includes of moved Clang headers
AnthonyLatsis May 1, 2025
59d6a25
[LLDB] Swift: Adjust calls to moved & renamed methods
AnthonyLatsis May 1, 2025
e8f5534
[LLDB] Swift: s/DWARFRangeList/llvm::DWARFAddressRangeVector
AnthonyLatsis May 1, 2025
c9f9633
[LLDB] Swift: Adjust call to `Function::GetStartLineSourceInfo` (para…
AnthonyLatsis May 1, 2025
5c9b85d
[LLDB] Swift: Refactor call to now `protected` method
AnthonyLatsis May 1, 2025
8b90bf4
[LLDB] Swift: Add missing argument to call
AnthonyLatsis May 1, 2025
4bf37fd
[LLDB] Swift: Refactor `TypeSystemSwiftTypeRef` after `CompilerContex…
AnthonyLatsis May 1, 2025
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
1 change: 1 addition & 0 deletions lldb/source/Core/Mangled.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,7 @@ ConstString Mangled::GetDemangledNameImpl(
// explicitly unsupported on llvm.org.
#ifdef LLDB_ENABLE_SWIFT
{
const char *mangled_name = m_mangled.GetCString();
Log *log = GetLog(LLDBLog::Demangle);
LLDB_LOGF(log, "demangle swift: %s", mangled_name);
std::string demangled(SwiftLanguageRuntime::DemangleSymbolAsString(
Expand Down
12 changes: 6 additions & 6 deletions lldb/source/Host/macosx/objcxx/HostInfoMacOSXSwift.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -285,12 +285,12 @@ HostInfoMacOSX::GetSwiftResourceDir(llvm::Triple triple,
if (it != g_resource_dir_cache.end())
return it->getValue();

auto value = DetectSwiftResourceDir(
platform_sdk_path, swift_stdlib_os_dir,
HostInfo::GetSwiftResourceDir().GetPath(),
HostInfo::GetXcodeContentsDirectory().GetPath(),
PlatformDarwin::GetCurrentToolchainDirectory().GetPath(),
PlatformDarwin::GetCurrentCommandLineToolsDirectory().GetPath());
auto value =
DetectSwiftResourceDir(platform_sdk_path, swift_stdlib_os_dir,
HostInfo::GetSwiftResourceDir().GetPath(),
HostInfo::GetXcodeContentsDirectory().GetPath(),
GetCurrentXcodeToolchainDirectory().GetPath(),
GetCurrentCommandLineToolsDirectory().GetPath());
g_resource_dir_cache.insert({key, value});
return g_resource_dir_cache[key];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@

#include "llvm-c/Analysis.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/RewriteBuffer.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/BinaryFormat/Dwarf.h"
#include "llvm/IR/IRBuilder.h"
Expand All @@ -62,7 +63,6 @@
#include "llvm/TargetParser/Host.h"

#include "clang/Basic/Module.h"
#include "clang/Rewrite/Core/RewriteBuffer.h"

#include "swift/AST/ASTContext.h"
#include "swift/AST/DiagnosticConsumer.h"
Expand Down Expand Up @@ -1050,9 +1050,12 @@ MaterializeVariable(SwiftASTManipulatorBase::VariableInfo &variable,
// this check scattered in several places in the codebase, we should at
// some point centralize it.
lldb::StackFrameSP stack_frame_sp = stack_frame_wp.lock();
std::optional<uint64_t> size =
auto size_or_err =
variable.GetType().GetByteSize(stack_frame_sp.get());
if (repl && size && *size == 0) {
if (!size_or_err)
return size_or_err.takeError();
uint64_t size = *size_or_err;
if (repl && size == 0) {
auto &repl_mat = *llvm::cast<SwiftREPLMaterializer>(&materializer);
offset = repl_mat.AddREPLResultVariable(
variable.GetType(), variable.GetDecl(),
Expand Down Expand Up @@ -2341,7 +2344,7 @@ bool SwiftExpressionParser::RewriteExpression(
if (num_diags == 0)
return false;

clang::RewriteBuffer rewrite_buf;
llvm::RewriteBuffer rewrite_buf;
llvm::StringRef text_ref(m_expr.Text());
rewrite_buf.Initialize(text_ref);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,13 @@ class EntityREPLResultVariable : public Materializer::Entity {
ret->ValueUpdated();

if (variable) {
const size_t pvar_byte_size = ret->GetByteSize().value_or(0);
auto pvar_byte_size_or_err = ret->GetByteSize();
if (!pvar_byte_size_or_err) {
err = Status::FromError(pvar_byte_size_or_err.takeError());
return;
}

const uint64_t pvar_byte_size = *pvar_byte_size_or_err;
uint8_t *pvar_data = ret->GetValueBytes();

Status read_error;
Expand Down Expand Up @@ -226,9 +232,13 @@ class EntityREPLResultVariable : public Materializer::Entity {
demangle_ctx.clear();
}

std::optional<uint64_t> size =
auto size_or_err =
m_type.GetByteSize(execution_unit->GetBestExecutionContextScope());
if (size && *size == 0) {
if (!size_or_err) {
err = Status::FromError(size_or_err.takeError());
return;
}
if (*size_or_err == 0) {
MakeREPLResult(*execution_unit, err, nullptr);
return;
}
Expand Down Expand Up @@ -413,10 +423,15 @@ class EntityREPLPersistentVariable : public Materializer::Entity {
FixupResilientGlobal(var_addr, compiler_type, *execution_unit,
exe_scope->CalculateProcess(), read_error);

auto size_or_err = m_persistent_variable_sp->GetByteSize();
if (!size_or_err) {
err = Status::FromError(size_or_err.takeError());
return;
}

// FIXME: This may not work if the value is not bitwise-takable.
execution_unit->ReadMemory(
m_persistent_variable_sp->GetValueBytes(), var_addr,
m_persistent_variable_sp->GetByteSize().value_or(0), read_error);
execution_unit->ReadMemory(m_persistent_variable_sp->GetValueBytes(),
var_addr, *size_or_err, read_error);

if (!read_error.Success()) {
err = Status::FromErrorStringWithFormatv(
Expand Down Expand Up @@ -477,12 +492,13 @@ class EntityREPLPersistentVariable : public Materializer::Entity {
if (!err.Success()) {
dump_stream.Printf(" <could not be read>\n");
} else {
DataBufferHeap data(m_persistent_variable_sp->GetByteSize().value_or(0),
0);
uint64_t size =
llvm::expectedToOptional(m_persistent_variable_sp->GetByteSize())
.value_or(0);

DataBufferHeap data(size, 0);

map.ReadMemory(data.GetBytes(), target_address,
m_persistent_variable_sp->GetByteSize().value_or(0),
err);
map.ReadMemory(data.GetBytes(), target_address, size, err);

if (!err.Success()) {
dump_stream.Printf(" <could not be read>\n");
Expand Down
8 changes: 4 additions & 4 deletions lldb/source/Plugins/Language/Swift/SwiftFormatters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2050,8 +2050,8 @@ bool lldb_private::formatters::swift::SIMDVector_SummaryProvider(
return false;

ExecutionContext exe_ctx = valobj.GetExecutionContextRef().Lock(true);
std::optional<uint64_t> opt_type_size =
simd_type.GetByteSize(exe_ctx.GetBestExecutionContextScope());
std::optional<uint64_t> opt_type_size = llvm::expectedToOptional(
simd_type.GetByteSize(exe_ctx.GetBestExecutionContextScope()));
if (!opt_type_size)
return false;
uint64_t type_size = *opt_type_size;
Expand All @@ -2065,8 +2065,8 @@ bool lldb_private::formatters::swift::SIMDVector_SummaryProvider(
if (!arg_type)
return false;

std::optional<uint64_t> opt_arg_size =
arg_type.GetByteSize(exe_ctx.GetBestExecutionContextScope());
std::optional<uint64_t> opt_arg_size = llvm::expectedToOptional(
arg_type.GetByteSize(exe_ctx.GetBestExecutionContextScope()));
if (!opt_arg_size)
return false;
uint64_t arg_size = *opt_arg_size;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,12 +197,14 @@ class SwiftHiddenFrameRecognizer : public StackFrameRecognizer {
if (!sc.function)
return {};

FileSpec source_file;
SupportFileSP source_file_sp;
uint32_t line_no;
sc.function->GetStartLineSourceInfo(source_file, line_no);
sc.function->GetStartLineSourceInfo(source_file_sp, line_no);
// FIXME: these <compiler-generated> frames should be marked artificial
// by the Swift compiler.
if (source_file.GetFilename() == "<compiler-generated>" && line_no == 0)
if (source_file_sp &&
source_file_sp->GetSpecOnly().GetFilename() == "<compiler-generated>" &&
line_no == 0)
return m_hidden_frame;

auto symbol_name =
Expand Down
2 changes: 1 addition & 1 deletion lldb/source/Plugins/Language/Swift/SwiftLanguage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -845,7 +845,7 @@ ExtractSwiftTypeNameFromCxxInteropType(CompilerType type) {
}

const clang::RecordDecl *record_decl = record_type->getDecl();
auto *metadata = tsc->GetMetadata(record_decl);
auto metadata = tsc->GetMetadata(record_decl);
if (metadata && !metadata->GetIsPotentiallySwiftInteropType())
return {};

Expand Down
7 changes: 6 additions & 1 deletion lldb/source/Plugins/Language/Swift/SwiftOptionSet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ void lldb_private::formatters::swift::SwiftOptionSetSummaryProvider::
std::string lldb_private::formatters::swift::SwiftOptionSetSummaryProvider::
GetDescription() {
StreamString sstr;
sstr.Printf("`%s `%s%s%s%s%s%s%s", "Swift OptionSet summary provider",
sstr.Printf("`%s `%s%s%s%s%s%s%s", GetName().c_str(),
Cascades() ? "" : " (not cascading)", " (may show children)",
!DoesPrintValue(nullptr) ? " (hide value)" : "",
IsOneLiner() ? " (one-line printout)" : "",
Expand All @@ -128,6 +128,11 @@ std::string lldb_private::formatters::swift::SwiftOptionSetSummaryProvider::
return sstr.GetString().str();
}

std::string
lldb_private::formatters::swift::SwiftOptionSetSummaryProvider::GetName() {
return "Swift OptionSet summary provider";
}

bool lldb_private::formatters::swift::SwiftOptionSetSummaryProvider::
FormatObject(ValueObject *valobj, std::string &dest,
const TypeSummaryOptions &options) {
Expand Down
1 change: 1 addition & 0 deletions lldb/source/Plugins/Language/Swift/SwiftOptionSet.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ struct SwiftOptionSetSummaryProvider : public TypeSummaryImpl {
bool FormatObject(ValueObject *valobj, std::string &dest,
const TypeSummaryOptions &options) override;
std::string GetDescription() override;
std::string GetName() override;
bool DoesPrintChildren(ValueObject *valobj) const override;

private:
Expand Down
7 changes: 6 additions & 1 deletion lldb/source/Plugins/Language/Swift/SwiftOptional.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ using namespace lldb_private::formatters::swift;
std::string lldb_private::formatters::swift::SwiftOptionalSummaryProvider::
GetDescription() {
StreamString sstr;
sstr.Printf("`%s `%s%s%s%s%s%s%s", "Swift.Optional summary provider",
sstr.Printf("`%s `%s%s%s%s%s%s%s", GetName().c_str(),
Cascades() ? "" : " (not cascading)", " (may show children)",
!DoesPrintValue(nullptr) ? " (hide value)" : "",
IsOneLiner() ? " (one-line printout)" : "",
Expand All @@ -38,6 +38,11 @@ std::string lldb_private::formatters::swift::SwiftOptionalSummaryProvider::
return sstr.GetString().str();
}

std::string
lldb_private::formatters::swift::SwiftOptionalSummaryProvider::GetName() {
return "Swift.Optional summary provider";
}

/// If this ValueObject is an Optional<T> with the Some(T) case selected,
/// retrieve the value of the Some case.
///
Expand Down
1 change: 1 addition & 0 deletions lldb/source/Plugins/Language/Swift/SwiftOptional.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ struct SwiftOptionalSummaryProvider : public TypeSummaryImpl {
bool FormatObject(ValueObject *valobj, std::string &dest,
const TypeSummaryOptions &options) override;
std::string GetDescription() override;
std::string GetName() override;
bool DoesPrintChildren(ValueObject *valobj) const override;
bool DoesPrintValue(ValueObject *valobj) const override;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2538,7 +2538,7 @@ static llvm::Expected<addr_t> ReadAsyncContextRegisterFromUnwind(
pc.GetFileAddress());

const RegisterKind unwind_regkind = unwind_plan->GetRegisterKind();
UnwindPlan::RowSP row = unwind_plan->GetRowForFunctionOffset(
auto *row = unwind_plan->GetRowForFunctionOffset(
pc.GetFileAddress() - func_start_addr.GetFileAddress());

// To request info about a register from the unwind plan, the register must
Expand Down Expand Up @@ -2707,26 +2707,26 @@ SwiftLanguageRuntime::GetRuntimeUnwindPlan(ProcessSP process_sp,
if (!async_ctx)
return log_expected(async_ctx.takeError());

UnwindPlan::RowSP row(new UnwindPlan::Row);
UnwindPlan::Row row;
const int32_t ptr_size = 8;
row->SetOffset(0);
row.SetOffset(0);

// The CFA of a funclet is its own async context.
row->GetCFAValue().SetIsConstant(*async_ctx);
row.GetCFAValue().SetIsConstant(*async_ctx);

// The value of the async register in the parent frame (which is the
// continuation funclet) is the async context of this frame.
row->SetRegisterLocationToIsConstant(regnums->async_ctx_regnum, *async_ctx,
/*can_replace=*/false);
row.SetRegisterLocationToIsConstant(regnums->async_ctx_regnum, *async_ctx,
/*can_replace=*/false);

if (std::optional<addr_t> pc_after_prologue =
TrySkipVirtualParentProlog(*async_ctx, *process_sp))
row->SetRegisterLocationToIsConstant(regnums->pc_regnum, *pc_after_prologue,
false);
row.SetRegisterLocationToIsConstant(regnums->pc_regnum, *pc_after_prologue,
false);
else
row->SetRegisterLocationToAtCFAPlusOffset(regnums->pc_regnum, ptr_size,
false);
row->SetUnspecifiedRegistersAreUndefined(true);
row.SetRegisterLocationToAtCFAPlusOffset(regnums->pc_regnum, ptr_size,
false);
row.SetUnspecifiedRegistersAreUndefined(true);

UnwindPlanSP plan = std::make_shared<UnwindPlan>(lldb::eRegisterKindDWARF);
plan->AppendRow(row);
Expand All @@ -2744,31 +2744,31 @@ UnwindPlanSP SwiftLanguageRuntime::GetFollowAsyncContextUnwindPlan(
bool &behaves_like_zeroth_frame) {
LLDB_SCOPED_TIMER();

UnwindPlan::RowSP row(new UnwindPlan::Row);
UnwindPlan::Row row;
const int32_t ptr_size = 8;
row->SetOffset(0);
row.SetOffset(0);

std::optional<AsyncUnwindRegisterNumbers> regnums =
GetAsyncUnwindRegisterNumbers(arch.GetMachine());
if (!regnums)
return UnwindPlanSP();

row->GetCFAValue().SetIsRegisterDereferenced(regnums->async_ctx_regnum);
row.GetCFAValue().SetIsRegisterDereferenced(regnums->async_ctx_regnum);
// The value of the async register in the parent frame (which is the
// continuation funclet) is the async context of this frame.
row->SetRegisterLocationToIsCFAPlusOffset(regnums->async_ctx_regnum,
/*offset*/ 0, false);
row.SetRegisterLocationToIsCFAPlusOffset(regnums->async_ctx_regnum,
/*offset*/ 0, false);

const unsigned num_indirections = 1;
if (std::optional<addr_t> pc_after_prologue = TrySkipVirtualParentProlog(
GetAsyncContext(regctx), *process_sp, num_indirections))
row->SetRegisterLocationToIsConstant(regnums->pc_regnum, *pc_after_prologue,
false);
row.SetRegisterLocationToIsConstant(regnums->pc_regnum, *pc_after_prologue,
false);
else
row->SetRegisterLocationToAtCFAPlusOffset(regnums->pc_regnum, ptr_size,
false);
row.SetRegisterLocationToAtCFAPlusOffset(regnums->pc_regnum, ptr_size,
false);

row->SetUnspecifiedRegistersAreUndefined(true);
row.SetUnspecifiedRegistersAreUndefined(true);

UnwindPlanSP plan = std::make_shared<UnwindPlan>(lldb::eRegisterKindDWARF);
plan->AppendRow(row);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2987,8 +2987,7 @@ std::optional<SwiftNominalType> GetSwiftClass(ValueObject &valobj,

auto isa_load_addr = descriptor_sp->GetISA();
Address isa;
const auto &sections = objc_runtime.GetTargetRef().GetSectionLoadList();
if (!sections.ResolveLoadAddress(isa_load_addr, isa))
if (!objc_runtime.GetTargetRef().ResolveLoadAddress(isa_load_addr, isa))
return {};

// Next, iterate over the Module's symbol table, looking for a symbol with
Expand Down
Loading