xref: /freebsd/contrib/llvm-project/lldb/include/lldb/DataFormatters/FormatManager.h (revision 7ef62cebc2f965b0f640263e179276928885e33d)
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 
21 #include "lldb/DataFormatters/FormatCache.h"
22 #include "lldb/DataFormatters/FormatClasses.h"
23 #include "lldb/DataFormatters/FormattersContainer.h"
24 #include "lldb/DataFormatters/LanguageCategory.h"
25 #include "lldb/DataFormatters/TypeCategory.h"
26 #include "lldb/DataFormatters/TypeCategoryMap.h"
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 
36 class FormatManager : public IFormatChangeListener {
37   typedef FormattersContainer<TypeSummaryImpl> NamedSummariesMap;
38   typedef TypeCategoryMap::MapType::iterator CategoryMapIterator;
39 
40 public:
41   typedef std::map<lldb::LanguageType, LanguageCategory::UniquePointer>
42       LanguageCategories;
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,
54                  TypeCategoryMap::Position pos = TypeCategoryMap::Default) {
55     EnableCategory(category_name, pos, {});
56   }
57 
58   void EnableCategory(ConstString category_name,
59                       TypeCategoryMap::Position pos, lldb::LanguageType lang) {
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,
73                  TypeCategoryMap::Position pos = TypeCategoryMap::Default) {
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 
91   uint32_t GetCategoriesCount() { return m_categories_map.GetCount(); }
92 
93   lldb::TypeCategoryImplSP GetCategoryAtIndex(size_t index) {
94     return m_categories_map.GetAtIndex(index);
95   }
96 
97   void ForEachCategory(TypeCategoryMap::ForEachCallback callback);
98 
99   lldb::TypeCategoryImplSP GetCategory(const char *category_name = nullptr,
100                                        bool can_create = true) {
101     if (!category_name)
102       return GetCategory(m_default_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
128   GetSyntheticChildren(ValueObject &valobj, lldb::DynamicValueType use_dynamic);
129 
130   bool
131   AnyMatches(const FormattersMatchCandidate &candidate_type,
132              TypeCategoryImpl::FormatCategoryItems items =
133                  TypeCategoryImpl::ALL_ITEM_TYPES,
134              bool only_enabled = true, const char **matching_category = nullptr,
135              TypeCategoryImpl::FormatCategoryItems *matching_type = nullptr) {
136     return m_categories_map.AnyMatches(candidate_type, 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   // when DataExtractor dumps a vectorOfT, it uses a predefined format for each
148   // item this method returns it, or eFormatInvalid if vector_format is not a
149   // vectorOf
150   static lldb::Format GetSingleItemFormat(lldb::Format vector_format);
151 
152   // this returns true if the ValueObjectPrinter is *highly encouraged* to
153   // actually represent this ValueObject in one-liner format If this object has
154   // a summary formatter, however, we should not try and do one-lining, just
155   // let the summary do the right thing
156   bool ShouldPrintAsOneLiner(ValueObject &valobj);
157 
158   void Changed() override;
159 
160   uint32_t GetCurrentRevision() override { return m_last_revision; }
161 
162   static FormattersMatchVector
163   GetPossibleMatches(ValueObject &valobj, lldb::DynamicValueType use_dynamic) {
164     FormattersMatchVector matches;
165     GetPossibleMatches(valobj, valobj.GetCompilerType(), use_dynamic, matches,
166                        FormattersMatchCandidate::Flags(), true);
167     return matches;
168   }
169 
170   static ConstString GetTypeForCache(ValueObject &, lldb::DynamicValueType);
171 
172   LanguageCategory *GetCategoryForLanguage(lldb::LanguageType lang_type);
173 
174   static std::vector<lldb::LanguageType>
175   GetCandidateLanguages(lldb::LanguageType lang_type);
176 
177 private:
178   static void GetPossibleMatches(ValueObject &valobj,
179                                  CompilerType compiler_type,
180                                  lldb::DynamicValueType use_dynamic,
181                                  FormattersMatchVector &entries,
182                                  FormattersMatchCandidate::Flags current_flags,
183                                  bool root_level = false);
184 
185   std::atomic<uint32_t> m_last_revision;
186   FormatCache m_format_cache;
187   std::recursive_mutex m_language_categories_mutex;
188   LanguageCategories m_language_categories_map;
189   NamedSummariesMap m_named_summaries_map;
190   TypeCategoryMap m_categories_map;
191 
192   ConstString m_default_category_name;
193   ConstString m_system_category_name;
194   ConstString m_vectortypes_category_name;
195 
196   template <typename ImplSP>
197   ImplSP Get(ValueObject &valobj, lldb::DynamicValueType use_dynamic);
198   template <typename ImplSP> ImplSP GetCached(FormattersMatchData &match_data);
199   template <typename ImplSP> ImplSP GetHardcoded(FormattersMatchData &);
200 
201   TypeCategoryMap &GetCategories() { return m_categories_map; }
202 
203   // These functions are meant to initialize formatters that are very low-
204   // level/global in nature and do not naturally belong in any language. The
205   // intent is that most formatters go in language-specific categories.
206   // Eventually, the runtimes should also be allowed to vend their own
207   // formatters, and then one could put formatters that depend on specific
208   // library load events in the language runtimes, on an as-needed basis
209   void LoadSystemFormatters();
210 
211   void LoadVectorFormatters();
212 
213   friend class FormattersMatchData;
214 };
215 
216 } // namespace lldb_private
217 
218 #endif // LLDB_DATAFORMATTERS_FORMATMANAGER_H
219