]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - include/lldb/DataFormatters/FormatManager.h
Vendor import of lldb trunk r338150:
[FreeBSD/FreeBSD.git] / include / lldb / DataFormatters / FormatManager.h
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 #include <atomic>
16 #include <initializer_list>
17 #include <map>
18 #include <mutex>
19 #include <vector>
20
21 // Other libraries and framework includes
22 // Project includes
23 #include "lldb/lldb-enumerations.h"
24 #include "lldb/lldb-public.h"
25
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"
32
33 namespace lldb_private {
34
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
40
41 class FormatManager : public IFormatChangeListener {
42   typedef FormatMap<ConstString, TypeSummaryImpl> NamedSummariesMap;
43   typedef TypeCategoryMap::MapType::iterator CategoryMapIterator;
44
45 public:
46   typedef std::map<lldb::LanguageType, LanguageCategory::UniquePointer>
47       LanguageCategories;
48
49   FormatManager();
50
51   ~FormatManager() override = default;
52
53   NamedSummariesMap &GetNamedSummaryContainer() {
54     return m_named_summaries_map;
55   }
56
57   void
58   EnableCategory(const ConstString &category_name,
59                  TypeCategoryMap::Position pos = TypeCategoryMap::Default) {
60     EnableCategory(category_name, pos,
61                    std::initializer_list<lldb::LanguageType>());
62   }
63
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);
68   }
69
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);
78     }
79   }
80
81   void DisableCategory(const ConstString &category_name) {
82     m_categories_map.Disable(category_name);
83   }
84
85   void
86   EnableCategory(const lldb::TypeCategoryImplSP &category,
87                  TypeCategoryMap::Position pos = TypeCategoryMap::Default) {
88     m_categories_map.Enable(category, pos);
89   }
90
91   void DisableCategory(const lldb::TypeCategoryImplSP &category) {
92     m_categories_map.Disable(category);
93   }
94
95   void EnableAllCategories();
96
97   void DisableAllCategories();
98
99   bool DeleteCategory(const ConstString &category_name) {
100     return m_categories_map.Delete(category_name);
101   }
102
103   void ClearCategories() { return m_categories_map.Clear(); }
104
105   uint32_t GetCategoriesCount() { return m_categories_map.GetCount(); }
106
107   lldb::TypeCategoryImplSP GetCategoryAtIndex(size_t index) {
108     return m_categories_map.GetAtIndex(index);
109   }
110
111   void ForEachCategory(TypeCategoryMap::ForEachCallback callback);
112
113   lldb::TypeCategoryImplSP GetCategory(const char *category_name = nullptr,
114                                        bool can_create = true) {
115     if (!category_name)
116       return GetCategory(m_default_category_name);
117     return GetCategory(ConstString(category_name));
118   }
119
120   lldb::TypeCategoryImplSP 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::TypeValidatorImplSP
143   GetValidatorForType(lldb::TypeNameSpecifierImplSP type_sp);
144
145   lldb::TypeFormatImplSP GetFormat(ValueObject &valobj,
146                                    lldb::DynamicValueType use_dynamic);
147
148   lldb::TypeSummaryImplSP GetSummaryFormat(ValueObject &valobj,
149                                            lldb::DynamicValueType use_dynamic);
150
151 #ifndef LLDB_DISABLE_PYTHON
152   lldb::SyntheticChildrenSP
153   GetSyntheticChildren(ValueObject &valobj, lldb::DynamicValueType use_dynamic);
154 #endif
155
156   lldb::TypeValidatorImplSP GetValidator(ValueObject &valobj,
157                                          lldb::DynamicValueType use_dynamic);
158
159   bool
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);
167   }
168
169   static bool GetFormatFromCString(const char *format_cstr,
170                                    bool partial_match_ok, lldb::Format &format);
171
172   static char GetFormatAsFormatChar(lldb::Format format);
173
174   static const char *GetFormatAsCString(lldb::Format format);
175
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
180   // qualifier
181   static ConstString GetValidTypeName(const ConstString &type);
182
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
185   // vectorOf
186   static lldb::Format GetSingleItemFormat(lldb::Format vector_format);
187
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);
193
194   void Changed() override;
195
196   uint32_t GetCurrentRevision() override { return m_last_revision; }
197
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);
204     return matches;
205   }
206
207   static ConstString GetTypeForCache(ValueObject &, lldb::DynamicValueType);
208
209   LanguageCategory *GetCategoryForLanguage(lldb::LanguageType lang_type);
210
211   static std::vector<lldb::LanguageType>
212   GetCandidateLanguages(lldb::LanguageType lang_type);
213
214 private:
215   static std::vector<lldb::LanguageType>
216   GetCandidateLanguages(ValueObject &valobj);
217
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);
225
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;
232
233   ConstString m_default_category_name;
234   ConstString m_system_category_name;
235   ConstString m_vectortypes_category_name;
236
237   lldb::TypeFormatImplSP GetHardcodedFormat(FormattersMatchData &);
238
239   lldb::TypeSummaryImplSP GetHardcodedSummaryFormat(FormattersMatchData &);
240
241   lldb::SyntheticChildrenSP
242   GetHardcodedSyntheticChildren(FormattersMatchData &);
243
244   lldb::TypeValidatorImplSP GetHardcodedValidator(FormattersMatchData &);
245
246   TypeCategoryMap &GetCategories() { return m_categories_map; }
247
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();
255
256   void LoadVectorFormatters();
257
258   friend class FormattersMatchData;
259 };
260
261 } // namespace lldb_private
262
263 #endif // lldb_FormatManager_h_