18#include "llvm/ADT/StringExtras.h"
19#include "llvm/Support/ConvertUTF.h"
40 llvm_unreachable(
"unsupported length");
41 memcpy(
m_data, bytes, size);
63template <StringElementType type>
70 if (codepoint <= 0x1F || codepoint == 0x7F)
74 if (codepoint >= 0x80 && codepoint <= 0x9F)
78 if (codepoint == 0x2028 || codepoint == 0x2029)
82 if (codepoint == 0x200E || codepoint == 0x200F ||
83 (codepoint >= 0x202A &&
88 if (codepoint >= 0xFFF9 &&
98 const bool is_swift_escape_style =
99 escape_style == StringPrinter::EscapeStyle::Swift;
106 if (is_swift_escape_style)
110 if (is_swift_escape_style)
120 if (is_swift_escape_style)
126 if (is_swift_escape_style)
137 uint8_t *buffer, uint8_t *buffer_end, uint8_t *&next,
148 if (llvm::isPrint(*buffer))
151 unsigned escaped_len;
152 constexpr unsigned max_buffer_size = 7;
153 uint8_t data[max_buffer_size];
154 switch (escape_style) {
155 case StringPrinter::EscapeStyle::CXX:
157 escaped_len = snprintf((
char *)data, max_buffer_size,
"\\x%02x", *buffer);
159 case StringPrinter::EscapeStyle::Swift:
161 escaped_len = snprintf((
char *)data, max_buffer_size,
"\\u{%x}", *buffer);
164 lldbassert(escaped_len > 0 &&
"unknown string escape style");
165 return {data, escaped_len};
170 uint8_t *buffer, uint8_t *buffer_end, uint8_t *&next,
175 if (!llvm::isLegalUTF8Sequence(buffer, buffer_end))
180 llvm::UTF32 codepoint = 0;
181 const llvm::UTF8 *buffer_for_conversion = buffer;
182 llvm::ConversionResult result = llvm::convertUTF8Sequence(
183 &buffer_for_conversion, buffer_end, &codepoint, llvm::strictConversion);
184 assert(result == llvm::conversionOK &&
185 "Failed to convert legal utf8 sequence");
189 const unsigned utf8_encoded_len = buffer_for_conversion - buffer;
190 next = buffer + utf8_encoded_len;
196 return {buffer, utf8_encoded_len};
198 unsigned escaped_len;
199 constexpr unsigned max_buffer_size = 13;
200 uint8_t data[max_buffer_size];
201 switch (escape_style) {
202 case StringPrinter::EscapeStyle::CXX:
204 escaped_len = snprintf((
char *)data, max_buffer_size,
"\\U%08x", codepoint);
206 case StringPrinter::EscapeStyle::Swift:
208 escaped_len = snprintf((
char *)data, max_buffer_size,
"\\u{%x}", codepoint);
211 lldbassert(escaped_len > 0 &&
"unknown string escape style");
212 return {data, escaped_len};
219 uint8_t *buffer_end, uint8_t *&next,
221 if (!buffer || buffer >= buffer_end)
225 case StringElementType::ASCII:
228 case StringElementType::UTF8:
240 case GetPrintableElementType::UTF8:
241 case GetPrintableElementType::ASCII:
242 return [escape_style, elem_type](uint8_t *buffer, uint8_t *buffer_end,
244 return GetPrintable(elem_type == GetPrintableElementType::UTF8
245 ? StringElementType::UTF8
246 : StringElementType::ASCII,
247 buffer, buffer_end, next, escape_style);
250 llvm_unreachable(
"bad element type");
255template <
typename SourceDataType>
258 llvm::ConversionResult (*ConvertFunction)(
const SourceDataType **,
259 const SourceDataType *,
260 llvm::UTF8 **, llvm::UTF8 *,
261 llvm::ConversionFlags),
263 assert(dump_options.
GetStream() &&
"need a Stream to print the string to");
269 auto data(dump_options.
GetData());
271 if (data.GetByteSize() && data.GetDataStart() && data.GetDataEnd()) {
272 const int bufferSPSize = data.GetByteSize();
274 const int origin_encoding = 8 *
sizeof(SourceDataType);
275 source_size = bufferSPSize / (origin_encoding / 4);
278 const SourceDataType *data_ptr =
279 (
const SourceDataType *)data.GetDataStart();
280 const SourceDataType *data_end_ptr = data_ptr + source_size;
284 if (zero_is_terminator) {
285 while (data_ptr < data_end_ptr) {
287 data_end_ptr = data_ptr;
293 data_ptr = (
const SourceDataType *)data.GetDataStart();
297 llvm::UTF8 *utf8_data_ptr =
nullptr;
298 llvm::UTF8 *utf8_data_end_ptr =
nullptr;
300 if (ConvertFunction) {
301 utf8_data_buffer_sp =
302 std::make_shared<DataBufferHeap>(4 * bufferSPSize, 0);
303 utf8_data_ptr = (llvm::UTF8 *)utf8_data_buffer_sp->GetBytes();
304 utf8_data_end_ptr = utf8_data_ptr + utf8_data_buffer_sp->GetByteSize();
305 ConvertFunction(&data_ptr, data_end_ptr, &utf8_data_ptr,
306 utf8_data_end_ptr, llvm::lenientConversion);
307 if (!zero_is_terminator)
308 utf8_data_end_ptr = utf8_data_ptr;
312 (llvm::UTF8 *)utf8_data_buffer_sp->GetBytes();
316 utf8_data_ptr =
const_cast<llvm::UTF8 *
>(
317 reinterpret_cast<const llvm::UTF8 *
>(data_ptr));
318 utf8_data_end_ptr =
const_cast<llvm::UTF8 *
>(
319 reinterpret_cast<const llvm::UTF8 *
>(data_end_ptr));
324 if (escape_non_printables)
331 for (; utf8_data_ptr < utf8_data_end_ptr;) {
332 if (zero_is_terminator && !*utf8_data_ptr)
335 if (escape_non_printables) {
336 uint8_t *next_data =
nullptr;
338 escaping_callback(utf8_data_ptr, utf8_data_end_ptr, next_data);
339 auto printable_bytes = printable.GetBytes();
340 auto printable_size = printable.GetSize();
343 if (!printable_bytes || !next_data)
346 for (
unsigned c = 0; c < printable_size; c++)
347 stream.
Printf(
"%c", *(printable_bytes + c));
348 utf8_data_ptr = (uint8_t *)next_data;
350 stream.
Printf(
"%c", *utf8_data_ptr);
368 valobj.
GetTargetSP()->GetDebugger().GetEscapeNonPrintables());
375 valobj.
GetTargetSP()->GetDebugger().GetEscapeNonPrintables());
393namespace formatters {
395template <
typename SourceDataType>
399 llvm::ConversionResult (*ConvertFunction)(
const SourceDataType **,
400 const SourceDataType *,
401 llvm::UTF8 **, llvm::UTF8 *,
402 llvm::ConversionFlags)) {
403 assert(options.
GetStream() &&
"need a Stream to print the string to");
415 constexpr int type_width =
sizeof(SourceDataType);
416 constexpr int origin_encoding = 8 * type_width;
417 if (origin_encoding != 8 && origin_encoding != 16 && origin_encoding != 32)
420 if (origin_encoding != 8 && !ConvertFunction)
425 bool is_truncated =
false;
426 const auto max_size = target_sp->GetMaximumSizeOfStringSummary();
429 if (elem_type == StringElementType::ASCII && !options.
GetSourceSize()) {
437 sourceSize = max_size;
438 needs_zero_terminator =
true;
442 if (sourceSize > max_size) {
443 sourceSize = max_size;
448 sourceSize = max_size;
449 needs_zero_terminator =
true;
452 const int bufferSPSize = sourceSize * type_width;
458 if (sourceSize != 0 && !buffer_sp->GetBytes())
462 char *buffer =
reinterpret_cast<char *
>(buffer_sp->GetBytes());
464 if (elem_type == StringElementType::ASCII)
465 target_sp->ReadCStringFromMemory(options.
GetLocation(), buffer,
466 bufferSPSize,
error);
467 else if (needs_zero_terminator)
468 target_sp->ReadStringFromMemory(options.
GetLocation(), buffer,
469 bufferSPSize,
error, type_width);
479 DataExtractor(buffer_sp, target_sp->GetArchitecture().GetByteOrder(),
480 target_sp->GetArchitecture().GetAddressByteSize()));
484 if (needs_zero_terminator)
488 ? GetPrintableElementType::ASCII
489 : GetPrintableElementType::UTF8;
494bool StringPrinter::ReadStringAndDumpToStream<StringElementType::UTF8>(
495 const ReadStringAndDumpToStreamOptions &options) {
496 return ReadEncodedBufferAndDumpToStream<llvm::UTF8>(StringElementType::UTF8,
501bool StringPrinter::ReadStringAndDumpToStream<StringElementType::UTF16>(
502 const ReadStringAndDumpToStreamOptions &options) {
503 return ReadEncodedBufferAndDumpToStream<llvm::UTF16>(
504 StringElementType::UTF16, options, llvm::ConvertUTF16toUTF8);
508bool StringPrinter::ReadStringAndDumpToStream<StringElementType::UTF32>(
509 const ReadStringAndDumpToStreamOptions &options) {
510 return ReadEncodedBufferAndDumpToStream<llvm::UTF32>(
511 StringElementType::UTF32, options, llvm::ConvertUTF32toUTF8);
515bool StringPrinter::ReadStringAndDumpToStream<StringElementType::ASCII>(
516 const ReadStringAndDumpToStreamOptions &options) {
517 return ReadEncodedBufferAndDumpToStream<char>(StringElementType::ASCII,
522bool StringPrinter::ReadBufferAndDumpToStream<StringElementType::UTF8>(
523 const ReadBufferAndDumpToStreamOptions &options) {
524 return DumpEncodedBufferToStream<llvm::UTF8>(GetPrintableElementType::UTF8,
529bool StringPrinter::ReadBufferAndDumpToStream<StringElementType::UTF16>(
530 const ReadBufferAndDumpToStreamOptions &options) {
532 llvm::ConvertUTF16toUTF8, options);
536bool StringPrinter::ReadBufferAndDumpToStream<StringElementType::UTF32>(
537 const ReadBufferAndDumpToStreamOptions &options) {
539 llvm::ConvertUTF32toUTF8, options);
543bool StringPrinter::ReadBufferAndDumpToStream<StringElementType::ASCII>(
544 const ReadBufferAndDumpToStreamOptions &options) {
static llvm::raw_ostream & error(Stream &strm)
DecodedCharBuffer GetPrintableImpl< StringElementType::ASCII >(uint8_t *buffer, uint8_t *buffer_end, uint8_t *&next, StringPrinter::EscapeStyle escape_style)
static DecodedCharBuffer GetPrintableImpl(uint8_t *buffer, uint8_t *buffer_end, uint8_t *&next, StringPrinter::EscapeStyle escape_style)
DecodedCharBuffer attemptASCIIEscape(llvm::UTF32 c, StringPrinter::EscapeStyle escape_style)
std::function< DecodedCharBuffer(uint8_t *, uint8_t *, uint8_t *&)> EscapingHelper
static bool DumpEncodedBufferToStream(GetPrintableElementType style, llvm::ConversionResult(*ConvertFunction)(const SourceDataType **, const SourceDataType *, llvm::UTF8 **, llvm::UTF8 *, llvm::ConversionFlags), const StringPrinter::ReadBufferAndDumpToStreamOptions &dump_options)
Read a string encoded in accordance with.
DecodedCharBuffer GetPrintableImpl< StringElementType::UTF8 >(uint8_t *buffer, uint8_t *buffer_end, uint8_t *&next, StringPrinter::EscapeStyle escape_style)
static bool isprint32(char32_t codepoint)
static DecodedCharBuffer GetPrintable(StringElementType type, uint8_t *buffer, uint8_t *buffer_end, uint8_t *&next, StringPrinter::EscapeStyle escape_style)
static EscapingHelper GetDefaultEscapingHelper(GetPrintableElementType elem_type, StringPrinter::EscapeStyle escape_style)
DecodedCharBuffer stores the decoded contents of a single character.
DecodedCharBuffer(const uint8_t *bytes, size_t size)
DecodedCharBuffer(const char *bytes, size_t size)
static constexpr unsigned MaxLength
const uint8_t * GetBytes() const
DecodedCharBuffer(std::nullptr_t)
uint8_t m_data[MaxLength]
A subclass of DataBuffer that stores a data buffer on the heap.
A stream class that can stream formatted output to a file.
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
lldb::TargetSP GetTargetSP() const
#define UNUSED_IF_ASSERT_DISABLED(x)
#define LLDB_INVALID_ADDRESS
A class that represents a running process on the host machine.
std::shared_ptr< lldb_private::WritableDataBuffer > WritableDataBufferSP
std::shared_ptr< lldb_private::Target > TargetSP