]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - contrib/llvm/tools/lldb/source/DataFormatters/TypeCategory.cpp
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.git] / contrib / llvm / tools / lldb / source / DataFormatters / TypeCategory.cpp
1 //===-- TypeCategory.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/lldb-python.h"
11
12 #include "lldb/DataFormatters/TypeCategory.h"
13
14 // C Includes
15 // C++ Includes
16 // Other libraries and framework includes
17 // Project includes
18
19 using namespace lldb;
20 using namespace lldb_private;
21
22 TypeCategoryImpl::TypeCategoryImpl(IFormatChangeListener* clist,
23                                    ConstString name) :
24 m_format_cont("format","regex-format",clist),
25 m_summary_cont("summary","regex-summary",clist),
26 m_filter_cont("filter","regex-filter",clist),
27 #ifndef LLDB_DISABLE_PYTHON
28 m_synth_cont("synth","regex-synth",clist),
29 #endif
30 m_enabled(false),
31 m_change_listener(clist),
32 m_mutex(Mutex::eMutexTypeRecursive),
33 m_name(name)
34 {}
35
36 bool
37 TypeCategoryImpl::Get (ValueObject& valobj,
38                        const FormattersMatchVector& candidates,
39                        lldb::TypeFormatImplSP& entry,
40                        uint32_t* reason)
41 {
42     if (!IsEnabled())
43         return false;
44     if (GetTypeFormatsContainer()->Get(candidates, entry, reason))
45         return true;
46     bool regex = GetRegexTypeFormatsContainer()->Get(candidates, entry, reason);
47     if (regex && reason)
48         *reason |= lldb_private::eFormatterChoiceCriterionRegularExpressionSummary;
49     return regex;
50 }
51
52 bool
53 TypeCategoryImpl::Get (ValueObject& valobj,
54                        const FormattersMatchVector& candidates,
55                        lldb::TypeSummaryImplSP& entry,
56                        uint32_t* reason)
57 {
58     if (!IsEnabled())
59         return false;
60     if (GetTypeSummariesContainer()->Get(candidates, entry, reason))
61         return true;
62     bool regex = GetRegexTypeSummariesContainer()->Get(candidates, entry, reason);
63     if (regex && reason)
64         *reason |= lldb_private::eFormatterChoiceCriterionRegularExpressionSummary;
65     return regex;
66 }
67
68 bool
69 TypeCategoryImpl::Get (ValueObject& valobj,
70                        const FormattersMatchVector& candidates,
71                        lldb::SyntheticChildrenSP& entry,
72                        uint32_t* reason)
73 {
74     if (!IsEnabled())
75         return false;
76     TypeFilterImpl::SharedPointer filter_sp;
77     uint32_t reason_filter = 0;
78     bool regex_filter = false;
79     // first find both Filter and Synth, and then check which is most recent
80     
81     if (!GetTypeFiltersContainer()->Get(candidates, filter_sp, &reason_filter))
82         regex_filter = GetRegexTypeFiltersContainer()->Get (candidates, filter_sp, &reason_filter);
83     
84 #ifndef LLDB_DISABLE_PYTHON
85     bool regex_synth = false;
86     uint32_t reason_synth = 0;
87     bool pick_synth = false;
88     ScriptedSyntheticChildren::SharedPointer synth;
89     if (!GetTypeSyntheticsContainer()->Get(candidates, synth, &reason_synth))
90         regex_synth = GetRegexTypeSyntheticsContainer()->Get (candidates, synth, &reason_synth);
91     if (!filter_sp.get() && !synth.get())
92         return false;
93     else if (!filter_sp.get() && synth.get())
94         pick_synth = true;
95     
96     else if (filter_sp.get() && !synth.get())
97         pick_synth = false;
98     
99     else /*if (filter_sp.get() && synth.get())*/
100     {
101         if (filter_sp->GetRevision() > synth->GetRevision())
102             pick_synth = false;
103         else
104             pick_synth = true;
105     }
106     if (pick_synth)
107     {
108         if (regex_synth && reason)
109             *reason |= lldb_private::eFormatterChoiceCriterionRegularExpressionFilter;
110         entry = synth;
111         return true;
112     }
113     else
114     {
115         if (regex_filter && reason)
116             *reason |= lldb_private::eFormatterChoiceCriterionRegularExpressionFilter;
117         entry = filter_sp;
118         return true;
119     }
120     
121 #else
122     if (filter_sp)
123     {
124         entry = filter_sp;
125         return true;
126     }
127 #endif
128     
129     return false;
130 }
131
132 void
133 TypeCategoryImpl::Clear (FormatCategoryItems items)
134 {
135     if ( (items & eFormatCategoryItemValue)  == eFormatCategoryItemValue )
136         GetTypeFormatsContainer()->Clear();
137     if ( (items & eFormatCategoryItemRegexValue) == eFormatCategoryItemRegexValue )
138         GetRegexTypeFormatsContainer()->Clear();
139
140     if ( (items & eFormatCategoryItemSummary) == eFormatCategoryItemSummary )
141         GetTypeSummariesContainer()->Clear();
142     if ( (items & eFormatCategoryItemRegexSummary) == eFormatCategoryItemRegexSummary )
143         GetRegexTypeSummariesContainer()->Clear();
144
145     if ( (items & eFormatCategoryItemFilter)  == eFormatCategoryItemFilter )
146         GetTypeFiltersContainer()->Clear();
147     if ( (items & eFormatCategoryItemRegexFilter) == eFormatCategoryItemRegexFilter )
148         GetRegexTypeFiltersContainer()->Clear();
149
150 #ifndef LLDB_DISABLE_PYTHON
151     if ( (items & eFormatCategoryItemSynth)  == eFormatCategoryItemSynth )
152         GetTypeSyntheticsContainer()->Clear();
153     if ( (items & eFormatCategoryItemRegexSynth) == eFormatCategoryItemRegexSynth )
154         GetRegexTypeSyntheticsContainer()->Clear();
155 #endif
156 }
157
158 bool
159 TypeCategoryImpl::Delete (ConstString name,
160                           FormatCategoryItems items)
161 {
162     bool success = false;
163     
164     if ( (items & eFormatCategoryItemValue)  == eFormatCategoryItemValue )
165         success = GetTypeFormatsContainer()->Delete(name) || success;
166     if ( (items & eFormatCategoryItemRegexValue) == eFormatCategoryItemRegexValue )
167         success = GetRegexTypeFormatsContainer()->Delete(name) || success;
168
169     if ( (items & eFormatCategoryItemSummary) == eFormatCategoryItemSummary )
170         success = GetTypeSummariesContainer()->Delete(name) || success;
171     if ( (items & eFormatCategoryItemRegexSummary) == eFormatCategoryItemRegexSummary )
172         success = GetRegexTypeSummariesContainer()->Delete(name) || success;
173
174     if ( (items & eFormatCategoryItemFilter)  == eFormatCategoryItemFilter )
175         success = GetTypeFiltersContainer()->Delete(name) || success;
176     if ( (items & eFormatCategoryItemRegexFilter) == eFormatCategoryItemRegexFilter )
177         success = GetRegexTypeFiltersContainer()->Delete(name) || success;
178
179 #ifndef LLDB_DISABLE_PYTHON
180     if ( (items & eFormatCategoryItemSynth)  == eFormatCategoryItemSynth )
181         success = GetTypeSyntheticsContainer()->Delete(name) || success;
182     if ( (items & eFormatCategoryItemRegexSynth) == eFormatCategoryItemRegexSynth )
183         success = GetRegexTypeSyntheticsContainer()->Delete(name) || success;
184 #endif
185     return success;
186 }
187
188 uint32_t
189 TypeCategoryImpl::GetCount (FormatCategoryItems items)
190 {
191     uint32_t count = 0;
192
193     if ( (items & eFormatCategoryItemValue) == eFormatCategoryItemValue )
194         count += GetTypeFormatsContainer()->GetCount();
195     if ( (items & eFormatCategoryItemRegexValue) == eFormatCategoryItemRegexValue )
196         count += GetRegexTypeFormatsContainer()->GetCount();
197     
198     if ( (items & eFormatCategoryItemSummary) == eFormatCategoryItemSummary )
199         count += GetTypeSummariesContainer()->GetCount();
200     if ( (items & eFormatCategoryItemRegexSummary) == eFormatCategoryItemRegexSummary )
201         count += GetRegexTypeSummariesContainer()->GetCount();
202
203     if ( (items & eFormatCategoryItemFilter)  == eFormatCategoryItemFilter )
204         count += GetTypeFiltersContainer()->GetCount();
205     if ( (items & eFormatCategoryItemRegexFilter) == eFormatCategoryItemRegexFilter )
206         count += GetRegexTypeFiltersContainer()->GetCount();
207
208 #ifndef LLDB_DISABLE_PYTHON
209     if ( (items & eFormatCategoryItemSynth)  == eFormatCategoryItemSynth )
210         count += GetTypeSyntheticsContainer()->GetCount();
211     if ( (items & eFormatCategoryItemRegexSynth) == eFormatCategoryItemRegexSynth )
212         count += GetRegexTypeSyntheticsContainer()->GetCount();
213 #endif
214     return count;
215 }
216
217 bool
218 TypeCategoryImpl::AnyMatches(ConstString type_name,
219                              FormatCategoryItems items,
220                              bool only_enabled,
221                              const char** matching_category,
222                              FormatCategoryItems* matching_type)
223 {
224     if (!IsEnabled() && only_enabled)
225         return false;
226     
227     lldb::TypeFormatImplSP format_sp;
228     lldb::TypeSummaryImplSP summary_sp;
229     TypeFilterImpl::SharedPointer filter_sp;
230 #ifndef LLDB_DISABLE_PYTHON
231     ScriptedSyntheticChildren::SharedPointer synth_sp;
232 #endif
233     
234     if ( (items & eFormatCategoryItemValue) == eFormatCategoryItemValue )
235     {
236         if (GetTypeFormatsContainer()->Get(type_name, format_sp))
237         {
238             if (matching_category)
239                 *matching_category = m_name.GetCString();
240             if (matching_type)
241                 *matching_type = eFormatCategoryItemValue;
242             return true;
243         }
244     }
245     if ( (items & eFormatCategoryItemRegexValue) == eFormatCategoryItemRegexValue )
246     {
247         if (GetRegexTypeFormatsContainer()->Get(type_name, format_sp))
248         {
249             if (matching_category)
250                 *matching_category = m_name.GetCString();
251             if (matching_type)
252                 *matching_type = eFormatCategoryItemRegexValue;
253             return true;
254         }
255     }
256     
257     if ( (items & eFormatCategoryItemSummary) == eFormatCategoryItemSummary )
258     {
259         if (GetTypeSummariesContainer()->Get(type_name, summary_sp))
260         {
261             if (matching_category)
262                 *matching_category = m_name.GetCString();
263             if (matching_type)
264                 *matching_type = eFormatCategoryItemSummary;
265             return true;
266         }
267     }
268     if ( (items & eFormatCategoryItemRegexSummary) == eFormatCategoryItemRegexSummary )
269     {
270         if (GetRegexTypeSummariesContainer()->Get(type_name, summary_sp))
271         {
272             if (matching_category)
273                 *matching_category = m_name.GetCString();
274             if (matching_type)
275                 *matching_type = eFormatCategoryItemRegexSummary;
276             return true;
277         }
278     }
279     
280     if ( (items & eFormatCategoryItemFilter)  == eFormatCategoryItemFilter )
281     {
282         if (GetTypeFiltersContainer()->Get(type_name, filter_sp))
283         {
284             if (matching_category)
285                 *matching_category = m_name.GetCString();
286             if (matching_type)
287                 *matching_type = eFormatCategoryItemFilter;
288             return true;
289         }
290     }
291     if ( (items & eFormatCategoryItemRegexFilter) == eFormatCategoryItemRegexFilter )
292     {
293         if (GetRegexTypeFiltersContainer()->Get(type_name, filter_sp))
294         {
295             if (matching_category)
296                 *matching_category = m_name.GetCString();
297             if (matching_type)
298                 *matching_type = eFormatCategoryItemRegexFilter;
299             return true;
300         }
301     }
302     
303 #ifndef LLDB_DISABLE_PYTHON
304     if ( (items & eFormatCategoryItemSynth)  == eFormatCategoryItemSynth )
305     {
306         if (GetTypeSyntheticsContainer()->Get(type_name, synth_sp))
307         {
308             if (matching_category)
309                 *matching_category = m_name.GetCString();
310             if (matching_type)
311                 *matching_type = eFormatCategoryItemSynth;
312             return true;
313         }
314     }
315     if ( (items & eFormatCategoryItemRegexSynth) == eFormatCategoryItemRegexSynth )
316     {
317         if (GetRegexTypeSyntheticsContainer()->Get(type_name, synth_sp))
318         {
319             if (matching_category)
320                 *matching_category = m_name.GetCString();
321             if (matching_type)
322                 *matching_type = eFormatCategoryItemRegexSynth;
323             return true;
324         }
325     }
326 #endif
327     return false;
328 }
329
330 TypeCategoryImpl::FormatContainer::MapValueType
331 TypeCategoryImpl::GetFormatForType (lldb::TypeNameSpecifierImplSP type_sp)
332 {
333     FormatContainer::MapValueType retval;
334     
335     if (type_sp)
336     {
337         if (type_sp->IsRegex())
338             GetRegexTypeFormatsContainer()->GetExact(ConstString(type_sp->GetName()),retval);
339         else
340             GetTypeFormatsContainer()->GetExact(ConstString(type_sp->GetName()),retval);
341     }
342     
343     return retval;
344 }
345
346 TypeCategoryImpl::SummaryContainer::MapValueType
347 TypeCategoryImpl::GetSummaryForType (lldb::TypeNameSpecifierImplSP type_sp)
348 {
349     SummaryContainer::MapValueType retval;
350     
351     if (type_sp)
352     {
353         if (type_sp->IsRegex())
354             GetRegexTypeSummariesContainer()->GetExact(ConstString(type_sp->GetName()),retval);
355         else
356             GetTypeSummariesContainer()->GetExact(ConstString(type_sp->GetName()),retval);
357     }
358     
359     return retval;
360 }
361
362 TypeCategoryImpl::FilterContainer::MapValueType
363 TypeCategoryImpl::GetFilterForType (lldb::TypeNameSpecifierImplSP type_sp)
364 {
365     FilterContainer::MapValueType retval;
366     
367     if (type_sp)
368     {
369         if (type_sp->IsRegex())
370             GetRegexTypeFiltersContainer()->GetExact(ConstString(type_sp->GetName()),retval);
371         else
372             GetTypeFiltersContainer()->GetExact(ConstString(type_sp->GetName()),retval);
373     }
374     
375     return retval;
376 }
377
378 #ifndef LLDB_DISABLE_PYTHON
379 TypeCategoryImpl::SynthContainer::MapValueType
380 TypeCategoryImpl::GetSyntheticForType (lldb::TypeNameSpecifierImplSP type_sp)
381 {
382     SynthContainer::MapValueType retval;
383     
384     if (type_sp)
385     {
386         if (type_sp->IsRegex())
387             GetRegexTypeSyntheticsContainer()->GetExact(ConstString(type_sp->GetName()),retval);
388         else
389             GetTypeSyntheticsContainer()->GetExact(ConstString(type_sp->GetName()),retval);
390     }
391     
392     return retval;
393 }
394 #endif
395
396 lldb::TypeNameSpecifierImplSP
397 TypeCategoryImpl::GetTypeNameSpecifierForSummaryAtIndex (size_t index)
398 {
399     if (index < GetTypeSummariesContainer()->GetCount())
400         return GetTypeSummariesContainer()->GetTypeNameSpecifierAtIndex(index);
401     else
402         return GetRegexTypeSummariesContainer()->GetTypeNameSpecifierAtIndex(index-GetTypeSummariesContainer()->GetCount());
403 }
404
405 TypeCategoryImpl::FormatContainer::MapValueType
406 TypeCategoryImpl::GetFormatAtIndex (size_t index)
407 {
408     if (index < GetTypeFormatsContainer()->GetCount())
409         return GetTypeFormatsContainer()->GetAtIndex(index);
410     else
411         return GetRegexTypeFormatsContainer()->GetAtIndex(index-GetTypeFormatsContainer()->GetCount());
412 }
413
414 TypeCategoryImpl::SummaryContainer::MapValueType
415 TypeCategoryImpl::GetSummaryAtIndex (size_t index)
416 {
417     if (index < GetTypeSummariesContainer()->GetCount())
418         return GetTypeSummariesContainer()->GetAtIndex(index);
419     else
420         return GetRegexTypeSummariesContainer()->GetAtIndex(index-GetTypeSummariesContainer()->GetCount());
421 }
422
423 TypeCategoryImpl::FilterContainer::MapValueType
424 TypeCategoryImpl::GetFilterAtIndex (size_t index)
425 {
426     if (index < GetTypeFiltersContainer()->GetCount())
427         return GetTypeFiltersContainer()->GetAtIndex(index);
428     else
429         return GetRegexTypeFiltersContainer()->GetAtIndex(index-GetTypeFiltersContainer()->GetCount());
430 }
431
432 lldb::TypeNameSpecifierImplSP
433 TypeCategoryImpl::GetTypeNameSpecifierForFormatAtIndex (size_t index)
434 {
435     if (index < GetTypeFormatsContainer()->GetCount())
436         return GetTypeFormatsContainer()->GetTypeNameSpecifierAtIndex(index);
437     else
438         return GetRegexTypeFormatsContainer()->GetTypeNameSpecifierAtIndex(index-GetTypeFormatsContainer()->GetCount());
439 }
440
441 lldb::TypeNameSpecifierImplSP
442 TypeCategoryImpl::GetTypeNameSpecifierForFilterAtIndex (size_t index)
443 {
444     if (index < GetTypeFiltersContainer()->GetCount())
445         return GetTypeFiltersContainer()->GetTypeNameSpecifierAtIndex(index);
446     else
447         return GetRegexTypeFiltersContainer()->GetTypeNameSpecifierAtIndex(index-GetTypeFiltersContainer()->GetCount());
448 }
449
450 #ifndef LLDB_DISABLE_PYTHON
451 TypeCategoryImpl::SynthContainer::MapValueType
452 TypeCategoryImpl::GetSyntheticAtIndex (size_t index)
453 {
454     if (index < GetTypeSyntheticsContainer()->GetCount())
455         return GetTypeSyntheticsContainer()->GetAtIndex(index);
456     else
457         return GetRegexTypeSyntheticsContainer()->GetAtIndex(index-GetTypeSyntheticsContainer()->GetCount());
458 }
459
460 lldb::TypeNameSpecifierImplSP
461 TypeCategoryImpl::GetTypeNameSpecifierForSyntheticAtIndex (size_t index)
462 {
463     if (index < GetTypeSyntheticsContainer()->GetCount())
464         return GetTypeSyntheticsContainer()->GetTypeNameSpecifierAtIndex(index);
465     else
466         return GetRegexTypeSyntheticsContainer()->GetTypeNameSpecifierAtIndex(index - GetTypeSyntheticsContainer()->GetCount());
467 }
468 #endif
469
470 void
471 TypeCategoryImpl::Enable (bool value, uint32_t position)
472 {
473     Mutex::Locker locker(m_mutex);
474     m_enabled = value;
475     m_enabled_position = position;
476     if (m_change_listener)
477         m_change_listener->Changed();
478 }