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
|
//===--- CacheTokens.cpp - Caching of lexer tokens for PCH support --------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This provides a possible implementation of PCH support for Clang that is
// based on caching lexed tokens and identifiers.
//
//===----------------------------------------------------------------------===//
#include "clang.h"
#include "clang/Basic/FileManager.h"
#include "clang/Basic/SourceManager.h"
#include "clang/Basic/IdentifierTable.h"
#include "clang/Basic/Diagnostic.h"
#include "clang/Lex/Lexer.h"
#include "clang/Lex/Preprocessor.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/System/Path.h"
using namespace clang;
typedef uint32_t Offset;
typedef llvm::DenseMap<const FileEntry*,std::pair<Offset,Offset> > PCHMap;
typedef llvm::DenseMap<const IdentifierInfo*,uint32_t> IDMap;
static void Emit32(llvm::raw_ostream& Out, uint32_t V) {
Out << (unsigned char)(V);
Out << (unsigned char)(V >> 8);
Out << (unsigned char)(V >> 16);
Out << (unsigned char)(V >> 24);
}
static void Emit8(llvm::raw_ostream& Out, uint32_t V) {
Out << (unsigned char)(V);
}
static void EmitBuf(llvm::raw_ostream& Out, const char* I, const char* E) {
for ( ; I != E ; ++I) Out << *I;
}
static uint32_t ResolveID(IDMap& IM, uint32_t& idx, const IdentifierInfo* II) {
// Null IdentifierInfo's map to the persistent ID 0.
if (!II)
return 0;
IDMap::iterator I = IM.find(II);
if (I == IM.end()) {
IM[II] = ++idx; // Pre-increment since '0' is reserved for NULL.
return idx;
}
return I->second; // We've already added 1.
}
static void EmitToken(llvm::raw_ostream& Out, const Token& T,
const SourceManager& SMgr,
uint32_t& idcount, IDMap& IM) {
Emit8(Out, T.getKind());
Emit8(Out, T.getFlags());
Emit32(Out, ResolveID(IM, idcount, T.getIdentifierInfo()));
Emit32(Out, SMgr.getFullFilePos(T.getLocation()));
Emit32(Out, T.getLength());
}
struct IDData {
const IdentifierInfo* II;
uint32_t FileOffset;
const IdentifierTable::const_iterator::value_type* Str;
};
static std::pair<Offset,Offset>
EmitIdentifierTable(llvm::raw_fd_ostream& Out, uint32_t max,
const IdentifierTable& T, const IDMap& IM) {
// Build an inverse map from persistent IDs -> IdentifierInfo*.
typedef std::vector<IDData> InverseIDMap;
InverseIDMap IIDMap;
IIDMap.resize(max);
// Generate mapping from persistent IDs -> IdentifierInfo*.
for (IDMap::const_iterator I=IM.begin(), E=IM.end(); I!=E; ++I) {
// Decrement by 1 because we are using a vector for the lookup and
// 0 is reserved for NULL.
assert(I->second > 0);
assert(I->second-1 < IIDMap.size());
IIDMap[I->second-1].II = I->first;
}
// Get the string data associated with the IdentifierInfo.
for (IdentifierTable::const_iterator I=T.begin(), E=T.end(); I!=E; ++I) {
IDMap::const_iterator IDI = IM.find(&(I->getValue()));
if (IDI == IM.end()) continue;
IIDMap[IDI->second-1].Str = &(*I);
}
Offset DataOff = Out.tell();
for (InverseIDMap::iterator I=IIDMap.begin(), E=IIDMap.end(); I!=E; ++I) {
// Record the location for this data.
I->FileOffset = Out.tell();
// Write out the keyword.
unsigned len = I->Str->getKeyLength();
Emit32(Out, len);
const char* buf = I->Str->getKeyData();
EmitBuf(Out, buf, buf+len);
}
// Now emit the table mapping from persistent IDs to PTH file offsets.
Offset IDOff = Out.tell();
// Emit the number of identifiers.
Emit32(Out, max);
for (InverseIDMap::iterator I=IIDMap.begin(), E=IIDMap.end(); I!=E; ++I)
Emit32(Out, I->FileOffset);
return std::make_pair(DataOff, IDOff);
}
Offset EmitFileTable(llvm::raw_fd_ostream& Out, SourceManager& SM, PCHMap& PM) {
Offset off = (Offset) Out.tell();
// Output the size of the table.
Emit32(Out, PM.size());
for (PCHMap::iterator I=PM.begin(), E=PM.end(); I!=E; ++I) {
const FileEntry* FE = I->first;
const char* Name = FE->getName();
unsigned size = strlen(Name);
Emit32(Out, size);
EmitBuf(Out, Name, Name+size);
Emit32(Out, I->second.first);
Emit32(Out, I->second.second);
}
return off;
}
static std::pair<Offset,Offset>
LexTokens(llvm::raw_fd_ostream& Out, Lexer& L, Preprocessor& PP,
uint32_t& idcount, IDMap& IM) {
// Record the location within the token file.
Offset off = (Offset) Out.tell();
SourceManager& SMgr = PP.getSourceManager();
// Keep track of matching '#if' ... '#endif'.
typedef std::vector<std::pair<Offset, unsigned> > PPCondTable;
PPCondTable PPCond;
std::vector<unsigned> PPStartCond;
bool ParsingPreprocessorDirective = false;
Token Tok;
do {
L.LexFromRawLexer(Tok);
if ((Tok.isAtStartOfLine() || Tok.is(tok::eof)) &&
ParsingPreprocessorDirective) {
// Insert an eom token into the token cache. It has the same
// position as the next token that is not on the same line as the
// preprocessor directive. Observe that we continue processing
// 'Tok' when we exit this branch.
Token Tmp = Tok;
Tmp.setKind(tok::eom);
Tmp.clearFlag(Token::StartOfLine);
Tmp.setIdentifierInfo(0);
EmitToken(Out, Tmp, SMgr, idcount, IM);
ParsingPreprocessorDirective = false;
}
if (Tok.is(tok::identifier)) {
Tok.setIdentifierInfo(PP.LookUpIdentifierInfo(Tok));
continue;
}
if (Tok.is(tok::hash) && Tok.isAtStartOfLine()) {
// Special processing for #include. Store the '#' token and lex
// the next token.
assert(!ParsingPreprocessorDirective);
Offset HashOff = (Offset) Out.tell();
EmitToken(Out, Tok, SMgr, idcount, IM);
// Get the next token.
L.LexFromRawLexer(Tok);
assert(!Tok.isAtStartOfLine());
// Did we see 'include'/'import'/'include_next'?
if (!Tok.is(tok::identifier))
continue;
IdentifierInfo* II = PP.LookUpIdentifierInfo(Tok);
Tok.setIdentifierInfo(II);
tok::PPKeywordKind K = II->getPPKeywordID();
assert(K != tok::pp_not_keyword);
ParsingPreprocessorDirective = true;
switch (K) {
default:
break;
case tok::pp_include:
case tok::pp_import:
case tok::pp_include_next: {
// Save the 'include' token.
EmitToken(Out, Tok, SMgr, idcount, IM);
// Lex the next token as an include string.
L.setParsingPreprocessorDirective(true);
L.LexIncludeFilename(Tok);
L.setParsingPreprocessorDirective(false);
assert(!Tok.isAtStartOfLine());
if
|