Skip to content

Commit 24ef40f

Browse files
[lldb] Upgrade GetIndexOfChildWithName to use llvm::Expected (llvm#136693)
This patch replaces the use of `UINT32_MAX` as the error return value of `GetIndexOfChildWithName` with `llvm::Expected`. 1. Replace `CalculateNumChildrenIgnoringErrors` with `CalculateNumChildren`. See [this comment](llvm#136693 (comment)). 2. Update `lldb_private::formatters::ExtractIndexFromString` to use `llvm::Expected`. See [this comment](llvm#136693 (comment)). 3. Create a new class which carries both user and internal errors. See [this comment](llvm#136693 (comment)).
1 parent e49951f commit 24ef40f

Some content is hidden

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

52 files changed

+410
-236
lines changed

lldb/include/lldb/DataFormatters/TypeSynthetic.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class SyntheticChildrenFrontEnd {
5151

5252
virtual lldb::ValueObjectSP GetChildAtIndex(uint32_t idx) = 0;
5353

54-
virtual size_t GetIndexOfChildWithName(ConstString name) = 0;
54+
virtual llvm::Expected<size_t> GetIndexOfChildWithName(ConstString name) = 0;
5555

5656
/// This function is assumed to always succeed and if it fails, the front-end
5757
/// should know to deal with it in the correct way (most probably, by refusing
@@ -117,8 +117,9 @@ class SyntheticValueProviderFrontEnd : public SyntheticChildrenFrontEnd {
117117

118118
lldb::ValueObjectSP GetChildAtIndex(uint32_t idx) override { return nullptr; }
119119

120-
size_t GetIndexOfChildWithName(ConstString name) override {
121-
return UINT32_MAX;
120+
llvm::Expected<size_t> GetIndexOfChildWithName(ConstString name) override {
121+
return llvm::createStringError("Type has no child named '%s'",
122+
name.AsCString());
122123
}
123124

124125
lldb::ChildCacheState Update() override {
@@ -343,7 +344,7 @@ class TypeFilterImpl : public SyntheticChildren {
343344

344345
bool MightHaveChildren() override { return filter->GetCount() > 0; }
345346

346-
size_t GetIndexOfChildWithName(ConstString name) override;
347+
llvm::Expected<size_t> GetIndexOfChildWithName(ConstString name) override;
347348

348349
typedef std::shared_ptr<SyntheticChildrenFrontEnd> SharedPointer;
349350

@@ -442,7 +443,7 @@ class ScriptedSyntheticChildren : public SyntheticChildren {
442443

443444
bool MightHaveChildren() override;
444445

445-
size_t GetIndexOfChildWithName(ConstString name) override;
446+
llvm::Expected<size_t> GetIndexOfChildWithName(ConstString name) override;
446447

447448
lldb::ValueObjectSP GetSyntheticValue() override;
448449

lldb/include/lldb/DataFormatters/VectorIterator.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class VectorIteratorSyntheticFrontEnd : public SyntheticChildrenFrontEnd {
3232

3333
bool MightHaveChildren() override;
3434

35-
size_t GetIndexOfChildWithName(ConstString name) override;
35+
llvm::Expected<size_t> GetIndexOfChildWithName(ConstString name) override;
3636

3737
private:
3838
ExecutionContextRef m_exe_ctx_ref;

lldb/include/lldb/Interpreter/ScriptInterpreter.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -385,10 +385,10 @@ class ScriptInterpreter : public PluginInterface {
385385
return lldb::ValueObjectSP();
386386
}
387387

388-
virtual int
388+
virtual llvm::Expected<int>
389389
GetIndexOfChildWithName(const StructuredData::ObjectSP &implementor,
390390
const char *child_name) {
391-
return UINT32_MAX;
391+
return llvm::createStringError("Type has no child named '%s'", child_name);
392392
}
393393

394394
virtual bool

lldb/include/lldb/Symbol/CompilerType.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -481,9 +481,10 @@ class CompilerType {
481481

482482
/// Lookup a child given a name. This function will match base class names and
483483
/// member member names in "clang_type" only, not descendants.
484-
uint32_t GetIndexOfChildWithName(llvm::StringRef name,
485-
ExecutionContext *exe_ctx,
486-
bool omit_empty_base_classes) const;
484+
485+
llvm::Expected<uint32_t>
486+
GetIndexOfChildWithName(llvm::StringRef name, ExecutionContext *exe_ctx,
487+
bool omit_empty_base_classes) const;
487488

488489
/// Lookup a child member given a name. This function will match member names
489490
/// only and will descend into "clang_type" children in search for the first

lldb/include/lldb/Symbol/TypeSystem.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -409,10 +409,10 @@ class TypeSystem : public PluginInterface,
409409

410410
// Lookup a child given a name. This function will match base class names and
411411
// member member names in "clang_type" only, not descendants.
412-
virtual uint32_t GetIndexOfChildWithName(lldb::opaque_compiler_type_t type,
413-
llvm::StringRef name,
414-
ExecutionContext *exe_ctx,
415-
bool omit_empty_base_classes) = 0;
412+
virtual llvm::Expected<uint32_t>
413+
GetIndexOfChildWithName(lldb::opaque_compiler_type_t type,
414+
llvm::StringRef name, ExecutionContext *exe_ctx,
415+
bool omit_empty_base_classes) = 0;
416416

417417
// Lookup a child member given a name. This function will match member names
418418
// only and will descend into "clang_type" children in search for the first

lldb/include/lldb/ValueObject/ValueObject.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,7 @@ class ValueObject {
502502
virtual lldb::ValueObjectSP GetChildMemberWithName(llvm::StringRef name,
503503
bool can_create = true);
504504

505-
virtual size_t GetIndexOfChildWithName(llvm::StringRef name);
505+
virtual llvm::Expected<size_t> GetIndexOfChildWithName(llvm::StringRef name);
506506

507507
llvm::Expected<uint32_t> GetNumChildren(uint32_t max = UINT32_MAX);
508508
/// Like \c GetNumChildren but returns 0 on error. You probably

lldb/include/lldb/ValueObject/ValueObjectRegister.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class ValueObjectRegisterSet : public ValueObject {
5252
lldb::ValueObjectSP GetChildMemberWithName(llvm::StringRef name,
5353
bool can_create = true) override;
5454

55-
size_t GetIndexOfChildWithName(llvm::StringRef name) override;
55+
llvm::Expected<size_t> GetIndexOfChildWithName(llvm::StringRef name) override;
5656

5757
protected:
5858
bool UpdateValue() override;

lldb/include/lldb/ValueObject/ValueObjectSyntheticFilter.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class ValueObjectSynthetic : public ValueObject {
5757
lldb::ValueObjectSP GetChildMemberWithName(llvm::StringRef name,
5858
bool can_create = true) override;
5959

60-
size_t GetIndexOfChildWithName(llvm::StringRef name) override;
60+
llvm::Expected<size_t> GetIndexOfChildWithName(llvm::StringRef name) override;
6161

6262
lldb::ValueObjectSP
6363
GetDynamicValue(lldb::DynamicValueType valueType) override;

lldb/source/API/SBValue.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -688,13 +688,15 @@ SBValue SBValue::GetChildAtIndex(uint32_t idx,
688688
uint32_t SBValue::GetIndexOfChildWithName(const char *name) {
689689
LLDB_INSTRUMENT_VA(this, name);
690690

691-
uint32_t idx = UINT32_MAX;
692691
ValueLocker locker;
693692
lldb::ValueObjectSP value_sp(GetSP(locker));
694693
if (value_sp) {
695-
idx = value_sp->GetIndexOfChildWithName(name);
694+
if (auto idx_or_err = value_sp->GetIndexOfChildWithName(name))
695+
return *idx_or_err;
696+
else
697+
llvm::consumeError(idx_or_err.takeError());
696698
}
697-
return idx;
699+
return UINT32_MAX;
698700
}
699701

700702
SBValue SBValue::GetChildMemberWithName(const char *name) {

lldb/source/DataFormatters/FormatterBytecode.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "FormatterBytecode.h"
1010
#include "lldb/Utility/LLDBLog.h"
1111
#include "lldb/ValueObject/ValueObject.h"
12+
#include "lldb/ValueObject/ValueObjectConstResult.h"
1213
#include "llvm/ADT/StringExtras.h"
1314
#include "llvm/Support/DataExtractor.h"
1415
#include "llvm/Support/Format.h"
@@ -486,7 +487,10 @@ llvm::Error Interpret(std::vector<ControlStackElement> &control,
486487
TYPE_CHECK(Object, String);
487488
auto name = data.Pop<std::string>();
488489
POP_VALOBJ(valobj);
489-
data.Push((uint64_t)valobj->GetIndexOfChildWithName(name));
490+
if (auto index_or_err = valobj->GetIndexOfChildWithName(name))
491+
data.Push((uint64_t)*index_or_err);
492+
else
493+
return index_or_err.takeError();
490494
break;
491495
}
492496
case sel_get_type: {

0 commit comments

Comments
 (0)