1 //===-- LibCxxUnorderedMap.cpp -----------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/lldb-python.h"
12 #include "lldb/DataFormatters/CXXFormatterFunctions.h"
14 #include "lldb/Core/DataBufferHeap.h"
15 #include "lldb/Core/Error.h"
16 #include "lldb/Core/Stream.h"
17 #include "lldb/Core/ValueObject.h"
18 #include "lldb/Core/ValueObjectConstResult.h"
19 #include "lldb/Host/Endian.h"
20 #include "lldb/Symbol/ClangASTContext.h"
21 #include "lldb/Target/ObjCLanguageRuntime.h"
22 #include "lldb/Target/Target.h"
25 using namespace lldb_private;
26 using namespace lldb_private::formatters;
28 lldb_private::formatters::LibcxxStdUnorderedMapSyntheticFrontEnd::LibcxxStdUnorderedMapSyntheticFrontEnd (lldb::ValueObjectSP valobj_sp) :
29 SyntheticChildrenFrontEnd(*valobj_sp.get()),
32 m_next_element(nullptr),
41 lldb_private::formatters::LibcxxStdUnorderedMapSyntheticFrontEnd::CalculateNumChildren ()
43 if (m_num_elements != UINT32_MAX)
44 return m_num_elements;
49 lldb_private::formatters::LibcxxStdUnorderedMapSyntheticFrontEnd::GetChildAtIndex (size_t idx)
51 if (idx >= CalculateNumChildren())
52 return lldb::ValueObjectSP();
54 return lldb::ValueObjectSP();
56 auto cached = m_children.find(idx);
57 if (cached != m_children.end())
58 return cached->second;
60 while (idx >= m_elements_cache.size())
62 if (m_next_element == nullptr)
63 return lldb::ValueObjectSP();
66 ValueObjectSP node_sp = m_next_element->Dereference(error);
67 if (!node_sp || error.Fail())
68 return lldb::ValueObjectSP();
70 ValueObjectSP value_sp = node_sp->GetChildMemberWithName(ConstString("__value_"), true);
71 ValueObjectSP hash_sp = node_sp->GetChildMemberWithName(ConstString("__hash_"), true);
72 if (!hash_sp || !value_sp)
73 return lldb::ValueObjectSP();
74 m_elements_cache.push_back({value_sp.get(),hash_sp->GetValueAsUnsigned(0)});
75 m_next_element = node_sp->GetChildMemberWithName(ConstString("__next_"),true).get();
76 if (!m_next_element || m_next_element->GetValueAsUnsigned(0) == 0)
77 m_next_element = nullptr;
80 std::pair<ValueObject*, uint64_t> val_hash = m_elements_cache[idx];
82 return lldb::ValueObjectSP();
84 stream.Printf("[%zu]",idx);
86 val_hash.first->GetData(data);
87 const bool thread_and_frame_only_if_stopped = true;
88 ExecutionContext exe_ctx = val_hash.first->GetExecutionContextRef().Lock(thread_and_frame_only_if_stopped);
89 return val_hash.first->CreateValueObjectFromData(stream.GetData(),
92 val_hash.first->GetClangType());
96 lldb_private::formatters::LibcxxStdUnorderedMapSyntheticFrontEnd::Update()
98 m_num_elements = UINT32_MAX;
99 m_next_element = nullptr;
100 m_elements_cache.clear();
102 ValueObjectSP table_sp = m_backend.GetChildMemberWithName(ConstString("__table_"), true);
105 ValueObjectSP num_elements_sp = table_sp->GetChildAtNamePath({ConstString("__p2_"),ConstString("__first_")});
106 if (!num_elements_sp)
108 m_num_elements = num_elements_sp->GetValueAsUnsigned(0);
109 m_tree = table_sp->GetChildAtNamePath({ConstString("__p1_"),ConstString("__first_"),ConstString("__next_")}).get();
110 if (m_num_elements > 0)
111 m_next_element = table_sp->GetChildAtNamePath({ConstString("__p1_"),ConstString("__first_"),ConstString("__next_")}).get();
116 lldb_private::formatters::LibcxxStdUnorderedMapSyntheticFrontEnd::MightHaveChildren ()
122 lldb_private::formatters::LibcxxStdUnorderedMapSyntheticFrontEnd::GetIndexOfChildWithName (const ConstString &name)
124 return ExtractIndexFromString(name.GetCString());
127 lldb_private::formatters::LibcxxStdUnorderedMapSyntheticFrontEnd::~LibcxxStdUnorderedMapSyntheticFrontEnd ()
130 SyntheticChildrenFrontEnd*
131 lldb_private::formatters::LibcxxStdUnorderedMapSyntheticFrontEndCreator (CXXSyntheticChildren*, lldb::ValueObjectSP valobj_sp)
135 return (new LibcxxStdUnorderedMapSyntheticFrontEnd(valobj_sp));