Change GetChildCompilerTypeAtIndex to return Expected (NFC) (#92979)

This change is a general improvement of the internal API. My motivation
is to use this in the Swift typesystem plugin.
This commit is contained in:
Adrian Prantl
2024-05-22 08:52:33 -07:00
committed by GitHub
parent 101f977f2c
commit ac1dc05b33
10 changed files with 67 additions and 37 deletions

View File

@@ -436,7 +436,7 @@ public:
uint32_t *bitfield_bit_size_ptr = nullptr,
bool *is_bitfield_ptr = nullptr) const;
CompilerType GetChildCompilerTypeAtIndex(
llvm::Expected<CompilerType> GetChildCompilerTypeAtIndex(
ExecutionContext *exe_ctx, size_t idx, bool transparent_pointers,
bool omit_empty_base_classes, bool ignore_array_bounds,
std::string &child_name, uint32_t &child_byte_size,

View File

@@ -359,7 +359,7 @@ public:
return CompilerDecl();
}
virtual CompilerType GetChildCompilerTypeAtIndex(
virtual llvm::Expected<CompilerType> GetChildCompilerTypeAtIndex(
lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, size_t idx,
bool transparent_pointers, bool omit_empty_base_classes,
bool ignore_array_bounds, std::string &child_name,

View File

@@ -505,15 +505,23 @@ ValueObject *ValueObject::CreateChildAtIndex(size_t idx,
uint64_t language_flags = 0;
const bool transparent_pointers = !synthetic_array_member;
CompilerType child_compiler_type;
ExecutionContext exe_ctx(GetExecutionContextRef());
child_compiler_type = GetCompilerType().GetChildCompilerTypeAtIndex(
&exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
ignore_array_bounds, child_name_str, child_byte_size, child_byte_offset,
child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
child_is_deref_of_parent, this, language_flags);
auto child_compiler_type_or_err =
GetCompilerType().GetChildCompilerTypeAtIndex(
&exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
ignore_array_bounds, child_name_str, child_byte_size,
child_byte_offset, child_bitfield_bit_size, child_bitfield_bit_offset,
child_is_base_class, child_is_deref_of_parent, this, language_flags);
CompilerType child_compiler_type;
if (!child_compiler_type_or_err)
LLDB_LOG_ERROR(GetLog(LLDBLog::Types),
child_compiler_type_or_err.takeError(),
"could not find child: {0}");
else
child_compiler_type = *child_compiler_type_or_err;
if (child_compiler_type) {
if (synthetic_index)
child_byte_offset += child_byte_size * synthetic_index;
@@ -2624,16 +2632,23 @@ ValueObjectSP ValueObject::Dereference(Status &error) {
bool child_is_deref_of_parent = false;
const bool transparent_pointers = false;
CompilerType compiler_type = GetCompilerType();
CompilerType child_compiler_type;
uint64_t language_flags = 0;
ExecutionContext exe_ctx(GetExecutionContextRef());
child_compiler_type = compiler_type.GetChildCompilerTypeAtIndex(
CompilerType child_compiler_type;
auto child_compiler_type_or_err = compiler_type.GetChildCompilerTypeAtIndex(
&exe_ctx, 0, transparent_pointers, omit_empty_base_classes,
ignore_array_bounds, child_name_str, child_byte_size, child_byte_offset,
child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
child_is_deref_of_parent, this, language_flags);
if (!child_compiler_type_or_err)
LLDB_LOG_ERROR(GetLog(LLDBLog::Types),
child_compiler_type_or_err.takeError(),
"could not find child: {0}");
else
child_compiler_type = *child_compiler_type_or_err;
if (child_compiler_type && child_byte_size) {
ConstString child_name;
if (!child_name_str.empty())

View File

@@ -17,6 +17,8 @@
#include "lldb/Target/ExecutionContext.h"
#include "lldb/Utility/DataBufferHeap.h"
#include "lldb/Utility/Endian.h"
#include "lldb/Utility/LLDBLog.h"
#include "lldb/Utility/Log.h"
#include "lldb/Utility/Scalar.h"
#include <string>
@@ -66,15 +68,21 @@ ValueObject *ValueObjectConstResultImpl::CreateChildAtIndex(
const bool transparent_pointers = !synthetic_array_member;
CompilerType compiler_type = m_impl_backend->GetCompilerType();
CompilerType child_compiler_type;
ExecutionContext exe_ctx(m_impl_backend->GetExecutionContextRef());
child_compiler_type = compiler_type.GetChildCompilerTypeAtIndex(
auto child_compiler_type_or_err = compiler_type.GetChildCompilerTypeAtIndex(
&exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
ignore_array_bounds, child_name_str, child_byte_size, child_byte_offset,
child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
child_is_deref_of_parent, m_impl_backend, language_flags);
CompilerType child_compiler_type;
if (!child_compiler_type_or_err)
LLDB_LOG_ERROR(GetLog(LLDBLog::Types),
child_compiler_type_or_err.takeError(),
"could not find child: {0}");
else
child_compiler_type = *child_compiler_type_or_err;
// One might think we should check that the size of the children
// is always strictly positive, hence we could avoid creating a

View File

@@ -903,7 +903,8 @@ private:
}
// get child
CompilerType GetChildType(uint32_t i, std::string &name, uint32_t &size) {
llvm::Expected<CompilerType> GetChildType(uint32_t i, std::string &name,
uint32_t &size) {
// GetChild constant inputs
const bool transparent_pointers = false;
const bool omit_empty_base_classes = true;

View File

@@ -12,6 +12,7 @@
#include "Plugins/ExpressionParser/Clang/ClangPersistentVariables.h"
#include "Plugins/TypeSystem/Clang/TypeSystemClang.h"
#include "lldb/Core/ValueObject.h"
#include "lldb/Core/ValueObjectConstResult.h"
#include "lldb/DataFormatters/FormattersHelpers.h"
#include "lldb/Symbol/CompilerType.h"
#include "lldb/Symbol/TypeSystem.h"
@@ -105,13 +106,16 @@ public:
bool child_is_deref_of_parent = false;
uint64_t language_flags = 0;
const CompilerType child_type =
m_block_struct_type.GetChildCompilerTypeAtIndex(
&exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
child_bitfield_bit_size, child_bitfield_bit_offset,
child_is_base_class, child_is_deref_of_parent, value_object,
language_flags);
auto child_type_or_err = m_block_struct_type.GetChildCompilerTypeAtIndex(
&exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
child_is_deref_of_parent, value_object, language_flags);
if (!child_type_or_err)
return ValueObjectConstResult::Create(
exe_ctx.GetBestExecutionContextScope(),
Status(child_type_or_err.takeError()));
CompilerType child_type = *child_type_or_err;
ValueObjectSP struct_pointer_sp =
m_backend.Cast(m_block_struct_type.GetPointerType());

View File

@@ -295,13 +295,13 @@ void lldb_private::formatters::LibcxxStdMapSyntheticFrontEnd::GetValueOffset(
bool child_is_base_class;
bool child_is_deref_of_parent;
uint64_t language_flags;
if (tree_node_type
.GetChildCompilerTypeAtIndex(
nullptr, 4, true, true, true, child_name, child_byte_size,
child_byte_offset, child_bitfield_bit_size,
child_bitfield_bit_offset, child_is_base_class,
child_is_deref_of_parent, nullptr, language_flags)
.IsValid())
auto child_type =
llvm::expectedToStdOptional(tree_node_type.GetChildCompilerTypeAtIndex(
nullptr, 4, true, true, true, child_name, child_byte_size,
child_byte_offset, child_bitfield_bit_size,
child_bitfield_bit_offset, child_is_base_class,
child_is_deref_of_parent, nullptr, language_flags));
if (child_type && child_type->IsValid())
m_skip_size = (uint32_t)child_byte_offset;
}
}

View File

@@ -6130,7 +6130,7 @@ uint32_t TypeSystemClang::GetNumPointeeChildren(clang::QualType type) {
return 0;
}
CompilerType TypeSystemClang::GetChildCompilerTypeAtIndex(
llvm::Expected<CompilerType> TypeSystemClang::GetChildCompilerTypeAtIndex(
lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, size_t idx,
bool transparent_pointers, bool omit_empty_base_classes,
bool ignore_array_bounds, std::string &child_name,
@@ -6156,11 +6156,8 @@ CompilerType TypeSystemClang::GetChildCompilerTypeAtIndex(
auto num_children_or_err =
GetNumChildren(type, omit_empty_base_classes, exe_ctx);
if (!num_children_or_err) {
LLDB_LOG_ERRORV(GetLog(LLDBLog::Types), num_children_or_err.takeError(),
"{0}");
return {};
}
if (!num_children_or_err)
return num_children_or_err.takeError();
const bool idx_is_valid = idx < *num_children_or_err;
int32_t bit_offset;
@@ -6242,7 +6239,10 @@ CompilerType TypeSystemClang::GetChildCompilerTypeAtIndex(
std::optional<uint64_t> size =
base_class_clang_type.GetBitSize(get_exe_scope());
if (!size)
return {};
return llvm::make_error<llvm::StringError>(
"no size info for base class",
llvm::inconvertibleErrorCode());
uint64_t base_class_clang_type_bit_size = *size;
// Base classes bit sizes should be a multiple of 8 bits in size
@@ -6274,7 +6274,9 @@ CompilerType TypeSystemClang::GetChildCompilerTypeAtIndex(
std::optional<uint64_t> size =
field_clang_type.GetByteSize(get_exe_scope());
if (!size)
return {};
return llvm::make_error<llvm::StringError>(
"no size info for field", llvm::inconvertibleErrorCode());
child_byte_size = *size;
const uint32_t child_bit_size = child_byte_size * 8;

View File

@@ -887,7 +887,7 @@ public:
static uint32_t GetNumPointeeChildren(clang::QualType type);
CompilerType GetChildCompilerTypeAtIndex(
llvm::Expected<CompilerType> GetChildCompilerTypeAtIndex(
lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, size_t idx,
bool transparent_pointers, bool omit_empty_base_classes,
bool ignore_array_bounds, std::string &child_name,

View File

@@ -902,7 +902,7 @@ uint32_t CompilerType::GetIndexOfFieldWithName(
return UINT32_MAX;
}
CompilerType CompilerType::GetChildCompilerTypeAtIndex(
llvm::Expected<CompilerType> CompilerType::GetChildCompilerTypeAtIndex(
ExecutionContext *exe_ctx, size_t idx, bool transparent_pointers,
bool omit_empty_base_classes, bool ignore_array_bounds,
std::string &child_name, uint32_t &child_byte_size,