Skip to content

Commit 90b9922

Browse files
authored
[lldb] Split ValueObject::CreateChildAtIndex into two functions (#94455)
The the function is doing two fairly different things, depending on how it is called. While this allows for some code reuse, it also makes it hard to override it correctly. Possibly for this reason ValueObjectSynthetic overerides GetChildAtIndex instead, which forces it to reimplement some of its functionality, most notably caching of generated children. Splitting this up makes it easier to move the caching to a common place (and hopefully makes the code easier to follow in general).
1 parent d843c02 commit 90b9922

14 files changed

+178
-125
lines changed

lldb/include/lldb/Core/ValueObject.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -959,9 +959,12 @@ class ValueObject {
959959
/// Should only be called by ValueObject::GetChildAtIndex().
960960
///
961961
/// \return A ValueObject managed by this ValueObject's manager.
962-
virtual ValueObject *CreateChildAtIndex(size_t idx,
963-
bool synthetic_array_member,
964-
int32_t synthetic_index);
962+
virtual ValueObject *CreateChildAtIndex(size_t idx);
963+
964+
/// Should only be called by ValueObject::GetSyntheticArrayMember().
965+
///
966+
/// \return A ValueObject managed by this ValueObject's manager.
967+
virtual ValueObject *CreateSyntheticArrayMember(size_t idx);
965968

966969
/// Should only be called by ValueObject::GetNumChildren().
967970
virtual llvm::Expected<uint32_t>

lldb/include/lldb/Core/ValueObjectConstResult.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,6 @@ class ValueObjectConstResult : public ValueObject {
7979

8080
lldb::ValueObjectSP Dereference(Status &error) override;
8181

82-
ValueObject *CreateChildAtIndex(size_t idx, bool synthetic_array_member,
83-
int32_t synthetic_index) override;
84-
8582
lldb::ValueObjectSP GetSyntheticChildAtOffset(
8683
uint32_t offset, const CompilerType &type, bool can_create,
8784
ConstString name_const_str = ConstString()) override;
@@ -151,6 +148,13 @@ class ValueObjectConstResult : public ValueObject {
151148
ValueObjectConstResult(ExecutionContextScope *exe_scope,
152149
ValueObjectManager &manager, const Status &error);
153150

151+
ValueObject *CreateChildAtIndex(size_t idx) override {
152+
return m_impl.CreateChildAtIndex(idx);
153+
}
154+
ValueObject *CreateSyntheticArrayMember(size_t idx) override {
155+
return m_impl.CreateSyntheticArrayMember(idx);
156+
}
157+
154158
ValueObjectConstResult(const ValueObjectConstResult &) = delete;
155159
const ValueObjectConstResult &
156160
operator=(const ValueObjectConstResult &) = delete;

lldb/include/lldb/Core/ValueObjectConstResultCast.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,6 @@ class ValueObjectConstResultCast : public ValueObjectCast {
3535

3636
lldb::ValueObjectSP Dereference(Status &error) override;
3737

38-
ValueObject *CreateChildAtIndex(size_t idx, bool synthetic_array_member,
39-
int32_t synthetic_index) override;
40-
4138
virtual CompilerType GetCompilerType() {
4239
return ValueObjectCast::GetCompilerType();
4340
}
@@ -61,6 +58,13 @@ class ValueObjectConstResultCast : public ValueObjectCast {
6158
friend class ValueObjectConstResult;
6259
friend class ValueObjectConstResultImpl;
6360

61+
ValueObject *CreateChildAtIndex(size_t idx) override {
62+
return m_impl.CreateChildAtIndex(idx);
63+
}
64+
ValueObject *CreateSyntheticArrayMember(size_t idx) override {
65+
return m_impl.CreateSyntheticArrayMember(idx);
66+
}
67+
6468
ValueObjectConstResultCast(const ValueObjectConstResultCast &) = delete;
6569
const ValueObjectConstResultCast &
6670
operator=(const ValueObjectConstResultCast &) = delete;

lldb/include/lldb/Core/ValueObjectConstResultChild.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,6 @@ class ValueObjectConstResultChild : public ValueObjectChild {
4141

4242
lldb::ValueObjectSP Dereference(Status &error) override;
4343

44-
ValueObject *CreateChildAtIndex(size_t idx, bool synthetic_array_member,
45-
int32_t synthetic_index) override;
46-
4744
virtual CompilerType GetCompilerType() {
4845
return ValueObjectChild::GetCompilerType();
4946
}
@@ -70,6 +67,13 @@ class ValueObjectConstResultChild : public ValueObjectChild {
7067
friend class ValueObjectConstResult;
7168
friend class ValueObjectConstResultImpl;
7269

70+
ValueObject *CreateChildAtIndex(size_t idx) override {
71+
return m_impl.CreateChildAtIndex(idx);
72+
}
73+
ValueObject *CreateSyntheticArrayMember(size_t idx) override {
74+
return m_impl.CreateSyntheticArrayMember(idx);
75+
}
76+
7377
ValueObjectConstResultChild(const ValueObjectConstResultChild &) = delete;
7478
const ValueObjectConstResultChild &
7579
operator=(const ValueObjectConstResultChild &) = delete;

lldb/include/lldb/Core/ValueObjectConstResultImpl.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ class ValueObjectConstResultImpl {
3838

3939
lldb::ValueObjectSP Dereference(Status &error);
4040

41-
ValueObject *CreateChildAtIndex(size_t idx, bool synthetic_array_member,
42-
int32_t synthetic_index);
41+
ValueObject *CreateChildAtIndex(size_t idx);
42+
ValueObject *CreateSyntheticArrayMember(size_t idx);
4343

4444
lldb::ValueObjectSP
4545
GetSyntheticChildAtOffset(uint32_t offset, const CompilerType &type,

lldb/include/lldb/Core/ValueObjectRegister.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,6 @@ class ValueObjectRegisterSet : public ValueObject {
4949

5050
llvm::Expected<uint32_t> CalculateNumChildren(uint32_t max) override;
5151

52-
ValueObject *CreateChildAtIndex(size_t idx, bool synthetic_array_member,
53-
int32_t synthetic_index) override;
54-
5552
lldb::ValueObjectSP GetChildMemberWithName(llvm::StringRef name,
5653
bool can_create = true) override;
5754

@@ -73,6 +70,11 @@ class ValueObjectRegisterSet : public ValueObject {
7370
ValueObjectManager &manager,
7471
lldb::RegisterContextSP &reg_ctx_sp, uint32_t set_idx);
7572

73+
ValueObject *CreateChildAtIndex(size_t idx) override;
74+
ValueObject *CreateSyntheticArrayMember(size_t idx) override {
75+
return nullptr;
76+
}
77+
7678
// For ValueObject only
7779
ValueObjectRegisterSet(const ValueObjectRegisterSet &) = delete;
7880
const ValueObjectRegisterSet &

lldb/include/lldb/Core/ValueObjectVTable.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,6 @@ class ValueObjectVTable : public ValueObject {
6666

6767
llvm::Expected<uint32_t> CalculateNumChildren(uint32_t max) override;
6868

69-
ValueObject *CreateChildAtIndex(size_t idx, bool synthetic_array_member,
70-
int32_t synthetic_index) override;
71-
7269
lldb::ValueType GetValueType() const override;
7370

7471
ConstString GetTypeName() override;
@@ -95,6 +92,11 @@ class ValueObjectVTable : public ValueObject {
9592
private:
9693
ValueObjectVTable(ValueObject &parent);
9794

95+
ValueObject *CreateChildAtIndex(size_t idx) override;
96+
ValueObject *CreateSyntheticArrayMember(size_t idx) override {
97+
return nullptr;
98+
}
99+
98100
// For ValueObject only
99101
ValueObjectVTable(const ValueObjectVTable &) = delete;
100102
const ValueObjectVTable &operator=(const ValueObjectVTable &) = delete;

lldb/source/Core/ValueObject.cpp

Lines changed: 55 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ ValueObjectSP ValueObject::GetChildAtIndex(uint32_t idx, bool can_create) {
382382
if (can_create && !m_children.HasChildAtIndex(idx)) {
383383
// No we haven't created the child at this index, so lets have our
384384
// subclass do it and cache the result for quick future access.
385-
m_children.SetChildAtIndex(idx, CreateChildAtIndex(idx, false, 0));
385+
m_children.SetChildAtIndex(idx, CreateChildAtIndex(idx));
386386
}
387387

388388
ValueObject *child = m_children.GetChildAtIndex(idx);
@@ -488,66 +488,85 @@ void ValueObject::SetNumChildren(uint32_t num_children) {
488488
m_children.SetChildrenCount(num_children);
489489
}
490490

491-
ValueObject *ValueObject::CreateChildAtIndex(size_t idx,
492-
bool synthetic_array_member,
493-
int32_t synthetic_index) {
494-
ValueObject *valobj = nullptr;
495-
491+
ValueObject *ValueObject::CreateChildAtIndex(size_t idx) {
496492
bool omit_empty_base_classes = true;
497-
bool ignore_array_bounds = synthetic_array_member;
498-
std::string child_name_str;
493+
bool ignore_array_bounds = false;
494+
std::string child_name;
499495
uint32_t child_byte_size = 0;
500496
int32_t child_byte_offset = 0;
501497
uint32_t child_bitfield_bit_size = 0;
502498
uint32_t child_bitfield_bit_offset = 0;
503499
bool child_is_base_class = false;
504500
bool child_is_deref_of_parent = false;
505501
uint64_t language_flags = 0;
506-
507-
const bool transparent_pointers = !synthetic_array_member;
502+
const bool transparent_pointers = true;
508503

509504
ExecutionContext exe_ctx(GetExecutionContextRef());
510505

511506
auto child_compiler_type_or_err =
512507
GetCompilerType().GetChildCompilerTypeAtIndex(
513508
&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,
509+
ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
510+
child_bitfield_bit_size, child_bitfield_bit_offset,
516511
child_is_base_class, child_is_deref_of_parent, this, language_flags);
517-
CompilerType child_compiler_type;
518-
if (!child_compiler_type_or_err)
512+
if (!child_compiler_type_or_err || !child_compiler_type_or_err->IsValid()) {
519513
LLDB_LOG_ERROR(GetLog(LLDBLog::Types),
520514
child_compiler_type_or_err.takeError(),
521515
"could not find child: {0}");
522-
else
523-
child_compiler_type = *child_compiler_type_or_err;
516+
return nullptr;
517+
}
518+
519+
return new ValueObjectChild(
520+
*this, *child_compiler_type_or_err, ConstString(child_name),
521+
child_byte_size, child_byte_offset, child_bitfield_bit_size,
522+
child_bitfield_bit_offset, child_is_base_class, child_is_deref_of_parent,
523+
eAddressTypeInvalid, language_flags);
524+
}
525+
526+
ValueObject *ValueObject::CreateSyntheticArrayMember(size_t idx) {
527+
bool omit_empty_base_classes = true;
528+
bool ignore_array_bounds = true;
529+
std::string child_name;
530+
uint32_t child_byte_size = 0;
531+
int32_t child_byte_offset = 0;
532+
uint32_t child_bitfield_bit_size = 0;
533+
uint32_t child_bitfield_bit_offset = 0;
534+
bool child_is_base_class = false;
535+
bool child_is_deref_of_parent = false;
536+
uint64_t language_flags = 0;
537+
const bool transparent_pointers = false;
524538

525-
if (child_compiler_type) {
526-
if (synthetic_index)
527-
child_byte_offset += child_byte_size * synthetic_index;
539+
ExecutionContext exe_ctx(GetExecutionContextRef());
528540

529-
ConstString child_name;
530-
if (!child_name_str.empty())
531-
child_name.SetCString(child_name_str.c_str());
541+
auto child_compiler_type_or_err =
542+
GetCompilerType().GetChildCompilerTypeAtIndex(
543+
&exe_ctx, 0, transparent_pointers, omit_empty_base_classes,
544+
ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
545+
child_bitfield_bit_size, child_bitfield_bit_offset,
546+
child_is_base_class, child_is_deref_of_parent, this, language_flags);
547+
if (!child_compiler_type_or_err) {
548+
LLDB_LOG_ERROR(GetLog(LLDBLog::Types),
549+
child_compiler_type_or_err.takeError(),
550+
"could not find child: {0}");
551+
return nullptr;
552+
}
553+
554+
if (child_compiler_type_or_err->IsValid()) {
555+
child_byte_offset += child_byte_size * idx;
532556

533-
valobj = new ValueObjectChild(
534-
*this, child_compiler_type, child_name, child_byte_size,
535-
child_byte_offset, child_bitfield_bit_size, child_bitfield_bit_offset,
536-
child_is_base_class, child_is_deref_of_parent, eAddressTypeInvalid,
537-
language_flags);
557+
return new ValueObjectChild(
558+
*this, *child_compiler_type_or_err, ConstString(child_name),
559+
child_byte_size, child_byte_offset, child_bitfield_bit_size,
560+
child_bitfield_bit_offset, child_is_base_class,
561+
child_is_deref_of_parent, eAddressTypeInvalid, language_flags);
538562
}
539563

540564
// In case of an incomplete type, try to use the ValueObject's
541565
// synthetic value to create the child ValueObject.
542-
if (!valobj && synthetic_array_member) {
543-
if (ValueObjectSP synth_valobj_sp = GetSyntheticValue()) {
544-
valobj = synth_valobj_sp
545-
->GetChildAtIndex(synthetic_index, synthetic_array_member)
546-
.get();
547-
}
548-
}
566+
if (ValueObjectSP synth_valobj_sp = GetSyntheticValue())
567+
return synth_valobj_sp->GetChildAtIndex(idx, /*can_create=*/true).get();
549568

550-
return valobj;
569+
return nullptr;
551570
}
552571

553572
bool ValueObject::GetSummaryAsCString(TypeSummaryImpl *summary_ptr,
@@ -1616,7 +1635,7 @@ ValueObjectSP ValueObject::GetSyntheticArrayMember(size_t index,
16161635
ValueObject *synthetic_child;
16171636
// We haven't made a synthetic array member for INDEX yet, so lets make
16181637
// one and cache it for any future reference.
1619-
synthetic_child = CreateChildAtIndex(0, true, index);
1638+
synthetic_child = CreateSyntheticArrayMember(index);
16201639

16211640
// Cache the value if we got one back...
16221641
if (synthetic_child) {

lldb/source/Core/ValueObjectConstResult.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -267,12 +267,6 @@ lldb::addr_t ValueObjectConstResult::GetAddressOf(bool scalar_is_load_address,
267267
return m_impl.GetAddressOf(scalar_is_load_address, address_type);
268268
}
269269

270-
ValueObject *ValueObjectConstResult::CreateChildAtIndex(
271-
size_t idx, bool synthetic_array_member, int32_t synthetic_index) {
272-
return m_impl.CreateChildAtIndex(idx, synthetic_array_member,
273-
synthetic_index);
274-
}
275-
276270
size_t ValueObjectConstResult::GetPointeeData(DataExtractor &data,
277271
uint32_t item_idx,
278272
uint32_t item_count) {

lldb/source/Core/ValueObjectConstResultCast.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,6 @@ lldb::ValueObjectSP ValueObjectConstResultCast::AddressOf(Status &error) {
4444
return m_impl.AddressOf(error);
4545
}
4646

47-
ValueObject *ValueObjectConstResultCast::CreateChildAtIndex(
48-
size_t idx, bool synthetic_array_member, int32_t synthetic_index) {
49-
return m_impl.CreateChildAtIndex(idx, synthetic_array_member,
50-
synthetic_index);
51-
}
52-
5347
size_t ValueObjectConstResultCast::GetPointeeData(DataExtractor &data,
5448
uint32_t item_idx,
5549
uint32_t item_count) {

lldb/source/Core/ValueObjectConstResultChild.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,6 @@ lldb::addr_t ValueObjectConstResultChild::GetAddressOf(
5656
return m_impl.GetAddressOf(scalar_is_load_address, address_type);
5757
}
5858

59-
ValueObject *ValueObjectConstResultChild::CreateChildAtIndex(
60-
size_t idx, bool synthetic_array_member, int32_t synthetic_index) {
61-
return m_impl.CreateChildAtIndex(idx, synthetic_array_member,
62-
synthetic_index);
63-
}
64-
6559
size_t ValueObjectConstResultChild::GetPointeeData(DataExtractor &data,
6660
uint32_t item_idx,
6761
uint32_t item_count) {

0 commit comments

Comments
 (0)