1f4a2713aSLionel Sambuc //===--- CacheTokens.cpp - Caching of lexer tokens for PTH support --------===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc // The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc //
10f4a2713aSLionel Sambuc // This provides a possible implementation of PTH support for Clang that is
11f4a2713aSLionel Sambuc // based on caching lexed tokens and identifiers.
12f4a2713aSLionel Sambuc //
13f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
14f4a2713aSLionel Sambuc
15f4a2713aSLionel Sambuc #include "clang/Frontend/Utils.h"
16f4a2713aSLionel Sambuc #include "clang/Basic/Diagnostic.h"
17f4a2713aSLionel Sambuc #include "clang/Basic/FileManager.h"
18f4a2713aSLionel Sambuc #include "clang/Basic/FileSystemStatCache.h"
19f4a2713aSLionel Sambuc #include "clang/Basic/IdentifierTable.h"
20f4a2713aSLionel Sambuc #include "clang/Basic/SourceManager.h"
21f4a2713aSLionel Sambuc #include "clang/Lex/Lexer.h"
22f4a2713aSLionel Sambuc #include "clang/Lex/Preprocessor.h"
23f4a2713aSLionel Sambuc #include "llvm/ADT/StringExtras.h"
24f4a2713aSLionel Sambuc #include "llvm/ADT/StringMap.h"
25*0a6a1f1dSLionel Sambuc #include "llvm/Support/EndianStream.h"
26f4a2713aSLionel Sambuc #include "llvm/Support/FileSystem.h"
27f4a2713aSLionel Sambuc #include "llvm/Support/MemoryBuffer.h"
28*0a6a1f1dSLionel Sambuc #include "llvm/Support/OnDiskHashTable.h"
29f4a2713aSLionel Sambuc #include "llvm/Support/Path.h"
30f4a2713aSLionel Sambuc #include "llvm/Support/raw_ostream.h"
31f4a2713aSLionel Sambuc
32f4a2713aSLionel Sambuc // FIXME: put this somewhere else?
33f4a2713aSLionel Sambuc #ifndef S_ISDIR
34f4a2713aSLionel Sambuc #define S_ISDIR(x) (((x)&_S_IFDIR)!=0)
35f4a2713aSLionel Sambuc #endif
36f4a2713aSLionel Sambuc
37f4a2713aSLionel Sambuc using namespace clang;
38f4a2713aSLionel Sambuc
39f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
40f4a2713aSLionel Sambuc // PTH-specific stuff.
41f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
42f4a2713aSLionel Sambuc
43*0a6a1f1dSLionel Sambuc typedef uint32_t Offset;
44*0a6a1f1dSLionel Sambuc
45f4a2713aSLionel Sambuc namespace {
46f4a2713aSLionel Sambuc class PTHEntry {
47f4a2713aSLionel Sambuc Offset TokenData, PPCondData;
48f4a2713aSLionel Sambuc
49f4a2713aSLionel Sambuc public:
PTHEntry()50f4a2713aSLionel Sambuc PTHEntry() {}
51f4a2713aSLionel Sambuc
PTHEntry(Offset td,Offset ppcd)52f4a2713aSLionel Sambuc PTHEntry(Offset td, Offset ppcd)
53f4a2713aSLionel Sambuc : TokenData(td), PPCondData(ppcd) {}
54f4a2713aSLionel Sambuc
getTokenOffset() const55f4a2713aSLionel Sambuc Offset getTokenOffset() const { return TokenData; }
getPPCondTableOffset() const56f4a2713aSLionel Sambuc Offset getPPCondTableOffset() const { return PPCondData; }
57f4a2713aSLionel Sambuc };
58f4a2713aSLionel Sambuc
59f4a2713aSLionel Sambuc
60f4a2713aSLionel Sambuc class PTHEntryKeyVariant {
61f4a2713aSLionel Sambuc union { const FileEntry* FE; const char* Path; };
62f4a2713aSLionel Sambuc enum { IsFE = 0x1, IsDE = 0x2, IsNoExist = 0x0 } Kind;
63f4a2713aSLionel Sambuc FileData *Data;
64f4a2713aSLionel Sambuc
65f4a2713aSLionel Sambuc public:
PTHEntryKeyVariant(const FileEntry * fe)66*0a6a1f1dSLionel Sambuc PTHEntryKeyVariant(const FileEntry *fe) : FE(fe), Kind(IsFE), Data(nullptr) {}
67f4a2713aSLionel Sambuc
PTHEntryKeyVariant(FileData * Data,const char * path)68f4a2713aSLionel Sambuc PTHEntryKeyVariant(FileData *Data, const char *path)
69f4a2713aSLionel Sambuc : Path(path), Kind(IsDE), Data(new FileData(*Data)) {}
70f4a2713aSLionel Sambuc
PTHEntryKeyVariant(const char * path)71f4a2713aSLionel Sambuc explicit PTHEntryKeyVariant(const char *path)
72*0a6a1f1dSLionel Sambuc : Path(path), Kind(IsNoExist), Data(nullptr) {}
73f4a2713aSLionel Sambuc
isFile() const74f4a2713aSLionel Sambuc bool isFile() const { return Kind == IsFE; }
75f4a2713aSLionel Sambuc
getString() const76f4a2713aSLionel Sambuc StringRef getString() const {
77f4a2713aSLionel Sambuc return Kind == IsFE ? FE->getName() : Path;
78f4a2713aSLionel Sambuc }
79f4a2713aSLionel Sambuc
getKind() const80f4a2713aSLionel Sambuc unsigned getKind() const { return (unsigned) Kind; }
81f4a2713aSLionel Sambuc
EmitData(raw_ostream & Out)82f4a2713aSLionel Sambuc void EmitData(raw_ostream& Out) {
83*0a6a1f1dSLionel Sambuc using namespace llvm::support;
84*0a6a1f1dSLionel Sambuc endian::Writer<little> LE(Out);
85f4a2713aSLionel Sambuc switch (Kind) {
86f4a2713aSLionel Sambuc case IsFE: {
87f4a2713aSLionel Sambuc // Emit stat information.
88f4a2713aSLionel Sambuc llvm::sys::fs::UniqueID UID = FE->getUniqueID();
89*0a6a1f1dSLionel Sambuc LE.write<uint64_t>(UID.getFile());
90*0a6a1f1dSLionel Sambuc LE.write<uint64_t>(UID.getDevice());
91*0a6a1f1dSLionel Sambuc LE.write<uint64_t>(FE->getModificationTime());
92*0a6a1f1dSLionel Sambuc LE.write<uint64_t>(FE->getSize());
93f4a2713aSLionel Sambuc } break;
94f4a2713aSLionel Sambuc case IsDE:
95f4a2713aSLionel Sambuc // Emit stat information.
96*0a6a1f1dSLionel Sambuc LE.write<uint64_t>(Data->UniqueID.getFile());
97*0a6a1f1dSLionel Sambuc LE.write<uint64_t>(Data->UniqueID.getDevice());
98*0a6a1f1dSLionel Sambuc LE.write<uint64_t>(Data->ModTime);
99*0a6a1f1dSLionel Sambuc LE.write<uint64_t>(Data->Size);
100f4a2713aSLionel Sambuc delete Data;
101f4a2713aSLionel Sambuc break;
102f4a2713aSLionel Sambuc default:
103f4a2713aSLionel Sambuc break;
104f4a2713aSLionel Sambuc }
105f4a2713aSLionel Sambuc }
106f4a2713aSLionel Sambuc
getRepresentationLength() const107f4a2713aSLionel Sambuc unsigned getRepresentationLength() const {
108f4a2713aSLionel Sambuc return Kind == IsNoExist ? 0 : 4 + 4 + 2 + 8 + 8;
109f4a2713aSLionel Sambuc }
110f4a2713aSLionel Sambuc };
111f4a2713aSLionel Sambuc
112f4a2713aSLionel Sambuc class FileEntryPTHEntryInfo {
113f4a2713aSLionel Sambuc public:
114f4a2713aSLionel Sambuc typedef PTHEntryKeyVariant key_type;
115f4a2713aSLionel Sambuc typedef key_type key_type_ref;
116f4a2713aSLionel Sambuc
117f4a2713aSLionel Sambuc typedef PTHEntry data_type;
118f4a2713aSLionel Sambuc typedef const PTHEntry& data_type_ref;
119f4a2713aSLionel Sambuc
120*0a6a1f1dSLionel Sambuc typedef unsigned hash_value_type;
121*0a6a1f1dSLionel Sambuc typedef unsigned offset_type;
122*0a6a1f1dSLionel Sambuc
ComputeHash(PTHEntryKeyVariant V)123*0a6a1f1dSLionel Sambuc static hash_value_type ComputeHash(PTHEntryKeyVariant V) {
124f4a2713aSLionel Sambuc return llvm::HashString(V.getString());
125f4a2713aSLionel Sambuc }
126f4a2713aSLionel Sambuc
127f4a2713aSLionel Sambuc static std::pair<unsigned,unsigned>
EmitKeyDataLength(raw_ostream & Out,PTHEntryKeyVariant V,const PTHEntry & E)128f4a2713aSLionel Sambuc EmitKeyDataLength(raw_ostream& Out, PTHEntryKeyVariant V,
129f4a2713aSLionel Sambuc const PTHEntry& E) {
130*0a6a1f1dSLionel Sambuc using namespace llvm::support;
131*0a6a1f1dSLionel Sambuc endian::Writer<little> LE(Out);
132f4a2713aSLionel Sambuc
133f4a2713aSLionel Sambuc unsigned n = V.getString().size() + 1 + 1;
134*0a6a1f1dSLionel Sambuc LE.write<uint16_t>(n);
135f4a2713aSLionel Sambuc
136f4a2713aSLionel Sambuc unsigned m = V.getRepresentationLength() + (V.isFile() ? 4 + 4 : 0);
137*0a6a1f1dSLionel Sambuc LE.write<uint8_t>(m);
138f4a2713aSLionel Sambuc
139f4a2713aSLionel Sambuc return std::make_pair(n, m);
140f4a2713aSLionel Sambuc }
141f4a2713aSLionel Sambuc
EmitKey(raw_ostream & Out,PTHEntryKeyVariant V,unsigned n)142f4a2713aSLionel Sambuc static void EmitKey(raw_ostream& Out, PTHEntryKeyVariant V, unsigned n){
143*0a6a1f1dSLionel Sambuc using namespace llvm::support;
144f4a2713aSLionel Sambuc // Emit the entry kind.
145*0a6a1f1dSLionel Sambuc endian::Writer<little>(Out).write<uint8_t>((unsigned)V.getKind());
146f4a2713aSLionel Sambuc // Emit the string.
147f4a2713aSLionel Sambuc Out.write(V.getString().data(), n - 1);
148f4a2713aSLionel Sambuc }
149f4a2713aSLionel Sambuc
EmitData(raw_ostream & Out,PTHEntryKeyVariant V,const PTHEntry & E,unsigned)150f4a2713aSLionel Sambuc static void EmitData(raw_ostream& Out, PTHEntryKeyVariant V,
151f4a2713aSLionel Sambuc const PTHEntry& E, unsigned) {
152*0a6a1f1dSLionel Sambuc using namespace llvm::support;
153*0a6a1f1dSLionel Sambuc endian::Writer<little> LE(Out);
154f4a2713aSLionel Sambuc
155f4a2713aSLionel Sambuc // For file entries emit the offsets into the PTH file for token data
156f4a2713aSLionel Sambuc // and the preprocessor blocks table.
157f4a2713aSLionel Sambuc if (V.isFile()) {
158*0a6a1f1dSLionel Sambuc LE.write<uint32_t>(E.getTokenOffset());
159*0a6a1f1dSLionel Sambuc LE.write<uint32_t>(E.getPPCondTableOffset());
160f4a2713aSLionel Sambuc }
161f4a2713aSLionel Sambuc
162f4a2713aSLionel Sambuc // Emit any other data associated with the key (i.e., stat information).
163f4a2713aSLionel Sambuc V.EmitData(Out);
164f4a2713aSLionel Sambuc }
165f4a2713aSLionel Sambuc };
166f4a2713aSLionel Sambuc
167f4a2713aSLionel Sambuc class OffsetOpt {
168f4a2713aSLionel Sambuc bool valid;
169f4a2713aSLionel Sambuc Offset off;
170f4a2713aSLionel Sambuc public:
OffsetOpt()171f4a2713aSLionel Sambuc OffsetOpt() : valid(false) {}
hasOffset() const172f4a2713aSLionel Sambuc bool hasOffset() const { return valid; }
getOffset() const173f4a2713aSLionel Sambuc Offset getOffset() const { assert(valid); return off; }
setOffset(Offset o)174f4a2713aSLionel Sambuc void setOffset(Offset o) { off = o; valid = true; }
175f4a2713aSLionel Sambuc };
176f4a2713aSLionel Sambuc } // end anonymous namespace
177f4a2713aSLionel Sambuc
178*0a6a1f1dSLionel Sambuc typedef llvm::OnDiskChainedHashTableGenerator<FileEntryPTHEntryInfo> PTHMap;
179f4a2713aSLionel Sambuc
180f4a2713aSLionel Sambuc namespace {
181f4a2713aSLionel Sambuc class PTHWriter {
182f4a2713aSLionel Sambuc typedef llvm::DenseMap<const IdentifierInfo*,uint32_t> IDMap;
183f4a2713aSLionel Sambuc typedef llvm::StringMap<OffsetOpt, llvm::BumpPtrAllocator> CachedStrsTy;
184f4a2713aSLionel Sambuc
185f4a2713aSLionel Sambuc IDMap IM;
186f4a2713aSLionel Sambuc llvm::raw_fd_ostream& Out;
187f4a2713aSLionel Sambuc Preprocessor& PP;
188f4a2713aSLionel Sambuc uint32_t idcount;
189f4a2713aSLionel Sambuc PTHMap PM;
190f4a2713aSLionel Sambuc CachedStrsTy CachedStrs;
191f4a2713aSLionel Sambuc Offset CurStrOffset;
192f4a2713aSLionel Sambuc std::vector<llvm::StringMapEntry<OffsetOpt>*> StrEntries;
193f4a2713aSLionel Sambuc
194f4a2713aSLionel Sambuc //// Get the persistent id for the given IdentifierInfo*.
195f4a2713aSLionel Sambuc uint32_t ResolveID(const IdentifierInfo* II);
196f4a2713aSLionel Sambuc
197f4a2713aSLionel Sambuc /// Emit a token to the PTH file.
198f4a2713aSLionel Sambuc void EmitToken(const Token& T);
199f4a2713aSLionel Sambuc
Emit8(uint32_t V)200*0a6a1f1dSLionel Sambuc void Emit8(uint32_t V) {
201*0a6a1f1dSLionel Sambuc using namespace llvm::support;
202*0a6a1f1dSLionel Sambuc endian::Writer<little>(Out).write<uint8_t>(V);
203*0a6a1f1dSLionel Sambuc }
204f4a2713aSLionel Sambuc
Emit16(uint32_t V)205*0a6a1f1dSLionel Sambuc void Emit16(uint32_t V) {
206*0a6a1f1dSLionel Sambuc using namespace llvm::support;
207*0a6a1f1dSLionel Sambuc endian::Writer<little>(Out).write<uint16_t>(V);
208*0a6a1f1dSLionel Sambuc }
209f4a2713aSLionel Sambuc
Emit32(uint32_t V)210*0a6a1f1dSLionel Sambuc void Emit32(uint32_t V) {
211*0a6a1f1dSLionel Sambuc using namespace llvm::support;
212*0a6a1f1dSLionel Sambuc endian::Writer<little>(Out).write<uint32_t>(V);
213*0a6a1f1dSLionel Sambuc }
214f4a2713aSLionel Sambuc
EmitBuf(const char * Ptr,unsigned NumBytes)215f4a2713aSLionel Sambuc void EmitBuf(const char *Ptr, unsigned NumBytes) {
216f4a2713aSLionel Sambuc Out.write(Ptr, NumBytes);
217f4a2713aSLionel Sambuc }
218f4a2713aSLionel Sambuc
EmitString(StringRef V)219f4a2713aSLionel Sambuc void EmitString(StringRef V) {
220*0a6a1f1dSLionel Sambuc using namespace llvm::support;
221*0a6a1f1dSLionel Sambuc endian::Writer<little>(Out).write<uint16_t>(V.size());
222f4a2713aSLionel Sambuc EmitBuf(V.data(), V.size());
223f4a2713aSLionel Sambuc }
224f4a2713aSLionel Sambuc
225f4a2713aSLionel Sambuc /// EmitIdentifierTable - Emits two tables to the PTH file. The first is
226f4a2713aSLionel Sambuc /// a hashtable mapping from identifier strings to persistent IDs.
227f4a2713aSLionel Sambuc /// The second is a straight table mapping from persistent IDs to string data
228f4a2713aSLionel Sambuc /// (the keys of the first table).
229f4a2713aSLionel Sambuc std::pair<Offset, Offset> EmitIdentifierTable();
230f4a2713aSLionel Sambuc
231f4a2713aSLionel Sambuc /// EmitFileTable - Emit a table mapping from file name strings to PTH
232f4a2713aSLionel Sambuc /// token data.
EmitFileTable()233f4a2713aSLionel Sambuc Offset EmitFileTable() { return PM.Emit(Out); }
234f4a2713aSLionel Sambuc
235f4a2713aSLionel Sambuc PTHEntry LexTokens(Lexer& L);
236f4a2713aSLionel Sambuc Offset EmitCachedSpellings();
237f4a2713aSLionel Sambuc
238f4a2713aSLionel Sambuc public:
PTHWriter(llvm::raw_fd_ostream & out,Preprocessor & pp)239f4a2713aSLionel Sambuc PTHWriter(llvm::raw_fd_ostream& out, Preprocessor& pp)
240f4a2713aSLionel Sambuc : Out(out), PP(pp), idcount(0), CurStrOffset(0) {}
241f4a2713aSLionel Sambuc
getPM()242f4a2713aSLionel Sambuc PTHMap &getPM() { return PM; }
243f4a2713aSLionel Sambuc void GeneratePTH(const std::string &MainFile);
244f4a2713aSLionel Sambuc };
245f4a2713aSLionel Sambuc } // end anonymous namespace
246f4a2713aSLionel Sambuc
ResolveID(const IdentifierInfo * II)247f4a2713aSLionel Sambuc uint32_t PTHWriter::ResolveID(const IdentifierInfo* II) {
248f4a2713aSLionel Sambuc // Null IdentifierInfo's map to the persistent ID 0.
249f4a2713aSLionel Sambuc if (!II)
250f4a2713aSLionel Sambuc return 0;
251f4a2713aSLionel Sambuc
252f4a2713aSLionel Sambuc IDMap::iterator I = IM.find(II);
253f4a2713aSLionel Sambuc if (I != IM.end())
254f4a2713aSLionel Sambuc return I->second; // We've already added 1.
255f4a2713aSLionel Sambuc
256f4a2713aSLionel Sambuc IM[II] = ++idcount; // Pre-increment since '0' is reserved for NULL.
257f4a2713aSLionel Sambuc return idcount;
258f4a2713aSLionel Sambuc }
259f4a2713aSLionel Sambuc
EmitToken(const Token & T)260f4a2713aSLionel Sambuc void PTHWriter::EmitToken(const Token& T) {
261f4a2713aSLionel Sambuc // Emit the token kind, flags, and length.
262f4a2713aSLionel Sambuc Emit32(((uint32_t) T.getKind()) | ((((uint32_t) T.getFlags())) << 8)|
263f4a2713aSLionel Sambuc (((uint32_t) T.getLength()) << 16));
264f4a2713aSLionel Sambuc
265f4a2713aSLionel Sambuc if (!T.isLiteral()) {
266f4a2713aSLionel Sambuc Emit32(ResolveID(T.getIdentifierInfo()));
267f4a2713aSLionel Sambuc } else {
268f4a2713aSLionel Sambuc // We cache *un-cleaned* spellings. This gives us 100% fidelity with the
269f4a2713aSLionel Sambuc // source code.
270f4a2713aSLionel Sambuc StringRef s(T.getLiteralData(), T.getLength());
271f4a2713aSLionel Sambuc
272f4a2713aSLionel Sambuc // Get the string entry.
273*0a6a1f1dSLionel Sambuc auto &E = *CachedStrs.insert(std::make_pair(s, OffsetOpt())).first;
274f4a2713aSLionel Sambuc
275f4a2713aSLionel Sambuc // If this is a new string entry, bump the PTH offset.
276*0a6a1f1dSLionel Sambuc if (!E.second.hasOffset()) {
277*0a6a1f1dSLionel Sambuc E.second.setOffset(CurStrOffset);
278*0a6a1f1dSLionel Sambuc StrEntries.push_back(&E);
279f4a2713aSLionel Sambuc CurStrOffset += s.size() + 1;
280f4a2713aSLionel Sambuc }
281f4a2713aSLionel Sambuc
282f4a2713aSLionel Sambuc // Emit the relative offset into the PTH file for the spelling string.
283*0a6a1f1dSLionel Sambuc Emit32(E.second.getOffset());
284f4a2713aSLionel Sambuc }
285f4a2713aSLionel Sambuc
286f4a2713aSLionel Sambuc // Emit the offset into the original source file of this token so that we
287f4a2713aSLionel Sambuc // can reconstruct its SourceLocation.
288f4a2713aSLionel Sambuc Emit32(PP.getSourceManager().getFileOffset(T.getLocation()));
289f4a2713aSLionel Sambuc }
290f4a2713aSLionel Sambuc
LexTokens(Lexer & L)291f4a2713aSLionel Sambuc PTHEntry PTHWriter::LexTokens(Lexer& L) {
292f4a2713aSLionel Sambuc // Pad 0's so that we emit tokens to a 4-byte alignment.
293f4a2713aSLionel Sambuc // This speed up reading them back in.
294*0a6a1f1dSLionel Sambuc using namespace llvm::support;
295*0a6a1f1dSLionel Sambuc endian::Writer<little> LE(Out);
296*0a6a1f1dSLionel Sambuc uint32_t TokenOff = Out.tell();
297*0a6a1f1dSLionel Sambuc for (uint64_t N = llvm::OffsetToAlignment(TokenOff, 4); N; --N, ++TokenOff)
298*0a6a1f1dSLionel Sambuc LE.write<uint8_t>(0);
299f4a2713aSLionel Sambuc
300f4a2713aSLionel Sambuc // Keep track of matching '#if' ... '#endif'.
301f4a2713aSLionel Sambuc typedef std::vector<std::pair<Offset, unsigned> > PPCondTable;
302f4a2713aSLionel Sambuc PPCondTable PPCond;
303f4a2713aSLionel Sambuc std::vector<unsigned> PPStartCond;
304f4a2713aSLionel Sambuc bool ParsingPreprocessorDirective = false;
305f4a2713aSLionel Sambuc Token Tok;
306f4a2713aSLionel Sambuc
307f4a2713aSLionel Sambuc do {
308f4a2713aSLionel Sambuc L.LexFromRawLexer(Tok);
309f4a2713aSLionel Sambuc NextToken:
310f4a2713aSLionel Sambuc
311f4a2713aSLionel Sambuc if ((Tok.isAtStartOfLine() || Tok.is(tok::eof)) &&
312f4a2713aSLionel Sambuc ParsingPreprocessorDirective) {
313f4a2713aSLionel Sambuc // Insert an eod token into the token cache. It has the same
314f4a2713aSLionel Sambuc // position as the next token that is not on the same line as the
315f4a2713aSLionel Sambuc // preprocessor directive. Observe that we continue processing
316f4a2713aSLionel Sambuc // 'Tok' when we exit this branch.
317f4a2713aSLionel Sambuc Token Tmp = Tok;
318f4a2713aSLionel Sambuc Tmp.setKind(tok::eod);
319f4a2713aSLionel Sambuc Tmp.clearFlag(Token::StartOfLine);
320*0a6a1f1dSLionel Sambuc Tmp.setIdentifierInfo(nullptr);
321f4a2713aSLionel Sambuc EmitToken(Tmp);
322f4a2713aSLionel Sambuc ParsingPreprocessorDirective = false;
323f4a2713aSLionel Sambuc }
324f4a2713aSLionel Sambuc
325f4a2713aSLionel Sambuc if (Tok.is(tok::raw_identifier)) {
326f4a2713aSLionel Sambuc PP.LookUpIdentifierInfo(Tok);
327f4a2713aSLionel Sambuc EmitToken(Tok);
328f4a2713aSLionel Sambuc continue;
329f4a2713aSLionel Sambuc }
330f4a2713aSLionel Sambuc
331f4a2713aSLionel Sambuc if (Tok.is(tok::hash) && Tok.isAtStartOfLine()) {
332f4a2713aSLionel Sambuc // Special processing for #include. Store the '#' token and lex
333f4a2713aSLionel Sambuc // the next token.
334f4a2713aSLionel Sambuc assert(!ParsingPreprocessorDirective);
335f4a2713aSLionel Sambuc Offset HashOff = (Offset) Out.tell();
336f4a2713aSLionel Sambuc
337f4a2713aSLionel Sambuc // Get the next token.
338f4a2713aSLionel Sambuc Token NextTok;
339f4a2713aSLionel Sambuc L.LexFromRawLexer(NextTok);
340f4a2713aSLionel Sambuc
341f4a2713aSLionel Sambuc // If we see the start of line, then we had a null directive "#". In
342f4a2713aSLionel Sambuc // this case, discard both tokens.
343f4a2713aSLionel Sambuc if (NextTok.isAtStartOfLine())
344f4a2713aSLionel Sambuc goto NextToken;
345f4a2713aSLionel Sambuc
346f4a2713aSLionel Sambuc // The token is the start of a directive. Emit it.
347f4a2713aSLionel Sambuc EmitToken(Tok);
348f4a2713aSLionel Sambuc Tok = NextTok;
349f4a2713aSLionel Sambuc
350f4a2713aSLionel Sambuc // Did we see 'include'/'import'/'include_next'?
351f4a2713aSLionel Sambuc if (Tok.isNot(tok::raw_identifier)) {
352f4a2713aSLionel Sambuc EmitToken(Tok);
353f4a2713aSLionel Sambuc continue;
354f4a2713aSLionel Sambuc }
355f4a2713aSLionel Sambuc
356f4a2713aSLionel Sambuc IdentifierInfo* II = PP.LookUpIdentifierInfo(Tok);
357f4a2713aSLionel Sambuc tok::PPKeywordKind K = II->getPPKeywordID();
358f4a2713aSLionel Sambuc
359f4a2713aSLionel Sambuc ParsingPreprocessorDirective = true;
360f4a2713aSLionel Sambuc
361f4a2713aSLionel Sambuc switch (K) {
362f4a2713aSLionel Sambuc case tok::pp_not_keyword:
363f4a2713aSLionel Sambuc // Invalid directives "#foo" can occur in #if 0 blocks etc, just pass
364f4a2713aSLionel Sambuc // them through.
365f4a2713aSLionel Sambuc default:
366f4a2713aSLionel Sambuc break;
367f4a2713aSLionel Sambuc
368f4a2713aSLionel Sambuc case tok::pp_include:
369f4a2713aSLionel Sambuc case tok::pp_import:
370f4a2713aSLionel Sambuc case tok::pp_include_next: {
371f4a2713aSLionel Sambuc // Save the 'include' token.
372f4a2713aSLionel Sambuc EmitToken(Tok);
373f4a2713aSLionel Sambuc // Lex the next token as an include string.
374f4a2713aSLionel Sambuc L.setParsingPreprocessorDirective(true);
375f4a2713aSLionel Sambuc L.LexIncludeFilename(Tok);
376f4a2713aSLionel Sambuc L.setParsingPreprocessorDirective(false);
377f4a2713aSLionel Sambuc assert(!Tok.isAtStartOfLine());
378f4a2713aSLionel Sambuc if (Tok.is(tok::raw_identifier))
379f4a2713aSLionel Sambuc PP.LookUpIdentifierInfo(Tok);
380f4a2713aSLionel Sambuc
381f4a2713aSLionel Sambuc break;
382f4a2713aSLionel Sambuc }
383f4a2713aSLionel Sambuc case tok::pp_if:
384f4a2713aSLionel Sambuc case tok::pp_ifdef:
385f4a2713aSLionel Sambuc case tok::pp_ifndef: {
386f4a2713aSLionel Sambuc // Add an entry for '#if' and friends. We initially set the target
387f4a2713aSLionel Sambuc // index to 0. This will get backpatched when we hit #endif.
388f4a2713aSLionel Sambuc PPStartCond.push_back(PPCond.size());
389f4a2713aSLionel Sambuc PPCond.push_back(std::make_pair(HashOff, 0U));
390f4a2713aSLionel Sambuc break;
391f4a2713aSLionel Sambuc }
392f4a2713aSLionel Sambuc case tok::pp_endif: {
393f4a2713aSLionel Sambuc // Add an entry for '#endif'. We set the target table index to itself.
394f4a2713aSLionel Sambuc // This will later be set to zero when emitting to the PTH file. We
395f4a2713aSLionel Sambuc // use 0 for uninitialized indices because that is easier to debug.
396f4a2713aSLionel Sambuc unsigned index = PPCond.size();
397f4a2713aSLionel Sambuc // Backpatch the opening '#if' entry.
398f4a2713aSLionel Sambuc assert(!PPStartCond.empty());
399f4a2713aSLionel Sambuc assert(PPCond.size() > PPStartCond.back());
400f4a2713aSLionel Sambuc assert(PPCond[PPStartCond.back()].second == 0);
401f4a2713aSLionel Sambuc PPCond[PPStartCond.back()].second = index;
402f4a2713aSLionel Sambuc PPStartCond.pop_back();
403f4a2713aSLionel Sambuc // Add the new entry to PPCond.
404f4a2713aSLionel Sambuc PPCond.push_back(std::make_pair(HashOff, index));
405f4a2713aSLionel Sambuc EmitToken(Tok);
406f4a2713aSLionel Sambuc
407f4a2713aSLionel Sambuc // Some files have gibberish on the same line as '#endif'.
408f4a2713aSLionel Sambuc // Discard these tokens.
409f4a2713aSLionel Sambuc do
410f4a2713aSLionel Sambuc L.LexFromRawLexer(Tok);
411f4a2713aSLionel Sambuc while (Tok.isNot(tok::eof) && !Tok.isAtStartOfLine());
412f4a2713aSLionel Sambuc // We have the next token in hand.
413f4a2713aSLionel Sambuc // Don't immediately lex the next one.
414f4a2713aSLionel Sambuc goto NextToken;
415f4a2713aSLionel Sambuc }
416f4a2713aSLionel Sambuc case tok::pp_elif:
417f4a2713aSLionel Sambuc case tok::pp_else: {
418f4a2713aSLionel Sambuc // Add an entry for #elif or #else.
419f4a2713aSLionel Sambuc // This serves as both a closing and opening of a conditional block.
420f4a2713aSLionel Sambuc // This means that its entry will get backpatched later.
421f4a2713aSLionel Sambuc unsigned index = PPCond.size();
422f4a2713aSLionel Sambuc // Backpatch the previous '#if' entry.
423f4a2713aSLionel Sambuc assert(!PPStartCond.empty());
424f4a2713aSLionel Sambuc assert(PPCond.size() > PPStartCond.back());
425f4a2713aSLionel Sambuc assert(PPCond[PPStartCond.back()].second == 0);
426f4a2713aSLionel Sambuc PPCond[PPStartCond.back()].second = index;
427f4a2713aSLionel Sambuc PPStartCond.pop_back();
428f4a2713aSLionel Sambuc // Now add '#elif' as a new block opening.
429f4a2713aSLionel Sambuc PPCond.push_back(std::make_pair(HashOff, 0U));
430f4a2713aSLionel Sambuc PPStartCond.push_back(index);
431f4a2713aSLionel Sambuc break;
432f4a2713aSLionel Sambuc }
433f4a2713aSLionel Sambuc }
434f4a2713aSLionel Sambuc }
435f4a2713aSLionel Sambuc
436f4a2713aSLionel Sambuc EmitToken(Tok);
437f4a2713aSLionel Sambuc }
438f4a2713aSLionel Sambuc while (Tok.isNot(tok::eof));
439f4a2713aSLionel Sambuc
440f4a2713aSLionel Sambuc assert(PPStartCond.empty() && "Error: imblanced preprocessor conditionals.");
441f4a2713aSLionel Sambuc
442f4a2713aSLionel Sambuc // Next write out PPCond.
443f4a2713aSLionel Sambuc Offset PPCondOff = (Offset) Out.tell();
444f4a2713aSLionel Sambuc
445f4a2713aSLionel Sambuc // Write out the size of PPCond so that clients can identifer empty tables.
446f4a2713aSLionel Sambuc Emit32(PPCond.size());
447f4a2713aSLionel Sambuc
448f4a2713aSLionel Sambuc for (unsigned i = 0, e = PPCond.size(); i!=e; ++i) {
449f4a2713aSLionel Sambuc Emit32(PPCond[i].first - TokenOff);
450f4a2713aSLionel Sambuc uint32_t x = PPCond[i].second;
451f4a2713aSLionel Sambuc assert(x != 0 && "PPCond entry not backpatched.");
452f4a2713aSLionel Sambuc // Emit zero for #endifs. This allows us to do checking when
453f4a2713aSLionel Sambuc // we read the PTH file back in.
454f4a2713aSLionel Sambuc Emit32(x == i ? 0 : x);
455f4a2713aSLionel Sambuc }
456f4a2713aSLionel Sambuc
457f4a2713aSLionel Sambuc return PTHEntry(TokenOff, PPCondOff);
458f4a2713aSLionel Sambuc }
459f4a2713aSLionel Sambuc
EmitCachedSpellings()460f4a2713aSLionel Sambuc Offset PTHWriter::EmitCachedSpellings() {
461f4a2713aSLionel Sambuc // Write each cached strings to the PTH file.
462f4a2713aSLionel Sambuc Offset SpellingsOff = Out.tell();
463f4a2713aSLionel Sambuc
464f4a2713aSLionel Sambuc for (std::vector<llvm::StringMapEntry<OffsetOpt>*>::iterator
465f4a2713aSLionel Sambuc I = StrEntries.begin(), E = StrEntries.end(); I!=E; ++I)
466f4a2713aSLionel Sambuc EmitBuf((*I)->getKeyData(), (*I)->getKeyLength()+1 /*nul included*/);
467f4a2713aSLionel Sambuc
468f4a2713aSLionel Sambuc return SpellingsOff;
469f4a2713aSLionel Sambuc }
470f4a2713aSLionel Sambuc
GeneratePTH(const std::string & MainFile)471f4a2713aSLionel Sambuc void PTHWriter::GeneratePTH(const std::string &MainFile) {
472f4a2713aSLionel Sambuc // Generate the prologue.
473f4a2713aSLionel Sambuc Out << "cfe-pth" << '\0';
474f4a2713aSLionel Sambuc Emit32(PTHManager::Version);
475f4a2713aSLionel Sambuc
476f4a2713aSLionel Sambuc // Leave 4 words for the prologue.
477f4a2713aSLionel Sambuc Offset PrologueOffset = Out.tell();
478f4a2713aSLionel Sambuc for (unsigned i = 0; i < 4; ++i)
479f4a2713aSLionel Sambuc Emit32(0);
480f4a2713aSLionel Sambuc
481f4a2713aSLionel Sambuc // Write the name of the MainFile.
482f4a2713aSLionel Sambuc if (!MainFile.empty()) {
483f4a2713aSLionel Sambuc EmitString(MainFile);
484f4a2713aSLionel Sambuc } else {
485f4a2713aSLionel Sambuc // String with 0 bytes.
486f4a2713aSLionel Sambuc Emit16(0);
487f4a2713aSLionel Sambuc }
488f4a2713aSLionel Sambuc Emit8(0);
489f4a2713aSLionel Sambuc
490f4a2713aSLionel Sambuc // Iterate over all the files in SourceManager. Create a lexer
491f4a2713aSLionel Sambuc // for each file and cache the tokens.
492f4a2713aSLionel Sambuc SourceManager &SM = PP.getSourceManager();
493f4a2713aSLionel Sambuc const LangOptions &LOpts = PP.getLangOpts();
494f4a2713aSLionel Sambuc
495f4a2713aSLionel Sambuc for (SourceManager::fileinfo_iterator I = SM.fileinfo_begin(),
496f4a2713aSLionel Sambuc E = SM.fileinfo_end(); I != E; ++I) {
497f4a2713aSLionel Sambuc const SrcMgr::ContentCache &C = *I->second;
498f4a2713aSLionel Sambuc const FileEntry *FE = C.OrigEntry;
499f4a2713aSLionel Sambuc
500f4a2713aSLionel Sambuc // FIXME: Handle files with non-absolute paths.
501f4a2713aSLionel Sambuc if (llvm::sys::path::is_relative(FE->getName()))
502f4a2713aSLionel Sambuc continue;
503f4a2713aSLionel Sambuc
504f4a2713aSLionel Sambuc const llvm::MemoryBuffer *B = C.getBuffer(PP.getDiagnostics(), SM);
505f4a2713aSLionel Sambuc if (!B) continue;
506f4a2713aSLionel Sambuc
507f4a2713aSLionel Sambuc FileID FID = SM.createFileID(FE, SourceLocation(), SrcMgr::C_User);
508f4a2713aSLionel Sambuc const llvm::MemoryBuffer *FromFile = SM.getBuffer(FID);
509f4a2713aSLionel Sambuc Lexer L(FID, FromFile, SM, LOpts);
510f4a2713aSLionel Sambuc PM.insert(FE, LexTokens(L));
511f4a2713aSLionel Sambuc }
512f4a2713aSLionel Sambuc
513f4a2713aSLionel Sambuc // Write out the identifier table.
514f4a2713aSLionel Sambuc const std::pair<Offset,Offset> &IdTableOff = EmitIdentifierTable();
515f4a2713aSLionel Sambuc
516f4a2713aSLionel Sambuc // Write out the cached strings table.
517f4a2713aSLionel Sambuc Offset SpellingOff = EmitCachedSpellings();
518f4a2713aSLionel Sambuc
519f4a2713aSLionel Sambuc // Write out the file table.
520f4a2713aSLionel Sambuc Offset FileTableOff = EmitFileTable();
521f4a2713aSLionel Sambuc
522f4a2713aSLionel Sambuc // Finally, write the prologue.
523f4a2713aSLionel Sambuc Out.seek(PrologueOffset);
524f4a2713aSLionel Sambuc Emit32(IdTableOff.first);
525f4a2713aSLionel Sambuc Emit32(IdTableOff.second);
526f4a2713aSLionel Sambuc Emit32(FileTableOff);
527f4a2713aSLionel Sambuc Emit32(SpellingOff);
528f4a2713aSLionel Sambuc }
529f4a2713aSLionel Sambuc
530f4a2713aSLionel Sambuc namespace {
531f4a2713aSLionel Sambuc /// StatListener - A simple "interpose" object used to monitor stat calls
532f4a2713aSLionel Sambuc /// invoked by FileManager while processing the original sources used
533f4a2713aSLionel Sambuc /// as input to PTH generation. StatListener populates the PTHWriter's
534f4a2713aSLionel Sambuc /// file map with stat information for directories as well as negative stats.
535f4a2713aSLionel Sambuc /// Stat information for files are populated elsewhere.
536f4a2713aSLionel Sambuc class StatListener : public FileSystemStatCache {
537f4a2713aSLionel Sambuc PTHMap &PM;
538f4a2713aSLionel Sambuc public:
StatListener(PTHMap & pm)539f4a2713aSLionel Sambuc StatListener(PTHMap &pm) : PM(pm) {}
~StatListener()540f4a2713aSLionel Sambuc ~StatListener() {}
541f4a2713aSLionel Sambuc
getStat(const char * Path,FileData & Data,bool isFile,std::unique_ptr<vfs::File> * F,vfs::FileSystem & FS)542f4a2713aSLionel Sambuc LookupResult getStat(const char *Path, FileData &Data, bool isFile,
543*0a6a1f1dSLionel Sambuc std::unique_ptr<vfs::File> *F,
544*0a6a1f1dSLionel Sambuc vfs::FileSystem &FS) override {
545*0a6a1f1dSLionel Sambuc LookupResult Result = statChained(Path, Data, isFile, F, FS);
546f4a2713aSLionel Sambuc
547f4a2713aSLionel Sambuc if (Result == CacheMissing) // Failed 'stat'.
548f4a2713aSLionel Sambuc PM.insert(PTHEntryKeyVariant(Path), PTHEntry());
549f4a2713aSLionel Sambuc else if (Data.IsDirectory) {
550f4a2713aSLionel Sambuc // Only cache directories with absolute paths.
551f4a2713aSLionel Sambuc if (llvm::sys::path::is_relative(Path))
552f4a2713aSLionel Sambuc return Result;
553f4a2713aSLionel Sambuc
554f4a2713aSLionel Sambuc PM.insert(PTHEntryKeyVariant(&Data, Path), PTHEntry());
555f4a2713aSLionel Sambuc }
556f4a2713aSLionel Sambuc
557f4a2713aSLionel Sambuc return Result;
558f4a2713aSLionel Sambuc }
559f4a2713aSLionel Sambuc };
560f4a2713aSLionel Sambuc } // end anonymous namespace
561f4a2713aSLionel Sambuc
562f4a2713aSLionel Sambuc
CacheTokens(Preprocessor & PP,llvm::raw_fd_ostream * OS)563f4a2713aSLionel Sambuc void clang::CacheTokens(Preprocessor &PP, llvm::raw_fd_ostream* OS) {
564f4a2713aSLionel Sambuc // Get the name of the main file.
565f4a2713aSLionel Sambuc const SourceManager &SrcMgr = PP.getSourceManager();
566f4a2713aSLionel Sambuc const FileEntry *MainFile = SrcMgr.getFileEntryForID(SrcMgr.getMainFileID());
567f4a2713aSLionel Sambuc SmallString<128> MainFilePath(MainFile->getName());
568f4a2713aSLionel Sambuc
569f4a2713aSLionel Sambuc llvm::sys::fs::make_absolute(MainFilePath);
570f4a2713aSLionel Sambuc
571f4a2713aSLionel Sambuc // Create the PTHWriter.
572f4a2713aSLionel Sambuc PTHWriter PW(*OS, PP);
573f4a2713aSLionel Sambuc
574f4a2713aSLionel Sambuc // Install the 'stat' system call listener in the FileManager.
575*0a6a1f1dSLionel Sambuc auto StatCacheOwner = llvm::make_unique<StatListener>(PW.getPM());
576*0a6a1f1dSLionel Sambuc StatListener *StatCache = StatCacheOwner.get();
577*0a6a1f1dSLionel Sambuc PP.getFileManager().addStatCache(std::move(StatCacheOwner),
578*0a6a1f1dSLionel Sambuc /*AtBeginning=*/true);
579f4a2713aSLionel Sambuc
580f4a2713aSLionel Sambuc // Lex through the entire file. This will populate SourceManager with
581f4a2713aSLionel Sambuc // all of the header information.
582f4a2713aSLionel Sambuc Token Tok;
583f4a2713aSLionel Sambuc PP.EnterMainSourceFile();
584f4a2713aSLionel Sambuc do { PP.Lex(Tok); } while (Tok.isNot(tok::eof));
585f4a2713aSLionel Sambuc
586f4a2713aSLionel Sambuc // Generate the PTH file.
587f4a2713aSLionel Sambuc PP.getFileManager().removeStatCache(StatCache);
588f4a2713aSLionel Sambuc PW.GeneratePTH(MainFilePath.str());
589f4a2713aSLionel Sambuc }
590f4a2713aSLionel Sambuc
591f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
592f4a2713aSLionel Sambuc
593f4a2713aSLionel Sambuc namespace {
594f4a2713aSLionel Sambuc class PTHIdKey {
595f4a2713aSLionel Sambuc public:
596f4a2713aSLionel Sambuc const IdentifierInfo* II;
597f4a2713aSLionel Sambuc uint32_t FileOffset;
598f4a2713aSLionel Sambuc };
599f4a2713aSLionel Sambuc
600f4a2713aSLionel Sambuc class PTHIdentifierTableTrait {
601f4a2713aSLionel Sambuc public:
602f4a2713aSLionel Sambuc typedef PTHIdKey* key_type;
603f4a2713aSLionel Sambuc typedef key_type key_type_ref;
604f4a2713aSLionel Sambuc
605f4a2713aSLionel Sambuc typedef uint32_t data_type;
606f4a2713aSLionel Sambuc typedef data_type data_type_ref;
607f4a2713aSLionel Sambuc
608*0a6a1f1dSLionel Sambuc typedef unsigned hash_value_type;
609*0a6a1f1dSLionel Sambuc typedef unsigned offset_type;
610*0a6a1f1dSLionel Sambuc
ComputeHash(PTHIdKey * key)611*0a6a1f1dSLionel Sambuc static hash_value_type ComputeHash(PTHIdKey* key) {
612f4a2713aSLionel Sambuc return llvm::HashString(key->II->getName());
613f4a2713aSLionel Sambuc }
614f4a2713aSLionel Sambuc
615f4a2713aSLionel Sambuc static std::pair<unsigned,unsigned>
EmitKeyDataLength(raw_ostream & Out,const PTHIdKey * key,uint32_t)616f4a2713aSLionel Sambuc EmitKeyDataLength(raw_ostream& Out, const PTHIdKey* key, uint32_t) {
617*0a6a1f1dSLionel Sambuc using namespace llvm::support;
618f4a2713aSLionel Sambuc unsigned n = key->II->getLength() + 1;
619*0a6a1f1dSLionel Sambuc endian::Writer<little>(Out).write<uint16_t>(n);
620f4a2713aSLionel Sambuc return std::make_pair(n, sizeof(uint32_t));
621f4a2713aSLionel Sambuc }
622f4a2713aSLionel Sambuc
EmitKey(raw_ostream & Out,PTHIdKey * key,unsigned n)623f4a2713aSLionel Sambuc static void EmitKey(raw_ostream& Out, PTHIdKey* key, unsigned n) {
624f4a2713aSLionel Sambuc // Record the location of the key data. This is used when generating
625f4a2713aSLionel Sambuc // the mapping from persistent IDs to strings.
626f4a2713aSLionel Sambuc key->FileOffset = Out.tell();
627f4a2713aSLionel Sambuc Out.write(key->II->getNameStart(), n);
628f4a2713aSLionel Sambuc }
629f4a2713aSLionel Sambuc
EmitData(raw_ostream & Out,PTHIdKey *,uint32_t pID,unsigned)630f4a2713aSLionel Sambuc static void EmitData(raw_ostream& Out, PTHIdKey*, uint32_t pID,
631f4a2713aSLionel Sambuc unsigned) {
632*0a6a1f1dSLionel Sambuc using namespace llvm::support;
633*0a6a1f1dSLionel Sambuc endian::Writer<little>(Out).write<uint32_t>(pID);
634f4a2713aSLionel Sambuc }
635f4a2713aSLionel Sambuc };
636f4a2713aSLionel Sambuc } // end anonymous namespace
637f4a2713aSLionel Sambuc
638f4a2713aSLionel Sambuc /// EmitIdentifierTable - Emits two tables to the PTH file. The first is
639f4a2713aSLionel Sambuc /// a hashtable mapping from identifier strings to persistent IDs. The second
640f4a2713aSLionel Sambuc /// is a straight table mapping from persistent IDs to string data (the
641f4a2713aSLionel Sambuc /// keys of the first table).
642f4a2713aSLionel Sambuc ///
EmitIdentifierTable()643f4a2713aSLionel Sambuc std::pair<Offset,Offset> PTHWriter::EmitIdentifierTable() {
644f4a2713aSLionel Sambuc // Build two maps:
645f4a2713aSLionel Sambuc // (1) an inverse map from persistent IDs -> (IdentifierInfo*,Offset)
646f4a2713aSLionel Sambuc // (2) a map from (IdentifierInfo*, Offset)* -> persistent IDs
647f4a2713aSLionel Sambuc
648f4a2713aSLionel Sambuc // Note that we use 'calloc', so all the bytes are 0.
649f4a2713aSLionel Sambuc PTHIdKey *IIDMap = (PTHIdKey*)calloc(idcount, sizeof(PTHIdKey));
650f4a2713aSLionel Sambuc
651f4a2713aSLionel Sambuc // Create the hashtable.
652*0a6a1f1dSLionel Sambuc llvm::OnDiskChainedHashTableGenerator<PTHIdentifierTableTrait> IIOffMap;
653f4a2713aSLionel Sambuc
654f4a2713aSLionel Sambuc // Generate mapping from persistent IDs -> IdentifierInfo*.
655f4a2713aSLionel Sambuc for (IDMap::iterator I = IM.begin(), E = IM.end(); I != E; ++I) {
656f4a2713aSLionel Sambuc // Decrement by 1 because we are using a vector for the lookup and
657f4a2713aSLionel Sambuc // 0 is reserved for NULL.
658f4a2713aSLionel Sambuc assert(I->second > 0);
659f4a2713aSLionel Sambuc assert(I->second-1 < idcount);
660f4a2713aSLionel Sambuc unsigned idx = I->second-1;
661f4a2713aSLionel Sambuc
662f4a2713aSLionel Sambuc // Store the mapping from persistent ID to IdentifierInfo*
663f4a2713aSLionel Sambuc IIDMap[idx].II = I->first;
664f4a2713aSLionel Sambuc
665f4a2713aSLionel Sambuc // Store the reverse mapping in a hashtable.
666f4a2713aSLionel Sambuc IIOffMap.insert(&IIDMap[idx], I->second);
667f4a2713aSLionel Sambuc }
668f4a2713aSLionel Sambuc
669f4a2713aSLionel Sambuc // Write out the inverse map first. This causes the PCIDKey entries to
670f4a2713aSLionel Sambuc // record PTH file offsets for the string data. This is used to write
671f4a2713aSLionel Sambuc // the second table.
672f4a2713aSLionel Sambuc Offset StringTableOffset = IIOffMap.Emit(Out);
673f4a2713aSLionel Sambuc
674f4a2713aSLionel Sambuc // Now emit the table mapping from persistent IDs to PTH file offsets.
675f4a2713aSLionel Sambuc Offset IDOff = Out.tell();
676f4a2713aSLionel Sambuc Emit32(idcount); // Emit the number of identifiers.
677f4a2713aSLionel Sambuc for (unsigned i = 0 ; i < idcount; ++i)
678f4a2713aSLionel Sambuc Emit32(IIDMap[i].FileOffset);
679f4a2713aSLionel Sambuc
680f4a2713aSLionel Sambuc // Finally, release the inverse map.
681f4a2713aSLionel Sambuc free(IIDMap);
682f4a2713aSLionel Sambuc
683f4a2713aSLionel Sambuc return std::make_pair(IDOff, StringTableOffset);
684f4a2713aSLionel Sambuc }
685