1 //===-- TypeCategory.cpp -------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/lldb-python.h"
12 #include "lldb/DataFormatters/TypeCategory.h"
16 // Other libraries and framework includes
20 using namespace lldb_private;
22 TypeCategoryImpl::TypeCategoryImpl(IFormatChangeListener* clist,
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),
30 m_validator_cont("validator","regex-validator",clist),
32 m_change_listener(clist),
33 m_mutex(Mutex::eMutexTypeRecursive),
38 TypeCategoryImpl::Get (ValueObject& valobj,
39 const FormattersMatchVector& candidates,
40 lldb::TypeFormatImplSP& entry,
45 if (GetTypeFormatsContainer()->Get(candidates, entry, reason))
47 bool regex = GetRegexTypeFormatsContainer()->Get(candidates, entry, reason);
49 *reason |= lldb_private::eFormatterChoiceCriterionRegularExpressionSummary;
54 TypeCategoryImpl::Get (ValueObject& valobj,
55 const FormattersMatchVector& candidates,
56 lldb::TypeSummaryImplSP& entry,
61 if (GetTypeSummariesContainer()->Get(candidates, entry, reason))
63 bool regex = GetRegexTypeSummariesContainer()->Get(candidates, entry, reason);
65 *reason |= lldb_private::eFormatterChoiceCriterionRegularExpressionSummary;
70 TypeCategoryImpl::Get (ValueObject& valobj,
71 const FormattersMatchVector& candidates,
72 lldb::SyntheticChildrenSP& entry,
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
82 if (!GetTypeFiltersContainer()->Get(candidates, filter_sp, &reason_filter))
83 regex_filter = GetRegexTypeFiltersContainer()->Get (candidates, filter_sp, &reason_filter);
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())
94 else if (!filter_sp.get() && synth.get())
97 else if (filter_sp.get() && !synth.get())
100 else /*if (filter_sp.get() && synth.get())*/
102 if (filter_sp->GetRevision() > synth->GetRevision())
109 if (regex_synth && reason)
110 *reason |= lldb_private::eFormatterChoiceCriterionRegularExpressionFilter;
116 if (regex_filter && reason)
117 *reason |= lldb_private::eFormatterChoiceCriterionRegularExpressionFilter;
134 TypeCategoryImpl::Get (ValueObject& valobj,
135 const FormattersMatchVector& candidates,
136 lldb::TypeValidatorImplSP& entry,
141 if (GetTypeValidatorsContainer()->Get(candidates, entry, reason))
143 bool regex = GetRegexTypeValidatorsContainer()->Get(candidates, entry, reason);
145 *reason |= lldb_private::eFormatterChoiceCriterionRegularExpressionSummary;
150 TypeCategoryImpl::Clear (FormatCategoryItems items)
152 if ( (items & eFormatCategoryItemValue) == eFormatCategoryItemValue )
153 GetTypeFormatsContainer()->Clear();
154 if ( (items & eFormatCategoryItemRegexValue) == eFormatCategoryItemRegexValue )
155 GetRegexTypeFormatsContainer()->Clear();
157 if ( (items & eFormatCategoryItemSummary) == eFormatCategoryItemSummary )
158 GetTypeSummariesContainer()->Clear();
159 if ( (items & eFormatCategoryItemRegexSummary) == eFormatCategoryItemRegexSummary )
160 GetRegexTypeSummariesContainer()->Clear();
162 if ( (items & eFormatCategoryItemFilter) == eFormatCategoryItemFilter )
163 GetTypeFiltersContainer()->Clear();
164 if ( (items & eFormatCategoryItemRegexFilter) == eFormatCategoryItemRegexFilter )
165 GetRegexTypeFiltersContainer()->Clear();
167 #ifndef LLDB_DISABLE_PYTHON
168 if ( (items & eFormatCategoryItemSynth) == eFormatCategoryItemSynth )
169 GetTypeSyntheticsContainer()->Clear();
170 if ( (items & eFormatCategoryItemRegexSynth) == eFormatCategoryItemRegexSynth )
171 GetRegexTypeSyntheticsContainer()->Clear();
174 if ( (items & eFormatCategoryItemValidator) == eFormatCategoryItemValidator )
175 GetTypeValidatorsContainer()->Clear();
176 if ( (items & eFormatCategoryItemRegexValidator) == eFormatCategoryItemRegexValidator )
177 GetRegexTypeValidatorsContainer()->Clear();
181 TypeCategoryImpl::Delete (ConstString name,
182 FormatCategoryItems items)
184 bool success = false;
186 if ( (items & eFormatCategoryItemValue) == eFormatCategoryItemValue )
187 success = GetTypeFormatsContainer()->Delete(name) || success;
188 if ( (items & eFormatCategoryItemRegexValue) == eFormatCategoryItemRegexValue )
189 success = GetRegexTypeFormatsContainer()->Delete(name) || success;
191 if ( (items & eFormatCategoryItemSummary) == eFormatCategoryItemSummary )
192 success = GetTypeSummariesContainer()->Delete(name) || success;
193 if ( (items & eFormatCategoryItemRegexSummary) == eFormatCategoryItemRegexSummary )
194 success = GetRegexTypeSummariesContainer()->Delete(name) || success;
196 if ( (items & eFormatCategoryItemFilter) == eFormatCategoryItemFilter )
197 success = GetTypeFiltersContainer()->Delete(name) || success;
198 if ( (items & eFormatCategoryItemRegexFilter) == eFormatCategoryItemRegexFilter )
199 success = GetRegexTypeFiltersContainer()->Delete(name) || success;
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;
208 if ( (items & eFormatCategoryItemValidator) == eFormatCategoryItemValidator )
209 success = GetTypeValidatorsContainer()->Delete(name) || success;
210 if ( (items & eFormatCategoryItemRegexValidator) == eFormatCategoryItemRegexValidator )
211 success = GetRegexTypeValidatorsContainer()->Delete(name) || success;
217 TypeCategoryImpl::GetCount (FormatCategoryItems items)
221 if ( (items & eFormatCategoryItemValue) == eFormatCategoryItemValue )
222 count += GetTypeFormatsContainer()->GetCount();
223 if ( (items & eFormatCategoryItemRegexValue) == eFormatCategoryItemRegexValue )
224 count += GetRegexTypeFormatsContainer()->GetCount();
226 if ( (items & eFormatCategoryItemSummary) == eFormatCategoryItemSummary )
227 count += GetTypeSummariesContainer()->GetCount();
228 if ( (items & eFormatCategoryItemRegexSummary) == eFormatCategoryItemRegexSummary )
229 count += GetRegexTypeSummariesContainer()->GetCount();
231 if ( (items & eFormatCategoryItemFilter) == eFormatCategoryItemFilter )
232 count += GetTypeFiltersContainer()->GetCount();
233 if ( (items & eFormatCategoryItemRegexFilter) == eFormatCategoryItemRegexFilter )
234 count += GetRegexTypeFiltersContainer()->GetCount();
236 #ifndef LLDB_DISABLE_PYTHON
237 if ( (items & eFormatCategoryItemSynth) == eFormatCategoryItemSynth )
238 count += GetTypeSyntheticsContainer()->GetCount();
239 if ( (items & eFormatCategoryItemRegexSynth) == eFormatCategoryItemRegexSynth )
240 count += GetRegexTypeSyntheticsContainer()->GetCount();
243 if ( (items & eFormatCategoryItemValidator) == eFormatCategoryItemValidator )
244 count += GetTypeValidatorsContainer()->GetCount();
245 if ( (items & eFormatCategoryItemRegexValidator) == eFormatCategoryItemRegexValidator )
246 count += GetRegexTypeValidatorsContainer()->GetCount();
252 TypeCategoryImpl::AnyMatches(ConstString type_name,
253 FormatCategoryItems items,
255 const char** matching_category,
256 FormatCategoryItems* matching_type)
258 if (!IsEnabled() && only_enabled)
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;
267 TypeValidatorImpl::SharedPointer validator_sp;
269 if ( (items & eFormatCategoryItemValue) == eFormatCategoryItemValue )
271 if (GetTypeFormatsContainer()->Get(type_name, format_sp))
273 if (matching_category)
274 *matching_category = m_name.GetCString();
276 *matching_type = eFormatCategoryItemValue;
280 if ( (items & eFormatCategoryItemRegexValue) == eFormatCategoryItemRegexValue )
282 if (GetRegexTypeFormatsContainer()->Get(type_name, format_sp))
284 if (matching_category)
285 *matching_category = m_name.GetCString();
287 *matching_type = eFormatCategoryItemRegexValue;
292 if ( (items & eFormatCategoryItemSummary) == eFormatCategoryItemSummary )
294 if (GetTypeSummariesContainer()->Get(type_name, summary_sp))
296 if (matching_category)
297 *matching_category = m_name.GetCString();
299 *matching_type = eFormatCategoryItemSummary;
303 if ( (items & eFormatCategoryItemRegexSummary) == eFormatCategoryItemRegexSummary )
305 if (GetRegexTypeSummariesContainer()->Get(type_name, summary_sp))
307 if (matching_category)
308 *matching_category = m_name.GetCString();
310 *matching_type = eFormatCategoryItemRegexSummary;
315 if ( (items & eFormatCategoryItemFilter) == eFormatCategoryItemFilter )
317 if (GetTypeFiltersContainer()->Get(type_name, filter_sp))
319 if (matching_category)
320 *matching_category = m_name.GetCString();
322 *matching_type = eFormatCategoryItemFilter;
326 if ( (items & eFormatCategoryItemRegexFilter) == eFormatCategoryItemRegexFilter )
328 if (GetRegexTypeFiltersContainer()->Get(type_name, filter_sp))
330 if (matching_category)
331 *matching_category = m_name.GetCString();
333 *matching_type = eFormatCategoryItemRegexFilter;
338 #ifndef LLDB_DISABLE_PYTHON
339 if ( (items & eFormatCategoryItemSynth) == eFormatCategoryItemSynth )
341 if (GetTypeSyntheticsContainer()->Get(type_name, synth_sp))
343 if (matching_category)
344 *matching_category = m_name.GetCString();
346 *matching_type = eFormatCategoryItemSynth;
350 if ( (items & eFormatCategoryItemRegexSynth) == eFormatCategoryItemRegexSynth )
352 if (GetRegexTypeSyntheticsContainer()->Get(type_name, synth_sp))
354 if (matching_category)
355 *matching_category = m_name.GetCString();
357 *matching_type = eFormatCategoryItemRegexSynth;
363 if ( (items & eFormatCategoryItemValidator) == eFormatCategoryItemValidator )
365 if (GetTypeValidatorsContainer()->Get(type_name, validator_sp))
367 if (matching_category)
368 *matching_category = m_name.GetCString();
370 *matching_type = eFormatCategoryItemValidator;
374 if ( (items & eFormatCategoryItemRegexValidator) == eFormatCategoryItemRegexValidator )
376 if (GetRegexTypeValidatorsContainer()->Get(type_name, validator_sp))
378 if (matching_category)
379 *matching_category = m_name.GetCString();
381 *matching_type = eFormatCategoryItemRegexValidator;
389 TypeCategoryImpl::FormatContainer::MapValueType
390 TypeCategoryImpl::GetFormatForType (lldb::TypeNameSpecifierImplSP type_sp)
392 FormatContainer::MapValueType retval;
396 if (type_sp->IsRegex())
397 GetRegexTypeFormatsContainer()->GetExact(ConstString(type_sp->GetName()),retval);
399 GetTypeFormatsContainer()->GetExact(ConstString(type_sp->GetName()),retval);
405 TypeCategoryImpl::SummaryContainer::MapValueType
406 TypeCategoryImpl::GetSummaryForType (lldb::TypeNameSpecifierImplSP type_sp)
408 SummaryContainer::MapValueType retval;
412 if (type_sp->IsRegex())
413 GetRegexTypeSummariesContainer()->GetExact(ConstString(type_sp->GetName()),retval);
415 GetTypeSummariesContainer()->GetExact(ConstString(type_sp->GetName()),retval);
421 TypeCategoryImpl::FilterContainer::MapValueType
422 TypeCategoryImpl::GetFilterForType (lldb::TypeNameSpecifierImplSP type_sp)
424 FilterContainer::MapValueType retval;
428 if (type_sp->IsRegex())
429 GetRegexTypeFiltersContainer()->GetExact(ConstString(type_sp->GetName()),retval);
431 GetTypeFiltersContainer()->GetExact(ConstString(type_sp->GetName()),retval);
437 #ifndef LLDB_DISABLE_PYTHON
438 TypeCategoryImpl::SynthContainer::MapValueType
439 TypeCategoryImpl::GetSyntheticForType (lldb::TypeNameSpecifierImplSP type_sp)
441 SynthContainer::MapValueType retval;
445 if (type_sp->IsRegex())
446 GetRegexTypeSyntheticsContainer()->GetExact(ConstString(type_sp->GetName()),retval);
448 GetTypeSyntheticsContainer()->GetExact(ConstString(type_sp->GetName()),retval);
455 TypeCategoryImpl::ValidatorContainer::MapValueType
456 TypeCategoryImpl::GetValidatorForType (lldb::TypeNameSpecifierImplSP type_sp)
458 ValidatorContainer::MapValueType retval;
462 if (type_sp->IsRegex())
463 GetRegexTypeValidatorsContainer()->GetExact(ConstString(type_sp->GetName()),retval);
465 GetTypeValidatorsContainer()->GetExact(ConstString(type_sp->GetName()),retval);
471 lldb::TypeNameSpecifierImplSP
472 TypeCategoryImpl::GetTypeNameSpecifierForSummaryAtIndex (size_t index)
474 if (index < GetTypeSummariesContainer()->GetCount())
475 return GetTypeSummariesContainer()->GetTypeNameSpecifierAtIndex(index);
477 return GetRegexTypeSummariesContainer()->GetTypeNameSpecifierAtIndex(index-GetTypeSummariesContainer()->GetCount());
480 TypeCategoryImpl::FormatContainer::MapValueType
481 TypeCategoryImpl::GetFormatAtIndex (size_t index)
483 if (index < GetTypeFormatsContainer()->GetCount())
484 return GetTypeFormatsContainer()->GetAtIndex(index);
486 return GetRegexTypeFormatsContainer()->GetAtIndex(index-GetTypeFormatsContainer()->GetCount());
489 TypeCategoryImpl::SummaryContainer::MapValueType
490 TypeCategoryImpl::GetSummaryAtIndex (size_t index)
492 if (index < GetTypeSummariesContainer()->GetCount())
493 return GetTypeSummariesContainer()->GetAtIndex(index);
495 return GetRegexTypeSummariesContainer()->GetAtIndex(index-GetTypeSummariesContainer()->GetCount());
498 TypeCategoryImpl::FilterContainer::MapValueType
499 TypeCategoryImpl::GetFilterAtIndex (size_t index)
501 if (index < GetTypeFiltersContainer()->GetCount())
502 return GetTypeFiltersContainer()->GetAtIndex(index);
504 return GetRegexTypeFiltersContainer()->GetAtIndex(index-GetTypeFiltersContainer()->GetCount());
507 lldb::TypeNameSpecifierImplSP
508 TypeCategoryImpl::GetTypeNameSpecifierForFormatAtIndex (size_t index)
510 if (index < GetTypeFormatsContainer()->GetCount())
511 return GetTypeFormatsContainer()->GetTypeNameSpecifierAtIndex(index);
513 return GetRegexTypeFormatsContainer()->GetTypeNameSpecifierAtIndex(index-GetTypeFormatsContainer()->GetCount());
516 lldb::TypeNameSpecifierImplSP
517 TypeCategoryImpl::GetTypeNameSpecifierForFilterAtIndex (size_t index)
519 if (index < GetTypeFiltersContainer()->GetCount())
520 return GetTypeFiltersContainer()->GetTypeNameSpecifierAtIndex(index);
522 return GetRegexTypeFiltersContainer()->GetTypeNameSpecifierAtIndex(index-GetTypeFiltersContainer()->GetCount());
525 #ifndef LLDB_DISABLE_PYTHON
526 TypeCategoryImpl::SynthContainer::MapValueType
527 TypeCategoryImpl::GetSyntheticAtIndex (size_t index)
529 if (index < GetTypeSyntheticsContainer()->GetCount())
530 return GetTypeSyntheticsContainer()->GetAtIndex(index);
532 return GetRegexTypeSyntheticsContainer()->GetAtIndex(index-GetTypeSyntheticsContainer()->GetCount());
535 lldb::TypeNameSpecifierImplSP
536 TypeCategoryImpl::GetTypeNameSpecifierForSyntheticAtIndex (size_t index)
538 if (index < GetTypeSyntheticsContainer()->GetCount())
539 return GetTypeSyntheticsContainer()->GetTypeNameSpecifierAtIndex(index);
541 return GetRegexTypeSyntheticsContainer()->GetTypeNameSpecifierAtIndex(index - GetTypeSyntheticsContainer()->GetCount());
545 TypeCategoryImpl::ValidatorContainer::MapValueType
546 TypeCategoryImpl::GetValidatorAtIndex (size_t index)
548 if (index < GetTypeValidatorsContainer()->GetCount())
549 return GetTypeValidatorsContainer()->GetAtIndex(index);
551 return GetRegexTypeValidatorsContainer()->GetAtIndex(index-GetTypeValidatorsContainer()->GetCount());
554 lldb::TypeNameSpecifierImplSP
555 TypeCategoryImpl::GetTypeNameSpecifierForValidatorAtIndex (size_t index)
557 if (index < GetTypeValidatorsContainer()->GetCount())
558 return GetTypeValidatorsContainer()->GetTypeNameSpecifierAtIndex(index);
560 return GetRegexTypeValidatorsContainer()->GetTypeNameSpecifierAtIndex(index - GetTypeValidatorsContainer()->GetCount());
564 TypeCategoryImpl::Enable (bool value, uint32_t position)
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();