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
37 // class DataVisualization is the high-level front-end of this feature
38 // clients should refer to that class as the entry-point into the data
40 // unless they have a good reason to bypass it and prefer to use this file's
43 class FormatManager : public IFormatChangeListener {
44 typedef FormatMap<ConstString, TypeSummaryImpl> NamedSummariesMap;
45 typedef TypeCategoryMap::MapType::iterator CategoryMapIterator;
48 typedef std::map<lldb::LanguageType, LanguageCategory::UniquePointer>
53 ~FormatManager() override = default;
55 NamedSummariesMap &GetNamedSummaryContainer() {
56 return m_named_summaries_map;
60 EnableCategory(const ConstString &category_name,
61 TypeCategoryMap::Position pos = TypeCategoryMap::Default) {
62 EnableCategory(category_name, pos,
63 std::initializer_list<lldb::LanguageType>());
66 void EnableCategory(const ConstString &category_name,
67 TypeCategoryMap::Position pos, lldb::LanguageType lang) {
68 std::initializer_list<lldb::LanguageType> langs = {lang};
69 EnableCategory(category_name, pos, langs);
72 void EnableCategory(const ConstString &category_name,
73 TypeCategoryMap::Position pos = TypeCategoryMap::Default,
74 std::initializer_list<lldb::LanguageType> langs = {}) {
75 TypeCategoryMap::ValueSP category_sp;
76 if (m_categories_map.Get(category_name, category_sp) && category_sp) {
77 m_categories_map.Enable(category_sp, pos);
78 for (const lldb::LanguageType lang : langs)
79 category_sp->AddLanguage(lang);
83 void DisableCategory(const ConstString &category_name) {
84 m_categories_map.Disable(category_name);
88 EnableCategory(const lldb::TypeCategoryImplSP &category,
89 TypeCategoryMap::Position pos = TypeCategoryMap::Default) {
90 m_categories_map.Enable(category, pos);
93 void DisableCategory(const lldb::TypeCategoryImplSP &category) {
94 m_categories_map.Disable(category);
97 void EnableAllCategories();
99 void DisableAllCategories();
101 bool DeleteCategory(const ConstString &category_name) {
102 return m_categories_map.Delete(category_name);
105 void ClearCategories() { return m_categories_map.Clear(); }
107 uint32_t GetCategoriesCount() { return m_categories_map.GetCount(); }
109 lldb::TypeCategoryImplSP GetCategoryAtIndex(size_t index) {
110 return m_categories_map.GetAtIndex(index);
113 void ForEachCategory(TypeCategoryMap::ForEachCallback callback);
115 lldb::TypeCategoryImplSP GetCategory(const char *category_name = nullptr,
116 bool can_create = true) {
118 return GetCategory(m_default_category_name);
119 return GetCategory(ConstString(category_name));
122 lldb::TypeCategoryImplSP GetCategory(const ConstString &category_name,
123 bool can_create = true);
125 lldb::TypeFormatImplSP
126 GetFormatForType(lldb::TypeNameSpecifierImplSP type_sp);
128 lldb::TypeSummaryImplSP
129 GetSummaryForType(lldb::TypeNameSpecifierImplSP type_sp);
131 lldb::TypeFilterImplSP
132 GetFilterForType(lldb::TypeNameSpecifierImplSP type_sp);
134 #ifndef LLDB_DISABLE_PYTHON
135 lldb::ScriptedSyntheticChildrenSP
136 GetSyntheticForType(lldb::TypeNameSpecifierImplSP type_sp);
139 #ifndef LLDB_DISABLE_PYTHON
140 lldb::SyntheticChildrenSP
141 GetSyntheticChildrenForType(lldb::TypeNameSpecifierImplSP type_sp);
144 lldb::TypeValidatorImplSP
145 GetValidatorForType(lldb::TypeNameSpecifierImplSP type_sp);
147 lldb::TypeFormatImplSP GetFormat(ValueObject &valobj,
148 lldb::DynamicValueType use_dynamic);
150 lldb::TypeSummaryImplSP GetSummaryFormat(ValueObject &valobj,
151 lldb::DynamicValueType use_dynamic);
153 #ifndef LLDB_DISABLE_PYTHON
154 lldb::SyntheticChildrenSP
155 GetSyntheticChildren(ValueObject &valobj, lldb::DynamicValueType use_dynamic);
158 lldb::TypeValidatorImplSP GetValidator(ValueObject &valobj,
159 lldb::DynamicValueType use_dynamic);
162 AnyMatches(ConstString type_name,
163 TypeCategoryImpl::FormatCategoryItems items =
164 TypeCategoryImpl::ALL_ITEM_TYPES,
165 bool only_enabled = true, const char **matching_category = nullptr,
166 TypeCategoryImpl::FormatCategoryItems *matching_type = nullptr) {
167 return m_categories_map.AnyMatches(type_name, items, only_enabled,
168 matching_category, matching_type);
171 static bool GetFormatFromCString(const char *format_cstr,
172 bool partial_match_ok, lldb::Format &format);
174 static char GetFormatAsFormatChar(lldb::Format format);
176 static const char *GetFormatAsCString(lldb::Format format);
178 // if the user tries to add formatters for, say, "struct Foo"
179 // those will not match any type because of the way we strip qualifiers from
181 // this method looks for the case where the user is adding a
182 // "class","struct","enum" or "union" Foo
183 // and strips the unnecessary qualifier
184 static ConstString GetValidTypeName(const ConstString &type);
186 // when DataExtractor dumps a vectorOfT, it uses a predefined format for each
188 // this method returns it, or eFormatInvalid if vector_format is not a
190 static lldb::Format GetSingleItemFormat(lldb::Format vector_format);
192 // this returns true if the ValueObjectPrinter is *highly encouraged*
193 // to actually represent this ValueObject in one-liner format
194 // If this object has a summary formatter, however, we should not
195 // try and do one-lining, just let the summary do the right thing
196 bool ShouldPrintAsOneLiner(ValueObject &valobj);
198 void Changed() override;
200 uint32_t GetCurrentRevision() override { return m_last_revision; }
202 static FormattersMatchVector
203 GetPossibleMatches(ValueObject &valobj, lldb::DynamicValueType use_dynamic) {
204 FormattersMatchVector matches;
205 GetPossibleMatches(valobj, valobj.GetCompilerType(),
206 lldb_private::eFormatterChoiceCriterionDirectChoice,
207 use_dynamic, matches, false, false, false, true);
211 static ConstString GetTypeForCache(ValueObject &, lldb::DynamicValueType);
213 LanguageCategory *GetCategoryForLanguage(lldb::LanguageType lang_type);
215 static std::vector<lldb::LanguageType>
216 GetCandidateLanguages(lldb::LanguageType lang_type);
219 static std::vector<lldb::LanguageType>
220 GetCandidateLanguages(ValueObject &valobj);
222 static void GetPossibleMatches(ValueObject &valobj,
223 CompilerType compiler_type, uint32_t reason,
224 lldb::DynamicValueType use_dynamic,
225 FormattersMatchVector &entries,
226 bool did_strip_ptr, bool did_strip_ref,
227 bool did_strip_typedef,
228 bool root_level = false);
230 std::atomic<uint32_t> m_last_revision;
231 FormatCache m_format_cache;
232 std::recursive_mutex m_language_categories_mutex;
233 LanguageCategories m_language_categories_map;
234 NamedSummariesMap m_named_summaries_map;
235 TypeCategoryMap m_categories_map;
237 ConstString m_default_category_name;
238 ConstString m_system_category_name;
239 ConstString m_vectortypes_category_name;
241 lldb::TypeFormatImplSP GetHardcodedFormat(FormattersMatchData &);
243 lldb::TypeSummaryImplSP GetHardcodedSummaryFormat(FormattersMatchData &);
245 lldb::SyntheticChildrenSP
246 GetHardcodedSyntheticChildren(FormattersMatchData &);
248 lldb::TypeValidatorImplSP GetHardcodedValidator(FormattersMatchData &);
250 TypeCategoryMap &GetCategories() { return m_categories_map; }
252 // These functions are meant to initialize formatters that are very
253 // low-level/global in nature
254 // and do not naturally belong in any language. The intent is that most
256 // language-specific categories. Eventually, the runtimes should also be
257 // allowed to vend their
258 // own formatters, and then one could put formatters that depend on specific
259 // library load events
260 // in the language runtimes, on an as-needed basis
261 void LoadSystemFormatters();
263 void LoadVectorFormatters();
265 friend class FormattersMatchData;
268 } // namespace lldb_private
270 #endif // lldb_FormatManager_h_