Skip to content

Commit ac1dc05

Browse files
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.
1 parent 101f977 commit ac1dc05

File tree

10 files changed

+67
-37
lines changed

10 files changed

+67
-37
lines changed

lldb/include/lldb/Symbol/CompilerType.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ class CompilerType {
436436
uint32_t *bitfield_bit_size_ptr = nullptr,
437437
bool *is_bitfield_ptr = nullptr) const;
438438

439-
CompilerType GetChildCompilerTypeAtIndex(
439+
llvm::Expected<CompilerType> GetChildCompilerTypeAtIndex(
440440
ExecutionContext *exe_ctx, size_t idx, bool transparent_pointers,
441441
bool omit_empty_base_classes, bool ignore_array_bounds,
442442
std::string &child_name, uint32_t &child_byte_size,

lldb/include/lldb/Symbol/TypeSystem.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ class TypeSystem : public PluginInterface,
359359
return CompilerDecl();
360360
}
361361

362-
virtual CompilerType GetChildCompilerTypeAtIndex(
362+
virtual llvm::Expected<CompilerType> GetChildCompilerTypeAtIndex(
363363
lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, size_t idx,
364364
bool transparent_pointers, bool omit_empty_base_classes,
365365
bool ignore_array_bounds, std::string &child_name,

lldb/source/Core/ValueObject.cpp

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -505,15 +505,23 @@ ValueObject *ValueObject::CreateChildAtIndex(size_t idx,
505505
uint64_t language_flags = 0;
506506

507507
const bool transparent_pointers = !synthetic_array_member;
508-
CompilerType child_compiler_type;
509508

510509
ExecutionContext exe_ctx(GetExecutionContextRef());
511510

512-
child_compiler_type = GetCompilerType().GetChildCompilerTypeAtIndex(
513-
&exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
514-
ignore_array_bounds, child_name_str, child_byte_size, child_byte_offset,
515-
child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
516-
child_is_deref_of_parent, this, language_flags);
511+
auto child_compiler_type_or_err =
512+
GetCompilerType().GetChildCompilerTypeAtIndex(
513+
&exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
514+
ignore_array_bounds, child_name_str, child_byte_size,
515+
child_byte_offset, child_bitfield_bit_size, child_bitfield_bit_offset,
516+
child_is_base_class, child_is_deref_of_parent, this, language_flags);
517+
CompilerType child_compiler_type;
518+
if (!child_compiler_type_or_err)
519+
LLDB_LOG_ERROR(GetLog(LLDBLog::Types),
520+
child_compiler_type_or_err.takeError(),
521+
"could not find child: {0}");
522+
else
523+
child_compiler_type = *child_compiler_type_or_err;
524+
517525
if (child_compiler_type) {
518526
if (synthetic_index)
519527
child_byte_offset += child_byte_size * synthetic_index;
@@ -2624,16 +2632,23 @@ ValueObjectSP ValueObject::Dereference(Status &error) {
26242632
bool child_is_deref_of_parent = false;
26252633
const bool transparent_pointers = false;
26262634
CompilerType compiler_type = GetCompilerType();
2627-
CompilerType child_compiler_type;
26282635
uint64_t language_flags = 0;
26292636

26302637
ExecutionContext exe_ctx(GetExecutionContextRef());
26312638

2632-
child_compiler_type = compiler_type.GetChildCompilerTypeAtIndex(
2639+
CompilerType child_compiler_type;
2640+
auto child_compiler_type_or_err = compiler_type.GetChildCompilerTypeAtIndex(
26332641
&exe_ctx, 0, transparent_pointers, omit_empty_base_classes,
26342642
ignore_array_bounds, child_name_str, child_byte_size, child_byte_offset,
26352643
child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
26362644
child_is_deref_of_parent, this, language_flags);
2645+
if (!child_compiler_type_or_err)
2646+
LLDB_LOG_ERROR(GetLog(LLDBLog::Types),
2647+
child_compiler_type_or_err.takeError(),
2648+
"could not find child: {0}");
2649+
else
2650+
child_compiler_type = *child_compiler_type_or_err;
2651+
26372652
if (child_compiler_type && child_byte_size) {
26382653
ConstString child_name;
26392654
if (!child_name_str.empty())

lldb/source/Core/ValueObjectConstResultImpl.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#include "lldb/Target/ExecutionContext.h"
1818
#include "lldb/Utility/DataBufferHeap.h"
1919
#include "lldb/Utility/Endian.h"
20+
#include "lldb/Utility/LLDBLog.h"
21+
#include "lldb/Utility/Log.h"
2022
#include "lldb/Utility/Scalar.h"
2123

2224
#include <string>
@@ -66,15 +68,21 @@ ValueObject *ValueObjectConstResultImpl::CreateChildAtIndex(
6668

6769
const bool transparent_pointers = !synthetic_array_member;
6870
CompilerType compiler_type = m_impl_backend->GetCompilerType();
69-
CompilerType child_compiler_type;
7071

7172
ExecutionContext exe_ctx(m_impl_backend->GetExecutionContextRef());
7273

73-
child_compiler_type = compiler_type.GetChildCompilerTypeAtIndex(
74+
auto child_compiler_type_or_err = compiler_type.GetChildCompilerTypeAtIndex(
7475
&exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
7576
ignore_array_bounds, child_name_str, child_byte_size, child_byte_offset,
7677
child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
7778
child_is_deref_of_parent, m_impl_backend, language_flags);
79+
CompilerType child_compiler_type;
80+
if (!child_compiler_type_or_err)
81+
LLDB_LOG_ERROR(GetLog(LLDBLog::Types),
82+
child_compiler_type_or_err.takeError(),
83+
"could not find child: {0}");
84+
else
85+
child_compiler_type = *child_compiler_type_or_err;
7886

7987
// One might think we should check that the size of the children
8088
// is always strictly positive, hence we could avoid creating a

lldb/source/Plugins/ABI/PowerPC/ABISysV_ppc64.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -903,7 +903,8 @@ class ReturnValueExtractor {
903903
}
904904

905905
// get child
906-
CompilerType GetChildType(uint32_t i, std::string &name, uint32_t &size) {
906+
llvm::Expected<CompilerType> GetChildType(uint32_t i, std::string &name,
907+
uint32_t &size) {
907908
// GetChild constant inputs
908909
const bool transparent_pointers = false;
909910
const bool omit_empty_base_classes = true;

lldb/source/Plugins/Language/CPlusPlus/BlockPointer.cpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "Plugins/ExpressionParser/Clang/ClangPersistentVariables.h"
1313
#include "Plugins/TypeSystem/Clang/TypeSystemClang.h"
1414
#include "lldb/Core/ValueObject.h"
15+
#include "lldb/Core/ValueObjectConstResult.h"
1516
#include "lldb/DataFormatters/FormattersHelpers.h"
1617
#include "lldb/Symbol/CompilerType.h"
1718
#include "lldb/Symbol/TypeSystem.h"
@@ -105,13 +106,16 @@ class BlockPointerSyntheticFrontEnd : public SyntheticChildrenFrontEnd {
105106
bool child_is_deref_of_parent = false;
106107
uint64_t language_flags = 0;
107108

108-
const CompilerType child_type =
109-
m_block_struct_type.GetChildCompilerTypeAtIndex(
110-
&exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
111-
ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
112-
child_bitfield_bit_size, child_bitfield_bit_offset,
113-
child_is_base_class, child_is_deref_of_parent, value_object,
114-
language_flags);
109+
auto child_type_or_err = m_block_struct_type.GetChildCompilerTypeAtIndex(
110+
&exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
111+
ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
112+
child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
113+
child_is_deref_of_parent, value_object, language_flags);
114+
if (!child_type_or_err)
115+
return ValueObjectConstResult::Create(
116+
exe_ctx.GetBestExecutionContextScope(),
117+
Status(child_type_or_err.takeError()));
118+
CompilerType child_type = *child_type_or_err;
115119

116120
ValueObjectSP struct_pointer_sp =
117121
m_backend.Cast(m_block_struct_type.GetPointerType());

lldb/source/Plugins/Language/CPlusPlus/LibCxxMap.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -295,13 +295,13 @@ void lldb_private::formatters::LibcxxStdMapSyntheticFrontEnd::GetValueOffset(
295295
bool child_is_base_class;
296296
bool child_is_deref_of_parent;
297297
uint64_t language_flags;
298-
if (tree_node_type
299-
.GetChildCompilerTypeAtIndex(
300-
nullptr, 4, true, true, true, child_name, child_byte_size,
301-
child_byte_offset, child_bitfield_bit_size,
302-
child_bitfield_bit_offset, child_is_base_class,
303-
child_is_deref_of_parent, nullptr, language_flags)
304-
.IsValid())
298+
auto child_type =
299+
llvm::expectedToStdOptional(tree_node_type.GetChildCompilerTypeAtIndex(
300+
nullptr, 4, true, true, true, child_name, child_byte_size,
301+
child_byte_offset, child_bitfield_bit_size,
302+
child_bitfield_bit_offset, child_is_base_class,
303+
child_is_deref_of_parent, nullptr, language_flags));
304+
if (child_type && child_type->IsValid())
305305
m_skip_size = (uint32_t)child_byte_offset;
306306
}
307307
}

lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6130,7 +6130,7 @@ uint32_t TypeSystemClang::GetNumPointeeChildren(clang::QualType type) {
61306130
return 0;
61316131
}
61326132

6133-
CompilerType TypeSystemClang::GetChildCompilerTypeAtIndex(
6133+
llvm::Expected<CompilerType> TypeSystemClang::GetChildCompilerTypeAtIndex(
61346134
lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, size_t idx,
61356135
bool transparent_pointers, bool omit_empty_base_classes,
61366136
bool ignore_array_bounds, std::string &child_name,
@@ -6156,11 +6156,8 @@ CompilerType TypeSystemClang::GetChildCompilerTypeAtIndex(
61566156

61576157
auto num_children_or_err =
61586158
GetNumChildren(type, omit_empty_base_classes, exe_ctx);
6159-
if (!num_children_or_err) {
6160-
LLDB_LOG_ERRORV(GetLog(LLDBLog::Types), num_children_or_err.takeError(),
6161-
"{0}");
6162-
return {};
6163-
}
6159+
if (!num_children_or_err)
6160+
return num_children_or_err.takeError();
61646161

61656162
const bool idx_is_valid = idx < *num_children_or_err;
61666163
int32_t bit_offset;
@@ -6242,7 +6239,10 @@ CompilerType TypeSystemClang::GetChildCompilerTypeAtIndex(
62426239
std::optional<uint64_t> size =
62436240
base_class_clang_type.GetBitSize(get_exe_scope());
62446241
if (!size)
6245-
return {};
6242+
return llvm::make_error<llvm::StringError>(
6243+
"no size info for base class",
6244+
llvm::inconvertibleErrorCode());
6245+
62466246
uint64_t base_class_clang_type_bit_size = *size;
62476247

62486248
// Base classes bit sizes should be a multiple of 8 bits in size
@@ -6274,7 +6274,9 @@ CompilerType TypeSystemClang::GetChildCompilerTypeAtIndex(
62746274
std::optional<uint64_t> size =
62756275
field_clang_type.GetByteSize(get_exe_scope());
62766276
if (!size)
6277-
return {};
6277+
return llvm::make_error<llvm::StringError>(
6278+
"no size info for field", llvm::inconvertibleErrorCode());
6279+
62786280
child_byte_size = *size;
62796281
const uint32_t child_bit_size = child_byte_size * 8;
62806282

lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -887,7 +887,7 @@ class TypeSystemClang : public TypeSystem {
887887

888888
static uint32_t GetNumPointeeChildren(clang::QualType type);
889889

890-
CompilerType GetChildCompilerTypeAtIndex(
890+
llvm::Expected<CompilerType> GetChildCompilerTypeAtIndex(
891891
lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, size_t idx,
892892
bool transparent_pointers, bool omit_empty_base_classes,
893893
bool ignore_array_bounds, std::string &child_name,

lldb/source/Symbol/CompilerType.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -902,7 +902,7 @@ uint32_t CompilerType::GetIndexOfFieldWithName(
902902
return UINT32_MAX;
903903
}
904904

905-
CompilerType CompilerType::GetChildCompilerTypeAtIndex(
905+
llvm::Expected<CompilerType> CompilerType::GetChildCompilerTypeAtIndex(
906906
ExecutionContext *exe_ctx, size_t idx, bool transparent_pointers,
907907
bool omit_empty_base_classes, bool ignore_array_bounds,
908908
std::string &child_name, uint32_t &child_byte_size,

0 commit comments

Comments
 (0)