Path: blob/main/contrib/llvm-project/lldb/source/DataFormatters/FormattersHelpers.cpp
39587 views
//===-- FormattersHelpers.cpp ---------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7891011#include "lldb/DataFormatters/FormattersHelpers.h"12#include "lldb/Core/Module.h"13#include "lldb/Target/StackFrame.h"14#include "lldb/Target/Target.h"15#include "lldb/Target/Thread.h"16#include "lldb/Utility/ConstString.h"17#include "lldb/Utility/RegularExpression.h"1819using namespace lldb;20using namespace lldb_private;21using namespace lldb_private::formatters;2223void lldb_private::formatters::AddFormat(24TypeCategoryImpl::SharedPointer category_sp, lldb::Format format,25llvm::StringRef type_name, TypeFormatImpl::Flags flags, bool regex) {26lldb::TypeFormatImplSP format_sp(new TypeFormatImpl_Format(format, flags));2728FormatterMatchType match_type =29regex ? eFormatterMatchRegex : eFormatterMatchExact;30category_sp->AddTypeFormat(type_name, match_type, format_sp);31}3233void lldb_private::formatters::AddSummary(34TypeCategoryImpl::SharedPointer category_sp, TypeSummaryImplSP summary_sp,35llvm::StringRef type_name, bool regex) {36FormatterMatchType match_type =37regex ? eFormatterMatchRegex : eFormatterMatchExact;38category_sp->AddTypeSummary(type_name, match_type, summary_sp);39}4041void lldb_private::formatters::AddStringSummary(42TypeCategoryImpl::SharedPointer category_sp, const char *string,43llvm::StringRef type_name, TypeSummaryImpl::Flags flags, bool regex) {44lldb::TypeSummaryImplSP summary_sp(new StringSummaryFormat(flags, string));4546FormatterMatchType match_type =47regex ? eFormatterMatchRegex : eFormatterMatchExact;48category_sp->AddTypeSummary(type_name, match_type, summary_sp);49}5051void lldb_private::formatters::AddOneLineSummary(52TypeCategoryImpl::SharedPointer category_sp, llvm::StringRef type_name,53TypeSummaryImpl::Flags flags, bool regex) {54flags.SetShowMembersOneLiner(true);55lldb::TypeSummaryImplSP summary_sp(new StringSummaryFormat(flags, ""));5657FormatterMatchType match_type =58regex ? eFormatterMatchRegex : eFormatterMatchExact;59category_sp->AddTypeSummary(type_name, match_type, summary_sp);60}6162void lldb_private::formatters::AddCXXSummary(63TypeCategoryImpl::SharedPointer category_sp,64CXXFunctionSummaryFormat::Callback funct, const char *description,65llvm::StringRef type_name, TypeSummaryImpl::Flags flags, bool regex) {66lldb::TypeSummaryImplSP summary_sp(67new CXXFunctionSummaryFormat(flags, funct, description));6869FormatterMatchType match_type =70regex ? eFormatterMatchRegex : eFormatterMatchExact;71category_sp->AddTypeSummary(type_name, match_type, summary_sp);72}7374void lldb_private::formatters::AddCXXSynthetic(75TypeCategoryImpl::SharedPointer category_sp,76CXXSyntheticChildren::CreateFrontEndCallback generator,77const char *description, llvm::StringRef type_name,78ScriptedSyntheticChildren::Flags flags, bool regex) {79lldb::SyntheticChildrenSP synth_sp(80new CXXSyntheticChildren(flags, description, generator));81FormatterMatchType match_type =82regex ? eFormatterMatchRegex : eFormatterMatchExact;83category_sp->AddTypeSynthetic(type_name, match_type, synth_sp);84}8586void lldb_private::formatters::AddFilter(87TypeCategoryImpl::SharedPointer category_sp,88std::vector<std::string> children, const char *description,89llvm::StringRef type_name, ScriptedSyntheticChildren::Flags flags,90bool regex) {91TypeFilterImplSP filter_sp(new TypeFilterImpl(flags));92for (auto child : children)93filter_sp->AddExpressionPath(child);94FormatterMatchType match_type =95regex ? eFormatterMatchRegex : eFormatterMatchExact;96category_sp->AddTypeFilter(type_name, match_type, filter_sp);97}9899size_t lldb_private::formatters::ExtractIndexFromString(const char *item_name) {100if (!item_name || !*item_name)101return UINT32_MAX;102if (*item_name != '[')103return UINT32_MAX;104item_name++;105char *endptr = nullptr;106unsigned long int idx = ::strtoul(item_name, &endptr, 0);107if (idx == 0 && endptr == item_name)108return UINT32_MAX;109if (idx == ULONG_MAX)110return UINT32_MAX;111return idx;112}113114Address115lldb_private::formatters::GetArrayAddressOrPointerValue(ValueObject &valobj) {116lldb::addr_t data_addr = LLDB_INVALID_ADDRESS;117AddressType type;118119if (valobj.IsPointerType())120data_addr = valobj.GetPointerValue(&type);121else if (valobj.IsArrayType())122data_addr = valobj.GetAddressOf(/*scalar_is_load_address=*/true, &type);123if (data_addr != LLDB_INVALID_ADDRESS && type == eAddressTypeFile)124return Address(data_addr, valobj.GetModule()->GetSectionList());125126return data_addr;127}128129130