forked from mikael-s-persson/templight
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TemplightEntryPrinter.cpp
172 lines (141 loc) · 4.58 KB
/
TemplightEntryPrinter.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
//===- TemplightEntryPrinter.cpp ------ Clang Templight Entry Printer -*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "TemplightEntryPrinter.h"
#include <llvm/Support/FileSystem.h>
#include <llvm/Support/MemoryBuffer.h>
#include <llvm/Support/raw_ostream.h>
#include <llvm/Support/Regex.h>
namespace clang {
void TemplightEntryPrinter::skipEntry() {
if ( SkippedEndingsCount ) {
++SkippedEndingsCount; // note: this is needed because skipped entries are begin entries for which "shouldIgnoreEntry" is never called.
return; // Already skipping entries.
}
SkippedEndingsCount = 1;
}
bool TemplightEntryPrinter::shouldIgnoreEntry(const PrintableTemplightEntryBegin &Entry) {
// Check the black-lists:
// (1) Is currently ignoring entries?
if ( SkippedEndingsCount ) {
++SkippedEndingsCount;
return true;
}
// (2) Regexes:
if ( ( CoRegex && ( CoRegex->match(Entry.Name) ) ) ||
( IdRegex && ( IdRegex->match(Entry.Name) ) ) ) {
skipEntry();
return true;
}
return false;
}
bool TemplightEntryPrinter::shouldIgnoreEntry(const PrintableTemplightEntryEnd &Entry) {
// Check the black-lists:
// (1) Is currently ignoring entries?
if ( SkippedEndingsCount ) {
--SkippedEndingsCount;
return true;
}
return false;
}
void TemplightEntryPrinter::printEntry(const PrintableTemplightEntryBegin &Entry) {
if ( shouldIgnoreEntry(Entry) )
return;
if ( p_writer )
p_writer->printEntry(Entry);
}
void TemplightEntryPrinter::printEntry(const PrintableTemplightEntryEnd &Entry) {
if ( shouldIgnoreEntry(Entry) )
return;
if ( p_writer )
p_writer->printEntry(Entry);
}
void TemplightEntryPrinter::initialize(const std::string& SourceName) {
if ( p_writer )
p_writer->initialize(SourceName);
}
void TemplightEntryPrinter::finalize() {
if ( p_writer )
p_writer->finalize();
}
TemplightEntryPrinter::TemplightEntryPrinter(const std::string &Output) : SkippedEndingsCount(0), TraceOS(0) {
if ( Output == "-" ) {
TraceOS = &llvm::outs();
} else {
std::error_code error;
TraceOS = new llvm::raw_fd_ostream(Output, error, llvm::sys::fs::F_None);
if ( error ) {
llvm::errs() <<
"Error: [Templight] Can not open file to write trace of template instantiations: "
<< Output << " Error: " << error.message();
TraceOS = 0;
return;
}
}
}
TemplightEntryPrinter::~TemplightEntryPrinter() {
p_writer.reset(); // Delete writer before the trace-OS.
if ( TraceOS ) {
TraceOS->flush();
if ( TraceOS != &llvm::outs() )
delete TraceOS;
}
}
bool TemplightEntryPrinter::isValid() const { return p_writer.get(); }
llvm::raw_ostream* TemplightEntryPrinter::getTraceStream() const { return TraceOS; }
void TemplightEntryPrinter::takeWriter(TemplightWriter* aPWriter) {
p_writer.reset(aPWriter);
}
void TemplightEntryPrinter::readBlacklists(const std::string& BLFilename) {
if ( BLFilename.empty() ) {
CoRegex.reset();
IdRegex.reset();
return;
}
std::string CoPattern, IdPattern;
llvm::ErrorOr< std::unique_ptr<llvm::MemoryBuffer> >
file_epbuf = llvm::MemoryBuffer::getFile(llvm::Twine(BLFilename));
if(!file_epbuf || (!file_epbuf.get())) {
llvm::errs() << "Error: [Templight-Action] Could not open the blacklist file!\n";
CoRegex.reset();
IdRegex.reset();
return;
}
llvm::Regex findCo("^context ");
llvm::Regex findId("^identifier ");
const char* it = file_epbuf.get()->getBufferStart();
const char* it_mark = file_epbuf.get()->getBufferStart();
const char* it_end = file_epbuf.get()->getBufferEnd();
while( it_mark != it_end ) {
it_mark = std::find(it, it_end, '\n');
if(*(it_mark-1) == '\r')
--it_mark;
llvm::StringRef curLine(&(*it), it_mark - it);
if( findCo.match(curLine) ) {
if(!CoPattern.empty())
CoPattern += '|';
CoPattern += '(';
CoPattern.append(&(*(it+8)), it_mark - it - 8);
CoPattern += ')';
} else if( findId.match(curLine) ) {
if(!IdPattern.empty())
IdPattern += '|';
IdPattern += '(';
IdPattern.append(&(*(it+11)), it_mark - it - 11);
IdPattern += ')';
}
while( (it_mark != it_end) &&
((*it_mark == '\n') || (*it_mark == '\r')) )
++it_mark;
it = it_mark;
}
CoRegex.reset(new llvm::Regex(CoPattern));
IdRegex.reset(new llvm::Regex(IdPattern));
return;
}
}