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_
16 #include <initializer_list>
21 // Other libraries and framework includes
23 #include "lldb/lldb-enumerations.h"
24 #include "lldb/lldb-public.h"
26 #include "lldb/DataFormatters/FormatCache.h"
27 #include "lldb/DataFormatters/FormatClasses.h"
28 #include "lldb/DataFormatters/FormattersContainer.h"
29 #include "lldb/DataFormatters/LanguageCategory.h"
30 #include "lldb/DataFormatters/TypeCategory.h"
31 #include "lldb/DataFormatters/TypeCategoryMap.h"
33 namespace lldb_private {
35 // this file (and its. cpp) contain the low-level implementation of LLDB Data
36 // Visualization class DataVisualization is the high-level front-end of this
37 // feature clients should refer to that class as the entry-point into the data
38 // formatters unless they have a good reason to bypass it and prefer to use
39 // this file's objects directly
41 class FormatManager : public IFormatChangeListener {
42 typedef FormatMap<ConstString, TypeSummaryImpl> NamedSummariesMap;
43 typedef TypeCategoryMap::MapType::iterator CategoryMapIterator;
46 typedef std::map<lldb::LanguageType, LanguageCategory::UniquePointer>
51 ~FormatManager() override = default;
53 NamedSummariesMap &GetNamedSummaryContainer() {
54 return m_named_summaries_map;
58 EnableCategory(const ConstString &category_name,
59 TypeCategoryMap::Position pos = TypeCategoryMap::Default) {
60 EnableCategory(category_name, pos,
61 std::initializer_list<lldb::LanguageType>());
64 void EnableCategory(const ConstString &category_name,
65 TypeCategoryMap::Position pos, lldb::LanguageType lang) {
66 std::initializer_list<lldb::LanguageType> langs = {lang};
67 EnableCategory(category_name, pos, langs);
70 void EnableCategory(const ConstString &category_name,
71 TypeCategoryMap::Position pos = TypeCategoryMap::Default,
72 std::initializer_list<lldb::LanguageType> langs = {}) {
73 TypeCategoryMap::ValueSP category_sp;
74 if (m_categories_map.Get(category_name, category_sp) && category_sp) {
75 m_categories_map.Enable(category_sp, pos);
76 for (const lldb::LanguageType lang : langs)
77 category_sp->AddLanguage(lang);
81 void DisableCategory(const ConstString &category_name) {
82 m_categories_map.Disable(category_name);
86 EnableCategory(const lldb::TypeCategoryImplSP &category,
87 TypeCategoryMap::Position pos = TypeCategoryMap::Default) {
88 m_categories_map.Enable(category, pos);
91 void DisableCategory(const lldb::TypeCategoryImplSP &category) {
92 m_categories_map.Disable(category);
95 void EnableAllCategories();
97 void DisableAllCategories();
99 bool DeleteCategory(const ConstString &category_name) {
100 return m_categories_map.Delete(category_name);
103 void ClearCategories() { return m_categories_map.Clear(); }
105 uint32_t GetCategoriesCount() { return m_categories_map.GetCount(); }
107 lldb::TypeCategoryImplSP GetCategoryAtIndex(size_t index) {
108 return m_categories_map.GetAtIndex(index);
111 void ForEachCategory(TypeCategoryMap::ForEachCallback callback);
113 lldb::TypeCategoryImplSP GetCategory(const char *category_name = nullptr,
114 bool can_create = true) {
116 return GetCategory(m_default_category_name);
117 return GetCategory(ConstString(category_name));
120 lldb::TypeCategoryImplSP GetCategory(const ConstString &category_name,
121 bool can_create = true);
123 lldb::TypeFormatImplSP
124 GetFormatForType(lldb::TypeNameSpecifierImplSP type_sp);
126 lldb::TypeSummaryImplSP
127 GetSummaryForType(lldb::TypeNameSpecifierImplSP type_sp);
129 lldb::TypeFilterImplSP
130 GetFilterForType(lldb::TypeNameSpecifierImplSP type_sp);
132 #ifndef LLDB_DISABLE_PYTHON
133 lldb::ScriptedSyntheticChildrenSP
134 GetSyntheticForType(lldb::TypeNameSpecifierImplSP type_sp);
137 #ifndef LLDB_DISABLE_PYTHON
138 lldb::SyntheticChildrenSP
139 GetSyntheticChildrenForType(lldb::TypeNameSpecifierImplSP type_sp);
142 lldb::TypeValidatorImplSP
143 GetValidatorForType(lldb::TypeNameSpecifierImplSP type_sp);
145 lldb::TypeFormatImplSP GetFormat(ValueObject &valobj,
146 lldb::DynamicValueType use_dynamic);
148 lldb::TypeSummaryImplSP GetSummaryFormat(ValueObject &valobj,
149 lldb::DynamicValueType use_dynamic);
151 #ifndef LLDB_DISABLE_PYTHON
152 lldb::SyntheticChildrenSP
153 GetSyntheticChildren(ValueObject &valobj, lldb::DynamicValueType use_dynamic);
156 lldb::TypeValidatorImplSP GetValidator(ValueObject &valobj,
157 lldb::DynamicValueType use_dynamic);
160 AnyMatches(ConstString type_name,
161 TypeCategoryImpl::FormatCategoryItems items =
162 TypeCategoryImpl::ALL_ITEM_TYPES,
163 bool only_enabled = true, const char **matching_category = nullptr,
164 TypeCategoryImpl::FormatCategoryItems *matching_type = nullptr) {
165 return m_categories_map.AnyMatches(type_name, items, only_enabled,
166 matching_category, matching_type);
169 static bool GetFormatFromCString(const char *format_cstr,
170 bool partial_match_ok, lldb::Format &format);
172 static char GetFormatAsFormatChar(lldb::Format format);
174 static const char *GetFormatAsCString(lldb::Format format);
176 // if the user tries to add formatters for, say, "struct Foo" those will not
177 // match any type because of the way we strip qualifiers from typenames this
178 // method looks for the case where the user is adding a
179 // "class","struct","enum" or "union" Foo and strips the unnecessary
181 static ConstString GetValidTypeName(const ConstString &type);
183 // when DataExtractor dumps a vectorOfT, it uses a predefined format for each
184 // item this method returns it, or eFormatInvalid if vector_format is not a
186 static lldb::Format GetSingleItemFormat(lldb::Format vector_format);
188 // this returns true if the ValueObjectPrinter is *highly encouraged* to
189 // actually represent this ValueObject in one-liner format If this object has
190 // a summary formatter, however, we should not try and do one-lining, just
191 // let the summary do the right thing
192 bool ShouldPrintAsOneLiner(ValueObject &valobj);
194 void Changed() override;
196 uint32_t GetCurrentRevision() override { return m_last_revision; }
198 static FormattersMatchVector
199 GetPossibleMatches(ValueObject &valobj, lldb::DynamicValueType use_dynamic) {
200 FormattersMatchVector matches;
201 GetPossibleMatches(valobj, valobj.GetCompilerType(),
202 lldb_private::eFormatterChoiceCriterionDirectChoice,
203 use_dynamic, matches, false, false, false, true);
207 static ConstString GetTypeForCache(ValueObject &, lldb::DynamicValueType);
209 LanguageCategory *GetCategoryForLanguage(lldb::LanguageType lang_type);
211 static std::vector<lldb::LanguageType>
212 GetCandidateLanguages(lldb::LanguageType lang_type);
215 static std::vector<lldb::LanguageType>
216 GetCandidateLanguages(ValueObject &valobj);
218 static void GetPossibleMatches(ValueObject &valobj,
219 CompilerType compiler_type, uint32_t reason,
220 lldb::DynamicValueType use_dynamic,
221 FormattersMatchVector &entries,
222 bool did_strip_ptr, bool did_strip_ref,
223 bool did_strip_typedef,
224 bool root_level = false);
226 std::atomic<uint32_t> m_last_revision;
227 FormatCache m_format_cache;
228 std::recursive_mutex m_language_categories_mutex;
229 LanguageCategories m_language_categories_map;
230 NamedSummariesMap m_named_summaries_map;
231 TypeCategoryMap m_categories_map;
233 ConstString m_default_category_name;
234 ConstString m_system_category_name;
235 ConstString m_vectortypes_category_name;
237 lldb::TypeFormatImplSP GetHardcodedFormat(FormattersMatchData &);
239 lldb::TypeSummaryImplSP GetHardcodedSummaryFormat(FormattersMatchData &);
241 lldb::SyntheticChildrenSP
242 GetHardcodedSyntheticChildren(FormattersMatchData &);
244 lldb::TypeValidatorImplSP GetHardcodedValidator(FormattersMatchData &);
246 TypeCategoryMap &GetCategories() { return m_categories_map; }
248 // These functions are meant to initialize formatters that are very low-
249 // level/global in nature and do not naturally belong in any language. The
250 // intent is that most formatters go in language-specific categories.
251 // Eventually, the runtimes should also be allowed to vend their own
252 // formatters, and then one could put formatters that depend on specific
253 // library load events in the language runtimes, on an as-needed basis
254 void LoadSystemFormatters();
256 void LoadVectorFormatters();
258 friend class FormattersMatchData;
261 } // namespace lldb_private
263 #endif // lldb_FormatManager_h_