forked from mikael-s-persson/templight
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TemplightProtobufWriter.cpp
362 lines (299 loc) · 10.7 KB
/
TemplightProtobufWriter.cpp
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
//===- TemplightProtobufWriter.cpp ------ Clang Templight Protobuf Writer -*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "TemplightProtobufWriter.h"
#include "PrintableTemplightEntries.h"
#include "ThinProtobuf.h"
#include <llvm/Support/raw_ostream.h>
#include <llvm/Support/Compression.h>
#include <llvm/Support/Error.h>
#include <string>
#include <cstdint>
namespace clang {
TemplightProtobufWriter::TemplightProtobufWriter(llvm::raw_ostream& aOS, int aCompressLevel) :
TemplightWriter(aOS), compressionMode(aCompressLevel) { }
void TemplightProtobufWriter::initialize(const std::string& aSourceName) {
std::string hdr_contents;
{
llvm::raw_string_ostream OS_inner(hdr_contents);
/*
message TemplightHeader {
required uint32 version = 1;
optional string source_file = 2;
}
*/
llvm::protobuf::saveVarInt(OS_inner, 1, 1); // version
if ( !aSourceName.empty() )
llvm::protobuf::saveString(OS_inner, 2, aSourceName); // source_file
}
llvm::raw_string_ostream OS(buffer);
// required TemplightHeader header = 1;
llvm::protobuf::saveString(OS, 1, hdr_contents);
}
void TemplightProtobufWriter::finalize() {
// repeated TemplightTrace traces = 1;
llvm::protobuf::saveString(OutputOS, 1, buffer);
}
std::string TemplightProtobufWriter::printEntryLocation(
const std::string& FileName, int Line, int Column) {
/*
message SourceLocation {
optional string file_name = 1;
required uint32 file_id = 2;
required uint32 line = 3;
optional uint32 column = 4;
}
*/
std::string location_contents;
llvm::raw_string_ostream OS_inner(location_contents);
std::unordered_map< std::string, std::size_t >::iterator
it = fileNameMap.find(FileName);
if ( it == fileNameMap.end() ) {
llvm::protobuf::saveString(OS_inner, 1, FileName); // file_name
std::size_t file_id = fileNameMap.size();
llvm::protobuf::saveVarInt(OS_inner, 2, file_id); // file_id
fileNameMap[FileName] = file_id;
} else {
llvm::protobuf::saveVarInt(OS_inner, 2, it->second); // file_id
}
llvm::protobuf::saveVarInt(OS_inner, 3, Line); // line
llvm::protobuf::saveVarInt(OS_inner, 4, Column); // column
OS_inner.str();
return location_contents; // NRVO
}
static void trimSpaces(std::string::iterator& it, std::string::iterator& it_end) {
while ( it < it_end ) {
if ( *it == ' ' )
++it;
else if ( *it_end == ' ' )
--it_end;
else
break;
}
++it_end;
}
std::size_t TemplightProtobufWriter::createDictionaryEntry(const std::string& NameOrig) {
std::unordered_map< std::string, std::size_t >::iterator
it_found = templateNameMap.find(NameOrig);
if ( it_found != templateNameMap.end() )
return it_found->second;
// FIXME: Convert this code to being constructive of "Name", instead of destructive (replacing sub-strings with '\0' characters).
std::string Name = NameOrig;
std::string::iterator it_open = Name.end();
std::string::iterator it_colon_lo = Name.begin();
int srch_state = 0;
llvm::SmallVector<std::size_t, 8> markers;
for(std::string::iterator it = Name.begin(); it != Name.end(); ++it) {
switch(srch_state) {
case 0:
if ( *it == '<' ) {
// check for "operator<<", "operator<" and "operator<="
llvm::StringRef test_str(Name.data(), it - Name.begin() + 1);
if ( test_str.endswith("operator<") ) {
it_open = Name.end();
srch_state = 0;
} else {
it_open = it;
++srch_state;
}
} else if ( (*it == ':') && (it + 1 < Name.end()) && (*(it+1) == ':') ) {
if ( it_colon_lo < it ) {
markers.push_back( createDictionaryEntry(std::string(it_colon_lo, it)) );
std::size_t offset_lo = it_colon_lo - Name.begin();
Name.replace(it_colon_lo, it, 1, '\0');
it = Name.begin() + offset_lo + 2;
} else {
it += 1;
}
it_colon_lo = it + 1;
it_open = Name.end();
}
break;
case 1:
if ( *it == '<' ) {
// check for "operator<<" and "operator<"
llvm::StringRef test_str(Name.data(), it - Name.begin() + 1);
if ( test_str.endswith("operator<<<") ) {
it_open = it;
srch_state = 1;
} else {
++srch_state;
}
} else if ( ( *it == ',' ) || ( *it == '>' ) ) {
if ( it_colon_lo < it_open ) {
std::size_t offset_end = it - it_open;
std::size_t offset_lo = it_colon_lo - Name.begin();
markers.push_back( createDictionaryEntry(std::string(it_colon_lo, it_open)) );
Name.replace(it_colon_lo, it_open, 1, '\0');
it_open = Name.begin() + offset_lo + 1;
it = it_open + offset_end;
it_colon_lo = Name.end();
}
std::string::iterator it_lo = it_open + 1;
std::string::iterator it_hi = it - 1;
trimSpaces(it_lo, it_hi);
// Create or find the marker entry:
markers.push_back( createDictionaryEntry(std::string(it_lo, it_hi)) );
std::size_t offset_end = it - it_hi;
std::size_t offset_lo = it_lo - Name.begin();
Name.replace(it_lo, it_hi, 1, '\0');
it = Name.begin() + offset_lo + 1 + offset_end;
it_open = it;
it_colon_lo = Name.end();
if ( *it == '>' ) {
it_open = Name.end();
srch_state = 0;
it_colon_lo = it + 1;
}
}
break;
default:
if ( *it == '<' ) {
++srch_state;
} else if ( *it == '>' ) {
--srch_state;
}
break;
}
}
if ( !markers.empty() && it_colon_lo != Name.end() ) {
markers.push_back( createDictionaryEntry(std::string(it_colon_lo, Name.end())) );
Name.replace(it_colon_lo, Name.end(), 1, '\0');
}
/*
message DictionaryEntry {
required string marked_name = 1;
repeated uint32 marker_ids = 2;
}
*/
std::string dict_entry;
llvm::raw_string_ostream OS_dict(dict_entry);
llvm::protobuf::saveString(OS_dict, 1, Name); // marked_name
for(llvm::SmallVector<std::size_t, 8>::iterator mk = markers.begin(),
mk_end = markers.end(); mk != mk_end; ++mk) {
llvm::protobuf::saveVarInt(OS_dict, 2, *mk); // marker_ids
}
OS_dict.str();
std::size_t id = templateNameMap.size();
templateNameMap[NameOrig] = id;
llvm::raw_string_ostream OS_outer(buffer);
// repeated DictionaryEntry names = 3;
llvm::protobuf::saveString(OS_outer, 3, dict_entry);
return id;
}
std::string TemplightProtobufWriter::printTemplateName(const std::string& Name) {
/*
message TemplateName {
optional string name = 1;
optional bytes compressed_name = 2;
}
*/
std::string tname_contents;
llvm::raw_string_ostream OS_inner(tname_contents);
switch( compressionMode ) {
case 1: { // zlib-compressed name:
llvm::SmallVector<char, 32> CompressedBuffer;
if ( auto E = llvm::zlib::compress(llvm::StringRef(Name), CompressedBuffer) ) {
handleAllErrors(std::move(E), [](const llvm::StringError&) {});
// fall through to case 0
} else {
// optional bytes compressed_name = 2;
llvm::protobuf::saveString(OS_inner, 2,
llvm::StringRef(CompressedBuffer.begin(), CompressedBuffer.size()));
break;
}
}
case 0:
// optional string name = 1;
llvm::protobuf::saveString(OS_inner, 1, Name); // name
break;
case 2:
default:
// optional uint32 dict_id = 3;
llvm::protobuf::saveVarInt(OS_inner, 3,
createDictionaryEntry(Name));
break;
}
OS_inner.str();
return tname_contents; // NRVO
}
void TemplightProtobufWriter::printEntry(const PrintableTemplightEntryBegin& aEntry) {
std::string entry_contents;
{
llvm::raw_string_ostream OS_inner(entry_contents);
/*
message Begin {
required InstantiationKind kind = 1;
required string name = 2;
required SourceLocation location = 3;
optional double time_stamp = 4;
optional uint64 memory_usage = 5;
optional SourceLocation template_origin = 6;
}
*/
llvm::protobuf::saveVarInt(OS_inner, 1, aEntry.InstantiationKind); // kind
llvm::protobuf::saveString(OS_inner, 2,
printTemplateName(aEntry.Name)); // name
llvm::protobuf::saveString(OS_inner, 3,
printEntryLocation(aEntry.FileName,
aEntry.Line,
aEntry.Column)); // location
llvm::protobuf::saveDouble(OS_inner, 4, aEntry.TimeStamp); // time_stamp
if ( aEntry.MemoryUsage > 0 )
llvm::protobuf::saveVarInt(OS_inner, 5, aEntry.MemoryUsage); // memory_usage
if ( !aEntry.TempOri_FileName.empty() )
llvm::protobuf::saveString(OS_inner, 6,
printEntryLocation(aEntry.TempOri_FileName,
aEntry.TempOri_Line,
aEntry.TempOri_Column)); // template_origin
}
std::string oneof_contents;
{
llvm::raw_string_ostream OS_inner(oneof_contents);
/*
oneof begin_or_end {
Begin begin = 1;
End end = 2;
}
*/
llvm::protobuf::saveString(OS_inner, 1, entry_contents); // begin
}
llvm::raw_string_ostream OS(buffer);
// repeated TemplightEntry entries = 2;
llvm::protobuf::saveString(OS, 2, oneof_contents);
}
void TemplightProtobufWriter::printEntry(const PrintableTemplightEntryEnd& aEntry) {
std::string entry_contents;
{
llvm::raw_string_ostream OS_inner(entry_contents);
/*
message End {
optional double time_stamp = 1;
optional uint64 memory_usage = 2;
}
*/
llvm::protobuf::saveDouble(OS_inner, 1, aEntry.TimeStamp); // time_stamp
if ( aEntry.MemoryUsage > 0 )
llvm::protobuf::saveVarInt(OS_inner, 2, aEntry.MemoryUsage); // memory_usage
}
std::string oneof_contents;
{
llvm::raw_string_ostream OS_inner(oneof_contents);
/*
oneof begin_or_end {
Begin begin = 1;
End end = 2;
}
*/
llvm::protobuf::saveString(OS_inner, 2, entry_contents); // end
}
llvm::raw_string_ostream OS(buffer);
// repeated TemplightEntry entries = 2;
llvm::protobuf::saveString(OS, 2, oneof_contents);
}
} // namespace clang