blob: 05b41d0de0c88e3f93345ed45e099a79593ec47e [file] [log] [blame]
Enrico Granatae2e220a2013-09-12 00:48:47 +00001//===-- LibCxxUnorderedMap.cpp -----------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "lldb/lldb-python.h"
11
12#include "lldb/DataFormatters/CXXFormatterFunctions.h"
13
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"
23
24using namespace lldb;
25using namespace lldb_private;
26using namespace lldb_private::formatters;
27
28lldb_private::formatters::LibcxxStdUnorderedMapSyntheticFrontEnd::LibcxxStdUnorderedMapSyntheticFrontEnd (lldb::ValueObjectSP valobj_sp) :
29SyntheticChildrenFrontEnd(*valobj_sp.get()),
30m_tree(NULL),
31m_num_elements(0),
32m_next_element(nullptr),
33m_children(),
34m_elements_cache()
35{
36 if (valobj_sp)
37 Update();
38}
39
40size_t
41lldb_private::formatters::LibcxxStdUnorderedMapSyntheticFrontEnd::CalculateNumChildren ()
42{
43 if (m_num_elements != UINT32_MAX)
44 return m_num_elements;
45 return 0;
46}
47
48lldb::ValueObjectSP
49lldb_private::formatters::LibcxxStdUnorderedMapSyntheticFrontEnd::GetChildAtIndex (size_t idx)
50{
51 if (idx >= CalculateNumChildren())
52 return lldb::ValueObjectSP();
53 if (m_tree == NULL)
54 return lldb::ValueObjectSP();
55
56 auto cached = m_children.find(idx);
57 if (cached != m_children.end())
58 return cached->second;
59
60 while (idx >= m_elements_cache.size())
61 {
62 if (m_next_element == nullptr)
63 return lldb::ValueObjectSP();
64
65 Error error;
66 ValueObjectSP node_sp = m_next_element->Dereference(error);
67 if (!node_sp || error.Fail())
68 return lldb::ValueObjectSP();
69
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;
78 }
79
80 std::pair<ValueObject*, uint64_t> val_hash = m_elements_cache[idx];
81 if (!val_hash.first)
82 return lldb::ValueObjectSP();
83 StreamString stream;
84 stream.Printf("[%zu]",idx);
85 DataExtractor data;
86 val_hash.first->GetData(data);
Greg Clayton44d93782014-01-27 23:43:24 +000087 const bool thread_and_frame_only_if_stopped = true;
88 ExecutionContext exe_ctx = val_hash.first->GetExecutionContextRef().Lock(thread_and_frame_only_if_stopped);
Enrico Granatae2e220a2013-09-12 00:48:47 +000089 return val_hash.first->CreateValueObjectFromData(stream.GetData(),
90 data,
91 exe_ctx,
92 val_hash.first->GetClangType());
93}
94
95bool
96lldb_private::formatters::LibcxxStdUnorderedMapSyntheticFrontEnd::Update()
97{
98 m_num_elements = UINT32_MAX;
99 m_next_element = nullptr;
100 m_elements_cache.clear();
101 m_children.clear();
102 ValueObjectSP table_sp = m_backend.GetChildMemberWithName(ConstString("__table_"), true);
103 if (!table_sp)
104 return false;
105 ValueObjectSP num_elements_sp = table_sp->GetChildAtNamePath({ConstString("__p2_"),ConstString("__first_")});
106 if (!num_elements_sp)
107 return false;
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();
112 return false;
113}
114
115bool
116lldb_private::formatters::LibcxxStdUnorderedMapSyntheticFrontEnd::MightHaveChildren ()
117{
118 return true;
119}
120
121size_t
122lldb_private::formatters::LibcxxStdUnorderedMapSyntheticFrontEnd::GetIndexOfChildWithName (const ConstString &name)
123{
124 return ExtractIndexFromString(name.GetCString());
125}
126
127lldb_private::formatters::LibcxxStdUnorderedMapSyntheticFrontEnd::~LibcxxStdUnorderedMapSyntheticFrontEnd ()
128{}
129
130SyntheticChildrenFrontEnd*
131lldb_private::formatters::LibcxxStdUnorderedMapSyntheticFrontEndCreator (CXXSyntheticChildren*, lldb::ValueObjectSP valobj_sp)
132{
133 if (!valobj_sp)
134 return NULL;
135 return (new LibcxxStdUnorderedMapSyntheticFrontEnd(valobj_sp));
136}