FormatClasses.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. //===-- FormatClasses.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_FORMATCLASSES_H
  9. #define LLDB_DATAFORMATTERS_FORMATCLASSES_H
  10. #include <functional>
  11. #include <memory>
  12. #include <string>
  13. #include <vector>
  14. #include "lldb/DataFormatters/TypeFormat.h"
  15. #include "lldb/DataFormatters/TypeSummary.h"
  16. #include "lldb/DataFormatters/TypeSynthetic.h"
  17. #include "lldb/Symbol/CompilerType.h"
  18. #include "lldb/Symbol/Type.h"
  19. #include "lldb/lldb-enumerations.h"
  20. #include "lldb/lldb-public.h"
  21. namespace lldb_private {
  22. class HardcodedFormatters {
  23. public:
  24. template <typename FormatterType>
  25. using HardcodedFormatterFinder =
  26. std::function<typename FormatterType::SharedPointer(
  27. lldb_private::ValueObject &, lldb::DynamicValueType,
  28. FormatManager &)>;
  29. template <typename FormatterType>
  30. using HardcodedFormatterFinders =
  31. std::vector<HardcodedFormatterFinder<FormatterType>>;
  32. typedef HardcodedFormatterFinders<TypeFormatImpl> HardcodedFormatFinder;
  33. typedef HardcodedFormatterFinders<TypeSummaryImpl> HardcodedSummaryFinder;
  34. typedef HardcodedFormatterFinders<SyntheticChildren> HardcodedSyntheticFinder;
  35. };
  36. class FormattersMatchCandidate {
  37. public:
  38. FormattersMatchCandidate(ConstString name, bool strip_ptr,
  39. bool strip_ref, bool strip_tydef)
  40. : m_type_name(name), m_stripped_pointer(strip_ptr),
  41. m_stripped_reference(strip_ref), m_stripped_typedef(strip_tydef) {}
  42. ~FormattersMatchCandidate() = default;
  43. ConstString GetTypeName() const { return m_type_name; }
  44. bool DidStripPointer() const { return m_stripped_pointer; }
  45. bool DidStripReference() const { return m_stripped_reference; }
  46. bool DidStripTypedef() const { return m_stripped_typedef; }
  47. template <class Formatter>
  48. bool IsMatch(const std::shared_ptr<Formatter> &formatter_sp) const {
  49. if (!formatter_sp)
  50. return false;
  51. if (formatter_sp->Cascades() == false && DidStripTypedef())
  52. return false;
  53. if (formatter_sp->SkipsPointers() && DidStripPointer())
  54. return false;
  55. if (formatter_sp->SkipsReferences() && DidStripReference())
  56. return false;
  57. return true;
  58. }
  59. private:
  60. ConstString m_type_name;
  61. bool m_stripped_pointer;
  62. bool m_stripped_reference;
  63. bool m_stripped_typedef;
  64. };
  65. typedef std::vector<FormattersMatchCandidate> FormattersMatchVector;
  66. typedef std::vector<lldb::LanguageType> CandidateLanguagesVector;
  67. class FormattersMatchData {
  68. public:
  69. FormattersMatchData(ValueObject &, lldb::DynamicValueType);
  70. FormattersMatchVector GetMatchesVector();
  71. ConstString GetTypeForCache();
  72. CandidateLanguagesVector GetCandidateLanguages();
  73. ValueObject &GetValueObject();
  74. lldb::DynamicValueType GetDynamicValueType();
  75. private:
  76. ValueObject &m_valobj;
  77. lldb::DynamicValueType m_dynamic_value_type;
  78. std::pair<FormattersMatchVector, bool> m_formatters_match_vector;
  79. ConstString m_type_for_cache;
  80. CandidateLanguagesVector m_candidate_languages;
  81. };
  82. class TypeNameSpecifierImpl {
  83. public:
  84. TypeNameSpecifierImpl() : m_is_regex(false), m_type() {}
  85. TypeNameSpecifierImpl(llvm::StringRef name, bool is_regex)
  86. : m_is_regex(is_regex), m_type() {
  87. m_type.m_type_name = std::string(name);
  88. }
  89. // if constructing with a given type, is_regex cannot be true since we are
  90. // giving an exact type to match
  91. TypeNameSpecifierImpl(lldb::TypeSP type) : m_is_regex(false), m_type() {
  92. if (type) {
  93. m_type.m_type_name = std::string(type->GetName().GetStringRef());
  94. m_type.m_compiler_type = type->GetForwardCompilerType();
  95. }
  96. }
  97. TypeNameSpecifierImpl(CompilerType type) : m_is_regex(false), m_type() {
  98. if (type.IsValid()) {
  99. m_type.m_type_name.assign(type.GetTypeName().GetCString());
  100. m_type.m_compiler_type = type;
  101. }
  102. }
  103. const char *GetName() {
  104. if (m_type.m_type_name.size())
  105. return m_type.m_type_name.c_str();
  106. return nullptr;
  107. }
  108. CompilerType GetCompilerType() {
  109. if (m_type.m_compiler_type.IsValid())
  110. return m_type.m_compiler_type;
  111. return CompilerType();
  112. }
  113. bool IsRegex() { return m_is_regex; }
  114. private:
  115. bool m_is_regex;
  116. // TODO: Replace this with TypeAndOrName.
  117. struct TypeOrName {
  118. std::string m_type_name;
  119. CompilerType m_compiler_type;
  120. };
  121. TypeOrName m_type;
  122. TypeNameSpecifierImpl(const TypeNameSpecifierImpl &) = delete;
  123. const TypeNameSpecifierImpl &
  124. operator=(const TypeNameSpecifierImpl &) = delete;
  125. };
  126. } // namespace lldb_private
  127. #endif // LLDB_DATAFORMATTERS_FORMATCLASSES_H