llvm-project
795 строк · 28.7 Кб
1//===-- FormatManager.cpp -------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "lldb/DataFormatters/FormatManager.h"
10
11#include "lldb/Core/Debugger.h"
12#include "lldb/Core/ValueObject.h"
13#include "lldb/DataFormatters/FormattersHelpers.h"
14#include "lldb/DataFormatters/LanguageCategory.h"
15#include "lldb/Interpreter/ScriptInterpreter.h"
16#include "lldb/Target/ExecutionContext.h"
17#include "lldb/Target/Language.h"
18#include "lldb/Utility/LLDBLog.h"
19#include "lldb/Utility/Log.h"
20#include "llvm/ADT/STLExtras.h"
21
22using namespace lldb;
23using namespace lldb_private;
24using namespace lldb_private::formatters;
25
26struct FormatInfo {
27Format format;
28const char format_char; // One or more format characters that can be used for
29// this format.
30const char *format_name; // Long format name that can be used to specify the
31// current format
32};
33
34static constexpr FormatInfo g_format_infos[] = {
35{eFormatDefault, '\0', "default"},
36{eFormatBoolean, 'B', "boolean"},
37{eFormatBinary, 'b', "binary"},
38{eFormatBytes, 'y', "bytes"},
39{eFormatBytesWithASCII, 'Y', "bytes with ASCII"},
40{eFormatChar, 'c', "character"},
41{eFormatCharPrintable, 'C', "printable character"},
42{eFormatComplexFloat, 'F', "complex float"},
43{eFormatCString, 's', "c-string"},
44{eFormatDecimal, 'd', "decimal"},
45{eFormatEnum, 'E', "enumeration"},
46{eFormatHex, 'x', "hex"},
47{eFormatHexUppercase, 'X', "uppercase hex"},
48{eFormatFloat, 'f', "float"},
49{eFormatOctal, 'o', "octal"},
50{eFormatOSType, 'O', "OSType"},
51{eFormatUnicode16, 'U', "unicode16"},
52{eFormatUnicode32, '\0', "unicode32"},
53{eFormatUnsigned, 'u', "unsigned decimal"},
54{eFormatPointer, 'p', "pointer"},
55{eFormatVectorOfChar, '\0', "char[]"},
56{eFormatVectorOfSInt8, '\0', "int8_t[]"},
57{eFormatVectorOfUInt8, '\0', "uint8_t[]"},
58{eFormatVectorOfSInt16, '\0', "int16_t[]"},
59{eFormatVectorOfUInt16, '\0', "uint16_t[]"},
60{eFormatVectorOfSInt32, '\0', "int32_t[]"},
61{eFormatVectorOfUInt32, '\0', "uint32_t[]"},
62{eFormatVectorOfSInt64, '\0', "int64_t[]"},
63{eFormatVectorOfUInt64, '\0', "uint64_t[]"},
64{eFormatVectorOfFloat16, '\0', "float16[]"},
65{eFormatVectorOfFloat32, '\0', "float32[]"},
66{eFormatVectorOfFloat64, '\0', "float64[]"},
67{eFormatVectorOfUInt128, '\0', "uint128_t[]"},
68{eFormatComplexInteger, 'I', "complex integer"},
69{eFormatCharArray, 'a', "character array"},
70{eFormatAddressInfo, 'A', "address"},
71{eFormatHexFloat, '\0', "hex float"},
72{eFormatInstruction, 'i', "instruction"},
73{eFormatVoid, 'v', "void"},
74{eFormatUnicode8, 'u', "unicode8"},
75};
76
77static_assert((sizeof(g_format_infos) / sizeof(g_format_infos[0])) ==
78kNumFormats,
79"All formats must have a corresponding info entry.");
80
81static uint32_t g_num_format_infos = std::size(g_format_infos);
82
83static bool GetFormatFromFormatChar(char format_char, Format &format) {
84for (uint32_t i = 0; i < g_num_format_infos; ++i) {
85if (g_format_infos[i].format_char == format_char) {
86format = g_format_infos[i].format;
87return true;
88}
89}
90format = eFormatInvalid;
91return false;
92}
93
94static bool GetFormatFromFormatName(llvm::StringRef format_name,
95Format &format) {
96uint32_t i;
97for (i = 0; i < g_num_format_infos; ++i) {
98if (format_name.equals_insensitive(g_format_infos[i].format_name)) {
99format = g_format_infos[i].format;
100return true;
101}
102}
103
104for (i = 0; i < g_num_format_infos; ++i) {
105if (llvm::StringRef(g_format_infos[i].format_name)
106.starts_with_insensitive(format_name)) {
107format = g_format_infos[i].format;
108return true;
109}
110}
111format = eFormatInvalid;
112return false;
113}
114
115void FormatManager::Changed() {
116++m_last_revision;
117m_format_cache.Clear();
118std::lock_guard<std::recursive_mutex> guard(m_language_categories_mutex);
119for (auto &iter : m_language_categories_map) {
120if (iter.second)
121iter.second->GetFormatCache().Clear();
122}
123}
124
125bool FormatManager::GetFormatFromCString(const char *format_cstr,
126lldb::Format &format) {
127bool success = false;
128if (format_cstr && format_cstr[0]) {
129if (format_cstr[1] == '\0') {
130success = GetFormatFromFormatChar(format_cstr[0], format);
131if (success)
132return true;
133}
134
135success = GetFormatFromFormatName(format_cstr, format);
136}
137if (!success)
138format = eFormatInvalid;
139return success;
140}
141
142char FormatManager::GetFormatAsFormatChar(lldb::Format format) {
143for (uint32_t i = 0; i < g_num_format_infos; ++i) {
144if (g_format_infos[i].format == format)
145return g_format_infos[i].format_char;
146}
147return '\0';
148}
149
150const char *FormatManager::GetFormatAsCString(Format format) {
151if (format >= eFormatDefault && format < kNumFormats)
152return g_format_infos[format].format_name;
153return nullptr;
154}
155
156void FormatManager::EnableAllCategories() {
157m_categories_map.EnableAllCategories();
158std::lock_guard<std::recursive_mutex> guard(m_language_categories_mutex);
159for (auto &iter : m_language_categories_map) {
160if (iter.second)
161iter.second->Enable();
162}
163}
164
165void FormatManager::DisableAllCategories() {
166m_categories_map.DisableAllCategories();
167std::lock_guard<std::recursive_mutex> guard(m_language_categories_mutex);
168for (auto &iter : m_language_categories_map) {
169if (iter.second)
170iter.second->Disable();
171}
172}
173
174void FormatManager::GetPossibleMatches(
175ValueObject &valobj, CompilerType compiler_type,
176lldb::DynamicValueType use_dynamic, FormattersMatchVector &entries,
177FormattersMatchCandidate::Flags current_flags, bool root_level) {
178compiler_type = compiler_type.GetTypeForFormatters();
179ConstString type_name(compiler_type.GetTypeName());
180// A ValueObject that couldn't be made correctly won't necessarily have a
181// target. We aren't going to find a formatter in this case anyway, so we
182// should just exit.
183TargetSP target_sp = valobj.GetTargetSP();
184if (!target_sp)
185return;
186ScriptInterpreter *script_interpreter =
187target_sp->GetDebugger().GetScriptInterpreter();
188if (valobj.GetBitfieldBitSize() > 0) {
189StreamString sstring;
190sstring.Printf("%s:%d", type_name.AsCString(), valobj.GetBitfieldBitSize());
191ConstString bitfieldname(sstring.GetString());
192entries.push_back({bitfieldname, script_interpreter,
193TypeImpl(compiler_type), current_flags});
194}
195
196if (!compiler_type.IsMeaninglessWithoutDynamicResolution()) {
197entries.push_back({type_name, script_interpreter, TypeImpl(compiler_type),
198current_flags});
199
200ConstString display_type_name(compiler_type.GetTypeName());
201if (display_type_name != type_name)
202entries.push_back({display_type_name, script_interpreter,
203TypeImpl(compiler_type), current_flags});
204}
205
206for (bool is_rvalue_ref = true, j = true;
207j && compiler_type.IsReferenceType(nullptr, &is_rvalue_ref); j = false) {
208CompilerType non_ref_type = compiler_type.GetNonReferenceType();
209GetPossibleMatches(valobj, non_ref_type, use_dynamic, entries,
210current_flags.WithStrippedReference());
211if (non_ref_type.IsTypedefType()) {
212CompilerType deffed_referenced_type = non_ref_type.GetTypedefedType();
213deffed_referenced_type =
214is_rvalue_ref ? deffed_referenced_type.GetRValueReferenceType()
215: deffed_referenced_type.GetLValueReferenceType();
216// this is not exactly the usual meaning of stripping typedefs
217GetPossibleMatches(
218valobj, deffed_referenced_type,
219use_dynamic, entries, current_flags.WithStrippedTypedef());
220}
221}
222
223if (compiler_type.IsPointerType()) {
224CompilerType non_ptr_type = compiler_type.GetPointeeType();
225GetPossibleMatches(valobj, non_ptr_type, use_dynamic, entries,
226current_flags.WithStrippedPointer());
227if (non_ptr_type.IsTypedefType()) {
228CompilerType deffed_pointed_type =
229non_ptr_type.GetTypedefedType().GetPointerType();
230// this is not exactly the usual meaning of stripping typedefs
231GetPossibleMatches(valobj, deffed_pointed_type, use_dynamic, entries,
232current_flags.WithStrippedTypedef());
233}
234}
235
236// For arrays with typedef-ed elements, we add a candidate with the typedef
237// stripped.
238uint64_t array_size;
239if (compiler_type.IsArrayType(nullptr, &array_size, nullptr)) {
240ExecutionContext exe_ctx(valobj.GetExecutionContextRef());
241CompilerType element_type = compiler_type.GetArrayElementType(
242exe_ctx.GetBestExecutionContextScope());
243if (element_type.IsTypedefType()) {
244// Get the stripped element type and compute the stripped array type
245// from it.
246CompilerType deffed_array_type =
247element_type.GetTypedefedType().GetArrayType(array_size);
248// this is not exactly the usual meaning of stripping typedefs
249GetPossibleMatches(
250valobj, deffed_array_type,
251use_dynamic, entries, current_flags.WithStrippedTypedef());
252}
253}
254
255for (lldb::LanguageType language_type :
256GetCandidateLanguages(valobj.GetObjectRuntimeLanguage())) {
257if (Language *language = Language::FindPlugin(language_type)) {
258for (const FormattersMatchCandidate& candidate :
259language->GetPossibleFormattersMatches(valobj, use_dynamic)) {
260entries.push_back(candidate);
261}
262}
263}
264
265// try to strip typedef chains
266if (compiler_type.IsTypedefType()) {
267CompilerType deffed_type = compiler_type.GetTypedefedType();
268GetPossibleMatches(valobj, deffed_type, use_dynamic, entries,
269current_flags.WithStrippedTypedef());
270}
271
272if (root_level) {
273do {
274if (!compiler_type.IsValid())
275break;
276
277CompilerType unqual_compiler_ast_type =
278compiler_type.GetFullyUnqualifiedType();
279if (!unqual_compiler_ast_type.IsValid())
280break;
281if (unqual_compiler_ast_type.GetOpaqueQualType() !=
282compiler_type.GetOpaqueQualType())
283GetPossibleMatches(valobj, unqual_compiler_ast_type, use_dynamic,
284entries, current_flags);
285} while (false);
286
287// if all else fails, go to static type
288if (valobj.IsDynamic()) {
289lldb::ValueObjectSP static_value_sp(valobj.GetStaticValue());
290if (static_value_sp)
291GetPossibleMatches(*static_value_sp.get(),
292static_value_sp->GetCompilerType(), use_dynamic,
293entries, current_flags, true);
294}
295}
296}
297
298lldb::TypeFormatImplSP
299FormatManager::GetFormatForType(lldb::TypeNameSpecifierImplSP type_sp) {
300if (!type_sp)
301return lldb::TypeFormatImplSP();
302lldb::TypeFormatImplSP format_chosen_sp;
303uint32_t num_categories = m_categories_map.GetCount();
304lldb::TypeCategoryImplSP category_sp;
305uint32_t prio_category = UINT32_MAX;
306for (uint32_t category_id = 0; category_id < num_categories; category_id++) {
307category_sp = GetCategoryAtIndex(category_id);
308if (!category_sp->IsEnabled())
309continue;
310lldb::TypeFormatImplSP format_current_sp =
311category_sp->GetFormatForType(type_sp);
312if (format_current_sp &&
313(format_chosen_sp.get() == nullptr ||
314(prio_category > category_sp->GetEnabledPosition()))) {
315prio_category = category_sp->GetEnabledPosition();
316format_chosen_sp = format_current_sp;
317}
318}
319return format_chosen_sp;
320}
321
322lldb::TypeSummaryImplSP
323FormatManager::GetSummaryForType(lldb::TypeNameSpecifierImplSP type_sp) {
324if (!type_sp)
325return lldb::TypeSummaryImplSP();
326lldb::TypeSummaryImplSP summary_chosen_sp;
327uint32_t num_categories = m_categories_map.GetCount();
328lldb::TypeCategoryImplSP category_sp;
329uint32_t prio_category = UINT32_MAX;
330for (uint32_t category_id = 0; category_id < num_categories; category_id++) {
331category_sp = GetCategoryAtIndex(category_id);
332if (!category_sp->IsEnabled())
333continue;
334lldb::TypeSummaryImplSP summary_current_sp =
335category_sp->GetSummaryForType(type_sp);
336if (summary_current_sp &&
337(summary_chosen_sp.get() == nullptr ||
338(prio_category > category_sp->GetEnabledPosition()))) {
339prio_category = category_sp->GetEnabledPosition();
340summary_chosen_sp = summary_current_sp;
341}
342}
343return summary_chosen_sp;
344}
345
346lldb::TypeFilterImplSP
347FormatManager::GetFilterForType(lldb::TypeNameSpecifierImplSP type_sp) {
348if (!type_sp)
349return lldb::TypeFilterImplSP();
350lldb::TypeFilterImplSP filter_chosen_sp;
351uint32_t num_categories = m_categories_map.GetCount();
352lldb::TypeCategoryImplSP category_sp;
353uint32_t prio_category = UINT32_MAX;
354for (uint32_t category_id = 0; category_id < num_categories; category_id++) {
355category_sp = GetCategoryAtIndex(category_id);
356if (!category_sp->IsEnabled())
357continue;
358lldb::TypeFilterImplSP filter_current_sp(
359(TypeFilterImpl *)category_sp->GetFilterForType(type_sp).get());
360if (filter_current_sp &&
361(filter_chosen_sp.get() == nullptr ||
362(prio_category > category_sp->GetEnabledPosition()))) {
363prio_category = category_sp->GetEnabledPosition();
364filter_chosen_sp = filter_current_sp;
365}
366}
367return filter_chosen_sp;
368}
369
370lldb::ScriptedSyntheticChildrenSP
371FormatManager::GetSyntheticForType(lldb::TypeNameSpecifierImplSP type_sp) {
372if (!type_sp)
373return lldb::ScriptedSyntheticChildrenSP();
374lldb::ScriptedSyntheticChildrenSP synth_chosen_sp;
375uint32_t num_categories = m_categories_map.GetCount();
376lldb::TypeCategoryImplSP category_sp;
377uint32_t prio_category = UINT32_MAX;
378for (uint32_t category_id = 0; category_id < num_categories; category_id++) {
379category_sp = GetCategoryAtIndex(category_id);
380if (!category_sp->IsEnabled())
381continue;
382lldb::ScriptedSyntheticChildrenSP synth_current_sp(
383(ScriptedSyntheticChildren *)category_sp->GetSyntheticForType(type_sp)
384.get());
385if (synth_current_sp &&
386(synth_chosen_sp.get() == nullptr ||
387(prio_category > category_sp->GetEnabledPosition()))) {
388prio_category = category_sp->GetEnabledPosition();
389synth_chosen_sp = synth_current_sp;
390}
391}
392return synth_chosen_sp;
393}
394
395void FormatManager::ForEachCategory(TypeCategoryMap::ForEachCallback callback) {
396m_categories_map.ForEach(callback);
397std::lock_guard<std::recursive_mutex> guard(m_language_categories_mutex);
398for (const auto &entry : m_language_categories_map) {
399if (auto category_sp = entry.second->GetCategory()) {
400if (!callback(category_sp))
401break;
402}
403}
404}
405
406lldb::TypeCategoryImplSP
407FormatManager::GetCategory(ConstString category_name, bool can_create) {
408if (!category_name)
409return GetCategory(m_default_category_name);
410lldb::TypeCategoryImplSP category;
411if (m_categories_map.Get(category_name, category))
412return category;
413
414if (!can_create)
415return lldb::TypeCategoryImplSP();
416
417m_categories_map.Add(
418category_name,
419lldb::TypeCategoryImplSP(new TypeCategoryImpl(this, category_name)));
420return GetCategory(category_name);
421}
422
423lldb::Format FormatManager::GetSingleItemFormat(lldb::Format vector_format) {
424switch (vector_format) {
425case eFormatVectorOfChar:
426return eFormatCharArray;
427
428case eFormatVectorOfSInt8:
429case eFormatVectorOfSInt16:
430case eFormatVectorOfSInt32:
431case eFormatVectorOfSInt64:
432return eFormatDecimal;
433
434case eFormatVectorOfUInt8:
435case eFormatVectorOfUInt16:
436case eFormatVectorOfUInt32:
437case eFormatVectorOfUInt64:
438case eFormatVectorOfUInt128:
439return eFormatHex;
440
441case eFormatVectorOfFloat16:
442case eFormatVectorOfFloat32:
443case eFormatVectorOfFloat64:
444return eFormatFloat;
445
446default:
447return lldb::eFormatInvalid;
448}
449}
450
451bool FormatManager::ShouldPrintAsOneLiner(ValueObject &valobj) {
452TargetSP target_sp = valobj.GetTargetSP();
453// if settings say no oneline whatsoever
454if (target_sp && !target_sp->GetDebugger().GetAutoOneLineSummaries())
455return false; // then don't oneline
456
457// if this object has a summary, then ask the summary
458if (valobj.GetSummaryFormat().get() != nullptr)
459return valobj.GetSummaryFormat()->IsOneLiner();
460
461const size_t max_num_children =
462(target_sp ? *target_sp : Target::GetGlobalProperties())
463.GetMaximumNumberOfChildrenToDisplay();
464auto num_children = valobj.GetNumChildren(max_num_children);
465if (!num_children) {
466llvm::consumeError(num_children.takeError());
467return true;
468}
469// no children, no party
470if (*num_children == 0)
471return false;
472
473// ask the type if it has any opinion about this eLazyBoolCalculate == no
474// opinion; other values should be self explanatory
475CompilerType compiler_type(valobj.GetCompilerType());
476if (compiler_type.IsValid()) {
477switch (compiler_type.ShouldPrintAsOneLiner(&valobj)) {
478case eLazyBoolNo:
479return false;
480case eLazyBoolYes:
481return true;
482case eLazyBoolCalculate:
483break;
484}
485}
486
487size_t total_children_name_len = 0;
488
489for (size_t idx = 0; idx < *num_children; idx++) {
490bool is_synth_val = false;
491ValueObjectSP child_sp(valobj.GetChildAtIndex(idx));
492// something is wrong here - bail out
493if (!child_sp)
494return false;
495
496// also ask the child's type if it has any opinion
497CompilerType child_compiler_type(child_sp->GetCompilerType());
498if (child_compiler_type.IsValid()) {
499switch (child_compiler_type.ShouldPrintAsOneLiner(child_sp.get())) {
500case eLazyBoolYes:
501// an opinion of yes is only binding for the child, so keep going
502case eLazyBoolCalculate:
503break;
504case eLazyBoolNo:
505// but if the child says no, then it's a veto on the whole thing
506return false;
507}
508}
509
510// if we decided to define synthetic children for a type, we probably care
511// enough to show them, but avoid nesting children in children
512if (child_sp->GetSyntheticChildren().get() != nullptr) {
513ValueObjectSP synth_sp(child_sp->GetSyntheticValue());
514// wait.. wat? just get out of here..
515if (!synth_sp)
516return false;
517// but if we only have them to provide a value, keep going
518if (!synth_sp->MightHaveChildren() &&
519synth_sp->DoesProvideSyntheticValue())
520is_synth_val = true;
521else
522return false;
523}
524
525total_children_name_len += child_sp->GetName().GetLength();
526
527// 50 itself is a "randomly" chosen number - the idea is that
528// overly long structs should not get this treatment
529// FIXME: maybe make this a user-tweakable setting?
530if (total_children_name_len > 50)
531return false;
532
533// if a summary is there..
534if (child_sp->GetSummaryFormat()) {
535// and it wants children, then bail out
536if (child_sp->GetSummaryFormat()->DoesPrintChildren(child_sp.get()))
537return false;
538}
539
540// if this child has children..
541if (child_sp->HasChildren()) {
542// ...and no summary...
543// (if it had a summary and the summary wanted children, we would have
544// bailed out anyway
545// so this only makes us bail out if this has no summary and we would
546// then print children)
547if (!child_sp->GetSummaryFormat() && !is_synth_val) // but again only do
548// that if not a
549// synthetic valued
550// child
551return false; // then bail out
552}
553}
554return true;
555}
556
557ConstString FormatManager::GetTypeForCache(ValueObject &valobj,
558lldb::DynamicValueType use_dynamic) {
559ValueObjectSP valobj_sp = valobj.GetQualifiedRepresentationIfAvailable(
560use_dynamic, valobj.IsSynthetic());
561if (valobj_sp && valobj_sp->GetCompilerType().IsValid()) {
562if (!valobj_sp->GetCompilerType().IsMeaninglessWithoutDynamicResolution())
563return valobj_sp->GetQualifiedTypeName();
564}
565return ConstString();
566}
567
568std::vector<lldb::LanguageType>
569FormatManager::GetCandidateLanguages(lldb::LanguageType lang_type) {
570switch (lang_type) {
571case lldb::eLanguageTypeC:
572case lldb::eLanguageTypeC89:
573case lldb::eLanguageTypeC99:
574case lldb::eLanguageTypeC11:
575case lldb::eLanguageTypeC_plus_plus:
576case lldb::eLanguageTypeC_plus_plus_03:
577case lldb::eLanguageTypeC_plus_plus_11:
578case lldb::eLanguageTypeC_plus_plus_14:
579return {lldb::eLanguageTypeC_plus_plus, lldb::eLanguageTypeObjC};
580default:
581return {lang_type};
582}
583llvm_unreachable("Fully covered switch");
584}
585
586LanguageCategory *
587FormatManager::GetCategoryForLanguage(lldb::LanguageType lang_type) {
588std::lock_guard<std::recursive_mutex> guard(m_language_categories_mutex);
589auto iter = m_language_categories_map.find(lang_type),
590end = m_language_categories_map.end();
591if (iter != end)
592return iter->second.get();
593LanguageCategory *lang_category = new LanguageCategory(lang_type);
594m_language_categories_map[lang_type] =
595LanguageCategory::UniquePointer(lang_category);
596return lang_category;
597}
598
599template <typename ImplSP>
600ImplSP FormatManager::GetHardcoded(FormattersMatchData &match_data) {
601ImplSP retval_sp;
602for (lldb::LanguageType lang_type : match_data.GetCandidateLanguages()) {
603if (LanguageCategory *lang_category = GetCategoryForLanguage(lang_type)) {
604if (lang_category->GetHardcoded(*this, match_data, retval_sp))
605return retval_sp;
606}
607}
608return retval_sp;
609}
610
611namespace {
612template <typename ImplSP> const char *FormatterKind;
613template <> const char *FormatterKind<lldb::TypeFormatImplSP> = "format";
614template <> const char *FormatterKind<lldb::TypeSummaryImplSP> = "summary";
615template <> const char *FormatterKind<lldb::SyntheticChildrenSP> = "synthetic";
616} // namespace
617
618#define FORMAT_LOG(Message) "[%s] " Message, FormatterKind<ImplSP>
619
620template <typename ImplSP>
621ImplSP FormatManager::Get(ValueObject &valobj,
622lldb::DynamicValueType use_dynamic) {
623FormattersMatchData match_data(valobj, use_dynamic);
624if (ImplSP retval_sp = GetCached<ImplSP>(match_data))
625return retval_sp;
626
627Log *log = GetLog(LLDBLog::DataFormatters);
628
629LLDB_LOGF(log, FORMAT_LOG("Search failed. Giving language a chance."));
630for (lldb::LanguageType lang_type : match_data.GetCandidateLanguages()) {
631if (LanguageCategory *lang_category = GetCategoryForLanguage(lang_type)) {
632ImplSP retval_sp;
633if (lang_category->Get(match_data, retval_sp))
634if (retval_sp) {
635LLDB_LOGF(log, FORMAT_LOG("Language search success. Returning."));
636return retval_sp;
637}
638}
639}
640
641LLDB_LOGF(log, FORMAT_LOG("Search failed. Giving hardcoded a chance."));
642return GetHardcoded<ImplSP>(match_data);
643}
644
645template <typename ImplSP>
646ImplSP FormatManager::GetCached(FormattersMatchData &match_data) {
647ImplSP retval_sp;
648Log *log = GetLog(LLDBLog::DataFormatters);
649if (match_data.GetTypeForCache()) {
650LLDB_LOGF(log, "\n\n" FORMAT_LOG("Looking into cache for type %s"),
651match_data.GetTypeForCache().AsCString("<invalid>"));
652if (m_format_cache.Get(match_data.GetTypeForCache(), retval_sp)) {
653if (log) {
654LLDB_LOGF(log, FORMAT_LOG("Cache search success. Returning."));
655LLDB_LOGV(log, "Cache hits: {0} - Cache Misses: {1}",
656m_format_cache.GetCacheHits(),
657m_format_cache.GetCacheMisses());
658}
659return retval_sp;
660}
661LLDB_LOGF(log, FORMAT_LOG("Cache search failed. Going normal route"));
662}
663
664m_categories_map.Get(match_data, retval_sp);
665if (match_data.GetTypeForCache() && (!retval_sp || !retval_sp->NonCacheable())) {
666LLDB_LOGF(log, FORMAT_LOG("Caching %p for type %s"),
667static_cast<void *>(retval_sp.get()),
668match_data.GetTypeForCache().AsCString("<invalid>"));
669m_format_cache.Set(match_data.GetTypeForCache(), retval_sp);
670}
671LLDB_LOGV(log, "Cache hits: {0} - Cache Misses: {1}",
672m_format_cache.GetCacheHits(), m_format_cache.GetCacheMisses());
673return retval_sp;
674}
675
676#undef FORMAT_LOG
677
678lldb::TypeFormatImplSP
679FormatManager::GetFormat(ValueObject &valobj,
680lldb::DynamicValueType use_dynamic) {
681return Get<lldb::TypeFormatImplSP>(valobj, use_dynamic);
682}
683
684lldb::TypeSummaryImplSP
685FormatManager::GetSummaryFormat(ValueObject &valobj,
686lldb::DynamicValueType use_dynamic) {
687return Get<lldb::TypeSummaryImplSP>(valobj, use_dynamic);
688}
689
690lldb::SyntheticChildrenSP
691FormatManager::GetSyntheticChildren(ValueObject &valobj,
692lldb::DynamicValueType use_dynamic) {
693return Get<lldb::SyntheticChildrenSP>(valobj, use_dynamic);
694}
695
696FormatManager::FormatManager()
697: m_last_revision(0), m_format_cache(), m_language_categories_mutex(),
698m_language_categories_map(), m_named_summaries_map(this),
699m_categories_map(this), m_default_category_name(ConstString("default")),
700m_system_category_name(ConstString("system")),
701m_vectortypes_category_name(ConstString("VectorTypes")) {
702LoadSystemFormatters();
703LoadVectorFormatters();
704
705EnableCategory(m_vectortypes_category_name, TypeCategoryMap::Last,
706lldb::eLanguageTypeObjC_plus_plus);
707EnableCategory(m_system_category_name, TypeCategoryMap::Last,
708lldb::eLanguageTypeObjC_plus_plus);
709}
710
711void FormatManager::LoadSystemFormatters() {
712TypeSummaryImpl::Flags string_flags;
713string_flags.SetCascades(true)
714.SetSkipPointers(true)
715.SetSkipReferences(false)
716.SetDontShowChildren(true)
717.SetDontShowValue(false)
718.SetShowMembersOneLiner(false)
719.SetHideItemNames(false);
720
721TypeSummaryImpl::Flags string_array_flags;
722string_array_flags.SetCascades(true)
723.SetSkipPointers(true)
724.SetSkipReferences(false)
725.SetDontShowChildren(true)
726.SetDontShowValue(true)
727.SetShowMembersOneLiner(false)
728.SetHideItemNames(false);
729
730lldb::TypeSummaryImplSP string_format(
731new StringSummaryFormat(string_flags, "${var%s}"));
732
733lldb::TypeSummaryImplSP string_array_format(
734new StringSummaryFormat(string_array_flags, "${var%char[]}"));
735
736TypeCategoryImpl::SharedPointer sys_category_sp =
737GetCategory(m_system_category_name);
738
739sys_category_sp->AddTypeSummary(R"(^(unsigned )?char ?(\*|\[\])$)",
740eFormatterMatchRegex, string_format);
741
742sys_category_sp->AddTypeSummary(R"(^((un)?signed )?char ?\[[0-9]+\]$)",
743eFormatterMatchRegex, string_array_format);
744
745lldb::TypeSummaryImplSP ostype_summary(
746new StringSummaryFormat(TypeSummaryImpl::Flags()
747.SetCascades(false)
748.SetSkipPointers(true)
749.SetSkipReferences(true)
750.SetDontShowChildren(true)
751.SetDontShowValue(false)
752.SetShowMembersOneLiner(false)
753.SetHideItemNames(false),
754"${var%O}"));
755
756sys_category_sp->AddTypeSummary("OSType", eFormatterMatchExact,
757ostype_summary);
758
759TypeFormatImpl::Flags fourchar_flags;
760fourchar_flags.SetCascades(true).SetSkipPointers(true).SetSkipReferences(
761true);
762
763AddFormat(sys_category_sp, lldb::eFormatOSType, "FourCharCode",
764fourchar_flags);
765}
766
767void FormatManager::LoadVectorFormatters() {
768TypeCategoryImpl::SharedPointer vectors_category_sp =
769GetCategory(m_vectortypes_category_name);
770
771TypeSummaryImpl::Flags vector_flags;
772vector_flags.SetCascades(true)
773.SetSkipPointers(true)
774.SetSkipReferences(false)
775.SetDontShowChildren(true)
776.SetDontShowValue(false)
777.SetShowMembersOneLiner(true)
778.SetHideItemNames(true);
779
780AddStringSummary(vectors_category_sp, "${var.uint128}", "builtin_type_vec128",
781vector_flags);
782AddStringSummary(vectors_category_sp, "", "float[4]", vector_flags);
783AddStringSummary(vectors_category_sp, "", "int32_t[4]", vector_flags);
784AddStringSummary(vectors_category_sp, "", "int16_t[8]", vector_flags);
785AddStringSummary(vectors_category_sp, "", "vDouble", vector_flags);
786AddStringSummary(vectors_category_sp, "", "vFloat", vector_flags);
787AddStringSummary(vectors_category_sp, "", "vSInt8", vector_flags);
788AddStringSummary(vectors_category_sp, "", "vSInt16", vector_flags);
789AddStringSummary(vectors_category_sp, "", "vSInt32", vector_flags);
790AddStringSummary(vectors_category_sp, "", "vUInt16", vector_flags);
791AddStringSummary(vectors_category_sp, "", "vUInt8", vector_flags);
792AddStringSummary(vectors_category_sp, "", "vUInt16", vector_flags);
793AddStringSummary(vectors_category_sp, "", "vUInt32", vector_flags);
794AddStringSummary(vectors_category_sp, "", "vBool32", vector_flags);
795}
796