blob: fcb4d2f12db117bf1da46fb76ceae36ff1d75630 [file] [log] [blame]
Ted Kremenek85888962008-10-21 00:54:44 +00001//===--- CacheTokens.cpp - Caching of lexer tokens for PCH support --------===//
2//
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//
10// This provides a possible implementation of PCH support for Clang that is
11// based on caching lexed tokens and identifiers.
12//
13//===----------------------------------------------------------------------===//
14
15#include "clang.h"
16#include "clang/Basic/FileManager.h"
17#include "clang/Basic/SourceManager.h"
18#include "clang/Basic/IdentifierTable.h"
19#include "clang/Basic/Diagnostic.h"
20#include "clang/Lex/Lexer.h"
21#include "clang/Lex/Preprocessor.h"
Ted Kremenekbe295332009-01-08 02:44:06 +000022#include "llvm/ADT/StringMap.h"
Ted Kremenek85888962008-10-21 00:54:44 +000023#include "llvm/Support/MemoryBuffer.h"
24#include "llvm/Support/raw_ostream.h"
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +000025#include "llvm/System/Path.h"
Ted Kremenekb978c662009-01-08 01:17:37 +000026#include "llvm/Support/Compiler.h"
Ted Kremenek72b1b152009-01-15 18:47:46 +000027#include "llvm/Support/Streams.h"
Ted Kremenek85888962008-10-21 00:54:44 +000028
29using namespace clang;
30
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +000031typedef uint32_t Offset;
32
Ted Kremeneke338ccd2009-01-09 00:37:37 +000033typedef std::vector<std::pair<Offset, llvm::StringMapEntry<Offset>*> >
Ted Kremenekbe295332009-01-08 02:44:06 +000034 SpellMapTy;
35
36namespace {
37class VISIBILITY_HIDDEN PCHEntry {
38 Offset TokenData, PPCondData;
39 union { Offset SpellingOff; SpellMapTy* Spellings; };
40
41public:
42 PCHEntry() {}
43
44 PCHEntry(Offset td, Offset ppcd, SpellMapTy* sp)
45 : TokenData(td), PPCondData(ppcd), Spellings(sp) {}
46
47 Offset getTokenOffset() const { return TokenData; }
48 Offset getPPCondTableOffset() const { return PPCondData; }
49 SpellMapTy& getSpellings() const { return *Spellings; }
50
51 void setSpellingTableOffset(Offset off) { SpellingOff = off; }
52 Offset getSpellingTableOffset() const { return SpellingOff; }
53
54};
55} // end anonymous namespace
56
57typedef llvm::DenseMap<const FileEntry*, PCHEntry> PCHMap;
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +000058typedef llvm::DenseMap<const IdentifierInfo*,uint32_t> IDMap;
Ted Kremeneke338ccd2009-01-09 00:37:37 +000059typedef llvm::StringMap<Offset, llvm::BumpPtrAllocator> CachedStrsTy;
Ted Kremenek85888962008-10-21 00:54:44 +000060
Ted Kremenekb978c662009-01-08 01:17:37 +000061namespace {
62class VISIBILITY_HIDDEN PTHWriter {
63 IDMap IM;
64 llvm::raw_fd_ostream& Out;
65 Preprocessor& PP;
66 uint32_t idcount;
67 PCHMap PM;
Ted Kremenekbe295332009-01-08 02:44:06 +000068 CachedStrsTy CachedStrs;
69
70 SpellMapTy* CurSpellMap;
Ted Kremenek8f174e12008-12-23 02:52:12 +000071
Ted Kremenekb978c662009-01-08 01:17:37 +000072 //// Get the persistent id for the given IdentifierInfo*.
73 uint32_t ResolveID(const IdentifierInfo* II);
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +000074
Ted Kremenekb978c662009-01-08 01:17:37 +000075 /// Emit a token to the PTH file.
76 void EmitToken(const Token& T);
77
78 void Emit8(uint32_t V) {
79 Out << (unsigned char)(V);
80 }
81
82 void Emit16(uint32_t V) {
83 Out << (unsigned char)(V);
84 Out << (unsigned char)(V >> 8);
85 assert((V >> 16) == 0);
86 }
87
88 void Emit24(uint32_t V) {
89 Out << (unsigned char)(V);
90 Out << (unsigned char)(V >> 8);
91 Out << (unsigned char)(V >> 16);
92 assert((V >> 24) == 0);
93 }
94
95 void Emit32(uint32_t V) {
96 Out << (unsigned char)(V);
97 Out << (unsigned char)(V >> 8);
98 Out << (unsigned char)(V >> 16);
99 Out << (unsigned char)(V >> 24);
100 }
101
102 void EmitBuf(const char* I, const char* E) {
103 for ( ; I != E ; ++I) Out << *I;
104 }
105
Ted Kremenek293b4af2009-01-15 01:26:25 +0000106 std::pair<Offset,std::pair<Offset, Offset> > EmitIdentifierTable();
Ted Kremenekb978c662009-01-08 01:17:37 +0000107 Offset EmitFileTable();
Ted Kremenekbe295332009-01-08 02:44:06 +0000108 PCHEntry LexTokens(Lexer& L);
109 void EmitCachedSpellings();
110
Ted Kremenekb978c662009-01-08 01:17:37 +0000111public:
112 PTHWriter(llvm::raw_fd_ostream& out, Preprocessor& pp)
113 : Out(out), PP(pp), idcount(0) {}
114
115 void GeneratePTH();
116};
117} // end anonymous namespace
118
119uint32_t PTHWriter::ResolveID(const IdentifierInfo* II) {
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000120 // Null IdentifierInfo's map to the persistent ID 0.
121 if (!II)
122 return 0;
123
Ted Kremenek85888962008-10-21 00:54:44 +0000124 IDMap::iterator I = IM.find(II);
125
126 if (I == IM.end()) {
Ted Kremenekb978c662009-01-08 01:17:37 +0000127 IM[II] = ++idcount; // Pre-increment since '0' is reserved for NULL.
128 return idcount;
Ted Kremenek85888962008-10-21 00:54:44 +0000129 }
130
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000131 return I->second; // We've already added 1.
Ted Kremenek85888962008-10-21 00:54:44 +0000132}
133
Ted Kremenekb978c662009-01-08 01:17:37 +0000134void PTHWriter::EmitToken(const Token& T) {
135 uint32_t fpos = PP.getSourceManager().getFullFilePos(T.getLocation());
Ted Kremenek7b78b7c2009-01-19 23:13:15 +0000136
137 Emit32(((uint32_t) T.getKind()) |
138 (((uint32_t) T.getFlags()) << 8) |
139 (((uint32_t) T.getLength()) << 16));
140 Emit32(ResolveID(T.getIdentifierInfo()));
Ted Kremenekb978c662009-01-08 01:17:37 +0000141 Emit32(fpos);
Ted Kremenek7b78b7c2009-01-19 23:13:15 +0000142
Chris Lattner47246be2009-01-26 19:29:26 +0000143 // Literals (strings, numbers, characters) get cached spellings.
144 if (T.isLiteral()) {
145 // FIXME: This uses the slow getSpelling(). Perhaps we do better
146 // in the future? This only slows down PTH generation.
147 const std::string &spelling = PP.getSpelling(T);
148 const char* s = spelling.c_str();
149
150 // Get the string entry.
151 llvm::StringMapEntry<Offset> *E =
152 &CachedStrs.GetOrCreateValue(s, s+spelling.size());
Ted Kremenekb978c662009-01-08 01:17:37 +0000153
Chris Lattner47246be2009-01-26 19:29:26 +0000154 // Store the address of the string entry in our spelling map.
155 CurSpellMap->push_back(std::make_pair(fpos, E));
Ted Kremenekb978c662009-01-08 01:17:37 +0000156 }
Ted Kremenek85888962008-10-21 00:54:44 +0000157}
158
Ted Kremenekb978c662009-01-08 01:17:37 +0000159namespace {
160struct VISIBILITY_HIDDEN IDData {
Ted Kremenekfa59aad2008-11-26 23:58:26 +0000161 const IdentifierInfo* II;
162 uint32_t FileOffset;
Ted Kremenek293b4af2009-01-15 01:26:25 +0000163};
164
165class VISIBILITY_HIDDEN CompareIDDataIndex {
166 IDData* Table;
167public:
168 CompareIDDataIndex(IDData* table) : Table(table) {}
169
170 bool operator()(unsigned i, unsigned j) const {
Ted Kremenek72b1b152009-01-15 18:47:46 +0000171 const IdentifierInfo* II_i = Table[i].II;
172 const IdentifierInfo* II_j = Table[j].II;
173
174 unsigned i_len = II_i->getLength();
175 unsigned j_len = II_j->getLength();
176
177 if (i_len > j_len)
178 return false;
179
180 if (i_len < j_len)
181 return true;
182
183 // Otherwise, compare the strings themselves!
184 return strncmp(II_i->getName(), II_j->getName(), i_len) < 0;
Ted Kremenek293b4af2009-01-15 01:26:25 +0000185 }
Ted Kremenekfa59aad2008-11-26 23:58:26 +0000186};
Ted Kremenekb978c662009-01-08 01:17:37 +0000187}
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000188
Ted Kremenek293b4af2009-01-15 01:26:25 +0000189std::pair<Offset,std::pair<Offset,Offset> >
190PTHWriter::EmitIdentifierTable() {
191 llvm::BumpPtrAllocator Alloc;
Ted Kremenekfa59aad2008-11-26 23:58:26 +0000192
193 // Build an inverse map from persistent IDs -> IdentifierInfo*.
Ted Kremenek293b4af2009-01-15 01:26:25 +0000194 IDData* IIDMap = Alloc.Allocate<IDData>(idcount);
Ted Kremenek85888962008-10-21 00:54:44 +0000195
Ted Kremenekfa59aad2008-11-26 23:58:26 +0000196 // Generate mapping from persistent IDs -> IdentifierInfo*.
Ted Kremenekb978c662009-01-08 01:17:37 +0000197 for (IDMap::iterator I=IM.begin(), E=IM.end(); I!=E; ++I) {
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000198 // Decrement by 1 because we are using a vector for the lookup and
199 // 0 is reserved for NULL.
200 assert(I->second > 0);
Ted Kremenek293b4af2009-01-15 01:26:25 +0000201 assert(I->second-1 < idcount);
202 unsigned idx = I->second-1;
203 IIDMap[idx].II = I->first;
204 }
Ted Kremenekfa59aad2008-11-26 23:58:26 +0000205
Ted Kremenek293b4af2009-01-15 01:26:25 +0000206 // We want to write out the strings in lexical order to support binary
207 // search of strings to identifiers. Create such a table.
208 unsigned *LexicalOrder = Alloc.Allocate<unsigned>(idcount);
209 for (unsigned i = 0; i < idcount ; ++i ) LexicalOrder[i] = i;
210 std::sort(LexicalOrder, LexicalOrder+idcount, CompareIDDataIndex(IIDMap));
211
212 // Write out the lexically-sorted table of persistent ids.
213 Offset LexicalOff = Out.tell();
214 for (unsigned i = 0; i < idcount ; ++i) Emit32(LexicalOrder[i]);
215
216 // Write out the string data itself.
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000217 Offset DataOff = Out.tell();
Ted Kremenek6183e482008-12-03 01:16:39 +0000218
Ted Kremenek293b4af2009-01-15 01:26:25 +0000219 for (unsigned i = 0; i < idcount; ++i) {
220 IDData& d = IIDMap[i];
221 d.FileOffset = Out.tell(); // Record the location for this data.
222 unsigned len = d.II->getLength(); // Write out the string length.
Ted Kremenekb978c662009-01-08 01:17:37 +0000223 Emit32(len);
Ted Kremenek293b4af2009-01-15 01:26:25 +0000224 const char* buf = d.II->getName(); // Write out the string data.
Ted Kremenek72b1b152009-01-15 18:47:46 +0000225 EmitBuf(buf, buf+len);
226 // Emit a null character for those clients expecting that IdentifierInfo
227 // strings are null terminated.
228 Emit8('\0');
Ted Kremenek85888962008-10-21 00:54:44 +0000229 }
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000230
Ted Kremenekfa59aad2008-11-26 23:58:26 +0000231 // Now emit the table mapping from persistent IDs to PTH file offsets.
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000232 Offset IDOff = Out.tell();
Ted Kremenek293b4af2009-01-15 01:26:25 +0000233 Emit32(idcount); // Emit the number of identifiers.
234 for (unsigned i = 0 ; i < idcount; ++i) Emit32(IIDMap[i].FileOffset);
Ted Kremenek6183e482008-12-03 01:16:39 +0000235
Ted Kremenek293b4af2009-01-15 01:26:25 +0000236 return std::make_pair(DataOff, std::make_pair(IDOff, LexicalOff));
Ted Kremenek85888962008-10-21 00:54:44 +0000237}
238
Ted Kremenekb978c662009-01-08 01:17:37 +0000239Offset PTHWriter::EmitFileTable() {
240 // Determine the offset where this table appears in the PTH file.
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000241 Offset off = (Offset) Out.tell();
Ted Kremenekb978c662009-01-08 01:17:37 +0000242
Ted Kremenekfa59aad2008-11-26 23:58:26 +0000243 // Output the size of the table.
Ted Kremenekb978c662009-01-08 01:17:37 +0000244 Emit32(PM.size());
Ted Kremenekfa59aad2008-11-26 23:58:26 +0000245
246 for (PCHMap::iterator I=PM.begin(), E=PM.end(); I!=E; ++I) {
Ted Kremenekfa59aad2008-11-26 23:58:26 +0000247 const FileEntry* FE = I->first;
Ted Kremenek8dffd9b2008-12-04 22:36:44 +0000248 const char* Name = FE->getName();
249 unsigned size = strlen(Name);
Ted Kremenekb978c662009-01-08 01:17:37 +0000250 Emit32(size);
251 EmitBuf(Name, Name+size);
Ted Kremenekbe295332009-01-08 02:44:06 +0000252 Emit32(I->second.getTokenOffset());
253 Emit32(I->second.getPPCondTableOffset());
254 Emit32(I->second.getSpellingTableOffset());
Ted Kremenekfa59aad2008-11-26 23:58:26 +0000255 }
256
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000257 return off;
258}
259
Ted Kremenekbe295332009-01-08 02:44:06 +0000260PCHEntry PTHWriter::LexTokens(Lexer& L) {
Ted Kremenek7b78b7c2009-01-19 23:13:15 +0000261 // Pad 0's so that we emit tokens to a 4-byte alignment.
262 // This speed up reading them back in.
263 Offset off = (Offset) Out.tell();
264 for (unsigned Pad = off % 4 ; Pad != 0 ; --Pad, ++off) Emit8(0);
Ted Kremenekfb645b62008-12-11 23:36:38 +0000265
266 // Keep track of matching '#if' ... '#endif'.
267 typedef std::vector<std::pair<Offset, unsigned> > PPCondTable;
268 PPCondTable PPCond;
Ted Kremenekdad7b342008-12-12 18:31:09 +0000269 std::vector<unsigned> PPStartCond;
Ted Kremeneke5680f32008-12-23 01:30:52 +0000270 bool ParsingPreprocessorDirective = false;
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000271
Ted Kremenekbe295332009-01-08 02:44:06 +0000272 // Allocate a spelling map for this source file.
273 llvm::OwningPtr<SpellMapTy> Spellings(new SpellMapTy());
274 CurSpellMap = Spellings.get();
275
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000276 Token Tok;
277
278 do {
279 L.LexFromRawLexer(Tok);
280
Ted Kremeneke5680f32008-12-23 01:30:52 +0000281 if ((Tok.isAtStartOfLine() || Tok.is(tok::eof)) &&
282 ParsingPreprocessorDirective) {
283 // Insert an eom token into the token cache. It has the same
284 // position as the next token that is not on the same line as the
285 // preprocessor directive. Observe that we continue processing
286 // 'Tok' when we exit this branch.
287 Token Tmp = Tok;
288 Tmp.setKind(tok::eom);
289 Tmp.clearFlag(Token::StartOfLine);
290 Tmp.setIdentifierInfo(0);
Ted Kremenekb978c662009-01-08 01:17:37 +0000291 EmitToken(Tmp);
Ted Kremeneke5680f32008-12-23 01:30:52 +0000292 ParsingPreprocessorDirective = false;
293 }
294
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000295 if (Tok.is(tok::identifier)) {
296 Tok.setIdentifierInfo(PP.LookUpIdentifierInfo(Tok));
Ted Kremeneke5680f32008-12-23 01:30:52 +0000297 continue;
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000298 }
Ted Kremeneke5680f32008-12-23 01:30:52 +0000299
300 if (Tok.is(tok::hash) && Tok.isAtStartOfLine()) {
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000301 // Special processing for #include. Store the '#' token and lex
302 // the next token.
Ted Kremeneke5680f32008-12-23 01:30:52 +0000303 assert(!ParsingPreprocessorDirective);
Ted Kremenekfb645b62008-12-11 23:36:38 +0000304 Offset HashOff = (Offset) Out.tell();
Ted Kremenekb978c662009-01-08 01:17:37 +0000305 EmitToken(Tok);
Ted Kremenekdad7b342008-12-12 18:31:09 +0000306
307 // Get the next token.
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000308 L.LexFromRawLexer(Tok);
Ted Kremeneke5680f32008-12-23 01:30:52 +0000309
310 assert(!Tok.isAtStartOfLine());
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000311
312 // Did we see 'include'/'import'/'include_next'?
313 if (!Tok.is(tok::identifier))
314 continue;
315
316 IdentifierInfo* II = PP.LookUpIdentifierInfo(Tok);
317 Tok.setIdentifierInfo(II);
318 tok::PPKeywordKind K = II->getPPKeywordID();
319
Ted Kremeneke5680f32008-12-23 01:30:52 +0000320 assert(K != tok::pp_not_keyword);
321 ParsingPreprocessorDirective = true;
322
323 switch (K) {
324 default:
325 break;
326 case tok::pp_include:
327 case tok::pp_import:
328 case tok::pp_include_next: {
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000329 // Save the 'include' token.
Ted Kremenekb978c662009-01-08 01:17:37 +0000330 EmitToken(Tok);
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000331 // Lex the next token as an include string.
332 L.setParsingPreprocessorDirective(true);
333 L.LexIncludeFilename(Tok);
334 L.setParsingPreprocessorDirective(false);
Ted Kremeneke5680f32008-12-23 01:30:52 +0000335 assert(!Tok.isAtStartOfLine());
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000336 if (Tok.is(tok::identifier))
337 Tok.setIdentifierInfo(PP.LookUpIdentifierInfo(Tok));
Ted Kremeneke5680f32008-12-23 01:30:52 +0000338
339 break;
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000340 }
Ted Kremeneke5680f32008-12-23 01:30:52 +0000341 case tok::pp_if:
342 case tok::pp_ifdef:
343 case tok::pp_ifndef: {
Ted Kremenekfb645b62008-12-11 23:36:38 +0000344 // Ad an entry for '#if' and friends. We initially set the target index
345 // to 0. This will get backpatched when we hit #endif.
346 PPStartCond.push_back(PPCond.size());
Ted Kremenekdad7b342008-12-12 18:31:09 +0000347 PPCond.push_back(std::make_pair(HashOff, 0U));
Ted Kremeneke5680f32008-12-23 01:30:52 +0000348 break;
Ted Kremenekfb645b62008-12-11 23:36:38 +0000349 }
Ted Kremeneke5680f32008-12-23 01:30:52 +0000350 case tok::pp_endif: {
Ted Kremenekfb645b62008-12-11 23:36:38 +0000351 // Add an entry for '#endif'. We set the target table index to itself.
Ted Kremenekdad7b342008-12-12 18:31:09 +0000352 // This will later be set to zero when emitting to the PTH file. We
353 // use 0 for uninitialized indices because that is easier to debug.
Ted Kremenekfb645b62008-12-11 23:36:38 +0000354 unsigned index = PPCond.size();
Ted Kremenekfb645b62008-12-11 23:36:38 +0000355 // Backpatch the opening '#if' entry.
Ted Kremenekdad7b342008-12-12 18:31:09 +0000356 assert(!PPStartCond.empty());
357 assert(PPCond.size() > PPStartCond.back());
Ted Kremenekfb645b62008-12-11 23:36:38 +0000358 assert(PPCond[PPStartCond.back()].second == 0);
359 PPCond[PPStartCond.back()].second = index;
360 PPStartCond.pop_back();
Ted Kremenekdad7b342008-12-12 18:31:09 +0000361 // Add the new entry to PPCond.
362 PPCond.push_back(std::make_pair(HashOff, index));
Ted Kremeneke5680f32008-12-23 01:30:52 +0000363 break;
Ted Kremenekfb645b62008-12-11 23:36:38 +0000364 }
Ted Kremeneke5680f32008-12-23 01:30:52 +0000365 case tok::pp_elif:
366 case tok::pp_else: {
367 // Add an entry for #elif or #else.
Ted Kremenekdad7b342008-12-12 18:31:09 +0000368 // This serves as both a closing and opening of a conditional block.
369 // This means that its entry will get backpatched later.
Ted Kremenekfb645b62008-12-11 23:36:38 +0000370 unsigned index = PPCond.size();
Ted Kremenekfb645b62008-12-11 23:36:38 +0000371 // Backpatch the previous '#if' entry.
Ted Kremenekdad7b342008-12-12 18:31:09 +0000372 assert(!PPStartCond.empty());
373 assert(PPCond.size() > PPStartCond.back());
Ted Kremenekfb645b62008-12-11 23:36:38 +0000374 assert(PPCond[PPStartCond.back()].second == 0);
375 PPCond[PPStartCond.back()].second = index;
376 PPStartCond.pop_back();
377 // Now add '#elif' as a new block opening.
Ted Kremenekdad7b342008-12-12 18:31:09 +0000378 PPCond.push_back(std::make_pair(HashOff, 0U));
379 PPStartCond.push_back(index);
Ted Kremeneke5680f32008-12-23 01:30:52 +0000380 break;
381 }
Ted Kremenekfb645b62008-12-11 23:36:38 +0000382 }
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000383 }
384 }
Ted Kremenekb978c662009-01-08 01:17:37 +0000385 while (EmitToken(Tok), Tok.isNot(tok::eof));
386
Ted Kremenekdad7b342008-12-12 18:31:09 +0000387 assert(PPStartCond.empty() && "Error: imblanced preprocessor conditionals.");
Ted Kremenekb978c662009-01-08 01:17:37 +0000388
Ted Kremenekfb645b62008-12-11 23:36:38 +0000389 // Next write out PPCond.
390 Offset PPCondOff = (Offset) Out.tell();
Ted Kremenekdad7b342008-12-12 18:31:09 +0000391
392 // Write out the size of PPCond so that clients can identifer empty tables.
Ted Kremenekb978c662009-01-08 01:17:37 +0000393 Emit32(PPCond.size());
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000394
Ted Kremenekdad7b342008-12-12 18:31:09 +0000395 for (unsigned i = 0, e = PPCond.size(); i!=e; ++i) {
Ted Kremenekb978c662009-01-08 01:17:37 +0000396 Emit32(PPCond[i].first - off);
Ted Kremenekdad7b342008-12-12 18:31:09 +0000397 uint32_t x = PPCond[i].second;
398 assert(x != 0 && "PPCond entry not backpatched.");
399 // Emit zero for #endifs. This allows us to do checking when
400 // we read the PTH file back in.
Ted Kremenekb978c662009-01-08 01:17:37 +0000401 Emit32(x == i ? 0 : x);
Ted Kremenekfb645b62008-12-11 23:36:38 +0000402 }
403
Ted Kremenekbe295332009-01-08 02:44:06 +0000404 return PCHEntry(off, PPCondOff, Spellings.take());
405}
406
407void PTHWriter::EmitCachedSpellings() {
408 // Write each cached string to the PTH file and update the
409 // the string map entry to contain the relevant offset.
410 //
411 // FIXME: We can write the strings out in order of their frequency. This
412 // may result in better locality.
413 //
414 for (CachedStrsTy::iterator I = CachedStrs.begin(), E = CachedStrs.end();
415 I!=E; ++I) {
416
417 Offset off = Out.tell();
418
419 // Write out the length of the string before the string itself.
420 unsigned len = I->getKeyLength();
Ted Kremeneke338ccd2009-01-09 00:37:37 +0000421 Emit16(len);
Ted Kremenekbe295332009-01-08 02:44:06 +0000422
423 // Write out the string data.
424 const char* data = I->getKeyData();
425 EmitBuf(data, data+len);
426
Ted Kremeneke338ccd2009-01-09 00:37:37 +0000427 // Write out a single blank character.
428 Emit8(' ');
429
Ted Kremenekbe295332009-01-08 02:44:06 +0000430 // Now patch the offset of the string in the PTH file into the string map.
Ted Kremeneke338ccd2009-01-09 00:37:37 +0000431 I->setValue(off);
Ted Kremenekbe295332009-01-08 02:44:06 +0000432 }
433
434 // Now emit the spelling tables.
435 for (PCHMap::iterator I=PM.begin(), E=PM.end(); I!=E; ++I) {
436 SpellMapTy& spellings = I->second.getSpellings();
437 I->second.setSpellingTableOffset(Out.tell());
438
439 // Write out the number of spellings.
440 unsigned n = spellings.size();
441 Emit32(n);
442
443 for (unsigned i = 0; i < n; ++i) {
Ted Kremenekbe295332009-01-08 02:44:06 +0000444 // Write out the offset of the token within the source file.
445 Emit32(spellings[i].first);
446
447 // Write out the offset of the spelling data within the PTH file.
Ted Kremeneke338ccd2009-01-09 00:37:37 +0000448 Emit32(spellings[i].second->getValue());
Ted Kremenekbe295332009-01-08 02:44:06 +0000449 }
450
451 // Delete the spelling map for this source file.
452 delete &spellings;
453 }
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000454}
Ted Kremenek85888962008-10-21 00:54:44 +0000455
Ted Kremenekb978c662009-01-08 01:17:37 +0000456void PTHWriter::GeneratePTH() {
Ted Kremenek85888962008-10-21 00:54:44 +0000457 // Iterate over all the files in SourceManager. Create a lexer
458 // for each file and cache the tokens.
Chris Lattnerc6fe32a2009-01-17 03:48:08 +0000459 SourceManager &SM = PP.getSourceManager();
460 const LangOptions &LOpts = PP.getLangOptions();
Ted Kremenek85888962008-10-21 00:54:44 +0000461
Chris Lattnerc6fe32a2009-01-17 03:48:08 +0000462 for (SourceManager::fileinfo_iterator I = SM.fileinfo_begin(),
463 E = SM.fileinfo_end(); I != E; ++I) {
464 const SrcMgr::ContentCache &C = *I;
465 const FileEntry *FE = C.Entry;
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000466
467 // FIXME: Handle files with non-absolute paths.
468 llvm::sys::Path P(FE->getName());
469 if (!P.isAbsolute())
470 continue;
Ted Kremenek85888962008-10-21 00:54:44 +0000471
Chris Lattner5c263852009-01-17 03:49:48 +0000472 assert(!PM.count(FE) && "fileinfo's are not uniqued on FileEntry?");
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000473
Chris Lattnerc6fe32a2009-01-17 03:48:08 +0000474 const llvm::MemoryBuffer *B = C.getBuffer();
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000475 if (!B) continue;
Ted Kremenekfb645b62008-12-11 23:36:38 +0000476
Chris Lattner2b2453a2009-01-17 06:22:33 +0000477 FileID FID = SM.createFileID(FE, SourceLocation(), SrcMgr::C_User);
Chris Lattner025c3a62009-01-17 07:35:14 +0000478 Lexer L(FID, SM, LOpts);
Ted Kremenekb978c662009-01-08 01:17:37 +0000479 PM[FE] = LexTokens(L);
Daniel Dunbar31309ab2008-11-26 02:18:33 +0000480 }
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000481
482 // Write out the identifier table.
Ted Kremenek293b4af2009-01-15 01:26:25 +0000483 const std::pair<Offset, std::pair<Offset,Offset> >& IdTableOff
484 = EmitIdentifierTable();
Ted Kremenek85888962008-10-21 00:54:44 +0000485
Ted Kremenekbe295332009-01-08 02:44:06 +0000486 // Write out the cached strings table.
487 EmitCachedSpellings();
488
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000489 // Write out the file table.
Ted Kremenekb978c662009-01-08 01:17:37 +0000490 Offset FileTableOff = EmitFileTable();
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000491
492 // Finally, write out the offset table at the end.
Ted Kremenekb978c662009-01-08 01:17:37 +0000493 Emit32(IdTableOff.first);
Ted Kremenek293b4af2009-01-15 01:26:25 +0000494 Emit32(IdTableOff.second.first);
495 Emit32(IdTableOff.second.second);
Ted Kremenekb978c662009-01-08 01:17:37 +0000496 Emit32(FileTableOff);
497}
498
499void clang::CacheTokens(Preprocessor& PP, const std::string& OutFile) {
500 // Lex through the entire file. This will populate SourceManager with
501 // all of the header information.
502 Token Tok;
503 PP.EnterMainSourceFile();
504 do { PP.Lex(Tok); } while (Tok.isNot(tok::eof));
505
506 // Open up the PTH file.
507 std::string ErrMsg;
508 llvm::raw_fd_ostream Out(OutFile.c_str(), true, ErrMsg);
509
510 if (!ErrMsg.empty()) {
511 llvm::errs() << "PTH error: " << ErrMsg << "\n";
512 return;
513 }
514
515 // Create the PTHWriter and generate the PTH file.
516 PTHWriter PW(Out, PP);
517 PW.GeneratePTH();
Ted Kremenek85888962008-10-21 00:54:44 +0000518}