LLDB  mainline
FormatManager.h
Go to the documentation of this file.
1 //===-- FormatManager.h -----------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef lldb_FormatManager_h_
10 #define lldb_FormatManager_h_
11 
12 #include <atomic>
13 #include <initializer_list>
14 #include <map>
15 #include <mutex>
16 #include <vector>
17 
18 #include "lldb/lldb-enumerations.h"
19 #include "lldb/lldb-public.h"
20 
27 
28 namespace lldb_private {
29 
30 // this file (and its. cpp) contain the low-level implementation of LLDB Data
31 // Visualization class DataVisualization is the high-level front-end of this
32 // feature clients should refer to that class as the entry-point into the data
33 // formatters unless they have a good reason to bypass it and prefer to use
34 // this file's objects directly
35 
38  typedef TypeCategoryMap::MapType::iterator CategoryMapIterator;
39 
40 public:
41  typedef std::map<lldb::LanguageType, LanguageCategory::UniquePointer>
43 
44  FormatManager();
45 
46  ~FormatManager() override = default;
47 
48  NamedSummariesMap &GetNamedSummaryContainer() {
49  return m_named_summaries_map;
50  }
51 
52  void
53  EnableCategory(ConstString category_name,
55  EnableCategory(category_name, pos,
56  std::initializer_list<lldb::LanguageType>());
57  }
58 
59  void EnableCategory(ConstString category_name,
61  std::initializer_list<lldb::LanguageType> langs = {lang};
62  EnableCategory(category_name, pos, langs);
63  }
64 
65  void EnableCategory(ConstString category_name,
67  std::initializer_list<lldb::LanguageType> langs = {}) {
68  TypeCategoryMap::ValueSP category_sp;
69  if (m_categories_map.Get(category_name, category_sp) && category_sp) {
70  m_categories_map.Enable(category_sp, pos);
71  for (const lldb::LanguageType lang : langs)
72  category_sp->AddLanguage(lang);
73  }
74  }
75 
76  void DisableCategory(ConstString category_name) {
77  m_categories_map.Disable(category_name);
78  }
79 
80  void
81  EnableCategory(const lldb::TypeCategoryImplSP &category,
83  m_categories_map.Enable(category, pos);
84  }
85 
86  void DisableCategory(const lldb::TypeCategoryImplSP &category) {
87  m_categories_map.Disable(category);
88  }
89 
90  void EnableAllCategories();
91 
92  void DisableAllCategories();
93 
94  bool DeleteCategory(ConstString category_name) {
95  return m_categories_map.Delete(category_name);
96  }
97 
98  void ClearCategories() { return m_categories_map.Clear(); }
99 
100  uint32_t GetCategoriesCount() { return m_categories_map.GetCount(); }
101 
102  lldb::TypeCategoryImplSP GetCategoryAtIndex(size_t index) {
103  return m_categories_map.GetAtIndex(index);
104  }
105 
107 
108  lldb::TypeCategoryImplSP GetCategory(const char *category_name = nullptr,
109  bool can_create = true) {
110  if (!category_name)
111  return GetCategory(m_default_category_name);
112  return GetCategory(ConstString(category_name));
113  }
114 
115  lldb::TypeCategoryImplSP GetCategory(ConstString category_name,
116  bool can_create = true);
117 
118  lldb::TypeFormatImplSP
119  GetFormatForType(lldb::TypeNameSpecifierImplSP type_sp);
120 
121  lldb::TypeSummaryImplSP
122  GetSummaryForType(lldb::TypeNameSpecifierImplSP type_sp);
123 
124  lldb::TypeFilterImplSP
125  GetFilterForType(lldb::TypeNameSpecifierImplSP type_sp);
126 
127  lldb::ScriptedSyntheticChildrenSP
128  GetSyntheticForType(lldb::TypeNameSpecifierImplSP type_sp);
129 
130  lldb::TypeValidatorImplSP
131  GetValidatorForType(lldb::TypeNameSpecifierImplSP type_sp);
132 
133  lldb::TypeFormatImplSP GetFormat(ValueObject &valobj,
134  lldb::DynamicValueType use_dynamic);
135 
136  lldb::TypeSummaryImplSP GetSummaryFormat(ValueObject &valobj,
137  lldb::DynamicValueType use_dynamic);
138 
139  lldb::SyntheticChildrenSP
141 
142  lldb::TypeValidatorImplSP GetValidator(ValueObject &valobj,
143  lldb::DynamicValueType use_dynamic);
144 
145  bool
149  bool only_enabled = true, const char **matching_category = nullptr,
150  TypeCategoryImpl::FormatCategoryItems *matching_type = nullptr) {
151  return m_categories_map.AnyMatches(type_name, items, only_enabled,
152  matching_category, matching_type);
153  }
154 
155  static bool GetFormatFromCString(const char *format_cstr,
156  bool partial_match_ok, lldb::Format &format);
157 
158  static char GetFormatAsFormatChar(lldb::Format format);
159 
160  static const char *GetFormatAsCString(lldb::Format format);
161 
162  // if the user tries to add formatters for, say, "struct Foo" those will not
163  // match any type because of the way we strip qualifiers from typenames this
164  // method looks for the case where the user is adding a
165  // "class","struct","enum" or "union" Foo and strips the unnecessary
166  // qualifier
168 
169  // when DataExtractor dumps a vectorOfT, it uses a predefined format for each
170  // item this method returns it, or eFormatInvalid if vector_format is not a
171  // vectorOf
172  static lldb::Format GetSingleItemFormat(lldb::Format vector_format);
173 
174  // this returns true if the ValueObjectPrinter is *highly encouraged* to
175  // actually represent this ValueObject in one-liner format If this object has
176  // a summary formatter, however, we should not try and do one-lining, just
177  // let the summary do the right thing
178  bool ShouldPrintAsOneLiner(ValueObject &valobj);
179 
180  void Changed() override;
181 
182  uint32_t GetCurrentRevision() override { return m_last_revision; }
183 
184  static FormattersMatchVector
186  FormattersMatchVector matches;
187  GetPossibleMatches(valobj, valobj.GetCompilerType(),
189  use_dynamic, matches, false, false, false, true);
190  return matches;
191  }
192 
194 
196 
197  static std::vector<lldb::LanguageType>
199 
200 private:
201  static std::vector<lldb::LanguageType>
203 
204  static void GetPossibleMatches(ValueObject &valobj,
205  CompilerType compiler_type, uint32_t reason,
206  lldb::DynamicValueType use_dynamic,
207  FormattersMatchVector &entries,
208  bool did_strip_ptr, bool did_strip_ref,
209  bool did_strip_typedef,
210  bool root_level = false);
211 
212  std::atomic<uint32_t> m_last_revision;
213  FormatCache m_format_cache;
214  std::recursive_mutex m_language_categories_mutex;
215  LanguageCategories m_language_categories_map;
216  NamedSummariesMap m_named_summaries_map;
217  TypeCategoryMap m_categories_map;
218 
219  ConstString m_default_category_name;
220  ConstString m_system_category_name;
221  ConstString m_vectortypes_category_name;
222 
223  lldb::TypeFormatImplSP GetHardcodedFormat(FormattersMatchData &);
224 
225  lldb::TypeSummaryImplSP GetHardcodedSummaryFormat(FormattersMatchData &);
226 
227  lldb::SyntheticChildrenSP
228  GetHardcodedSyntheticChildren(FormattersMatchData &);
229 
230  lldb::TypeValidatorImplSP GetHardcodedValidator(FormattersMatchData &);
231 
232  TypeCategoryMap &GetCategories() { return m_categories_map; }
233 
234  // These functions are meant to initialize formatters that are very low-
235  // level/global in nature and do not naturally belong in any language. The
236  // intent is that most formatters go in language-specific categories.
237  // Eventually, the runtimes should also be allowed to vend their own
238  // formatters, and then one could put formatters that depend on specific
239  // library load events in the language runtimes, on an as-needed basis
240  void LoadSystemFormatters();
241 
242  void LoadVectorFormatters();
243 
244  friend class FormattersMatchData;
245 };
246 
247 } // namespace lldb_private
248 
249 #endif // lldb_FormatManager_h_
lldb::TypeSummaryImplSP GetSummaryFormat(ValueObject &valobj, lldb::DynamicValueType use_dynamic)
lldb::ScriptedSyntheticChildrenSP GetSyntheticForType(lldb::TypeNameSpecifierImplSP type_sp)
lldb::TypeSummaryImplSP GetSummaryForType(lldb::TypeNameSpecifierImplSP type_sp)
CompilerType GetCompilerType()
Enumerations for broadcasting.
Definition: SBLaunchInfo.h:14
lldb::TypeFormatImplSP GetFormat(ValueObject &valobj, lldb::DynamicValueType use_dynamic)
static const uint16_t ALL_ITEM_TYPES
Definition: TypeCategory.h:75
~FormatManager() override=default
bool DeleteCategory(ConstString category_name)
Definition: FormatManager.h:94
bool Disable(KeyType category_name)
lldb::TypeValidatorImplSP GetValidatorForType(lldb::TypeNameSpecifierImplSP type_sp)
void ForEachCategory(TypeCategoryMap::ForEachCallback callback)
void DisableCategory(ConstString category_name)
Definition: FormatManager.h:76
std::vector< FormattersMatchCandidate > FormattersMatchVector
Definition: FormatClasses.h:86
void DisableCategory(const lldb::TypeCategoryImplSP &category)
Definition: FormatManager.h:86
lldb::TypeFilterImplSP GetFilterForType(lldb::TypeNameSpecifierImplSP type_sp)
static const Position Default
bool AnyMatches(ConstString type_name, TypeCategoryImpl::FormatCategoryItems items=TypeCategoryImpl::ALL_ITEM_TYPES, bool only_enabled=true, const char **matching_category=nullptr, TypeCategoryImpl::FormatCategoryItems *matching_type=nullptr)
static bool GetFormatFromCString(const char *format_cstr, bool partial_match_ok, lldb::Format &format)
LanguageType
Programming language type.
lldb::TypeCategoryImplSP GetCategoryAtIndex(size_t index)
static const char * GetFormatAsCString(lldb::Format format)
uint32_t GetCurrentRevision() override
LanguageCategory * GetCategoryForLanguage(lldb::LanguageType lang_type)
std::function< bool(const ValueSP &)> ForEachCallback
lldb::TypeFormatImplSP GetFormatForType(lldb::TypeNameSpecifierImplSP type_sp)
static char GetFormatAsFormatChar(lldb::Format format)
lldb::TypeCategoryImplSP GetAtIndex(uint32_t)
bool ShouldPrintAsOneLiner(ValueObject &valobj)
void EnableCategory(const lldb::TypeCategoryImplSP &category, TypeCategoryMap::Position pos=TypeCategoryMap::Default)
Definition: FormatManager.h:81
static std::vector< lldb::LanguageType > GetCandidateLanguages(lldb::LanguageType lang_type)
std::map< lldb::LanguageType, LanguageCategory::UniquePointer > LanguageCategories
Definition: FormatManager.h:42
static ConstString GetTypeForCache(ValueObject &, lldb::DynamicValueType)
A uniqued constant string class.
Definition: ConstString.h:38
void EnableCategory(ConstString category_name, TypeCategoryMap::Position pos, lldb::LanguageType lang)
Definition: FormatManager.h:59
bool AnyMatches(ConstString type_name, TypeCategoryImpl::FormatCategoryItems items=TypeCategoryImpl::ALL_ITEM_TYPES, bool only_enabled=true, const char **matching_category=nullptr, TypeCategoryImpl::FormatCategoryItems *matching_type=nullptr)
lldb::TypeCategoryImplSP GetCategory(const char *category_name=nullptr, bool can_create=true)
lldb::SyntheticChildrenSP GetSyntheticChildren(ValueObject &valobj, lldb::DynamicValueType use_dynamic)
void EnableCategory(ConstString category_name, TypeCategoryMap::Position pos=TypeCategoryMap::Default, std::initializer_list< lldb::LanguageType > langs={})
Definition: FormatManager.h:65
static lldb::Format GetSingleItemFormat(lldb::Format vector_format)
NamedSummariesMap & GetNamedSummaryContainer()
Definition: FormatManager.h:48
bool Get(KeyType name, ValueSP &entry)
lldb::TypeValidatorImplSP GetValidator(ValueObject &valobj, lldb::DynamicValueType use_dynamic)
void EnableCategory(ConstString category_name, TypeCategoryMap::Position pos=TypeCategoryMap::Default)
Definition: FormatManager.h:53
static ConstString GetValidTypeName(ConstString type)
bool Enable(KeyType category_name, Position pos=Default)
static FormattersMatchVector GetPossibleMatches(ValueObject &valobj, lldb::DynamicValueType use_dynamic)