]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/include/lldb/DataFormatters/StringPrinter.h
Merge clang 7.0.1 and several follow-up changes
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / include / lldb / DataFormatters / StringPrinter.h
1 //===-- StringPrinter.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 liblldb_StringPrinter_h_
11 #define liblldb_StringPrinter_h_
12
13 // C Includes
14 // C++ Includes
15 #include <functional>
16 #include <string>
17
18 // Other libraries and framework includes
19 // Project includes
20 #include "lldb/lldb-forward.h"
21
22 #include "lldb/Utility/DataExtractor.h"
23
24 namespace lldb_private {
25 namespace formatters {
26 class StringPrinter {
27 public:
28   enum class StringElementType { ASCII, UTF8, UTF16, UTF32 };
29
30   enum class GetPrintableElementType { ASCII, UTF8 };
31
32   class ReadStringAndDumpToStreamOptions {
33   public:
34     ReadStringAndDumpToStreamOptions()
35         : m_location(0), m_process_sp(), m_stream(nullptr), m_prefix_token(),
36           m_suffix_token(), m_quote('"'), m_source_size(0),
37           m_needs_zero_termination(true), m_escape_non_printables(true),
38           m_ignore_max_length(false), m_zero_is_terminator(true),
39           m_language_type(lldb::eLanguageTypeUnknown) {}
40
41     ReadStringAndDumpToStreamOptions(ValueObject &valobj);
42
43     ReadStringAndDumpToStreamOptions &SetLocation(uint64_t l) {
44       m_location = l;
45       return *this;
46     }
47
48     uint64_t GetLocation() const { return m_location; }
49
50     ReadStringAndDumpToStreamOptions &SetProcessSP(lldb::ProcessSP p) {
51       m_process_sp = p;
52       return *this;
53     }
54
55     lldb::ProcessSP GetProcessSP() const { return m_process_sp; }
56
57     ReadStringAndDumpToStreamOptions &SetStream(Stream *s) {
58       m_stream = s;
59       return *this;
60     }
61
62     Stream *GetStream() const { return m_stream; }
63
64     ReadStringAndDumpToStreamOptions &SetPrefixToken(const std::string &p) {
65       m_prefix_token = p;
66       return *this;
67     }
68
69     ReadStringAndDumpToStreamOptions &SetPrefixToken(std::nullptr_t) {
70       m_prefix_token.clear();
71       return *this;
72     }
73
74     const char *GetPrefixToken() const { return m_prefix_token.c_str(); }
75
76     ReadStringAndDumpToStreamOptions &SetSuffixToken(const std::string &p) {
77       m_suffix_token = p;
78       return *this;
79     }
80
81     ReadStringAndDumpToStreamOptions &SetSuffixToken(std::nullptr_t) {
82       m_suffix_token.clear();
83       return *this;
84     }
85
86     const char *GetSuffixToken() const { return m_suffix_token.c_str(); }
87
88     ReadStringAndDumpToStreamOptions &SetQuote(char q) {
89       m_quote = q;
90       return *this;
91     }
92
93     char GetQuote() const { return m_quote; }
94
95     ReadStringAndDumpToStreamOptions &SetSourceSize(uint32_t s) {
96       m_source_size = s;
97       return *this;
98     }
99
100     uint32_t GetSourceSize() const { return m_source_size; }
101
102     ReadStringAndDumpToStreamOptions &SetNeedsZeroTermination(bool z) {
103       m_needs_zero_termination = z;
104       return *this;
105     }
106
107     bool GetNeedsZeroTermination() const { return m_needs_zero_termination; }
108
109     ReadStringAndDumpToStreamOptions &SetBinaryZeroIsTerminator(bool e) {
110       m_zero_is_terminator = e;
111       return *this;
112     }
113
114     bool GetBinaryZeroIsTerminator() const { return m_zero_is_terminator; }
115
116     ReadStringAndDumpToStreamOptions &SetEscapeNonPrintables(bool e) {
117       m_escape_non_printables = e;
118       return *this;
119     }
120
121     bool GetEscapeNonPrintables() const { return m_escape_non_printables; }
122
123     ReadStringAndDumpToStreamOptions &SetIgnoreMaxLength(bool e) {
124       m_ignore_max_length = e;
125       return *this;
126     }
127
128     bool GetIgnoreMaxLength() const { return m_ignore_max_length; }
129
130     ReadStringAndDumpToStreamOptions &SetLanguage(lldb::LanguageType l) {
131       m_language_type = l;
132       return *this;
133     }
134
135     lldb::LanguageType GetLanguage() const
136
137     {
138       return m_language_type;
139     }
140
141   private:
142     uint64_t m_location;
143     lldb::ProcessSP m_process_sp;
144     Stream *m_stream;
145     std::string m_prefix_token;
146     std::string m_suffix_token;
147     char m_quote;
148     uint32_t m_source_size;
149     bool m_needs_zero_termination;
150     bool m_escape_non_printables;
151     bool m_ignore_max_length;
152     bool m_zero_is_terminator;
153     lldb::LanguageType m_language_type;
154   };
155
156   class ReadBufferAndDumpToStreamOptions {
157   public:
158     ReadBufferAndDumpToStreamOptions()
159         : m_data(), m_stream(nullptr), m_prefix_token(), m_suffix_token(),
160           m_quote('"'), m_source_size(0), m_escape_non_printables(true),
161           m_zero_is_terminator(true), m_is_truncated(false),
162           m_language_type(lldb::eLanguageTypeUnknown) {}
163
164     ReadBufferAndDumpToStreamOptions(ValueObject &valobj);
165
166     ReadBufferAndDumpToStreamOptions(
167         const ReadStringAndDumpToStreamOptions &options);
168
169     ReadBufferAndDumpToStreamOptions &SetData(DataExtractor d) {
170       m_data = d;
171       return *this;
172     }
173
174     lldb_private::DataExtractor GetData() const { return m_data; }
175
176     ReadBufferAndDumpToStreamOptions &SetStream(Stream *s) {
177       m_stream = s;
178       return *this;
179     }
180
181     Stream *GetStream() const { return m_stream; }
182
183     ReadBufferAndDumpToStreamOptions &SetPrefixToken(const std::string &p) {
184       m_prefix_token = p;
185       return *this;
186     }
187
188     ReadBufferAndDumpToStreamOptions &SetPrefixToken(std::nullptr_t) {
189       m_prefix_token.clear();
190       return *this;
191     }
192
193     const char *GetPrefixToken() const { return m_prefix_token.c_str(); }
194
195     ReadBufferAndDumpToStreamOptions &SetSuffixToken(const std::string &p) {
196       m_suffix_token = p;
197       return *this;
198     }
199
200     ReadBufferAndDumpToStreamOptions &SetSuffixToken(std::nullptr_t) {
201       m_suffix_token.clear();
202       return *this;
203     }
204
205     const char *GetSuffixToken() const { return m_suffix_token.c_str(); }
206
207     ReadBufferAndDumpToStreamOptions &SetQuote(char q) {
208       m_quote = q;
209       return *this;
210     }
211
212     char GetQuote() const { return m_quote; }
213
214     ReadBufferAndDumpToStreamOptions &SetSourceSize(uint32_t s) {
215       m_source_size = s;
216       return *this;
217     }
218
219     uint32_t GetSourceSize() const { return m_source_size; }
220
221     ReadBufferAndDumpToStreamOptions &SetEscapeNonPrintables(bool e) {
222       m_escape_non_printables = e;
223       return *this;
224     }
225
226     bool GetEscapeNonPrintables() const { return m_escape_non_printables; }
227
228     ReadBufferAndDumpToStreamOptions &SetBinaryZeroIsTerminator(bool e) {
229       m_zero_is_terminator = e;
230       return *this;
231     }
232
233     bool GetBinaryZeroIsTerminator() const { return m_zero_is_terminator; }
234
235     ReadBufferAndDumpToStreamOptions &SetIsTruncated(bool t) {
236       m_is_truncated = t;
237       return *this;
238     }
239
240     bool GetIsTruncated() const { return m_is_truncated; }
241
242     ReadBufferAndDumpToStreamOptions &SetLanguage(lldb::LanguageType l) {
243       m_language_type = l;
244       return *this;
245     }
246
247     lldb::LanguageType GetLanguage() const
248
249     {
250       return m_language_type;
251     }
252
253   private:
254     DataExtractor m_data;
255     Stream *m_stream;
256     std::string m_prefix_token;
257     std::string m_suffix_token;
258     char m_quote;
259     uint32_t m_source_size;
260     bool m_escape_non_printables;
261     bool m_zero_is_terminator;
262     bool m_is_truncated;
263     lldb::LanguageType m_language_type;
264   };
265
266   // I can't use a std::unique_ptr for this because the Deleter is a template
267   // argument there
268   // and I want the same type to represent both pointers I want to free and
269   // pointers I don't need to free - which is what this class essentially is
270   // It's very specialized to the needs of this file, and not suggested for
271   // general use
272   template <typename T = uint8_t, typename U = char, typename S = size_t>
273   struct StringPrinterBufferPointer {
274   public:
275     typedef std::function<void(const T *)> Deleter;
276
277     StringPrinterBufferPointer(std::nullptr_t ptr)
278         : m_data(nullptr), m_size(0), m_deleter() {}
279
280     StringPrinterBufferPointer(const T *bytes, S size,
281                                Deleter deleter = nullptr)
282         : m_data(bytes), m_size(size), m_deleter(deleter) {}
283
284     StringPrinterBufferPointer(const U *bytes, S size,
285                                Deleter deleter = nullptr)
286         : m_data(reinterpret_cast<const T *>(bytes)), m_size(size),
287           m_deleter(deleter) {}
288
289     StringPrinterBufferPointer(StringPrinterBufferPointer &&rhs)
290         : m_data(rhs.m_data), m_size(rhs.m_size), m_deleter(rhs.m_deleter) {
291       rhs.m_data = nullptr;
292     }
293
294     StringPrinterBufferPointer(const StringPrinterBufferPointer &rhs)
295         : m_data(rhs.m_data), m_size(rhs.m_size), m_deleter(rhs.m_deleter) {
296       rhs.m_data = nullptr; // this is why m_data has to be mutable
297     }
298
299     ~StringPrinterBufferPointer() {
300       if (m_data && m_deleter)
301         m_deleter(m_data);
302       m_data = nullptr;
303     }
304
305     const T *GetBytes() const { return m_data; }
306
307     const S GetSize() const { return m_size; }
308
309     StringPrinterBufferPointer &
310     operator=(const StringPrinterBufferPointer &rhs) {
311       if (m_data && m_deleter)
312         m_deleter(m_data);
313       m_data = rhs.m_data;
314       m_size = rhs.m_size;
315       m_deleter = rhs.m_deleter;
316       rhs.m_data = nullptr;
317       return *this;
318     }
319
320   private:
321     mutable const T *m_data;
322     size_t m_size;
323     Deleter m_deleter;
324   };
325
326   typedef std::function<StringPrinter::StringPrinterBufferPointer<
327       uint8_t, char, size_t>(uint8_t *, uint8_t *, uint8_t *&)>
328       EscapingHelper;
329   typedef std::function<EscapingHelper(GetPrintableElementType)>
330       EscapingHelperGenerator;
331
332   static EscapingHelper
333   GetDefaultEscapingHelper(GetPrintableElementType elem_type);
334
335   template <StringElementType element_type>
336   static bool
337   ReadStringAndDumpToStream(const ReadStringAndDumpToStreamOptions &options);
338
339   template <StringElementType element_type>
340   static bool
341   ReadBufferAndDumpToStream(const ReadBufferAndDumpToStreamOptions &options);
342 };
343
344 } // namespace formatters
345 } // namespace lldb_private
346
347 #endif // liblldb_StringPrinter_h_