diff options
author | Adrian Prantl <aprantl@apple.com> | 2025-03-05 10:21:19 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-03-05 10:21:19 -0800 |
commit | 878a64f94a264ea4b564d6063614ddb0b5da3f6c (patch) | |
tree | 5aaa6c770d5764fd3f9822882376c85c7ed427ef /lldb/source | |
parent | 03da079968845e1f1312d09ff4e2ecee1933552e (diff) | |
download | llvm-878a64f94a264ea4b564d6063614ddb0b5da3f6c.zip llvm-878a64f94a264ea4b564d6063614ddb0b5da3f6c.tar.gz llvm-878a64f94a264ea4b564d6063614ddb0b5da3f6c.tar.bz2 |
[lldb] Upgrade CompilerType::GetBitSize to return llvm::Expected (#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
Diffstat (limited to 'lldb/source')
59 files changed, 484 insertions, 335 deletions
diff --git a/lldb/source/API/SBType.cpp b/lldb/source/API/SBType.cpp index 9eb1f0c..00f2871 100644 --- a/lldb/source/API/SBType.cpp +++ b/lldb/source/API/SBType.cpp @@ -127,8 +127,8 @@ uint64_t SBType::GetByteSize() { LLDB_INSTRUMENT_VA(this); if (IsValid()) - if (std::optional<uint64_t> size = - m_opaque_sp->GetCompilerType(false).GetByteSize(nullptr)) + if (std::optional<uint64_t> size = llvm::expectedToOptional( + m_opaque_sp->GetCompilerType(false).GetByteSize(nullptr))) return *size; return 0; } diff --git a/lldb/source/API/SBValue.cpp b/lldb/source/API/SBValue.cpp index a707b9a..6b91120 100644 --- a/lldb/source/API/SBValue.cpp +++ b/lldb/source/API/SBValue.cpp @@ -329,7 +329,7 @@ size_t SBValue::GetByteSize() { ValueLocker locker; lldb::ValueObjectSP value_sp(GetSP(locker)); if (value_sp) { - result = value_sp->GetByteSize().value_or(0); + result = llvm::expectedToOptional(value_sp->GetByteSize()).value_or(0); } return result; diff --git a/lldb/source/Commands/CommandObjectMemory.cpp b/lldb/source/Commands/CommandObjectMemory.cpp index f2fa539..7140333 100644 --- a/lldb/source/Commands/CommandObjectMemory.cpp +++ b/lldb/source/Commands/CommandObjectMemory.cpp @@ -519,14 +519,14 @@ protected: --pointer_count; } - std::optional<uint64_t> size = compiler_type.GetByteSize(nullptr); - if (!size) { + auto size_or_err = compiler_type.GetByteSize(nullptr); + if (!size_or_err) { result.AppendErrorWithFormat( - "unable to get the byte size of the type '%s'\n", - view_as_type_cstr); + "unable to get the byte size of the type '%s'\n%s", + view_as_type_cstr, llvm::toString(size_or_err.takeError()).c_str()); return; } - m_format_options.GetByteSizeValue() = *size; + m_format_options.GetByteSizeValue() = *size_or_err; if (!m_format_options.GetCountValue().OptionWasSet()) m_format_options.GetCountValue() = 1; @@ -639,15 +639,16 @@ protected: if (!m_format_options.GetFormatValue().OptionWasSet()) m_format_options.GetFormatValue().SetCurrentValue(eFormatDefault); - std::optional<uint64_t> size = compiler_type.GetByteSize(nullptr); - if (!size) { - result.AppendError("can't get size of type"); + auto size_or_err = compiler_type.GetByteSize(nullptr); + if (!size_or_err) { + result.AppendError(llvm::toString(size_or_err.takeError())); return; } - bytes_read = *size * m_format_options.GetCountValue().GetCurrentValue(); + auto size = *size_or_err; + bytes_read = size * m_format_options.GetCountValue().GetCurrentValue(); if (argc > 0) - addr = addr + (*size * m_memory_options.m_offset.GetCurrentValue()); + addr = addr + (size * m_memory_options.m_offset.GetCurrentValue()); } else if (m_format_options.GetFormatValue().GetCurrentValue() != eFormatCString) { data_sp = std::make_shared<DataBufferHeap>(total_byte_size, '\0'); @@ -1034,8 +1035,8 @@ protected: frame, result_sp)) && result_sp) { uint64_t value = result_sp->GetValueAsUnsigned(0); - std::optional<uint64_t> size = - result_sp->GetCompilerType().GetByteSize(nullptr); + std::optional<uint64_t> size = llvm::expectedToOptional( + result_sp->GetCompilerType().GetByteSize(nullptr)); if (!size) return; switch (*size) { diff --git a/lldb/source/Commands/CommandObjectWatchpoint.cpp b/lldb/source/Commands/CommandObjectWatchpoint.cpp index 766d650..20f4b91 100644 --- a/lldb/source/Commands/CommandObjectWatchpoint.cpp +++ b/lldb/source/Commands/CommandObjectWatchpoint.cpp @@ -867,9 +867,10 @@ protected: if (addr_type == eAddressTypeLoad) { // We're in business. // Find out the size of this variable. - size = m_option_watchpoint.watch_size.GetCurrentValue() == 0 - ? valobj_sp->GetByteSize().value_or(0) - : m_option_watchpoint.watch_size.GetCurrentValue(); + size = + m_option_watchpoint.watch_size.GetCurrentValue() == 0 + ? llvm::expectedToOptional(valobj_sp->GetByteSize()).value_or(0) + : m_option_watchpoint.watch_size.GetCurrentValue(); } compiler_type = valobj_sp->GetCompilerType(); } else { @@ -1080,7 +1081,8 @@ protected: /// of the expression, so convert to that if we found a valid type. CompilerType compiler_type(valobj_sp->GetCompilerType()); - std::optional<uint64_t> valobj_size = valobj_sp->GetByteSize(); + std::optional<uint64_t> valobj_size = + llvm::expectedToOptional(valobj_sp->GetByteSize()); // Set the type as a uint8_t array if the size being watched is // larger than the ValueObject's size (which is probably the size // of a pointer). diff --git a/lldb/source/Core/Value.cpp b/lldb/source/Core/Value.cpp index 70299cb..c91b3f8 100644 --- a/lldb/source/Core/Value.cpp +++ b/lldb/source/Core/Value.cpp @@ -24,6 +24,8 @@ #include "lldb/Utility/DataExtractor.h" #include "lldb/Utility/Endian.h" #include "lldb/Utility/FileSpec.h" +#include "lldb/Utility/LLDBLog.h" +#include "lldb/Utility/Log.h" #include "lldb/Utility/State.h" #include "lldb/Utility/Stream.h" #include "lldb/lldb-defines.h" @@ -223,10 +225,16 @@ uint64_t Value::GetValueByteSize(Status *error_ptr, ExecutionContext *exe_ctx) { case ContextType::Variable: // Variable * { auto *scope = exe_ctx ? exe_ctx->GetBestExecutionContextScope() : nullptr; - if (std::optional<uint64_t> size = GetCompilerType().GetByteSize(scope)) { + auto size_or_err = GetCompilerType().GetByteSize(scope); + if (!size_or_err) { + if (error_ptr && error_ptr->Success()) + *error_ptr = Status::FromError(size_or_err.takeError()); + else + LLDB_LOG_ERRORV(GetLog(LLDBLog::Types), size_or_err.takeError(), "{0}"); + } else { if (error_ptr) error_ptr->Clear(); - return *size; + return *size_or_err; } break; } @@ -321,8 +329,9 @@ Status Value::GetValueAsData(ExecutionContext *exe_ctx, DataExtractor &data, AddressType address_type = eAddressTypeFile; Address file_so_addr; const CompilerType &ast_type = GetCompilerType(); - std::optional<uint64_t> type_size = ast_type.GetByteSize( - exe_ctx ? exe_ctx->GetBestExecutionContextScope() : nullptr); + std::optional<uint64_t> type_size = + llvm::expectedToOptional(ast_type.GetByteSize( + exe_ctx ? exe_ctx->GetBestExecutionContextScope() : nullptr)); // Nothing to be done for a zero-sized type. if (type_size && *type_size == 0) return error; diff --git a/lldb/source/DataFormatters/TypeFormat.cpp b/lldb/source/DataFormatters/TypeFormat.cpp index 409c452..f4cb8b4 100644 --- a/lldb/source/DataFormatters/TypeFormat.cpp +++ b/lldb/source/DataFormatters/TypeFormat.cpp @@ -96,16 +96,20 @@ bool TypeFormatImpl_Format::FormatObject(ValueObject *valobj, ExecutionContextScope *exe_scope = exe_ctx.GetBestExecutionContextScope(); - std::optional<uint64_t> size = compiler_type.GetByteSize(exe_scope); - if (!size) + auto size_or_err = compiler_type.GetByteSize(exe_scope); + if (!size_or_err) { + LLDB_LOG_ERRORV( + GetLog(LLDBLog::Types), size_or_err.takeError(), + "Cannot get size of type while formatting object: {0}"); return false; + } StreamString sstr; compiler_type.DumpTypeValue( &sstr, // The stream to use for display GetFormat(), // Format to display this type with data, // Data to extract from 0, // Byte offset into "m_data" - *size, // Byte size of item in "m_data" + *size_or_err, // Byte size of item in "m_data" valobj->GetBitfieldBitSize(), // Bitfield bit size valobj->GetBitfieldBitOffset(), // Bitfield bit offset exe_scope); diff --git a/lldb/source/DataFormatters/VectorType.cpp b/lldb/source/DataFormatters/VectorType.cpp index fa3fb1b..162b075 100644 --- a/lldb/source/DataFormatters/VectorType.cpp +++ b/lldb/source/DataFormatters/VectorType.cpp @@ -197,15 +197,15 @@ static lldb::Format GetItemFormatForFormat(lldb::Format format, static std::optional<size_t> CalculateNumChildren(CompilerType container_elem_type, uint64_t num_elements, CompilerType element_type) { - std::optional<uint64_t> container_elem_size = - container_elem_type.GetByteSize(/* exe_scope */ nullptr); + std::optional<uint64_t> container_elem_size = llvm::expectedToOptional( + container_elem_type.GetByteSize(/* exe_scope */ nullptr)); if (!container_elem_size) return {}; auto container_size = *container_elem_size * num_elements; - std::optional<uint64_t> element_size = - element_type.GetByteSize(/* exe_scope */ nullptr); + std::optional<uint64_t> element_size = llvm::expectedToOptional( + element_type.GetByteSize(/* exe_scope */ nullptr)); if (!element_size || !*element_size) return {}; @@ -236,10 +236,11 @@ public: nullptr, Status::FromError(num_children_or_err.takeError())); if (idx >= *num_children_or_err) return {}; - std::optional<uint64_t> size = m_child_type.GetByteSize(nullptr); - if (!size) - return {}; - auto offset = idx * *size; + auto size_or_err = m_child_type.GetByteSize(nullptr); + if (!size_or_err) + return ValueObjectConstResult::Create( + nullptr, Status::FromError(size_or_err.takeError())); + auto offset = idx * *size_or_err; StreamString idx_name; idx_name.Printf("[%" PRIu64 "]", (uint64_t)idx); ValueObjectSP child_sp(m_backend.GetSyntheticChildAtOffset( diff --git a/lldb/source/Expression/ExpressionVariable.cpp b/lldb/source/Expression/ExpressionVariable.cpp index f0a2898..9e8ea60 100644 --- a/lldb/source/Expression/ExpressionVariable.cpp +++ b/lldb/source/Expression/ExpressionVariable.cpp @@ -20,7 +20,8 @@ char ExpressionVariable::ID; ExpressionVariable::ExpressionVariable() : m_flags(0) {} uint8_t *ExpressionVariable::GetValueBytes() { - std::optional<uint64_t> byte_size = m_frozen_sp->GetByteSize(); + std::optional<uint64_t> byte_size = + llvm::expectedToOptional(m_frozen_sp->GetByteSize()); if (byte_size && *byte_size) { if (m_frozen_sp->GetDataExtractor().GetByteSize() < *byte_size) { m_frozen_sp->GetValue().ResizeData(*byte_size); diff --git a/lldb/source/Expression/Materializer.cpp b/lldb/source/Expression/Materializer.cpp index 13a72a9..8d48b5e 100644 --- a/lldb/source/Expression/Materializer.cpp +++ b/lldb/source/Expression/Materializer.cpp @@ -78,8 +78,9 @@ public: const bool zero_memory = false; lldb::addr_t mem = map.Malloc( - m_persistent_variable_sp->GetByteSize().value_or(0), 8, - lldb::ePermissionsReadable | lldb::ePermissionsWritable, + llvm::expectedToOptional(m_persistent_variable_sp->GetByteSize()) + .value_or(0), + 8, lldb::ePermissionsReadable | lldb::ePermissionsWritable, IRMemoryMap::eAllocationPolicyMirror, zero_memory, allocate_error); if (!allocate_error.Success()) { @@ -116,9 +117,11 @@ public: Status write_error; - map.WriteMemory(mem, m_persistent_variable_sp->GetValueBytes(), - m_persistent_variable_sp->GetByteSize().value_or(0), - write_error); + map.WriteMemory( + mem, m_persistent_variable_sp->GetValueBytes(), + llvm::expectedToOptional(m_persistent_variable_sp->GetByteSize()) + .value_or(0), + write_error); if (!write_error.Success()) { err = Status::FromErrorStringWithFormat( @@ -246,7 +249,8 @@ public: map.GetBestExecutionContextScope(), m_persistent_variable_sp.get()->GetCompilerType(), m_persistent_variable_sp->GetName(), location, eAddressTypeLoad, - m_persistent_variable_sp->GetByteSize().value_or(0)); + llvm::expectedToOptional(m_persistent_variable_sp->GetByteSize()) + .value_or(0)); if (frame_top != LLDB_INVALID_ADDRESS && frame_bottom != LLDB_INVALID_ADDRESS && location >= frame_bottom && @@ -291,7 +295,8 @@ public: LLDB_LOGF(log, "Dematerializing %s from 0x%" PRIx64 " (size = %llu)", m_persistent_variable_sp->GetName().GetCString(), (uint64_t)mem, - (unsigned long long)m_persistent_variable_sp->GetByteSize() + (unsigned long long)llvm::expectedToOptional( + m_persistent_variable_sp->GetByteSize()) .value_or(0)); // Read the contents of the spare memory area @@ -300,9 +305,11 @@ public: Status read_error; - map.ReadMemory(m_persistent_variable_sp->GetValueBytes(), mem, - m_persistent_variable_sp->GetByteSize().value_or(0), - read_error); + map.ReadMemory( + m_persistent_variable_sp->GetValueBytes(), mem, + llvm::expectedToOptional(m_persistent_variable_sp->GetByteSize()) + .value_or(0), + read_error); if (!read_error.Success()) { err = Status::FromErrorStringWithFormat( @@ -383,12 +390,16 @@ public: if (!err.Success()) { dump_stream.Printf(" <could not be read>\n"); } else { - DataBufferHeap data(m_persistent_variable_sp->GetByteSize().value_or(0), - 0); + DataBufferHeap data( + llvm::expectedToOptional(m_persistent_variable_sp->GetByteSize()) + .value_or(0), + 0); - map.ReadMemory(data.GetBytes(), target_address, - m_persistent_variable_sp->GetByteSize().value_or(0), - err); + map.ReadMemory( + data.GetBytes(), target_address, + llvm::expectedToOptional(m_persistent_variable_sp->GetByteSize()) + .value_or(0), + err); if (!err.Success()) { dump_stream.Printf(" <could not be read>\n"); @@ -529,7 +540,8 @@ public: return; } - if (data.GetByteSize() < GetByteSize(scope)) { + if (data.GetByteSize() < + llvm::expectedToOptional(GetByteSize(scope)).value_or(0)) { if (data.GetByteSize() == 0 && !LocationExpressionIsValid()) { err = Status::FromErrorStringWithFormat( "the variable '%s' has no location, " @@ -539,7 +551,8 @@ public: err = Status::FromErrorStringWithFormat( "size of variable %s (%" PRIu64 ") is larger than the ValueObject's size (%" PRIu64 ")", - GetName().AsCString(), GetByteSize(scope).value_or(0), + GetName().AsCString(), + llvm::expectedToOptional(GetByteSize(scope)).value_or(0), data.GetByteSize()); } return; @@ -632,8 +645,10 @@ public: Status extract_error; - map.GetMemoryData(data, m_temporary_allocation, - valobj_sp->GetByteSize().value_or(0), extract_error); + map.GetMemoryData( + data, m_temporary_allocation, + llvm::expectedToOptional(valobj_sp->GetByteSize()).value_or(0), + extract_error); if (!extract_error.Success()) { err = Status::FromErrorStringWithFormat( @@ -776,7 +791,7 @@ private: /// /// \returns On success, returns byte size of the type associated /// with this variable. Returns std::nullopt otherwise. - virtual std::optional<uint64_t> + virtual llvm::Expected<uint64_t> GetByteSize(ExecutionContextScope *scope) const = 0; /// Returns 'true' if the location expression associated with this variable @@ -817,7 +832,7 @@ public: return ValueObjectVariable::Create(scope, m_variable_sp); } - std::optional<uint64_t> + llvm::Expected<uint64_t> GetByteSize(ExecutionContextScope *scope) const override { return m_variable_sp->GetType()->GetByteSize(scope); } @@ -860,12 +875,12 @@ public: return m_valobj_sp; } - std::optional<uint64_t> + llvm::Expected<uint64_t> GetByteSize(ExecutionContextScope *scope) const override { if (m_valobj_sp) return m_valobj_sp->GetCompilerType().GetByteSize(scope); - return {}; + return llvm::createStringError("no value object"); } bool LocationExpressionIsValid() const override { @@ -937,12 +952,12 @@ public: if (!exe_scope) exe_scope = map.GetBestExecutionContextScope(); - std::optional<uint64_t> byte_size = m_type.GetByteSize(exe_scope); - if (!byte_size) { - err = Status::FromErrorStringWithFormat( - "can't get size of type \"%s\"", m_type.GetTypeName().AsCString()); + auto byte_size_or_err = m_type.GetByteSize(exe_scope); + if (!byte_size_or_err) { + err = Status::FromError(byte_size_or_err.takeError()); return; } + auto byte_size = *byte_size_or_err; std::optional<size_t> opt_bit_align = m_type.GetTypeBitAlign(exe_scope); if (!opt_bit_align) { @@ -958,10 +973,10 @@ public: const bool zero_memory = true; m_temporary_allocation = map.Malloc( - *byte_size, byte_align, + byte_size, byte_align, lldb::ePermissionsReadable | lldb::ePermissionsWritable, IRMemoryMap::eAllocationPolicyMirror, zero_memory, alloc_error); - m_temporary_allocation_size = *byte_size; + m_temporary_allocation_size = byte_size; if (!alloc_error.Success()) { err = Status::FromErrorStringWithFormat( @@ -1085,7 +1100,8 @@ public: ret->ValueUpdated(); - const size_t pvar_byte_size = ret->GetByteSize().value_or(0); + const size_t pvar_byte_size = + llvm::expectedToOptional(ret->GetByteSize()).value_or(0); uint8_t *pvar_data = ret->GetValueBytes(); map.ReadMemory(pvar_data, address, pvar_byte_size, read_error); diff --git a/lldb/source/Host/macosx/objcxx/Host.mm b/lldb/source/Host/macosx/objcxx/Host.mm index 5b3d04a..bb270f6 100644 --- a/lldb/source/Host/macosx/objcxx/Host.mm +++ b/lldb/source/Host/macosx/objcxx/Host.mm @@ -1471,7 +1471,7 @@ Status Host::ShellExpandArguments(ProcessLaunchInfo &launch_info) { char *wd = getcwd(nullptr, 0); if (wd == nullptr) { error = Status::FromErrorStringWithFormat( - "cwd does not exist; cannot launch with shell argument expansion"); + "cwd does not exist: Cannot launch with shell argument expansion"); return error; } else { FileSpec working_dir(wd); diff --git a/lldb/source/Plugins/ABI/AArch64/ABIMacOSX_arm64.cpp b/lldb/source/Plugins/ABI/AArch64/ABIMacOSX_arm64.cpp index d208c6f8..4b3018b 100644 --- a/lldb/source/Plugins/ABI/AArch64/ABIMacOSX_arm64.cpp +++ b/lldb/source/Plugins/ABI/AArch64/ABIMacOSX_arm64.cpp @@ -142,7 +142,8 @@ bool ABIMacOSX_arm64::GetArgumentValues(Thread &thread, return false; CompilerType value_type = value->GetCompilerType(); - std::optional<uint64_t> bit_size = value_type.GetBitSize(&thread); + std::optional<uint64_t> bit_size = + llvm::expectedToOptional(value_type.GetBitSize(&thread)); if (!bit_size) return false; @@ -482,8 +483,8 @@ static bool LoadValueFromConsecutiveGPRRegisters( uint32_t &NGRN, // NGRN (see ABI documentation) uint32_t &NSRN, // NSRN (see ABI documentation) DataExtractor &data) { - std::optional<uint64_t> byte_size = - value_type.GetByteSize(exe_ctx.GetBestExecutionContextScope()); + std::optional<uint64_t> byte_size = llvm::expectedToOptional( + value_type.GetByteSize(exe_ctx.GetBestExecutionContextScope())); if (!byte_size || *byte_size == 0) return false; @@ -500,8 +501,8 @@ static bool LoadValueFromConsecutiveGPRRegisters( if (NSRN < 8 && (8 - NSRN) >= homogeneous_count) { if (!base_type) return false; - std::optional<uint64_t> base_byte_size = - base_type.GetByteSize(exe_ctx.GetBestExecutionContextScope()); + std::optional<uint64_t> base_byte_size = llvm::expectedToOptional( + base_type.GetByteSize(exe_ctx.GetBestExecutionContextScope())); if (!base_byte_size) return false; uint32_t data_offset = 0; @@ -635,7 +636,8 @@ ValueObjectSP ABIMacOSX_arm64::GetReturnValueObjectImpl( if (!reg_ctx) return return_valobj_sp; - std::optional<uint64_t> byte_size = return_compiler_type.GetByteSize(&thread); + std::optional<uint64_t> byte_size = + llvm::expectedToOptional(return_compiler_type.GetByteSize(&thread)); if (!byte_size) return return_valobj_sp; diff --git a/lldb/source/Plugins/ABI/AArch64/ABISysV_arm64.cpp b/lldb/source/Plugins/ABI/AArch64/ABISysV_arm64.cpp index 25803c9..ca794cd 100644 --- a/lldb/source/Plugins/ABI/AArch64/ABISysV_arm64.cpp +++ b/lldb/source/Plugins/ABI/AArch64/ABISysV_arm64.cpp @@ -215,7 +215,8 @@ bool ABISysV_arm64::GetArgumentValues(Thread &thread, ValueList &values) const { if (value_type) { bool is_signed = false; size_t bit_width = 0; - std::optional<uint64_t> bit_size = value_type.GetBitSize(&thread); + std::optional<uint64_t> bit_size = + llvm::expectedToOptional(value_type.GetBitSize(&thread)); if (!bit_size) return false; if (value_type.IsIntegerOrEnumerationType(is_signed)) { @@ -524,8 +525,8 @@ static bool LoadValueFromConsecutiveGPRRegisters( uint32_t &NGRN, // NGRN (see ABI documentation) uint32_t &NSRN, // NSRN (see ABI documentation) DataExtractor &data) { - std::optional<uint64_t> byte_size = - value_type.GetByteSize(exe_ctx.GetBestExecutionContextScope()); + std::optional<uint64_t> byte_size = llvm::expectedToOptional( + value_type.GetByteSize(exe_ctx.GetBestExecutionContextScope())); if (byte_size || *byte_size == 0) return false; @@ -543,8 +544,8 @@ static bool LoadValueFromConsecutiveGPRRegisters( if (NSRN < 8 && (8 - NSRN) >= homogeneous_count) { if (!base_type) return false; - std::optional<uint64_t> base_byte_size = - base_type.GetByteSize(exe_ctx.GetBestExecutionContextScope()); + std::optional<uint64_t> base_byte_size = llvm::expectedToOptional( + base_type.GetByteSize(exe_ctx.GetBestExecutionContextScope())); if (!base_byte_size) return false; uint32_t data_offset = 0; @@ -673,7 +674,8 @@ ValueObjectSP ABISysV_arm64::GetReturnValueObjectImpl( if (!reg_ctx) return return_valobj_sp; - std::optional<uint64_t> byte_size = return_compiler_type.GetByteSize(&thread); + std::optional<uint64_t> byte_size = + llvm::expectedToOptional(return_compiler_type.GetByteSize(&thread)); if (!byte_size) return return_valobj_sp; diff --git a/lldb/source/Plugins/ABI/ARM/ABIMacOSX_arm.cpp b/lldb/source/Plugins/ABI/ARM/ABIMacOSX_arm.cpp index d85f4a5..27d0474 100644 --- a/lldb/source/Plugins/ABI/ARM/ABIMacOSX_arm.cpp +++ b/lldb/source/Plugins/ABI/ARM/ABIMacOSX_arm.cpp @@ -1447,7 +1447,8 @@ bool ABIMacOSX_arm::GetArgumentValues(Thread &thread, ValueList &values) const { if (compiler_type) { bool is_signed = false; size_t bit_width = 0; - std::optional<uint64_t> bit_size = compiler_type.GetBitSize(&thread); + std::optional<uint64_t> bit_size = + llvm::expectedToOptional(compiler_type.GetBitSize(&thread)); if (!bit_size) return false; if (compiler_type.IsIntegerOrEnumerationType(is_signed)) @@ -1553,7 +1554,8 @@ ValueObjectSP ABIMacOSX_arm::GetReturnValueObjectImpl( const RegisterInfo *r0_reg_info = reg_ctx->GetRegisterInfoByName("r0", 0); if (compiler_type.IsIntegerOrEnumerationType(is_signed)) { - std::optional<uint64_t> bit_width = compiler_type.GetBitSize(&thread); + std::optional<uint64_t> bit_width = + llvm::expectedToOptional(compiler_type.GetBitSize(&thread)); if (!bit_width) return return_valobj_sp; @@ -1574,7 +1576,7 @@ ValueObjectSP ABIMacOSX_arm::GetReturnValueObjectImpl( reg_ctx->GetRegisterInfoByName("r3", 0); if (r1_reg_info && r2_reg_info && r3_reg_info) { std::optional<uint64_t> byte_size = - compiler_type.GetByteSize(&thread); + llvm::expectedToOptional(compiler_type.GetByteSize(&thread)); if (!byte_size) return return_valobj_sp; ProcessSP process_sp(thread.GetProcess()); diff --git a/lldb/source/Plugins/ABI/ARM/ABISysV_arm.cpp b/lldb/source/Plugins/ABI/ARM/ABISysV_arm.cpp index 38f4413..cf051b4 100644 --- a/lldb/source/Plugins/ABI/ARM/ABISysV_arm.cpp +++ b/lldb/source/Plugins/ABI/ARM/ABISysV_arm.cpp @@ -1452,7 +1452,8 @@ bool ABISysV_arm::GetArgumentValues(Thread &thread, ValueList &values) const { size_t bit_width = 0; if (compiler_type.IsIntegerOrEnumerationType(is_signed) || compiler_type.IsPointerOrReferenceType()) { - if (std::optional<uint64_t> size = compiler_type.GetBitSize(&thread)) + if (std::optional<uint64_t> size = + llvm::expectedToOptional(compiler_type.GetBitSize(&thread))) bit_width = *size; } else { // We only handle integer, pointer and reference types currently... @@ -1559,8 +1560,10 @@ ValueObjectSP ABISysV_arm::GetReturnValueObjectImpl( const RegisterInfo *r0_reg_info = reg_ctx->GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG1); - std::optional<uint64_t> bit_width = compiler_type.GetBitSize(&thread); - std::optional<uint64_t> byte_size = compiler_type.GetByteSize(&thread); + std::optional<uint64_t> bit_width = + llvm::expectedToOptional(compiler_type.GetBitSize(&thread)); + std::optional<uint64_t> byte_size = + llvm::expectedToOptional(compiler_type.GetByteSize(&thread)); if (!bit_width || !byte_size) return return_valobj_sp; @@ -1696,7 +1699,8 @@ ValueObjectSP ABISysV_arm::GetReturnValueObjectImpl( compiler_type.IsHomogeneousAggregate(&base_type); if (homogeneous_count > 0 && homogeneous_count <= 4) { - std::optional<uint64_t> base_byte_size = base_type.GetByteSize(&thread); + std::optional<uint64_t> base_byte_size = + llvm::expectedToOptional(base_type.GetByteSize(&thread)); if (base_type.IsVectorType()) { if (base_byte_size && (*base_byte_size == 8 || *base_byte_size == 16)) { @@ -1725,7 +1729,7 @@ ValueObjectSP ABISysV_arm::GetReturnValueObjectImpl( if (base_type.IsFloatingPointType(float_count, is_complex)) { std::optional<uint64_t> base_byte_size = - base_type.GetByteSize(&thread); + llvm::expectedToOptional(base_type.GetByteSize(&thread)); if (float_count == 2 && is_complex) { if (index != 0 && base_byte_size && vfp_byte_size != *base_byte_size) diff --git a/lldb/source/Plugins/ABI/LoongArch/ABISysV_loongarch.cpp b/lldb/source/Plugins/ABI/LoongArch/ABISysV_loongarch.cpp index 1dd6070..14ef8a0 100644 --- a/lldb/source/Plugins/ABI/LoongArch/ABISysV_loongarch.cpp +++ b/lldb/source/Plugins/ABI/LoongArch/ABISysV_loongarch.cpp @@ -491,7 +491,8 @@ ValueObjectSP ABISysV_loongarch::GetReturnValueObjectSimple( value.SetCompilerType(compiler_type); const uint32_t type_flags = compiler_type.GetTypeInfo(); - const size_t byte_size = compiler_type.GetByteSize(&thread).value_or(0); + const size_t byte_size = + llvm::expectedToOptional(compiler_type.GetByteSize(&thread)).value_or(0); const ArchSpec arch = thread.GetProcess()->GetTarget().GetArchitecture(); const llvm::Triple::ArchType machine = arch.GetMachine(); diff --git a/lldb/source/Plugins/ABI/Mips/ABISysV_mips.cpp b/lldb/source/Plugins/ABI/Mips/ABISysV_mips.cpp index d7ebe22c..a2b60a0 100644 --- a/lldb/source/Plugins/ABI/Mips/ABISysV_mips.cpp +++ b/lldb/source/Plugins/ABI/Mips/ABISysV_mips.cpp @@ -801,7 +801,8 @@ ValueObjectSP ABISysV_mips::GetReturnValueObjectImpl( // In MIPS register "r2" (v0) holds the integer function return values const RegisterInfo *r2_reg_info = reg_ctx->GetRegisterInfoByName("r2", 0); - std::optional<uint64_t> bit_width = return_compiler_type.GetBitSize(&thread); + std::optional<uint64_t> bit_width = + llvm::expectedToOptional(return_compiler_type.GetBitSize(&thread)); if (!bit_width) return return_valobj_sp; if (return_compiler_type.IsIntegerOrEnumerationType(is_signed)) { diff --git a/lldb/source/Plugins/ABI/Mips/ABISysV_mips64.cpp b/lldb/source/Plugins/ABI/Mips/ABISysV_mips64.cpp index dcaef20..763d614 100644 --- a/lldb/source/Plugins/ABI/Mips/ABISysV_mips64.cpp +++ b/lldb/source/Plugins/ABI/Mips/ABISysV_mips64.cpp @@ -750,7 +750,8 @@ ValueObjectSP ABISysV_mips64::GetReturnValueObjectImpl( Target *target = exe_ctx.GetTargetPtr(); const ArchSpec target_arch = target->GetArchitecture(); ByteOrder target_byte_order = target_arch.GetByteOrder(); - std::optional<uint64_t> byte_size = return_compiler_type.GetByteSize(&thread); + std::optional<uint64_t> byte_size = + llvm::expectedToOptional(return_compiler_type.GetByteSize(&thread)); if (!byte_size) return return_valobj_sp; const uint32_t type_flags = return_compiler_type.GetTypeInfo(nullptr); @@ -959,8 +960,8 @@ ValueObjectSP ABISysV_mips64::GetReturnValueObjectImpl( CompilerType field_compiler_type = return_compiler_type.GetFieldAtIndex( idx, name, &field_bit_offset, nullptr, nullptr); - std::optional<uint64_t> field_byte_width = - field_compiler_type.GetByteSize(&thread); + std::optional<uint64_t> field_byte_width = llvm::expectedToOptional( + field_compiler_type.GetByteSize(&thread)); if (!field_byte_width) return return_valobj_sp; @@ -1032,7 +1033,7 @@ ValueObjectSP ABISysV_mips64::GetReturnValueObjectImpl( CompilerType field_compiler_type = return_compiler_type.GetFieldAtIndex( idx, name, &field_bit_offset, nullptr, nullptr); std::optional<uint64_t> field_byte_width = - field_compiler_type.GetByteSize(&thread); + llvm::expectedToOptional(field_compiler_type.GetByteSize(&thread)); // if we don't know the size of the field (e.g. invalid type), just // bail out diff --git a/lldb/source/Plugins/ABI/PowerPC/ABISysV_ppc.cpp b/lldb/source/Plugins/ABI/PowerPC/ABISysV_ppc.cpp index 405ba57..93e1fa48 100644 --- a/lldb/source/Plugins/ABI/PowerPC/ABISysV_ppc.cpp +++ b/lldb/source/Plugins/ABI/PowerPC/ABISysV_ppc.cpp @@ -395,7 +395,8 @@ bool ABISysV_ppc::GetArgumentValues(Thread &thread, ValueList &values) const { // We currently only support extracting values with Clang QualTypes. Do we // care about others? CompilerType compiler_type = value->GetCompilerType(); - std::optional<uint64_t> bit_size = compiler_type.GetBitSize(&thread); + std::optional<uint64_t> bit_size = + llvm::expectedToOptional(compiler_type.GetBitSize(&thread)); if (!bit_size) return false; bool is_signed; @@ -459,7 +460,7 @@ Status ABISysV_ppc::SetReturnValueObject(lldb::StackFrameSP &frame_sp, "We don't support returning complex values at present"); else { std::optional<uint64_t> bit_width = - compiler_type.GetBitSize(frame_sp.get()); + llvm::expectedToOptional(compiler_type.GetBitSize(frame_sp.get())); if (!bit_width) { error = Status::FromErrorString("can't get type size"); return error; @@ -524,7 +525,7 @@ ValueObjectSP ABISysV_ppc::GetReturnValueObjectSimple( // Extract the register context so we can read arguments from registers std::optional<uint64_t> byte_size = - return_compiler_type.GetByteSize(&thread); + llvm::expectedToOptional(return_compiler_type.GetByteSize(&thread)); if (!byte_size) return return_valobj_sp; uint64_t raw_value = thread.GetRegisterContext()->ReadRegisterAsUnsigned( @@ -571,7 +572,7 @@ ValueObjectSP ABISysV_ppc::GetReturnValueObjectSimple( // Don't handle complex yet. } else { std::optional<uint64_t> byte_size = - return_compiler_type.GetByteSize(&thread); + llvm::expectedToOptional(return_compiler_type.GetByteSize(&thread)); if (byte_size && *byte_size <= sizeof(long double)) { const RegisterInfo *f1_info = reg_ctx->GetRegisterInfoByName("f1", 0); RegisterValue f1_value; @@ -605,7 +606,7 @@ ValueObjectSP ABISysV_ppc::GetReturnValueObjectSimple( thread.GetStackFrameAtIndex(0).get(), value, ConstString("")); } else if (type_flags & eTypeIsVector) { std::optional<uint64_t> byte_size = - return_compiler_type.GetByteSize(&thread); + llvm::expectedToOptional(return_compiler_type.GetByteSize(&thread)); if (byte_size && *byte_size > 0) { const RegisterInfo *altivec_reg = reg_ctx->GetRegisterInfoByName("v2", 0); if (altivec_reg) { @@ -655,7 +656,8 @@ ValueObjectSP ABISysV_ppc::GetReturnValueObjectImpl( if (!reg_ctx_sp) return return_valobj_sp; - std::optional<uint64_t> bit_width = return_compiler_type.GetBitSize(&thread); + std::optional<uint64_t> bit_width = + llvm::expectedToOptional(return_compiler_type.GetBitSize(&thread)); if (!bit_width) return return_valobj_sp; if (return_compiler_type.IsAggregateType()) { @@ -698,7 +700,7 @@ ValueObjectSP ABISysV_ppc::GetReturnValueObjectImpl( CompilerType field_compiler_type = return_compiler_type.GetFieldAtIndex( idx, name, &field_bit_offset, nullptr, nullptr); std::optional<uint64_t> field_bit_width = - field_compiler_type.GetBitSize(&thread); + llvm::expectedToOptional(field_compiler_type.GetBitSize(&thread)); if (!field_bit_width) return return_valobj_sp; diff --git a/lldb/source/Plugins/ABI/PowerPC/ABISysV_ppc64.cpp b/lldb/source/Plugins/ABI/PowerPC/ABISysV_ppc64.cpp index 0392dab..8e9b56d 100644 --- a/lldb/source/Plugins/ABI/PowerPC/ABISysV_ppc64.cpp +++ b/lldb/source/Plugins/ABI/PowerPC/ABISysV_ppc64.cpp @@ -272,7 +272,8 @@ bool ABISysV_ppc64::GetArgumentValues(Thread &thread, ValueList &values) const { // We currently only support extracting values with Clang QualTypes. Do we // care about others? CompilerType compiler_type = value->GetCompilerType(); - std::optional<uint64_t> bit_size = compiler_type.GetBitSize(&thread); + std::optional<uint64_t> bit_size = + llvm::expectedToOptional(compiler_type.GetBitSize(&thread)); if (!bit_size) return false; bool is_signed; @@ -344,7 +345,7 @@ Status ABISysV_ppc64::SetReturnValueObject(lldb::StackFrameSP &frame_sp, "We don't support returning complex values at present"); else { std::optional<uint64_t> bit_width = - compiler_type.GetBitSize(frame_sp.get()); + llvm::expectedToOptional(compiler_type.GetBitSize(frame_sp.get())); if (!bit_width) { error = Status::FromErrorString("can't get size of type"); return error; @@ -568,7 +569,8 @@ private: ReturnValueExtractor(Thread &thread, CompilerType &type, RegisterContext *reg_ctx, ProcessSP process_sp) : m_thread(thread), m_type(type), - m_byte_size(m_type.GetByteSize(&thread).value_or(0)), + m_byte_size( + llvm::expectedToOptional(m_type.GetByteSize(&thread)).value_or(0)), m_data_up(new DataBufferHeap(m_byte_size, 0)), m_reg_ctx(reg_ctx), m_process_sp(process_sp), m_byte_order(process_sp->GetByteOrder()), m_addr_size( @@ -644,7 +646,8 @@ private: DataExtractor de(&raw_data, sizeof(raw_data), m_byte_order, m_addr_size); lldb::offset_t offset = 0; - std::optional<uint64_t> byte_size = type.GetByteSize(m_process_sp.get()); + std::optional<uint64_t> byte_size = + llvm::expectedToOptional(type.GetByteSize(m_process_sp.get())); if (!byte_size) return {}; switch (*byte_size) { @@ -784,7 +787,7 @@ private: if (m_type.IsHomogeneousAggregate(&elem_type)) { uint32_t type_flags = elem_type.GetTypeInfo(); std::optional<uint64_t> elem_size = - elem_type.GetByteSize(m_process_sp.get()); + llvm::expectedToOptional(elem_type.GetByteSize(m_process_sp.get())); if (!elem_size) return {}; if (type_flags & eTypeIsComplex || !(type_flags & eTypeIsFloat)) { diff --git a/lldb/source/Plugins/ABI/RISCV/ABISysV_riscv.cpp b/lldb/source/Plugins/ABI/RISCV/ABISysV_riscv.cpp index 06415a6..99263ce 100644 --- a/lldb/source/Plugins/ABI/RISCV/ABISysV_riscv.cpp +++ b/lldb/source/Plugins/ABI/RISCV/ABISysV_riscv.cpp @@ -621,7 +621,8 @@ ABISysV_riscv::GetReturnValueObjectSimple(Thread &thread, value.SetCompilerType(compiler_type); const uint32_t type_flags = compiler_type.GetTypeInfo(); - const size_t byte_size = compiler_type.GetByteSize(&thread).value_or(0); + const size_t byte_size = + llvm::expectedToOptional(compiler_type.GetByteSize(&thread)).value_or(0); const ArchSpec arch = thread.GetProcess()->GetTarget().GetArchitecture(); const llvm::Triple::ArchType machine = arch.GetMachine(); diff --git a/lldb/source/Plugins/ABI/SystemZ/ABISysV_s390x.cpp b/lldb/source/Plugins/ABI/SystemZ/ABISysV_s390x.cpp index a88a3b0..89a8381 100644 --- a/lldb/source/Plugins/ABI/SystemZ/ABISysV_s390x.cpp +++ b/lldb/source/Plugins/ABI/SystemZ/ABISysV_s390x.cpp @@ -356,7 +356,8 @@ bool ABISysV_s390x::GetArgumentValues(Thread &thread, ValueList &values) const { // We currently only support extracting values with Clang QualTypes. Do we // care about others? CompilerType compiler_type = value->GetCompilerType(); - std::optional<uint64_t> bit_size = compiler_type.GetBitSize(&thread); + std::optional<uint64_t> bit_size = + llvm::expectedToOptional(compiler_type.GetBitSize(&thread)); if (!bit_size) return false; bool is_signed; @@ -428,7 +429,7 @@ Status ABISysV_s390x::SetReturnValueObject(lldb::StackFrameSP &frame_sp, "We don't support returning complex values at present"); else { std::optional<uint64_t> bit_width = - compiler_type.GetBitSize(frame_sp.get()); + llvm::expectedToOptional(compiler_type.GetBitSize(frame_sp.get())); if (!bit_width) { error = Status::FromErrorString("can't get type size"); return error; @@ -496,7 +497,7 @@ ValueObjectSP ABISysV_s390x::GetReturnValueObjectSimple( if (type_flags & eTypeIsInteger) { // Extract the register context so we can read arguments from registers. std::optional<uint64_t> byte_size = - return_compiler_type.GetByteSize(&thread); + llvm::expectedToOptional(return_compiler_type.GetByteSize(&thread)); if (!byte_size) return return_valobj_sp; uint64_t raw_value = thread.GetRegisterContext()->ReadRegisterAsUnsigned( @@ -543,7 +544,7 @@ ValueObjectSP ABISysV_s390x::GetReturnValueObjectSimple( // Don't handle complex yet. } else { std::optional<uint64_t> byte_size = - return_compiler_type.GetByteSize(&thread); + llvm::expectedToOptional(return_compiler_type.GetByteSize(&thread)); if (byte_size && *byte_size <= sizeof(long double)) { const RegisterInfo *f0_info = reg_ctx->GetRegisterInfoByName("f0", 0); RegisterValue f0_value; diff --git a/lldb/source/Plugins/ABI/X86/ABIMacOSX_i386.cpp b/lldb/source/Plugins/ABI/X86/ABIMacOSX_i386.cpp index d484b78..5ede8d2 100644 --- a/lldb/source/Plugins/ABI/X86/ABIMacOSX_i386.cpp +++ b/lldb/source/Plugins/ABI/X86/ABIMacOSX_i386.cpp @@ -165,7 +165,8 @@ bool ABIMacOSX_i386::GetArgumentValues(Thread &thread, // We currently only support extracting values with Clang QualTypes. Do we // care about others? CompilerType compiler_type(value->GetCompilerType()); - std::optional<uint64_t> bit_size = compiler_type.GetBitSize(&thread); + std::optional<uint64_t> bit_size = + llvm::expectedToOptional(compiler_type.GetBitSize(&thread)); if (bit_size) { bool is_signed; if (compiler_type.IsIntegerOrEnumerationType(is_signed)) @@ -275,7 +276,8 @@ ABIMacOSX_i386::GetReturnValueObjectImpl(Thread &thread, bool is_signed; if (compiler_type.IsIntegerOrEnumerationType(is_signed)) { - std::optional<uint64_t> bit_width = compiler_type.GetBitSize(&thread); + std::optional<uint64_t> bit_width = + llvm::expectedToOptional(compiler_type.GetBitSize(&thread)); if (!bit_width) return return_valobj_sp; unsigned eax_id = diff --git a/lldb/source/Plugins/ABI/X86/ABISysV_i386.cpp b/lldb/source/Plugins/ABI/X86/ABISysV_i386.cpp index f6b3666..bf35470 100644 --- a/lldb/source/Plugins/ABI/X86/ABISysV_i386.cpp +++ b/lldb/source/Plugins/ABI/X86/ABISysV_i386.cpp @@ -182,7 +182,8 @@ bool ABISysV_i386::GetArgumentValues(Thread &thread, ValueList &values) const { // Currently: Support for extracting values with Clang QualTypes only. CompilerType compiler_type(value->GetCompilerType()); - std::optional<uint64_t> bit_size = compiler_type.GetBitSize(&thread); + std::optional<uint64_t> bit_size = + llvm::expectedToOptional(compiler_type.GetBitSize(&thread)); if (bit_size) { bool is_signed; if (compiler_type.IsIntegerOrEnumerationType(is_signed)) { @@ -392,7 +393,7 @@ ValueObjectSP ABISysV_i386::GetReturnValueObjectSimple( { value.SetValueType(Value::ValueType::Scalar); std::optional<uint64_t> byte_size = - return_compiler_type.GetByteSize(&thread); + llvm::expectedToOptional(return_compiler_type.GetByteSize(&thread)); if (!byte_size) return return_valobj_sp; bool success = false; @@ -517,7 +518,7 @@ ValueObjectSP ABISysV_i386::GetReturnValueObjectSimple( } else if (type_flags & eTypeIsVector) // 'Packed' { std::optional<uint64_t> byte_size = - return_compiler_type.GetByteSize(&thread); + llvm::expectedToOptional(return_compiler_type.GetByteSize(&thread)); if (byte_size && *byte_size > 0) { const RegisterInfo *vec_reg = reg_ctx->GetRegisterInfoByName("xmm0", 0); if (vec_reg == nullptr) diff --git a/lldb/source/Plugins/ABI/X86/ABISysV_x86_64.cpp b/lldb/source/Plugins/ABI/X86/ABISysV_x86_64.cpp index a224b3b..c6dba91 100644 --- a/lldb/source/Plugins/ABI/X86/ABISysV_x86_64.cpp +++ b/lldb/source/Plugins/ABI/X86/ABISysV_x86_64.cpp @@ -270,7 +270,8 @@ bool ABISysV_x86_64::GetArgumentValues(Thread &thread, // We currently only support extracting values with Clang QualTypes. Do we // care about others? CompilerType compiler_type = value->GetCompilerType(); - std::optional<uint64_t> bit_size = compiler_type.GetBitSize(&thread); + std::optional<uint64_t> bit_size = + llvm::expectedToOptional(compiler_type.GetBitSize(&thread)); if (!bit_size) return false; bool is_signed; @@ -342,7 +343,7 @@ Status ABISysV_x86_64::SetReturnValueObject(lldb::StackFrameSP &frame_sp, "We don't support returning complex values at present"); else { std::optional<uint64_t> bit_width = - compiler_type.GetBitSize(frame_sp.get()); + llvm::expectedToOptional(compiler_type.GetBitSize(frame_sp.get())); if (!bit_width) { error = Status::FromErrorString("can't get type size"); return error; @@ -412,7 +413,7 @@ ValueObjectSP ABISysV_x86_64::GetReturnValueObjectSimple( // Extract the register context so we can read arguments from registers std::optional<uint64_t> byte_size = - return_compiler_type.GetByteSize(&thread); + llvm::expectedToOptional(return_compiler_type.GetByteSize(&thread)); if (!byte_size) return return_valobj_sp; uint64_t raw_value = thread.GetRegisterContext()->ReadRegisterAsUnsigned( @@ -459,7 +460,7 @@ ValueObjectSP ABISysV_x86_64::GetReturnValueObjectSimple( // Don't handle complex yet. } else { std::optional<uint64_t> byte_size = - return_compiler_type.GetByteSize(&thread); + llvm::expectedToOptional(return_compiler_type.GetByteSize(&thread)); if (byte_size && *byte_size <= sizeof(long double)) { const RegisterInfo *xmm0_info = reg_ctx->GetRegisterInfoByName("xmm0", 0); @@ -498,7 +499,7 @@ ValueObjectSP ABISysV_x86_64::GetReturnValueObjectSimple( thread.GetStackFrameAtIndex(0).get(), value, ConstString("")); } else if (type_flags & eTypeIsVector) { std::optional<uint64_t> byte_size = - return_compiler_type.GetByteSize(&thread); + llvm::expectedToOptional(return_compiler_type.GetByteSize(&thread)); if (byte_size && *byte_size > 0) { const RegisterInfo *altivec_reg = reg_ctx->GetRegisterInfoByName("xmm0", 0); @@ -593,7 +594,7 @@ static bool FlattenAggregateType( CompilerType field_compiler_type = return_compiler_type.GetFieldAtIndex( idx, name, &field_bit_offset, nullptr, nullptr); std::optional<uint64_t> field_bit_width = - field_compiler_type.GetBitSize(&thread); + llvm::expectedToOptional(field_compiler_type.GetBitSize(&thread)); // if we don't know the size of the field (e.g. invalid type), exit if (!field_bit_width || *field_bit_width == 0) { @@ -635,7 +636,8 @@ ValueObjectSP ABISysV_x86_64::GetReturnValueObjectImpl( if (!reg_ctx_sp) return return_valobj_sp; - std::optional<uint64_t> bit_width = return_compiler_type.GetBitSize(&thread); + std::optional<uint64_t> bit_width = + llvm::expectedToOptional(return_compiler_type.GetBitSize(&thread)); if (!bit_width) return return_valobj_sp; if (return_compiler_type.IsAggregateType()) { @@ -698,7 +700,10 @@ ValueObjectSP ABISysV_x86_64::GetReturnValueObjectImpl( bool is_complex; CompilerType field_compiler_type = aggregate_compiler_types[idx]; - uint32_t field_byte_width = (uint32_t) (*field_compiler_type.GetByteSize(&thread)); + uint32_t field_byte_width = + (uint32_t)(llvm::expectedToOptional( + field_compiler_type.GetByteSize(&thread)) + .value_or(0)); uint32_t field_byte_offset = aggregate_field_offsets[idx]; uint32_t field_bit_width = field_byte_width * 8; diff --git a/lldb/source/Plugins/ABI/X86/ABIWindows_x86_64.cpp b/lldb/source/Plugins/ABI/X86/ABIWindows_x86_64.cpp index 8c6cea6..04f0bf1 100644 --- a/lldb/source/Plugins/ABI/X86/ABIWindows_x86_64.cpp +++ b/lldb/source/Plugins/ABI/X86/ABIWindows_x86_64.cpp @@ -275,7 +275,8 @@ bool ABIWindows_x86_64::GetArgumentValues(Thread &thread, return false; CompilerType compiler_type = value->GetCompilerType(); - std::optional<uint64_t> bit_size = compiler_type.GetBitSize(&thread); + std::optional<uint64_t> bit_size = + llvm::expectedToOptional(compiler_type.GetBitSize(&thread)); if (!bit_size) return false; bool is_signed; @@ -347,7 +348,7 @@ Status ABIWindows_x86_64::SetReturnValueObject(lldb::StackFrameSP &frame_sp, "We don't support returning complex values at present"); else { std::optional<uint64_t> bit_width = - compiler_type.GetBitSize(frame_sp.get()); + llvm::expectedToOptional(compiler_type.GetBitSize(frame_sp.get())); if (!bit_width) { error = Status::FromErrorString("can't get type size"); return error; @@ -418,7 +419,7 @@ ValueObjectSP ABIWindows_x86_64::GetReturnValueObjectSimple( if (type_flags & eTypeIsInteger) { // Extract the register context so we can read arguments from registers std::optional<uint64_t> byte_size = - return_compiler_type.GetByteSize(&thread); + llvm::expectedToOptional(return_compiler_type.GetByteSize(&thread)); if (!byte_size) return return_valobj_sp; uint64_t raw_value = thread.GetRegisterContext()->ReadRegisterAsUnsigned( @@ -465,7 +466,7 @@ ValueObjectSP ABIWindows_x86_64::GetReturnValueObjectSimple( // Don't handle complex yet. } else { std::optional<uint64_t> byte_size = - return_compiler_type.GetByteSize(&thread); + llvm::expectedToOptional(return_compiler_type.GetByteSize(&thread)); if (byte_size && *byte_size <= sizeof(long double)) { const RegisterInfo *xmm0_info = reg_ctx->GetRegisterInfoByName("xmm0", 0); @@ -503,7 +504,7 @@ ValueObjectSP ABIWindows_x86_64::GetReturnValueObjectSimple( thread.GetStackFrameAtIndex(0).get(), value, ConstString("")); } else if (type_flags & eTypeIsVector) { std::optional<uint64_t> byte_size = - return_compiler_type.GetByteSize(&thread); + llvm::expectedToOptional(return_compiler_type.GetByteSize(&thread)); if (byte_size && *byte_size > 0) { const RegisterInfo *xmm_reg = reg_ctx->GetRegisterInfoByName("xmm0", 0); @@ -564,7 +565,7 @@ static bool FlattenAggregateType( CompilerType field_compiler_type = return_compiler_type.GetFieldAtIndex( idx, name, &field_bit_offset, nullptr, nullptr); std::optional<uint64_t> field_bit_width = - field_compiler_type.GetBitSize(&thread); + llvm::expectedToOptional(field_compiler_type.GetBitSize(&thread)); // if we don't know the size of the field (e.g. invalid type), exit if (!field_bit_width || *field_bit_width == 0) { @@ -614,7 +615,8 @@ ValueObjectSP ABIWindows_x86_64::GetReturnValueObjectImpl( return return_valobj_sp; } - std::optional<uint64_t> bit_width = return_compiler_type.GetBitSize(&thread); + std::optional<uint64_t> bit_width = + llvm::expectedToOptional(return_compiler_type.GetBitSize(&thread)); if (!bit_width) { return return_valobj_sp; } diff --git a/lldb/source/Plugins/ExpressionParser/Clang/IRForTarget.cpp b/lldb/source/Plugins/ExpressionParser/Clang/IRForTarget.cpp index a414ad6..879f006 100644 --- a/lldb/source/Plugins/ExpressionParser/Clang/IRForTarget.cpp +++ b/lldb/source/Plugins/ExpressionParser/Clang/IRForTarget.cpp @@ -298,16 +298,17 @@ bool IRForTarget::CreateResultVariable(llvm::Function &llvm_function) { } lldb::TargetSP target_sp(m_execution_unit.GetTarget()); - std::optional<uint64_t> bit_size = m_result_type.GetBitSize(target_sp.get()); - if (!bit_size) { + auto bit_size_or_err = m_result_type.GetBitSize(target_sp.get()); + if (!bit_size_or_err) { lldb_private::StreamString type_desc_stream; m_result_type.DumpTypeDescription(&type_desc_stream); LLDB_LOG(log, "Result type has unknown size"); m_error_stream.Printf("Error [IRForTarget]: Size of result type '%s' " - "couldn't be determined\n", - type_desc_stream.GetData()); + "couldn't be determined\n%s", + type_desc_stream.GetData(), + llvm::toString(bit_size_or_err.takeError()).c_str()); return false; } @@ -322,7 +323,8 @@ bool IRForTarget::CreateResultVariable(llvm::Function &llvm_function) { LLDB_LOG(log, "Creating a new result global: \"{0}\" with size {1}", m_result_name, - m_result_type.GetByteSize(target_sp.get()).value_or(0)); + llvm::expectedToOptional(m_result_type.GetByteSize(target_sp.get())) + .value_or(0)); // Construct a new result global and set up its metadata @@ -1035,7 +1037,8 @@ bool IRForTarget::MaybeHandleVariable(Value *llvm_value_ptr) { } auto *target = m_execution_unit.GetTarget().get(); - std::optional<uint64_t> value_size = compiler_type.GetByteSize(target); + std::optional<uint64_t> value_size = + llvm::expectedToOptional(compiler_type.GetByteSize(target)); if (!value_size) return false; std::optional<size_t> opt_alignment = compiler_type.GetTypeBitAlign(target); diff --git a/lldb/source/Plugins/Language/CPlusPlus/CxxStringTypes.cpp b/lldb/source/Plugins/Language/CPlusPlus/CxxStringTypes.cpp index 6d810cd..fc17b76 100644 --- a/lldb/source/Plugins/Language/CPlusPlus/CxxStringTypes.cpp +++ b/lldb/source/Plugins/Language/CPlusPlus/CxxStringTypes.cpp @@ -123,7 +123,8 @@ bool lldb_private::formatters::WCharStringSummaryProvider( return false; // Safe to pass nullptr for exe_scope here. - std::optional<uint64_t> size = wchar_compiler_type.GetBitSize(nullptr); + std::optional<uint64_t> size = + llvm::expectedToOptional(wchar_compiler_type.GetBitSize(nullptr)); if (!size) return false; const uint32_t wchar_size = *size; @@ -183,7 +184,8 @@ bool lldb_private::formatters::WCharSummaryProvider( return false; // Safe to pass nullptr for exe_scope here. - std::optional<uint64_t> size = wchar_compiler_type.GetBitSize(nullptr); + std::optional<uint64_t> size = + llvm::expectedToOptional(wchar_compiler_type.GetBitSize(nullptr)); if (!size) return false; const uint32_t wchar_size = *size; diff --git a/lldb/source/Plugins/Language/CPlusPlus/GenericBitset.cpp b/lldb/source/Plugins/Language/CPlusPlus/GenericBitset.cpp index 934b456..03671c3 100644 --- a/lldb/source/Plugins/Language/CPlusPlus/GenericBitset.cpp +++ b/lldb/source/Plugins/Language/CPlusPlus/GenericBitset.cpp @@ -110,8 +110,8 @@ ValueObjectSP GenericBitsetFrontEnd::GetChildAtIndex(uint32_t idx) { ValueObjectSP chunk; // For small bitsets __first_ is not an array, but a plain size_t. if (m_first->GetCompilerType().IsArrayType(&type)) { - std::optional<uint64_t> bit_size = - type.GetBitSize(ctx.GetBestExecutionContextScope()); + std::optional<uint64_t> bit_size = llvm::expectedToOptional( + type.GetBitSize(ctx.GetBestExecutionContextScope())); if (!bit_size || *bit_size == 0) return {}; chunk = m_first->GetChildAtIndex(idx / *bit_size); @@ -122,8 +122,8 @@ ValueObjectSP GenericBitsetFrontEnd::GetChildAtIndex(uint32_t idx) { if (!type || !chunk) return {}; - std::optional<uint64_t> bit_size = - type.GetBitSize(ctx.GetBestExecutionContextScope()); + std::optional<uint64_t> bit_size = llvm::expectedToOptional( + type.GetBitSize(ctx.GetBestExecutionContextScope())); if (!bit_size || *bit_size == 0) return {}; size_t chunk_idx = idx % *bit_size; diff --git a/lldb/source/Plugins/Language/CPlusPlus/LibCxx.cpp b/lldb/source/Plugins/Language/CPlusPlus/LibCxx.cpp index 98e787d..63620c6 100644 --- a/lldb/source/Plugins/Language/CPlusPlus/LibCxx.cpp +++ b/lldb/source/Plugins/Language/CPlusPlus/LibCxx.cpp @@ -501,8 +501,8 @@ ExtractLibcxxStringInfo(ValueObject &valobj) { // likely that the string isn't initialized and we're reading garbage. ExecutionContext exe_ctx(location_sp->GetExecutionContextRef()); const std::optional<uint64_t> max_bytes = - location_sp->GetCompilerType().GetByteSize( - exe_ctx.GetBestExecutionContextScope()); + llvm::expectedToOptional(location_sp->GetCompilerType().GetByteSize( + exe_ctx.GetBestExecutionContextScope())); if (!max_bytes || size > *max_bytes) return {}; diff --git a/lldb/source/Plugins/Language/CPlusPlus/LibCxxInitializerList.cpp b/lldb/source/Plugins/Language/CPlusPlus/LibCxxInitializerList.cpp index cd13455..ffc8942 100644 --- a/lldb/source/Plugins/Language/CPlusPlus/LibCxxInitializerList.cpp +++ b/lldb/source/Plugins/Language/CPlusPlus/LibCxxInitializerList.cpp @@ -88,8 +88,11 @@ lldb_private::formatters::LibcxxInitializerListSyntheticFrontEnd::Update() { if (!m_element_type.IsValid()) return lldb::ChildCacheState::eRefetch; - if (std::optional<uint64_t> size = m_element_type.GetByteSize(nullptr)) { - m_element_size = *size; + llvm::Expected<uint64_t> size_or_err = m_element_type.GetByteSize(nullptr); + if (!size_or_err) + LLDB_LOG_ERRORV(GetLog(LLDBLog::Types), size_or_err.takeError(), "{0}"); + else { + m_element_size = *size_or_err; // Store raw pointers or end up with a circular dependency. m_start = m_backend.GetChildMemberWithName("__begin_").get(); } diff --git a/lldb/source/Plugins/Language/CPlusPlus/LibCxxProxyArray.cpp b/lldb/source/Plugins/Language/CPlusPlus/LibCxxProxyArray.cpp index fdb8f07..23fcff5 100644 --- a/lldb/source/Plugins/Language/CPlusPlus/LibCxxProxyArray.cpp +++ b/lldb/source/Plugins/Language/CPlusPlus/LibCxxProxyArray.cpp @@ -137,7 +137,8 @@ lldb_private::formatters::LibcxxStdProxyArraySyntheticFrontEnd::Update() { return ChildCacheState::eRefetch; m_element_type = type.GetTypeTemplateArgument(0); - if (std::optional<uint64_t> size = m_element_type.GetByteSize(nullptr)) + if (std::optional<uint64_t> size = + llvm::expectedToOptional(m_element_type.GetByteSize(nullptr))) m_element_size = *size; if (m_element_size == 0) @@ -152,7 +153,8 @@ lldb_private::formatters::LibcxxStdProxyArraySyntheticFrontEnd::Update() { return ChildCacheState::eRefetch; m_element_type_size_t = type.GetTypeTemplateArgument(0); - if (std::optional<uint64_t> size = m_element_type_size_t.GetByteSize(nullptr)) + if (std::optional<uint64_t> size = + llvm::expectedToOptional(m_element_type_size_t.GetByteSize(nullptr))) m_element_size_size_t = *size; if (m_element_size_size_t == 0) diff --git a/lldb/source/Plugins/Language/CPlusPlus/LibCxxSliceArray.cpp b/lldb/source/Plugins/Language/CPlusPlus/LibCxxSliceArray.cpp index 523a7ab..dba80db 100644 --- a/lldb/source/Plugins/Language/CPlusPlus/LibCxxSliceArray.cpp +++ b/lldb/source/Plugins/Language/CPlusPlus/LibCxxSliceArray.cpp @@ -123,7 +123,8 @@ lldb_private::formatters::LibcxxStdSliceArraySyntheticFrontEnd::Update() { return ChildCacheState::eRefetch; m_element_type = type.GetTypeTemplateArgument(0); - if (std::optional<uint64_t> size = m_element_type.GetByteSize(nullptr)) + if (std::optional<uint64_t> size = + llvm::expectedToOptional(m_element_type.GetByteSize(nullptr))) m_element_size = *size; if (m_element_size == 0) diff --git a/lldb/source/Plugins/Language/CPlusPlus/LibCxxSpan.cpp b/lldb/source/Plugins/Language/CPlusPlus/LibCxxSpan.cpp index 21ee830..acda410 100644 --- a/lldb/source/Plugins/Language/CPlusPlus/LibCxxSpan.cpp +++ b/lldb/source/Plugins/Language/CPlusPlus/LibCxxSpan.cpp @@ -102,8 +102,11 @@ lldb_private::formatters::LibcxxStdSpanSyntheticFrontEnd::Update() { m_element_type = data_type_finder_sp->GetCompilerType().GetPointeeType(); // Get element size. - if (std::optional<uint64_t> size = m_element_type.GetByteSize(nullptr)) { - m_element_size = *size; + llvm::Expected<uint64_t> size_or_err = m_element_type.GetByteSize(nullptr); + if (!size_or_err) + LLDB_LOG_ERRORV(GetLog(LLDBLog::Types), size_or_err.takeError(), "{0}"); + else { + m_element_size = *size_or_err; // Get data. if (m_element_size > 0) { diff --git a/lldb/source/Plugins/Language/CPlusPlus/LibCxxValarray.cpp b/lldb/source/Plugins/Language/CPlusPlus/LibCxxValarray.cpp index 18c9c9b..53ad3d9 100644 --- a/lldb/source/Plugins/Language/CPlusPlus/LibCxxValarray.cpp +++ b/lldb/source/Plugins/Language/CPlusPlus/LibCxxValarray.cpp @@ -104,7 +104,8 @@ lldb_private::formatters::LibcxxStdValarraySyntheticFrontEnd::Update() { return ChildCacheState::eRefetch; m_element_type = type.GetTypeTemplateArgument(0); - if (std::optional<uint64_t> size = m_element_type.GetByteSize(nullptr)) + if (std::optional<uint64_t> size = + llvm::expectedToOptional(m_element_type.GetByteSize(nullptr))) m_element_size = *size; if (m_element_size == 0) diff --git a/lldb/source/Plugins/Language/CPlusPlus/LibCxxVariant.cpp b/lldb/source/Plugins/Language/CPlusPlus/LibCxxVariant.cpp index c3cb1fd..701946d 100644 --- a/lldb/source/Plugins/Language/CPlusPlus/LibCxxVariant.cpp +++ b/lldb/source/Plugins/Language/CPlusPlus/LibCxxVariant.cpp @@ -97,10 +97,13 @@ LibcxxVariantGetIndexValidity(ValueObjectSP &impl_sp) { // the byte size. CompilerType index_type = index_sp->GetCompilerType(); - std::optional<uint64_t> index_type_bytes = index_type.GetByteSize(nullptr); - if (!index_type_bytes) - return LibcxxVariantIndexValidity::Invalid; - + llvm::Expected<uint64_t> index_type_bytes = index_type.GetByteSize(nullptr); + if (!index_type_bytes) { + LLDB_LOG_ERRORV(GetLog(LLDBLog::Types), index_type_bytes.takeError(), + "{0}"); + if (!index_type_bytes) + return LibcxxVariantIndexValidity::Invalid; + } uint64_t npos_value = VariantNposValue(*index_type_bytes); uint64_t index_value = index_sp->GetValueAsUnsigned(0); diff --git a/lldb/source/Plugins/Language/CPlusPlus/LibCxxVector.cpp b/lldb/source/Plugins/Language/CPlusPlus/LibCxxVector.cpp index ae3ed63..d538cac 100644 --- a/lldb/source/Plugins/Language/CPlusPlus/LibCxxVector.cpp +++ b/lldb/source/Plugins/Language/CPlusPlus/LibCxxVector.cpp @@ -137,8 +137,11 @@ lldb_private::formatters::LibcxxStdVectorSyntheticFrontEnd::Update() { return lldb::ChildCacheState::eRefetch; m_element_type = data_sp->GetCompilerType().GetPointeeType(); - if (std::optional<uint64_t> size = m_element_type.GetByteSize(nullptr)) { - m_element_size = *size; + llvm::Expected<uint64_t> size_or_err = m_element_type.GetByteSize(nullptr); + if (!size_or_err) + LLDB_LOG_ERRORV(GetLog(LLDBLog::Types), size_or_err.takeError(), "{0}"); + else { + m_element_size = *size_or_err; if (m_element_size > 0) { // store raw pointers or end up with a circular dependency @@ -198,7 +201,8 @@ lldb_private::formatters::LibcxxVectorBoolSyntheticFrontEnd::GetChildAtIndex( return {}; mask = 1 << bit_index; bool bit_set = ((byte & mask) != 0); - std::optional<uint64_t> size = m_bool_type.GetByteSize(nullptr); + std::optional<uint64_t> size = + llvm::expectedToOptional(m_bool_type.GetByteSize(nullptr)); if (!size) return {}; WritableDataBufferSP buffer_sp(new DataBufferHeap(*size, 0)); diff --git a/lldb/source/Plugins/Language/CPlusPlus/LibStdcpp.cpp b/lldb/source/Plugins/Language/CPlusPlus/LibStdcpp.cpp index 127c0cd..a97264f 100644 --- a/lldb/source/Plugins/Language/CPlusPlus/LibStdcpp.cpp +++ b/lldb/source/Plugins/Language/CPlusPlus/LibStdcpp.cpp @@ -309,7 +309,8 @@ bool lldb_private::formatters::LibStdcppWStringSummaryProvider( return false; // Safe to pass nullptr for exe_scope here. - std::optional<uint64_t> size = wchar_compiler_type.GetBitSize(nullptr); + std::optional<uint64_t> size = + llvm::expectedToOptional(wchar_compiler_type.GetBitSize(nullptr)); if (!size) return false; const uint32_t wchar_size = *size; diff --git a/lldb/source/Plugins/Language/CPlusPlus/LibStdcppUniquePointer.cpp b/lldb/source/Plugins/Language/CPlusPlus/LibStdcppUniquePointer.cpp index 209aace..722203f 100644 --- a/lldb/source/Plugins/Language/CPlusPlus/LibStdcppUniquePointer.cpp +++ b/lldb/source/Plugins/Language/CPlusPlus/LibStdcppUniquePointer.cpp @@ -101,7 +101,8 @@ lldb::ChildCacheState LibStdcppUniquePtrSyntheticFrontEnd::Update() { // storage due to no_unique_address, so infer the actual size from the total // size of the unique_ptr class. If sizeof(unique_ptr) == sizeof(void*) then // the deleter is empty and should be hidden. - if (tuple_sp->GetByteSize() > ptr_obj->GetByteSize()) { + if (llvm::expectedToOptional(tuple_sp->GetByteSize()).value_or(0) > + llvm::expectedToOptional(ptr_obj->GetByteSize()).value_or(0)) { ValueObjectSP del_obj = tuple_frontend->GetChildAtIndex(1); if (del_obj) m_del_obj = del_obj->Clone(ConstString("deleter")).get(); diff --git a/lldb/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCClassDescriptorV2.cpp b/lldb/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCClassDescriptorV2.cpp index 54c9f32..dac9393 100644 --- a/lldb/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCClassDescriptorV2.cpp +++ b/lldb/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCClassDescriptorV2.cpp @@ -722,7 +722,7 @@ void ClassDescriptorV2::iVarsStorage::fill(AppleObjCRuntimeV2 &runtime, "name = {0}, encoding = {1}, offset_ptr = {2:x}, size = " "{3}, type_size = {4}", name, type, offset_ptr, size, - ivar_type.GetByteSize(nullptr).value_or(0)); + expectedToOptional(ivar_type.GetByteSize(nullptr)).value_or(0)); Scalar offset_scalar; Status error; const int offset_ptr_size = 4; diff --git a/lldb/source/Plugins/RegisterTypeBuilder/RegisterTypeBuilderClang.cpp b/lldb/source/Plugins/RegisterTypeBuilder/RegisterTypeBuilderClang.cpp index 1ecde7be..f19dc8b 100644 --- a/lldb/source/Plugins/RegisterTypeBuilder/RegisterTypeBuilderClang.cpp +++ b/lldb/source/Plugins/RegisterTypeBuilder/RegisterTypeBuilderClang.cpp @@ -116,7 +116,8 @@ CompilerType RegisterTypeBuilderClang::GetRegisterType( type_system->SetIsPacked(fields_type); // This should be true if RegisterFlags padded correctly. - assert(*fields_type.GetByteSize(nullptr) == flags.GetSize()); + assert(llvm::expectedToOptional(fields_type.GetByteSize(nullptr)) + .value_or(0) == flags.GetSize()); } return fields_type; diff --git a/lldb/source/Plugins/SymbolFile/CTF/SymbolFileCTF.cpp b/lldb/source/Plugins/SymbolFile/CTF/SymbolFileCTF.cpp index 0b8862f..c0b931f 100644 --- a/lldb/source/Plugins/SymbolFile/CTF/SymbolFileCTF.cpp +++ b/lldb/source/Plugins/SymbolFile/CTF/SymbolFileCTF.cpp @@ -437,14 +437,11 @@ SymbolFileCTF::CreateArray(const CTFArray &ctf_array) { llvm::formatv("Could not find array element type: {0}", ctf_array.type), llvm::inconvertibleErrorCode()); - std::optional<uint64_t> element_size = element_type->GetByteSize(nullptr); - if (!element_size) - return llvm::make_error<llvm::StringError>( - llvm::formatv("could not get element size of type: {0}", - ctf_array.type), - llvm::inconvertibleErrorCode()); + auto element_size_or_err = element_type->GetByteSize(nullptr); + if (!element_size_or_err) + return element_size_or_err.takeError(); - uint64_t size = ctf_array.nelems * *element_size; + uint64_t size = ctf_array.nelems * *element_size_or_err; CompilerType compiler_type = m_ast->CreateArrayType( element_type->GetFullCompilerType(), ctf_array.nelems, @@ -544,7 +541,8 @@ bool SymbolFileCTF::CompleteType(CompilerType &compiler_type) { for (const CTFRecord::Field &field : ctf_record->fields) { Type *field_type = ResolveTypeUID(field.type); assert(field_type && "field must be complete"); - const uint32_t field_size = field_type->GetByteSize(nullptr).value_or(0); + const uint32_t field_size = + llvm::expectedToOptional(field_type->GetByteSize(nullptr)).value_or(0); TypeSystemClang::AddFieldToRecordType(compiler_type, field.name, field_type->GetFullCompilerType(), eAccessPublic, field_size); diff --git a/lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp b/lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp index 2d4d225..0b63275 100644 --- a/lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp +++ b/lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp @@ -290,9 +290,10 @@ TypeSP DWARFASTParserClang::ParseTypeFromClangModule(const SymbolContext &sc, SymbolFileDWARF *dwarf = die.GetDWARF(); auto type_sp = dwarf->MakeType( - die.GetID(), pcm_type_sp->GetName(), pcm_type_sp->GetByteSize(nullptr), - nullptr, LLDB_INVALID_UID, Type::eEncodingInvalid, - &pcm_type_sp->GetDeclaration(), type, Type::ResolveState::Forward, + die.GetID(), pcm_type_sp->GetName(), + llvm::expectedToOptional(pcm_type_sp->GetByteSize(nullptr)), nullptr, + LLDB_INVALID_UID, Type::eEncodingInvalid, &pcm_type_sp->GetDeclaration(), + type, Type::ResolveState::Forward, TypePayloadClang(GetOwningClangModule(die))); clang::TagDecl *tag_decl = TypeSystemClang::GetAsTagDecl(type); if (tag_decl) { @@ -1463,7 +1464,8 @@ DWARFASTParserClang::ParseArrayType(const DWARFDIE &die, bit_stride = array_info->bit_stride; } if (byte_stride == 0 && bit_stride == 0) - byte_stride = element_type->GetByteSize(nullptr).value_or(0); + byte_stride = llvm::expectedToOptional(element_type->GetByteSize(nullptr)) + .value_or(0); CompilerType array_element_type = element_type->GetForwardCompilerType(); TypeSystemClang::RequireCompleteType(array_element_type); @@ -1515,7 +1517,7 @@ TypeSP DWARFASTParserClang::ParsePointerToMemberType( class_clang_type, pointee_clang_type); if (std::optional<uint64_t> clang_type_size = - clang_type.GetByteSize(nullptr)) { + llvm::expectedToOptional(clang_type.GetByteSize(nullptr))) { return dwarf->MakeType(die.GetID(), attrs.name, *clang_type_size, nullptr, LLDB_INVALID_UID, Type::eEncodingIsUID, nullptr, clang_type, Type::ResolveState::Forward); @@ -1976,7 +1978,8 @@ private: static std::optional<clang::APValue> MakeAPValue(const clang::ASTContext &ast, CompilerType clang_type, uint64_t value) { - std::optional<uint64_t> bit_width = clang_type.GetBitSize(nullptr); + std::optional<uint64_t> bit_width = + llvm::expectedToOptional(clang_type.GetBitSize(nullptr)); if (!bit_width) return std::nullopt; @@ -2246,9 +2249,10 @@ bool DWARFASTParserClang::CompleteEnumType(const DWARFDIE &die, if (TypeSystemClang::StartTagDeclarationDefinition(clang_type)) { if (die.HasChildren()) - ParseChildEnumerators(clang_type, - clang_type.IsEnumerationIntegerTypeSigned(), - type->GetByteSize(nullptr).value_or(0), die); + ParseChildEnumerators( + clang_type, clang_type.IsEnumerationIntegerTypeSigned(), + llvm::expectedToOptional(type->GetByteSize(nullptr)).value_or(0), + die); TypeSystemClang::CompleteTagDeclarationDefinition(clang_type); } @@ -2981,7 +2985,7 @@ void DWARFASTParserClang::ParseSingleMember( } else { auto byte_size = attrs.byte_size; if (!byte_size) - byte_size = member_type->GetByteSize(nullptr); + byte_size = llvm::expectedToOptional(member_type->GetByteSize(nullptr)); ObjectFile *objfile = die.GetDWARF()->GetObjectFile(); if (objfile->GetByteOrder() == eByteOrderLittle) { @@ -3042,7 +3046,7 @@ void DWARFASTParserClang::ParseSingleMember( // TODO: we shouldn't silently ignore the bit_size if we fail // to GetByteSize. if (std::optional<uint64_t> clang_type_size = - member_type->GetByteSize(nullptr)) { + llvm::expectedToOptional(member_type->GetByteSize(nullptr))) { this_field_info.bit_size = *clang_type_size * character_width; } @@ -3851,7 +3855,9 @@ void DWARFASTParserClang::ParseRustVariantPart( m_ast.AddFieldToRecordType( field_type, "$discr$", discriminant_type->GetFullCompilerType(), lldb::eAccessPublic, variants.discriminant().byte_offset); - offset += discriminant_type->GetByteSize(nullptr).value_or(0); + offset += + llvm::expectedToOptional(discriminant_type->GetByteSize(nullptr)) + .value_or(0); } m_ast.AddFieldToRecordType(field_type, "value", diff --git a/lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp b/lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp index d562de8..d1aaf0b 100644 --- a/lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp +++ b/lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp @@ -2077,8 +2077,9 @@ SymbolFileDWARF::GlobalVariableMap &SymbolFileDWARF::GetGlobalAranges() { location_result->GetScalar().ULongLong(); lldb::addr_t byte_size = 1; if (var_sp->GetType()) - byte_size = - var_sp->GetType()->GetByteSize(nullptr).value_or(0); + byte_size = llvm::expectedToOptional( + var_sp->GetType()->GetByteSize(nullptr)) + .value_or(0); m_global_aranges_up->Append(GlobalVariableMap::Entry( file_addr, byte_size, var_sp.get())); } @@ -3622,9 +3623,11 @@ VariableSP SymbolFileDWARF::ParseVariableDIE(const SymbolContext &sc, DWARFFormValue::IsDataForm(const_value_form.Form()); if (use_type_size_for_value && type_sp->GetType()) { DWARFExpression *location = location_list.GetMutableExpressionAtAddress(); - location->UpdateValue(const_value_form.Unsigned(), - type_sp->GetType()->GetByteSize(nullptr).value_or(0), - die.GetCU()->GetAddressByteSize()); + location->UpdateValue( + const_value_form.Unsigned(), + llvm::expectedToOptional(type_sp->GetType()->GetByteSize(nullptr)) + .value_or(0), + die.GetCU()->GetAddressByteSize()); } return std::make_shared<Variable>( diff --git a/lldb/source/Plugins/SymbolFile/NativePDB/SymbolFileNativePDB.cpp b/lldb/source/Plugins/SymbolFile/NativePDB/SymbolFileNativePDB.cpp index 4e472d0..ce03601 100644 --- a/lldb/source/Plugins/SymbolFile/NativePDB/SymbolFileNativePDB.cpp +++ b/lldb/source/Plugins/SymbolFile/NativePDB/SymbolFileNativePDB.cpp @@ -552,8 +552,8 @@ lldb::TypeSP SymbolFileNativePDB::CreateModifierType(PdbTypeSymId type_id, lldb::TypeSP modified_type = GetOrCreateType(mr.ModifiedType); return MakeType(toOpaqueUid(type_id), ConstString(name), - modified_type->GetByteSize(nullptr), nullptr, - LLDB_INVALID_UID, Type::eEncodingIsUID, decl, ct, + llvm::expectedToOptional(modified_type->GetByteSize(nullptr)), + nullptr, LLDB_INVALID_UID, Type::eEncodingIsUID, decl, ct, Type::ResolveState::Full); } @@ -670,10 +670,11 @@ lldb::TypeSP SymbolFileNativePDB::CreateTagType(PdbTypeSymId type_id, Declaration decl; TypeSP underlying_type = GetOrCreateType(er.UnderlyingType); - return MakeType(toOpaqueUid(type_id), ConstString(uname), - underlying_type->GetByteSize(nullptr), nullptr, - LLDB_INVALID_UID, lldb_private::Type::eEncodingIsUID, decl, - ct, lldb_private::Type::ResolveState::Forward); + return MakeType( + toOpaqueUid(type_id), ConstString(uname), + llvm::expectedToOptional(underlying_type->GetByteSize(nullptr)), nullptr, + LLDB_INVALID_UID, lldb_private::Type::eEncodingIsUID, decl, ct, + lldb_private::Type::ResolveState::Forward); } TypeSP SymbolFileNativePDB::CreateArrayType(PdbTypeSymId type_id, @@ -1915,11 +1916,12 @@ TypeSP SymbolFileNativePDB::CreateTypedef(PdbGlobalSymId id) { ts->GetNativePDBParser()->GetOrCreateTypedefDecl(id); Declaration decl; - return MakeType( - toOpaqueUid(id), ConstString(udt.Name), target_type->GetByteSize(nullptr), - nullptr, target_type->GetID(), lldb_private::Type::eEncodingIsTypedefUID, - decl, target_type->GetForwardCompilerType(), - lldb_private::Type::ResolveState::Forward); + return MakeType(toOpaqueUid(id), ConstString(udt.Name), + llvm::expectedToOptional(target_type->GetByteSize(nullptr)), + nullptr, target_type->GetID(), + lldb_private::Type::eEncodingIsTypedefUID, decl, + target_type->GetForwardCompilerType(), + lldb_private::Type::ResolveState::Forward); } TypeSP SymbolFileNativePDB::GetOrCreateTypedef(PdbGlobalSymId id) { diff --git a/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp b/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp index 1e0c7f0..4ca4752 100644 --- a/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp +++ b/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp @@ -4769,7 +4769,7 @@ TypeSystemClang::GetFloatTypeSemantics(size_t byte_size) { return llvm::APFloatBase::Bogus(); } -std::optional<uint64_t> +llvm::Expected<uint64_t> TypeSystemClang::GetObjCBitSize(QualType qual_type, ExecutionContextScope *exe_scope) { assert(qual_type->isObjCObjectOrInterfaceType()); @@ -4802,11 +4802,14 @@ TypeSystemClang::GetObjCBitSize(QualType qual_type, getASTContext().getTypeSize(getASTContext().ObjCBuiltinClassTy); } -std::optional<uint64_t> +llvm::Expected<uint64_t> TypeSystemClang::GetBitSize(lldb::opaque_compiler_type_t type, ExecutionContextScope *exe_scope) { + const bool base_name_only = true; if (!GetCompleteType(type)) - return std::nullopt; + return llvm::createStringError( + "could not complete type %s", + GetTypeName(type, base_name_only).AsCString("")); clang::QualType qual_type(GetCanonicalQualType(type)); const clang::Type::TypeClass type_class = qual_type->getTypeClass(); @@ -4832,7 +4835,9 @@ TypeSystemClang::GetBitSize(lldb::opaque_compiler_type_t type, return bit_size; } - return std::nullopt; + return llvm::createStringError( + "could not get size of type %s", + GetTypeName(type, base_name_only).AsCString("")); } std::optional<size_t> @@ -6301,12 +6306,14 @@ llvm::Expected<CompilerType> TypeSystemClang::GetChildCompilerTypeAtIndex( child_byte_offset = bit_offset / 8; CompilerType base_class_clang_type = GetType(base_class->getType()); child_name = base_class_clang_type.GetTypeName().AsCString(""); - std::optional<uint64_t> size = + auto size_or_err = base_class_clang_type.GetBitSize(get_exe_scope()); - if (!size) - return llvm::createStringError("no size info for base class"); + if (!size_or_err) + return llvm::joinErrors( + llvm::createStringError("no size info for base class"), + size_or_err.takeError()); - uint64_t base_class_clang_type_bit_size = *size; + uint64_t base_class_clang_type_bit_size = *size_or_err; // Base classes bit sizes should be a multiple of 8 bits in size assert(base_class_clang_type_bit_size % 8 == 0); @@ -6334,12 +6341,13 @@ llvm::Expected<CompilerType> TypeSystemClang::GetChildCompilerTypeAtIndex( // alignment (field_type_info.second) from the AST context. CompilerType field_clang_type = GetType(field->getType()); assert(field_idx < record_layout.getFieldCount()); - std::optional<uint64_t> size = - field_clang_type.GetByteSize(get_exe_scope()); - if (!size) - return llvm::createStringError("no size info for field"); + auto size_or_err = field_clang_type.GetByteSize(get_exe_scope()); + if (!size_or_err) + return llvm::joinErrors( + llvm::createStringError("no size info for field"), + size_or_err.takeError()); - child_byte_size = *size; + child_byte_size = *size_or_err; const uint32_t child_bit_size = child_byte_size * 8; // Figure out the field offset within the current struct/union/class @@ -6509,12 +6517,12 @@ llvm::Expected<CompilerType> TypeSystemClang::GetChildCompilerTypeAtIndex( // We have a pointer to an simple type if (idx == 0 && pointee_clang_type.GetCompleteType()) { - if (std::optional<uint64_t> size = - pointee_clang_type.GetByteSize(get_exe_scope())) { - child_byte_size = *size; - child_byte_offset = 0; - return pointee_clang_type; - } + auto size_or_err = pointee_clang_type.GetByteSize(get_exe_scope()); + if (!size_or_err) + return size_or_err.takeError(); + child_byte_size = *size_or_err; + child_byte_offset = 0; + return pointee_clang_type; } } } @@ -6532,12 +6540,12 @@ llvm::Expected<CompilerType> TypeSystemClang::GetChildCompilerTypeAtIndex( ::snprintf(element_name, sizeof(element_name), "[%" PRIu64 "]", static_cast<uint64_t>(idx)); child_name.assign(element_name); - if (std::optional<uint64_t> size = - element_type.GetByteSize(get_exe_scope())) { - child_byte_size = *size; - child_byte_offset = (int32_t)idx * (int32_t)child_byte_size; - return element_type; - } + auto size_or_err = element_type.GetByteSize(get_exe_scope()); + if (!size_or_err) + return size_or_err.takeError(); + child_byte_size = *size_or_err; + child_byte_offset = (int32_t)idx * (int32_t)child_byte_size; + return element_type; } } } @@ -6551,12 +6559,12 @@ llvm::Expected<CompilerType> TypeSystemClang::GetChildCompilerTypeAtIndex( CompilerType element_type = GetType(array->getElementType()); if (element_type.GetCompleteType()) { child_name = std::string(llvm::formatv("[{0}]", idx)); - if (std::optional<uint64_t> size = - element_type.GetByteSize(get_exe_scope())) { - child_byte_size = *size; - child_byte_offset = (int32_t)idx * (int32_t)child_byte_size; - return element_type; - } + auto size_or_err = element_type.GetByteSize(get_exe_scope()); + if (!size_or_err) + return size_or_err.takeError(); + child_byte_size = *size_or_err; + child_byte_offset = (int32_t)idx * (int32_t)child_byte_size; + return element_type; } } } @@ -6590,12 +6598,12 @@ llvm::Expected<CompilerType> TypeSystemClang::GetChildCompilerTypeAtIndex( // We have a pointer to an simple type if (idx == 0) { - if (std::optional<uint64_t> size = - pointee_clang_type.GetByteSize(get_exe_scope())) { - child_byte_size = *size; - child_byte_offset = 0; - return pointee_clang_type; - } + auto size_or_err = pointee_clang_type.GetByteSize(get_exe_scope()); + if (!size_or_err) + return size_or_err.takeError(); + child_byte_size = *size_or_err; + child_byte_offset = 0; + return pointee_clang_type; } } break; @@ -6628,12 +6636,12 @@ llvm::Expected<CompilerType> TypeSystemClang::GetChildCompilerTypeAtIndex( // We have a pointer to an simple type if (idx == 0) { - if (std::optional<uint64_t> size = - pointee_clang_type.GetByteSize(get_exe_scope())) { - child_byte_size = *size; - child_byte_offset = 0; - return pointee_clang_type; - } + auto size_or_err = pointee_clang_type.GetByteSize(get_exe_scope()); + if (!size_or_err) + return size_or_err.takeError(); + child_byte_size = *size_or_err; + child_byte_offset = 0; + return pointee_clang_type; } } } diff --git a/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.h b/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.h index 99d9bec..6579f7b 100644 --- a/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.h +++ b/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.h @@ -827,15 +827,17 @@ public: const llvm::fltSemantics &GetFloatTypeSemantics(size_t byte_size) override; - std::optional<uint64_t> GetByteSize(lldb::opaque_compiler_type_t type, - ExecutionContextScope *exe_scope) { - if (std::optional<uint64_t> bit_size = GetBitSize(type, exe_scope)) - return (*bit_size + 7) / 8; - return std::nullopt; + llvm::Expected<uint64_t> GetByteSize(lldb::opaque_compiler_type_t type, + ExecutionContextScope *exe_scope) { + auto bit_size_or_err = GetBitSize(type, exe_scope); + if (!bit_size_or_err) + return bit_size_or_err.takeError(); + return (*bit_size_or_err + 7) / 8; } - std::optional<uint64_t> GetBitSize(lldb::opaque_compiler_type_t type, - ExecutionContextScope *exe_scope) override; + llvm::Expected<uint64_t> + GetBitSize(lldb::opaque_compiler_type_t type, + ExecutionContextScope *exe_scope) override; lldb::Encoding GetEncoding(lldb::opaque_compiler_type_t type, uint64_t &count) override; @@ -1185,8 +1187,8 @@ private: /// on creation of a new instance. void LogCreation() const; - std::optional<uint64_t> GetObjCBitSize(clang::QualType qual_type, - ExecutionContextScope *exe_scope); + llvm::Expected<uint64_t> GetObjCBitSize(clang::QualType qual_type, + ExecutionContextScope *exe_scope); // Classes that inherit from TypeSystemClang can see and modify these std::string m_target_triple; diff --git a/lldb/source/Symbol/CompilerType.cpp b/lldb/source/Symbol/CompilerType.cpp index 09820fb..22fdd24 100644 --- a/lldb/source/Symbol/CompilerType.cpp +++ b/lldb/source/Symbol/CompilerType.cpp @@ -15,6 +15,8 @@ #include "lldb/Utility/ConstString.h" #include "lldb/Utility/DataBufferHeap.h" #include "lldb/Utility/DataExtractor.h" +#include "lldb/Utility/LLDBLog.h" +#include "lldb/Utility/Log.h" #include "lldb/Utility/Scalar.h" #include "lldb/Utility/Stream.h" #include "lldb/Utility/StreamString.h" @@ -769,19 +771,20 @@ CompilerType::GetBasicTypeFromAST(lldb::BasicType basic_type) const { } // Exploring the type -std::optional<uint64_t> +llvm::Expected<uint64_t> CompilerType::GetBitSize(ExecutionContextScope *exe_scope) const { if (IsValid()) if (auto type_system_sp = GetTypeSystem()) return type_system_sp->GetBitSize(m_type, exe_scope); - return {}; + return llvm::createStringError("Invalid type: Cannot determine size"); } -std::optional<uint64_t> +llvm::Expected<uint64_t> CompilerType::GetByteSize(ExecutionContextScope *exe_scope) const { - if (std::optional<uint64_t> bit_size = GetBitSize(exe_scope)) - return (*bit_size + 7) / 8; - return {}; + auto bit_size_or_err = GetBitSize(exe_scope); + if (!bit_size_or_err) + return bit_size_or_err.takeError(); + return (*bit_size_or_err + 7) / 8; } std::optional<size_t> @@ -1104,10 +1107,18 @@ bool CompilerType::GetValueAsScalar(const lldb_private::DataExtractor &data, if (encoding == lldb::eEncodingInvalid || count != 1) return false; - std::optional<uint64_t> byte_size = GetByteSize(exe_scope); + auto byte_size_or_err = GetByteSize(exe_scope); + if (!byte_size_or_err) { + LLDB_LOG_ERRORV( + GetLog(LLDBLog::Types), byte_size_or_err.takeError(), + "Cannot get value as scalar: Cannot determine type size: {0}"); + return false; + } + uint64_t byte_size = *byte_size_or_err; + // A bit or byte size of 0 is not a bug, but it doesn't make sense to read a // scalar of zero size. - if (!byte_size || *byte_size == 0) + if (byte_size == 0) return false; lldb::offset_t offset = data_byte_offset; @@ -1117,15 +1128,15 @@ bool CompilerType::GetValueAsScalar(const lldb_private::DataExtractor &data, case lldb::eEncodingVector: break; case lldb::eEncodingUint: - if (*byte_size <= sizeof(unsigned long long)) { - uint64_t uval64 = data.GetMaxU64(&offset, *byte_size); - if (*byte_size <= sizeof(unsigned int)) { + if (byte_size <= sizeof(unsigned long long)) { + uint64_t uval64 = data.GetMaxU64(&offset, byte_size); + if (byte_size <= sizeof(unsigned int)) { value = (unsigned int)uval64; return true; - } else if (*byte_size <= sizeof(unsigned long)) { + } else if (byte_size <= sizeof(unsigned long)) { value = (unsigned long)uval64; return true; - } else if (*byte_size <= sizeof(unsigned long long)) { + } else if (byte_size <= sizeof(unsigned long long)) { value = (unsigned long long)uval64; return true; } else @@ -1134,15 +1145,15 @@ bool CompilerType::GetValueAsScalar(const lldb_private::DataExtractor &data, break; case lldb::eEncodingSint: - if (*byte_size <= sizeof(long long)) { - int64_t sval64 = data.GetMaxS64(&offset, *byte_size); - if (*byte_size <= sizeof(int)) { + if (byte_size <= sizeof(long long)) { + int64_t sval64 = data.GetMaxS64(&offset, byte_size); + if (byte_size <= sizeof(int)) { value = (int)sval64; return true; - } else if (*byte_size <= sizeof(long)) { + } else if (byte_size <= sizeof(long)) { value = (long)sval64; return true; - } else if (*byte_size <= sizeof(long long)) { + } else if (byte_size <= sizeof(long long)) { value = (long long)sval64; return true; } else @@ -1151,10 +1162,10 @@ bool CompilerType::GetValueAsScalar(const lldb_private::DataExtractor &data, break; case lldb::eEncodingIEEE754: - if (*byte_size <= sizeof(long double)) { + if (byte_size <= sizeof(long double)) { uint32_t u32; uint64_t u64; - if (*byte_size == sizeof(float)) { + if (byte_size == sizeof(float)) { if (sizeof(float) == sizeof(uint32_t)) { u32 = data.GetU32(&offset); value = *((float *)&u32); @@ -1164,7 +1175,7 @@ bool CompilerType::GetValueAsScalar(const lldb_private::DataExtractor &data, value = *((float *)&u64); return true; } - } else if (*byte_size == sizeof(double)) { + } else if (byte_size == sizeof(double)) { if (sizeof(double) == sizeof(uint32_t)) { u32 = data.GetU32(&offset); value = *((double *)&u32); @@ -1174,7 +1185,7 @@ bool CompilerType::GetValueAsScalar(const lldb_private::DataExtractor &data, value = *((double *)&u64); return true; } - } else if (*byte_size == sizeof(long double)) { + } else if (byte_size == sizeof(long double)) { if (sizeof(long double) == sizeof(uint32_t)) { u32 = data.GetU32(&offset); value = *((long double *)&u32); diff --git a/lldb/source/Symbol/Type.cpp b/lldb/source/Symbol/Type.cpp index f7b44ad..0a886e5 100644 --- a/lldb/source/Symbol/Type.cpp +++ b/lldb/source/Symbol/Type.cpp @@ -455,14 +455,18 @@ Type *Type::GetEncodingType() { return m_encoding_type; } -std::optional<uint64_t> Type::GetByteSize(ExecutionContextScope *exe_scope) { +llvm::Expected<uint64_t> Type::GetByteSize(ExecutionContextScope *exe_scope) { if (m_byte_size_has_value) return static_cast<uint64_t>(m_byte_size); switch (m_encoding_uid_type) { case eEncodingInvalid: + return llvm::createStringError("could not get type size: invalid encoding"); + case eEncodingIsSyntheticUID: - break; + return llvm::createStringError( + "could not get type size: synthetic encoding"); + case eEncodingIsUID: case eEncodingIsConstUID: case eEncodingIsRestrictUID: @@ -472,18 +476,18 @@ std::optional<uint64_t> Type::GetByteSize(ExecutionContextScope *exe_scope) { Type *encoding_type = GetEncodingType(); if (encoding_type) if (std::optional<uint64_t> size = - encoding_type->GetByteSize(exe_scope)) { + llvm::expectedToOptional(encoding_type->GetByteSize(exe_scope))) { m_byte_size = *size; m_byte_size_has_value = true; return static_cast<uint64_t>(m_byte_size); } - if (std::optional<uint64_t> size = - GetLayoutCompilerType().GetByteSize(exe_scope)) { - m_byte_size = *size; - m_byte_size_has_value = true; - return static_cast<uint64_t>(m_byte_size); - } + auto size_or_err = GetLayoutCompilerType().GetByteSize(exe_scope); + if (!size_or_err) + return size_or_err.takeError(); + m_byte_size = *size_or_err; + m_byte_size_has_value = true; + return static_cast<uint64_t>(m_byte_size); } break; // If we are a pointer or reference, then this is just a pointer size; @@ -498,7 +502,8 @@ std::optional<uint64_t> Type::GetByteSize(ExecutionContextScope *exe_scope) { } } break; } - return {}; + return llvm::createStringError( + "could not get type size: unexpected encoding"); } llvm::Expected<uint32_t> Type::GetNumChildren(bool omit_empty_base_classes) { @@ -539,7 +544,9 @@ bool Type::ReadFromMemory(ExecutionContext *exe_ctx, lldb::addr_t addr, } const uint64_t byte_size = - GetByteSize(exe_ctx ? exe_ctx->GetBestExecutionContextScope() : nullptr) + llvm::expectedToOptional( + GetByteSize(exe_ctx ? exe_ctx->GetBestExecutionContextScope() + : nullptr)) .value_or(0); if (data.GetByteSize() < byte_size) { lldb::DataBufferSP data_sp(new DataBufferHeap(byte_size, '\0')); diff --git a/lldb/source/Target/StackFrame.cpp b/lldb/source/Target/StackFrame.cpp index f8061ff..d92b7d8 100644 --- a/lldb/source/Target/StackFrame.cpp +++ b/lldb/source/Target/StackFrame.cpp @@ -1480,7 +1480,9 @@ lldb::ValueObjectSP StackFrame::GuessValueForAddress(lldb::addr_t addr) { namespace { ValueObjectSP GetValueForOffset(StackFrame &frame, ValueObjectSP &parent, int64_t offset) { - if (offset < 0 || uint64_t(offset) >= parent->GetByteSize()) { + if (offset < 0 || + uint64_t(offset) >= + llvm::expectedToOptional(parent->GetByteSize()).value_or(0)) { return ValueObjectSP(); } @@ -1497,7 +1499,8 @@ ValueObjectSP GetValueForOffset(StackFrame &frame, ValueObjectSP &parent, } int64_t child_offset = child_sp->GetByteOffset(); - int64_t child_size = child_sp->GetByteSize().value_or(0); + int64_t child_size = + llvm::expectedToOptional(child_sp->GetByteSize()).value_or(0); if (offset >= child_offset && offset < (child_offset + child_size)) { return GetValueForOffset(frame, child_sp, offset - child_offset); @@ -1529,9 +1532,13 @@ ValueObjectSP GetValueForDereferincingOffset(StackFrame &frame, return ValueObjectSP(); } - if (offset >= 0 && uint64_t(offset) >= pointee->GetByteSize()) { - int64_t index = offset / pointee->GetByteSize().value_or(1); - offset = offset % pointee->GetByteSize().value_or(1); + if (offset >= 0 && + uint64_t(offset) >= + llvm::expectedToOptional(pointee->GetByteSize()).value_or(0)) { + uint64_t size = + llvm::expectedToOptional(pointee->GetByteSize()).value_or(1); + int64_t index = offset / size; + offset = offset % size; const bool can_create = true; pointee = base->GetSyntheticArrayMember(index, can_create); } diff --git a/lldb/source/Target/Thread.cpp b/lldb/source/Target/Thread.cpp index 50f7c73..cdadc14 100644 --- a/lldb/source/Target/Thread.cpp +++ b/lldb/source/Target/Thread.cpp @@ -2096,10 +2096,13 @@ lldb::ValueObjectSP Thread::GetSiginfoValue() { return ValueObjectConstResult::Create( &target, Status::FromErrorString("no siginfo_t for the platform")); - std::optional<uint64_t> type_size = type.GetByteSize(nullptr); - assert(type_size); + auto type_size_or_err = type.GetByteSize(nullptr); + if (!type_size_or_err) + return ValueObjectConstResult::Create( + &target, Status::FromError(type_size_or_err.takeError())); + llvm::Expected<std::unique_ptr<llvm::MemoryBuffer>> data = - GetSiginfo(*type_size); + GetSiginfo(*type_size_or_err); if (!data) return ValueObjectConstResult::Create(&target, Status::FromError(data.takeError())); diff --git a/lldb/source/ValueObject/ValueObject.cpp b/lldb/source/ValueObject/ValueObject.cpp index 9d98f62..eac2435 100644 --- a/lldb/source/ValueObject/ValueObject.cpp +++ b/lldb/source/ValueObject/ValueObject.cpp @@ -677,8 +677,8 @@ size_t ValueObject::GetPointeeData(DataExtractor &data, uint32_t item_idx, ExecutionContext exe_ctx(GetExecutionContextRef()); std::optional<uint64_t> item_type_size = - pointee_or_element_compiler_type.GetByteSize( - exe_ctx.GetBestExecutionContextScope()); + llvm::expectedToOptional(pointee_or_element_compiler_type.GetByteSize( + exe_ctx.GetBestExecutionContextScope())); if (!item_type_size) return 0; const uint64_t bytes = item_count * *item_type_size; @@ -794,7 +794,7 @@ bool ValueObject::SetData(DataExtractor &data, Status &error) { uint64_t count = 0; const Encoding encoding = GetCompilerType().GetEncoding(count); - const size_t byte_size = GetByteSize().value_or(0); + const size_t byte_size = llvm::expectedToOptional(GetByteSize()).value_or(0); Value::ValueType value_type = m_value.GetValueType(); @@ -1224,7 +1224,8 @@ void ValueObject::SetValueFromInteger(const llvm::APInt &value, Status &error) { // Verify the proposed new value is the right size. lldb::TargetSP target = GetTargetSP(); uint64_t byte_size = 0; - if (auto temp = GetCompilerType().GetByteSize(target.get())) + if (auto temp = + llvm::expectedToOptional(GetCompilerType().GetByteSize(target.get()))) byte_size = temp.value(); if (value.getBitWidth() != byte_size * CHAR_BIT) { error = Status::FromErrorString( @@ -1287,7 +1288,8 @@ void ValueObject::SetValueFromInteger(lldb::ValueObjectSP new_val_sp, if (success) { lldb::TargetSP target = GetTargetSP(); uint64_t num_bits = 0; - if (auto temp = new_val_sp->GetCompilerType().GetBitSize(target.get())) + if (auto temp = llvm::expectedToOptional( + new_val_sp->GetCompilerType().GetBitSize(target.get()))) num_bits = temp.value(); SetValueFromInteger(llvm::APInt(num_bits, int_val), error); } else @@ -1679,7 +1681,7 @@ bool ValueObject::SetValueFromCString(const char *value_str, Status &error) { uint64_t count = 0; const Encoding encoding = GetCompilerType().GetEncoding(count); - const size_t byte_size = GetByteSize().value_or(0); + const size_t byte_size = llvm::expectedToOptional(GetByteSize()).value_or(0); Value::ValueType value_type = m_value.GetValueType(); @@ -1863,13 +1865,15 @@ ValueObjectSP ValueObject::GetSyntheticBitFieldChild(uint32_t from, uint32_t to, uint32_t bit_field_offset = from; if (GetDataExtractor().GetByteOrder() == eByteOrderBig) bit_field_offset = - GetByteSize().value_or(0) * 8 - bit_field_size - bit_field_offset; + llvm::expectedToOptional(GetByteSize()).value_or(0) * 8 - + bit_field_size - bit_field_offset; // We haven't made a synthetic array member for INDEX yet, so lets make // one and cache it for any future reference. ValueObjectChild *synthetic_child = new ValueObjectChild( - *this, GetCompilerType(), index_const_str, GetByteSize().value_or(0), - 0, bit_field_size, bit_field_offset, false, false, - eAddressTypeInvalid, 0); + *this, GetCompilerType(), index_const_str, + llvm::expectedToOptional(GetByteSize()).value_or(0), 0, + bit_field_size, bit_field_offset, false, false, eAddressTypeInvalid, + 0); // Cache the value if we got one back... if (synthetic_child) { @@ -1904,8 +1908,8 @@ ValueObjectSP ValueObject::GetSyntheticChildAtOffset( return {}; ExecutionContext exe_ctx(GetExecutionContextRef()); - std::optional<uint64_t> size = - type.GetByteSize(exe_ctx.GetBestExecutionContextScope()); + std::optional<uint64_t> size = llvm::expectedToOptional( + type.GetByteSize(exe_ctx.GetBestExecutionContextScope())); if (!size) return {}; ValueObjectChild *synthetic_child = @@ -1946,8 +1950,8 @@ ValueObjectSP ValueObject::GetSyntheticBase(uint32_t offset, const bool is_base_class = true; ExecutionContext exe_ctx(GetExecutionContextRef()); - std::optional<uint64_t> size = - type.GetByteSize(exe_ctx.GetBestExecutionContextScope()); + std::optional<uint64_t> size = llvm::expectedToOptional( + type.GetByteSize(exe_ctx.GetBestExecutionContextScope())); if (!size) return {}; ValueObjectChild *synthetic_child = @@ -2999,8 +3003,10 @@ ValueObjectSP ValueObject::Cast(const CompilerType &compiler_type) { ExecutionContextScope *exe_scope = ExecutionContext(GetExecutionContextRef()).GetBestExecutionContextScope(); - if (compiler_type.GetByteSize(exe_scope) <= - GetCompilerType().GetByteSize(exe_scope) || + if (llvm::expectedToOptional(compiler_type.GetByteSize(exe_scope)) + .value_or(0) <= + llvm::expectedToOptional(GetCompilerType().GetByteSize(exe_scope)) + .value_or(0) || m_value.GetValueType() == Value::ValueType::LoadAddress) return DoCast(compiler_type); @@ -3225,9 +3231,10 @@ lldb::ValueObjectSP ValueObject::CastToBasicType(CompilerType type) { lldb::TargetSP target = GetTargetSP(); uint64_t type_byte_size = 0; uint64_t val_byte_size = 0; - if (auto temp = type.GetByteSize(target.get())) + if (auto temp = llvm::expectedToOptional(type.GetByteSize(target.get()))) type_byte_size = temp.value(); - if (auto temp = GetCompilerType().GetByteSize(target.get())) + if (auto temp = + llvm::expectedToOptional(GetCompilerType().GetByteSize(target.get()))) val_byte_size = temp.value(); if (is_pointer) { @@ -3377,7 +3384,7 @@ lldb::ValueObjectSP ValueObject::CastToEnumType(CompilerType type) { lldb::TargetSP target = GetTargetSP(); uint64_t byte_size = 0; - if (auto temp = type.GetByteSize(target.get())) + if (auto temp = llvm::expectedToOptional(type.GetByteSize(target.get()))) byte_size = temp.value(); if (is_float) { @@ -3653,7 +3660,7 @@ ValueObject::CreateValueObjectFromAPInt(lldb::TargetSP target, llvm::StringRef name) { ExecutionContext exe_ctx(target.get(), false); uint64_t byte_size = 0; - if (auto temp = type.GetByteSize(target.get())) + if (auto temp = llvm::expectedToOptional(type.GetByteSize(target.get()))) byte_size = temp.value(); lldb::DataExtractorSP data_sp = std::make_shared<DataExtractor>( reinterpret_cast<const void *>(v.getRawData()), byte_size, @@ -3681,7 +3688,8 @@ ValueObject::CreateValueObjectFromBool(lldb::TargetSP target, bool value, } ExecutionContext exe_ctx(target.get(), false); uint64_t byte_size = 0; - if (auto temp = target_type.GetByteSize(target.get())) + if (auto temp = + llvm::expectedToOptional(target_type.GetByteSize(target.get()))) byte_size = temp.value(); lldb::DataExtractorSP data_sp = std::make_shared<DataExtractor>( reinterpret_cast<const void *>(&value), byte_size, exe_ctx.GetByteOrder(), @@ -3699,7 +3707,7 @@ lldb::ValueObjectSP ValueObject::CreateValueObjectFromNullptr( uintptr_t zero = 0; ExecutionContext exe_ctx(target.get(), false); uint64_t byte_size = 0; - if (auto temp = type.GetByteSize(target.get())) + if (auto temp = llvm::expectedToOptional(type.GetByteSize(target.get()))) byte_size = temp.value(); lldb::DataExtractorSP data_sp = std::make_shared<DataExtractor>( reinterpret_cast<const void *>(zero), byte_size, exe_ctx.GetByteOrder(), diff --git a/lldb/source/ValueObject/ValueObjectCast.cpp b/lldb/source/ValueObject/ValueObjectCast.cpp index 6241f23..de2d7fa 100644 --- a/lldb/source/ValueObject/ValueObjectCast.cpp +++ b/lldb/source/ValueObject/ValueObjectCast.cpp @@ -49,7 +49,7 @@ llvm::Expected<uint32_t> ValueObjectCast::CalculateNumChildren(uint32_t max) { return *children_count <= max ? *children_count : max; } -std::optional<uint64_t> ValueObjectCast::GetByteSize() { +llvm::Expected<uint64_t> ValueObjectCast::GetByteSize() { ExecutionContext exe_ctx(GetExecutionContextRef()); return m_value.GetValueByteSize(nullptr, &exe_ctx); } diff --git a/lldb/source/ValueObject/ValueObjectConstResult.cpp b/lldb/source/ValueObject/ValueObjectConstResult.cpp index ba4f7aa..01f87052 100644 --- a/lldb/source/ValueObject/ValueObjectConstResult.cpp +++ b/lldb/source/ValueObject/ValueObjectConstResult.cpp @@ -203,14 +203,18 @@ lldb::ValueType ValueObjectConstResult::GetValueType() const { return eValueTypeConstResult; } -std::optional<uint64_t> ValueObjectConstResult::GetByteSize() { +llvm::Expected<uint64_t> ValueObjectConstResult::GetByteSize() { ExecutionContext exe_ctx(GetExecutionContextRef()); if (!m_byte_size) { - if (auto size = GetCompilerType().GetByteSize( - exe_ctx.GetBestExecutionContextScope())) - SetByteSize(*size); + auto size_or_err = + GetCompilerType().GetByteSize(exe_ctx.GetBestExecutionContextScope()); + if (!size_or_err) + return size_or_err; + SetByteSize(*size_or_err); } - return m_byte_size; + if (m_byte_size) + return *m_byte_size; + return llvm::createStringError("unknown size of const result"); } void ValueObjectConstResult::SetByteSize(size_t size) { m_byte_size = size; } diff --git a/lldb/source/ValueObject/ValueObjectDynamicValue.cpp b/lldb/source/ValueObject/ValueObjectDynamicValue.cpp index ecd663a..4c2cf07 100644 --- a/lldb/source/ValueObject/ValueObjectDynamicValue.cpp +++ b/lldb/source/ValueObject/ValueObjectDynamicValue.cpp @@ -98,7 +98,7 @@ ValueObjectDynamicValue::CalculateNumChildren(uint32_t max) { return m_parent->GetNumChildren(max); } -std::optional<uint64_t> ValueObjectDynamicValue::GetByteSize() { +llvm::Expected<uint64_t> ValueObjectDynamicValue::GetByteSize() { const bool success = UpdateValueIfNeeded(false); if (success && m_dynamic_type_info.HasType()) { ExecutionContext exe_ctx(GetExecutionContextRef()); @@ -248,7 +248,8 @@ bool ValueObjectDynamicValue::UpdateValue() { // If we found a host address but it doesn't fit in the buffer, there's // nothing we can do. if (local_buffer.size() < - m_dynamic_type_info.GetCompilerType().GetByteSize(exe_scope)) { + llvm::expectedToOptional( + m_dynamic_type_info.GetCompilerType().GetByteSize(exe_scope))) { SetValueIsValid(false); return false; } diff --git a/lldb/source/ValueObject/ValueObjectMemory.cpp b/lldb/source/ValueObject/ValueObjectMemory.cpp index e2b5e8b..3d8d80c 100644 --- a/lldb/source/ValueObject/ValueObjectMemory.cpp +++ b/lldb/source/ValueObject/ValueObjectMemory.cpp @@ -143,10 +143,14 @@ llvm::Expected<uint32_t> ValueObjectMemory::CalculateNumChildren(uint32_t max) { return *child_count <= max ? *child_count : max; } -std::optional<uint64_t> ValueObjectMemory::GetByteSize() { +llvm::Expected<uint64_t> ValueObjectMemory::GetByteSize() { ExecutionContext exe_ctx(GetExecutionContextRef()); - if (m_type_sp) - return m_type_sp->GetByteSize(exe_ctx.GetBestExecutionContextScope()); + if (m_type_sp) { + if (auto size = + m_type_sp->GetByteSize(exe_ctx.GetBestExecutionContextScope())) + return *size; + return llvm::createStringError("could not get byte size of memory object"); + } return m_compiler_type.GetByteSize(exe_ctx.GetBestExecutionContextScope()); } diff --git a/lldb/source/ValueObject/ValueObjectRegister.cpp b/lldb/source/ValueObject/ValueObjectRegister.cpp index 805c921..a56d316 100644 --- a/lldb/source/ValueObject/ValueObjectRegister.cpp +++ b/lldb/source/ValueObject/ValueObjectRegister.cpp @@ -84,7 +84,7 @@ ValueObjectRegisterSet::CalculateNumChildren(uint32_t max) { return 0; } -std::optional<uint64_t> ValueObjectRegisterSet::GetByteSize() { return 0; } +llvm::Expected<uint64_t> ValueObjectRegisterSet::GetByteSize() { return 0; } bool ValueObjectRegisterSet::UpdateValue() { m_error.Clear(); @@ -226,7 +226,7 @@ ValueObjectRegister::CalculateNumChildren(uint32_t max) { return *children_count <= max ? *children_count : max; } -std::optional<uint64_t> ValueObjectRegister::GetByteSize() { +llvm::Expected<uint64_t> ValueObjectRegister::GetByteSize() { return m_reg_info.byte_size; } diff --git a/lldb/source/ValueObject/ValueObjectSyntheticFilter.cpp b/lldb/source/ValueObject/ValueObjectSyntheticFilter.cpp index fbb329b..d49c27f 100644 --- a/lldb/source/ValueObject/ValueObjectSyntheticFilter.cpp +++ b/lldb/source/ValueObject/ValueObjectSyntheticFilter.cpp @@ -133,7 +133,7 @@ bool ValueObjectSynthetic::MightHaveChildren() { return (m_might_have_children != eLazyBoolNo); } -std::optional<uint64_t> ValueObjectSynthetic::GetByteSize() { +llvm::Expected<uint64_t> ValueObjectSynthetic::GetByteSize() { return m_parent->GetByteSize(); } diff --git a/lldb/source/ValueObject/ValueObjectVTable.cpp b/lldb/source/ValueObject/ValueObjectVTable.cpp index 71711543..92bd086 100644 --- a/lldb/source/ValueObject/ValueObjectVTable.cpp +++ b/lldb/source/ValueObject/ValueObjectVTable.cpp @@ -31,7 +31,7 @@ public: ~ValueObjectVTableChild() override = default; - std::optional<uint64_t> GetByteSize() override { return m_addr_size; }; + llvm::Expected<uint64_t> GetByteSize() override { return m_addr_size; }; llvm::Expected<uint32_t> CalculateNumChildren(uint32_t max) override { return 0; @@ -154,10 +154,10 @@ ValueObjectVTable::ValueObjectVTable(ValueObject &parent) SetFormat(eFormatPointer); } -std::optional<uint64_t> ValueObjectVTable::GetByteSize() { +llvm::Expected<uint64_t> ValueObjectVTable::GetByteSize() { if (m_vtable_symbol) return m_vtable_symbol->GetByteSize(); - return std::nullopt; + return llvm::createStringError("no symbol for vtable"); } llvm::Expected<uint32_t> ValueObjectVTable::CalculateNumChildren(uint32_t max) { diff --git a/lldb/source/ValueObject/ValueObjectVariable.cpp b/lldb/source/ValueObject/ValueObjectVariable.cpp index 6a482b9..12a84f9 100644 --- a/lldb/source/ValueObject/ValueObjectVariable.cpp +++ b/lldb/source/ValueObject/ValueObjectVariable.cpp @@ -110,14 +110,10 @@ ValueObjectVariable::CalculateNumChildren(uint32_t max) { return *child_count <= max ? *child_count : max; } -std::optional<uint64_t> ValueObjectVariable::GetByteSize() { +llvm::Expected<uint64_t> ValueObjectVariable::GetByteSize() { ExecutionContext exe_ctx(GetExecutionContextRef()); CompilerType type(GetCompilerType()); - - if (!type.IsValid()) - return {}; - return type.GetByteSize(exe_ctx.GetBestExecutionContextScope()); } |