FormatManager.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. #ifndef LLDB_DATAFORMATTERS_FORMATMANAGER_H
  9. #define LLDB_DATAFORMATTERS_FORMATMANAGER_H
  10. #include <atomic>
  11. #include <initializer_list>
  12. #include <map>
  13. #include <mutex>
  14. #include <vector>
  15. #include "lldb/lldb-enumerations.h"
  16. #include "lldb/lldb-public.h"
  17. #include "lldb/DataFormatters/FormatCache.h"
  18. #include "lldb/DataFormatters/FormatClasses.h"
  19. #include "lldb/DataFormatters/FormattersContainer.h"
  20. #include "lldb/DataFormatters/LanguageCategory.h"
  21. #include "lldb/DataFormatters/TypeCategory.h"
  22. #include "lldb/DataFormatters/TypeCategoryMap.h"
  23. namespace lldb_private {
  24. // this file (and its. cpp) contain the low-level implementation of LLDB Data
  25. // Visualization class DataVisualization is the high-level front-end of this
  26. // feature clients should refer to that class as the entry-point into the data
  27. // formatters unless they have a good reason to bypass it and prefer to use
  28. // this file's objects directly
  29. class FormatManager : public IFormatChangeListener {
  30. typedef FormattersContainer<TypeSummaryImpl> NamedSummariesMap;
  31. typedef TypeCategoryMap::MapType::iterator CategoryMapIterator;
  32. public:
  33. typedef std::map<lldb::LanguageType, LanguageCategory::UniquePointer>
  34. LanguageCategories;
  35. FormatManager();
  36. ~FormatManager() override = default;
  37. NamedSummariesMap &GetNamedSummaryContainer() {
  38. return m_named_summaries_map;
  39. }
  40. void
  41. EnableCategory(ConstString category_name,
  42. TypeCategoryMap::Position pos = TypeCategoryMap::Default) {
  43. EnableCategory(category_name, pos, {});
  44. }
  45. void EnableCategory(ConstString category_name,
  46. TypeCategoryMap::Position pos, lldb::LanguageType lang) {
  47. TypeCategoryMap::ValueSP category_sp;
  48. if (m_categories_map.Get(category_name, category_sp) && category_sp) {
  49. m_categories_map.Enable(category_sp, pos);
  50. category_sp->AddLanguage(lang);
  51. }
  52. }
  53. void DisableCategory(ConstString category_name) {
  54. m_categories_map.Disable(category_name);
  55. }
  56. void
  57. EnableCategory(const lldb::TypeCategoryImplSP &category,
  58. TypeCategoryMap::Position pos = TypeCategoryMap::Default) {
  59. m_categories_map.Enable(category, pos);
  60. }
  61. void DisableCategory(const lldb::TypeCategoryImplSP &category) {
  62. m_categories_map.Disable(category);
  63. }
  64. void EnableAllCategories();
  65. void DisableAllCategories();
  66. bool DeleteCategory(ConstString category_name) {
  67. return m_categories_map.Delete(category_name);
  68. }
  69. void ClearCategories() { return m_categories_map.Clear(); }
  70. uint32_t GetCategoriesCount() { return m_categories_map.GetCount(); }
  71. lldb::TypeCategoryImplSP GetCategoryAtIndex(size_t index) {
  72. return m_categories_map.GetAtIndex(index);
  73. }
  74. void ForEachCategory(TypeCategoryMap::ForEachCallback callback);
  75. lldb::TypeCategoryImplSP GetCategory(const char *category_name = nullptr,
  76. bool can_create = true) {
  77. if (!category_name)
  78. return GetCategory(m_default_category_name);
  79. return GetCategory(ConstString(category_name));
  80. }
  81. lldb::TypeCategoryImplSP GetCategory(ConstString category_name,
  82. bool can_create = true);
  83. lldb::TypeFormatImplSP
  84. GetFormatForType(lldb::TypeNameSpecifierImplSP type_sp);
  85. lldb::TypeSummaryImplSP
  86. GetSummaryForType(lldb::TypeNameSpecifierImplSP type_sp);
  87. lldb::TypeFilterImplSP
  88. GetFilterForType(lldb::TypeNameSpecifierImplSP type_sp);
  89. lldb::ScriptedSyntheticChildrenSP
  90. GetSyntheticForType(lldb::TypeNameSpecifierImplSP type_sp);
  91. lldb::TypeFormatImplSP GetFormat(ValueObject &valobj,
  92. lldb::DynamicValueType use_dynamic);
  93. lldb::TypeSummaryImplSP GetSummaryFormat(ValueObject &valobj,
  94. lldb::DynamicValueType use_dynamic);
  95. lldb::SyntheticChildrenSP
  96. GetSyntheticChildren(ValueObject &valobj, lldb::DynamicValueType use_dynamic);
  97. bool
  98. AnyMatches(ConstString type_name,
  99. TypeCategoryImpl::FormatCategoryItems items =
  100. TypeCategoryImpl::ALL_ITEM_TYPES,
  101. bool only_enabled = true, const char **matching_category = nullptr,
  102. TypeCategoryImpl::FormatCategoryItems *matching_type = nullptr) {
  103. return m_categories_map.AnyMatches(type_name, items, only_enabled,
  104. matching_category, matching_type);
  105. }
  106. static bool GetFormatFromCString(const char *format_cstr,
  107. bool partial_match_ok, lldb::Format &format);
  108. static char GetFormatAsFormatChar(lldb::Format format);
  109. static const char *GetFormatAsCString(lldb::Format format);
  110. // when DataExtractor dumps a vectorOfT, it uses a predefined format for each
  111. // item this method returns it, or eFormatInvalid if vector_format is not a
  112. // vectorOf
  113. static lldb::Format GetSingleItemFormat(lldb::Format vector_format);
  114. // this returns true if the ValueObjectPrinter is *highly encouraged* to
  115. // actually represent this ValueObject in one-liner format If this object has
  116. // a summary formatter, however, we should not try and do one-lining, just
  117. // let the summary do the right thing
  118. bool ShouldPrintAsOneLiner(ValueObject &valobj);
  119. void Changed() override;
  120. uint32_t GetCurrentRevision() override { return m_last_revision; }
  121. static FormattersMatchVector
  122. GetPossibleMatches(ValueObject &valobj, lldb::DynamicValueType use_dynamic) {
  123. FormattersMatchVector matches;
  124. GetPossibleMatches(valobj, valobj.GetCompilerType(),
  125. use_dynamic, matches, false, false, false, true);
  126. return matches;
  127. }
  128. static ConstString GetTypeForCache(ValueObject &, lldb::DynamicValueType);
  129. LanguageCategory *GetCategoryForLanguage(lldb::LanguageType lang_type);
  130. static std::vector<lldb::LanguageType>
  131. GetCandidateLanguages(lldb::LanguageType lang_type);
  132. private:
  133. static void GetPossibleMatches(ValueObject &valobj,
  134. CompilerType compiler_type,
  135. lldb::DynamicValueType use_dynamic,
  136. FormattersMatchVector &entries,
  137. bool did_strip_ptr, bool did_strip_ref,
  138. bool did_strip_typedef,
  139. bool root_level = false);
  140. std::atomic<uint32_t> m_last_revision;
  141. FormatCache m_format_cache;
  142. std::recursive_mutex m_language_categories_mutex;
  143. LanguageCategories m_language_categories_map;
  144. NamedSummariesMap m_named_summaries_map;
  145. TypeCategoryMap m_categories_map;
  146. ConstString m_default_category_name;
  147. ConstString m_system_category_name;
  148. ConstString m_vectortypes_category_name;
  149. template <typename ImplSP>
  150. ImplSP Get(ValueObject &valobj, lldb::DynamicValueType use_dynamic);
  151. template <typename ImplSP> ImplSP GetCached(FormattersMatchData &match_data);
  152. template <typename ImplSP> ImplSP GetHardcoded(FormattersMatchData &);
  153. TypeCategoryMap &GetCategories() { return m_categories_map; }
  154. // These functions are meant to initialize formatters that are very low-
  155. // level/global in nature and do not naturally belong in any language. The
  156. // intent is that most formatters go in language-specific categories.
  157. // Eventually, the runtimes should also be allowed to vend their own
  158. // formatters, and then one could put formatters that depend on specific
  159. // library load events in the language runtimes, on an as-needed basis
  160. void LoadSystemFormatters();
  161. void LoadVectorFormatters();
  162. friend class FormattersMatchData;
  163. };
  164. } // namespace lldb_private
  165. #endif // LLDB_DATAFORMATTERS_FORMATMANAGER_H