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
20 ConstString default_cs("default");
21 lldb::TypeCategoryImplSP default_sp =
22 std::make_shared<TypeCategoryImpl>(listener, default_cs);
23 Add(default_cs, default_sp);
24 Enable(default_cs, First);
25}
26
28 {
29 std::lock_guard<std::recursive_mutex> guard(m_map_mutex);
30 m_map[name] = entry;
31 }
32 // Release the mutex to avoid a potential deadlock between
33 // TypeCategoryMap::m_map_mutex and
34 // FormatManager::m_language_categories_mutex which can be acquired in
35 // reverse order when calling FormatManager::Changed.
36 if (listener)
37 listener->Changed();
38}
39
41 {
42 std::lock_guard<std::recursive_mutex> guard(m_map_mutex);
43 MapIterator iter = m_map.find(name);
44 if (iter == m_map.end())
45 return false;
46 m_map.erase(name);
47 Disable(name);
48 }
49 // Release the mutex to avoid a potential deadlock between
50 // TypeCategoryMap::m_map_mutex and
51 // FormatManager::m_language_categories_mutex which can be acquired in
52 // reverse order when calling FormatManager::Changed.
53 if (listener)
54 listener->Changed();
55 return true;
56}
57
58bool TypeCategoryMap::Enable(KeyType category_name, Position pos) {
59 std::lock_guard<std::recursive_mutex> guard(m_map_mutex);
60 TypeCategoryImplSP category;
61 if (!Get(category_name, category))
62 return false;
63 return Enable(category, pos);
64}
65
66bool TypeCategoryMap::Disable(KeyType category_name) {
67 std::lock_guard<std::recursive_mutex> guard(m_map_mutex);
68 TypeCategoryImplSP category;
69 if (!Get(category_name, category))
70 return false;
71 return Disable(category);
72}
73
75 std::lock_guard<std::recursive_mutex> guard(m_map_mutex);
76 if (category.get()) {
77 Position pos_w = pos;
78 if (pos == First || m_active_categories.size() == 0)
79 m_active_categories.push_front(category);
80 else if (pos == Last || pos == m_active_categories.size())
81 m_active_categories.push_back(category);
82 else if (pos < m_active_categories.size()) {
83 ActiveCategoriesList::iterator iter = m_active_categories.begin();
84 while (pos_w) {
85 pos_w--, iter++;
86 }
87 m_active_categories.insert(iter, category);
88 } else
89 return false;
90 category->Enable(true, pos);
91 return true;
92 }
93 return false;
94}
95
97 std::lock_guard<std::recursive_mutex> guard(m_map_mutex);
98 if (category.get()) {
100 category->Disable();
101 return true;
102 }
103 return false;
104}
105
107 std::lock_guard<std::recursive_mutex> guard(m_map_mutex);
108 std::vector<TypeCategoryImplSP> sorted_categories(m_map.size(), TypeCategoryImplSP());
109 MapType::iterator iter = m_map.begin(), end = m_map.end();
110 for (; iter != end; ++iter) {
111 if (iter->second->IsEnabled())
112 continue;
113 auto pos = iter->second->GetLastEnabledPosition();
114 if (pos >= sorted_categories.size()) {
115 auto iter = llvm::find_if(sorted_categories,
116 [](const TypeCategoryImplSP &sp) -> bool {
117 return sp.get() == nullptr;
118 });
119 pos = std::distance(sorted_categories.begin(), iter);
120 }
121 sorted_categories.at(pos) = iter->second;
122 }
123 decltype(sorted_categories)::iterator viter = sorted_categories.begin(),
124 vend = sorted_categories.end();
125 for (; viter != vend; viter++)
126 if (*viter)
127 Enable(*viter, Last);
128}
129
131 std::lock_guard<std::recursive_mutex> guard(m_map_mutex);
132 for (Position p = First; !m_active_categories.empty(); p++) {
133 m_active_categories.front()->SetEnabledPosition(p);
135 }
136}
137
139 {
140 std::lock_guard<std::recursive_mutex> guard(m_map_mutex);
141 m_map.clear();
142 m_active_categories.clear();
143 }
144 // Release the mutex to avoid a potential deadlock between
145 // TypeCategoryMap::m_map_mutex and
146 // FormatManager::m_language_categories_mutex which can be acquired in
147 // reverse order when calling FormatManager::Changed.
148 if (listener)
149 listener->Changed();
150}
151
153 std::lock_guard<std::recursive_mutex> guard(m_map_mutex);
154 MapIterator iter = m_map.find(name);
155 if (iter == m_map.end())
156 return false;
157 entry = iter->second;
158 return true;
159}
160
162 const FormattersMatchCandidate &candidate_type,
163 TypeCategoryImpl::FormatCategoryItems items, bool only_enabled,
164 const char **matching_category,
166 std::lock_guard<std::recursive_mutex> guard(m_map_mutex);
167
168 MapIterator pos, end = m_map.end();
169 for (pos = m_map.begin(); pos != end; pos++) {
170 if (pos->second->AnyMatches(candidate_type, items, only_enabled,
171 matching_category, matching_type))
172 return true;
173 }
174 return false;
175}
176
177template <typename ImplSP>
178void TypeCategoryMap::Get(FormattersMatchData &match_data, ImplSP &retval) {
179 std::lock_guard<std::recursive_mutex> guard(m_map_mutex);
180
182
184
185 if (log) {
186 for (auto match : match_data.GetMatchesVector()) {
187 LLDB_LOGF(
188 log, "[%s] candidate match = %s %s %s %s ptr-stripped-depth=%u",
189 __FUNCTION__, match.GetTypeName().GetCString(),
190 match.DidStripPointer() ? "strip-pointers" : "no-strip-pointers",
191 match.DidStripReference() ? "strip-reference" : "no-strip-reference",
192 match.DidStripTypedef() ? "strip-typedef" : "no-strip-typedef",
193 match.GetPtrStrippedDepth());
194 }
195 }
196
197 for (begin = m_active_categories.begin(); begin != end; begin++) {
198 lldb::TypeCategoryImplSP category_sp = *begin;
199 ImplSP current_format;
200 LLDB_LOGF(log, "[%s] Trying to use category %s", __FUNCTION__,
201 category_sp->GetName());
202 if (!category_sp->Get(
204 match_data.GetMatchesVector(), current_format))
205 continue;
206
207 retval = std::move(current_format);
208 return;
209 }
210 LLDB_LOGF(log, "[%s] nothing found - returning empty SP", __FUNCTION__);
211}
212
213/// Explicit instantiations for the three types.
214/// \{
215template void
217 lldb::TypeFormatImplSP &retval);
218template void
223/// \}
224
226 if (callback) {
227 std::lock_guard<std::recursive_mutex> guard(m_map_mutex);
228
229 // loop through enabled categories in respective order
230 {
232 for (begin = m_active_categories.begin(); begin != end; begin++) {
233 lldb::TypeCategoryImplSP category = *begin;
234 if (!callback(category))
235 break;
236 }
237 }
238
239 // loop through disabled categories in just any order
240 {
241 MapIterator pos, end = m_map.end();
242 for (pos = m_map.begin(); pos != end; pos++) {
243 if (pos->second->IsEnabled())
244 continue;
245 if (!callback(pos->second))
246 break;
247 }
248 }
249 }
250}
251
253 std::lock_guard<std::recursive_mutex> guard(m_map_mutex);
254
255 if (index < m_map.size()) {
256 MapIterator pos, end = m_map.end();
257 for (pos = m_map.begin(); pos != end; pos++) {
258 if (index == 0)
259 return pos->second;
260 index--;
261 }
262 }
263
264 return TypeCategoryImplSP();
265}
#define LLDB_LOGF(log,...)
Definition Log.h:376
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()
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition Log.h:332
std::shared_ptr< lldb_private::TypeSummaryImpl > TypeSummaryImplSP
std::shared_ptr< lldb_private::TypeFormatImpl > TypeFormatImplSP
std::shared_ptr< lldb_private::SyntheticChildren > SyntheticChildrenSP
std::shared_ptr< lldb_private::TypeCategoryImpl > TypeCategoryImplSP