]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/source/DataFormatters/TypeCategoryMap.cpp
Upgrade to OpenSSH 7.2p2.
[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/Core/Log.h"
13 #include "lldb/DataFormatters/FormatClasses.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 (FormattersMatchData& match_data)
222 {
223     Mutex::Locker locker(m_map_mutex);
224     
225     uint32_t reason_why;
226     ActiveCategoriesIterator begin, end = m_active_categories.end();
227     
228     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_DATAFORMATTERS));
229     
230     if (log)
231     {
232         for (auto match : match_data.GetMatchesVector())
233         {
234             log->Printf("[CategoryMap::GetSummaryFormat] candidate match = %s %s %s %s reason = %" PRIu32,
235                         match.GetTypeName().GetCString(),
236                         match.DidStripPointer() ? "strip-pointers" : "no-strip-pointers",
237                         match.DidStripReference() ? "strip-reference" : "no-strip-reference",
238                         match.DidStripTypedef() ? "strip-typedef" : "no-strip-typedef",
239                         match.GetReason());
240         }
241     }
242
243     for (begin = m_active_categories.begin(); begin != end; begin++)
244     {
245         lldb::TypeCategoryImplSP category_sp = *begin;
246         lldb::TypeFormatImplSP current_format;
247         if (log)
248             log->Printf("[TypeCategoryMap::GetFormat] Trying to use category %s", category_sp->GetName());
249         if (!category_sp->Get(match_data.GetValueObject(), match_data.GetMatchesVector(), current_format, &reason_why))
250             continue;
251         return current_format;
252     }
253     if (log)
254         log->Printf("[TypeCategoryMap::GetFormat] nothing found - returning empty SP");
255     return lldb::TypeFormatImplSP();
256 }
257
258 lldb::TypeSummaryImplSP
259 TypeCategoryMap::GetSummaryFormat (FormattersMatchData& match_data)
260 {
261     Mutex::Locker locker(m_map_mutex);
262     
263     uint32_t reason_why;
264     ActiveCategoriesIterator begin, end = m_active_categories.end();
265     
266     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_DATAFORMATTERS));
267     
268     if (log)
269     {
270         for (auto match : match_data.GetMatchesVector())
271         {
272             log->Printf("[CategoryMap::GetSummaryFormat] candidate match = %s %s %s %s reason = %" PRIu32,
273                         match.GetTypeName().GetCString(),
274                         match.DidStripPointer() ? "strip-pointers" : "no-strip-pointers",
275                         match.DidStripReference() ? "strip-reference" : "no-strip-reference",
276                         match.DidStripTypedef() ? "strip-typedef" : "no-strip-typedef",
277                         match.GetReason());
278         }
279     }
280
281     for (begin = m_active_categories.begin(); begin != end; begin++)
282     {
283         lldb::TypeCategoryImplSP category_sp = *begin;
284         lldb::TypeSummaryImplSP current_format;
285         if (log)
286             log->Printf("[CategoryMap::GetSummaryFormat] Trying to use category %s", category_sp->GetName());
287         if (!category_sp->Get(match_data.GetValueObject(), match_data.GetMatchesVector(), current_format, &reason_why))
288             continue;
289         return current_format;
290     }
291     if (log)
292         log->Printf("[CategoryMap::GetSummaryFormat] nothing found - returning empty SP");
293     return lldb::TypeSummaryImplSP();
294 }
295
296 #ifndef LLDB_DISABLE_PYTHON
297 lldb::SyntheticChildrenSP
298 TypeCategoryMap::GetSyntheticChildren (FormattersMatchData& match_data)
299 {
300     Mutex::Locker locker(m_map_mutex);
301     
302     uint32_t reason_why;
303     
304     ActiveCategoriesIterator begin, end = m_active_categories.end();
305     
306     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_DATAFORMATTERS));
307     
308     if (log)
309     {
310         for (auto match : match_data.GetMatchesVector())
311         {
312             log->Printf("[CategoryMap::GetSummaryFormat] candidate match = %s %s %s %s reason = %" PRIu32,
313                         match.GetTypeName().GetCString(),
314                         match.DidStripPointer() ? "strip-pointers" : "no-strip-pointers",
315                         match.DidStripReference() ? "strip-reference" : "no-strip-reference",
316                         match.DidStripTypedef() ? "strip-typedef" : "no-strip-typedef",
317                         match.GetReason());
318         }
319     }
320
321     for (begin = m_active_categories.begin(); begin != end; begin++)
322     {
323         lldb::TypeCategoryImplSP category_sp = *begin;
324         lldb::SyntheticChildrenSP current_format;
325         if (log)
326             log->Printf("[CategoryMap::GetSyntheticChildren] Trying to use category %s", category_sp->GetName());
327         if (!category_sp->Get(match_data.GetValueObject(), match_data.GetMatchesVector(), current_format, &reason_why))
328             continue;
329         return current_format;
330     }
331     if (log)
332         log->Printf("[CategoryMap::GetSyntheticChildren] nothing found - returning empty SP");
333     return lldb::SyntheticChildrenSP();
334 }
335 #endif
336
337 lldb::TypeValidatorImplSP
338 TypeCategoryMap::GetValidator (FormattersMatchData& match_data)
339 {
340     Mutex::Locker locker(m_map_mutex);
341     
342     uint32_t reason_why;
343     ActiveCategoriesIterator begin, end = m_active_categories.end();
344     
345     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_DATAFORMATTERS));
346     
347     if (log)
348     {
349         for (auto match : match_data.GetMatchesVector())
350         {
351             log->Printf("[CategoryMap::GetValidator] candidate match = %s %s %s %s reason = %" PRIu32,
352                         match.GetTypeName().GetCString(),
353                         match.DidStripPointer() ? "strip-pointers" : "no-strip-pointers",
354                         match.DidStripReference() ? "strip-reference" : "no-strip-reference",
355                         match.DidStripTypedef() ? "strip-typedef" : "no-strip-typedef",
356                         match.GetReason());
357         }
358     }
359
360     for (begin = m_active_categories.begin(); begin != end; begin++)
361     {
362         lldb::TypeCategoryImplSP category_sp = *begin;
363         lldb::TypeValidatorImplSP current_format;
364         if (log)
365             log->Printf("[CategoryMap::GetValidator] Trying to use category %s", category_sp->GetName());
366         if (!category_sp->Get(match_data.GetValueObject(), match_data.GetMatchesVector(), current_format, &reason_why))
367             continue;
368         return current_format;
369     }
370     if (log)
371         log->Printf("[CategoryMap::GetValidator] nothing found - returning empty SP");
372     return lldb::TypeValidatorImplSP();
373 }
374
375 void
376 TypeCategoryMap::ForEach(ForEachCallback callback)
377 {
378     if (callback)
379     {
380         Mutex::Locker locker(m_map_mutex);
381         
382         // loop through enabled categories in respective order
383         {
384             ActiveCategoriesIterator begin, end = m_active_categories.end();
385             for (begin = m_active_categories.begin(); begin != end; begin++)
386             {
387                 lldb::TypeCategoryImplSP category = *begin;
388                 if (!callback(category))
389                     break;
390             }
391         }
392         
393         // loop through disabled categories in just any order
394         {
395             MapIterator pos, end = m_map.end();
396             for (pos = m_map.begin(); pos != end; pos++)
397             {
398                 if (pos->second->IsEnabled())
399                     continue;
400                 KeyType type = pos->first;
401                 if (!callback(pos->second))
402                     break;
403             }
404         }
405     }
406 }
407
408 TypeCategoryImplSP
409 TypeCategoryMap::GetAtIndex (uint32_t index)
410 {
411     Mutex::Locker locker(m_map_mutex);
412     
413     if (index < m_map.size())
414     {
415         MapIterator pos, end = m_map.end();
416         for (pos = m_map.begin(); pos != end; pos++)
417         {
418             if (index == 0)
419                 return pos->second;
420             index--;
421         }
422     }
423     
424     return TypeCategoryImplSP();
425 }