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 "lldb/Core/Debugger.h"
120b57cec5SDimitry Andric #include "lldb/DataFormatters/FormattersHelpers.h"
130b57cec5SDimitry Andric #include "lldb/DataFormatters/LanguageCategory.h"
14bdd1243dSDimitry Andric #include "lldb/Interpreter/ScriptInterpreter.h"
150b57cec5SDimitry Andric #include "lldb/Target/ExecutionContext.h"
160b57cec5SDimitry Andric #include "lldb/Target/Language.h"
1781ad6265SDimitry Andric #include "lldb/Utility/LLDBLog.h"
180b57cec5SDimitry Andric #include "lldb/Utility/Log.h"
1981ad6265SDimitry Andric #include "llvm/ADT/STLExtras.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 
80bdd1243dSDimitry Andric static uint32_t g_num_format_infos = std::size(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(llvm::StringRef format_name,bool partial_match_ok,Format & format)9381ad6265SDimitry Andric static bool GetFormatFromFormatName(llvm::StringRef 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) {
9781ad6265SDimitry Andric     if (format_name.equals_insensitive(g_format_infos[i].format_name)) {
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) {
10581ad6265SDimitry Andric       if (llvm::StringRef(g_format_infos[i].format_name)
106*fe013be4SDimitry Andric               .starts_with_insensitive(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,FormattersMatchCandidate::Flags current_flags,bool root_level)1760b57cec5SDimitry Andric void FormatManager::GetPossibleMatches(
1775ffd83dbSDimitry Andric     ValueObject &valobj, CompilerType compiler_type,
1780b57cec5SDimitry Andric     lldb::DynamicValueType use_dynamic, FormattersMatchVector &entries,
179bdd1243dSDimitry Andric     FormattersMatchCandidate::Flags current_flags, bool root_level) {
1800b57cec5SDimitry Andric   compiler_type = compiler_type.GetTypeForFormatters();
1815ffd83dbSDimitry Andric   ConstString type_name(compiler_type.GetTypeName());
182bdd1243dSDimitry Andric   ScriptInterpreter *script_interpreter =
183bdd1243dSDimitry Andric       valobj.GetTargetSP()->GetDebugger().GetScriptInterpreter();
1840b57cec5SDimitry Andric   if (valobj.GetBitfieldBitSize() > 0) {
1850b57cec5SDimitry Andric     StreamString sstring;
1860b57cec5SDimitry Andric     sstring.Printf("%s:%d", type_name.AsCString(), valobj.GetBitfieldBitSize());
1870b57cec5SDimitry Andric     ConstString bitfieldname(sstring.GetString());
188bdd1243dSDimitry Andric     entries.push_back({bitfieldname, script_interpreter,
189bdd1243dSDimitry Andric                        TypeImpl(compiler_type), current_flags});
1900b57cec5SDimitry Andric   }
1910b57cec5SDimitry Andric 
1920b57cec5SDimitry Andric   if (!compiler_type.IsMeaninglessWithoutDynamicResolution()) {
193bdd1243dSDimitry Andric     entries.push_back({type_name, script_interpreter, TypeImpl(compiler_type),
194bdd1243dSDimitry Andric                        current_flags});
1950b57cec5SDimitry Andric 
1965ffd83dbSDimitry Andric     ConstString display_type_name(compiler_type.GetTypeName());
1970b57cec5SDimitry Andric     if (display_type_name != type_name)
198bdd1243dSDimitry Andric       entries.push_back({display_type_name, script_interpreter,
199bdd1243dSDimitry Andric                          TypeImpl(compiler_type), current_flags});
2000b57cec5SDimitry Andric   }
2010b57cec5SDimitry Andric 
2020b57cec5SDimitry Andric   for (bool is_rvalue_ref = true, j = true;
2030b57cec5SDimitry Andric        j && compiler_type.IsReferenceType(nullptr, &is_rvalue_ref); j = false) {
2040b57cec5SDimitry Andric     CompilerType non_ref_type = compiler_type.GetNonReferenceType();
205bdd1243dSDimitry Andric     GetPossibleMatches(valobj, non_ref_type, use_dynamic, entries,
206bdd1243dSDimitry Andric                        current_flags.WithStrippedReference());
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();
212bdd1243dSDimitry Andric       // this is not exactly the usual meaning of stripping typedefs
2130b57cec5SDimitry Andric       GetPossibleMatches(
2140b57cec5SDimitry Andric           valobj, deffed_referenced_type,
215bdd1243dSDimitry Andric           use_dynamic, entries, current_flags.WithStrippedTypedef());
2160b57cec5SDimitry Andric     }
2170b57cec5SDimitry Andric   }
2180b57cec5SDimitry Andric 
2190b57cec5SDimitry Andric   if (compiler_type.IsPointerType()) {
2200b57cec5SDimitry Andric     CompilerType non_ptr_type = compiler_type.GetPointeeType();
221bdd1243dSDimitry Andric     GetPossibleMatches(valobj, non_ptr_type, use_dynamic, entries,
222bdd1243dSDimitry Andric                        current_flags.WithStrippedPointer());
2230b57cec5SDimitry Andric     if (non_ptr_type.IsTypedefType()) {
2240b57cec5SDimitry Andric       CompilerType deffed_pointed_type =
2250b57cec5SDimitry Andric           non_ptr_type.GetTypedefedType().GetPointerType();
226bdd1243dSDimitry Andric       // this is not exactly the usual meaning of stripping typedefs
227bdd1243dSDimitry Andric       GetPossibleMatches(valobj, deffed_pointed_type, use_dynamic, entries,
228bdd1243dSDimitry Andric                          current_flags.WithStrippedTypedef());
2290b57cec5SDimitry Andric     }
2300b57cec5SDimitry Andric   }
2310b57cec5SDimitry Andric 
232480093f4SDimitry Andric   // For arrays with typedef-ed elements, we add a candidate with the typedef
233480093f4SDimitry Andric   // stripped.
234480093f4SDimitry Andric   uint64_t array_size;
235480093f4SDimitry Andric   if (compiler_type.IsArrayType(nullptr, &array_size, nullptr)) {
236e8d8bef9SDimitry Andric     ExecutionContext exe_ctx(valobj.GetExecutionContextRef());
237e8d8bef9SDimitry Andric     CompilerType element_type = compiler_type.GetArrayElementType(
238e8d8bef9SDimitry Andric         exe_ctx.GetBestExecutionContextScope());
239480093f4SDimitry Andric     if (element_type.IsTypedefType()) {
240480093f4SDimitry Andric       // Get the stripped element type and compute the stripped array type
241480093f4SDimitry Andric       // from it.
242480093f4SDimitry Andric       CompilerType deffed_array_type =
243480093f4SDimitry Andric           element_type.GetTypedefedType().GetArrayType(array_size);
244bdd1243dSDimitry Andric       // this is not exactly the usual meaning of stripping typedefs
245480093f4SDimitry Andric       GetPossibleMatches(
246480093f4SDimitry Andric           valobj, deffed_array_type,
247bdd1243dSDimitry Andric           use_dynamic, entries, current_flags.WithStrippedTypedef());
248480093f4SDimitry Andric     }
249480093f4SDimitry Andric   }
250480093f4SDimitry Andric 
251480093f4SDimitry Andric   for (lldb::LanguageType language_type :
252480093f4SDimitry Andric        GetCandidateLanguages(valobj.GetObjectRuntimeLanguage())) {
2530b57cec5SDimitry Andric     if (Language *language = Language::FindPlugin(language_type)) {
254bdd1243dSDimitry Andric       for (const FormattersMatchCandidate& candidate :
2550b57cec5SDimitry Andric            language->GetPossibleFormattersMatches(valobj, use_dynamic)) {
256bdd1243dSDimitry Andric         entries.push_back(candidate);
2570b57cec5SDimitry Andric       }
2580b57cec5SDimitry Andric     }
2590b57cec5SDimitry Andric   }
2600b57cec5SDimitry Andric 
2610b57cec5SDimitry Andric   // try to strip typedef chains
2620b57cec5SDimitry Andric   if (compiler_type.IsTypedefType()) {
2630b57cec5SDimitry Andric     CompilerType deffed_type = compiler_type.GetTypedefedType();
264bdd1243dSDimitry Andric     GetPossibleMatches(valobj, deffed_type, use_dynamic, entries,
265bdd1243dSDimitry Andric                        current_flags.WithStrippedTypedef());
2660b57cec5SDimitry Andric   }
2670b57cec5SDimitry Andric 
2680b57cec5SDimitry Andric   if (root_level) {
2690b57cec5SDimitry Andric     do {
2700b57cec5SDimitry Andric       if (!compiler_type.IsValid())
2710b57cec5SDimitry Andric         break;
2720b57cec5SDimitry Andric 
2730b57cec5SDimitry Andric       CompilerType unqual_compiler_ast_type =
2740b57cec5SDimitry Andric           compiler_type.GetFullyUnqualifiedType();
2750b57cec5SDimitry Andric       if (!unqual_compiler_ast_type.IsValid())
2760b57cec5SDimitry Andric         break;
2770b57cec5SDimitry Andric       if (unqual_compiler_ast_type.GetOpaqueQualType() !=
2780b57cec5SDimitry Andric           compiler_type.GetOpaqueQualType())
279bdd1243dSDimitry Andric         GetPossibleMatches(valobj, unqual_compiler_ast_type, use_dynamic,
280bdd1243dSDimitry Andric                            entries, current_flags);
2810b57cec5SDimitry Andric     } while (false);
2820b57cec5SDimitry Andric 
2830b57cec5SDimitry Andric     // if all else fails, go to static type
2840b57cec5SDimitry Andric     if (valobj.IsDynamic()) {
2850b57cec5SDimitry Andric       lldb::ValueObjectSP static_value_sp(valobj.GetStaticValue());
2860b57cec5SDimitry Andric       if (static_value_sp)
287bdd1243dSDimitry Andric         GetPossibleMatches(*static_value_sp.get(),
288bdd1243dSDimitry Andric                            static_value_sp->GetCompilerType(), use_dynamic,
289bdd1243dSDimitry Andric                            entries, current_flags, true);
2900b57cec5SDimitry Andric     }
2910b57cec5SDimitry Andric   }
2920b57cec5SDimitry Andric }
2930b57cec5SDimitry Andric 
2940b57cec5SDimitry Andric lldb::TypeFormatImplSP
GetFormatForType(lldb::TypeNameSpecifierImplSP type_sp)2950b57cec5SDimitry Andric FormatManager::GetFormatForType(lldb::TypeNameSpecifierImplSP type_sp) {
2960b57cec5SDimitry Andric   if (!type_sp)
2970b57cec5SDimitry Andric     return lldb::TypeFormatImplSP();
2980b57cec5SDimitry Andric   lldb::TypeFormatImplSP format_chosen_sp;
2990b57cec5SDimitry Andric   uint32_t num_categories = m_categories_map.GetCount();
3000b57cec5SDimitry Andric   lldb::TypeCategoryImplSP category_sp;
3010b57cec5SDimitry Andric   uint32_t prio_category = UINT32_MAX;
3020b57cec5SDimitry Andric   for (uint32_t category_id = 0; category_id < num_categories; category_id++) {
3030b57cec5SDimitry Andric     category_sp = GetCategoryAtIndex(category_id);
3040b57cec5SDimitry Andric     if (!category_sp->IsEnabled())
3050b57cec5SDimitry Andric       continue;
3060b57cec5SDimitry Andric     lldb::TypeFormatImplSP format_current_sp =
3070b57cec5SDimitry Andric         category_sp->GetFormatForType(type_sp);
3080b57cec5SDimitry Andric     if (format_current_sp &&
3090b57cec5SDimitry Andric         (format_chosen_sp.get() == nullptr ||
3100b57cec5SDimitry Andric          (prio_category > category_sp->GetEnabledPosition()))) {
3110b57cec5SDimitry Andric       prio_category = category_sp->GetEnabledPosition();
3120b57cec5SDimitry Andric       format_chosen_sp = format_current_sp;
3130b57cec5SDimitry Andric     }
3140b57cec5SDimitry Andric   }
3150b57cec5SDimitry Andric   return format_chosen_sp;
3160b57cec5SDimitry Andric }
3170b57cec5SDimitry Andric 
3180b57cec5SDimitry Andric lldb::TypeSummaryImplSP
GetSummaryForType(lldb::TypeNameSpecifierImplSP type_sp)3190b57cec5SDimitry Andric FormatManager::GetSummaryForType(lldb::TypeNameSpecifierImplSP type_sp) {
3200b57cec5SDimitry Andric   if (!type_sp)
3210b57cec5SDimitry Andric     return lldb::TypeSummaryImplSP();
3220b57cec5SDimitry Andric   lldb::TypeSummaryImplSP summary_chosen_sp;
3230b57cec5SDimitry Andric   uint32_t num_categories = m_categories_map.GetCount();
3240b57cec5SDimitry Andric   lldb::TypeCategoryImplSP category_sp;
3250b57cec5SDimitry Andric   uint32_t prio_category = UINT32_MAX;
3260b57cec5SDimitry Andric   for (uint32_t category_id = 0; category_id < num_categories; category_id++) {
3270b57cec5SDimitry Andric     category_sp = GetCategoryAtIndex(category_id);
3280b57cec5SDimitry Andric     if (!category_sp->IsEnabled())
3290b57cec5SDimitry Andric       continue;
3300b57cec5SDimitry Andric     lldb::TypeSummaryImplSP summary_current_sp =
3310b57cec5SDimitry Andric         category_sp->GetSummaryForType(type_sp);
3320b57cec5SDimitry Andric     if (summary_current_sp &&
3330b57cec5SDimitry Andric         (summary_chosen_sp.get() == nullptr ||
3340b57cec5SDimitry Andric          (prio_category > category_sp->GetEnabledPosition()))) {
3350b57cec5SDimitry Andric       prio_category = category_sp->GetEnabledPosition();
3360b57cec5SDimitry Andric       summary_chosen_sp = summary_current_sp;
3370b57cec5SDimitry Andric     }
3380b57cec5SDimitry Andric   }
3390b57cec5SDimitry Andric   return summary_chosen_sp;
3400b57cec5SDimitry Andric }
3410b57cec5SDimitry Andric 
3420b57cec5SDimitry Andric lldb::TypeFilterImplSP
GetFilterForType(lldb::TypeNameSpecifierImplSP type_sp)3430b57cec5SDimitry Andric FormatManager::GetFilterForType(lldb::TypeNameSpecifierImplSP type_sp) {
3440b57cec5SDimitry Andric   if (!type_sp)
3450b57cec5SDimitry Andric     return lldb::TypeFilterImplSP();
3460b57cec5SDimitry Andric   lldb::TypeFilterImplSP filter_chosen_sp;
3470b57cec5SDimitry Andric   uint32_t num_categories = m_categories_map.GetCount();
3480b57cec5SDimitry Andric   lldb::TypeCategoryImplSP category_sp;
3490b57cec5SDimitry Andric   uint32_t prio_category = UINT32_MAX;
3500b57cec5SDimitry Andric   for (uint32_t category_id = 0; category_id < num_categories; category_id++) {
3510b57cec5SDimitry Andric     category_sp = GetCategoryAtIndex(category_id);
3520b57cec5SDimitry Andric     if (!category_sp->IsEnabled())
3530b57cec5SDimitry Andric       continue;
3540b57cec5SDimitry Andric     lldb::TypeFilterImplSP filter_current_sp(
3550b57cec5SDimitry Andric         (TypeFilterImpl *)category_sp->GetFilterForType(type_sp).get());
3560b57cec5SDimitry Andric     if (filter_current_sp &&
3570b57cec5SDimitry Andric         (filter_chosen_sp.get() == nullptr ||
3580b57cec5SDimitry Andric          (prio_category > category_sp->GetEnabledPosition()))) {
3590b57cec5SDimitry Andric       prio_category = category_sp->GetEnabledPosition();
3600b57cec5SDimitry Andric       filter_chosen_sp = filter_current_sp;
3610b57cec5SDimitry Andric     }
3620b57cec5SDimitry Andric   }
3630b57cec5SDimitry Andric   return filter_chosen_sp;
3640b57cec5SDimitry Andric }
3650b57cec5SDimitry Andric 
3660b57cec5SDimitry Andric lldb::ScriptedSyntheticChildrenSP
GetSyntheticForType(lldb::TypeNameSpecifierImplSP type_sp)3670b57cec5SDimitry Andric FormatManager::GetSyntheticForType(lldb::TypeNameSpecifierImplSP type_sp) {
3680b57cec5SDimitry Andric   if (!type_sp)
3690b57cec5SDimitry Andric     return lldb::ScriptedSyntheticChildrenSP();
3700b57cec5SDimitry Andric   lldb::ScriptedSyntheticChildrenSP synth_chosen_sp;
3710b57cec5SDimitry Andric   uint32_t num_categories = m_categories_map.GetCount();
3720b57cec5SDimitry Andric   lldb::TypeCategoryImplSP category_sp;
3730b57cec5SDimitry Andric   uint32_t prio_category = UINT32_MAX;
3740b57cec5SDimitry Andric   for (uint32_t category_id = 0; category_id < num_categories; category_id++) {
3750b57cec5SDimitry Andric     category_sp = GetCategoryAtIndex(category_id);
3760b57cec5SDimitry Andric     if (!category_sp->IsEnabled())
3770b57cec5SDimitry Andric       continue;
3780b57cec5SDimitry Andric     lldb::ScriptedSyntheticChildrenSP synth_current_sp(
3790b57cec5SDimitry Andric         (ScriptedSyntheticChildren *)category_sp->GetSyntheticForType(type_sp)
3800b57cec5SDimitry Andric             .get());
3810b57cec5SDimitry Andric     if (synth_current_sp &&
3820b57cec5SDimitry Andric         (synth_chosen_sp.get() == nullptr ||
3830b57cec5SDimitry Andric          (prio_category > category_sp->GetEnabledPosition()))) {
3840b57cec5SDimitry Andric       prio_category = category_sp->GetEnabledPosition();
3850b57cec5SDimitry Andric       synth_chosen_sp = synth_current_sp;
3860b57cec5SDimitry Andric     }
3870b57cec5SDimitry Andric   }
3880b57cec5SDimitry Andric   return synth_chosen_sp;
3890b57cec5SDimitry Andric }
3900b57cec5SDimitry Andric 
ForEachCategory(TypeCategoryMap::ForEachCallback callback)3910b57cec5SDimitry Andric void FormatManager::ForEachCategory(TypeCategoryMap::ForEachCallback callback) {
3920b57cec5SDimitry Andric   m_categories_map.ForEach(callback);
3930b57cec5SDimitry Andric   std::lock_guard<std::recursive_mutex> guard(m_language_categories_mutex);
3940b57cec5SDimitry Andric   for (const auto &entry : m_language_categories_map) {
3950b57cec5SDimitry Andric     if (auto category_sp = entry.second->GetCategory()) {
3960b57cec5SDimitry Andric       if (!callback(category_sp))
3970b57cec5SDimitry Andric         break;
3980b57cec5SDimitry Andric     }
3990b57cec5SDimitry Andric   }
4000b57cec5SDimitry Andric }
4010b57cec5SDimitry Andric 
4020b57cec5SDimitry Andric lldb::TypeCategoryImplSP
GetCategory(ConstString category_name,bool can_create)4030b57cec5SDimitry Andric FormatManager::GetCategory(ConstString category_name, bool can_create) {
4040b57cec5SDimitry Andric   if (!category_name)
4050b57cec5SDimitry Andric     return GetCategory(m_default_category_name);
4060b57cec5SDimitry Andric   lldb::TypeCategoryImplSP category;
4070b57cec5SDimitry Andric   if (m_categories_map.Get(category_name, category))
4080b57cec5SDimitry Andric     return category;
4090b57cec5SDimitry Andric 
4100b57cec5SDimitry Andric   if (!can_create)
4110b57cec5SDimitry Andric     return lldb::TypeCategoryImplSP();
4120b57cec5SDimitry Andric 
4130b57cec5SDimitry Andric   m_categories_map.Add(
4140b57cec5SDimitry Andric       category_name,
4150b57cec5SDimitry Andric       lldb::TypeCategoryImplSP(new TypeCategoryImpl(this, category_name)));
4160b57cec5SDimitry Andric   return GetCategory(category_name);
4170b57cec5SDimitry Andric }
4180b57cec5SDimitry Andric 
GetSingleItemFormat(lldb::Format vector_format)4190b57cec5SDimitry Andric lldb::Format FormatManager::GetSingleItemFormat(lldb::Format vector_format) {
4200b57cec5SDimitry Andric   switch (vector_format) {
4210b57cec5SDimitry Andric   case eFormatVectorOfChar:
4220b57cec5SDimitry Andric     return eFormatCharArray;
4230b57cec5SDimitry Andric 
4240b57cec5SDimitry Andric   case eFormatVectorOfSInt8:
4250b57cec5SDimitry Andric   case eFormatVectorOfSInt16:
4260b57cec5SDimitry Andric   case eFormatVectorOfSInt32:
4270b57cec5SDimitry Andric   case eFormatVectorOfSInt64:
4280b57cec5SDimitry Andric     return eFormatDecimal;
4290b57cec5SDimitry Andric 
4300b57cec5SDimitry Andric   case eFormatVectorOfUInt8:
4310b57cec5SDimitry Andric   case eFormatVectorOfUInt16:
4320b57cec5SDimitry Andric   case eFormatVectorOfUInt32:
4330b57cec5SDimitry Andric   case eFormatVectorOfUInt64:
4340b57cec5SDimitry Andric   case eFormatVectorOfUInt128:
4350b57cec5SDimitry Andric     return eFormatHex;
4360b57cec5SDimitry Andric 
4370b57cec5SDimitry Andric   case eFormatVectorOfFloat16:
4380b57cec5SDimitry Andric   case eFormatVectorOfFloat32:
4390b57cec5SDimitry Andric   case eFormatVectorOfFloat64:
4400b57cec5SDimitry Andric     return eFormatFloat;
4410b57cec5SDimitry Andric 
4420b57cec5SDimitry Andric   default:
4430b57cec5SDimitry Andric     return lldb::eFormatInvalid;
4440b57cec5SDimitry Andric   }
4450b57cec5SDimitry Andric }
4460b57cec5SDimitry Andric 
ShouldPrintAsOneLiner(ValueObject & valobj)4470b57cec5SDimitry Andric bool FormatManager::ShouldPrintAsOneLiner(ValueObject &valobj) {
4480b57cec5SDimitry Andric   // if settings say no oneline whatsoever
4490b57cec5SDimitry Andric   if (valobj.GetTargetSP().get() &&
4500b57cec5SDimitry Andric       !valobj.GetTargetSP()->GetDebugger().GetAutoOneLineSummaries())
4510b57cec5SDimitry Andric     return false; // then don't oneline
4520b57cec5SDimitry Andric 
4530b57cec5SDimitry Andric   // if this object has a summary, then ask the summary
4540b57cec5SDimitry Andric   if (valobj.GetSummaryFormat().get() != nullptr)
4550b57cec5SDimitry Andric     return valobj.GetSummaryFormat()->IsOneLiner();
4560b57cec5SDimitry Andric 
4570b57cec5SDimitry Andric   // no children, no party
4580b57cec5SDimitry Andric   if (valobj.GetNumChildren() == 0)
4590b57cec5SDimitry Andric     return false;
4600b57cec5SDimitry Andric 
4610b57cec5SDimitry Andric   // ask the type if it has any opinion about this eLazyBoolCalculate == no
4620b57cec5SDimitry Andric   // opinion; other values should be self explanatory
4630b57cec5SDimitry Andric   CompilerType compiler_type(valobj.GetCompilerType());
4640b57cec5SDimitry Andric   if (compiler_type.IsValid()) {
4650b57cec5SDimitry Andric     switch (compiler_type.ShouldPrintAsOneLiner(&valobj)) {
4660b57cec5SDimitry Andric     case eLazyBoolNo:
4670b57cec5SDimitry Andric       return false;
4680b57cec5SDimitry Andric     case eLazyBoolYes:
4690b57cec5SDimitry Andric       return true;
4700b57cec5SDimitry Andric     case eLazyBoolCalculate:
4710b57cec5SDimitry Andric       break;
4720b57cec5SDimitry Andric     }
4730b57cec5SDimitry Andric   }
4740b57cec5SDimitry Andric 
4750b57cec5SDimitry Andric   size_t total_children_name_len = 0;
4760b57cec5SDimitry Andric 
4770b57cec5SDimitry Andric   for (size_t idx = 0; idx < valobj.GetNumChildren(); idx++) {
4780b57cec5SDimitry Andric     bool is_synth_val = false;
479*fe013be4SDimitry Andric     ValueObjectSP child_sp(valobj.GetChildAtIndex(idx));
4800b57cec5SDimitry Andric     // something is wrong here - bail out
4810b57cec5SDimitry Andric     if (!child_sp)
4820b57cec5SDimitry Andric       return false;
4830b57cec5SDimitry Andric 
4840b57cec5SDimitry Andric     // also ask the child's type if it has any opinion
4850b57cec5SDimitry Andric     CompilerType child_compiler_type(child_sp->GetCompilerType());
4860b57cec5SDimitry Andric     if (child_compiler_type.IsValid()) {
4870b57cec5SDimitry Andric       switch (child_compiler_type.ShouldPrintAsOneLiner(child_sp.get())) {
4880b57cec5SDimitry Andric       case eLazyBoolYes:
4890b57cec5SDimitry Andric       // an opinion of yes is only binding for the child, so keep going
4900b57cec5SDimitry Andric       case eLazyBoolCalculate:
4910b57cec5SDimitry Andric         break;
4920b57cec5SDimitry Andric       case eLazyBoolNo:
4930b57cec5SDimitry Andric         // but if the child says no, then it's a veto on the whole thing
4940b57cec5SDimitry Andric         return false;
4950b57cec5SDimitry Andric       }
4960b57cec5SDimitry Andric     }
4970b57cec5SDimitry Andric 
4980b57cec5SDimitry Andric     // if we decided to define synthetic children for a type, we probably care
4990b57cec5SDimitry Andric     // enough to show them, but avoid nesting children in children
5000b57cec5SDimitry Andric     if (child_sp->GetSyntheticChildren().get() != nullptr) {
5010b57cec5SDimitry Andric       ValueObjectSP synth_sp(child_sp->GetSyntheticValue());
5020b57cec5SDimitry Andric       // wait.. wat? just get out of here..
5030b57cec5SDimitry Andric       if (!synth_sp)
5040b57cec5SDimitry Andric         return false;
5050b57cec5SDimitry Andric       // but if we only have them to provide a value, keep going
5060b57cec5SDimitry Andric       if (!synth_sp->MightHaveChildren() &&
5070b57cec5SDimitry Andric           synth_sp->DoesProvideSyntheticValue())
5080b57cec5SDimitry Andric         is_synth_val = true;
5090b57cec5SDimitry Andric       else
5100b57cec5SDimitry Andric         return false;
5110b57cec5SDimitry Andric     }
5120b57cec5SDimitry Andric 
5130b57cec5SDimitry Andric     total_children_name_len += child_sp->GetName().GetLength();
5140b57cec5SDimitry Andric 
5150b57cec5SDimitry Andric     // 50 itself is a "randomly" chosen number - the idea is that
5160b57cec5SDimitry Andric     // overly long structs should not get this treatment
5170b57cec5SDimitry Andric     // FIXME: maybe make this a user-tweakable setting?
5180b57cec5SDimitry Andric     if (total_children_name_len > 50)
5190b57cec5SDimitry Andric       return false;
5200b57cec5SDimitry Andric 
5210b57cec5SDimitry Andric     // if a summary is there..
5220b57cec5SDimitry Andric     if (child_sp->GetSummaryFormat()) {
5230b57cec5SDimitry Andric       // and it wants children, then bail out
5240b57cec5SDimitry Andric       if (child_sp->GetSummaryFormat()->DoesPrintChildren(child_sp.get()))
5250b57cec5SDimitry Andric         return false;
5260b57cec5SDimitry Andric     }
5270b57cec5SDimitry Andric 
5280b57cec5SDimitry Andric     // if this child has children..
5290b57cec5SDimitry Andric     if (child_sp->GetNumChildren()) {
5300b57cec5SDimitry Andric       // ...and no summary...
5310b57cec5SDimitry Andric       // (if it had a summary and the summary wanted children, we would have
5320b57cec5SDimitry Andric       // bailed out anyway
5330b57cec5SDimitry Andric       //  so this only makes us bail out if this has no summary and we would
5340b57cec5SDimitry Andric       //  then print children)
5350b57cec5SDimitry Andric       if (!child_sp->GetSummaryFormat() && !is_synth_val) // but again only do
5360b57cec5SDimitry Andric                                                           // that if not a
5370b57cec5SDimitry Andric                                                           // synthetic valued
5380b57cec5SDimitry Andric                                                           // child
5390b57cec5SDimitry Andric         return false;                                     // then bail out
5400b57cec5SDimitry Andric     }
5410b57cec5SDimitry Andric   }
5420b57cec5SDimitry Andric   return true;
5430b57cec5SDimitry Andric }
5440b57cec5SDimitry Andric 
GetTypeForCache(ValueObject & valobj,lldb::DynamicValueType use_dynamic)5450b57cec5SDimitry Andric ConstString FormatManager::GetTypeForCache(ValueObject &valobj,
5460b57cec5SDimitry Andric                                            lldb::DynamicValueType use_dynamic) {
5470b57cec5SDimitry Andric   ValueObjectSP valobj_sp = valobj.GetQualifiedRepresentationIfAvailable(
5480b57cec5SDimitry Andric       use_dynamic, valobj.IsSynthetic());
5490b57cec5SDimitry Andric   if (valobj_sp && valobj_sp->GetCompilerType().IsValid()) {
5500b57cec5SDimitry Andric     if (!valobj_sp->GetCompilerType().IsMeaninglessWithoutDynamicResolution())
5510b57cec5SDimitry Andric       return valobj_sp->GetQualifiedTypeName();
5520b57cec5SDimitry Andric   }
5530b57cec5SDimitry Andric   return ConstString();
5540b57cec5SDimitry Andric }
5550b57cec5SDimitry Andric 
5560b57cec5SDimitry Andric std::vector<lldb::LanguageType>
GetCandidateLanguages(lldb::LanguageType lang_type)5570b57cec5SDimitry Andric FormatManager::GetCandidateLanguages(lldb::LanguageType lang_type) {
5580b57cec5SDimitry Andric   switch (lang_type) {
5590b57cec5SDimitry Andric   case lldb::eLanguageTypeC:
5600b57cec5SDimitry Andric   case lldb::eLanguageTypeC89:
5610b57cec5SDimitry Andric   case lldb::eLanguageTypeC99:
5620b57cec5SDimitry Andric   case lldb::eLanguageTypeC11:
5630b57cec5SDimitry Andric   case lldb::eLanguageTypeC_plus_plus:
5640b57cec5SDimitry Andric   case lldb::eLanguageTypeC_plus_plus_03:
5650b57cec5SDimitry Andric   case lldb::eLanguageTypeC_plus_plus_11:
5660b57cec5SDimitry Andric   case lldb::eLanguageTypeC_plus_plus_14:
5670b57cec5SDimitry Andric     return {lldb::eLanguageTypeC_plus_plus, lldb::eLanguageTypeObjC};
5680b57cec5SDimitry Andric   default:
5690b57cec5SDimitry Andric     return {lang_type};
5700b57cec5SDimitry Andric   }
571480093f4SDimitry Andric   llvm_unreachable("Fully covered switch");
5720b57cec5SDimitry Andric }
5730b57cec5SDimitry Andric 
5740b57cec5SDimitry Andric LanguageCategory *
GetCategoryForLanguage(lldb::LanguageType lang_type)5750b57cec5SDimitry Andric FormatManager::GetCategoryForLanguage(lldb::LanguageType lang_type) {
5760b57cec5SDimitry Andric   std::lock_guard<std::recursive_mutex> guard(m_language_categories_mutex);
5770b57cec5SDimitry Andric   auto iter = m_language_categories_map.find(lang_type),
5780b57cec5SDimitry Andric        end = m_language_categories_map.end();
5790b57cec5SDimitry Andric   if (iter != end)
5800b57cec5SDimitry Andric     return iter->second.get();
5810b57cec5SDimitry Andric   LanguageCategory *lang_category = new LanguageCategory(lang_type);
5820b57cec5SDimitry Andric   m_language_categories_map[lang_type] =
5830b57cec5SDimitry Andric       LanguageCategory::UniquePointer(lang_category);
5840b57cec5SDimitry Andric   return lang_category;
5850b57cec5SDimitry Andric }
5860b57cec5SDimitry Andric 
587480093f4SDimitry Andric template <typename ImplSP>
GetHardcoded(FormattersMatchData & match_data)588480093f4SDimitry Andric ImplSP FormatManager::GetHardcoded(FormattersMatchData &match_data) {
589480093f4SDimitry Andric   ImplSP retval_sp;
5900b57cec5SDimitry Andric   for (lldb::LanguageType lang_type : match_data.GetCandidateLanguages()) {
5910b57cec5SDimitry Andric     if (LanguageCategory *lang_category = GetCategoryForLanguage(lang_type)) {
5920b57cec5SDimitry Andric       if (lang_category->GetHardcoded(*this, match_data, retval_sp))
593480093f4SDimitry Andric         return retval_sp;
594480093f4SDimitry Andric     }
595480093f4SDimitry Andric   }
596480093f4SDimitry Andric   return retval_sp;
597480093f4SDimitry Andric }
598480093f4SDimitry Andric 
599*fe013be4SDimitry Andric namespace {
600*fe013be4SDimitry Andric template <typename ImplSP> const char *FormatterKind;
601*fe013be4SDimitry Andric template <> const char *FormatterKind<lldb::TypeFormatImplSP> = "format";
602*fe013be4SDimitry Andric template <> const char *FormatterKind<lldb::TypeSummaryImplSP> = "summary";
603*fe013be4SDimitry Andric template <> const char *FormatterKind<lldb::SyntheticChildrenSP> = "synthetic";
604*fe013be4SDimitry Andric } // namespace
605*fe013be4SDimitry Andric 
606*fe013be4SDimitry Andric #define FORMAT_LOG(Message) "[%s] " Message, FormatterKind<ImplSP>
607*fe013be4SDimitry Andric 
608480093f4SDimitry Andric template <typename ImplSP>
Get(ValueObject & valobj,lldb::DynamicValueType use_dynamic)609480093f4SDimitry Andric ImplSP FormatManager::Get(ValueObject &valobj,
610480093f4SDimitry Andric                           lldb::DynamicValueType use_dynamic) {
611480093f4SDimitry Andric   FormattersMatchData match_data(valobj, use_dynamic);
612480093f4SDimitry Andric   if (ImplSP retval_sp = GetCached<ImplSP>(match_data))
613480093f4SDimitry Andric     return retval_sp;
614480093f4SDimitry Andric 
61581ad6265SDimitry Andric   Log *log = GetLog(LLDBLog::DataFormatters);
616480093f4SDimitry Andric 
617*fe013be4SDimitry Andric   LLDB_LOGF(log, FORMAT_LOG("Search failed. Giving language a chance."));
618480093f4SDimitry Andric   for (lldb::LanguageType lang_type : match_data.GetCandidateLanguages()) {
619480093f4SDimitry Andric     if (LanguageCategory *lang_category = GetCategoryForLanguage(lang_type)) {
620480093f4SDimitry Andric       ImplSP retval_sp;
621480093f4SDimitry Andric       if (lang_category->Get(match_data, retval_sp))
622480093f4SDimitry Andric         if (retval_sp) {
623*fe013be4SDimitry Andric           LLDB_LOGF(log, FORMAT_LOG("Language search success. Returning."));
624480093f4SDimitry Andric           return retval_sp;
625480093f4SDimitry Andric         }
6260b57cec5SDimitry Andric     }
6270b57cec5SDimitry Andric   }
6280b57cec5SDimitry Andric 
629*fe013be4SDimitry Andric   LLDB_LOGF(log, FORMAT_LOG("Search failed. Giving hardcoded a chance."));
630480093f4SDimitry Andric   return GetHardcoded<ImplSP>(match_data);
631480093f4SDimitry Andric }
632480093f4SDimitry Andric 
633480093f4SDimitry Andric template <typename ImplSP>
GetCached(FormattersMatchData & match_data)634480093f4SDimitry Andric ImplSP FormatManager::GetCached(FormattersMatchData &match_data) {
635480093f4SDimitry Andric   ImplSP retval_sp;
63681ad6265SDimitry Andric   Log *log = GetLog(LLDBLog::DataFormatters);
637480093f4SDimitry Andric   if (match_data.GetTypeForCache()) {
638*fe013be4SDimitry Andric     LLDB_LOGF(log, "\n\n" FORMAT_LOG("Looking into cache for type %s"),
639480093f4SDimitry Andric               match_data.GetTypeForCache().AsCString("<invalid>"));
640480093f4SDimitry Andric     if (m_format_cache.Get(match_data.GetTypeForCache(), retval_sp)) {
641480093f4SDimitry Andric       if (log) {
642*fe013be4SDimitry Andric         LLDB_LOGF(log, FORMAT_LOG("Cache search success. Returning."));
643480093f4SDimitry Andric         LLDB_LOGV(log, "Cache hits: {0} - Cache Misses: {1}",
644480093f4SDimitry Andric                   m_format_cache.GetCacheHits(),
645480093f4SDimitry Andric                   m_format_cache.GetCacheMisses());
646480093f4SDimitry Andric       }
647480093f4SDimitry Andric       return retval_sp;
648480093f4SDimitry Andric     }
649*fe013be4SDimitry Andric     LLDB_LOGF(log, FORMAT_LOG("Cache search failed. Going normal route"));
650480093f4SDimitry Andric   }
651480093f4SDimitry Andric 
652480093f4SDimitry Andric   m_categories_map.Get(match_data, retval_sp);
653480093f4SDimitry Andric   if (match_data.GetTypeForCache() && (!retval_sp || !retval_sp->NonCacheable())) {
654*fe013be4SDimitry Andric     LLDB_LOGF(log, FORMAT_LOG("Caching %p for type %s"),
655480093f4SDimitry Andric               static_cast<void *>(retval_sp.get()),
656480093f4SDimitry Andric               match_data.GetTypeForCache().AsCString("<invalid>"));
657480093f4SDimitry Andric     m_format_cache.Set(match_data.GetTypeForCache(), retval_sp);
658480093f4SDimitry Andric   }
659480093f4SDimitry Andric   LLDB_LOGV(log, "Cache hits: {0} - Cache Misses: {1}",
660480093f4SDimitry Andric             m_format_cache.GetCacheHits(), m_format_cache.GetCacheMisses());
6610b57cec5SDimitry Andric   return retval_sp;
6620b57cec5SDimitry Andric }
6630b57cec5SDimitry Andric 
664*fe013be4SDimitry Andric #undef FORMAT_LOG
665*fe013be4SDimitry Andric 
6660b57cec5SDimitry Andric lldb::TypeFormatImplSP
GetFormat(ValueObject & valobj,lldb::DynamicValueType use_dynamic)6670b57cec5SDimitry Andric FormatManager::GetFormat(ValueObject &valobj,
6680b57cec5SDimitry Andric                          lldb::DynamicValueType use_dynamic) {
669480093f4SDimitry Andric   return Get<lldb::TypeFormatImplSP>(valobj, use_dynamic);
6700b57cec5SDimitry Andric }
6710b57cec5SDimitry Andric 
6720b57cec5SDimitry Andric lldb::TypeSummaryImplSP
GetSummaryFormat(ValueObject & valobj,lldb::DynamicValueType use_dynamic)6730b57cec5SDimitry Andric FormatManager::GetSummaryFormat(ValueObject &valobj,
6740b57cec5SDimitry Andric                                 lldb::DynamicValueType use_dynamic) {
675480093f4SDimitry Andric   return Get<lldb::TypeSummaryImplSP>(valobj, use_dynamic);
6760b57cec5SDimitry Andric }
6770b57cec5SDimitry Andric 
6780b57cec5SDimitry Andric lldb::SyntheticChildrenSP
GetSyntheticChildren(ValueObject & valobj,lldb::DynamicValueType use_dynamic)6790b57cec5SDimitry Andric FormatManager::GetSyntheticChildren(ValueObject &valobj,
6800b57cec5SDimitry Andric                                     lldb::DynamicValueType use_dynamic) {
681480093f4SDimitry Andric   return Get<lldb::SyntheticChildrenSP>(valobj, use_dynamic);
6820b57cec5SDimitry Andric }
6830b57cec5SDimitry Andric 
FormatManager()6840b57cec5SDimitry Andric FormatManager::FormatManager()
6850b57cec5SDimitry Andric     : m_last_revision(0), m_format_cache(), m_language_categories_mutex(),
6860b57cec5SDimitry Andric       m_language_categories_map(), m_named_summaries_map(this),
6870b57cec5SDimitry Andric       m_categories_map(this), m_default_category_name(ConstString("default")),
6880b57cec5SDimitry Andric       m_system_category_name(ConstString("system")),
6890b57cec5SDimitry Andric       m_vectortypes_category_name(ConstString("VectorTypes")) {
6900b57cec5SDimitry Andric   LoadSystemFormatters();
6910b57cec5SDimitry Andric   LoadVectorFormatters();
6920b57cec5SDimitry Andric 
6930b57cec5SDimitry Andric   EnableCategory(m_vectortypes_category_name, TypeCategoryMap::Last,
6940b57cec5SDimitry Andric                  lldb::eLanguageTypeObjC_plus_plus);
6950b57cec5SDimitry Andric   EnableCategory(m_system_category_name, TypeCategoryMap::Last,
6960b57cec5SDimitry Andric                  lldb::eLanguageTypeObjC_plus_plus);
6970b57cec5SDimitry Andric }
6980b57cec5SDimitry Andric 
LoadSystemFormatters()6990b57cec5SDimitry Andric void FormatManager::LoadSystemFormatters() {
7000b57cec5SDimitry Andric   TypeSummaryImpl::Flags string_flags;
7010b57cec5SDimitry Andric   string_flags.SetCascades(true)
7020b57cec5SDimitry Andric       .SetSkipPointers(true)
7030b57cec5SDimitry Andric       .SetSkipReferences(false)
7040b57cec5SDimitry Andric       .SetDontShowChildren(true)
7050b57cec5SDimitry Andric       .SetDontShowValue(false)
7060b57cec5SDimitry Andric       .SetShowMembersOneLiner(false)
7070b57cec5SDimitry Andric       .SetHideItemNames(false);
7080b57cec5SDimitry Andric 
7090b57cec5SDimitry Andric   TypeSummaryImpl::Flags string_array_flags;
7100b57cec5SDimitry Andric   string_array_flags.SetCascades(true)
7110b57cec5SDimitry Andric       .SetSkipPointers(true)
7120b57cec5SDimitry Andric       .SetSkipReferences(false)
7130b57cec5SDimitry Andric       .SetDontShowChildren(true)
7140b57cec5SDimitry Andric       .SetDontShowValue(true)
7150b57cec5SDimitry Andric       .SetShowMembersOneLiner(false)
7160b57cec5SDimitry Andric       .SetHideItemNames(false);
7170b57cec5SDimitry Andric 
7180b57cec5SDimitry Andric   lldb::TypeSummaryImplSP string_format(
7190b57cec5SDimitry Andric       new StringSummaryFormat(string_flags, "${var%s}"));
7200b57cec5SDimitry Andric 
7210b57cec5SDimitry Andric   lldb::TypeSummaryImplSP string_array_format(
722349cc55cSDimitry Andric       new StringSummaryFormat(string_array_flags, "${var%char[]}"));
7230b57cec5SDimitry Andric 
7240b57cec5SDimitry Andric   TypeCategoryImpl::SharedPointer sys_category_sp =
7250b57cec5SDimitry Andric       GetCategory(m_system_category_name);
7260b57cec5SDimitry Andric 
727bdd1243dSDimitry Andric   sys_category_sp->AddTypeSummary(R"(^(unsigned )?char ?(\*|\[\])$)",
728bdd1243dSDimitry Andric                                   eFormatterMatchRegex, string_format);
7290eae32dcSDimitry Andric 
730bdd1243dSDimitry Andric   sys_category_sp->AddTypeSummary(R"(^((un)?signed )?char ?\[[0-9]+\]$)",
731bdd1243dSDimitry Andric                                   eFormatterMatchRegex, string_array_format);
7320b57cec5SDimitry Andric 
7330b57cec5SDimitry Andric   lldb::TypeSummaryImplSP ostype_summary(
7340b57cec5SDimitry Andric       new StringSummaryFormat(TypeSummaryImpl::Flags()
7350b57cec5SDimitry Andric                                   .SetCascades(false)
7360b57cec5SDimitry Andric                                   .SetSkipPointers(true)
7370b57cec5SDimitry Andric                                   .SetSkipReferences(true)
7380b57cec5SDimitry Andric                                   .SetDontShowChildren(true)
7390b57cec5SDimitry Andric                                   .SetDontShowValue(false)
7400b57cec5SDimitry Andric                                   .SetShowMembersOneLiner(false)
7410b57cec5SDimitry Andric                                   .SetHideItemNames(false),
7420b57cec5SDimitry Andric                               "${var%O}"));
7430b57cec5SDimitry Andric 
744bdd1243dSDimitry Andric   sys_category_sp->AddTypeSummary("OSType", eFormatterMatchExact,
7450b57cec5SDimitry Andric                                   ostype_summary);
7460b57cec5SDimitry Andric 
7470b57cec5SDimitry Andric   TypeFormatImpl::Flags fourchar_flags;
7480b57cec5SDimitry Andric   fourchar_flags.SetCascades(true).SetSkipPointers(true).SetSkipReferences(
7490b57cec5SDimitry Andric       true);
7500b57cec5SDimitry Andric 
751*fe013be4SDimitry Andric   AddFormat(sys_category_sp, lldb::eFormatOSType, "FourCharCode",
7520b57cec5SDimitry Andric             fourchar_flags);
7530b57cec5SDimitry Andric }
7540b57cec5SDimitry Andric 
LoadVectorFormatters()7550b57cec5SDimitry Andric void FormatManager::LoadVectorFormatters() {
7560b57cec5SDimitry Andric   TypeCategoryImpl::SharedPointer vectors_category_sp =
7570b57cec5SDimitry Andric       GetCategory(m_vectortypes_category_name);
7580b57cec5SDimitry Andric 
7590b57cec5SDimitry Andric   TypeSummaryImpl::Flags vector_flags;
7600b57cec5SDimitry Andric   vector_flags.SetCascades(true)
7610b57cec5SDimitry Andric       .SetSkipPointers(true)
7620b57cec5SDimitry Andric       .SetSkipReferences(false)
7630b57cec5SDimitry Andric       .SetDontShowChildren(true)
7640b57cec5SDimitry Andric       .SetDontShowValue(false)
7650b57cec5SDimitry Andric       .SetShowMembersOneLiner(true)
7660b57cec5SDimitry Andric       .SetHideItemNames(true);
7670b57cec5SDimitry Andric 
768*fe013be4SDimitry Andric   AddStringSummary(vectors_category_sp, "${var.uint128}", "builtin_type_vec128",
7690b57cec5SDimitry Andric                    vector_flags);
770*fe013be4SDimitry Andric   AddStringSummary(vectors_category_sp, "", "float[4]", vector_flags);
771*fe013be4SDimitry Andric   AddStringSummary(vectors_category_sp, "", "int32_t[4]", vector_flags);
772*fe013be4SDimitry Andric   AddStringSummary(vectors_category_sp, "", "int16_t[8]", vector_flags);
773*fe013be4SDimitry Andric   AddStringSummary(vectors_category_sp, "", "vDouble", vector_flags);
774*fe013be4SDimitry Andric   AddStringSummary(vectors_category_sp, "", "vFloat", vector_flags);
775*fe013be4SDimitry Andric   AddStringSummary(vectors_category_sp, "", "vSInt8", vector_flags);
776*fe013be4SDimitry Andric   AddStringSummary(vectors_category_sp, "", "vSInt16", vector_flags);
777*fe013be4SDimitry Andric   AddStringSummary(vectors_category_sp, "", "vSInt32", vector_flags);
778*fe013be4SDimitry Andric   AddStringSummary(vectors_category_sp, "", "vUInt16", vector_flags);
779*fe013be4SDimitry Andric   AddStringSummary(vectors_category_sp, "", "vUInt8", vector_flags);
780*fe013be4SDimitry Andric   AddStringSummary(vectors_category_sp, "", "vUInt16", vector_flags);
781*fe013be4SDimitry Andric   AddStringSummary(vectors_category_sp, "", "vUInt32", vector_flags);
782*fe013be4SDimitry Andric   AddStringSummary(vectors_category_sp, "", "vBool32", vector_flags);
7830b57cec5SDimitry Andric }
784