1 //===-- LibCxxList.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 namespace lldb_private {
29 namespace formatters {
30 class LibcxxStdMapSyntheticFrontEnd : public SyntheticChildrenFrontEnd
33 LibcxxStdMapSyntheticFrontEnd (lldb::ValueObjectSP valobj_sp);
36 CalculateNumChildren ();
38 virtual lldb::ValueObjectSP
39 GetChildAtIndex (size_t idx);
48 GetIndexOfChildWithName (const ConstString &name);
51 ~LibcxxStdMapSyntheticFrontEnd ();
57 GetValueOffset (const lldb::ValueObjectSP& node);
60 ValueObject* m_root_node;
61 ClangASTType m_element_type;
64 std::map<size_t,lldb::ValueObjectSP> m_children;
73 explicit MapEntry (ValueObjectSP entry_sp) : m_entry_sp(entry_sp) {}
74 MapEntry (const MapEntry& rhs) : m_entry_sp(rhs.m_entry_sp) {}
75 explicit MapEntry (ValueObject* entry) : m_entry_sp(entry ? entry->GetSP() : ValueObjectSP()) {}
80 static ConstString g_left("__left_");
83 return m_entry_sp->GetChildMemberWithName(g_left, true);
89 static ConstString g_right("__right_");
92 return m_entry_sp->GetChildMemberWithName(g_right, true);
98 static ConstString g_parent("__parent_");
101 return m_entry_sp->GetChildMemberWithName(g_parent, true);
109 return m_entry_sp->GetValueAsUnsigned(0);
117 return m_entry_sp->GetError().Fail();
123 return (value() == 0);
133 SetEntry (ValueObjectSP entry)
139 operator == (const MapEntry& rhs) const
141 return (rhs.m_entry_sp.get() == m_entry_sp.get());
145 ValueObjectSP m_entry_sp;
152 MapIterator (MapEntry entry, size_t depth = 0) : m_entry(entry), m_max_depth(depth), m_error(false) {}
153 MapIterator (ValueObjectSP entry, size_t depth = 0) : m_entry(entry), m_max_depth(depth), m_error(false) {}
154 MapIterator (const MapIterator& rhs) : m_entry(rhs.m_entry),m_max_depth(rhs.m_max_depth), m_error(false) {}
155 MapIterator (ValueObject* entry, size_t depth = 0) : m_entry(entry), m_max_depth(depth), m_error(false) {}
160 return m_entry.GetEntry();
164 advance (size_t count)
166 ValueObjectSP fail(nullptr);
176 (steps > m_max_depth))
179 return m_entry.GetEntry();
187 MapEntry right(m_entry.right());
188 if (right.null() == false)
190 m_entry = tree_min(std::move(right));
194 while (!is_left_child(m_entry))
201 m_entry.SetEntry(m_entry.parent());
203 if (steps > m_max_depth)
205 m_entry = MapEntry();
209 m_entry = MapEntry(m_entry.parent());
214 tree_min (MapEntry&& x)
218 MapEntry left(x.left());
220 while (left.null() == false)
228 left.SetEntry(x.left());
230 if (steps > m_max_depth)
237 is_left_child (const MapEntry& x)
241 MapEntry rhs(x.parent());
242 rhs.SetEntry(rhs.left());
243 return x.value() == rhs.value();
251 lldb_private::formatters::LibcxxStdMapSyntheticFrontEnd::LibcxxStdMapSyntheticFrontEnd (lldb::ValueObjectSP valobj_sp) :
252 SyntheticChildrenFrontEnd(*valobj_sp.get()),
256 m_skip_size(UINT32_MAX),
265 lldb_private::formatters::LibcxxStdMapSyntheticFrontEnd::CalculateNumChildren ()
267 if (m_count != UINT32_MAX)
271 ValueObjectSP m_item(m_tree->GetChildMemberWithName(ConstString("__pair3_"), true));
274 m_item = m_item->GetChildMemberWithName(ConstString("__first_"), true);
277 m_count = m_item->GetValueAsUnsigned(0);
282 lldb_private::formatters::LibcxxStdMapSyntheticFrontEnd::GetDataType()
284 if (m_element_type.GetOpaqueQualType() && m_element_type.GetASTContext())
286 m_element_type.Clear();
289 deref = m_root_node->Dereference(error);
290 if (!deref || error.Fail())
292 deref = deref->GetChildMemberWithName(ConstString("__value_"), true);
295 m_element_type = deref->GetClangType();
300 lldb_private::formatters::LibcxxStdMapSyntheticFrontEnd::GetValueOffset (const lldb::ValueObjectSP& node)
302 if (m_skip_size != UINT32_MAX)
306 ClangASTType node_type(node->GetClangType());
308 if (node_type.GetIndexOfFieldWithName("__value_", NULL, &bit_offset) == UINT32_MAX)
310 m_skip_size = bit_offset / 8u;
314 lldb_private::formatters::LibcxxStdMapSyntheticFrontEnd::GetChildAtIndex (size_t idx)
316 static ConstString g___cc("__cc");
317 static ConstString g___nc("__nc");
320 if (idx >= CalculateNumChildren())
321 return lldb::ValueObjectSP();
322 if (m_tree == NULL || m_root_node == NULL)
323 return lldb::ValueObjectSP();
325 auto cached = m_children.find(idx);
326 if (cached != m_children.end())
327 return cached->second;
329 bool need_to_skip = (idx > 0);
330 MapIterator iterator(m_root_node, CalculateNumChildren());
331 ValueObjectSP iterated_sp(iterator.advance(idx));
332 if (iterated_sp.get() == NULL)
334 // this tree is garbage - stop
335 m_tree = NULL; // this will stop all future searches until an Update() happens
343 iterated_sp = iterated_sp->Dereference(error);
344 if (!iterated_sp || error.Fail())
347 return lldb::ValueObjectSP();
349 GetValueOffset(iterated_sp);
350 iterated_sp = iterated_sp->GetChildMemberWithName(ConstString("__value_"), true);
354 return lldb::ValueObjectSP();
359 // because of the way our debug info is made, we need to read item 0 first
360 // so that we can cache information used to generate other elements
361 if (m_skip_size == UINT32_MAX)
363 if (m_skip_size == UINT32_MAX)
366 return lldb::ValueObjectSP();
368 iterated_sp = iterated_sp->GetSyntheticChildAtOffset(m_skip_size, m_element_type, true);
372 return lldb::ValueObjectSP();
379 return lldb::ValueObjectSP();
381 // at this point we have a valid
382 // we need to copy current_sp into a new object otherwise we will end up with all items named __value_
385 iterated_sp->GetData(data, error);
389 return lldb::ValueObjectSP();
392 name.Printf("[%" PRIu64 "]", (uint64_t)idx);
393 auto potential_child_sp = CreateValueObjectFromData(name.GetData(), data, m_backend.GetExecutionContextRef(), m_element_type);
394 if (potential_child_sp)
396 switch (potential_child_sp->GetNumChildren())
400 auto child0_sp = potential_child_sp->GetChildAtIndex(0, true);
401 if (child0_sp && child0_sp->GetName() == g___cc)
402 potential_child_sp = child0_sp;
407 auto child0_sp = potential_child_sp->GetChildAtIndex(0, true);
408 auto child1_sp = potential_child_sp->GetChildAtIndex(1, true);
409 if (child0_sp && child0_sp->GetName() == g___cc &&
410 child1_sp && child1_sp->GetName() == g___nc)
411 potential_child_sp = child0_sp;
415 potential_child_sp->SetName(ConstString(name.GetData()));
417 return (m_children[idx] = potential_child_sp);
421 lldb_private::formatters::LibcxxStdMapSyntheticFrontEnd::Update()
423 m_count = UINT32_MAX;
424 m_tree = m_root_node = NULL;
426 m_tree = m_backend.GetChildMemberWithName(ConstString("__tree_"), true).get();
429 m_root_node = m_tree->GetChildMemberWithName(ConstString("__begin_node_"), true).get();
434 lldb_private::formatters::LibcxxStdMapSyntheticFrontEnd::MightHaveChildren ()
440 lldb_private::formatters::LibcxxStdMapSyntheticFrontEnd::GetIndexOfChildWithName (const ConstString &name)
442 return ExtractIndexFromString(name.GetCString());
445 lldb_private::formatters::LibcxxStdMapSyntheticFrontEnd::~LibcxxStdMapSyntheticFrontEnd ()
448 SyntheticChildrenFrontEnd*
449 lldb_private::formatters::LibcxxStdMapSyntheticFrontEndCreator (CXXSyntheticChildren*, lldb::ValueObjectSP valobj_sp)
453 return (new LibcxxStdMapSyntheticFrontEnd(valobj_sp));