]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/source/DataFormatters/FormatCache.cpp
Merge llvm, clang, compiler-rt, libc++, libunwind, lld, lldb and openmp
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / source / DataFormatters / FormatCache.cpp
1 //===-- FormatCache.cpp ------------------------------------------*- C++
2 //-*-===//
3 //
4 //                     The LLVM Compiler Infrastructure
5 //
6 // This file is distributed under the University of Illinois Open Source
7 // License. See LICENSE.TXT for details.
8 //
9 //===----------------------------------------------------------------------===//
10
11
12
13
14 #include "lldb/DataFormatters/FormatCache.h"
15
16 using namespace lldb;
17 using namespace lldb_private;
18
19 FormatCache::Entry::Entry()
20     : m_format_cached(false), m_summary_cached(false),
21       m_synthetic_cached(false), m_validator_cached(false), m_format_sp(),
22       m_summary_sp(), m_synthetic_sp(), m_validator_sp() {}
23
24 FormatCache::Entry::Entry(lldb::TypeFormatImplSP format_sp)
25     : m_summary_cached(false), m_synthetic_cached(false),
26       m_validator_cached(false), m_summary_sp(), m_synthetic_sp(),
27       m_validator_sp() {
28   SetFormat(format_sp);
29 }
30
31 FormatCache::Entry::Entry(lldb::TypeSummaryImplSP summary_sp)
32     : m_format_cached(false), m_synthetic_cached(false),
33       m_validator_cached(false), m_format_sp(), m_synthetic_sp(),
34       m_validator_sp() {
35   SetSummary(summary_sp);
36 }
37
38 FormatCache::Entry::Entry(lldb::SyntheticChildrenSP synthetic_sp)
39     : m_format_cached(false), m_summary_cached(false),
40       m_validator_cached(false), m_format_sp(), m_summary_sp(),
41       m_validator_sp() {
42   SetSynthetic(synthetic_sp);
43 }
44
45 FormatCache::Entry::Entry(lldb::TypeValidatorImplSP validator_sp)
46     : m_format_cached(false), m_summary_cached(false),
47       m_synthetic_cached(false), m_format_sp(), m_summary_sp(),
48       m_synthetic_sp() {
49   SetValidator(validator_sp);
50 }
51
52 FormatCache::Entry::Entry(lldb::TypeFormatImplSP format_sp,
53                           lldb::TypeSummaryImplSP summary_sp,
54                           lldb::SyntheticChildrenSP synthetic_sp,
55                           lldb::TypeValidatorImplSP validator_sp) {
56   SetFormat(format_sp);
57   SetSummary(summary_sp);
58   SetSynthetic(synthetic_sp);
59   SetValidator(validator_sp);
60 }
61
62 bool FormatCache::Entry::IsFormatCached() { return m_format_cached; }
63
64 bool FormatCache::Entry::IsSummaryCached() { return m_summary_cached; }
65
66 bool FormatCache::Entry::IsSyntheticCached() { return m_synthetic_cached; }
67
68 bool FormatCache::Entry::IsValidatorCached() { return m_validator_cached; }
69
70 lldb::TypeFormatImplSP FormatCache::Entry::GetFormat() { return m_format_sp; }
71
72 lldb::TypeSummaryImplSP FormatCache::Entry::GetSummary() {
73   return m_summary_sp;
74 }
75
76 lldb::SyntheticChildrenSP FormatCache::Entry::GetSynthetic() {
77   return m_synthetic_sp;
78 }
79
80 lldb::TypeValidatorImplSP FormatCache::Entry::GetValidator() {
81   return m_validator_sp;
82 }
83
84 void FormatCache::Entry::SetFormat(lldb::TypeFormatImplSP format_sp) {
85   m_format_cached = true;
86   m_format_sp = format_sp;
87 }
88
89 void FormatCache::Entry::SetSummary(lldb::TypeSummaryImplSP summary_sp) {
90   m_summary_cached = true;
91   m_summary_sp = summary_sp;
92 }
93
94 void FormatCache::Entry::SetSynthetic(lldb::SyntheticChildrenSP synthetic_sp) {
95   m_synthetic_cached = true;
96   m_synthetic_sp = synthetic_sp;
97 }
98
99 void FormatCache::Entry::SetValidator(lldb::TypeValidatorImplSP validator_sp) {
100   m_validator_cached = true;
101   m_validator_sp = validator_sp;
102 }
103
104 FormatCache::FormatCache()
105     : m_map(), m_mutex()
106 #ifdef LLDB_CONFIGURATION_DEBUG
107       ,
108       m_cache_hits(0), m_cache_misses(0)
109 #endif
110 {
111 }
112
113 FormatCache::Entry &FormatCache::GetEntry(const ConstString &type) {
114   auto i = m_map.find(type), e = m_map.end();
115   if (i != e)
116     return i->second;
117   m_map[type] = FormatCache::Entry();
118   return m_map[type];
119 }
120
121 bool FormatCache::GetFormat(const ConstString &type,
122                             lldb::TypeFormatImplSP &format_sp) {
123   std::lock_guard<std::recursive_mutex> guard(m_mutex);
124   auto entry = GetEntry(type);
125   if (entry.IsFormatCached()) {
126 #ifdef LLDB_CONFIGURATION_DEBUG
127     m_cache_hits++;
128 #endif
129     format_sp = entry.GetFormat();
130     return true;
131   }
132 #ifdef LLDB_CONFIGURATION_DEBUG
133   m_cache_misses++;
134 #endif
135   format_sp.reset();
136   return false;
137 }
138
139 bool FormatCache::GetSummary(const ConstString &type,
140                              lldb::TypeSummaryImplSP &summary_sp) {
141   std::lock_guard<std::recursive_mutex> guard(m_mutex);
142   auto entry = GetEntry(type);
143   if (entry.IsSummaryCached()) {
144 #ifdef LLDB_CONFIGURATION_DEBUG
145     m_cache_hits++;
146 #endif
147     summary_sp = entry.GetSummary();
148     return true;
149   }
150 #ifdef LLDB_CONFIGURATION_DEBUG
151   m_cache_misses++;
152 #endif
153   summary_sp.reset();
154   return false;
155 }
156
157 bool FormatCache::GetSynthetic(const ConstString &type,
158                                lldb::SyntheticChildrenSP &synthetic_sp) {
159   std::lock_guard<std::recursive_mutex> guard(m_mutex);
160   auto entry = GetEntry(type);
161   if (entry.IsSyntheticCached()) {
162 #ifdef LLDB_CONFIGURATION_DEBUG
163     m_cache_hits++;
164 #endif
165     synthetic_sp = entry.GetSynthetic();
166     return true;
167   }
168 #ifdef LLDB_CONFIGURATION_DEBUG
169   m_cache_misses++;
170 #endif
171   synthetic_sp.reset();
172   return false;
173 }
174
175 bool FormatCache::GetValidator(const ConstString &type,
176                                lldb::TypeValidatorImplSP &validator_sp) {
177   std::lock_guard<std::recursive_mutex> guard(m_mutex);
178   auto entry = GetEntry(type);
179   if (entry.IsValidatorCached()) {
180 #ifdef LLDB_CONFIGURATION_DEBUG
181     m_cache_hits++;
182 #endif
183     validator_sp = entry.GetValidator();
184     return true;
185   }
186 #ifdef LLDB_CONFIGURATION_DEBUG
187   m_cache_misses++;
188 #endif
189   validator_sp.reset();
190   return false;
191 }
192
193 void FormatCache::SetFormat(const ConstString &type,
194                             lldb::TypeFormatImplSP &format_sp) {
195   std::lock_guard<std::recursive_mutex> guard(m_mutex);
196   GetEntry(type).SetFormat(format_sp);
197 }
198
199 void FormatCache::SetSummary(const ConstString &type,
200                              lldb::TypeSummaryImplSP &summary_sp) {
201   std::lock_guard<std::recursive_mutex> guard(m_mutex);
202   GetEntry(type).SetSummary(summary_sp);
203 }
204
205 void FormatCache::SetSynthetic(const ConstString &type,
206                                lldb::SyntheticChildrenSP &synthetic_sp) {
207   std::lock_guard<std::recursive_mutex> guard(m_mutex);
208   GetEntry(type).SetSynthetic(synthetic_sp);
209 }
210
211 void FormatCache::SetValidator(const ConstString &type,
212                                lldb::TypeValidatorImplSP &validator_sp) {
213   std::lock_guard<std::recursive_mutex> guard(m_mutex);
214   GetEntry(type).SetValidator(validator_sp);
215 }
216
217 void FormatCache::Clear() {
218   std::lock_guard<std::recursive_mutex> guard(m_mutex);
219   m_map.clear();
220 }