Skip to content

Commit 16b8a0d

Browse files
authored
[lldb] Change interface of StructuredData::Array::GetItemAtIndexAsInteger (#71993)
This is a follow-up to (#71613) and (#71961).
1 parent 09e32ab commit 16b8a0d

File tree

4 files changed

+21
-35
lines changed

4 files changed

+21
-35
lines changed

lldb/include/lldb/Utility/StructuredData.h

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -221,31 +221,17 @@ class StructuredData {
221221
}
222222

223223
template <class IntType>
224-
bool GetItemAtIndexAsInteger(size_t idx, IntType &result) const {
225-
ObjectSP value_sp = GetItemAtIndex(idx);
226-
if (value_sp.get()) {
224+
std::optional<IntType> GetItemAtIndexAsInteger(size_t idx) const {
225+
if (auto item_sp = GetItemAtIndex(idx)) {
227226
if constexpr (std::numeric_limits<IntType>::is_signed) {
228-
if (auto signed_value = value_sp->GetAsSignedInteger()) {
229-
result = static_cast<IntType>(signed_value->GetValue());
230-
return true;
231-
}
227+
if (auto *signed_value = item_sp->GetAsSignedInteger())
228+
return static_cast<IntType>(signed_value->GetValue());
232229
} else {
233-
if (auto unsigned_value = value_sp->GetAsUnsignedInteger()) {
234-
result = static_cast<IntType>(unsigned_value->GetValue());
235-
return true;
236-
}
230+
if (auto *unsigned_value = item_sp->GetAsUnsignedInteger())
231+
return static_cast<IntType>(unsigned_value->GetValue());
237232
}
238233
}
239-
return false;
240-
}
241-
242-
template <class IntType>
243-
bool GetItemAtIndexAsInteger(size_t idx, IntType &result,
244-
IntType default_val) const {
245-
bool success = GetItemAtIndexAsInteger(idx, result);
246-
if (!success)
247-
result = default_val;
248-
return success;
234+
return {};
249235
}
250236

251237
std::optional<llvm::StringRef> GetItemAtIndexAsString(size_t idx) const {

lldb/source/Breakpoint/BreakpointResolverName.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,14 +161,14 @@ BreakpointResolverSP BreakpointResolverName::CreateFromStructuredData(
161161
error.SetErrorString("BRN::CFSD: name entry is not a string.");
162162
return nullptr;
163163
}
164-
std::underlying_type<FunctionNameType>::type fnt;
165-
success = names_mask_array->GetItemAtIndexAsInteger(i, fnt);
166-
if (!success) {
164+
auto maybe_fnt = names_mask_array->GetItemAtIndexAsInteger<
165+
std::underlying_type<FunctionNameType>::type>(i);
166+
if (!maybe_fnt) {
167167
error.SetErrorString("BRN::CFSD: name mask entry is not an integer.");
168168
return nullptr;
169169
}
170170
names.push_back(std::string(*maybe_name));
171-
name_masks.push_back(static_cast<FunctionNameType>(fnt));
171+
name_masks.push_back(static_cast<FunctionNameType>(*maybe_fnt));
172172
}
173173

174174
std::shared_ptr<BreakpointResolverName> resolver_sp =

lldb/source/Plugins/InstrumentationRuntime/TSan/InstrumentationRuntimeTSan.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -592,9 +592,10 @@ addr_t InstrumentationRuntimeTSan::GetFirstNonInternalFramePc(
592592
if (skip_one_frame && i == 0)
593593
continue;
594594

595-
addr_t addr;
596-
if (!trace_array->GetItemAtIndexAsInteger(i, addr))
595+
auto maybe_addr = trace_array->GetItemAtIndexAsInteger<addr_t>(i);
596+
if (!maybe_addr)
597597
continue;
598+
addr_t addr = *maybe_addr;
598599

599600
lldb_private::Address so_addr;
600601
if (!process_sp->GetTarget().GetSectionLoadList().ResolveLoadAddress(

lldb/source/Target/DynamicRegisterInfo.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -349,10 +349,8 @@ DynamicRegisterInfo::SetRegisterInfo(const StructuredData::Dictionary &dict,
349349
const size_t num_regs = invalidate_reg_list->GetSize();
350350
if (num_regs > 0) {
351351
for (uint32_t idx = 0; idx < num_regs; ++idx) {
352-
uint64_t invalidate_reg_num;
353-
std::optional<llvm::StringRef> maybe_invalidate_reg_name =
354-
invalidate_reg_list->GetItemAtIndexAsString(idx);
355-
if (maybe_invalidate_reg_name) {
352+
if (auto maybe_invalidate_reg_name =
353+
invalidate_reg_list->GetItemAtIndexAsString(idx)) {
356354
const RegisterInfo *invalidate_reg_info =
357355
GetRegisterInfo(*maybe_invalidate_reg_name);
358356
if (invalidate_reg_info) {
@@ -365,10 +363,11 @@ DynamicRegisterInfo::SetRegisterInfo(const StructuredData::Dictionary &dict,
365363
"\"%s\" while parsing register \"%s\"\n",
366364
maybe_invalidate_reg_name->str().c_str(), reg_info.name);
367365
}
368-
} else if (invalidate_reg_list->GetItemAtIndexAsInteger(
369-
idx, invalidate_reg_num)) {
370-
if (invalidate_reg_num != UINT64_MAX)
371-
m_invalidate_regs_map[i].push_back(invalidate_reg_num);
366+
} else if (auto maybe_invalidate_reg_num =
367+
invalidate_reg_list->GetItemAtIndexAsInteger<uint64_t>(
368+
idx)) {
369+
if (*maybe_invalidate_reg_num != UINT64_MAX)
370+
m_invalidate_regs_map[i].push_back(*maybe_invalidate_reg_num);
372371
else
373372
printf("error: 'invalidate-regs' list value wasn't a valid "
374373
"integer\n");

0 commit comments

Comments
 (0)