forked from mikael-s-persson/templight
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TemplightTracer.cpp
368 lines (300 loc) · 12.5 KB
/
TemplightTracer.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
362
363
364
365
366
367
//===- TemplightTracer.cpp ------ Clang Templight Profiler / Tracer -*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "TemplightTracer.h"
#include "TemplightProtobufWriter.h"
#include "PrintableTemplightEntries.h"
#include "TemplightEntryPrinter.h"
#include <clang/Basic/FileManager.h>
#include <clang/Basic/SourceManager.h>
#include <clang/Sema/ActiveTemplateInst.h>
#include <clang/Sema/Sema.h>
#include <llvm/Support/raw_ostream.h>
#include <llvm/Support/Format.h>
#include <llvm/Support/Timer.h>
#include <llvm/Support/Process.h>
#include <llvm/Support/YAMLTraits.h>
#include <string>
#include <vector>
#include <memory>
#include <cstdint>
namespace clang {
namespace {
struct RawTemplightTraceEntry {
bool IsTemplateBegin;
std::size_t ParentBeginIdx;
ActiveTemplateInstantiation::InstantiationKind InstantiationKind;
Decl *Entity;
SourceLocation PointOfInstantiation;
double TimeStamp;
std::uint64_t MemoryUsage;
static const std::size_t invalid_parent = ~std::size_t(0);
RawTemplightTraceEntry() : IsTemplateBegin(true), ParentBeginIdx(invalid_parent),
InstantiationKind(ActiveTemplateInstantiation::TemplateInstantiation),
Entity(0), TimeStamp(0.0), MemoryUsage(0) { };
};
PrintableTemplightEntryBegin rawToPrintableBegin(const Sema &TheSema, const RawTemplightTraceEntry& Entry) {
PrintableTemplightEntryBegin Ret;
Ret.InstantiationKind = Entry.InstantiationKind;
NamedDecl *NamedTemplate = dyn_cast_or_null<NamedDecl>(Entry.Entity);
if (NamedTemplate) {
llvm::raw_string_ostream OS(Ret.Name);
NamedTemplate->getNameForDiagnostic(OS, TheSema.getLangOpts(), true);
if (!OS.tell()) {
Decl *Ctx = Decl::castFromDeclContext(NamedTemplate->getDeclContext());
NamedDecl *NamedCtx = dyn_cast_or_null<NamedDecl>(Ctx);
if (NamedCtx) {
if (isa<CXXRecordDecl>(NamedTemplate))
{
CXXRecordDecl *Decl = cast<CXXRecordDecl>(NamedTemplate);
if (Decl->isLambda()) {
OS << "lambda at ";
Decl->getLocation().print(OS, TheSema.getSourceManager());
}
}
else if (isa<ParmVarDecl>(NamedTemplate))
{
ParmVarDecl *Decl = cast<ParmVarDecl>(NamedTemplate);
OS << "function parameter " << Decl->getFunctionScopeIndex() << " ";
if (Decl->getFunctionScopeDepth() > 0) OS << "(at depth " << Decl->getFunctionScopeDepth() << ") ";
OS << "of ";
NamedCtx->getNameForDiagnostic(OS, TheSema.getLangOpts(), true);
}
else if (isa<TemplateTypeParmDecl>(NamedTemplate))
{
TemplateTypeParmDecl *Decl = cast<TemplateTypeParmDecl>(NamedTemplate);
const Type *Ty = Decl->getTypeForDecl();
if (Ty) {
const TemplateTypeParmType *TTPT = dyn_cast_or_null<TemplateTypeParmType>(Ty);
if (TTPT) {
OS << "template type parameter " << TTPT->getIndex() << " ";
if (TTPT->getDepth() > 0) OS << "(at depth " << TTPT->getDepth() << ") ";
OS << "of ";
NamedCtx->getNameForDiagnostic(OS, TheSema.getLangOpts(), true);
}
}
}
else if (isa<NonTypeTemplateParmDecl>(NamedTemplate))
{
NonTypeTemplateParmDecl *Decl = cast<NonTypeTemplateParmDecl>(NamedTemplate);
OS << "template non-type parameter " << Decl->getIndex() << " ";
if (Decl->getDepth() > 0) OS << "(at depth " << Decl->getDepth() << ") ";
OS << "of ";
NamedCtx->getNameForDiagnostic(OS, TheSema.getLangOpts(), true);
}
else if (isa<TemplateTemplateParmDecl>(NamedTemplate))
{
TemplateTemplateParmDecl *Decl = cast<TemplateTemplateParmDecl>(NamedTemplate);
OS << "template template parameter " << Decl->getIndex() << " ";
if (Decl->getDepth() > 0) OS << "(at depth " << Decl->getDepth() << ") ";
OS << "of ";
NamedCtx->getNameForDiagnostic(OS, TheSema.getLangOpts(), true);
}
}
}
}
if (Ret.Name.empty()) {
llvm::errs() << "Warning: [Templight] " << Entry.Entity->getDeclKindName() << " with no name at ";
llvm::errs() << Entry.Entity->getLocation().printToString(TheSema.getSourceManager()) << "\n";
}
PresumedLoc Loc = TheSema.getSourceManager().getPresumedLoc(Entry.PointOfInstantiation);
if (!Loc.isInvalid()) {
Ret.FileName = Loc.getFilename();
Ret.Line = Loc.getLine();
Ret.Column = Loc.getColumn();
} else {
Ret.FileName = "";
Ret.Line = 0;
Ret.Column = 0;
}
Ret.TimeStamp = Entry.TimeStamp;
Ret.MemoryUsage = Entry.MemoryUsage;
if (Entry.Entity) {
PresumedLoc Loc = TheSema.getSourceManager().getPresumedLoc(Entry.Entity->getLocation());
if (!Loc.isInvalid()) {
Ret.TempOri_FileName = Loc.getFilename();
Ret.TempOri_Line = Loc.getLine();
Ret.TempOri_Column = Loc.getColumn();
} else {
Ret.TempOri_FileName = "";
Ret.TempOri_Line = 0;
Ret.TempOri_Column = 0;
}
}
return Ret;
}
PrintableTemplightEntryEnd rawToPrintableEnd(const Sema &TheSema, const RawTemplightTraceEntry& Entry) {
return {Entry.TimeStamp, Entry.MemoryUsage};
}
} // unnamed namespace
class TemplightTracer::TracePrinter : public TemplightEntryPrinter {
public:
void skipRawEntry(const RawTemplightTraceEntry &Entry) {
skipEntry();
}
bool shouldIgnoreRawEntry(const RawTemplightTraceEntry &Entry) {
// Avoid some duplication of memoization entries:
if ( ( Entry.InstantiationKind == ActiveTemplateInstantiation::Memoization ) &&
LastClosedMemoization && ( LastClosedMemoization == Entry.Entity ) ) {
return true;
}
// if we have an end entry, we must ensure it corresponds to the current begin entry:
// these checks are a bit redundant and overly cautious, but better safe than sorry when sanitizing.
if ( (!Entry.IsTemplateBegin) &&
( ( TraceEntries.empty() ) ||
( CurrentParentBegin == RawTemplightTraceEntry::invalid_parent ) ||
( CurrentParentBegin >= TraceEntries.size() ) ||
!( ( TraceEntries[CurrentParentBegin].InstantiationKind == Entry.InstantiationKind ) &&
( TraceEntries[CurrentParentBegin].Entity == Entry.Entity ) ) ) ) {
return true; // ignore end entries that don't match the current begin entry.
}
return false;
};
void printOrSkipEntry(RawTemplightTraceEntry &Entry) {
if ( IgnoreSystemFlag && !Entry.PointOfInstantiation.isInvalid() &&
TheSema.getSourceManager()
.isInSystemHeader(Entry.PointOfInstantiation) ) {
skipRawEntry(Entry); // recursively skip all entries until end of this one.
} else {
if ( Entry.IsTemplateBegin ) {
printEntry(rawToPrintableBegin(TheSema, Entry));
} else {
printEntry(rawToPrintableEnd(TheSema, Entry));
}
}
};
void printCachedRawEntries() {
for(std::vector<RawTemplightTraceEntry>::iterator it = TraceEntries.begin();
it != TraceEntries.end(); ++it)
printOrSkipEntry(*it);
TraceEntries.clear();
CurrentParentBegin = RawTemplightTraceEntry::invalid_parent;
};
void printRawEntry(RawTemplightTraceEntry Entry, bool inSafeMode = false) {
if ( shouldIgnoreRawEntry(Entry) )
return;
if ( inSafeMode )
printOrSkipEntry(Entry);
// Always maintain a stack of cached trace entries such that the sanity of the traces can be enforced.
if ( Entry.IsTemplateBegin ) {
Entry.ParentBeginIdx = CurrentParentBegin;
CurrentParentBegin = TraceEntries.size();
} else { // note: this point should not be reached if CurrentParentBegin is not valid.
Entry.ParentBeginIdx = TraceEntries[CurrentParentBegin].ParentBeginIdx;
CurrentParentBegin = Entry.ParentBeginIdx;
};
TraceEntries.push_back(Entry);
if ( Entry.IsTemplateBegin )
LastClosedMemoization = nullptr;
if ( !Entry.IsTemplateBegin &&
( Entry.InstantiationKind == ActiveTemplateInstantiation::Memoization ) )
LastClosedMemoization = Entry.Entity;
if ( !Entry.IsTemplateBegin &&
( Entry.InstantiationKind == TraceEntries.front().InstantiationKind ) &&
( Entry.Entity == TraceEntries.front().Entity ) ) { // did we reach the end of the top-level begin entry?
if ( !inSafeMode ) { // if not in safe-mode, print out the cached entries.
printCachedRawEntries();
} else { // if in safe-mode, simply clear the cached entries.
TraceEntries.clear();
CurrentParentBegin = RawTemplightTraceEntry::invalid_parent;
}
}
};
void startTrace() {
// get the source name from the source manager:
FileID fileID = TheSema.getSourceManager().getMainFileID();
std::string src_name =
TheSema.getSourceManager().getFileEntryForID(fileID)->getName();
initialize(src_name);
};
void endTrace() {
printCachedRawEntries();
finalize();
};
TracePrinter(const Sema &aSema, const std::string &Output, bool IgnoreSystem = false) :
TemplightEntryPrinter(Output), TheSema(aSema),
LastClosedMemoization(nullptr),
CurrentParentBegin(RawTemplightTraceEntry::invalid_parent),
IgnoreSystemFlag(IgnoreSystem) { };
~TracePrinter() { };
const Sema &TheSema;
std::vector<RawTemplightTraceEntry> TraceEntries;
Decl* LastClosedMemoization;
std::size_t CurrentParentBegin;
unsigned IgnoreSystemFlag : 1;
};
void TemplightTracer::atTemplateBeginImpl(const Sema &TheSema,
const ActiveTemplateInstantiation& Inst) {
if ( !Printer )
return;
RawTemplightTraceEntry Entry;
Entry.IsTemplateBegin = true;
Entry.InstantiationKind = Inst.Kind;
Entry.Entity = Inst.Entity;
Entry.PointOfInstantiation = Inst.PointOfInstantiation;
// NOTE: Use this function because it produces time since start of process.
llvm::sys::TimePoint<> now;
std::chrono::nanoseconds user, sys;
llvm::sys::Process::GetTimeUsage(now, user, sys);
if(user != std::chrono::nanoseconds::zero())
now = llvm::sys::TimePoint<>(user);
using Seconds = std::chrono::duration<double, std::ratio<1>>;
Entry.TimeStamp = Seconds(now.time_since_epoch()).count();
Entry.MemoryUsage = (MemoryFlag ? llvm::sys::Process::GetMallocUsage() : 0);
Printer->printRawEntry(Entry, SafeModeFlag);
}
void TemplightTracer::atTemplateEndImpl(const Sema &TheSema,
const ActiveTemplateInstantiation& Inst) {
if ( !Printer )
return;
RawTemplightTraceEntry Entry;
Entry.IsTemplateBegin = false;
Entry.InstantiationKind = Inst.Kind;
Entry.Entity = Inst.Entity;
// NOTE: Use this function because it produces time since start of process.
llvm::sys::TimePoint<> now;
std::chrono::nanoseconds user, sys;
llvm::sys::Process::GetTimeUsage(now, user, sys);
if(user != std::chrono::nanoseconds::zero())
now = llvm::sys::TimePoint<>(user);
using Seconds = std::chrono::duration<double, std::ratio<1>>;
Entry.TimeStamp = Seconds(now.time_since_epoch()).count();
Entry.MemoryUsage = (MemoryFlag ? llvm::sys::Process::GetMallocUsage() : 0);
Printer->printRawEntry(Entry, SafeModeFlag);
}
TemplightTracer::TemplightTracer(const Sema &TheSema,
std::string Output,
bool Memory, bool Safemode,
bool IgnoreSystem) :
MemoryFlag(Memory),
SafeModeFlag(Safemode) {
Printer.reset(new TemplightTracer::TracePrinter(TheSema, Output, IgnoreSystem));
if ( !Printer->getTraceStream() ) {
llvm::errs() << "Error: [Templight-Tracer] Failed to create template trace file!";
Printer.reset();
llvm::errs() << "Note: [Templight] Template trace has been disabled.";
return;
}
Printer->takeWriter(new clang::TemplightProtobufWriter(*Printer->getTraceStream()));
}
TemplightTracer::~TemplightTracer() {
// must be defined here due to TracePrinter being incomplete in header.
}
void TemplightTracer::initializeImpl(const Sema &) {
if ( Printer )
Printer->startTrace();
}
void TemplightTracer::finalizeImpl(const Sema &) {
if ( Printer )
Printer->endTrace();
}
void TemplightTracer::readBlacklists(const std::string& BLFilename) {
if ( Printer )
Printer->readBlacklists(BLFilename);
}
} // namespace clang