From 878a64f94a264ea4b564d6063614ddb0b5da3f6c Mon Sep 17 00:00:00 2001 From: Adrian Prantl Date: Wed, 5 Mar 2025 10:21:19 -0800 Subject: [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 --- lldb/source/Commands/CommandObjectMemory.cpp | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) (limited to 'lldb/source/Commands/CommandObjectMemory.cpp') 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 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 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(total_byte_size, '\0'); @@ -1034,8 +1035,8 @@ protected: frame, result_sp)) && result_sp) { uint64_t value = result_sp->GetValueAsUnsigned(0); - std::optional size = - result_sp->GetCompilerType().GetByteSize(nullptr); + std::optional size = llvm::expectedToOptional( + result_sp->GetCompilerType().GetByteSize(nullptr)); if (!size) return; switch (*size) { -- cgit v1.1