Skip to content

Commit

Permalink
Change GetNumChildren()/CalculateNumChildren() methods return llvm::E…
Browse files Browse the repository at this point in the history
…xpected (#84219)

Change GetNumChildren()/CalculateNumChildren() methods return
llvm::Expected

This is an NFC change that does not yet add any error handling or change
any code to return any errors.

This is the second big change in the patch series started with
#83501

A follow-up PR will wire up error handling.
  • Loading branch information
adrian-prantl committed Mar 9, 2024
1 parent c228289 commit 624ea68
Show file tree
Hide file tree
Showing 73 changed files with 399 additions and 248 deletions.
11 changes: 9 additions & 2 deletions lldb/include/lldb/Core/ValueObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,13 @@ class ValueObject {

virtual size_t GetIndexOfChildWithName(llvm::StringRef name);

uint32_t GetNumChildren(uint32_t max = UINT32_MAX);
llvm::Expected<uint32_t> GetNumChildren(uint32_t max = UINT32_MAX);
/// Like \c GetNumChildren but returns 0 on error. You probably
/// shouldn't be using this function. It exists primarily to ease the
/// transition to more pervasive error handling while not all APIs
/// have been updated.
uint32_t GetNumChildrenIgnoringErrors(uint32_t max = UINT32_MAX);
bool HasChildren() { return GetNumChildrenIgnoringErrors() > 0; }

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

Expand Down Expand Up @@ -958,7 +964,8 @@ class ValueObject {
int32_t synthetic_index);

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

void SetNumChildren(uint32_t num_children);

Expand Down
2 changes: 1 addition & 1 deletion lldb/include/lldb/Core/ValueObjectCast.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class ValueObjectCast : public ValueObject {

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

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

lldb::ValueType GetValueType() const override;

Expand Down
2 changes: 1 addition & 1 deletion lldb/include/lldb/Core/ValueObjectChild.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class ValueObjectChild : public ValueObject {

lldb::ValueType GetValueType() const override;

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

ConstString GetTypeName() override;

Expand Down
2 changes: 1 addition & 1 deletion lldb/include/lldb/Core/ValueObjectConstResult.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class ValueObjectConstResult : public ValueObject {

lldb::ValueType GetValueType() const override;

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

ConstString GetTypeName() override;

Expand Down
2 changes: 1 addition & 1 deletion lldb/include/lldb/Core/ValueObjectDynamicValue.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class ValueObjectDynamicValue : public ValueObject {

ConstString GetDisplayTypeName() override;

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

lldb::ValueType GetValueType() const override;

Expand Down
2 changes: 1 addition & 1 deletion lldb/include/lldb/Core/ValueObjectMemory.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class ValueObjectMemory : public ValueObject {

ConstString GetDisplayTypeName() override;

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

lldb::ValueType GetValueType() const override;

Expand Down
4 changes: 2 additions & 2 deletions lldb/include/lldb/Core/ValueObjectRegister.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class ValueObjectRegisterSet : public ValueObject {

ConstString GetQualifiedTypeName() override;

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

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

ConstString GetTypeName() override;

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

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

Expand Down
2 changes: 1 addition & 1 deletion lldb/include/lldb/Core/ValueObjectSyntheticFilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class ValueObjectSynthetic : public ValueObject {

bool MightHaveChildren() override;

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

lldb::ValueType GetValueType() const override;

Expand Down
2 changes: 1 addition & 1 deletion lldb/include/lldb/Core/ValueObjectVTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class ValueObjectVTable : public ValueObject {

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

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

ValueObject *CreateChildAtIndex(size_t idx, bool synthetic_array_member,
int32_t synthetic_index) override;
Expand Down
2 changes: 1 addition & 1 deletion lldb/include/lldb/Core/ValueObjectVariable.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class ValueObjectVariable : public ValueObject {

ConstString GetDisplayTypeName() override;

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

lldb::ValueType GetValueType() const override;

Expand Down
20 changes: 13 additions & 7 deletions lldb/include/lldb/DataFormatters/TypeSynthetic.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,17 @@ class SyntheticChildrenFrontEnd {

virtual ~SyntheticChildrenFrontEnd() = default;

virtual uint32_t CalculateNumChildren() = 0;
virtual llvm::Expected<uint32_t> CalculateNumChildren() = 0;

virtual uint32_t CalculateNumChildren(uint32_t max) {
virtual llvm::Expected<uint32_t> CalculateNumChildren(uint32_t max) {
auto count = CalculateNumChildren();
return count <= max ? count : max;
if (!count)
return count;
return *count <= max ? *count : max;
}

uint32_t CalculateNumChildrenIgnoringErrors(uint32_t max = UINT32_MAX);

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

virtual size_t GetIndexOfChildWithName(ConstString name) = 0;
Expand Down Expand Up @@ -109,7 +113,7 @@ class SyntheticValueProviderFrontEnd : public SyntheticChildrenFrontEnd {

~SyntheticValueProviderFrontEnd() override = default;

uint32_t CalculateNumChildren() override { return 0; }
llvm::Expected<uint32_t> CalculateNumChildren() override { return 0; }

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

Expand Down Expand Up @@ -322,7 +326,9 @@ class TypeFilterImpl : public SyntheticChildren {

~FrontEnd() override = default;

uint32_t CalculateNumChildren() override { return filter->GetCount(); }
llvm::Expected<uint32_t> CalculateNumChildren() override {
return filter->GetCount();
}

lldb::ValueObjectSP GetChildAtIndex(uint32_t idx) override {
if (idx >= filter->GetCount())
Expand Down Expand Up @@ -426,9 +432,9 @@ class ScriptedSyntheticChildren : public SyntheticChildren {

bool IsValid();

uint32_t CalculateNumChildren() override;
llvm::Expected<uint32_t> CalculateNumChildren() override;

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

lldb::ValueObjectSP GetChildAtIndex(uint32_t idx) override;

Expand Down
2 changes: 1 addition & 1 deletion lldb/include/lldb/DataFormatters/VectorIterator.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class VectorIteratorSyntheticFrontEnd : public SyntheticChildrenFrontEnd {
VectorIteratorSyntheticFrontEnd(lldb::ValueObjectSP valobj_sp,
llvm::ArrayRef<ConstString> item_names);

uint32_t CalculateNumChildren() override;
llvm::Expected<uint32_t> CalculateNumChildren() override;

lldb::ValueObjectSP GetChildAtIndex(uint32_t idx) override;

Expand Down
5 changes: 3 additions & 2 deletions lldb/include/lldb/Symbol/CompilerType.h
Original file line number Diff line number Diff line change
Expand Up @@ -386,8 +386,9 @@ class CompilerType {

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

uint32_t GetNumChildren(bool omit_empty_base_classes,
const ExecutionContext *exe_ctx) const;
llvm::Expected<uint32_t>
GetNumChildren(bool omit_empty_base_classes,
const ExecutionContext *exe_ctx) const;

lldb::BasicType GetBasicTypeEnumeration() const;

Expand Down
2 changes: 1 addition & 1 deletion lldb/include/lldb/Symbol/Type.h
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ class Type : public std::enable_shared_from_this<Type>, public UserID {

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

uint32_t GetNumChildren(bool omit_empty_base_classes);
llvm::Expected<uint32_t> GetNumChildren(bool omit_empty_base_classes);

bool IsAggregateType();

Expand Down
7 changes: 4 additions & 3 deletions lldb/include/lldb/Symbol/TypeSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -300,9 +300,10 @@ class TypeSystem : public PluginInterface,

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

virtual uint32_t GetNumChildren(lldb::opaque_compiler_type_t type,
bool omit_empty_base_classes,
const ExecutionContext *exe_ctx) = 0;
virtual llvm::Expected<uint32_t>
GetNumChildren(lldb::opaque_compiler_type_t type,
bool omit_empty_base_classes,
const ExecutionContext *exe_ctx) = 0;

virtual CompilerType GetBuiltinTypeByName(ConstString name);

Expand Down
3 changes: 2 additions & 1 deletion lldb/include/lldb/Target/StackFrameRecognizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,8 @@ class ValueObjectRecognizerSynthesizedValue : public ValueObject {
m_value = m_parent->GetValue();
return true;
}
uint32_t CalculateNumChildren(uint32_t max = UINT32_MAX) override {
llvm::Expected<uint32_t>
CalculateNumChildren(uint32_t max = UINT32_MAX) override {
return m_parent->GetNumChildren(max);
}
CompilerType GetCompilerTypeImpl() override {
Expand Down
14 changes: 14 additions & 0 deletions lldb/include/lldb/Utility/Log.h
Original file line number Diff line number Diff line change
Expand Up @@ -373,4 +373,18 @@ template <typename Cat> Log *GetLog(Cat mask) {
::llvm::consumeError(::std::move(error_private)); \
} while (0)

// Write message to the verbose log, if error is set. In the log
// message refer to the error with {0}. Error is cleared regardless of
// whether logging is enabled.
#define LLDB_LOG_ERRORV(log, error, ...) \
do { \
::lldb_private::Log *log_private = (log); \
::llvm::Error error_private = (error); \
if (log_private && log_private->GetVerbose() && error_private) { \
log_private->FormatError(::std::move(error_private), __FILE__, __func__, \
__VA_ARGS__); \
} else \
::llvm::consumeError(::std::move(error_private)); \
} while (0)

#endif // LLDB_UTILITY_LOG_H
2 changes: 1 addition & 1 deletion lldb/source/API/SBValue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -947,7 +947,7 @@ uint32_t SBValue::GetNumChildren(uint32_t max) {
ValueLocker locker;
lldb::ValueObjectSP value_sp(GetSP(locker));
if (value_sp)
num_children = value_sp->GetNumChildren(max);
num_children = value_sp->GetNumChildrenIgnoringErrors(max);

return num_children;
}
Expand Down
2 changes: 1 addition & 1 deletion lldb/source/Core/FormatEntity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -926,7 +926,7 @@ static bool DumpValue(Stream &s, const SymbolContext *sc,
s.PutChar('[');

if (index_higher < 0)
index_higher = valobj->GetNumChildren() - 1;
index_higher = valobj->GetNumChildrenIgnoringErrors() - 1;

uint32_t max_num_children =
target->GetTargetSP()->GetMaximumNumberOfChildrenToDisplay();
Expand Down
2 changes: 1 addition & 1 deletion lldb/source/Core/IOHandlerCursesGUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4519,7 +4519,7 @@ struct Row {
calculated_children = true;
ValueObjectSP valobj = value.GetSP();
if (valobj) {
const size_t num_children = valobj->GetNumChildren();
const uint32_t num_children = valobj->GetNumChildrenIgnoringErrors();
for (size_t i = 0; i < num_children; ++i) {
children.push_back(Row(valobj->GetChildAtIndex(i), this));
}
Expand Down
31 changes: 23 additions & 8 deletions lldb/source/Core/ValueObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ ValueObjectSP ValueObject::GetChildAtIndex(uint32_t idx, bool can_create) {
// We may need to update our value if we are dynamic
if (IsPossibleDynamicType())
UpdateValueIfNeeded(false);
if (idx < GetNumChildren()) {
if (idx < GetNumChildrenIgnoringErrors()) {
// Check if we have already made the child value object?
if (can_create && !m_children.HasChildAtIndex(idx)) {
// No we haven't created the child at this index, so lets have our
Expand Down Expand Up @@ -440,7 +440,7 @@ ValueObjectSP ValueObject::GetChildMemberWithName(llvm::StringRef name,
return child_sp;
}

uint32_t ValueObject::GetNumChildren(uint32_t max) {
llvm::Expected<uint32_t> ValueObject::GetNumChildren(uint32_t max) {
UpdateValueIfNeeded();

if (max < UINT32_MAX) {
Expand All @@ -452,19 +452,32 @@ uint32_t ValueObject::GetNumChildren(uint32_t max) {
}

if (!m_flags.m_children_count_valid) {
SetNumChildren(CalculateNumChildren());
auto num_children_or_err = CalculateNumChildren();
if (num_children_or_err)
SetNumChildren(*num_children_or_err);
else
return num_children_or_err;
}
return m_children.GetChildrenCount();
}

uint32_t ValueObject::GetNumChildrenIgnoringErrors(uint32_t max) {
auto value_or_err = GetNumChildren(max);
if (value_or_err)
return *value_or_err;
LLDB_LOG_ERRORV(GetLog(LLDBLog::DataFormatters), value_or_err.takeError(),
"{0}");
return 0;
}

bool ValueObject::MightHaveChildren() {
bool has_children = false;
const uint32_t type_info = GetTypeInfo();
if (type_info) {
if (type_info & (eTypeHasChildren | eTypeIsPointer | eTypeIsReference))
has_children = true;
} else {
has_children = GetNumChildren() > 0;
has_children = GetNumChildrenIgnoringErrors() > 0;
}
return has_children;
}
Expand Down Expand Up @@ -1176,7 +1189,7 @@ bool ValueObject::DumpPrintableRepresentation(
if (flags.Test(eTypeIsArray)) {
if ((custom_format == eFormatBytes) ||
(custom_format == eFormatBytesWithASCII)) {
const size_t count = GetNumChildren();
const size_t count = GetNumChildrenIgnoringErrors();

s << '[';
for (size_t low = 0; low < count; low++) {
Expand Down Expand Up @@ -1215,7 +1228,7 @@ bool ValueObject::DumpPrintableRepresentation(
// format should be printed
// directly
{
const size_t count = GetNumChildren();
const size_t count = GetNumChildrenIgnoringErrors();

Format format = FormatManager::GetSingleItemFormat(custom_format);

Expand Down Expand Up @@ -1294,7 +1307,7 @@ bool ValueObject::DumpPrintableRepresentation(
break;

case eValueObjectRepresentationStyleChildrenCount:
strm.Printf("%" PRIu64 "", (uint64_t)GetNumChildren());
strm.Printf("%" PRIu64 "", (uint64_t)GetNumChildrenIgnoringErrors());
str = strm.GetString();
break;

Expand Down Expand Up @@ -2320,7 +2333,9 @@ ValueObjectSP ValueObject::GetValueForExpressionPath_Impl(
child_valobj_sp = root->GetSyntheticArrayMember(index, true);
if (!child_valobj_sp)
if (root->HasSyntheticValue() &&
root->GetSyntheticValue()->GetNumChildren() > index)
llvm::expectedToStdOptional(
root->GetSyntheticValue()->GetNumChildren())
.value_or(0) > index)
child_valobj_sp =
root->GetSyntheticValue()->GetChildAtIndex(index);
if (child_valobj_sp) {
Expand Down
6 changes: 4 additions & 2 deletions lldb/source/Core/ValueObjectCast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,13 @@ ValueObjectCast::~ValueObjectCast() = default;

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

uint32_t ValueObjectCast::CalculateNumChildren(uint32_t max) {
llvm::Expected<uint32_t> ValueObjectCast::CalculateNumChildren(uint32_t max) {
ExecutionContext exe_ctx(GetExecutionContextRef());
auto children_count = GetCompilerType().GetNumChildren(
true, &exe_ctx);
return children_count <= max ? children_count : max;
if (!children_count)
return children_count;
return *children_count <= max ? *children_count : max;
}

std::optional<uint64_t> ValueObjectCast::GetByteSize() {
Expand Down
6 changes: 4 additions & 2 deletions lldb/source/Core/ValueObjectChild.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,12 @@ lldb::ValueType ValueObjectChild::GetValueType() const {
return m_parent->GetValueType();
}

uint32_t ValueObjectChild::CalculateNumChildren(uint32_t max) {
llvm::Expected<uint32_t> ValueObjectChild::CalculateNumChildren(uint32_t max) {
ExecutionContext exe_ctx(GetExecutionContextRef());
auto children_count = GetCompilerType().GetNumChildren(true, &exe_ctx);
return children_count <= max ? children_count : max;
if (!children_count)
return children_count;
return *children_count <= max ? *children_count : max;
}

static void AdjustForBitfieldness(ConstString &name,
Expand Down

0 comments on commit 624ea68

Please sign in to comment.