]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/source/DataFormatters/TypeCategory.cpp
Update compiler-rt to trunk r230183. This has some of our patches
[FreeBSD/FreeBSD.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_validator_cont("validator","regex-validator",clist),
31 m_enabled(false),
32 m_change_listener(clist),
33 m_mutex(Mutex::eMutexTypeRecursive),
34 m_name(name)
35 {}
36
37 bool
38 TypeCategoryImpl::Get (ValueObject& valobj,
39                        const FormattersMatchVector& candidates,
40                        lldb::TypeFormatImplSP& entry,
41                        uint32_t* reason)
42 {
43     if (!IsEnabled())
44         return false;
45     if (GetTypeFormatsContainer()->Get(candidates, entry, reason))
46         return true;
47     bool regex = GetRegexTypeFormatsContainer()->Get(candidates, entry, reason);
48     if (regex && reason)
49         *reason |= lldb_private::eFormatterChoiceCriterionRegularExpressionSummary;
50     return regex;
51 }
52
53 bool
54 TypeCategoryImpl::Get (ValueObject& valobj,
55                        const FormattersMatchVector& candidates,
56                        lldb::TypeSummaryImplSP& entry,
57                        uint32_t* reason)
58 {
59     if (!IsEnabled())
60         return false;
61     if (GetTypeSummariesContainer()->Get(candidates, entry, reason))
62         return true;
63     bool regex = GetRegexTypeSummariesContainer()->Get(candidates, entry, reason);
64     if (regex && reason)
65         *reason |= lldb_private::eFormatterChoiceCriterionRegularExpressionSummary;
66     return regex;
67 }
68
69 bool
70 TypeCategoryImpl::Get (ValueObject& valobj,
71                        const FormattersMatchVector& candidates,
72                        lldb::SyntheticChildrenSP& entry,
73                        uint32_t* reason)
74 {
75     if (!IsEnabled())
76         return false;
77     TypeFilterImpl::SharedPointer filter_sp;
78     uint32_t reason_filter = 0;
79     bool regex_filter = false;
80     // first find both Filter and Synth, and then check which is most recent
81     
82     if (!GetTypeFiltersContainer()->Get(candidates, filter_sp, &reason_filter))
83         regex_filter = GetRegexTypeFiltersContainer()->Get (candidates, filter_sp, &reason_filter);
84     
85 #ifndef LLDB_DISABLE_PYTHON
86     bool regex_synth = false;
87     uint32_t reason_synth = 0;
88     bool pick_synth = false;
89     ScriptedSyntheticChildren::SharedPointer synth;
90     if (!GetTypeSyntheticsContainer()->Get(candidates, synth, &reason_synth))
91         regex_synth = GetRegexTypeSyntheticsContainer()->Get (candidates, synth, &reason_synth);
92     if (!filter_sp.get() && !synth.get())
93         return false;
94     else if (!filter_sp.get() && synth.get())
95         pick_synth = true;
96     
97     else if (filter_sp.get() && !synth.get())
98         pick_synth = false;
99     
100     else /*if (filter_sp.get() && synth.get())*/
101     {
102         if (filter_sp->GetRevision() > synth->GetRevision())
103             pick_synth = false;
104         else
105             pick_synth = true;
106     }
107     if (pick_synth)
108     {
109         if (regex_synth && reason)
110             *reason |= lldb_private::eFormatterChoiceCriterionRegularExpressionFilter;
111         entry = synth;
112         return true;
113     }
114     else
115     {
116         if (regex_filter && reason)
117             *reason |= lldb_private::eFormatterChoiceCriterionRegularExpressionFilter;
118         entry = filter_sp;
119         return true;
120     }
121     
122 #else
123     if (filter_sp)
124     {
125         entry = filter_sp;
126         return true;
127     }
128 #endif
129     
130     return false;
131 }
132
133 bool
134 TypeCategoryImpl::Get (ValueObject& valobj,
135                        const FormattersMatchVector& candidates,
136                        lldb::TypeValidatorImplSP& entry,
137                        uint32_t* reason)
138 {
139     if (!IsEnabled())
140         return false;
141     if (GetTypeValidatorsContainer()->Get(candidates, entry, reason))
142         return true;
143     bool regex = GetRegexTypeValidatorsContainer()->Get(candidates, entry, reason);
144     if (regex && reason)
145         *reason |= lldb_private::eFormatterChoiceCriterionRegularExpressionSummary;
146     return regex;
147 }
148
149 void
150 TypeCategoryImpl::Clear (FormatCategoryItems items)
151 {
152     if ( (items & eFormatCategoryItemValue)  == eFormatCategoryItemValue )
153         GetTypeFormatsContainer()->Clear();
154     if ( (items & eFormatCategoryItemRegexValue) == eFormatCategoryItemRegexValue )
155         GetRegexTypeFormatsContainer()->Clear();
156
157     if ( (items & eFormatCategoryItemSummary) == eFormatCategoryItemSummary )
158         GetTypeSummariesContainer()->Clear();
159     if ( (items & eFormatCategoryItemRegexSummary) == eFormatCategoryItemRegexSummary )
160         GetRegexTypeSummariesContainer()->Clear();
161
162     if ( (items & eFormatCategoryItemFilter)  == eFormatCategoryItemFilter )
163         GetTypeFiltersContainer()->Clear();
164     if ( (items & eFormatCategoryItemRegexFilter) == eFormatCategoryItemRegexFilter )
165         GetRegexTypeFiltersContainer()->Clear();
166
167 #ifndef LLDB_DISABLE_PYTHON
168     if ( (items & eFormatCategoryItemSynth)  == eFormatCategoryItemSynth )
169         GetTypeSyntheticsContainer()->Clear();
170     if ( (items & eFormatCategoryItemRegexSynth) == eFormatCategoryItemRegexSynth )
171         GetRegexTypeSyntheticsContainer()->Clear();
172 #endif
173     
174     if ( (items & eFormatCategoryItemValidator)  == eFormatCategoryItemValidator )
175         GetTypeValidatorsContainer()->Clear();
176     if ( (items & eFormatCategoryItemRegexValidator) == eFormatCategoryItemRegexValidator )
177         GetRegexTypeValidatorsContainer()->Clear();
178 }
179
180 bool
181 TypeCategoryImpl::Delete (ConstString name,
182                           FormatCategoryItems items)
183 {
184     bool success = false;
185     
186     if ( (items & eFormatCategoryItemValue)  == eFormatCategoryItemValue )
187         success = GetTypeFormatsContainer()->Delete(name) || success;
188     if ( (items & eFormatCategoryItemRegexValue) == eFormatCategoryItemRegexValue )
189         success = GetRegexTypeFormatsContainer()->Delete(name) || success;
190
191     if ( (items & eFormatCategoryItemSummary) == eFormatCategoryItemSummary )
192         success = GetTypeSummariesContainer()->Delete(name) || success;
193     if ( (items & eFormatCategoryItemRegexSummary) == eFormatCategoryItemRegexSummary )
194         success = GetRegexTypeSummariesContainer()->Delete(name) || success;
195
196     if ( (items & eFormatCategoryItemFilter)  == eFormatCategoryItemFilter )
197         success = GetTypeFiltersContainer()->Delete(name) || success;
198     if ( (items & eFormatCategoryItemRegexFilter) == eFormatCategoryItemRegexFilter )
199         success = GetRegexTypeFiltersContainer()->Delete(name) || success;
200
201 #ifndef LLDB_DISABLE_PYTHON
202     if ( (items & eFormatCategoryItemSynth)  == eFormatCategoryItemSynth )
203         success = GetTypeSyntheticsContainer()->Delete(name) || success;
204     if ( (items & eFormatCategoryItemRegexSynth) == eFormatCategoryItemRegexSynth )
205         success = GetRegexTypeSyntheticsContainer()->Delete(name) || success;
206 #endif
207     
208     if ( (items & eFormatCategoryItemValidator)  == eFormatCategoryItemValidator )
209         success = GetTypeValidatorsContainer()->Delete(name) || success;
210     if ( (items & eFormatCategoryItemRegexValidator) == eFormatCategoryItemRegexValidator )
211         success = GetRegexTypeValidatorsContainer()->Delete(name) || success;
212     
213     return success;
214 }
215
216 uint32_t
217 TypeCategoryImpl::GetCount (FormatCategoryItems items)
218 {
219     uint32_t count = 0;
220
221     if ( (items & eFormatCategoryItemValue) == eFormatCategoryItemValue )
222         count += GetTypeFormatsContainer()->GetCount();
223     if ( (items & eFormatCategoryItemRegexValue) == eFormatCategoryItemRegexValue )
224         count += GetRegexTypeFormatsContainer()->GetCount();
225     
226     if ( (items & eFormatCategoryItemSummary) == eFormatCategoryItemSummary )
227         count += GetTypeSummariesContainer()->GetCount();
228     if ( (items & eFormatCategoryItemRegexSummary) == eFormatCategoryItemRegexSummary )
229         count += GetRegexTypeSummariesContainer()->GetCount();
230
231     if ( (items & eFormatCategoryItemFilter)  == eFormatCategoryItemFilter )
232         count += GetTypeFiltersContainer()->GetCount();
233     if ( (items & eFormatCategoryItemRegexFilter) == eFormatCategoryItemRegexFilter )
234         count += GetRegexTypeFiltersContainer()->GetCount();
235
236 #ifndef LLDB_DISABLE_PYTHON
237     if ( (items & eFormatCategoryItemSynth)  == eFormatCategoryItemSynth )
238         count += GetTypeSyntheticsContainer()->GetCount();
239     if ( (items & eFormatCategoryItemRegexSynth) == eFormatCategoryItemRegexSynth )
240         count += GetRegexTypeSyntheticsContainer()->GetCount();
241 #endif
242     
243     if ( (items & eFormatCategoryItemValidator)  == eFormatCategoryItemValidator )
244         count += GetTypeValidatorsContainer()->GetCount();
245     if ( (items & eFormatCategoryItemRegexValidator) == eFormatCategoryItemRegexValidator )
246         count += GetRegexTypeValidatorsContainer()->GetCount();
247     
248     return count;
249 }
250
251 bool
252 TypeCategoryImpl::AnyMatches(ConstString type_name,
253                              FormatCategoryItems items,
254                              bool only_enabled,
255                              const char** matching_category,
256                              FormatCategoryItems* matching_type)
257 {
258     if (!IsEnabled() && only_enabled)
259         return false;
260     
261     lldb::TypeFormatImplSP format_sp;
262     lldb::TypeSummaryImplSP summary_sp;
263     TypeFilterImpl::SharedPointer filter_sp;
264 #ifndef LLDB_DISABLE_PYTHON
265     ScriptedSyntheticChildren::SharedPointer synth_sp;
266 #endif
267     TypeValidatorImpl::SharedPointer validator_sp;
268     
269     if ( (items & eFormatCategoryItemValue) == eFormatCategoryItemValue )
270     {
271         if (GetTypeFormatsContainer()->Get(type_name, format_sp))
272         {
273             if (matching_category)
274                 *matching_category = m_name.GetCString();
275             if (matching_type)
276                 *matching_type = eFormatCategoryItemValue;
277             return true;
278         }
279     }
280     if ( (items & eFormatCategoryItemRegexValue) == eFormatCategoryItemRegexValue )
281     {
282         if (GetRegexTypeFormatsContainer()->Get(type_name, format_sp))
283         {
284             if (matching_category)
285                 *matching_category = m_name.GetCString();
286             if (matching_type)
287                 *matching_type = eFormatCategoryItemRegexValue;
288             return true;
289         }
290     }
291     
292     if ( (items & eFormatCategoryItemSummary) == eFormatCategoryItemSummary )
293     {
294         if (GetTypeSummariesContainer()->Get(type_name, summary_sp))
295         {
296             if (matching_category)
297                 *matching_category = m_name.GetCString();
298             if (matching_type)
299                 *matching_type = eFormatCategoryItemSummary;
300             return true;
301         }
302     }
303     if ( (items & eFormatCategoryItemRegexSummary) == eFormatCategoryItemRegexSummary )
304     {
305         if (GetRegexTypeSummariesContainer()->Get(type_name, summary_sp))
306         {
307             if (matching_category)
308                 *matching_category = m_name.GetCString();
309             if (matching_type)
310                 *matching_type = eFormatCategoryItemRegexSummary;
311             return true;
312         }
313     }
314     
315     if ( (items & eFormatCategoryItemFilter)  == eFormatCategoryItemFilter )
316     {
317         if (GetTypeFiltersContainer()->Get(type_name, filter_sp))
318         {
319             if (matching_category)
320                 *matching_category = m_name.GetCString();
321             if (matching_type)
322                 *matching_type = eFormatCategoryItemFilter;
323             return true;
324         }
325     }
326     if ( (items & eFormatCategoryItemRegexFilter) == eFormatCategoryItemRegexFilter )
327     {
328         if (GetRegexTypeFiltersContainer()->Get(type_name, filter_sp))
329         {
330             if (matching_category)
331                 *matching_category = m_name.GetCString();
332             if (matching_type)
333                 *matching_type = eFormatCategoryItemRegexFilter;
334             return true;
335         }
336     }
337     
338 #ifndef LLDB_DISABLE_PYTHON
339     if ( (items & eFormatCategoryItemSynth)  == eFormatCategoryItemSynth )
340     {
341         if (GetTypeSyntheticsContainer()->Get(type_name, synth_sp))
342         {
343             if (matching_category)
344                 *matching_category = m_name.GetCString();
345             if (matching_type)
346                 *matching_type = eFormatCategoryItemSynth;
347             return true;
348         }
349     }
350     if ( (items & eFormatCategoryItemRegexSynth) == eFormatCategoryItemRegexSynth )
351     {
352         if (GetRegexTypeSyntheticsContainer()->Get(type_name, synth_sp))
353         {
354             if (matching_category)
355                 *matching_category = m_name.GetCString();
356             if (matching_type)
357                 *matching_type = eFormatCategoryItemRegexSynth;
358             return true;
359         }
360     }
361 #endif
362     
363     if ( (items & eFormatCategoryItemValidator)  == eFormatCategoryItemValidator )
364     {
365         if (GetTypeValidatorsContainer()->Get(type_name, validator_sp))
366         {
367             if (matching_category)
368                 *matching_category = m_name.GetCString();
369             if (matching_type)
370                 *matching_type = eFormatCategoryItemValidator;
371             return true;
372         }
373     }
374     if ( (items & eFormatCategoryItemRegexValidator) == eFormatCategoryItemRegexValidator )
375     {
376         if (GetRegexTypeValidatorsContainer()->Get(type_name, validator_sp))
377         {
378             if (matching_category)
379                 *matching_category = m_name.GetCString();
380             if (matching_type)
381                 *matching_type = eFormatCategoryItemRegexValidator;
382             return true;
383         }
384     }
385     
386     return false;
387 }
388
389 TypeCategoryImpl::FormatContainer::MapValueType
390 TypeCategoryImpl::GetFormatForType (lldb::TypeNameSpecifierImplSP type_sp)
391 {
392     FormatContainer::MapValueType retval;
393     
394     if (type_sp)
395     {
396         if (type_sp->IsRegex())
397             GetRegexTypeFormatsContainer()->GetExact(ConstString(type_sp->GetName()),retval);
398         else
399             GetTypeFormatsContainer()->GetExact(ConstString(type_sp->GetName()),retval);
400     }
401     
402     return retval;
403 }
404
405 TypeCategoryImpl::SummaryContainer::MapValueType
406 TypeCategoryImpl::GetSummaryForType (lldb::TypeNameSpecifierImplSP type_sp)
407 {
408     SummaryContainer::MapValueType retval;
409     
410     if (type_sp)
411     {
412         if (type_sp->IsRegex())
413             GetRegexTypeSummariesContainer()->GetExact(ConstString(type_sp->GetName()),retval);
414         else
415             GetTypeSummariesContainer()->GetExact(ConstString(type_sp->GetName()),retval);
416     }
417     
418     return retval;
419 }
420
421 TypeCategoryImpl::FilterContainer::MapValueType
422 TypeCategoryImpl::GetFilterForType (lldb::TypeNameSpecifierImplSP type_sp)
423 {
424     FilterContainer::MapValueType retval;
425     
426     if (type_sp)
427     {
428         if (type_sp->IsRegex())
429             GetRegexTypeFiltersContainer()->GetExact(ConstString(type_sp->GetName()),retval);
430         else
431             GetTypeFiltersContainer()->GetExact(ConstString(type_sp->GetName()),retval);
432     }
433     
434     return retval;
435 }
436
437 #ifndef LLDB_DISABLE_PYTHON
438 TypeCategoryImpl::SynthContainer::MapValueType
439 TypeCategoryImpl::GetSyntheticForType (lldb::TypeNameSpecifierImplSP type_sp)
440 {
441     SynthContainer::MapValueType retval;
442     
443     if (type_sp)
444     {
445         if (type_sp->IsRegex())
446             GetRegexTypeSyntheticsContainer()->GetExact(ConstString(type_sp->GetName()),retval);
447         else
448             GetTypeSyntheticsContainer()->GetExact(ConstString(type_sp->GetName()),retval);
449     }
450     
451     return retval;
452 }
453 #endif
454
455 TypeCategoryImpl::ValidatorContainer::MapValueType
456 TypeCategoryImpl::GetValidatorForType (lldb::TypeNameSpecifierImplSP type_sp)
457 {
458     ValidatorContainer::MapValueType retval;
459     
460     if (type_sp)
461     {
462         if (type_sp->IsRegex())
463             GetRegexTypeValidatorsContainer()->GetExact(ConstString(type_sp->GetName()),retval);
464         else
465             GetTypeValidatorsContainer()->GetExact(ConstString(type_sp->GetName()),retval);
466     }
467     
468     return retval;
469 }
470
471 lldb::TypeNameSpecifierImplSP
472 TypeCategoryImpl::GetTypeNameSpecifierForSummaryAtIndex (size_t index)
473 {
474     if (index < GetTypeSummariesContainer()->GetCount())
475         return GetTypeSummariesContainer()->GetTypeNameSpecifierAtIndex(index);
476     else
477         return GetRegexTypeSummariesContainer()->GetTypeNameSpecifierAtIndex(index-GetTypeSummariesContainer()->GetCount());
478 }
479
480 TypeCategoryImpl::FormatContainer::MapValueType
481 TypeCategoryImpl::GetFormatAtIndex (size_t index)
482 {
483     if (index < GetTypeFormatsContainer()->GetCount())
484         return GetTypeFormatsContainer()->GetAtIndex(index);
485     else
486         return GetRegexTypeFormatsContainer()->GetAtIndex(index-GetTypeFormatsContainer()->GetCount());
487 }
488
489 TypeCategoryImpl::SummaryContainer::MapValueType
490 TypeCategoryImpl::GetSummaryAtIndex (size_t index)
491 {
492     if (index < GetTypeSummariesContainer()->GetCount())
493         return GetTypeSummariesContainer()->GetAtIndex(index);
494     else
495         return GetRegexTypeSummariesContainer()->GetAtIndex(index-GetTypeSummariesContainer()->GetCount());
496 }
497
498 TypeCategoryImpl::FilterContainer::MapValueType
499 TypeCategoryImpl::GetFilterAtIndex (size_t index)
500 {
501     if (index < GetTypeFiltersContainer()->GetCount())
502         return GetTypeFiltersContainer()->GetAtIndex(index);
503     else
504         return GetRegexTypeFiltersContainer()->GetAtIndex(index-GetTypeFiltersContainer()->GetCount());
505 }
506
507 lldb::TypeNameSpecifierImplSP
508 TypeCategoryImpl::GetTypeNameSpecifierForFormatAtIndex (size_t index)
509 {
510     if (index < GetTypeFormatsContainer()->GetCount())
511         return GetTypeFormatsContainer()->GetTypeNameSpecifierAtIndex(index);
512     else
513         return GetRegexTypeFormatsContainer()->GetTypeNameSpecifierAtIndex(index-GetTypeFormatsContainer()->GetCount());
514 }
515
516 lldb::TypeNameSpecifierImplSP
517 TypeCategoryImpl::GetTypeNameSpecifierForFilterAtIndex (size_t index)
518 {
519     if (index < GetTypeFiltersContainer()->GetCount())
520         return GetTypeFiltersContainer()->GetTypeNameSpecifierAtIndex(index);
521     else
522         return GetRegexTypeFiltersContainer()->GetTypeNameSpecifierAtIndex(index-GetTypeFiltersContainer()->GetCount());
523 }
524
525 #ifndef LLDB_DISABLE_PYTHON
526 TypeCategoryImpl::SynthContainer::MapValueType
527 TypeCategoryImpl::GetSyntheticAtIndex (size_t index)
528 {
529     if (index < GetTypeSyntheticsContainer()->GetCount())
530         return GetTypeSyntheticsContainer()->GetAtIndex(index);
531     else
532         return GetRegexTypeSyntheticsContainer()->GetAtIndex(index-GetTypeSyntheticsContainer()->GetCount());
533 }
534
535 lldb::TypeNameSpecifierImplSP
536 TypeCategoryImpl::GetTypeNameSpecifierForSyntheticAtIndex (size_t index)
537 {
538     if (index < GetTypeSyntheticsContainer()->GetCount())
539         return GetTypeSyntheticsContainer()->GetTypeNameSpecifierAtIndex(index);
540     else
541         return GetRegexTypeSyntheticsContainer()->GetTypeNameSpecifierAtIndex(index - GetTypeSyntheticsContainer()->GetCount());
542 }
543 #endif
544
545 TypeCategoryImpl::ValidatorContainer::MapValueType
546 TypeCategoryImpl::GetValidatorAtIndex (size_t index)
547 {
548     if (index < GetTypeValidatorsContainer()->GetCount())
549         return GetTypeValidatorsContainer()->GetAtIndex(index);
550     else
551         return GetRegexTypeValidatorsContainer()->GetAtIndex(index-GetTypeValidatorsContainer()->GetCount());
552 }
553
554 lldb::TypeNameSpecifierImplSP
555 TypeCategoryImpl::GetTypeNameSpecifierForValidatorAtIndex (size_t index)
556 {
557     if (index < GetTypeValidatorsContainer()->GetCount())
558         return GetTypeValidatorsContainer()->GetTypeNameSpecifierAtIndex(index);
559     else
560         return GetRegexTypeValidatorsContainer()->GetTypeNameSpecifierAtIndex(index - GetTypeValidatorsContainer()->GetCount());
561 }
562
563 void
564 TypeCategoryImpl::Enable (bool value, uint32_t position)
565 {
566     Mutex::Locker locker(m_mutex);
567     if ( (m_enabled = value) )
568         m_enabled_position = position;
569     if (m_change_listener)
570         m_change_listener->Changed();
571 }