1435933ddSDimitry Andric //===-- FormattersHelpers.cpp -------------------------------------*- C++
2435933ddSDimitry Andric //-*-===//
39f2f44ceSEd Maste //
49f2f44ceSEd Maste //                     The LLVM Compiler Infrastructure
59f2f44ceSEd Maste //
69f2f44ceSEd Maste // This file is distributed under the University of Illinois Open Source
79f2f44ceSEd Maste // License. See LICENSE.TXT for details.
89f2f44ceSEd Maste //
99f2f44ceSEd Maste //===----------------------------------------------------------------------===//
109f2f44ceSEd Maste 
119f2f44ceSEd Maste 
129f2f44ceSEd Maste 
139f2f44ceSEd Maste 
149f2f44ceSEd Maste #include "lldb/DataFormatters/FormattersHelpers.h"
159f2f44ceSEd Maste 
169f2f44ceSEd Maste #include "lldb/Target/StackFrame.h"
179f2f44ceSEd Maste #include "lldb/Target/Target.h"
189f2f44ceSEd Maste #include "lldb/Target/Thread.h"
19f678e45dSDimitry Andric #include "lldb/Utility/ConstString.h"
20f678e45dSDimitry Andric #include "lldb/Utility/RegularExpression.h"
219f2f44ceSEd Maste 
229f2f44ceSEd Maste using namespace lldb;
239f2f44ceSEd Maste using namespace lldb_private;
249f2f44ceSEd Maste using namespace lldb_private::formatters;
259f2f44ceSEd Maste 
AddFormat(TypeCategoryImpl::SharedPointer category_sp,lldb::Format format,ConstString type_name,TypeFormatImpl::Flags flags,bool regex)26435933ddSDimitry Andric void lldb_private::formatters::AddFormat(
27435933ddSDimitry Andric     TypeCategoryImpl::SharedPointer category_sp, lldb::Format format,
28435933ddSDimitry Andric     ConstString type_name, TypeFormatImpl::Flags flags, bool regex) {
299f2f44ceSEd Maste   lldb::TypeFormatImplSP format_sp(new TypeFormatImpl_Format(format, flags));
309f2f44ceSEd Maste 
319f2f44ceSEd Maste   if (regex)
32435933ddSDimitry Andric     category_sp->GetRegexTypeFormatsContainer()->Add(
33435933ddSDimitry Andric         RegularExpressionSP(new RegularExpression(type_name.GetStringRef())),
34435933ddSDimitry Andric         format_sp);
359f2f44ceSEd Maste   else
369f2f44ceSEd Maste     category_sp->GetTypeFormatsContainer()->Add(type_name, format_sp);
379f2f44ceSEd Maste }
389f2f44ceSEd Maste 
AddSummary(TypeCategoryImpl::SharedPointer category_sp,TypeSummaryImplSP summary_sp,ConstString type_name,bool regex)39435933ddSDimitry Andric void lldb_private::formatters::AddSummary(
40435933ddSDimitry Andric     TypeCategoryImpl::SharedPointer category_sp, TypeSummaryImplSP summary_sp,
41435933ddSDimitry Andric     ConstString type_name, bool regex) {
429f2f44ceSEd Maste   if (regex)
43435933ddSDimitry Andric     category_sp->GetRegexTypeSummariesContainer()->Add(
44435933ddSDimitry Andric         RegularExpressionSP(new RegularExpression(type_name.GetStringRef())),
45435933ddSDimitry Andric         summary_sp);
469f2f44ceSEd Maste   else
479f2f44ceSEd Maste     category_sp->GetTypeSummariesContainer()->Add(type_name, summary_sp);
489f2f44ceSEd Maste }
499f2f44ceSEd Maste 
AddStringSummary(TypeCategoryImpl::SharedPointer category_sp,const char * string,ConstString type_name,TypeSummaryImpl::Flags flags,bool regex)50435933ddSDimitry Andric void lldb_private::formatters::AddStringSummary(
51435933ddSDimitry Andric     TypeCategoryImpl::SharedPointer category_sp, const char *string,
52435933ddSDimitry Andric     ConstString type_name, TypeSummaryImpl::Flags flags, bool regex) {
53435933ddSDimitry Andric   lldb::TypeSummaryImplSP summary_sp(new StringSummaryFormat(flags, string));
549f2f44ceSEd Maste 
559f2f44ceSEd Maste   if (regex)
56435933ddSDimitry Andric     category_sp->GetRegexTypeSummariesContainer()->Add(
57435933ddSDimitry Andric         RegularExpressionSP(new RegularExpression(type_name.GetStringRef())),
58435933ddSDimitry Andric         summary_sp);
599f2f44ceSEd Maste   else
609f2f44ceSEd Maste     category_sp->GetTypeSummariesContainer()->Add(type_name, summary_sp);
619f2f44ceSEd Maste }
629f2f44ceSEd Maste 
AddOneLineSummary(TypeCategoryImpl::SharedPointer category_sp,ConstString type_name,TypeSummaryImpl::Flags flags,bool regex)63435933ddSDimitry Andric void lldb_private::formatters::AddOneLineSummary(
64435933ddSDimitry Andric     TypeCategoryImpl::SharedPointer category_sp, ConstString type_name,
65435933ddSDimitry Andric     TypeSummaryImpl::Flags flags, bool regex) {
669f2f44ceSEd Maste   flags.SetShowMembersOneLiner(true);
679f2f44ceSEd Maste   lldb::TypeSummaryImplSP summary_sp(new StringSummaryFormat(flags, ""));
689f2f44ceSEd Maste 
699f2f44ceSEd Maste   if (regex)
70435933ddSDimitry Andric     category_sp->GetRegexTypeSummariesContainer()->Add(
71435933ddSDimitry Andric         RegularExpressionSP(new RegularExpression(type_name.GetStringRef())),
72435933ddSDimitry Andric         summary_sp);
739f2f44ceSEd Maste   else
749f2f44ceSEd Maste     category_sp->GetTypeSummariesContainer()->Add(type_name, summary_sp);
759f2f44ceSEd Maste }
769f2f44ceSEd Maste 
779f2f44ceSEd Maste #ifndef LLDB_DISABLE_PYTHON
AddCXXSummary(TypeCategoryImpl::SharedPointer category_sp,CXXFunctionSummaryFormat::Callback funct,const char * description,ConstString type_name,TypeSummaryImpl::Flags flags,bool regex)78435933ddSDimitry Andric void lldb_private::formatters::AddCXXSummary(
79435933ddSDimitry Andric     TypeCategoryImpl::SharedPointer category_sp,
80435933ddSDimitry Andric     CXXFunctionSummaryFormat::Callback funct, const char *description,
81435933ddSDimitry Andric     ConstString type_name, TypeSummaryImpl::Flags flags, bool regex) {
82435933ddSDimitry Andric   lldb::TypeSummaryImplSP summary_sp(
83435933ddSDimitry Andric       new CXXFunctionSummaryFormat(flags, funct, description));
849f2f44ceSEd Maste   if (regex)
85435933ddSDimitry Andric     category_sp->GetRegexTypeSummariesContainer()->Add(
86435933ddSDimitry Andric         RegularExpressionSP(new RegularExpression(type_name.GetStringRef())),
87435933ddSDimitry Andric         summary_sp);
889f2f44ceSEd Maste   else
899f2f44ceSEd Maste     category_sp->GetTypeSummariesContainer()->Add(type_name, summary_sp);
909f2f44ceSEd Maste }
919f2f44ceSEd Maste 
AddCXXSynthetic(TypeCategoryImpl::SharedPointer category_sp,CXXSyntheticChildren::CreateFrontEndCallback generator,const char * description,ConstString type_name,ScriptedSyntheticChildren::Flags flags,bool regex)92435933ddSDimitry Andric void lldb_private::formatters::AddCXXSynthetic(
93435933ddSDimitry Andric     TypeCategoryImpl::SharedPointer category_sp,
949f2f44ceSEd Maste     CXXSyntheticChildren::CreateFrontEndCallback generator,
95435933ddSDimitry Andric     const char *description, ConstString type_name,
96435933ddSDimitry Andric     ScriptedSyntheticChildren::Flags flags, bool regex) {
97435933ddSDimitry Andric   lldb::SyntheticChildrenSP synth_sp(
98435933ddSDimitry Andric       new CXXSyntheticChildren(flags, description, generator));
999f2f44ceSEd Maste   if (regex)
100435933ddSDimitry Andric     category_sp->GetRegexTypeSyntheticsContainer()->Add(
101435933ddSDimitry Andric         RegularExpressionSP(new RegularExpression(type_name.GetStringRef())),
102435933ddSDimitry Andric         synth_sp);
1039f2f44ceSEd Maste   else
1049f2f44ceSEd Maste     category_sp->GetTypeSyntheticsContainer()->Add(type_name, synth_sp);
1059f2f44ceSEd Maste }
1069f2f44ceSEd Maste 
AddFilter(TypeCategoryImpl::SharedPointer category_sp,std::vector<std::string> children,const char * description,ConstString type_name,ScriptedSyntheticChildren::Flags flags,bool regex)107435933ddSDimitry Andric void lldb_private::formatters::AddFilter(
108435933ddSDimitry Andric     TypeCategoryImpl::SharedPointer category_sp,
109435933ddSDimitry Andric     std::vector<std::string> children, const char *description,
110435933ddSDimitry Andric     ConstString type_name, ScriptedSyntheticChildren::Flags flags, bool regex) {
1119f2f44ceSEd Maste   TypeFilterImplSP filter_sp(new TypeFilterImpl(flags));
1129f2f44ceSEd Maste   for (auto child : children)
1139f2f44ceSEd Maste     filter_sp->AddExpressionPath(child);
1149f2f44ceSEd Maste   if (regex)
115435933ddSDimitry Andric     category_sp->GetRegexTypeFiltersContainer()->Add(
116435933ddSDimitry Andric         RegularExpressionSP(new RegularExpression(type_name.GetStringRef())),
117435933ddSDimitry Andric         filter_sp);
1189f2f44ceSEd Maste   else
1199f2f44ceSEd Maste     category_sp->GetTypeFiltersContainer()->Add(type_name, filter_sp);
1209f2f44ceSEd Maste }
1219f2f44ceSEd Maste #endif
1229f2f44ceSEd Maste 
ExtractIndexFromString(const char * item_name)123435933ddSDimitry Andric size_t lldb_private::formatters::ExtractIndexFromString(const char *item_name) {
1249f2f44ceSEd Maste   if (!item_name || !*item_name)
1259f2f44ceSEd Maste     return UINT32_MAX;
1269f2f44ceSEd Maste   if (*item_name != '[')
1279f2f44ceSEd Maste     return UINT32_MAX;
1289f2f44ceSEd Maste   item_name++;
1299f2f44ceSEd Maste   char *endptr = NULL;
1309f2f44ceSEd Maste   unsigned long int idx = ::strtoul(item_name, &endptr, 0);
1319f2f44ceSEd Maste   if (idx == 0 && endptr == item_name)
1329f2f44ceSEd Maste     return UINT32_MAX;
1339f2f44ceSEd Maste   if (idx == ULONG_MAX)
1349f2f44ceSEd Maste     return UINT32_MAX;
1359f2f44ceSEd Maste   return idx;
1369f2f44ceSEd Maste }
1379f2f44ceSEd Maste 
1389f2f44ceSEd Maste lldb::addr_t
GetArrayAddressOrPointerValue(ValueObject & valobj)139435933ddSDimitry Andric lldb_private::formatters::GetArrayAddressOrPointerValue(ValueObject &valobj) {
1409f2f44ceSEd Maste   lldb::addr_t data_addr = LLDB_INVALID_ADDRESS;
1419f2f44ceSEd Maste 
1429f2f44ceSEd Maste   if (valobj.IsPointerType())
1439f2f44ceSEd Maste     data_addr = valobj.GetValueAsUnsigned(0);
1449f2f44ceSEd Maste   else if (valobj.IsArrayType())
1459f2f44ceSEd Maste     data_addr = valobj.GetAddressOf();
1469f2f44ceSEd Maste 
1479f2f44ceSEd Maste   return data_addr;
1489f2f44ceSEd Maste }
149