Skip to content

Commit 878a64f

Browse files
[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
1 parent 03da079 commit 878a64f

File tree

77 files changed

+505
-355
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

77 files changed

+505
-355
lines changed

lldb/include/lldb/Expression/ExpressionVariable.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class ExpressionVariable
3333

3434
virtual ~ExpressionVariable() = default;
3535

36-
std::optional<uint64_t> GetByteSize() { return m_frozen_sp->GetByteSize(); }
36+
llvm::Expected<uint64_t> GetByteSize() { return m_frozen_sp->GetByteSize(); }
3737

3838
ConstString GetName() { return m_frozen_sp->GetName(); }
3939

lldb/include/lldb/Symbol/CompilerType.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -391,9 +391,9 @@ class CompilerType {
391391
struct IntegralTemplateArgument;
392392

393393
/// Return the size of the type in bytes.
394-
std::optional<uint64_t> GetByteSize(ExecutionContextScope *exe_scope) const;
394+
llvm::Expected<uint64_t> GetByteSize(ExecutionContextScope *exe_scope) const;
395395
/// Return the size of the type in bits.
396-
std::optional<uint64_t> GetBitSize(ExecutionContextScope *exe_scope) const;
396+
llvm::Expected<uint64_t> GetBitSize(ExecutionContextScope *exe_scope) const;
397397

398398
lldb::Encoding GetEncoding(uint64_t &count) const;
399399

lldb/include/lldb/Symbol/Type.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ class Type : public std::enable_shared_from_this<Type>, public UserID {
480480

481481
ConstString GetBaseName();
482482

483-
std::optional<uint64_t> GetByteSize(ExecutionContextScope *exe_scope);
483+
llvm::Expected<uint64_t> GetByteSize(ExecutionContextScope *exe_scope);
484484

485485
llvm::Expected<uint32_t> GetNumChildren(bool omit_empty_base_classes);
486486

lldb/include/lldb/Symbol/TypeSystem.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ class TypeSystem : public PluginInterface,
312312

313313
virtual const llvm::fltSemantics &GetFloatTypeSemantics(size_t byte_size) = 0;
314314

315-
virtual std::optional<uint64_t>
315+
virtual llvm::Expected<uint64_t>
316316
GetBitSize(lldb::opaque_compiler_type_t type,
317317
ExecutionContextScope *exe_scope) = 0;
318318

lldb/include/lldb/Target/StackFrameRecognizer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ class ValueObjectRecognizerSynthesizedValue : public ValueObject {
180180
SetName(parent.GetName());
181181
}
182182

183-
std::optional<uint64_t> GetByteSize() override {
183+
llvm::Expected<uint64_t> GetByteSize() override {
184184
return m_parent->GetByteSize();
185185
}
186186
lldb::ValueType GetValueType() const override { return m_type; }

lldb/include/lldb/ValueObject/ValueObject.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ class ValueObject {
357357
virtual bool CanProvideValue();
358358

359359
// Subclasses must implement the functions below.
360-
virtual std::optional<uint64_t> GetByteSize() = 0;
360+
virtual llvm::Expected<uint64_t> GetByteSize() = 0;
361361

362362
virtual lldb::ValueType GetValueType() const = 0;
363363

lldb/include/lldb/ValueObject/ValueObjectCast.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class ValueObjectCast : public ValueObject {
3030
static lldb::ValueObjectSP Create(ValueObject &parent, ConstString name,
3131
const CompilerType &cast_type);
3232

33-
std::optional<uint64_t> GetByteSize() override;
33+
llvm::Expected<uint64_t> GetByteSize() override;
3434

3535
llvm::Expected<uint32_t> CalculateNumChildren(uint32_t max) override;
3636

lldb/include/lldb/ValueObject/ValueObjectChild.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class ValueObjectChild : public ValueObject {
2929
public:
3030
~ValueObjectChild() override;
3131

32-
std::optional<uint64_t> GetByteSize() override { return m_byte_size; }
32+
llvm::Expected<uint64_t> GetByteSize() override { return m_byte_size; }
3333

3434
lldb::offset_t GetByteOffset() override { return m_byte_offset; }
3535

lldb/include/lldb/ValueObject/ValueObjectConstResult.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class ValueObjectConstResult : public ValueObject {
6464
static lldb::ValueObjectSP Create(ExecutionContextScope *exe_scope,
6565
Status &&error);
6666

67-
std::optional<uint64_t> GetByteSize() override;
67+
llvm::Expected<uint64_t> GetByteSize() override;
6868

6969
lldb::ValueType GetValueType() const override;
7070

lldb/include/lldb/ValueObject/ValueObjectDynamicValue.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class ValueObjectDynamicValue : public ValueObject {
3535
public:
3636
~ValueObjectDynamicValue() override = default;
3737

38-
std::optional<uint64_t> GetByteSize() override;
38+
llvm::Expected<uint64_t> GetByteSize() override;
3939

4040
ConstString GetTypeName() override;
4141

lldb/include/lldb/ValueObject/ValueObjectMemory.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class ValueObjectMemory : public ValueObject {
4141
const Address &address,
4242
const CompilerType &ast_type);
4343

44-
std::optional<uint64_t> GetByteSize() override;
44+
llvm::Expected<uint64_t> GetByteSize() override;
4545

4646
ConstString GetTypeName() override;
4747

lldb/include/lldb/ValueObject/ValueObjectRegister.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class ValueObjectRegisterSet : public ValueObject {
3737
lldb::RegisterContextSP &reg_ctx_sp,
3838
uint32_t set_idx);
3939

40-
std::optional<uint64_t> GetByteSize() override;
40+
llvm::Expected<uint64_t> GetByteSize() override;
4141

4242
lldb::ValueType GetValueType() const override {
4343
return lldb::eValueTypeRegisterSet;
@@ -89,7 +89,7 @@ class ValueObjectRegister : public ValueObject {
8989
lldb::RegisterContextSP &reg_ctx_sp,
9090
const RegisterInfo *reg_info);
9191

92-
std::optional<uint64_t> GetByteSize() override;
92+
llvm ::Expected<uint64_t> GetByteSize() override;
9393

9494
lldb::ValueType GetValueType() const override {
9595
return lldb::eValueTypeRegister;

lldb/include/lldb/ValueObject/ValueObjectSyntheticFilter.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class ValueObjectSynthetic : public ValueObject {
3737
public:
3838
~ValueObjectSynthetic() override;
3939

40-
std::optional<uint64_t> GetByteSize() override;
40+
llvm::Expected<uint64_t> GetByteSize() override;
4141

4242
ConstString GetTypeName() override;
4343

lldb/include/lldb/ValueObject/ValueObjectVTable.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class ValueObjectVTable : public ValueObject {
6262

6363
static lldb::ValueObjectSP Create(ValueObject &parent);
6464

65-
std::optional<uint64_t> GetByteSize() override;
65+
llvm::Expected<uint64_t> GetByteSize() override;
6666

6767
llvm::Expected<uint32_t> CalculateNumChildren(uint32_t max) override;
6868

lldb/include/lldb/ValueObject/ValueObjectVariable.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class ValueObjectVariable : public ValueObject {
3838
static lldb::ValueObjectSP Create(ExecutionContextScope *exe_scope,
3939
const lldb::VariableSP &var_sp);
4040

41-
std::optional<uint64_t> GetByteSize() override;
41+
llvm::Expected<uint64_t> GetByteSize() override;
4242

4343
ConstString GetTypeName() override;
4444

lldb/source/API/SBType.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,8 @@ uint64_t SBType::GetByteSize() {
127127
LLDB_INSTRUMENT_VA(this);
128128

129129
if (IsValid())
130-
if (std::optional<uint64_t> size =
131-
m_opaque_sp->GetCompilerType(false).GetByteSize(nullptr))
130+
if (std::optional<uint64_t> size = llvm::expectedToOptional(
131+
m_opaque_sp->GetCompilerType(false).GetByteSize(nullptr)))
132132
return *size;
133133
return 0;
134134
}

lldb/source/API/SBValue.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ size_t SBValue::GetByteSize() {
329329
ValueLocker locker;
330330
lldb::ValueObjectSP value_sp(GetSP(locker));
331331
if (value_sp) {
332-
result = value_sp->GetByteSize().value_or(0);
332+
result = llvm::expectedToOptional(value_sp->GetByteSize()).value_or(0);
333333
}
334334

335335
return result;

lldb/source/Commands/CommandObjectMemory.cpp

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -519,14 +519,14 @@ class CommandObjectMemoryRead : public CommandObjectParsed {
519519
--pointer_count;
520520
}
521521

522-
std::optional<uint64_t> size = compiler_type.GetByteSize(nullptr);
523-
if (!size) {
522+
auto size_or_err = compiler_type.GetByteSize(nullptr);
523+
if (!size_or_err) {
524524
result.AppendErrorWithFormat(
525-
"unable to get the byte size of the type '%s'\n",
526-
view_as_type_cstr);
525+
"unable to get the byte size of the type '%s'\n%s",
526+
view_as_type_cstr, llvm::toString(size_or_err.takeError()).c_str());
527527
return;
528528
}
529-
m_format_options.GetByteSizeValue() = *size;
529+
m_format_options.GetByteSizeValue() = *size_or_err;
530530

531531
if (!m_format_options.GetCountValue().OptionWasSet())
532532
m_format_options.GetCountValue() = 1;
@@ -639,15 +639,16 @@ class CommandObjectMemoryRead : public CommandObjectParsed {
639639
if (!m_format_options.GetFormatValue().OptionWasSet())
640640
m_format_options.GetFormatValue().SetCurrentValue(eFormatDefault);
641641

642-
std::optional<uint64_t> size = compiler_type.GetByteSize(nullptr);
643-
if (!size) {
644-
result.AppendError("can't get size of type");
642+
auto size_or_err = compiler_type.GetByteSize(nullptr);
643+
if (!size_or_err) {
644+
result.AppendError(llvm::toString(size_or_err.takeError()));
645645
return;
646646
}
647-
bytes_read = *size * m_format_options.GetCountValue().GetCurrentValue();
647+
auto size = *size_or_err;
648+
bytes_read = size * m_format_options.GetCountValue().GetCurrentValue();
648649

649650
if (argc > 0)
650-
addr = addr + (*size * m_memory_options.m_offset.GetCurrentValue());
651+
addr = addr + (size * m_memory_options.m_offset.GetCurrentValue());
651652
} else if (m_format_options.GetFormatValue().GetCurrentValue() !=
652653
eFormatCString) {
653654
data_sp = std::make_shared<DataBufferHeap>(total_byte_size, '\0');
@@ -1034,8 +1035,8 @@ class CommandObjectMemoryFind : public CommandObjectParsed {
10341035
frame, result_sp)) &&
10351036
result_sp) {
10361037
uint64_t value = result_sp->GetValueAsUnsigned(0);
1037-
std::optional<uint64_t> size =
1038-
result_sp->GetCompilerType().GetByteSize(nullptr);
1038+
std::optional<uint64_t> size = llvm::expectedToOptional(
1039+
result_sp->GetCompilerType().GetByteSize(nullptr));
10391040
if (!size)
10401041
return;
10411042
switch (*size) {

lldb/source/Commands/CommandObjectWatchpoint.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -867,9 +867,10 @@ corresponding to the byte size of the data type.");
867867
if (addr_type == eAddressTypeLoad) {
868868
// We're in business.
869869
// Find out the size of this variable.
870-
size = m_option_watchpoint.watch_size.GetCurrentValue() == 0
871-
? valobj_sp->GetByteSize().value_or(0)
872-
: m_option_watchpoint.watch_size.GetCurrentValue();
870+
size =
871+
m_option_watchpoint.watch_size.GetCurrentValue() == 0
872+
? llvm::expectedToOptional(valobj_sp->GetByteSize()).value_or(0)
873+
: m_option_watchpoint.watch_size.GetCurrentValue();
873874
}
874875
compiler_type = valobj_sp->GetCompilerType();
875876
} else {
@@ -1080,7 +1081,8 @@ class CommandObjectWatchpointSetExpression : public CommandObjectRaw {
10801081
/// of the expression, so convert to that if we found a valid type.
10811082
CompilerType compiler_type(valobj_sp->GetCompilerType());
10821083

1083-
std::optional<uint64_t> valobj_size = valobj_sp->GetByteSize();
1084+
std::optional<uint64_t> valobj_size =
1085+
llvm::expectedToOptional(valobj_sp->GetByteSize());
10841086
// Set the type as a uint8_t array if the size being watched is
10851087
// larger than the ValueObject's size (which is probably the size
10861088
// of a pointer).

lldb/source/Core/Value.cpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
#include "lldb/Utility/DataExtractor.h"
2525
#include "lldb/Utility/Endian.h"
2626
#include "lldb/Utility/FileSpec.h"
27+
#include "lldb/Utility/LLDBLog.h"
28+
#include "lldb/Utility/Log.h"
2729
#include "lldb/Utility/State.h"
2830
#include "lldb/Utility/Stream.h"
2931
#include "lldb/lldb-defines.h"
@@ -223,10 +225,16 @@ uint64_t Value::GetValueByteSize(Status *error_ptr, ExecutionContext *exe_ctx) {
223225
case ContextType::Variable: // Variable *
224226
{
225227
auto *scope = exe_ctx ? exe_ctx->GetBestExecutionContextScope() : nullptr;
226-
if (std::optional<uint64_t> size = GetCompilerType().GetByteSize(scope)) {
228+
auto size_or_err = GetCompilerType().GetByteSize(scope);
229+
if (!size_or_err) {
230+
if (error_ptr && error_ptr->Success())
231+
*error_ptr = Status::FromError(size_or_err.takeError());
232+
else
233+
LLDB_LOG_ERRORV(GetLog(LLDBLog::Types), size_or_err.takeError(), "{0}");
234+
} else {
227235
if (error_ptr)
228236
error_ptr->Clear();
229-
return *size;
237+
return *size_or_err;
230238
}
231239
break;
232240
}
@@ -321,8 +329,9 @@ Status Value::GetValueAsData(ExecutionContext *exe_ctx, DataExtractor &data,
321329
AddressType address_type = eAddressTypeFile;
322330
Address file_so_addr;
323331
const CompilerType &ast_type = GetCompilerType();
324-
std::optional<uint64_t> type_size = ast_type.GetByteSize(
325-
exe_ctx ? exe_ctx->GetBestExecutionContextScope() : nullptr);
332+
std::optional<uint64_t> type_size =
333+
llvm::expectedToOptional(ast_type.GetByteSize(
334+
exe_ctx ? exe_ctx->GetBestExecutionContextScope() : nullptr));
326335
// Nothing to be done for a zero-sized type.
327336
if (type_size && *type_size == 0)
328337
return error;

lldb/source/DataFormatters/TypeFormat.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,16 +96,20 @@ bool TypeFormatImpl_Format::FormatObject(ValueObject *valobj,
9696

9797
ExecutionContextScope *exe_scope =
9898
exe_ctx.GetBestExecutionContextScope();
99-
std::optional<uint64_t> size = compiler_type.GetByteSize(exe_scope);
100-
if (!size)
99+
auto size_or_err = compiler_type.GetByteSize(exe_scope);
100+
if (!size_or_err) {
101+
LLDB_LOG_ERRORV(
102+
GetLog(LLDBLog::Types), size_or_err.takeError(),
103+
"Cannot get size of type while formatting object: {0}");
101104
return false;
105+
}
102106
StreamString sstr;
103107
compiler_type.DumpTypeValue(
104108
&sstr, // The stream to use for display
105109
GetFormat(), // Format to display this type with
106110
data, // Data to extract from
107111
0, // Byte offset into "m_data"
108-
*size, // Byte size of item in "m_data"
112+
*size_or_err, // Byte size of item in "m_data"
109113
valobj->GetBitfieldBitSize(), // Bitfield bit size
110114
valobj->GetBitfieldBitOffset(), // Bitfield bit offset
111115
exe_scope);

lldb/source/DataFormatters/VectorType.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -197,15 +197,15 @@ static lldb::Format GetItemFormatForFormat(lldb::Format format,
197197
static std::optional<size_t>
198198
CalculateNumChildren(CompilerType container_elem_type, uint64_t num_elements,
199199
CompilerType element_type) {
200-
std::optional<uint64_t> container_elem_size =
201-
container_elem_type.GetByteSize(/* exe_scope */ nullptr);
200+
std::optional<uint64_t> container_elem_size = llvm::expectedToOptional(
201+
container_elem_type.GetByteSize(/* exe_scope */ nullptr));
202202
if (!container_elem_size)
203203
return {};
204204

205205
auto container_size = *container_elem_size * num_elements;
206206

207-
std::optional<uint64_t> element_size =
208-
element_type.GetByteSize(/* exe_scope */ nullptr);
207+
std::optional<uint64_t> element_size = llvm::expectedToOptional(
208+
element_type.GetByteSize(/* exe_scope */ nullptr));
209209
if (!element_size || !*element_size)
210210
return {};
211211

@@ -236,10 +236,11 @@ class VectorTypeSyntheticFrontEnd : public SyntheticChildrenFrontEnd {
236236
nullptr, Status::FromError(num_children_or_err.takeError()));
237237
if (idx >= *num_children_or_err)
238238
return {};
239-
std::optional<uint64_t> size = m_child_type.GetByteSize(nullptr);
240-
if (!size)
241-
return {};
242-
auto offset = idx * *size;
239+
auto size_or_err = m_child_type.GetByteSize(nullptr);
240+
if (!size_or_err)
241+
return ValueObjectConstResult::Create(
242+
nullptr, Status::FromError(size_or_err.takeError()));
243+
auto offset = idx * *size_or_err;
243244
StreamString idx_name;
244245
idx_name.Printf("[%" PRIu64 "]", (uint64_t)idx);
245246
ValueObjectSP child_sp(m_backend.GetSyntheticChildAtOffset(

lldb/source/Expression/ExpressionVariable.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ char ExpressionVariable::ID;
2020
ExpressionVariable::ExpressionVariable() : m_flags(0) {}
2121

2222
uint8_t *ExpressionVariable::GetValueBytes() {
23-
std::optional<uint64_t> byte_size = m_frozen_sp->GetByteSize();
23+
std::optional<uint64_t> byte_size =
24+
llvm::expectedToOptional(m_frozen_sp->GetByteSize());
2425
if (byte_size && *byte_size) {
2526
if (m_frozen_sp->GetDataExtractor().GetByteSize() < *byte_size) {
2627
m_frozen_sp->GetValue().ResizeData(*byte_size);

0 commit comments

Comments
 (0)