Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:42:46

0001 //===-- StringPrinter.h -----------------------------------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef LLDB_DATAFORMATTERS_STRINGPRINTER_H
0010 #define LLDB_DATAFORMATTERS_STRINGPRINTER_H
0011 
0012 #include <functional>
0013 #include <string>
0014 
0015 #include "lldb/Core/Address.h"
0016 #include "lldb/Utility/DataExtractor.h"
0017 #include "lldb/lldb-forward.h"
0018 
0019 namespace lldb_private {
0020 namespace formatters {
0021 class StringPrinter {
0022 public:
0023   enum class StringElementType { ASCII, UTF8, UTF16, UTF32 };
0024 
0025   enum class GetPrintableElementType { ASCII, UTF8 };
0026 
0027   enum class EscapeStyle { CXX, Swift };
0028 
0029   class DumpToStreamOptions {
0030   public:
0031     DumpToStreamOptions() = default;
0032 
0033     void SetStream(Stream *s) { m_stream = s; }
0034 
0035     Stream *GetStream() const { return m_stream; }
0036 
0037     void SetPrefixToken(const std::string &p) { m_prefix_token = p; }
0038 
0039     void SetPrefixToken(std::nullptr_t) { m_prefix_token.clear(); }
0040 
0041     const char *GetPrefixToken() const { return m_prefix_token.c_str(); }
0042 
0043     void SetSuffixToken(const std::string &p) { m_suffix_token = p; }
0044 
0045     void SetSuffixToken(std::nullptr_t) { m_suffix_token.clear(); }
0046 
0047     const char *GetSuffixToken() const { return m_suffix_token.c_str(); }
0048 
0049     void SetQuote(char q) { m_quote = q; }
0050 
0051     char GetQuote() const { return m_quote; }
0052 
0053     void SetSourceSize(uint32_t s) { m_source_size = s; }
0054 
0055     uint32_t GetSourceSize() const { return m_source_size; }
0056 
0057     void SetNeedsZeroTermination(bool z) { m_needs_zero_termination = z; }
0058 
0059     bool GetNeedsZeroTermination() const { return m_needs_zero_termination; }
0060 
0061     void SetBinaryZeroIsTerminator(bool e) { m_zero_is_terminator = e; }
0062 
0063     bool GetBinaryZeroIsTerminator() const { return m_zero_is_terminator; }
0064 
0065     void SetEscapeNonPrintables(bool e) { m_escape_non_printables = e; }
0066 
0067     bool GetEscapeNonPrintables() const { return m_escape_non_printables; }
0068 
0069     void SetIgnoreMaxLength(bool e) { m_ignore_max_length = e; }
0070 
0071     bool GetIgnoreMaxLength() const { return m_ignore_max_length; }
0072 
0073     void SetEscapeStyle(EscapeStyle style) { m_escape_style = style; }
0074 
0075     EscapeStyle GetEscapeStyle() const { return m_escape_style; }
0076 
0077   private:
0078     /// The used output stream.
0079     Stream *m_stream = nullptr;
0080     /// String that should be printed before the heading quote character.
0081     std::string m_prefix_token;
0082     /// String that should be printed after the trailing quote character.
0083     std::string m_suffix_token;
0084     /// The quote character that should surround the string.
0085     char m_quote = '"';
0086     /// The length of the memory region that should be dumped in bytes.
0087     uint32_t m_source_size = 0;
0088     bool m_needs_zero_termination = true;
0089     /// True iff non-printable characters should be escaped when dumping
0090     /// them to the stream.
0091     bool m_escape_non_printables = true;
0092     /// True iff the max-string-summary-length setting of the target should
0093     /// be ignored.
0094     bool m_ignore_max_length = false;
0095     /// True iff a zero bytes ('\0') should terminate the memory region that
0096     /// is being dumped.
0097     bool m_zero_is_terminator = true;
0098     /// The language-specific style for escaping special characters.
0099     EscapeStyle m_escape_style = EscapeStyle::CXX;
0100   };
0101 
0102   class ReadStringAndDumpToStreamOptions : public DumpToStreamOptions {
0103   public:
0104     ReadStringAndDumpToStreamOptions() = default;
0105 
0106     ReadStringAndDumpToStreamOptions(ValueObject &valobj);
0107 
0108     void SetLocation(Address l) { m_location = std::move(l); }
0109 
0110     const Address &GetLocation() const { return m_location; }
0111 
0112     void SetTargetSP(lldb::TargetSP t) { m_target_sp = std::move(t); }
0113 
0114     lldb::TargetSP GetTargetSP() const { return m_target_sp; }
0115 
0116     void SetHasSourceSize(bool e) { m_has_source_size = e; }
0117 
0118     bool HasSourceSize() const { return m_has_source_size; }
0119 
0120   private:
0121     Address m_location;
0122     lldb::TargetSP m_target_sp;
0123     /// True iff we know the source size of the string.
0124     bool m_has_source_size = false;
0125   };
0126 
0127   class ReadBufferAndDumpToStreamOptions : public DumpToStreamOptions {
0128   public:
0129     ReadBufferAndDumpToStreamOptions() = default;
0130 
0131     ReadBufferAndDumpToStreamOptions(ValueObject &valobj);
0132 
0133     ReadBufferAndDumpToStreamOptions(
0134         const ReadStringAndDumpToStreamOptions &options);
0135 
0136     void SetData(DataExtractor &&d) { m_data = std::move(d); }
0137 
0138     const lldb_private::DataExtractor &GetData() const { return m_data; }
0139 
0140     void SetIsTruncated(bool t) { m_is_truncated = t; }
0141 
0142     bool GetIsTruncated() const { return m_is_truncated; }
0143   private:
0144     DataExtractor m_data;
0145     bool m_is_truncated = false;
0146   };
0147 
0148   template <StringElementType element_type>
0149   static bool
0150   ReadStringAndDumpToStream(const ReadStringAndDumpToStreamOptions &options);
0151 
0152   template <StringElementType element_type>
0153   static bool
0154   ReadBufferAndDumpToStream(const ReadBufferAndDumpToStreamOptions &options);
0155 };
0156 
0157 } // namespace formatters
0158 } // namespace lldb_private
0159 
0160 #endif // LLDB_DATAFORMATTERS_STRINGPRINTER_H