blob: 2cb65826ad6382269db84cfe6181cc610dadd0bb [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"
Stephen Hines651f13c2014-04-23 16:59:28 -070026#include "llvm/Support/EndianStream.h"
Michael J. Spencerfbfd1802010-12-21 16:45:57 +000027#include "llvm/Support/FileSystem.h"
Ted Kremenek85888962008-10-21 00:54:44 +000028#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000029#include "llvm/Support/Path.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000030#include "llvm/Support/raw_ostream.h"
Ted Kremenek85888962008-10-21 00:54:44 +000031
Cedric Venetea684e62009-02-14 16:15:20 +000032// FIXME: put this somewhere else?
33#ifndef S_ISDIR
34#define S_ISDIR(x) (((x)&_S_IFDIR)!=0)
35#endif
36
Ted Kremenek85888962008-10-21 00:54:44 +000037using namespace clang;
Douglas Gregor9378ba42009-04-20 07:08:21 +000038using namespace clang::io;
Ted Kremenekf0e1f792009-02-10 01:14:45 +000039
40//===----------------------------------------------------------------------===//
41// PTH-specific stuff.
42//===----------------------------------------------------------------------===//
43
Ted Kremenekbe295332009-01-08 02:44:06 +000044namespace {
Benjamin Kramerbd218282009-11-28 10:07:24 +000045class PTHEntry {
Mike Stump1eb44332009-09-09 15:08:12 +000046 Offset TokenData, PPCondData;
Ted Kremenekbe295332009-01-08 02:44:06 +000047
Mike Stump1eb44332009-09-09 15:08:12 +000048public:
Ted Kremeneka4b44dd2009-02-13 19:13:46 +000049 PTHEntry() {}
Ted Kremenekbe295332009-01-08 02:44:06 +000050
Ted Kremeneka4b44dd2009-02-13 19:13:46 +000051 PTHEntry(Offset td, Offset ppcd)
Ted Kremenek277faca2009-01-27 00:01:05 +000052 : TokenData(td), PPCondData(ppcd) {}
Mike Stump1eb44332009-09-09 15:08:12 +000053
54 Offset getTokenOffset() const { return TokenData; }
Ted Kremenekbe295332009-01-08 02:44:06 +000055 Offset getPPCondTableOffset() const { return PPCondData; }
Ted Kremenek277faca2009-01-27 00:01:05 +000056};
Mike Stump1eb44332009-09-09 15:08:12 +000057
58
Benjamin Kramerbd218282009-11-28 10:07:24 +000059class PTHEntryKeyVariant {
Ted Kremenekad6ce5c2009-02-13 22:07:44 +000060 union { const FileEntry* FE; const char* Path; };
Ted Kremeneka4b44dd2009-02-13 19:13:46 +000061 enum { IsFE = 0x1, IsDE = 0x2, IsNoExist = 0x0 } Kind;
Rafael Espindola0fda0f72013-08-01 21:42:11 +000062 FileData *Data;
63
Ted Kremenekd8c02922009-02-10 22:16:22 +000064public:
Rafael Espindola0fda0f72013-08-01 21:42:11 +000065 PTHEntryKeyVariant(const FileEntry *fe) : FE(fe), Kind(IsFE), Data(0) {}
Ted Kremenekad6ce5c2009-02-13 22:07:44 +000066
Rafael Espindola0fda0f72013-08-01 21:42:11 +000067 PTHEntryKeyVariant(FileData *Data, const char *path)
68 : Path(path), Kind(IsDE), Data(new FileData(*Data)) {}
Ted Kremenekad6ce5c2009-02-13 22:07:44 +000069
Rafael Espindola0fda0f72013-08-01 21:42:11 +000070 explicit PTHEntryKeyVariant(const char *path)
71 : Path(path), Kind(IsNoExist), Data(0) {}
Mike Stump1eb44332009-09-09 15:08:12 +000072
Ted Kremenekad6ce5c2009-02-13 22:07:44 +000073 bool isFile() const { return Kind == IsFE; }
Mike Stump1eb44332009-09-09 15:08:12 +000074
Chris Lattner5f9e2722011-07-23 10:55:15 +000075 StringRef getString() const {
Ted Kremenekad6ce5c2009-02-13 22:07:44 +000076 return Kind == IsFE ? FE->getName() : Path;
Ted Kremeneka4b44dd2009-02-13 19:13:46 +000077 }
Mike Stump1eb44332009-09-09 15:08:12 +000078
Ted Kremeneka4b44dd2009-02-13 19:13:46 +000079 unsigned getKind() const { return (unsigned) Kind; }
Mike Stump1eb44332009-09-09 15:08:12 +000080
Chris Lattner5f9e2722011-07-23 10:55:15 +000081 void EmitData(raw_ostream& Out) {
Stephen Hines651f13c2014-04-23 16:59:28 -070082 using namespace llvm::support;
83 endian::Writer<little> LE(Out);
Ted Kremeneka4b44dd2009-02-13 19:13:46 +000084 switch (Kind) {
Rafael Espindola0fda0f72013-08-01 21:42:11 +000085 case IsFE: {
Mike Stumpb7166332010-01-20 02:03:14 +000086 // Emit stat information.
Rafael Espindola0fda0f72013-08-01 21:42:11 +000087 llvm::sys::fs::UniqueID UID = FE->getUniqueID();
Stephen Hines651f13c2014-04-23 16:59:28 -070088 LE.write<uint64_t>(UID.getFile());
89 LE.write<uint64_t>(UID.getDevice());
90 LE.write<uint64_t>(FE->getModificationTime());
91 LE.write<uint64_t>(FE->getSize());
Rafael Espindola0fda0f72013-08-01 21:42:11 +000092 } break;
Mike Stumpb7166332010-01-20 02:03:14 +000093 case IsDE:
94 // Emit stat information.
Stephen Hines651f13c2014-04-23 16:59:28 -070095 LE.write<uint64_t>(Data->UniqueID.getFile());
96 LE.write<uint64_t>(Data->UniqueID.getDevice());
97 LE.write<uint64_t>(Data->ModTime);
98 LE.write<uint64_t>(Data->Size);
Rafael Espindola0fda0f72013-08-01 21:42:11 +000099 delete Data;
Mike Stumpb7166332010-01-20 02:03:14 +0000100 break;
101 default:
102 break;
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000103 }
104 }
Mike Stump1eb44332009-09-09 15:08:12 +0000105
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000106 unsigned getRepresentationLength() const {
Ted Kremenekad6ce5c2009-02-13 22:07:44 +0000107 return Kind == IsNoExist ? 0 : 4 + 4 + 2 + 8 + 8;
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000108 }
109};
Mike Stump1eb44332009-09-09 15:08:12 +0000110
Benjamin Kramerbd218282009-11-28 10:07:24 +0000111class FileEntryPTHEntryInfo {
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000112public:
113 typedef PTHEntryKeyVariant key_type;
Ted Kremenekd8c02922009-02-10 22:16:22 +0000114 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +0000115
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000116 typedef PTHEntry data_type;
117 typedef const PTHEntry& data_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +0000118
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000119 static unsigned ComputeHash(PTHEntryKeyVariant V) {
Daniel Dunbardf5a2372009-10-17 20:43:58 +0000120 return llvm::HashString(V.getString());
Ted Kremenekd8c02922009-02-10 22:16:22 +0000121 }
Mike Stump1eb44332009-09-09 15:08:12 +0000122
123 static std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +0000124 EmitKeyDataLength(raw_ostream& Out, PTHEntryKeyVariant V,
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000125 const PTHEntry& E) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700126 using namespace llvm::support;
127 endian::Writer<little> LE(Out);
Ted Kremenekd8c02922009-02-10 22:16:22 +0000128
Daniel Dunbardf5a2372009-10-17 20:43:58 +0000129 unsigned n = V.getString().size() + 1 + 1;
Stephen Hines651f13c2014-04-23 16:59:28 -0700130 LE.write<uint16_t>(n);
Mike Stump1eb44332009-09-09 15:08:12 +0000131
Ted Kremenekad6ce5c2009-02-13 22:07:44 +0000132 unsigned m = V.getRepresentationLength() + (V.isFile() ? 4 + 4 : 0);
Stephen Hines651f13c2014-04-23 16:59:28 -0700133 LE.write<uint8_t>(m);
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000134
135 return std::make_pair(n, m);
Ted Kremenekd8c02922009-02-10 22:16:22 +0000136 }
Mike Stump1eb44332009-09-09 15:08:12 +0000137
Chris Lattner5f9e2722011-07-23 10:55:15 +0000138 static void EmitKey(raw_ostream& Out, PTHEntryKeyVariant V, unsigned n){
Stephen Hines651f13c2014-04-23 16:59:28 -0700139 using namespace llvm::support;
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000140 // Emit the entry kind.
Stephen Hines651f13c2014-04-23 16:59:28 -0700141 endian::Writer<little>(Out).write<uint8_t>((unsigned)V.getKind());
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000142 // Emit the string.
Daniel Dunbardf5a2372009-10-17 20:43:58 +0000143 Out.write(V.getString().data(), n - 1);
Ted Kremenekd8c02922009-02-10 22:16:22 +0000144 }
Mike Stump1eb44332009-09-09 15:08:12 +0000145
Chris Lattner5f9e2722011-07-23 10:55:15 +0000146 static void EmitData(raw_ostream& Out, PTHEntryKeyVariant V,
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000147 const PTHEntry& E, unsigned) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700148 using namespace llvm::support;
149 endian::Writer<little> LE(Out);
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000150
151 // For file entries emit the offsets into the PTH file for token data
152 // and the preprocessor blocks table.
Ted Kremenekad6ce5c2009-02-13 22:07:44 +0000153 if (V.isFile()) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700154 LE.write<uint32_t>(E.getTokenOffset());
155 LE.write<uint32_t>(E.getPPCondTableOffset());
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000156 }
Mike Stump1eb44332009-09-09 15:08:12 +0000157
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000158 // Emit any other data associated with the key (i.e., stat information).
159 V.EmitData(Out);
Mike Stump1eb44332009-09-09 15:08:12 +0000160 }
Ted Kremenekd8c02922009-02-10 22:16:22 +0000161};
Mike Stump1eb44332009-09-09 15:08:12 +0000162
Ted Kremenek277faca2009-01-27 00:01:05 +0000163class OffsetOpt {
164 bool valid;
165 Offset off;
166public:
167 OffsetOpt() : valid(false) {}
168 bool hasOffset() const { return valid; }
169 Offset getOffset() const { assert(valid); return off; }
170 void setOffset(Offset o) { off = o; valid = true; }
Ted Kremenekbe295332009-01-08 02:44:06 +0000171};
172} // end anonymous namespace
173
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000174typedef OnDiskChainedHashTableGenerator<FileEntryPTHEntryInfo> PTHMap;
Ted Kremenek85888962008-10-21 00:54:44 +0000175
Ted Kremenekb978c662009-01-08 01:17:37 +0000176namespace {
Benjamin Kramerbd218282009-11-28 10:07:24 +0000177class PTHWriter {
Kovarththanan Rajaratnam622ab502010-03-18 07:45:30 +0000178 typedef llvm::DenseMap<const IdentifierInfo*,uint32_t> IDMap;
179 typedef llvm::StringMap<OffsetOpt, llvm::BumpPtrAllocator> CachedStrsTy;
180
Ted Kremenekb978c662009-01-08 01:17:37 +0000181 IDMap IM;
182 llvm::raw_fd_ostream& Out;
183 Preprocessor& PP;
184 uint32_t idcount;
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000185 PTHMap PM;
Ted Kremenekbe295332009-01-08 02:44:06 +0000186 CachedStrsTy CachedStrs;
Ted Kremenek277faca2009-01-27 00:01:05 +0000187 Offset CurStrOffset;
188 std::vector<llvm::StringMapEntry<OffsetOpt>*> StrEntries;
Ted Kremenek8f174e12008-12-23 02:52:12 +0000189
Ted Kremenekb978c662009-01-08 01:17:37 +0000190 //// Get the persistent id for the given IdentifierInfo*.
191 uint32_t ResolveID(const IdentifierInfo* II);
Mike Stump1eb44332009-09-09 15:08:12 +0000192
Ted Kremenekb978c662009-01-08 01:17:37 +0000193 /// Emit a token to the PTH file.
194 void EmitToken(const Token& T);
195
Stephen Hines651f13c2014-04-23 16:59:28 -0700196 void Emit8(uint32_t V) {
197 using namespace llvm::support;
198 endian::Writer<little>(Out).write<uint8_t>(V);
199 }
Mike Stump1eb44332009-09-09 15:08:12 +0000200
Stephen Hines651f13c2014-04-23 16:59:28 -0700201 void Emit16(uint32_t V) {
202 using namespace llvm::support;
203 endian::Writer<little>(Out).write<uint16_t>(V);
204 }
Mike Stump1eb44332009-09-09 15:08:12 +0000205
Stephen Hines651f13c2014-04-23 16:59:28 -0700206 void Emit32(uint32_t V) {
207 using namespace llvm::support;
208 endian::Writer<little>(Out).write<uint32_t>(V);
209 }
Ted Kremeneke0ea5dc2009-02-10 01:06:17 +0000210
Chris Lattnerf2390362009-03-28 00:16:20 +0000211 void EmitBuf(const char *Ptr, unsigned NumBytes) {
212 Out.write(Ptr, NumBytes);
Ted Kremenekb978c662009-01-08 01:17:37 +0000213 }
Mike Stump1eb44332009-09-09 15:08:12 +0000214
Chris Lattner5f9e2722011-07-23 10:55:15 +0000215 void EmitString(StringRef V) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700216 using namespace llvm::support;
217 endian::Writer<little>(Out).write<uint16_t>(V.size());
Kovarththanan Rajaratnamc3cde072010-03-14 08:35:19 +0000218 EmitBuf(V.data(), V.size());
219 }
220
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000221 /// EmitIdentifierTable - Emits two tables to the PTH file. The first is
222 /// a hashtable mapping from identifier strings to persistent IDs.
223 /// The second is a straight table mapping from persistent IDs to string data
224 /// (the keys of the first table).
Ted Kremenekf1de4642009-02-11 16:06:55 +0000225 std::pair<Offset, Offset> EmitIdentifierTable();
Mike Stump1eb44332009-09-09 15:08:12 +0000226
Ted Kremenekf1de4642009-02-11 16:06:55 +0000227 /// EmitFileTable - Emit a table mapping from file name strings to PTH
228 /// token data.
229 Offset EmitFileTable() { return PM.Emit(Out); }
230
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000231 PTHEntry LexTokens(Lexer& L);
Ted Kremenek277faca2009-01-27 00:01:05 +0000232 Offset EmitCachedSpellings();
Ted Kremenekad6ce5c2009-02-13 22:07:44 +0000233
Ted Kremenekb978c662009-01-08 01:17:37 +0000234public:
Mike Stump1eb44332009-09-09 15:08:12 +0000235 PTHWriter(llvm::raw_fd_ostream& out, Preprocessor& pp)
Ted Kremenek277faca2009-01-27 00:01:05 +0000236 : Out(out), PP(pp), idcount(0), CurStrOffset(0) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000237
Chris Lattner52ba8702009-03-28 00:55:35 +0000238 PTHMap &getPM() { return PM; }
Kovarththanan Rajaratnam74e485e2010-03-14 07:38:15 +0000239 void GeneratePTH(const std::string &MainFile);
Ted Kremenekb978c662009-01-08 01:17:37 +0000240};
241} // end anonymous namespace
Mike Stump1eb44332009-09-09 15:08:12 +0000242
243uint32_t PTHWriter::ResolveID(const IdentifierInfo* II) {
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000244 // Null IdentifierInfo's map to the persistent ID 0.
245 if (!II)
246 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000247
Ted Kremenek85888962008-10-21 00:54:44 +0000248 IDMap::iterator I = IM.find(II);
Chris Lattnerf2390362009-03-28 00:16:20 +0000249 if (I != IM.end())
250 return I->second; // We've already added 1.
Mike Stump1eb44332009-09-09 15:08:12 +0000251
Chris Lattnerf2390362009-03-28 00:16:20 +0000252 IM[II] = ++idcount; // Pre-increment since '0' is reserved for NULL.
253 return idcount;
Ted Kremenek85888962008-10-21 00:54:44 +0000254}
255
Ted Kremenekb978c662009-01-08 01:17:37 +0000256void PTHWriter::EmitToken(const Token& T) {
Ted Kremenekf2a223f2009-02-24 01:26:56 +0000257 // Emit the token kind, flags, and length.
258 Emit32(((uint32_t) T.getKind()) | ((((uint32_t) T.getFlags())) << 8)|
259 (((uint32_t) T.getLength()) << 16));
Mike Stump1eb44332009-09-09 15:08:12 +0000260
Chris Lattnerf2390362009-03-28 00:16:20 +0000261 if (!T.isLiteral()) {
262 Emit32(ResolveID(T.getIdentifierInfo()));
263 } else {
Ted Kremenekf2a223f2009-02-24 01:26:56 +0000264 // We cache *un-cleaned* spellings. This gives us 100% fidelity with the
265 // source code.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000266 StringRef s(T.getLiteralData(), T.getLength());
Ted Kremenek25cbd9f2009-02-24 00:30:21 +0000267
Chris Lattner47246be2009-01-26 19:29:26 +0000268 // Get the string entry.
Jay Foad65aa6882011-06-21 15:13:30 +0000269 llvm::StringMapEntry<OffsetOpt> *E = &CachedStrs.GetOrCreateValue(s);
Mike Stump1eb44332009-09-09 15:08:12 +0000270
Ted Kremenekf2a223f2009-02-24 01:26:56 +0000271 // If this is a new string entry, bump the PTH offset.
Ted Kremenek277faca2009-01-27 00:01:05 +0000272 if (!E->getValue().hasOffset()) {
273 E->getValue().setOffset(CurStrOffset);
274 StrEntries.push_back(E);
Jay Foad65aa6882011-06-21 15:13:30 +0000275 CurStrOffset += s.size() + 1;
Ted Kremenek277faca2009-01-27 00:01:05 +0000276 }
Mike Stump1eb44332009-09-09 15:08:12 +0000277
Ted Kremenek25cbd9f2009-02-24 00:30:21 +0000278 // Emit the relative offset into the PTH file for the spelling string.
Ted Kremenek277faca2009-01-27 00:01:05 +0000279 Emit32(E->getValue().getOffset());
Ted Kremenekb978c662009-01-08 01:17:37 +0000280 }
Mike Stump1eb44332009-09-09 15:08:12 +0000281
Ted Kremenek25cbd9f2009-02-24 00:30:21 +0000282 // Emit the offset into the original source file of this token so that we
283 // can reconstruct its SourceLocation.
Chris Lattner52c29082009-01-27 06:27:13 +0000284 Emit32(PP.getSourceManager().getFileOffset(T.getLocation()));
Ted Kremenek85888962008-10-21 00:54:44 +0000285}
286
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000287PTHEntry PTHWriter::LexTokens(Lexer& L) {
Ted Kremenek7b78b7c2009-01-19 23:13:15 +0000288 // Pad 0's so that we emit tokens to a 4-byte alignment.
289 // This speed up reading them back in.
Ted Kremeneke0ea5dc2009-02-10 01:06:17 +0000290 Pad(Out, 4);
Kovarththanan Rajaratnam8f0d2702010-03-18 07:18:10 +0000291 Offset TokenOff = (Offset) Out.tell();
Mike Stump1eb44332009-09-09 15:08:12 +0000292
Ted Kremenekfb645b62008-12-11 23:36:38 +0000293 // Keep track of matching '#if' ... '#endif'.
294 typedef std::vector<std::pair<Offset, unsigned> > PPCondTable;
295 PPCondTable PPCond;
Ted Kremenekdad7b342008-12-12 18:31:09 +0000296 std::vector<unsigned> PPStartCond;
Ted Kremeneke5680f32008-12-23 01:30:52 +0000297 bool ParsingPreprocessorDirective = false;
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000298 Token Tok;
Mike Stump1eb44332009-09-09 15:08:12 +0000299
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000300 do {
301 L.LexFromRawLexer(Tok);
Ted Kremenek726080d2009-02-10 22:43:16 +0000302 NextToken:
303
Ted Kremeneke5680f32008-12-23 01:30:52 +0000304 if ((Tok.isAtStartOfLine() || Tok.is(tok::eof)) &&
305 ParsingPreprocessorDirective) {
Peter Collingbourne84021552011-02-28 02:37:51 +0000306 // Insert an eod token into the token cache. It has the same
Ted Kremeneke5680f32008-12-23 01:30:52 +0000307 // position as the next token that is not on the same line as the
308 // preprocessor directive. Observe that we continue processing
309 // 'Tok' when we exit this branch.
310 Token Tmp = Tok;
Peter Collingbourne84021552011-02-28 02:37:51 +0000311 Tmp.setKind(tok::eod);
Ted Kremeneke5680f32008-12-23 01:30:52 +0000312 Tmp.clearFlag(Token::StartOfLine);
313 Tmp.setIdentifierInfo(0);
Ted Kremenekb978c662009-01-08 01:17:37 +0000314 EmitToken(Tmp);
Ted Kremeneke5680f32008-12-23 01:30:52 +0000315 ParsingPreprocessorDirective = false;
316 }
Mike Stump1eb44332009-09-09 15:08:12 +0000317
Abramo Bagnarac4bf2b92010-12-22 08:23:18 +0000318 if (Tok.is(tok::raw_identifier)) {
Kovarththanan Rajaratnam65cc1e82010-03-13 08:53:33 +0000319 PP.LookUpIdentifierInfo(Tok);
Ted Kremeneke4f6b1e2009-02-10 22:27:09 +0000320 EmitToken(Tok);
Ted Kremeneke5680f32008-12-23 01:30:52 +0000321 continue;
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000322 }
Ted Kremeneke5680f32008-12-23 01:30:52 +0000323
324 if (Tok.is(tok::hash) && Tok.isAtStartOfLine()) {
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000325 // Special processing for #include. Store the '#' token and lex
326 // the next token.
Ted Kremeneke5680f32008-12-23 01:30:52 +0000327 assert(!ParsingPreprocessorDirective);
Ted Kremenekfb645b62008-12-11 23:36:38 +0000328 Offset HashOff = (Offset) Out.tell();
Ted Kremenekdad7b342008-12-12 18:31:09 +0000329
330 // Get the next token.
Ted Kremenek5c53f4c2010-07-27 02:58:57 +0000331 Token NextTok;
332 L.LexFromRawLexer(NextTok);
Chris Lattner21356192009-04-19 07:25:40 +0000333
Ted Kremenek5c53f4c2010-07-27 02:58:57 +0000334 // If we see the start of line, then we had a null directive "#". In
335 // this case, discard both tokens.
336 if (NextTok.isAtStartOfLine())
Chris Lattner21356192009-04-19 07:25:40 +0000337 goto NextToken;
Abramo Bagnarac4bf2b92010-12-22 08:23:18 +0000338
Ted Kremenek5c53f4c2010-07-27 02:58:57 +0000339 // The token is the start of a directive. Emit it.
340 EmitToken(Tok);
341 Tok = NextTok;
Mike Stump1eb44332009-09-09 15:08:12 +0000342
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000343 // Did we see 'include'/'import'/'include_next'?
Abramo Bagnarac4bf2b92010-12-22 08:23:18 +0000344 if (Tok.isNot(tok::raw_identifier)) {
Ted Kremeneke4f6b1e2009-02-10 22:27:09 +0000345 EmitToken(Tok);
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000346 continue;
Ted Kremeneke4f6b1e2009-02-10 22:27:09 +0000347 }
Mike Stump1eb44332009-09-09 15:08:12 +0000348
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000349 IdentifierInfo* II = PP.LookUpIdentifierInfo(Tok);
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000350 tok::PPKeywordKind K = II->getPPKeywordID();
Mike Stump1eb44332009-09-09 15:08:12 +0000351
Ted Kremeneke5680f32008-12-23 01:30:52 +0000352 ParsingPreprocessorDirective = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000353
Ted Kremeneke5680f32008-12-23 01:30:52 +0000354 switch (K) {
Chris Lattneraa269c22009-04-19 07:32:03 +0000355 case tok::pp_not_keyword:
356 // Invalid directives "#foo" can occur in #if 0 blocks etc, just pass
357 // them through.
Ted Kremeneke5680f32008-12-23 01:30:52 +0000358 default:
359 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000360
Ted Kremeneke5680f32008-12-23 01:30:52 +0000361 case tok::pp_include:
362 case tok::pp_import:
Mike Stump1eb44332009-09-09 15:08:12 +0000363 case tok::pp_include_next: {
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000364 // Save the 'include' token.
Ted Kremenekb978c662009-01-08 01:17:37 +0000365 EmitToken(Tok);
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000366 // Lex the next token as an include string.
367 L.setParsingPreprocessorDirective(true);
Mike Stump1eb44332009-09-09 15:08:12 +0000368 L.LexIncludeFilename(Tok);
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000369 L.setParsingPreprocessorDirective(false);
Ted Kremeneke5680f32008-12-23 01:30:52 +0000370 assert(!Tok.isAtStartOfLine());
Abramo Bagnarac4bf2b92010-12-22 08:23:18 +0000371 if (Tok.is(tok::raw_identifier))
Kovarththanan Rajaratnam65cc1e82010-03-13 08:53:33 +0000372 PP.LookUpIdentifierInfo(Tok);
Mike Stump1eb44332009-09-09 15:08:12 +0000373
Ted Kremeneke5680f32008-12-23 01:30:52 +0000374 break;
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000375 }
Ted Kremeneke5680f32008-12-23 01:30:52 +0000376 case tok::pp_if:
377 case tok::pp_ifdef:
378 case tok::pp_ifndef: {
Ted Kremeneke4f6b1e2009-02-10 22:27:09 +0000379 // Add an entry for '#if' and friends. We initially set the target
380 // index to 0. This will get backpatched when we hit #endif.
Ted Kremenekfb645b62008-12-11 23:36:38 +0000381 PPStartCond.push_back(PPCond.size());
Ted Kremenekdad7b342008-12-12 18:31:09 +0000382 PPCond.push_back(std::make_pair(HashOff, 0U));
Ted Kremeneke5680f32008-12-23 01:30:52 +0000383 break;
Ted Kremenekfb645b62008-12-11 23:36:38 +0000384 }
Ted Kremeneke5680f32008-12-23 01:30:52 +0000385 case tok::pp_endif: {
Ted Kremenekfb645b62008-12-11 23:36:38 +0000386 // Add an entry for '#endif'. We set the target table index to itself.
Ted Kremenekdad7b342008-12-12 18:31:09 +0000387 // This will later be set to zero when emitting to the PTH file. We
388 // use 0 for uninitialized indices because that is easier to debug.
Ted Kremenekfb645b62008-12-11 23:36:38 +0000389 unsigned index = PPCond.size();
Ted Kremenekfb645b62008-12-11 23:36:38 +0000390 // Backpatch the opening '#if' entry.
Ted Kremenekdad7b342008-12-12 18:31:09 +0000391 assert(!PPStartCond.empty());
392 assert(PPCond.size() > PPStartCond.back());
Ted Kremenekfb645b62008-12-11 23:36:38 +0000393 assert(PPCond[PPStartCond.back()].second == 0);
394 PPCond[PPStartCond.back()].second = index;
Mike Stump1eb44332009-09-09 15:08:12 +0000395 PPStartCond.pop_back();
396 // Add the new entry to PPCond.
Ted Kremenekdad7b342008-12-12 18:31:09 +0000397 PPCond.push_back(std::make_pair(HashOff, index));
Ted Kremenek726080d2009-02-10 22:43:16 +0000398 EmitToken(Tok);
Mike Stump1eb44332009-09-09 15:08:12 +0000399
Ted Kremenek726080d2009-02-10 22:43:16 +0000400 // Some files have gibberish on the same line as '#endif'.
401 // Discard these tokens.
Chris Lattnerd4b14462009-04-19 07:15:51 +0000402 do
403 L.LexFromRawLexer(Tok);
404 while (Tok.isNot(tok::eof) && !Tok.isAtStartOfLine());
Ted Kremenek726080d2009-02-10 22:43:16 +0000405 // We have the next token in hand.
406 // Don't immediately lex the next one.
Mike Stump1eb44332009-09-09 15:08:12 +0000407 goto NextToken;
Ted Kremenekfb645b62008-12-11 23:36:38 +0000408 }
Ted Kremeneke5680f32008-12-23 01:30:52 +0000409 case tok::pp_elif:
410 case tok::pp_else: {
411 // Add an entry for #elif or #else.
Ted Kremenekdad7b342008-12-12 18:31:09 +0000412 // This serves as both a closing and opening of a conditional block.
413 // This means that its entry will get backpatched later.
Ted Kremenekfb645b62008-12-11 23:36:38 +0000414 unsigned index = PPCond.size();
Ted Kremenekfb645b62008-12-11 23:36:38 +0000415 // Backpatch the previous '#if' entry.
Ted Kremenekdad7b342008-12-12 18:31:09 +0000416 assert(!PPStartCond.empty());
417 assert(PPCond.size() > PPStartCond.back());
Ted Kremenekfb645b62008-12-11 23:36:38 +0000418 assert(PPCond[PPStartCond.back()].second == 0);
419 PPCond[PPStartCond.back()].second = index;
420 PPStartCond.pop_back();
421 // Now add '#elif' as a new block opening.
Ted Kremenekdad7b342008-12-12 18:31:09 +0000422 PPCond.push_back(std::make_pair(HashOff, 0U));
423 PPStartCond.push_back(index);
Ted Kremeneke5680f32008-12-23 01:30:52 +0000424 break;
425 }
Ted Kremenekfb645b62008-12-11 23:36:38 +0000426 }
Ted Kremeneke4f6b1e2009-02-10 22:27:09 +0000427 }
Mike Stump1eb44332009-09-09 15:08:12 +0000428
Ted Kremeneke4f6b1e2009-02-10 22:27:09 +0000429 EmitToken(Tok);
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000430 }
Ted Kremeneke4f6b1e2009-02-10 22:27:09 +0000431 while (Tok.isNot(tok::eof));
Ted Kremenekb978c662009-01-08 01:17:37 +0000432
Ted Kremenekdad7b342008-12-12 18:31:09 +0000433 assert(PPStartCond.empty() && "Error: imblanced preprocessor conditionals.");
Ted Kremenekb978c662009-01-08 01:17:37 +0000434
Ted Kremenekfb645b62008-12-11 23:36:38 +0000435 // Next write out PPCond.
436 Offset PPCondOff = (Offset) Out.tell();
Ted Kremenekdad7b342008-12-12 18:31:09 +0000437
438 // Write out the size of PPCond so that clients can identifer empty tables.
Ted Kremenekb978c662009-01-08 01:17:37 +0000439 Emit32(PPCond.size());
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000440
Ted Kremenekdad7b342008-12-12 18:31:09 +0000441 for (unsigned i = 0, e = PPCond.size(); i!=e; ++i) {
Kovarththanan Rajaratnam8f0d2702010-03-18 07:18:10 +0000442 Emit32(PPCond[i].first - TokenOff);
Ted Kremenekdad7b342008-12-12 18:31:09 +0000443 uint32_t x = PPCond[i].second;
444 assert(x != 0 && "PPCond entry not backpatched.");
445 // Emit zero for #endifs. This allows us to do checking when
446 // we read the PTH file back in.
Ted Kremenekb978c662009-01-08 01:17:37 +0000447 Emit32(x == i ? 0 : x);
Ted Kremenekfb645b62008-12-11 23:36:38 +0000448 }
449
Kovarththanan Rajaratnam8f0d2702010-03-18 07:18:10 +0000450 return PTHEntry(TokenOff, PPCondOff);
Ted Kremenekbe295332009-01-08 02:44:06 +0000451}
452
Ted Kremenek277faca2009-01-27 00:01:05 +0000453Offset PTHWriter::EmitCachedSpellings() {
454 // Write each cached strings to the PTH file.
455 Offset SpellingsOff = Out.tell();
Mike Stump1eb44332009-09-09 15:08:12 +0000456
Ted Kremenek277faca2009-01-27 00:01:05 +0000457 for (std::vector<llvm::StringMapEntry<OffsetOpt>*>::iterator
Chris Lattnerf2390362009-03-28 00:16:20 +0000458 I = StrEntries.begin(), E = StrEntries.end(); I!=E; ++I)
459 EmitBuf((*I)->getKeyData(), (*I)->getKeyLength()+1 /*nul included*/);
Mike Stump1eb44332009-09-09 15:08:12 +0000460
Ted Kremenek277faca2009-01-27 00:01:05 +0000461 return SpellingsOff;
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000462}
Ted Kremenek85888962008-10-21 00:54:44 +0000463
Kovarththanan Rajaratnam74e485e2010-03-14 07:38:15 +0000464void PTHWriter::GeneratePTH(const std::string &MainFile) {
Ted Kremeneke1b64982009-01-26 21:43:14 +0000465 // Generate the prologue.
Richard Smithc141b512012-08-17 03:55:43 +0000466 Out << "cfe-pth" << '\0';
Ted Kremenek67d15052009-01-26 21:50:21 +0000467 Emit32(PTHManager::Version);
Mike Stump1eb44332009-09-09 15:08:12 +0000468
Ted Kremeneka4bd8eb2009-02-11 23:34:32 +0000469 // Leave 4 words for the prologue.
470 Offset PrologueOffset = Out.tell();
Chris Lattnerf2390362009-03-28 00:16:20 +0000471 for (unsigned i = 0; i < 4; ++i)
472 Emit32(0);
Mike Stump1eb44332009-09-09 15:08:12 +0000473
Ted Kremenekd5cded42009-03-19 22:10:38 +0000474 // Write the name of the MainFile.
Kovarththanan Rajaratnam2e6051a2010-03-14 07:55:43 +0000475 if (!MainFile.empty()) {
Kovarththanan Rajaratnam8f0d2702010-03-18 07:18:10 +0000476 EmitString(MainFile);
Chris Lattnerf2390362009-03-28 00:16:20 +0000477 } else {
Ted Kremenekd5cded42009-03-19 22:10:38 +0000478 // String with 0 bytes.
479 Emit16(0);
480 }
481 Emit8(0);
Mike Stump1eb44332009-09-09 15:08:12 +0000482
Ted Kremenek85888962008-10-21 00:54:44 +0000483 // Iterate over all the files in SourceManager. Create a lexer
484 // for each file and cache the tokens.
Chris Lattnerc6fe32a2009-01-17 03:48:08 +0000485 SourceManager &SM = PP.getSourceManager();
David Blaikie4e4d0842012-03-11 07:00:24 +0000486 const LangOptions &LOpts = PP.getLangOpts();
Mike Stump1eb44332009-09-09 15:08:12 +0000487
Chris Lattnerc6fe32a2009-01-17 03:48:08 +0000488 for (SourceManager::fileinfo_iterator I = SM.fileinfo_begin(),
489 E = SM.fileinfo_end(); I != E; ++I) {
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000490 const SrcMgr::ContentCache &C = *I->second;
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +0000491 const FileEntry *FE = C.OrigEntry;
Mike Stump1eb44332009-09-09 15:08:12 +0000492
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000493 // FIXME: Handle files with non-absolute paths.
Michael J. Spencer256053b2010-12-17 21:22:22 +0000494 if (llvm::sys::path::is_relative(FE->getName()))
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000495 continue;
Ted Kremenek85888962008-10-21 00:54:44 +0000496
Chris Lattnere127a0d2010-04-20 20:35:58 +0000497 const llvm::MemoryBuffer *B = C.getBuffer(PP.getDiagnostics(), SM);
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000498 if (!B) continue;
Ted Kremenekfb645b62008-12-11 23:36:38 +0000499
Chris Lattner2b2453a2009-01-17 06:22:33 +0000500 FileID FID = SM.createFileID(FE, SourceLocation(), SrcMgr::C_User);
Chris Lattner6e290142009-11-30 04:18:44 +0000501 const llvm::MemoryBuffer *FromFile = SM.getBuffer(FID);
502 Lexer L(FID, FromFile, SM, LOpts);
Ted Kremenekd8c02922009-02-10 22:16:22 +0000503 PM.insert(FE, LexTokens(L));
Daniel Dunbar31309ab2008-11-26 02:18:33 +0000504 }
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000505
506 // Write out the identifier table.
Chris Lattnerf2390362009-03-28 00:16:20 +0000507 const std::pair<Offset,Offset> &IdTableOff = EmitIdentifierTable();
Mike Stump1eb44332009-09-09 15:08:12 +0000508
Ted Kremenekbe295332009-01-08 02:44:06 +0000509 // Write out the cached strings table.
Ted Kremenek277faca2009-01-27 00:01:05 +0000510 Offset SpellingOff = EmitCachedSpellings();
Mike Stump1eb44332009-09-09 15:08:12 +0000511
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000512 // Write out the file table.
Mike Stump1eb44332009-09-09 15:08:12 +0000513 Offset FileTableOff = EmitFileTable();
514
Ted Kremeneka4bd8eb2009-02-11 23:34:32 +0000515 // Finally, write the prologue.
516 Out.seek(PrologueOffset);
Ted Kremenekb978c662009-01-08 01:17:37 +0000517 Emit32(IdTableOff.first);
Ted Kremenekf1de4642009-02-11 16:06:55 +0000518 Emit32(IdTableOff.second);
Ted Kremenekb978c662009-01-08 01:17:37 +0000519 Emit32(FileTableOff);
Ted Kremenek277faca2009-01-27 00:01:05 +0000520 Emit32(SpellingOff);
Ted Kremenekb978c662009-01-08 01:17:37 +0000521}
522
Chris Lattner52ba8702009-03-28 00:55:35 +0000523namespace {
524/// StatListener - A simple "interpose" object used to monitor stat calls
525/// invoked by FileManager while processing the original sources used
526/// as input to PTH generation. StatListener populates the PTHWriter's
527/// file map with stat information for directories as well as negative stats.
528/// Stat information for files are populated elsewhere.
Chris Lattner10e286a2010-11-23 19:19:34 +0000529class StatListener : public FileSystemStatCache {
Chris Lattner52ba8702009-03-28 00:55:35 +0000530 PTHMap &PM;
531public:
532 StatListener(PTHMap &pm) : PM(pm) {}
533 ~StatListener() {}
Mike Stump1eb44332009-09-09 15:08:12 +0000534
Rafael Espindola0fda0f72013-08-01 21:42:11 +0000535 LookupResult getStat(const char *Path, FileData &Data, bool isFile,
Stephen Hines651f13c2014-04-23 16:59:28 -0700536 vfs::File **F, vfs::FileSystem &FS) override {
537 LookupResult Result = statChained(Path, Data, isFile, F, FS);
Mike Stump1eb44332009-09-09 15:08:12 +0000538
Chris Lattnerd6f61112010-11-23 20:05:15 +0000539 if (Result == CacheMissing) // Failed 'stat'.
Chris Lattner10e286a2010-11-23 19:19:34 +0000540 PM.insert(PTHEntryKeyVariant(Path), PTHEntry());
Rafael Espindola0fda0f72013-08-01 21:42:11 +0000541 else if (Data.IsDirectory) {
Chris Lattner52ba8702009-03-28 00:55:35 +0000542 // Only cache directories with absolute paths.
Michael J. Spencer256053b2010-12-17 21:22:22 +0000543 if (llvm::sys::path::is_relative(Path))
Chris Lattner10e286a2010-11-23 19:19:34 +0000544 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +0000545
Rafael Espindola0fda0f72013-08-01 21:42:11 +0000546 PM.insert(PTHEntryKeyVariant(&Data, Path), PTHEntry());
Chris Lattner52ba8702009-03-28 00:55:35 +0000547 }
Mike Stump1eb44332009-09-09 15:08:12 +0000548
Chris Lattner10e286a2010-11-23 19:19:34 +0000549 return Result;
Chris Lattner52ba8702009-03-28 00:55:35 +0000550 }
551};
552} // end anonymous namespace
553
554
Eli Friedmanf54fce82009-05-19 01:02:07 +0000555void clang::CacheTokens(Preprocessor &PP, llvm::raw_fd_ostream* OS) {
Ted Kremenekd5cded42009-03-19 22:10:38 +0000556 // Get the name of the main file.
557 const SourceManager &SrcMgr = PP.getSourceManager();
558 const FileEntry *MainFile = SrcMgr.getFileEntryForID(SrcMgr.getMainFileID());
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000559 SmallString<128> MainFilePath(MainFile->getName());
Mike Stump1eb44332009-09-09 15:08:12 +0000560
Michael J. Spencerfbfd1802010-12-21 16:45:57 +0000561 llvm::sys::fs::make_absolute(MainFilePath);
Ted Kremenekad6ce5c2009-02-13 22:07:44 +0000562
563 // Create the PTHWriter.
Eli Friedmanf54fce82009-05-19 01:02:07 +0000564 PTHWriter PW(*OS, PP);
Mike Stump1eb44332009-09-09 15:08:12 +0000565
Ted Kremenekad6ce5c2009-02-13 22:07:44 +0000566 // Install the 'stat' system call listener in the FileManager.
Douglas Gregor52e71082009-10-16 18:18:30 +0000567 StatListener *StatCache = new StatListener(PW.getPM());
568 PP.getFileManager().addStatCache(StatCache, /*AtBeginning=*/true);
Mike Stump1eb44332009-09-09 15:08:12 +0000569
Ted Kremenekad6ce5c2009-02-13 22:07:44 +0000570 // Lex through the entire file. This will populate SourceManager with
571 // all of the header information.
572 Token Tok;
Chris Lattnere127a0d2010-04-20 20:35:58 +0000573 PP.EnterMainSourceFile();
Ted Kremenekad6ce5c2009-02-13 22:07:44 +0000574 do { PP.Lex(Tok); } while (Tok.isNot(tok::eof));
Mike Stump1eb44332009-09-09 15:08:12 +0000575
Ted Kremenekad6ce5c2009-02-13 22:07:44 +0000576 // Generate the PTH file.
Douglas Gregor52e71082009-10-16 18:18:30 +0000577 PP.getFileManager().removeStatCache(StatCache);
Kovarththanan Rajaratnam74e485e2010-03-14 07:38:15 +0000578 PW.GeneratePTH(MainFilePath.str());
Ted Kremenek85888962008-10-21 00:54:44 +0000579}
Ted Kremeneke0ea5dc2009-02-10 01:06:17 +0000580
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000581//===----------------------------------------------------------------------===//
582
Kovarththanan Rajaratnam48673e62010-03-14 06:48:05 +0000583namespace {
Chris Lattnerf2390362009-03-28 00:16:20 +0000584class PTHIdKey {
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000585public:
586 const IdentifierInfo* II;
587 uint32_t FileOffset;
588};
589
Benjamin Kramerbd218282009-11-28 10:07:24 +0000590class PTHIdentifierTableTrait {
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000591public:
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000592 typedef PTHIdKey* key_type;
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000593 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +0000594
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000595 typedef uint32_t data_type;
596 typedef data_type data_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +0000597
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000598 static unsigned ComputeHash(PTHIdKey* key) {
Daniel Dunbar01eb9b92009-10-18 21:17:35 +0000599 return llvm::HashString(key->II->getName());
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000600 }
Mike Stump1eb44332009-09-09 15:08:12 +0000601
602 static std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +0000603 EmitKeyDataLength(raw_ostream& Out, const PTHIdKey* key, uint32_t) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700604 using namespace llvm::support;
Daniel Dunbare013d682009-10-18 20:26:12 +0000605 unsigned n = key->II->getLength() + 1;
Stephen Hines651f13c2014-04-23 16:59:28 -0700606 endian::Writer<little>(Out).write<uint16_t>(n);
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000607 return std::make_pair(n, sizeof(uint32_t));
608 }
Mike Stump1eb44332009-09-09 15:08:12 +0000609
Chris Lattner5f9e2722011-07-23 10:55:15 +0000610 static void EmitKey(raw_ostream& Out, PTHIdKey* key, unsigned n) {
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000611 // Record the location of the key data. This is used when generating
612 // the mapping from persistent IDs to strings.
613 key->FileOffset = Out.tell();
Daniel Dunbare013d682009-10-18 20:26:12 +0000614 Out.write(key->II->getNameStart(), n);
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000615 }
Mike Stump1eb44332009-09-09 15:08:12 +0000616
Chris Lattner5f9e2722011-07-23 10:55:15 +0000617 static void EmitData(raw_ostream& Out, PTHIdKey*, uint32_t pID,
Ted Kremenek337edcd2009-02-12 03:26:59 +0000618 unsigned) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700619 using namespace llvm::support;
620 endian::Writer<little>(Out).write<uint32_t>(pID);
Mike Stump1eb44332009-09-09 15:08:12 +0000621 }
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000622};
623} // end anonymous namespace
624
625/// EmitIdentifierTable - Emits two tables to the PTH file. The first is
626/// a hashtable mapping from identifier strings to persistent IDs. The second
627/// is a straight table mapping from persistent IDs to string data (the
628/// keys of the first table).
629///
630std::pair<Offset,Offset> PTHWriter::EmitIdentifierTable() {
631 // Build two maps:
632 // (1) an inverse map from persistent IDs -> (IdentifierInfo*,Offset)
633 // (2) a map from (IdentifierInfo*, Offset)* -> persistent IDs
634
635 // Note that we use 'calloc', so all the bytes are 0.
Chris Lattnerf2390362009-03-28 00:16:20 +0000636 PTHIdKey *IIDMap = (PTHIdKey*)calloc(idcount, sizeof(PTHIdKey));
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000637
638 // Create the hashtable.
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000639 OnDiskChainedHashTableGenerator<PTHIdentifierTableTrait> IIOffMap;
Mike Stump1eb44332009-09-09 15:08:12 +0000640
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000641 // Generate mapping from persistent IDs -> IdentifierInfo*.
Chris Lattnerf2390362009-03-28 00:16:20 +0000642 for (IDMap::iterator I = IM.begin(), E = IM.end(); I != E; ++I) {
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000643 // Decrement by 1 because we are using a vector for the lookup and
644 // 0 is reserved for NULL.
645 assert(I->second > 0);
646 assert(I->second-1 < idcount);
647 unsigned idx = I->second-1;
Mike Stump1eb44332009-09-09 15:08:12 +0000648
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000649 // Store the mapping from persistent ID to IdentifierInfo*
650 IIDMap[idx].II = I->first;
Mike Stump1eb44332009-09-09 15:08:12 +0000651
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000652 // Store the reverse mapping in a hashtable.
653 IIOffMap.insert(&IIDMap[idx], I->second);
654 }
Mike Stump1eb44332009-09-09 15:08:12 +0000655
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000656 // Write out the inverse map first. This causes the PCIDKey entries to
657 // record PTH file offsets for the string data. This is used to write
658 // the second table.
659 Offset StringTableOffset = IIOffMap.Emit(Out);
Mike Stump1eb44332009-09-09 15:08:12 +0000660
661 // Now emit the table mapping from persistent IDs to PTH file offsets.
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000662 Offset IDOff = Out.tell();
663 Emit32(idcount); // Emit the number of identifiers.
Chris Lattnerf2390362009-03-28 00:16:20 +0000664 for (unsigned i = 0 ; i < idcount; ++i)
665 Emit32(IIDMap[i].FileOffset);
Mike Stump1eb44332009-09-09 15:08:12 +0000666
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000667 // Finally, release the inverse map.
668 free(IIDMap);
Mike Stump1eb44332009-09-09 15:08:12 +0000669
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000670 return std::make_pair(IDOff, StringTableOffset);
671}