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