blob: 02d6cec8fcab37228b871cf0b8053cdb138d2cdc [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
Eli Friedmanb09f6e12009-05-19 04:14:29 +000015#include "clang/Frontend/Utils.h"
Ted Kremenek85888962008-10-21 00:54:44 +000016#include "clang/Basic/FileManager.h"
17#include "clang/Basic/SourceManager.h"
18#include "clang/Basic/IdentifierTable.h"
19#include "clang/Basic/Diagnostic.h"
Douglas Gregor9378ba42009-04-20 07:08:21 +000020#include "clang/Basic/OnDiskHashTable.h"
Ted Kremenek85888962008-10-21 00:54:44 +000021#include "clang/Lex/Lexer.h"
22#include "clang/Lex/Preprocessor.h"
Daniel Dunbardf5a2372009-10-17 20:43:58 +000023#include "llvm/ADT/StringExtras.h"
24#include "llvm/ADT/StringMap.h"
Ted Kremenek85888962008-10-21 00:54:44 +000025#include "llvm/Support/MemoryBuffer.h"
26#include "llvm/Support/raw_ostream.h"
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +000027#include "llvm/System/Path.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;
Douglas Gregor9378ba42009-04-20 07:08:21 +000035using namespace clang::io;
Ted Kremenekf0e1f792009-02-10 01:14:45 +000036
37//===----------------------------------------------------------------------===//
38// PTH-specific stuff.
39//===----------------------------------------------------------------------===//
40
Ted Kremenekbe295332009-01-08 02:44:06 +000041namespace {
Benjamin Kramerbd218282009-11-28 10:07:24 +000042class PTHEntry {
Mike Stump1eb44332009-09-09 15:08:12 +000043 Offset TokenData, PPCondData;
Ted Kremenekbe295332009-01-08 02:44:06 +000044
Mike Stump1eb44332009-09-09 15:08:12 +000045public:
Ted Kremeneka4b44dd2009-02-13 19:13:46 +000046 PTHEntry() {}
Ted Kremenekbe295332009-01-08 02:44:06 +000047
Ted Kremeneka4b44dd2009-02-13 19:13:46 +000048 PTHEntry(Offset td, Offset ppcd)
Ted Kremenek277faca2009-01-27 00:01:05 +000049 : TokenData(td), PPCondData(ppcd) {}
Mike Stump1eb44332009-09-09 15:08:12 +000050
51 Offset getTokenOffset() const { return TokenData; }
Ted Kremenekbe295332009-01-08 02:44:06 +000052 Offset getPPCondTableOffset() const { return PPCondData; }
Ted Kremenek277faca2009-01-27 00:01:05 +000053};
Mike Stump1eb44332009-09-09 15:08:12 +000054
55
Benjamin Kramerbd218282009-11-28 10:07:24 +000056class PTHEntryKeyVariant {
Ted Kremenekad6ce5c2009-02-13 22:07:44 +000057 union { const FileEntry* FE; const char* Path; };
Ted Kremeneka4b44dd2009-02-13 19:13:46 +000058 enum { IsFE = 0x1, IsDE = 0x2, IsNoExist = 0x0 } Kind;
Ted Kremenekad6ce5c2009-02-13 22:07:44 +000059 struct stat *StatBuf;
Ted Kremenekd8c02922009-02-10 22:16:22 +000060public:
Ted Kremenekad6ce5c2009-02-13 22:07:44 +000061 PTHEntryKeyVariant(const FileEntry *fe)
62 : FE(fe), Kind(IsFE), StatBuf(0) {}
63
64 PTHEntryKeyVariant(struct stat* statbuf, const char* path)
65 : Path(path), Kind(IsDE), StatBuf(new struct stat(*statbuf)) {}
66
Kovarththanan Rajaratnamc8e5eac2010-03-07 11:21:46 +000067 explicit PTHEntryKeyVariant(const char* path)
Ted Kremenekad6ce5c2009-02-13 22:07:44 +000068 : Path(path), Kind(IsNoExist), StatBuf(0) {}
Mike Stump1eb44332009-09-09 15:08:12 +000069
Ted Kremenekad6ce5c2009-02-13 22:07:44 +000070 bool isFile() const { return Kind == IsFE; }
Mike Stump1eb44332009-09-09 15:08:12 +000071
Daniel Dunbardf5a2372009-10-17 20:43:58 +000072 llvm::StringRef getString() const {
Ted Kremenekad6ce5c2009-02-13 22:07:44 +000073 return Kind == IsFE ? FE->getName() : Path;
Ted Kremeneka4b44dd2009-02-13 19:13:46 +000074 }
Mike Stump1eb44332009-09-09 15:08:12 +000075
Ted Kremeneka4b44dd2009-02-13 19:13:46 +000076 unsigned getKind() const { return (unsigned) Kind; }
Mike Stump1eb44332009-09-09 15:08:12 +000077
Ted Kremeneka4b44dd2009-02-13 19:13:46 +000078 void EmitData(llvm::raw_ostream& Out) {
79 switch (Kind) {
Mike Stumpb7166332010-01-20 02:03:14 +000080 case IsFE:
81 // Emit stat information.
82 ::Emit32(Out, FE->getInode());
83 ::Emit32(Out, FE->getDevice());
84 ::Emit16(Out, FE->getFileMode());
85 ::Emit64(Out, FE->getModificationTime());
86 ::Emit64(Out, FE->getSize());
87 break;
88 case IsDE:
89 // Emit stat information.
90 ::Emit32(Out, (uint32_t) StatBuf->st_ino);
91 ::Emit32(Out, (uint32_t) StatBuf->st_dev);
92 ::Emit16(Out, (uint16_t) StatBuf->st_mode);
93 ::Emit64(Out, (uint64_t) StatBuf->st_mtime);
94 ::Emit64(Out, (uint64_t) StatBuf->st_size);
95 delete StatBuf;
96 break;
97 default:
98 break;
Ted Kremeneka4b44dd2009-02-13 19:13:46 +000099 }
100 }
Mike Stump1eb44332009-09-09 15:08:12 +0000101
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000102 unsigned getRepresentationLength() const {
Ted Kremenekad6ce5c2009-02-13 22:07:44 +0000103 return Kind == IsNoExist ? 0 : 4 + 4 + 2 + 8 + 8;
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000104 }
105};
Mike Stump1eb44332009-09-09 15:08:12 +0000106
Benjamin Kramerbd218282009-11-28 10:07:24 +0000107class FileEntryPTHEntryInfo {
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000108public:
109 typedef PTHEntryKeyVariant key_type;
Ted Kremenekd8c02922009-02-10 22:16:22 +0000110 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +0000111
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000112 typedef PTHEntry data_type;
113 typedef const PTHEntry& data_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +0000114
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000115 static unsigned ComputeHash(PTHEntryKeyVariant V) {
Daniel Dunbardf5a2372009-10-17 20:43:58 +0000116 return llvm::HashString(V.getString());
Ted Kremenekd8c02922009-02-10 22:16:22 +0000117 }
Mike Stump1eb44332009-09-09 15:08:12 +0000118
119 static std::pair<unsigned,unsigned>
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000120 EmitKeyDataLength(llvm::raw_ostream& Out, PTHEntryKeyVariant V,
121 const PTHEntry& E) {
Ted Kremenekd8c02922009-02-10 22:16:22 +0000122
Daniel Dunbardf5a2372009-10-17 20:43:58 +0000123 unsigned n = V.getString().size() + 1 + 1;
Ted Kremenekd8c02922009-02-10 22:16:22 +0000124 ::Emit16(Out, n);
Mike Stump1eb44332009-09-09 15:08:12 +0000125
Ted Kremenekad6ce5c2009-02-13 22:07:44 +0000126 unsigned m = V.getRepresentationLength() + (V.isFile() ? 4 + 4 : 0);
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000127 ::Emit8(Out, m);
128
129 return std::make_pair(n, m);
Ted Kremenekd8c02922009-02-10 22:16:22 +0000130 }
Mike Stump1eb44332009-09-09 15:08:12 +0000131
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000132 static void EmitKey(llvm::raw_ostream& Out, PTHEntryKeyVariant V, unsigned n){
133 // Emit the entry kind.
134 ::Emit8(Out, (unsigned) V.getKind());
135 // Emit the string.
Daniel Dunbardf5a2372009-10-17 20:43:58 +0000136 Out.write(V.getString().data(), n - 1);
Ted Kremenekd8c02922009-02-10 22:16:22 +0000137 }
Mike Stump1eb44332009-09-09 15:08:12 +0000138
139 static void EmitData(llvm::raw_ostream& Out, PTHEntryKeyVariant V,
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000140 const PTHEntry& E, unsigned) {
141
142
143 // For file entries emit the offsets into the PTH file for token data
144 // and the preprocessor blocks table.
Ted Kremenekad6ce5c2009-02-13 22:07:44 +0000145 if (V.isFile()) {
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000146 ::Emit32(Out, E.getTokenOffset());
147 ::Emit32(Out, E.getPPCondTableOffset());
148 }
Mike Stump1eb44332009-09-09 15:08:12 +0000149
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000150 // Emit any other data associated with the key (i.e., stat information).
151 V.EmitData(Out);
Mike Stump1eb44332009-09-09 15:08:12 +0000152 }
Ted Kremenekd8c02922009-02-10 22:16:22 +0000153};
Mike Stump1eb44332009-09-09 15:08:12 +0000154
Ted Kremenek277faca2009-01-27 00:01:05 +0000155class OffsetOpt {
156 bool valid;
157 Offset off;
158public:
159 OffsetOpt() : valid(false) {}
160 bool hasOffset() const { return valid; }
161 Offset getOffset() const { assert(valid); return off; }
162 void setOffset(Offset o) { off = o; valid = true; }
Ted Kremenekbe295332009-01-08 02:44:06 +0000163};
164} // end anonymous namespace
165
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000166typedef OnDiskChainedHashTableGenerator<FileEntryPTHEntryInfo> PTHMap;
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000167typedef llvm::DenseMap<const IdentifierInfo*,uint32_t> IDMap;
Ted Kremenek277faca2009-01-27 00:01:05 +0000168typedef llvm::StringMap<OffsetOpt, llvm::BumpPtrAllocator> CachedStrsTy;
Ted Kremenek85888962008-10-21 00:54:44 +0000169
Ted Kremenekb978c662009-01-08 01:17:37 +0000170namespace {
Benjamin Kramerbd218282009-11-28 10:07:24 +0000171class PTHWriter {
Ted Kremenekb978c662009-01-08 01:17:37 +0000172 IDMap IM;
173 llvm::raw_fd_ostream& Out;
174 Preprocessor& PP;
175 uint32_t idcount;
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000176 PTHMap PM;
Ted Kremenekbe295332009-01-08 02:44:06 +0000177 CachedStrsTy CachedStrs;
Ted Kremenek277faca2009-01-27 00:01:05 +0000178 Offset CurStrOffset;
179 std::vector<llvm::StringMapEntry<OffsetOpt>*> StrEntries;
Ted Kremenek8f174e12008-12-23 02:52:12 +0000180
Ted Kremenekb978c662009-01-08 01:17:37 +0000181 //// Get the persistent id for the given IdentifierInfo*.
182 uint32_t ResolveID(const IdentifierInfo* II);
Mike Stump1eb44332009-09-09 15:08:12 +0000183
Ted Kremenekb978c662009-01-08 01:17:37 +0000184 /// Emit a token to the PTH file.
185 void EmitToken(const Token& T);
186
Kovarththanan Rajaratnamfbcc0712010-03-05 15:40:54 +0000187 void Emit8(uint32_t V) { ::Emit8(Out, V); }
Mike Stump1eb44332009-09-09 15:08:12 +0000188
Ted Kremeneke0ea5dc2009-02-10 01:06:17 +0000189 void Emit16(uint32_t V) { ::Emit16(Out, V); }
Mike Stump1eb44332009-09-09 15:08:12 +0000190
Kovarththanan Rajaratnambe08ac72010-03-02 17:41:26 +0000191 void Emit24(uint32_t V) { ::Emit24(Out, V); }
Ted Kremenekb978c662009-01-08 01:17:37 +0000192
Ted Kremeneke0ea5dc2009-02-10 01:06:17 +0000193 void Emit32(uint32_t V) { ::Emit32(Out, V); }
194
Chris Lattnerf2390362009-03-28 00:16:20 +0000195 void EmitBuf(const char *Ptr, unsigned NumBytes) {
196 Out.write(Ptr, NumBytes);
Ted Kremenekb978c662009-01-08 01:17:37 +0000197 }
Mike Stump1eb44332009-09-09 15:08:12 +0000198
Kovarththanan Rajaratnamc3cde072010-03-14 08:35:19 +0000199 void EmitString(llvm::StringRef V) {
200 ::Emit16(Out, V.size());
201 EmitBuf(V.data(), V.size());
202 }
203
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000204 /// EmitIdentifierTable - Emits two tables to the PTH file. The first is
205 /// a hashtable mapping from identifier strings to persistent IDs.
206 /// The second is a straight table mapping from persistent IDs to string data
207 /// (the keys of the first table).
Ted Kremenekf1de4642009-02-11 16:06:55 +0000208 std::pair<Offset, Offset> EmitIdentifierTable();
Mike Stump1eb44332009-09-09 15:08:12 +0000209
Ted Kremenekf1de4642009-02-11 16:06:55 +0000210 /// EmitFileTable - Emit a table mapping from file name strings to PTH
211 /// token data.
212 Offset EmitFileTable() { return PM.Emit(Out); }
213
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000214 PTHEntry LexTokens(Lexer& L);
Ted Kremenek277faca2009-01-27 00:01:05 +0000215 Offset EmitCachedSpellings();
Ted Kremenekad6ce5c2009-02-13 22:07:44 +0000216
Ted Kremenekb978c662009-01-08 01:17:37 +0000217public:
Mike Stump1eb44332009-09-09 15:08:12 +0000218 PTHWriter(llvm::raw_fd_ostream& out, Preprocessor& pp)
Ted Kremenek277faca2009-01-27 00:01:05 +0000219 : Out(out), PP(pp), idcount(0), CurStrOffset(0) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000220
Chris Lattner52ba8702009-03-28 00:55:35 +0000221 PTHMap &getPM() { return PM; }
Kovarththanan Rajaratnam74e485e2010-03-14 07:38:15 +0000222 void GeneratePTH(const std::string &MainFile);
Ted Kremenekb978c662009-01-08 01:17:37 +0000223};
224} // end anonymous namespace
Mike Stump1eb44332009-09-09 15:08:12 +0000225
226uint32_t PTHWriter::ResolveID(const IdentifierInfo* II) {
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000227 // Null IdentifierInfo's map to the persistent ID 0.
228 if (!II)
229 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000230
Ted Kremenek85888962008-10-21 00:54:44 +0000231 IDMap::iterator I = IM.find(II);
Chris Lattnerf2390362009-03-28 00:16:20 +0000232 if (I != IM.end())
233 return I->second; // We've already added 1.
Mike Stump1eb44332009-09-09 15:08:12 +0000234
Chris Lattnerf2390362009-03-28 00:16:20 +0000235 IM[II] = ++idcount; // Pre-increment since '0' is reserved for NULL.
236 return idcount;
Ted Kremenek85888962008-10-21 00:54:44 +0000237}
238
Ted Kremenekb978c662009-01-08 01:17:37 +0000239void PTHWriter::EmitToken(const Token& T) {
Ted Kremenekf2a223f2009-02-24 01:26:56 +0000240 // Emit the token kind, flags, and length.
241 Emit32(((uint32_t) T.getKind()) | ((((uint32_t) T.getFlags())) << 8)|
242 (((uint32_t) T.getLength()) << 16));
Mike Stump1eb44332009-09-09 15:08:12 +0000243
Chris Lattnerf2390362009-03-28 00:16:20 +0000244 if (!T.isLiteral()) {
245 Emit32(ResolveID(T.getIdentifierInfo()));
246 } else {
Ted Kremenekf2a223f2009-02-24 01:26:56 +0000247 // We cache *un-cleaned* spellings. This gives us 100% fidelity with the
248 // source code.
249 const char* s = T.getLiteralData();
250 unsigned len = T.getLength();
Ted Kremenek25cbd9f2009-02-24 00:30:21 +0000251
Chris Lattner47246be2009-01-26 19:29:26 +0000252 // Get the string entry.
Ted Kremenekf2a223f2009-02-24 01:26:56 +0000253 llvm::StringMapEntry<OffsetOpt> *E = &CachedStrs.GetOrCreateValue(s, s+len);
Mike Stump1eb44332009-09-09 15:08:12 +0000254
Ted Kremenekf2a223f2009-02-24 01:26:56 +0000255 // If this is a new string entry, bump the PTH offset.
Ted Kremenek277faca2009-01-27 00:01:05 +0000256 if (!E->getValue().hasOffset()) {
257 E->getValue().setOffset(CurStrOffset);
258 StrEntries.push_back(E);
Ted Kremenekf2a223f2009-02-24 01:26:56 +0000259 CurStrOffset += len + 1;
Ted Kremenek277faca2009-01-27 00:01:05 +0000260 }
Mike Stump1eb44332009-09-09 15:08:12 +0000261
Ted Kremenek25cbd9f2009-02-24 00:30:21 +0000262 // Emit the relative offset into the PTH file for the spelling string.
Ted Kremenek277faca2009-01-27 00:01:05 +0000263 Emit32(E->getValue().getOffset());
Ted Kremenekb978c662009-01-08 01:17:37 +0000264 }
Mike Stump1eb44332009-09-09 15:08:12 +0000265
Ted Kremenek25cbd9f2009-02-24 00:30:21 +0000266 // Emit the offset into the original source file of this token so that we
267 // can reconstruct its SourceLocation.
Chris Lattner52c29082009-01-27 06:27:13 +0000268 Emit32(PP.getSourceManager().getFileOffset(T.getLocation()));
Ted Kremenek85888962008-10-21 00:54:44 +0000269}
270
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000271PTHEntry PTHWriter::LexTokens(Lexer& L) {
Ted Kremenek7b78b7c2009-01-19 23:13:15 +0000272 // Pad 0's so that we emit tokens to a 4-byte alignment.
273 // This speed up reading them back in.
Ted Kremeneke0ea5dc2009-02-10 01:06:17 +0000274 Pad(Out, 4);
275 Offset off = (Offset) Out.tell();
Mike Stump1eb44332009-09-09 15:08:12 +0000276
Ted Kremenekfb645b62008-12-11 23:36:38 +0000277 // Keep track of matching '#if' ... '#endif'.
278 typedef std::vector<std::pair<Offset, unsigned> > PPCondTable;
279 PPCondTable PPCond;
Ted Kremenekdad7b342008-12-12 18:31:09 +0000280 std::vector<unsigned> PPStartCond;
Ted Kremeneke5680f32008-12-23 01:30:52 +0000281 bool ParsingPreprocessorDirective = false;
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000282 Token Tok;
Mike Stump1eb44332009-09-09 15:08:12 +0000283
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000284 do {
285 L.LexFromRawLexer(Tok);
Ted Kremenek726080d2009-02-10 22:43:16 +0000286 NextToken:
287
Ted Kremeneke5680f32008-12-23 01:30:52 +0000288 if ((Tok.isAtStartOfLine() || Tok.is(tok::eof)) &&
289 ParsingPreprocessorDirective) {
290 // Insert an eom token into the token cache. It has the same
291 // position as the next token that is not on the same line as the
292 // preprocessor directive. Observe that we continue processing
293 // 'Tok' when we exit this branch.
294 Token Tmp = Tok;
295 Tmp.setKind(tok::eom);
296 Tmp.clearFlag(Token::StartOfLine);
297 Tmp.setIdentifierInfo(0);
Ted Kremenekb978c662009-01-08 01:17:37 +0000298 EmitToken(Tmp);
Ted Kremeneke5680f32008-12-23 01:30:52 +0000299 ParsingPreprocessorDirective = false;
300 }
Mike Stump1eb44332009-09-09 15:08:12 +0000301
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000302 if (Tok.is(tok::identifier)) {
Kovarththanan Rajaratnam65cc1e82010-03-13 08:53:33 +0000303 PP.LookUpIdentifierInfo(Tok);
Ted Kremeneke4f6b1e2009-02-10 22:27:09 +0000304 EmitToken(Tok);
Ted Kremeneke5680f32008-12-23 01:30:52 +0000305 continue;
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000306 }
Ted Kremeneke5680f32008-12-23 01:30:52 +0000307
308 if (Tok.is(tok::hash) && Tok.isAtStartOfLine()) {
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000309 // Special processing for #include. Store the '#' token and lex
310 // the next token.
Ted Kremeneke5680f32008-12-23 01:30:52 +0000311 assert(!ParsingPreprocessorDirective);
Ted Kremenekfb645b62008-12-11 23:36:38 +0000312 Offset HashOff = (Offset) Out.tell();
Ted Kremenekb978c662009-01-08 01:17:37 +0000313 EmitToken(Tok);
Ted Kremenekdad7b342008-12-12 18:31:09 +0000314
315 // Get the next token.
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000316 L.LexFromRawLexer(Tok);
Chris Lattner21356192009-04-19 07:25:40 +0000317
318 // If we see the start of line, then we had a null directive "#".
319 if (Tok.isAtStartOfLine())
320 goto NextToken;
Mike Stump1eb44332009-09-09 15:08:12 +0000321
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000322 // Did we see 'include'/'import'/'include_next'?
Chris Lattnerd4b14462009-04-19 07:15:51 +0000323 if (Tok.isNot(tok::identifier)) {
Ted Kremeneke4f6b1e2009-02-10 22:27:09 +0000324 EmitToken(Tok);
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000325 continue;
Ted Kremeneke4f6b1e2009-02-10 22:27:09 +0000326 }
Mike Stump1eb44332009-09-09 15:08:12 +0000327
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000328 IdentifierInfo* II = PP.LookUpIdentifierInfo(Tok);
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000329 tok::PPKeywordKind K = II->getPPKeywordID();
Mike Stump1eb44332009-09-09 15:08:12 +0000330
Ted Kremeneke5680f32008-12-23 01:30:52 +0000331 ParsingPreprocessorDirective = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000332
Ted Kremeneke5680f32008-12-23 01:30:52 +0000333 switch (K) {
Chris Lattneraa269c22009-04-19 07:32:03 +0000334 case tok::pp_not_keyword:
335 // Invalid directives "#foo" can occur in #if 0 blocks etc, just pass
336 // them through.
Ted Kremeneke5680f32008-12-23 01:30:52 +0000337 default:
338 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000339
Ted Kremeneke5680f32008-12-23 01:30:52 +0000340 case tok::pp_include:
341 case tok::pp_import:
Mike Stump1eb44332009-09-09 15:08:12 +0000342 case tok::pp_include_next: {
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000343 // Save the 'include' token.
Ted Kremenekb978c662009-01-08 01:17:37 +0000344 EmitToken(Tok);
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000345 // Lex the next token as an include string.
346 L.setParsingPreprocessorDirective(true);
Mike Stump1eb44332009-09-09 15:08:12 +0000347 L.LexIncludeFilename(Tok);
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000348 L.setParsingPreprocessorDirective(false);
Ted Kremeneke5680f32008-12-23 01:30:52 +0000349 assert(!Tok.isAtStartOfLine());
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000350 if (Tok.is(tok::identifier))
Kovarththanan Rajaratnam65cc1e82010-03-13 08:53:33 +0000351 PP.LookUpIdentifierInfo(Tok);
Mike Stump1eb44332009-09-09 15:08:12 +0000352
Ted Kremeneke5680f32008-12-23 01:30:52 +0000353 break;
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000354 }
Ted Kremeneke5680f32008-12-23 01:30:52 +0000355 case tok::pp_if:
356 case tok::pp_ifdef:
357 case tok::pp_ifndef: {
Ted Kremeneke4f6b1e2009-02-10 22:27:09 +0000358 // Add an entry for '#if' and friends. We initially set the target
359 // index to 0. This will get backpatched when we hit #endif.
Ted Kremenekfb645b62008-12-11 23:36:38 +0000360 PPStartCond.push_back(PPCond.size());
Ted Kremenekdad7b342008-12-12 18:31:09 +0000361 PPCond.push_back(std::make_pair(HashOff, 0U));
Ted Kremeneke5680f32008-12-23 01:30:52 +0000362 break;
Ted Kremenekfb645b62008-12-11 23:36:38 +0000363 }
Ted Kremeneke5680f32008-12-23 01:30:52 +0000364 case tok::pp_endif: {
Ted Kremenekfb645b62008-12-11 23:36:38 +0000365 // Add an entry for '#endif'. We set the target table index to itself.
Ted Kremenekdad7b342008-12-12 18:31:09 +0000366 // This will later be set to zero when emitting to the PTH file. We
367 // use 0 for uninitialized indices because that is easier to debug.
Ted Kremenekfb645b62008-12-11 23:36:38 +0000368 unsigned index = PPCond.size();
Ted Kremenekfb645b62008-12-11 23:36:38 +0000369 // Backpatch the opening '#if' entry.
Ted Kremenekdad7b342008-12-12 18:31:09 +0000370 assert(!PPStartCond.empty());
371 assert(PPCond.size() > PPStartCond.back());
Ted Kremenekfb645b62008-12-11 23:36:38 +0000372 assert(PPCond[PPStartCond.back()].second == 0);
373 PPCond[PPStartCond.back()].second = index;
Mike Stump1eb44332009-09-09 15:08:12 +0000374 PPStartCond.pop_back();
375 // Add the new entry to PPCond.
Ted Kremenekdad7b342008-12-12 18:31:09 +0000376 PPCond.push_back(std::make_pair(HashOff, index));
Ted Kremenek726080d2009-02-10 22:43:16 +0000377 EmitToken(Tok);
Mike Stump1eb44332009-09-09 15:08:12 +0000378
Ted Kremenek726080d2009-02-10 22:43:16 +0000379 // Some files have gibberish on the same line as '#endif'.
380 // Discard these tokens.
Chris Lattnerd4b14462009-04-19 07:15:51 +0000381 do
382 L.LexFromRawLexer(Tok);
383 while (Tok.isNot(tok::eof) && !Tok.isAtStartOfLine());
Ted Kremenek726080d2009-02-10 22:43:16 +0000384 // We have the next token in hand.
385 // Don't immediately lex the next one.
Mike Stump1eb44332009-09-09 15:08:12 +0000386 goto NextToken;
Ted Kremenekfb645b62008-12-11 23:36:38 +0000387 }
Ted Kremeneke5680f32008-12-23 01:30:52 +0000388 case tok::pp_elif:
389 case tok::pp_else: {
390 // Add an entry for #elif or #else.
Ted Kremenekdad7b342008-12-12 18:31:09 +0000391 // This serves as both a closing and opening of a conditional block.
392 // This means that its entry will get backpatched later.
Ted Kremenekfb645b62008-12-11 23:36:38 +0000393 unsigned index = PPCond.size();
Ted Kremenekfb645b62008-12-11 23:36:38 +0000394 // Backpatch the previous '#if' entry.
Ted Kremenekdad7b342008-12-12 18:31:09 +0000395 assert(!PPStartCond.empty());
396 assert(PPCond.size() > PPStartCond.back());
Ted Kremenekfb645b62008-12-11 23:36:38 +0000397 assert(PPCond[PPStartCond.back()].second == 0);
398 PPCond[PPStartCond.back()].second = index;
399 PPStartCond.pop_back();
400 // Now add '#elif' as a new block opening.
Ted Kremenekdad7b342008-12-12 18:31:09 +0000401 PPCond.push_back(std::make_pair(HashOff, 0U));
402 PPStartCond.push_back(index);
Ted Kremeneke5680f32008-12-23 01:30:52 +0000403 break;
404 }
Ted Kremenekfb645b62008-12-11 23:36:38 +0000405 }
Ted Kremeneke4f6b1e2009-02-10 22:27:09 +0000406 }
Mike Stump1eb44332009-09-09 15:08:12 +0000407
Ted Kremeneke4f6b1e2009-02-10 22:27:09 +0000408 EmitToken(Tok);
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000409 }
Ted Kremeneke4f6b1e2009-02-10 22:27:09 +0000410 while (Tok.isNot(tok::eof));
Ted Kremenekb978c662009-01-08 01:17:37 +0000411
Ted Kremenekdad7b342008-12-12 18:31:09 +0000412 assert(PPStartCond.empty() && "Error: imblanced preprocessor conditionals.");
Ted Kremenekb978c662009-01-08 01:17:37 +0000413
Ted Kremenekfb645b62008-12-11 23:36:38 +0000414 // Next write out PPCond.
415 Offset PPCondOff = (Offset) Out.tell();
Ted Kremenekdad7b342008-12-12 18:31:09 +0000416
417 // Write out the size of PPCond so that clients can identifer empty tables.
Ted Kremenekb978c662009-01-08 01:17:37 +0000418 Emit32(PPCond.size());
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000419
Ted Kremenekdad7b342008-12-12 18:31:09 +0000420 for (unsigned i = 0, e = PPCond.size(); i!=e; ++i) {
Ted Kremenekb978c662009-01-08 01:17:37 +0000421 Emit32(PPCond[i].first - off);
Ted Kremenekdad7b342008-12-12 18:31:09 +0000422 uint32_t x = PPCond[i].second;
423 assert(x != 0 && "PPCond entry not backpatched.");
424 // Emit zero for #endifs. This allows us to do checking when
425 // we read the PTH file back in.
Ted Kremenekb978c662009-01-08 01:17:37 +0000426 Emit32(x == i ? 0 : x);
Ted Kremenekfb645b62008-12-11 23:36:38 +0000427 }
428
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000429 return PTHEntry(off, PPCondOff);
Ted Kremenekbe295332009-01-08 02:44:06 +0000430}
431
Ted Kremenek277faca2009-01-27 00:01:05 +0000432Offset PTHWriter::EmitCachedSpellings() {
433 // Write each cached strings to the PTH file.
434 Offset SpellingsOff = Out.tell();
Mike Stump1eb44332009-09-09 15:08:12 +0000435
Ted Kremenek277faca2009-01-27 00:01:05 +0000436 for (std::vector<llvm::StringMapEntry<OffsetOpt>*>::iterator
Chris Lattnerf2390362009-03-28 00:16:20 +0000437 I = StrEntries.begin(), E = StrEntries.end(); I!=E; ++I)
438 EmitBuf((*I)->getKeyData(), (*I)->getKeyLength()+1 /*nul included*/);
Mike Stump1eb44332009-09-09 15:08:12 +0000439
Ted Kremenek277faca2009-01-27 00:01:05 +0000440 return SpellingsOff;
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000441}
Ted Kremenek85888962008-10-21 00:54:44 +0000442
Kovarththanan Rajaratnam74e485e2010-03-14 07:38:15 +0000443void PTHWriter::GeneratePTH(const std::string &MainFile) {
Ted Kremeneke1b64982009-01-26 21:43:14 +0000444 // Generate the prologue.
445 Out << "cfe-pth";
Ted Kremenek67d15052009-01-26 21:50:21 +0000446 Emit32(PTHManager::Version);
Mike Stump1eb44332009-09-09 15:08:12 +0000447
Ted Kremeneka4bd8eb2009-02-11 23:34:32 +0000448 // Leave 4 words for the prologue.
449 Offset PrologueOffset = Out.tell();
Chris Lattnerf2390362009-03-28 00:16:20 +0000450 for (unsigned i = 0; i < 4; ++i)
451 Emit32(0);
Mike Stump1eb44332009-09-09 15:08:12 +0000452
Ted Kremenekd5cded42009-03-19 22:10:38 +0000453 // Write the name of the MainFile.
Kovarththanan Rajaratnam2e6051a2010-03-14 07:55:43 +0000454 if (!MainFile.empty()) {
Kovarththanan Rajaratnamc3cde072010-03-14 08:35:19 +0000455 EmitString(MainFile);
Chris Lattnerf2390362009-03-28 00:16:20 +0000456 } else {
Ted Kremenekd5cded42009-03-19 22:10:38 +0000457 // String with 0 bytes.
458 Emit16(0);
459 }
460 Emit8(0);
Mike Stump1eb44332009-09-09 15:08:12 +0000461
Ted Kremenek85888962008-10-21 00:54:44 +0000462 // Iterate over all the files in SourceManager. Create a lexer
463 // for each file and cache the tokens.
Chris Lattnerc6fe32a2009-01-17 03:48:08 +0000464 SourceManager &SM = PP.getSourceManager();
465 const LangOptions &LOpts = PP.getLangOptions();
Mike Stump1eb44332009-09-09 15:08:12 +0000466
Chris Lattnerc6fe32a2009-01-17 03:48:08 +0000467 for (SourceManager::fileinfo_iterator I = SM.fileinfo_begin(),
468 E = SM.fileinfo_end(); I != E; ++I) {
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000469 const SrcMgr::ContentCache &C = *I->second;
Chris Lattnerc6fe32a2009-01-17 03:48:08 +0000470 const FileEntry *FE = C.Entry;
Mike Stump1eb44332009-09-09 15:08:12 +0000471
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000472 // FIXME: Handle files with non-absolute paths.
473 llvm::sys::Path P(FE->getName());
474 if (!P.isAbsolute())
475 continue;
Ted Kremenek85888962008-10-21 00:54:44 +0000476
Douglas Gregor36c35ba2010-03-16 00:35:39 +0000477 const llvm::MemoryBuffer *B = C.getBuffer(PP.getDiagnostics());
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000478 if (!B) continue;
Ted Kremenekfb645b62008-12-11 23:36:38 +0000479
Chris Lattner2b2453a2009-01-17 06:22:33 +0000480 FileID FID = SM.createFileID(FE, SourceLocation(), SrcMgr::C_User);
Chris Lattner6e290142009-11-30 04:18:44 +0000481 const llvm::MemoryBuffer *FromFile = SM.getBuffer(FID);
482 Lexer L(FID, FromFile, SM, LOpts);
Ted Kremenekd8c02922009-02-10 22:16:22 +0000483 PM.insert(FE, LexTokens(L));
Daniel Dunbar31309ab2008-11-26 02:18:33 +0000484 }
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000485
486 // Write out the identifier table.
Chris Lattnerf2390362009-03-28 00:16:20 +0000487 const std::pair<Offset,Offset> &IdTableOff = EmitIdentifierTable();
Mike Stump1eb44332009-09-09 15:08:12 +0000488
Ted Kremenekbe295332009-01-08 02:44:06 +0000489 // Write out the cached strings table.
Ted Kremenek277faca2009-01-27 00:01:05 +0000490 Offset SpellingOff = EmitCachedSpellings();
Mike Stump1eb44332009-09-09 15:08:12 +0000491
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000492 // Write out the file table.
Mike Stump1eb44332009-09-09 15:08:12 +0000493 Offset FileTableOff = EmitFileTable();
494
Ted Kremeneka4bd8eb2009-02-11 23:34:32 +0000495 // Finally, write the prologue.
496 Out.seek(PrologueOffset);
Ted Kremenekb978c662009-01-08 01:17:37 +0000497 Emit32(IdTableOff.first);
Ted Kremenekf1de4642009-02-11 16:06:55 +0000498 Emit32(IdTableOff.second);
Ted Kremenekb978c662009-01-08 01:17:37 +0000499 Emit32(FileTableOff);
Ted Kremenek277faca2009-01-27 00:01:05 +0000500 Emit32(SpellingOff);
Ted Kremenekb978c662009-01-08 01:17:37 +0000501}
502
Chris Lattner52ba8702009-03-28 00:55:35 +0000503namespace {
504/// StatListener - A simple "interpose" object used to monitor stat calls
505/// invoked by FileManager while processing the original sources used
506/// as input to PTH generation. StatListener populates the PTHWriter's
507/// file map with stat information for directories as well as negative stats.
508/// Stat information for files are populated elsewhere.
509class StatListener : public StatSysCallCache {
510 PTHMap &PM;
511public:
512 StatListener(PTHMap &pm) : PM(pm) {}
513 ~StatListener() {}
Mike Stump1eb44332009-09-09 15:08:12 +0000514
Chris Lattner52ba8702009-03-28 00:55:35 +0000515 int stat(const char *path, struct stat *buf) {
Douglas Gregor52e71082009-10-16 18:18:30 +0000516 int result = StatSysCallCache::stat(path, buf);
Mike Stump1eb44332009-09-09 15:08:12 +0000517
Chris Lattner52ba8702009-03-28 00:55:35 +0000518 if (result != 0) // Failed 'stat'.
Kovarththanan Rajaratnamc8e5eac2010-03-07 11:21:46 +0000519 PM.insert(PTHEntryKeyVariant(path), PTHEntry());
Chris Lattner52ba8702009-03-28 00:55:35 +0000520 else if (S_ISDIR(buf->st_mode)) {
521 // Only cache directories with absolute paths.
522 if (!llvm::sys::Path(path).isAbsolute())
523 return result;
Mike Stump1eb44332009-09-09 15:08:12 +0000524
Chris Lattner52ba8702009-03-28 00:55:35 +0000525 PM.insert(PTHEntryKeyVariant(buf, path), PTHEntry());
526 }
Mike Stump1eb44332009-09-09 15:08:12 +0000527
Chris Lattner52ba8702009-03-28 00:55:35 +0000528 return result;
529 }
530};
531} // end anonymous namespace
532
533
Eli Friedmanf54fce82009-05-19 01:02:07 +0000534void clang::CacheTokens(Preprocessor &PP, llvm::raw_fd_ostream* OS) {
Ted Kremenekd5cded42009-03-19 22:10:38 +0000535 // Get the name of the main file.
536 const SourceManager &SrcMgr = PP.getSourceManager();
537 const FileEntry *MainFile = SrcMgr.getFileEntryForID(SrcMgr.getMainFileID());
538 llvm::sys::Path MainFilePath(MainFile->getName());
Mike Stump1eb44332009-09-09 15:08:12 +0000539
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +0000540 MainFilePath.makeAbsolute();
Ted Kremenekad6ce5c2009-02-13 22:07:44 +0000541
542 // Create the PTHWriter.
Eli Friedmanf54fce82009-05-19 01:02:07 +0000543 PTHWriter PW(*OS, PP);
Mike Stump1eb44332009-09-09 15:08:12 +0000544
Ted Kremenekad6ce5c2009-02-13 22:07:44 +0000545 // Install the 'stat' system call listener in the FileManager.
Douglas Gregor52e71082009-10-16 18:18:30 +0000546 StatListener *StatCache = new StatListener(PW.getPM());
547 PP.getFileManager().addStatCache(StatCache, /*AtBeginning=*/true);
Mike Stump1eb44332009-09-09 15:08:12 +0000548
Ted Kremenekad6ce5c2009-02-13 22:07:44 +0000549 // Lex through the entire file. This will populate SourceManager with
550 // all of the header information.
551 Token Tok;
552 PP.EnterMainSourceFile();
553 do { PP.Lex(Tok); } while (Tok.isNot(tok::eof));
Mike Stump1eb44332009-09-09 15:08:12 +0000554
Ted Kremenekad6ce5c2009-02-13 22:07:44 +0000555 // Generate the PTH file.
Douglas Gregor52e71082009-10-16 18:18:30 +0000556 PP.getFileManager().removeStatCache(StatCache);
Kovarththanan Rajaratnam74e485e2010-03-14 07:38:15 +0000557 PW.GeneratePTH(MainFilePath.str());
Ted Kremenek85888962008-10-21 00:54:44 +0000558}
Ted Kremeneke0ea5dc2009-02-10 01:06:17 +0000559
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000560//===----------------------------------------------------------------------===//
561
Kovarththanan Rajaratnam48673e62010-03-14 06:48:05 +0000562namespace {
Chris Lattnerf2390362009-03-28 00:16:20 +0000563class PTHIdKey {
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000564public:
565 const IdentifierInfo* II;
566 uint32_t FileOffset;
567};
568
Benjamin Kramerbd218282009-11-28 10:07:24 +0000569class PTHIdentifierTableTrait {
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000570public:
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000571 typedef PTHIdKey* key_type;
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000572 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +0000573
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000574 typedef uint32_t data_type;
575 typedef data_type data_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +0000576
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000577 static unsigned ComputeHash(PTHIdKey* key) {
Daniel Dunbar01eb9b92009-10-18 21:17:35 +0000578 return llvm::HashString(key->II->getName());
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000579 }
Mike Stump1eb44332009-09-09 15:08:12 +0000580
581 static std::pair<unsigned,unsigned>
582 EmitKeyDataLength(llvm::raw_ostream& Out, const PTHIdKey* key, uint32_t) {
Daniel Dunbare013d682009-10-18 20:26:12 +0000583 unsigned n = key->II->getLength() + 1;
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000584 ::Emit16(Out, n);
585 return std::make_pair(n, sizeof(uint32_t));
586 }
Mike Stump1eb44332009-09-09 15:08:12 +0000587
Douglas Gregor3432fa52009-04-20 07:36:26 +0000588 static void EmitKey(llvm::raw_ostream& Out, PTHIdKey* key, unsigned n) {
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000589 // Record the location of the key data. This is used when generating
590 // the mapping from persistent IDs to strings.
591 key->FileOffset = Out.tell();
Daniel Dunbare013d682009-10-18 20:26:12 +0000592 Out.write(key->II->getNameStart(), n);
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000593 }
Mike Stump1eb44332009-09-09 15:08:12 +0000594
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000595 static void EmitData(llvm::raw_ostream& Out, PTHIdKey*, uint32_t pID,
Ted Kremenek337edcd2009-02-12 03:26:59 +0000596 unsigned) {
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000597 ::Emit32(Out, pID);
Mike Stump1eb44332009-09-09 15:08:12 +0000598 }
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000599};
600} // end anonymous namespace
601
602/// EmitIdentifierTable - Emits two tables to the PTH file. The first is
603/// a hashtable mapping from identifier strings to persistent IDs. The second
604/// is a straight table mapping from persistent IDs to string data (the
605/// keys of the first table).
606///
607std::pair<Offset,Offset> PTHWriter::EmitIdentifierTable() {
608 // Build two maps:
609 // (1) an inverse map from persistent IDs -> (IdentifierInfo*,Offset)
610 // (2) a map from (IdentifierInfo*, Offset)* -> persistent IDs
611
612 // Note that we use 'calloc', so all the bytes are 0.
Chris Lattnerf2390362009-03-28 00:16:20 +0000613 PTHIdKey *IIDMap = (PTHIdKey*)calloc(idcount, sizeof(PTHIdKey));
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000614
615 // Create the hashtable.
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000616 OnDiskChainedHashTableGenerator<PTHIdentifierTableTrait> IIOffMap;
Mike Stump1eb44332009-09-09 15:08:12 +0000617
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000618 // Generate mapping from persistent IDs -> IdentifierInfo*.
Chris Lattnerf2390362009-03-28 00:16:20 +0000619 for (IDMap::iterator I = IM.begin(), E = IM.end(); I != E; ++I) {
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000620 // Decrement by 1 because we are using a vector for the lookup and
621 // 0 is reserved for NULL.
622 assert(I->second > 0);
623 assert(I->second-1 < idcount);
624 unsigned idx = I->second-1;
Mike Stump1eb44332009-09-09 15:08:12 +0000625
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000626 // Store the mapping from persistent ID to IdentifierInfo*
627 IIDMap[idx].II = I->first;
Mike Stump1eb44332009-09-09 15:08:12 +0000628
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000629 // Store the reverse mapping in a hashtable.
630 IIOffMap.insert(&IIDMap[idx], I->second);
631 }
Mike Stump1eb44332009-09-09 15:08:12 +0000632
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000633 // Write out the inverse map first. This causes the PCIDKey entries to
634 // record PTH file offsets for the string data. This is used to write
635 // the second table.
636 Offset StringTableOffset = IIOffMap.Emit(Out);
Mike Stump1eb44332009-09-09 15:08:12 +0000637
638 // Now emit the table mapping from persistent IDs to PTH file offsets.
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000639 Offset IDOff = Out.tell();
640 Emit32(idcount); // Emit the number of identifiers.
Chris Lattnerf2390362009-03-28 00:16:20 +0000641 for (unsigned i = 0 ; i < idcount; ++i)
642 Emit32(IIDMap[i].FileOffset);
Mike Stump1eb44332009-09-09 15:08:12 +0000643
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000644 // Finally, release the inverse map.
645 free(IIDMap);
Mike Stump1eb44332009-09-09 15:08:12 +0000646
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000647 return std::make_pair(IDOff, StringTableOffset);
648}