From a39f7bbf3706330bca101a96ba6a88dd801456b7 Mon Sep 17 00:00:00 2001 From: Adrian Prantl Date: Mon, 3 Mar 2025 14:05:37 -0800 Subject: [PATCH] [lldb] Upgrade CompilerType::GetBitSize to return llvm::Expected 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/Expression/ExpressionVariable.h | 2 +- lldb/include/lldb/Symbol/CompilerType.h | 4 +- lldb/include/lldb/Symbol/Type.h | 2 +- lldb/include/lldb/Symbol/TypeSystem.h | 2 +- .../lldb/Target/StackFrameRecognizer.h | 2 +- lldb/include/lldb/ValueObject/ValueObject.h | 2 +- .../lldb/ValueObject/ValueObjectCast.h | 2 +- .../lldb/ValueObject/ValueObjectChild.h | 2 +- .../lldb/ValueObject/ValueObjectConstResult.h | 2 +- .../ValueObject/ValueObjectDynamicValue.h | 2 +- .../lldb/ValueObject/ValueObjectMemory.h | 2 +- .../lldb/ValueObject/ValueObjectRegister.h | 4 +- .../ValueObject/ValueObjectSyntheticFilter.h | 2 +- .../lldb/ValueObject/ValueObjectVTable.h | 2 +- .../lldb/ValueObject/ValueObjectVariable.h | 2 +- lldb/source/API/SBType.cpp | 4 +- lldb/source/API/SBValue.cpp | 2 +- lldb/source/Commands/CommandObjectMemory.cpp | 25 ++--- .../Commands/CommandObjectWatchpoint.cpp | 10 +- lldb/source/Core/Value.cpp | 17 +++- lldb/source/DataFormatters/TypeFormat.cpp | 10 +- lldb/source/DataFormatters/VectorType.cpp | 17 ++-- lldb/source/Expression/ExpressionVariable.cpp | 3 +- lldb/source/Expression/Materializer.cpp | 76 +++++++++------ lldb/source/Host/macosx/objcxx/Host.mm | 2 +- .../Plugins/ABI/AArch64/ABIMacOSX_arm64.cpp | 14 +-- .../Plugins/ABI/AArch64/ABISysV_arm64.cpp | 14 +-- lldb/source/Plugins/ABI/ARM/ABIMacOSX_arm.cpp | 8 +- lldb/source/Plugins/ABI/ARM/ABISysV_arm.cpp | 14 ++- .../ABI/LoongArch/ABISysV_loongarch.cpp | 3 +- lldb/source/Plugins/ABI/Mips/ABISysV_mips.cpp | 3 +- .../Plugins/ABI/Mips/ABISysV_mips64.cpp | 9 +- .../Plugins/ABI/PowerPC/ABISysV_ppc.cpp | 16 ++-- .../Plugins/ABI/PowerPC/ABISysV_ppc64.cpp | 13 ++- .../Plugins/ABI/RISCV/ABISysV_riscv.cpp | 3 +- .../Plugins/ABI/SystemZ/ABISysV_s390x.cpp | 9 +- .../source/Plugins/ABI/X86/ABIMacOSX_i386.cpp | 6 +- lldb/source/Plugins/ABI/X86/ABISysV_i386.cpp | 7 +- .../source/Plugins/ABI/X86/ABISysV_x86_64.cpp | 21 +++-- .../Plugins/ABI/X86/ABIWindows_x86_64.cpp | 16 ++-- .../ExpressionParser/Clang/IRForTarget.cpp | 15 +-- .../Language/CPlusPlus/CxxStringTypes.cpp | 6 +- .../Language/CPlusPlus/GenericBitset.cpp | 8 +- .../Plugins/Language/CPlusPlus/LibCxx.cpp | 4 +- .../CPlusPlus/LibCxxInitializerList.cpp | 7 +- .../Language/CPlusPlus/LibCxxProxyArray.cpp | 6 +- .../Language/CPlusPlus/LibCxxSliceArray.cpp | 3 +- .../Plugins/Language/CPlusPlus/LibCxxSpan.cpp | 7 +- .../Language/CPlusPlus/LibCxxValarray.cpp | 3 +- .../Language/CPlusPlus/LibCxxVariant.cpp | 11 ++- .../Language/CPlusPlus/LibCxxVector.cpp | 10 +- .../Plugins/Language/CPlusPlus/LibStdcpp.cpp | 3 +- .../CPlusPlus/LibStdcppUniquePointer.cpp | 3 +- .../AppleObjCClassDescriptorV2.cpp | 2 +- .../RegisterTypeBuilderClang.cpp | 3 +- .../Plugins/SymbolFile/CTF/SymbolFileCTF.cpp | 14 ++- .../SymbolFile/DWARF/DWARFASTParserClang.cpp | 30 +++--- .../SymbolFile/DWARF/SymbolFileDWARF.cpp | 13 ++- .../NativePDB/SymbolFileNativePDB.cpp | 24 ++--- .../TypeSystem/Clang/TypeSystemClang.cpp | 94 ++++++++++--------- .../TypeSystem/Clang/TypeSystemClang.h | 20 ++-- lldb/source/Symbol/CompilerType.cpp | 55 ++++++----- lldb/source/Symbol/Type.cpp | 29 +++--- lldb/source/Target/StackFrame.cpp | 17 +++- lldb/source/Target/Thread.cpp | 9 +- lldb/source/ValueObject/ValueObject.cpp | 52 +++++----- lldb/source/ValueObject/ValueObjectCast.cpp | 2 +- .../ValueObject/ValueObjectConstResult.cpp | 14 ++- .../ValueObject/ValueObjectDynamicValue.cpp | 5 +- lldb/source/ValueObject/ValueObjectMemory.cpp | 10 +- .../ValueObject/ValueObjectRegister.cpp | 4 +- .../ValueObjectSyntheticFilter.cpp | 2 +- lldb/source/ValueObject/ValueObjectVTable.cpp | 6 +- .../ValueObject/ValueObjectVariable.cpp | 6 +- .../DWARF/x86/class-type-nullptr-deref.s | 2 +- .../DWARF/x86/debug-types-signature-loop.s | 2 +- .../Platform/PlatformSiginfoTest.cpp | 3 +- 77 files changed, 505 insertions(+), 355 deletions(-) diff --git a/lldb/include/lldb/Expression/ExpressionVariable.h b/lldb/include/lldb/Expression/ExpressionVariable.h index f5bd938921966..68fa1c878a0e3 100644 --- a/lldb/include/lldb/Expression/ExpressionVariable.h +++ b/lldb/include/lldb/Expression/ExpressionVariable.h @@ -33,7 +33,7 @@ class ExpressionVariable virtual ~ExpressionVariable() = default; - std::optional GetByteSize() { return m_frozen_sp->GetByteSize(); } + llvm::Expected GetByteSize() { return m_frozen_sp->GetByteSize(); } ConstString GetName() { return m_frozen_sp->GetName(); } diff --git a/lldb/include/lldb/Symbol/CompilerType.h b/lldb/include/lldb/Symbol/CompilerType.h index fe4fcbccee370..41a1676dabd76 100644 --- a/lldb/include/lldb/Symbol/CompilerType.h +++ b/lldb/include/lldb/Symbol/CompilerType.h @@ -391,9 +391,9 @@ class CompilerType { struct IntegralTemplateArgument; /// Return the size of the type in bytes. - std::optional GetByteSize(ExecutionContextScope *exe_scope) const; + llvm::Expected GetByteSize(ExecutionContextScope *exe_scope) const; /// Return the size of the type in bits. - std::optional GetBitSize(ExecutionContextScope *exe_scope) const; + llvm::Expected GetBitSize(ExecutionContextScope *exe_scope) const; lldb::Encoding GetEncoding(uint64_t &count) const; diff --git a/lldb/include/lldb/Symbol/Type.h b/lldb/include/lldb/Symbol/Type.h index 91188fe6ea483..e657357b942f1 100644 --- a/lldb/include/lldb/Symbol/Type.h +++ b/lldb/include/lldb/Symbol/Type.h @@ -480,7 +480,7 @@ class Type : public std::enable_shared_from_this, public UserID { ConstString GetBaseName(); - std::optional GetByteSize(ExecutionContextScope *exe_scope); + llvm::Expected GetByteSize(ExecutionContextScope *exe_scope); llvm::Expected GetNumChildren(bool omit_empty_base_classes); diff --git a/lldb/include/lldb/Symbol/TypeSystem.h b/lldb/include/lldb/Symbol/TypeSystem.h index b06bfa583bad6..59fb066e087d3 100644 --- a/lldb/include/lldb/Symbol/TypeSystem.h +++ b/lldb/include/lldb/Symbol/TypeSystem.h @@ -312,7 +312,7 @@ class TypeSystem : public PluginInterface, virtual const llvm::fltSemantics &GetFloatTypeSemantics(size_t byte_size) = 0; - virtual std::optional + virtual llvm::Expected GetBitSize(lldb::opaque_compiler_type_t type, ExecutionContextScope *exe_scope) = 0; diff --git a/lldb/include/lldb/Target/StackFrameRecognizer.h b/lldb/include/lldb/Target/StackFrameRecognizer.h index 8cf69a82eb8c0..eb603242e2c5e 100644 --- a/lldb/include/lldb/Target/StackFrameRecognizer.h +++ b/lldb/include/lldb/Target/StackFrameRecognizer.h @@ -180,7 +180,7 @@ class ValueObjectRecognizerSynthesizedValue : public ValueObject { SetName(parent.GetName()); } - std::optional GetByteSize() override { + llvm::Expected GetByteSize() override { return m_parent->GetByteSize(); } lldb::ValueType GetValueType() const override { return m_type; } diff --git a/lldb/include/lldb/ValueObject/ValueObject.h b/lldb/include/lldb/ValueObject/ValueObject.h index a0f53d20327cd..06d2589002ed0 100644 --- a/lldb/include/lldb/ValueObject/ValueObject.h +++ b/lldb/include/lldb/ValueObject/ValueObject.h @@ -357,7 +357,7 @@ class ValueObject { virtual bool CanProvideValue(); // Subclasses must implement the functions below. - virtual std::optional GetByteSize() = 0; + virtual llvm::Expected GetByteSize() = 0; virtual lldb::ValueType GetValueType() const = 0; diff --git a/lldb/include/lldb/ValueObject/ValueObjectCast.h b/lldb/include/lldb/ValueObject/ValueObjectCast.h index 9d174ae5ca609..e62c33a549a58 100644 --- a/lldb/include/lldb/ValueObject/ValueObjectCast.h +++ b/lldb/include/lldb/ValueObject/ValueObjectCast.h @@ -30,7 +30,7 @@ class ValueObjectCast : public ValueObject { static lldb::ValueObjectSP Create(ValueObject &parent, ConstString name, const CompilerType &cast_type); - std::optional GetByteSize() override; + llvm::Expected GetByteSize() override; llvm::Expected CalculateNumChildren(uint32_t max) override; diff --git a/lldb/include/lldb/ValueObject/ValueObjectChild.h b/lldb/include/lldb/ValueObject/ValueObjectChild.h index e8c974a3a10a7..a386f726b9dda 100644 --- a/lldb/include/lldb/ValueObject/ValueObjectChild.h +++ b/lldb/include/lldb/ValueObject/ValueObjectChild.h @@ -29,7 +29,7 @@ class ValueObjectChild : public ValueObject { public: ~ValueObjectChild() override; - std::optional GetByteSize() override { return m_byte_size; } + llvm::Expected GetByteSize() override { return m_byte_size; } lldb::offset_t GetByteOffset() override { return m_byte_offset; } diff --git a/lldb/include/lldb/ValueObject/ValueObjectConstResult.h b/lldb/include/lldb/ValueObject/ValueObjectConstResult.h index e4ed1f399bf6b..2ee531f5858e1 100644 --- a/lldb/include/lldb/ValueObject/ValueObjectConstResult.h +++ b/lldb/include/lldb/ValueObject/ValueObjectConstResult.h @@ -64,7 +64,7 @@ class ValueObjectConstResult : public ValueObject { static lldb::ValueObjectSP Create(ExecutionContextScope *exe_scope, Status &&error); - std::optional GetByteSize() override; + llvm::Expected GetByteSize() override; lldb::ValueType GetValueType() const override; diff --git a/lldb/include/lldb/ValueObject/ValueObjectDynamicValue.h b/lldb/include/lldb/ValueObject/ValueObjectDynamicValue.h index 145a46e295566..51a73b8062c22 100644 --- a/lldb/include/lldb/ValueObject/ValueObjectDynamicValue.h +++ b/lldb/include/lldb/ValueObject/ValueObjectDynamicValue.h @@ -35,7 +35,7 @@ class ValueObjectDynamicValue : public ValueObject { public: ~ValueObjectDynamicValue() override = default; - std::optional GetByteSize() override; + llvm::Expected GetByteSize() override; ConstString GetTypeName() override; diff --git a/lldb/include/lldb/ValueObject/ValueObjectMemory.h b/lldb/include/lldb/ValueObject/ValueObjectMemory.h index cfc36d0d610db..c1bd28434e324 100644 --- a/lldb/include/lldb/ValueObject/ValueObjectMemory.h +++ b/lldb/include/lldb/ValueObject/ValueObjectMemory.h @@ -41,7 +41,7 @@ class ValueObjectMemory : public ValueObject { const Address &address, const CompilerType &ast_type); - std::optional GetByteSize() override; + llvm::Expected GetByteSize() override; ConstString GetTypeName() override; diff --git a/lldb/include/lldb/ValueObject/ValueObjectRegister.h b/lldb/include/lldb/ValueObject/ValueObjectRegister.h index fafbfd0341115..0812dc575aaa1 100644 --- a/lldb/include/lldb/ValueObject/ValueObjectRegister.h +++ b/lldb/include/lldb/ValueObject/ValueObjectRegister.h @@ -37,7 +37,7 @@ class ValueObjectRegisterSet : public ValueObject { lldb::RegisterContextSP ®_ctx_sp, uint32_t set_idx); - std::optional GetByteSize() override; + llvm::Expected GetByteSize() override; lldb::ValueType GetValueType() const override { return lldb::eValueTypeRegisterSet; @@ -89,7 +89,7 @@ class ValueObjectRegister : public ValueObject { lldb::RegisterContextSP ®_ctx_sp, const RegisterInfo *reg_info); - std::optional GetByteSize() override; + llvm ::Expected GetByteSize() override; lldb::ValueType GetValueType() const override { return lldb::eValueTypeRegister; diff --git a/lldb/include/lldb/ValueObject/ValueObjectSyntheticFilter.h b/lldb/include/lldb/ValueObject/ValueObjectSyntheticFilter.h index 2811658fd8f1f..df205a258a997 100644 --- a/lldb/include/lldb/ValueObject/ValueObjectSyntheticFilter.h +++ b/lldb/include/lldb/ValueObject/ValueObjectSyntheticFilter.h @@ -37,7 +37,7 @@ class ValueObjectSynthetic : public ValueObject { public: ~ValueObjectSynthetic() override; - std::optional GetByteSize() override; + llvm::Expected GetByteSize() override; ConstString GetTypeName() override; diff --git a/lldb/include/lldb/ValueObject/ValueObjectVTable.h b/lldb/include/lldb/ValueObject/ValueObjectVTable.h index 9cf13be093a8d..618456cbd120c 100644 --- a/lldb/include/lldb/ValueObject/ValueObjectVTable.h +++ b/lldb/include/lldb/ValueObject/ValueObjectVTable.h @@ -62,7 +62,7 @@ class ValueObjectVTable : public ValueObject { static lldb::ValueObjectSP Create(ValueObject &parent); - std::optional GetByteSize() override; + llvm::Expected GetByteSize() override; llvm::Expected CalculateNumChildren(uint32_t max) override; diff --git a/lldb/include/lldb/ValueObject/ValueObjectVariable.h b/lldb/include/lldb/ValueObject/ValueObjectVariable.h index 9f66af808425a..16030cd4edbac 100644 --- a/lldb/include/lldb/ValueObject/ValueObjectVariable.h +++ b/lldb/include/lldb/ValueObject/ValueObjectVariable.h @@ -38,7 +38,7 @@ class ValueObjectVariable : public ValueObject { static lldb::ValueObjectSP Create(ExecutionContextScope *exe_scope, const lldb::VariableSP &var_sp); - std::optional GetByteSize() override; + llvm::Expected GetByteSize() override; ConstString GetTypeName() override; diff --git a/lldb/source/API/SBType.cpp b/lldb/source/API/SBType.cpp index 9eb1f0c75ea05..00f28717d97f3 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 size = - m_opaque_sp->GetCompilerType(false).GetByteSize(nullptr)) + if (std::optional 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 a707b9aa7589c..6b91120f6427a 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 f2fa5391f8f33..7140333bb3cde 100644 --- a/lldb/source/Commands/CommandObjectMemory.cpp +++ b/lldb/source/Commands/CommandObjectMemory.cpp @@ -519,14 +519,14 @@ class CommandObjectMemoryRead : public CommandObjectParsed { --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 @@ class CommandObjectMemoryRead : public CommandObjectParsed { 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 @@ class CommandObjectMemoryFind : public CommandObjectParsed { 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) { diff --git a/lldb/source/Commands/CommandObjectWatchpoint.cpp b/lldb/source/Commands/CommandObjectWatchpoint.cpp index 766d650a2ca07..20f4b91f15340 100644 --- a/lldb/source/Commands/CommandObjectWatchpoint.cpp +++ b/lldb/source/Commands/CommandObjectWatchpoint.cpp @@ -867,9 +867,10 @@ corresponding to the byte size of the data type."); 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 @@ class CommandObjectWatchpointSetExpression : public CommandObjectRaw { /// of the expression, so convert to that if we found a valid type. CompilerType compiler_type(valobj_sp->GetCompilerType()); - std::optional valobj_size = valobj_sp->GetByteSize(); + std::optional 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 70299cb8455a1..c91b3f852f986 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 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 type_size = ast_type.GetByteSize( - exe_ctx ? exe_ctx->GetBestExecutionContextScope() : nullptr); + std::optional 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 409c452110bdd..f4cb8b46d272a 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 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 fa3fb1b674efb..162b075ec87d2 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 CalculateNumChildren(CompilerType container_elem_type, uint64_t num_elements, CompilerType element_type) { - std::optional container_elem_size = - container_elem_type.GetByteSize(/* exe_scope */ nullptr); + std::optional 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 element_size = - element_type.GetByteSize(/* exe_scope */ nullptr); + std::optional element_size = llvm::expectedToOptional( + element_type.GetByteSize(/* exe_scope */ nullptr)); if (!element_size || !*element_size) return {}; @@ -236,10 +236,11 @@ class VectorTypeSyntheticFrontEnd : public SyntheticChildrenFrontEnd { nullptr, Status::FromError(num_children_or_err.takeError())); if (idx >= *num_children_or_err) return {}; - std::optional 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 f0a28988822fa..9e8ea60f8e052 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 byte_size = m_frozen_sp->GetByteSize(); + std::optional 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 13a72a9921e1d..8d48b5e50041c 100644 --- a/lldb/source/Expression/Materializer.cpp +++ b/lldb/source/Expression/Materializer.cpp @@ -78,8 +78,9 @@ class EntityPersistentVariable : public Materializer::Entity { 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 @@ class EntityPersistentVariable : public Materializer::Entity { 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 @@ class EntityPersistentVariable : public Materializer::Entity { 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 @@ class EntityPersistentVariable : public Materializer::Entity { 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 @@ class EntityPersistentVariable : public Materializer::Entity { 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 @@ class EntityPersistentVariable : public Materializer::Entity { if (!err.Success()) { dump_stream.Printf(" \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(" \n"); @@ -529,7 +540,8 @@ class EntityVariableBase : public Materializer::Entity { 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 @@ class EntityVariableBase : public Materializer::Entity { 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 @@ class EntityVariableBase : public Materializer::Entity { 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 @@ class EntityVariableBase : public Materializer::Entity { /// /// \returns On success, returns byte size of the type associated /// with this variable. Returns std::nullopt otherwise. - virtual std::optional + virtual llvm::Expected GetByteSize(ExecutionContextScope *scope) const = 0; /// Returns 'true' if the location expression associated with this variable @@ -817,7 +832,7 @@ class EntityVariable : public EntityVariableBase { return ValueObjectVariable::Create(scope, m_variable_sp); } - std::optional + llvm::Expected GetByteSize(ExecutionContextScope *scope) const override { return m_variable_sp->GetType()->GetByteSize(scope); } @@ -860,12 +875,12 @@ class EntityValueObject : public EntityVariableBase { return m_valobj_sp; } - std::optional + llvm::Expected 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 @@ class EntityResultVariable : public Materializer::Entity { if (!exe_scope) exe_scope = map.GetBestExecutionContextScope(); - std::optional 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 opt_bit_align = m_type.GetTypeBitAlign(exe_scope); if (!opt_bit_align) { @@ -958,10 +973,10 @@ class EntityResultVariable : public Materializer::Entity { 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 @@ class EntityResultVariable : public Materializer::Entity { 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 5b3d04a6b587b..bb270f6a44e43 100644 --- a/lldb/source/Host/macosx/objcxx/Host.mm +++ b/lldb/source/Host/macosx/objcxx/Host.mm @@ -1471,7 +1471,7 @@ static bool ShouldLaunchUsingXPC(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 d208c6f874692..4b3018bb40a49 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 bit_size = value_type.GetBitSize(&thread); + std::optional 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 byte_size = - value_type.GetByteSize(exe_ctx.GetBestExecutionContextScope()); + std::optional 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 base_byte_size = - base_type.GetByteSize(exe_ctx.GetBestExecutionContextScope()); + std::optional 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 byte_size = return_compiler_type.GetByteSize(&thread); + std::optional 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 25803c9799ce4..ca794cd604fb1 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 bit_size = value_type.GetBitSize(&thread); + std::optional 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 byte_size = - value_type.GetByteSize(exe_ctx.GetBestExecutionContextScope()); + std::optional 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 base_byte_size = - base_type.GetByteSize(exe_ctx.GetBestExecutionContextScope()); + std::optional 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 byte_size = return_compiler_type.GetByteSize(&thread); + std::optional 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 d85f4a5943acf..27d0474cc9d19 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 bit_size = compiler_type.GetBitSize(&thread); + std::optional 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 bit_width = compiler_type.GetBitSize(&thread); + std::optional 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 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 38f4413bb263b..cf051b48e3fc1 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 size = compiler_type.GetBitSize(&thread)) + if (std::optional 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 bit_width = compiler_type.GetBitSize(&thread); - std::optional byte_size = compiler_type.GetByteSize(&thread); + std::optional bit_width = + llvm::expectedToOptional(compiler_type.GetBitSize(&thread)); + std::optional 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 base_byte_size = base_type.GetByteSize(&thread); + std::optional 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 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 1dd6070ec29c5..14ef8a0177aac 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 d7ebe22c90261..a2b60a07e9ca2 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 bit_width = return_compiler_type.GetBitSize(&thread); + std::optional 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 dcaef20b1a036..763d6140558f6 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 byte_size = return_compiler_type.GetByteSize(&thread); + std::optional 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 field_byte_width = - field_compiler_type.GetByteSize(&thread); + std::optional 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 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 405ba57deba83..93e1fa48039fe 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 bit_size = compiler_type.GetBitSize(&thread); + std::optional 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 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 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 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 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 bit_width = return_compiler_type.GetBitSize(&thread); + std::optional 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 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 0392dab3f118b..8e9b56ddc2efe 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 bit_size = compiler_type.GetBitSize(&thread); + std::optional 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 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 @@ class ReturnValueExtractor { 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 @@ class ReturnValueExtractor { DataExtractor de(&raw_data, sizeof(raw_data), m_byte_order, m_addr_size); lldb::offset_t offset = 0; - std::optional byte_size = type.GetByteSize(m_process_sp.get()); + std::optional byte_size = + llvm::expectedToOptional(type.GetByteSize(m_process_sp.get())); if (!byte_size) return {}; switch (*byte_size) { @@ -784,7 +787,7 @@ class ReturnValueExtractor { if (m_type.IsHomogeneousAggregate(&elem_type)) { uint32_t type_flags = elem_type.GetTypeInfo(); std::optional 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 06415a6e7f52c..99263ce391f34 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 a88a3b0e0825d..89a8381f3fe4a 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 bit_size = compiler_type.GetBitSize(&thread); + std::optional 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 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 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 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 d484b781a5136..5ede8d2c1db23 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 bit_size = compiler_type.GetBitSize(&thread); + std::optional 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 bit_width = compiler_type.GetBitSize(&thread); + std::optional 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 f6b3666632728..bf3547085052a 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 bit_size = compiler_type.GetBitSize(&thread); + std::optional 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 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 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 a224b3b9d4c23..c6dba91cd4613 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 bit_size = compiler_type.GetBitSize(&thread); + std::optional 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 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 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 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 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 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 bit_width = return_compiler_type.GetBitSize(&thread); + std::optional 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 8c6cea679fbd2..04f0bf112ae64 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 bit_size = compiler_type.GetBitSize(&thread); + std::optional 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 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 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 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 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 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 bit_width = return_compiler_type.GetBitSize(&thread); + std::optional 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 a414ad652448e..879f006336ba5 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 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 value_size = compiler_type.GetByteSize(target); + std::optional value_size = + llvm::expectedToOptional(compiler_type.GetByteSize(target)); if (!value_size) return false; std::optional 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 6d810cd30cd8c..fc17b76804d9f 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 size = wchar_compiler_type.GetBitSize(nullptr); + std::optional 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 size = wchar_compiler_type.GetBitSize(nullptr); + std::optional 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 934b456884ac0..03671c3efed6f 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 bit_size = - type.GetBitSize(ctx.GetBestExecutionContextScope()); + std::optional 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 bit_size = - type.GetBitSize(ctx.GetBestExecutionContextScope()); + std::optional 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 98e787dacc505..63620c6bf0ddd 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 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 cd13455a2e460..ffc894256626c 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 size = m_element_type.GetByteSize(nullptr)) { - m_element_size = *size; + llvm::Expected 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 fdb8f07ec4006..23fcff5e88bb2 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 size = m_element_type.GetByteSize(nullptr)) + if (std::optional 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 size = m_element_type_size_t.GetByteSize(nullptr)) + if (std::optional 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 523a7ab1001ec..dba80db3906f9 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 size = m_element_type.GetByteSize(nullptr)) + if (std::optional 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 21ee83041c065..acda41024cf03 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 size = m_element_type.GetByteSize(nullptr)) { - m_element_size = *size; + llvm::Expected 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 18c9c9b0e8710..53ad3d91a62a4 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 size = m_element_type.GetByteSize(nullptr)) + if (std::optional 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 c3cb1fdcb4251..701946d44cb40 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 index_type_bytes = index_type.GetByteSize(nullptr); - if (!index_type_bytes) - return LibcxxVariantIndexValidity::Invalid; - + llvm::Expected 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 ae3ed6326b45f..d538cac9f9134 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 size = m_element_type.GetByteSize(nullptr)) { - m_element_size = *size; + llvm::Expected 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 size = m_bool_type.GetByteSize(nullptr); + std::optional 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 127c0cd6666a8..a97264f007076 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 size = wchar_compiler_type.GetBitSize(nullptr); + std::optional 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 209aaced23c7d..722203f17e382 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 54c9f328b5b78..dac93931bab1b 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 1ecde7bee5820..f19dc8b1e6e58 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 0b8862f64ceb8..c0b931f5c131a 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 element_size = element_type->GetByteSize(nullptr); - if (!element_size) - return llvm::make_error( - 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 2d4d22559963f..0b632751574ad 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 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 @@ class DWARFASTParserClang::DelayedAddObjCClassProperty { static std::optional MakeAPValue(const clang::ASTContext &ast, CompilerType clang_type, uint64_t value) { - std::optional bit_width = clang_type.GetBitSize(nullptr); + std::optional 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 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 58b544a9a137b..5fb551cede245 100644 --- a/lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp +++ b/lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp @@ -2072,8 +2072,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())); } @@ -3617,9 +3618,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( diff --git a/lldb/source/Plugins/SymbolFile/NativePDB/SymbolFileNativePDB.cpp b/lldb/source/Plugins/SymbolFile/NativePDB/SymbolFileNativePDB.cpp index 4e472d0a0b0f2..ce0360120efeb 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 1e0c7f0514941..4ca4752310868 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 +llvm::Expected 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 +llvm::Expected 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 @@ -6301,12 +6306,14 @@ llvm::Expected 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 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 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 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 TypeSystemClang::GetChildCompilerTypeAtIndex( // We have a pointer to an simple type if (idx == 0 && pointee_clang_type.GetCompleteType()) { - if (std::optional 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 TypeSystemClang::GetChildCompilerTypeAtIndex( ::snprintf(element_name, sizeof(element_name), "[%" PRIu64 "]", static_cast(idx)); child_name.assign(element_name); - if (std::optional 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 TypeSystemClang::GetChildCompilerTypeAtIndex( CompilerType element_type = GetType(array->getElementType()); if (element_type.GetCompleteType()) { child_name = std::string(llvm::formatv("[{0}]", idx)); - if (std::optional 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 TypeSystemClang::GetChildCompilerTypeAtIndex( // We have a pointer to an simple type if (idx == 0) { - if (std::optional 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 TypeSystemClang::GetChildCompilerTypeAtIndex( // We have a pointer to an simple type if (idx == 0) { - if (std::optional 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 99d9becffd128..6579f7b68a9d2 100644 --- a/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.h +++ b/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.h @@ -827,15 +827,17 @@ class TypeSystemClang : public TypeSystem { const llvm::fltSemantics &GetFloatTypeSemantics(size_t byte_size) override; - std::optional GetByteSize(lldb::opaque_compiler_type_t type, - ExecutionContextScope *exe_scope) { - if (std::optional bit_size = GetBitSize(type, exe_scope)) - return (*bit_size + 7) / 8; - return std::nullopt; + llvm::Expected 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 GetBitSize(lldb::opaque_compiler_type_t type, - ExecutionContextScope *exe_scope) override; + llvm::Expected + 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 @@ class TypeSystemClang : public TypeSystem { /// on creation of a new instance. void LogCreation() const; - std::optional GetObjCBitSize(clang::QualType qual_type, - ExecutionContextScope *exe_scope); + llvm::Expected 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 09820fb3f0101..22fdd24fc7cd5 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 +llvm::Expected 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 +llvm::Expected CompilerType::GetByteSize(ExecutionContextScope *exe_scope) const { - if (std::optional 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 @@ -1104,10 +1107,18 @@ bool CompilerType::GetValueAsScalar(const lldb_private::DataExtractor &data, if (encoding == lldb::eEncodingInvalid || count != 1) return false; - std::optional 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 f7b44ade0da16..0a886e56100a1 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 Type::GetByteSize(ExecutionContextScope *exe_scope) { +llvm::Expected Type::GetByteSize(ExecutionContextScope *exe_scope) { if (m_byte_size_has_value) return static_cast(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 Type::GetByteSize(ExecutionContextScope *exe_scope) { Type *encoding_type = GetEncodingType(); if (encoding_type) if (std::optional 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(m_byte_size); } - if (std::optional size = - GetLayoutCompilerType().GetByteSize(exe_scope)) { - m_byte_size = *size; - m_byte_size_has_value = true; - return static_cast(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(m_byte_size); } break; // If we are a pointer or reference, then this is just a pointer size; @@ -498,7 +502,8 @@ std::optional Type::GetByteSize(ExecutionContextScope *exe_scope) { } } break; } - return {}; + return llvm::createStringError( + "could not get type size: unexpected encoding"); } llvm::Expected 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 f8061ffad1466..d92b7d8b9d899 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 50f7c73f2c4c1..cdadc14d2b6b4 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 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> 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 9d98f62c0379b..eac24353de90b 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 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 size = - type.GetByteSize(exe_ctx.GetBestExecutionContextScope()); + std::optional 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 size = - type.GetByteSize(exe_ctx.GetBestExecutionContextScope()); + std::optional 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( reinterpret_cast(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( reinterpret_cast(&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( reinterpret_cast(zero), byte_size, exe_ctx.GetByteOrder(), diff --git a/lldb/source/ValueObject/ValueObjectCast.cpp b/lldb/source/ValueObject/ValueObjectCast.cpp index 6241f23979365..de2d7fa97d6f4 100644 --- a/lldb/source/ValueObject/ValueObjectCast.cpp +++ b/lldb/source/ValueObject/ValueObjectCast.cpp @@ -49,7 +49,7 @@ llvm::Expected ValueObjectCast::CalculateNumChildren(uint32_t max) { return *children_count <= max ? *children_count : max; } -std::optional ValueObjectCast::GetByteSize() { +llvm::Expected 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 ba4f7aa244626..01f870529a14d 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 ValueObjectConstResult::GetByteSize() { +llvm::Expected 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 ecd663af68c2d..4c2cf0738d057 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 ValueObjectDynamicValue::GetByteSize() { +llvm::Expected 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 e2b5e8bfadf56..3d8d80c6ec480 100644 --- a/lldb/source/ValueObject/ValueObjectMemory.cpp +++ b/lldb/source/ValueObject/ValueObjectMemory.cpp @@ -143,10 +143,14 @@ llvm::Expected ValueObjectMemory::CalculateNumChildren(uint32_t max) { return *child_count <= max ? *child_count : max; } -std::optional ValueObjectMemory::GetByteSize() { +llvm::Expected 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 805c921bfc9b8..a56d3160d9ae2 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 ValueObjectRegisterSet::GetByteSize() { return 0; } +llvm::Expected 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 ValueObjectRegister::GetByteSize() { +llvm::Expected ValueObjectRegister::GetByteSize() { return m_reg_info.byte_size; } diff --git a/lldb/source/ValueObject/ValueObjectSyntheticFilter.cpp b/lldb/source/ValueObject/ValueObjectSyntheticFilter.cpp index fbb329b0896de..d49c27f0006bc 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 ValueObjectSynthetic::GetByteSize() { +llvm::Expected ValueObjectSynthetic::GetByteSize() { return m_parent->GetByteSize(); } diff --git a/lldb/source/ValueObject/ValueObjectVTable.cpp b/lldb/source/ValueObject/ValueObjectVTable.cpp index 7171154339f91..92bd086d88ee4 100644 --- a/lldb/source/ValueObject/ValueObjectVTable.cpp +++ b/lldb/source/ValueObject/ValueObjectVTable.cpp @@ -31,7 +31,7 @@ class ValueObjectVTableChild : public ValueObject { ~ValueObjectVTableChild() override = default; - std::optional GetByteSize() override { return m_addr_size; }; + llvm::Expected GetByteSize() override { return m_addr_size; }; llvm::Expected CalculateNumChildren(uint32_t max) override { return 0; @@ -154,10 +154,10 @@ ValueObjectVTable::ValueObjectVTable(ValueObject &parent) SetFormat(eFormatPointer); } -std::optional ValueObjectVTable::GetByteSize() { +llvm::Expected ValueObjectVTable::GetByteSize() { if (m_vtable_symbol) return m_vtable_symbol->GetByteSize(); - return std::nullopt; + return llvm::createStringError("no symbol for vtable"); } llvm::Expected ValueObjectVTable::CalculateNumChildren(uint32_t max) { diff --git a/lldb/source/ValueObject/ValueObjectVariable.cpp b/lldb/source/ValueObject/ValueObjectVariable.cpp index 6a482b91ad4be..12a84f9f2ed74 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 ValueObjectVariable::GetByteSize() { +llvm::Expected ValueObjectVariable::GetByteSize() { ExecutionContext exe_ctx(GetExecutionContextRef()); CompilerType type(GetCompilerType()); - - if (!type.IsValid()) - return {}; - return type.GetByteSize(exe_ctx.GetBestExecutionContextScope()); } diff --git a/lldb/test/Shell/SymbolFile/DWARF/x86/class-type-nullptr-deref.s b/lldb/test/Shell/SymbolFile/DWARF/x86/class-type-nullptr-deref.s index 4fbc1894c8c44..b47eba78e77cf 100644 --- a/lldb/test/Shell/SymbolFile/DWARF/x86/class-type-nullptr-deref.s +++ b/lldb/test/Shell/SymbolFile/DWARF/x86/class-type-nullptr-deref.s @@ -4,7 +4,7 @@ # RUN: llvm-mc --triple x86_64-pc-linux %s --filetype=obj -o %t # RUN: %lldb %t -o "target variable x" -o exit 2>&1 | FileCheck %s -# CHECK: Unable to determine byte size. +# CHECK: Invalid type: Cannot determine size # This tests a fix for a crash. If things are working we don't get a segfault. diff --git a/lldb/test/Shell/SymbolFile/DWARF/x86/debug-types-signature-loop.s b/lldb/test/Shell/SymbolFile/DWARF/x86/debug-types-signature-loop.s index 64b22830e8f28..73b85fc9fa44e 100644 --- a/lldb/test/Shell/SymbolFile/DWARF/x86/debug-types-signature-loop.s +++ b/lldb/test/Shell/SymbolFile/DWARF/x86/debug-types-signature-loop.s @@ -4,7 +4,7 @@ # RUN: ld.lld %t.o -o %t # RUN: %lldb %t -o "target variable e" -b | FileCheck %s -# CHECK: error: Unable to determine byte size. +# CHECK: error: Invalid type: Cannot determine size .type e,@object # @e .section .rodata,"a",@progbits diff --git a/lldb/unittests/Platform/PlatformSiginfoTest.cpp b/lldb/unittests/Platform/PlatformSiginfoTest.cpp index 54dbcb41beca3..2726357e65e57 100644 --- a/lldb/unittests/Platform/PlatformSiginfoTest.cpp +++ b/lldb/unittests/Platform/PlatformSiginfoTest.cpp @@ -68,7 +68,8 @@ class PlatformSiginfoTest : public ::testing::Test { } EXPECT_EQ(total_offset, offset * 8); - EXPECT_EQ(field_type.GetByteSize(nullptr), std::optional(size)); + EXPECT_EQ(llvm::expectedToOptional(field_type.GetByteSize(nullptr)), + std::optional(size)); } void ExpectFields(const CompilerType &container,