StringPrinter.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. //===-- StringPrinter.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_STRINGPRINTER_H
  9. #define LLDB_DATAFORMATTERS_STRINGPRINTER_H
  10. #include <functional>
  11. #include <string>
  12. #include "lldb/lldb-forward.h"
  13. #include "lldb/Utility/DataExtractor.h"
  14. namespace lldb_private {
  15. namespace formatters {
  16. class StringPrinter {
  17. public:
  18. enum class StringElementType { ASCII, UTF8, UTF16, UTF32 };
  19. enum class GetPrintableElementType { ASCII, UTF8 };
  20. enum class EscapeStyle { CXX, Swift };
  21. class DumpToStreamOptions {
  22. public:
  23. DumpToStreamOptions() = default;
  24. void SetStream(Stream *s) { m_stream = s; }
  25. Stream *GetStream() const { return m_stream; }
  26. void SetPrefixToken(const std::string &p) { m_prefix_token = p; }
  27. void SetPrefixToken(std::nullptr_t) { m_prefix_token.clear(); }
  28. const char *GetPrefixToken() const { return m_prefix_token.c_str(); }
  29. void SetSuffixToken(const std::string &p) { m_suffix_token = p; }
  30. void SetSuffixToken(std::nullptr_t) { m_suffix_token.clear(); }
  31. const char *GetSuffixToken() const { return m_suffix_token.c_str(); }
  32. void SetQuote(char q) { m_quote = q; }
  33. char GetQuote() const { return m_quote; }
  34. void SetSourceSize(uint32_t s) { m_source_size = s; }
  35. uint32_t GetSourceSize() const { return m_source_size; }
  36. void SetNeedsZeroTermination(bool z) { m_needs_zero_termination = z; }
  37. bool GetNeedsZeroTermination() const { return m_needs_zero_termination; }
  38. void SetBinaryZeroIsTerminator(bool e) { m_zero_is_terminator = e; }
  39. bool GetBinaryZeroIsTerminator() const { return m_zero_is_terminator; }
  40. void SetEscapeNonPrintables(bool e) { m_escape_non_printables = e; }
  41. bool GetEscapeNonPrintables() const { return m_escape_non_printables; }
  42. void SetIgnoreMaxLength(bool e) { m_ignore_max_length = e; }
  43. bool GetIgnoreMaxLength() const { return m_ignore_max_length; }
  44. void SetEscapeStyle(EscapeStyle style) { m_escape_style = style; }
  45. EscapeStyle GetEscapeStyle() const { return m_escape_style; }
  46. private:
  47. /// The used output stream.
  48. Stream *m_stream = nullptr;
  49. /// String that should be printed before the heading quote character.
  50. std::string m_prefix_token;
  51. /// String that should be printed after the trailing quote character.
  52. std::string m_suffix_token;
  53. /// The quote character that should surround the string.
  54. char m_quote = '"';
  55. /// The length of the memory region that should be dumped in bytes.
  56. uint32_t m_source_size = 0;
  57. bool m_needs_zero_termination = true;
  58. /// True iff non-printable characters should be escaped when dumping
  59. /// them to the stream.
  60. bool m_escape_non_printables = true;
  61. /// True iff the max-string-summary-length setting of the target should
  62. /// be ignored.
  63. bool m_ignore_max_length = false;
  64. /// True iff a zero bytes ('\0') should terminate the memory region that
  65. /// is being dumped.
  66. bool m_zero_is_terminator = true;
  67. /// The language-specific style for escaping special characters.
  68. EscapeStyle m_escape_style = EscapeStyle::CXX;
  69. };
  70. class ReadStringAndDumpToStreamOptions : public DumpToStreamOptions {
  71. public:
  72. ReadStringAndDumpToStreamOptions() = default;
  73. ReadStringAndDumpToStreamOptions(ValueObject &valobj);
  74. void SetLocation(uint64_t l) { m_location = l; }
  75. uint64_t GetLocation() const { return m_location; }
  76. void SetProcessSP(lldb::ProcessSP p) { m_process_sp = std::move(p); }
  77. lldb::ProcessSP GetProcessSP() const { return m_process_sp; }
  78. void SetHasSourceSize(bool e) { m_has_source_size = e; }
  79. bool HasSourceSize() const { return m_has_source_size; }
  80. private:
  81. uint64_t m_location = 0;
  82. lldb::ProcessSP m_process_sp;
  83. /// True iff we know the source size of the string.
  84. bool m_has_source_size = false;
  85. };
  86. class ReadBufferAndDumpToStreamOptions : public DumpToStreamOptions {
  87. public:
  88. ReadBufferAndDumpToStreamOptions() = default;
  89. ReadBufferAndDumpToStreamOptions(ValueObject &valobj);
  90. ReadBufferAndDumpToStreamOptions(
  91. const ReadStringAndDumpToStreamOptions &options);
  92. void SetData(DataExtractor d) { m_data = d; }
  93. lldb_private::DataExtractor GetData() const { return m_data; }
  94. void SetIsTruncated(bool t) { m_is_truncated = t; }
  95. bool GetIsTruncated() const { return m_is_truncated; }
  96. private:
  97. DataExtractor m_data;
  98. bool m_is_truncated = false;
  99. };
  100. template <StringElementType element_type>
  101. static bool
  102. ReadStringAndDumpToStream(const ReadStringAndDumpToStreamOptions &options);
  103. template <StringElementType element_type>
  104. static bool
  105. ReadBufferAndDumpToStream(const ReadBufferAndDumpToStreamOptions &options);
  106. };
  107. } // namespace formatters
  108. } // namespace lldb_private
  109. #endif // LLDB_DATAFORMATTERS_STRINGPRINTER_H