forked from MaskRay/ccls
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserializer.cc
453 lines (413 loc) · 13.3 KB
/
serializer.cc
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
#include "serializer.h"
#include "filesystem.hh"
#include "log.hh"
#include "serializers/binary.h"
#include "serializers/json.h"
#include "indexer.h"
#include <stdexcept>
using namespace llvm;
bool gTestOutputMode = false;
//// Elementary types
void Reflect(Reader& visitor, uint8_t& value) {
value = visitor.GetUInt8();
}
void Reflect(Writer& visitor, uint8_t& value) {
visitor.UInt8(value);
}
void Reflect(Reader& visitor, short& value) {
if (!visitor.IsInt())
throw std::invalid_argument("short");
value = (short)visitor.GetInt();
}
void Reflect(Writer& visitor, short& value) {
visitor.Int(value);
}
void Reflect(Reader& visitor, unsigned short& value) {
if (!visitor.IsInt())
throw std::invalid_argument("unsigned short");
value = (unsigned short)visitor.GetInt();
}
void Reflect(Writer& visitor, unsigned short& value) {
visitor.Int(value);
}
void Reflect(Reader& visitor, int& value) {
if (!visitor.IsInt())
throw std::invalid_argument("int");
value = visitor.GetInt();
}
void Reflect(Writer& visitor, int& value) {
visitor.Int(value);
}
void Reflect(Reader& visitor, unsigned& value) {
if (!visitor.IsUInt64())
throw std::invalid_argument("unsigned");
value = visitor.GetUInt32();
}
void Reflect(Writer& visitor, unsigned& value) {
visitor.UInt32(value);
}
void Reflect(Reader& visitor, long& value) {
if (!visitor.IsInt64())
throw std::invalid_argument("long");
value = long(visitor.GetInt64());
}
void Reflect(Writer& visitor, long& value) {
visitor.Int64(value);
}
void Reflect(Reader& visitor, unsigned long& value) {
if (!visitor.IsUInt64())
throw std::invalid_argument("unsigned long");
value = (unsigned long)visitor.GetUInt64();
}
void Reflect(Writer& visitor, unsigned long& value) {
visitor.UInt64(value);
}
void Reflect(Reader& visitor, long long& value) {
if (!visitor.IsInt64())
throw std::invalid_argument("long long");
value = visitor.GetInt64();
}
void Reflect(Writer& visitor, long long& value) {
visitor.Int64(value);
}
void Reflect(Reader& visitor, unsigned long long& value) {
if (!visitor.IsUInt64())
throw std::invalid_argument("unsigned long long");
value = visitor.GetUInt64();
}
void Reflect(Writer& visitor, unsigned long long& value) {
visitor.UInt64(value);
}
void Reflect(Reader& visitor, double& value) {
if (!visitor.IsDouble())
throw std::invalid_argument("double");
value = visitor.GetDouble();
}
void Reflect(Writer& visitor, double& value) {
visitor.Double(value);
}
void Reflect(Reader& visitor, bool& value) {
if (!visitor.IsBool())
throw std::invalid_argument("bool");
value = visitor.GetBool();
}
void Reflect(Writer& visitor, bool& value) {
visitor.Bool(value);
}
void Reflect(Reader& visitor, std::string& value) {
if (!visitor.IsString())
throw std::invalid_argument("std::string");
value = visitor.GetString();
}
void Reflect(Writer& visitor, std::string& value) {
visitor.String(value.c_str(), (rapidjson::SizeType)value.size());
}
void Reflect(Reader&, std::string_view&) {
assert(0);
}
void Reflect(Writer& visitor, std::string_view& data) {
if (data.empty())
visitor.String("");
else
visitor.String(&data[0], (rapidjson::SizeType)data.size());
}
void Reflect(Reader& visitor, NtString& value) {
if (!visitor.IsString())
throw std::invalid_argument("std::string");
value = visitor.GetString();
}
void Reflect(Writer& visitor, NtString& value) {
const char* s = value.c_str();
visitor.String(s ? s : "");
}
void Reflect(Reader& visitor, JsonNull& value) {
assert(visitor.Format() == SerializeFormat::Json);
visitor.GetNull();
}
void Reflect(Writer& visitor, JsonNull& value) {
visitor.Null();
}
// std::unordered_map
template <typename V>
void Reflect(Reader& visitor, std::unordered_map<Usr, V>& map) {
visitor.IterArray([&](Reader& entry) {
V val;
Reflect(entry, val);
auto usr = val.usr;
map[usr] = std::move(val);
});
}
template <typename V>
void Reflect(Writer& visitor, std::unordered_map<Usr, V>& map) {
std::vector<std::pair<uint64_t, V>> xs(map.begin(), map.end());
std::sort(xs.begin(), xs.end(),
[](const auto& a, const auto& b) { return a.first < b.first; });
visitor.StartArray(xs.size());
for (auto& it : xs)
Reflect(visitor, it.second);
visitor.EndArray();
}
// Used by IndexFile::dependencies. Timestamps are emitted for Binary.
void Reflect(Reader& visitor, StringMap<int64_t>& map) {
visitor.IterArray([&](Reader& entry) {
std::string name;
Reflect(entry, name);
if (visitor.Format() == SerializeFormat::Binary)
Reflect(entry, map[name]);
else
map[name] = 0;
});
}
void Reflect(Writer& visitor, StringMap<int64_t>& map) {
visitor.StartArray(map.size());
for (auto& it : map) {
std::string key = it.first();
Reflect(visitor, key);
if (visitor.Format() == SerializeFormat::Binary)
Reflect(visitor, it.second);
}
visitor.EndArray();
}
// TODO: Move this to indexer.cc
void Reflect(Reader& visitor, IndexInclude& value) {
REFLECT_MEMBER_START();
REFLECT_MEMBER(line);
REFLECT_MEMBER(resolved_path);
REFLECT_MEMBER_END();
}
void Reflect(Writer& visitor, IndexInclude& value) {
REFLECT_MEMBER_START();
REFLECT_MEMBER(line);
if (gTestOutputMode) {
std::string basename = llvm::sys::path::filename(value.resolved_path);
if (!StartsWith(value.resolved_path, "&"))
basename = "&" + basename;
REFLECT_MEMBER2("resolved_path", basename);
} else {
REFLECT_MEMBER(resolved_path);
}
REFLECT_MEMBER_END();
}
template <typename Def>
void ReflectHoverAndComments(Reader& visitor, Def& def) {
ReflectMember(visitor, "hover", def.hover);
ReflectMember(visitor, "comments", def.comments);
}
template <typename Def>
void ReflectHoverAndComments(Writer& visitor, Def& def) {
// Don't emit empty hover and comments in JSON test mode.
if (!gTestOutputMode || !def.hover.empty())
ReflectMember(visitor, "hover", def.hover);
if (!gTestOutputMode || !def.comments.empty())
ReflectMember(visitor, "comments", def.comments);
}
template <typename Def>
void ReflectShortName(Reader& visitor, Def& def) {
if (gTestOutputMode) {
std::string short_name;
ReflectMember(visitor, "short_name", short_name);
def.short_name_offset = def.detailed_name.find(short_name);
assert(def.short_name_offset != std::string::npos);
def.short_name_size = short_name.size();
} else {
ReflectMember(visitor, "short_name_offset", def.short_name_offset);
ReflectMember(visitor, "short_name_size", def.short_name_size);
}
}
template <typename Def>
void ReflectShortName(Writer& visitor, Def& def) {
if (gTestOutputMode) {
std::string short_name(
def.detailed_name.substr(def.short_name_offset, def.short_name_size));
ReflectMember(visitor, "short_name", short_name);
} else {
ReflectMember(visitor, "short_name_offset", def.short_name_offset);
ReflectMember(visitor, "short_name_size", def.short_name_size);
}
}
template <typename TVisitor>
void Reflect(TVisitor& visitor, IndexType& value) {
REFLECT_MEMBER_START();
REFLECT_MEMBER2("usr", value.usr);
REFLECT_MEMBER2("detailed_name", value.def.detailed_name);
REFLECT_MEMBER2("qual_name_offset", value.def.qual_name_offset);
ReflectShortName(visitor, value.def);
REFLECT_MEMBER2("kind", value.def.kind);
ReflectHoverAndComments(visitor, value.def);
REFLECT_MEMBER2("declarations", value.declarations);
REFLECT_MEMBER2("spell", value.def.spell);
REFLECT_MEMBER2("extent", value.def.extent);
REFLECT_MEMBER2("alias_of", value.def.alias_of);
REFLECT_MEMBER2("bases", value.def.bases);
REFLECT_MEMBER2("derived", value.derived);
REFLECT_MEMBER2("types", value.def.types);
REFLECT_MEMBER2("funcs", value.def.funcs);
REFLECT_MEMBER2("vars", value.def.vars);
REFLECT_MEMBER2("instances", value.instances);
REFLECT_MEMBER2("uses", value.uses);
REFLECT_MEMBER_END();
}
template <typename TVisitor>
void Reflect(TVisitor& visitor, IndexFunc& value) {
REFLECT_MEMBER_START();
REFLECT_MEMBER2("usr", value.usr);
REFLECT_MEMBER2("detailed_name", value.def.detailed_name);
REFLECT_MEMBER2("qual_name_offset", value.def.qual_name_offset);
ReflectShortName(visitor, value.def);
REFLECT_MEMBER2("kind", value.def.kind);
REFLECT_MEMBER2("storage", value.def.storage);
ReflectHoverAndComments(visitor, value.def);
REFLECT_MEMBER2("declarations", value.declarations);
REFLECT_MEMBER2("spell", value.def.spell);
REFLECT_MEMBER2("extent", value.def.extent);
REFLECT_MEMBER2("declaring_type", value.def.declaring_type);
REFLECT_MEMBER2("bases", value.def.bases);
REFLECT_MEMBER2("derived", value.derived);
REFLECT_MEMBER2("vars", value.def.vars);
REFLECT_MEMBER2("uses", value.uses);
REFLECT_MEMBER2("callees", value.def.callees);
REFLECT_MEMBER_END();
}
template <typename TVisitor>
void Reflect(TVisitor& visitor, IndexVar& value) {
REFLECT_MEMBER_START();
REFLECT_MEMBER2("usr", value.usr);
REFLECT_MEMBER2("detailed_name", value.def.detailed_name);
REFLECT_MEMBER2("qual_name_offset", value.def.qual_name_offset);
ReflectShortName(visitor, value.def);
ReflectHoverAndComments(visitor, value.def);
REFLECT_MEMBER2("declarations", value.declarations);
REFLECT_MEMBER2("spell", value.def.spell);
REFLECT_MEMBER2("extent", value.def.extent);
REFLECT_MEMBER2("type", value.def.type);
REFLECT_MEMBER2("uses", value.uses);
REFLECT_MEMBER2("kind", value.def.kind);
REFLECT_MEMBER2("storage", value.def.storage);
REFLECT_MEMBER_END();
}
// IndexFile
bool ReflectMemberStart(Writer& visitor, IndexFile& value) {
visitor.StartObject();
return true;
}
template <typename TVisitor>
void Reflect(TVisitor& visitor, IndexFile& value) {
REFLECT_MEMBER_START();
if (!gTestOutputMode) {
REFLECT_MEMBER(last_write_time);
REFLECT_MEMBER(language);
REFLECT_MEMBER(import_file);
REFLECT_MEMBER(args);
REFLECT_MEMBER(dependencies);
}
REFLECT_MEMBER(includes);
REFLECT_MEMBER(skipped_by_preprocessor);
REFLECT_MEMBER(usr2func);
REFLECT_MEMBER(usr2type);
REFLECT_MEMBER(usr2var);
REFLECT_MEMBER_END();
}
void Reflect(Reader& visitor, SerializeFormat& value) {
std::string fmt = visitor.GetString();
value = fmt[0] == 'b' ? SerializeFormat::Binary : SerializeFormat::Json;
}
void Reflect(Writer& visitor, SerializeFormat& value) {
switch (value) {
case SerializeFormat::Binary:
visitor.String("binary");
break;
case SerializeFormat::Json:
visitor.String("json");
break;
}
}
std::string Serialize(SerializeFormat format, IndexFile& file) {
switch (format) {
case SerializeFormat::Binary: {
BinaryWriter writer;
int major = IndexFile::kMajorVersion;
int minor = IndexFile::kMinorVersion;
Reflect(writer, major);
Reflect(writer, minor);
Reflect(writer, file);
return writer.Take();
}
case SerializeFormat::Json: {
rapidjson::StringBuffer output;
rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(output);
writer.SetFormatOptions(
rapidjson::PrettyFormatOptions::kFormatSingleLineArray);
writer.SetIndent(' ', 2);
JsonWriter json_writer(&writer);
if (!gTestOutputMode) {
std::string version = std::to_string(IndexFile::kMajorVersion);
for (char c : version)
output.Put(c);
output.Put('\n');
}
Reflect(json_writer, file);
return output.GetString();
}
}
return "";
}
std::unique_ptr<IndexFile> Deserialize(
SerializeFormat format,
const std::string& path,
const std::string& serialized_index_content,
const std::string& file_content,
std::optional<int> expected_version) {
if (serialized_index_content.empty())
return nullptr;
std::unique_ptr<IndexFile> file;
switch (format) {
case SerializeFormat::Binary: {
try {
int major, minor;
if (serialized_index_content.size() < 8)
throw std::invalid_argument("Invalid");
BinaryReader reader(serialized_index_content);
Reflect(reader, major);
Reflect(reader, minor);
if (major != IndexFile::kMajorVersion ||
minor != IndexFile::kMinorVersion)
throw std::invalid_argument("Invalid version");
file = std::make_unique<IndexFile>(path, file_content);
Reflect(reader, *file);
} catch (std::invalid_argument& e) {
LOG_S(INFO) << "failed to deserialize '" << path
<< "': " << e.what();
return nullptr;
}
break;
}
case SerializeFormat::Json: {
rapidjson::Document reader;
if (gTestOutputMode || !expected_version) {
reader.Parse(serialized_index_content.c_str());
} else {
const char* p = strchr(serialized_index_content.c_str(), '\n');
if (!p)
return nullptr;
if (atoi(serialized_index_content.c_str()) != *expected_version)
return nullptr;
reader.Parse(p + 1);
}
if (reader.HasParseError())
return nullptr;
file = std::make_unique<IndexFile>(path, file_content);
JsonReader json_reader{&reader};
try {
Reflect(json_reader, *file);
} catch (std::invalid_argument& e) {
LOG_S(INFO) << "'" << path << "': failed to deserialize "
<< json_reader.GetPath() << "." << e.what();
return nullptr;
}
break;
}
}
// Restore non-serialized state.
file->path = path;
return file;
}