blob: 33986f8254983b81008a2bedb50c905b1d045c2f [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());
136 Emit8(T.getKind());
137 Emit8(T.getFlags());
138 Emit24(ResolveID(T.getIdentifierInfo()));
139 Emit32(fpos);
140 Emit16(T.getLength());
141
Ted Kremenekb978c662009-01-08 01:17:37 +0000142 // For specific tokens we cache their spelling.
143 if (T.getIdentifierInfo())
144 return;
145
146 switch (T.getKind()) {
Ted Kremeneke338ccd2009-01-09 00:37:37 +0000147 default:
148 break;
149 case tok::string_literal:
Ted Kremenekb978c662009-01-08 01:17:37 +0000150 case tok::wide_string_literal:
151 case tok::angle_string_literal:
Ted Kremeneke338ccd2009-01-09 00:37:37 +0000152 case tok::numeric_constant:
Ted Kremenekbe295332009-01-08 02:44:06 +0000153 case tok::char_constant: {
154 // FIXME: This uses the slow getSpelling(). Perhaps we do better
155 // in the future? This only slows down PTH generation.
Ted Kremeneke338ccd2009-01-09 00:37:37 +0000156 const std::string& spelling = PP.getSpelling(T);
Ted Kremenekbe295332009-01-08 02:44:06 +0000157 const char* s = spelling.c_str();
158
159 // Get the string entry.
Ted Kremeneke338ccd2009-01-09 00:37:37 +0000160 llvm::StringMapEntry<Offset> *E =
161 &CachedStrs.GetOrCreateValue(s, s+spelling.size());
Ted Kremenekbe295332009-01-08 02:44:06 +0000162
163 // Store the address of the string entry in our spelling map.
164 (*CurSpellMap).push_back(std::make_pair(fpos, E));
165
Ted Kremenekb978c662009-01-08 01:17:37 +0000166 break;
Ted Kremenekbe295332009-01-08 02:44:06 +0000167 }
Ted Kremenekb978c662009-01-08 01:17:37 +0000168 }
Ted Kremenek85888962008-10-21 00:54:44 +0000169}
170
Ted Kremenekb978c662009-01-08 01:17:37 +0000171namespace {
172struct VISIBILITY_HIDDEN IDData {
Ted Kremenekfa59aad2008-11-26 23:58:26 +0000173 const IdentifierInfo* II;
174 uint32_t FileOffset;
Ted Kremenek293b4af2009-01-15 01:26:25 +0000175};
176
177class VISIBILITY_HIDDEN CompareIDDataIndex {
178 IDData* Table;
179public:
180 CompareIDDataIndex(IDData* table) : Table(table) {}
181
182 bool operator()(unsigned i, unsigned j) const {
Ted Kremenek72b1b152009-01-15 18:47:46 +0000183 const IdentifierInfo* II_i = Table[i].II;
184 const IdentifierInfo* II_j = Table[j].II;
185
186 unsigned i_len = II_i->getLength();
187 unsigned j_len = II_j->getLength();
188
189 if (i_len > j_len)
190 return false;
191
192 if (i_len < j_len)
193 return true;
194
195 // Otherwise, compare the strings themselves!
196 return strncmp(II_i->getName(), II_j->getName(), i_len) < 0;
Ted Kremenek293b4af2009-01-15 01:26:25 +0000197 }
Ted Kremenekfa59aad2008-11-26 23:58:26 +0000198};
Ted Kremenekb978c662009-01-08 01:17:37 +0000199}
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000200
Ted Kremenek293b4af2009-01-15 01:26:25 +0000201std::pair<Offset,std::pair<Offset,Offset> >
202PTHWriter::EmitIdentifierTable() {
203 llvm::BumpPtrAllocator Alloc;
Ted Kremenekfa59aad2008-11-26 23:58:26 +0000204
205 // Build an inverse map from persistent IDs -> IdentifierInfo*.
Ted Kremenek293b4af2009-01-15 01:26:25 +0000206 IDData* IIDMap = Alloc.Allocate<IDData>(idcount);
Ted Kremenek85888962008-10-21 00:54:44 +0000207
Ted Kremenekfa59aad2008-11-26 23:58:26 +0000208 // Generate mapping from persistent IDs -> IdentifierInfo*.
Ted Kremenekb978c662009-01-08 01:17:37 +0000209 for (IDMap::iterator I=IM.begin(), E=IM.end(); I!=E; ++I) {
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000210 // Decrement by 1 because we are using a vector for the lookup and
211 // 0 is reserved for NULL.
212 assert(I->second > 0);
Ted Kremenek293b4af2009-01-15 01:26:25 +0000213 assert(I->second-1 < idcount);
214 unsigned idx = I->second-1;
215 IIDMap[idx].II = I->first;
216 }
Ted Kremenekfa59aad2008-11-26 23:58:26 +0000217
Ted Kremenek293b4af2009-01-15 01:26:25 +0000218 // We want to write out the strings in lexical order to support binary
219 // search of strings to identifiers. Create such a table.
220 unsigned *LexicalOrder = Alloc.Allocate<unsigned>(idcount);
221 for (unsigned i = 0; i < idcount ; ++i ) LexicalOrder[i] = i;
222 std::sort(LexicalOrder, LexicalOrder+idcount, CompareIDDataIndex(IIDMap));
223
224 // Write out the lexically-sorted table of persistent ids.
225 Offset LexicalOff = Out.tell();
226 for (unsigned i = 0; i < idcount ; ++i) Emit32(LexicalOrder[i]);
227
228 // Write out the string data itself.
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000229 Offset DataOff = Out.tell();
Ted Kremenek6183e482008-12-03 01:16:39 +0000230
Ted Kremenek293b4af2009-01-15 01:26:25 +0000231 for (unsigned i = 0; i < idcount; ++i) {
232 IDData& d = IIDMap[i];
233 d.FileOffset = Out.tell(); // Record the location for this data.
234 unsigned len = d.II->getLength(); // Write out the string length.
Ted Kremenekb978c662009-01-08 01:17:37 +0000235 Emit32(len);
Ted Kremenek293b4af2009-01-15 01:26:25 +0000236 const char* buf = d.II->getName(); // Write out the string data.
Ted Kremenek72b1b152009-01-15 18:47:46 +0000237 EmitBuf(buf, buf+len);
238 // Emit a null character for those clients expecting that IdentifierInfo
239 // strings are null terminated.
240 Emit8('\0');
Ted Kremenek85888962008-10-21 00:54:44 +0000241 }
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000242
Ted Kremenekfa59aad2008-11-26 23:58:26 +0000243 // Now emit the table mapping from persistent IDs to PTH file offsets.
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000244 Offset IDOff = Out.tell();
Ted Kremenek293b4af2009-01-15 01:26:25 +0000245 Emit32(idcount); // Emit the number of identifiers.
246 for (unsigned i = 0 ; i < idcount; ++i) Emit32(IIDMap[i].FileOffset);
Ted Kremenek6183e482008-12-03 01:16:39 +0000247
Ted Kremenek293b4af2009-01-15 01:26:25 +0000248 return std::make_pair(DataOff, std::make_pair(IDOff, LexicalOff));
Ted Kremenek85888962008-10-21 00:54:44 +0000249}
250
Ted Kremenekb978c662009-01-08 01:17:37 +0000251Offset PTHWriter::EmitFileTable() {
252 // Determine the offset where this table appears in the PTH file.
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000253 Offset off = (Offset) Out.tell();
Ted Kremenekb978c662009-01-08 01:17:37 +0000254
Ted Kremenekfa59aad2008-11-26 23:58:26 +0000255 // Output the size of the table.
Ted Kremenekb978c662009-01-08 01:17:37 +0000256 Emit32(PM.size());
Ted Kremenekfa59aad2008-11-26 23:58:26 +0000257
258 for (PCHMap::iterator I=PM.begin(), E=PM.end(); I!=E; ++I) {
Ted Kremenekfa59aad2008-11-26 23:58:26 +0000259 const FileEntry* FE = I->first;
Ted Kremenek8dffd9b2008-12-04 22:36:44 +0000260 const char* Name = FE->getName();
261 unsigned size = strlen(Name);
Ted Kremenekb978c662009-01-08 01:17:37 +0000262 Emit32(size);
263 EmitBuf(Name, Name+size);
Ted Kremenekbe295332009-01-08 02:44:06 +0000264 Emit32(I->second.getTokenOffset());
265 Emit32(I->second.getPPCondTableOffset());
266 Emit32(I->second.getSpellingTableOffset());
Ted Kremenekfa59aad2008-11-26 23:58:26 +0000267 }
268
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000269 return off;
270}
271
Ted Kremenekbe295332009-01-08 02:44:06 +0000272PCHEntry PTHWriter::LexTokens(Lexer& L) {
Ted Kremenekb978c662009-01-08 01:17:37 +0000273
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000274 // Record the location within the token file.
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000275 Offset off = (Offset) Out.tell();
Ted Kremenekfb645b62008-12-11 23:36:38 +0000276
277 // Keep track of matching '#if' ... '#endif'.
278 typedef std::vector<std::pair<Offset, unsigned> > PPCondTable;
279 PPCondTable PPCond;
Ted Kremenekdad7b342008-12-12 18:31:09 +0000280 std::vector<unsigned> PPStartCond;
Ted Kremeneke5680f32008-12-23 01:30:52 +0000281 bool ParsingPreprocessorDirective = false;
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000282
Ted Kremenekbe295332009-01-08 02:44:06 +0000283 // Allocate a spelling map for this source file.
284 llvm::OwningPtr<SpellMapTy> Spellings(new SpellMapTy());
285 CurSpellMap = Spellings.get();
286
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000287 Token Tok;
288
289 do {
290 L.LexFromRawLexer(Tok);
291
Ted Kremeneke5680f32008-12-23 01:30:52 +0000292 if ((Tok.isAtStartOfLine() || Tok.is(tok::eof)) &&
293 ParsingPreprocessorDirective) {
294 // Insert an eom token into the token cache. It has the same
295 // position as the next token that is not on the same line as the
296 // preprocessor directive. Observe that we continue processing
297 // 'Tok' when we exit this branch.
298 Token Tmp = Tok;
299 Tmp.setKind(tok::eom);
300 Tmp.clearFlag(Token::StartOfLine);
301 Tmp.setIdentifierInfo(0);
Ted Kremenekb978c662009-01-08 01:17:37 +0000302 EmitToken(Tmp);
Ted Kremeneke5680f32008-12-23 01:30:52 +0000303 ParsingPreprocessorDirective = false;
304 }
305
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000306 if (Tok.is(tok::identifier)) {
307 Tok.setIdentifierInfo(PP.LookUpIdentifierInfo(Tok));
Ted Kremeneke5680f32008-12-23 01:30:52 +0000308 continue;
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000309 }
Ted Kremeneke5680f32008-12-23 01:30:52 +0000310
311 if (Tok.is(tok::hash) && Tok.isAtStartOfLine()) {
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000312 // Special processing for #include. Store the '#' token and lex
313 // the next token.
Ted Kremeneke5680f32008-12-23 01:30:52 +0000314 assert(!ParsingPreprocessorDirective);
Ted Kremenekfb645b62008-12-11 23:36:38 +0000315 Offset HashOff = (Offset) Out.tell();
Ted Kremenekb978c662009-01-08 01:17:37 +0000316 EmitToken(Tok);
Ted Kremenekdad7b342008-12-12 18:31:09 +0000317
318 // Get the next token.
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000319 L.LexFromRawLexer(Tok);
Ted Kremeneke5680f32008-12-23 01:30:52 +0000320
321 assert(!Tok.isAtStartOfLine());
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000322
323 // Did we see 'include'/'import'/'include_next'?
324 if (!Tok.is(tok::identifier))
325 continue;
326
327 IdentifierInfo* II = PP.LookUpIdentifierInfo(Tok);
328 Tok.setIdentifierInfo(II);
329 tok::PPKeywordKind K = II->getPPKeywordID();
330
Ted Kremeneke5680f32008-12-23 01:30:52 +0000331 assert(K != tok::pp_not_keyword);
332 ParsingPreprocessorDirective = true;
333
334 switch (K) {
335 default:
336 break;
337 case tok::pp_include:
338 case tok::pp_import:
339 case tok::pp_include_next: {
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000340 // Save the 'include' token.
Ted Kremenekb978c662009-01-08 01:17:37 +0000341 EmitToken(Tok);
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000342 // Lex the next token as an include string.
343 L.setParsingPreprocessorDirective(true);
344 L.LexIncludeFilename(Tok);
345 L.setParsingPreprocessorDirective(false);
Ted Kremeneke5680f32008-12-23 01:30:52 +0000346 assert(!Tok.isAtStartOfLine());
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000347 if (Tok.is(tok::identifier))
348 Tok.setIdentifierInfo(PP.LookUpIdentifierInfo(Tok));
Ted Kremeneke5680f32008-12-23 01:30:52 +0000349
350 break;
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000351 }
Ted Kremeneke5680f32008-12-23 01:30:52 +0000352 case tok::pp_if:
353 case tok::pp_ifdef:
354 case tok::pp_ifndef: {
Ted Kremenekfb645b62008-12-11 23:36:38 +0000355 // Ad an entry for '#if' and friends. We initially set the target index
356 // to 0. This will get backpatched when we hit #endif.
357 PPStartCond.push_back(PPCond.size());
Ted Kremenekdad7b342008-12-12 18:31:09 +0000358 PPCond.push_back(std::make_pair(HashOff, 0U));
Ted Kremeneke5680f32008-12-23 01:30:52 +0000359 break;
Ted Kremenekfb645b62008-12-11 23:36:38 +0000360 }
Ted Kremeneke5680f32008-12-23 01:30:52 +0000361 case tok::pp_endif: {
Ted Kremenekfb645b62008-12-11 23:36:38 +0000362 // Add an entry for '#endif'. We set the target table index to itself.
Ted Kremenekdad7b342008-12-12 18:31:09 +0000363 // This will later be set to zero when emitting to the PTH file. We
364 // use 0 for uninitialized indices because that is easier to debug.
Ted Kremenekfb645b62008-12-11 23:36:38 +0000365 unsigned index = PPCond.size();
Ted Kremenekfb645b62008-12-11 23:36:38 +0000366 // Backpatch the opening '#if' entry.
Ted Kremenekdad7b342008-12-12 18:31:09 +0000367 assert(!PPStartCond.empty());
368 assert(PPCond.size() > PPStartCond.back());
Ted Kremenekfb645b62008-12-11 23:36:38 +0000369 assert(PPCond[PPStartCond.back()].second == 0);
370 PPCond[PPStartCond.back()].second = index;
371 PPStartCond.pop_back();
Ted Kremenekdad7b342008-12-12 18:31:09 +0000372 // Add the new entry to PPCond.
373 PPCond.push_back(std::make_pair(HashOff, index));
Ted Kremeneke5680f32008-12-23 01:30:52 +0000374 break;
Ted Kremenekfb645b62008-12-11 23:36:38 +0000375 }
Ted Kremeneke5680f32008-12-23 01:30:52 +0000376 case tok::pp_elif:
377 case tok::pp_else: {
378 // Add an entry for #elif or #else.
Ted Kremenekdad7b342008-12-12 18:31:09 +0000379 // This serves as both a closing and opening of a conditional block.
380 // This means that its entry will get backpatched later.
Ted Kremenekfb645b62008-12-11 23:36:38 +0000381 unsigned index = PPCond.size();
Ted Kremenekfb645b62008-12-11 23:36:38 +0000382 // Backpatch the previous '#if' entry.
Ted Kremenekdad7b342008-12-12 18:31:09 +0000383 assert(!PPStartCond.empty());
384 assert(PPCond.size() > PPStartCond.back());
Ted Kremenekfb645b62008-12-11 23:36:38 +0000385 assert(PPCond[PPStartCond.back()].second == 0);
386 PPCond[PPStartCond.back()].second = index;
387 PPStartCond.pop_back();
388 // Now add '#elif' as a new block opening.
Ted Kremenekdad7b342008-12-12 18:31:09 +0000389 PPCond.push_back(std::make_pair(HashOff, 0U));
390 PPStartCond.push_back(index);
Ted Kremeneke5680f32008-12-23 01:30:52 +0000391 break;
392 }
Ted Kremenekfb645b62008-12-11 23:36:38 +0000393 }
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000394 }
395 }
Ted Kremenekb978c662009-01-08 01:17:37 +0000396 while (EmitToken(Tok), Tok.isNot(tok::eof));
397
Ted Kremenekdad7b342008-12-12 18:31:09 +0000398 assert(PPStartCond.empty() && "Error: imblanced preprocessor conditionals.");
Ted Kremenekb978c662009-01-08 01:17:37 +0000399
Ted Kremenekfb645b62008-12-11 23:36:38 +0000400 // Next write out PPCond.
401 Offset PPCondOff = (Offset) Out.tell();
Ted Kremenekdad7b342008-12-12 18:31:09 +0000402
403 // Write out the size of PPCond so that clients can identifer empty tables.
Ted Kremenekb978c662009-01-08 01:17:37 +0000404 Emit32(PPCond.size());
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000405
Ted Kremenekdad7b342008-12-12 18:31:09 +0000406 for (unsigned i = 0, e = PPCond.size(); i!=e; ++i) {
Ted Kremenekb978c662009-01-08 01:17:37 +0000407 Emit32(PPCond[i].first - off);
Ted Kremenekdad7b342008-12-12 18:31:09 +0000408 uint32_t x = PPCond[i].second;
409 assert(x != 0 && "PPCond entry not backpatched.");
410 // Emit zero for #endifs. This allows us to do checking when
411 // we read the PTH file back in.
Ted Kremenekb978c662009-01-08 01:17:37 +0000412 Emit32(x == i ? 0 : x);
Ted Kremenekfb645b62008-12-11 23:36:38 +0000413 }
414
Ted Kremenekbe295332009-01-08 02:44:06 +0000415 return PCHEntry(off, PPCondOff, Spellings.take());
416}
417
418void PTHWriter::EmitCachedSpellings() {
419 // Write each cached string to the PTH file and update the
420 // the string map entry to contain the relevant offset.
421 //
422 // FIXME: We can write the strings out in order of their frequency. This
423 // may result in better locality.
424 //
425 for (CachedStrsTy::iterator I = CachedStrs.begin(), E = CachedStrs.end();
426 I!=E; ++I) {
427
428 Offset off = Out.tell();
429
430 // Write out the length of the string before the string itself.
431 unsigned len = I->getKeyLength();
Ted Kremeneke338ccd2009-01-09 00:37:37 +0000432 Emit16(len);
Ted Kremenekbe295332009-01-08 02:44:06 +0000433
434 // Write out the string data.
435 const char* data = I->getKeyData();
436 EmitBuf(data, data+len);
437
Ted Kremeneke338ccd2009-01-09 00:37:37 +0000438 // Write out a single blank character.
439 Emit8(' ');
440
Ted Kremenekbe295332009-01-08 02:44:06 +0000441 // Now patch the offset of the string in the PTH file into the string map.
Ted Kremeneke338ccd2009-01-09 00:37:37 +0000442 I->setValue(off);
Ted Kremenekbe295332009-01-08 02:44:06 +0000443 }
444
445 // Now emit the spelling tables.
446 for (PCHMap::iterator I=PM.begin(), E=PM.end(); I!=E; ++I) {
447 SpellMapTy& spellings = I->second.getSpellings();
448 I->second.setSpellingTableOffset(Out.tell());
449
450 // Write out the number of spellings.
451 unsigned n = spellings.size();
452 Emit32(n);
453
454 for (unsigned i = 0; i < n; ++i) {
Ted Kremenekbe295332009-01-08 02:44:06 +0000455 // Write out the offset of the token within the source file.
456 Emit32(spellings[i].first);
457
458 // Write out the offset of the spelling data within the PTH file.
Ted Kremeneke338ccd2009-01-09 00:37:37 +0000459 Emit32(spellings[i].second->getValue());
Ted Kremenekbe295332009-01-08 02:44:06 +0000460 }
461
462 // Delete the spelling map for this source file.
463 delete &spellings;
464 }
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000465}
Ted Kremenek85888962008-10-21 00:54:44 +0000466
Ted Kremenekb978c662009-01-08 01:17:37 +0000467void PTHWriter::GeneratePTH() {
Ted Kremenek85888962008-10-21 00:54:44 +0000468 // Iterate over all the files in SourceManager. Create a lexer
469 // for each file and cache the tokens.
Chris Lattnerc6fe32a2009-01-17 03:48:08 +0000470 SourceManager &SM = PP.getSourceManager();
471 const LangOptions &LOpts = PP.getLangOptions();
Ted Kremenek85888962008-10-21 00:54:44 +0000472
Chris Lattnerc6fe32a2009-01-17 03:48:08 +0000473 for (SourceManager::fileinfo_iterator I = SM.fileinfo_begin(),
474 E = SM.fileinfo_end(); I != E; ++I) {
475 const SrcMgr::ContentCache &C = *I;
476 const FileEntry *FE = C.Entry;
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000477
478 // FIXME: Handle files with non-absolute paths.
479 llvm::sys::Path P(FE->getName());
480 if (!P.isAbsolute())
481 continue;
Ted Kremenek85888962008-10-21 00:54:44 +0000482
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000483 PCHMap::iterator PI = PM.find(FE); // Have we already processed this file?
484 if (PI != PM.end()) continue;
485
Chris Lattnerc6fe32a2009-01-17 03:48:08 +0000486 const llvm::MemoryBuffer *B = C.getBuffer();
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000487 if (!B) continue;
Ted Kremenekfb645b62008-12-11 23:36:38 +0000488
Chris Lattnerc6fe32a2009-01-17 03:48:08 +0000489 unsigned FID = SM.createFileID(FE, SourceLocation(), SrcMgr::C_User);
490 Lexer L(SourceLocation::getFileLoc(FID, 0), LOpts,
491 B->getBufferStart(), B->getBufferEnd(), B);
Ted Kremenekb978c662009-01-08 01:17:37 +0000492 PM[FE] = LexTokens(L);
Daniel Dunbar31309ab2008-11-26 02:18:33 +0000493 }
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000494
495 // Write out the identifier table.
Ted Kremenek293b4af2009-01-15 01:26:25 +0000496 const std::pair<Offset, std::pair<Offset,Offset> >& IdTableOff
497 = EmitIdentifierTable();
Ted Kremenek85888962008-10-21 00:54:44 +0000498
Ted Kremenekbe295332009-01-08 02:44:06 +0000499 // Write out the cached strings table.
500 EmitCachedSpellings();
501
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000502 // Write out the file table.
Ted Kremenekb978c662009-01-08 01:17:37 +0000503 Offset FileTableOff = EmitFileTable();
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000504
505 // Finally, write out the offset table at the end.
Ted Kremenekb978c662009-01-08 01:17:37 +0000506 Emit32(IdTableOff.first);
Ted Kremenek293b4af2009-01-15 01:26:25 +0000507 Emit32(IdTableOff.second.first);
508 Emit32(IdTableOff.second.second);
Ted Kremenekb978c662009-01-08 01:17:37 +0000509 Emit32(FileTableOff);
510}
511
512void clang::CacheTokens(Preprocessor& PP, const std::string& OutFile) {
513 // Lex through the entire file. This will populate SourceManager with
514 // all of the header information.
515 Token Tok;
516 PP.EnterMainSourceFile();
517 do { PP.Lex(Tok); } while (Tok.isNot(tok::eof));
518
519 // Open up the PTH file.
520 std::string ErrMsg;
521 llvm::raw_fd_ostream Out(OutFile.c_str(), true, ErrMsg);
522
523 if (!ErrMsg.empty()) {
524 llvm::errs() << "PTH error: " << ErrMsg << "\n";
525 return;
526 }
527
528 // Create the PTHWriter and generate the PTH file.
529 PTHWriter PW(Out, PP);
530 PW.GeneratePTH();
Ted Kremenek85888962008-10-21 00:54:44 +0000531}