Skip to content

Commit cc079ca

Browse files
committed
Let GetNumChildren()/CalculateNumChildren() return llvm::Expected
This is an NFC change that does not yet add any error handling or change any code to return any errors.
1 parent d6d52d2 commit cc079ca

File tree

72 files changed

+402
-247
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

72 files changed

+402
-247
lines changed

lldb/include/lldb/Core/ValueObject.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ class ValueObject {
476476

477477
virtual size_t GetIndexOfChildWithName(llvm::StringRef name);
478478

479-
uint32_t GetNumChildren(uint32_t max = UINT32_MAX);
479+
llvm::Expected<uint32_t> GetNumChildren(uint32_t max = UINT32_MAX);
480480

481481
const Value &GetValue() const { return m_value; }
482482

@@ -958,7 +958,8 @@ class ValueObject {
958958
int32_t synthetic_index);
959959

960960
/// Should only be called by ValueObject::GetNumChildren().
961-
virtual uint32_t CalculateNumChildren(uint32_t max = UINT32_MAX) = 0;
961+
virtual llvm::Expected<uint32_t>
962+
CalculateNumChildren(uint32_t max = UINT32_MAX) = 0;
962963

963964
void SetNumChildren(uint32_t num_children);
964965

lldb/include/lldb/Core/ValueObjectCast.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class ValueObjectCast : public ValueObject {
3333

3434
std::optional<uint64_t> GetByteSize() override;
3535

36-
uint32_t CalculateNumChildren(uint32_t max) override;
36+
llvm::Expected<uint32_t> CalculateNumChildren(uint32_t max) override;
3737

3838
lldb::ValueType GetValueType() const override;
3939

lldb/include/lldb/Core/ValueObjectChild.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class ValueObjectChild : public ValueObject {
3939

4040
lldb::ValueType GetValueType() const override;
4141

42-
uint32_t CalculateNumChildren(uint32_t max) override;
42+
llvm::Expected<uint32_t> CalculateNumChildren(uint32_t max) override;
4343

4444
ConstString GetTypeName() override;
4545

lldb/include/lldb/Core/ValueObjectConstResult.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class ValueObjectConstResult : public ValueObject {
6767

6868
lldb::ValueType GetValueType() const override;
6969

70-
uint32_t CalculateNumChildren(uint32_t max) override;
70+
llvm::Expected<uint32_t> CalculateNumChildren(uint32_t max) override;
7171

7272
ConstString GetTypeName() override;
7373

lldb/include/lldb/Core/ValueObjectDynamicValue.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class ValueObjectDynamicValue : public ValueObject {
4343

4444
ConstString GetDisplayTypeName() override;
4545

46-
uint32_t CalculateNumChildren(uint32_t max) override;
46+
llvm::Expected<uint32_t> CalculateNumChildren(uint32_t max) override;
4747

4848
lldb::ValueType GetValueType() const override;
4949

lldb/include/lldb/Core/ValueObjectMemory.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class ValueObjectMemory : public ValueObject {
4747

4848
ConstString GetDisplayTypeName() override;
4949

50-
uint32_t CalculateNumChildren(uint32_t max) override;
50+
llvm::Expected<uint32_t> CalculateNumChildren(uint32_t max) override;
5151

5252
lldb::ValueType GetValueType() const override;
5353

lldb/include/lldb/Core/ValueObjectRegister.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class ValueObjectRegisterSet : public ValueObject {
4747

4848
ConstString GetQualifiedTypeName() override;
4949

50-
uint32_t CalculateNumChildren(uint32_t max) override;
50+
llvm::Expected<uint32_t> CalculateNumChildren(uint32_t max) override;
5151

5252
ValueObject *CreateChildAtIndex(size_t idx, bool synthetic_array_member,
5353
int32_t synthetic_index) override;
@@ -95,7 +95,7 @@ class ValueObjectRegister : public ValueObject {
9595

9696
ConstString GetTypeName() override;
9797

98-
uint32_t CalculateNumChildren(uint32_t max) override;
98+
llvm::Expected<uint32_t> CalculateNumChildren(uint32_t max) override;
9999

100100
bool SetValueFromCString(const char *value_str, Status &error) override;
101101

lldb/include/lldb/Core/ValueObjectSyntheticFilter.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class ValueObjectSynthetic : public ValueObject {
4747

4848
bool MightHaveChildren() override;
4949

50-
uint32_t CalculateNumChildren(uint32_t max) override;
50+
llvm::Expected<uint32_t> CalculateNumChildren(uint32_t max) override;
5151

5252
lldb::ValueType GetValueType() const override;
5353

lldb/include/lldb/Core/ValueObjectVTable.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class ValueObjectVTable : public ValueObject {
6464

6565
std::optional<uint64_t> GetByteSize() override;
6666

67-
uint32_t CalculateNumChildren(uint32_t max) override;
67+
llvm::Expected<uint32_t> CalculateNumChildren(uint32_t max) override;
6868

6969
ValueObject *CreateChildAtIndex(size_t idx, bool synthetic_array_member,
7070
int32_t synthetic_index) override;

lldb/include/lldb/Core/ValueObjectVariable.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class ValueObjectVariable : public ValueObject {
4646

4747
ConstString GetDisplayTypeName() override;
4848

49-
uint32_t CalculateNumChildren(uint32_t max) override;
49+
llvm::Expected<uint32_t> CalculateNumChildren(uint32_t max) override;
5050

5151
lldb::ValueType GetValueType() const override;
5252

lldb/include/lldb/DataFormatters/TypeSynthetic.h

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,13 @@ class SyntheticChildrenFrontEnd {
3838

3939
virtual ~SyntheticChildrenFrontEnd() = default;
4040

41-
virtual uint32_t CalculateNumChildren() = 0;
41+
virtual llvm::Expected<uint32_t> CalculateNumChildren() = 0;
4242

43-
virtual uint32_t CalculateNumChildren(uint32_t max) {
43+
virtual llvm::Expected<uint32_t> CalculateNumChildren(uint32_t max) {
4444
auto count = CalculateNumChildren();
45-
return count <= max ? count : max;
45+
if (!count)
46+
return count;
47+
return *count <= max ? *count : max;
4648
}
4749

4850
virtual lldb::ValueObjectSP GetChildAtIndex(uint32_t idx) = 0;
@@ -109,7 +111,7 @@ class SyntheticValueProviderFrontEnd : public SyntheticChildrenFrontEnd {
109111

110112
~SyntheticValueProviderFrontEnd() override = default;
111113

112-
uint32_t CalculateNumChildren() override { return 0; }
114+
llvm::Expected<uint32_t> CalculateNumChildren() override { return 0; }
113115

114116
lldb::ValueObjectSP GetChildAtIndex(uint32_t idx) override { return nullptr; }
115117

@@ -322,7 +324,9 @@ class TypeFilterImpl : public SyntheticChildren {
322324

323325
~FrontEnd() override = default;
324326

325-
uint32_t CalculateNumChildren() override { return filter->GetCount(); }
327+
llvm::Expected<uint32_t> CalculateNumChildren() override {
328+
return filter->GetCount();
329+
}
326330

327331
lldb::ValueObjectSP GetChildAtIndex(uint32_t idx) override {
328332
if (idx >= filter->GetCount())
@@ -426,9 +430,9 @@ class ScriptedSyntheticChildren : public SyntheticChildren {
426430

427431
bool IsValid();
428432

429-
uint32_t CalculateNumChildren() override;
433+
llvm::Expected<uint32_t> CalculateNumChildren() override;
430434

431-
uint32_t CalculateNumChildren(uint32_t max) override;
435+
llvm::Expected<uint32_t> CalculateNumChildren(uint32_t max) override;
432436

433437
lldb::ValueObjectSP GetChildAtIndex(uint32_t idx) override;
434438

lldb/include/lldb/DataFormatters/VectorIterator.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class VectorIteratorSyntheticFrontEnd : public SyntheticChildrenFrontEnd {
2424
VectorIteratorSyntheticFrontEnd(lldb::ValueObjectSP valobj_sp,
2525
llvm::ArrayRef<ConstString> item_names);
2626

27-
uint32_t CalculateNumChildren() override;
27+
llvm::Expected<uint32_t> CalculateNumChildren() override;
2828

2929
lldb::ValueObjectSP GetChildAtIndex(uint32_t idx) override;
3030

lldb/include/lldb/Symbol/CompilerType.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -386,8 +386,9 @@ class CompilerType {
386386

387387
std::optional<size_t> GetTypeBitAlign(ExecutionContextScope *exe_scope) const;
388388

389-
uint32_t GetNumChildren(bool omit_empty_base_classes,
390-
const ExecutionContext *exe_ctx) const;
389+
llvm::Expected<uint32_t>
390+
GetNumChildren(bool omit_empty_base_classes,
391+
const ExecutionContext *exe_ctx) const;
391392

392393
lldb::BasicType GetBasicTypeEnumeration() const;
393394

lldb/include/lldb/Symbol/Type.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ class Type : public std::enable_shared_from_this<Type>, public UserID {
440440

441441
std::optional<uint64_t> GetByteSize(ExecutionContextScope *exe_scope);
442442

443-
uint32_t GetNumChildren(bool omit_empty_base_classes);
443+
llvm::Expected<uint32_t> GetNumChildren(bool omit_empty_base_classes);
444444

445445
bool IsAggregateType();
446446

lldb/include/lldb/Symbol/TypeSystem.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -300,9 +300,10 @@ class TypeSystem : public PluginInterface,
300300

301301
virtual lldb::Format GetFormat(lldb::opaque_compiler_type_t type) = 0;
302302

303-
virtual uint32_t GetNumChildren(lldb::opaque_compiler_type_t type,
304-
bool omit_empty_base_classes,
305-
const ExecutionContext *exe_ctx) = 0;
303+
virtual llvm::Expected<uint32_t>
304+
GetNumChildren(lldb::opaque_compiler_type_t type,
305+
bool omit_empty_base_classes,
306+
const ExecutionContext *exe_ctx) = 0;
306307

307308
virtual CompilerType GetBuiltinTypeByName(ConstString name);
308309

lldb/include/lldb/Target/StackFrameRecognizer.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,8 @@ class ValueObjectRecognizerSynthesizedValue : public ValueObject {
164164
m_value = m_parent->GetValue();
165165
return true;
166166
}
167-
uint32_t CalculateNumChildren(uint32_t max = UINT32_MAX) override {
167+
llvm::Expected<uint32_t>
168+
CalculateNumChildren(uint32_t max = UINT32_MAX) override {
168169
return m_parent->GetNumChildren(max);
169170
}
170171
CompilerType GetCompilerTypeImpl() override {

lldb/include/lldb/Utility/Log.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,4 +373,16 @@ template <typename Cat> Log *GetLog(Cat mask) {
373373
::llvm::consumeError(::std::move(error_private)); \
374374
} while (0)
375375

376+
/// Like \c llvm::expectedToStdOptional(value_or_err).value_or(value) but
377+
/// logs any error to channel.
378+
template <typename T>
379+
T ValueOrLogV(lldb_private::Log *log, llvm::Expected<T> &&value_or_err,
380+
T value) {
381+
if (value_or_err)
382+
return *value_or_err;
383+
if (log && log->GetVerbose())
384+
LLDB_LOG_ERROR(log, value_or_err.takeError(), "{0}");
385+
return value;
386+
}
387+
376388
#endif // LLDB_UTILITY_LOG_H

lldb/source/API/SBValue.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -947,7 +947,8 @@ uint32_t SBValue::GetNumChildren(uint32_t max) {
947947
ValueLocker locker;
948948
lldb::ValueObjectSP value_sp(GetSP(locker));
949949
if (value_sp)
950-
num_children = value_sp->GetNumChildren(max);
950+
num_children =
951+
ValueOrLogV(GetLog(LLDBLog::API), value_sp->GetNumChildren(max), 0u);
951952

952953
return num_children;
953954
}

lldb/source/Core/FormatEntity.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -926,7 +926,9 @@ static bool DumpValue(Stream &s, const SymbolContext *sc,
926926
s.PutChar('[');
927927

928928
if (index_higher < 0)
929-
index_higher = valobj->GetNumChildren() - 1;
929+
index_higher = ValueOrLogV(GetLog(LLDBLog::DataFormatters),
930+
valobj->GetNumChildren(), 0u) -
931+
1;
930932

931933
uint32_t max_num_children =
932934
target->GetTargetSP()->GetMaximumNumberOfChildrenToDisplay();

lldb/source/Core/IOHandlerCursesGUI.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4519,7 +4519,8 @@ struct Row {
45194519
calculated_children = true;
45204520
ValueObjectSP valobj = value.GetSP();
45214521
if (valobj) {
4522-
const size_t num_children = valobj->GetNumChildren();
4522+
const uint32_t num_children = ValueOrLogV(
4523+
GetLog(LLDBLog::DataFormatters), valobj->GetNumChildren(), 0u);
45234524
for (size_t i = 0; i < num_children; ++i) {
45244525
children.push_back(Row(valobj->GetChildAtIndex(i), this));
45254526
}

lldb/source/Core/ValueObject.cpp

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,8 @@ ValueObjectSP ValueObject::GetChildAtIndex(uint32_t idx, bool can_create) {
377377
// We may need to update our value if we are dynamic
378378
if (IsPossibleDynamicType())
379379
UpdateValueIfNeeded(false);
380-
if (idx < GetNumChildren()) {
380+
if (idx <
381+
ValueOrLogV(GetLog(LLDBLog::DataFormatters), GetNumChildren(), 0u)) {
381382
// Check if we have already made the child value object?
382383
if (can_create && !m_children.HasChildAtIndex(idx)) {
383384
// No we haven't created the child at this index, so lets have our
@@ -440,7 +441,7 @@ ValueObjectSP ValueObject::GetChildMemberWithName(llvm::StringRef name,
440441
return child_sp;
441442
}
442443

443-
uint32_t ValueObject::GetNumChildren(uint32_t max) {
444+
llvm::Expected<uint32_t> ValueObject::GetNumChildren(uint32_t max) {
444445
UpdateValueIfNeeded();
445446

446447
if (max < UINT32_MAX) {
@@ -452,7 +453,11 @@ uint32_t ValueObject::GetNumChildren(uint32_t max) {
452453
}
453454

454455
if (!m_flags.m_children_count_valid) {
455-
SetNumChildren(CalculateNumChildren());
456+
auto num_children_or_err = CalculateNumChildren();
457+
if (num_children_or_err)
458+
SetNumChildren(*num_children_or_err);
459+
else
460+
return num_children_or_err;
456461
}
457462
return m_children.GetChildrenCount();
458463
}
@@ -464,7 +469,8 @@ bool ValueObject::MightHaveChildren() {
464469
if (type_info & (eTypeHasChildren | eTypeIsPointer | eTypeIsReference))
465470
has_children = true;
466471
} else {
467-
has_children = GetNumChildren() > 0;
472+
has_children =
473+
ValueOrLogV(GetLog(LLDBLog::DataFormatters), GetNumChildren(), 0u) > 0;
468474
}
469475
return has_children;
470476
}
@@ -1176,7 +1182,8 @@ bool ValueObject::DumpPrintableRepresentation(
11761182
if (flags.Test(eTypeIsArray)) {
11771183
if ((custom_format == eFormatBytes) ||
11781184
(custom_format == eFormatBytesWithASCII)) {
1179-
const size_t count = GetNumChildren();
1185+
const size_t count = ValueOrLogV(GetLog(LLDBLog::DataFormatters),
1186+
GetNumChildren(), 0u);
11801187

11811188
s << '[';
11821189
for (size_t low = 0; low < count; low++) {
@@ -1215,7 +1222,8 @@ bool ValueObject::DumpPrintableRepresentation(
12151222
// format should be printed
12161223
// directly
12171224
{
1218-
const size_t count = GetNumChildren();
1225+
const size_t count = ValueOrLogV(GetLog(LLDBLog::DataFormatters),
1226+
GetNumChildren(), 0u);
12191227

12201228
Format format = FormatManager::GetSingleItemFormat(custom_format);
12211229

@@ -1294,7 +1302,9 @@ bool ValueObject::DumpPrintableRepresentation(
12941302
break;
12951303

12961304
case eValueObjectRepresentationStyleChildrenCount:
1297-
strm.Printf("%" PRIu64 "", (uint64_t)GetNumChildren());
1305+
strm.Printf("%" PRIu64 "",
1306+
(uint64_t)ValueOrLogV(GetLog(LLDBLog::DataFormatters),
1307+
GetNumChildren(), 0u));
12981308
str = strm.GetString();
12991309
break;
13001310

@@ -2320,7 +2330,9 @@ ValueObjectSP ValueObject::GetValueForExpressionPath_Impl(
23202330
child_valobj_sp = root->GetSyntheticArrayMember(index, true);
23212331
if (!child_valobj_sp)
23222332
if (root->HasSyntheticValue() &&
2323-
root->GetSyntheticValue()->GetNumChildren() > index)
2333+
llvm::expectedToStdOptional(
2334+
root->GetSyntheticValue()->GetNumChildren())
2335+
.value_or(0) > index)
23242336
child_valobj_sp =
23252337
root->GetSyntheticValue()->GetChildAtIndex(index);
23262338
if (child_valobj_sp) {

lldb/source/Core/ValueObjectCast.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,13 @@ ValueObjectCast::~ValueObjectCast() = default;
4141

4242
CompilerType ValueObjectCast::GetCompilerTypeImpl() { return m_cast_type; }
4343

44-
uint32_t ValueObjectCast::CalculateNumChildren(uint32_t max) {
44+
llvm::Expected<uint32_t> ValueObjectCast::CalculateNumChildren(uint32_t max) {
4545
ExecutionContext exe_ctx(GetExecutionContextRef());
4646
auto children_count = GetCompilerType().GetNumChildren(
4747
true, &exe_ctx);
48-
return children_count <= max ? children_count : max;
48+
if (!children_count)
49+
return children_count;
50+
return *children_count <= max ? *children_count : max;
4951
}
5052

5153
std::optional<uint64_t> ValueObjectCast::GetByteSize() {

lldb/source/Core/ValueObjectChild.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,12 @@ lldb::ValueType ValueObjectChild::GetValueType() const {
4949
return m_parent->GetValueType();
5050
}
5151

52-
uint32_t ValueObjectChild::CalculateNumChildren(uint32_t max) {
52+
llvm::Expected<uint32_t> ValueObjectChild::CalculateNumChildren(uint32_t max) {
5353
ExecutionContext exe_ctx(GetExecutionContextRef());
5454
auto children_count = GetCompilerType().GetNumChildren(true, &exe_ctx);
55-
return children_count <= max ? children_count : max;
55+
if (!children_count)
56+
return children_count;
57+
return *children_count <= max ? *children_count : max;
5658
}
5759

5860
static void AdjustForBitfieldness(ConstString &name,

lldb/source/Core/ValueObjectConstResult.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,10 +216,13 @@ std::optional<uint64_t> ValueObjectConstResult::GetByteSize() {
216216

217217
void ValueObjectConstResult::SetByteSize(size_t size) { m_byte_size = size; }
218218

219-
uint32_t ValueObjectConstResult::CalculateNumChildren(uint32_t max) {
219+
llvm::Expected<uint32_t>
220+
ValueObjectConstResult::CalculateNumChildren(uint32_t max) {
220221
ExecutionContext exe_ctx(GetExecutionContextRef());
221222
auto children_count = GetCompilerType().GetNumChildren(true, &exe_ctx);
222-
return children_count <= max ? children_count : max;
223+
if (!children_count)
224+
return children_count;
225+
return *children_count <= max ? *children_count : max;
223226
}
224227

225228
ConstString ValueObjectConstResult::GetTypeName() {

0 commit comments

Comments
 (0)