blob: 91dd492a80e933904a0fe7ee374b8e37d53cc0b6 [file] [log] [blame]
Ted Kremenek29942a32009-02-13 19:13:46 +00001//===--- CacheTokens.cpp - Caching of lexer tokens for PTH support --------===//
Ted Kremenek8ab23ad2008-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 Kremenek29942a32009-02-13 19:13:46 +000010// This provides a possible implementation of PTH support for Clang that is
Ted Kremenek8ab23ad2008-10-21 00:54:44 +000011// based on caching lexed tokens and identifiers.
12//
13//===----------------------------------------------------------------------===//
14
Ted Kremenek8ab23ad2008-10-21 00:54:44 +000015#include "clang/Basic/Diagnostic.h"
Chris Lattner226efd32010-11-23 19:19:34 +000016#include "clang/Basic/FileManager.h"
17#include "clang/Basic/FileSystemStatCache.h"
18#include "clang/Basic/IdentifierTable.h"
Chris Lattner226efd32010-11-23 19:19:34 +000019#include "clang/Basic/SourceManager.h"
Mehdi Amini9670f842016-07-18 19:02:11 +000020#include "clang/Frontend/Utils.h"
Ted Kremenek8ab23ad2008-10-21 00:54:44 +000021#include "clang/Lex/Lexer.h"
Reid Kleckner738d48d2015-11-02 17:53:55 +000022#include "clang/Lex/PTHManager.h"
Ted Kremenek8ab23ad2008-10-21 00:54:44 +000023#include "clang/Lex/Preprocessor.h"
Daniel Dunbar283ccf42009-10-17 20:43:58 +000024#include "llvm/ADT/StringExtras.h"
25#include "llvm/ADT/StringMap.h"
Justin Bognere1c147c2014-03-28 22:03:19 +000026#include "llvm/Support/EndianStream.h"
Michael J. Spencer740857f2010-12-21 16:45:57 +000027#include "llvm/Support/FileSystem.h"
Ted Kremenek8ab23ad2008-10-21 00:54:44 +000028#include "llvm/Support/MemoryBuffer.h"
Justin Bognerbb094f02014-04-18 19:57:06 +000029#include "llvm/Support/OnDiskHashTable.h"
Michael J. Spencer8aaf4992010-11-29 18:12:39 +000030#include "llvm/Support/Path.h"
Ted Kremenek8ab23ad2008-10-21 00:54:44 +000031
Cedric Venetd3c80de2009-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 Kremenek8ab23ad2008-10-21 00:54:44 +000037using namespace clang;
Ted Kremenek351f7882009-02-10 01:14:45 +000038
39//===----------------------------------------------------------------------===//
40// PTH-specific stuff.
41//===----------------------------------------------------------------------===//
42
Justin Bognerbb094f02014-04-18 19:57:06 +000043typedef uint32_t Offset;
44
Ted Kremenek17f09da02009-01-08 02:44:06 +000045namespace {
Benjamin Kramer16634c22009-11-28 10:07:24 +000046class PTHEntry {
Mike Stump11289f42009-09-09 15:08:12 +000047 Offset TokenData, PPCondData;
Ted Kremenek17f09da02009-01-08 02:44:06 +000048
Mike Stump11289f42009-09-09 15:08:12 +000049public:
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +000050 PTHEntry() {}
Ted Kremenek17f09da02009-01-08 02:44:06 +000051
Ted Kremenek29942a32009-02-13 19:13:46 +000052 PTHEntry(Offset td, Offset ppcd)
Ted Kremenek8d178f42009-01-27 00:01:05 +000053 : TokenData(td), PPCondData(ppcd) {}
Mike Stump11289f42009-09-09 15:08:12 +000054
55 Offset getTokenOffset() const { return TokenData; }
Ted Kremenek17f09da02009-01-08 02:44:06 +000056 Offset getPPCondTableOffset() const { return PPCondData; }
Ted Kremenek8d178f42009-01-27 00:01:05 +000057};
Mike Stump11289f42009-09-09 15:08:12 +000058
59
Benjamin Kramer16634c22009-11-28 10:07:24 +000060class PTHEntryKeyVariant {
Mehdi Amini0df59d82016-10-11 07:31:29 +000061 union {
62 const FileEntry *FE;
63 StringRef Path;
64 };
Ted Kremenek29942a32009-02-13 19:13:46 +000065 enum { IsFE = 0x1, IsDE = 0x2, IsNoExist = 0x0 } Kind;
Rafael Espindolaf8f91b82013-08-01 21:42:11 +000066 FileData *Data;
67
Ted Kremenek86423a92009-02-10 22:16:22 +000068public:
Craig Topper49a27902014-05-22 04:46:25 +000069 PTHEntryKeyVariant(const FileEntry *fe) : FE(fe), Kind(IsFE), Data(nullptr) {}
Ted Kremenek2fd18ec2009-02-13 22:07:44 +000070
Mehdi Amini0df59d82016-10-11 07:31:29 +000071 PTHEntryKeyVariant(FileData *Data, StringRef Path)
72 : Path(Path), Kind(IsDE), Data(new FileData(*Data)) {}
Ted Kremenek2fd18ec2009-02-13 22:07:44 +000073
Mehdi Amini0df59d82016-10-11 07:31:29 +000074 explicit PTHEntryKeyVariant(StringRef Path)
75 : Path(Path), Kind(IsNoExist), Data(nullptr) {}
Mike Stump11289f42009-09-09 15:08:12 +000076
Ted Kremenek2fd18ec2009-02-13 22:07:44 +000077 bool isFile() const { return Kind == IsFE; }
Mike Stump11289f42009-09-09 15:08:12 +000078
Chris Lattner0e62c1c2011-07-23 10:55:15 +000079 StringRef getString() const {
Ted Kremenek2fd18ec2009-02-13 22:07:44 +000080 return Kind == IsFE ? FE->getName() : Path;
Ted Kremenek29942a32009-02-13 19:13:46 +000081 }
Mike Stump11289f42009-09-09 15:08:12 +000082
Ted Kremenek29942a32009-02-13 19:13:46 +000083 unsigned getKind() const { return (unsigned) Kind; }
Mike Stump11289f42009-09-09 15:08:12 +000084
Chris Lattner0e62c1c2011-07-23 10:55:15 +000085 void EmitData(raw_ostream& Out) {
Justin Bognere1c147c2014-03-28 22:03:19 +000086 using namespace llvm::support;
87 endian::Writer<little> LE(Out);
Ted Kremenek29942a32009-02-13 19:13:46 +000088 switch (Kind) {
Rafael Espindolaf8f91b82013-08-01 21:42:11 +000089 case IsFE: {
Mike Stump281d6d72010-01-20 02:03:14 +000090 // Emit stat information.
Rafael Espindolaf8f91b82013-08-01 21:42:11 +000091 llvm::sys::fs::UniqueID UID = FE->getUniqueID();
Justin Bognere1c147c2014-03-28 22:03:19 +000092 LE.write<uint64_t>(UID.getFile());
93 LE.write<uint64_t>(UID.getDevice());
94 LE.write<uint64_t>(FE->getModificationTime());
95 LE.write<uint64_t>(FE->getSize());
Rafael Espindolaf8f91b82013-08-01 21:42:11 +000096 } break;
Mike Stump281d6d72010-01-20 02:03:14 +000097 case IsDE:
98 // Emit stat information.
Justin Bognere1c147c2014-03-28 22:03:19 +000099 LE.write<uint64_t>(Data->UniqueID.getFile());
100 LE.write<uint64_t>(Data->UniqueID.getDevice());
101 LE.write<uint64_t>(Data->ModTime);
102 LE.write<uint64_t>(Data->Size);
Rafael Espindolaf8f91b82013-08-01 21:42:11 +0000103 delete Data;
Mike Stump281d6d72010-01-20 02:03:14 +0000104 break;
105 default:
106 break;
Ted Kremenek29942a32009-02-13 19:13:46 +0000107 }
108 }
Mike Stump11289f42009-09-09 15:08:12 +0000109
Ted Kremenek29942a32009-02-13 19:13:46 +0000110 unsigned getRepresentationLength() const {
Reid Klecknerf7e61472015-11-02 20:47:31 +0000111 return Kind == IsNoExist ? 0 : 4 * 8;
Ted Kremenek29942a32009-02-13 19:13:46 +0000112 }
113};
Mike Stump11289f42009-09-09 15:08:12 +0000114
Benjamin Kramer16634c22009-11-28 10:07:24 +0000115class FileEntryPTHEntryInfo {
Ted Kremenek29942a32009-02-13 19:13:46 +0000116public:
117 typedef PTHEntryKeyVariant key_type;
Ted Kremenek86423a92009-02-10 22:16:22 +0000118 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +0000119
Ted Kremenek29942a32009-02-13 19:13:46 +0000120 typedef PTHEntry data_type;
121 typedef const PTHEntry& data_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +0000122
Justin Bogner25463f12014-04-18 20:27:24 +0000123 typedef unsigned hash_value_type;
124 typedef unsigned offset_type;
125
126 static hash_value_type ComputeHash(PTHEntryKeyVariant V) {
Daniel Dunbar283ccf42009-10-17 20:43:58 +0000127 return llvm::HashString(V.getString());
Ted Kremenek86423a92009-02-10 22:16:22 +0000128 }
Mike Stump11289f42009-09-09 15:08:12 +0000129
130 static std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000131 EmitKeyDataLength(raw_ostream& Out, PTHEntryKeyVariant V,
Ted Kremenek29942a32009-02-13 19:13:46 +0000132 const PTHEntry& E) {
Justin Bognere1c147c2014-03-28 22:03:19 +0000133 using namespace llvm::support;
134 endian::Writer<little> LE(Out);
Ted Kremenek86423a92009-02-10 22:16:22 +0000135
Daniel Dunbar283ccf42009-10-17 20:43:58 +0000136 unsigned n = V.getString().size() + 1 + 1;
Justin Bognere1c147c2014-03-28 22:03:19 +0000137 LE.write<uint16_t>(n);
Mike Stump11289f42009-09-09 15:08:12 +0000138
Ted Kremenek2fd18ec2009-02-13 22:07:44 +0000139 unsigned m = V.getRepresentationLength() + (V.isFile() ? 4 + 4 : 0);
Justin Bognere1c147c2014-03-28 22:03:19 +0000140 LE.write<uint8_t>(m);
Ted Kremenek29942a32009-02-13 19:13:46 +0000141
142 return std::make_pair(n, m);
Ted Kremenek86423a92009-02-10 22:16:22 +0000143 }
Mike Stump11289f42009-09-09 15:08:12 +0000144
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000145 static void EmitKey(raw_ostream& Out, PTHEntryKeyVariant V, unsigned n){
Justin Bognere1c147c2014-03-28 22:03:19 +0000146 using namespace llvm::support;
Ted Kremenek29942a32009-02-13 19:13:46 +0000147 // Emit the entry kind.
Justin Bognere1c147c2014-03-28 22:03:19 +0000148 endian::Writer<little>(Out).write<uint8_t>((unsigned)V.getKind());
Ted Kremenek29942a32009-02-13 19:13:46 +0000149 // Emit the string.
Daniel Dunbar283ccf42009-10-17 20:43:58 +0000150 Out.write(V.getString().data(), n - 1);
Ted Kremenek86423a92009-02-10 22:16:22 +0000151 }
Mike Stump11289f42009-09-09 15:08:12 +0000152
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000153 static void EmitData(raw_ostream& Out, PTHEntryKeyVariant V,
Ted Kremenek29942a32009-02-13 19:13:46 +0000154 const PTHEntry& E, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +0000155 using namespace llvm::support;
156 endian::Writer<little> LE(Out);
Ted Kremenek29942a32009-02-13 19:13:46 +0000157
158 // For file entries emit the offsets into the PTH file for token data
159 // and the preprocessor blocks table.
Ted Kremenek2fd18ec2009-02-13 22:07:44 +0000160 if (V.isFile()) {
Justin Bognere1c147c2014-03-28 22:03:19 +0000161 LE.write<uint32_t>(E.getTokenOffset());
162 LE.write<uint32_t>(E.getPPCondTableOffset());
Ted Kremenek29942a32009-02-13 19:13:46 +0000163 }
Mike Stump11289f42009-09-09 15:08:12 +0000164
Ted Kremenek29942a32009-02-13 19:13:46 +0000165 // Emit any other data associated with the key (i.e., stat information).
166 V.EmitData(Out);
Mike Stump11289f42009-09-09 15:08:12 +0000167 }
Ted Kremenek86423a92009-02-10 22:16:22 +0000168};
Mike Stump11289f42009-09-09 15:08:12 +0000169
Ted Kremenek8d178f42009-01-27 00:01:05 +0000170class OffsetOpt {
171 bool valid;
172 Offset off;
173public:
174 OffsetOpt() : valid(false) {}
175 bool hasOffset() const { return valid; }
176 Offset getOffset() const { assert(valid); return off; }
177 void setOffset(Offset o) { off = o; valid = true; }
Ted Kremenek17f09da02009-01-08 02:44:06 +0000178};
179} // end anonymous namespace
180
Justin Bognerbb094f02014-04-18 19:57:06 +0000181typedef llvm::OnDiskChainedHashTableGenerator<FileEntryPTHEntryInfo> PTHMap;
Ted Kremenek8ab23ad2008-10-21 00:54:44 +0000182
Ted Kremenek352b8ba2009-01-08 01:17:37 +0000183namespace {
Benjamin Kramer16634c22009-11-28 10:07:24 +0000184class PTHWriter {
Kovarththanan Rajaratnam5497e492010-03-18 07:45:30 +0000185 typedef llvm::DenseMap<const IdentifierInfo*,uint32_t> IDMap;
186 typedef llvm::StringMap<OffsetOpt, llvm::BumpPtrAllocator> CachedStrsTy;
187
Rafael Espindola2f16bc12015-04-14 15:15:49 +0000188 raw_pwrite_stream &Out;
Ted Kremenek352b8ba2009-01-08 01:17:37 +0000189 Preprocessor& PP;
Alexander Shaposhnikov024bb4b2016-09-13 20:17:57 +0000190 IDMap IM;
191 std::vector<llvm::StringMapEntry<OffsetOpt>*> StrEntries;
Ted Kremenek29942a32009-02-13 19:13:46 +0000192 PTHMap PM;
Ted Kremenek17f09da02009-01-08 02:44:06 +0000193 CachedStrsTy CachedStrs;
Alexander Shaposhnikov024bb4b2016-09-13 20:17:57 +0000194 uint32_t idcount;
Ted Kremenek8d178f42009-01-27 00:01:05 +0000195 Offset CurStrOffset;
Ted Kremenek1bd0a552008-12-23 02:52:12 +0000196
Ted Kremenek352b8ba2009-01-08 01:17:37 +0000197 //// Get the persistent id for the given IdentifierInfo*.
198 uint32_t ResolveID(const IdentifierInfo* II);
Mike Stump11289f42009-09-09 15:08:12 +0000199
Ted Kremenek352b8ba2009-01-08 01:17:37 +0000200 /// Emit a token to the PTH file.
201 void EmitToken(const Token& T);
202
Justin Bognere1c147c2014-03-28 22:03:19 +0000203 void Emit8(uint32_t V) {
204 using namespace llvm::support;
205 endian::Writer<little>(Out).write<uint8_t>(V);
206 }
Mike Stump11289f42009-09-09 15:08:12 +0000207
Justin Bognere1c147c2014-03-28 22:03:19 +0000208 void Emit16(uint32_t V) {
209 using namespace llvm::support;
210 endian::Writer<little>(Out).write<uint16_t>(V);
211 }
Mike Stump11289f42009-09-09 15:08:12 +0000212
Justin Bognere1c147c2014-03-28 22:03:19 +0000213 void Emit32(uint32_t V) {
214 using namespace llvm::support;
215 endian::Writer<little>(Out).write<uint32_t>(V);
216 }
Ted Kremenek5b229bc2009-02-10 01:06:17 +0000217
Chris Lattnera94d1392009-03-28 00:16:20 +0000218 void EmitBuf(const char *Ptr, unsigned NumBytes) {
219 Out.write(Ptr, NumBytes);
Ted Kremenek352b8ba2009-01-08 01:17:37 +0000220 }
Mike Stump11289f42009-09-09 15:08:12 +0000221
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000222 void EmitString(StringRef V) {
Justin Bognere1c147c2014-03-28 22:03:19 +0000223 using namespace llvm::support;
224 endian::Writer<little>(Out).write<uint16_t>(V.size());
Kovarththanan Rajaratnamb4c0f5c2010-03-14 08:35:19 +0000225 EmitBuf(V.data(), V.size());
226 }
227
Ted Kremeneke5554de2009-02-11 21:29:16 +0000228 /// EmitIdentifierTable - Emits two tables to the PTH file. The first is
229 /// a hashtable mapping from identifier strings to persistent IDs.
230 /// The second is a straight table mapping from persistent IDs to string data
231 /// (the keys of the first table).
Ted Kremenek8527e3a2009-02-11 16:06:55 +0000232 std::pair<Offset, Offset> EmitIdentifierTable();
Mike Stump11289f42009-09-09 15:08:12 +0000233
Ted Kremenek8527e3a2009-02-11 16:06:55 +0000234 /// EmitFileTable - Emit a table mapping from file name strings to PTH
235 /// token data.
236 Offset EmitFileTable() { return PM.Emit(Out); }
237
Ted Kremenek29942a32009-02-13 19:13:46 +0000238 PTHEntry LexTokens(Lexer& L);
Ted Kremenek8d178f42009-01-27 00:01:05 +0000239 Offset EmitCachedSpellings();
Ted Kremenek2fd18ec2009-02-13 22:07:44 +0000240
Ted Kremenek352b8ba2009-01-08 01:17:37 +0000241public:
Rafael Espindola2f16bc12015-04-14 15:15:49 +0000242 PTHWriter(raw_pwrite_stream &out, Preprocessor &pp)
243 : Out(out), PP(pp), idcount(0), CurStrOffset(0) {}
Mike Stump11289f42009-09-09 15:08:12 +0000244
Chris Lattner2b9e7ef2009-03-28 00:55:35 +0000245 PTHMap &getPM() { return PM; }
Benjamin Kramer0772c422016-02-13 13:42:54 +0000246 void GeneratePTH(StringRef MainFile);
Ted Kremenek352b8ba2009-01-08 01:17:37 +0000247};
248} // end anonymous namespace
Mike Stump11289f42009-09-09 15:08:12 +0000249
250uint32_t PTHWriter::ResolveID(const IdentifierInfo* II) {
Ted Kremenekccaab172008-12-02 19:44:08 +0000251 // Null IdentifierInfo's map to the persistent ID 0.
252 if (!II)
253 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000254
Ted Kremenek8ab23ad2008-10-21 00:54:44 +0000255 IDMap::iterator I = IM.find(II);
Chris Lattnera94d1392009-03-28 00:16:20 +0000256 if (I != IM.end())
257 return I->second; // We've already added 1.
Mike Stump11289f42009-09-09 15:08:12 +0000258
Chris Lattnera94d1392009-03-28 00:16:20 +0000259 IM[II] = ++idcount; // Pre-increment since '0' is reserved for NULL.
260 return idcount;
Ted Kremenek8ab23ad2008-10-21 00:54:44 +0000261}
262
Ted Kremenek352b8ba2009-01-08 01:17:37 +0000263void PTHWriter::EmitToken(const Token& T) {
Ted Kremenekd6e190d2009-02-24 01:26:56 +0000264 // Emit the token kind, flags, and length.
265 Emit32(((uint32_t) T.getKind()) | ((((uint32_t) T.getFlags())) << 8)|
266 (((uint32_t) T.getLength()) << 16));
Mike Stump11289f42009-09-09 15:08:12 +0000267
Chris Lattnera94d1392009-03-28 00:16:20 +0000268 if (!T.isLiteral()) {
269 Emit32(ResolveID(T.getIdentifierInfo()));
270 } else {
Ted Kremenekd6e190d2009-02-24 01:26:56 +0000271 // We cache *un-cleaned* spellings. This gives us 100% fidelity with the
272 // source code.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000273 StringRef s(T.getLiteralData(), T.getLength());
Ted Kremenek1f11c652009-02-24 00:30:21 +0000274
Chris Lattner5a7971e2009-01-26 19:29:26 +0000275 // Get the string entry.
David Blaikie13156b62014-11-19 03:06:06 +0000276 auto &E = *CachedStrs.insert(std::make_pair(s, OffsetOpt())).first;
Mike Stump11289f42009-09-09 15:08:12 +0000277
Ted Kremenekd6e190d2009-02-24 01:26:56 +0000278 // If this is a new string entry, bump the PTH offset.
David Blaikie13156b62014-11-19 03:06:06 +0000279 if (!E.second.hasOffset()) {
280 E.second.setOffset(CurStrOffset);
281 StrEntries.push_back(&E);
Jay Foad9a6b0982011-06-21 15:13:30 +0000282 CurStrOffset += s.size() + 1;
Ted Kremenek8d178f42009-01-27 00:01:05 +0000283 }
Mike Stump11289f42009-09-09 15:08:12 +0000284
Ted Kremenek1f11c652009-02-24 00:30:21 +0000285 // Emit the relative offset into the PTH file for the spelling string.
David Blaikie13156b62014-11-19 03:06:06 +0000286 Emit32(E.second.getOffset());
Ted Kremenek352b8ba2009-01-08 01:17:37 +0000287 }
Mike Stump11289f42009-09-09 15:08:12 +0000288
Ted Kremenek1f11c652009-02-24 00:30:21 +0000289 // Emit the offset into the original source file of this token so that we
290 // can reconstruct its SourceLocation.
Chris Lattnerc360bf22009-01-27 06:27:13 +0000291 Emit32(PP.getSourceManager().getFileOffset(T.getLocation()));
Ted Kremenek8ab23ad2008-10-21 00:54:44 +0000292}
293
Ted Kremenek29942a32009-02-13 19:13:46 +0000294PTHEntry PTHWriter::LexTokens(Lexer& L) {
Ted Kremenek8433f0b2009-01-19 23:13:15 +0000295 // Pad 0's so that we emit tokens to a 4-byte alignment.
296 // This speed up reading them back in.
Justin Bognerbb094f02014-04-18 19:57:06 +0000297 using namespace llvm::support;
298 endian::Writer<little> LE(Out);
299 uint32_t TokenOff = Out.tell();
300 for (uint64_t N = llvm::OffsetToAlignment(TokenOff, 4); N; --N, ++TokenOff)
301 LE.write<uint8_t>(0);
Mike Stump11289f42009-09-09 15:08:12 +0000302
Ted Kremenek864eb392008-12-11 23:36:38 +0000303 // Keep track of matching '#if' ... '#endif'.
304 typedef std::vector<std::pair<Offset, unsigned> > PPCondTable;
305 PPCondTable PPCond;
Ted Kremenekee87e0b2008-12-12 18:31:09 +0000306 std::vector<unsigned> PPStartCond;
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000307 bool ParsingPreprocessorDirective = false;
Ted Kremenekbf2d6012008-11-26 03:36:26 +0000308 Token Tok;
Mike Stump11289f42009-09-09 15:08:12 +0000309
Ted Kremenekbf2d6012008-11-26 03:36:26 +0000310 do {
311 L.LexFromRawLexer(Tok);
Ted Kremenekc7d69642009-02-10 22:43:16 +0000312 NextToken:
313
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000314 if ((Tok.isAtStartOfLine() || Tok.is(tok::eof)) &&
315 ParsingPreprocessorDirective) {
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000316 // Insert an eod token into the token cache. It has the same
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000317 // position as the next token that is not on the same line as the
318 // preprocessor directive. Observe that we continue processing
319 // 'Tok' when we exit this branch.
320 Token Tmp = Tok;
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000321 Tmp.setKind(tok::eod);
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000322 Tmp.clearFlag(Token::StartOfLine);
Craig Topper49a27902014-05-22 04:46:25 +0000323 Tmp.setIdentifierInfo(nullptr);
Ted Kremenek352b8ba2009-01-08 01:17:37 +0000324 EmitToken(Tmp);
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000325 ParsingPreprocessorDirective = false;
326 }
Mike Stump11289f42009-09-09 15:08:12 +0000327
Abramo Bagnaraea4f7c72010-12-22 08:23:18 +0000328 if (Tok.is(tok::raw_identifier)) {
Kovarththanan Rajaratname5f1c192010-03-13 08:53:33 +0000329 PP.LookUpIdentifierInfo(Tok);
Ted Kremenek99c72752009-02-10 22:27:09 +0000330 EmitToken(Tok);
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000331 continue;
Ted Kremenekbf2d6012008-11-26 03:36:26 +0000332 }
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000333
334 if (Tok.is(tok::hash) && Tok.isAtStartOfLine()) {
Ted Kremenekbf2d6012008-11-26 03:36:26 +0000335 // Special processing for #include. Store the '#' token and lex
336 // the next token.
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000337 assert(!ParsingPreprocessorDirective);
Ted Kremenek864eb392008-12-11 23:36:38 +0000338 Offset HashOff = (Offset) Out.tell();
Ted Kremenekee87e0b2008-12-12 18:31:09 +0000339
340 // Get the next token.
Ted Kremenekfee882a2010-07-27 02:58:57 +0000341 Token NextTok;
342 L.LexFromRawLexer(NextTok);
Chris Lattner9941ce32009-04-19 07:25:40 +0000343
Ted Kremenekfee882a2010-07-27 02:58:57 +0000344 // If we see the start of line, then we had a null directive "#". In
345 // this case, discard both tokens.
346 if (NextTok.isAtStartOfLine())
Chris Lattner9941ce32009-04-19 07:25:40 +0000347 goto NextToken;
Abramo Bagnaraea4f7c72010-12-22 08:23:18 +0000348
Ted Kremenekfee882a2010-07-27 02:58:57 +0000349 // The token is the start of a directive. Emit it.
350 EmitToken(Tok);
351 Tok = NextTok;
Mike Stump11289f42009-09-09 15:08:12 +0000352
Ted Kremenekbf2d6012008-11-26 03:36:26 +0000353 // Did we see 'include'/'import'/'include_next'?
Abramo Bagnaraea4f7c72010-12-22 08:23:18 +0000354 if (Tok.isNot(tok::raw_identifier)) {
Ted Kremenek99c72752009-02-10 22:27:09 +0000355 EmitToken(Tok);
Ted Kremenekbf2d6012008-11-26 03:36:26 +0000356 continue;
Ted Kremenek99c72752009-02-10 22:27:09 +0000357 }
Mike Stump11289f42009-09-09 15:08:12 +0000358
Ted Kremenekbf2d6012008-11-26 03:36:26 +0000359 IdentifierInfo* II = PP.LookUpIdentifierInfo(Tok);
Ted Kremenekbf2d6012008-11-26 03:36:26 +0000360 tok::PPKeywordKind K = II->getPPKeywordID();
Mike Stump11289f42009-09-09 15:08:12 +0000361
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000362 ParsingPreprocessorDirective = true;
Mike Stump11289f42009-09-09 15:08:12 +0000363
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000364 switch (K) {
Chris Lattner30b55dc2009-04-19 07:32:03 +0000365 case tok::pp_not_keyword:
366 // Invalid directives "#foo" can occur in #if 0 blocks etc, just pass
367 // them through.
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000368 default:
369 break;
Mike Stump11289f42009-09-09 15:08:12 +0000370
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000371 case tok::pp_include:
372 case tok::pp_import:
Mike Stump11289f42009-09-09 15:08:12 +0000373 case tok::pp_include_next: {
Ted Kremenekbf2d6012008-11-26 03:36:26 +0000374 // Save the 'include' token.
Ted Kremenek352b8ba2009-01-08 01:17:37 +0000375 EmitToken(Tok);
Ted Kremenekbf2d6012008-11-26 03:36:26 +0000376 // Lex the next token as an include string.
377 L.setParsingPreprocessorDirective(true);
Mike Stump11289f42009-09-09 15:08:12 +0000378 L.LexIncludeFilename(Tok);
Ted Kremenekbf2d6012008-11-26 03:36:26 +0000379 L.setParsingPreprocessorDirective(false);
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000380 assert(!Tok.isAtStartOfLine());
Abramo Bagnaraea4f7c72010-12-22 08:23:18 +0000381 if (Tok.is(tok::raw_identifier))
Kovarththanan Rajaratname5f1c192010-03-13 08:53:33 +0000382 PP.LookUpIdentifierInfo(Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000383
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000384 break;
Ted Kremenekbf2d6012008-11-26 03:36:26 +0000385 }
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000386 case tok::pp_if:
387 case tok::pp_ifdef:
388 case tok::pp_ifndef: {
Ted Kremenek99c72752009-02-10 22:27:09 +0000389 // Add an entry for '#if' and friends. We initially set the target
390 // index to 0. This will get backpatched when we hit #endif.
Ted Kremenek864eb392008-12-11 23:36:38 +0000391 PPStartCond.push_back(PPCond.size());
Ted Kremenekee87e0b2008-12-12 18:31:09 +0000392 PPCond.push_back(std::make_pair(HashOff, 0U));
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000393 break;
Ted Kremenek864eb392008-12-11 23:36:38 +0000394 }
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000395 case tok::pp_endif: {
Ted Kremenek864eb392008-12-11 23:36:38 +0000396 // Add an entry for '#endif'. We set the target table index to itself.
Ted Kremenekee87e0b2008-12-12 18:31:09 +0000397 // This will later be set to zero when emitting to the PTH file. We
398 // use 0 for uninitialized indices because that is easier to debug.
Ted Kremenek864eb392008-12-11 23:36:38 +0000399 unsigned index = PPCond.size();
Ted Kremenek864eb392008-12-11 23:36:38 +0000400 // Backpatch the opening '#if' entry.
Ted Kremenekee87e0b2008-12-12 18:31:09 +0000401 assert(!PPStartCond.empty());
402 assert(PPCond.size() > PPStartCond.back());
Ted Kremenek864eb392008-12-11 23:36:38 +0000403 assert(PPCond[PPStartCond.back()].second == 0);
404 PPCond[PPStartCond.back()].second = index;
Mike Stump11289f42009-09-09 15:08:12 +0000405 PPStartCond.pop_back();
406 // Add the new entry to PPCond.
Ted Kremenekee87e0b2008-12-12 18:31:09 +0000407 PPCond.push_back(std::make_pair(HashOff, index));
Ted Kremenekc7d69642009-02-10 22:43:16 +0000408 EmitToken(Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000409
Ted Kremenekc7d69642009-02-10 22:43:16 +0000410 // Some files have gibberish on the same line as '#endif'.
411 // Discard these tokens.
Chris Lattner6ada5d72009-04-19 07:15:51 +0000412 do
413 L.LexFromRawLexer(Tok);
414 while (Tok.isNot(tok::eof) && !Tok.isAtStartOfLine());
Ted Kremenekc7d69642009-02-10 22:43:16 +0000415 // We have the next token in hand.
416 // Don't immediately lex the next one.
Mike Stump11289f42009-09-09 15:08:12 +0000417 goto NextToken;
Ted Kremenek864eb392008-12-11 23:36:38 +0000418 }
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000419 case tok::pp_elif:
420 case tok::pp_else: {
421 // Add an entry for #elif or #else.
Ted Kremenekee87e0b2008-12-12 18:31:09 +0000422 // This serves as both a closing and opening of a conditional block.
423 // This means that its entry will get backpatched later.
Ted Kremenek864eb392008-12-11 23:36:38 +0000424 unsigned index = PPCond.size();
Ted Kremenek864eb392008-12-11 23:36:38 +0000425 // Backpatch the previous '#if' entry.
Ted Kremenekee87e0b2008-12-12 18:31:09 +0000426 assert(!PPStartCond.empty());
427 assert(PPCond.size() > PPStartCond.back());
Ted Kremenek864eb392008-12-11 23:36:38 +0000428 assert(PPCond[PPStartCond.back()].second == 0);
429 PPCond[PPStartCond.back()].second = index;
430 PPStartCond.pop_back();
431 // Now add '#elif' as a new block opening.
Ted Kremenekee87e0b2008-12-12 18:31:09 +0000432 PPCond.push_back(std::make_pair(HashOff, 0U));
433 PPStartCond.push_back(index);
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000434 break;
435 }
Ted Kremenek864eb392008-12-11 23:36:38 +0000436 }
Ted Kremenek99c72752009-02-10 22:27:09 +0000437 }
Mike Stump11289f42009-09-09 15:08:12 +0000438
Ted Kremenek99c72752009-02-10 22:27:09 +0000439 EmitToken(Tok);
Ted Kremenekbf2d6012008-11-26 03:36:26 +0000440 }
Ted Kremenek99c72752009-02-10 22:27:09 +0000441 while (Tok.isNot(tok::eof));
Ted Kremenek352b8ba2009-01-08 01:17:37 +0000442
Ted Kremenekee87e0b2008-12-12 18:31:09 +0000443 assert(PPStartCond.empty() && "Error: imblanced preprocessor conditionals.");
Ted Kremenek352b8ba2009-01-08 01:17:37 +0000444
Ted Kremenek864eb392008-12-11 23:36:38 +0000445 // Next write out PPCond.
446 Offset PPCondOff = (Offset) Out.tell();
Ted Kremenekee87e0b2008-12-12 18:31:09 +0000447
448 // Write out the size of PPCond so that clients can identifer empty tables.
Ted Kremenek352b8ba2009-01-08 01:17:37 +0000449 Emit32(PPCond.size());
Ted Kremenekccaab172008-12-02 19:44:08 +0000450
Ted Kremenekee87e0b2008-12-12 18:31:09 +0000451 for (unsigned i = 0, e = PPCond.size(); i!=e; ++i) {
Kovarththanan Rajaratnam3fd80822010-03-18 07:18:10 +0000452 Emit32(PPCond[i].first - TokenOff);
Ted Kremenekee87e0b2008-12-12 18:31:09 +0000453 uint32_t x = PPCond[i].second;
454 assert(x != 0 && "PPCond entry not backpatched.");
455 // Emit zero for #endifs. This allows us to do checking when
456 // we read the PTH file back in.
Ted Kremenek352b8ba2009-01-08 01:17:37 +0000457 Emit32(x == i ? 0 : x);
Ted Kremenek864eb392008-12-11 23:36:38 +0000458 }
459
Kovarththanan Rajaratnam3fd80822010-03-18 07:18:10 +0000460 return PTHEntry(TokenOff, PPCondOff);
Ted Kremenek17f09da02009-01-08 02:44:06 +0000461}
462
Ted Kremenek8d178f42009-01-27 00:01:05 +0000463Offset PTHWriter::EmitCachedSpellings() {
464 // Write each cached strings to the PTH file.
465 Offset SpellingsOff = Out.tell();
Mike Stump11289f42009-09-09 15:08:12 +0000466
Ted Kremenek8d178f42009-01-27 00:01:05 +0000467 for (std::vector<llvm::StringMapEntry<OffsetOpt>*>::iterator
Chris Lattnera94d1392009-03-28 00:16:20 +0000468 I = StrEntries.begin(), E = StrEntries.end(); I!=E; ++I)
469 EmitBuf((*I)->getKeyData(), (*I)->getKeyLength()+1 /*nul included*/);
Mike Stump11289f42009-09-09 15:08:12 +0000470
Ted Kremenek8d178f42009-01-27 00:01:05 +0000471 return SpellingsOff;
Ted Kremenekbf2d6012008-11-26 03:36:26 +0000472}
Ted Kremenek8ab23ad2008-10-21 00:54:44 +0000473
Rafael Espindola2f16bc12015-04-14 15:15:49 +0000474static uint32_t swap32le(uint32_t X) {
475 return llvm::support::endian::byte_swap<uint32_t, llvm::support::little>(X);
476}
477
478static void pwrite32le(raw_pwrite_stream &OS, uint32_t Val, uint64_t &Off) {
479 uint32_t LEVal = swap32le(Val);
480 OS.pwrite(reinterpret_cast<const char *>(&LEVal), 4, Off);
481 Off += 4;
482}
483
Benjamin Kramer0772c422016-02-13 13:42:54 +0000484void PTHWriter::GeneratePTH(StringRef MainFile) {
Ted Kremenekeb8c8fb2009-01-26 21:43:14 +0000485 // Generate the prologue.
Richard Smith2683b4c2012-08-17 03:55:43 +0000486 Out << "cfe-pth" << '\0';
Ted Kremenek978b5be2009-01-26 21:50:21 +0000487 Emit32(PTHManager::Version);
Mike Stump11289f42009-09-09 15:08:12 +0000488
Ted Kremenek4c1d41f2009-02-11 23:34:32 +0000489 // Leave 4 words for the prologue.
490 Offset PrologueOffset = Out.tell();
Chris Lattnera94d1392009-03-28 00:16:20 +0000491 for (unsigned i = 0; i < 4; ++i)
492 Emit32(0);
Mike Stump11289f42009-09-09 15:08:12 +0000493
Ted Kremenek7040b572009-03-19 22:10:38 +0000494 // Write the name of the MainFile.
Kovarththanan Rajaratnamd3fa9722010-03-14 07:55:43 +0000495 if (!MainFile.empty()) {
Kovarththanan Rajaratnam3fd80822010-03-18 07:18:10 +0000496 EmitString(MainFile);
Chris Lattnera94d1392009-03-28 00:16:20 +0000497 } else {
Ted Kremenek7040b572009-03-19 22:10:38 +0000498 // String with 0 bytes.
499 Emit16(0);
500 }
501 Emit8(0);
Mike Stump11289f42009-09-09 15:08:12 +0000502
Ted Kremenek8ab23ad2008-10-21 00:54:44 +0000503 // Iterate over all the files in SourceManager. Create a lexer
504 // for each file and cache the tokens.
Chris Lattner1abd2092009-01-17 03:48:08 +0000505 SourceManager &SM = PP.getSourceManager();
David Blaikiebbafb8a2012-03-11 07:00:24 +0000506 const LangOptions &LOpts = PP.getLangOpts();
Mike Stump11289f42009-09-09 15:08:12 +0000507
Chris Lattner1abd2092009-01-17 03:48:08 +0000508 for (SourceManager::fileinfo_iterator I = SM.fileinfo_begin(),
509 E = SM.fileinfo_end(); I != E; ++I) {
Chris Lattnerc8233df2009-02-03 07:30:45 +0000510 const SrcMgr::ContentCache &C = *I->second;
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +0000511 const FileEntry *FE = C.OrigEntry;
Mike Stump11289f42009-09-09 15:08:12 +0000512
Ted Kremenekccaab172008-12-02 19:44:08 +0000513 // FIXME: Handle files with non-absolute paths.
Michael J. Spencerf28df4c2010-12-17 21:22:22 +0000514 if (llvm::sys::path::is_relative(FE->getName()))
Ted Kremenekccaab172008-12-02 19:44:08 +0000515 continue;
Ted Kremenek8ab23ad2008-10-21 00:54:44 +0000516
Chris Lattnerfb24a3a2010-04-20 20:35:58 +0000517 const llvm::MemoryBuffer *B = C.getBuffer(PP.getDiagnostics(), SM);
Ted Kremenekbf2d6012008-11-26 03:36:26 +0000518 if (!B) continue;
Ted Kremenek864eb392008-12-11 23:36:38 +0000519
Chris Lattnerd32480d2009-01-17 06:22:33 +0000520 FileID FID = SM.createFileID(FE, SourceLocation(), SrcMgr::C_User);
Chris Lattner710bb872009-11-30 04:18:44 +0000521 const llvm::MemoryBuffer *FromFile = SM.getBuffer(FID);
522 Lexer L(FID, FromFile, SM, LOpts);
Ted Kremenek86423a92009-02-10 22:16:22 +0000523 PM.insert(FE, LexTokens(L));
Daniel Dunbar3ea94852008-11-26 02:18:33 +0000524 }
Ted Kremenekbf2d6012008-11-26 03:36:26 +0000525
526 // Write out the identifier table.
Chris Lattnera94d1392009-03-28 00:16:20 +0000527 const std::pair<Offset,Offset> &IdTableOff = EmitIdentifierTable();
Mike Stump11289f42009-09-09 15:08:12 +0000528
Ted Kremenek17f09da02009-01-08 02:44:06 +0000529 // Write out the cached strings table.
Ted Kremenek8d178f42009-01-27 00:01:05 +0000530 Offset SpellingOff = EmitCachedSpellings();
Mike Stump11289f42009-09-09 15:08:12 +0000531
Ted Kremenekbf2d6012008-11-26 03:36:26 +0000532 // Write out the file table.
Mike Stump11289f42009-09-09 15:08:12 +0000533 Offset FileTableOff = EmitFileTable();
534
Ted Kremenek4c1d41f2009-02-11 23:34:32 +0000535 // Finally, write the prologue.
Rafael Espindola2f16bc12015-04-14 15:15:49 +0000536 uint64_t Off = PrologueOffset;
537 pwrite32le(Out, IdTableOff.first, Off);
538 pwrite32le(Out, IdTableOff.second, Off);
539 pwrite32le(Out, FileTableOff, Off);
540 pwrite32le(Out, SpellingOff, Off);
Ted Kremenek352b8ba2009-01-08 01:17:37 +0000541}
542
Chris Lattner2b9e7ef2009-03-28 00:55:35 +0000543namespace {
544/// StatListener - A simple "interpose" object used to monitor stat calls
545/// invoked by FileManager while processing the original sources used
546/// as input to PTH generation. StatListener populates the PTHWriter's
547/// file map with stat information for directories as well as negative stats.
548/// Stat information for files are populated elsewhere.
Chris Lattner226efd32010-11-23 19:19:34 +0000549class StatListener : public FileSystemStatCache {
Chris Lattner2b9e7ef2009-03-28 00:55:35 +0000550 PTHMap &PM;
551public:
552 StatListener(PTHMap &pm) : PM(pm) {}
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +0000553 ~StatListener() override {}
Mike Stump11289f42009-09-09 15:08:12 +0000554
Mehdi Amini0df59d82016-10-11 07:31:29 +0000555 LookupResult getStat(StringRef Path, FileData &Data, bool isFile,
David Blaikie326ffb32014-07-08 15:46:02 +0000556 std::unique_ptr<vfs::File> *F,
557 vfs::FileSystem &FS) override {
Ben Langmuirc8130a72014-02-20 21:59:23 +0000558 LookupResult Result = statChained(Path, Data, isFile, F, FS);
Mike Stump11289f42009-09-09 15:08:12 +0000559
Chris Lattner8f0583d2010-11-23 20:05:15 +0000560 if (Result == CacheMissing) // Failed 'stat'.
Chris Lattner226efd32010-11-23 19:19:34 +0000561 PM.insert(PTHEntryKeyVariant(Path), PTHEntry());
Rafael Espindolaf8f91b82013-08-01 21:42:11 +0000562 else if (Data.IsDirectory) {
Chris Lattner2b9e7ef2009-03-28 00:55:35 +0000563 // Only cache directories with absolute paths.
Michael J. Spencerf28df4c2010-12-17 21:22:22 +0000564 if (llvm::sys::path::is_relative(Path))
Chris Lattner226efd32010-11-23 19:19:34 +0000565 return Result;
Mike Stump11289f42009-09-09 15:08:12 +0000566
Rafael Espindolaf8f91b82013-08-01 21:42:11 +0000567 PM.insert(PTHEntryKeyVariant(&Data, Path), PTHEntry());
Chris Lattner2b9e7ef2009-03-28 00:55:35 +0000568 }
Mike Stump11289f42009-09-09 15:08:12 +0000569
Chris Lattner226efd32010-11-23 19:19:34 +0000570 return Result;
Chris Lattner2b9e7ef2009-03-28 00:55:35 +0000571 }
572};
573} // end anonymous namespace
574
Rafael Espindola2f16bc12015-04-14 15:15:49 +0000575void clang::CacheTokens(Preprocessor &PP, raw_pwrite_stream *OS) {
Ted Kremenek7040b572009-03-19 22:10:38 +0000576 // Get the name of the main file.
577 const SourceManager &SrcMgr = PP.getSourceManager();
578 const FileEntry *MainFile = SrcMgr.getFileEntryForID(SrcMgr.getMainFileID());
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000579 SmallString<128> MainFilePath(MainFile->getName());
Mike Stump11289f42009-09-09 15:08:12 +0000580
Michael J. Spencer740857f2010-12-21 16:45:57 +0000581 llvm::sys::fs::make_absolute(MainFilePath);
Ted Kremenek2fd18ec2009-02-13 22:07:44 +0000582
583 // Create the PTHWriter.
Eli Friedmanfcb57d52009-05-19 01:02:07 +0000584 PTHWriter PW(*OS, PP);
Mike Stump11289f42009-09-09 15:08:12 +0000585
Ted Kremenek2fd18ec2009-02-13 22:07:44 +0000586 // Install the 'stat' system call listener in the FileManager.
David Blaikie23430cc2014-08-11 21:29:24 +0000587 auto StatCacheOwner = llvm::make_unique<StatListener>(PW.getPM());
588 StatListener *StatCache = StatCacheOwner.get();
589 PP.getFileManager().addStatCache(std::move(StatCacheOwner),
590 /*AtBeginning=*/true);
Mike Stump11289f42009-09-09 15:08:12 +0000591
Ted Kremenek2fd18ec2009-02-13 22:07:44 +0000592 // Lex through the entire file. This will populate SourceManager with
593 // all of the header information.
594 Token Tok;
Chris Lattnerfb24a3a2010-04-20 20:35:58 +0000595 PP.EnterMainSourceFile();
Ted Kremenek2fd18ec2009-02-13 22:07:44 +0000596 do { PP.Lex(Tok); } while (Tok.isNot(tok::eof));
Mike Stump11289f42009-09-09 15:08:12 +0000597
Ted Kremenek2fd18ec2009-02-13 22:07:44 +0000598 // Generate the PTH file.
Douglas Gregord2eb58a2009-10-16 18:18:30 +0000599 PP.getFileManager().removeStatCache(StatCache);
Kovarththanan Rajaratnamfb076202010-03-14 07:38:15 +0000600 PW.GeneratePTH(MainFilePath.str());
Ted Kremenek8ab23ad2008-10-21 00:54:44 +0000601}
Ted Kremenek5b229bc2009-02-10 01:06:17 +0000602
Ted Kremeneke5554de2009-02-11 21:29:16 +0000603//===----------------------------------------------------------------------===//
604
Kovarththanan Rajaratnamb65a1482010-03-14 06:48:05 +0000605namespace {
Chris Lattnera94d1392009-03-28 00:16:20 +0000606class PTHIdKey {
Ted Kremeneke5554de2009-02-11 21:29:16 +0000607public:
608 const IdentifierInfo* II;
609 uint32_t FileOffset;
610};
611
Benjamin Kramer16634c22009-11-28 10:07:24 +0000612class PTHIdentifierTableTrait {
Ted Kremeneke5554de2009-02-11 21:29:16 +0000613public:
Ted Kremenek29942a32009-02-13 19:13:46 +0000614 typedef PTHIdKey* key_type;
Ted Kremeneke5554de2009-02-11 21:29:16 +0000615 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +0000616
Ted Kremeneke5554de2009-02-11 21:29:16 +0000617 typedef uint32_t data_type;
618 typedef data_type data_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +0000619
Justin Bogner25463f12014-04-18 20:27:24 +0000620 typedef unsigned hash_value_type;
621 typedef unsigned offset_type;
622
623 static hash_value_type ComputeHash(PTHIdKey* key) {
Daniel Dunbar07d07852009-10-18 21:17:35 +0000624 return llvm::HashString(key->II->getName());
Ted Kremeneke5554de2009-02-11 21:29:16 +0000625 }
Mike Stump11289f42009-09-09 15:08:12 +0000626
627 static std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000628 EmitKeyDataLength(raw_ostream& Out, const PTHIdKey* key, uint32_t) {
Justin Bognere1c147c2014-03-28 22:03:19 +0000629 using namespace llvm::support;
Daniel Dunbar2c422dc92009-10-18 20:26:12 +0000630 unsigned n = key->II->getLength() + 1;
Justin Bognere1c147c2014-03-28 22:03:19 +0000631 endian::Writer<little>(Out).write<uint16_t>(n);
Ted Kremeneke5554de2009-02-11 21:29:16 +0000632 return std::make_pair(n, sizeof(uint32_t));
633 }
Mike Stump11289f42009-09-09 15:08:12 +0000634
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000635 static void EmitKey(raw_ostream& Out, PTHIdKey* key, unsigned n) {
Ted Kremeneke5554de2009-02-11 21:29:16 +0000636 // Record the location of the key data. This is used when generating
637 // the mapping from persistent IDs to strings.
638 key->FileOffset = Out.tell();
Daniel Dunbar2c422dc92009-10-18 20:26:12 +0000639 Out.write(key->II->getNameStart(), n);
Ted Kremeneke5554de2009-02-11 21:29:16 +0000640 }
Mike Stump11289f42009-09-09 15:08:12 +0000641
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000642 static void EmitData(raw_ostream& Out, PTHIdKey*, uint32_t pID,
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000643 unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +0000644 using namespace llvm::support;
645 endian::Writer<little>(Out).write<uint32_t>(pID);
Mike Stump11289f42009-09-09 15:08:12 +0000646 }
Ted Kremeneke5554de2009-02-11 21:29:16 +0000647};
648} // end anonymous namespace
649
650/// EmitIdentifierTable - Emits two tables to the PTH file. The first is
651/// a hashtable mapping from identifier strings to persistent IDs. The second
652/// is a straight table mapping from persistent IDs to string data (the
653/// keys of the first table).
654///
655std::pair<Offset,Offset> PTHWriter::EmitIdentifierTable() {
656 // Build two maps:
657 // (1) an inverse map from persistent IDs -> (IdentifierInfo*,Offset)
658 // (2) a map from (IdentifierInfo*, Offset)* -> persistent IDs
659
660 // Note that we use 'calloc', so all the bytes are 0.
Chris Lattnera94d1392009-03-28 00:16:20 +0000661 PTHIdKey *IIDMap = (PTHIdKey*)calloc(idcount, sizeof(PTHIdKey));
Ted Kremeneke5554de2009-02-11 21:29:16 +0000662
663 // Create the hashtable.
Justin Bognerbb094f02014-04-18 19:57:06 +0000664 llvm::OnDiskChainedHashTableGenerator<PTHIdentifierTableTrait> IIOffMap;
Mike Stump11289f42009-09-09 15:08:12 +0000665
Ted Kremeneke5554de2009-02-11 21:29:16 +0000666 // Generate mapping from persistent IDs -> IdentifierInfo*.
Chris Lattnera94d1392009-03-28 00:16:20 +0000667 for (IDMap::iterator I = IM.begin(), E = IM.end(); I != E; ++I) {
Ted Kremeneke5554de2009-02-11 21:29:16 +0000668 // Decrement by 1 because we are using a vector for the lookup and
669 // 0 is reserved for NULL.
670 assert(I->second > 0);
671 assert(I->second-1 < idcount);
672 unsigned idx = I->second-1;
Mike Stump11289f42009-09-09 15:08:12 +0000673
Ted Kremeneke5554de2009-02-11 21:29:16 +0000674 // Store the mapping from persistent ID to IdentifierInfo*
675 IIDMap[idx].II = I->first;
Mike Stump11289f42009-09-09 15:08:12 +0000676
Ted Kremeneke5554de2009-02-11 21:29:16 +0000677 // Store the reverse mapping in a hashtable.
678 IIOffMap.insert(&IIDMap[idx], I->second);
679 }
Mike Stump11289f42009-09-09 15:08:12 +0000680
Ted Kremeneke5554de2009-02-11 21:29:16 +0000681 // Write out the inverse map first. This causes the PCIDKey entries to
682 // record PTH file offsets for the string data. This is used to write
683 // the second table.
684 Offset StringTableOffset = IIOffMap.Emit(Out);
Mike Stump11289f42009-09-09 15:08:12 +0000685
686 // Now emit the table mapping from persistent IDs to PTH file offsets.
Ted Kremeneke5554de2009-02-11 21:29:16 +0000687 Offset IDOff = Out.tell();
688 Emit32(idcount); // Emit the number of identifiers.
Chris Lattnera94d1392009-03-28 00:16:20 +0000689 for (unsigned i = 0 ; i < idcount; ++i)
690 Emit32(IIDMap[i].FileOffset);
Mike Stump11289f42009-09-09 15:08:12 +0000691
Ted Kremeneke5554de2009-02-11 21:29:16 +0000692 // Finally, release the inverse map.
693 free(IIDMap);
Mike Stump11289f42009-09-09 15:08:12 +0000694
Ted Kremeneke5554de2009-02-11 21:29:16 +0000695 return std::make_pair(IDOff, StringTableOffset);
696}