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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
|
//===-- StructuredData.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/Utility/StructuredData.h"
#include "lldb/Utility/FileSpec.h"
#include "lldb/Utility/Status.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/Support/MemoryBuffer.h"
#include <cerrno>
#include <cinttypes>
#include <cstdlib>
using namespace lldb_private;
using namespace llvm;
static StructuredData::ObjectSP ParseJSONValue(json::Value &value);
static StructuredData::ObjectSP ParseJSONObject(json::Object *object);
static StructuredData::ObjectSP ParseJSONArray(json::Array *array);
StructuredData::ObjectSP StructuredData::ParseJSON(llvm::StringRef json_text) {
llvm::Expected<json::Value> value = json::parse(json_text);
if (!value) {
llvm::consumeError(value.takeError());
return nullptr;
}
return ParseJSONValue(*value);
}
StructuredData::ObjectSP
StructuredData::ParseJSONFromFile(const FileSpec &input_spec, Status &error) {
StructuredData::ObjectSP return_sp;
auto buffer_or_error = llvm::MemoryBuffer::getFile(input_spec.GetPath());
if (!buffer_or_error) {
error = Status::FromErrorStringWithFormatv(
"could not open input file: {0} - {1}.", input_spec.GetPath(),
buffer_or_error.getError().message());
return return_sp;
}
llvm::Expected<json::Value> value =
json::parse(buffer_or_error.get()->getBuffer().str());
if (value)
return ParseJSONValue(*value);
error = Status::FromError(value.takeError());
return StructuredData::ObjectSP();
}
bool StructuredData::IsRecordType(const ObjectSP object_sp) {
return object_sp->GetType() == lldb::eStructuredDataTypeArray ||
object_sp->GetType() == lldb::eStructuredDataTypeDictionary;
}
static StructuredData::ObjectSP ParseJSONValue(json::Value &value) {
if (json::Object *O = value.getAsObject())
return ParseJSONObject(O);
if (json::Array *A = value.getAsArray())
return ParseJSONArray(A);
if (auto s = value.getAsString())
return std::make_shared<StructuredData::String>(*s);
if (auto b = value.getAsBoolean())
return std::make_shared<StructuredData::Boolean>(*b);
if (auto u = value.getAsUINT64())
return std::make_shared<StructuredData::UnsignedInteger>(*u);
if (auto i = value.getAsInteger())
return std::make_shared<StructuredData::SignedInteger>(*i);
if (auto d = value.getAsNumber())
return std::make_shared<StructuredData::Float>(*d);
if (auto n = value.getAsNull())
return std::make_shared<StructuredData::Null>();
return StructuredData::ObjectSP();
}
static StructuredData::ObjectSP ParseJSONObject(json::Object *object) {
auto dict_up = std::make_unique<StructuredData::Dictionary>();
for (auto &KV : *object) {
StringRef key = KV.first;
json::Value value = KV.second;
if (StructuredData::ObjectSP value_sp = ParseJSONValue(value))
dict_up->AddItem(key, value_sp);
}
return std::move(dict_up);
}
static StructuredData::ObjectSP ParseJSONArray(json::Array *array) {
auto array_up = std::make_unique<StructuredData::Array>();
for (json::Value &value : *array) {
if (StructuredData::ObjectSP value_sp = ParseJSONValue(value))
array_up->AddItem(value_sp);
}
return std::move(array_up);
}
StructuredData::ObjectSP
StructuredData::Object::GetObjectForDotSeparatedPath(llvm::StringRef path) {
if (GetType() == lldb::eStructuredDataTypeDictionary) {
std::pair<llvm::StringRef, llvm::StringRef> match = path.split('.');
llvm::StringRef key = match.first;
ObjectSP value = GetAsDictionary()->GetValueForKey(key);
if (!value)
return {};
// Do we have additional words to descend? If not, return the value
// we're at right now.
if (match.second.empty())
return value;
return value->GetObjectForDotSeparatedPath(match.second);
}
if (GetType() == lldb::eStructuredDataTypeArray) {
std::pair<llvm::StringRef, llvm::StringRef> match = path.split('[');
if (match.second.empty())
return shared_from_this();
uint64_t val = 0;
if (!llvm::to_integer(match.second, val, /* Base = */ 10))
return {};
return GetAsArray()->GetItemAtIndex(val);
}
return shared_from_this();
}
void StructuredData::Object::DumpToStdout(bool pretty_print) const {
json::OStream stream(llvm::outs(), pretty_print ? 2 : 0);
Serialize(stream);
}
void StructuredData::Array::Serialize(json::OStream &s) const {
s.arrayBegin();
for (const auto &item_sp : m_items) {
item_sp->Serialize(s);
}
s.arrayEnd();
}
void StructuredData::Float::Serialize(json::OStream &s) const {
s.value(m_value);
}
void StructuredData::Boolean::Serialize(json::OStream &s) const {
s.value(m_value);
}
void StructuredData::String::Serialize(json::OStream &s) const {
s.value(m_value);
}
void StructuredData::Dictionary::Serialize(json::OStream &s) const {
s.objectBegin();
// To ensure the output format is always stable, we sort the dictionary by key
// first.
using Entry = std::pair<llvm::StringRef, ObjectSP>;
std::vector<Entry> sorted_entries;
for (const auto &pair : m_dict)
sorted_entries.push_back({pair.first(), pair.second});
llvm::sort(sorted_entries);
for (const auto &pair : sorted_entries) {
s.attributeBegin(pair.first);
pair.second->Serialize(s);
s.attributeEnd();
}
s.objectEnd();
}
void StructuredData::Null::Serialize(json::OStream &s) const {
s.value(nullptr);
}
void StructuredData::Generic::Serialize(json::OStream &s) const {
s.value(llvm::formatv("{0:X}", m_object));
}
void StructuredData::Float::GetDescription(lldb_private::Stream &s) const {
s.Printf("%f", m_value);
}
void StructuredData::Boolean::GetDescription(lldb_private::Stream &s) const {
s.Printf(m_value ? "True" : "False");
}
void StructuredData::String::GetDescription(lldb_private::Stream &s) const {
s.Printf("%s", m_value.empty() ? "\"\"" : m_value.c_str());
}
void StructuredData::Array::GetDescription(lldb_private::Stream &s) const {
size_t index = 0;
size_t indentation_level = s.GetIndentLevel();
for (const auto &item_sp : m_items) {
// Sanitize.
if (!item_sp)
continue;
// Reset original indentation level.
s.SetIndentLevel(indentation_level);
s.Indent();
// Print key
s.Printf("[%zu]:", index++);
// Return to new line and increase indentation if value is record type.
// Otherwise add spacing.
bool should_indent = IsRecordType(item_sp);
if (should_indent) {
s.EOL();
s.IndentMore();
} else {
s.PutChar(' ');
}
// Print value and new line if now last pair.
item_sp->GetDescription(s);
if (item_sp != *(--m_items.end()))
s.EOL();
// Reset indentation level if it was incremented previously.
if (should_indent)
s.IndentLess();
}
}
void StructuredData::Dictionary::GetDescription(lldb_private::Stream &s) const {
size_t indentation_level = s.GetIndentLevel();
// To ensure the output format is always stable, we sort the dictionary by key
// first.
using Entry = std::pair<llvm::StringRef, ObjectSP>;
std::vector<Entry> sorted_entries;
for (const auto &pair : m_dict)
sorted_entries.push_back({pair.first(), pair.second});
llvm::sort(sorted_entries);
for (auto iter = sorted_entries.begin(); iter != sorted_entries.end();
iter++) {
// Sanitize.
if (iter->first.empty() || !iter->second)
continue;
// Reset original indentation level.
s.SetIndentLevel(indentation_level);
s.Indent();
// Print key.
s.Format("{0}:", iter->first);
// Return to new line and increase indentation if value is record type.
// Otherwise add spacing.
bool should_indent = IsRecordType(iter->second);
if (should_indent) {
s.EOL();
s.IndentMore();
} else {
s.PutChar(' ');
}
// Print value and new line if now last pair.
iter->second->GetDescription(s);
if (std::next(iter) != sorted_entries.end())
s.EOL();
// Reset indentation level if it was incremented previously.
if (should_indent)
s.IndentLess();
}
}
void StructuredData::Null::GetDescription(lldb_private::Stream &s) const {
s.Printf("NULL");
}
void StructuredData::Generic::GetDescription(lldb_private::Stream &s) const {
s.Printf("%p", m_object);
}
|