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-public.h"
24 #include "lldb/lldb-enumerations.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 Visualization
36 // class DataVisualization is the high-level front-end of this feature
37 // clients should refer to that class as the entry-point into the data formatters
38 // unless they have a good reason to bypass it and prefer to use this file's objects directly
40 class FormatManager : public IFormatChangeListener
42 typedef FormatMap<ConstString, TypeSummaryImpl> NamedSummariesMap;
43 typedef TypeCategoryMap::MapType::iterator CategoryMapIterator;
45 typedef std::map<lldb::LanguageType, LanguageCategory::UniquePointer> LanguageCategories;
49 ~FormatManager() override = default;
52 GetNamedSummaryContainer ()
54 return m_named_summaries_map;
58 EnableCategory (const ConstString& category_name,
59 TypeCategoryMap::Position pos = TypeCategoryMap::Default)
61 EnableCategory(category_name,
63 std::initializer_list<lldb::LanguageType>());
67 EnableCategory (const ConstString& category_name,
68 TypeCategoryMap::Position pos,
69 lldb::LanguageType lang)
71 std::initializer_list<lldb::LanguageType> langs = {lang};
72 EnableCategory(category_name,
78 EnableCategory (const ConstString& category_name,
79 TypeCategoryMap::Position pos = TypeCategoryMap::Default,
80 std::initializer_list<lldb::LanguageType> langs = {})
82 TypeCategoryMap::ValueSP category_sp;
83 if (m_categories_map.Get(category_name, category_sp) && category_sp)
85 m_categories_map.Enable(category_sp, pos);
86 for (const lldb::LanguageType lang : langs)
87 category_sp->AddLanguage(lang);
92 DisableCategory (const ConstString& category_name)
94 m_categories_map.Disable(category_name);
98 EnableCategory (const lldb::TypeCategoryImplSP& category,
99 TypeCategoryMap::Position pos = TypeCategoryMap::Default)
101 m_categories_map.Enable(category,
106 DisableCategory (const lldb::TypeCategoryImplSP& category)
108 m_categories_map.Disable(category);
112 EnableAllCategories ();
115 DisableAllCategories ();
118 DeleteCategory (const ConstString& category_name)
120 return m_categories_map.Delete(category_name);
126 return m_categories_map.Clear();
130 GetCategoriesCount ()
132 return m_categories_map.GetCount();
135 lldb::TypeCategoryImplSP
136 GetCategoryAtIndex (size_t index)
138 return m_categories_map.GetAtIndex(index);
142 ForEachCategory (TypeCategoryMap::ForEachCallback callback);
144 lldb::TypeCategoryImplSP
145 GetCategory(const char* category_name = nullptr,
146 bool can_create = true)
149 return GetCategory(m_default_category_name);
150 return GetCategory(ConstString(category_name));
153 lldb::TypeCategoryImplSP
154 GetCategory (const ConstString& category_name,
155 bool can_create = true);
157 lldb::TypeFormatImplSP
158 GetFormatForType (lldb::TypeNameSpecifierImplSP type_sp);
160 lldb::TypeSummaryImplSP
161 GetSummaryForType (lldb::TypeNameSpecifierImplSP type_sp);
163 lldb::TypeFilterImplSP
164 GetFilterForType (lldb::TypeNameSpecifierImplSP type_sp);
166 #ifndef LLDB_DISABLE_PYTHON
167 lldb::ScriptedSyntheticChildrenSP
168 GetSyntheticForType (lldb::TypeNameSpecifierImplSP type_sp);
171 #ifndef LLDB_DISABLE_PYTHON
172 lldb::SyntheticChildrenSP
173 GetSyntheticChildrenForType (lldb::TypeNameSpecifierImplSP type_sp);
176 lldb::TypeValidatorImplSP
177 GetValidatorForType (lldb::TypeNameSpecifierImplSP type_sp);
179 lldb::TypeFormatImplSP
180 GetFormat (ValueObject& valobj,
181 lldb::DynamicValueType use_dynamic);
183 lldb::TypeSummaryImplSP
184 GetSummaryFormat (ValueObject& valobj,
185 lldb::DynamicValueType use_dynamic);
187 #ifndef LLDB_DISABLE_PYTHON
188 lldb::SyntheticChildrenSP
189 GetSyntheticChildren (ValueObject& valobj,
190 lldb::DynamicValueType use_dynamic);
193 lldb::TypeValidatorImplSP
194 GetValidator (ValueObject& valobj,
195 lldb::DynamicValueType use_dynamic);
198 AnyMatches(ConstString type_name,
199 TypeCategoryImpl::FormatCategoryItems items = TypeCategoryImpl::ALL_ITEM_TYPES,
200 bool only_enabled = true,
201 const char** matching_category = nullptr,
202 TypeCategoryImpl::FormatCategoryItems* matching_type = nullptr)
204 return m_categories_map.AnyMatches(type_name,
212 GetFormatFromCString (const char *format_cstr,
213 bool partial_match_ok,
214 lldb::Format &format);
217 GetFormatAsFormatChar (lldb::Format format);
220 GetFormatAsCString (lldb::Format format);
222 // if the user tries to add formatters for, say, "struct Foo"
223 // those will not match any type because of the way we strip qualifiers from typenames
224 // this method looks for the case where the user is adding a "class","struct","enum" or "union" Foo
225 // and strips the unnecessary qualifier
227 GetValidTypeName (const ConstString& type);
229 // when DataExtractor dumps a vectorOfT, it uses a predefined format for each item
230 // this method returns it, or eFormatInvalid if vector_format is not a vectorOf
232 GetSingleItemFormat (lldb::Format vector_format);
234 // this returns true if the ValueObjectPrinter is *highly encouraged*
235 // to actually represent this ValueObject in one-liner format
236 // If this object has a summary formatter, however, we should not
237 // try and do one-lining, just let the summary do the right thing
239 ShouldPrintAsOneLiner (ValueObject& valobj);
245 GetCurrentRevision () override
247 return m_last_revision;
250 static FormattersMatchVector
251 GetPossibleMatches (ValueObject& valobj,
252 lldb::DynamicValueType use_dynamic)
254 FormattersMatchVector matches;
255 GetPossibleMatches (valobj,
256 valobj.GetCompilerType(),
257 lldb_private::eFormatterChoiceCriterionDirectChoice,
268 GetTypeForCache (ValueObject&, lldb::DynamicValueType);
271 GetCategoryForLanguage (lldb::LanguageType lang_type);
273 static std::vector<lldb::LanguageType>
274 GetCandidateLanguages (lldb::LanguageType lang_type);
277 static std::vector<lldb::LanguageType>
278 GetCandidateLanguages (ValueObject& valobj);
281 GetPossibleMatches (ValueObject& valobj,
282 CompilerType compiler_type,
284 lldb::DynamicValueType use_dynamic,
285 FormattersMatchVector& entries,
288 bool did_strip_typedef,
289 bool root_level = false);
291 std::atomic<uint32_t> m_last_revision;
292 FormatCache m_format_cache;
293 std::recursive_mutex m_language_categories_mutex;
294 LanguageCategories m_language_categories_map;
295 NamedSummariesMap m_named_summaries_map;
296 TypeCategoryMap m_categories_map;
298 ConstString m_default_category_name;
299 ConstString m_system_category_name;
300 ConstString m_vectortypes_category_name;
302 lldb::TypeFormatImplSP
303 GetHardcodedFormat (FormattersMatchData&);
305 lldb::TypeSummaryImplSP
306 GetHardcodedSummaryFormat (FormattersMatchData&);
308 lldb::SyntheticChildrenSP
309 GetHardcodedSyntheticChildren (FormattersMatchData&);
311 lldb::TypeValidatorImplSP
312 GetHardcodedValidator (FormattersMatchData&);
317 return m_categories_map;
320 // These functions are meant to initialize formatters that are very low-level/global in nature
321 // and do not naturally belong in any language. The intent is that most formatters go in
322 // language-specific categories. Eventually, the runtimes should also be allowed to vend their
323 // own formatters, and then one could put formatters that depend on specific library load events
324 // in the language runtimes, on an as-needed basis
326 LoadSystemFormatters ();
329 LoadVectorFormatters ();
331 friend class FormattersMatchData;
334 } // namespace lldb_private
336 #endif // lldb_FormatManager_h_