xref: /src/contrib/llvm-project/lldb/source/DataFormatters/FormattersHelpers.cpp (revision 06c3fb2749bda94cb5201f81ffdb8fa6c3161b2e)
1cfca06d7SDimitry Andric //===-- FormattersHelpers.cpp ---------------------------------------------===//
2e81d9d49SDimitry Andric //
35f29bb8aSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45f29bb8aSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
55f29bb8aSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e81d9d49SDimitry Andric //
7e81d9d49SDimitry Andric //===----------------------------------------------------------------------===//
8e81d9d49SDimitry Andric 
9e81d9d49SDimitry Andric 
10e81d9d49SDimitry Andric 
11e81d9d49SDimitry Andric 
12e81d9d49SDimitry Andric #include "lldb/DataFormatters/FormattersHelpers.h"
13c0981da4SDimitry Andric #include "lldb/Core/Module.h"
14e81d9d49SDimitry Andric #include "lldb/Target/StackFrame.h"
15e81d9d49SDimitry Andric #include "lldb/Target/Target.h"
16e81d9d49SDimitry Andric #include "lldb/Target/Thread.h"
1774a628f7SDimitry Andric #include "lldb/Utility/ConstString.h"
1874a628f7SDimitry Andric #include "lldb/Utility/RegularExpression.h"
19e81d9d49SDimitry Andric 
20e81d9d49SDimitry Andric using namespace lldb;
21e81d9d49SDimitry Andric using namespace lldb_private;
22e81d9d49SDimitry Andric using namespace lldb_private::formatters;
23e81d9d49SDimitry Andric 
AddFormat(TypeCategoryImpl::SharedPointer category_sp,lldb::Format format,llvm::StringRef type_name,TypeFormatImpl::Flags flags,bool regex)2414f1b3e8SDimitry Andric void lldb_private::formatters::AddFormat(
2514f1b3e8SDimitry Andric     TypeCategoryImpl::SharedPointer category_sp, lldb::Format format,
267fa27ce4SDimitry Andric     llvm::StringRef type_name, TypeFormatImpl::Flags flags, bool regex) {
27e81d9d49SDimitry Andric   lldb::TypeFormatImplSP format_sp(new TypeFormatImpl_Format(format, flags));
28e81d9d49SDimitry Andric 
29e3b55780SDimitry Andric   FormatterMatchType match_type =
30e3b55780SDimitry Andric       regex ? eFormatterMatchRegex : eFormatterMatchExact;
317fa27ce4SDimitry Andric   category_sp->AddTypeFormat(type_name, match_type, format_sp);
32e81d9d49SDimitry Andric }
33e81d9d49SDimitry Andric 
AddSummary(TypeCategoryImpl::SharedPointer category_sp,TypeSummaryImplSP summary_sp,llvm::StringRef type_name,bool regex)3414f1b3e8SDimitry Andric void lldb_private::formatters::AddSummary(
3514f1b3e8SDimitry Andric     TypeCategoryImpl::SharedPointer category_sp, TypeSummaryImplSP summary_sp,
367fa27ce4SDimitry Andric     llvm::StringRef type_name, bool regex) {
37e3b55780SDimitry Andric   FormatterMatchType match_type =
38e3b55780SDimitry Andric       regex ? eFormatterMatchRegex : eFormatterMatchExact;
397fa27ce4SDimitry Andric   category_sp->AddTypeSummary(type_name, match_type, summary_sp);
40e81d9d49SDimitry Andric }
41e81d9d49SDimitry Andric 
AddStringSummary(TypeCategoryImpl::SharedPointer category_sp,const char * string,llvm::StringRef type_name,TypeSummaryImpl::Flags flags,bool regex)4214f1b3e8SDimitry Andric void lldb_private::formatters::AddStringSummary(
4314f1b3e8SDimitry Andric     TypeCategoryImpl::SharedPointer category_sp, const char *string,
447fa27ce4SDimitry Andric     llvm::StringRef type_name, TypeSummaryImpl::Flags flags, bool regex) {
4514f1b3e8SDimitry Andric   lldb::TypeSummaryImplSP summary_sp(new StringSummaryFormat(flags, string));
46e81d9d49SDimitry Andric 
47e3b55780SDimitry Andric   FormatterMatchType match_type =
48e3b55780SDimitry Andric       regex ? eFormatterMatchRegex : eFormatterMatchExact;
497fa27ce4SDimitry Andric   category_sp->AddTypeSummary(type_name, match_type, summary_sp);
50e81d9d49SDimitry Andric }
51e81d9d49SDimitry Andric 
AddOneLineSummary(TypeCategoryImpl::SharedPointer category_sp,llvm::StringRef type_name,TypeSummaryImpl::Flags flags,bool regex)5214f1b3e8SDimitry Andric void lldb_private::formatters::AddOneLineSummary(
537fa27ce4SDimitry Andric     TypeCategoryImpl::SharedPointer category_sp, llvm::StringRef type_name,
5414f1b3e8SDimitry Andric     TypeSummaryImpl::Flags flags, bool regex) {
55e81d9d49SDimitry Andric   flags.SetShowMembersOneLiner(true);
56e81d9d49SDimitry Andric   lldb::TypeSummaryImplSP summary_sp(new StringSummaryFormat(flags, ""));
57e81d9d49SDimitry Andric 
58e3b55780SDimitry Andric   FormatterMatchType match_type =
59e3b55780SDimitry Andric       regex ? eFormatterMatchRegex : eFormatterMatchExact;
607fa27ce4SDimitry Andric   category_sp->AddTypeSummary(type_name, match_type, summary_sp);
61e81d9d49SDimitry Andric }
62e81d9d49SDimitry Andric 
AddCXXSummary(TypeCategoryImpl::SharedPointer category_sp,CXXFunctionSummaryFormat::Callback funct,const char * description,llvm::StringRef type_name,TypeSummaryImpl::Flags flags,bool regex)6314f1b3e8SDimitry Andric void lldb_private::formatters::AddCXXSummary(
6414f1b3e8SDimitry Andric     TypeCategoryImpl::SharedPointer category_sp,
6514f1b3e8SDimitry Andric     CXXFunctionSummaryFormat::Callback funct, const char *description,
667fa27ce4SDimitry Andric     llvm::StringRef type_name, TypeSummaryImpl::Flags flags, bool regex) {
6714f1b3e8SDimitry Andric   lldb::TypeSummaryImplSP summary_sp(
6814f1b3e8SDimitry Andric       new CXXFunctionSummaryFormat(flags, funct, description));
69e3b55780SDimitry Andric 
70e3b55780SDimitry Andric   FormatterMatchType match_type =
71e3b55780SDimitry Andric       regex ? eFormatterMatchRegex : eFormatterMatchExact;
727fa27ce4SDimitry Andric   category_sp->AddTypeSummary(type_name, match_type, summary_sp);
73e81d9d49SDimitry Andric }
74e81d9d49SDimitry Andric 
AddCXXSynthetic(TypeCategoryImpl::SharedPointer category_sp,CXXSyntheticChildren::CreateFrontEndCallback generator,const char * description,llvm::StringRef type_name,ScriptedSyntheticChildren::Flags flags,bool regex)7514f1b3e8SDimitry Andric void lldb_private::formatters::AddCXXSynthetic(
7614f1b3e8SDimitry Andric     TypeCategoryImpl::SharedPointer category_sp,
77e81d9d49SDimitry Andric     CXXSyntheticChildren::CreateFrontEndCallback generator,
787fa27ce4SDimitry Andric     const char *description, llvm::StringRef type_name,
7914f1b3e8SDimitry Andric     ScriptedSyntheticChildren::Flags flags, bool regex) {
8014f1b3e8SDimitry Andric   lldb::SyntheticChildrenSP synth_sp(
8114f1b3e8SDimitry Andric       new CXXSyntheticChildren(flags, description, generator));
82e3b55780SDimitry Andric   FormatterMatchType match_type =
83e3b55780SDimitry Andric       regex ? eFormatterMatchRegex : eFormatterMatchExact;
847fa27ce4SDimitry Andric   category_sp->AddTypeSynthetic(type_name, match_type, synth_sp);
85e81d9d49SDimitry Andric }
86e81d9d49SDimitry Andric 
AddFilter(TypeCategoryImpl::SharedPointer category_sp,std::vector<std::string> children,const char * description,llvm::StringRef type_name,ScriptedSyntheticChildren::Flags flags,bool regex)8714f1b3e8SDimitry Andric void lldb_private::formatters::AddFilter(
8814f1b3e8SDimitry Andric     TypeCategoryImpl::SharedPointer category_sp,
8914f1b3e8SDimitry Andric     std::vector<std::string> children, const char *description,
907fa27ce4SDimitry Andric     llvm::StringRef type_name, ScriptedSyntheticChildren::Flags flags,
917fa27ce4SDimitry Andric     bool regex) {
92e81d9d49SDimitry Andric   TypeFilterImplSP filter_sp(new TypeFilterImpl(flags));
93e81d9d49SDimitry Andric   for (auto child : children)
94e81d9d49SDimitry Andric     filter_sp->AddExpressionPath(child);
95e3b55780SDimitry Andric   FormatterMatchType match_type =
96e3b55780SDimitry Andric       regex ? eFormatterMatchRegex : eFormatterMatchExact;
977fa27ce4SDimitry Andric   category_sp->AddTypeFilter(type_name, match_type, filter_sp);
98e81d9d49SDimitry Andric }
99e81d9d49SDimitry Andric 
ExtractIndexFromString(const char * item_name)10014f1b3e8SDimitry Andric size_t lldb_private::formatters::ExtractIndexFromString(const char *item_name) {
101e81d9d49SDimitry Andric   if (!item_name || !*item_name)
102e81d9d49SDimitry Andric     return UINT32_MAX;
103e81d9d49SDimitry Andric   if (*item_name != '[')
104e81d9d49SDimitry Andric     return UINT32_MAX;
105e81d9d49SDimitry Andric   item_name++;
1065f29bb8aSDimitry Andric   char *endptr = nullptr;
107e81d9d49SDimitry Andric   unsigned long int idx = ::strtoul(item_name, &endptr, 0);
108e81d9d49SDimitry Andric   if (idx == 0 && endptr == item_name)
109e81d9d49SDimitry Andric     return UINT32_MAX;
110e81d9d49SDimitry Andric   if (idx == ULONG_MAX)
111e81d9d49SDimitry Andric     return UINT32_MAX;
112e81d9d49SDimitry Andric   return idx;
113e81d9d49SDimitry Andric }
114e81d9d49SDimitry Andric 
115c0981da4SDimitry Andric Address
GetArrayAddressOrPointerValue(ValueObject & valobj)11614f1b3e8SDimitry Andric lldb_private::formatters::GetArrayAddressOrPointerValue(ValueObject &valobj) {
117e81d9d49SDimitry Andric   lldb::addr_t data_addr = LLDB_INVALID_ADDRESS;
118c0981da4SDimitry Andric   AddressType type;
119e81d9d49SDimitry Andric 
120e81d9d49SDimitry Andric   if (valobj.IsPointerType())
121c0981da4SDimitry Andric     data_addr = valobj.GetPointerValue(&type);
122e81d9d49SDimitry Andric   else if (valobj.IsArrayType())
123c0981da4SDimitry Andric     data_addr = valobj.GetAddressOf(/*scalar_is_load_address=*/true, &type);
124c0981da4SDimitry Andric   if (data_addr != LLDB_INVALID_ADDRESS && type == eAddressTypeFile)
125c0981da4SDimitry Andric     return Address(data_addr, valobj.GetModule()->GetSectionList());
126e81d9d49SDimitry Andric 
127e81d9d49SDimitry Andric   return data_addr;
128e81d9d49SDimitry Andric }
129