blob: 0c30b049579d5792a9dce7f62f4ffffe0cd4da3c [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"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000028#include "llvm/Support/Path.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000029#include "llvm/Support/raw_ostream.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;
Rafael Espindola0fda0f72013-08-01 21:42:11 +000061 FileData *Data;
62
Ted Kremenekd8c02922009-02-10 22:16:22 +000063public:
Rafael Espindola0fda0f72013-08-01 21:42:11 +000064 PTHEntryKeyVariant(const FileEntry *fe) : FE(fe), Kind(IsFE), Data(0) {}
Ted Kremenekad6ce5c2009-02-13 22:07:44 +000065
Rafael Espindola0fda0f72013-08-01 21:42:11 +000066 PTHEntryKeyVariant(FileData *Data, const char *path)
67 : Path(path), Kind(IsDE), Data(new FileData(*Data)) {}
Ted Kremenekad6ce5c2009-02-13 22:07:44 +000068
Rafael Espindola0fda0f72013-08-01 21:42:11 +000069 explicit PTHEntryKeyVariant(const char *path)
70 : Path(path), Kind(IsNoExist), Data(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) {
Rafael Espindola0fda0f72013-08-01 21:42:11 +000082 case IsFE: {
Mike Stumpb7166332010-01-20 02:03:14 +000083 // Emit stat information.
Rafael Espindola0fda0f72013-08-01 21:42:11 +000084 llvm::sys::fs::UniqueID UID = FE->getUniqueID();
85 ::Emit64(Out, UID.getFile());
86 ::Emit64(Out, UID.getDevice());
Mike Stumpb7166332010-01-20 02:03:14 +000087 ::Emit64(Out, FE->getModificationTime());
88 ::Emit64(Out, FE->getSize());
Rafael Espindola0fda0f72013-08-01 21:42:11 +000089 } break;
Mike Stumpb7166332010-01-20 02:03:14 +000090 case IsDE:
91 // Emit stat information.
Rafael Espindola0fda0f72013-08-01 21:42:11 +000092 ::Emit64(Out, Data->UniqueID.getFile());
93 ::Emit64(Out, Data->UniqueID.getDevice());
94 ::Emit64(Out, Data->ModTime);
95 ::Emit64(Out, Data->Size);
96 delete Data;
Mike Stumpb7166332010-01-20 02:03:14 +000097 break;
98 default:
99 break;
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000100 }
101 }
Mike Stump1eb44332009-09-09 15:08:12 +0000102
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000103 unsigned getRepresentationLength() const {
Ted Kremenekad6ce5c2009-02-13 22:07:44 +0000104 return Kind == IsNoExist ? 0 : 4 + 4 + 2 + 8 + 8;
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000105 }
106};
Mike Stump1eb44332009-09-09 15:08:12 +0000107
Benjamin Kramerbd218282009-11-28 10:07:24 +0000108class FileEntryPTHEntryInfo {
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000109public:
110 typedef PTHEntryKeyVariant key_type;
Ted Kremenekd8c02922009-02-10 22:16:22 +0000111 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +0000112
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000113 typedef PTHEntry data_type;
114 typedef const PTHEntry& data_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +0000115
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000116 static unsigned ComputeHash(PTHEntryKeyVariant V) {
Daniel Dunbardf5a2372009-10-17 20:43:58 +0000117 return llvm::HashString(V.getString());
Ted Kremenekd8c02922009-02-10 22:16:22 +0000118 }
Mike Stump1eb44332009-09-09 15:08:12 +0000119
120 static std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +0000121 EmitKeyDataLength(raw_ostream& Out, PTHEntryKeyVariant V,
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000122 const PTHEntry& E) {
Ted Kremenekd8c02922009-02-10 22:16:22 +0000123
Daniel Dunbardf5a2372009-10-17 20:43:58 +0000124 unsigned n = V.getString().size() + 1 + 1;
Ted Kremenekd8c02922009-02-10 22:16:22 +0000125 ::Emit16(Out, n);
Mike Stump1eb44332009-09-09 15:08:12 +0000126
Ted Kremenekad6ce5c2009-02-13 22:07:44 +0000127 unsigned m = V.getRepresentationLength() + (V.isFile() ? 4 + 4 : 0);
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000128 ::Emit8(Out, m);
129
130 return std::make_pair(n, m);
Ted Kremenekd8c02922009-02-10 22:16:22 +0000131 }
Mike Stump1eb44332009-09-09 15:08:12 +0000132
Chris Lattner5f9e2722011-07-23 10:55:15 +0000133 static void EmitKey(raw_ostream& Out, PTHEntryKeyVariant V, unsigned n){
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000134 // Emit the entry kind.
135 ::Emit8(Out, (unsigned) V.getKind());
136 // Emit the string.
Daniel Dunbardf5a2372009-10-17 20:43:58 +0000137 Out.write(V.getString().data(), n - 1);
Ted Kremenekd8c02922009-02-10 22:16:22 +0000138 }
Mike Stump1eb44332009-09-09 15:08:12 +0000139
Chris Lattner5f9e2722011-07-23 10:55:15 +0000140 static void EmitData(raw_ostream& Out, PTHEntryKeyVariant V,
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000141 const PTHEntry& E, unsigned) {
142
143
144 // For file entries emit the offsets into the PTH file for token data
145 // and the preprocessor blocks table.
Ted Kremenekad6ce5c2009-02-13 22:07:44 +0000146 if (V.isFile()) {
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000147 ::Emit32(Out, E.getTokenOffset());
148 ::Emit32(Out, E.getPPCondTableOffset());
149 }
Mike Stump1eb44332009-09-09 15:08:12 +0000150
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000151 // Emit any other data associated with the key (i.e., stat information).
152 V.EmitData(Out);
Mike Stump1eb44332009-09-09 15:08:12 +0000153 }
Ted Kremenekd8c02922009-02-10 22:16:22 +0000154};
Mike Stump1eb44332009-09-09 15:08:12 +0000155
Ted Kremenek277faca2009-01-27 00:01:05 +0000156class OffsetOpt {
157 bool valid;
158 Offset off;
159public:
160 OffsetOpt() : valid(false) {}
161 bool hasOffset() const { return valid; }
162 Offset getOffset() const { assert(valid); return off; }
163 void setOffset(Offset o) { off = o; valid = true; }
Ted Kremenekbe295332009-01-08 02:44:06 +0000164};
165} // end anonymous namespace
166
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000167typedef OnDiskChainedHashTableGenerator<FileEntryPTHEntryInfo> PTHMap;
Ted Kremenek85888962008-10-21 00:54:44 +0000168
Ted Kremenekb978c662009-01-08 01:17:37 +0000169namespace {
Benjamin Kramerbd218282009-11-28 10:07:24 +0000170class PTHWriter {
Kovarththanan Rajaratnam622ab502010-03-18 07:45:30 +0000171 typedef llvm::DenseMap<const IdentifierInfo*,uint32_t> IDMap;
172 typedef llvm::StringMap<OffsetOpt, llvm::BumpPtrAllocator> CachedStrsTy;
173
Ted Kremenekb978c662009-01-08 01:17:37 +0000174 IDMap IM;
175 llvm::raw_fd_ostream& Out;
176 Preprocessor& PP;
177 uint32_t idcount;
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000178 PTHMap PM;
Ted Kremenekbe295332009-01-08 02:44:06 +0000179 CachedStrsTy CachedStrs;
Ted Kremenek277faca2009-01-27 00:01:05 +0000180 Offset CurStrOffset;
181 std::vector<llvm::StringMapEntry<OffsetOpt>*> StrEntries;
Ted Kremenek8f174e12008-12-23 02:52:12 +0000182
Ted Kremenekb978c662009-01-08 01:17:37 +0000183 //// Get the persistent id for the given IdentifierInfo*.
184 uint32_t ResolveID(const IdentifierInfo* II);
Mike Stump1eb44332009-09-09 15:08:12 +0000185
Ted Kremenekb978c662009-01-08 01:17:37 +0000186 /// Emit a token to the PTH file.
187 void EmitToken(const Token& T);
188
Kovarththanan Rajaratnamfbcc0712010-03-05 15:40:54 +0000189 void Emit8(uint32_t V) { ::Emit8(Out, V); }
Mike Stump1eb44332009-09-09 15:08:12 +0000190
Ted Kremeneke0ea5dc2009-02-10 01:06:17 +0000191 void Emit16(uint32_t V) { ::Emit16(Out, V); }
Mike Stump1eb44332009-09-09 15:08:12 +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
Chris Lattner5f9e2722011-07-23 10:55:15 +0000199 void EmitString(StringRef V) {
Kovarththanan Rajaratnamc3cde072010-03-14 08:35:19 +0000200 ::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.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000249 StringRef s(T.getLiteralData(), T.getLength());
Ted Kremenek25cbd9f2009-02-24 00:30:21 +0000250
Chris Lattner47246be2009-01-26 19:29:26 +0000251 // Get the string entry.
Jay Foad65aa6882011-06-21 15:13:30 +0000252 llvm::StringMapEntry<OffsetOpt> *E = &CachedStrs.GetOrCreateValue(s);
Mike Stump1eb44332009-09-09 15:08:12 +0000253
Ted Kremenekf2a223f2009-02-24 01:26:56 +0000254 // If this is a new string entry, bump the PTH offset.
Ted Kremenek277faca2009-01-27 00:01:05 +0000255 if (!E->getValue().hasOffset()) {
256 E->getValue().setOffset(CurStrOffset);
257 StrEntries.push_back(E);
Jay Foad65aa6882011-06-21 15:13:30 +0000258 CurStrOffset += s.size() + 1;
Ted Kremenek277faca2009-01-27 00:01:05 +0000259 }
Mike Stump1eb44332009-09-09 15:08:12 +0000260
Ted Kremenek25cbd9f2009-02-24 00:30:21 +0000261 // Emit the relative offset into the PTH file for the spelling string.
Ted Kremenek277faca2009-01-27 00:01:05 +0000262 Emit32(E->getValue().getOffset());
Ted Kremenekb978c662009-01-08 01:17:37 +0000263 }
Mike Stump1eb44332009-09-09 15:08:12 +0000264
Ted Kremenek25cbd9f2009-02-24 00:30:21 +0000265 // Emit the offset into the original source file of this token so that we
266 // can reconstruct its SourceLocation.
Chris Lattner52c29082009-01-27 06:27:13 +0000267 Emit32(PP.getSourceManager().getFileOffset(T.getLocation()));
Ted Kremenek85888962008-10-21 00:54:44 +0000268}
269
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000270PTHEntry PTHWriter::LexTokens(Lexer& L) {
Ted Kremenek7b78b7c2009-01-19 23:13:15 +0000271 // Pad 0's so that we emit tokens to a 4-byte alignment.
272 // This speed up reading them back in.
Ted Kremeneke0ea5dc2009-02-10 01:06:17 +0000273 Pad(Out, 4);
Kovarththanan Rajaratnam8f0d2702010-03-18 07:18:10 +0000274 Offset TokenOff = (Offset) Out.tell();
Mike Stump1eb44332009-09-09 15:08:12 +0000275
Ted Kremenekfb645b62008-12-11 23:36:38 +0000276 // Keep track of matching '#if' ... '#endif'.
277 typedef std::vector<std::pair<Offset, unsigned> > PPCondTable;
278 PPCondTable PPCond;
Ted Kremenekdad7b342008-12-12 18:31:09 +0000279 std::vector<unsigned> PPStartCond;
Ted Kremeneke5680f32008-12-23 01:30:52 +0000280 bool ParsingPreprocessorDirective = false;
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000281 Token Tok;
Mike Stump1eb44332009-09-09 15:08:12 +0000282
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000283 do {
284 L.LexFromRawLexer(Tok);
Ted Kremenek726080d2009-02-10 22:43:16 +0000285 NextToken:
286
Ted Kremeneke5680f32008-12-23 01:30:52 +0000287 if ((Tok.isAtStartOfLine() || Tok.is(tok::eof)) &&
288 ParsingPreprocessorDirective) {
Peter Collingbourne84021552011-02-28 02:37:51 +0000289 // Insert an eod token into the token cache. It has the same
Ted Kremeneke5680f32008-12-23 01:30:52 +0000290 // position as the next token that is not on the same line as the
291 // preprocessor directive. Observe that we continue processing
292 // 'Tok' when we exit this branch.
293 Token Tmp = Tok;
Peter Collingbourne84021552011-02-28 02:37:51 +0000294 Tmp.setKind(tok::eod);
Ted Kremeneke5680f32008-12-23 01:30:52 +0000295 Tmp.clearFlag(Token::StartOfLine);
296 Tmp.setIdentifierInfo(0);
Ted Kremenekb978c662009-01-08 01:17:37 +0000297 EmitToken(Tmp);
Ted Kremeneke5680f32008-12-23 01:30:52 +0000298 ParsingPreprocessorDirective = false;
299 }
Mike Stump1eb44332009-09-09 15:08:12 +0000300
Abramo Bagnarac4bf2b92010-12-22 08:23:18 +0000301 if (Tok.is(tok::raw_identifier)) {
Kovarththanan Rajaratnam65cc1e82010-03-13 08:53:33 +0000302 PP.LookUpIdentifierInfo(Tok);
Ted Kremeneke4f6b1e2009-02-10 22:27:09 +0000303 EmitToken(Tok);
Ted Kremeneke5680f32008-12-23 01:30:52 +0000304 continue;
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000305 }
Ted Kremeneke5680f32008-12-23 01:30:52 +0000306
307 if (Tok.is(tok::hash) && Tok.isAtStartOfLine()) {
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000308 // Special processing for #include. Store the '#' token and lex
309 // the next token.
Ted Kremeneke5680f32008-12-23 01:30:52 +0000310 assert(!ParsingPreprocessorDirective);
Ted Kremenekfb645b62008-12-11 23:36:38 +0000311 Offset HashOff = (Offset) Out.tell();
Ted Kremenekdad7b342008-12-12 18:31:09 +0000312
313 // Get the next token.
Ted Kremenek5c53f4c2010-07-27 02:58:57 +0000314 Token NextTok;
315 L.LexFromRawLexer(NextTok);
Chris Lattner21356192009-04-19 07:25:40 +0000316
Ted Kremenek5c53f4c2010-07-27 02:58:57 +0000317 // If we see the start of line, then we had a null directive "#". In
318 // this case, discard both tokens.
319 if (NextTok.isAtStartOfLine())
Chris Lattner21356192009-04-19 07:25:40 +0000320 goto NextToken;
Abramo Bagnarac4bf2b92010-12-22 08:23:18 +0000321
Ted Kremenek5c53f4c2010-07-27 02:58:57 +0000322 // The token is the start of a directive. Emit it.
323 EmitToken(Tok);
324 Tok = NextTok;
Mike Stump1eb44332009-09-09 15:08:12 +0000325
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000326 // Did we see 'include'/'import'/'include_next'?
Abramo Bagnarac4bf2b92010-12-22 08:23:18 +0000327 if (Tok.isNot(tok::raw_identifier)) {
Ted Kremeneke4f6b1e2009-02-10 22:27:09 +0000328 EmitToken(Tok);
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000329 continue;
Ted Kremeneke4f6b1e2009-02-10 22:27:09 +0000330 }
Mike Stump1eb44332009-09-09 15:08:12 +0000331
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000332 IdentifierInfo* II = PP.LookUpIdentifierInfo(Tok);
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000333 tok::PPKeywordKind K = II->getPPKeywordID();
Mike Stump1eb44332009-09-09 15:08:12 +0000334
Ted Kremeneke5680f32008-12-23 01:30:52 +0000335 ParsingPreprocessorDirective = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000336
Ted Kremeneke5680f32008-12-23 01:30:52 +0000337 switch (K) {
Chris Lattneraa269c22009-04-19 07:32:03 +0000338 case tok::pp_not_keyword:
339 // Invalid directives "#foo" can occur in #if 0 blocks etc, just pass
340 // them through.
Ted Kremeneke5680f32008-12-23 01:30:52 +0000341 default:
342 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000343
Ted Kremeneke5680f32008-12-23 01:30:52 +0000344 case tok::pp_include:
345 case tok::pp_import:
Mike Stump1eb44332009-09-09 15:08:12 +0000346 case tok::pp_include_next: {
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000347 // Save the 'include' token.
Ted Kremenekb978c662009-01-08 01:17:37 +0000348 EmitToken(Tok);
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000349 // Lex the next token as an include string.
350 L.setParsingPreprocessorDirective(true);
Mike Stump1eb44332009-09-09 15:08:12 +0000351 L.LexIncludeFilename(Tok);
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000352 L.setParsingPreprocessorDirective(false);
Ted Kremeneke5680f32008-12-23 01:30:52 +0000353 assert(!Tok.isAtStartOfLine());
Abramo Bagnarac4bf2b92010-12-22 08:23:18 +0000354 if (Tok.is(tok::raw_identifier))
Kovarththanan Rajaratnam65cc1e82010-03-13 08:53:33 +0000355 PP.LookUpIdentifierInfo(Tok);
Mike Stump1eb44332009-09-09 15:08:12 +0000356
Ted Kremeneke5680f32008-12-23 01:30:52 +0000357 break;
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000358 }
Ted Kremeneke5680f32008-12-23 01:30:52 +0000359 case tok::pp_if:
360 case tok::pp_ifdef:
361 case tok::pp_ifndef: {
Ted Kremeneke4f6b1e2009-02-10 22:27:09 +0000362 // Add an entry for '#if' and friends. We initially set the target
363 // index to 0. This will get backpatched when we hit #endif.
Ted Kremenekfb645b62008-12-11 23:36:38 +0000364 PPStartCond.push_back(PPCond.size());
Ted Kremenekdad7b342008-12-12 18:31:09 +0000365 PPCond.push_back(std::make_pair(HashOff, 0U));
Ted Kremeneke5680f32008-12-23 01:30:52 +0000366 break;
Ted Kremenekfb645b62008-12-11 23:36:38 +0000367 }
Ted Kremeneke5680f32008-12-23 01:30:52 +0000368 case tok::pp_endif: {
Ted Kremenekfb645b62008-12-11 23:36:38 +0000369 // Add an entry for '#endif'. We set the target table index to itself.
Ted Kremenekdad7b342008-12-12 18:31:09 +0000370 // This will later be set to zero when emitting to the PTH file. We
371 // use 0 for uninitialized indices because that is easier to debug.
Ted Kremenekfb645b62008-12-11 23:36:38 +0000372 unsigned index = PPCond.size();
Ted Kremenekfb645b62008-12-11 23:36:38 +0000373 // Backpatch the opening '#if' entry.
Ted Kremenekdad7b342008-12-12 18:31:09 +0000374 assert(!PPStartCond.empty());
375 assert(PPCond.size() > PPStartCond.back());
Ted Kremenekfb645b62008-12-11 23:36:38 +0000376 assert(PPCond[PPStartCond.back()].second == 0);
377 PPCond[PPStartCond.back()].second = index;
Mike Stump1eb44332009-09-09 15:08:12 +0000378 PPStartCond.pop_back();
379 // Add the new entry to PPCond.
Ted Kremenekdad7b342008-12-12 18:31:09 +0000380 PPCond.push_back(std::make_pair(HashOff, index));
Ted Kremenek726080d2009-02-10 22:43:16 +0000381 EmitToken(Tok);
Mike Stump1eb44332009-09-09 15:08:12 +0000382
Ted Kremenek726080d2009-02-10 22:43:16 +0000383 // Some files have gibberish on the same line as '#endif'.
384 // Discard these tokens.
Chris Lattnerd4b14462009-04-19 07:15:51 +0000385 do
386 L.LexFromRawLexer(Tok);
387 while (Tok.isNot(tok::eof) && !Tok.isAtStartOfLine());
Ted Kremenek726080d2009-02-10 22:43:16 +0000388 // We have the next token in hand.
389 // Don't immediately lex the next one.
Mike Stump1eb44332009-09-09 15:08:12 +0000390 goto NextToken;
Ted Kremenekfb645b62008-12-11 23:36:38 +0000391 }
Ted Kremeneke5680f32008-12-23 01:30:52 +0000392 case tok::pp_elif:
393 case tok::pp_else: {
394 // Add an entry for #elif or #else.
Ted Kremenekdad7b342008-12-12 18:31:09 +0000395 // This serves as both a closing and opening of a conditional block.
396 // This means that its entry will get backpatched later.
Ted Kremenekfb645b62008-12-11 23:36:38 +0000397 unsigned index = PPCond.size();
Ted Kremenekfb645b62008-12-11 23:36:38 +0000398 // Backpatch the previous '#if' entry.
Ted Kremenekdad7b342008-12-12 18:31:09 +0000399 assert(!PPStartCond.empty());
400 assert(PPCond.size() > PPStartCond.back());
Ted Kremenekfb645b62008-12-11 23:36:38 +0000401 assert(PPCond[PPStartCond.back()].second == 0);
402 PPCond[PPStartCond.back()].second = index;
403 PPStartCond.pop_back();
404 // Now add '#elif' as a new block opening.
Ted Kremenekdad7b342008-12-12 18:31:09 +0000405 PPCond.push_back(std::make_pair(HashOff, 0U));
406 PPStartCond.push_back(index);
Ted Kremeneke5680f32008-12-23 01:30:52 +0000407 break;
408 }
Ted Kremenekfb645b62008-12-11 23:36:38 +0000409 }
Ted Kremeneke4f6b1e2009-02-10 22:27:09 +0000410 }
Mike Stump1eb44332009-09-09 15:08:12 +0000411
Ted Kremeneke4f6b1e2009-02-10 22:27:09 +0000412 EmitToken(Tok);
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000413 }
Ted Kremeneke4f6b1e2009-02-10 22:27:09 +0000414 while (Tok.isNot(tok::eof));
Ted Kremenekb978c662009-01-08 01:17:37 +0000415
Ted Kremenekdad7b342008-12-12 18:31:09 +0000416 assert(PPStartCond.empty() && "Error: imblanced preprocessor conditionals.");
Ted Kremenekb978c662009-01-08 01:17:37 +0000417
Ted Kremenekfb645b62008-12-11 23:36:38 +0000418 // Next write out PPCond.
419 Offset PPCondOff = (Offset) Out.tell();
Ted Kremenekdad7b342008-12-12 18:31:09 +0000420
421 // Write out the size of PPCond so that clients can identifer empty tables.
Ted Kremenekb978c662009-01-08 01:17:37 +0000422 Emit32(PPCond.size());
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000423
Ted Kremenekdad7b342008-12-12 18:31:09 +0000424 for (unsigned i = 0, e = PPCond.size(); i!=e; ++i) {
Kovarththanan Rajaratnam8f0d2702010-03-18 07:18:10 +0000425 Emit32(PPCond[i].first - TokenOff);
Ted Kremenekdad7b342008-12-12 18:31:09 +0000426 uint32_t x = PPCond[i].second;
427 assert(x != 0 && "PPCond entry not backpatched.");
428 // Emit zero for #endifs. This allows us to do checking when
429 // we read the PTH file back in.
Ted Kremenekb978c662009-01-08 01:17:37 +0000430 Emit32(x == i ? 0 : x);
Ted Kremenekfb645b62008-12-11 23:36:38 +0000431 }
432
Kovarththanan Rajaratnam8f0d2702010-03-18 07:18:10 +0000433 return PTHEntry(TokenOff, PPCondOff);
Ted Kremenekbe295332009-01-08 02:44:06 +0000434}
435
Ted Kremenek277faca2009-01-27 00:01:05 +0000436Offset PTHWriter::EmitCachedSpellings() {
437 // Write each cached strings to the PTH file.
438 Offset SpellingsOff = Out.tell();
Mike Stump1eb44332009-09-09 15:08:12 +0000439
Ted Kremenek277faca2009-01-27 00:01:05 +0000440 for (std::vector<llvm::StringMapEntry<OffsetOpt>*>::iterator
Chris Lattnerf2390362009-03-28 00:16:20 +0000441 I = StrEntries.begin(), E = StrEntries.end(); I!=E; ++I)
442 EmitBuf((*I)->getKeyData(), (*I)->getKeyLength()+1 /*nul included*/);
Mike Stump1eb44332009-09-09 15:08:12 +0000443
Ted Kremenek277faca2009-01-27 00:01:05 +0000444 return SpellingsOff;
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000445}
Ted Kremenek85888962008-10-21 00:54:44 +0000446
Kovarththanan Rajaratnam74e485e2010-03-14 07:38:15 +0000447void PTHWriter::GeneratePTH(const std::string &MainFile) {
Ted Kremeneke1b64982009-01-26 21:43:14 +0000448 // Generate the prologue.
Richard Smithc141b512012-08-17 03:55:43 +0000449 Out << "cfe-pth" << '\0';
Ted Kremenek67d15052009-01-26 21:50:21 +0000450 Emit32(PTHManager::Version);
Mike Stump1eb44332009-09-09 15:08:12 +0000451
Ted Kremeneka4bd8eb2009-02-11 23:34:32 +0000452 // Leave 4 words for the prologue.
453 Offset PrologueOffset = Out.tell();
Chris Lattnerf2390362009-03-28 00:16:20 +0000454 for (unsigned i = 0; i < 4; ++i)
455 Emit32(0);
Mike Stump1eb44332009-09-09 15:08:12 +0000456
Ted Kremenekd5cded42009-03-19 22:10:38 +0000457 // Write the name of the MainFile.
Kovarththanan Rajaratnam2e6051a2010-03-14 07:55:43 +0000458 if (!MainFile.empty()) {
Kovarththanan Rajaratnam8f0d2702010-03-18 07:18:10 +0000459 EmitString(MainFile);
Chris Lattnerf2390362009-03-28 00:16:20 +0000460 } else {
Ted Kremenekd5cded42009-03-19 22:10:38 +0000461 // String with 0 bytes.
462 Emit16(0);
463 }
464 Emit8(0);
Mike Stump1eb44332009-09-09 15:08:12 +0000465
Ted Kremenek85888962008-10-21 00:54:44 +0000466 // Iterate over all the files in SourceManager. Create a lexer
467 // for each file and cache the tokens.
Chris Lattnerc6fe32a2009-01-17 03:48:08 +0000468 SourceManager &SM = PP.getSourceManager();
David Blaikie4e4d0842012-03-11 07:00:24 +0000469 const LangOptions &LOpts = PP.getLangOpts();
Mike Stump1eb44332009-09-09 15:08:12 +0000470
Chris Lattnerc6fe32a2009-01-17 03:48:08 +0000471 for (SourceManager::fileinfo_iterator I = SM.fileinfo_begin(),
472 E = SM.fileinfo_end(); I != E; ++I) {
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000473 const SrcMgr::ContentCache &C = *I->second;
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +0000474 const FileEntry *FE = C.OrigEntry;
Mike Stump1eb44332009-09-09 15:08:12 +0000475
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000476 // FIXME: Handle files with non-absolute paths.
Michael J. Spencer256053b2010-12-17 21:22:22 +0000477 if (llvm::sys::path::is_relative(FE->getName()))
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000478 continue;
Ted Kremenek85888962008-10-21 00:54:44 +0000479
Chris Lattnere127a0d2010-04-20 20:35:58 +0000480 const llvm::MemoryBuffer *B = C.getBuffer(PP.getDiagnostics(), SM);
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000481 if (!B) continue;
Ted Kremenekfb645b62008-12-11 23:36:38 +0000482
Chris Lattner2b2453a2009-01-17 06:22:33 +0000483 FileID FID = SM.createFileID(FE, SourceLocation(), SrcMgr::C_User);
Chris Lattner6e290142009-11-30 04:18:44 +0000484 const llvm::MemoryBuffer *FromFile = SM.getBuffer(FID);
485 Lexer L(FID, FromFile, SM, LOpts);
Ted Kremenekd8c02922009-02-10 22:16:22 +0000486 PM.insert(FE, LexTokens(L));
Daniel Dunbar31309ab2008-11-26 02:18:33 +0000487 }
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000488
489 // Write out the identifier table.
Chris Lattnerf2390362009-03-28 00:16:20 +0000490 const std::pair<Offset,Offset> &IdTableOff = EmitIdentifierTable();
Mike Stump1eb44332009-09-09 15:08:12 +0000491
Ted Kremenekbe295332009-01-08 02:44:06 +0000492 // Write out the cached strings table.
Ted Kremenek277faca2009-01-27 00:01:05 +0000493 Offset SpellingOff = EmitCachedSpellings();
Mike Stump1eb44332009-09-09 15:08:12 +0000494
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000495 // Write out the file table.
Mike Stump1eb44332009-09-09 15:08:12 +0000496 Offset FileTableOff = EmitFileTable();
497
Ted Kremeneka4bd8eb2009-02-11 23:34:32 +0000498 // Finally, write the prologue.
499 Out.seek(PrologueOffset);
Ted Kremenekb978c662009-01-08 01:17:37 +0000500 Emit32(IdTableOff.first);
Ted Kremenekf1de4642009-02-11 16:06:55 +0000501 Emit32(IdTableOff.second);
Ted Kremenekb978c662009-01-08 01:17:37 +0000502 Emit32(FileTableOff);
Ted Kremenek277faca2009-01-27 00:01:05 +0000503 Emit32(SpellingOff);
Ted Kremenekb978c662009-01-08 01:17:37 +0000504}
505
Chris Lattner52ba8702009-03-28 00:55:35 +0000506namespace {
507/// StatListener - A simple "interpose" object used to monitor stat calls
508/// invoked by FileManager while processing the original sources used
509/// as input to PTH generation. StatListener populates the PTHWriter's
510/// file map with stat information for directories as well as negative stats.
511/// Stat information for files are populated elsewhere.
Chris Lattner10e286a2010-11-23 19:19:34 +0000512class StatListener : public FileSystemStatCache {
Chris Lattner52ba8702009-03-28 00:55:35 +0000513 PTHMap &PM;
514public:
515 StatListener(PTHMap &pm) : PM(pm) {}
516 ~StatListener() {}
Mike Stump1eb44332009-09-09 15:08:12 +0000517
Rafael Espindola0fda0f72013-08-01 21:42:11 +0000518 LookupResult getStat(const char *Path, FileData &Data, bool isFile,
519 int *FileDescriptor) {
520 LookupResult Result = statChained(Path, Data, isFile, FileDescriptor);
Mike Stump1eb44332009-09-09 15:08:12 +0000521
Chris Lattnerd6f61112010-11-23 20:05:15 +0000522 if (Result == CacheMissing) // Failed 'stat'.
Chris Lattner10e286a2010-11-23 19:19:34 +0000523 PM.insert(PTHEntryKeyVariant(Path), PTHEntry());
Rafael Espindola0fda0f72013-08-01 21:42:11 +0000524 else if (Data.IsDirectory) {
Chris Lattner52ba8702009-03-28 00:55:35 +0000525 // Only cache directories with absolute paths.
Michael J. Spencer256053b2010-12-17 21:22:22 +0000526 if (llvm::sys::path::is_relative(Path))
Chris Lattner10e286a2010-11-23 19:19:34 +0000527 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +0000528
Rafael Espindola0fda0f72013-08-01 21:42:11 +0000529 PM.insert(PTHEntryKeyVariant(&Data, Path), PTHEntry());
Chris Lattner52ba8702009-03-28 00:55:35 +0000530 }
Mike Stump1eb44332009-09-09 15:08:12 +0000531
Chris Lattner10e286a2010-11-23 19:19:34 +0000532 return Result;
Chris Lattner52ba8702009-03-28 00:55:35 +0000533 }
534};
535} // end anonymous namespace
536
537
Eli Friedmanf54fce82009-05-19 01:02:07 +0000538void clang::CacheTokens(Preprocessor &PP, llvm::raw_fd_ostream* OS) {
Ted Kremenekd5cded42009-03-19 22:10:38 +0000539 // Get the name of the main file.
540 const SourceManager &SrcMgr = PP.getSourceManager();
541 const FileEntry *MainFile = SrcMgr.getFileEntryForID(SrcMgr.getMainFileID());
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000542 SmallString<128> MainFilePath(MainFile->getName());
Mike Stump1eb44332009-09-09 15:08:12 +0000543
Michael J. Spencerfbfd1802010-12-21 16:45:57 +0000544 llvm::sys::fs::make_absolute(MainFilePath);
Ted Kremenekad6ce5c2009-02-13 22:07:44 +0000545
546 // Create the PTHWriter.
Eli Friedmanf54fce82009-05-19 01:02:07 +0000547 PTHWriter PW(*OS, PP);
Mike Stump1eb44332009-09-09 15:08:12 +0000548
Ted Kremenekad6ce5c2009-02-13 22:07:44 +0000549 // Install the 'stat' system call listener in the FileManager.
Douglas Gregor52e71082009-10-16 18:18:30 +0000550 StatListener *StatCache = new StatListener(PW.getPM());
551 PP.getFileManager().addStatCache(StatCache, /*AtBeginning=*/true);
Mike Stump1eb44332009-09-09 15:08:12 +0000552
Ted Kremenekad6ce5c2009-02-13 22:07:44 +0000553 // Lex through the entire file. This will populate SourceManager with
554 // all of the header information.
555 Token Tok;
Chris Lattnere127a0d2010-04-20 20:35:58 +0000556 PP.EnterMainSourceFile();
Ted Kremenekad6ce5c2009-02-13 22:07:44 +0000557 do { PP.Lex(Tok); } while (Tok.isNot(tok::eof));
Mike Stump1eb44332009-09-09 15:08:12 +0000558
Ted Kremenekad6ce5c2009-02-13 22:07:44 +0000559 // Generate the PTH file.
Douglas Gregor52e71082009-10-16 18:18:30 +0000560 PP.getFileManager().removeStatCache(StatCache);
Kovarththanan Rajaratnam74e485e2010-03-14 07:38:15 +0000561 PW.GeneratePTH(MainFilePath.str());
Ted Kremenek85888962008-10-21 00:54:44 +0000562}
Ted Kremeneke0ea5dc2009-02-10 01:06:17 +0000563
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000564//===----------------------------------------------------------------------===//
565
Kovarththanan Rajaratnam48673e62010-03-14 06:48:05 +0000566namespace {
Chris Lattnerf2390362009-03-28 00:16:20 +0000567class PTHIdKey {
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000568public:
569 const IdentifierInfo* II;
570 uint32_t FileOffset;
571};
572
Benjamin Kramerbd218282009-11-28 10:07:24 +0000573class PTHIdentifierTableTrait {
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000574public:
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000575 typedef PTHIdKey* key_type;
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000576 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +0000577
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000578 typedef uint32_t data_type;
579 typedef data_type data_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +0000580
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000581 static unsigned ComputeHash(PTHIdKey* key) {
Daniel Dunbar01eb9b92009-10-18 21:17:35 +0000582 return llvm::HashString(key->II->getName());
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000583 }
Mike Stump1eb44332009-09-09 15:08:12 +0000584
585 static std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +0000586 EmitKeyDataLength(raw_ostream& Out, const PTHIdKey* key, uint32_t) {
Daniel Dunbare013d682009-10-18 20:26:12 +0000587 unsigned n = key->II->getLength() + 1;
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000588 ::Emit16(Out, n);
589 return std::make_pair(n, sizeof(uint32_t));
590 }
Mike Stump1eb44332009-09-09 15:08:12 +0000591
Chris Lattner5f9e2722011-07-23 10:55:15 +0000592 static void EmitKey(raw_ostream& Out, PTHIdKey* key, unsigned n) {
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000593 // Record the location of the key data. This is used when generating
594 // the mapping from persistent IDs to strings.
595 key->FileOffset = Out.tell();
Daniel Dunbare013d682009-10-18 20:26:12 +0000596 Out.write(key->II->getNameStart(), n);
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000597 }
Mike Stump1eb44332009-09-09 15:08:12 +0000598
Chris Lattner5f9e2722011-07-23 10:55:15 +0000599 static void EmitData(raw_ostream& Out, PTHIdKey*, uint32_t pID,
Ted Kremenek337edcd2009-02-12 03:26:59 +0000600 unsigned) {
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000601 ::Emit32(Out, pID);
Mike Stump1eb44332009-09-09 15:08:12 +0000602 }
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000603};
604} // end anonymous namespace
605
606/// EmitIdentifierTable - Emits two tables to the PTH file. The first is
607/// a hashtable mapping from identifier strings to persistent IDs. The second
608/// is a straight table mapping from persistent IDs to string data (the
609/// keys of the first table).
610///
611std::pair<Offset,Offset> PTHWriter::EmitIdentifierTable() {
612 // Build two maps:
613 // (1) an inverse map from persistent IDs -> (IdentifierInfo*,Offset)
614 // (2) a map from (IdentifierInfo*, Offset)* -> persistent IDs
615
616 // Note that we use 'calloc', so all the bytes are 0.
Chris Lattnerf2390362009-03-28 00:16:20 +0000617 PTHIdKey *IIDMap = (PTHIdKey*)calloc(idcount, sizeof(PTHIdKey));
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000618
619 // Create the hashtable.
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000620 OnDiskChainedHashTableGenerator<PTHIdentifierTableTrait> IIOffMap;
Mike Stump1eb44332009-09-09 15:08:12 +0000621
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000622 // Generate mapping from persistent IDs -> IdentifierInfo*.
Chris Lattnerf2390362009-03-28 00:16:20 +0000623 for (IDMap::iterator I = IM.begin(), E = IM.end(); I != E; ++I) {
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000624 // Decrement by 1 because we are using a vector for the lookup and
625 // 0 is reserved for NULL.
626 assert(I->second > 0);
627 assert(I->second-1 < idcount);
628 unsigned idx = I->second-1;
Mike Stump1eb44332009-09-09 15:08:12 +0000629
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000630 // Store the mapping from persistent ID to IdentifierInfo*
631 IIDMap[idx].II = I->first;
Mike Stump1eb44332009-09-09 15:08:12 +0000632
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000633 // Store the reverse mapping in a hashtable.
634 IIOffMap.insert(&IIDMap[idx], I->second);
635 }
Mike Stump1eb44332009-09-09 15:08:12 +0000636
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000637 // Write out the inverse map first. This causes the PCIDKey entries to
638 // record PTH file offsets for the string data. This is used to write
639 // the second table.
640 Offset StringTableOffset = IIOffMap.Emit(Out);
Mike Stump1eb44332009-09-09 15:08:12 +0000641
642 // Now emit the table mapping from persistent IDs to PTH file offsets.
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000643 Offset IDOff = Out.tell();
644 Emit32(idcount); // Emit the number of identifiers.
Chris Lattnerf2390362009-03-28 00:16:20 +0000645 for (unsigned i = 0 ; i < idcount; ++i)
646 Emit32(IIDMap[i].FileOffset);
Mike Stump1eb44332009-09-09 15:08:12 +0000647
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000648 // Finally, release the inverse map.
649 free(IIDMap);
Mike Stump1eb44332009-09-09 15:08:12 +0000650
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000651 return std::make_pair(IDOff, StringTableOffset);
652}