15ffd83dbSDimitry Andric //===-- FormatManager.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 #include "lldb/DataFormatters/FormatManager.h"
100b57cec5SDimitry Andric
110b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h"
120b57cec5SDimitry Andric
130b57cec5SDimitry Andric
140b57cec5SDimitry Andric #include "lldb/Core/Debugger.h"
150b57cec5SDimitry Andric #include "lldb/DataFormatters/FormattersHelpers.h"
160b57cec5SDimitry Andric #include "lldb/DataFormatters/LanguageCategory.h"
170b57cec5SDimitry Andric #include "lldb/Target/ExecutionContext.h"
180b57cec5SDimitry Andric #include "lldb/Target/Language.h"
190b57cec5SDimitry Andric #include "lldb/Utility/Log.h"
200b57cec5SDimitry Andric
210b57cec5SDimitry Andric using namespace lldb;
220b57cec5SDimitry Andric using namespace lldb_private;
230b57cec5SDimitry Andric using namespace lldb_private::formatters;
240b57cec5SDimitry Andric
250b57cec5SDimitry Andric struct FormatInfo {
260b57cec5SDimitry Andric Format format;
270b57cec5SDimitry Andric const char format_char; // One or more format characters that can be used for
280b57cec5SDimitry Andric // this format.
290b57cec5SDimitry Andric const char *format_name; // Long format name that can be used to specify the
300b57cec5SDimitry Andric // current format
310b57cec5SDimitry Andric };
320b57cec5SDimitry Andric
339dba64beSDimitry Andric static constexpr FormatInfo g_format_infos[] = {
340b57cec5SDimitry Andric {eFormatDefault, '\0', "default"},
350b57cec5SDimitry Andric {eFormatBoolean, 'B', "boolean"},
360b57cec5SDimitry Andric {eFormatBinary, 'b', "binary"},
370b57cec5SDimitry Andric {eFormatBytes, 'y', "bytes"},
380b57cec5SDimitry Andric {eFormatBytesWithASCII, 'Y', "bytes with ASCII"},
390b57cec5SDimitry Andric {eFormatChar, 'c', "character"},
400b57cec5SDimitry Andric {eFormatCharPrintable, 'C', "printable character"},
410b57cec5SDimitry Andric {eFormatComplexFloat, 'F', "complex float"},
420b57cec5SDimitry Andric {eFormatCString, 's', "c-string"},
430b57cec5SDimitry Andric {eFormatDecimal, 'd', "decimal"},
440b57cec5SDimitry Andric {eFormatEnum, 'E', "enumeration"},
450b57cec5SDimitry Andric {eFormatHex, 'x', "hex"},
460b57cec5SDimitry Andric {eFormatHexUppercase, 'X', "uppercase hex"},
470b57cec5SDimitry Andric {eFormatFloat, 'f', "float"},
480b57cec5SDimitry Andric {eFormatOctal, 'o', "octal"},
490b57cec5SDimitry Andric {eFormatOSType, 'O', "OSType"},
500b57cec5SDimitry Andric {eFormatUnicode16, 'U', "unicode16"},
510b57cec5SDimitry Andric {eFormatUnicode32, '\0', "unicode32"},
520b57cec5SDimitry Andric {eFormatUnsigned, 'u', "unsigned decimal"},
530b57cec5SDimitry Andric {eFormatPointer, 'p', "pointer"},
540b57cec5SDimitry Andric {eFormatVectorOfChar, '\0', "char[]"},
550b57cec5SDimitry Andric {eFormatVectorOfSInt8, '\0', "int8_t[]"},
560b57cec5SDimitry Andric {eFormatVectorOfUInt8, '\0', "uint8_t[]"},
570b57cec5SDimitry Andric {eFormatVectorOfSInt16, '\0', "int16_t[]"},
580b57cec5SDimitry Andric {eFormatVectorOfUInt16, '\0', "uint16_t[]"},
590b57cec5SDimitry Andric {eFormatVectorOfSInt32, '\0', "int32_t[]"},
600b57cec5SDimitry Andric {eFormatVectorOfUInt32, '\0', "uint32_t[]"},
610b57cec5SDimitry Andric {eFormatVectorOfSInt64, '\0', "int64_t[]"},
620b57cec5SDimitry Andric {eFormatVectorOfUInt64, '\0', "uint64_t[]"},
630b57cec5SDimitry Andric {eFormatVectorOfFloat16, '\0', "float16[]"},
640b57cec5SDimitry Andric {eFormatVectorOfFloat32, '\0', "float32[]"},
650b57cec5SDimitry Andric {eFormatVectorOfFloat64, '\0', "float64[]"},
660b57cec5SDimitry Andric {eFormatVectorOfUInt128, '\0', "uint128_t[]"},
670b57cec5SDimitry Andric {eFormatComplexInteger, 'I', "complex integer"},
680b57cec5SDimitry Andric {eFormatCharArray, 'a', "character array"},
690b57cec5SDimitry Andric {eFormatAddressInfo, 'A', "address"},
700b57cec5SDimitry Andric {eFormatHexFloat, '\0', "hex float"},
710b57cec5SDimitry Andric {eFormatInstruction, 'i', "instruction"},
729dba64beSDimitry Andric {eFormatVoid, 'v', "void"},
739dba64beSDimitry Andric {eFormatUnicode8, 'u', "unicode8"},
749dba64beSDimitry Andric };
759dba64beSDimitry Andric
769dba64beSDimitry Andric static_assert((sizeof(g_format_infos) / sizeof(g_format_infos[0])) ==
779dba64beSDimitry Andric kNumFormats,
789dba64beSDimitry Andric "All formats must have a corresponding info entry.");
790b57cec5SDimitry Andric
800b57cec5SDimitry Andric static uint32_t g_num_format_infos = llvm::array_lengthof(g_format_infos);
810b57cec5SDimitry Andric
GetFormatFromFormatChar(char format_char,Format & format)820b57cec5SDimitry Andric static bool GetFormatFromFormatChar(char format_char, Format &format) {
830b57cec5SDimitry Andric for (uint32_t i = 0; i < g_num_format_infos; ++i) {
840b57cec5SDimitry Andric if (g_format_infos[i].format_char == format_char) {
850b57cec5SDimitry Andric format = g_format_infos[i].format;
860b57cec5SDimitry Andric return true;
870b57cec5SDimitry Andric }
880b57cec5SDimitry Andric }
890b57cec5SDimitry Andric format = eFormatInvalid;
900b57cec5SDimitry Andric return false;
910b57cec5SDimitry Andric }
920b57cec5SDimitry Andric
GetFormatFromFormatName(const char * format_name,bool partial_match_ok,Format & format)930b57cec5SDimitry Andric static bool GetFormatFromFormatName(const char *format_name,
940b57cec5SDimitry Andric bool partial_match_ok, Format &format) {
950b57cec5SDimitry Andric uint32_t i;
960b57cec5SDimitry Andric for (i = 0; i < g_num_format_infos; ++i) {
970b57cec5SDimitry Andric if (strcasecmp(g_format_infos[i].format_name, format_name) == 0) {
980b57cec5SDimitry Andric format = g_format_infos[i].format;
990b57cec5SDimitry Andric return true;
1000b57cec5SDimitry Andric }
1010b57cec5SDimitry Andric }
1020b57cec5SDimitry Andric
1030b57cec5SDimitry Andric if (partial_match_ok) {
1040b57cec5SDimitry Andric for (i = 0; i < g_num_format_infos; ++i) {
1050b57cec5SDimitry Andric if (strcasestr(g_format_infos[i].format_name, format_name) ==
1060b57cec5SDimitry Andric g_format_infos[i].format_name) {
1070b57cec5SDimitry Andric format = g_format_infos[i].format;
1080b57cec5SDimitry Andric return true;
1090b57cec5SDimitry Andric }
1100b57cec5SDimitry Andric }
1110b57cec5SDimitry Andric }
1120b57cec5SDimitry Andric format = eFormatInvalid;
1130b57cec5SDimitry Andric return false;
1140b57cec5SDimitry Andric }
1150b57cec5SDimitry Andric
Changed()1160b57cec5SDimitry Andric void FormatManager::Changed() {
1170b57cec5SDimitry Andric ++m_last_revision;
1180b57cec5SDimitry Andric m_format_cache.Clear();
1190b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_language_categories_mutex);
1200b57cec5SDimitry Andric for (auto &iter : m_language_categories_map) {
1210b57cec5SDimitry Andric if (iter.second)
1220b57cec5SDimitry Andric iter.second->GetFormatCache().Clear();
1230b57cec5SDimitry Andric }
1240b57cec5SDimitry Andric }
1250b57cec5SDimitry Andric
GetFormatFromCString(const char * format_cstr,bool partial_match_ok,lldb::Format & format)1260b57cec5SDimitry Andric bool FormatManager::GetFormatFromCString(const char *format_cstr,
1270b57cec5SDimitry Andric bool partial_match_ok,
1280b57cec5SDimitry Andric lldb::Format &format) {
1290b57cec5SDimitry Andric bool success = false;
1300b57cec5SDimitry Andric if (format_cstr && format_cstr[0]) {
1310b57cec5SDimitry Andric if (format_cstr[1] == '\0') {
1320b57cec5SDimitry Andric success = GetFormatFromFormatChar(format_cstr[0], format);
1330b57cec5SDimitry Andric if (success)
1340b57cec5SDimitry Andric return true;
1350b57cec5SDimitry Andric }
1360b57cec5SDimitry Andric
1370b57cec5SDimitry Andric success = GetFormatFromFormatName(format_cstr, partial_match_ok, format);
1380b57cec5SDimitry Andric }
1390b57cec5SDimitry Andric if (!success)
1400b57cec5SDimitry Andric format = eFormatInvalid;
1410b57cec5SDimitry Andric return success;
1420b57cec5SDimitry Andric }
1430b57cec5SDimitry Andric
GetFormatAsFormatChar(lldb::Format format)1440b57cec5SDimitry Andric char FormatManager::GetFormatAsFormatChar(lldb::Format format) {
1450b57cec5SDimitry Andric for (uint32_t i = 0; i < g_num_format_infos; ++i) {
1460b57cec5SDimitry Andric if (g_format_infos[i].format == format)
1470b57cec5SDimitry Andric return g_format_infos[i].format_char;
1480b57cec5SDimitry Andric }
1490b57cec5SDimitry Andric return '\0';
1500b57cec5SDimitry Andric }
1510b57cec5SDimitry Andric
GetFormatAsCString(Format format)1520b57cec5SDimitry Andric const char *FormatManager::GetFormatAsCString(Format format) {
1530b57cec5SDimitry Andric if (format >= eFormatDefault && format < kNumFormats)
1540b57cec5SDimitry Andric return g_format_infos[format].format_name;
1550b57cec5SDimitry Andric return nullptr;
1560b57cec5SDimitry Andric }
1570b57cec5SDimitry Andric
EnableAllCategories()1580b57cec5SDimitry Andric void FormatManager::EnableAllCategories() {
1590b57cec5SDimitry Andric m_categories_map.EnableAllCategories();
1600b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_language_categories_mutex);
1610b57cec5SDimitry Andric for (auto &iter : m_language_categories_map) {
1620b57cec5SDimitry Andric if (iter.second)
1630b57cec5SDimitry Andric iter.second->Enable();
1640b57cec5SDimitry Andric }
1650b57cec5SDimitry Andric }
1660b57cec5SDimitry Andric
DisableAllCategories()1670b57cec5SDimitry Andric void FormatManager::DisableAllCategories() {
1680b57cec5SDimitry Andric m_categories_map.DisableAllCategories();
1690b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_language_categories_mutex);
1700b57cec5SDimitry Andric for (auto &iter : m_language_categories_map) {
1710b57cec5SDimitry Andric if (iter.second)
1720b57cec5SDimitry Andric iter.second->Disable();
1730b57cec5SDimitry Andric }
1740b57cec5SDimitry Andric }
1750b57cec5SDimitry Andric
GetPossibleMatches(ValueObject & valobj,CompilerType compiler_type,lldb::DynamicValueType use_dynamic,FormattersMatchVector & entries,bool did_strip_ptr,bool did_strip_ref,bool did_strip_typedef,bool root_level)1760b57cec5SDimitry Andric void FormatManager::GetPossibleMatches(
1775ffd83dbSDimitry Andric ValueObject &valobj, CompilerType compiler_type,
1780b57cec5SDimitry Andric lldb::DynamicValueType use_dynamic, FormattersMatchVector &entries,
1790b57cec5SDimitry Andric bool did_strip_ptr, bool did_strip_ref, bool did_strip_typedef,
1800b57cec5SDimitry Andric bool root_level) {
1810b57cec5SDimitry Andric compiler_type = compiler_type.GetTypeForFormatters();
1825ffd83dbSDimitry Andric ConstString type_name(compiler_type.GetTypeName());
1830b57cec5SDimitry Andric if (valobj.GetBitfieldBitSize() > 0) {
1840b57cec5SDimitry Andric StreamString sstring;
1850b57cec5SDimitry Andric sstring.Printf("%s:%d", type_name.AsCString(), valobj.GetBitfieldBitSize());
1860b57cec5SDimitry Andric ConstString bitfieldname(sstring.GetString());
1870b57cec5SDimitry Andric entries.push_back(
1885ffd83dbSDimitry Andric {bitfieldname, did_strip_ptr, did_strip_ref, did_strip_typedef});
1890b57cec5SDimitry Andric }
1900b57cec5SDimitry Andric
1910b57cec5SDimitry Andric if (!compiler_type.IsMeaninglessWithoutDynamicResolution()) {
1920b57cec5SDimitry Andric entries.push_back(
1935ffd83dbSDimitry Andric {type_name, did_strip_ptr, did_strip_ref, did_strip_typedef});
1940b57cec5SDimitry Andric
1955ffd83dbSDimitry Andric ConstString display_type_name(compiler_type.GetTypeName());
1960b57cec5SDimitry Andric if (display_type_name != type_name)
1975ffd83dbSDimitry Andric entries.push_back({display_type_name, did_strip_ptr,
1980b57cec5SDimitry Andric did_strip_ref, did_strip_typedef});
1990b57cec5SDimitry Andric }
2000b57cec5SDimitry Andric
2010b57cec5SDimitry Andric for (bool is_rvalue_ref = true, j = true;
2020b57cec5SDimitry Andric j && compiler_type.IsReferenceType(nullptr, &is_rvalue_ref); j = false) {
2030b57cec5SDimitry Andric CompilerType non_ref_type = compiler_type.GetNonReferenceType();
2040b57cec5SDimitry Andric GetPossibleMatches(
2050b57cec5SDimitry Andric valobj, non_ref_type,
2060b57cec5SDimitry Andric use_dynamic, entries, did_strip_ptr, true, did_strip_typedef);
2070b57cec5SDimitry Andric if (non_ref_type.IsTypedefType()) {
2080b57cec5SDimitry Andric CompilerType deffed_referenced_type = non_ref_type.GetTypedefedType();
2090b57cec5SDimitry Andric deffed_referenced_type =
2100b57cec5SDimitry Andric is_rvalue_ref ? deffed_referenced_type.GetRValueReferenceType()
2110b57cec5SDimitry Andric : deffed_referenced_type.GetLValueReferenceType();
2120b57cec5SDimitry Andric GetPossibleMatches(
2130b57cec5SDimitry Andric valobj, deffed_referenced_type,
2140b57cec5SDimitry Andric use_dynamic, entries, did_strip_ptr, did_strip_ref,
2150b57cec5SDimitry Andric true); // this is not exactly the usual meaning of stripping typedefs
2160b57cec5SDimitry Andric }
2170b57cec5SDimitry Andric }
2180b57cec5SDimitry Andric
2190b57cec5SDimitry Andric if (compiler_type.IsPointerType()) {
2200b57cec5SDimitry Andric CompilerType non_ptr_type = compiler_type.GetPointeeType();
2210b57cec5SDimitry Andric GetPossibleMatches(
2220b57cec5SDimitry Andric valobj, non_ptr_type,
2230b57cec5SDimitry Andric use_dynamic, entries, true, did_strip_ref, did_strip_typedef);
2240b57cec5SDimitry Andric if (non_ptr_type.IsTypedefType()) {
2250b57cec5SDimitry Andric CompilerType deffed_pointed_type =
2260b57cec5SDimitry Andric non_ptr_type.GetTypedefedType().GetPointerType();
227480093f4SDimitry Andric const bool stripped_typedef = true;
2280b57cec5SDimitry Andric GetPossibleMatches(
2290b57cec5SDimitry Andric valobj, deffed_pointed_type,
2300b57cec5SDimitry Andric use_dynamic, entries, did_strip_ptr, did_strip_ref,
231480093f4SDimitry Andric stripped_typedef); // this is not exactly the usual meaning of
232480093f4SDimitry Andric // stripping typedefs
2330b57cec5SDimitry Andric }
2340b57cec5SDimitry Andric }
2350b57cec5SDimitry Andric
236480093f4SDimitry Andric // For arrays with typedef-ed elements, we add a candidate with the typedef
237480093f4SDimitry Andric // stripped.
238480093f4SDimitry Andric uint64_t array_size;
239480093f4SDimitry Andric if (compiler_type.IsArrayType(nullptr, &array_size, nullptr)) {
240*af732203SDimitry Andric ExecutionContext exe_ctx(valobj.GetExecutionContextRef());
241*af732203SDimitry Andric CompilerType element_type = compiler_type.GetArrayElementType(
242*af732203SDimitry Andric exe_ctx.GetBestExecutionContextScope());
243480093f4SDimitry Andric if (element_type.IsTypedefType()) {
244480093f4SDimitry Andric // Get the stripped element type and compute the stripped array type
245480093f4SDimitry Andric // from it.
246480093f4SDimitry Andric CompilerType deffed_array_type =
247480093f4SDimitry Andric element_type.GetTypedefedType().GetArrayType(array_size);
248480093f4SDimitry Andric const bool stripped_typedef = true;
249480093f4SDimitry Andric GetPossibleMatches(
250480093f4SDimitry Andric valobj, deffed_array_type,
251480093f4SDimitry Andric use_dynamic, entries, did_strip_ptr, did_strip_ref,
252480093f4SDimitry Andric stripped_typedef); // this is not exactly the usual meaning of
253480093f4SDimitry Andric // stripping typedefs
254480093f4SDimitry Andric }
255480093f4SDimitry Andric }
256480093f4SDimitry Andric
257480093f4SDimitry Andric for (lldb::LanguageType language_type :
258480093f4SDimitry Andric GetCandidateLanguages(valobj.GetObjectRuntimeLanguage())) {
2590b57cec5SDimitry Andric if (Language *language = Language::FindPlugin(language_type)) {
2600b57cec5SDimitry Andric for (ConstString candidate :
2610b57cec5SDimitry Andric language->GetPossibleFormattersMatches(valobj, use_dynamic)) {
2620b57cec5SDimitry Andric entries.push_back(
2630b57cec5SDimitry Andric {candidate,
2640b57cec5SDimitry Andric did_strip_ptr, did_strip_ref, did_strip_typedef});
2650b57cec5SDimitry Andric }
2660b57cec5SDimitry Andric }
2670b57cec5SDimitry Andric }
2680b57cec5SDimitry Andric
2690b57cec5SDimitry Andric // try to strip typedef chains
2700b57cec5SDimitry Andric if (compiler_type.IsTypedefType()) {
2710b57cec5SDimitry Andric CompilerType deffed_type = compiler_type.GetTypedefedType();
2720b57cec5SDimitry Andric GetPossibleMatches(
2730b57cec5SDimitry Andric valobj, deffed_type,
2740b57cec5SDimitry Andric use_dynamic, entries, did_strip_ptr, did_strip_ref, true);
2750b57cec5SDimitry Andric }
2760b57cec5SDimitry Andric
2770b57cec5SDimitry Andric if (root_level) {
2780b57cec5SDimitry Andric do {
2790b57cec5SDimitry Andric if (!compiler_type.IsValid())
2800b57cec5SDimitry Andric break;
2810b57cec5SDimitry Andric
2820b57cec5SDimitry Andric CompilerType unqual_compiler_ast_type =
2830b57cec5SDimitry Andric compiler_type.GetFullyUnqualifiedType();
2840b57cec5SDimitry Andric if (!unqual_compiler_ast_type.IsValid())
2850b57cec5SDimitry Andric break;
2860b57cec5SDimitry Andric if (unqual_compiler_ast_type.GetOpaqueQualType() !=
2870b57cec5SDimitry Andric compiler_type.GetOpaqueQualType())
2885ffd83dbSDimitry Andric GetPossibleMatches(valobj, unqual_compiler_ast_type,
2890b57cec5SDimitry Andric use_dynamic, entries, did_strip_ptr, did_strip_ref,
2900b57cec5SDimitry Andric did_strip_typedef);
2910b57cec5SDimitry Andric } while (false);
2920b57cec5SDimitry Andric
2930b57cec5SDimitry Andric // if all else fails, go to static type
2940b57cec5SDimitry Andric if (valobj.IsDynamic()) {
2950b57cec5SDimitry Andric lldb::ValueObjectSP static_value_sp(valobj.GetStaticValue());
2960b57cec5SDimitry Andric if (static_value_sp)
2970b57cec5SDimitry Andric GetPossibleMatches(
2980b57cec5SDimitry Andric *static_value_sp.get(), static_value_sp->GetCompilerType(),
2990b57cec5SDimitry Andric use_dynamic, entries, did_strip_ptr, did_strip_ref,
3000b57cec5SDimitry Andric did_strip_typedef, true);
3010b57cec5SDimitry Andric }
3020b57cec5SDimitry Andric }
3030b57cec5SDimitry Andric }
3040b57cec5SDimitry Andric
3050b57cec5SDimitry Andric lldb::TypeFormatImplSP
GetFormatForType(lldb::TypeNameSpecifierImplSP type_sp)3060b57cec5SDimitry Andric FormatManager::GetFormatForType(lldb::TypeNameSpecifierImplSP type_sp) {
3070b57cec5SDimitry Andric if (!type_sp)
3080b57cec5SDimitry Andric return lldb::TypeFormatImplSP();
3090b57cec5SDimitry Andric lldb::TypeFormatImplSP format_chosen_sp;
3100b57cec5SDimitry Andric uint32_t num_categories = m_categories_map.GetCount();
3110b57cec5SDimitry Andric lldb::TypeCategoryImplSP category_sp;
3120b57cec5SDimitry Andric uint32_t prio_category = UINT32_MAX;
3130b57cec5SDimitry Andric for (uint32_t category_id = 0; category_id < num_categories; category_id++) {
3140b57cec5SDimitry Andric category_sp = GetCategoryAtIndex(category_id);
3150b57cec5SDimitry Andric if (!category_sp->IsEnabled())
3160b57cec5SDimitry Andric continue;
3170b57cec5SDimitry Andric lldb::TypeFormatImplSP format_current_sp =
3180b57cec5SDimitry Andric category_sp->GetFormatForType(type_sp);
3190b57cec5SDimitry Andric if (format_current_sp &&
3200b57cec5SDimitry Andric (format_chosen_sp.get() == nullptr ||
3210b57cec5SDimitry Andric (prio_category > category_sp->GetEnabledPosition()))) {
3220b57cec5SDimitry Andric prio_category = category_sp->GetEnabledPosition();
3230b57cec5SDimitry Andric format_chosen_sp = format_current_sp;
3240b57cec5SDimitry Andric }
3250b57cec5SDimitry Andric }
3260b57cec5SDimitry Andric return format_chosen_sp;
3270b57cec5SDimitry Andric }
3280b57cec5SDimitry Andric
3290b57cec5SDimitry Andric lldb::TypeSummaryImplSP
GetSummaryForType(lldb::TypeNameSpecifierImplSP type_sp)3300b57cec5SDimitry Andric FormatManager::GetSummaryForType(lldb::TypeNameSpecifierImplSP type_sp) {
3310b57cec5SDimitry Andric if (!type_sp)
3320b57cec5SDimitry Andric return lldb::TypeSummaryImplSP();
3330b57cec5SDimitry Andric lldb::TypeSummaryImplSP summary_chosen_sp;
3340b57cec5SDimitry Andric uint32_t num_categories = m_categories_map.GetCount();
3350b57cec5SDimitry Andric lldb::TypeCategoryImplSP category_sp;
3360b57cec5SDimitry Andric uint32_t prio_category = UINT32_MAX;
3370b57cec5SDimitry Andric for (uint32_t category_id = 0; category_id < num_categories; category_id++) {
3380b57cec5SDimitry Andric category_sp = GetCategoryAtIndex(category_id);
3390b57cec5SDimitry Andric if (!category_sp->IsEnabled())
3400b57cec5SDimitry Andric continue;
3410b57cec5SDimitry Andric lldb::TypeSummaryImplSP summary_current_sp =
3420b57cec5SDimitry Andric category_sp->GetSummaryForType(type_sp);
3430b57cec5SDimitry Andric if (summary_current_sp &&
3440b57cec5SDimitry Andric (summary_chosen_sp.get() == nullptr ||
3450b57cec5SDimitry Andric (prio_category > category_sp->GetEnabledPosition()))) {
3460b57cec5SDimitry Andric prio_category = category_sp->GetEnabledPosition();
3470b57cec5SDimitry Andric summary_chosen_sp = summary_current_sp;
3480b57cec5SDimitry Andric }
3490b57cec5SDimitry Andric }
3500b57cec5SDimitry Andric return summary_chosen_sp;
3510b57cec5SDimitry Andric }
3520b57cec5SDimitry Andric
3530b57cec5SDimitry Andric lldb::TypeFilterImplSP
GetFilterForType(lldb::TypeNameSpecifierImplSP type_sp)3540b57cec5SDimitry Andric FormatManager::GetFilterForType(lldb::TypeNameSpecifierImplSP type_sp) {
3550b57cec5SDimitry Andric if (!type_sp)
3560b57cec5SDimitry Andric return lldb::TypeFilterImplSP();
3570b57cec5SDimitry Andric lldb::TypeFilterImplSP filter_chosen_sp;
3580b57cec5SDimitry Andric uint32_t num_categories = m_categories_map.GetCount();
3590b57cec5SDimitry Andric lldb::TypeCategoryImplSP category_sp;
3600b57cec5SDimitry Andric uint32_t prio_category = UINT32_MAX;
3610b57cec5SDimitry Andric for (uint32_t category_id = 0; category_id < num_categories; category_id++) {
3620b57cec5SDimitry Andric category_sp = GetCategoryAtIndex(category_id);
3630b57cec5SDimitry Andric if (!category_sp->IsEnabled())
3640b57cec5SDimitry Andric continue;
3650b57cec5SDimitry Andric lldb::TypeFilterImplSP filter_current_sp(
3660b57cec5SDimitry Andric (TypeFilterImpl *)category_sp->GetFilterForType(type_sp).get());
3670b57cec5SDimitry Andric if (filter_current_sp &&
3680b57cec5SDimitry Andric (filter_chosen_sp.get() == nullptr ||
3690b57cec5SDimitry Andric (prio_category > category_sp->GetEnabledPosition()))) {
3700b57cec5SDimitry Andric prio_category = category_sp->GetEnabledPosition();
3710b57cec5SDimitry Andric filter_chosen_sp = filter_current_sp;
3720b57cec5SDimitry Andric }
3730b57cec5SDimitry Andric }
3740b57cec5SDimitry Andric return filter_chosen_sp;
3750b57cec5SDimitry Andric }
3760b57cec5SDimitry Andric
3770b57cec5SDimitry Andric lldb::ScriptedSyntheticChildrenSP
GetSyntheticForType(lldb::TypeNameSpecifierImplSP type_sp)3780b57cec5SDimitry Andric FormatManager::GetSyntheticForType(lldb::TypeNameSpecifierImplSP type_sp) {
3790b57cec5SDimitry Andric if (!type_sp)
3800b57cec5SDimitry Andric return lldb::ScriptedSyntheticChildrenSP();
3810b57cec5SDimitry Andric lldb::ScriptedSyntheticChildrenSP synth_chosen_sp;
3820b57cec5SDimitry Andric uint32_t num_categories = m_categories_map.GetCount();
3830b57cec5SDimitry Andric lldb::TypeCategoryImplSP category_sp;
3840b57cec5SDimitry Andric uint32_t prio_category = UINT32_MAX;
3850b57cec5SDimitry Andric for (uint32_t category_id = 0; category_id < num_categories; category_id++) {
3860b57cec5SDimitry Andric category_sp = GetCategoryAtIndex(category_id);
3870b57cec5SDimitry Andric if (!category_sp->IsEnabled())
3880b57cec5SDimitry Andric continue;
3890b57cec5SDimitry Andric lldb::ScriptedSyntheticChildrenSP synth_current_sp(
3900b57cec5SDimitry Andric (ScriptedSyntheticChildren *)category_sp->GetSyntheticForType(type_sp)
3910b57cec5SDimitry Andric .get());
3920b57cec5SDimitry Andric if (synth_current_sp &&
3930b57cec5SDimitry Andric (synth_chosen_sp.get() == nullptr ||
3940b57cec5SDimitry Andric (prio_category > category_sp->GetEnabledPosition()))) {
3950b57cec5SDimitry Andric prio_category = category_sp->GetEnabledPosition();
3960b57cec5SDimitry Andric synth_chosen_sp = synth_current_sp;
3970b57cec5SDimitry Andric }
3980b57cec5SDimitry Andric }
3990b57cec5SDimitry Andric return synth_chosen_sp;
4000b57cec5SDimitry Andric }
4010b57cec5SDimitry Andric
ForEachCategory(TypeCategoryMap::ForEachCallback callback)4020b57cec5SDimitry Andric void FormatManager::ForEachCategory(TypeCategoryMap::ForEachCallback callback) {
4030b57cec5SDimitry Andric m_categories_map.ForEach(callback);
4040b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_language_categories_mutex);
4050b57cec5SDimitry Andric for (const auto &entry : m_language_categories_map) {
4060b57cec5SDimitry Andric if (auto category_sp = entry.second->GetCategory()) {
4070b57cec5SDimitry Andric if (!callback(category_sp))
4080b57cec5SDimitry Andric break;
4090b57cec5SDimitry Andric }
4100b57cec5SDimitry Andric }
4110b57cec5SDimitry Andric }
4120b57cec5SDimitry Andric
4130b57cec5SDimitry Andric lldb::TypeCategoryImplSP
GetCategory(ConstString category_name,bool can_create)4140b57cec5SDimitry Andric FormatManager::GetCategory(ConstString category_name, bool can_create) {
4150b57cec5SDimitry Andric if (!category_name)
4160b57cec5SDimitry Andric return GetCategory(m_default_category_name);
4170b57cec5SDimitry Andric lldb::TypeCategoryImplSP category;
4180b57cec5SDimitry Andric if (m_categories_map.Get(category_name, category))
4190b57cec5SDimitry Andric return category;
4200b57cec5SDimitry Andric
4210b57cec5SDimitry Andric if (!can_create)
4220b57cec5SDimitry Andric return lldb::TypeCategoryImplSP();
4230b57cec5SDimitry Andric
4240b57cec5SDimitry Andric m_categories_map.Add(
4250b57cec5SDimitry Andric category_name,
4260b57cec5SDimitry Andric lldb::TypeCategoryImplSP(new TypeCategoryImpl(this, category_name)));
4270b57cec5SDimitry Andric return GetCategory(category_name);
4280b57cec5SDimitry Andric }
4290b57cec5SDimitry Andric
GetSingleItemFormat(lldb::Format vector_format)4300b57cec5SDimitry Andric lldb::Format FormatManager::GetSingleItemFormat(lldb::Format vector_format) {
4310b57cec5SDimitry Andric switch (vector_format) {
4320b57cec5SDimitry Andric case eFormatVectorOfChar:
4330b57cec5SDimitry Andric return eFormatCharArray;
4340b57cec5SDimitry Andric
4350b57cec5SDimitry Andric case eFormatVectorOfSInt8:
4360b57cec5SDimitry Andric case eFormatVectorOfSInt16:
4370b57cec5SDimitry Andric case eFormatVectorOfSInt32:
4380b57cec5SDimitry Andric case eFormatVectorOfSInt64:
4390b57cec5SDimitry Andric return eFormatDecimal;
4400b57cec5SDimitry Andric
4410b57cec5SDimitry Andric case eFormatVectorOfUInt8:
4420b57cec5SDimitry Andric case eFormatVectorOfUInt16:
4430b57cec5SDimitry Andric case eFormatVectorOfUInt32:
4440b57cec5SDimitry Andric case eFormatVectorOfUInt64:
4450b57cec5SDimitry Andric case eFormatVectorOfUInt128:
4460b57cec5SDimitry Andric return eFormatHex;
4470b57cec5SDimitry Andric
4480b57cec5SDimitry Andric case eFormatVectorOfFloat16:
4490b57cec5SDimitry Andric case eFormatVectorOfFloat32:
4500b57cec5SDimitry Andric case eFormatVectorOfFloat64:
4510b57cec5SDimitry Andric return eFormatFloat;
4520b57cec5SDimitry Andric
4530b57cec5SDimitry Andric default:
4540b57cec5SDimitry Andric return lldb::eFormatInvalid;
4550b57cec5SDimitry Andric }
4560b57cec5SDimitry Andric }
4570b57cec5SDimitry Andric
ShouldPrintAsOneLiner(ValueObject & valobj)4580b57cec5SDimitry Andric bool FormatManager::ShouldPrintAsOneLiner(ValueObject &valobj) {
4590b57cec5SDimitry Andric // if settings say no oneline whatsoever
4600b57cec5SDimitry Andric if (valobj.GetTargetSP().get() &&
4610b57cec5SDimitry Andric !valobj.GetTargetSP()->GetDebugger().GetAutoOneLineSummaries())
4620b57cec5SDimitry Andric return false; // then don't oneline
4630b57cec5SDimitry Andric
4640b57cec5SDimitry Andric // if this object has a summary, then ask the summary
4650b57cec5SDimitry Andric if (valobj.GetSummaryFormat().get() != nullptr)
4660b57cec5SDimitry Andric return valobj.GetSummaryFormat()->IsOneLiner();
4670b57cec5SDimitry Andric
4680b57cec5SDimitry Andric // no children, no party
4690b57cec5SDimitry Andric if (valobj.GetNumChildren() == 0)
4700b57cec5SDimitry Andric return false;
4710b57cec5SDimitry Andric
4720b57cec5SDimitry Andric // ask the type if it has any opinion about this eLazyBoolCalculate == no
4730b57cec5SDimitry Andric // opinion; other values should be self explanatory
4740b57cec5SDimitry Andric CompilerType compiler_type(valobj.GetCompilerType());
4750b57cec5SDimitry Andric if (compiler_type.IsValid()) {
4760b57cec5SDimitry Andric switch (compiler_type.ShouldPrintAsOneLiner(&valobj)) {
4770b57cec5SDimitry Andric case eLazyBoolNo:
4780b57cec5SDimitry Andric return false;
4790b57cec5SDimitry Andric case eLazyBoolYes:
4800b57cec5SDimitry Andric return true;
4810b57cec5SDimitry Andric case eLazyBoolCalculate:
4820b57cec5SDimitry Andric break;
4830b57cec5SDimitry Andric }
4840b57cec5SDimitry Andric }
4850b57cec5SDimitry Andric
4860b57cec5SDimitry Andric size_t total_children_name_len = 0;
4870b57cec5SDimitry Andric
4880b57cec5SDimitry Andric for (size_t idx = 0; idx < valobj.GetNumChildren(); idx++) {
4890b57cec5SDimitry Andric bool is_synth_val = false;
4900b57cec5SDimitry Andric ValueObjectSP child_sp(valobj.GetChildAtIndex(idx, true));
4910b57cec5SDimitry Andric // something is wrong here - bail out
4920b57cec5SDimitry Andric if (!child_sp)
4930b57cec5SDimitry Andric return false;
4940b57cec5SDimitry Andric
4950b57cec5SDimitry Andric // also ask the child's type if it has any opinion
4960b57cec5SDimitry Andric CompilerType child_compiler_type(child_sp->GetCompilerType());
4970b57cec5SDimitry Andric if (child_compiler_type.IsValid()) {
4980b57cec5SDimitry Andric switch (child_compiler_type.ShouldPrintAsOneLiner(child_sp.get())) {
4990b57cec5SDimitry Andric case eLazyBoolYes:
5000b57cec5SDimitry Andric // an opinion of yes is only binding for the child, so keep going
5010b57cec5SDimitry Andric case eLazyBoolCalculate:
5020b57cec5SDimitry Andric break;
5030b57cec5SDimitry Andric case eLazyBoolNo:
5040b57cec5SDimitry Andric // but if the child says no, then it's a veto on the whole thing
5050b57cec5SDimitry Andric return false;
5060b57cec5SDimitry Andric }
5070b57cec5SDimitry Andric }
5080b57cec5SDimitry Andric
5090b57cec5SDimitry Andric // if we decided to define synthetic children for a type, we probably care
5100b57cec5SDimitry Andric // enough to show them, but avoid nesting children in children
5110b57cec5SDimitry Andric if (child_sp->GetSyntheticChildren().get() != nullptr) {
5120b57cec5SDimitry Andric ValueObjectSP synth_sp(child_sp->GetSyntheticValue());
5130b57cec5SDimitry Andric // wait.. wat? just get out of here..
5140b57cec5SDimitry Andric if (!synth_sp)
5150b57cec5SDimitry Andric return false;
5160b57cec5SDimitry Andric // but if we only have them to provide a value, keep going
5170b57cec5SDimitry Andric if (!synth_sp->MightHaveChildren() &&
5180b57cec5SDimitry Andric synth_sp->DoesProvideSyntheticValue())
5190b57cec5SDimitry Andric is_synth_val = true;
5200b57cec5SDimitry Andric else
5210b57cec5SDimitry Andric return false;
5220b57cec5SDimitry Andric }
5230b57cec5SDimitry Andric
5240b57cec5SDimitry Andric total_children_name_len += child_sp->GetName().GetLength();
5250b57cec5SDimitry Andric
5260b57cec5SDimitry Andric // 50 itself is a "randomly" chosen number - the idea is that
5270b57cec5SDimitry Andric // overly long structs should not get this treatment
5280b57cec5SDimitry Andric // FIXME: maybe make this a user-tweakable setting?
5290b57cec5SDimitry Andric if (total_children_name_len > 50)
5300b57cec5SDimitry Andric return false;
5310b57cec5SDimitry Andric
5320b57cec5SDimitry Andric // if a summary is there..
5330b57cec5SDimitry Andric if (child_sp->GetSummaryFormat()) {
5340b57cec5SDimitry Andric // and it wants children, then bail out
5350b57cec5SDimitry Andric if (child_sp->GetSummaryFormat()->DoesPrintChildren(child_sp.get()))
5360b57cec5SDimitry Andric return false;
5370b57cec5SDimitry Andric }
5380b57cec5SDimitry Andric
5390b57cec5SDimitry Andric // if this child has children..
5400b57cec5SDimitry Andric if (child_sp->GetNumChildren()) {
5410b57cec5SDimitry Andric // ...and no summary...
5420b57cec5SDimitry Andric // (if it had a summary and the summary wanted children, we would have
5430b57cec5SDimitry Andric // bailed out anyway
5440b57cec5SDimitry Andric // so this only makes us bail out if this has no summary and we would
5450b57cec5SDimitry Andric // then print children)
5460b57cec5SDimitry Andric if (!child_sp->GetSummaryFormat() && !is_synth_val) // but again only do
5470b57cec5SDimitry Andric // that if not a
5480b57cec5SDimitry Andric // synthetic valued
5490b57cec5SDimitry Andric // child
5500b57cec5SDimitry Andric return false; // then bail out
5510b57cec5SDimitry Andric }
5520b57cec5SDimitry Andric }
5530b57cec5SDimitry Andric return true;
5540b57cec5SDimitry Andric }
5550b57cec5SDimitry Andric
GetTypeForCache(ValueObject & valobj,lldb::DynamicValueType use_dynamic)5560b57cec5SDimitry Andric ConstString FormatManager::GetTypeForCache(ValueObject &valobj,
5570b57cec5SDimitry Andric lldb::DynamicValueType use_dynamic) {
5580b57cec5SDimitry Andric ValueObjectSP valobj_sp = valobj.GetQualifiedRepresentationIfAvailable(
5590b57cec5SDimitry Andric use_dynamic, valobj.IsSynthetic());
5600b57cec5SDimitry Andric if (valobj_sp && valobj_sp->GetCompilerType().IsValid()) {
5610b57cec5SDimitry Andric if (!valobj_sp->GetCompilerType().IsMeaninglessWithoutDynamicResolution())
5620b57cec5SDimitry Andric return valobj_sp->GetQualifiedTypeName();
5630b57cec5SDimitry Andric }
5640b57cec5SDimitry Andric return ConstString();
5650b57cec5SDimitry Andric }
5660b57cec5SDimitry Andric
5670b57cec5SDimitry Andric std::vector<lldb::LanguageType>
GetCandidateLanguages(lldb::LanguageType lang_type)5680b57cec5SDimitry Andric FormatManager::GetCandidateLanguages(lldb::LanguageType lang_type) {
5690b57cec5SDimitry Andric switch (lang_type) {
5700b57cec5SDimitry Andric case lldb::eLanguageTypeC:
5710b57cec5SDimitry Andric case lldb::eLanguageTypeC89:
5720b57cec5SDimitry Andric case lldb::eLanguageTypeC99:
5730b57cec5SDimitry Andric case lldb::eLanguageTypeC11:
5740b57cec5SDimitry Andric case lldb::eLanguageTypeC_plus_plus:
5750b57cec5SDimitry Andric case lldb::eLanguageTypeC_plus_plus_03:
5760b57cec5SDimitry Andric case lldb::eLanguageTypeC_plus_plus_11:
5770b57cec5SDimitry Andric case lldb::eLanguageTypeC_plus_plus_14:
5780b57cec5SDimitry Andric return {lldb::eLanguageTypeC_plus_plus, lldb::eLanguageTypeObjC};
5790b57cec5SDimitry Andric default:
5800b57cec5SDimitry Andric return {lang_type};
5810b57cec5SDimitry Andric }
582480093f4SDimitry Andric llvm_unreachable("Fully covered switch");
5830b57cec5SDimitry Andric }
5840b57cec5SDimitry Andric
5850b57cec5SDimitry Andric LanguageCategory *
GetCategoryForLanguage(lldb::LanguageType lang_type)5860b57cec5SDimitry Andric FormatManager::GetCategoryForLanguage(lldb::LanguageType lang_type) {
5870b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_language_categories_mutex);
5880b57cec5SDimitry Andric auto iter = m_language_categories_map.find(lang_type),
5890b57cec5SDimitry Andric end = m_language_categories_map.end();
5900b57cec5SDimitry Andric if (iter != end)
5910b57cec5SDimitry Andric return iter->second.get();
5920b57cec5SDimitry Andric LanguageCategory *lang_category = new LanguageCategory(lang_type);
5930b57cec5SDimitry Andric m_language_categories_map[lang_type] =
5940b57cec5SDimitry Andric LanguageCategory::UniquePointer(lang_category);
5950b57cec5SDimitry Andric return lang_category;
5960b57cec5SDimitry Andric }
5970b57cec5SDimitry Andric
598480093f4SDimitry Andric template <typename ImplSP>
GetHardcoded(FormattersMatchData & match_data)599480093f4SDimitry Andric ImplSP FormatManager::GetHardcoded(FormattersMatchData &match_data) {
600480093f4SDimitry Andric ImplSP retval_sp;
6010b57cec5SDimitry Andric for (lldb::LanguageType lang_type : match_data.GetCandidateLanguages()) {
6020b57cec5SDimitry Andric if (LanguageCategory *lang_category = GetCategoryForLanguage(lang_type)) {
6030b57cec5SDimitry Andric if (lang_category->GetHardcoded(*this, match_data, retval_sp))
604480093f4SDimitry Andric return retval_sp;
605480093f4SDimitry Andric }
606480093f4SDimitry Andric }
607480093f4SDimitry Andric return retval_sp;
608480093f4SDimitry Andric }
609480093f4SDimitry Andric
610480093f4SDimitry Andric template <typename ImplSP>
Get(ValueObject & valobj,lldb::DynamicValueType use_dynamic)611480093f4SDimitry Andric ImplSP FormatManager::Get(ValueObject &valobj,
612480093f4SDimitry Andric lldb::DynamicValueType use_dynamic) {
613480093f4SDimitry Andric FormattersMatchData match_data(valobj, use_dynamic);
614480093f4SDimitry Andric if (ImplSP retval_sp = GetCached<ImplSP>(match_data))
615480093f4SDimitry Andric return retval_sp;
616480093f4SDimitry Andric
617480093f4SDimitry Andric Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_DATAFORMATTERS));
618480093f4SDimitry Andric
619480093f4SDimitry Andric LLDB_LOGF(log, "[%s] Search failed. Giving language a chance.", __FUNCTION__);
620480093f4SDimitry Andric for (lldb::LanguageType lang_type : match_data.GetCandidateLanguages()) {
621480093f4SDimitry Andric if (LanguageCategory *lang_category = GetCategoryForLanguage(lang_type)) {
622480093f4SDimitry Andric ImplSP retval_sp;
623480093f4SDimitry Andric if (lang_category->Get(match_data, retval_sp))
624480093f4SDimitry Andric if (retval_sp) {
625480093f4SDimitry Andric LLDB_LOGF(log, "[%s] Language search success. Returning.",
626480093f4SDimitry Andric __FUNCTION__);
627480093f4SDimitry Andric return retval_sp;
628480093f4SDimitry Andric }
6290b57cec5SDimitry Andric }
6300b57cec5SDimitry Andric }
6310b57cec5SDimitry Andric
632480093f4SDimitry Andric LLDB_LOGF(log, "[%s] Search failed. Giving hardcoded a chance.",
633480093f4SDimitry Andric __FUNCTION__);
634480093f4SDimitry Andric return GetHardcoded<ImplSP>(match_data);
635480093f4SDimitry Andric }
636480093f4SDimitry Andric
637480093f4SDimitry Andric template <typename ImplSP>
GetCached(FormattersMatchData & match_data)638480093f4SDimitry Andric ImplSP FormatManager::GetCached(FormattersMatchData &match_data) {
639480093f4SDimitry Andric ImplSP retval_sp;
640480093f4SDimitry Andric Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_DATAFORMATTERS));
641480093f4SDimitry Andric if (match_data.GetTypeForCache()) {
642480093f4SDimitry Andric LLDB_LOGF(log, "\n\n[%s] Looking into cache for type %s", __FUNCTION__,
643480093f4SDimitry Andric match_data.GetTypeForCache().AsCString("<invalid>"));
644480093f4SDimitry Andric if (m_format_cache.Get(match_data.GetTypeForCache(), retval_sp)) {
645480093f4SDimitry Andric if (log) {
646480093f4SDimitry Andric LLDB_LOGF(log, "[%s] Cache search success. Returning.", __FUNCTION__);
647480093f4SDimitry Andric LLDB_LOGV(log, "Cache hits: {0} - Cache Misses: {1}",
648480093f4SDimitry Andric m_format_cache.GetCacheHits(),
649480093f4SDimitry Andric m_format_cache.GetCacheMisses());
650480093f4SDimitry Andric }
651480093f4SDimitry Andric return retval_sp;
652480093f4SDimitry Andric }
653480093f4SDimitry Andric LLDB_LOGF(log, "[%s] Cache search failed. Going normal route",
654480093f4SDimitry Andric __FUNCTION__);
655480093f4SDimitry Andric }
656480093f4SDimitry Andric
657480093f4SDimitry Andric m_categories_map.Get(match_data, retval_sp);
658480093f4SDimitry Andric if (match_data.GetTypeForCache() && (!retval_sp || !retval_sp->NonCacheable())) {
659480093f4SDimitry Andric LLDB_LOGF(log, "[%s] Caching %p for type %s", __FUNCTION__,
660480093f4SDimitry Andric static_cast<void *>(retval_sp.get()),
661480093f4SDimitry Andric match_data.GetTypeForCache().AsCString("<invalid>"));
662480093f4SDimitry Andric m_format_cache.Set(match_data.GetTypeForCache(), retval_sp);
663480093f4SDimitry Andric }
664480093f4SDimitry Andric LLDB_LOGV(log, "Cache hits: {0} - Cache Misses: {1}",
665480093f4SDimitry Andric m_format_cache.GetCacheHits(), m_format_cache.GetCacheMisses());
6660b57cec5SDimitry Andric return retval_sp;
6670b57cec5SDimitry Andric }
6680b57cec5SDimitry Andric
6690b57cec5SDimitry Andric lldb::TypeFormatImplSP
GetFormat(ValueObject & valobj,lldb::DynamicValueType use_dynamic)6700b57cec5SDimitry Andric FormatManager::GetFormat(ValueObject &valobj,
6710b57cec5SDimitry Andric lldb::DynamicValueType use_dynamic) {
672480093f4SDimitry Andric return Get<lldb::TypeFormatImplSP>(valobj, use_dynamic);
6730b57cec5SDimitry Andric }
6740b57cec5SDimitry Andric
6750b57cec5SDimitry Andric lldb::TypeSummaryImplSP
GetSummaryFormat(ValueObject & valobj,lldb::DynamicValueType use_dynamic)6760b57cec5SDimitry Andric FormatManager::GetSummaryFormat(ValueObject &valobj,
6770b57cec5SDimitry Andric lldb::DynamicValueType use_dynamic) {
678480093f4SDimitry Andric return Get<lldb::TypeSummaryImplSP>(valobj, use_dynamic);
6790b57cec5SDimitry Andric }
6800b57cec5SDimitry Andric
6810b57cec5SDimitry Andric lldb::SyntheticChildrenSP
GetSyntheticChildren(ValueObject & valobj,lldb::DynamicValueType use_dynamic)6820b57cec5SDimitry Andric FormatManager::GetSyntheticChildren(ValueObject &valobj,
6830b57cec5SDimitry Andric lldb::DynamicValueType use_dynamic) {
684480093f4SDimitry Andric return Get<lldb::SyntheticChildrenSP>(valobj, use_dynamic);
6850b57cec5SDimitry Andric }
6860b57cec5SDimitry Andric
FormatManager()6870b57cec5SDimitry Andric FormatManager::FormatManager()
6880b57cec5SDimitry Andric : m_last_revision(0), m_format_cache(), m_language_categories_mutex(),
6890b57cec5SDimitry Andric m_language_categories_map(), m_named_summaries_map(this),
6900b57cec5SDimitry Andric m_categories_map(this), m_default_category_name(ConstString("default")),
6910b57cec5SDimitry Andric m_system_category_name(ConstString("system")),
6920b57cec5SDimitry Andric m_vectortypes_category_name(ConstString("VectorTypes")) {
6930b57cec5SDimitry Andric LoadSystemFormatters();
6940b57cec5SDimitry Andric LoadVectorFormatters();
6950b57cec5SDimitry Andric
6960b57cec5SDimitry Andric EnableCategory(m_vectortypes_category_name, TypeCategoryMap::Last,
6970b57cec5SDimitry Andric lldb::eLanguageTypeObjC_plus_plus);
6980b57cec5SDimitry Andric EnableCategory(m_system_category_name, TypeCategoryMap::Last,
6990b57cec5SDimitry Andric lldb::eLanguageTypeObjC_plus_plus);
7000b57cec5SDimitry Andric }
7010b57cec5SDimitry Andric
LoadSystemFormatters()7020b57cec5SDimitry Andric void FormatManager::LoadSystemFormatters() {
7030b57cec5SDimitry Andric TypeSummaryImpl::Flags string_flags;
7040b57cec5SDimitry Andric string_flags.SetCascades(true)
7050b57cec5SDimitry Andric .SetSkipPointers(true)
7060b57cec5SDimitry Andric .SetSkipReferences(false)
7070b57cec5SDimitry Andric .SetDontShowChildren(true)
7080b57cec5SDimitry Andric .SetDontShowValue(false)
7090b57cec5SDimitry Andric .SetShowMembersOneLiner(false)
7100b57cec5SDimitry Andric .SetHideItemNames(false);
7110b57cec5SDimitry Andric
7120b57cec5SDimitry Andric TypeSummaryImpl::Flags string_array_flags;
7130b57cec5SDimitry Andric string_array_flags.SetCascades(true)
7140b57cec5SDimitry Andric .SetSkipPointers(true)
7150b57cec5SDimitry Andric .SetSkipReferences(false)
7160b57cec5SDimitry Andric .SetDontShowChildren(true)
7170b57cec5SDimitry Andric .SetDontShowValue(true)
7180b57cec5SDimitry Andric .SetShowMembersOneLiner(false)
7190b57cec5SDimitry Andric .SetHideItemNames(false);
7200b57cec5SDimitry Andric
7210b57cec5SDimitry Andric lldb::TypeSummaryImplSP string_format(
7220b57cec5SDimitry Andric new StringSummaryFormat(string_flags, "${var%s}"));
7230b57cec5SDimitry Andric
7240b57cec5SDimitry Andric lldb::TypeSummaryImplSP string_array_format(
7250b57cec5SDimitry Andric new StringSummaryFormat(string_array_flags, "${var%s}"));
7260b57cec5SDimitry Andric
7279dba64beSDimitry Andric RegularExpression any_size_char_arr(llvm::StringRef("char \\[[0-9]+\\]"));
7280b57cec5SDimitry Andric
7290b57cec5SDimitry Andric TypeCategoryImpl::SharedPointer sys_category_sp =
7300b57cec5SDimitry Andric GetCategory(m_system_category_name);
7310b57cec5SDimitry Andric
7320b57cec5SDimitry Andric sys_category_sp->GetTypeSummariesContainer()->Add(ConstString("char *"),
7330b57cec5SDimitry Andric string_format);
7340b57cec5SDimitry Andric sys_category_sp->GetTypeSummariesContainer()->Add(
7350b57cec5SDimitry Andric ConstString("unsigned char *"), string_format);
7369dba64beSDimitry Andric sys_category_sp->GetRegexTypeSummariesContainer()->Add(
7379dba64beSDimitry Andric std::move(any_size_char_arr), string_array_format);
7380b57cec5SDimitry Andric
7390b57cec5SDimitry Andric lldb::TypeSummaryImplSP ostype_summary(
7400b57cec5SDimitry Andric new StringSummaryFormat(TypeSummaryImpl::Flags()
7410b57cec5SDimitry Andric .SetCascades(false)
7420b57cec5SDimitry Andric .SetSkipPointers(true)
7430b57cec5SDimitry Andric .SetSkipReferences(true)
7440b57cec5SDimitry Andric .SetDontShowChildren(true)
7450b57cec5SDimitry Andric .SetDontShowValue(false)
7460b57cec5SDimitry Andric .SetShowMembersOneLiner(false)
7470b57cec5SDimitry Andric .SetHideItemNames(false),
7480b57cec5SDimitry Andric "${var%O}"));
7490b57cec5SDimitry Andric
7500b57cec5SDimitry Andric sys_category_sp->GetTypeSummariesContainer()->Add(ConstString("OSType"),
7510b57cec5SDimitry Andric ostype_summary);
7520b57cec5SDimitry Andric
7530b57cec5SDimitry Andric TypeFormatImpl::Flags fourchar_flags;
7540b57cec5SDimitry Andric fourchar_flags.SetCascades(true).SetSkipPointers(true).SetSkipReferences(
7550b57cec5SDimitry Andric true);
7560b57cec5SDimitry Andric
7570b57cec5SDimitry Andric AddFormat(sys_category_sp, lldb::eFormatOSType, ConstString("FourCharCode"),
7580b57cec5SDimitry Andric fourchar_flags);
7590b57cec5SDimitry Andric }
7600b57cec5SDimitry Andric
LoadVectorFormatters()7610b57cec5SDimitry Andric void FormatManager::LoadVectorFormatters() {
7620b57cec5SDimitry Andric TypeCategoryImpl::SharedPointer vectors_category_sp =
7630b57cec5SDimitry Andric GetCategory(m_vectortypes_category_name);
7640b57cec5SDimitry Andric
7650b57cec5SDimitry Andric TypeSummaryImpl::Flags vector_flags;
7660b57cec5SDimitry Andric vector_flags.SetCascades(true)
7670b57cec5SDimitry Andric .SetSkipPointers(true)
7680b57cec5SDimitry Andric .SetSkipReferences(false)
7690b57cec5SDimitry Andric .SetDontShowChildren(true)
7700b57cec5SDimitry Andric .SetDontShowValue(false)
7710b57cec5SDimitry Andric .SetShowMembersOneLiner(true)
7720b57cec5SDimitry Andric .SetHideItemNames(true);
7730b57cec5SDimitry Andric
7740b57cec5SDimitry Andric AddStringSummary(vectors_category_sp, "${var.uint128}",
7750b57cec5SDimitry Andric ConstString("builtin_type_vec128"), vector_flags);
7760b57cec5SDimitry Andric
7770b57cec5SDimitry Andric AddStringSummary(vectors_category_sp, "", ConstString("float [4]"),
7780b57cec5SDimitry Andric vector_flags);
7790b57cec5SDimitry Andric AddStringSummary(vectors_category_sp, "", ConstString("int32_t [4]"),
7800b57cec5SDimitry Andric vector_flags);
7810b57cec5SDimitry Andric AddStringSummary(vectors_category_sp, "", ConstString("int16_t [8]"),
7820b57cec5SDimitry Andric vector_flags);
7830b57cec5SDimitry Andric AddStringSummary(vectors_category_sp, "", ConstString("vDouble"),
7840b57cec5SDimitry Andric vector_flags);
7850b57cec5SDimitry Andric AddStringSummary(vectors_category_sp, "", ConstString("vFloat"),
7860b57cec5SDimitry Andric vector_flags);
7870b57cec5SDimitry Andric AddStringSummary(vectors_category_sp, "", ConstString("vSInt8"),
7880b57cec5SDimitry Andric vector_flags);
7890b57cec5SDimitry Andric AddStringSummary(vectors_category_sp, "", ConstString("vSInt16"),
7900b57cec5SDimitry Andric vector_flags);
7910b57cec5SDimitry Andric AddStringSummary(vectors_category_sp, "", ConstString("vSInt32"),
7920b57cec5SDimitry Andric vector_flags);
7930b57cec5SDimitry Andric AddStringSummary(vectors_category_sp, "", ConstString("vUInt16"),
7940b57cec5SDimitry Andric vector_flags);
7950b57cec5SDimitry Andric AddStringSummary(vectors_category_sp, "", ConstString("vUInt8"),
7960b57cec5SDimitry Andric vector_flags);
7970b57cec5SDimitry Andric AddStringSummary(vectors_category_sp, "", ConstString("vUInt16"),
7980b57cec5SDimitry Andric vector_flags);
7990b57cec5SDimitry Andric AddStringSummary(vectors_category_sp, "", ConstString("vUInt32"),
8000b57cec5SDimitry Andric vector_flags);
8010b57cec5SDimitry Andric AddStringSummary(vectors_category_sp, "", ConstString("vBool32"),
8020b57cec5SDimitry Andric vector_flags);
8030b57cec5SDimitry Andric }
804