Coverage Report

Created: 2022-01-18 06:27

/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
6.33k
    DumpToStreamOptions() = default;
32
33
6.32k
    void SetStream(Stream *s) { m_stream = s; }
34
35
14.0k
    Stream *GetStream() const { return m_stream; }
36
37
3.32k
    void SetPrefixToken(const std::string &p) { m_prefix_token = p; }
38
39
2.94k
    void SetPrefixToken(std::nullptr_t) { m_prefix_token.clear(); }
40
41
11.2k
    const char *GetPrefixToken() const { return m_prefix_token.c_str(); }
42
43
2.63k
    void SetSuffixToken(const std::string &p) { m_suffix_token = p; }
44
45
0
    void SetSuffixToken(std::nullptr_t) { m_suffix_token.clear(); }
46
47
11.2k
    const char *GetSuffixToken() const { return m_suffix_token.c_str(); }
48
49
5.11k
    void SetQuote(char q) { m_quote = q; }
50
51
21.1k
    char GetQuote() const { return m_quote; }
52
53
6.21k
    void SetSourceSize(uint32_t s) { m_source_size = s; }
54
55
11.1k
    uint32_t GetSourceSize() const { return m_source_size; }
56
57
1.88k
    void SetNeedsZeroTermination(bool z) { m_needs_zero_termination = z; }
58
59
1.37k
    bool GetNeedsZeroTermination() const { return m_needs_zero_termination; }
60
61
6.28k
    void SetBinaryZeroIsTerminator(bool e) { m_zero_is_terminator = e; }
62
63
6.29k
    bool GetBinaryZeroIsTerminator() const { return m_zero_is_terminator; }
64
65
6.33k
    void SetEscapeNonPrintables(bool e) { m_escape_non_printables = e; }
66
67
6.29k
    bool GetEscapeNonPrintables() const { return m_escape_non_printables; }
68
69
1.32k
    void SetIgnoreMaxLength(bool e) { m_ignore_max_length = e; }
70
71
464
    bool GetIgnoreMaxLength() const { return m_ignore_max_length; }
72
73
1.42k
    void SetEscapeStyle(EscapeStyle style) { m_escape_style = style; }
74
75
6.27k
    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
1.37k
    ReadStringAndDumpToStreamOptions() = default;
105
106
    ReadStringAndDumpToStreamOptions(ValueObject &valobj);
107
108
1.37k
    void SetLocation(Address l) { m_location = std::move(l); }
109
110
4.12k
    const Address &GetLocation() const { return m_location; }
111
112
1.37k
    void SetTargetSP(lldb::TargetSP t) { m_target_sp = std::move(t); }
113
114
1.37k
    lldb::TargetSP GetTargetSP() const { return m_target_sp; }
115
116
1.26k
    void SetHasSourceSize(bool e) { m_has_source_size = e; }
117
118
571
    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
4.95k
    ReadBufferAndDumpToStreamOptions() = default;
130
131
    ReadBufferAndDumpToStreamOptions(ValueObject &valobj);
132
133
    ReadBufferAndDumpToStreamOptions(
134
        const ReadStringAndDumpToStreamOptions &options);
135
136
4.94k
    void SetData(DataExtractor &&d) { m_data = std::move(d); }
137
138
4.94k
    const lldb_private::DataExtractor &GetData() const { return m_data; }
139
140
3.48k
    void SetIsTruncated(bool t) { m_is_truncated = t; }
141
142
4.94k
    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