Skip to content

Commit b3d1302

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

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

+408
-232
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
@@ -30,7 +30,7 @@ class VectorIteratorSyntheticFrontEnd : public SyntheticChildrenFrontEnd {
3030

3131
lldb::ChildCacheState Update() override;
3232

33-
size_t GetIndexOfChildWithName(ConstString name) override;
33+
llvm::Expected<size_t> GetIndexOfChildWithName(ConstString name) override;
3434

3535
private:
3636
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
@@ -368,10 +368,10 @@ class ScriptInterpreter : public PluginInterface {
368368
return lldb::ValueObjectSP();
369369
}
370370

371-
virtual int
371+
virtual llvm::Expected<int>
372372
GetIndexOfChildWithName(const StructuredData::ObjectSP &implementor,
373373
const char *child_name) {
374-
return UINT32_MAX;
374+
return llvm::createStringError("Type has no child named '%s'", child_name);
375375
}
376376

377377
virtual bool

lldb/include/lldb/Symbol/CompilerType.h

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

445445
/// Lookup a child given a name. This function will match base class names and
446446
/// member member names in "clang_type" only, not descendants.
447-
uint32_t GetIndexOfChildWithName(llvm::StringRef name,
448-
bool omit_empty_base_classes) const;
447+
llvm::Expected<uint32_t>
448+
GetIndexOfChildWithName(llvm::StringRef name,
449+
bool omit_empty_base_classes) const;
449450

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

lldb/include/lldb/Symbol/TypeSystem.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -373,9 +373,12 @@ class TypeSystem : public PluginInterface,
373373
bool &child_is_base_class, bool &child_is_deref_of_parent,
374374
ValueObject *valobj, uint64_t &language_flags) = 0;
375375

376-
virtual uint32_t GetIndexOfChildWithName(lldb::opaque_compiler_type_t type,
377-
llvm::StringRef name,
378-
bool omit_empty_base_classes) = 0;
376+
// Lookup a child given a name. This function will match base class names and
377+
// member member names in "clang_type" only, not descendants.
378+
virtual llvm::Expected<uint32_t>
379+
GetIndexOfChildWithName(lldb::opaque_compiler_type_t type,
380+
llvm::StringRef name,
381+
bool omit_empty_base_classes) = 0;
379382

380383
virtual size_t GetIndexOfChildMemberWithName(
381384
lldb::opaque_compiler_type_t type, llvm::StringRef name,

lldb/include/lldb/ValueObject/ValueObject.h

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

501-
virtual size_t GetIndexOfChildWithName(llvm::StringRef name);
501+
virtual llvm::Expected<size_t> GetIndexOfChildWithName(llvm::StringRef name);
502502

503503
llvm::Expected<uint32_t> GetNumChildren(uint32_t max = UINT32_MAX);
504504
/// 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
@@ -704,13 +704,15 @@ SBValue SBValue::GetChildAtIndex(uint32_t idx,
704704
uint32_t SBValue::GetIndexOfChildWithName(const char *name) {
705705
LLDB_INSTRUMENT_VA(this, name);
706706

707-
uint32_t idx = UINT32_MAX;
708707
ValueLocker locker;
709708
lldb::ValueObjectSP value_sp(GetSP(locker));
710709
if (value_sp) {
711-
idx = value_sp->GetIndexOfChildWithName(name);
710+
if (auto idx_or_err = value_sp->GetIndexOfChildWithName(name))
711+
return *idx_or_err;
712+
else
713+
llvm::consumeError(idx_or_err.takeError());
712714
}
713-
return idx;
715+
return UINT32_MAX;
714716
}
715717

716718
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"
@@ -489,7 +490,10 @@ llvm::Error Interpret(std::vector<ControlStackElement> &control,
489490
TYPE_CHECK(Object, String);
490491
auto name = data.Pop<std::string>();
491492
POP_VALOBJ(valobj);
492-
data.Push((uint64_t)valobj->GetIndexOfChildWithName(name));
493+
if (auto index_or_err = valobj->GetIndexOfChildWithName(name))
494+
data.Push((uint64_t)*index_or_err);
495+
else
496+
return index_or_err.takeError();
493497
break;
494498
}
495499
case sel_get_type: {

0 commit comments

Comments
 (0)