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_DATAFORMATTERS_FORMATMANAGER_H
10 #define LLDB_DATAFORMATTERS_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  }
57 
58  void EnableCategory(ConstString category_name,
60  TypeCategoryMap::ValueSP category_sp;
61  if (m_categories_map.Get(category_name, category_sp) && category_sp) {
62  m_categories_map.Enable(category_sp, pos);
63  category_sp->AddLanguage(lang);
64  }
65  }
66 
67  void DisableCategory(ConstString category_name) {
68  m_categories_map.Disable(category_name);
69  }
70 
71  void
72  EnableCategory(const lldb::TypeCategoryImplSP &category,
74  m_categories_map.Enable(category, pos);
75  }
76 
77  void DisableCategory(const lldb::TypeCategoryImplSP &category) {
78  m_categories_map.Disable(category);
79  }
80 
81  void EnableAllCategories();
82 
83  void DisableAllCategories();
84 
85  bool DeleteCategory(ConstString category_name) {
86  return m_categories_map.Delete(category_name);
87  }
88 
89  void ClearCategories() { return m_categories_map.Clear(); }
90 
92 
93  lldb::TypeCategoryImplSP GetCategoryAtIndex(size_t index) {
94  return m_categories_map.GetAtIndex(index);
95  }
96 
98 
99  lldb::TypeCategoryImplSP GetCategory(const char *category_name = nullptr,
100  bool can_create = true) {
101  if (!category_name)
103  return GetCategory(ConstString(category_name));
104  }
105 
106  lldb::TypeCategoryImplSP GetCategory(ConstString category_name,
107  bool can_create = true);
108 
109  lldb::TypeFormatImplSP
110  GetFormatForType(lldb::TypeNameSpecifierImplSP type_sp);
111 
112  lldb::TypeSummaryImplSP
113  GetSummaryForType(lldb::TypeNameSpecifierImplSP type_sp);
114 
115  lldb::TypeFilterImplSP
116  GetFilterForType(lldb::TypeNameSpecifierImplSP type_sp);
117 
118  lldb::ScriptedSyntheticChildrenSP
119  GetSyntheticForType(lldb::TypeNameSpecifierImplSP type_sp);
120 
121  lldb::TypeFormatImplSP GetFormat(ValueObject &valobj,
122  lldb::DynamicValueType use_dynamic);
123 
124  lldb::TypeSummaryImplSP GetSummaryFormat(ValueObject &valobj,
125  lldb::DynamicValueType use_dynamic);
126 
127  lldb::SyntheticChildrenSP
129 
130  bool
134  bool only_enabled = true, const char **matching_category = nullptr,
135  TypeCategoryImpl::FormatCategoryItems *matching_type = nullptr) {
136  return m_categories_map.AnyMatches(type_name, items, only_enabled,
137  matching_category, matching_type);
138  }
139 
140  static bool GetFormatFromCString(const char *format_cstr,
141  bool partial_match_ok, lldb::Format &format);
142 
143  static char GetFormatAsFormatChar(lldb::Format format);
144 
145  static const char *GetFormatAsCString(lldb::Format format);
146 
147  // if the user tries to add formatters for, say, "struct Foo" those will not
148  // match any type because of the way we strip qualifiers from typenames this
149  // method looks for the case where the user is adding a
150  // "class","struct","enum" or "union" Foo and strips the unnecessary
151  // qualifier
153 
154  // when DataExtractor dumps a vectorOfT, it uses a predefined format for each
155  // item this method returns it, or eFormatInvalid if vector_format is not a
156  // vectorOf
157  static lldb::Format GetSingleItemFormat(lldb::Format vector_format);
158 
159  // this returns true if the ValueObjectPrinter is *highly encouraged* to
160  // actually represent this ValueObject in one-liner format If this object has
161  // a summary formatter, however, we should not try and do one-lining, just
162  // let the summary do the right thing
163  bool ShouldPrintAsOneLiner(ValueObject &valobj);
164 
165  void Changed() override;
166 
168 
169  static FormattersMatchVector
171  FormattersMatchVector matches;
172  GetPossibleMatches(valobj, valobj.GetCompilerType(),
173  use_dynamic, matches, false, false, false, true);
174  return matches;
175  }
176 
178 
180 
181  static std::vector<lldb::LanguageType>
183 
184 private:
185  static void GetPossibleMatches(ValueObject &valobj,
186  CompilerType compiler_type,
187  lldb::DynamicValueType use_dynamic,
188  FormattersMatchVector &entries,
189  bool did_strip_ptr, bool did_strip_ref,
190  bool did_strip_typedef,
191  bool root_level = false);
192 
193  std::atomic<uint32_t> m_last_revision;
195  std::recursive_mutex m_language_categories_mutex;
197  NamedSummariesMap m_named_summaries_map;
199 
203 
204  template <typename ImplSP>
205  ImplSP Get(ValueObject &valobj, lldb::DynamicValueType use_dynamic);
206  template <typename ImplSP> ImplSP GetCached(FormattersMatchData &match_data);
207  template <typename ImplSP> ImplSP GetHardcoded(FormattersMatchData &);
208 
210 
211  // These functions are meant to initialize formatters that are very low-
212  // level/global in nature and do not naturally belong in any language. The
213  // intent is that most formatters go in language-specific categories.
214  // Eventually, the runtimes should also be allowed to vend their own
215  // formatters, and then one could put formatters that depend on specific
216  // library load events in the language runtimes, on an as-needed basis
217  void LoadSystemFormatters();
218 
219  void LoadVectorFormatters();
220 
221  friend class FormattersMatchData;
222 };
223 
224 } // namespace lldb_private
225 
226 #endif // LLDB_DATAFORMATTERS_FORMATMANAGER_H
lldb::TypeSummaryImplSP GetSummaryFormat(ValueObject &valobj, lldb::DynamicValueType use_dynamic)
ValueType::SharedPointer ValueSP
lldb::ScriptedSyntheticChildrenSP GetSyntheticForType(lldb::TypeNameSpecifierImplSP type_sp)
lldb::TypeSummaryImplSP GetSummaryForType(lldb::TypeNameSpecifierImplSP type_sp)
CompilerType GetCompilerType()
A class that represents a running process on the host machine.
lldb::TypeFormatImplSP GetFormat(ValueObject &valobj, lldb::DynamicValueType use_dynamic)
static const uint16_t ALL_ITEM_TYPES
Definition: TypeCategory.h:74
~FormatManager() override=default
bool DeleteCategory(ConstString category_name)
Definition: FormatManager.h:85
bool Disable(KeyType category_name)
TypeCategoryMap m_categories_map
void ForEachCategory(TypeCategoryMap::ForEachCallback callback)
void DisableCategory(ConstString category_name)
Definition: FormatManager.h:67
ImplSP GetHardcoded(FormattersMatchData &)
std::vector< FormattersMatchCandidate > FormattersMatchVector
Definition: FormatClasses.h:81
TypeCategoryMap::MapType::iterator CategoryMapIterator
Definition: FormatManager.h:38
NamedSummariesMap m_named_summaries_map
void DisableCategory(const lldb::TypeCategoryImplSP &category)
Definition: FormatManager.h:77
Format
Display format definitions.
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)
std::recursive_mutex m_language_categories_mutex
LanguageType
Programming language type.
LanguageCategories m_language_categories_map
lldb::TypeCategoryImplSP GetCategoryAtIndex(size_t index)
Definition: FormatManager.h:93
static const char * GetFormatAsCString(lldb::Format format)
uint32_t GetCurrentRevision() override
ImplSP Get(ValueObject &valobj, lldb::DynamicValueType use_dynamic)
LanguageCategory * GetCategoryForLanguage(lldb::LanguageType lang_type)
std::function< bool(const ValueSP &)> ForEachCallback
ImplSP GetCached(FormattersMatchData &match_data)
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:72
static std::vector< lldb::LanguageType > GetCandidateLanguages(lldb::LanguageType lang_type)
ConstString m_vectortypes_category_name
std::atomic< uint32_t > m_last_revision
FormatMap< ConstString, TypeSummaryImpl > NamedSummariesMap
Definition: FormatManager.h:37
std::map< lldb::LanguageType, LanguageCategory::UniquePointer > LanguageCategories
Definition: FormatManager.h:42
TypeCategoryMap & GetCategories()
static ConstString GetTypeForCache(ValueObject &, lldb::DynamicValueType)
A uniqued constant string class.
Definition: ConstString.h:40
void EnableCategory(ConstString category_name, TypeCategoryMap::Position pos, lldb::LanguageType lang)
Definition: FormatManager.h:58
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)
Represents a generic type in a programming language.
Definition: CompilerType.h:33
lldb::TypeCategoryImplSP GetCategory(const char *category_name=nullptr, bool can_create=true)
Definition: FormatManager.h:99
lldb::SyntheticChildrenSP GetSyntheticChildren(ValueObject &valobj, lldb::DynamicValueType use_dynamic)
static lldb::Format GetSingleItemFormat(lldb::Format vector_format)
NamedSummariesMap & GetNamedSummaryContainer()
Definition: FormatManager.h:48
bool Get(KeyType name, ValueSP &entry)
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)