15ffd83dbSDimitry Andric //===-- FormattersHelpers.cpp ---------------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric
90b57cec5SDimitry Andric
100b57cec5SDimitry Andric
110b57cec5SDimitry Andric
120b57cec5SDimitry Andric #include "lldb/DataFormatters/FormattersHelpers.h"
13349cc55cSDimitry Andric #include "lldb/Core/Module.h"
140b57cec5SDimitry Andric #include "lldb/Target/StackFrame.h"
150b57cec5SDimitry Andric #include "lldb/Target/Target.h"
160b57cec5SDimitry Andric #include "lldb/Target/Thread.h"
170b57cec5SDimitry Andric #include "lldb/Utility/ConstString.h"
180b57cec5SDimitry Andric #include "lldb/Utility/RegularExpression.h"
190b57cec5SDimitry Andric
200b57cec5SDimitry Andric using namespace lldb;
210b57cec5SDimitry Andric using namespace lldb_private;
220b57cec5SDimitry Andric using namespace lldb_private::formatters;
230b57cec5SDimitry Andric
AddFormat(TypeCategoryImpl::SharedPointer category_sp,lldb::Format format,llvm::StringRef type_name,TypeFormatImpl::Flags flags,bool regex)240b57cec5SDimitry Andric void lldb_private::formatters::AddFormat(
250b57cec5SDimitry Andric TypeCategoryImpl::SharedPointer category_sp, lldb::Format format,
26*fe013be4SDimitry Andric llvm::StringRef type_name, TypeFormatImpl::Flags flags, bool regex) {
270b57cec5SDimitry Andric lldb::TypeFormatImplSP format_sp(new TypeFormatImpl_Format(format, flags));
280b57cec5SDimitry Andric
29bdd1243dSDimitry Andric FormatterMatchType match_type =
30bdd1243dSDimitry Andric regex ? eFormatterMatchRegex : eFormatterMatchExact;
31*fe013be4SDimitry Andric category_sp->AddTypeFormat(type_name, match_type, format_sp);
320b57cec5SDimitry Andric }
330b57cec5SDimitry Andric
AddSummary(TypeCategoryImpl::SharedPointer category_sp,TypeSummaryImplSP summary_sp,llvm::StringRef type_name,bool regex)340b57cec5SDimitry Andric void lldb_private::formatters::AddSummary(
350b57cec5SDimitry Andric TypeCategoryImpl::SharedPointer category_sp, TypeSummaryImplSP summary_sp,
36*fe013be4SDimitry Andric llvm::StringRef type_name, bool regex) {
37bdd1243dSDimitry Andric FormatterMatchType match_type =
38bdd1243dSDimitry Andric regex ? eFormatterMatchRegex : eFormatterMatchExact;
39*fe013be4SDimitry Andric category_sp->AddTypeSummary(type_name, match_type, summary_sp);
400b57cec5SDimitry Andric }
410b57cec5SDimitry Andric
AddStringSummary(TypeCategoryImpl::SharedPointer category_sp,const char * string,llvm::StringRef type_name,TypeSummaryImpl::Flags flags,bool regex)420b57cec5SDimitry Andric void lldb_private::formatters::AddStringSummary(
430b57cec5SDimitry Andric TypeCategoryImpl::SharedPointer category_sp, const char *string,
44*fe013be4SDimitry Andric llvm::StringRef type_name, TypeSummaryImpl::Flags flags, bool regex) {
450b57cec5SDimitry Andric lldb::TypeSummaryImplSP summary_sp(new StringSummaryFormat(flags, string));
460b57cec5SDimitry Andric
47bdd1243dSDimitry Andric FormatterMatchType match_type =
48bdd1243dSDimitry Andric regex ? eFormatterMatchRegex : eFormatterMatchExact;
49*fe013be4SDimitry Andric category_sp->AddTypeSummary(type_name, match_type, summary_sp);
500b57cec5SDimitry Andric }
510b57cec5SDimitry Andric
AddOneLineSummary(TypeCategoryImpl::SharedPointer category_sp,llvm::StringRef type_name,TypeSummaryImpl::Flags flags,bool regex)520b57cec5SDimitry Andric void lldb_private::formatters::AddOneLineSummary(
53*fe013be4SDimitry Andric TypeCategoryImpl::SharedPointer category_sp, llvm::StringRef type_name,
540b57cec5SDimitry Andric TypeSummaryImpl::Flags flags, bool regex) {
550b57cec5SDimitry Andric flags.SetShowMembersOneLiner(true);
560b57cec5SDimitry Andric lldb::TypeSummaryImplSP summary_sp(new StringSummaryFormat(flags, ""));
570b57cec5SDimitry Andric
58bdd1243dSDimitry Andric FormatterMatchType match_type =
59bdd1243dSDimitry Andric regex ? eFormatterMatchRegex : eFormatterMatchExact;
60*fe013be4SDimitry Andric category_sp->AddTypeSummary(type_name, match_type, summary_sp);
610b57cec5SDimitry Andric }
620b57cec5SDimitry Andric
AddCXXSummary(TypeCategoryImpl::SharedPointer category_sp,CXXFunctionSummaryFormat::Callback funct,const char * description,llvm::StringRef type_name,TypeSummaryImpl::Flags flags,bool regex)630b57cec5SDimitry Andric void lldb_private::formatters::AddCXXSummary(
640b57cec5SDimitry Andric TypeCategoryImpl::SharedPointer category_sp,
650b57cec5SDimitry Andric CXXFunctionSummaryFormat::Callback funct, const char *description,
66*fe013be4SDimitry Andric llvm::StringRef type_name, TypeSummaryImpl::Flags flags, bool regex) {
670b57cec5SDimitry Andric lldb::TypeSummaryImplSP summary_sp(
680b57cec5SDimitry Andric new CXXFunctionSummaryFormat(flags, funct, description));
69bdd1243dSDimitry Andric
70bdd1243dSDimitry Andric FormatterMatchType match_type =
71bdd1243dSDimitry Andric regex ? eFormatterMatchRegex : eFormatterMatchExact;
72*fe013be4SDimitry Andric category_sp->AddTypeSummary(type_name, match_type, summary_sp);
730b57cec5SDimitry Andric }
740b57cec5SDimitry Andric
AddCXXSynthetic(TypeCategoryImpl::SharedPointer category_sp,CXXSyntheticChildren::CreateFrontEndCallback generator,const char * description,llvm::StringRef type_name,ScriptedSyntheticChildren::Flags flags,bool regex)750b57cec5SDimitry Andric void lldb_private::formatters::AddCXXSynthetic(
760b57cec5SDimitry Andric TypeCategoryImpl::SharedPointer category_sp,
770b57cec5SDimitry Andric CXXSyntheticChildren::CreateFrontEndCallback generator,
78*fe013be4SDimitry Andric const char *description, llvm::StringRef type_name,
790b57cec5SDimitry Andric ScriptedSyntheticChildren::Flags flags, bool regex) {
800b57cec5SDimitry Andric lldb::SyntheticChildrenSP synth_sp(
810b57cec5SDimitry Andric new CXXSyntheticChildren(flags, description, generator));
82bdd1243dSDimitry Andric FormatterMatchType match_type =
83bdd1243dSDimitry Andric regex ? eFormatterMatchRegex : eFormatterMatchExact;
84*fe013be4SDimitry Andric category_sp->AddTypeSynthetic(type_name, match_type, synth_sp);
850b57cec5SDimitry Andric }
860b57cec5SDimitry Andric
AddFilter(TypeCategoryImpl::SharedPointer category_sp,std::vector<std::string> children,const char * description,llvm::StringRef type_name,ScriptedSyntheticChildren::Flags flags,bool regex)870b57cec5SDimitry Andric void lldb_private::formatters::AddFilter(
880b57cec5SDimitry Andric TypeCategoryImpl::SharedPointer category_sp,
890b57cec5SDimitry Andric std::vector<std::string> children, const char *description,
90*fe013be4SDimitry Andric llvm::StringRef type_name, ScriptedSyntheticChildren::Flags flags,
91*fe013be4SDimitry Andric bool regex) {
920b57cec5SDimitry Andric TypeFilterImplSP filter_sp(new TypeFilterImpl(flags));
930b57cec5SDimitry Andric for (auto child : children)
940b57cec5SDimitry Andric filter_sp->AddExpressionPath(child);
95bdd1243dSDimitry Andric FormatterMatchType match_type =
96bdd1243dSDimitry Andric regex ? eFormatterMatchRegex : eFormatterMatchExact;
97*fe013be4SDimitry Andric category_sp->AddTypeFilter(type_name, match_type, filter_sp);
980b57cec5SDimitry Andric }
990b57cec5SDimitry Andric
ExtractIndexFromString(const char * item_name)1000b57cec5SDimitry Andric size_t lldb_private::formatters::ExtractIndexFromString(const char *item_name) {
1010b57cec5SDimitry Andric if (!item_name || !*item_name)
1020b57cec5SDimitry Andric return UINT32_MAX;
1030b57cec5SDimitry Andric if (*item_name != '[')
1040b57cec5SDimitry Andric return UINT32_MAX;
1050b57cec5SDimitry Andric item_name++;
1060b57cec5SDimitry Andric char *endptr = nullptr;
1070b57cec5SDimitry Andric unsigned long int idx = ::strtoul(item_name, &endptr, 0);
1080b57cec5SDimitry Andric if (idx == 0 && endptr == item_name)
1090b57cec5SDimitry Andric return UINT32_MAX;
1100b57cec5SDimitry Andric if (idx == ULONG_MAX)
1110b57cec5SDimitry Andric return UINT32_MAX;
1120b57cec5SDimitry Andric return idx;
1130b57cec5SDimitry Andric }
1140b57cec5SDimitry Andric
115349cc55cSDimitry Andric Address
GetArrayAddressOrPointerValue(ValueObject & valobj)1160b57cec5SDimitry Andric lldb_private::formatters::GetArrayAddressOrPointerValue(ValueObject &valobj) {
1170b57cec5SDimitry Andric lldb::addr_t data_addr = LLDB_INVALID_ADDRESS;
118349cc55cSDimitry Andric AddressType type;
1190b57cec5SDimitry Andric
1200b57cec5SDimitry Andric if (valobj.IsPointerType())
121349cc55cSDimitry Andric data_addr = valobj.GetPointerValue(&type);
1220b57cec5SDimitry Andric else if (valobj.IsArrayType())
123349cc55cSDimitry Andric data_addr = valobj.GetAddressOf(/*scalar_is_load_address=*/true, &type);
124349cc55cSDimitry Andric if (data_addr != LLDB_INVALID_ADDRESS && type == eAddressTypeFile)
125349cc55cSDimitry Andric return Address(data_addr, valobj.GetModule()->GetSectionList());
1260b57cec5SDimitry Andric
1270b57cec5SDimitry Andric return data_addr;
1280b57cec5SDimitry Andric }
129