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/DataFormatters/CXXFormatterFunctions.h"
12 #include "lldb/Core/DataBufferHeap.h"
13 #include "lldb/Core/Error.h"
14 #include "lldb/Core/Stream.h"
15 #include "lldb/Core/ValueObject.h"
16 #include "lldb/Core/ValueObjectConstResult.h"
17 #include "lldb/Host/Endian.h"
18 #include "lldb/Symbol/ClangASTContext.h"
19 #include "lldb/Target/ObjCLanguageRuntime.h"
20 #include "lldb/Target/Target.h"
23 using namespace lldb_private;
24 using namespace lldb_private::formatters;
26 namespace lldb_private {
27 namespace formatters {
28 class LibcxxStdMapSyntheticFrontEnd : public SyntheticChildrenFrontEnd
31 LibcxxStdMapSyntheticFrontEnd (lldb::ValueObjectSP valobj_sp);
34 CalculateNumChildren ();
36 virtual lldb::ValueObjectSP
37 GetChildAtIndex (size_t idx);
46 GetIndexOfChildWithName (const ConstString &name);
49 ~LibcxxStdMapSyntheticFrontEnd ();
55 GetValueOffset (const lldb::ValueObjectSP& node);
58 ValueObject* m_root_node;
59 ClangASTType m_element_type;
62 std::map<size_t,lldb::ValueObjectSP> m_children;
71 explicit MapEntry (ValueObjectSP entry_sp) : m_entry_sp(entry_sp) {}
72 MapEntry (const MapEntry& rhs) : m_entry_sp(rhs.m_entry_sp) {}
73 explicit MapEntry (ValueObject* entry) : m_entry_sp(entry ? entry->GetSP() : ValueObjectSP()) {}
78 static ConstString g_left("__left_");
81 return m_entry_sp->GetChildMemberWithName(g_left, true);
87 static ConstString g_right("__right_");
90 return m_entry_sp->GetChildMemberWithName(g_right, true);
96 static ConstString g_parent("__parent_");
99 return m_entry_sp->GetChildMemberWithName(g_parent, true);
107 return m_entry_sp->GetValueAsUnsigned(0);
115 return m_entry_sp->GetError().Fail();
121 return (value() == 0);
131 SetEntry (ValueObjectSP entry)
137 operator == (const MapEntry& rhs) const
139 return (rhs.m_entry_sp.get() == m_entry_sp.get());
143 ValueObjectSP m_entry_sp;
150 MapIterator (MapEntry entry, size_t depth = 0) : m_entry(entry), m_max_depth(depth), m_error(false) {}
151 MapIterator (ValueObjectSP entry, size_t depth = 0) : m_entry(entry), m_max_depth(depth), m_error(false) {}
152 MapIterator (const MapIterator& rhs) : m_entry(rhs.m_entry),m_max_depth(rhs.m_max_depth), m_error(false) {}
153 MapIterator (ValueObject* entry, size_t depth = 0) : m_entry(entry), m_max_depth(depth), m_error(false) {}
158 return m_entry.GetEntry();
162 advance (size_t count)
164 ValueObjectSP fail(nullptr);
174 (steps > m_max_depth))
177 return m_entry.GetEntry();
185 MapEntry right(m_entry.right());
186 if (right.null() == false)
188 m_entry = tree_min(std::move(right));
192 while (!is_left_child(m_entry))
199 m_entry.SetEntry(m_entry.parent());
201 if (steps > m_max_depth)
203 m_entry = MapEntry();
207 m_entry = MapEntry(m_entry.parent());
212 tree_min (MapEntry&& x)
216 MapEntry left(x.left());
218 while (left.null() == false)
226 left.SetEntry(x.left());
228 if (steps > m_max_depth)
235 is_left_child (const MapEntry& x)
239 MapEntry rhs(x.parent());
240 rhs.SetEntry(rhs.left());
241 return x.value() == rhs.value();
249 lldb_private::formatters::LibcxxStdMapSyntheticFrontEnd::LibcxxStdMapSyntheticFrontEnd (lldb::ValueObjectSP valobj_sp) :
250 SyntheticChildrenFrontEnd(*valobj_sp.get()),
254 m_skip_size(UINT32_MAX),
263 lldb_private::formatters::LibcxxStdMapSyntheticFrontEnd::CalculateNumChildren ()
265 if (m_count != UINT32_MAX)
269 ValueObjectSP m_item(m_tree->GetChildMemberWithName(ConstString("__pair3_"), true));
272 m_item = m_item->GetChildMemberWithName(ConstString("__first_"), true);
275 m_count = m_item->GetValueAsUnsigned(0);
280 lldb_private::formatters::LibcxxStdMapSyntheticFrontEnd::GetDataType()
282 if (m_element_type.GetOpaqueQualType() && m_element_type.GetASTContext())
284 m_element_type.Clear();
287 deref = m_root_node->Dereference(error);
288 if (!deref || error.Fail())
290 deref = deref->GetChildMemberWithName(ConstString("__value_"), true);
293 m_element_type = deref->GetClangType();
298 lldb_private::formatters::LibcxxStdMapSyntheticFrontEnd::GetValueOffset (const lldb::ValueObjectSP& node)
300 if (m_skip_size != UINT32_MAX)
304 ClangASTType node_type(node->GetClangType());
306 if (node_type.GetIndexOfFieldWithName("__value_", NULL, &bit_offset) == UINT32_MAX)
308 m_skip_size = bit_offset / 8u;
312 lldb_private::formatters::LibcxxStdMapSyntheticFrontEnd::GetChildAtIndex (size_t idx)
314 static ConstString g___cc("__cc");
315 static ConstString g___nc("__nc");
318 if (idx >= CalculateNumChildren())
319 return lldb::ValueObjectSP();
320 if (m_tree == NULL || m_root_node == NULL)
321 return lldb::ValueObjectSP();
323 auto cached = m_children.find(idx);
324 if (cached != m_children.end())
325 return cached->second;
327 bool need_to_skip = (idx > 0);
328 MapIterator iterator(m_root_node, CalculateNumChildren());
329 ValueObjectSP iterated_sp(iterator.advance(idx));
330 if (iterated_sp.get() == NULL)
332 // this tree is garbage - stop
333 m_tree = NULL; // this will stop all future searches until an Update() happens
341 iterated_sp = iterated_sp->Dereference(error);
342 if (!iterated_sp || error.Fail())
345 return lldb::ValueObjectSP();
347 GetValueOffset(iterated_sp);
348 iterated_sp = iterated_sp->GetChildMemberWithName(ConstString("__value_"), true);
352 return lldb::ValueObjectSP();
357 // because of the way our debug info is made, we need to read item 0 first
358 // so that we can cache information used to generate other elements
359 if (m_skip_size == UINT32_MAX)
361 if (m_skip_size == UINT32_MAX)
364 return lldb::ValueObjectSP();
366 iterated_sp = iterated_sp->GetSyntheticChildAtOffset(m_skip_size, m_element_type, true);
370 return lldb::ValueObjectSP();
377 return lldb::ValueObjectSP();
379 // at this point we have a valid
380 // we need to copy current_sp into a new object otherwise we will end up with all items named __value_
383 iterated_sp->GetData(data, error);
387 return lldb::ValueObjectSP();
390 name.Printf("[%" PRIu64 "]", (uint64_t)idx);
391 auto potential_child_sp = CreateValueObjectFromData(name.GetData(), data, m_backend.GetExecutionContextRef(), m_element_type);
392 if (potential_child_sp)
394 switch (potential_child_sp->GetNumChildren())
398 auto child0_sp = potential_child_sp->GetChildAtIndex(0, true);
399 if (child0_sp && child0_sp->GetName() == g___cc)
400 potential_child_sp = child0_sp;
405 auto child0_sp = potential_child_sp->GetChildAtIndex(0, true);
406 auto child1_sp = potential_child_sp->GetChildAtIndex(1, true);
407 if (child0_sp && child0_sp->GetName() == g___cc &&
408 child1_sp && child1_sp->GetName() == g___nc)
409 potential_child_sp = child0_sp;
413 potential_child_sp->SetName(ConstString(name.GetData()));
415 return (m_children[idx] = potential_child_sp);
419 lldb_private::formatters::LibcxxStdMapSyntheticFrontEnd::Update()
421 m_count = UINT32_MAX;
422 m_tree = m_root_node = NULL;
424 m_tree = m_backend.GetChildMemberWithName(ConstString("__tree_"), true).get();
427 m_root_node = m_tree->GetChildMemberWithName(ConstString("__begin_node_"), true).get();
432 lldb_private::formatters::LibcxxStdMapSyntheticFrontEnd::MightHaveChildren ()
438 lldb_private::formatters::LibcxxStdMapSyntheticFrontEnd::GetIndexOfChildWithName (const ConstString &name)
440 return ExtractIndexFromString(name.GetCString());
443 lldb_private::formatters::LibcxxStdMapSyntheticFrontEnd::~LibcxxStdMapSyntheticFrontEnd ()
446 SyntheticChildrenFrontEnd*
447 lldb_private::formatters::LibcxxStdMapSyntheticFrontEndCreator (CXXSyntheticChildren*, lldb::ValueObjectSP valobj_sp)
451 return (new LibcxxStdMapSyntheticFrontEnd(valobj_sp));