xref: /NextBSD/contrib/llvm/tools/lldb/include/lldb/DataFormatters/FormatManager.h (revision 84d351007654069f9643c8e4b4802a7f5f08ee42)
1 //===-- FormatManager.h -------------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef lldb_FormatManager_h_
11 #define lldb_FormatManager_h_
12 
13 // C Includes
14 // C++ Includes
15 
16 // Other libraries and framework includes
17 // Project includes
18 #include "lldb/lldb-public.h"
19 #include "lldb/lldb-enumerations.h"
20 
21 #include "lldb/DataFormatters/FormatCache.h"
22 #include "lldb/DataFormatters/FormatClasses.h"
23 #include "lldb/DataFormatters/FormattersContainer.h"
24 #include "lldb/DataFormatters/TypeCategory.h"
25 #include "lldb/DataFormatters/TypeCategoryMap.h"
26 
27 #include <atomic>
28 #include <functional>
29 
30 namespace lldb_private {
31 
32 // this file (and its. cpp) contain the low-level implementation of LLDB Data Visualization
33 // class DataVisualization is the high-level front-end of this feature
34 // clients should refer to that class as the entry-point into the data formatters
35 // unless they have a good reason to bypass it and prefer to use this file's objects directly
36 
37 class FormatManager : public IFormatChangeListener
38 {
39     typedef FormatMap<ConstString, TypeSummaryImpl> NamedSummariesMap;
40     typedef TypeCategoryMap::MapType::iterator CategoryMapIterator;
41 public:
42 
43     template <typename FormatterType>
44     using HardcodedFormatterFinder = std::function<typename FormatterType::SharedPointer (lldb_private::ValueObject&,
45                                                                                           lldb::DynamicValueType,
46                                                                                           FormatManager&)>;
47 
48     template <typename FormatterType>
49     using HardcodedFormatterFinders = std::vector<HardcodedFormatterFinder<FormatterType>>;
50 
51     typedef TypeCategoryMap::CallbackType CategoryCallback;
52 
53     FormatManager ();
54 
55     NamedSummariesMap&
GetNamedSummaryContainer()56     GetNamedSummaryContainer ()
57     {
58         return m_named_summaries_map;
59     }
60 
61     void
62     EnableCategory (const ConstString& category_name,
63                     TypeCategoryMap::Position pos = TypeCategoryMap::Default)
64     {
65         m_categories_map.Enable(category_name,
66                                 pos);
67     }
68 
69     void
DisableCategory(const ConstString & category_name)70     DisableCategory (const ConstString& category_name)
71     {
72         m_categories_map.Disable(category_name);
73     }
74 
75     void
76     EnableCategory (const lldb::TypeCategoryImplSP& category,
77                     TypeCategoryMap::Position pos = TypeCategoryMap::Default)
78     {
79         m_categories_map.Enable(category,
80                                 pos);
81     }
82 
83     void
DisableCategory(const lldb::TypeCategoryImplSP & category)84     DisableCategory (const lldb::TypeCategoryImplSP& category)
85     {
86         m_categories_map.Disable(category);
87     }
88 
89     void
EnableAllCategories()90     EnableAllCategories ()
91     {
92         m_categories_map.EnableAllCategories ();
93     }
94 
95     void
DisableAllCategories()96     DisableAllCategories ()
97     {
98         m_categories_map.DisableAllCategories ();
99     }
100 
101     bool
DeleteCategory(const ConstString & category_name)102     DeleteCategory (const ConstString& category_name)
103     {
104         return m_categories_map.Delete(category_name);
105     }
106 
107     void
ClearCategories()108     ClearCategories ()
109     {
110         return m_categories_map.Clear();
111     }
112 
113     uint32_t
GetCategoriesCount()114     GetCategoriesCount ()
115     {
116         return m_categories_map.GetCount();
117     }
118 
119     lldb::TypeCategoryImplSP
GetCategoryAtIndex(size_t index)120     GetCategoryAtIndex (size_t index)
121     {
122         return m_categories_map.GetAtIndex(index);
123     }
124 
125     void
LoopThroughCategories(CategoryCallback callback,void * param)126     LoopThroughCategories (CategoryCallback callback, void* param)
127     {
128         m_categories_map.LoopThrough(callback, param);
129     }
130 
131     lldb::TypeCategoryImplSP
132     GetCategory (const char* category_name = NULL,
133                  bool can_create = true)
134     {
135         if (!category_name)
136             return GetCategory(m_default_category_name);
137         return GetCategory(ConstString(category_name));
138     }
139 
140     lldb::TypeCategoryImplSP
141     GetCategory (const ConstString& category_name,
142                  bool can_create = true);
143 
144     lldb::TypeFormatImplSP
145     GetFormatForType (lldb::TypeNameSpecifierImplSP type_sp);
146 
147     lldb::TypeSummaryImplSP
148     GetSummaryForType (lldb::TypeNameSpecifierImplSP type_sp);
149 
150     lldb::TypeFilterImplSP
151     GetFilterForType (lldb::TypeNameSpecifierImplSP type_sp);
152 
153 #ifndef LLDB_DISABLE_PYTHON
154     lldb::ScriptedSyntheticChildrenSP
155     GetSyntheticForType (lldb::TypeNameSpecifierImplSP type_sp);
156 #endif
157 
158 #ifndef LLDB_DISABLE_PYTHON
159     lldb::SyntheticChildrenSP
160     GetSyntheticChildrenForType (lldb::TypeNameSpecifierImplSP type_sp);
161 #endif
162 
163     lldb::TypeValidatorImplSP
164     GetValidatorForType (lldb::TypeNameSpecifierImplSP type_sp);
165 
166     lldb::TypeFormatImplSP
167     GetFormat (ValueObject& valobj,
168                lldb::DynamicValueType use_dynamic);
169 
170     lldb::TypeSummaryImplSP
171     GetSummaryFormat (ValueObject& valobj,
172                       lldb::DynamicValueType use_dynamic);
173 
174 #ifndef LLDB_DISABLE_PYTHON
175     lldb::SyntheticChildrenSP
176     GetSyntheticChildren (ValueObject& valobj,
177                           lldb::DynamicValueType use_dynamic);
178 #endif
179 
180     lldb::TypeValidatorImplSP
181     GetValidator (ValueObject& valobj,
182                   lldb::DynamicValueType use_dynamic);
183 
184     bool
185     AnyMatches (ConstString type_name,
186                 TypeCategoryImpl::FormatCategoryItems items = TypeCategoryImpl::ALL_ITEM_TYPES,
187                 bool only_enabled = true,
188                 const char** matching_category = NULL,
189                 TypeCategoryImpl::FormatCategoryItems* matching_type = NULL)
190     {
191         return m_categories_map.AnyMatches(type_name,
192                                            items,
193                                            only_enabled,
194                                            matching_category,
195                                            matching_type);
196     }
197 
198     static bool
199     GetFormatFromCString (const char *format_cstr,
200                           bool partial_match_ok,
201                           lldb::Format &format);
202 
203     static char
204     GetFormatAsFormatChar (lldb::Format format);
205 
206     static const char *
207     GetFormatAsCString (lldb::Format format);
208 
209     // if the user tries to add formatters for, say, "struct Foo"
210     // those will not match any type because of the way we strip qualifiers from typenames
211     // this method looks for the case where the user is adding a "class","struct","enum" or "union" Foo
212     // and strips the unnecessary qualifier
213     static ConstString
214     GetValidTypeName (const ConstString& type);
215 
216     // when DataExtractor dumps a vectorOfT, it uses a predefined format for each item
217     // this method returns it, or eFormatInvalid if vector_format is not a vectorOf
218     static lldb::Format
219     GetSingleItemFormat (lldb::Format vector_format);
220 
221     // this returns true if the ValueObjectPrinter is *highly encouraged*
222     // to actually represent this ValueObject in one-liner format
223     // If this object has a summary formatter, however, we should not
224     // try and do one-lining, just let the summary do the right thing
225     bool
226     ShouldPrintAsOneLiner (ValueObject& valobj);
227 
228     void
Changed()229     Changed ()
230     {
231         ++m_last_revision;
232         m_format_cache.Clear ();
233     }
234 
235     uint32_t
GetCurrentRevision()236     GetCurrentRevision ()
237     {
238         return m_last_revision;
239     }
240 
~FormatManager()241     ~FormatManager ()
242     {
243     }
244 
245     static FormattersMatchVector
GetPossibleMatches(ValueObject & valobj,lldb::DynamicValueType use_dynamic)246     GetPossibleMatches (ValueObject& valobj,
247                         lldb::DynamicValueType use_dynamic)
248     {
249         FormattersMatchVector matches;
250         GetPossibleMatches (valobj,
251                             valobj.GetClangType(),
252                             lldb_private::eFormatterChoiceCriterionDirectChoice,
253                             use_dynamic,
254                             matches,
255                             false,
256                             false,
257                             false,
258                             true);
259         return matches;
260     }
261 
262 private:
263 
264     static void
265     GetPossibleMatches (ValueObject& valobj,
266                         ClangASTType clang_type,
267                         uint32_t reason,
268                         lldb::DynamicValueType use_dynamic,
269                         FormattersMatchVector& entries,
270                         bool did_strip_ptr,
271                         bool did_strip_ref,
272                         bool did_strip_typedef,
273                         bool root_level = false);
274 
275     FormatCache m_format_cache;
276     NamedSummariesMap m_named_summaries_map;
277     std::atomic<uint32_t> m_last_revision;
278     TypeCategoryMap m_categories_map;
279 
280     ConstString m_default_category_name;
281     ConstString m_system_category_name;
282     ConstString m_gnu_cpp_category_name;
283     ConstString m_libcxx_category_name;
284     ConstString m_objc_category_name;
285     ConstString m_corefoundation_category_name;
286     ConstString m_coregraphics_category_name;
287     ConstString m_coreservices_category_name;
288     ConstString m_vectortypes_category_name;
289     ConstString m_appkit_category_name;
290     ConstString m_coremedia_category_name;
291 
292     HardcodedFormatterFinders<TypeFormatImpl> m_hardcoded_formats;
293     HardcodedFormatterFinders<TypeSummaryImpl> m_hardcoded_summaries;
294     HardcodedFormatterFinders<SyntheticChildren> m_hardcoded_synthetics;
295     HardcodedFormatterFinders<TypeValidatorImpl> m_hardcoded_validators;
296 
297     lldb::TypeFormatImplSP
298     GetHardcodedFormat (ValueObject&,lldb::DynamicValueType);
299 
300     lldb::TypeSummaryImplSP
301     GetHardcodedSummaryFormat (ValueObject&,lldb::DynamicValueType);
302 
303     lldb::SyntheticChildrenSP
304     GetHardcodedSyntheticChildren (ValueObject&,lldb::DynamicValueType);
305 
306     lldb::TypeValidatorImplSP
307     GetHardcodedValidator (ValueObject&,lldb::DynamicValueType);
308 
309     TypeCategoryMap&
GetCategories()310     GetCategories ()
311     {
312         return m_categories_map;
313     }
314 
315     // WARNING: these are temporary functions that setup formatters
316     // while a few of these actually should be globally available and setup by LLDB itself
317     // most would actually belong to the users' lldbinit file or to some other form of configurable
318     // storage
319     void
320     LoadLibStdcppFormatters ();
321 
322     void
323     LoadLibcxxFormatters ();
324 
325     void
326     LoadSystemFormatters ();
327 
328     void
329     LoadObjCFormatters ();
330 
331     void
332     LoadCoreMediaFormatters ();
333 
334     void
335     LoadHardcodedFormatters ();
336 };
337 
338 } // namespace lldb_private
339 
340 #endif	// lldb_FormatManager_h_
341