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