1 //===-- TypeCategoryMap.cpp ----------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/DataFormatters/TypeCategoryMap.h"
12 #include "lldb/Core/Log.h"
13 #include "lldb/DataFormatters/FormatClasses.h"
17 // Other libraries and framework includes
21 using namespace lldb_private;
23 TypeCategoryMap::TypeCategoryMap(IFormatChangeListener *lst)
24 : m_map_mutex(), listener(lst), m_map(), m_active_categories()
26 ConstString default_cs("default");
27 lldb::TypeCategoryImplSP default_sp = lldb::TypeCategoryImplSP(new TypeCategoryImpl(listener, default_cs));
28 Add(default_cs, default_sp);
29 Enable(default_cs, First);
33 TypeCategoryMap::Add (KeyType name, const ValueSP& entry)
35 std::lock_guard<std::recursive_mutex> guard(m_map_mutex);
42 TypeCategoryMap::Delete (KeyType name)
44 std::lock_guard<std::recursive_mutex> guard(m_map_mutex);
45 MapIterator iter = m_map.find(name);
46 if (iter == m_map.end())
56 TypeCategoryMap::Enable (KeyType category_name, Position pos)
58 std::lock_guard<std::recursive_mutex> guard(m_map_mutex);
60 if (!Get(category_name,category))
62 return Enable(category, pos);
66 TypeCategoryMap::Disable (KeyType category_name)
68 std::lock_guard<std::recursive_mutex> guard(m_map_mutex);
70 if (!Get(category_name,category))
72 return Disable(category);
76 TypeCategoryMap::Enable (ValueSP category, Position pos)
78 std::lock_guard<std::recursive_mutex> guard(m_map_mutex);
82 if (pos == First || m_active_categories.size() == 0)
83 m_active_categories.push_front(category);
84 else if (pos == Last || pos == m_active_categories.size())
85 m_active_categories.push_back(category);
86 else if (pos < m_active_categories.size())
88 ActiveCategoriesList::iterator iter = m_active_categories.begin();
93 m_active_categories.insert(iter,category);
97 category->Enable(true,
105 TypeCategoryMap::Disable (ValueSP category)
107 std::lock_guard<std::recursive_mutex> guard(m_map_mutex);
110 m_active_categories.remove_if(delete_matching_categories(category));
118 TypeCategoryMap::EnableAllCategories ()
120 std::lock_guard<std::recursive_mutex> guard(m_map_mutex);
121 std::vector<ValueSP> sorted_categories(m_map.size(), ValueSP());
122 MapType::iterator iter = m_map.begin(), end = m_map.end();
123 for (; iter != end; ++iter)
125 if (iter->second->IsEnabled())
127 auto pos = iter->second->GetLastEnabledPosition();
128 if (pos >= sorted_categories.size())
130 auto iter = std::find_if(sorted_categories.begin(),
131 sorted_categories.end(),
132 [] (const ValueSP& sp) -> bool {
133 return sp.get() == nullptr;
135 pos = std::distance(sorted_categories.begin(), iter);
137 sorted_categories.at(pos) = iter->second;
139 decltype(sorted_categories)::iterator viter = sorted_categories.begin(), vend = sorted_categories.end();
140 for (; viter != vend; viter++)
142 Enable(*viter, Last);
146 TypeCategoryMap::DisableAllCategories ()
148 std::lock_guard<std::recursive_mutex> guard(m_map_mutex);
150 for (; false == m_active_categories.empty(); p++)
152 m_active_categories.front()->SetEnabledPosition(p);
153 Disable(m_active_categories.front());
158 TypeCategoryMap::Clear ()
160 std::lock_guard<std::recursive_mutex> guard(m_map_mutex);
162 m_active_categories.clear();
168 TypeCategoryMap::Get (KeyType name, ValueSP& entry)
170 std::lock_guard<std::recursive_mutex> guard(m_map_mutex);
171 MapIterator iter = m_map.find(name);
172 if (iter == m_map.end())
174 entry = iter->second;
179 TypeCategoryMap::Get (uint32_t pos, ValueSP& entry)
181 std::lock_guard<std::recursive_mutex> guard(m_map_mutex);
182 MapIterator iter = m_map.begin();
183 MapIterator end = m_map.end();
191 entry = iter->second;
196 TypeCategoryMap::AnyMatches (ConstString type_name,
197 TypeCategoryImpl::FormatCategoryItems items,
199 const char** matching_category,
200 TypeCategoryImpl::FormatCategoryItems* matching_type)
202 std::lock_guard<std::recursive_mutex> guard(m_map_mutex);
204 MapIterator pos, end = m_map.end();
205 for (pos = m_map.begin(); pos != end; pos++)
207 if (pos->second->AnyMatches(type_name,
217 lldb::TypeFormatImplSP
218 TypeCategoryMap::GetFormat (FormattersMatchData& match_data)
220 std::lock_guard<std::recursive_mutex> guard(m_map_mutex);
223 ActiveCategoriesIterator begin, end = m_active_categories.end();
225 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_DATAFORMATTERS));
229 for (auto match : match_data.GetMatchesVector())
231 log->Printf("[CategoryMap::GetFormat] candidate match = %s %s %s %s reason = %" PRIu32,
232 match.GetTypeName().GetCString(),
233 match.DidStripPointer() ? "strip-pointers" : "no-strip-pointers",
234 match.DidStripReference() ? "strip-reference" : "no-strip-reference",
235 match.DidStripTypedef() ? "strip-typedef" : "no-strip-typedef",
240 for (begin = m_active_categories.begin(); begin != end; begin++)
242 lldb::TypeCategoryImplSP category_sp = *begin;
243 lldb::TypeFormatImplSP current_format;
245 log->Printf("[TypeCategoryMap::GetFormat] Trying to use category %s", category_sp->GetName());
246 if (!category_sp->Get(match_data.GetValueObject(), match_data.GetMatchesVector(), current_format, &reason_why))
248 return current_format;
251 log->Printf("[TypeCategoryMap::GetFormat] nothing found - returning empty SP");
252 return lldb::TypeFormatImplSP();
255 lldb::TypeSummaryImplSP
256 TypeCategoryMap::GetSummaryFormat (FormattersMatchData& match_data)
258 std::lock_guard<std::recursive_mutex> guard(m_map_mutex);
261 ActiveCategoriesIterator begin, end = m_active_categories.end();
263 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_DATAFORMATTERS));
267 for (auto match : match_data.GetMatchesVector())
269 log->Printf("[CategoryMap::GetSummaryFormat] candidate match = %s %s %s %s reason = %" PRIu32,
270 match.GetTypeName().GetCString(),
271 match.DidStripPointer() ? "strip-pointers" : "no-strip-pointers",
272 match.DidStripReference() ? "strip-reference" : "no-strip-reference",
273 match.DidStripTypedef() ? "strip-typedef" : "no-strip-typedef",
278 for (begin = m_active_categories.begin(); begin != end; begin++)
280 lldb::TypeCategoryImplSP category_sp = *begin;
281 lldb::TypeSummaryImplSP current_format;
283 log->Printf("[CategoryMap::GetSummaryFormat] Trying to use category %s", category_sp->GetName());
284 if (!category_sp->Get(match_data.GetValueObject(), match_data.GetMatchesVector(), current_format, &reason_why))
286 return current_format;
289 log->Printf("[CategoryMap::GetSummaryFormat] nothing found - returning empty SP");
290 return lldb::TypeSummaryImplSP();
293 #ifndef LLDB_DISABLE_PYTHON
294 lldb::SyntheticChildrenSP
295 TypeCategoryMap::GetSyntheticChildren (FormattersMatchData& match_data)
297 std::lock_guard<std::recursive_mutex> guard(m_map_mutex);
301 ActiveCategoriesIterator begin, end = m_active_categories.end();
303 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_DATAFORMATTERS));
307 for (auto match : match_data.GetMatchesVector())
309 log->Printf("[CategoryMap::GetSyntheticChildren] candidate match = %s %s %s %s reason = %" PRIu32,
310 match.GetTypeName().GetCString(),
311 match.DidStripPointer() ? "strip-pointers" : "no-strip-pointers",
312 match.DidStripReference() ? "strip-reference" : "no-strip-reference",
313 match.DidStripTypedef() ? "strip-typedef" : "no-strip-typedef",
318 for (begin = m_active_categories.begin(); begin != end; begin++)
320 lldb::TypeCategoryImplSP category_sp = *begin;
321 lldb::SyntheticChildrenSP current_format;
323 log->Printf("[CategoryMap::GetSyntheticChildren] Trying to use category %s", category_sp->GetName());
324 if (!category_sp->Get(match_data.GetValueObject(), match_data.GetMatchesVector(), current_format, &reason_why))
326 return current_format;
329 log->Printf("[CategoryMap::GetSyntheticChildren] nothing found - returning empty SP");
330 return lldb::SyntheticChildrenSP();
334 lldb::TypeValidatorImplSP
335 TypeCategoryMap::GetValidator (FormattersMatchData& match_data)
337 std::lock_guard<std::recursive_mutex> guard(m_map_mutex);
340 ActiveCategoriesIterator begin, end = m_active_categories.end();
342 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_DATAFORMATTERS));
346 for (auto match : match_data.GetMatchesVector())
348 log->Printf("[CategoryMap::GetValidator] candidate match = %s %s %s %s reason = %" PRIu32,
349 match.GetTypeName().GetCString(),
350 match.DidStripPointer() ? "strip-pointers" : "no-strip-pointers",
351 match.DidStripReference() ? "strip-reference" : "no-strip-reference",
352 match.DidStripTypedef() ? "strip-typedef" : "no-strip-typedef",
357 for (begin = m_active_categories.begin(); begin != end; begin++)
359 lldb::TypeCategoryImplSP category_sp = *begin;
360 lldb::TypeValidatorImplSP current_format;
362 log->Printf("[CategoryMap::GetValidator] Trying to use category %s", category_sp->GetName());
363 if (!category_sp->Get(match_data.GetValueObject(), match_data.GetMatchesVector(), current_format, &reason_why))
365 return current_format;
368 log->Printf("[CategoryMap::GetValidator] nothing found - returning empty SP");
369 return lldb::TypeValidatorImplSP();
373 TypeCategoryMap::ForEach(ForEachCallback callback)
377 std::lock_guard<std::recursive_mutex> guard(m_map_mutex);
379 // loop through enabled categories in respective order
381 ActiveCategoriesIterator begin, end = m_active_categories.end();
382 for (begin = m_active_categories.begin(); begin != end; begin++)
384 lldb::TypeCategoryImplSP category = *begin;
385 if (!callback(category))
390 // loop through disabled categories in just any order
392 MapIterator pos, end = m_map.end();
393 for (pos = m_map.begin(); pos != end; pos++)
395 if (pos->second->IsEnabled())
397 KeyType type = pos->first;
398 if (!callback(pos->second))
406 TypeCategoryMap::GetAtIndex (uint32_t index)
408 std::lock_guard<std::recursive_mutex> guard(m_map_mutex);
410 if (index < m_map.size())
412 MapIterator pos, end = m_map.end();
413 for (pos = m_map.begin(); pos != end; pos++)
421 return TypeCategoryImplSP();