StringPrinterTests.cpp
5.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
//===-- StringPrinterTests.cpp --------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "lldb/DataFormatters/StringPrinter.h"
#include "lldb/Utility/DataExtractor.h"
#include "lldb/Utility/Endian.h"
#include "lldb/Utility/StreamString.h"
#include "llvm/ADT/Optional.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/raw_ostream.h"
#include "gtest/gtest.h"
#include <string>
using namespace lldb;
using namespace lldb_private;
using lldb_private::formatters::StringPrinter;
using llvm::Optional;
using llvm::StringRef;
#define QUOTE(x) std::string("\"" x "\"")
/// Format \p input according to the specified string encoding and special char
/// escape style.
template <StringPrinter::StringElementType elem_ty>
static Optional<std::string> format(StringRef input,
StringPrinter::EscapeStyle escape_style) {
StreamString out;
StringPrinter::ReadBufferAndDumpToStreamOptions opts;
opts.SetStream(&out);
opts.SetSourceSize(input.size());
opts.SetNeedsZeroTermination(true);
opts.SetEscapeNonPrintables(true);
opts.SetIgnoreMaxLength(false);
opts.SetEscapeStyle(escape_style);
DataExtractor extractor(input.data(), input.size(),
endian::InlHostByteOrder(), sizeof(void *));
opts.SetData(extractor);
const bool success = StringPrinter::ReadBufferAndDumpToStream<elem_ty>(opts);
if (!success)
return llvm::None;
return out.GetString().str();
}
// Test ASCII formatting for C++. This behaves exactly like UTF8 formatting for
// C++, although that's questionable (see FIXME in StringPrinter.cpp).
TEST(StringPrinterTests, CxxASCII) {
auto fmt = [](StringRef str) {
return format<StringPrinter::StringElementType::ASCII>(
str, StringPrinter::EscapeStyle::CXX);
};
// Special escapes.
EXPECT_EQ(fmt({"\0", 1}), QUOTE(""));
EXPECT_EQ(fmt("\a"), QUOTE(R"(\a)"));
EXPECT_EQ(fmt("\b"), QUOTE(R"(\b)"));
EXPECT_EQ(fmt("\f"), QUOTE(R"(\f)"));
EXPECT_EQ(fmt("\n"), QUOTE(R"(\n)"));
EXPECT_EQ(fmt("\r"), QUOTE(R"(\r)"));
EXPECT_EQ(fmt("\t"), QUOTE(R"(\t)"));
EXPECT_EQ(fmt("\v"), QUOTE(R"(\v)"));
EXPECT_EQ(fmt("\""), QUOTE(R"(\")"));
EXPECT_EQ(fmt("\'"), QUOTE(R"(')"));
EXPECT_EQ(fmt("\\"), QUOTE(R"(\\)"));
// Printable characters.
EXPECT_EQ(fmt("'"), QUOTE("'"));
EXPECT_EQ(fmt("a"), QUOTE("a"));
EXPECT_EQ(fmt("Z"), QUOTE("Z"));
EXPECT_EQ(fmt("🥑"), QUOTE("🥑"));
// Octal (\nnn), hex (\xnn), extended octal (\unnnn or \Unnnnnnnn).
EXPECT_EQ(fmt("\uD55C"), QUOTE("\uD55C"));
EXPECT_EQ(fmt("\U00010348"), QUOTE("\U00010348"));
EXPECT_EQ(fmt("\376"), QUOTE(R"(\xfe)")); // \376 is 254 in decimal.
EXPECT_EQ(fmt("\xfe"), QUOTE(R"(\xfe)")); // \xfe is 254 in decimal.
}
// Test UTF8 formatting for C++.
TEST(StringPrinterTests, CxxUTF8) {
auto fmt = [](StringRef str) {
return format<StringPrinter::StringElementType::UTF8>(
str, StringPrinter::EscapeStyle::CXX);
};
// Special escapes.
EXPECT_EQ(fmt({"\0", 1}), QUOTE(""));
EXPECT_EQ(fmt("\a"), QUOTE(R"(\a)"));
EXPECT_EQ(fmt("\b"), QUOTE(R"(\b)"));
EXPECT_EQ(fmt("\f"), QUOTE(R"(\f)"));
EXPECT_EQ(fmt("\n"), QUOTE(R"(\n)"));
EXPECT_EQ(fmt("\r"), QUOTE(R"(\r)"));
EXPECT_EQ(fmt("\t"), QUOTE(R"(\t)"));
EXPECT_EQ(fmt("\v"), QUOTE(R"(\v)"));
EXPECT_EQ(fmt("\""), QUOTE(R"(\")"));
EXPECT_EQ(fmt("\'"), QUOTE(R"(')"));
EXPECT_EQ(fmt("\\"), QUOTE(R"(\\)"));
// Printable characters.
EXPECT_EQ(fmt("'"), QUOTE("'"));
EXPECT_EQ(fmt("a"), QUOTE("a"));
EXPECT_EQ(fmt("Z"), QUOTE("Z"));
EXPECT_EQ(fmt("🥑"), QUOTE("🥑"));
// Octal (\nnn), hex (\xnn), extended octal (\unnnn or \Unnnnnnnn).
EXPECT_EQ(fmt("\uD55C"), QUOTE("\uD55C"));
EXPECT_EQ(fmt("\U00010348"), QUOTE("\U00010348"));
EXPECT_EQ(fmt("\376"), QUOTE(R"(\xfe)")); // \376 is 254 in decimal.
EXPECT_EQ(fmt("\xfe"), QUOTE(R"(\xfe)")); // \xfe is 254 in decimal.
}
// Test UTF8 formatting for Swift.
TEST(StringPrinterTests, SwiftUTF8) {
auto fmt = [](StringRef str) {
return format<StringPrinter::StringElementType::UTF8>(
str, StringPrinter::EscapeStyle::Swift);
};
// Special escapes.
EXPECT_EQ(fmt({"\0", 1}), QUOTE(""));
EXPECT_EQ(fmt("\a"), QUOTE(R"(\a)"));
EXPECT_EQ(fmt("\b"), QUOTE(R"(\u{8})"));
EXPECT_EQ(fmt("\f"), QUOTE(R"(\u{c})"));
EXPECT_EQ(fmt("\n"), QUOTE(R"(\n)"));
EXPECT_EQ(fmt("\r"), QUOTE(R"(\r)"));
EXPECT_EQ(fmt("\t"), QUOTE(R"(\t)"));
EXPECT_EQ(fmt("\v"), QUOTE(R"(\u{b})"));
EXPECT_EQ(fmt("\""), QUOTE(R"(\")"));
EXPECT_EQ(fmt("\'"), QUOTE(R"(\')"));
EXPECT_EQ(fmt("\\"), QUOTE(R"(\\)"));
// Printable characters.
EXPECT_EQ(fmt("'"), QUOTE(R"(\')"));
EXPECT_EQ(fmt("a"), QUOTE("a"));
EXPECT_EQ(fmt("Z"), QUOTE("Z"));
EXPECT_EQ(fmt("🥑"), QUOTE("🥑"));
// Octal (\nnn), hex (\xnn), extended octal (\unnnn or \Unnnnnnnn).
EXPECT_EQ(fmt("\uD55C"), QUOTE("\uD55C"));
EXPECT_EQ(fmt("\U00010348"), QUOTE("\U00010348"));
EXPECT_EQ(fmt("\376"), QUOTE(R"(\u{fe})")); // \376 is 254 in decimal.
EXPECT_EQ(fmt("\xfe"), QUOTE(R"(\u{fe})")); // \xfe is 254 in decimal.
}