blob: 4ca350e6fb075bf7a58c0bc5cf2772581f55443d [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;
69 for ( ; n ; --n ) Emit8(Out, 0);
Ted Kremeneke0ea5dc2009-02-10 01:06:17 +000070}
71
Ted Kremenek7e3a0042009-02-11 21:29:16 +000072// Bernstein hash function:
73// This is basically copy-and-paste from StringMap. This likely won't
74// stay here, which is why I didn't both to expose this function from
75// String Map.
76static unsigned BernsteinHash(const char* x) {
77 unsigned int R = 0;
78 for ( ; *x != '\0' ; ++x) R = R * 33 + *x;
79 return R + (R >> 5);
80}
81
Ted Kremenekf0e1f792009-02-10 01:14:45 +000082//===----------------------------------------------------------------------===//
83// On Disk Hashtable Logic. This will eventually get refactored and put
84// elsewhere.
85//===----------------------------------------------------------------------===//
86
87template<typename Info>
88class OnDiskChainedHashTableGenerator {
89 unsigned NumBuckets;
90 unsigned NumEntries;
91 llvm::BumpPtrAllocator BA;
92
93 class Item {
94 public:
Ted Kremenekd8c02922009-02-10 22:16:22 +000095 typename Info::key_type key;
96 typename Info::data_type data;
Ted Kremenekf0e1f792009-02-10 01:14:45 +000097 Item *next;
98 const uint32_t hash;
99
Ted Kremenekd8c02922009-02-10 22:16:22 +0000100 Item(typename Info::key_type_ref k, typename Info::data_type_ref d)
101 : key(k), data(d), next(0), hash(Info::ComputeHash(k)) {}
Ted Kremenekf0e1f792009-02-10 01:14:45 +0000102 };
103
104 class Bucket {
105 public:
106 Offset off;
107 Item* head;
108 unsigned length;
109
110 Bucket() {}
111 };
112
113 Bucket* Buckets;
114
115private:
Ted Kremenekd8c02922009-02-10 22:16:22 +0000116 void insert(Bucket* b, size_t size, Item* E) {
Ted Kremenekf0e1f792009-02-10 01:14:45 +0000117 unsigned idx = E->hash & (size - 1);
118 Bucket& B = b[idx];
119 E->next = B.head;
120 ++B.length;
121 B.head = E;
122 }
123
124 void resize(size_t newsize) {
Ted Kremenekd8c02922009-02-10 22:16:22 +0000125 Bucket* newBuckets = (Bucket*) calloc(newsize, sizeof(Bucket));
126 // Populate newBuckets with the old entries.
Ted Kremenekf0e1f792009-02-10 01:14:45 +0000127 for (unsigned i = 0; i < NumBuckets; ++i)
Ted Kremenekd8c02922009-02-10 22:16:22 +0000128 for (Item* E = Buckets[i].head; E ; ) {
Ted Kremenekf0e1f792009-02-10 01:14:45 +0000129 Item* N = E->next;
Ted Kremenekd8c02922009-02-10 22:16:22 +0000130 E->next = 0;
Ted Kremenekf0e1f792009-02-10 01:14:45 +0000131 insert(newBuckets, newsize, E);
132 E = N;
133 }
134
135 free(Buckets);
136 NumBuckets = newsize;
137 Buckets = newBuckets;
138 }
139
140public:
141
Ted Kremenekd8c02922009-02-10 22:16:22 +0000142 void insert(typename Info::key_type_ref key,
143 typename Info::data_type_ref data) {
144
Ted Kremenekf0e1f792009-02-10 01:14:45 +0000145 ++NumEntries;
146 if (4*NumEntries >= 3*NumBuckets) resize(NumBuckets*2);
147 insert(Buckets, NumBuckets, new (BA.Allocate<Item>()) Item(key, data));
148 }
149
150 Offset Emit(llvm::raw_fd_ostream& out) {
151 // Emit the payload of the table.
152 for (unsigned i = 0; i < NumBuckets; ++i) {
153 Bucket& B = Buckets[i];
154 if (!B.head) continue;
155
156 // Store the offset for the data of this bucket.
Ted Kremenekf0e1f792009-02-10 01:14:45 +0000157 B.off = out.tell();
158
Ted Kremenekd8c02922009-02-10 22:16:22 +0000159 // Write out the number of items in the bucket.
160 Emit16(out, B.length);
Ted Kremenekf0e1f792009-02-10 01:14:45 +0000161
162 // Write out the entries in the bucket.
163 for (Item *I = B.head; I ; I = I->next) {
164 Emit32(out, I->hash);
Ted Kremenekd8c02922009-02-10 22:16:22 +0000165 const std::pair<unsigned, unsigned>& Len =
166 Info::EmitKeyDataLength(out, I->key, I->data);
167 Info::EmitKey(out, I->key, Len.first);
Ted Kremenek337edcd2009-02-12 03:26:59 +0000168 Info::EmitData(out, I->key, I->data, Len.second);
Ted Kremenekf0e1f792009-02-10 01:14:45 +0000169 }
170 }
171
172 // Emit the hashtable itself.
173 Pad(out, 4);
174 Offset TableOff = out.tell();
Ted Kremenekd8c02922009-02-10 22:16:22 +0000175 Emit32(out, NumBuckets);
176 Emit32(out, NumEntries);
Ted Kremenekf0e1f792009-02-10 01:14:45 +0000177 for (unsigned i = 0; i < NumBuckets; ++i) Emit32(out, Buckets[i].off);
178
179 return TableOff;
180 }
181
182 OnDiskChainedHashTableGenerator() {
183 NumEntries = 0;
Ted Kremenekd8c02922009-02-10 22:16:22 +0000184 NumBuckets = 64;
185 // Note that we do not need to run the constructors of the individual
186 // Bucket objects since 'calloc' returns bytes that are all 0.
187 Buckets = (Bucket*) calloc(NumBuckets, sizeof(Bucket));
Ted Kremenekf0e1f792009-02-10 01:14:45 +0000188 }
189
190 ~OnDiskChainedHashTableGenerator() {
191 free(Buckets);
192 }
193};
194
195//===----------------------------------------------------------------------===//
196// PTH-specific stuff.
197//===----------------------------------------------------------------------===//
198
Ted Kremenekbe295332009-01-08 02:44:06 +0000199namespace {
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000200class VISIBILITY_HIDDEN PTHEntry {
Ted Kremenekbe295332009-01-08 02:44:06 +0000201 Offset TokenData, PPCondData;
Ted Kremenekbe295332009-01-08 02:44:06 +0000202
203public:
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000204 PTHEntry() {}
Ted Kremenekbe295332009-01-08 02:44:06 +0000205
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000206 PTHEntry(Offset td, Offset ppcd)
Ted Kremenek277faca2009-01-27 00:01:05 +0000207 : TokenData(td), PPCondData(ppcd) {}
Ted Kremenekbe295332009-01-08 02:44:06 +0000208
Ted Kremenek277faca2009-01-27 00:01:05 +0000209 Offset getTokenOffset() const { return TokenData; }
Ted Kremenekbe295332009-01-08 02:44:06 +0000210 Offset getPPCondTableOffset() const { return PPCondData; }
Ted Kremenek277faca2009-01-27 00:01:05 +0000211};
Ted Kremenekbe295332009-01-08 02:44:06 +0000212
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000213
214class VISIBILITY_HIDDEN PTHEntryKeyVariant {
Ted Kremenekad6ce5c2009-02-13 22:07:44 +0000215 union { const FileEntry* FE; const char* Path; };
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000216 enum { IsFE = 0x1, IsDE = 0x2, IsNoExist = 0x0 } Kind;
Ted Kremenekad6ce5c2009-02-13 22:07:44 +0000217 struct stat *StatBuf;
Ted Kremenekd8c02922009-02-10 22:16:22 +0000218public:
Ted Kremenekad6ce5c2009-02-13 22:07:44 +0000219 PTHEntryKeyVariant(const FileEntry *fe)
220 : FE(fe), Kind(IsFE), StatBuf(0) {}
221
222 PTHEntryKeyVariant(struct stat* statbuf, const char* path)
223 : Path(path), Kind(IsDE), StatBuf(new struct stat(*statbuf)) {}
224
225 PTHEntryKeyVariant(const char* path)
226 : Path(path), Kind(IsNoExist), StatBuf(0) {}
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000227
Ted Kremenekad6ce5c2009-02-13 22:07:44 +0000228 bool isFile() const { return Kind == IsFE; }
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000229
230 const char* getCString() const {
Ted Kremenekad6ce5c2009-02-13 22:07:44 +0000231 return Kind == IsFE ? FE->getName() : Path;
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000232 }
233
234 unsigned getKind() const { return (unsigned) Kind; }
235
236 void EmitData(llvm::raw_ostream& Out) {
237 switch (Kind) {
238 case IsFE:
239 // Emit stat information.
240 ::Emit32(Out, FE->getInode());
241 ::Emit32(Out, FE->getDevice());
242 ::Emit16(Out, FE->getFileMode());
243 ::Emit64(Out, FE->getModificationTime());
244 ::Emit64(Out, FE->getSize());
245 break;
246 case IsDE:
Ted Kremenekad6ce5c2009-02-13 22:07:44 +0000247 // Emit stat information.
248 ::Emit32(Out, (uint32_t) StatBuf->st_ino);
249 ::Emit32(Out, (uint32_t) StatBuf->st_dev);
250 ::Emit16(Out, (uint16_t) StatBuf->st_mode);
251 ::Emit64(Out, (uint64_t) StatBuf->st_mtime);
252 ::Emit64(Out, (uint64_t) StatBuf->st_size);
253 delete StatBuf;
254 break;
255 default:
256 break;
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000257 }
258 }
259
260 unsigned getRepresentationLength() const {
Ted Kremenekad6ce5c2009-02-13 22:07:44 +0000261 return Kind == IsNoExist ? 0 : 4 + 4 + 2 + 8 + 8;
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000262 }
263};
264
265class VISIBILITY_HIDDEN FileEntryPTHEntryInfo {
266public:
267 typedef PTHEntryKeyVariant key_type;
Ted Kremenekd8c02922009-02-10 22:16:22 +0000268 typedef key_type key_type_ref;
269
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000270 typedef PTHEntry data_type;
271 typedef const PTHEntry& data_type_ref;
Ted Kremenekd8c02922009-02-10 22:16:22 +0000272
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000273 static unsigned ComputeHash(PTHEntryKeyVariant V) {
274 return BernsteinHash(V.getCString());
Ted Kremenekd8c02922009-02-10 22:16:22 +0000275 }
276
277 static std::pair<unsigned,unsigned>
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000278 EmitKeyDataLength(llvm::raw_ostream& Out, PTHEntryKeyVariant V,
279 const PTHEntry& E) {
Ted Kremenekd8c02922009-02-10 22:16:22 +0000280
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000281 unsigned n = strlen(V.getCString()) + 1 + 1;
Ted Kremenekd8c02922009-02-10 22:16:22 +0000282 ::Emit16(Out, n);
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000283
Ted Kremenekad6ce5c2009-02-13 22:07:44 +0000284 unsigned m = V.getRepresentationLength() + (V.isFile() ? 4 + 4 : 0);
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000285 ::Emit8(Out, m);
286
287 return std::make_pair(n, m);
Ted Kremenekd8c02922009-02-10 22:16:22 +0000288 }
289
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000290 static void EmitKey(llvm::raw_ostream& Out, PTHEntryKeyVariant V, unsigned n){
291 // Emit the entry kind.
292 ::Emit8(Out, (unsigned) V.getKind());
293 // Emit the string.
294 Out.write(V.getCString(), n - 1);
Ted Kremenekd8c02922009-02-10 22:16:22 +0000295 }
296
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000297 static void EmitData(llvm::raw_ostream& Out, PTHEntryKeyVariant V,
298 const PTHEntry& E, unsigned) {
299
300
301 // For file entries emit the offsets into the PTH file for token data
302 // and the preprocessor blocks table.
Ted Kremenekad6ce5c2009-02-13 22:07:44 +0000303 if (V.isFile()) {
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000304 ::Emit32(Out, E.getTokenOffset());
305 ::Emit32(Out, E.getPPCondTableOffset());
306 }
307
308 // Emit any other data associated with the key (i.e., stat information).
309 V.EmitData(Out);
Ted Kremenekd8c02922009-02-10 22:16:22 +0000310 }
311};
312
Ted Kremenek277faca2009-01-27 00:01:05 +0000313class OffsetOpt {
314 bool valid;
315 Offset off;
316public:
317 OffsetOpt() : valid(false) {}
318 bool hasOffset() const { return valid; }
319 Offset getOffset() const { assert(valid); return off; }
320 void setOffset(Offset o) { off = o; valid = true; }
Ted Kremenekbe295332009-01-08 02:44:06 +0000321};
322} // end anonymous namespace
323
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000324typedef OnDiskChainedHashTableGenerator<FileEntryPTHEntryInfo> PTHMap;
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000325typedef llvm::DenseMap<const IdentifierInfo*,uint32_t> IDMap;
Ted Kremenek277faca2009-01-27 00:01:05 +0000326typedef llvm::StringMap<OffsetOpt, llvm::BumpPtrAllocator> CachedStrsTy;
Ted Kremenek85888962008-10-21 00:54:44 +0000327
Ted Kremenekb978c662009-01-08 01:17:37 +0000328namespace {
329class VISIBILITY_HIDDEN PTHWriter {
330 IDMap IM;
331 llvm::raw_fd_ostream& Out;
332 Preprocessor& PP;
333 uint32_t idcount;
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000334 PTHMap PM;
Ted Kremenekbe295332009-01-08 02:44:06 +0000335 CachedStrsTy CachedStrs;
Ted Kremenek277faca2009-01-27 00:01:05 +0000336 Offset CurStrOffset;
337 std::vector<llvm::StringMapEntry<OffsetOpt>*> StrEntries;
Ted Kremenek8f174e12008-12-23 02:52:12 +0000338
Ted Kremenekb978c662009-01-08 01:17:37 +0000339 //// Get the persistent id for the given IdentifierInfo*.
340 uint32_t ResolveID(const IdentifierInfo* II);
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000341
Ted Kremenekb978c662009-01-08 01:17:37 +0000342 /// Emit a token to the PTH file.
343 void EmitToken(const Token& T);
344
345 void Emit8(uint32_t V) {
346 Out << (unsigned char)(V);
347 }
348
Ted Kremeneke0ea5dc2009-02-10 01:06:17 +0000349 void Emit16(uint32_t V) { ::Emit16(Out, V); }
Ted Kremenekb978c662009-01-08 01:17:37 +0000350
351 void Emit24(uint32_t V) {
352 Out << (unsigned char)(V);
353 Out << (unsigned char)(V >> 8);
354 Out << (unsigned char)(V >> 16);
355 assert((V >> 24) == 0);
356 }
357
Ted Kremeneke0ea5dc2009-02-10 01:06:17 +0000358 void Emit32(uint32_t V) { ::Emit32(Out, V); }
359
Ted Kremenekb978c662009-01-08 01:17:37 +0000360 void EmitBuf(const char* I, const char* E) {
361 for ( ; I != E ; ++I) Out << *I;
362 }
363
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000364 /// EmitIdentifierTable - Emits two tables to the PTH file. The first is
365 /// a hashtable mapping from identifier strings to persistent IDs.
366 /// The second is a straight table mapping from persistent IDs to string data
367 /// (the keys of the first table).
Ted Kremenekf1de4642009-02-11 16:06:55 +0000368 std::pair<Offset, Offset> EmitIdentifierTable();
369
370 /// EmitFileTable - Emit a table mapping from file name strings to PTH
371 /// token data.
372 Offset EmitFileTable() { return PM.Emit(Out); }
373
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000374 PTHEntry LexTokens(Lexer& L);
Ted Kremenek277faca2009-01-27 00:01:05 +0000375 Offset EmitCachedSpellings();
Ted Kremenekad6ce5c2009-02-13 22:07:44 +0000376
377 /// StatListener - A simple "interpose" object used to monitor stat calls
378 /// invoked by FileManager while processing the original sources used
379 /// as input to PTH generation. StatListener populates the PTHWriter's
380 /// file map with stat information for directories as well as negative stats.
381 /// Stat information for files are populated elsewhere.
382 class StatListener : public StatSysCallCache {
383 PTHMap& PM;
384 public:
385 StatListener(PTHMap& pm) : PM(pm) {}
386 ~StatListener() {}
387
388 int stat(const char *path, struct stat *buf) {
389 int result = ::stat(path, buf);
390
391 if (result != 0) // Failed 'stat'.
392 PM.insert(path, PTHEntry());
393 else if (S_ISDIR(buf->st_mode)) {
394 // Only cache directories with absolute paths.
395 if (!llvm::sys::Path(path).isAbsolute())
396 return result;
397
398 PM.insert(PTHEntryKeyVariant(buf, path), PTHEntry());
399 }
400
401 return result;
402 }
403 };
Ted Kremenekbe295332009-01-08 02:44:06 +0000404
Ted Kremenekb978c662009-01-08 01:17:37 +0000405public:
406 PTHWriter(llvm::raw_fd_ostream& out, Preprocessor& pp)
Ted Kremenek277faca2009-01-27 00:01:05 +0000407 : Out(out), PP(pp), idcount(0), CurStrOffset(0) {}
Ted Kremenekb978c662009-01-08 01:17:37 +0000408
Ted Kremenekd5cded42009-03-19 22:10:38 +0000409 void GeneratePTH(const std::string *MainFile = 0);
Ted Kremenekad6ce5c2009-02-13 22:07:44 +0000410
411 StatSysCallCache *createStatListener() {
412 return new StatListener(PM);
413 }
Ted Kremenekb978c662009-01-08 01:17:37 +0000414};
415} // end anonymous namespace
416
417uint32_t PTHWriter::ResolveID(const IdentifierInfo* II) {
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000418 // Null IdentifierInfo's map to the persistent ID 0.
419 if (!II)
420 return 0;
421
Ted Kremenek85888962008-10-21 00:54:44 +0000422 IDMap::iterator I = IM.find(II);
423
424 if (I == IM.end()) {
Ted Kremenekb978c662009-01-08 01:17:37 +0000425 IM[II] = ++idcount; // Pre-increment since '0' is reserved for NULL.
426 return idcount;
Ted Kremenek85888962008-10-21 00:54:44 +0000427 }
428
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000429 return I->second; // We've already added 1.
Ted Kremenek85888962008-10-21 00:54:44 +0000430}
431
Ted Kremenekb978c662009-01-08 01:17:37 +0000432void PTHWriter::EmitToken(const Token& T) {
Ted Kremenekf2a223f2009-02-24 01:26:56 +0000433 // Emit the token kind, flags, and length.
434 Emit32(((uint32_t) T.getKind()) | ((((uint32_t) T.getFlags())) << 8)|
435 (((uint32_t) T.getLength()) << 16));
436
Chris Lattner47246be2009-01-26 19:29:26 +0000437 if (T.isLiteral()) {
Ted Kremenekf2a223f2009-02-24 01:26:56 +0000438 // We cache *un-cleaned* spellings. This gives us 100% fidelity with the
439 // source code.
440 const char* s = T.getLiteralData();
441 unsigned len = T.getLength();
Ted Kremenek25cbd9f2009-02-24 00:30:21 +0000442
Chris Lattner47246be2009-01-26 19:29:26 +0000443 // Get the string entry.
Ted Kremenekf2a223f2009-02-24 01:26:56 +0000444 llvm::StringMapEntry<OffsetOpt> *E = &CachedStrs.GetOrCreateValue(s, s+len);
Ted Kremenek277faca2009-01-27 00:01:05 +0000445
Ted Kremenekf2a223f2009-02-24 01:26:56 +0000446 // If this is a new string entry, bump the PTH offset.
Ted Kremenek277faca2009-01-27 00:01:05 +0000447 if (!E->getValue().hasOffset()) {
448 E->getValue().setOffset(CurStrOffset);
449 StrEntries.push_back(E);
Ted Kremenekf2a223f2009-02-24 01:26:56 +0000450 CurStrOffset += len + 1;
Ted Kremenek277faca2009-01-27 00:01:05 +0000451 }
452
Ted Kremenek25cbd9f2009-02-24 00:30:21 +0000453 // Emit the relative offset into the PTH file for the spelling string.
Ted Kremenek277faca2009-01-27 00:01:05 +0000454 Emit32(E->getValue().getOffset());
Ted Kremenekb978c662009-01-08 01:17:37 +0000455 }
Ted Kremenekf2a223f2009-02-24 01:26:56 +0000456 else
Ted Kremenek25cbd9f2009-02-24 00:30:21 +0000457 Emit32(ResolveID(T.getIdentifierInfo()));
Ted Kremenek25cbd9f2009-02-24 00:30:21 +0000458
459 // Emit the offset into the original source file of this token so that we
460 // can reconstruct its SourceLocation.
Chris Lattner52c29082009-01-27 06:27:13 +0000461 Emit32(PP.getSourceManager().getFileOffset(T.getLocation()));
Ted Kremenek85888962008-10-21 00:54:44 +0000462}
463
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000464PTHEntry PTHWriter::LexTokens(Lexer& L) {
Ted Kremenek7b78b7c2009-01-19 23:13:15 +0000465 // Pad 0's so that we emit tokens to a 4-byte alignment.
466 // This speed up reading them back in.
Ted Kremeneke0ea5dc2009-02-10 01:06:17 +0000467 Pad(Out, 4);
468 Offset off = (Offset) Out.tell();
Ted Kremenekfb645b62008-12-11 23:36:38 +0000469
470 // Keep track of matching '#if' ... '#endif'.
471 typedef std::vector<std::pair<Offset, unsigned> > PPCondTable;
472 PPCondTable PPCond;
Ted Kremenekdad7b342008-12-12 18:31:09 +0000473 std::vector<unsigned> PPStartCond;
Ted Kremeneke5680f32008-12-23 01:30:52 +0000474 bool ParsingPreprocessorDirective = false;
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000475 Token Tok;
476
477 do {
478 L.LexFromRawLexer(Tok);
Ted Kremenek726080d2009-02-10 22:43:16 +0000479 NextToken:
480
Ted Kremeneke5680f32008-12-23 01:30:52 +0000481 if ((Tok.isAtStartOfLine() || Tok.is(tok::eof)) &&
482 ParsingPreprocessorDirective) {
483 // Insert an eom token into the token cache. It has the same
484 // position as the next token that is not on the same line as the
485 // preprocessor directive. Observe that we continue processing
486 // 'Tok' when we exit this branch.
487 Token Tmp = Tok;
488 Tmp.setKind(tok::eom);
489 Tmp.clearFlag(Token::StartOfLine);
490 Tmp.setIdentifierInfo(0);
Ted Kremenekb978c662009-01-08 01:17:37 +0000491 EmitToken(Tmp);
Ted Kremeneke5680f32008-12-23 01:30:52 +0000492 ParsingPreprocessorDirective = false;
493 }
494
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000495 if (Tok.is(tok::identifier)) {
496 Tok.setIdentifierInfo(PP.LookUpIdentifierInfo(Tok));
Ted Kremeneke4f6b1e2009-02-10 22:27:09 +0000497 EmitToken(Tok);
Ted Kremeneke5680f32008-12-23 01:30:52 +0000498 continue;
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000499 }
Ted Kremeneke5680f32008-12-23 01:30:52 +0000500
501 if (Tok.is(tok::hash) && Tok.isAtStartOfLine()) {
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000502 // Special processing for #include. Store the '#' token and lex
503 // the next token.
Ted Kremeneke5680f32008-12-23 01:30:52 +0000504 assert(!ParsingPreprocessorDirective);
Ted Kremenekfb645b62008-12-11 23:36:38 +0000505 Offset HashOff = (Offset) Out.tell();
Ted Kremenekb978c662009-01-08 01:17:37 +0000506 EmitToken(Tok);
Ted Kremenekdad7b342008-12-12 18:31:09 +0000507
508 // Get the next token.
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000509 L.LexFromRawLexer(Tok);
Ted Kremeneke5680f32008-12-23 01:30:52 +0000510
511 assert(!Tok.isAtStartOfLine());
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000512
513 // Did we see 'include'/'import'/'include_next'?
Ted Kremeneke4f6b1e2009-02-10 22:27:09 +0000514 if (!Tok.is(tok::identifier)) {
515 EmitToken(Tok);
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000516 continue;
Ted Kremeneke4f6b1e2009-02-10 22:27:09 +0000517 }
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000518
519 IdentifierInfo* II = PP.LookUpIdentifierInfo(Tok);
520 Tok.setIdentifierInfo(II);
521 tok::PPKeywordKind K = II->getPPKeywordID();
522
Ted Kremeneke5680f32008-12-23 01:30:52 +0000523 assert(K != tok::pp_not_keyword);
524 ParsingPreprocessorDirective = true;
525
526 switch (K) {
527 default:
528 break;
529 case tok::pp_include:
530 case tok::pp_import:
531 case tok::pp_include_next: {
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000532 // Save the 'include' token.
Ted Kremenekb978c662009-01-08 01:17:37 +0000533 EmitToken(Tok);
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000534 // Lex the next token as an include string.
535 L.setParsingPreprocessorDirective(true);
536 L.LexIncludeFilename(Tok);
537 L.setParsingPreprocessorDirective(false);
Ted Kremeneke5680f32008-12-23 01:30:52 +0000538 assert(!Tok.isAtStartOfLine());
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000539 if (Tok.is(tok::identifier))
540 Tok.setIdentifierInfo(PP.LookUpIdentifierInfo(Tok));
Ted Kremeneke5680f32008-12-23 01:30:52 +0000541
542 break;
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000543 }
Ted Kremeneke5680f32008-12-23 01:30:52 +0000544 case tok::pp_if:
545 case tok::pp_ifdef:
546 case tok::pp_ifndef: {
Ted Kremeneke4f6b1e2009-02-10 22:27:09 +0000547 // Add an entry for '#if' and friends. We initially set the target
548 // index to 0. This will get backpatched when we hit #endif.
Ted Kremenekfb645b62008-12-11 23:36:38 +0000549 PPStartCond.push_back(PPCond.size());
Ted Kremenekdad7b342008-12-12 18:31:09 +0000550 PPCond.push_back(std::make_pair(HashOff, 0U));
Ted Kremeneke5680f32008-12-23 01:30:52 +0000551 break;
Ted Kremenekfb645b62008-12-11 23:36:38 +0000552 }
Ted Kremeneke5680f32008-12-23 01:30:52 +0000553 case tok::pp_endif: {
Ted Kremenekfb645b62008-12-11 23:36:38 +0000554 // Add an entry for '#endif'. We set the target table index to itself.
Ted Kremenekdad7b342008-12-12 18:31:09 +0000555 // This will later be set to zero when emitting to the PTH file. We
556 // use 0 for uninitialized indices because that is easier to debug.
Ted Kremenekfb645b62008-12-11 23:36:38 +0000557 unsigned index = PPCond.size();
Ted Kremenekfb645b62008-12-11 23:36:38 +0000558 // Backpatch the opening '#if' entry.
Ted Kremenekdad7b342008-12-12 18:31:09 +0000559 assert(!PPStartCond.empty());
560 assert(PPCond.size() > PPStartCond.back());
Ted Kremenekfb645b62008-12-11 23:36:38 +0000561 assert(PPCond[PPStartCond.back()].second == 0);
562 PPCond[PPStartCond.back()].second = index;
563 PPStartCond.pop_back();
Ted Kremenekdad7b342008-12-12 18:31:09 +0000564 // Add the new entry to PPCond.
565 PPCond.push_back(std::make_pair(HashOff, index));
Ted Kremenek726080d2009-02-10 22:43:16 +0000566 EmitToken(Tok);
567
568 // Some files have gibberish on the same line as '#endif'.
569 // Discard these tokens.
570 do L.LexFromRawLexer(Tok); while (!Tok.is(tok::eof) &&
571 !Tok.isAtStartOfLine());
572 // We have the next token in hand.
573 // Don't immediately lex the next one.
574 goto NextToken;
Ted Kremenekfb645b62008-12-11 23:36:38 +0000575 }
Ted Kremeneke5680f32008-12-23 01:30:52 +0000576 case tok::pp_elif:
577 case tok::pp_else: {
578 // Add an entry for #elif or #else.
Ted Kremenekdad7b342008-12-12 18:31:09 +0000579 // This serves as both a closing and opening of a conditional block.
580 // This means that its entry will get backpatched later.
Ted Kremenekfb645b62008-12-11 23:36:38 +0000581 unsigned index = PPCond.size();
Ted Kremenekfb645b62008-12-11 23:36:38 +0000582 // Backpatch the previous '#if' entry.
Ted Kremenekdad7b342008-12-12 18:31:09 +0000583 assert(!PPStartCond.empty());
584 assert(PPCond.size() > PPStartCond.back());
Ted Kremenekfb645b62008-12-11 23:36:38 +0000585 assert(PPCond[PPStartCond.back()].second == 0);
586 PPCond[PPStartCond.back()].second = index;
587 PPStartCond.pop_back();
588 // Now add '#elif' as a new block opening.
Ted Kremenekdad7b342008-12-12 18:31:09 +0000589 PPCond.push_back(std::make_pair(HashOff, 0U));
590 PPStartCond.push_back(index);
Ted Kremeneke5680f32008-12-23 01:30:52 +0000591 break;
592 }
Ted Kremenekfb645b62008-12-11 23:36:38 +0000593 }
Ted Kremeneke4f6b1e2009-02-10 22:27:09 +0000594 }
595
596 EmitToken(Tok);
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000597 }
Ted Kremeneke4f6b1e2009-02-10 22:27:09 +0000598 while (Tok.isNot(tok::eof));
Ted Kremenekb978c662009-01-08 01:17:37 +0000599
Ted Kremenekdad7b342008-12-12 18:31:09 +0000600 assert(PPStartCond.empty() && "Error: imblanced preprocessor conditionals.");
Ted Kremenekb978c662009-01-08 01:17:37 +0000601
Ted Kremenekfb645b62008-12-11 23:36:38 +0000602 // Next write out PPCond.
603 Offset PPCondOff = (Offset) Out.tell();
Ted Kremenekdad7b342008-12-12 18:31:09 +0000604
605 // Write out the size of PPCond so that clients can identifer empty tables.
Ted Kremenekb978c662009-01-08 01:17:37 +0000606 Emit32(PPCond.size());
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000607
Ted Kremenekdad7b342008-12-12 18:31:09 +0000608 for (unsigned i = 0, e = PPCond.size(); i!=e; ++i) {
Ted Kremenekb978c662009-01-08 01:17:37 +0000609 Emit32(PPCond[i].first - off);
Ted Kremenekdad7b342008-12-12 18:31:09 +0000610 uint32_t x = PPCond[i].second;
611 assert(x != 0 && "PPCond entry not backpatched.");
612 // Emit zero for #endifs. This allows us to do checking when
613 // we read the PTH file back in.
Ted Kremenekb978c662009-01-08 01:17:37 +0000614 Emit32(x == i ? 0 : x);
Ted Kremenekfb645b62008-12-11 23:36:38 +0000615 }
616
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000617 return PTHEntry(off, PPCondOff);
Ted Kremenekbe295332009-01-08 02:44:06 +0000618}
619
Ted Kremenek277faca2009-01-27 00:01:05 +0000620Offset PTHWriter::EmitCachedSpellings() {
621 // Write each cached strings to the PTH file.
622 Offset SpellingsOff = Out.tell();
623
624 for (std::vector<llvm::StringMapEntry<OffsetOpt>*>::iterator
625 I = StrEntries.begin(), E = StrEntries.end(); I!=E; ++I) {
Ted Kremenekbe295332009-01-08 02:44:06 +0000626
Ted Kremenek277faca2009-01-27 00:01:05 +0000627 const char* data = (*I)->getKeyData();
628 EmitBuf(data, data + (*I)->getKeyLength());
629 Emit8('\0');
Ted Kremenekbe295332009-01-08 02:44:06 +0000630 }
631
Ted Kremenek277faca2009-01-27 00:01:05 +0000632 return SpellingsOff;
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000633}
Ted Kremenek85888962008-10-21 00:54:44 +0000634
Ted Kremenekd5cded42009-03-19 22:10:38 +0000635void PTHWriter::GeneratePTH(const std::string *MainFile) {
Ted Kremeneke1b64982009-01-26 21:43:14 +0000636 // Generate the prologue.
637 Out << "cfe-pth";
Ted Kremenek67d15052009-01-26 21:50:21 +0000638 Emit32(PTHManager::Version);
Ted Kremeneka4bd8eb2009-02-11 23:34:32 +0000639
640 // Leave 4 words for the prologue.
641 Offset PrologueOffset = Out.tell();
642 for (unsigned i = 0; i < 4 * sizeof(uint32_t); ++i) Emit8(0);
Ted Kremenekd5cded42009-03-19 22:10:38 +0000643
644 // Write the name of the MainFile.
645 if (MainFile && MainFile->length() > 0) {
646 Emit16(MainFile->length());
647 EmitBuf(&((*MainFile)[0]), &((*MainFile)[0]) + MainFile->length());
648 }
649 else {
650 // String with 0 bytes.
651 Emit16(0);
652 }
653 Emit8(0);
Ted Kremeneke1b64982009-01-26 21:43:14 +0000654
Ted Kremenek85888962008-10-21 00:54:44 +0000655 // Iterate over all the files in SourceManager. Create a lexer
656 // for each file and cache the tokens.
Chris Lattnerc6fe32a2009-01-17 03:48:08 +0000657 SourceManager &SM = PP.getSourceManager();
658 const LangOptions &LOpts = PP.getLangOptions();
Ted Kremenek85888962008-10-21 00:54:44 +0000659
Chris Lattnerc6fe32a2009-01-17 03:48:08 +0000660 for (SourceManager::fileinfo_iterator I = SM.fileinfo_begin(),
661 E = SM.fileinfo_end(); I != E; ++I) {
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000662 const SrcMgr::ContentCache &C = *I->second;
Chris Lattnerc6fe32a2009-01-17 03:48:08 +0000663 const FileEntry *FE = C.Entry;
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000664
665 // FIXME: Handle files with non-absolute paths.
666 llvm::sys::Path P(FE->getName());
667 if (!P.isAbsolute())
668 continue;
Ted Kremenek85888962008-10-21 00:54:44 +0000669
Chris Lattnerc6fe32a2009-01-17 03:48:08 +0000670 const llvm::MemoryBuffer *B = C.getBuffer();
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000671 if (!B) continue;
Ted Kremenekfb645b62008-12-11 23:36:38 +0000672
Chris Lattner2b2453a2009-01-17 06:22:33 +0000673 FileID FID = SM.createFileID(FE, SourceLocation(), SrcMgr::C_User);
Chris Lattner025c3a62009-01-17 07:35:14 +0000674 Lexer L(FID, SM, LOpts);
Ted Kremenekd8c02922009-02-10 22:16:22 +0000675 PM.insert(FE, LexTokens(L));
Daniel Dunbar31309ab2008-11-26 02:18:33 +0000676 }
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000677
678 // Write out the identifier table.
Ted Kremenekf1de4642009-02-11 16:06:55 +0000679 const std::pair<Offset,Offset>& IdTableOff = EmitIdentifierTable();
Ted Kremenek85888962008-10-21 00:54:44 +0000680
Ted Kremenekbe295332009-01-08 02:44:06 +0000681 // Write out the cached strings table.
Ted Kremenek277faca2009-01-27 00:01:05 +0000682 Offset SpellingOff = EmitCachedSpellings();
Ted Kremenekbe295332009-01-08 02:44:06 +0000683
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000684 // Write out the file table.
Ted Kremenekb978c662009-01-08 01:17:37 +0000685 Offset FileTableOff = EmitFileTable();
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000686
Ted Kremeneka4bd8eb2009-02-11 23:34:32 +0000687 // Finally, write the prologue.
688 Out.seek(PrologueOffset);
Ted Kremenekb978c662009-01-08 01:17:37 +0000689 Emit32(IdTableOff.first);
Ted Kremenekf1de4642009-02-11 16:06:55 +0000690 Emit32(IdTableOff.second);
Ted Kremenekb978c662009-01-08 01:17:37 +0000691 Emit32(FileTableOff);
Ted Kremenek277faca2009-01-27 00:01:05 +0000692 Emit32(SpellingOff);
Ted Kremenekb978c662009-01-08 01:17:37 +0000693}
694
695void clang::CacheTokens(Preprocessor& PP, const std::string& OutFile) {
Ted Kremenekb978c662009-01-08 01:17:37 +0000696 // Open up the PTH file.
697 std::string ErrMsg;
698 llvm::raw_fd_ostream Out(OutFile.c_str(), true, ErrMsg);
699
700 if (!ErrMsg.empty()) {
701 llvm::errs() << "PTH error: " << ErrMsg << "\n";
702 return;
703 }
Ted Kremenekd5cded42009-03-19 22:10:38 +0000704
705 // Get the name of the main file.
706 const SourceManager &SrcMgr = PP.getSourceManager();
707 const FileEntry *MainFile = SrcMgr.getFileEntryForID(SrcMgr.getMainFileID());
708 llvm::sys::Path MainFilePath(MainFile->getName());
709 std::string MainFileName;
710
711 if (!MainFilePath.isAbsolute()) {
712 llvm::sys::Path P = llvm::sys::Path::GetCurrentDirectory();
713 P.appendComponent(MainFilePath.toString());
714 MainFileName = P.toString();
715 }
716 else {
717 MainFileName = MainFilePath.toString();
718 }
Ted Kremenekad6ce5c2009-02-13 22:07:44 +0000719
720 // Create the PTHWriter.
Ted Kremenekb978c662009-01-08 01:17:37 +0000721 PTHWriter PW(Out, PP);
Ted Kremenekad6ce5c2009-02-13 22:07:44 +0000722
723 // Install the 'stat' system call listener in the FileManager.
724 PP.getFileManager().setStatCache(PW.createStatListener());
725
726 // Lex through the entire file. This will populate SourceManager with
727 // all of the header information.
728 Token Tok;
729 PP.EnterMainSourceFile();
730 do { PP.Lex(Tok); } while (Tok.isNot(tok::eof));
731
Ted Kremenekad6ce5c2009-02-13 22:07:44 +0000732 // Generate the PTH file.
733 PP.getFileManager().setStatCache(0);
Ted Kremenekd5cded42009-03-19 22:10:38 +0000734 PW.GeneratePTH(&MainFileName);
Ted Kremenek85888962008-10-21 00:54:44 +0000735}
Ted Kremeneke0ea5dc2009-02-10 01:06:17 +0000736
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000737//===----------------------------------------------------------------------===//
738
739namespace {
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000740class VISIBILITY_HIDDEN PTHIdKey {
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000741public:
742 const IdentifierInfo* II;
743 uint32_t FileOffset;
744};
745
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000746class VISIBILITY_HIDDEN PTHIdentifierTableTrait {
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000747public:
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000748 typedef PTHIdKey* key_type;
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000749 typedef key_type key_type_ref;
750
751 typedef uint32_t data_type;
752 typedef data_type data_type_ref;
753
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000754 static unsigned ComputeHash(PTHIdKey* key) {
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000755 return BernsteinHash(key->II->getName());
756 }
757
758 static std::pair<unsigned,unsigned>
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000759 EmitKeyDataLength(llvm::raw_ostream& Out, const PTHIdKey* key, uint32_t) {
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000760 unsigned n = strlen(key->II->getName()) + 1;
761 ::Emit16(Out, n);
762 return std::make_pair(n, sizeof(uint32_t));
763 }
764
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000765 static void EmitKey(llvm::raw_fd_ostream& Out, PTHIdKey* key, unsigned n) {
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000766 // Record the location of the key data. This is used when generating
767 // the mapping from persistent IDs to strings.
768 key->FileOffset = Out.tell();
769 Out.write(key->II->getName(), n);
770 }
771
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000772 static void EmitData(llvm::raw_ostream& Out, PTHIdKey*, uint32_t pID,
Ted Kremenek337edcd2009-02-12 03:26:59 +0000773 unsigned) {
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000774 ::Emit32(Out, pID);
775 }
776};
777} // end anonymous namespace
778
779/// EmitIdentifierTable - Emits two tables to the PTH file. The first is
780/// a hashtable mapping from identifier strings to persistent IDs. The second
781/// is a straight table mapping from persistent IDs to string data (the
782/// keys of the first table).
783///
784std::pair<Offset,Offset> PTHWriter::EmitIdentifierTable() {
785 // Build two maps:
786 // (1) an inverse map from persistent IDs -> (IdentifierInfo*,Offset)
787 // (2) a map from (IdentifierInfo*, Offset)* -> persistent IDs
788
789 // Note that we use 'calloc', so all the bytes are 0.
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000790 PTHIdKey* IIDMap = (PTHIdKey*) calloc(idcount, sizeof(PTHIdKey));
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000791
792 // Create the hashtable.
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000793 OnDiskChainedHashTableGenerator<PTHIdentifierTableTrait> IIOffMap;
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000794
795 // Generate mapping from persistent IDs -> IdentifierInfo*.
796 for (IDMap::iterator I=IM.begin(), E=IM.end(); I!=E; ++I) {
797 // Decrement by 1 because we are using a vector for the lookup and
798 // 0 is reserved for NULL.
799 assert(I->second > 0);
800 assert(I->second-1 < idcount);
801 unsigned idx = I->second-1;
802
803 // Store the mapping from persistent ID to IdentifierInfo*
804 IIDMap[idx].II = I->first;
805
806 // Store the reverse mapping in a hashtable.
807 IIOffMap.insert(&IIDMap[idx], I->second);
808 }
809
810 // Write out the inverse map first. This causes the PCIDKey entries to
811 // record PTH file offsets for the string data. This is used to write
812 // the second table.
813 Offset StringTableOffset = IIOffMap.Emit(Out);
814
815 // Now emit the table mapping from persistent IDs to PTH file offsets.
816 Offset IDOff = Out.tell();
817 Emit32(idcount); // Emit the number of identifiers.
818 for (unsigned i = 0 ; i < idcount; ++i) Emit32(IIDMap[i].FileOffset);
819
820 // Finally, release the inverse map.
821 free(IIDMap);
822
823 return std::make_pair(IDOff, StringTableOffset);
824}