diff --git a/lldb/include/lldb/Expression/ExpressionVariable.h b/lldb/include/lldb/Expression/ExpressionVariable.h index f344e341ed717..178a2d9934005 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 7d88672a2d533..1185ef3aef479 100644 --- a/lldb/include/lldb/Symbol/CompilerType.h +++ b/lldb/include/lldb/Symbol/CompilerType.h @@ -418,9 +418,10 @@ 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; + /// Return the stride of the type in bits. std::optional GetByteStride(ExecutionContextScope *exe_scope) const; diff --git a/lldb/include/lldb/Symbol/Type.h b/lldb/include/lldb/Symbol/Type.h index bb2d9dc3de7fe..3608201dbf032 100644 --- a/lldb/include/lldb/Symbol/Type.h +++ b/lldb/include/lldb/Symbol/Type.h @@ -460,7 +460,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 db12271b7e869..5d8564ecfb182 100644 --- a/lldb/include/lldb/Symbol/TypeSystem.h +++ b/lldb/include/lldb/Symbol/TypeSystem.h @@ -341,7 +341,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 0f77e03bbe48f..7934a995a973d 100644 --- a/lldb/include/lldb/Target/StackFrameRecognizer.h +++ b/lldb/include/lldb/Target/StackFrameRecognizer.h @@ -177,7 +177,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 8a324846135ea..b41e20faf7c38 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 b9dda23abe6b3..da6550b96e032 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 e15ae4f9b3d22..231e6b3e37bbb 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 e5ff6b493f09e..944125f2d2eb4 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 b5612f21f1156..8fa7020d84908 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 fbeab97b8cabf..e1568b8eab245 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 bd93c04c16d24..d80dc89e68791 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/FormattersHelpers.cpp b/lldb/source/DataFormatters/FormattersHelpers.cpp index 123b29a47a272..3b0f7990cbd1b 100644 --- a/lldb/source/DataFormatters/FormattersHelpers.cpp +++ b/lldb/source/DataFormatters/FormattersHelpers.cpp @@ -285,7 +285,9 @@ bool lldb_private::formatters::FormatBoundsSafetyPointer( uint64_t pointee_byte_size = 0; if (compiler_type.IsPointerType(&pointee_type)) { pointee_byte_size = - pointee_type.GetByteSize(valobj.GetTargetSP().get()).value_or(0); + llvm::expectedToOptional( + pointee_type.GetByteSize(valobj.GetTargetSP().get())) + .value_or(0); } if (auto maybe_oob_info = GetOOBInfo(ptr, /*upper_bound=*/upper_bound, diff --git a/lldb/source/DataFormatters/TypeFormat.cpp b/lldb/source/DataFormatters/TypeFormat.cpp index 854917e86afd5..48fbf9ba7c305 100644 --- a/lldb/source/DataFormatters/TypeFormat.cpp +++ b/lldb/source/DataFormatters/TypeFormat.cpp @@ -99,13 +99,20 @@ bool TypeFormatImpl_Format::FormatObject(ValueObject *valobj, return false; } + 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 cba107b7da890..c7f59548ec235 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 9c9f9b2b2d8ba..ed283af45f7ad 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 1660f0a02a70f..d41705fd7eb32 100644 --- a/lldb/source/Expression/Materializer.cpp +++ b/lldb/source/Expression/Materializer.cpp @@ -79,8 +79,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()) { @@ -117,9 +118,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( @@ -247,7 +250,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 && @@ -292,7 +296,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 @@ -301,9 +306,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( @@ -384,12 +391,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"); @@ -534,9 +545,12 @@ class EntityVariableBase : public Materializer::Entity { valobj_sp->GetData(data, extract_error); if (!extract_error.Success()) { if (valobj_type.GetMinimumLanguage() == lldb::eLanguageTypeSwift) { - std::optional size = - valobj_type.GetByteSize(frame_sp.get()); - if (size && *size == 0) { + auto size_or_err = valobj_type.GetByteSize(frame_sp.get()); + if (!size_or_err) { + err = Status::FromError(size_or_err.takeError()); + return; + } + if (*size_or_err == 0) { // We don't need to materialize empty structs in Swift. return; } @@ -554,7 +568,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, " @@ -564,7 +579,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; @@ -663,14 +679,19 @@ 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()) { if (valobj_type.GetMinimumLanguage() == lldb::eLanguageTypeSwift) { - std::optional size = - valobj_type.GetByteSize(frame_sp.get()); - if (size && *size == 0) + auto size_or_err = valobj_type.GetByteSize(frame_sp.get()); + if (!size_or_err) { + err = Status::FromError(size_or_err.takeError()); + return; + } + if (*size_or_err == 0) // We don't need to dematerialize empty structs in Swift. return; } @@ -815,7 +836,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 @@ -856,7 +877,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); } @@ -899,12 +920,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 { @@ -976,12 +997,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) { @@ -997,10 +1018,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( @@ -1131,7 +1152,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 06817fbd67496..82a418dfeb2d3 100644 --- a/lldb/source/Host/macosx/objcxx/Host.mm +++ b/lldb/source/Host/macosx/objcxx/Host.mm @@ -1467,7 +1467,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 cb121c14048f0..49a9a42eab709 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; @@ -490,8 +491,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; @@ -508,8 +509,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; @@ -643,7 +644,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 93b8141e97ef8..ed66b579d580a 100644 --- a/lldb/source/Plugins/ABI/AArch64/ABISysV_arm64.cpp +++ b/lldb/source/Plugins/ABI/AArch64/ABISysV_arm64.cpp @@ -147,7 +147,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)) { @@ -464,8 +465,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; @@ -483,8 +484,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; @@ -613,7 +614,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/ARC/ABISysV_arc.cpp b/lldb/source/Plugins/ABI/ARC/ABISysV_arc.cpp index 6f60663ca51e1..6fd6633f95fb3 100644 --- a/lldb/source/Plugins/ABI/ARC/ABISysV_arc.cpp +++ b/lldb/source/Plugins/ABI/ARC/ABISysV_arc.cpp @@ -459,7 +459,9 @@ ABISysV_arc::GetReturnValueObjectSimple(Thread &thread, const uint32_t type_flags = compiler_type.GetTypeInfo(); // Integer return type. if (type_flags & eTypeIsInteger) { - 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); auto raw_value = ReadRawValue(reg_ctx, byte_size); const bool is_signed = (type_flags & eTypeIsSigned) != 0; @@ -483,7 +485,9 @@ ABISysV_arc::GetReturnValueObjectSimple(Thread &thread, if (compiler_type.IsFloatingPointType(float_count, is_complex) && 1 == float_count && !is_complex) { - 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); auto raw_value = ReadRawValue(reg_ctx, byte_size); if (!SetSizedFloat(value.GetScalar(), raw_value, byte_size)) diff --git a/lldb/source/Plugins/ABI/ARM/ABIMacOSX_arm.cpp b/lldb/source/Plugins/ABI/ARM/ABIMacOSX_arm.cpp index 08c613c7b0d0c..7e14a57a59f69 100644 --- a/lldb/source/Plugins/ABI/ARM/ABIMacOSX_arm.cpp +++ b/lldb/source/Plugins/ABI/ARM/ABIMacOSX_arm.cpp @@ -1453,7 +1453,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)) @@ -1559,7 +1560,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; @@ -1580,7 +1582,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 1a0e44f1936b8..4da411bb6df68 100644 --- a/lldb/source/Plugins/ABI/ARM/ABISysV_arm.cpp +++ b/lldb/source/Plugins/ABI/ARM/ABISysV_arm.cpp @@ -1459,7 +1459,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... @@ -1566,8 +1567,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; @@ -1703,7 +1706,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)) { @@ -1732,7 +1736,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/Mips/ABISysV_mips.cpp b/lldb/source/Plugins/ABI/Mips/ABISysV_mips.cpp index d21ee8ac04a21..1b2f3c91bca1d 100644 --- a/lldb/source/Plugins/ABI/Mips/ABISysV_mips.cpp +++ b/lldb/source/Plugins/ABI/Mips/ABISysV_mips.cpp @@ -807,7 +807,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 100d52bfd1c8b..d94098c8077ee 100644 --- a/lldb/source/Plugins/ABI/Mips/ABISysV_mips64.cpp +++ b/lldb/source/Plugins/ABI/Mips/ABISysV_mips64.cpp @@ -756,7 +756,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); @@ -965,8 +966,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; @@ -1038,7 +1039,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 e482b8d4c4c46..e66d01642e294 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 0aa51ef654274..368f51fc06242 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 6c96ac9817ab7..33c743a1e5b6f 100644 --- a/lldb/source/Plugins/ABI/RISCV/ABISysV_riscv.cpp +++ b/lldb/source/Plugins/ABI/RISCV/ABISysV_riscv.cpp @@ -544,7 +544,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 82853c96a0f74..3817a9ed2a289 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 f03acbcedb351..1d4b8ed2fbc7a 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 19ec5fa49bbea..0a1cceca65a19 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 97eb4c0fb73fc..920ba69d67e3f 100644 --- a/lldb/source/Plugins/ABI/X86/ABISysV_x86_64.cpp +++ b/lldb/source/Plugins/ABI/X86/ABISysV_x86_64.cpp @@ -268,7 +268,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; @@ -340,7 +341,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; @@ -410,7 +411,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( @@ -457,7 +458,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); @@ -497,7 +498,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); @@ -835,7 +836,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) { @@ -877,7 +878,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()) { @@ -975,7 +977,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 098293c1d6046..e024768b69d45 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; } @@ -673,7 +675,10 @@ ValueObjectSP ABIWindows_x86_64::GetReturnValueObjectImpl( uint32_t count; 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]; // this is unlikely w/o the overall size being greater than 8 bytes diff --git a/lldb/source/Plugins/ExpressionParser/Clang/IRForTarget.cpp b/lldb/source/Plugins/ExpressionParser/Clang/IRForTarget.cpp index b4884334236a1..bcc21f6e89bc3 100644 --- a/lldb/source/Plugins/ExpressionParser/Clang/IRForTarget.cpp +++ b/lldb/source/Plugins/ExpressionParser/Clang/IRForTarget.cpp @@ -301,16 +301,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; } @@ -325,7 +326,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 @@ -1038,7 +1040,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/ExpressionParser/Swift/SwiftExpressionParser.cpp b/lldb/source/Plugins/ExpressionParser/Swift/SwiftExpressionParser.cpp index ff3b8c39aa4ba..dcb2d3a8f135d 100644 --- a/lldb/source/Plugins/ExpressionParser/Swift/SwiftExpressionParser.cpp +++ b/lldb/source/Plugins/ExpressionParser/Swift/SwiftExpressionParser.cpp @@ -1048,9 +1048,12 @@ MaterializeVariable(SwiftASTManipulatorBase::VariableInfo &variable, // this check scattered in several places in the codebase, we should at // some point centralize it. lldb::StackFrameSP stack_frame_sp = stack_frame_wp.lock(); - std::optional size = + auto size_or_err = variable.GetType().GetByteSize(stack_frame_sp.get()); - if (repl && size && *size == 0) { + if (!size_or_err) + return size_or_err.takeError(); + uint64_t size = *size_or_err; + if (repl && size == 0) { auto &repl_mat = *llvm::cast(&materializer); offset = repl_mat.AddREPLResultVariable( variable.GetType(), variable.GetDecl(), diff --git a/lldb/source/Plugins/ExpressionParser/Swift/SwiftREPLMaterializer.cpp b/lldb/source/Plugins/ExpressionParser/Swift/SwiftREPLMaterializer.cpp index 18c27607cac9e..6a4d10c8d9939 100644 --- a/lldb/source/Plugins/ExpressionParser/Swift/SwiftREPLMaterializer.cpp +++ b/lldb/source/Plugins/ExpressionParser/Swift/SwiftREPLMaterializer.cpp @@ -158,7 +158,13 @@ class EntityREPLResultVariable : public Materializer::Entity { ret->ValueUpdated(); if (variable) { - const size_t pvar_byte_size = ret->GetByteSize().value_or(0); + auto pvar_byte_size_or_err = ret->GetByteSize(); + if (!pvar_byte_size_or_err) { + err = Status::FromError(pvar_byte_size_or_err.takeError()); + return; + } + + const uint64_t pvar_byte_size = *pvar_byte_size_or_err; uint8_t *pvar_data = ret->GetValueBytes(); Status read_error; @@ -226,9 +232,13 @@ class EntityREPLResultVariable : public Materializer::Entity { demangle_ctx.clear(); } - std::optional size = + auto size_or_err = m_type.GetByteSize(execution_unit->GetBestExecutionContextScope()); - if (size && *size == 0) { + if (!size_or_err) { + err = Status::FromError(size_or_err.takeError()); + return; + } + if (*size_or_err == 0) { MakeREPLResult(*execution_unit, err, nullptr); return; } @@ -413,10 +423,15 @@ class EntityREPLPersistentVariable : public Materializer::Entity { FixupResilientGlobal(var_addr, compiler_type, *execution_unit, exe_scope->CalculateProcess(), read_error); + auto size_or_err = m_persistent_variable_sp->GetByteSize(); + if (!size_or_err) { + err = Status::FromError(size_or_err.takeError()); + return; + } + // FIXME: This may not work if the value is not bitwise-takable. - execution_unit->ReadMemory( - m_persistent_variable_sp->GetValueBytes(), var_addr, - m_persistent_variable_sp->GetByteSize().value_or(0), read_error); + execution_unit->ReadMemory(m_persistent_variable_sp->GetValueBytes(), + var_addr, *size_or_err, read_error); if (!read_error.Success()) { err = Status::FromErrorStringWithFormatv( @@ -477,12 +492,13 @@ class EntityREPLPersistentVariable : public Materializer::Entity { if (!err.Success()) { dump_stream.Printf(" \n"); } else { - DataBufferHeap data(m_persistent_variable_sp->GetByteSize().value_or(0), - 0); + uint64_t size = + llvm::expectedToOptional(m_persistent_variable_sp->GetByteSize()) + .value_or(0); + + DataBufferHeap data(size, 0); - map.ReadMemory(data.GetBytes(), target_address, - m_persistent_variable_sp->GetByteSize().value_or(0), - err); + map.ReadMemory(data.GetBytes(), target_address, size, err); if (!err.Success()) { dump_stream.Printf(" \n"); 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 33955dccb6ccc..8f2a3fe9b6e01 100644 --- a/lldb/source/Plugins/Language/CPlusPlus/GenericBitset.cpp +++ b/lldb/source/Plugins/Language/CPlusPlus/GenericBitset.cpp @@ -111,8 +111,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); @@ -123,8 +123,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 d029855f6026d..3f22f1468a6f1 100644 --- a/lldb/source/Plugins/Language/CPlusPlus/LibCxx.cpp +++ b/lldb/source/Plugins/Language/CPlusPlus/LibCxx.cpp @@ -476,8 +476,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 67c6d1d3e5506..bbbdb44460329 100644 --- a/lldb/source/Plugins/Language/CPlusPlus/LibCxxInitializerList.cpp +++ b/lldb/source/Plugins/Language/CPlusPlus/LibCxxInitializerList.cpp @@ -90,8 +90,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 c659adbb9ab2e..72af329eb9649 100644 --- a/lldb/source/Plugins/Language/CPlusPlus/LibCxxProxyArray.cpp +++ b/lldb/source/Plugins/Language/CPlusPlus/LibCxxProxyArray.cpp @@ -139,7 +139,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) @@ -154,7 +155,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 5d607709d2c6f..48a7e885bd7a7 100644 --- a/lldb/source/Plugins/Language/CPlusPlus/LibCxxSliceArray.cpp +++ b/lldb/source/Plugins/Language/CPlusPlus/LibCxxSliceArray.cpp @@ -125,7 +125,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 15040295efe6d..6b4ddbfb7478f 100644 --- a/lldb/source/Plugins/Language/CPlusPlus/LibCxxSpan.cpp +++ b/lldb/source/Plugins/Language/CPlusPlus/LibCxxSpan.cpp @@ -104,8 +104,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 3f519f8c585f5..24a3a2ae8992d 100644 --- a/lldb/source/Plugins/Language/CPlusPlus/LibCxxValarray.cpp +++ b/lldb/source/Plugins/Language/CPlusPlus/LibCxxValarray.cpp @@ -106,7 +106,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 62794318e0777..910837f7c281b 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 eb65f1e57290e..0e5137bf1a722 100644 --- a/lldb/source/Plugins/Language/CPlusPlus/LibCxxVector.cpp +++ b/lldb/source/Plugins/Language/CPlusPlus/LibCxxVector.cpp @@ -132,8 +132,11 @@ lldb_private::formatters::LibcxxStdVectorSyntheticFrontEnd::Update() { return lldb::ChildCacheState::eRefetch; m_element_type = data_type_finder_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 0a1877471916d..11f728cc5f7df 100644 --- a/lldb/source/Plugins/Language/CPlusPlus/LibStdcpp.cpp +++ b/lldb/source/Plugins/Language/CPlusPlus/LibStdcpp.cpp @@ -317,7 +317,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 9447f7463f64a..593716cdd7c33 100644 --- a/lldb/source/Plugins/Language/CPlusPlus/LibStdcppUniquePointer.cpp +++ b/lldb/source/Plugins/Language/CPlusPlus/LibStdcppUniquePointer.cpp @@ -103,7 +103,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/Language/Swift/SwiftFormatters.cpp b/lldb/source/Plugins/Language/Swift/SwiftFormatters.cpp index 9361a5efd485d..f705b9df78357 100644 --- a/lldb/source/Plugins/Language/Swift/SwiftFormatters.cpp +++ b/lldb/source/Plugins/Language/Swift/SwiftFormatters.cpp @@ -1778,8 +1778,8 @@ bool lldb_private::formatters::swift::SIMDVector_SummaryProvider( return false; ExecutionContext exe_ctx = valobj.GetExecutionContextRef().Lock(true); - std::optional opt_type_size = - simd_type.GetByteSize(exe_ctx.GetBestExecutionContextScope()); + std::optional opt_type_size = llvm::expectedToOptional( + simd_type.GetByteSize(exe_ctx.GetBestExecutionContextScope())); if (!opt_type_size) return false; uint64_t type_size = *opt_type_size; @@ -1793,8 +1793,8 @@ bool lldb_private::formatters::swift::SIMDVector_SummaryProvider( if (!arg_type) return false; - std::optional opt_arg_size = - arg_type.GetByteSize(exe_ctx.GetBestExecutionContextScope()); + std::optional opt_arg_size = llvm::expectedToOptional( + arg_type.GetByteSize(exe_ctx.GetBestExecutionContextScope())); if (!opt_arg_size) return false; uint64_t arg_size = *opt_arg_size; 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/LanguageRuntime/Swift/SwiftLanguageRuntime.cpp b/lldb/source/Plugins/LanguageRuntime/Swift/SwiftLanguageRuntime.cpp index 8531da10b9831..10c48bd2ce46c 100644 --- a/lldb/source/Plugins/LanguageRuntime/Swift/SwiftLanguageRuntime.cpp +++ b/lldb/source/Plugins/LanguageRuntime/Swift/SwiftLanguageRuntime.cpp @@ -1761,8 +1761,8 @@ void SwiftLanguageRuntime::WillStartExecutingUserExpression( return; } ConstString BoolName("bool"); - std::optional bool_size = - ts->GetBuiltinTypeByName(BoolName).GetByteSize(nullptr); + std::optional bool_size = llvm::expectedToOptional( + ts->GetBuiltinTypeByName(BoolName).GetByteSize(nullptr)); if (!bool_size) return; @@ -1836,8 +1836,8 @@ void SwiftLanguageRuntime::DidFinishExecutingUserExpression( return; } ConstString BoolName("bool"); - std::optional bool_size = - ts->GetBuiltinTypeByName(BoolName).GetByteSize(nullptr); + std::optional bool_size = llvm::expectedToOptional( + ts->GetBuiltinTypeByName(BoolName).GetByteSize(nullptr)); if (!bool_size) return; diff --git a/lldb/source/Plugins/LanguageRuntime/Swift/SwiftLanguageRuntime.h b/lldb/source/Plugins/LanguageRuntime/Swift/SwiftLanguageRuntime.h index a376cf334d567..7096ff9e63e44 100644 --- a/lldb/source/Plugins/LanguageRuntime/Swift/SwiftLanguageRuntime.h +++ b/lldb/source/Plugins/LanguageRuntime/Swift/SwiftLanguageRuntime.h @@ -418,7 +418,7 @@ class SwiftLanguageRuntime : public LanguageRuntime { ExecutionContext *exe_ctx); /// Ask Remote Mirrors for the size of a Swift type. - std::optional GetBitSize(CompilerType type, + llvm::Expected GetBitSize(CompilerType type, ExecutionContextScope *exe_scope); /// Ask Remote mirrors for the stride of a Swift type. diff --git a/lldb/source/Plugins/LanguageRuntime/Swift/SwiftLanguageRuntimeDynamicTypeResolution.cpp b/lldb/source/Plugins/LanguageRuntime/Swift/SwiftLanguageRuntimeDynamicTypeResolution.cpp index efcf5b2630065..f1ebdbd995404 100644 --- a/lldb/source/Plugins/LanguageRuntime/Swift/SwiftLanguageRuntimeDynamicTypeResolution.cpp +++ b/lldb/source/Plugins/LanguageRuntime/Swift/SwiftLanguageRuntimeDynamicTypeResolution.cpp @@ -381,7 +381,8 @@ class LLDBTypeInfoProvider : public swift::remote::TypeInfoProvider { return nullptr; TypeSystemSwiftTypeRef &typesystem = *m_ts; // Build a TypeInfo for the Clang type. - auto size = clang_type.GetByteSize(exe_scope); + std::optional size = + llvm::expectedToOptional(clang_type.GetByteSize(exe_scope)); auto bit_align = clang_type.GetTypeBitAlign(exe_scope); std::vector fields; if (clang_type.IsAggregateType()) { @@ -1214,9 +1215,11 @@ llvm::Expected SwiftLanguageRuntime::GetChildCompilerTypeAtIndex( if (auto pack_element_type = ts->GetSILPackElementAtIndex(type, idx)) { llvm::raw_string_ostream os(child_name); os << '.' << idx; - child_byte_size = - GetBitSize(pack_element_type, exe_ctx.GetBestExecutionContextScope()) - .value_or(0); + auto size_or_err = + GetBitSize(pack_element_type, exe_ctx.GetBestExecutionContextScope()); + if (!size_or_err) + return size_or_err.takeError(); + child_byte_size = *size_or_err; int stack_dir = -1; child_byte_offset = ts->GetPointerByteSize() * idx * stack_dir; child_bitfield_bit_size = 0; @@ -2236,7 +2239,9 @@ bool SwiftLanguageRuntime::GetDynamicTypeAndAddress_Existential( if (use_local_buffer) - PushLocalBuffer(existential_address, in_value.GetByteSize().value_or(0)); + PushLocalBuffer( + existential_address, + llvm::expectedToOptional(in_value.GetByteSize()).value_or(0)); swift::remote::RemoteAddress remote_existential(existential_address); @@ -2522,7 +2527,7 @@ CompilerType SwiftLanguageRuntime::BindGenericTypeParameters( LLDB_LOG_ERROR( GetLog(LLDBLog::Expressions | LLDBLog::Types), type_ref_or_err.takeError(), - "Couldn't get rype ref when binding generic type parameters: {0}"); + "Couldn't get type ref when binding generic type parameters: {0}"); failure = true; return; } @@ -2687,8 +2692,8 @@ bool SwiftLanguageRuntime::GetDynamicTypeAndAddress_Value( ExecutionContextScope *exe_scope = exe_ctx.GetBestExecutionContextScope(); if (!exe_scope) return false; - std::optional size = - bound_type.GetByteSize(exe_ctx.GetBestExecutionContextScope()); + std::optional size = llvm::expectedToOptional( + bound_type.GetByteSize(exe_ctx.GetBestExecutionContextScope())); if (!size) return false; AddressType address_type; @@ -2704,7 +2709,8 @@ bool SwiftLanguageRuntime::GetDynamicTypeAndAddress_Value( if (in_value_buffer.empty()) return false; // If the dynamic type doesn't in the buffer we can't use it either. - if (in_value_buffer.size() < bound_type.GetByteSize(exe_scope)) + if (in_value_buffer.size() < + llvm::expectedToOptional(bound_type.GetByteSize(exe_scope)).value_or(0)) return false; value_type = Value::GetValueTypeFromAddressType(address_type); @@ -2810,8 +2816,9 @@ Value::ValueType SwiftLanguageRuntime::GetValueType( } if (use_local_buffer) - PushLocalBuffer(existential_address, - in_value.GetByteSize().value_or(0)); + PushLocalBuffer( + existential_address, + llvm::expectedToOptional(in_value.GetByteSize()).value_or(0)); // Read the value witness table and check if the data is inlined in // the existential container or not. @@ -3331,7 +3338,10 @@ SwiftLanguageRuntime::GetTypeRef(CompilerType type, auto type_ref_or_err = reflection_ctx->GetTypeRef( dem, node, module_holder->GetDescriptorFinder()); if (!type_ref_or_err) - return type_ref_or_err.takeError(); + return llvm::joinErrors( + llvm::createStringError("cannot get typeref for type %s", + type.GetMangledTypeName().GetCString()), + type_ref_or_err.takeError()); if (log && log->GetVerbose()) { std::stringstream ss; @@ -3537,14 +3547,12 @@ SwiftLanguageRuntime::ResolveTypeAlias(CompilerType alias) { return llvm::createStringError("cannot resolve type alias via reflection"); } -std::optional +llvm::Expected SwiftLanguageRuntime::GetBitSize(CompilerType type, ExecutionContextScope *exe_scope) { auto type_info_or_err = GetSwiftRuntimeTypeInfo(type, exe_scope); - if (!type_info_or_err) { - LLDB_LOG_ERROR(GetLog(LLDBLog::Types), type_info_or_err.takeError(), "{0}"); - return {}; - } + if (!type_info_or_err) + return type_info_or_err.takeError(); return type_info_or_err->getSize() * 8; } diff --git a/lldb/source/Plugins/LanguageRuntime/Swift/SwiftLanguageRuntimeRemoteAST.cpp b/lldb/source/Plugins/LanguageRuntime/Swift/SwiftLanguageRuntimeRemoteAST.cpp index 484b4270e5020..4da692895c8e5 100644 --- a/lldb/source/Plugins/LanguageRuntime/Swift/SwiftLanguageRuntimeRemoteAST.cpp +++ b/lldb/source/Plugins/LanguageRuntime/Swift/SwiftLanguageRuntimeRemoteAST.cpp @@ -245,7 +245,9 @@ SwiftLanguageRuntime::GetDynamicTypeAndAddress_ExistentialRemoteAST( if (!swift_type) return {}; if (use_local_buffer) - PushLocalBuffer(existential_address, in_value.GetByteSize().value_or(0)); + PushLocalBuffer( + existential_address, + llvm::expectedToOptional(in_value.GetByteSize()).value_or(0)); auto result = remote_ast.getDynamicTypeAndAddressForExistential( remote_existential, swift_type); 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 386ba44c5ea65..c4afb9af6a5a4 100644 --- a/lldb/source/Plugins/SymbolFile/CTF/SymbolFileCTF.cpp +++ b/lldb/source/Plugins/SymbolFile/CTF/SymbolFileCTF.cpp @@ -436,14 +436,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, @@ -543,7 +540,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 a92837d84ca72..f96ed38a2c0f8 100644 --- a/lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp +++ b/lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp @@ -227,9 +227,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) { @@ -1043,8 +1044,10 @@ TypeSP DWARFASTParserClang::ParseEnum(const SymbolContext &sc, if (def_die.HasChildren()) { bool is_signed = false; enumerator_clang_type.IsIntegerType(is_signed); - ParseChildEnumerators(clang_type, is_signed, - type_sp->GetByteSize(nullptr).value_or(0), def_die); + ParseChildEnumerators( + clang_type, is_signed, + llvm::expectedToOptional(type_sp->GetByteSize(nullptr)).value_or(0), + def_die); } TypeSystemClang::CompleteTagDeclarationDefinition(clang_type); } else { @@ -1491,7 +1494,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); @@ -1543,7 +1547,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); @@ -2131,7 +2135,8 @@ bool DWARFASTParserClang::ParseTemplateDIE( name = nullptr; if (tag == DW_TAG_template_value_parameter && uval64_valid) { - std::optional size = clang_type.GetBitSize(nullptr); + std::optional size = + llvm::expectedToOptional(clang_type.GetBitSize(nullptr)); if (!size) return false; llvm::APInt apint(*size, uval64, is_signed); @@ -2300,8 +2305,10 @@ bool DWARFASTParserClang::CompleteEnumType(const DWARFDIE &die, if (die.HasChildren()) { bool is_signed = false; clang_type.IsIntegerType(is_signed); - ParseChildEnumerators(clang_type, is_signed, - type->GetByteSize(nullptr).value_or(0), die); + ParseChildEnumerators( + clang_type, is_signed, + llvm::expectedToOptional(type->GetByteSize(nullptr)).value_or(0), + die); } TypeSystemClang::CompleteTagDeclarationDefinition(clang_type); } @@ -3024,7 +3031,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) { @@ -3112,7 +3119,7 @@ void DWARFASTParserClang::ParseSingleMember( last_field_info.bit_offset = field_bit_offset; if (std::optional clang_type_size = - member_type->GetByteSize(nullptr)) { + llvm::expectedToOptional(member_type->GetByteSize(nullptr))) { last_field_info.bit_size = *clang_type_size * character_width; } @@ -3961,7 +3968,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 63798331c5856..da4f4d036a201 100644 --- a/lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp +++ b/lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp @@ -2166,8 +2166,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())); } @@ -3830,9 +3831,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()); } // Swift let-bindings are marked by a DW_TAG_const_type. diff --git a/lldb/source/Plugins/SymbolFile/NativePDB/SymbolFileNativePDB.cpp b/lldb/source/Plugins/SymbolFile/NativePDB/SymbolFileNativePDB.cpp index eee57e28e27cc..4e50b5d961436 100644 --- a/lldb/source/Plugins/SymbolFile/NativePDB/SymbolFileNativePDB.cpp +++ b/lldb/source/Plugins/SymbolFile/NativePDB/SymbolFileNativePDB.cpp @@ -556,8 +556,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); } @@ -674,10 +674,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, @@ -1902,11 +1903,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 0dc113b5e9fd7..5fdbf0af1a6f5 100644 --- a/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp +++ b/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp @@ -4915,9 +4915,47 @@ 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()); + ExecutionContext exe_ctx(exe_scope); + if (Process *process = exe_ctx.GetProcessPtr()) { + if (ObjCLanguageRuntime *objc_runtime = + ObjCLanguageRuntime::Get(*process)) { + if (std::optional bit_size = + objc_runtime->GetTypeBitSize(GetType(qual_type))) + return *bit_size; + } + } else { + static bool g_printed = false; + if (!g_printed) { + StreamString s; + DumpTypeDescription(qual_type.getAsOpaquePtr(), s); + + llvm::outs() << "warning: trying to determine the size of type "; + llvm::outs() << s.GetString() << "\n"; + llvm::outs() << "without a valid ExecutionContext. this is not " + "reliable. please file a bug against LLDB.\n"; + llvm::outs() << "backtrace:\n"; + llvm::sys::PrintStackTrace(llvm::outs()); + llvm::outs() << "\n"; + g_printed = true; + } + } + + return getASTContext().getTypeSize(qual_type) + + getASTContext().getTypeSize(getASTContext().ObjCBuiltinClassTy); +} + +llvm::Expected TypeSystemClang::GetBitSize(lldb::opaque_compiler_type_t type, ExecutionContextScope *exe_scope) { + const bool base_name_only = true; + if (!GetCompleteType(type)) + return llvm::createStringError( + "could not complete type %s", + GetTypeName(type, base_name_only).AsCString("")); if (GetCompleteType(type)) { clang::QualType qual_type(GetCanonicalQualType(type)); const clang::Type::TypeClass type_class = qual_type->getTypeClass(); @@ -4926,7 +4964,9 @@ TypeSystemClang::GetBitSize(lldb::opaque_compiler_type_t type, if (GetCompleteType(type)) return getASTContext().getTypeSize(qual_type); else - return std::nullopt; + return llvm::createStringError( + "could not complete type %s", + GetTypeName(type, base_name_only).AsCString("")); break; case clang::Type::ObjCInterface: @@ -4976,7 +5016,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 @@ -6496,12 +6538,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); @@ -6529,12 +6573,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 @@ -6704,12 +6749,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; } } } @@ -6727,12 +6772,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; } } } @@ -6746,12 +6791,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; } } } @@ -6785,12 +6830,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; @@ -6823,12 +6868,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 ad7d173bc3f82..4abe4aa109611 100644 --- a/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.h +++ b/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.h @@ -898,15 +898,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; std::optional GetByteStride(lldb::opaque_compiler_type_t type, @@ -1253,6 +1255,9 @@ class TypeSystemClang : public TypeSystem { /// on creation of a new instance. void LogCreation() const; + 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; std::unique_ptr m_ast_up; diff --git a/lldb/source/Plugins/TypeSystem/Swift/SwiftASTContext.cpp b/lldb/source/Plugins/TypeSystem/Swift/SwiftASTContext.cpp index 1f256650ca909..61703fc1f7000 100644 --- a/lldb/source/Plugins/TypeSystem/Swift/SwiftASTContext.cpp +++ b/lldb/source/Plugins/TypeSystem/Swift/SwiftASTContext.cpp @@ -6603,17 +6603,18 @@ bool SwiftASTContext::IsFixedSize(CompilerType compiler_type) { return false; } -std::optional +llvm::Expected SwiftASTContext::GetBitSize(opaque_compiler_type_t type, ExecutionContextScope *exe_scope) { - VALID_OR_RETURN_CHECK_TYPE(type, std::nullopt); + VALID_OR_RETURN_CHECK_TYPE(type, llvm::createStringError("invalid context")); LLDB_SCOPED_TIMER(); // If the type has type parameters, bind them first. swift::CanType swift_can_type(GetCanonicalSwiftType(type)); if (swift_can_type->hasTypeParameter()) { if (!exe_scope) - return {}; + return llvm::createStringError( + "Cannot resolve generic type without a running process"); ExecutionContext exe_ctx; exe_scope->CalculateExecutionContext(exe_ctx); CompilerType bound_type = @@ -6622,14 +6623,19 @@ SwiftASTContext::GetBitSize(opaque_compiler_type_t type, // Check that the type has been bound successfully -- and if not, // log the event and bail out to avoid an infinite loop. swift::CanType swift_bound_type(GetCanonicalSwiftType(bound_type)); - if (swift_bound_type && swift_bound_type->hasTypeParameter()) { - LOG_PRINTF(GetLog(LLDBLog::Types), "Can't bind type: %s", - bound_type.GetTypeName().AsCString()); - return {}; - } + if (swift_bound_type && swift_bound_type->hasTypeParameter()) + return llvm::createStringError("Cannot bind type: %s", + bound_type.GetTypeName().AsCString("")); - // Note thay the bound type may be in a different AST context. - return bound_type.GetBitSize(exe_scope); + // Note that the bound type may be in a different AST context. + auto size_or_err = bound_type.GetBitSize(exe_scope); + if (!size_or_err && swift_bound_type->hasAnyPack()) { + // FIXME: We miss reflection support for pack types. + LLDB_LOG_ERROR(GetLog(LLDBLog::Types), size_or_err.takeError(), + "Could not get size of pack type. Ignoring: {0}"); + return GetPointerByteSize() * 3 * 8; + } + return size_or_err; } // LLDB's ValueObject subsystem expects functions to be a single @@ -6647,10 +6653,12 @@ SwiftASTContext::GetBitSize(opaque_compiler_type_t type, // Ask the dynamic type system. if (!exe_scope) - return {}; + return llvm::createStringError( + "Cannot determine size of type without a running process"); + if (auto *runtime = SwiftLanguageRuntime::Get(exe_scope->CalculateProcess())) return runtime->GetBitSize({weak_from_this(), type}, exe_scope); - return {}; + return llvm::createStringError("Cannot determine size of type"); } std::optional @@ -7378,7 +7386,8 @@ CompilerType SwiftASTContext::GetFieldAtIndex(opaque_compiler_type_t type, std::tie(child_type, name) = GetExistentialTypeChild( *this, **ast_ctx, compiler_type, protocol_info, idx); - std::optional child_size = child_type.GetByteSize(nullptr); + std::optional child_size = + llvm::expectedToOptional(child_type.GetByteSize(nullptr)); if (!child_size) return {}; if (bit_offset_ptr) @@ -7573,14 +7582,10 @@ llvm::Expected SwiftASTContext::GetChildCompilerTypeAtIndex( VALID_OR_RETURN_CHECK_TYPE(type, CompilerType()); LLDB_SCOPED_TIMER(); - auto get_type_size = [&exe_ctx](uint32_t &result, CompilerType type) { + auto get_type_size = [&exe_ctx](CompilerType type) { auto *exe_scope = exe_ctx ? exe_ctx->GetBestExecutionContextScope() : nullptr; - std::optional size = type.GetByteSize(exe_scope); - if (!size) - return false; - result = *size; - return true; + return type.GetByteSize(exe_scope); }; language_flags = 0; @@ -7660,9 +7665,13 @@ llvm::Expected SwiftASTContext::GetChildCompilerTypeAtIndex( const SwiftEnumDescriptor::ElementInfo *element_info = cached_enum_info->GetElementWithPayloadAtIndex(idx); child_name.assign(element_info->name.GetCString()); - if (!get_type_size(child_byte_size, element_info->payload_type)) - return llvm::createStringError("could not get size for enum element " + - llvm::Twine(idx)); + auto size_or_err = get_type_size(element_info->payload_type); + if (!size_or_err) + return llvm::joinErrors( + llvm::createStringError("could not get size for enum element " + + llvm::Twine(idx)), + size_or_err.takeError()); + child_byte_size = *size_or_err; child_byte_offset = 0; child_bitfield_bit_size = 0; child_bitfield_bit_offset = 0; @@ -7689,9 +7698,13 @@ llvm::Expected SwiftASTContext::GetChildCompilerTypeAtIndex( child_name = GetTupleElementName(tuple_type, idx, printed_idx); CompilerType child_type = ToCompilerType(child.getType().getPointer()); - if (!get_type_size(child_byte_size, child_type)) - return llvm::createStringError("could not get size of tuple element " + - child_name); + auto size_or_err = get_type_size(child_type); + if (!size_or_err) + return llvm::joinErrors( + llvm::createStringError("could not get size for tuple element " + + child_name), + size_or_err.takeError()); + child_byte_size = *size_or_err; child_is_base_class = false; child_is_deref_of_parent = false; @@ -7721,9 +7734,13 @@ llvm::Expected SwiftASTContext::GetChildCompilerTypeAtIndex( CompilerType child_type = ToCompilerType(fixed_array->getElementType()); llvm::raw_string_ostream(child_name) << idx; - if (!get_type_size(child_byte_size, child_type)) - return llvm::createStringError( - "could not get size of fixed array element " + child_name); + auto size_or_err = get_type_size(child_type); + if (!size_or_err) + return llvm::joinErrors( + llvm::createStringError("could not get size of fixes array element " + + child_name), + size_or_err.takeError()); + child_byte_size = *size_or_err; child_is_base_class = false; child_is_deref_of_parent = false; @@ -7751,8 +7768,12 @@ llvm::Expected SwiftASTContext::GetChildCompilerTypeAtIndex( ToCompilerType(superclass_swift_type.getPointer()); child_name = GetSuperclassName(superclass_type); - if (!get_type_size(child_byte_size, superclass_type)) - return llvm::createStringError("could not get size of super class"); + auto size_or_err = get_type_size(superclass_type); + if (!size_or_err) + return llvm::joinErrors( + llvm::createStringError("could not get size of super class"), + size_or_err.takeError()); + child_byte_size = *size_or_err; child_is_base_class = true; child_is_deref_of_parent = false; @@ -7791,10 +7812,13 @@ llvm::Expected SwiftASTContext::GetChildCompilerTypeAtIndex( CompilerType child_type = ToCompilerType(VD->getTypeInContext().getPointer()); child_name = VD->getNameStr().str(); - if (!get_type_size(child_byte_size, child_type)) - return llvm::createStringError("could not get size of field " + - child_name); - + auto size_or_err = get_type_size(child_type); + if (!size_or_err) + return llvm::joinErrors( + llvm::createStringError("could not get size of field " + + child_name), + size_or_err.takeError()); + child_byte_size = *size_or_err; child_is_base_class = false; child_is_deref_of_parent = false; child_byte_offset = 0; @@ -7817,9 +7841,13 @@ llvm::Expected SwiftASTContext::GetChildCompilerTypeAtIndex( CompilerType child_type = ToCompilerType(child_swift_type.getPointer()); child_name = property->getBaseName().userFacingName().str(); - if (!get_type_size(child_byte_size, child_type)) - return llvm::createStringError("could not get size of field " + - child_name); + auto size_or_err = get_type_size(child_type); + if (!size_or_err) + return llvm::joinErrors( + llvm::createStringError("could not get size of field " + + child_name), + size_or_err.takeError()); + child_byte_size = *size_or_err; child_is_base_class = false; child_is_deref_of_parent = false; @@ -7852,9 +7880,13 @@ llvm::Expected SwiftASTContext::GetChildCompilerTypeAtIndex( std::tie(child_type, child_name) = GetExistentialTypeChild( *this, **ast_ctx, compiler_type, protocol_info, idx); - if (!get_type_size(child_byte_size, child_type)) - return llvm::createStringError("could not get size of field " + - llvm::Twine(idx)); + auto size_or_err = get_type_size(child_type); + if (!size_or_err) + return llvm::joinErrors( + llvm::createStringError("could not get size of field " + + llvm::Twine(idx)), + size_or_err.takeError()); + child_byte_size = *size_or_err; child_byte_offset = idx * child_byte_size; child_bitfield_bit_size = 0; child_bitfield_bit_offset = 0; @@ -7878,8 +7910,12 @@ llvm::Expected SwiftASTContext::GetChildCompilerTypeAtIndex( // We have a pointer to a simple type if (idx == 0) { - if (!get_type_size(child_byte_size, pointee_clang_type)) - return llvm::createStringError("could not get size of lvalue"); + auto size_or_err = get_type_size(pointee_clang_type); + if (!size_or_err) + return llvm::joinErrors( + llvm::createStringError("could not get size of lvalue"), + 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/Swift/SwiftASTContext.h b/lldb/source/Plugins/TypeSystem/Swift/SwiftASTContext.h index f3d65256920e9..66d02212e17c6 100644 --- a/lldb/source/Plugins/TypeSystem/Swift/SwiftASTContext.h +++ b/lldb/source/Plugins/TypeSystem/Swift/SwiftASTContext.h @@ -705,7 +705,7 @@ class SwiftASTContext : public TypeSystemSwift { // Exploring the type - std::optional + llvm::Expected GetBitSize(lldb::opaque_compiler_type_t type, ExecutionContextScope *exe_scope) override; diff --git a/lldb/source/Plugins/TypeSystem/Swift/TypeSystemSwiftTypeRef.cpp b/lldb/source/Plugins/TypeSystem/Swift/TypeSystemSwiftTypeRef.cpp index d5cecf7a39749..67d636ca51281 100644 --- a/lldb/source/Plugins/TypeSystem/Swift/TypeSystemSwiftTypeRef.cpp +++ b/lldb/source/Plugins/TypeSystem/Swift/TypeSystemSwiftTypeRef.cpp @@ -3537,11 +3537,11 @@ CompilerType TypeSystemSwiftTypeRef::GetVoidFunctionType() { } // Exploring the type -std::optional +llvm::Expected TypeSystemSwiftTypeRef::GetBitSize(opaque_compiler_type_t type, ExecutionContextScope *exe_scope) { LLDB_SCOPED_TIMER(); - auto impl = [&]() -> std::optional { + auto impl = [&]() -> llvm::Expected { auto get_static_size = [&](bool cached_only) -> std::optional { if (IsMeaninglessWithoutDynamicResolution(type)) return {}; @@ -3582,21 +3582,25 @@ TypeSystemSwiftTypeRef::GetBitSize(opaque_compiler_type_t type, // pointer instead of the underlying object. if (Flags(clang_type.GetTypeInfo()).AllSet(eTypeIsObjC | eTypeIsClass)) return GetPointerByteSize() * 8; - if (auto clang_size = clang_type.GetBitSize(exe_scope)) - return clang_size; - } - if (!exe_scope) { - LLDB_LOGF(GetLog(LLDBLog::Types), - "Couldn't compute size of type %s without an execution " - "context.", - AsMangledName(type)); - return {}; + auto clang_size = clang_type.GetBitSize(exe_scope); + return clang_size; } + if (!exe_scope) + return llvm::createStringError( + "Cannot compute size of type %s without an execution context.", + AsMangledName(type)); // The hot code path is to ask the Swift runtime for the size. if (auto *runtime = SwiftLanguageRuntime::Get(exe_scope->CalculateProcess())) { - if (auto result = runtime->GetBitSize({weak_from_this(), type}, exe_scope)) - return result; + auto result_or_err = + runtime->GetBitSize({weak_from_this(), type}, exe_scope); + if (result_or_err) + return *result_or_err; + LLDB_LOG_ERROR( + GetLog(LLDBLog::Types), result_or_err.takeError(), + "Couldn't compute size of type {1} using Swift language runtime: {0}", + AsMangledName(type)); + // Runtime failed, fallback to SwiftASTContext. if (UseSwiftASTContextFallback(__FUNCTION__, type)) { if (auto swift_ast_context = @@ -3618,21 +3622,20 @@ TypeSystemSwiftTypeRef::GetBitSize(opaque_compiler_type_t type, // If we have already parsed this as an lldb::Type from DWARF, // return its static size. if (auto cached_type_static_size = get_static_size(true)) - return cached_type_static_size; + return *cached_type_static_size; // If we are here, we probably are in a target with no process and // inspect a gloabl variable. Do an (expensive) search for the // static type in the debug info. if (auto static_size = get_static_size(false)) - return static_size; - LLDB_LOGF(GetLog(LLDBLog::Types), - "Couldn't compute size of type %s using static debug info.", - AsMangledName(type)); - return {}; + return *static_size; + return llvm::createStringError( + "Cannot compute size of type %s using static debug info.", + AsMangledName(type)); }; if (exe_scope && exe_scope->CalculateProcess()) { - VALIDATE_AND_RETURN(impl, GetBitSize, type, exe_scope, - (ReconstructType(type, exe_scope), exe_scope)); + VALIDATE_AND_RETURN_EXPECTED(impl, GetBitSize, type, exe_scope, + (ReconstructType(type, exe_scope), exe_scope)); } else return impl(); } @@ -3928,8 +3931,7 @@ TypeSystemSwiftTypeRef::GetChildCompilerTypeAtIndex( child_byte_size, child_byte_offset, child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class, child_is_deref_of_parent, valobj, language_flags); - return llvm::createStringError(llvm::inconvertibleErrorCode(), - "no SwiftASTContext"); + return llvm::createStringError("no SwiftASTContext"); }; std::optional ast_num_children; auto get_ast_num_children = [&]() { @@ -3982,7 +3984,9 @@ TypeSystemSwiftTypeRef::GetChildCompilerTypeAtIndex( child_name = "rawValue"; auto bit_size = raw_value.GetBitSize( exe_ctx ? exe_ctx->GetBestExecutionContextScope() : nullptr); - child_byte_size = bit_size.value_or(0) / 8; + if (!bit_size) + return bit_size.takeError(); + child_byte_size = *bit_size / 8; child_byte_offset = 0; child_bitfield_bit_size = 0; child_bitfield_bit_offset = 0; diff --git a/lldb/source/Plugins/TypeSystem/Swift/TypeSystemSwiftTypeRef.h b/lldb/source/Plugins/TypeSystem/Swift/TypeSystemSwiftTypeRef.h index 6f61e6f7c1879..220b7560f0f88 100644 --- a/lldb/source/Plugins/TypeSystem/Swift/TypeSystemSwiftTypeRef.h +++ b/lldb/source/Plugins/TypeSystem/Swift/TypeSystemSwiftTypeRef.h @@ -196,7 +196,7 @@ class TypeSystemSwiftTypeRef : public TypeSystemSwift { CompilerType GetVoidFunctionType(); // Exploring the type - std::optional + llvm::Expected GetBitSize(lldb::opaque_compiler_type_t type, ExecutionContextScope *exe_scope) override; std::optional diff --git a/lldb/source/Symbol/CompilerType.cpp b/lldb/source/Symbol/CompilerType.cpp index 0a9d4283fd455..4eed615ad7bb3 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" @@ -809,19 +811,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 @@ -1157,10 +1160,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; @@ -1170,15 +1181,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 @@ -1187,15 +1198,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 @@ -1204,10 +1215,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); @@ -1217,7 +1228,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); @@ -1227,7 +1238,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 3823ddde39c70..f86af3b9eb637 100644 --- a/lldb/source/Symbol/Type.cpp +++ b/lldb/source/Symbol/Type.cpp @@ -448,14 +448,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: @@ -465,18 +469,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; @@ -491,7 +495,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) { @@ -532,7 +537,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 6d98f069a2b67..98be6ed4499ed 100644 --- a/lldb/source/Target/StackFrame.cpp +++ b/lldb/source/Target/StackFrame.cpp @@ -1488,7 +1488,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(); } @@ -1505,7 +1507,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); @@ -1537,9 +1540,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 dfa6b40ac5d9b..a25c12776eba5 100644 --- a/lldb/source/Target/Thread.cpp +++ b/lldb/source/Target/Thread.cpp @@ -2138,10 +2138,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 6364fec0a5794..509dd48f5a4b1 100644 --- a/lldb/source/ValueObject/ValueObject.cpp +++ b/lldb/source/ValueObject/ValueObject.cpp @@ -745,8 +745,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; @@ -862,7 +862,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(); @@ -1292,7 +1292,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( @@ -1355,7 +1356,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 @@ -1747,7 +1749,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(); @@ -1931,13 +1933,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) { @@ -1972,8 +1976,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 = @@ -2014,8 +2018,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 = @@ -3067,8 +3071,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); @@ -3293,9 +3299,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) { @@ -3442,7 +3449,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) { @@ -3717,7 +3724,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, @@ -3745,7 +3752,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(), @@ -3763,7 +3771,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/ValueObjectChild.cpp b/lldb/source/ValueObject/ValueObjectChild.cpp index d6773873b0ab1..d3a95716afdc5 100644 --- a/lldb/source/ValueObject/ValueObjectChild.cpp +++ b/lldb/source/ValueObject/ValueObjectChild.cpp @@ -201,8 +201,9 @@ bool ValueObjectChild::UpdateValue() { const bool thread_and_frame_only_if_stopped = true; ExecutionContext exe_ctx(GetExecutionContextRef().Lock( thread_and_frame_only_if_stopped)); - if (auto type_bit_size = GetCompilerType().GetBitSize( - exe_ctx.GetBestExecutionContextScope())) { + if (auto type_bit_size = + llvm::expectedToOptional(GetCompilerType().GetBitSize( + exe_ctx.GetBestExecutionContextScope()))) { uint64_t bitfield_end = m_bitfield_bit_size + m_bitfield_bit_offset; if (bitfield_end > *type_bit_size) { diff --git a/lldb/source/ValueObject/ValueObjectConstResult.cpp b/lldb/source/ValueObject/ValueObjectConstResult.cpp index dae0c5871c86f..f56ec4e16ab46 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 5e939ca30d492..20cf5c3d84f8e 100644 --- a/lldb/source/ValueObject/ValueObjectDynamicValue.cpp +++ b/lldb/source/ValueObject/ValueObjectDynamicValue.cpp @@ -108,7 +108,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()); @@ -286,7 +286,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 8bf15164bd59b..d9c548e8b0c8e 100644 --- a/lldb/source/ValueObject/ValueObjectMemory.cpp +++ b/lldb/source/ValueObject/ValueObjectMemory.cpp @@ -147,10 +147,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 33ec4373945d1..86467eb0565ce 100644 --- a/lldb/source/ValueObject/ValueObjectVariable.cpp +++ b/lldb/source/ValueObject/ValueObjectVariable.cpp @@ -119,14 +119,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()); } @@ -147,9 +143,13 @@ bool ValueObjectVariable::UpdateValue() { CompilerType var_type(GetCompilerTypeImpl()); if (var_type.IsValid() && var_type.GetMinimumLanguage() == lldb::eLanguageTypeSwift) { ExecutionContext exe_ctx(GetExecutionContextRef()); - std::optional size = - var_type.GetByteSize(exe_ctx.GetBestExecutionContextScope()); - if (size && *size == 0) { + auto size_or_err = + var_type.GetByteSize(exe_ctx.GetBestExecutionContextScope()); + if (!size_or_err) { + m_error = Status::FromError(size_or_err.takeError()); + return false; + } + if (*size_or_err == 0) { m_value.SetCompilerType(var_type); return m_error.Success(); } 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, diff --git a/lldb/unittests/SymbolFile/PDB/SymbolFilePDBTests.cpp b/lldb/unittests/SymbolFile/PDB/SymbolFilePDBTests.cpp index 4379ffac9d744..858aecd1b9798 100644 --- a/lldb/unittests/SymbolFile/PDB/SymbolFilePDBTests.cpp +++ b/lldb/unittests/SymbolFile/PDB/SymbolFilePDBTests.cpp @@ -371,7 +371,7 @@ TEST_F(SymbolFilePDBTests, TestSimpleClassTypes) { CompilerType compiler_type = udt_type->GetForwardCompilerType(); EXPECT_TRUE(TypeSystemClang::IsClassType(compiler_type.GetOpaqueQualType())); EXPECT_EQ(GetGlobalConstantInteger(session, "sizeof_Class"), - udt_type->GetByteSize(nullptr)); + llvm::expectedToOptional(udt_type->GetByteSize(nullptr))); } TEST_F(SymbolFilePDBTests, TestNestedClassTypes) { @@ -427,7 +427,7 @@ TEST_F(SymbolFilePDBTests, TestNestedClassTypes) { EXPECT_TRUE(TypeSystemClang::IsClassType(compiler_type.GetOpaqueQualType())); EXPECT_EQ(GetGlobalConstantInteger(session, "sizeof_NestedClass"), - udt_type->GetByteSize(nullptr)); + llvm::expectedToOptional(udt_type->GetByteSize(nullptr))); } TEST_F(SymbolFilePDBTests, TestClassInNamespace) { @@ -471,7 +471,7 @@ TEST_F(SymbolFilePDBTests, TestClassInNamespace) { EXPECT_TRUE(TypeSystemClang::IsClassType(compiler_type.GetOpaqueQualType())); EXPECT_EQ(GetGlobalConstantInteger(session, "sizeof_NSClass"), - udt_type->GetByteSize(nullptr)); + llvm::expectedToOptional(udt_type->GetByteSize(nullptr))); } TEST_F(SymbolFilePDBTests, TestEnumTypes) { @@ -501,7 +501,7 @@ TEST_F(SymbolFilePDBTests, TestEnumTypes) { std::string sizeof_var = "sizeof_"; sizeof_var.append(Enum); EXPECT_EQ(GetGlobalConstantInteger(session, sizeof_var), - enum_type->GetByteSize(nullptr)); + llvm::expectedToOptional(enum_type->GetByteSize(nullptr))); } } @@ -547,7 +547,7 @@ TEST_F(SymbolFilePDBTests, TestTypedefs) { std::string sizeof_var = "sizeof_"; sizeof_var.append(Typedef); EXPECT_EQ(GetGlobalConstantInteger(session, sizeof_var), - typedef_type->GetByteSize(nullptr)); + llvm::expectedToOptional(typedef_type->GetByteSize(nullptr))); } }