LLDB mainline
TypeCategoryMap.cpp
Go to the documentation of this file.
1//===-- TypeCategoryMap.cpp -----------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
10
13#include "lldb/Utility/Log.h"
14
15using namespace lldb;
16using namespace lldb_private;
17
19 : m_map_mutex(), listener(lst), m_map(), m_active_categories() {
20 ConstString default_cs("default");
21 lldb::TypeCategoryImplSP default_sp =
23 Add(default_cs, default_sp);
24 Enable(default_cs, First);
25}
26
28 std::lock_guard<std::recursive_mutex> guard(m_map_mutex);
29 m_map[name] = entry;
30 if (listener)
32}
33
35 std::lock_guard<std::recursive_mutex> guard(m_map_mutex);
36 MapIterator iter = m_map.find(name);
37 if (iter == m_map.end())
38 return false;
39 m_map.erase(name);
40 Disable(name);
41 if (listener)
43 return true;
44}
45
46bool TypeCategoryMap::Enable(KeyType category_name, Position pos) {
47 std::lock_guard<std::recursive_mutex> guard(m_map_mutex);
48 TypeCategoryImplSP category;
49 if (!Get(category_name, category))
50 return false;
51 return Enable(category, pos);
52}
53
54bool TypeCategoryMap::Disable(KeyType category_name) {
55 std::lock_guard<std::recursive_mutex> guard(m_map_mutex);
56 TypeCategoryImplSP category;
57 if (!Get(category_name, category))
58 return false;
59 return Disable(category);
60}
61
63 std::lock_guard<std::recursive_mutex> guard(m_map_mutex);
64 if (category.get()) {
65 Position pos_w = pos;
66 if (pos == First || m_active_categories.size() == 0)
67 m_active_categories.push_front(category);
68 else if (pos == Last || pos == m_active_categories.size())
69 m_active_categories.push_back(category);
70 else if (pos < m_active_categories.size()) {
71 ActiveCategoriesList::iterator iter = m_active_categories.begin();
72 while (pos_w) {
73 pos_w--, iter++;
74 }
75 m_active_categories.insert(iter, category);
76 } else
77 return false;
78 category->Enable(true, pos);
79 return true;
80 }
81 return false;
82}
83
85 std::lock_guard<std::recursive_mutex> guard(m_map_mutex);
86 if (category.get()) {
88 category->Disable();
89 return true;
90 }
91 return false;
92}
93
95 std::lock_guard<std::recursive_mutex> guard(m_map_mutex);
96 std::vector<TypeCategoryImplSP> sorted_categories(m_map.size(), TypeCategoryImplSP());
97 MapType::iterator iter = m_map.begin(), end = m_map.end();
98 for (; iter != end; ++iter) {
99 if (iter->second->IsEnabled())
100 continue;
101 auto pos = iter->second->GetLastEnabledPosition();
102 if (pos >= sorted_categories.size()) {
103 auto iter = std::find_if(
104 sorted_categories.begin(), sorted_categories.end(),
105 [](const TypeCategoryImplSP &sp) -> bool { return sp.get() == nullptr; });
106 pos = std::distance(sorted_categories.begin(), iter);
107 }
108 sorted_categories.at(pos) = iter->second;
109 }
110 decltype(sorted_categories)::iterator viter = sorted_categories.begin(),
111 vend = sorted_categories.end();
112 for (; viter != vend; viter++)
113 if (*viter)
114 Enable(*viter, Last);
115}
116
118 std::lock_guard<std::recursive_mutex> guard(m_map_mutex);
119 for (Position p = First; !m_active_categories.empty(); p++) {
120 m_active_categories.front()->SetEnabledPosition(p);
122 }
123}
124
126 std::lock_guard<std::recursive_mutex> guard(m_map_mutex);
127 m_map.clear();
128 m_active_categories.clear();
129 if (listener)
130 listener->Changed();
131}
132
134 std::lock_guard<std::recursive_mutex> guard(m_map_mutex);
135 MapIterator iter = m_map.find(name);
136 if (iter == m_map.end())
137 return false;
138 entry = iter->second;
139 return true;
140}
141
143 const FormattersMatchCandidate &candidate_type,
144 TypeCategoryImpl::FormatCategoryItems items, bool only_enabled,
145 const char **matching_category,
147 std::lock_guard<std::recursive_mutex> guard(m_map_mutex);
148
149 MapIterator pos, end = m_map.end();
150 for (pos = m_map.begin(); pos != end; pos++) {
151 if (pos->second->AnyMatches(candidate_type, items, only_enabled,
152 matching_category, matching_type))
153 return true;
154 }
155 return false;
156}
157
158template <typename ImplSP>
159void TypeCategoryMap::Get(FormattersMatchData &match_data, ImplSP &retval) {
160 std::lock_guard<std::recursive_mutex> guard(m_map_mutex);
161
163
165
166 if (log) {
167 for (auto match : match_data.GetMatchesVector()) {
168 LLDB_LOGF(
169 log,
170 "[%s] candidate match = %s %s %s %s",
171 __FUNCTION__,
172 match.GetTypeName().GetCString(),
173 match.DidStripPointer() ? "strip-pointers" : "no-strip-pointers",
174 match.DidStripReference() ? "strip-reference" : "no-strip-reference",
175 match.DidStripTypedef() ? "strip-typedef" : "no-strip-typedef");
176 }
177 }
178
179 for (begin = m_active_categories.begin(); begin != end; begin++) {
180 lldb::TypeCategoryImplSP category_sp = *begin;
181 ImplSP current_format;
182 LLDB_LOGF(log, "[%s] Trying to use category %s", __FUNCTION__,
183 category_sp->GetName());
184 if (!category_sp->Get(
186 match_data.GetMatchesVector(), current_format))
187 continue;
188
189 retval = std::move(current_format);
190 return;
191 }
192 LLDB_LOGF(log, "[%s] nothing found - returning empty SP", __FUNCTION__);
193}
194
195/// Explicit instantiations for the three types.
196/// \{
197template void
198TypeCategoryMap::Get<lldb::TypeFormatImplSP>(FormattersMatchData &match_data,
199 lldb::TypeFormatImplSP &retval);
200template void
201TypeCategoryMap::Get<lldb::TypeSummaryImplSP>(FormattersMatchData &match_data,
203template void TypeCategoryMap::Get<lldb::SyntheticChildrenSP>(
205/// \}
206
208 if (callback) {
209 std::lock_guard<std::recursive_mutex> guard(m_map_mutex);
210
211 // loop through enabled categories in respective order
212 {
214 for (begin = m_active_categories.begin(); begin != end; begin++) {
215 lldb::TypeCategoryImplSP category = *begin;
216 if (!callback(category))
217 break;
218 }
219 }
220
221 // loop through disabled categories in just any order
222 {
223 MapIterator pos, end = m_map.end();
224 for (pos = m_map.begin(); pos != end; pos++) {
225 if (pos->second->IsEnabled())
226 continue;
227 if (!callback(pos->second))
228 break;
229 }
230 }
231 }
232}
233
235 std::lock_guard<std::recursive_mutex> guard(m_map_mutex);
236
237 if (index < m_map.size()) {
238 MapIterator pos, end = m_map.end();
239 for (pos = m_map.begin(); pos != end; pos++) {
240 if (index == 0)
241 return pos->second;
242 index--;
243 }
244 }
245
246 return TypeCategoryImplSP();
247}
#define LLDB_LOGF(log,...)
Definition: Log.h:349
A uniqued constant string class.
Definition: ConstString.h:40
FormattersMatchVector GetMatchesVector()
std::function< bool(const lldb::TypeCategoryImplSP &)> ForEachCallback
static const Position Last
bool AnyMatches(const FormattersMatchCandidate &candidate_type, TypeCategoryImpl::FormatCategoryItems items=TypeCategoryImpl::ALL_ITEM_TYPES, bool only_enabled=true, const char **matching_category=nullptr, TypeCategoryImpl::FormatCategoryItems *matching_type=nullptr)
void Add(KeyType name, const lldb::TypeCategoryImplSP &entry)
bool Enable(KeyType category_name, Position pos=Default)
lldb::TypeCategoryImplSP GetAtIndex(uint32_t)
std::recursive_mutex m_map_mutex
ActiveCategoriesList m_active_categories
ActiveCategoriesList::iterator ActiveCategoriesIterator
bool Disable(KeyType category_name)
IFormatChangeListener * listener
bool Get(KeyType name, lldb::TypeCategoryImplSP &entry)
static const Position First
void ForEach(ForEachCallback callback)
TypeCategoryMap(IFormatChangeListener *lst)
lldb::LanguageType GetObjectRuntimeLanguage()
Definition: ValueObject.h:373
A class that represents a running process on the host machine.
Definition: SBAttachInfo.h:14
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition: Log.h:314
Definition: SBAddress.h:15
std::shared_ptr< lldb_private::TypeSummaryImpl > TypeSummaryImplSP
Definition: lldb-forward.h:463
std::shared_ptr< lldb_private::TypeFormatImpl > TypeFormatImplSP
Definition: lldb-forward.h:460
std::shared_ptr< lldb_private::SyntheticChildren > SyntheticChildrenSP
Definition: lldb-forward.h:433
std::shared_ptr< lldb_private::TypeCategoryImpl > TypeCategoryImplSP
Definition: lldb-forward.h:451