blob: 1d33105668fccf7856d77f3563e017beaaab358d [file] [log] [blame]
Ted Kremenek71c6cc62008-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 Kremenek2e395562009-01-08 02:44:06 +000022#include "llvm/ADT/StringMap.h"
Ted Kremenek71c6cc62008-10-21 00:54:44 +000023#include "llvm/Support/MemoryBuffer.h"
24#include "llvm/Support/raw_ostream.h"
Ted Kremenek62739d62008-12-02 19:44:08 +000025#include "llvm/System/Path.h"
Ted Kremenek40291882009-01-08 01:17:37 +000026#include "llvm/Support/Compiler.h"
Ted Kremenekd976c3d2009-01-15 18:47:46 +000027#include "llvm/Support/Streams.h"
Ted Kremenek71c6cc62008-10-21 00:54:44 +000028
29using namespace clang;
30
Ted Kremenek62739d62008-12-02 19:44:08 +000031typedef uint32_t Offset;
32
Ted Kremeneke98da522009-01-09 00:37:37 +000033typedef std::vector<std::pair<Offset, llvm::StringMapEntry<Offset>*> >
Ted Kremenek2e395562009-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 Kremenek62739d62008-12-02 19:44:08 +000058typedef llvm::DenseMap<const IdentifierInfo*,uint32_t> IDMap;
Ted Kremeneke98da522009-01-09 00:37:37 +000059typedef llvm::StringMap<Offset, llvm::BumpPtrAllocator> CachedStrsTy;
Ted Kremenek71c6cc62008-10-21 00:54:44 +000060
Ted Kremenek40291882009-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 Kremenek2e395562009-01-08 02:44:06 +000068 CachedStrsTy CachedStrs;
69
70 SpellMapTy* CurSpellMap;
Ted Kremenekd2c4abb2008-12-23 02:52:12 +000071
Ted Kremenek40291882009-01-08 01:17:37 +000072 //// Get the persistent id for the given IdentifierInfo*.
73 uint32_t ResolveID(const IdentifierInfo* II);
Ted Kremenek62739d62008-12-02 19:44:08 +000074
Ted Kremenek40291882009-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 Kremenekd4d6d222009-01-15 01:26:25 +0000106 std::pair<Offset,std::pair<Offset, Offset> > EmitIdentifierTable();
Ted Kremenek40291882009-01-08 01:17:37 +0000107 Offset EmitFileTable();
Ted Kremenek2e395562009-01-08 02:44:06 +0000108 PCHEntry LexTokens(Lexer& L);
109 void EmitCachedSpellings();
110
Ted Kremenek40291882009-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 Kremenek62739d62008-12-02 19:44:08 +0000120 // Null IdentifierInfo's map to the persistent ID 0.
121 if (!II)
122 return 0;
123
Ted Kremenek71c6cc62008-10-21 00:54:44 +0000124 IDMap::iterator I = IM.find(II);
125
126 if (I == IM.end()) {
Ted Kremenek40291882009-01-08 01:17:37 +0000127 IM[II] = ++idcount; // Pre-increment since '0' is reserved for NULL.
128 return idcount;
Ted Kremenek71c6cc62008-10-21 00:54:44 +0000129 }
130
Ted Kremenek62739d62008-12-02 19:44:08 +0000131 return I->second; // We've already added 1.
Ted Kremenek71c6cc62008-10-21 00:54:44 +0000132}
133
Ted Kremenek40291882009-01-08 01:17:37 +0000134void PTHWriter::EmitToken(const Token& T) {
135 uint32_t fpos = PP.getSourceManager().getFullFilePos(T.getLocation());
Ted Kremenekb8344ef2009-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 Kremenek40291882009-01-08 01:17:37 +0000141 Emit32(fpos);
Ted Kremenekb8344ef2009-01-19 23:13:15 +0000142
Ted Kremenek40291882009-01-08 01:17:37 +0000143 // For specific tokens we cache their spelling.
144 if (T.getIdentifierInfo())
145 return;
146
147 switch (T.getKind()) {
Ted Kremeneke98da522009-01-09 00:37:37 +0000148 default:
149 break;
150 case tok::string_literal:
Ted Kremenek40291882009-01-08 01:17:37 +0000151 case tok::wide_string_literal:
152 case tok::angle_string_literal:
Ted Kremeneke98da522009-01-09 00:37:37 +0000153 case tok::numeric_constant:
Ted Kremenek2e395562009-01-08 02:44:06 +0000154 case tok::char_constant: {
155 // FIXME: This uses the slow getSpelling(). Perhaps we do better
156 // in the future? This only slows down PTH generation.
Ted Kremeneke98da522009-01-09 00:37:37 +0000157 const std::string& spelling = PP.getSpelling(T);
Ted Kremenek2e395562009-01-08 02:44:06 +0000158 const char* s = spelling.c_str();
159
160 // Get the string entry.
Ted Kremeneke98da522009-01-09 00:37:37 +0000161 llvm::StringMapEntry<Offset> *E =
162 &CachedStrs.GetOrCreateValue(s, s+spelling.size());
Ted Kremenek2e395562009-01-08 02:44:06 +0000163
164 // Store the address of the string entry in our spelling map.
165 (*CurSpellMap).push_back(std::make_pair(fpos, E));
166
Ted Kremenek40291882009-01-08 01:17:37 +0000167 break;
Ted Kremenek2e395562009-01-08 02:44:06 +0000168 }
Ted Kremenek40291882009-01-08 01:17:37 +0000169 }
Ted Kremenek71c6cc62008-10-21 00:54:44 +0000170}
171
Ted Kremenek40291882009-01-08 01:17:37 +0000172namespace {
173struct VISIBILITY_HIDDEN IDData {
Ted Kremenek3e0bb5b2008-11-26 23:58:26 +0000174 const IdentifierInfo* II;
175 uint32_t FileOffset;
Ted Kremenekd4d6d222009-01-15 01:26:25 +0000176};
177
178class VISIBILITY_HIDDEN CompareIDDataIndex {
179 IDData* Table;
180public:
181 CompareIDDataIndex(IDData* table) : Table(table) {}
182
183 bool operator()(unsigned i, unsigned j) const {
Ted Kremenekd976c3d2009-01-15 18:47:46 +0000184 const IdentifierInfo* II_i = Table[i].II;
185 const IdentifierInfo* II_j = Table[j].II;
186
187 unsigned i_len = II_i->getLength();
188 unsigned j_len = II_j->getLength();
189
190 if (i_len > j_len)
191 return false;
192
193 if (i_len < j_len)
194 return true;
195
196 // Otherwise, compare the strings themselves!
197 return strncmp(II_i->getName(), II_j->getName(), i_len) < 0;
Ted Kremenekd4d6d222009-01-15 01:26:25 +0000198 }
Ted Kremenek3e0bb5b2008-11-26 23:58:26 +0000199};
Ted Kremenek40291882009-01-08 01:17:37 +0000200}
Ted Kremenekd330ec12008-11-26 03:36:26 +0000201
Ted Kremenekd4d6d222009-01-15 01:26:25 +0000202std::pair<Offset,std::pair<Offset,Offset> >
203PTHWriter::EmitIdentifierTable() {
204 llvm::BumpPtrAllocator Alloc;
Ted Kremenek3e0bb5b2008-11-26 23:58:26 +0000205
206 // Build an inverse map from persistent IDs -> IdentifierInfo*.
Ted Kremenekd4d6d222009-01-15 01:26:25 +0000207 IDData* IIDMap = Alloc.Allocate<IDData>(idcount);
Ted Kremenek71c6cc62008-10-21 00:54:44 +0000208
Ted Kremenek3e0bb5b2008-11-26 23:58:26 +0000209 // Generate mapping from persistent IDs -> IdentifierInfo*.
Ted Kremenek40291882009-01-08 01:17:37 +0000210 for (IDMap::iterator I=IM.begin(), E=IM.end(); I!=E; ++I) {
Ted Kremenek62739d62008-12-02 19:44:08 +0000211 // Decrement by 1 because we are using a vector for the lookup and
212 // 0 is reserved for NULL.
213 assert(I->second > 0);
Ted Kremenekd4d6d222009-01-15 01:26:25 +0000214 assert(I->second-1 < idcount);
215 unsigned idx = I->second-1;
216 IIDMap[idx].II = I->first;
217 }
Ted Kremenek3e0bb5b2008-11-26 23:58:26 +0000218
Ted Kremenekd4d6d222009-01-15 01:26:25 +0000219 // We want to write out the strings in lexical order to support binary
220 // search of strings to identifiers. Create such a table.
221 unsigned *LexicalOrder = Alloc.Allocate<unsigned>(idcount);
222 for (unsigned i = 0; i < idcount ; ++i ) LexicalOrder[i] = i;
223 std::sort(LexicalOrder, LexicalOrder+idcount, CompareIDDataIndex(IIDMap));
224
225 // Write out the lexically-sorted table of persistent ids.
226 Offset LexicalOff = Out.tell();
227 for (unsigned i = 0; i < idcount ; ++i) Emit32(LexicalOrder[i]);
228
229 // Write out the string data itself.
Ted Kremenek62739d62008-12-02 19:44:08 +0000230 Offset DataOff = Out.tell();
Ted Kremenekdb4c8e82008-12-03 01:16:39 +0000231
Ted Kremenekd4d6d222009-01-15 01:26:25 +0000232 for (unsigned i = 0; i < idcount; ++i) {
233 IDData& d = IIDMap[i];
234 d.FileOffset = Out.tell(); // Record the location for this data.
235 unsigned len = d.II->getLength(); // Write out the string length.
Ted Kremenek40291882009-01-08 01:17:37 +0000236 Emit32(len);
Ted Kremenekd4d6d222009-01-15 01:26:25 +0000237 const char* buf = d.II->getName(); // Write out the string data.
Ted Kremenekd976c3d2009-01-15 18:47:46 +0000238 EmitBuf(buf, buf+len);
239 // Emit a null character for those clients expecting that IdentifierInfo
240 // strings are null terminated.
241 Emit8('\0');
Ted Kremenek71c6cc62008-10-21 00:54:44 +0000242 }
Ted Kremenekd330ec12008-11-26 03:36:26 +0000243
Ted Kremenek3e0bb5b2008-11-26 23:58:26 +0000244 // Now emit the table mapping from persistent IDs to PTH file offsets.
Ted Kremenek62739d62008-12-02 19:44:08 +0000245 Offset IDOff = Out.tell();
Ted Kremenekd4d6d222009-01-15 01:26:25 +0000246 Emit32(idcount); // Emit the number of identifiers.
247 for (unsigned i = 0 ; i < idcount; ++i) Emit32(IIDMap[i].FileOffset);
Ted Kremenekdb4c8e82008-12-03 01:16:39 +0000248
Ted Kremenekd4d6d222009-01-15 01:26:25 +0000249 return std::make_pair(DataOff, std::make_pair(IDOff, LexicalOff));
Ted Kremenek71c6cc62008-10-21 00:54:44 +0000250}
251
Ted Kremenek40291882009-01-08 01:17:37 +0000252Offset PTHWriter::EmitFileTable() {
253 // Determine the offset where this table appears in the PTH file.
Ted Kremenek62739d62008-12-02 19:44:08 +0000254 Offset off = (Offset) Out.tell();
Ted Kremenek40291882009-01-08 01:17:37 +0000255
Ted Kremenek3e0bb5b2008-11-26 23:58:26 +0000256 // Output the size of the table.
Ted Kremenek40291882009-01-08 01:17:37 +0000257 Emit32(PM.size());
Ted Kremenek3e0bb5b2008-11-26 23:58:26 +0000258
259 for (PCHMap::iterator I=PM.begin(), E=PM.end(); I!=E; ++I) {
Ted Kremenek3e0bb5b2008-11-26 23:58:26 +0000260 const FileEntry* FE = I->first;
Ted Kremenek11090552008-12-04 22:36:44 +0000261 const char* Name = FE->getName();
262 unsigned size = strlen(Name);
Ted Kremenek40291882009-01-08 01:17:37 +0000263 Emit32(size);
264 EmitBuf(Name, Name+size);
Ted Kremenek2e395562009-01-08 02:44:06 +0000265 Emit32(I->second.getTokenOffset());
266 Emit32(I->second.getPPCondTableOffset());
267 Emit32(I->second.getSpellingTableOffset());
Ted Kremenek3e0bb5b2008-11-26 23:58:26 +0000268 }
269
Ted Kremenekd330ec12008-11-26 03:36:26 +0000270 return off;
271}
272
Ted Kremenek2e395562009-01-08 02:44:06 +0000273PCHEntry PTHWriter::LexTokens(Lexer& L) {
Ted Kremenekb8344ef2009-01-19 23:13:15 +0000274 // Pad 0's so that we emit tokens to a 4-byte alignment.
275 // This speed up reading them back in.
276 Offset off = (Offset) Out.tell();
277 for (unsigned Pad = off % 4 ; Pad != 0 ; --Pad, ++off) Emit8(0);
Ted Kremenek8309c922008-12-11 23:36:38 +0000278
279 // Keep track of matching '#if' ... '#endif'.
280 typedef std::vector<std::pair<Offset, unsigned> > PPCondTable;
281 PPCondTable PPCond;
Ted Kremenek0b5038d2008-12-12 18:31:09 +0000282 std::vector<unsigned> PPStartCond;
Ted Kremenek9ab79bf2008-12-23 01:30:52 +0000283 bool ParsingPreprocessorDirective = false;
Ted Kremenekd330ec12008-11-26 03:36:26 +0000284
Ted Kremenek2e395562009-01-08 02:44:06 +0000285 // Allocate a spelling map for this source file.
286 llvm::OwningPtr<SpellMapTy> Spellings(new SpellMapTy());
287 CurSpellMap = Spellings.get();
288
Ted Kremenekd330ec12008-11-26 03:36:26 +0000289 Token Tok;
290
291 do {
292 L.LexFromRawLexer(Tok);
293
Ted Kremenek9ab79bf2008-12-23 01:30:52 +0000294 if ((Tok.isAtStartOfLine() || Tok.is(tok::eof)) &&
295 ParsingPreprocessorDirective) {
296 // Insert an eom token into the token cache. It has the same
297 // position as the next token that is not on the same line as the
298 // preprocessor directive. Observe that we continue processing
299 // 'Tok' when we exit this branch.
300 Token Tmp = Tok;
301 Tmp.setKind(tok::eom);
302 Tmp.clearFlag(Token::StartOfLine);
303 Tmp.setIdentifierInfo(0);
Ted Kremenek40291882009-01-08 01:17:37 +0000304 EmitToken(Tmp);
Ted Kremenek9ab79bf2008-12-23 01:30:52 +0000305 ParsingPreprocessorDirective = false;
306 }
307
Ted Kremenekd330ec12008-11-26 03:36:26 +0000308 if (Tok.is(tok::identifier)) {
309 Tok.setIdentifierInfo(PP.LookUpIdentifierInfo(Tok));
Ted Kremenek9ab79bf2008-12-23 01:30:52 +0000310 continue;
Ted Kremenekd330ec12008-11-26 03:36:26 +0000311 }
Ted Kremenek9ab79bf2008-12-23 01:30:52 +0000312
313 if (Tok.is(tok::hash) && Tok.isAtStartOfLine()) {
Ted Kremenekd330ec12008-11-26 03:36:26 +0000314 // Special processing for #include. Store the '#' token and lex
315 // the next token.
Ted Kremenek9ab79bf2008-12-23 01:30:52 +0000316 assert(!ParsingPreprocessorDirective);
Ted Kremenek8309c922008-12-11 23:36:38 +0000317 Offset HashOff = (Offset) Out.tell();
Ted Kremenek40291882009-01-08 01:17:37 +0000318 EmitToken(Tok);
Ted Kremenek0b5038d2008-12-12 18:31:09 +0000319
320 // Get the next token.
Ted Kremenekd330ec12008-11-26 03:36:26 +0000321 L.LexFromRawLexer(Tok);
Ted Kremenek9ab79bf2008-12-23 01:30:52 +0000322
323 assert(!Tok.isAtStartOfLine());
Ted Kremenekd330ec12008-11-26 03:36:26 +0000324
325 // Did we see 'include'/'import'/'include_next'?
326 if (!Tok.is(tok::identifier))
327 continue;
328
329 IdentifierInfo* II = PP.LookUpIdentifierInfo(Tok);
330 Tok.setIdentifierInfo(II);
331 tok::PPKeywordKind K = II->getPPKeywordID();
332
Ted Kremenek9ab79bf2008-12-23 01:30:52 +0000333 assert(K != tok::pp_not_keyword);
334 ParsingPreprocessorDirective = true;
335
336 switch (K) {
337 default:
338 break;
339 case tok::pp_include:
340 case tok::pp_import:
341 case tok::pp_include_next: {
Ted Kremenekd330ec12008-11-26 03:36:26 +0000342 // Save the 'include' token.
Ted Kremenek40291882009-01-08 01:17:37 +0000343 EmitToken(Tok);
Ted Kremenekd330ec12008-11-26 03:36:26 +0000344 // Lex the next token as an include string.
345 L.setParsingPreprocessorDirective(true);
346 L.LexIncludeFilename(Tok);
347 L.setParsingPreprocessorDirective(false);
Ted Kremenek9ab79bf2008-12-23 01:30:52 +0000348 assert(!Tok.isAtStartOfLine());
Ted Kremenekd330ec12008-11-26 03:36:26 +0000349 if (Tok.is(tok::identifier))
350 Tok.setIdentifierInfo(PP.LookUpIdentifierInfo(Tok));
Ted Kremenek9ab79bf2008-12-23 01:30:52 +0000351
352 break;
Ted Kremenekd330ec12008-11-26 03:36:26 +0000353 }
Ted Kremenek9ab79bf2008-12-23 01:30:52 +0000354 case tok::pp_if:
355 case tok::pp_ifdef:
356 case tok::pp_ifndef: {
Ted Kremenek8309c922008-12-11 23:36:38 +0000357 // Ad an entry for '#if' and friends. We initially set the target index
358 // to 0. This will get backpatched when we hit #endif.
359 PPStartCond.push_back(PPCond.size());
Ted Kremenek0b5038d2008-12-12 18:31:09 +0000360 PPCond.push_back(std::make_pair(HashOff, 0U));
Ted Kremenek9ab79bf2008-12-23 01:30:52 +0000361 break;
Ted Kremenek8309c922008-12-11 23:36:38 +0000362 }
Ted Kremenek9ab79bf2008-12-23 01:30:52 +0000363 case tok::pp_endif: {
Ted Kremenek8309c922008-12-11 23:36:38 +0000364 // Add an entry for '#endif'. We set the target table index to itself.
Ted Kremenek0b5038d2008-12-12 18:31:09 +0000365 // This will later be set to zero when emitting to the PTH file. We
366 // use 0 for uninitialized indices because that is easier to debug.
Ted Kremenek8309c922008-12-11 23:36:38 +0000367 unsigned index = PPCond.size();
Ted Kremenek8309c922008-12-11 23:36:38 +0000368 // Backpatch the opening '#if' entry.
Ted Kremenek0b5038d2008-12-12 18:31:09 +0000369 assert(!PPStartCond.empty());
370 assert(PPCond.size() > PPStartCond.back());
Ted Kremenek8309c922008-12-11 23:36:38 +0000371 assert(PPCond[PPStartCond.back()].second == 0);
372 PPCond[PPStartCond.back()].second = index;
373 PPStartCond.pop_back();
Ted Kremenek0b5038d2008-12-12 18:31:09 +0000374 // Add the new entry to PPCond.
375 PPCond.push_back(std::make_pair(HashOff, index));
Ted Kremenek9ab79bf2008-12-23 01:30:52 +0000376 break;
Ted Kremenek8309c922008-12-11 23:36:38 +0000377 }
Ted Kremenek9ab79bf2008-12-23 01:30:52 +0000378 case tok::pp_elif:
379 case tok::pp_else: {
380 // Add an entry for #elif or #else.
Ted Kremenek0b5038d2008-12-12 18:31:09 +0000381 // This serves as both a closing and opening of a conditional block.
382 // This means that its entry will get backpatched later.
Ted Kremenek8309c922008-12-11 23:36:38 +0000383 unsigned index = PPCond.size();
Ted Kremenek8309c922008-12-11 23:36:38 +0000384 // Backpatch the previous '#if' entry.
Ted Kremenek0b5038d2008-12-12 18:31:09 +0000385 assert(!PPStartCond.empty());
386 assert(PPCond.size() > PPStartCond.back());
Ted Kremenek8309c922008-12-11 23:36:38 +0000387 assert(PPCond[PPStartCond.back()].second == 0);
388 PPCond[PPStartCond.back()].second = index;
389 PPStartCond.pop_back();
390 // Now add '#elif' as a new block opening.
Ted Kremenek0b5038d2008-12-12 18:31:09 +0000391 PPCond.push_back(std::make_pair(HashOff, 0U));
392 PPStartCond.push_back(index);
Ted Kremenek9ab79bf2008-12-23 01:30:52 +0000393 break;
394 }
Ted Kremenek8309c922008-12-11 23:36:38 +0000395 }
Ted Kremenekd330ec12008-11-26 03:36:26 +0000396 }
397 }
Ted Kremenek40291882009-01-08 01:17:37 +0000398 while (EmitToken(Tok), Tok.isNot(tok::eof));
399
Ted Kremenek0b5038d2008-12-12 18:31:09 +0000400 assert(PPStartCond.empty() && "Error: imblanced preprocessor conditionals.");
Ted Kremenek40291882009-01-08 01:17:37 +0000401
Ted Kremenek8309c922008-12-11 23:36:38 +0000402 // Next write out PPCond.
403 Offset PPCondOff = (Offset) Out.tell();
Ted Kremenek0b5038d2008-12-12 18:31:09 +0000404
405 // Write out the size of PPCond so that clients can identifer empty tables.
Ted Kremenek40291882009-01-08 01:17:37 +0000406 Emit32(PPCond.size());
Ted Kremenek62739d62008-12-02 19:44:08 +0000407
Ted Kremenek0b5038d2008-12-12 18:31:09 +0000408 for (unsigned i = 0, e = PPCond.size(); i!=e; ++i) {
Ted Kremenek40291882009-01-08 01:17:37 +0000409 Emit32(PPCond[i].first - off);
Ted Kremenek0b5038d2008-12-12 18:31:09 +0000410 uint32_t x = PPCond[i].second;
411 assert(x != 0 && "PPCond entry not backpatched.");
412 // Emit zero for #endifs. This allows us to do checking when
413 // we read the PTH file back in.
Ted Kremenek40291882009-01-08 01:17:37 +0000414 Emit32(x == i ? 0 : x);
Ted Kremenek8309c922008-12-11 23:36:38 +0000415 }
416
Ted Kremenek2e395562009-01-08 02:44:06 +0000417 return PCHEntry(off, PPCondOff, Spellings.take());
418}
419
420void PTHWriter::EmitCachedSpellings() {
421 // Write each cached string to the PTH file and update the
422 // the string map entry to contain the relevant offset.
423 //
424 // FIXME: We can write the strings out in order of their frequency. This
425 // may result in better locality.
426 //
427 for (CachedStrsTy::iterator I = CachedStrs.begin(), E = CachedStrs.end();
428 I!=E; ++I) {
429
430 Offset off = Out.tell();
431
432 // Write out the length of the string before the string itself.
433 unsigned len = I->getKeyLength();
Ted Kremeneke98da522009-01-09 00:37:37 +0000434 Emit16(len);
Ted Kremenek2e395562009-01-08 02:44:06 +0000435
436 // Write out the string data.
437 const char* data = I->getKeyData();
438 EmitBuf(data, data+len);
439
Ted Kremeneke98da522009-01-09 00:37:37 +0000440 // Write out a single blank character.
441 Emit8(' ');
442
Ted Kremenek2e395562009-01-08 02:44:06 +0000443 // Now patch the offset of the string in the PTH file into the string map.
Ted Kremeneke98da522009-01-09 00:37:37 +0000444 I->setValue(off);
Ted Kremenek2e395562009-01-08 02:44:06 +0000445 }
446
447 // Now emit the spelling tables.
448 for (PCHMap::iterator I=PM.begin(), E=PM.end(); I!=E; ++I) {
449 SpellMapTy& spellings = I->second.getSpellings();
450 I->second.setSpellingTableOffset(Out.tell());
451
452 // Write out the number of spellings.
453 unsigned n = spellings.size();
454 Emit32(n);
455
456 for (unsigned i = 0; i < n; ++i) {
Ted Kremenek2e395562009-01-08 02:44:06 +0000457 // Write out the offset of the token within the source file.
458 Emit32(spellings[i].first);
459
460 // Write out the offset of the spelling data within the PTH file.
Ted Kremeneke98da522009-01-09 00:37:37 +0000461 Emit32(spellings[i].second->getValue());
Ted Kremenek2e395562009-01-08 02:44:06 +0000462 }
463
464 // Delete the spelling map for this source file.
465 delete &spellings;
466 }
Ted Kremenekd330ec12008-11-26 03:36:26 +0000467}
Ted Kremenek71c6cc62008-10-21 00:54:44 +0000468
Ted Kremenek40291882009-01-08 01:17:37 +0000469void PTHWriter::GeneratePTH() {
Ted Kremenek71c6cc62008-10-21 00:54:44 +0000470 // Iterate over all the files in SourceManager. Create a lexer
471 // for each file and cache the tokens.
Chris Lattneref63fd52009-01-17 03:48:08 +0000472 SourceManager &SM = PP.getSourceManager();
473 const LangOptions &LOpts = PP.getLangOptions();
Ted Kremenek71c6cc62008-10-21 00:54:44 +0000474
Chris Lattneref63fd52009-01-17 03:48:08 +0000475 for (SourceManager::fileinfo_iterator I = SM.fileinfo_begin(),
476 E = SM.fileinfo_end(); I != E; ++I) {
477 const SrcMgr::ContentCache &C = *I;
478 const FileEntry *FE = C.Entry;
Ted Kremenek62739d62008-12-02 19:44:08 +0000479
480 // FIXME: Handle files with non-absolute paths.
481 llvm::sys::Path P(FE->getName());
482 if (!P.isAbsolute())
483 continue;
Ted Kremenek71c6cc62008-10-21 00:54:44 +0000484
Chris Lattner47d82682009-01-17 03:49:48 +0000485 assert(!PM.count(FE) && "fileinfo's are not uniqued on FileEntry?");
Ted Kremenekd330ec12008-11-26 03:36:26 +0000486
Chris Lattneref63fd52009-01-17 03:48:08 +0000487 const llvm::MemoryBuffer *B = C.getBuffer();
Ted Kremenekd330ec12008-11-26 03:36:26 +0000488 if (!B) continue;
Ted Kremenek8309c922008-12-11 23:36:38 +0000489
Chris Lattnerf4f776a2009-01-17 06:22:33 +0000490 FileID FID = SM.createFileID(FE, SourceLocation(), SrcMgr::C_User);
Chris Lattnerc7b23592009-01-17 07:35:14 +0000491 Lexer L(FID, SM, LOpts);
Ted Kremenek40291882009-01-08 01:17:37 +0000492 PM[FE] = LexTokens(L);
Daniel Dunbareee6d102008-11-26 02:18:33 +0000493 }
Ted Kremenekd330ec12008-11-26 03:36:26 +0000494
495 // Write out the identifier table.
Ted Kremenekd4d6d222009-01-15 01:26:25 +0000496 const std::pair<Offset, std::pair<Offset,Offset> >& IdTableOff
497 = EmitIdentifierTable();
Ted Kremenek71c6cc62008-10-21 00:54:44 +0000498
Ted Kremenek2e395562009-01-08 02:44:06 +0000499 // Write out the cached strings table.
500 EmitCachedSpellings();
501
Ted Kremenekd330ec12008-11-26 03:36:26 +0000502 // Write out the file table.
Ted Kremenek40291882009-01-08 01:17:37 +0000503 Offset FileTableOff = EmitFileTable();
Ted Kremenekd330ec12008-11-26 03:36:26 +0000504
505 // Finally, write out the offset table at the end.
Ted Kremenek40291882009-01-08 01:17:37 +0000506 Emit32(IdTableOff.first);
Ted Kremenekd4d6d222009-01-15 01:26:25 +0000507 Emit32(IdTableOff.second.first);
508 Emit32(IdTableOff.second.second);
Ted Kremenek40291882009-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 Kremenek71c6cc62008-10-21 00:54:44 +0000531}