]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/source/DataFormatters/TypeCategoryMap.cpp
Merge ^/head r305623 through r305686.
[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(), listener(lst), m_map(), m_active_categories()
25 {
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);
30 }
31
32 void
33 TypeCategoryMap::Add (KeyType name, const ValueSP& entry)
34 {
35     std::lock_guard<std::recursive_mutex> guard(m_map_mutex);
36     m_map[name] = entry;
37     if (listener)
38         listener->Changed();
39 }
40
41 bool
42 TypeCategoryMap::Delete (KeyType name)
43 {
44     std::lock_guard<std::recursive_mutex> guard(m_map_mutex);
45     MapIterator iter = m_map.find(name);
46     if (iter == m_map.end())
47         return false;
48     m_map.erase(name);
49     Disable(name);
50     if (listener)
51         listener->Changed();
52     return true;
53 }
54
55 bool
56 TypeCategoryMap::Enable (KeyType category_name, Position pos)
57 {
58     std::lock_guard<std::recursive_mutex> guard(m_map_mutex);
59     ValueSP category;
60     if (!Get(category_name,category))
61         return false;
62     return Enable(category, pos);
63 }
64
65 bool
66 TypeCategoryMap::Disable (KeyType category_name)
67 {
68     std::lock_guard<std::recursive_mutex> guard(m_map_mutex);
69     ValueSP category;
70     if (!Get(category_name,category))
71         return false;
72     return Disable(category);
73 }
74
75 bool
76 TypeCategoryMap::Enable (ValueSP category, Position pos)
77 {
78     std::lock_guard<std::recursive_mutex> guard(m_map_mutex);
79     if (category.get())
80     {
81         Position pos_w = pos;
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())
87         {
88             ActiveCategoriesList::iterator iter = m_active_categories.begin();
89             while (pos_w)
90             {
91                 pos_w--,iter++;
92             }
93             m_active_categories.insert(iter,category);
94         }
95         else
96             return false;
97         category->Enable(true,
98                          pos);
99         return true;
100     }
101     return false;
102 }
103
104 bool
105 TypeCategoryMap::Disable (ValueSP category)
106 {
107     std::lock_guard<std::recursive_mutex> guard(m_map_mutex);
108     if (category.get())
109     {
110         m_active_categories.remove_if(delete_matching_categories(category));
111         category->Disable();
112         return true;
113     }
114     return false;
115 }
116
117 void
118 TypeCategoryMap::EnableAllCategories ()
119 {
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)
124     {
125         if (iter->second->IsEnabled())
126             continue;
127         auto pos = iter->second->GetLastEnabledPosition();
128         if (pos >= sorted_categories.size())
129         {
130             auto iter = std::find_if(sorted_categories.begin(),
131                                      sorted_categories.end(),
132                                      [] (const ValueSP& sp) -> bool {
133                                          return sp.get() == nullptr;
134                                      });
135             pos = std::distance(sorted_categories.begin(), iter);
136         }
137         sorted_categories.at(pos) = iter->second;
138     }
139     decltype(sorted_categories)::iterator viter = sorted_categories.begin(), vend = sorted_categories.end();
140     for (; viter != vend; viter++)
141         if (viter->get())
142             Enable(*viter, Last);
143 }
144
145 void
146 TypeCategoryMap::DisableAllCategories ()
147 {
148     std::lock_guard<std::recursive_mutex> guard(m_map_mutex);
149     Position p = First;
150     for (; false == m_active_categories.empty(); p++)
151     {
152         m_active_categories.front()->SetEnabledPosition(p);
153         Disable(m_active_categories.front());
154     }
155 }
156
157 void
158 TypeCategoryMap::Clear ()
159 {
160     std::lock_guard<std::recursive_mutex> guard(m_map_mutex);
161     m_map.clear();
162     m_active_categories.clear();
163     if (listener)
164         listener->Changed();
165 }
166
167 bool
168 TypeCategoryMap::Get (KeyType name, ValueSP& entry)
169 {
170     std::lock_guard<std::recursive_mutex> guard(m_map_mutex);
171     MapIterator iter = m_map.find(name);
172     if (iter == m_map.end())
173         return false;
174     entry = iter->second;
175     return true;
176 }
177
178 bool
179 TypeCategoryMap::Get (uint32_t pos, ValueSP& entry)
180 {
181     std::lock_guard<std::recursive_mutex> guard(m_map_mutex);
182     MapIterator iter = m_map.begin();
183     MapIterator end = m_map.end();
184     while (pos > 0)
185     {
186         iter++;
187         pos--;
188         if (iter == end)
189             return false;
190     }
191     entry = iter->second;
192     return false;
193 }
194
195 bool
196 TypeCategoryMap::AnyMatches (ConstString type_name,
197                              TypeCategoryImpl::FormatCategoryItems items,
198                              bool only_enabled,
199                              const char** matching_category,
200                              TypeCategoryImpl::FormatCategoryItems* matching_type)
201 {
202     std::lock_guard<std::recursive_mutex> guard(m_map_mutex);
203
204     MapIterator pos, end = m_map.end();
205     for (pos = m_map.begin(); pos != end; pos++)
206     {
207         if (pos->second->AnyMatches(type_name,
208                                     items,
209                                     only_enabled,
210                                     matching_category,
211                                     matching_type))
212             return true;
213     }
214     return false;
215 }
216
217 lldb::TypeFormatImplSP
218 TypeCategoryMap::GetFormat (FormattersMatchData& match_data)
219 {
220     std::lock_guard<std::recursive_mutex> guard(m_map_mutex);
221
222     uint32_t reason_why;
223     ActiveCategoriesIterator begin, end = m_active_categories.end();
224     
225     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_DATAFORMATTERS));
226     
227     if (log)
228     {
229         for (auto match : match_data.GetMatchesVector())
230         {
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",
236                         match.GetReason());
237         }
238     }
239
240     for (begin = m_active_categories.begin(); begin != end; begin++)
241     {
242         lldb::TypeCategoryImplSP category_sp = *begin;
243         lldb::TypeFormatImplSP current_format;
244         if (log)
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))
247             continue;
248         return current_format;
249     }
250     if (log)
251         log->Printf("[TypeCategoryMap::GetFormat] nothing found - returning empty SP");
252     return lldb::TypeFormatImplSP();
253 }
254
255 lldb::TypeSummaryImplSP
256 TypeCategoryMap::GetSummaryFormat (FormattersMatchData& match_data)
257 {
258     std::lock_guard<std::recursive_mutex> guard(m_map_mutex);
259
260     uint32_t reason_why;
261     ActiveCategoriesIterator begin, end = m_active_categories.end();
262     
263     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_DATAFORMATTERS));
264     
265     if (log)
266     {
267         for (auto match : match_data.GetMatchesVector())
268         {
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",
274                         match.GetReason());
275         }
276     }
277
278     for (begin = m_active_categories.begin(); begin != end; begin++)
279     {
280         lldb::TypeCategoryImplSP category_sp = *begin;
281         lldb::TypeSummaryImplSP current_format;
282         if (log)
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))
285             continue;
286         return current_format;
287     }
288     if (log)
289         log->Printf("[CategoryMap::GetSummaryFormat] nothing found - returning empty SP");
290     return lldb::TypeSummaryImplSP();
291 }
292
293 #ifndef LLDB_DISABLE_PYTHON
294 lldb::SyntheticChildrenSP
295 TypeCategoryMap::GetSyntheticChildren (FormattersMatchData& match_data)
296 {
297     std::lock_guard<std::recursive_mutex> guard(m_map_mutex);
298
299     uint32_t reason_why;
300     
301     ActiveCategoriesIterator begin, end = m_active_categories.end();
302     
303     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_DATAFORMATTERS));
304     
305     if (log)
306     {
307         for (auto match : match_data.GetMatchesVector())
308         {
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",
314                         match.GetReason());
315         }
316     }
317
318     for (begin = m_active_categories.begin(); begin != end; begin++)
319     {
320         lldb::TypeCategoryImplSP category_sp = *begin;
321         lldb::SyntheticChildrenSP current_format;
322         if (log)
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))
325             continue;
326         return current_format;
327     }
328     if (log)
329         log->Printf("[CategoryMap::GetSyntheticChildren] nothing found - returning empty SP");
330     return lldb::SyntheticChildrenSP();
331 }
332 #endif
333
334 lldb::TypeValidatorImplSP
335 TypeCategoryMap::GetValidator (FormattersMatchData& match_data)
336 {
337     std::lock_guard<std::recursive_mutex> guard(m_map_mutex);
338
339     uint32_t reason_why;
340     ActiveCategoriesIterator begin, end = m_active_categories.end();
341     
342     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_DATAFORMATTERS));
343     
344     if (log)
345     {
346         for (auto match : match_data.GetMatchesVector())
347         {
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",
353                         match.GetReason());
354         }
355     }
356
357     for (begin = m_active_categories.begin(); begin != end; begin++)
358     {
359         lldb::TypeCategoryImplSP category_sp = *begin;
360         lldb::TypeValidatorImplSP current_format;
361         if (log)
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))
364             continue;
365         return current_format;
366     }
367     if (log)
368         log->Printf("[CategoryMap::GetValidator] nothing found - returning empty SP");
369     return lldb::TypeValidatorImplSP();
370 }
371
372 void
373 TypeCategoryMap::ForEach(ForEachCallback callback)
374 {
375     if (callback)
376     {
377         std::lock_guard<std::recursive_mutex> guard(m_map_mutex);
378
379         // loop through enabled categories in respective order
380         {
381             ActiveCategoriesIterator begin, end = m_active_categories.end();
382             for (begin = m_active_categories.begin(); begin != end; begin++)
383             {
384                 lldb::TypeCategoryImplSP category = *begin;
385                 if (!callback(category))
386                     break;
387             }
388         }
389         
390         // loop through disabled categories in just any order
391         {
392             MapIterator pos, end = m_map.end();
393             for (pos = m_map.begin(); pos != end; pos++)
394             {
395                 if (pos->second->IsEnabled())
396                     continue;
397                 KeyType type = pos->first;
398                 if (!callback(pos->second))
399                     break;
400             }
401         }
402     }
403 }
404
405 TypeCategoryImplSP
406 TypeCategoryMap::GetAtIndex (uint32_t index)
407 {
408     std::lock_guard<std::recursive_mutex> guard(m_map_mutex);
409
410     if (index < m_map.size())
411     {
412         MapIterator pos, end = m_map.end();
413         for (pos = m_map.begin(); pos != end; pos++)
414         {
415             if (index == 0)
416                 return pos->second;
417             index--;
418         }
419     }
420     
421     return TypeCategoryImplSP();
422 }