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 
29 namespace lldb_private {
30 
31 // this file (and its. cpp) contain the low-level implementation of LLDB Data Visualization
32 // class DataVisualization is the high-level front-end of this feature
33 // clients should refer to that class as the entry-point into the data formatters
34 // unless they have a good reason to bypass it and prefer to use this file's objects directly
35 
36 class FormatManager : public IFormatChangeListener
37 {
38     typedef FormatMap<ConstString, TypeSummaryImpl> NamedSummariesMap;
39     typedef TypeCategoryMap::MapType::iterator CategoryMapIterator;
40 public:
41 
42     typedef TypeCategoryMap::CallbackType CategoryCallback;
43 
44     FormatManager ();
45 
46     NamedSummariesMap&
GetNamedSummaryContainer()47     GetNamedSummaryContainer ()
48     {
49         return m_named_summaries_map;
50     }
51 
52     void
53     EnableCategory (const ConstString& category_name,
54                     TypeCategoryMap::Position pos = TypeCategoryMap::Default)
55     {
56         m_categories_map.Enable(category_name,
57                                 pos);
58     }
59 
60     void
DisableCategory(const ConstString & category_name)61     DisableCategory (const ConstString& category_name)
62     {
63         m_categories_map.Disable(category_name);
64     }
65 
66     void
67     EnableCategory (const lldb::TypeCategoryImplSP& category,
68                     TypeCategoryMap::Position pos = TypeCategoryMap::Default)
69     {
70         m_categories_map.Enable(category,
71                                 pos);
72     }
73 
74     void
DisableCategory(const lldb::TypeCategoryImplSP & category)75     DisableCategory (const lldb::TypeCategoryImplSP& category)
76     {
77         m_categories_map.Disable(category);
78     }
79 
80     bool
DeleteCategory(const ConstString & category_name)81     DeleteCategory (const ConstString& category_name)
82     {
83         return m_categories_map.Delete(category_name);
84     }
85 
86     void
ClearCategories()87     ClearCategories ()
88     {
89         return m_categories_map.Clear();
90     }
91 
92     uint32_t
GetCategoriesCount()93     GetCategoriesCount ()
94     {
95         return m_categories_map.GetCount();
96     }
97 
98     lldb::TypeCategoryImplSP
GetCategoryAtIndex(size_t index)99     GetCategoryAtIndex (size_t index)
100     {
101         return m_categories_map.GetAtIndex(index);
102     }
103 
104     void
LoopThroughCategories(CategoryCallback callback,void * param)105     LoopThroughCategories (CategoryCallback callback, void* param)
106     {
107         m_categories_map.LoopThrough(callback, param);
108     }
109 
110     lldb::TypeCategoryImplSP
111     GetCategory (const char* category_name = NULL,
112                  bool can_create = true)
113     {
114         if (!category_name)
115             return GetCategory(m_default_category_name);
116         return GetCategory(ConstString(category_name));
117     }
118 
119     lldb::TypeCategoryImplSP
120     GetCategory (const ConstString& category_name,
121                  bool can_create = true);
122 
123     lldb::TypeFormatImplSP
124     GetFormatForType (lldb::TypeNameSpecifierImplSP type_sp);
125 
126     lldb::TypeSummaryImplSP
127     GetSummaryForType (lldb::TypeNameSpecifierImplSP type_sp);
128 
129     lldb::TypeFilterImplSP
130     GetFilterForType (lldb::TypeNameSpecifierImplSP type_sp);
131 
132 #ifndef LLDB_DISABLE_PYTHON
133     lldb::ScriptedSyntheticChildrenSP
134     GetSyntheticForType (lldb::TypeNameSpecifierImplSP type_sp);
135 #endif
136 
137 #ifndef LLDB_DISABLE_PYTHON
138     lldb::SyntheticChildrenSP
139     GetSyntheticChildrenForType (lldb::TypeNameSpecifierImplSP type_sp);
140 #endif
141 
142     lldb::TypeFormatImplSP
143     GetFormat (ValueObject& valobj,
144                lldb::DynamicValueType use_dynamic);
145 
146     lldb::TypeSummaryImplSP
147     GetSummaryFormat (ValueObject& valobj,
148                       lldb::DynamicValueType use_dynamic);
149 
150 #ifndef LLDB_DISABLE_PYTHON
151     lldb::SyntheticChildrenSP
152     GetSyntheticChildren (ValueObject& valobj,
153                           lldb::DynamicValueType use_dynamic);
154 #endif
155 
156     bool
157     AnyMatches (ConstString type_name,
158                 TypeCategoryImpl::FormatCategoryItems items = TypeCategoryImpl::ALL_ITEM_TYPES,
159                 bool only_enabled = true,
160                 const char** matching_category = NULL,
161                 TypeCategoryImpl::FormatCategoryItems* matching_type = NULL)
162     {
163         return m_categories_map.AnyMatches(type_name,
164                                            items,
165                                            only_enabled,
166                                            matching_category,
167                                            matching_type);
168     }
169 
170     static bool
171     GetFormatFromCString (const char *format_cstr,
172                           bool partial_match_ok,
173                           lldb::Format &format);
174 
175     static char
176     GetFormatAsFormatChar (lldb::Format format);
177 
178     static const char *
179     GetFormatAsCString (lldb::Format format);
180 
181     // if the user tries to add formatters for, say, "struct Foo"
182     // those will not match any type because of the way we strip qualifiers from typenames
183     // this method looks for the case where the user is adding a "class","struct","enum" or "union" Foo
184     // and strips the unnecessary qualifier
185     static ConstString
186     GetValidTypeName (const ConstString& type);
187 
188     // when DataExtractor dumps a vectorOfT, it uses a predefined format for each item
189     // this method returns it, or eFormatInvalid if vector_format is not a vectorOf
190     static lldb::Format
191     GetSingleItemFormat (lldb::Format vector_format);
192 
193     // this returns true if the ValueObjectPrinter is *highly encouraged*
194     // to actually represent this ValueObject in one-liner format
195     // If this object has a summary formatter, however, we should not
196     // try and do one-lining, just let the summary do the right thing
197     bool
198     ShouldPrintAsOneLiner (ValueObject& valobj);
199 
200     void
Changed()201     Changed ()
202     {
203         ++m_last_revision;
204         m_format_cache.Clear ();
205     }
206 
207     uint32_t
GetCurrentRevision()208     GetCurrentRevision ()
209     {
210         return m_last_revision;
211     }
212 
~FormatManager()213     ~FormatManager ()
214     {
215     }
216 
217     static FormattersMatchVector
GetPossibleMatches(ValueObject & valobj,lldb::DynamicValueType use_dynamic)218     GetPossibleMatches (ValueObject& valobj,
219                         lldb::DynamicValueType use_dynamic)
220     {
221         FormattersMatchVector matches;
222         GetPossibleMatches (valobj,
223                             valobj.GetClangType(),
224                             lldb_private::eFormatterChoiceCriterionDirectChoice,
225                             use_dynamic,
226                             matches,
227                             false,
228                             false,
229                             false,
230                             true);
231         return matches;
232     }
233 
234 private:
235 
236     static void
237     GetPossibleMatches (ValueObject& valobj,
238                         ClangASTType clang_type,
239                         uint32_t reason,
240                         lldb::DynamicValueType use_dynamic,
241                         FormattersMatchVector& entries,
242                         bool did_strip_ptr,
243                         bool did_strip_ref,
244                         bool did_strip_typedef,
245                         bool root_level = false);
246 
247     FormatCache m_format_cache;
248     NamedSummariesMap m_named_summaries_map;
249     std::atomic<uint32_t> m_last_revision;
250     TypeCategoryMap m_categories_map;
251 
252     ConstString m_default_category_name;
253     ConstString m_system_category_name;
254     ConstString m_gnu_cpp_category_name;
255     ConstString m_libcxx_category_name;
256     ConstString m_objc_category_name;
257     ConstString m_corefoundation_category_name;
258     ConstString m_coregraphics_category_name;
259     ConstString m_coreservices_category_name;
260     ConstString m_vectortypes_category_name;
261     ConstString m_appkit_category_name;
262 
263     TypeCategoryMap&
GetCategories()264     GetCategories ()
265     {
266         return m_categories_map;
267     }
268 
269     // WARNING: these are temporary functions that setup formatters
270     // while a few of these actually should be globally available and setup by LLDB itself
271     // most would actually belong to the users' lldbinit file or to some other form of configurable
272     // storage
273     void
274     LoadLibStdcppFormatters ();
275 
276     void
277     LoadLibcxxFormatters ();
278 
279     void
280     LoadSystemFormatters ();
281 
282     void
283     LoadObjCFormatters ();
284 };
285 
286 } // namespace lldb_private
287 
288 #endif	// lldb_FormatManager_h_
289