1 //===-- FormatCache.cpp ------------------------------------------*- C++
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8 //===----------------------------------------------------------------------===//
13 #include "lldb/DataFormatters/FormatCache.h"
16 using namespace lldb_private;
18 FormatCache::Entry::Entry()
19 : m_format_cached(false), m_summary_cached(false),
20 m_synthetic_cached(false), m_validator_cached(false), m_format_sp(),
21 m_summary_sp(), m_synthetic_sp(), m_validator_sp() {}
23 FormatCache::Entry::Entry(lldb::TypeFormatImplSP format_sp)
24 : m_summary_cached(false), m_synthetic_cached(false),
25 m_validator_cached(false), m_summary_sp(), m_synthetic_sp(),
30 FormatCache::Entry::Entry(lldb::TypeSummaryImplSP summary_sp)
31 : m_format_cached(false), m_synthetic_cached(false),
32 m_validator_cached(false), m_format_sp(), m_synthetic_sp(),
34 SetSummary(summary_sp);
37 FormatCache::Entry::Entry(lldb::SyntheticChildrenSP synthetic_sp)
38 : m_format_cached(false), m_summary_cached(false),
39 m_validator_cached(false), m_format_sp(), m_summary_sp(),
41 SetSynthetic(synthetic_sp);
44 FormatCache::Entry::Entry(lldb::TypeValidatorImplSP validator_sp)
45 : m_format_cached(false), m_summary_cached(false),
46 m_synthetic_cached(false), m_format_sp(), m_summary_sp(),
48 SetValidator(validator_sp);
51 FormatCache::Entry::Entry(lldb::TypeFormatImplSP format_sp,
52 lldb::TypeSummaryImplSP summary_sp,
53 lldb::SyntheticChildrenSP synthetic_sp,
54 lldb::TypeValidatorImplSP validator_sp) {
56 SetSummary(summary_sp);
57 SetSynthetic(synthetic_sp);
58 SetValidator(validator_sp);
61 bool FormatCache::Entry::IsFormatCached() { return m_format_cached; }
63 bool FormatCache::Entry::IsSummaryCached() { return m_summary_cached; }
65 bool FormatCache::Entry::IsSyntheticCached() { return m_synthetic_cached; }
67 bool FormatCache::Entry::IsValidatorCached() { return m_validator_cached; }
69 lldb::TypeFormatImplSP FormatCache::Entry::GetFormat() { return m_format_sp; }
71 lldb::TypeSummaryImplSP FormatCache::Entry::GetSummary() {
75 lldb::SyntheticChildrenSP FormatCache::Entry::GetSynthetic() {
76 return m_synthetic_sp;
79 lldb::TypeValidatorImplSP FormatCache::Entry::GetValidator() {
80 return m_validator_sp;
83 void FormatCache::Entry::SetFormat(lldb::TypeFormatImplSP format_sp) {
84 m_format_cached = true;
85 m_format_sp = format_sp;
88 void FormatCache::Entry::SetSummary(lldb::TypeSummaryImplSP summary_sp) {
89 m_summary_cached = true;
90 m_summary_sp = summary_sp;
93 void FormatCache::Entry::SetSynthetic(lldb::SyntheticChildrenSP synthetic_sp) {
94 m_synthetic_cached = true;
95 m_synthetic_sp = synthetic_sp;
98 void FormatCache::Entry::SetValidator(lldb::TypeValidatorImplSP validator_sp) {
99 m_validator_cached = true;
100 m_validator_sp = validator_sp;
103 FormatCache::FormatCache()
105 #ifdef LLDB_CONFIGURATION_DEBUG
107 m_cache_hits(0), m_cache_misses(0)
112 FormatCache::Entry &FormatCache::GetEntry(ConstString type) {
113 auto i = m_map.find(type), e = m_map.end();
116 m_map[type] = FormatCache::Entry();
120 bool FormatCache::GetFormat(ConstString type,
121 lldb::TypeFormatImplSP &format_sp) {
122 std::lock_guard<std::recursive_mutex> guard(m_mutex);
123 auto entry = GetEntry(type);
124 if (entry.IsFormatCached()) {
125 #ifdef LLDB_CONFIGURATION_DEBUG
128 format_sp = entry.GetFormat();
131 #ifdef LLDB_CONFIGURATION_DEBUG
138 bool FormatCache::GetSummary(ConstString type,
139 lldb::TypeSummaryImplSP &summary_sp) {
140 std::lock_guard<std::recursive_mutex> guard(m_mutex);
141 auto entry = GetEntry(type);
142 if (entry.IsSummaryCached()) {
143 #ifdef LLDB_CONFIGURATION_DEBUG
146 summary_sp = entry.GetSummary();
149 #ifdef LLDB_CONFIGURATION_DEBUG
156 bool FormatCache::GetSynthetic(ConstString type,
157 lldb::SyntheticChildrenSP &synthetic_sp) {
158 std::lock_guard<std::recursive_mutex> guard(m_mutex);
159 auto entry = GetEntry(type);
160 if (entry.IsSyntheticCached()) {
161 #ifdef LLDB_CONFIGURATION_DEBUG
164 synthetic_sp = entry.GetSynthetic();
167 #ifdef LLDB_CONFIGURATION_DEBUG
170 synthetic_sp.reset();
174 bool FormatCache::GetValidator(ConstString type,
175 lldb::TypeValidatorImplSP &validator_sp) {
176 std::lock_guard<std::recursive_mutex> guard(m_mutex);
177 auto entry = GetEntry(type);
178 if (entry.IsValidatorCached()) {
179 #ifdef LLDB_CONFIGURATION_DEBUG
182 validator_sp = entry.GetValidator();
185 #ifdef LLDB_CONFIGURATION_DEBUG
188 validator_sp.reset();
192 void FormatCache::SetFormat(ConstString type,
193 lldb::TypeFormatImplSP &format_sp) {
194 std::lock_guard<std::recursive_mutex> guard(m_mutex);
195 GetEntry(type).SetFormat(format_sp);
198 void FormatCache::SetSummary(ConstString type,
199 lldb::TypeSummaryImplSP &summary_sp) {
200 std::lock_guard<std::recursive_mutex> guard(m_mutex);
201 GetEntry(type).SetSummary(summary_sp);
204 void FormatCache::SetSynthetic(ConstString type,
205 lldb::SyntheticChildrenSP &synthetic_sp) {
206 std::lock_guard<std::recursive_mutex> guard(m_mutex);
207 GetEntry(type).SetSynthetic(synthetic_sp);
210 void FormatCache::SetValidator(ConstString type,
211 lldb::TypeValidatorImplSP &validator_sp) {
212 std::lock_guard<std::recursive_mutex> guard(m_mutex);
213 GetEntry(type).SetValidator(validator_sp);
216 void FormatCache::Clear() {
217 std::lock_guard<std::recursive_mutex> guard(m_mutex);