Skip to content

[lldb] Change interface of StructuredData::Array::GetItemAtIndexAsInteger #71993

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 7 additions & 21 deletions lldb/include/lldb/Utility/StructuredData.h
Original file line number Diff line number Diff line change
Expand Up @@ -221,31 +221,17 @@ class StructuredData {
}

template <class IntType>
bool GetItemAtIndexAsInteger(size_t idx, IntType &result) const {
ObjectSP value_sp = GetItemAtIndex(idx);
if (value_sp.get()) {
std::optional<IntType> GetItemAtIndexAsInteger(size_t idx) const {
if (auto item_sp = GetItemAtIndex(idx)) {
if constexpr (std::numeric_limits<IntType>::is_signed) {
if (auto signed_value = value_sp->GetAsSignedInteger()) {
result = static_cast<IntType>(signed_value->GetValue());
return true;
}
if (auto *signed_value = item_sp->GetAsSignedInteger())
return static_cast<IntType>(signed_value->GetValue());
} else {
if (auto unsigned_value = value_sp->GetAsUnsignedInteger()) {
result = static_cast<IntType>(unsigned_value->GetValue());
return true;
}
if (auto *unsigned_value = item_sp->GetAsUnsignedInteger())
return static_cast<IntType>(unsigned_value->GetValue());
}
}
return false;
}

template <class IntType>
bool GetItemAtIndexAsInteger(size_t idx, IntType &result,
IntType default_val) const {
bool success = GetItemAtIndexAsInteger(idx, result);
if (!success)
result = default_val;
return success;
return {};
}

std::optional<llvm::StringRef> GetItemAtIndexAsString(size_t idx) const {
Expand Down
8 changes: 4 additions & 4 deletions lldb/source/Breakpoint/BreakpointResolverName.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,14 +161,14 @@ BreakpointResolverSP BreakpointResolverName::CreateFromStructuredData(
error.SetErrorString("BRN::CFSD: name entry is not a string.");
return nullptr;
}
std::underlying_type<FunctionNameType>::type fnt;
success = names_mask_array->GetItemAtIndexAsInteger(i, fnt);
if (!success) {
auto maybe_fnt = names_mask_array->GetItemAtIndexAsInteger<
std::underlying_type<FunctionNameType>::type>(i);
if (!maybe_fnt) {
error.SetErrorString("BRN::CFSD: name mask entry is not an integer.");
return nullptr;
}
names.push_back(std::string(*maybe_name));
name_masks.push_back(static_cast<FunctionNameType>(fnt));
name_masks.push_back(static_cast<FunctionNameType>(*maybe_fnt));
}

std::shared_ptr<BreakpointResolverName> resolver_sp =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -592,9 +592,10 @@ addr_t InstrumentationRuntimeTSan::GetFirstNonInternalFramePc(
if (skip_one_frame && i == 0)
continue;

addr_t addr;
if (!trace_array->GetItemAtIndexAsInteger(i, addr))
auto maybe_addr = trace_array->GetItemAtIndexAsInteger<addr_t>(i);
if (!maybe_addr)
continue;
addr_t addr = *maybe_addr;

lldb_private::Address so_addr;
if (!process_sp->GetTarget().GetSectionLoadList().ResolveLoadAddress(
Expand Down
15 changes: 7 additions & 8 deletions lldb/source/Target/DynamicRegisterInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -349,10 +349,8 @@ DynamicRegisterInfo::SetRegisterInfo(const StructuredData::Dictionary &dict,
const size_t num_regs = invalidate_reg_list->GetSize();
if (num_regs > 0) {
for (uint32_t idx = 0; idx < num_regs; ++idx) {
uint64_t invalidate_reg_num;
std::optional<llvm::StringRef> maybe_invalidate_reg_name =
invalidate_reg_list->GetItemAtIndexAsString(idx);
if (maybe_invalidate_reg_name) {
if (auto maybe_invalidate_reg_name =
invalidate_reg_list->GetItemAtIndexAsString(idx)) {
const RegisterInfo *invalidate_reg_info =
GetRegisterInfo(*maybe_invalidate_reg_name);
if (invalidate_reg_info) {
Expand All @@ -365,10 +363,11 @@ DynamicRegisterInfo::SetRegisterInfo(const StructuredData::Dictionary &dict,
"\"%s\" while parsing register \"%s\"\n",
maybe_invalidate_reg_name->str().c_str(), reg_info.name);
}
} else if (invalidate_reg_list->GetItemAtIndexAsInteger(
idx, invalidate_reg_num)) {
if (invalidate_reg_num != UINT64_MAX)
m_invalidate_regs_map[i].push_back(invalidate_reg_num);
} else if (auto maybe_invalidate_reg_num =
invalidate_reg_list->GetItemAtIndexAsInteger<uint64_t>(
idx)) {
if (*maybe_invalidate_reg_num != UINT64_MAX)
m_invalidate_regs_map[i].push_back(*maybe_invalidate_reg_num);
else
printf("error: 'invalidate-regs' list value wasn't a valid "
"integer\n");
Expand Down