blob: 6e693bbfe6bf8fc91be8e8d29252cd77cd8e1c31 [file] [log] [blame]
Ted Kremeneka4b44dd2009-02-13 19:13:46 +00001//===--- CacheTokens.cpp - Caching of lexer tokens for PTH support --------===//
Ted Kremenek85888962008-10-21 00:54:44 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Ted Kremeneka4b44dd2009-02-13 19:13:46 +000010// This provides a possible implementation of PTH support for Clang that is
Ted Kremenek85888962008-10-21 00:54:44 +000011// based on caching lexed tokens and identifiers.
12//
13//===----------------------------------------------------------------------===//
14
15#include "clang.h"
16#include "clang/Basic/FileManager.h"
17#include "clang/Basic/SourceManager.h"
18#include "clang/Basic/IdentifierTable.h"
19#include "clang/Basic/Diagnostic.h"
20#include "clang/Lex/Lexer.h"
21#include "clang/Lex/Preprocessor.h"
Ted Kremenekbe295332009-01-08 02:44:06 +000022#include "llvm/ADT/StringMap.h"
Ted Kremenek85888962008-10-21 00:54:44 +000023#include "llvm/Support/MemoryBuffer.h"
24#include "llvm/Support/raw_ostream.h"
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +000025#include "llvm/System/Path.h"
Ted Kremenekb978c662009-01-08 01:17:37 +000026#include "llvm/Support/Compiler.h"
Ted Kremenek72b1b152009-01-15 18:47:46 +000027#include "llvm/Support/Streams.h"
Ted Kremenek85888962008-10-21 00:54:44 +000028
Cedric Venetea684e62009-02-14 16:15:20 +000029// FIXME: put this somewhere else?
30#ifndef S_ISDIR
31#define S_ISDIR(x) (((x)&_S_IFDIR)!=0)
32#endif
33
Ted Kremenek85888962008-10-21 00:54:44 +000034using namespace clang;
35
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +000036typedef uint32_t Offset;
37
Ted Kremeneke0ea5dc2009-02-10 01:06:17 +000038static void Emit8(llvm::raw_ostream& Out, uint32_t V) {
39 Out << (unsigned char)(V);
40}
41
42static void Emit16(llvm::raw_ostream& Out, uint32_t V) {
43 Out << (unsigned char)(V);
44 Out << (unsigned char)(V >> 8);
45 assert((V >> 16) == 0);
46}
47
48static void Emit32(llvm::raw_ostream& Out, uint32_t V) {
49 Out << (unsigned char)(V);
50 Out << (unsigned char)(V >> 8);
51 Out << (unsigned char)(V >> 16);
52 Out << (unsigned char)(V >> 24);
53}
54
Ted Kremenek337edcd2009-02-12 03:26:59 +000055static void Emit64(llvm::raw_ostream& Out, uint64_t V) {
56 Out << (unsigned char)(V);
57 Out << (unsigned char)(V >> 8);
58 Out << (unsigned char)(V >> 16);
59 Out << (unsigned char)(V >> 24);
60 Out << (unsigned char)(V >> 32);
61 Out << (unsigned char)(V >> 40);
62 Out << (unsigned char)(V >> 48);
63 Out << (unsigned char)(V >> 56);
64}
65
Ted Kremenekd8c02922009-02-10 22:16:22 +000066static void Pad(llvm::raw_fd_ostream& Out, unsigned A) {
67 Offset off = (Offset) Out.tell();
68 uint32_t n = ((uintptr_t)(off+A-1) & ~(uintptr_t)(A-1)) - off;
Chris Lattnerf2390362009-03-28 00:16:20 +000069 for (; n ; --n)
70 Emit8(Out, 0);
Ted Kremeneke0ea5dc2009-02-10 01:06:17 +000071}
72
Ted Kremenek7e3a0042009-02-11 21:29:16 +000073// Bernstein hash function:
74// This is basically copy-and-paste from StringMap. This likely won't
75// stay here, which is why I didn't both to expose this function from
76// String Map.
77static unsigned BernsteinHash(const char* x) {
78 unsigned int R = 0;
79 for ( ; *x != '\0' ; ++x) R = R * 33 + *x;
80 return R + (R >> 5);
81}
82
Ted Kremenekf0e1f792009-02-10 01:14:45 +000083//===----------------------------------------------------------------------===//
84// On Disk Hashtable Logic. This will eventually get refactored and put
85// elsewhere.
86//===----------------------------------------------------------------------===//
87
88template<typename Info>
89class OnDiskChainedHashTableGenerator {
90 unsigned NumBuckets;
91 unsigned NumEntries;
92 llvm::BumpPtrAllocator BA;
93
94 class Item {
95 public:
Ted Kremenekd8c02922009-02-10 22:16:22 +000096 typename Info::key_type key;
97 typename Info::data_type data;
Ted Kremenekf0e1f792009-02-10 01:14:45 +000098 Item *next;
99 const uint32_t hash;
100
Ted Kremenekd8c02922009-02-10 22:16:22 +0000101 Item(typename Info::key_type_ref k, typename Info::data_type_ref d)
102 : key(k), data(d), next(0), hash(Info::ComputeHash(k)) {}
Ted Kremenekf0e1f792009-02-10 01:14:45 +0000103 };
104
105 class Bucket {
106 public:
107 Offset off;
108 Item* head;
109 unsigned length;
110
111 Bucket() {}
112 };
113
114 Bucket* Buckets;
115
116private:
Ted Kremenekd8c02922009-02-10 22:16:22 +0000117 void insert(Bucket* b, size_t size, Item* E) {
Ted Kremenekf0e1f792009-02-10 01:14:45 +0000118 unsigned idx = E->hash & (size - 1);
119 Bucket& B = b[idx];
120 E->next = B.head;
121 ++B.length;
122 B.head = E;
123 }
124
125 void resize(size_t newsize) {
Ted Kremenekd8c02922009-02-10 22:16:22 +0000126 Bucket* newBuckets = (Bucket*) calloc(newsize, sizeof(Bucket));
127 // Populate newBuckets with the old entries.
Ted Kremenekf0e1f792009-02-10 01:14:45 +0000128 for (unsigned i = 0; i < NumBuckets; ++i)
Ted Kremenekd8c02922009-02-10 22:16:22 +0000129 for (Item* E = Buckets[i].head; E ; ) {
Ted Kremenekf0e1f792009-02-10 01:14:45 +0000130 Item* N = E->next;
Ted Kremenekd8c02922009-02-10 22:16:22 +0000131 E->next = 0;
Ted Kremenekf0e1f792009-02-10 01:14:45 +0000132 insert(newBuckets, newsize, E);
133 E = N;
134 }
135
136 free(Buckets);
137 NumBuckets = newsize;
138 Buckets = newBuckets;
139 }
140
141public:
142
Ted Kremenekd8c02922009-02-10 22:16:22 +0000143 void insert(typename Info::key_type_ref key,
144 typename Info::data_type_ref data) {
145
Ted Kremenekf0e1f792009-02-10 01:14:45 +0000146 ++NumEntries;
147 if (4*NumEntries >= 3*NumBuckets) resize(NumBuckets*2);
148 insert(Buckets, NumBuckets, new (BA.Allocate<Item>()) Item(key, data));
149 }
150
151 Offset Emit(llvm::raw_fd_ostream& out) {
152 // Emit the payload of the table.
153 for (unsigned i = 0; i < NumBuckets; ++i) {
154 Bucket& B = Buckets[i];
155 if (!B.head) continue;
156
157 // Store the offset for the data of this bucket.
Ted Kremenekf0e1f792009-02-10 01:14:45 +0000158 B.off = out.tell();
159
Ted Kremenekd8c02922009-02-10 22:16:22 +0000160 // Write out the number of items in the bucket.
161 Emit16(out, B.length);
Ted Kremenekf0e1f792009-02-10 01:14:45 +0000162
163 // Write out the entries in the bucket.
164 for (Item *I = B.head; I ; I = I->next) {
165 Emit32(out, I->hash);
Ted Kremenekd8c02922009-02-10 22:16:22 +0000166 const std::pair<unsigned, unsigned>& Len =
167 Info::EmitKeyDataLength(out, I->key, I->data);
168 Info::EmitKey(out, I->key, Len.first);
Ted Kremenek337edcd2009-02-12 03:26:59 +0000169 Info::EmitData(out, I->key, I->data, Len.second);
Ted Kremenekf0e1f792009-02-10 01:14:45 +0000170 }
171 }
172
173 // Emit the hashtable itself.
174 Pad(out, 4);
175 Offset TableOff = out.tell();
Ted Kremenekd8c02922009-02-10 22:16:22 +0000176 Emit32(out, NumBuckets);
177 Emit32(out, NumEntries);
Ted Kremenekf0e1f792009-02-10 01:14:45 +0000178 for (unsigned i = 0; i < NumBuckets; ++i) Emit32(out, Buckets[i].off);
179
180 return TableOff;
181 }
182
183 OnDiskChainedHashTableGenerator() {
184 NumEntries = 0;
Ted Kremenekd8c02922009-02-10 22:16:22 +0000185 NumBuckets = 64;
186 // Note that we do not need to run the constructors of the individual
187 // Bucket objects since 'calloc' returns bytes that are all 0.
188 Buckets = (Bucket*) calloc(NumBuckets, sizeof(Bucket));
Ted Kremenekf0e1f792009-02-10 01:14:45 +0000189 }
190
191 ~OnDiskChainedHashTableGenerator() {
192 free(Buckets);
193 }
194};
195
196//===----------------------------------------------------------------------===//
197// PTH-specific stuff.
198//===----------------------------------------------------------------------===//
199
Ted Kremenekbe295332009-01-08 02:44:06 +0000200namespace {
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000201class VISIBILITY_HIDDEN PTHEntry {
Ted Kremenekbe295332009-01-08 02:44:06 +0000202 Offset TokenData, PPCondData;
Ted Kremenekbe295332009-01-08 02:44:06 +0000203
204public:
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000205 PTHEntry() {}
Ted Kremenekbe295332009-01-08 02:44:06 +0000206
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000207 PTHEntry(Offset td, Offset ppcd)
Ted Kremenek277faca2009-01-27 00:01:05 +0000208 : TokenData(td), PPCondData(ppcd) {}
Ted Kremenekbe295332009-01-08 02:44:06 +0000209
Ted Kremenek277faca2009-01-27 00:01:05 +0000210 Offset getTokenOffset() const { return TokenData; }
Ted Kremenekbe295332009-01-08 02:44:06 +0000211 Offset getPPCondTableOffset() const { return PPCondData; }
Ted Kremenek277faca2009-01-27 00:01:05 +0000212};
Ted Kremenekbe295332009-01-08 02:44:06 +0000213
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000214
215class VISIBILITY_HIDDEN PTHEntryKeyVariant {
Ted Kremenekad6ce5c2009-02-13 22:07:44 +0000216 union { const FileEntry* FE; const char* Path; };
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000217 enum { IsFE = 0x1, IsDE = 0x2, IsNoExist = 0x0 } Kind;
Ted Kremenekad6ce5c2009-02-13 22:07:44 +0000218 struct stat *StatBuf;
Ted Kremenekd8c02922009-02-10 22:16:22 +0000219public:
Ted Kremenekad6ce5c2009-02-13 22:07:44 +0000220 PTHEntryKeyVariant(const FileEntry *fe)
221 : FE(fe), Kind(IsFE), StatBuf(0) {}
222
223 PTHEntryKeyVariant(struct stat* statbuf, const char* path)
224 : Path(path), Kind(IsDE), StatBuf(new struct stat(*statbuf)) {}
225
226 PTHEntryKeyVariant(const char* path)
227 : Path(path), Kind(IsNoExist), StatBuf(0) {}
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000228
Ted Kremenekad6ce5c2009-02-13 22:07:44 +0000229 bool isFile() const { return Kind == IsFE; }
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000230
231 const char* getCString() const {
Ted Kremenekad6ce5c2009-02-13 22:07:44 +0000232 return Kind == IsFE ? FE->getName() : Path;
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000233 }
234
235 unsigned getKind() const { return (unsigned) Kind; }
236
237 void EmitData(llvm::raw_ostream& Out) {
238 switch (Kind) {
239 case IsFE:
240 // Emit stat information.
241 ::Emit32(Out, FE->getInode());
242 ::Emit32(Out, FE->getDevice());
243 ::Emit16(Out, FE->getFileMode());
244 ::Emit64(Out, FE->getModificationTime());
245 ::Emit64(Out, FE->getSize());
246 break;
247 case IsDE:
Ted Kremenekad6ce5c2009-02-13 22:07:44 +0000248 // Emit stat information.
249 ::Emit32(Out, (uint32_t) StatBuf->st_ino);
250 ::Emit32(Out, (uint32_t) StatBuf->st_dev);
251 ::Emit16(Out, (uint16_t) StatBuf->st_mode);
252 ::Emit64(Out, (uint64_t) StatBuf->st_mtime);
253 ::Emit64(Out, (uint64_t) StatBuf->st_size);
254 delete StatBuf;
255 break;
256 default:
257 break;
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000258 }
259 }
260
261 unsigned getRepresentationLength() const {
Ted Kremenekad6ce5c2009-02-13 22:07:44 +0000262 return Kind == IsNoExist ? 0 : 4 + 4 + 2 + 8 + 8;
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000263 }
264};
265
266class VISIBILITY_HIDDEN FileEntryPTHEntryInfo {
267public:
268 typedef PTHEntryKeyVariant key_type;
Ted Kremenekd8c02922009-02-10 22:16:22 +0000269 typedef key_type key_type_ref;
270
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000271 typedef PTHEntry data_type;
272 typedef const PTHEntry& data_type_ref;
Ted Kremenekd8c02922009-02-10 22:16:22 +0000273
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000274 static unsigned ComputeHash(PTHEntryKeyVariant V) {
275 return BernsteinHash(V.getCString());
Ted Kremenekd8c02922009-02-10 22:16:22 +0000276 }
277
278 static std::pair<unsigned,unsigned>
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000279 EmitKeyDataLength(llvm::raw_ostream& Out, PTHEntryKeyVariant V,
280 const PTHEntry& E) {
Ted Kremenekd8c02922009-02-10 22:16:22 +0000281
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000282 unsigned n = strlen(V.getCString()) + 1 + 1;
Ted Kremenekd8c02922009-02-10 22:16:22 +0000283 ::Emit16(Out, n);
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000284
Ted Kremenekad6ce5c2009-02-13 22:07:44 +0000285 unsigned m = V.getRepresentationLength() + (V.isFile() ? 4 + 4 : 0);
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000286 ::Emit8(Out, m);
287
288 return std::make_pair(n, m);
Ted Kremenekd8c02922009-02-10 22:16:22 +0000289 }
290
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000291 static void EmitKey(llvm::raw_ostream& Out, PTHEntryKeyVariant V, unsigned n){
292 // Emit the entry kind.
293 ::Emit8(Out, (unsigned) V.getKind());
294 // Emit the string.
295 Out.write(V.getCString(), n - 1);
Ted Kremenekd8c02922009-02-10 22:16:22 +0000296 }
297
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000298 static void EmitData(llvm::raw_ostream& Out, PTHEntryKeyVariant V,
299 const PTHEntry& E, unsigned) {
300
301
302 // For file entries emit the offsets into the PTH file for token data
303 // and the preprocessor blocks table.
Ted Kremenekad6ce5c2009-02-13 22:07:44 +0000304 if (V.isFile()) {
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000305 ::Emit32(Out, E.getTokenOffset());
306 ::Emit32(Out, E.getPPCondTableOffset());
307 }
308
309 // Emit any other data associated with the key (i.e., stat information).
310 V.EmitData(Out);
Ted Kremenekd8c02922009-02-10 22:16:22 +0000311 }
312};
313
Ted Kremenek277faca2009-01-27 00:01:05 +0000314class OffsetOpt {
315 bool valid;
316 Offset off;
317public:
318 OffsetOpt() : valid(false) {}
319 bool hasOffset() const { return valid; }
320 Offset getOffset() const { assert(valid); return off; }
321 void setOffset(Offset o) { off = o; valid = true; }
Ted Kremenekbe295332009-01-08 02:44:06 +0000322};
323} // end anonymous namespace
324
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000325typedef OnDiskChainedHashTableGenerator<FileEntryPTHEntryInfo> PTHMap;
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000326typedef llvm::DenseMap<const IdentifierInfo*,uint32_t> IDMap;
Ted Kremenek277faca2009-01-27 00:01:05 +0000327typedef llvm::StringMap<OffsetOpt, llvm::BumpPtrAllocator> CachedStrsTy;
Ted Kremenek85888962008-10-21 00:54:44 +0000328
Ted Kremenekb978c662009-01-08 01:17:37 +0000329namespace {
330class VISIBILITY_HIDDEN PTHWriter {
331 IDMap IM;
332 llvm::raw_fd_ostream& Out;
333 Preprocessor& PP;
334 uint32_t idcount;
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000335 PTHMap PM;
Ted Kremenekbe295332009-01-08 02:44:06 +0000336 CachedStrsTy CachedStrs;
Ted Kremenek277faca2009-01-27 00:01:05 +0000337 Offset CurStrOffset;
338 std::vector<llvm::StringMapEntry<OffsetOpt>*> StrEntries;
Ted Kremenek8f174e12008-12-23 02:52:12 +0000339
Ted Kremenekb978c662009-01-08 01:17:37 +0000340 //// Get the persistent id for the given IdentifierInfo*.
341 uint32_t ResolveID(const IdentifierInfo* II);
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000342
Ted Kremenekb978c662009-01-08 01:17:37 +0000343 /// Emit a token to the PTH file.
344 void EmitToken(const Token& T);
345
346 void Emit8(uint32_t V) {
347 Out << (unsigned char)(V);
348 }
349
Ted Kremeneke0ea5dc2009-02-10 01:06:17 +0000350 void Emit16(uint32_t V) { ::Emit16(Out, V); }
Ted Kremenekb978c662009-01-08 01:17:37 +0000351
352 void Emit24(uint32_t V) {
353 Out << (unsigned char)(V);
354 Out << (unsigned char)(V >> 8);
355 Out << (unsigned char)(V >> 16);
356 assert((V >> 24) == 0);
357 }
358
Ted Kremeneke0ea5dc2009-02-10 01:06:17 +0000359 void Emit32(uint32_t V) { ::Emit32(Out, V); }
360
Chris Lattnerf2390362009-03-28 00:16:20 +0000361 void EmitBuf(const char *Ptr, unsigned NumBytes) {
362 Out.write(Ptr, NumBytes);
Ted Kremenekb978c662009-01-08 01:17:37 +0000363 }
364
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000365 /// EmitIdentifierTable - Emits two tables to the PTH file. The first is
366 /// a hashtable mapping from identifier strings to persistent IDs.
367 /// The second is a straight table mapping from persistent IDs to string data
368 /// (the keys of the first table).
Ted Kremenekf1de4642009-02-11 16:06:55 +0000369 std::pair<Offset, Offset> EmitIdentifierTable();
370
371 /// EmitFileTable - Emit a table mapping from file name strings to PTH
372 /// token data.
373 Offset EmitFileTable() { return PM.Emit(Out); }
374
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000375 PTHEntry LexTokens(Lexer& L);
Ted Kremenek277faca2009-01-27 00:01:05 +0000376 Offset EmitCachedSpellings();
Ted Kremenekad6ce5c2009-02-13 22:07:44 +0000377
Ted Kremenekb978c662009-01-08 01:17:37 +0000378public:
379 PTHWriter(llvm::raw_fd_ostream& out, Preprocessor& pp)
Ted Kremenek277faca2009-01-27 00:01:05 +0000380 : Out(out), PP(pp), idcount(0), CurStrOffset(0) {}
Ted Kremenekb978c662009-01-08 01:17:37 +0000381
Chris Lattner52ba8702009-03-28 00:55:35 +0000382 PTHMap &getPM() { return PM; }
Ted Kremenekd5cded42009-03-19 22:10:38 +0000383 void GeneratePTH(const std::string *MainFile = 0);
Ted Kremenekb978c662009-01-08 01:17:37 +0000384};
385} // end anonymous namespace
386
387uint32_t PTHWriter::ResolveID(const IdentifierInfo* II) {
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000388 // Null IdentifierInfo's map to the persistent ID 0.
389 if (!II)
390 return 0;
391
Ted Kremenek85888962008-10-21 00:54:44 +0000392 IDMap::iterator I = IM.find(II);
Chris Lattnerf2390362009-03-28 00:16:20 +0000393 if (I != IM.end())
394 return I->second; // We've already added 1.
395
396 IM[II] = ++idcount; // Pre-increment since '0' is reserved for NULL.
397 return idcount;
Ted Kremenek85888962008-10-21 00:54:44 +0000398}
399
Ted Kremenekb978c662009-01-08 01:17:37 +0000400void PTHWriter::EmitToken(const Token& T) {
Ted Kremenekf2a223f2009-02-24 01:26:56 +0000401 // Emit the token kind, flags, and length.
402 Emit32(((uint32_t) T.getKind()) | ((((uint32_t) T.getFlags())) << 8)|
403 (((uint32_t) T.getLength()) << 16));
404
Chris Lattnerf2390362009-03-28 00:16:20 +0000405 if (!T.isLiteral()) {
406 Emit32(ResolveID(T.getIdentifierInfo()));
407 } else {
Ted Kremenekf2a223f2009-02-24 01:26:56 +0000408 // We cache *un-cleaned* spellings. This gives us 100% fidelity with the
409 // source code.
410 const char* s = T.getLiteralData();
411 unsigned len = T.getLength();
Ted Kremenek25cbd9f2009-02-24 00:30:21 +0000412
Chris Lattner47246be2009-01-26 19:29:26 +0000413 // Get the string entry.
Ted Kremenekf2a223f2009-02-24 01:26:56 +0000414 llvm::StringMapEntry<OffsetOpt> *E = &CachedStrs.GetOrCreateValue(s, s+len);
Ted Kremenek277faca2009-01-27 00:01:05 +0000415
Ted Kremenekf2a223f2009-02-24 01:26:56 +0000416 // If this is a new string entry, bump the PTH offset.
Ted Kremenek277faca2009-01-27 00:01:05 +0000417 if (!E->getValue().hasOffset()) {
418 E->getValue().setOffset(CurStrOffset);
419 StrEntries.push_back(E);
Ted Kremenekf2a223f2009-02-24 01:26:56 +0000420 CurStrOffset += len + 1;
Ted Kremenek277faca2009-01-27 00:01:05 +0000421 }
422
Ted Kremenek25cbd9f2009-02-24 00:30:21 +0000423 // Emit the relative offset into the PTH file for the spelling string.
Ted Kremenek277faca2009-01-27 00:01:05 +0000424 Emit32(E->getValue().getOffset());
Ted Kremenekb978c662009-01-08 01:17:37 +0000425 }
Ted Kremenek25cbd9f2009-02-24 00:30:21 +0000426
427 // Emit the offset into the original source file of this token so that we
428 // can reconstruct its SourceLocation.
Chris Lattner52c29082009-01-27 06:27:13 +0000429 Emit32(PP.getSourceManager().getFileOffset(T.getLocation()));
Ted Kremenek85888962008-10-21 00:54:44 +0000430}
431
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000432PTHEntry PTHWriter::LexTokens(Lexer& L) {
Ted Kremenek7b78b7c2009-01-19 23:13:15 +0000433 // Pad 0's so that we emit tokens to a 4-byte alignment.
434 // This speed up reading them back in.
Ted Kremeneke0ea5dc2009-02-10 01:06:17 +0000435 Pad(Out, 4);
436 Offset off = (Offset) Out.tell();
Ted Kremenekfb645b62008-12-11 23:36:38 +0000437
438 // Keep track of matching '#if' ... '#endif'.
439 typedef std::vector<std::pair<Offset, unsigned> > PPCondTable;
440 PPCondTable PPCond;
Ted Kremenekdad7b342008-12-12 18:31:09 +0000441 std::vector<unsigned> PPStartCond;
Ted Kremeneke5680f32008-12-23 01:30:52 +0000442 bool ParsingPreprocessorDirective = false;
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000443 Token Tok;
444
445 do {
446 L.LexFromRawLexer(Tok);
Ted Kremenek726080d2009-02-10 22:43:16 +0000447 NextToken:
448
Ted Kremeneke5680f32008-12-23 01:30:52 +0000449 if ((Tok.isAtStartOfLine() || Tok.is(tok::eof)) &&
450 ParsingPreprocessorDirective) {
451 // Insert an eom token into the token cache. It has the same
452 // position as the next token that is not on the same line as the
453 // preprocessor directive. Observe that we continue processing
454 // 'Tok' when we exit this branch.
455 Token Tmp = Tok;
456 Tmp.setKind(tok::eom);
457 Tmp.clearFlag(Token::StartOfLine);
458 Tmp.setIdentifierInfo(0);
Ted Kremenekb978c662009-01-08 01:17:37 +0000459 EmitToken(Tmp);
Ted Kremeneke5680f32008-12-23 01:30:52 +0000460 ParsingPreprocessorDirective = false;
461 }
462
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000463 if (Tok.is(tok::identifier)) {
464 Tok.setIdentifierInfo(PP.LookUpIdentifierInfo(Tok));
Ted Kremeneke4f6b1e2009-02-10 22:27:09 +0000465 EmitToken(Tok);
Ted Kremeneke5680f32008-12-23 01:30:52 +0000466 continue;
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000467 }
Ted Kremeneke5680f32008-12-23 01:30:52 +0000468
469 if (Tok.is(tok::hash) && Tok.isAtStartOfLine()) {
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000470 // Special processing for #include. Store the '#' token and lex
471 // the next token.
Ted Kremeneke5680f32008-12-23 01:30:52 +0000472 assert(!ParsingPreprocessorDirective);
Ted Kremenekfb645b62008-12-11 23:36:38 +0000473 Offset HashOff = (Offset) Out.tell();
Ted Kremenekb978c662009-01-08 01:17:37 +0000474 EmitToken(Tok);
Ted Kremenekdad7b342008-12-12 18:31:09 +0000475
476 // Get the next token.
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000477 L.LexFromRawLexer(Tok);
Ted Kremeneke5680f32008-12-23 01:30:52 +0000478
479 assert(!Tok.isAtStartOfLine());
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000480
481 // Did we see 'include'/'import'/'include_next'?
Ted Kremeneke4f6b1e2009-02-10 22:27:09 +0000482 if (!Tok.is(tok::identifier)) {
483 EmitToken(Tok);
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000484 continue;
Ted Kremeneke4f6b1e2009-02-10 22:27:09 +0000485 }
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000486
487 IdentifierInfo* II = PP.LookUpIdentifierInfo(Tok);
488 Tok.setIdentifierInfo(II);
489 tok::PPKeywordKind K = II->getPPKeywordID();
490
Ted Kremeneke5680f32008-12-23 01:30:52 +0000491 assert(K != tok::pp_not_keyword);
492 ParsingPreprocessorDirective = true;
493
494 switch (K) {
495 default:
496 break;
497 case tok::pp_include:
498 case tok::pp_import:
499 case tok::pp_include_next: {
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000500 // Save the 'include' token.
Ted Kremenekb978c662009-01-08 01:17:37 +0000501 EmitToken(Tok);
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000502 // Lex the next token as an include string.
503 L.setParsingPreprocessorDirective(true);
504 L.LexIncludeFilename(Tok);
505 L.setParsingPreprocessorDirective(false);
Ted Kremeneke5680f32008-12-23 01:30:52 +0000506 assert(!Tok.isAtStartOfLine());
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000507 if (Tok.is(tok::identifier))
508 Tok.setIdentifierInfo(PP.LookUpIdentifierInfo(Tok));
Ted Kremeneke5680f32008-12-23 01:30:52 +0000509
510 break;
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000511 }
Ted Kremeneke5680f32008-12-23 01:30:52 +0000512 case tok::pp_if:
513 case tok::pp_ifdef:
514 case tok::pp_ifndef: {
Ted Kremeneke4f6b1e2009-02-10 22:27:09 +0000515 // Add an entry for '#if' and friends. We initially set the target
516 // index to 0. This will get backpatched when we hit #endif.
Ted Kremenekfb645b62008-12-11 23:36:38 +0000517 PPStartCond.push_back(PPCond.size());
Ted Kremenekdad7b342008-12-12 18:31:09 +0000518 PPCond.push_back(std::make_pair(HashOff, 0U));
Ted Kremeneke5680f32008-12-23 01:30:52 +0000519 break;
Ted Kremenekfb645b62008-12-11 23:36:38 +0000520 }
Ted Kremeneke5680f32008-12-23 01:30:52 +0000521 case tok::pp_endif: {
Ted Kremenekfb645b62008-12-11 23:36:38 +0000522 // Add an entry for '#endif'. We set the target table index to itself.
Ted Kremenekdad7b342008-12-12 18:31:09 +0000523 // This will later be set to zero when emitting to the PTH file. We
524 // use 0 for uninitialized indices because that is easier to debug.
Ted Kremenekfb645b62008-12-11 23:36:38 +0000525 unsigned index = PPCond.size();
Ted Kremenekfb645b62008-12-11 23:36:38 +0000526 // Backpatch the opening '#if' entry.
Ted Kremenekdad7b342008-12-12 18:31:09 +0000527 assert(!PPStartCond.empty());
528 assert(PPCond.size() > PPStartCond.back());
Ted Kremenekfb645b62008-12-11 23:36:38 +0000529 assert(PPCond[PPStartCond.back()].second == 0);
530 PPCond[PPStartCond.back()].second = index;
531 PPStartCond.pop_back();
Ted Kremenekdad7b342008-12-12 18:31:09 +0000532 // Add the new entry to PPCond.
533 PPCond.push_back(std::make_pair(HashOff, index));
Ted Kremenek726080d2009-02-10 22:43:16 +0000534 EmitToken(Tok);
535
536 // Some files have gibberish on the same line as '#endif'.
537 // Discard these tokens.
538 do L.LexFromRawLexer(Tok); while (!Tok.is(tok::eof) &&
539 !Tok.isAtStartOfLine());
540 // We have the next token in hand.
541 // Don't immediately lex the next one.
542 goto NextToken;
Ted Kremenekfb645b62008-12-11 23:36:38 +0000543 }
Ted Kremeneke5680f32008-12-23 01:30:52 +0000544 case tok::pp_elif:
545 case tok::pp_else: {
546 // Add an entry for #elif or #else.
Ted Kremenekdad7b342008-12-12 18:31:09 +0000547 // This serves as both a closing and opening of a conditional block.
548 // This means that its entry will get backpatched later.
Ted Kremenekfb645b62008-12-11 23:36:38 +0000549 unsigned index = PPCond.size();
Ted Kremenekfb645b62008-12-11 23:36:38 +0000550 // Backpatch the previous '#if' entry.
Ted Kremenekdad7b342008-12-12 18:31:09 +0000551 assert(!PPStartCond.empty());
552 assert(PPCond.size() > PPStartCond.back());
Ted Kremenekfb645b62008-12-11 23:36:38 +0000553 assert(PPCond[PPStartCond.back()].second == 0);
554 PPCond[PPStartCond.back()].second = index;
555 PPStartCond.pop_back();
556 // Now add '#elif' as a new block opening.
Ted Kremenekdad7b342008-12-12 18:31:09 +0000557 PPCond.push_back(std::make_pair(HashOff, 0U));
558 PPStartCond.push_back(index);
Ted Kremeneke5680f32008-12-23 01:30:52 +0000559 break;
560 }
Ted Kremenekfb645b62008-12-11 23:36:38 +0000561 }
Ted Kremeneke4f6b1e2009-02-10 22:27:09 +0000562 }
563
564 EmitToken(Tok);
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000565 }
Ted Kremeneke4f6b1e2009-02-10 22:27:09 +0000566 while (Tok.isNot(tok::eof));
Ted Kremenekb978c662009-01-08 01:17:37 +0000567
Ted Kremenekdad7b342008-12-12 18:31:09 +0000568 assert(PPStartCond.empty() && "Error: imblanced preprocessor conditionals.");
Ted Kremenekb978c662009-01-08 01:17:37 +0000569
Ted Kremenekfb645b62008-12-11 23:36:38 +0000570 // Next write out PPCond.
571 Offset PPCondOff = (Offset) Out.tell();
Ted Kremenekdad7b342008-12-12 18:31:09 +0000572
573 // Write out the size of PPCond so that clients can identifer empty tables.
Ted Kremenekb978c662009-01-08 01:17:37 +0000574 Emit32(PPCond.size());
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000575
Ted Kremenekdad7b342008-12-12 18:31:09 +0000576 for (unsigned i = 0, e = PPCond.size(); i!=e; ++i) {
Ted Kremenekb978c662009-01-08 01:17:37 +0000577 Emit32(PPCond[i].first - off);
Ted Kremenekdad7b342008-12-12 18:31:09 +0000578 uint32_t x = PPCond[i].second;
579 assert(x != 0 && "PPCond entry not backpatched.");
580 // Emit zero for #endifs. This allows us to do checking when
581 // we read the PTH file back in.
Ted Kremenekb978c662009-01-08 01:17:37 +0000582 Emit32(x == i ? 0 : x);
Ted Kremenekfb645b62008-12-11 23:36:38 +0000583 }
584
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000585 return PTHEntry(off, PPCondOff);
Ted Kremenekbe295332009-01-08 02:44:06 +0000586}
587
Ted Kremenek277faca2009-01-27 00:01:05 +0000588Offset PTHWriter::EmitCachedSpellings() {
589 // Write each cached strings to the PTH file.
590 Offset SpellingsOff = Out.tell();
591
592 for (std::vector<llvm::StringMapEntry<OffsetOpt>*>::iterator
Chris Lattnerf2390362009-03-28 00:16:20 +0000593 I = StrEntries.begin(), E = StrEntries.end(); I!=E; ++I)
594 EmitBuf((*I)->getKeyData(), (*I)->getKeyLength()+1 /*nul included*/);
Ted Kremenekbe295332009-01-08 02:44:06 +0000595
Ted Kremenek277faca2009-01-27 00:01:05 +0000596 return SpellingsOff;
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000597}
Ted Kremenek85888962008-10-21 00:54:44 +0000598
Ted Kremenekd5cded42009-03-19 22:10:38 +0000599void PTHWriter::GeneratePTH(const std::string *MainFile) {
Ted Kremeneke1b64982009-01-26 21:43:14 +0000600 // Generate the prologue.
601 Out << "cfe-pth";
Ted Kremenek67d15052009-01-26 21:50:21 +0000602 Emit32(PTHManager::Version);
Ted Kremeneka4bd8eb2009-02-11 23:34:32 +0000603
604 // Leave 4 words for the prologue.
605 Offset PrologueOffset = Out.tell();
Chris Lattnerf2390362009-03-28 00:16:20 +0000606 for (unsigned i = 0; i < 4; ++i)
607 Emit32(0);
Ted Kremenekd5cded42009-03-19 22:10:38 +0000608
609 // Write the name of the MainFile.
Chris Lattnerf2390362009-03-28 00:16:20 +0000610 if (MainFile && !MainFile->empty()) {
Ted Kremenekd5cded42009-03-19 22:10:38 +0000611 Emit16(MainFile->length());
Chris Lattnerf2390362009-03-28 00:16:20 +0000612 EmitBuf(MainFile->data(), MainFile->length());
613 } else {
Ted Kremenekd5cded42009-03-19 22:10:38 +0000614 // String with 0 bytes.
615 Emit16(0);
616 }
617 Emit8(0);
Ted Kremeneke1b64982009-01-26 21:43:14 +0000618
Ted Kremenek85888962008-10-21 00:54:44 +0000619 // Iterate over all the files in SourceManager. Create a lexer
620 // for each file and cache the tokens.
Chris Lattnerc6fe32a2009-01-17 03:48:08 +0000621 SourceManager &SM = PP.getSourceManager();
622 const LangOptions &LOpts = PP.getLangOptions();
Ted Kremenek85888962008-10-21 00:54:44 +0000623
Chris Lattnerc6fe32a2009-01-17 03:48:08 +0000624 for (SourceManager::fileinfo_iterator I = SM.fileinfo_begin(),
625 E = SM.fileinfo_end(); I != E; ++I) {
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000626 const SrcMgr::ContentCache &C = *I->second;
Chris Lattnerc6fe32a2009-01-17 03:48:08 +0000627 const FileEntry *FE = C.Entry;
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000628
629 // FIXME: Handle files with non-absolute paths.
630 llvm::sys::Path P(FE->getName());
631 if (!P.isAbsolute())
632 continue;
Ted Kremenek85888962008-10-21 00:54:44 +0000633
Chris Lattnerc6fe32a2009-01-17 03:48:08 +0000634 const llvm::MemoryBuffer *B = C.getBuffer();
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000635 if (!B) continue;
Ted Kremenekfb645b62008-12-11 23:36:38 +0000636
Chris Lattner2b2453a2009-01-17 06:22:33 +0000637 FileID FID = SM.createFileID(FE, SourceLocation(), SrcMgr::C_User);
Chris Lattner025c3a62009-01-17 07:35:14 +0000638 Lexer L(FID, SM, LOpts);
Ted Kremenekd8c02922009-02-10 22:16:22 +0000639 PM.insert(FE, LexTokens(L));
Daniel Dunbar31309ab2008-11-26 02:18:33 +0000640 }
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000641
642 // Write out the identifier table.
Chris Lattnerf2390362009-03-28 00:16:20 +0000643 const std::pair<Offset,Offset> &IdTableOff = EmitIdentifierTable();
Ted Kremenek85888962008-10-21 00:54:44 +0000644
Ted Kremenekbe295332009-01-08 02:44:06 +0000645 // Write out the cached strings table.
Ted Kremenek277faca2009-01-27 00:01:05 +0000646 Offset SpellingOff = EmitCachedSpellings();
Ted Kremenekbe295332009-01-08 02:44:06 +0000647
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000648 // Write out the file table.
Ted Kremenekb978c662009-01-08 01:17:37 +0000649 Offset FileTableOff = EmitFileTable();
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000650
Ted Kremeneka4bd8eb2009-02-11 23:34:32 +0000651 // Finally, write the prologue.
652 Out.seek(PrologueOffset);
Ted Kremenekb978c662009-01-08 01:17:37 +0000653 Emit32(IdTableOff.first);
Ted Kremenekf1de4642009-02-11 16:06:55 +0000654 Emit32(IdTableOff.second);
Ted Kremenekb978c662009-01-08 01:17:37 +0000655 Emit32(FileTableOff);
Ted Kremenek277faca2009-01-27 00:01:05 +0000656 Emit32(SpellingOff);
Ted Kremenekb978c662009-01-08 01:17:37 +0000657}
658
Chris Lattner52ba8702009-03-28 00:55:35 +0000659namespace {
660/// StatListener - A simple "interpose" object used to monitor stat calls
661/// invoked by FileManager while processing the original sources used
662/// as input to PTH generation. StatListener populates the PTHWriter's
663/// file map with stat information for directories as well as negative stats.
664/// Stat information for files are populated elsewhere.
665class StatListener : public StatSysCallCache {
666 PTHMap &PM;
667public:
668 StatListener(PTHMap &pm) : PM(pm) {}
669 ~StatListener() {}
670
671 int stat(const char *path, struct stat *buf) {
672 int result = ::stat(path, buf);
673
674 if (result != 0) // Failed 'stat'.
675 PM.insert(path, PTHEntry());
676 else if (S_ISDIR(buf->st_mode)) {
677 // Only cache directories with absolute paths.
678 if (!llvm::sys::Path(path).isAbsolute())
679 return result;
680
681 PM.insert(PTHEntryKeyVariant(buf, path), PTHEntry());
682 }
683
684 return result;
685 }
686};
687} // end anonymous namespace
688
689
Chris Lattnerf2390362009-03-28 00:16:20 +0000690void clang::CacheTokens(Preprocessor &PP, const std::string &OutFile) {
Ted Kremenekb978c662009-01-08 01:17:37 +0000691 // Open up the PTH file.
692 std::string ErrMsg;
693 llvm::raw_fd_ostream Out(OutFile.c_str(), true, ErrMsg);
694
695 if (!ErrMsg.empty()) {
696 llvm::errs() << "PTH error: " << ErrMsg << "\n";
697 return;
698 }
Ted Kremenekd5cded42009-03-19 22:10:38 +0000699
700 // Get the name of the main file.
701 const SourceManager &SrcMgr = PP.getSourceManager();
702 const FileEntry *MainFile = SrcMgr.getFileEntryForID(SrcMgr.getMainFileID());
703 llvm::sys::Path MainFilePath(MainFile->getName());
704 std::string MainFileName;
705
706 if (!MainFilePath.isAbsolute()) {
707 llvm::sys::Path P = llvm::sys::Path::GetCurrentDirectory();
708 P.appendComponent(MainFilePath.toString());
709 MainFileName = P.toString();
Chris Lattnerf2390362009-03-28 00:16:20 +0000710 } else {
Ted Kremenekd5cded42009-03-19 22:10:38 +0000711 MainFileName = MainFilePath.toString();
712 }
Ted Kremenekad6ce5c2009-02-13 22:07:44 +0000713
714 // Create the PTHWriter.
Ted Kremenekb978c662009-01-08 01:17:37 +0000715 PTHWriter PW(Out, PP);
Ted Kremenekad6ce5c2009-02-13 22:07:44 +0000716
717 // Install the 'stat' system call listener in the FileManager.
Chris Lattner52ba8702009-03-28 00:55:35 +0000718 PP.getFileManager().setStatCache(new StatListener(PW.getPM()));
Ted Kremenekad6ce5c2009-02-13 22:07:44 +0000719
720 // Lex through the entire file. This will populate SourceManager with
721 // all of the header information.
722 Token Tok;
723 PP.EnterMainSourceFile();
724 do { PP.Lex(Tok); } while (Tok.isNot(tok::eof));
725
Ted Kremenekad6ce5c2009-02-13 22:07:44 +0000726 // Generate the PTH file.
727 PP.getFileManager().setStatCache(0);
Ted Kremenekd5cded42009-03-19 22:10:38 +0000728 PW.GeneratePTH(&MainFileName);
Ted Kremenek85888962008-10-21 00:54:44 +0000729}
Ted Kremeneke0ea5dc2009-02-10 01:06:17 +0000730
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000731//===----------------------------------------------------------------------===//
732
Chris Lattnerf2390362009-03-28 00:16:20 +0000733class PTHIdKey {
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000734public:
735 const IdentifierInfo* II;
736 uint32_t FileOffset;
737};
738
Chris Lattnerf2390362009-03-28 00:16:20 +0000739namespace {
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000740class VISIBILITY_HIDDEN PTHIdentifierTableTrait {
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000741public:
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000742 typedef PTHIdKey* key_type;
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000743 typedef key_type key_type_ref;
744
745 typedef uint32_t data_type;
746 typedef data_type data_type_ref;
747
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000748 static unsigned ComputeHash(PTHIdKey* key) {
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000749 return BernsteinHash(key->II->getName());
750 }
751
752 static std::pair<unsigned,unsigned>
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000753 EmitKeyDataLength(llvm::raw_ostream& Out, const PTHIdKey* key, uint32_t) {
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000754 unsigned n = strlen(key->II->getName()) + 1;
755 ::Emit16(Out, n);
756 return std::make_pair(n, sizeof(uint32_t));
757 }
758
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000759 static void EmitKey(llvm::raw_fd_ostream& Out, PTHIdKey* key, unsigned n) {
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000760 // Record the location of the key data. This is used when generating
761 // the mapping from persistent IDs to strings.
762 key->FileOffset = Out.tell();
763 Out.write(key->II->getName(), n);
764 }
765
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000766 static void EmitData(llvm::raw_ostream& Out, PTHIdKey*, uint32_t pID,
Ted Kremenek337edcd2009-02-12 03:26:59 +0000767 unsigned) {
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000768 ::Emit32(Out, pID);
769 }
770};
771} // end anonymous namespace
772
773/// EmitIdentifierTable - Emits two tables to the PTH file. The first is
774/// a hashtable mapping from identifier strings to persistent IDs. The second
775/// is a straight table mapping from persistent IDs to string data (the
776/// keys of the first table).
777///
778std::pair<Offset,Offset> PTHWriter::EmitIdentifierTable() {
779 // Build two maps:
780 // (1) an inverse map from persistent IDs -> (IdentifierInfo*,Offset)
781 // (2) a map from (IdentifierInfo*, Offset)* -> persistent IDs
782
783 // Note that we use 'calloc', so all the bytes are 0.
Chris Lattnerf2390362009-03-28 00:16:20 +0000784 PTHIdKey *IIDMap = (PTHIdKey*)calloc(idcount, sizeof(PTHIdKey));
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000785
786 // Create the hashtable.
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000787 OnDiskChainedHashTableGenerator<PTHIdentifierTableTrait> IIOffMap;
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000788
789 // Generate mapping from persistent IDs -> IdentifierInfo*.
Chris Lattnerf2390362009-03-28 00:16:20 +0000790 for (IDMap::iterator I = IM.begin(), E = IM.end(); I != E; ++I) {
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000791 // Decrement by 1 because we are using a vector for the lookup and
792 // 0 is reserved for NULL.
793 assert(I->second > 0);
794 assert(I->second-1 < idcount);
795 unsigned idx = I->second-1;
796
797 // Store the mapping from persistent ID to IdentifierInfo*
798 IIDMap[idx].II = I->first;
799
800 // Store the reverse mapping in a hashtable.
801 IIOffMap.insert(&IIDMap[idx], I->second);
802 }
803
804 // Write out the inverse map first. This causes the PCIDKey entries to
805 // record PTH file offsets for the string data. This is used to write
806 // the second table.
807 Offset StringTableOffset = IIOffMap.Emit(Out);
808
809 // Now emit the table mapping from persistent IDs to PTH file offsets.
810 Offset IDOff = Out.tell();
811 Emit32(idcount); // Emit the number of identifiers.
Chris Lattnerf2390362009-03-28 00:16:20 +0000812 for (unsigned i = 0 ; i < idcount; ++i)
813 Emit32(IIDMap[i].FileOffset);
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000814
815 // Finally, release the inverse map.
816 free(IIDMap);
817
818 return std::make_pair(IDOff, StringTableOffset);
819}