LibCxxUnorderedMap.cpp revision 263363
1//===-- 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);
87    ExecutionContext exe_ctx = val_hash.first->GetExecutionContextRef().Lock();
88    return val_hash.first->CreateValueObjectFromData(stream.GetData(),
89                                                     data,
90                                                     exe_ctx,
91                                                     val_hash.first->GetClangType());
92}
93
94bool
95lldb_private::formatters::LibcxxStdUnorderedMapSyntheticFrontEnd::Update()
96{
97    m_num_elements = UINT32_MAX;
98    m_next_element = nullptr;
99    m_elements_cache.clear();
100    m_children.clear();
101    ValueObjectSP table_sp = m_backend.GetChildMemberWithName(ConstString("__table_"), true);
102    if (!table_sp)
103        return false;
104    ValueObjectSP num_elements_sp = table_sp->GetChildAtNamePath({ConstString("__p2_"),ConstString("__first_")});
105    if (!num_elements_sp)
106        return false;
107    m_num_elements = num_elements_sp->GetValueAsUnsigned(0);
108    m_tree = table_sp->GetChildAtNamePath({ConstString("__p1_"),ConstString("__first_"),ConstString("__next_")}).get();
109    if (m_num_elements > 0)
110        m_next_element = table_sp->GetChildAtNamePath({ConstString("__p1_"),ConstString("__first_"),ConstString("__next_")}).get();
111    return false;
112}
113
114bool
115lldb_private::formatters::LibcxxStdUnorderedMapSyntheticFrontEnd::MightHaveChildren ()
116{
117    return true;
118}
119
120size_t
121lldb_private::formatters::LibcxxStdUnorderedMapSyntheticFrontEnd::GetIndexOfChildWithName (const ConstString &name)
122{
123    return ExtractIndexFromString(name.GetCString());
124}
125
126lldb_private::formatters::LibcxxStdUnorderedMapSyntheticFrontEnd::~LibcxxStdUnorderedMapSyntheticFrontEnd ()
127{}
128
129SyntheticChildrenFrontEnd*
130lldb_private::formatters::LibcxxStdUnorderedMapSyntheticFrontEndCreator (CXXSyntheticChildren*, lldb::ValueObjectSP valobj_sp)
131{
132    if (!valobj_sp)
133        return NULL;
134    return (new LibcxxStdUnorderedMapSyntheticFrontEnd(valobj_sp));
135}
136