1 //===-- FormatManager.h -----------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef lldb_FormatManager_h_
11 #define lldb_FormatManager_h_
14 #include <initializer_list>
19 #include "lldb/lldb-enumerations.h"
20 #include "lldb/lldb-public.h"
22 #include "lldb/DataFormatters/FormatCache.h"
23 #include "lldb/DataFormatters/FormatClasses.h"
24 #include "lldb/DataFormatters/FormattersContainer.h"
25 #include "lldb/DataFormatters/LanguageCategory.h"
26 #include "lldb/DataFormatters/TypeCategory.h"
27 #include "lldb/DataFormatters/TypeCategoryMap.h"
29 namespace lldb_private {
31 // this file (and its. cpp) contain the low-level implementation of LLDB Data
32 // Visualization class DataVisualization is the high-level front-end of this
33 // feature clients should refer to that class as the entry-point into the data
34 // formatters unless they have a good reason to bypass it and prefer to use
35 // this file's objects directly
37 class FormatManager : public IFormatChangeListener {
38 typedef FormatMap<ConstString, TypeSummaryImpl> NamedSummariesMap;
39 typedef TypeCategoryMap::MapType::iterator CategoryMapIterator;
42 typedef std::map<lldb::LanguageType, LanguageCategory::UniquePointer>
47 ~FormatManager() override = default;
49 NamedSummariesMap &GetNamedSummaryContainer() {
50 return m_named_summaries_map;
54 EnableCategory(const ConstString &category_name,
55 TypeCategoryMap::Position pos = TypeCategoryMap::Default) {
56 EnableCategory(category_name, pos,
57 std::initializer_list<lldb::LanguageType>());
60 void EnableCategory(const ConstString &category_name,
61 TypeCategoryMap::Position pos, lldb::LanguageType lang) {
62 std::initializer_list<lldb::LanguageType> langs = {lang};
63 EnableCategory(category_name, pos, langs);
66 void EnableCategory(const ConstString &category_name,
67 TypeCategoryMap::Position pos = TypeCategoryMap::Default,
68 std::initializer_list<lldb::LanguageType> langs = {}) {
69 TypeCategoryMap::ValueSP category_sp;
70 if (m_categories_map.Get(category_name, category_sp) && category_sp) {
71 m_categories_map.Enable(category_sp, pos);
72 for (const lldb::LanguageType lang : langs)
73 category_sp->AddLanguage(lang);
77 void DisableCategory(const ConstString &category_name) {
78 m_categories_map.Disable(category_name);
82 EnableCategory(const lldb::TypeCategoryImplSP &category,
83 TypeCategoryMap::Position pos = TypeCategoryMap::Default) {
84 m_categories_map.Enable(category, pos);
87 void DisableCategory(const lldb::TypeCategoryImplSP &category) {
88 m_categories_map.Disable(category);
91 void EnableAllCategories();
93 void DisableAllCategories();
95 bool DeleteCategory(const ConstString &category_name) {
96 return m_categories_map.Delete(category_name);
99 void ClearCategories() { return m_categories_map.Clear(); }
101 uint32_t GetCategoriesCount() { return m_categories_map.GetCount(); }
103 lldb::TypeCategoryImplSP GetCategoryAtIndex(size_t index) {
104 return m_categories_map.GetAtIndex(index);
107 void ForEachCategory(TypeCategoryMap::ForEachCallback callback);
109 lldb::TypeCategoryImplSP GetCategory(const char *category_name = nullptr,
110 bool can_create = true) {
112 return GetCategory(m_default_category_name);
113 return GetCategory(ConstString(category_name));
116 lldb::TypeCategoryImplSP GetCategory(const ConstString &category_name,
117 bool can_create = true);
119 lldb::TypeFormatImplSP
120 GetFormatForType(lldb::TypeNameSpecifierImplSP type_sp);
122 lldb::TypeSummaryImplSP
123 GetSummaryForType(lldb::TypeNameSpecifierImplSP type_sp);
125 lldb::TypeFilterImplSP
126 GetFilterForType(lldb::TypeNameSpecifierImplSP type_sp);
128 #ifndef LLDB_DISABLE_PYTHON
129 lldb::ScriptedSyntheticChildrenSP
130 GetSyntheticForType(lldb::TypeNameSpecifierImplSP type_sp);
133 #ifndef LLDB_DISABLE_PYTHON
134 lldb::SyntheticChildrenSP
135 GetSyntheticChildrenForType(lldb::TypeNameSpecifierImplSP type_sp);
138 lldb::TypeValidatorImplSP
139 GetValidatorForType(lldb::TypeNameSpecifierImplSP type_sp);
141 lldb::TypeFormatImplSP GetFormat(ValueObject &valobj,
142 lldb::DynamicValueType use_dynamic);
144 lldb::TypeSummaryImplSP GetSummaryFormat(ValueObject &valobj,
145 lldb::DynamicValueType use_dynamic);
147 #ifndef LLDB_DISABLE_PYTHON
148 lldb::SyntheticChildrenSP
149 GetSyntheticChildren(ValueObject &valobj, lldb::DynamicValueType use_dynamic);
152 lldb::TypeValidatorImplSP GetValidator(ValueObject &valobj,
153 lldb::DynamicValueType use_dynamic);
156 AnyMatches(ConstString type_name,
157 TypeCategoryImpl::FormatCategoryItems items =
158 TypeCategoryImpl::ALL_ITEM_TYPES,
159 bool only_enabled = true, const char **matching_category = nullptr,
160 TypeCategoryImpl::FormatCategoryItems *matching_type = nullptr) {
161 return m_categories_map.AnyMatches(type_name, items, only_enabled,
162 matching_category, matching_type);
165 static bool GetFormatFromCString(const char *format_cstr,
166 bool partial_match_ok, lldb::Format &format);
168 static char GetFormatAsFormatChar(lldb::Format format);
170 static const char *GetFormatAsCString(lldb::Format format);
172 // if the user tries to add formatters for, say, "struct Foo" those will not
173 // match any type because of the way we strip qualifiers from typenames this
174 // method looks for the case where the user is adding a
175 // "class","struct","enum" or "union" Foo and strips the unnecessary
177 static ConstString GetValidTypeName(const ConstString &type);
179 // when DataExtractor dumps a vectorOfT, it uses a predefined format for each
180 // item this method returns it, or eFormatInvalid if vector_format is not a
182 static lldb::Format GetSingleItemFormat(lldb::Format vector_format);
184 // this returns true if the ValueObjectPrinter is *highly encouraged* to
185 // actually represent this ValueObject in one-liner format If this object has
186 // a summary formatter, however, we should not try and do one-lining, just
187 // let the summary do the right thing
188 bool ShouldPrintAsOneLiner(ValueObject &valobj);
190 void Changed() override;
192 uint32_t GetCurrentRevision() override { return m_last_revision; }
194 static FormattersMatchVector
195 GetPossibleMatches(ValueObject &valobj, lldb::DynamicValueType use_dynamic) {
196 FormattersMatchVector matches;
197 GetPossibleMatches(valobj, valobj.GetCompilerType(),
198 lldb_private::eFormatterChoiceCriterionDirectChoice,
199 use_dynamic, matches, false, false, false, true);
203 static ConstString GetTypeForCache(ValueObject &, lldb::DynamicValueType);
205 LanguageCategory *GetCategoryForLanguage(lldb::LanguageType lang_type);
207 static std::vector<lldb::LanguageType>
208 GetCandidateLanguages(lldb::LanguageType lang_type);
211 static std::vector<lldb::LanguageType>
212 GetCandidateLanguages(ValueObject &valobj);
214 static void GetPossibleMatches(ValueObject &valobj,
215 CompilerType compiler_type, uint32_t reason,
216 lldb::DynamicValueType use_dynamic,
217 FormattersMatchVector &entries,
218 bool did_strip_ptr, bool did_strip_ref,
219 bool did_strip_typedef,
220 bool root_level = false);
222 std::atomic<uint32_t> m_last_revision;
223 FormatCache m_format_cache;
224 std::recursive_mutex m_language_categories_mutex;
225 LanguageCategories m_language_categories_map;
226 NamedSummariesMap m_named_summaries_map;
227 TypeCategoryMap m_categories_map;
229 ConstString m_default_category_name;
230 ConstString m_system_category_name;
231 ConstString m_vectortypes_category_name;
233 lldb::TypeFormatImplSP GetHardcodedFormat(FormattersMatchData &);
235 lldb::TypeSummaryImplSP GetHardcodedSummaryFormat(FormattersMatchData &);
237 lldb::SyntheticChildrenSP
238 GetHardcodedSyntheticChildren(FormattersMatchData &);
240 lldb::TypeValidatorImplSP GetHardcodedValidator(FormattersMatchData &);
242 TypeCategoryMap &GetCategories() { return m_categories_map; }
244 // These functions are meant to initialize formatters that are very low-
245 // level/global in nature and do not naturally belong in any language. The
246 // intent is that most formatters go in language-specific categories.
247 // Eventually, the runtimes should also be allowed to vend their own
248 // formatters, and then one could put formatters that depend on specific
249 // library load events in the language runtimes, on an as-needed basis
250 void LoadSystemFormatters();
252 void LoadVectorFormatters();
254 friend class FormattersMatchData;
257 } // namespace lldb_private
259 #endif // lldb_FormatManager_h_