blob: d909d526b51898e4d1281786ea6cce277d37b902 [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"
Chris Lattner10e286a2010-11-23 19:19:34 +000020#include "clang/Basic/SourceManager.h"
Ted Kremenek85888962008-10-21 00:54:44 +000021#include "clang/Lex/Lexer.h"
22#include "clang/Lex/Preprocessor.h"
Daniel Dunbardf5a2372009-10-17 20:43:58 +000023#include "llvm/ADT/StringExtras.h"
24#include "llvm/ADT/StringMap.h"
Stephen Hines651f13c2014-04-23 16:59:28 -070025#include "llvm/Support/EndianStream.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"
Stephen Hines6bcf27b2014-05-29 04:14:42 -070028#include "llvm/Support/OnDiskHashTable.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;
Ted Kremenekf0e1f792009-02-10 01:14:45 +000038
39//===----------------------------------------------------------------------===//
40// PTH-specific stuff.
41//===----------------------------------------------------------------------===//
42
Stephen Hines6bcf27b2014-05-29 04:14:42 -070043typedef uint32_t Offset;
44
Ted Kremenekbe295332009-01-08 02:44:06 +000045namespace {
Benjamin Kramerbd218282009-11-28 10:07:24 +000046class PTHEntry {
Mike Stump1eb44332009-09-09 15:08:12 +000047 Offset TokenData, PPCondData;
Ted Kremenekbe295332009-01-08 02:44:06 +000048
Mike Stump1eb44332009-09-09 15:08:12 +000049public:
Ted Kremeneka4b44dd2009-02-13 19:13:46 +000050 PTHEntry() {}
Ted Kremenekbe295332009-01-08 02:44:06 +000051
Ted Kremeneka4b44dd2009-02-13 19:13:46 +000052 PTHEntry(Offset td, Offset ppcd)
Ted Kremenek277faca2009-01-27 00:01:05 +000053 : TokenData(td), PPCondData(ppcd) {}
Mike Stump1eb44332009-09-09 15:08:12 +000054
55 Offset getTokenOffset() const { return TokenData; }
Ted Kremenekbe295332009-01-08 02:44:06 +000056 Offset getPPCondTableOffset() const { return PPCondData; }
Ted Kremenek277faca2009-01-27 00:01:05 +000057};
Mike Stump1eb44332009-09-09 15:08:12 +000058
59
Benjamin Kramerbd218282009-11-28 10:07:24 +000060class PTHEntryKeyVariant {
Ted Kremenekad6ce5c2009-02-13 22:07:44 +000061 union { const FileEntry* FE; const char* Path; };
Ted Kremeneka4b44dd2009-02-13 19:13:46 +000062 enum { IsFE = 0x1, IsDE = 0x2, IsNoExist = 0x0 } Kind;
Rafael Espindola0fda0f72013-08-01 21:42:11 +000063 FileData *Data;
64
Ted Kremenekd8c02922009-02-10 22:16:22 +000065public:
Stephen Hines6bcf27b2014-05-29 04:14:42 -070066 PTHEntryKeyVariant(const FileEntry *fe) : FE(fe), Kind(IsFE), Data(nullptr) {}
Ted Kremenekad6ce5c2009-02-13 22:07:44 +000067
Rafael Espindola0fda0f72013-08-01 21:42:11 +000068 PTHEntryKeyVariant(FileData *Data, const char *path)
69 : Path(path), Kind(IsDE), Data(new FileData(*Data)) {}
Ted Kremenekad6ce5c2009-02-13 22:07:44 +000070
Rafael Espindola0fda0f72013-08-01 21:42:11 +000071 explicit PTHEntryKeyVariant(const char *path)
Stephen Hines6bcf27b2014-05-29 04:14:42 -070072 : Path(path), Kind(IsNoExist), Data(nullptr) {}
Mike Stump1eb44332009-09-09 15:08:12 +000073
Ted Kremenekad6ce5c2009-02-13 22:07:44 +000074 bool isFile() const { return Kind == IsFE; }
Mike Stump1eb44332009-09-09 15:08:12 +000075
Chris Lattner5f9e2722011-07-23 10:55:15 +000076 StringRef getString() const {
Ted Kremenekad6ce5c2009-02-13 22:07:44 +000077 return Kind == IsFE ? FE->getName() : Path;
Ted Kremeneka4b44dd2009-02-13 19:13:46 +000078 }
Mike Stump1eb44332009-09-09 15:08:12 +000079
Ted Kremeneka4b44dd2009-02-13 19:13:46 +000080 unsigned getKind() const { return (unsigned) Kind; }
Mike Stump1eb44332009-09-09 15:08:12 +000081
Chris Lattner5f9e2722011-07-23 10:55:15 +000082 void EmitData(raw_ostream& Out) {
Stephen Hines651f13c2014-04-23 16:59:28 -070083 using namespace llvm::support;
84 endian::Writer<little> LE(Out);
Ted Kremeneka4b44dd2009-02-13 19:13:46 +000085 switch (Kind) {
Rafael Espindola0fda0f72013-08-01 21:42:11 +000086 case IsFE: {
Mike Stumpb7166332010-01-20 02:03:14 +000087 // Emit stat information.
Rafael Espindola0fda0f72013-08-01 21:42:11 +000088 llvm::sys::fs::UniqueID UID = FE->getUniqueID();
Stephen Hines651f13c2014-04-23 16:59:28 -070089 LE.write<uint64_t>(UID.getFile());
90 LE.write<uint64_t>(UID.getDevice());
91 LE.write<uint64_t>(FE->getModificationTime());
92 LE.write<uint64_t>(FE->getSize());
Rafael Espindola0fda0f72013-08-01 21:42:11 +000093 } break;
Mike Stumpb7166332010-01-20 02:03:14 +000094 case IsDE:
95 // Emit stat information.
Stephen Hines651f13c2014-04-23 16:59:28 -070096 LE.write<uint64_t>(Data->UniqueID.getFile());
97 LE.write<uint64_t>(Data->UniqueID.getDevice());
98 LE.write<uint64_t>(Data->ModTime);
99 LE.write<uint64_t>(Data->Size);
Rafael Espindola0fda0f72013-08-01 21:42:11 +0000100 delete Data;
Mike Stumpb7166332010-01-20 02:03:14 +0000101 break;
102 default:
103 break;
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000104 }
105 }
Mike Stump1eb44332009-09-09 15:08:12 +0000106
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000107 unsigned getRepresentationLength() const {
Ted Kremenekad6ce5c2009-02-13 22:07:44 +0000108 return Kind == IsNoExist ? 0 : 4 + 4 + 2 + 8 + 8;
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000109 }
110};
Mike Stump1eb44332009-09-09 15:08:12 +0000111
Benjamin Kramerbd218282009-11-28 10:07:24 +0000112class FileEntryPTHEntryInfo {
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000113public:
114 typedef PTHEntryKeyVariant key_type;
Ted Kremenekd8c02922009-02-10 22:16:22 +0000115 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +0000116
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000117 typedef PTHEntry data_type;
118 typedef const PTHEntry& data_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +0000119
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700120 typedef unsigned hash_value_type;
121 typedef unsigned offset_type;
122
123 static hash_value_type ComputeHash(PTHEntryKeyVariant V) {
Daniel Dunbardf5a2372009-10-17 20:43:58 +0000124 return llvm::HashString(V.getString());
Ted Kremenekd8c02922009-02-10 22:16:22 +0000125 }
Mike Stump1eb44332009-09-09 15:08:12 +0000126
127 static std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +0000128 EmitKeyDataLength(raw_ostream& Out, PTHEntryKeyVariant V,
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000129 const PTHEntry& E) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700130 using namespace llvm::support;
131 endian::Writer<little> LE(Out);
Ted Kremenekd8c02922009-02-10 22:16:22 +0000132
Daniel Dunbardf5a2372009-10-17 20:43:58 +0000133 unsigned n = V.getString().size() + 1 + 1;
Stephen Hines651f13c2014-04-23 16:59:28 -0700134 LE.write<uint16_t>(n);
Mike Stump1eb44332009-09-09 15:08:12 +0000135
Ted Kremenekad6ce5c2009-02-13 22:07:44 +0000136 unsigned m = V.getRepresentationLength() + (V.isFile() ? 4 + 4 : 0);
Stephen Hines651f13c2014-04-23 16:59:28 -0700137 LE.write<uint8_t>(m);
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000138
139 return std::make_pair(n, m);
Ted Kremenekd8c02922009-02-10 22:16:22 +0000140 }
Mike Stump1eb44332009-09-09 15:08:12 +0000141
Chris Lattner5f9e2722011-07-23 10:55:15 +0000142 static void EmitKey(raw_ostream& Out, PTHEntryKeyVariant V, unsigned n){
Stephen Hines651f13c2014-04-23 16:59:28 -0700143 using namespace llvm::support;
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000144 // Emit the entry kind.
Stephen Hines651f13c2014-04-23 16:59:28 -0700145 endian::Writer<little>(Out).write<uint8_t>((unsigned)V.getKind());
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000146 // Emit the string.
Daniel Dunbardf5a2372009-10-17 20:43:58 +0000147 Out.write(V.getString().data(), n - 1);
Ted Kremenekd8c02922009-02-10 22:16:22 +0000148 }
Mike Stump1eb44332009-09-09 15:08:12 +0000149
Chris Lattner5f9e2722011-07-23 10:55:15 +0000150 static void EmitData(raw_ostream& Out, PTHEntryKeyVariant V,
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000151 const PTHEntry& E, unsigned) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700152 using namespace llvm::support;
153 endian::Writer<little> LE(Out);
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000154
155 // For file entries emit the offsets into the PTH file for token data
156 // and the preprocessor blocks table.
Ted Kremenekad6ce5c2009-02-13 22:07:44 +0000157 if (V.isFile()) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700158 LE.write<uint32_t>(E.getTokenOffset());
159 LE.write<uint32_t>(E.getPPCondTableOffset());
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000160 }
Mike Stump1eb44332009-09-09 15:08:12 +0000161
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000162 // Emit any other data associated with the key (i.e., stat information).
163 V.EmitData(Out);
Mike Stump1eb44332009-09-09 15:08:12 +0000164 }
Ted Kremenekd8c02922009-02-10 22:16:22 +0000165};
Mike Stump1eb44332009-09-09 15:08:12 +0000166
Ted Kremenek277faca2009-01-27 00:01:05 +0000167class OffsetOpt {
168 bool valid;
169 Offset off;
170public:
171 OffsetOpt() : valid(false) {}
172 bool hasOffset() const { return valid; }
173 Offset getOffset() const { assert(valid); return off; }
174 void setOffset(Offset o) { off = o; valid = true; }
Ted Kremenekbe295332009-01-08 02:44:06 +0000175};
176} // end anonymous namespace
177
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700178typedef llvm::OnDiskChainedHashTableGenerator<FileEntryPTHEntryInfo> PTHMap;
Ted Kremenek85888962008-10-21 00:54:44 +0000179
Ted Kremenekb978c662009-01-08 01:17:37 +0000180namespace {
Benjamin Kramerbd218282009-11-28 10:07:24 +0000181class PTHWriter {
Kovarththanan Rajaratnam622ab502010-03-18 07:45:30 +0000182 typedef llvm::DenseMap<const IdentifierInfo*,uint32_t> IDMap;
183 typedef llvm::StringMap<OffsetOpt, llvm::BumpPtrAllocator> CachedStrsTy;
184
Ted Kremenekb978c662009-01-08 01:17:37 +0000185 IDMap IM;
186 llvm::raw_fd_ostream& Out;
187 Preprocessor& PP;
188 uint32_t idcount;
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000189 PTHMap PM;
Ted Kremenekbe295332009-01-08 02:44:06 +0000190 CachedStrsTy CachedStrs;
Ted Kremenek277faca2009-01-27 00:01:05 +0000191 Offset CurStrOffset;
192 std::vector<llvm::StringMapEntry<OffsetOpt>*> StrEntries;
Ted Kremenek8f174e12008-12-23 02:52:12 +0000193
Ted Kremenekb978c662009-01-08 01:17:37 +0000194 //// Get the persistent id for the given IdentifierInfo*.
195 uint32_t ResolveID(const IdentifierInfo* II);
Mike Stump1eb44332009-09-09 15:08:12 +0000196
Ted Kremenekb978c662009-01-08 01:17:37 +0000197 /// Emit a token to the PTH file.
198 void EmitToken(const Token& T);
199
Stephen Hines651f13c2014-04-23 16:59:28 -0700200 void Emit8(uint32_t V) {
201 using namespace llvm::support;
202 endian::Writer<little>(Out).write<uint8_t>(V);
203 }
Mike Stump1eb44332009-09-09 15:08:12 +0000204
Stephen Hines651f13c2014-04-23 16:59:28 -0700205 void Emit16(uint32_t V) {
206 using namespace llvm::support;
207 endian::Writer<little>(Out).write<uint16_t>(V);
208 }
Mike Stump1eb44332009-09-09 15:08:12 +0000209
Stephen Hines651f13c2014-04-23 16:59:28 -0700210 void Emit32(uint32_t V) {
211 using namespace llvm::support;
212 endian::Writer<little>(Out).write<uint32_t>(V);
213 }
Ted Kremeneke0ea5dc2009-02-10 01:06:17 +0000214
Chris Lattnerf2390362009-03-28 00:16:20 +0000215 void EmitBuf(const char *Ptr, unsigned NumBytes) {
216 Out.write(Ptr, NumBytes);
Ted Kremenekb978c662009-01-08 01:17:37 +0000217 }
Mike Stump1eb44332009-09-09 15:08:12 +0000218
Chris Lattner5f9e2722011-07-23 10:55:15 +0000219 void EmitString(StringRef V) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700220 using namespace llvm::support;
221 endian::Writer<little>(Out).write<uint16_t>(V.size());
Kovarththanan Rajaratnamc3cde072010-03-14 08:35:19 +0000222 EmitBuf(V.data(), V.size());
223 }
224
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000225 /// EmitIdentifierTable - Emits two tables to the PTH file. The first is
226 /// a hashtable mapping from identifier strings to persistent IDs.
227 /// The second is a straight table mapping from persistent IDs to string data
228 /// (the keys of the first table).
Ted Kremenekf1de4642009-02-11 16:06:55 +0000229 std::pair<Offset, Offset> EmitIdentifierTable();
Mike Stump1eb44332009-09-09 15:08:12 +0000230
Ted Kremenekf1de4642009-02-11 16:06:55 +0000231 /// EmitFileTable - Emit a table mapping from file name strings to PTH
232 /// token data.
233 Offset EmitFileTable() { return PM.Emit(Out); }
234
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000235 PTHEntry LexTokens(Lexer& L);
Ted Kremenek277faca2009-01-27 00:01:05 +0000236 Offset EmitCachedSpellings();
Ted Kremenekad6ce5c2009-02-13 22:07:44 +0000237
Ted Kremenekb978c662009-01-08 01:17:37 +0000238public:
Mike Stump1eb44332009-09-09 15:08:12 +0000239 PTHWriter(llvm::raw_fd_ostream& out, Preprocessor& pp)
Ted Kremenek277faca2009-01-27 00:01:05 +0000240 : Out(out), PP(pp), idcount(0), CurStrOffset(0) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000241
Chris Lattner52ba8702009-03-28 00:55:35 +0000242 PTHMap &getPM() { return PM; }
Kovarththanan Rajaratnam74e485e2010-03-14 07:38:15 +0000243 void GeneratePTH(const std::string &MainFile);
Ted Kremenekb978c662009-01-08 01:17:37 +0000244};
245} // end anonymous namespace
Mike Stump1eb44332009-09-09 15:08:12 +0000246
247uint32_t PTHWriter::ResolveID(const IdentifierInfo* II) {
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000248 // Null IdentifierInfo's map to the persistent ID 0.
249 if (!II)
250 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000251
Ted Kremenek85888962008-10-21 00:54:44 +0000252 IDMap::iterator I = IM.find(II);
Chris Lattnerf2390362009-03-28 00:16:20 +0000253 if (I != IM.end())
254 return I->second; // We've already added 1.
Mike Stump1eb44332009-09-09 15:08:12 +0000255
Chris Lattnerf2390362009-03-28 00:16:20 +0000256 IM[II] = ++idcount; // Pre-increment since '0' is reserved for NULL.
257 return idcount;
Ted Kremenek85888962008-10-21 00:54:44 +0000258}
259
Ted Kremenekb978c662009-01-08 01:17:37 +0000260void PTHWriter::EmitToken(const Token& T) {
Ted Kremenekf2a223f2009-02-24 01:26:56 +0000261 // Emit the token kind, flags, and length.
262 Emit32(((uint32_t) T.getKind()) | ((((uint32_t) T.getFlags())) << 8)|
263 (((uint32_t) T.getLength()) << 16));
Mike Stump1eb44332009-09-09 15:08:12 +0000264
Chris Lattnerf2390362009-03-28 00:16:20 +0000265 if (!T.isLiteral()) {
266 Emit32(ResolveID(T.getIdentifierInfo()));
267 } else {
Ted Kremenekf2a223f2009-02-24 01:26:56 +0000268 // We cache *un-cleaned* spellings. This gives us 100% fidelity with the
269 // source code.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000270 StringRef s(T.getLiteralData(), T.getLength());
Ted Kremenek25cbd9f2009-02-24 00:30:21 +0000271
Chris Lattner47246be2009-01-26 19:29:26 +0000272 // Get the string entry.
Stephen Hines176edba2014-12-01 14:53:08 -0800273 auto &E = *CachedStrs.insert(std::make_pair(s, OffsetOpt())).first;
Mike Stump1eb44332009-09-09 15:08:12 +0000274
Ted Kremenekf2a223f2009-02-24 01:26:56 +0000275 // If this is a new string entry, bump the PTH offset.
Stephen Hines176edba2014-12-01 14:53:08 -0800276 if (!E.second.hasOffset()) {
277 E.second.setOffset(CurStrOffset);
278 StrEntries.push_back(&E);
Jay Foad65aa6882011-06-21 15:13:30 +0000279 CurStrOffset += s.size() + 1;
Ted Kremenek277faca2009-01-27 00:01:05 +0000280 }
Mike Stump1eb44332009-09-09 15:08:12 +0000281
Ted Kremenek25cbd9f2009-02-24 00:30:21 +0000282 // Emit the relative offset into the PTH file for the spelling string.
Stephen Hines176edba2014-12-01 14:53:08 -0800283 Emit32(E.second.getOffset());
Ted Kremenekb978c662009-01-08 01:17:37 +0000284 }
Mike Stump1eb44332009-09-09 15:08:12 +0000285
Ted Kremenek25cbd9f2009-02-24 00:30:21 +0000286 // Emit the offset into the original source file of this token so that we
287 // can reconstruct its SourceLocation.
Chris Lattner52c29082009-01-27 06:27:13 +0000288 Emit32(PP.getSourceManager().getFileOffset(T.getLocation()));
Ted Kremenek85888962008-10-21 00:54:44 +0000289}
290
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000291PTHEntry PTHWriter::LexTokens(Lexer& L) {
Ted Kremenek7b78b7c2009-01-19 23:13:15 +0000292 // Pad 0's so that we emit tokens to a 4-byte alignment.
293 // This speed up reading them back in.
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700294 using namespace llvm::support;
295 endian::Writer<little> LE(Out);
296 uint32_t TokenOff = Out.tell();
297 for (uint64_t N = llvm::OffsetToAlignment(TokenOff, 4); N; --N, ++TokenOff)
298 LE.write<uint8_t>(0);
Mike Stump1eb44332009-09-09 15:08:12 +0000299
Ted Kremenekfb645b62008-12-11 23:36:38 +0000300 // Keep track of matching '#if' ... '#endif'.
301 typedef std::vector<std::pair<Offset, unsigned> > PPCondTable;
302 PPCondTable PPCond;
Ted Kremenekdad7b342008-12-12 18:31:09 +0000303 std::vector<unsigned> PPStartCond;
Ted Kremeneke5680f32008-12-23 01:30:52 +0000304 bool ParsingPreprocessorDirective = false;
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000305 Token Tok;
Mike Stump1eb44332009-09-09 15:08:12 +0000306
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000307 do {
308 L.LexFromRawLexer(Tok);
Ted Kremenek726080d2009-02-10 22:43:16 +0000309 NextToken:
310
Ted Kremeneke5680f32008-12-23 01:30:52 +0000311 if ((Tok.isAtStartOfLine() || Tok.is(tok::eof)) &&
312 ParsingPreprocessorDirective) {
Peter Collingbourne84021552011-02-28 02:37:51 +0000313 // Insert an eod token into the token cache. It has the same
Ted Kremeneke5680f32008-12-23 01:30:52 +0000314 // position as the next token that is not on the same line as the
315 // preprocessor directive. Observe that we continue processing
316 // 'Tok' when we exit this branch.
317 Token Tmp = Tok;
Peter Collingbourne84021552011-02-28 02:37:51 +0000318 Tmp.setKind(tok::eod);
Ted Kremeneke5680f32008-12-23 01:30:52 +0000319 Tmp.clearFlag(Token::StartOfLine);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700320 Tmp.setIdentifierInfo(nullptr);
Ted Kremenekb978c662009-01-08 01:17:37 +0000321 EmitToken(Tmp);
Ted Kremeneke5680f32008-12-23 01:30:52 +0000322 ParsingPreprocessorDirective = false;
323 }
Mike Stump1eb44332009-09-09 15:08:12 +0000324
Abramo Bagnarac4bf2b92010-12-22 08:23:18 +0000325 if (Tok.is(tok::raw_identifier)) {
Kovarththanan Rajaratnam65cc1e82010-03-13 08:53:33 +0000326 PP.LookUpIdentifierInfo(Tok);
Ted Kremeneke4f6b1e2009-02-10 22:27:09 +0000327 EmitToken(Tok);
Ted Kremeneke5680f32008-12-23 01:30:52 +0000328 continue;
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000329 }
Ted Kremeneke5680f32008-12-23 01:30:52 +0000330
331 if (Tok.is(tok::hash) && Tok.isAtStartOfLine()) {
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000332 // Special processing for #include. Store the '#' token and lex
333 // the next token.
Ted Kremeneke5680f32008-12-23 01:30:52 +0000334 assert(!ParsingPreprocessorDirective);
Ted Kremenekfb645b62008-12-11 23:36:38 +0000335 Offset HashOff = (Offset) Out.tell();
Ted Kremenekdad7b342008-12-12 18:31:09 +0000336
337 // Get the next token.
Ted Kremenek5c53f4c2010-07-27 02:58:57 +0000338 Token NextTok;
339 L.LexFromRawLexer(NextTok);
Chris Lattner21356192009-04-19 07:25:40 +0000340
Ted Kremenek5c53f4c2010-07-27 02:58:57 +0000341 // If we see the start of line, then we had a null directive "#". In
342 // this case, discard both tokens.
343 if (NextTok.isAtStartOfLine())
Chris Lattner21356192009-04-19 07:25:40 +0000344 goto NextToken;
Abramo Bagnarac4bf2b92010-12-22 08:23:18 +0000345
Ted Kremenek5c53f4c2010-07-27 02:58:57 +0000346 // The token is the start of a directive. Emit it.
347 EmitToken(Tok);
348 Tok = NextTok;
Mike Stump1eb44332009-09-09 15:08:12 +0000349
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000350 // Did we see 'include'/'import'/'include_next'?
Abramo Bagnarac4bf2b92010-12-22 08:23:18 +0000351 if (Tok.isNot(tok::raw_identifier)) {
Ted Kremeneke4f6b1e2009-02-10 22:27:09 +0000352 EmitToken(Tok);
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000353 continue;
Ted Kremeneke4f6b1e2009-02-10 22:27:09 +0000354 }
Mike Stump1eb44332009-09-09 15:08:12 +0000355
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000356 IdentifierInfo* II = PP.LookUpIdentifierInfo(Tok);
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000357 tok::PPKeywordKind K = II->getPPKeywordID();
Mike Stump1eb44332009-09-09 15:08:12 +0000358
Ted Kremeneke5680f32008-12-23 01:30:52 +0000359 ParsingPreprocessorDirective = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000360
Ted Kremeneke5680f32008-12-23 01:30:52 +0000361 switch (K) {
Chris Lattneraa269c22009-04-19 07:32:03 +0000362 case tok::pp_not_keyword:
363 // Invalid directives "#foo" can occur in #if 0 blocks etc, just pass
364 // them through.
Ted Kremeneke5680f32008-12-23 01:30:52 +0000365 default:
366 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000367
Ted Kremeneke5680f32008-12-23 01:30:52 +0000368 case tok::pp_include:
369 case tok::pp_import:
Mike Stump1eb44332009-09-09 15:08:12 +0000370 case tok::pp_include_next: {
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000371 // Save the 'include' token.
Ted Kremenekb978c662009-01-08 01:17:37 +0000372 EmitToken(Tok);
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000373 // Lex the next token as an include string.
374 L.setParsingPreprocessorDirective(true);
Mike Stump1eb44332009-09-09 15:08:12 +0000375 L.LexIncludeFilename(Tok);
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000376 L.setParsingPreprocessorDirective(false);
Ted Kremeneke5680f32008-12-23 01:30:52 +0000377 assert(!Tok.isAtStartOfLine());
Abramo Bagnarac4bf2b92010-12-22 08:23:18 +0000378 if (Tok.is(tok::raw_identifier))
Kovarththanan Rajaratnam65cc1e82010-03-13 08:53:33 +0000379 PP.LookUpIdentifierInfo(Tok);
Mike Stump1eb44332009-09-09 15:08:12 +0000380
Ted Kremeneke5680f32008-12-23 01:30:52 +0000381 break;
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000382 }
Ted Kremeneke5680f32008-12-23 01:30:52 +0000383 case tok::pp_if:
384 case tok::pp_ifdef:
385 case tok::pp_ifndef: {
Ted Kremeneke4f6b1e2009-02-10 22:27:09 +0000386 // Add an entry for '#if' and friends. We initially set the target
387 // index to 0. This will get backpatched when we hit #endif.
Ted Kremenekfb645b62008-12-11 23:36:38 +0000388 PPStartCond.push_back(PPCond.size());
Ted Kremenekdad7b342008-12-12 18:31:09 +0000389 PPCond.push_back(std::make_pair(HashOff, 0U));
Ted Kremeneke5680f32008-12-23 01:30:52 +0000390 break;
Ted Kremenekfb645b62008-12-11 23:36:38 +0000391 }
Ted Kremeneke5680f32008-12-23 01:30:52 +0000392 case tok::pp_endif: {
Ted Kremenekfb645b62008-12-11 23:36:38 +0000393 // Add an entry for '#endif'. We set the target table index to itself.
Ted Kremenekdad7b342008-12-12 18:31:09 +0000394 // This will later be set to zero when emitting to the PTH file. We
395 // use 0 for uninitialized indices because that is easier to debug.
Ted Kremenekfb645b62008-12-11 23:36:38 +0000396 unsigned index = PPCond.size();
Ted Kremenekfb645b62008-12-11 23:36:38 +0000397 // Backpatch the opening '#if' entry.
Ted Kremenekdad7b342008-12-12 18:31:09 +0000398 assert(!PPStartCond.empty());
399 assert(PPCond.size() > PPStartCond.back());
Ted Kremenekfb645b62008-12-11 23:36:38 +0000400 assert(PPCond[PPStartCond.back()].second == 0);
401 PPCond[PPStartCond.back()].second = index;
Mike Stump1eb44332009-09-09 15:08:12 +0000402 PPStartCond.pop_back();
403 // Add the new entry to PPCond.
Ted Kremenekdad7b342008-12-12 18:31:09 +0000404 PPCond.push_back(std::make_pair(HashOff, index));
Ted Kremenek726080d2009-02-10 22:43:16 +0000405 EmitToken(Tok);
Mike Stump1eb44332009-09-09 15:08:12 +0000406
Ted Kremenek726080d2009-02-10 22:43:16 +0000407 // Some files have gibberish on the same line as '#endif'.
408 // Discard these tokens.
Chris Lattnerd4b14462009-04-19 07:15:51 +0000409 do
410 L.LexFromRawLexer(Tok);
411 while (Tok.isNot(tok::eof) && !Tok.isAtStartOfLine());
Ted Kremenek726080d2009-02-10 22:43:16 +0000412 // We have the next token in hand.
413 // Don't immediately lex the next one.
Mike Stump1eb44332009-09-09 15:08:12 +0000414 goto NextToken;
Ted Kremenekfb645b62008-12-11 23:36:38 +0000415 }
Ted Kremeneke5680f32008-12-23 01:30:52 +0000416 case tok::pp_elif:
417 case tok::pp_else: {
418 // Add an entry for #elif or #else.
Ted Kremenekdad7b342008-12-12 18:31:09 +0000419 // This serves as both a closing and opening of a conditional block.
420 // This means that its entry will get backpatched later.
Ted Kremenekfb645b62008-12-11 23:36:38 +0000421 unsigned index = PPCond.size();
Ted Kremenekfb645b62008-12-11 23:36:38 +0000422 // Backpatch the previous '#if' entry.
Ted Kremenekdad7b342008-12-12 18:31:09 +0000423 assert(!PPStartCond.empty());
424 assert(PPCond.size() > PPStartCond.back());
Ted Kremenekfb645b62008-12-11 23:36:38 +0000425 assert(PPCond[PPStartCond.back()].second == 0);
426 PPCond[PPStartCond.back()].second = index;
427 PPStartCond.pop_back();
428 // Now add '#elif' as a new block opening.
Ted Kremenekdad7b342008-12-12 18:31:09 +0000429 PPCond.push_back(std::make_pair(HashOff, 0U));
430 PPStartCond.push_back(index);
Ted Kremeneke5680f32008-12-23 01:30:52 +0000431 break;
432 }
Ted Kremenekfb645b62008-12-11 23:36:38 +0000433 }
Ted Kremeneke4f6b1e2009-02-10 22:27:09 +0000434 }
Mike Stump1eb44332009-09-09 15:08:12 +0000435
Ted Kremeneke4f6b1e2009-02-10 22:27:09 +0000436 EmitToken(Tok);
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000437 }
Ted Kremeneke4f6b1e2009-02-10 22:27:09 +0000438 while (Tok.isNot(tok::eof));
Ted Kremenekb978c662009-01-08 01:17:37 +0000439
Ted Kremenekdad7b342008-12-12 18:31:09 +0000440 assert(PPStartCond.empty() && "Error: imblanced preprocessor conditionals.");
Ted Kremenekb978c662009-01-08 01:17:37 +0000441
Ted Kremenekfb645b62008-12-11 23:36:38 +0000442 // Next write out PPCond.
443 Offset PPCondOff = (Offset) Out.tell();
Ted Kremenekdad7b342008-12-12 18:31:09 +0000444
445 // Write out the size of PPCond so that clients can identifer empty tables.
Ted Kremenekb978c662009-01-08 01:17:37 +0000446 Emit32(PPCond.size());
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000447
Ted Kremenekdad7b342008-12-12 18:31:09 +0000448 for (unsigned i = 0, e = PPCond.size(); i!=e; ++i) {
Kovarththanan Rajaratnam8f0d2702010-03-18 07:18:10 +0000449 Emit32(PPCond[i].first - TokenOff);
Ted Kremenekdad7b342008-12-12 18:31:09 +0000450 uint32_t x = PPCond[i].second;
451 assert(x != 0 && "PPCond entry not backpatched.");
452 // Emit zero for #endifs. This allows us to do checking when
453 // we read the PTH file back in.
Ted Kremenekb978c662009-01-08 01:17:37 +0000454 Emit32(x == i ? 0 : x);
Ted Kremenekfb645b62008-12-11 23:36:38 +0000455 }
456
Kovarththanan Rajaratnam8f0d2702010-03-18 07:18:10 +0000457 return PTHEntry(TokenOff, PPCondOff);
Ted Kremenekbe295332009-01-08 02:44:06 +0000458}
459
Ted Kremenek277faca2009-01-27 00:01:05 +0000460Offset PTHWriter::EmitCachedSpellings() {
461 // Write each cached strings to the PTH file.
462 Offset SpellingsOff = Out.tell();
Mike Stump1eb44332009-09-09 15:08:12 +0000463
Ted Kremenek277faca2009-01-27 00:01:05 +0000464 for (std::vector<llvm::StringMapEntry<OffsetOpt>*>::iterator
Chris Lattnerf2390362009-03-28 00:16:20 +0000465 I = StrEntries.begin(), E = StrEntries.end(); I!=E; ++I)
466 EmitBuf((*I)->getKeyData(), (*I)->getKeyLength()+1 /*nul included*/);
Mike Stump1eb44332009-09-09 15:08:12 +0000467
Ted Kremenek277faca2009-01-27 00:01:05 +0000468 return SpellingsOff;
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000469}
Ted Kremenek85888962008-10-21 00:54:44 +0000470
Kovarththanan Rajaratnam74e485e2010-03-14 07:38:15 +0000471void PTHWriter::GeneratePTH(const std::string &MainFile) {
Ted Kremeneke1b64982009-01-26 21:43:14 +0000472 // Generate the prologue.
Richard Smithc141b512012-08-17 03:55:43 +0000473 Out << "cfe-pth" << '\0';
Ted Kremenek67d15052009-01-26 21:50:21 +0000474 Emit32(PTHManager::Version);
Mike Stump1eb44332009-09-09 15:08:12 +0000475
Ted Kremeneka4bd8eb2009-02-11 23:34:32 +0000476 // Leave 4 words for the prologue.
477 Offset PrologueOffset = Out.tell();
Chris Lattnerf2390362009-03-28 00:16:20 +0000478 for (unsigned i = 0; i < 4; ++i)
479 Emit32(0);
Mike Stump1eb44332009-09-09 15:08:12 +0000480
Ted Kremenekd5cded42009-03-19 22:10:38 +0000481 // Write the name of the MainFile.
Kovarththanan Rajaratnam2e6051a2010-03-14 07:55:43 +0000482 if (!MainFile.empty()) {
Kovarththanan Rajaratnam8f0d2702010-03-18 07:18:10 +0000483 EmitString(MainFile);
Chris Lattnerf2390362009-03-28 00:16:20 +0000484 } else {
Ted Kremenekd5cded42009-03-19 22:10:38 +0000485 // String with 0 bytes.
486 Emit16(0);
487 }
488 Emit8(0);
Mike Stump1eb44332009-09-09 15:08:12 +0000489
Ted Kremenek85888962008-10-21 00:54:44 +0000490 // Iterate over all the files in SourceManager. Create a lexer
491 // for each file and cache the tokens.
Chris Lattnerc6fe32a2009-01-17 03:48:08 +0000492 SourceManager &SM = PP.getSourceManager();
David Blaikie4e4d0842012-03-11 07:00:24 +0000493 const LangOptions &LOpts = PP.getLangOpts();
Mike Stump1eb44332009-09-09 15:08:12 +0000494
Chris Lattnerc6fe32a2009-01-17 03:48:08 +0000495 for (SourceManager::fileinfo_iterator I = SM.fileinfo_begin(),
496 E = SM.fileinfo_end(); I != E; ++I) {
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000497 const SrcMgr::ContentCache &C = *I->second;
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +0000498 const FileEntry *FE = C.OrigEntry;
Mike Stump1eb44332009-09-09 15:08:12 +0000499
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000500 // FIXME: Handle files with non-absolute paths.
Michael J. Spencer256053b2010-12-17 21:22:22 +0000501 if (llvm::sys::path::is_relative(FE->getName()))
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000502 continue;
Ted Kremenek85888962008-10-21 00:54:44 +0000503
Chris Lattnere127a0d2010-04-20 20:35:58 +0000504 const llvm::MemoryBuffer *B = C.getBuffer(PP.getDiagnostics(), SM);
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000505 if (!B) continue;
Ted Kremenekfb645b62008-12-11 23:36:38 +0000506
Chris Lattner2b2453a2009-01-17 06:22:33 +0000507 FileID FID = SM.createFileID(FE, SourceLocation(), SrcMgr::C_User);
Chris Lattner6e290142009-11-30 04:18:44 +0000508 const llvm::MemoryBuffer *FromFile = SM.getBuffer(FID);
509 Lexer L(FID, FromFile, SM, LOpts);
Ted Kremenekd8c02922009-02-10 22:16:22 +0000510 PM.insert(FE, LexTokens(L));
Daniel Dunbar31309ab2008-11-26 02:18:33 +0000511 }
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000512
513 // Write out the identifier table.
Chris Lattnerf2390362009-03-28 00:16:20 +0000514 const std::pair<Offset,Offset> &IdTableOff = EmitIdentifierTable();
Mike Stump1eb44332009-09-09 15:08:12 +0000515
Ted Kremenekbe295332009-01-08 02:44:06 +0000516 // Write out the cached strings table.
Ted Kremenek277faca2009-01-27 00:01:05 +0000517 Offset SpellingOff = EmitCachedSpellings();
Mike Stump1eb44332009-09-09 15:08:12 +0000518
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000519 // Write out the file table.
Mike Stump1eb44332009-09-09 15:08:12 +0000520 Offset FileTableOff = EmitFileTable();
521
Ted Kremeneka4bd8eb2009-02-11 23:34:32 +0000522 // Finally, write the prologue.
523 Out.seek(PrologueOffset);
Ted Kremenekb978c662009-01-08 01:17:37 +0000524 Emit32(IdTableOff.first);
Ted Kremenekf1de4642009-02-11 16:06:55 +0000525 Emit32(IdTableOff.second);
Ted Kremenekb978c662009-01-08 01:17:37 +0000526 Emit32(FileTableOff);
Ted Kremenek277faca2009-01-27 00:01:05 +0000527 Emit32(SpellingOff);
Ted Kremenekb978c662009-01-08 01:17:37 +0000528}
529
Chris Lattner52ba8702009-03-28 00:55:35 +0000530namespace {
531/// StatListener - A simple "interpose" object used to monitor stat calls
532/// invoked by FileManager while processing the original sources used
533/// as input to PTH generation. StatListener populates the PTHWriter's
534/// file map with stat information for directories as well as negative stats.
535/// Stat information for files are populated elsewhere.
Chris Lattner10e286a2010-11-23 19:19:34 +0000536class StatListener : public FileSystemStatCache {
Chris Lattner52ba8702009-03-28 00:55:35 +0000537 PTHMap &PM;
538public:
539 StatListener(PTHMap &pm) : PM(pm) {}
540 ~StatListener() {}
Mike Stump1eb44332009-09-09 15:08:12 +0000541
Rafael Espindola0fda0f72013-08-01 21:42:11 +0000542 LookupResult getStat(const char *Path, FileData &Data, bool isFile,
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700543 std::unique_ptr<vfs::File> *F,
544 vfs::FileSystem &FS) override {
Stephen Hines651f13c2014-04-23 16:59:28 -0700545 LookupResult Result = statChained(Path, Data, isFile, F, FS);
Mike Stump1eb44332009-09-09 15:08:12 +0000546
Chris Lattnerd6f61112010-11-23 20:05:15 +0000547 if (Result == CacheMissing) // Failed 'stat'.
Chris Lattner10e286a2010-11-23 19:19:34 +0000548 PM.insert(PTHEntryKeyVariant(Path), PTHEntry());
Rafael Espindola0fda0f72013-08-01 21:42:11 +0000549 else if (Data.IsDirectory) {
Chris Lattner52ba8702009-03-28 00:55:35 +0000550 // Only cache directories with absolute paths.
Michael J. Spencer256053b2010-12-17 21:22:22 +0000551 if (llvm::sys::path::is_relative(Path))
Chris Lattner10e286a2010-11-23 19:19:34 +0000552 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +0000553
Rafael Espindola0fda0f72013-08-01 21:42:11 +0000554 PM.insert(PTHEntryKeyVariant(&Data, Path), PTHEntry());
Chris Lattner52ba8702009-03-28 00:55:35 +0000555 }
Mike Stump1eb44332009-09-09 15:08:12 +0000556
Chris Lattner10e286a2010-11-23 19:19:34 +0000557 return Result;
Chris Lattner52ba8702009-03-28 00:55:35 +0000558 }
559};
560} // end anonymous namespace
561
562
Eli Friedmanf54fce82009-05-19 01:02:07 +0000563void clang::CacheTokens(Preprocessor &PP, llvm::raw_fd_ostream* OS) {
Ted Kremenekd5cded42009-03-19 22:10:38 +0000564 // Get the name of the main file.
565 const SourceManager &SrcMgr = PP.getSourceManager();
566 const FileEntry *MainFile = SrcMgr.getFileEntryForID(SrcMgr.getMainFileID());
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000567 SmallString<128> MainFilePath(MainFile->getName());
Mike Stump1eb44332009-09-09 15:08:12 +0000568
Michael J. Spencerfbfd1802010-12-21 16:45:57 +0000569 llvm::sys::fs::make_absolute(MainFilePath);
Ted Kremenekad6ce5c2009-02-13 22:07:44 +0000570
571 // Create the PTHWriter.
Eli Friedmanf54fce82009-05-19 01:02:07 +0000572 PTHWriter PW(*OS, PP);
Mike Stump1eb44332009-09-09 15:08:12 +0000573
Ted Kremenekad6ce5c2009-02-13 22:07:44 +0000574 // Install the 'stat' system call listener in the FileManager.
Stephen Hines176edba2014-12-01 14:53:08 -0800575 auto StatCacheOwner = llvm::make_unique<StatListener>(PW.getPM());
576 StatListener *StatCache = StatCacheOwner.get();
577 PP.getFileManager().addStatCache(std::move(StatCacheOwner),
578 /*AtBeginning=*/true);
Mike Stump1eb44332009-09-09 15:08:12 +0000579
Ted Kremenekad6ce5c2009-02-13 22:07:44 +0000580 // Lex through the entire file. This will populate SourceManager with
581 // all of the header information.
582 Token Tok;
Chris Lattnere127a0d2010-04-20 20:35:58 +0000583 PP.EnterMainSourceFile();
Ted Kremenekad6ce5c2009-02-13 22:07:44 +0000584 do { PP.Lex(Tok); } while (Tok.isNot(tok::eof));
Mike Stump1eb44332009-09-09 15:08:12 +0000585
Ted Kremenekad6ce5c2009-02-13 22:07:44 +0000586 // Generate the PTH file.
Douglas Gregor52e71082009-10-16 18:18:30 +0000587 PP.getFileManager().removeStatCache(StatCache);
Kovarththanan Rajaratnam74e485e2010-03-14 07:38:15 +0000588 PW.GeneratePTH(MainFilePath.str());
Ted Kremenek85888962008-10-21 00:54:44 +0000589}
Ted Kremeneke0ea5dc2009-02-10 01:06:17 +0000590
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000591//===----------------------------------------------------------------------===//
592
Kovarththanan Rajaratnam48673e62010-03-14 06:48:05 +0000593namespace {
Chris Lattnerf2390362009-03-28 00:16:20 +0000594class PTHIdKey {
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000595public:
596 const IdentifierInfo* II;
597 uint32_t FileOffset;
598};
599
Benjamin Kramerbd218282009-11-28 10:07:24 +0000600class PTHIdentifierTableTrait {
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000601public:
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000602 typedef PTHIdKey* key_type;
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000603 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +0000604
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000605 typedef uint32_t data_type;
606 typedef data_type data_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +0000607
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700608 typedef unsigned hash_value_type;
609 typedef unsigned offset_type;
610
611 static hash_value_type ComputeHash(PTHIdKey* key) {
Daniel Dunbar01eb9b92009-10-18 21:17:35 +0000612 return llvm::HashString(key->II->getName());
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000613 }
Mike Stump1eb44332009-09-09 15:08:12 +0000614
615 static std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +0000616 EmitKeyDataLength(raw_ostream& Out, const PTHIdKey* key, uint32_t) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700617 using namespace llvm::support;
Daniel Dunbare013d682009-10-18 20:26:12 +0000618 unsigned n = key->II->getLength() + 1;
Stephen Hines651f13c2014-04-23 16:59:28 -0700619 endian::Writer<little>(Out).write<uint16_t>(n);
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000620 return std::make_pair(n, sizeof(uint32_t));
621 }
Mike Stump1eb44332009-09-09 15:08:12 +0000622
Chris Lattner5f9e2722011-07-23 10:55:15 +0000623 static void EmitKey(raw_ostream& Out, PTHIdKey* key, unsigned n) {
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000624 // Record the location of the key data. This is used when generating
625 // the mapping from persistent IDs to strings.
626 key->FileOffset = Out.tell();
Daniel Dunbare013d682009-10-18 20:26:12 +0000627 Out.write(key->II->getNameStart(), n);
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000628 }
Mike Stump1eb44332009-09-09 15:08:12 +0000629
Chris Lattner5f9e2722011-07-23 10:55:15 +0000630 static void EmitData(raw_ostream& Out, PTHIdKey*, uint32_t pID,
Ted Kremenek337edcd2009-02-12 03:26:59 +0000631 unsigned) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700632 using namespace llvm::support;
633 endian::Writer<little>(Out).write<uint32_t>(pID);
Mike Stump1eb44332009-09-09 15:08:12 +0000634 }
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000635};
636} // end anonymous namespace
637
638/// EmitIdentifierTable - Emits two tables to the PTH file. The first is
639/// a hashtable mapping from identifier strings to persistent IDs. The second
640/// is a straight table mapping from persistent IDs to string data (the
641/// keys of the first table).
642///
643std::pair<Offset,Offset> PTHWriter::EmitIdentifierTable() {
644 // Build two maps:
645 // (1) an inverse map from persistent IDs -> (IdentifierInfo*,Offset)
646 // (2) a map from (IdentifierInfo*, Offset)* -> persistent IDs
647
648 // Note that we use 'calloc', so all the bytes are 0.
Chris Lattnerf2390362009-03-28 00:16:20 +0000649 PTHIdKey *IIDMap = (PTHIdKey*)calloc(idcount, sizeof(PTHIdKey));
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000650
651 // Create the hashtable.
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700652 llvm::OnDiskChainedHashTableGenerator<PTHIdentifierTableTrait> IIOffMap;
Mike Stump1eb44332009-09-09 15:08:12 +0000653
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000654 // Generate mapping from persistent IDs -> IdentifierInfo*.
Chris Lattnerf2390362009-03-28 00:16:20 +0000655 for (IDMap::iterator I = IM.begin(), E = IM.end(); I != E; ++I) {
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000656 // Decrement by 1 because we are using a vector for the lookup and
657 // 0 is reserved for NULL.
658 assert(I->second > 0);
659 assert(I->second-1 < idcount);
660 unsigned idx = I->second-1;
Mike Stump1eb44332009-09-09 15:08:12 +0000661
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000662 // Store the mapping from persistent ID to IdentifierInfo*
663 IIDMap[idx].II = I->first;
Mike Stump1eb44332009-09-09 15:08:12 +0000664
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000665 // Store the reverse mapping in a hashtable.
666 IIOffMap.insert(&IIDMap[idx], I->second);
667 }
Mike Stump1eb44332009-09-09 15:08:12 +0000668
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000669 // Write out the inverse map first. This causes the PCIDKey entries to
670 // record PTH file offsets for the string data. This is used to write
671 // the second table.
672 Offset StringTableOffset = IIOffMap.Emit(Out);
Mike Stump1eb44332009-09-09 15:08:12 +0000673
674 // Now emit the table mapping from persistent IDs to PTH file offsets.
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000675 Offset IDOff = Out.tell();
676 Emit32(idcount); // Emit the number of identifiers.
Chris Lattnerf2390362009-03-28 00:16:20 +0000677 for (unsigned i = 0 ; i < idcount; ++i)
678 Emit32(IIDMap[i].FileOffset);
Mike Stump1eb44332009-09-09 15:08:12 +0000679
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000680 // Finally, release the inverse map.
681 free(IIDMap);
Mike Stump1eb44332009-09-09 15:08:12 +0000682
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000683 return std::make_pair(IDOff, StringTableOffset);
684}