Skip to content

Commit 18227a6

Browse files
adrian-prantlbnbarham
authored andcommitted
[stable/20250402] Cherry-pick "[lldb] Upgrade CompilerType::GetBitSize to return llvm::Expected (llvm#129601)"
This patch pushes the error handling boundary for the GetBitSize() methods from Runtime into the Type and CompilerType APIs. This makes it easier to diagnose problems thanks to more meaningful error messages being available. GetBitSize() is often the first thing LLDB asks about a type, so this method is particularly important for a better user experience. rdar://145667239 (cherry picked from commit 878a64f) (cherry picked from commit 642dcd7) (cherry picked from commit d3e7a33)
1 parent 9cbd0a1 commit 18227a6

File tree

3 files changed

+36
-17
lines changed

3 files changed

+36
-17
lines changed

lldb/source/Plugins/ExpressionParser/Swift/SwiftExpressionParser.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1050,9 +1050,12 @@ MaterializeVariable(SwiftASTManipulatorBase::VariableInfo &variable,
10501050
// this check scattered in several places in the codebase, we should at
10511051
// some point centralize it.
10521052
lldb::StackFrameSP stack_frame_sp = stack_frame_wp.lock();
1053-
std::optional<uint64_t> size =
1053+
auto size_or_err =
10541054
variable.GetType().GetByteSize(stack_frame_sp.get());
1055-
if (repl && size && *size == 0) {
1055+
if (!size_or_err)
1056+
return size_or_err.takeError();
1057+
uint64_t size = *size_or_err;
1058+
if (repl && size == 0) {
10561059
auto &repl_mat = *llvm::cast<SwiftREPLMaterializer>(&materializer);
10571060
offset = repl_mat.AddREPLResultVariable(
10581061
variable.GetType(), variable.GetDecl(),

lldb/source/Plugins/ExpressionParser/Swift/SwiftREPLMaterializer.cpp

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,13 @@ class EntityREPLResultVariable : public Materializer::Entity {
158158
ret->ValueUpdated();
159159

160160
if (variable) {
161-
const size_t pvar_byte_size = ret->GetByteSize().value_or(0);
161+
auto pvar_byte_size_or_err = ret->GetByteSize();
162+
if (!pvar_byte_size_or_err) {
163+
err = Status::FromError(pvar_byte_size_or_err.takeError());
164+
return;
165+
}
166+
167+
const uint64_t pvar_byte_size = *pvar_byte_size_or_err;
162168
uint8_t *pvar_data = ret->GetValueBytes();
163169

164170
Status read_error;
@@ -226,9 +232,13 @@ class EntityREPLResultVariable : public Materializer::Entity {
226232
demangle_ctx.clear();
227233
}
228234

229-
std::optional<uint64_t> size =
235+
auto size_or_err =
230236
m_type.GetByteSize(execution_unit->GetBestExecutionContextScope());
231-
if (size && *size == 0) {
237+
if (!size_or_err) {
238+
err = Status::FromError(size_or_err.takeError());
239+
return;
240+
}
241+
if (*size_or_err == 0) {
232242
MakeREPLResult(*execution_unit, err, nullptr);
233243
return;
234244
}
@@ -413,10 +423,15 @@ class EntityREPLPersistentVariable : public Materializer::Entity {
413423
FixupResilientGlobal(var_addr, compiler_type, *execution_unit,
414424
exe_scope->CalculateProcess(), read_error);
415425

426+
auto size_or_err = m_persistent_variable_sp->GetByteSize();
427+
if (!size_or_err) {
428+
err = Status::FromError(size_or_err.takeError());
429+
return;
430+
}
431+
416432
// FIXME: This may not work if the value is not bitwise-takable.
417-
execution_unit->ReadMemory(
418-
m_persistent_variable_sp->GetValueBytes(), var_addr,
419-
m_persistent_variable_sp->GetByteSize().value_or(0), read_error);
433+
execution_unit->ReadMemory(m_persistent_variable_sp->GetValueBytes(),
434+
var_addr, *size_or_err, read_error);
420435

421436
if (!read_error.Success()) {
422437
err = Status::FromErrorStringWithFormatv(
@@ -477,12 +492,13 @@ class EntityREPLPersistentVariable : public Materializer::Entity {
477492
if (!err.Success()) {
478493
dump_stream.Printf(" <could not be read>\n");
479494
} else {
480-
DataBufferHeap data(m_persistent_variable_sp->GetByteSize().value_or(0),
481-
0);
495+
uint64_t size =
496+
llvm::expectedToOptional(m_persistent_variable_sp->GetByteSize())
497+
.value_or(0);
498+
499+
DataBufferHeap data(size, 0);
482500

483-
map.ReadMemory(data.GetBytes(), target_address,
484-
m_persistent_variable_sp->GetByteSize().value_or(0),
485-
err);
501+
map.ReadMemory(data.GetBytes(), target_address, size, err);
486502

487503
if (!err.Success()) {
488504
dump_stream.Printf(" <could not be read>\n");

lldb/source/Plugins/Language/Swift/SwiftFormatters.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2050,8 +2050,8 @@ bool lldb_private::formatters::swift::SIMDVector_SummaryProvider(
20502050
return false;
20512051

20522052
ExecutionContext exe_ctx = valobj.GetExecutionContextRef().Lock(true);
2053-
std::optional<uint64_t> opt_type_size =
2054-
simd_type.GetByteSize(exe_ctx.GetBestExecutionContextScope());
2053+
std::optional<uint64_t> opt_type_size = llvm::expectedToOptional(
2054+
simd_type.GetByteSize(exe_ctx.GetBestExecutionContextScope()));
20552055
if (!opt_type_size)
20562056
return false;
20572057
uint64_t type_size = *opt_type_size;
@@ -2065,8 +2065,8 @@ bool lldb_private::formatters::swift::SIMDVector_SummaryProvider(
20652065
if (!arg_type)
20662066
return false;
20672067

2068-
std::optional<uint64_t> opt_arg_size =
2069-
arg_type.GetByteSize(exe_ctx.GetBestExecutionContextScope());
2068+
std::optional<uint64_t> opt_arg_size = llvm::expectedToOptional(
2069+
arg_type.GetByteSize(exe_ctx.GetBestExecutionContextScope()));
20702070
if (!opt_arg_size)
20712071
return false;
20722072
uint64_t arg_size = *opt_arg_size;

0 commit comments

Comments
 (0)