| 1 | //===-- FormattersHelpers.cpp ---------------------------------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | #include "lldb/DataFormatters/FormattersHelpers.h" |
| 13 | #include "lldb/Core/Module.h" |
| 14 | #include "lldb/Target/StackFrame.h" |
| 15 | #include "lldb/Target/Target.h" |
| 16 | #include "lldb/Target/Thread.h" |
| 17 | #include "lldb/Utility/ConstString.h" |
| 18 | #include "lldb/Utility/RegularExpression.h" |
| 19 | |
| 20 | using namespace lldb; |
| 21 | using namespace lldb_private; |
| 22 | using namespace lldb_private::formatters; |
| 23 | |
| 24 | void lldb_private::formatters::AddFormat( |
| 25 | TypeCategoryImpl::SharedPointer category_sp, lldb::Format format, |
| 26 | llvm::StringRef type_name, TypeFormatImpl::Flags flags, bool regex) { |
| 27 | lldb::TypeFormatImplSP format_sp(new TypeFormatImpl_Format(format, flags)); |
| 28 | |
| 29 | FormatterMatchType match_type = |
| 30 | regex ? eFormatterMatchRegex : eFormatterMatchExact; |
| 31 | category_sp->AddTypeFormat(name: type_name, match_type, format_sp); |
| 32 | } |
| 33 | |
| 34 | void lldb_private::formatters::AddSummary( |
| 35 | TypeCategoryImpl::SharedPointer category_sp, TypeSummaryImplSP summary_sp, |
| 36 | llvm::StringRef type_name, bool regex) { |
| 37 | FormatterMatchType match_type = |
| 38 | regex ? eFormatterMatchRegex : eFormatterMatchExact; |
| 39 | category_sp->AddTypeSummary(name: type_name, match_type, summary_sp); |
| 40 | } |
| 41 | |
| 42 | void lldb_private::formatters::AddStringSummary( |
| 43 | TypeCategoryImpl::SharedPointer category_sp, const char *string, |
| 44 | llvm::StringRef type_name, TypeSummaryImpl::Flags flags, bool regex) { |
| 45 | lldb::TypeSummaryImplSP summary_sp(new StringSummaryFormat(flags, string)); |
| 46 | |
| 47 | FormatterMatchType match_type = |
| 48 | regex ? eFormatterMatchRegex : eFormatterMatchExact; |
| 49 | category_sp->AddTypeSummary(name: type_name, match_type, summary_sp); |
| 50 | } |
| 51 | |
| 52 | void lldb_private::formatters::AddOneLineSummary( |
| 53 | TypeCategoryImpl::SharedPointer category_sp, llvm::StringRef type_name, |
| 54 | TypeSummaryImpl::Flags flags, bool regex) { |
| 55 | flags.SetShowMembersOneLiner(true); |
| 56 | lldb::TypeSummaryImplSP summary_sp(new StringSummaryFormat(flags, "" )); |
| 57 | |
| 58 | FormatterMatchType match_type = |
| 59 | regex ? eFormatterMatchRegex : eFormatterMatchExact; |
| 60 | category_sp->AddTypeSummary(name: type_name, match_type, summary_sp); |
| 61 | } |
| 62 | |
| 63 | void lldb_private::formatters::AddCXXSummary( |
| 64 | TypeCategoryImpl::SharedPointer category_sp, |
| 65 | CXXFunctionSummaryFormat::Callback funct, const char *description, |
| 66 | llvm::StringRef type_name, TypeSummaryImpl::Flags flags, bool regex) { |
| 67 | lldb::TypeSummaryImplSP summary_sp( |
| 68 | new CXXFunctionSummaryFormat(flags, funct, description)); |
| 69 | |
| 70 | FormatterMatchType match_type = |
| 71 | regex ? eFormatterMatchRegex : eFormatterMatchExact; |
| 72 | category_sp->AddTypeSummary(name: type_name, match_type, summary_sp); |
| 73 | } |
| 74 | |
| 75 | void lldb_private::formatters::AddCXXSynthetic( |
| 76 | TypeCategoryImpl::SharedPointer category_sp, |
| 77 | CXXSyntheticChildren::CreateFrontEndCallback generator, |
| 78 | const char *description, llvm::StringRef type_name, |
| 79 | ScriptedSyntheticChildren::Flags flags, bool regex) { |
| 80 | lldb::SyntheticChildrenSP synth_sp( |
| 81 | new CXXSyntheticChildren(flags, description, generator)); |
| 82 | FormatterMatchType match_type = |
| 83 | regex ? eFormatterMatchRegex : eFormatterMatchExact; |
| 84 | category_sp->AddTypeSynthetic(name: type_name, match_type, synth_sp); |
| 85 | } |
| 86 | |
| 87 | void lldb_private::formatters::AddFilter( |
| 88 | TypeCategoryImpl::SharedPointer category_sp, |
| 89 | std::vector<std::string> children, const char *description, |
| 90 | llvm::StringRef type_name, ScriptedSyntheticChildren::Flags flags, |
| 91 | bool regex) { |
| 92 | TypeFilterImplSP filter_sp(new TypeFilterImpl(flags)); |
| 93 | for (auto child : children) |
| 94 | filter_sp->AddExpressionPath(path: child); |
| 95 | FormatterMatchType match_type = |
| 96 | regex ? eFormatterMatchRegex : eFormatterMatchExact; |
| 97 | category_sp->AddTypeFilter(name: type_name, match_type, filter_sp); |
| 98 | } |
| 99 | |
| 100 | std::optional<size_t> |
| 101 | lldb_private::formatters::(const char *item_name) { |
| 102 | if (!item_name || !*item_name) |
| 103 | return std::nullopt; |
| 104 | if (*item_name != '[') |
| 105 | return std::nullopt; |
| 106 | item_name++; |
| 107 | char *endptr = nullptr; |
| 108 | unsigned long int idx = ::strtoul(nptr: item_name, endptr: &endptr, base: 0); |
| 109 | if ((idx == 0 && endptr == item_name) || idx == ULONG_MAX) |
| 110 | return std::nullopt; |
| 111 | return idx; |
| 112 | } |
| 113 | |
| 114 | Address |
| 115 | lldb_private::formatters::GetArrayAddressOrPointerValue(ValueObject &valobj) { |
| 116 | ValueObject::AddrAndType data_addr; |
| 117 | |
| 118 | if (valobj.IsPointerType()) |
| 119 | data_addr = valobj.GetPointerValue(); |
| 120 | else if (valobj.IsArrayType()) |
| 121 | data_addr = valobj.GetAddressOf(/*scalar_is_load_address=*/true); |
| 122 | |
| 123 | if (data_addr.address != LLDB_INVALID_ADDRESS && |
| 124 | data_addr.type == eAddressTypeFile) |
| 125 | return Address(data_addr.address, valobj.GetModule()->GetSectionList()); |
| 126 | |
| 127 | return data_addr.address; |
| 128 | } |
| 129 | |