]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/source/DataFormatters/TypeCategoryMap.cpp
Use the new insecure-lan-zones option instead of listing each AS112 zone
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / source / DataFormatters / TypeCategoryMap.cpp
1 //===-- TypeCategoryMap.cpp ----------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "lldb/DataFormatters/TypeCategoryMap.h"
11
12 #include "lldb/DataFormatters/FormatClasses.h"
13 #include "lldb/DataFormatters/FormatManager.h"
14
15 // C Includes
16 // C++ Includes
17 // Other libraries and framework includes
18 // Project includes
19
20 using namespace lldb;
21 using namespace lldb_private;
22
23 TypeCategoryMap::TypeCategoryMap (IFormatChangeListener* lst) :
24 m_map_mutex(Mutex::eMutexTypeRecursive),
25 listener(lst),
26 m_map(),
27 m_active_categories()
28 {
29     ConstString default_cs("default");
30     lldb::TypeCategoryImplSP default_sp = lldb::TypeCategoryImplSP(new TypeCategoryImpl(listener, default_cs));
31     Add(default_cs,default_sp);
32     Enable(default_cs,First);
33 }
34
35 void
36 TypeCategoryMap::Add (KeyType name, const ValueSP& entry)
37 {
38     Mutex::Locker locker(m_map_mutex);
39     m_map[name] = entry;
40     if (listener)
41         listener->Changed();
42 }
43
44 bool
45 TypeCategoryMap::Delete (KeyType name)
46 {
47     Mutex::Locker locker(m_map_mutex);
48     MapIterator iter = m_map.find(name);
49     if (iter == m_map.end())
50         return false;
51     m_map.erase(name);
52     Disable(name);
53     if (listener)
54         listener->Changed();
55     return true;
56 }
57
58 bool
59 TypeCategoryMap::Enable (KeyType category_name, Position pos)
60 {
61     Mutex::Locker locker(m_map_mutex);
62     ValueSP category;
63     if (!Get(category_name,category))
64         return false;
65     return Enable(category, pos);
66 }
67
68 bool
69 TypeCategoryMap::Disable (KeyType category_name)
70 {
71     Mutex::Locker locker(m_map_mutex);
72     ValueSP category;
73     if (!Get(category_name,category))
74         return false;
75     return Disable(category);
76 }
77
78 bool
79 TypeCategoryMap::Enable (ValueSP category, Position pos)
80 {
81     Mutex::Locker locker(m_map_mutex);
82     if (category.get())
83     {
84         Position pos_w = pos;
85         if (pos == First || m_active_categories.size() == 0)
86             m_active_categories.push_front(category);
87         else if (pos == Last || pos == m_active_categories.size())
88             m_active_categories.push_back(category);
89         else if (pos < m_active_categories.size())
90         {
91             ActiveCategoriesList::iterator iter = m_active_categories.begin();
92             while (pos_w)
93             {
94                 pos_w--,iter++;
95             }
96             m_active_categories.insert(iter,category);
97         }
98         else
99             return false;
100         category->Enable(true,
101                          pos);
102         return true;
103     }
104     return false;
105 }
106
107 bool
108 TypeCategoryMap::Disable (ValueSP category)
109 {
110     Mutex::Locker locker(m_map_mutex);
111     if (category.get())
112     {
113         m_active_categories.remove_if(delete_matching_categories(category));
114         category->Disable();
115         return true;
116     }
117     return false;
118 }
119
120 void
121 TypeCategoryMap::EnableAllCategories ()
122 {
123     Mutex::Locker locker(m_map_mutex);
124     std::vector<ValueSP> sorted_categories(m_map.size(), ValueSP());
125     MapType::iterator iter = m_map.begin(), end = m_map.end();
126     for (; iter != end; ++iter)
127     {
128         if (iter->second->IsEnabled())
129             continue;
130         auto pos = iter->second->GetLastEnabledPosition();
131         if (pos >= sorted_categories.size())
132         {
133             auto iter = std::find_if(sorted_categories.begin(),
134                                      sorted_categories.end(),
135                                      [] (const ValueSP& sp) -> bool {
136                                          return sp.get() == nullptr;
137                                      });
138             pos = std::distance(sorted_categories.begin(), iter);
139         }
140         sorted_categories.at(pos) = iter->second;
141     }
142     decltype(sorted_categories)::iterator viter = sorted_categories.begin(), vend = sorted_categories.end();
143     for (; viter != vend; viter++)
144         if (viter->get())
145             Enable(*viter, Last);
146 }
147
148 void
149 TypeCategoryMap::DisableAllCategories ()
150 {
151     Mutex::Locker locker(m_map_mutex);
152     Position p = First;
153     for (; false == m_active_categories.empty(); p++)
154     {
155         m_active_categories.front()->SetEnabledPosition(p);
156         Disable(m_active_categories.front());
157     }
158 }
159
160 void
161 TypeCategoryMap::Clear ()
162 {
163     Mutex::Locker locker(m_map_mutex);
164     m_map.clear();
165     m_active_categories.clear();
166     if (listener)
167         listener->Changed();
168 }
169
170 bool
171 TypeCategoryMap::Get (KeyType name, ValueSP& entry)
172 {
173     Mutex::Locker locker(m_map_mutex);
174     MapIterator iter = m_map.find(name);
175     if (iter == m_map.end())
176         return false;
177     entry = iter->second;
178     return true;
179 }
180
181 bool
182 TypeCategoryMap::Get (uint32_t pos, ValueSP& entry)
183 {
184     Mutex::Locker locker(m_map_mutex);
185     MapIterator iter = m_map.begin();
186     MapIterator end = m_map.end();
187     while (pos > 0)
188     {
189         iter++;
190         pos--;
191         if (iter == end)
192             return false;
193     }
194     entry = iter->second;
195     return false;
196 }
197
198 bool
199 TypeCategoryMap::AnyMatches (ConstString type_name,
200                              TypeCategoryImpl::FormatCategoryItems items,
201                              bool only_enabled,
202                              const char** matching_category,
203                              TypeCategoryImpl::FormatCategoryItems* matching_type)
204 {
205     Mutex::Locker locker(m_map_mutex);
206     
207     MapIterator pos, end = m_map.end();
208     for (pos = m_map.begin(); pos != end; pos++)
209     {
210         if (pos->second->AnyMatches(type_name,
211                                     items,
212                                     only_enabled,
213                                     matching_category,
214                                     matching_type))
215             return true;
216     }
217     return false;
218 }
219
220 lldb::TypeFormatImplSP
221 TypeCategoryMap::GetFormat (ValueObject& valobj,
222                             lldb::DynamicValueType use_dynamic)
223 {
224     Mutex::Locker locker(m_map_mutex);
225     
226     uint32_t reason_why;
227     ActiveCategoriesIterator begin, end = m_active_categories.end();
228     
229     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
230     
231     FormattersMatchVector matches = FormatManager::GetPossibleMatches(valobj, use_dynamic);
232     
233     for (begin = m_active_categories.begin(); begin != end; begin++)
234     {
235         lldb::TypeCategoryImplSP category_sp = *begin;
236         lldb::TypeFormatImplSP current_format;
237         if (log)
238             log->Printf("\n[TypeCategoryMap::GetFormat] Trying to use category %s", category_sp->GetName());
239         if (!category_sp->Get(valobj, matches, current_format, &reason_why))
240             continue;
241         return current_format;
242     }
243     if (log)
244         log->Printf("[TypeCategoryMap::GetFormat] nothing found - returning empty SP");
245     return lldb::TypeFormatImplSP();
246 }
247
248 lldb::TypeSummaryImplSP
249 TypeCategoryMap::GetSummaryFormat (ValueObject& valobj,
250                                    lldb::DynamicValueType use_dynamic)
251 {
252     Mutex::Locker locker(m_map_mutex);
253     
254     uint32_t reason_why;
255     ActiveCategoriesIterator begin, end = m_active_categories.end();
256     
257     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
258     
259     FormattersMatchVector matches = FormatManager::GetPossibleMatches(valobj, use_dynamic);
260     
261     for (begin = m_active_categories.begin(); begin != end; begin++)
262     {
263         lldb::TypeCategoryImplSP category_sp = *begin;
264         lldb::TypeSummaryImplSP current_format;
265         if (log)
266             log->Printf("\n[CategoryMap::GetSummaryFormat] Trying to use category %s", category_sp->GetName());
267         if (!category_sp->Get(valobj, matches, current_format, &reason_why))
268             continue;
269         return current_format;
270     }
271     if (log)
272         log->Printf("[CategoryMap::GetSummaryFormat] nothing found - returning empty SP");
273     return lldb::TypeSummaryImplSP();
274 }
275
276 #ifndef LLDB_DISABLE_PYTHON
277 lldb::SyntheticChildrenSP
278 TypeCategoryMap::GetSyntheticChildren (ValueObject& valobj,
279                                        lldb::DynamicValueType use_dynamic)
280 {
281     Mutex::Locker locker(m_map_mutex);
282     
283     uint32_t reason_why;
284     
285     ActiveCategoriesIterator begin, end = m_active_categories.end();
286     
287     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
288     
289     FormattersMatchVector matches = FormatManager::GetPossibleMatches(valobj, use_dynamic);
290     
291     for (begin = m_active_categories.begin(); begin != end; begin++)
292     {
293         lldb::TypeCategoryImplSP category_sp = *begin;
294         lldb::SyntheticChildrenSP current_format;
295         if (log)
296             log->Printf("\n[CategoryMap::GetSyntheticChildren] Trying to use category %s", category_sp->GetName());
297         if (!category_sp->Get(valobj, matches, current_format, &reason_why))
298             continue;
299         return current_format;
300     }
301     if (log)
302         log->Printf("[CategoryMap::GetSyntheticChildren] nothing found - returning empty SP");
303     return lldb::SyntheticChildrenSP();
304 }
305 #endif
306
307 lldb::TypeValidatorImplSP
308 TypeCategoryMap::GetValidator (ValueObject& valobj,
309                                lldb::DynamicValueType use_dynamic)
310 {
311     Mutex::Locker locker(m_map_mutex);
312     
313     uint32_t reason_why;
314     ActiveCategoriesIterator begin, end = m_active_categories.end();
315     
316     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
317     
318     FormattersMatchVector matches = FormatManager::GetPossibleMatches(valobj, use_dynamic);
319     
320     for (begin = m_active_categories.begin(); begin != end; begin++)
321     {
322         lldb::TypeCategoryImplSP category_sp = *begin;
323         lldb::TypeValidatorImplSP current_format;
324         if (log)
325             log->Printf("\n[CategoryMap::GetValidator] Trying to use category %s", category_sp->GetName());
326         if (!category_sp->Get(valobj, matches, current_format, &reason_why))
327             continue;
328         return current_format;
329     }
330     if (log)
331         log->Printf("[CategoryMap::GetValidator] nothing found - returning empty SP");
332     return lldb::TypeValidatorImplSP();
333 }
334
335 void
336 TypeCategoryMap::LoopThrough(CallbackType callback, void* param)
337 {
338     if (callback)
339     {
340         Mutex::Locker locker(m_map_mutex);
341         
342         // loop through enabled categories in respective order
343         {
344             ActiveCategoriesIterator begin, end = m_active_categories.end();
345             for (begin = m_active_categories.begin(); begin != end; begin++)
346             {
347                 lldb::TypeCategoryImplSP category = *begin;
348                 ConstString type = ConstString(category->GetName());
349                 if (!callback(param, category))
350                     break;
351             }
352         }
353         
354         // loop through disabled categories in just any order
355         {
356             MapIterator pos, end = m_map.end();
357             for (pos = m_map.begin(); pos != end; pos++)
358             {
359                 if (pos->second->IsEnabled())
360                     continue;
361                 KeyType type = pos->first;
362                 if (!callback(param, pos->second))
363                     break;
364             }
365         }
366     }
367 }
368
369 TypeCategoryImplSP
370 TypeCategoryMap::GetAtIndex (uint32_t index)
371 {
372     Mutex::Locker locker(m_map_mutex);
373     
374     if (index < m_map.size())
375     {
376         MapIterator pos, end = m_map.end();
377         for (pos = m_map.begin(); pos != end; pos++)
378         {
379             if (index == 0)
380                 return pos->second;
381             index--;
382         }
383     }
384     
385     return TypeCategoryImplSP();
386 }