xref: /freebsd/contrib/llvm-project/lldb/include/lldb/DataFormatters/StringPrinter.h (revision 349cc55c9796c4596a5b9904cd3281af295f878f)
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/Core/Address.h"
16 #include "lldb/Utility/DataExtractor.h"
17 #include "lldb/lldb-forward.h"
18 
19 namespace lldb_private {
20 namespace formatters {
21 class StringPrinter {
22 public:
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 
SetStream(Stream * s)33     void SetStream(Stream *s) { m_stream = s; }
34 
GetStream()35     Stream *GetStream() const { return m_stream; }
36 
SetPrefixToken(const std::string & p)37     void SetPrefixToken(const std::string &p) { m_prefix_token = p; }
38 
SetPrefixToken(std::nullptr_t)39     void SetPrefixToken(std::nullptr_t) { m_prefix_token.clear(); }
40 
GetPrefixToken()41     const char *GetPrefixToken() const { return m_prefix_token.c_str(); }
42 
SetSuffixToken(const std::string & p)43     void SetSuffixToken(const std::string &p) { m_suffix_token = p; }
44 
SetSuffixToken(std::nullptr_t)45     void SetSuffixToken(std::nullptr_t) { m_suffix_token.clear(); }
46 
GetSuffixToken()47     const char *GetSuffixToken() const { return m_suffix_token.c_str(); }
48 
SetQuote(char q)49     void SetQuote(char q) { m_quote = q; }
50 
GetQuote()51     char GetQuote() const { return m_quote; }
52 
SetSourceSize(uint32_t s)53     void SetSourceSize(uint32_t s) { m_source_size = s; }
54 
GetSourceSize()55     uint32_t GetSourceSize() const { return m_source_size; }
56 
SetNeedsZeroTermination(bool z)57     void SetNeedsZeroTermination(bool z) { m_needs_zero_termination = z; }
58 
GetNeedsZeroTermination()59     bool GetNeedsZeroTermination() const { return m_needs_zero_termination; }
60 
SetBinaryZeroIsTerminator(bool e)61     void SetBinaryZeroIsTerminator(bool e) { m_zero_is_terminator = e; }
62 
GetBinaryZeroIsTerminator()63     bool GetBinaryZeroIsTerminator() const { return m_zero_is_terminator; }
64 
SetEscapeNonPrintables(bool e)65     void SetEscapeNonPrintables(bool e) { m_escape_non_printables = e; }
66 
GetEscapeNonPrintables()67     bool GetEscapeNonPrintables() const { return m_escape_non_printables; }
68 
SetIgnoreMaxLength(bool e)69     void SetIgnoreMaxLength(bool e) { m_ignore_max_length = e; }
70 
GetIgnoreMaxLength()71     bool GetIgnoreMaxLength() const { return m_ignore_max_length; }
72 
SetEscapeStyle(EscapeStyle style)73     void SetEscapeStyle(EscapeStyle style) { m_escape_style = style; }
74 
GetEscapeStyle()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 
SetLocation(Address l)108     void SetLocation(Address l) { m_location = std::move(l); }
109 
GetLocation()110     const Address &GetLocation() const { return m_location; }
111 
SetTargetSP(lldb::TargetSP t)112     void SetTargetSP(lldb::TargetSP t) { m_target_sp = std::move(t); }
113 
GetTargetSP()114     lldb::TargetSP GetTargetSP() const { return m_target_sp; }
115 
SetHasSourceSize(bool e)116     void SetHasSourceSize(bool e) { m_has_source_size = e; }
117 
HasSourceSize()118     bool HasSourceSize() const { return m_has_source_size; }
119 
120   private:
121     Address m_location;
122     lldb::TargetSP m_target_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 
SetData(DataExtractor && d)136     void SetData(DataExtractor &&d) { m_data = std::move(d); }
137 
GetData()138     const lldb_private::DataExtractor &GetData() const { return m_data; }
139 
SetIsTruncated(bool t)140     void SetIsTruncated(bool t) { m_is_truncated = t; }
141 
GetIsTruncated()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