Coverage Report

Created: 2023-11-11 10:31

/Users/buildslave/jenkins/workspace/coverage/llvm-project/lldb/include/lldb/DataFormatters/StringPrinter.h
Line
Count
Source (jump to first uncovered line)
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
4.91k
    DumpToStreamOptions() = default;
32
33
4.90k
    void SetStream(Stream *s) { m_stream = s; }
34
35
10.7k
    Stream *GetStream() const { return m_stream; }
36
37
2.35k
    void SetPrefixToken(const std::string &p) { m_prefix_token = p; }
38
39
2.49k
    void SetPrefixToken(std::nullptr_t) { m_prefix_token.clear(); }
40
41
8.81k
    const char *GetPrefixToken() const { return m_prefix_token.c_str(); }
42
43
1.90k
    void SetSuffixToken(const std::string &p) { m_suffix_token = p; }
44
45
0
    void SetSuffixToken(std::nullptr_t) { m_suffix_token.clear(); }
46
47
8.81k
    const char *GetSuffixToken() const { return m_suffix_token.c_str(); }
48
49
4.01k
    void SetQuote(char q) { m_quote = q; }
50
51
16.6k
    char GetQuote() const { return m_quote; }
52
53
4.84k
    void SetSourceSize(uint32_t s) { m_source_size = s; }
54
55
7.95k
    uint32_t GetSourceSize() const { return m_source_size; }
56
57
1.35k
    void SetNeedsZeroTermination(bool z) { m_needs_zero_termination = z; }
58
59
985
    bool GetNeedsZeroTermination() const { return m_needs_zero_termination; }
60
61
4.86k
    void SetBinaryZeroIsTerminator(bool e) { m_zero_is_terminator = e; }
62
63
4.07k
    bool GetBinaryZeroIsTerminator() const { return m_zero_is_terminator; }
64
65
4.91k
    void SetEscapeNonPrintables(bool e) { m_escape_non_printables = e; }
66
67
4.07k
    bool GetEscapeNonPrintables() const { return m_escape_non_printables; }
68
69
980
    void SetIgnoreMaxLength(bool e) { m_ignore_max_length = e; }
70
71
319
    bool GetIgnoreMaxLength() const { return m_ignore_max_length; }
72
73
1.04k
    void SetEscapeStyle(EscapeStyle style) { m_escape_style = style; }
74
75
4.06k
    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
989
    ReadStringAndDumpToStreamOptions() = default;
105
106
    ReadStringAndDumpToStreamOptions(ValueObject &valobj);
107
108
989
    void SetLocation(Address l) { m_location = std::move(l); }
109
110
2.95k
    const Address &GetLocation() const { return m_location; }
111
112
989
    void SetTargetSP(lldb::TargetSP t) { m_target_sp = std::move(t); }
113
114
985
    lldb::TargetSP GetTargetSP() const { return m_target_sp; }
115
116
923
    void SetHasSourceSize(bool e) { m_has_source_size = e; }
117
118
381
    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
3.92k
    ReadBufferAndDumpToStreamOptions() = default;
130
131
    ReadBufferAndDumpToStreamOptions(ValueObject &valobj);
132
133
    ReadBufferAndDumpToStreamOptions(
134
        const ReadStringAndDumpToStreamOptions &options);
135
136
3.91k
    void SetData(DataExtractor &&d) { m_data = std::move(d); }
137
138
3.91k
    const lldb_private::DataExtractor &GetData() const { return m_data; }
139
140
2.43k
    void SetIsTruncated(bool t) { m_is_truncated = t; }
141
142
3.91k
    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