blob: 8195445fe0e4842a61e68f384d26144dda17439a [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/Diagnostic.h"
Chris Lattner10e286a2010-11-23 19:19:34 +000017#include "clang/Basic/FileManager.h"
18#include "clang/Basic/FileSystemStatCache.h"
19#include "clang/Basic/IdentifierTable.h"
Douglas Gregor9378ba42009-04-20 07:08:21 +000020#include "clang/Basic/OnDiskHashTable.h"
Chris Lattner10e286a2010-11-23 19:19:34 +000021#include "clang/Basic/SourceManager.h"
Ted Kremenek85888962008-10-21 00:54:44 +000022#include "clang/Lex/Lexer.h"
23#include "clang/Lex/Preprocessor.h"
Daniel Dunbardf5a2372009-10-17 20:43:58 +000024#include "llvm/ADT/StringExtras.h"
25#include "llvm/ADT/StringMap.h"
Michael J. Spencerfbfd1802010-12-21 16:45:57 +000026#include "llvm/Support/FileSystem.h"
Ted Kremenek85888962008-10-21 00:54:44 +000027#include "llvm/Support/MemoryBuffer.h"
28#include "llvm/Support/raw_ostream.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000029#include "llvm/Support/Path.h"
Ted Kremenek85888962008-10-21 00:54:44 +000030
Cedric Venetea684e62009-02-14 16:15:20 +000031// FIXME: put this somewhere else?
32#ifndef S_ISDIR
33#define S_ISDIR(x) (((x)&_S_IFDIR)!=0)
34#endif
35
Ted Kremenek85888962008-10-21 00:54:44 +000036using namespace clang;
Douglas Gregor9378ba42009-04-20 07:08:21 +000037using namespace clang::io;
Ted Kremenekf0e1f792009-02-10 01:14:45 +000038
39//===----------------------------------------------------------------------===//
40// PTH-specific stuff.
41//===----------------------------------------------------------------------===//
42
Ted Kremenekbe295332009-01-08 02:44:06 +000043namespace {
Benjamin Kramerbd218282009-11-28 10:07:24 +000044class PTHEntry {
Mike Stump1eb44332009-09-09 15:08:12 +000045 Offset TokenData, PPCondData;
Ted Kremenekbe295332009-01-08 02:44:06 +000046
Mike Stump1eb44332009-09-09 15:08:12 +000047public:
Ted Kremeneka4b44dd2009-02-13 19:13:46 +000048 PTHEntry() {}
Ted Kremenekbe295332009-01-08 02:44:06 +000049
Ted Kremeneka4b44dd2009-02-13 19:13:46 +000050 PTHEntry(Offset td, Offset ppcd)
Ted Kremenek277faca2009-01-27 00:01:05 +000051 : TokenData(td), PPCondData(ppcd) {}
Mike Stump1eb44332009-09-09 15:08:12 +000052
53 Offset getTokenOffset() const { return TokenData; }
Ted Kremenekbe295332009-01-08 02:44:06 +000054 Offset getPPCondTableOffset() const { return PPCondData; }
Ted Kremenek277faca2009-01-27 00:01:05 +000055};
Mike Stump1eb44332009-09-09 15:08:12 +000056
57
Benjamin Kramerbd218282009-11-28 10:07:24 +000058class PTHEntryKeyVariant {
Ted Kremenekad6ce5c2009-02-13 22:07:44 +000059 union { const FileEntry* FE; const char* Path; };
Ted Kremeneka4b44dd2009-02-13 19:13:46 +000060 enum { IsFE = 0x1, IsDE = 0x2, IsNoExist = 0x0 } Kind;
Ted Kremenekad6ce5c2009-02-13 22:07:44 +000061 struct stat *StatBuf;
Ted Kremenekd8c02922009-02-10 22:16:22 +000062public:
Ted Kremenekad6ce5c2009-02-13 22:07:44 +000063 PTHEntryKeyVariant(const FileEntry *fe)
64 : FE(fe), Kind(IsFE), StatBuf(0) {}
65
66 PTHEntryKeyVariant(struct stat* statbuf, const char* path)
67 : Path(path), Kind(IsDE), StatBuf(new struct stat(*statbuf)) {}
68
Kovarththanan Rajaratnamc8e5eac2010-03-07 11:21:46 +000069 explicit PTHEntryKeyVariant(const char* path)
Ted Kremenekad6ce5c2009-02-13 22:07:44 +000070 : Path(path), Kind(IsNoExist), StatBuf(0) {}
Mike Stump1eb44332009-09-09 15:08:12 +000071
Ted Kremenekad6ce5c2009-02-13 22:07:44 +000072 bool isFile() const { return Kind == IsFE; }
Mike Stump1eb44332009-09-09 15:08:12 +000073
Chris Lattner5f9e2722011-07-23 10:55:15 +000074 StringRef getString() const {
Ted Kremenekad6ce5c2009-02-13 22:07:44 +000075 return Kind == IsFE ? FE->getName() : Path;
Ted Kremeneka4b44dd2009-02-13 19:13:46 +000076 }
Mike Stump1eb44332009-09-09 15:08:12 +000077
Ted Kremeneka4b44dd2009-02-13 19:13:46 +000078 unsigned getKind() const { return (unsigned) Kind; }
Mike Stump1eb44332009-09-09 15:08:12 +000079
Chris Lattner5f9e2722011-07-23 10:55:15 +000080 void EmitData(raw_ostream& Out) {
Ted Kremeneka4b44dd2009-02-13 19:13:46 +000081 switch (Kind) {
Mike Stumpb7166332010-01-20 02:03:14 +000082 case IsFE:
83 // Emit stat information.
84 ::Emit32(Out, FE->getInode());
85 ::Emit32(Out, FE->getDevice());
86 ::Emit16(Out, FE->getFileMode());
87 ::Emit64(Out, FE->getModificationTime());
88 ::Emit64(Out, FE->getSize());
89 break;
90 case IsDE:
91 // Emit stat information.
92 ::Emit32(Out, (uint32_t) StatBuf->st_ino);
93 ::Emit32(Out, (uint32_t) StatBuf->st_dev);
94 ::Emit16(Out, (uint16_t) StatBuf->st_mode);
95 ::Emit64(Out, (uint64_t) StatBuf->st_mtime);
96 ::Emit64(Out, (uint64_t) StatBuf->st_size);
97 delete StatBuf;
98 break;
99 default:
100 break;
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000101 }
102 }
Mike Stump1eb44332009-09-09 15:08:12 +0000103
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000104 unsigned getRepresentationLength() const {
Ted Kremenekad6ce5c2009-02-13 22:07:44 +0000105 return Kind == IsNoExist ? 0 : 4 + 4 + 2 + 8 + 8;
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000106 }
107};
Mike Stump1eb44332009-09-09 15:08:12 +0000108
Benjamin Kramerbd218282009-11-28 10:07:24 +0000109class FileEntryPTHEntryInfo {
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000110public:
111 typedef PTHEntryKeyVariant key_type;
Ted Kremenekd8c02922009-02-10 22:16:22 +0000112 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +0000113
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000114 typedef PTHEntry data_type;
115 typedef const PTHEntry& data_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +0000116
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000117 static unsigned ComputeHash(PTHEntryKeyVariant V) {
Daniel Dunbardf5a2372009-10-17 20:43:58 +0000118 return llvm::HashString(V.getString());
Ted Kremenekd8c02922009-02-10 22:16:22 +0000119 }
Mike Stump1eb44332009-09-09 15:08:12 +0000120
121 static std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +0000122 EmitKeyDataLength(raw_ostream& Out, PTHEntryKeyVariant V,
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000123 const PTHEntry& E) {
Ted Kremenekd8c02922009-02-10 22:16:22 +0000124
Daniel Dunbardf5a2372009-10-17 20:43:58 +0000125 unsigned n = V.getString().size() + 1 + 1;
Ted Kremenekd8c02922009-02-10 22:16:22 +0000126 ::Emit16(Out, n);
Mike Stump1eb44332009-09-09 15:08:12 +0000127
Ted Kremenekad6ce5c2009-02-13 22:07:44 +0000128 unsigned m = V.getRepresentationLength() + (V.isFile() ? 4 + 4 : 0);
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000129 ::Emit8(Out, m);
130
131 return std::make_pair(n, m);
Ted Kremenekd8c02922009-02-10 22:16:22 +0000132 }
Mike Stump1eb44332009-09-09 15:08:12 +0000133
Chris Lattner5f9e2722011-07-23 10:55:15 +0000134 static void EmitKey(raw_ostream& Out, PTHEntryKeyVariant V, unsigned n){
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000135 // Emit the entry kind.
136 ::Emit8(Out, (unsigned) V.getKind());
137 // Emit the string.
Daniel Dunbardf5a2372009-10-17 20:43:58 +0000138 Out.write(V.getString().data(), n - 1);
Ted Kremenekd8c02922009-02-10 22:16:22 +0000139 }
Mike Stump1eb44332009-09-09 15:08:12 +0000140
Chris Lattner5f9e2722011-07-23 10:55:15 +0000141 static void EmitData(raw_ostream& Out, PTHEntryKeyVariant V,
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000142 const PTHEntry& E, unsigned) {
143
144
145 // For file entries emit the offsets into the PTH file for token data
146 // and the preprocessor blocks table.
Ted Kremenekad6ce5c2009-02-13 22:07:44 +0000147 if (V.isFile()) {
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000148 ::Emit32(Out, E.getTokenOffset());
149 ::Emit32(Out, E.getPPCondTableOffset());
150 }
Mike Stump1eb44332009-09-09 15:08:12 +0000151
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000152 // Emit any other data associated with the key (i.e., stat information).
153 V.EmitData(Out);
Mike Stump1eb44332009-09-09 15:08:12 +0000154 }
Ted Kremenekd8c02922009-02-10 22:16:22 +0000155};
Mike Stump1eb44332009-09-09 15:08:12 +0000156
Ted Kremenek277faca2009-01-27 00:01:05 +0000157class OffsetOpt {
158 bool valid;
159 Offset off;
160public:
161 OffsetOpt() : valid(false) {}
162 bool hasOffset() const { return valid; }
163 Offset getOffset() const { assert(valid); return off; }
164 void setOffset(Offset o) { off = o; valid = true; }
Ted Kremenekbe295332009-01-08 02:44:06 +0000165};
166} // end anonymous namespace
167
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000168typedef OnDiskChainedHashTableGenerator<FileEntryPTHEntryInfo> PTHMap;
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 {
Kovarththanan Rajaratnam622ab502010-03-18 07:45:30 +0000172 typedef llvm::DenseMap<const IdentifierInfo*,uint32_t> IDMap;
173 typedef llvm::StringMap<OffsetOpt, llvm::BumpPtrAllocator> CachedStrsTy;
174
Ted Kremenekb978c662009-01-08 01:17:37 +0000175 IDMap IM;
176 llvm::raw_fd_ostream& Out;
177 Preprocessor& PP;
178 uint32_t idcount;
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000179 PTHMap PM;
Ted Kremenekbe295332009-01-08 02:44:06 +0000180 CachedStrsTy CachedStrs;
Ted Kremenek277faca2009-01-27 00:01:05 +0000181 Offset CurStrOffset;
182 std::vector<llvm::StringMapEntry<OffsetOpt>*> StrEntries;
Ted Kremenek8f174e12008-12-23 02:52:12 +0000183
Ted Kremenekb978c662009-01-08 01:17:37 +0000184 //// Get the persistent id for the given IdentifierInfo*.
185 uint32_t ResolveID(const IdentifierInfo* II);
Mike Stump1eb44332009-09-09 15:08:12 +0000186
Ted Kremenekb978c662009-01-08 01:17:37 +0000187 /// Emit a token to the PTH file.
188 void EmitToken(const Token& T);
189
Kovarththanan Rajaratnamfbcc0712010-03-05 15:40:54 +0000190 void Emit8(uint32_t V) { ::Emit8(Out, V); }
Mike Stump1eb44332009-09-09 15:08:12 +0000191
Ted Kremeneke0ea5dc2009-02-10 01:06:17 +0000192 void Emit16(uint32_t V) { ::Emit16(Out, V); }
Mike Stump1eb44332009-09-09 15:08:12 +0000193
Ted Kremeneke0ea5dc2009-02-10 01:06:17 +0000194 void Emit32(uint32_t V) { ::Emit32(Out, V); }
195
Chris Lattnerf2390362009-03-28 00:16:20 +0000196 void EmitBuf(const char *Ptr, unsigned NumBytes) {
197 Out.write(Ptr, NumBytes);
Ted Kremenekb978c662009-01-08 01:17:37 +0000198 }
Mike Stump1eb44332009-09-09 15:08:12 +0000199
Chris Lattner5f9e2722011-07-23 10:55:15 +0000200 void EmitString(StringRef V) {
Kovarththanan Rajaratnamc3cde072010-03-14 08:35:19 +0000201 ::Emit16(Out, V.size());
202 EmitBuf(V.data(), V.size());
203 }
204
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000205 /// EmitIdentifierTable - Emits two tables to the PTH file. The first is
206 /// a hashtable mapping from identifier strings to persistent IDs.
207 /// The second is a straight table mapping from persistent IDs to string data
208 /// (the keys of the first table).
Ted Kremenekf1de4642009-02-11 16:06:55 +0000209 std::pair<Offset, Offset> EmitIdentifierTable();
Mike Stump1eb44332009-09-09 15:08:12 +0000210
Ted Kremenekf1de4642009-02-11 16:06:55 +0000211 /// EmitFileTable - Emit a table mapping from file name strings to PTH
212 /// token data.
213 Offset EmitFileTable() { return PM.Emit(Out); }
214
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000215 PTHEntry LexTokens(Lexer& L);
Ted Kremenek277faca2009-01-27 00:01:05 +0000216 Offset EmitCachedSpellings();
Ted Kremenekad6ce5c2009-02-13 22:07:44 +0000217
Ted Kremenekb978c662009-01-08 01:17:37 +0000218public:
Mike Stump1eb44332009-09-09 15:08:12 +0000219 PTHWriter(llvm::raw_fd_ostream& out, Preprocessor& pp)
Ted Kremenek277faca2009-01-27 00:01:05 +0000220 : Out(out), PP(pp), idcount(0), CurStrOffset(0) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000221
Chris Lattner52ba8702009-03-28 00:55:35 +0000222 PTHMap &getPM() { return PM; }
Kovarththanan Rajaratnam74e485e2010-03-14 07:38:15 +0000223 void GeneratePTH(const std::string &MainFile);
Ted Kremenekb978c662009-01-08 01:17:37 +0000224};
225} // end anonymous namespace
Mike Stump1eb44332009-09-09 15:08:12 +0000226
227uint32_t PTHWriter::ResolveID(const IdentifierInfo* II) {
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000228 // Null IdentifierInfo's map to the persistent ID 0.
229 if (!II)
230 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000231
Ted Kremenek85888962008-10-21 00:54:44 +0000232 IDMap::iterator I = IM.find(II);
Chris Lattnerf2390362009-03-28 00:16:20 +0000233 if (I != IM.end())
234 return I->second; // We've already added 1.
Mike Stump1eb44332009-09-09 15:08:12 +0000235
Chris Lattnerf2390362009-03-28 00:16:20 +0000236 IM[II] = ++idcount; // Pre-increment since '0' is reserved for NULL.
237 return idcount;
Ted Kremenek85888962008-10-21 00:54:44 +0000238}
239
Ted Kremenekb978c662009-01-08 01:17:37 +0000240void PTHWriter::EmitToken(const Token& T) {
Ted Kremenekf2a223f2009-02-24 01:26:56 +0000241 // Emit the token kind, flags, and length.
242 Emit32(((uint32_t) T.getKind()) | ((((uint32_t) T.getFlags())) << 8)|
243 (((uint32_t) T.getLength()) << 16));
Mike Stump1eb44332009-09-09 15:08:12 +0000244
Chris Lattnerf2390362009-03-28 00:16:20 +0000245 if (!T.isLiteral()) {
246 Emit32(ResolveID(T.getIdentifierInfo()));
247 } else {
Ted Kremenekf2a223f2009-02-24 01:26:56 +0000248 // We cache *un-cleaned* spellings. This gives us 100% fidelity with the
249 // source code.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000250 StringRef s(T.getLiteralData(), T.getLength());
Ted Kremenek25cbd9f2009-02-24 00:30:21 +0000251
Chris Lattner47246be2009-01-26 19:29:26 +0000252 // Get the string entry.
Jay Foad65aa6882011-06-21 15:13:30 +0000253 llvm::StringMapEntry<OffsetOpt> *E = &CachedStrs.GetOrCreateValue(s);
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);
Jay Foad65aa6882011-06-21 15:13:30 +0000259 CurStrOffset += s.size() + 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);
Kovarththanan Rajaratnam8f0d2702010-03-18 07:18:10 +0000275 Offset TokenOff = (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) {
Peter Collingbourne84021552011-02-28 02:37:51 +0000290 // Insert an eod token into the token cache. It has the same
Ted Kremeneke5680f32008-12-23 01:30:52 +0000291 // 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;
Peter Collingbourne84021552011-02-28 02:37:51 +0000295 Tmp.setKind(tok::eod);
Ted Kremeneke5680f32008-12-23 01:30:52 +0000296 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
Abramo Bagnarac4bf2b92010-12-22 08:23:18 +0000302 if (Tok.is(tok::raw_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 Kremenekdad7b342008-12-12 18:31:09 +0000313
314 // Get the next token.
Ted Kremenek5c53f4c2010-07-27 02:58:57 +0000315 Token NextTok;
316 L.LexFromRawLexer(NextTok);
Chris Lattner21356192009-04-19 07:25:40 +0000317
Ted Kremenek5c53f4c2010-07-27 02:58:57 +0000318 // If we see the start of line, then we had a null directive "#". In
319 // this case, discard both tokens.
320 if (NextTok.isAtStartOfLine())
Chris Lattner21356192009-04-19 07:25:40 +0000321 goto NextToken;
Abramo Bagnarac4bf2b92010-12-22 08:23:18 +0000322
Ted Kremenek5c53f4c2010-07-27 02:58:57 +0000323 // The token is the start of a directive. Emit it.
324 EmitToken(Tok);
325 Tok = NextTok;
Mike Stump1eb44332009-09-09 15:08:12 +0000326
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000327 // Did we see 'include'/'import'/'include_next'?
Abramo Bagnarac4bf2b92010-12-22 08:23:18 +0000328 if (Tok.isNot(tok::raw_identifier)) {
Ted Kremeneke4f6b1e2009-02-10 22:27:09 +0000329 EmitToken(Tok);
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000330 continue;
Ted Kremeneke4f6b1e2009-02-10 22:27:09 +0000331 }
Mike Stump1eb44332009-09-09 15:08:12 +0000332
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000333 IdentifierInfo* II = PP.LookUpIdentifierInfo(Tok);
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000334 tok::PPKeywordKind K = II->getPPKeywordID();
Mike Stump1eb44332009-09-09 15:08:12 +0000335
Ted Kremeneke5680f32008-12-23 01:30:52 +0000336 ParsingPreprocessorDirective = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000337
Ted Kremeneke5680f32008-12-23 01:30:52 +0000338 switch (K) {
Chris Lattneraa269c22009-04-19 07:32:03 +0000339 case tok::pp_not_keyword:
340 // Invalid directives "#foo" can occur in #if 0 blocks etc, just pass
341 // them through.
Ted Kremeneke5680f32008-12-23 01:30:52 +0000342 default:
343 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000344
Ted Kremeneke5680f32008-12-23 01:30:52 +0000345 case tok::pp_include:
346 case tok::pp_import:
Mike Stump1eb44332009-09-09 15:08:12 +0000347 case tok::pp_include_next: {
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000348 // Save the 'include' token.
Ted Kremenekb978c662009-01-08 01:17:37 +0000349 EmitToken(Tok);
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000350 // Lex the next token as an include string.
351 L.setParsingPreprocessorDirective(true);
Mike Stump1eb44332009-09-09 15:08:12 +0000352 L.LexIncludeFilename(Tok);
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000353 L.setParsingPreprocessorDirective(false);
Ted Kremeneke5680f32008-12-23 01:30:52 +0000354 assert(!Tok.isAtStartOfLine());
Abramo Bagnarac4bf2b92010-12-22 08:23:18 +0000355 if (Tok.is(tok::raw_identifier))
Kovarththanan Rajaratnam65cc1e82010-03-13 08:53:33 +0000356 PP.LookUpIdentifierInfo(Tok);
Mike Stump1eb44332009-09-09 15:08:12 +0000357
Ted Kremeneke5680f32008-12-23 01:30:52 +0000358 break;
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000359 }
Ted Kremeneke5680f32008-12-23 01:30:52 +0000360 case tok::pp_if:
361 case tok::pp_ifdef:
362 case tok::pp_ifndef: {
Ted Kremeneke4f6b1e2009-02-10 22:27:09 +0000363 // Add an entry for '#if' and friends. We initially set the target
364 // index to 0. This will get backpatched when we hit #endif.
Ted Kremenekfb645b62008-12-11 23:36:38 +0000365 PPStartCond.push_back(PPCond.size());
Ted Kremenekdad7b342008-12-12 18:31:09 +0000366 PPCond.push_back(std::make_pair(HashOff, 0U));
Ted Kremeneke5680f32008-12-23 01:30:52 +0000367 break;
Ted Kremenekfb645b62008-12-11 23:36:38 +0000368 }
Ted Kremeneke5680f32008-12-23 01:30:52 +0000369 case tok::pp_endif: {
Ted Kremenekfb645b62008-12-11 23:36:38 +0000370 // Add an entry for '#endif'. We set the target table index to itself.
Ted Kremenekdad7b342008-12-12 18:31:09 +0000371 // This will later be set to zero when emitting to the PTH file. We
372 // use 0 for uninitialized indices because that is easier to debug.
Ted Kremenekfb645b62008-12-11 23:36:38 +0000373 unsigned index = PPCond.size();
Ted Kremenekfb645b62008-12-11 23:36:38 +0000374 // Backpatch the opening '#if' entry.
Ted Kremenekdad7b342008-12-12 18:31:09 +0000375 assert(!PPStartCond.empty());
376 assert(PPCond.size() > PPStartCond.back());
Ted Kremenekfb645b62008-12-11 23:36:38 +0000377 assert(PPCond[PPStartCond.back()].second == 0);
378 PPCond[PPStartCond.back()].second = index;
Mike Stump1eb44332009-09-09 15:08:12 +0000379 PPStartCond.pop_back();
380 // Add the new entry to PPCond.
Ted Kremenekdad7b342008-12-12 18:31:09 +0000381 PPCond.push_back(std::make_pair(HashOff, index));
Ted Kremenek726080d2009-02-10 22:43:16 +0000382 EmitToken(Tok);
Mike Stump1eb44332009-09-09 15:08:12 +0000383
Ted Kremenek726080d2009-02-10 22:43:16 +0000384 // Some files have gibberish on the same line as '#endif'.
385 // Discard these tokens.
Chris Lattnerd4b14462009-04-19 07:15:51 +0000386 do
387 L.LexFromRawLexer(Tok);
388 while (Tok.isNot(tok::eof) && !Tok.isAtStartOfLine());
Ted Kremenek726080d2009-02-10 22:43:16 +0000389 // We have the next token in hand.
390 // Don't immediately lex the next one.
Mike Stump1eb44332009-09-09 15:08:12 +0000391 goto NextToken;
Ted Kremenekfb645b62008-12-11 23:36:38 +0000392 }
Ted Kremeneke5680f32008-12-23 01:30:52 +0000393 case tok::pp_elif:
394 case tok::pp_else: {
395 // Add an entry for #elif or #else.
Ted Kremenekdad7b342008-12-12 18:31:09 +0000396 // This serves as both a closing and opening of a conditional block.
397 // This means that its entry will get backpatched later.
Ted Kremenekfb645b62008-12-11 23:36:38 +0000398 unsigned index = PPCond.size();
Ted Kremenekfb645b62008-12-11 23:36:38 +0000399 // Backpatch the previous '#if' entry.
Ted Kremenekdad7b342008-12-12 18:31:09 +0000400 assert(!PPStartCond.empty());
401 assert(PPCond.size() > PPStartCond.back());
Ted Kremenekfb645b62008-12-11 23:36:38 +0000402 assert(PPCond[PPStartCond.back()].second == 0);
403 PPCond[PPStartCond.back()].second = index;
404 PPStartCond.pop_back();
405 // Now add '#elif' as a new block opening.
Ted Kremenekdad7b342008-12-12 18:31:09 +0000406 PPCond.push_back(std::make_pair(HashOff, 0U));
407 PPStartCond.push_back(index);
Ted Kremeneke5680f32008-12-23 01:30:52 +0000408 break;
409 }
Ted Kremenekfb645b62008-12-11 23:36:38 +0000410 }
Ted Kremeneke4f6b1e2009-02-10 22:27:09 +0000411 }
Mike Stump1eb44332009-09-09 15:08:12 +0000412
Ted Kremeneke4f6b1e2009-02-10 22:27:09 +0000413 EmitToken(Tok);
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000414 }
Ted Kremeneke4f6b1e2009-02-10 22:27:09 +0000415 while (Tok.isNot(tok::eof));
Ted Kremenekb978c662009-01-08 01:17:37 +0000416
Ted Kremenekdad7b342008-12-12 18:31:09 +0000417 assert(PPStartCond.empty() && "Error: imblanced preprocessor conditionals.");
Ted Kremenekb978c662009-01-08 01:17:37 +0000418
Ted Kremenekfb645b62008-12-11 23:36:38 +0000419 // Next write out PPCond.
420 Offset PPCondOff = (Offset) Out.tell();
Ted Kremenekdad7b342008-12-12 18:31:09 +0000421
422 // Write out the size of PPCond so that clients can identifer empty tables.
Ted Kremenekb978c662009-01-08 01:17:37 +0000423 Emit32(PPCond.size());
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000424
Ted Kremenekdad7b342008-12-12 18:31:09 +0000425 for (unsigned i = 0, e = PPCond.size(); i!=e; ++i) {
Kovarththanan Rajaratnam8f0d2702010-03-18 07:18:10 +0000426 Emit32(PPCond[i].first - TokenOff);
Ted Kremenekdad7b342008-12-12 18:31:09 +0000427 uint32_t x = PPCond[i].second;
428 assert(x != 0 && "PPCond entry not backpatched.");
429 // Emit zero for #endifs. This allows us to do checking when
430 // we read the PTH file back in.
Ted Kremenekb978c662009-01-08 01:17:37 +0000431 Emit32(x == i ? 0 : x);
Ted Kremenekfb645b62008-12-11 23:36:38 +0000432 }
433
Kovarththanan Rajaratnam8f0d2702010-03-18 07:18:10 +0000434 return PTHEntry(TokenOff, PPCondOff);
Ted Kremenekbe295332009-01-08 02:44:06 +0000435}
436
Ted Kremenek277faca2009-01-27 00:01:05 +0000437Offset PTHWriter::EmitCachedSpellings() {
438 // Write each cached strings to the PTH file.
439 Offset SpellingsOff = Out.tell();
Mike Stump1eb44332009-09-09 15:08:12 +0000440
Ted Kremenek277faca2009-01-27 00:01:05 +0000441 for (std::vector<llvm::StringMapEntry<OffsetOpt>*>::iterator
Chris Lattnerf2390362009-03-28 00:16:20 +0000442 I = StrEntries.begin(), E = StrEntries.end(); I!=E; ++I)
443 EmitBuf((*I)->getKeyData(), (*I)->getKeyLength()+1 /*nul included*/);
Mike Stump1eb44332009-09-09 15:08:12 +0000444
Ted Kremenek277faca2009-01-27 00:01:05 +0000445 return SpellingsOff;
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000446}
Ted Kremenek85888962008-10-21 00:54:44 +0000447
Kovarththanan Rajaratnam74e485e2010-03-14 07:38:15 +0000448void PTHWriter::GeneratePTH(const std::string &MainFile) {
Ted Kremeneke1b64982009-01-26 21:43:14 +0000449 // Generate the prologue.
450 Out << "cfe-pth";
Ted Kremenek67d15052009-01-26 21:50:21 +0000451 Emit32(PTHManager::Version);
Mike Stump1eb44332009-09-09 15:08:12 +0000452
Ted Kremeneka4bd8eb2009-02-11 23:34:32 +0000453 // Leave 4 words for the prologue.
454 Offset PrologueOffset = Out.tell();
Chris Lattnerf2390362009-03-28 00:16:20 +0000455 for (unsigned i = 0; i < 4; ++i)
456 Emit32(0);
Mike Stump1eb44332009-09-09 15:08:12 +0000457
Ted Kremenekd5cded42009-03-19 22:10:38 +0000458 // Write the name of the MainFile.
Kovarththanan Rajaratnam2e6051a2010-03-14 07:55:43 +0000459 if (!MainFile.empty()) {
Kovarththanan Rajaratnam8f0d2702010-03-18 07:18:10 +0000460 EmitString(MainFile);
Chris Lattnerf2390362009-03-28 00:16:20 +0000461 } else {
Ted Kremenekd5cded42009-03-19 22:10:38 +0000462 // String with 0 bytes.
463 Emit16(0);
464 }
465 Emit8(0);
Mike Stump1eb44332009-09-09 15:08:12 +0000466
Ted Kremenek85888962008-10-21 00:54:44 +0000467 // Iterate over all the files in SourceManager. Create a lexer
468 // for each file and cache the tokens.
Chris Lattnerc6fe32a2009-01-17 03:48:08 +0000469 SourceManager &SM = PP.getSourceManager();
470 const LangOptions &LOpts = PP.getLangOptions();
Mike Stump1eb44332009-09-09 15:08:12 +0000471
Chris Lattnerc6fe32a2009-01-17 03:48:08 +0000472 for (SourceManager::fileinfo_iterator I = SM.fileinfo_begin(),
473 E = SM.fileinfo_end(); I != E; ++I) {
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000474 const SrcMgr::ContentCache &C = *I->second;
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +0000475 const FileEntry *FE = C.OrigEntry;
Mike Stump1eb44332009-09-09 15:08:12 +0000476
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000477 // FIXME: Handle files with non-absolute paths.
Michael J. Spencer256053b2010-12-17 21:22:22 +0000478 if (llvm::sys::path::is_relative(FE->getName()))
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000479 continue;
Ted Kremenek85888962008-10-21 00:54:44 +0000480
Chris Lattnere127a0d2010-04-20 20:35:58 +0000481 const llvm::MemoryBuffer *B = C.getBuffer(PP.getDiagnostics(), SM);
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000482 if (!B) continue;
Ted Kremenekfb645b62008-12-11 23:36:38 +0000483
Chris Lattner2b2453a2009-01-17 06:22:33 +0000484 FileID FID = SM.createFileID(FE, SourceLocation(), SrcMgr::C_User);
Chris Lattner6e290142009-11-30 04:18:44 +0000485 const llvm::MemoryBuffer *FromFile = SM.getBuffer(FID);
486 Lexer L(FID, FromFile, SM, LOpts);
Ted Kremenekd8c02922009-02-10 22:16:22 +0000487 PM.insert(FE, LexTokens(L));
Daniel Dunbar31309ab2008-11-26 02:18:33 +0000488 }
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000489
490 // Write out the identifier table.
Chris Lattnerf2390362009-03-28 00:16:20 +0000491 const std::pair<Offset,Offset> &IdTableOff = EmitIdentifierTable();
Mike Stump1eb44332009-09-09 15:08:12 +0000492
Ted Kremenekbe295332009-01-08 02:44:06 +0000493 // Write out the cached strings table.
Ted Kremenek277faca2009-01-27 00:01:05 +0000494 Offset SpellingOff = EmitCachedSpellings();
Mike Stump1eb44332009-09-09 15:08:12 +0000495
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000496 // Write out the file table.
Mike Stump1eb44332009-09-09 15:08:12 +0000497 Offset FileTableOff = EmitFileTable();
498
Ted Kremeneka4bd8eb2009-02-11 23:34:32 +0000499 // Finally, write the prologue.
500 Out.seek(PrologueOffset);
Ted Kremenekb978c662009-01-08 01:17:37 +0000501 Emit32(IdTableOff.first);
Ted Kremenekf1de4642009-02-11 16:06:55 +0000502 Emit32(IdTableOff.second);
Ted Kremenekb978c662009-01-08 01:17:37 +0000503 Emit32(FileTableOff);
Ted Kremenek277faca2009-01-27 00:01:05 +0000504 Emit32(SpellingOff);
Ted Kremenekb978c662009-01-08 01:17:37 +0000505}
506
Chris Lattner52ba8702009-03-28 00:55:35 +0000507namespace {
508/// StatListener - A simple "interpose" object used to monitor stat calls
509/// invoked by FileManager while processing the original sources used
510/// as input to PTH generation. StatListener populates the PTHWriter's
511/// file map with stat information for directories as well as negative stats.
512/// Stat information for files are populated elsewhere.
Chris Lattner10e286a2010-11-23 19:19:34 +0000513class StatListener : public FileSystemStatCache {
Chris Lattner52ba8702009-03-28 00:55:35 +0000514 PTHMap &PM;
515public:
516 StatListener(PTHMap &pm) : PM(pm) {}
517 ~StatListener() {}
Mike Stump1eb44332009-09-09 15:08:12 +0000518
Chris Lattner898a0612010-11-23 21:17:56 +0000519 LookupResult getStat(const char *Path, struct stat &StatBuf,
520 int *FileDescriptor) {
521 LookupResult Result = statChained(Path, StatBuf, FileDescriptor);
Mike Stump1eb44332009-09-09 15:08:12 +0000522
Chris Lattnerd6f61112010-11-23 20:05:15 +0000523 if (Result == CacheMissing) // Failed 'stat'.
Chris Lattner10e286a2010-11-23 19:19:34 +0000524 PM.insert(PTHEntryKeyVariant(Path), PTHEntry());
525 else if (S_ISDIR(StatBuf.st_mode)) {
Chris Lattner52ba8702009-03-28 00:55:35 +0000526 // Only cache directories with absolute paths.
Michael J. Spencer256053b2010-12-17 21:22:22 +0000527 if (llvm::sys::path::is_relative(Path))
Chris Lattner10e286a2010-11-23 19:19:34 +0000528 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +0000529
Chris Lattner10e286a2010-11-23 19:19:34 +0000530 PM.insert(PTHEntryKeyVariant(&StatBuf, Path), PTHEntry());
Chris Lattner52ba8702009-03-28 00:55:35 +0000531 }
Mike Stump1eb44332009-09-09 15:08:12 +0000532
Chris Lattner10e286a2010-11-23 19:19:34 +0000533 return Result;
Chris Lattner52ba8702009-03-28 00:55:35 +0000534 }
535};
536} // end anonymous namespace
537
538
Eli Friedmanf54fce82009-05-19 01:02:07 +0000539void clang::CacheTokens(Preprocessor &PP, llvm::raw_fd_ostream* OS) {
Ted Kremenekd5cded42009-03-19 22:10:38 +0000540 // Get the name of the main file.
541 const SourceManager &SrcMgr = PP.getSourceManager();
542 const FileEntry *MainFile = SrcMgr.getFileEntryForID(SrcMgr.getMainFileID());
Michael J. Spencerfbfd1802010-12-21 16:45:57 +0000543 llvm::SmallString<128> MainFilePath(MainFile->getName());
Mike Stump1eb44332009-09-09 15:08:12 +0000544
Michael J. Spencerfbfd1802010-12-21 16:45:57 +0000545 llvm::sys::fs::make_absolute(MainFilePath);
Ted Kremenekad6ce5c2009-02-13 22:07:44 +0000546
547 // Create the PTHWriter.
Eli Friedmanf54fce82009-05-19 01:02:07 +0000548 PTHWriter PW(*OS, PP);
Mike Stump1eb44332009-09-09 15:08:12 +0000549
Ted Kremenekad6ce5c2009-02-13 22:07:44 +0000550 // Install the 'stat' system call listener in the FileManager.
Douglas Gregor52e71082009-10-16 18:18:30 +0000551 StatListener *StatCache = new StatListener(PW.getPM());
552 PP.getFileManager().addStatCache(StatCache, /*AtBeginning=*/true);
Mike Stump1eb44332009-09-09 15:08:12 +0000553
Ted Kremenekad6ce5c2009-02-13 22:07:44 +0000554 // Lex through the entire file. This will populate SourceManager with
555 // all of the header information.
556 Token Tok;
Chris Lattnere127a0d2010-04-20 20:35:58 +0000557 PP.EnterMainSourceFile();
Ted Kremenekad6ce5c2009-02-13 22:07:44 +0000558 do { PP.Lex(Tok); } while (Tok.isNot(tok::eof));
Mike Stump1eb44332009-09-09 15:08:12 +0000559
Ted Kremenekad6ce5c2009-02-13 22:07:44 +0000560 // Generate the PTH file.
Douglas Gregor52e71082009-10-16 18:18:30 +0000561 PP.getFileManager().removeStatCache(StatCache);
Kovarththanan Rajaratnam74e485e2010-03-14 07:38:15 +0000562 PW.GeneratePTH(MainFilePath.str());
Ted Kremenek85888962008-10-21 00:54:44 +0000563}
Ted Kremeneke0ea5dc2009-02-10 01:06:17 +0000564
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000565//===----------------------------------------------------------------------===//
566
Kovarththanan Rajaratnam48673e62010-03-14 06:48:05 +0000567namespace {
Chris Lattnerf2390362009-03-28 00:16:20 +0000568class PTHIdKey {
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000569public:
570 const IdentifierInfo* II;
571 uint32_t FileOffset;
572};
573
Benjamin Kramerbd218282009-11-28 10:07:24 +0000574class PTHIdentifierTableTrait {
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000575public:
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000576 typedef PTHIdKey* key_type;
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000577 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +0000578
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000579 typedef uint32_t data_type;
580 typedef data_type data_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +0000581
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000582 static unsigned ComputeHash(PTHIdKey* key) {
Daniel Dunbar01eb9b92009-10-18 21:17:35 +0000583 return llvm::HashString(key->II->getName());
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000584 }
Mike Stump1eb44332009-09-09 15:08:12 +0000585
586 static std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +0000587 EmitKeyDataLength(raw_ostream& Out, const PTHIdKey* key, uint32_t) {
Daniel Dunbare013d682009-10-18 20:26:12 +0000588 unsigned n = key->II->getLength() + 1;
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000589 ::Emit16(Out, n);
590 return std::make_pair(n, sizeof(uint32_t));
591 }
Mike Stump1eb44332009-09-09 15:08:12 +0000592
Chris Lattner5f9e2722011-07-23 10:55:15 +0000593 static void EmitKey(raw_ostream& Out, PTHIdKey* key, unsigned n) {
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000594 // Record the location of the key data. This is used when generating
595 // the mapping from persistent IDs to strings.
596 key->FileOffset = Out.tell();
Daniel Dunbare013d682009-10-18 20:26:12 +0000597 Out.write(key->II->getNameStart(), n);
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000598 }
Mike Stump1eb44332009-09-09 15:08:12 +0000599
Chris Lattner5f9e2722011-07-23 10:55:15 +0000600 static void EmitData(raw_ostream& Out, PTHIdKey*, uint32_t pID,
Ted Kremenek337edcd2009-02-12 03:26:59 +0000601 unsigned) {
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000602 ::Emit32(Out, pID);
Mike Stump1eb44332009-09-09 15:08:12 +0000603 }
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000604};
605} // end anonymous namespace
606
607/// EmitIdentifierTable - Emits two tables to the PTH file. The first is
608/// a hashtable mapping from identifier strings to persistent IDs. The second
609/// is a straight table mapping from persistent IDs to string data (the
610/// keys of the first table).
611///
612std::pair<Offset,Offset> PTHWriter::EmitIdentifierTable() {
613 // Build two maps:
614 // (1) an inverse map from persistent IDs -> (IdentifierInfo*,Offset)
615 // (2) a map from (IdentifierInfo*, Offset)* -> persistent IDs
616
617 // Note that we use 'calloc', so all the bytes are 0.
Chris Lattnerf2390362009-03-28 00:16:20 +0000618 PTHIdKey *IIDMap = (PTHIdKey*)calloc(idcount, sizeof(PTHIdKey));
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000619
620 // Create the hashtable.
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000621 OnDiskChainedHashTableGenerator<PTHIdentifierTableTrait> IIOffMap;
Mike Stump1eb44332009-09-09 15:08:12 +0000622
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000623 // Generate mapping from persistent IDs -> IdentifierInfo*.
Chris Lattnerf2390362009-03-28 00:16:20 +0000624 for (IDMap::iterator I = IM.begin(), E = IM.end(); I != E; ++I) {
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000625 // Decrement by 1 because we are using a vector for the lookup and
626 // 0 is reserved for NULL.
627 assert(I->second > 0);
628 assert(I->second-1 < idcount);
629 unsigned idx = I->second-1;
Mike Stump1eb44332009-09-09 15:08:12 +0000630
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000631 // Store the mapping from persistent ID to IdentifierInfo*
632 IIDMap[idx].II = I->first;
Mike Stump1eb44332009-09-09 15:08:12 +0000633
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000634 // Store the reverse mapping in a hashtable.
635 IIOffMap.insert(&IIDMap[idx], I->second);
636 }
Mike Stump1eb44332009-09-09 15:08:12 +0000637
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000638 // Write out the inverse map first. This causes the PCIDKey entries to
639 // record PTH file offsets for the string data. This is used to write
640 // the second table.
641 Offset StringTableOffset = IIOffMap.Emit(Out);
Mike Stump1eb44332009-09-09 15:08:12 +0000642
643 // Now emit the table mapping from persistent IDs to PTH file offsets.
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000644 Offset IDOff = Out.tell();
645 Emit32(idcount); // Emit the number of identifiers.
Chris Lattnerf2390362009-03-28 00:16:20 +0000646 for (unsigned i = 0 ; i < idcount; ++i)
647 Emit32(IIDMap[i].FileOffset);
Mike Stump1eb44332009-09-09 15:08:12 +0000648
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000649 // Finally, release the inverse map.
650 free(IIDMap);
Mike Stump1eb44332009-09-09 15:08:12 +0000651
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000652 return std::make_pair(IDOff, StringTableOffset);
653}