blob: 4a6e2e9051bffe213f018a9330e0c5f978eab28a [file] [log] [blame]
Andrew Walbran3d2c1972020-04-07 12:24:26 +01001//===-- 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
Olivier Deprezf4ef2d02021-04-20 13:36:24 +02009#ifndef LLDB_DATAFORMATTERS_STRINGPRINTER_H
10#define LLDB_DATAFORMATTERS_STRINGPRINTER_H
Andrew Walbran3d2c1972020-04-07 12:24:26 +010011
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
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020027 enum class EscapeStyle { CXX, Swift };
28
29 class DumpToStreamOptions {
Andrew Walbran3d2c1972020-04-07 12:24:26 +010030 public:
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020031 DumpToStreamOptions() = default;
Andrew Walbran3d2c1972020-04-07 12:24:26 +010032
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020033 void SetStream(Stream *s) { m_stream = s; }
Andrew Walbran3d2c1972020-04-07 12:24:26 +010034
35 Stream *GetStream() const { return m_stream; }
36
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020037 void SetPrefixToken(const std::string &p) { m_prefix_token = p; }
Andrew Walbran3d2c1972020-04-07 12:24:26 +010038
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020039 void SetPrefixToken(std::nullptr_t) { m_prefix_token.clear(); }
Andrew Walbran3d2c1972020-04-07 12:24:26 +010040
41 const char *GetPrefixToken() const { return m_prefix_token.c_str(); }
42
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020043 void SetSuffixToken(const std::string &p) { m_suffix_token = p; }
Andrew Walbran3d2c1972020-04-07 12:24:26 +010044
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020045 void SetSuffixToken(std::nullptr_t) { m_suffix_token.clear(); }
Andrew Walbran3d2c1972020-04-07 12:24:26 +010046
47 const char *GetSuffixToken() const { return m_suffix_token.c_str(); }
48
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020049 void SetQuote(char q) { m_quote = q; }
Andrew Walbran3d2c1972020-04-07 12:24:26 +010050
51 char GetQuote() const { return m_quote; }
52
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020053 void SetSourceSize(uint32_t s) { m_source_size = s; }
Andrew Walbran3d2c1972020-04-07 12:24:26 +010054
55 uint32_t GetSourceSize() const { return m_source_size; }
56
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020057 void SetNeedsZeroTermination(bool z) { m_needs_zero_termination = z; }
Andrew Walbran3d2c1972020-04-07 12:24:26 +010058
59 bool GetNeedsZeroTermination() const { return m_needs_zero_termination; }
60
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020061 void SetBinaryZeroIsTerminator(bool e) { m_zero_is_terminator = e; }
Andrew Walbran3d2c1972020-04-07 12:24:26 +010062
63 bool GetBinaryZeroIsTerminator() const { return m_zero_is_terminator; }
64
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020065 void SetEscapeNonPrintables(bool e) { m_escape_non_printables = e; }
Andrew Walbran3d2c1972020-04-07 12:24:26 +010066
67 bool GetEscapeNonPrintables() const { return m_escape_non_printables; }
68
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020069 void SetIgnoreMaxLength(bool e) { m_ignore_max_length = e; }
Andrew Walbran3d2c1972020-04-07 12:24:26 +010070
71 bool GetIgnoreMaxLength() const { return m_ignore_max_length; }
72
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020073 void SetEscapeStyle(EscapeStyle style) { m_escape_style = style; }
Andrew Walbran3d2c1972020-04-07 12:24:26 +010074
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020075 EscapeStyle GetEscapeStyle() const { return m_escape_style; }
Andrew Walbran3d2c1972020-04-07 12:24:26 +010076
77 private:
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020078 /// The used output stream.
79 Stream *m_stream = nullptr;
80 /// String that should be printed before the heading quote character.
Andrew Walbran3d2c1972020-04-07 12:24:26 +010081 std::string m_prefix_token;
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020082 /// String that should be printed after the trailing quote character.
Andrew Walbran3d2c1972020-04-07 12:24:26 +010083 std::string m_suffix_token;
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020084 /// 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;
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100100 };
101
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200102 class ReadStringAndDumpToStreamOptions : public DumpToStreamOptions {
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100103 public:
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200104 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;
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100130
131 ReadBufferAndDumpToStreamOptions(ValueObject &valobj);
132
133 ReadBufferAndDumpToStreamOptions(
134 const ReadStringAndDumpToStreamOptions &options);
135
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200136 void SetData(DataExtractor d) { m_data = d; }
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100137
138 lldb_private::DataExtractor GetData() const { return m_data; }
139
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200140 void SetIsTruncated(bool t) { m_is_truncated = t; }
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100141
142 bool GetIsTruncated() const { return m_is_truncated; }
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100143 private:
144 DataExtractor m_data;
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200145 bool m_is_truncated = false;
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100146 };
147
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100148 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
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200160#endif // LLDB_DATAFORMATTERS_STRINGPRINTER_H