Skip to content

Commit 133bcac

Browse files
authored
[lldb] Change interface of StructuredData::Array::GetItemAtIndexAsDictionary (llvm#71961)
Similar to my previous patch (llvm#71613) where I changed `GetItemAtIndexAsString`, this patch makes the same change to `GetItemAtIndexAsDictionary`. `GetItemAtIndexAsDictionary` now returns a std::optional that is either `std::nullopt` or is a valid pointer. Therefore, if the optional is populated, we consider the pointer to always be valid (i.e. no need to check pointer validity).
1 parent bfe08c0 commit 133bcac

File tree

6 files changed

+37
-18
lines changed

6 files changed

+37
-18
lines changed

lldb/include/lldb/Utility/StructuredData.h

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -256,14 +256,24 @@ class StructuredData {
256256
return {};
257257
}
258258

259-
bool GetItemAtIndexAsDictionary(size_t idx, Dictionary *&result) const {
260-
result = nullptr;
261-
ObjectSP value_sp = GetItemAtIndex(idx);
262-
if (value_sp.get()) {
263-
result = value_sp->GetAsDictionary();
264-
return (result != nullptr);
259+
/// Retrieves the element at index \a idx from a StructuredData::Array if it
260+
/// is a Dictionary.
261+
///
262+
/// \param[in] idx
263+
/// The index of the element to retrieve.
264+
///
265+
/// \return
266+
/// If the element at index \a idx is a Dictionary, this method returns a
267+
/// valid pointer to the Dictionary wrapped in a std::optional. If the
268+
/// element is not a Dictionary or the index is invalid, this returns
269+
/// std::nullopt. Note that the underlying Dictionary pointer is never
270+
/// nullptr.
271+
std::optional<Dictionary *> GetItemAtIndexAsDictionary(size_t idx) const {
272+
if (auto item_sp = GetItemAtIndex(idx)) {
273+
if (auto *dict = item_sp->GetAsDictionary())
274+
return dict;
265275
}
266-
return false;
276+
return {};
267277
}
268278

269279
bool GetItemAtIndexAsArray(size_t idx, Array *&result) const {

lldb/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5688,14 +5688,16 @@ bool ObjectFileMachO::GetCorefileThreadExtraInfos(std::vector<tid_t> &tids) {
56885688
}
56895689
const size_t num_threads = threads->GetSize();
56905690
for (size_t i = 0; i < num_threads; i++) {
5691-
StructuredData::Dictionary *thread;
5692-
if (!threads->GetItemAtIndexAsDictionary(i, thread) || !thread) {
5691+
std::optional<StructuredData::Dictionary *> maybe_thread =
5692+
threads->GetItemAtIndexAsDictionary(i);
5693+
if (!maybe_thread) {
56935694
LLDB_LOGF(log,
56945695
"Unable to read 'process metadata' LC_NOTE, threads "
56955696
"array does not have a dictionary at index %zu.",
56965697
i);
56975698
return false;
56985699
}
5700+
StructuredData::Dictionary *thread = *maybe_thread;
56995701
tid_t tid = LLDB_INVALID_THREAD_ID;
57005702
if (thread->GetValueForKeyAsInteger<tid_t>("thread_id", tid))
57015703
if (tid == 0)

lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationClient.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2649,10 +2649,12 @@ size_t GDBRemoteCommunicationClient::QueryGDBServer(
26492649
return 0;
26502650

26512651
for (size_t i = 0, count = array->GetSize(); i < count; ++i) {
2652-
StructuredData::Dictionary *element = nullptr;
2653-
if (!array->GetItemAtIndexAsDictionary(i, element))
2652+
std::optional<StructuredData::Dictionary *> maybe_element =
2653+
array->GetItemAtIndexAsDictionary(i);
2654+
if (!maybe_element)
26542655
continue;
26552656

2657+
StructuredData::Dictionary *element = *maybe_element;
26562658
uint16_t port = 0;
26572659
if (StructuredData::ObjectSP port_osp =
26582660
element->GetValueForKey(llvm::StringRef("port")))

lldb/source/Plugins/Process/scripted/ScriptedThread.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,15 +176,17 @@ bool ScriptedThread::LoadArtificialStackFrames() {
176176
StackFrameListSP frames = GetStackFrameList();
177177

178178
for (size_t idx = 0; idx < arr_size; idx++) {
179-
StructuredData::Dictionary *dict;
180-
if (!arr_sp->GetItemAtIndexAsDictionary(idx, dict) || !dict)
179+
std::optional<StructuredData::Dictionary *> maybe_dict =
180+
arr_sp->GetItemAtIndexAsDictionary(idx);
181+
if (!maybe_dict)
181182
return ScriptedInterface::ErrorWithMessage<bool>(
182183
LLVM_PRETTY_FUNCTION,
183184
llvm::Twine(
184185
"Couldn't get artificial stackframe dictionary at index (" +
185186
llvm::Twine(idx) + llvm::Twine(") from stackframe array."))
186187
.str(),
187188
error, LLDBLog::Thread);
189+
StructuredData::Dictionary *dict = *maybe_dict;
188190

189191
lldb::addr_t pc;
190192
if (!dict->GetValueForKeyAsInteger("pc", pc))

lldb/source/Target/DynamicRegisterInfo.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,13 +231,15 @@ DynamicRegisterInfo::SetRegisterInfo(const StructuredData::Dictionary &dict,
231231
// InvalidateNameMap;
232232
// InvalidateNameMap invalidate_map;
233233
for (uint32_t i = 0; i < num_regs; ++i) {
234-
StructuredData::Dictionary *reg_info_dict = nullptr;
235-
if (!regs->GetItemAtIndexAsDictionary(i, reg_info_dict)) {
234+
std::optional<StructuredData::Dictionary *> maybe_reg_info_dict =
235+
regs->GetItemAtIndexAsDictionary(i);
236+
if (!maybe_reg_info_dict) {
236237
Clear();
237238
printf("error: items in the 'registers' array must be dictionaries\n");
238239
regs->DumpToStdout();
239240
return 0;
240241
}
242+
StructuredData::Dictionary *reg_info_dict = *maybe_reg_info_dict;
241243

242244
// { 'name':'rcx' , 'bitsize' : 64, 'offset' : 16,
243245
// 'encoding':'uint' , 'format':'hex' , 'set': 0, 'ehframe' : 2,

lldb/unittests/tools/lldb-server/tests/MessageObjects.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,12 @@ Expected<JThreadsInfo> JThreadsInfo::create(StringRef Response,
106106
return make_parsing_error("JThreadsInfo: JSON array");
107107

108108
for (size_t i = 0; i < array->GetSize(); i++) {
109-
StructuredData::Dictionary *thread_info;
110-
array->GetItemAtIndexAsDictionary(i, thread_info);
111-
if (!thread_info)
109+
std::optional<StructuredData::Dictionary *> maybe_thread_info =
110+
array->GetItemAtIndexAsDictionary(i);
111+
if (!maybe_thread_info)
112112
return make_parsing_error("JThreadsInfo: JSON obj at {0}", i);
113113

114+
StructuredData::Dictionary *thread_info = *maybe_thread_info;
114115
StringRef name, reason;
115116
thread_info->GetValueForKeyAsString("name", name);
116117
thread_info->GetValueForKeyAsString("reason", reason);

0 commit comments

Comments
 (0)