blob: 8c3bf51f16d69b35380c3e2699b6a473364a6d7a [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 Kremenek85888962008-10-21 00:54:44 +000027
28using namespace clang;
29
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +000030typedef uint32_t Offset;
31
Ted Kremenekbe295332009-01-08 02:44:06 +000032typedef std::vector<std::pair<Offset, llvm::StringMapEntry<Offset>*> >
33 SpellMapTy;
34
35namespace {
36class VISIBILITY_HIDDEN PCHEntry {
37 Offset TokenData, PPCondData;
38 union { Offset SpellingOff; SpellMapTy* Spellings; };
39
40public:
41 PCHEntry() {}
42
43 PCHEntry(Offset td, Offset ppcd, SpellMapTy* sp)
44 : TokenData(td), PPCondData(ppcd), Spellings(sp) {}
45
46 Offset getTokenOffset() const { return TokenData; }
47 Offset getPPCondTableOffset() const { return PPCondData; }
48 SpellMapTy& getSpellings() const { return *Spellings; }
49
50 void setSpellingTableOffset(Offset off) { SpellingOff = off; }
51 Offset getSpellingTableOffset() const { return SpellingOff; }
52
53};
54} // end anonymous namespace
55
56typedef llvm::DenseMap<const FileEntry*, PCHEntry> PCHMap;
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +000057typedef llvm::DenseMap<const IdentifierInfo*,uint32_t> IDMap;
Ted Kremenekbe295332009-01-08 02:44:06 +000058typedef llvm::StringMap<Offset, llvm::BumpPtrAllocator> CachedStrsTy;
Ted Kremenek85888962008-10-21 00:54:44 +000059
Ted Kremenekb978c662009-01-08 01:17:37 +000060namespace {
61class VISIBILITY_HIDDEN PTHWriter {
62 IDMap IM;
63 llvm::raw_fd_ostream& Out;
64 Preprocessor& PP;
65 uint32_t idcount;
66 PCHMap PM;
Ted Kremenekbe295332009-01-08 02:44:06 +000067 CachedStrsTy CachedStrs;
68
69 SpellMapTy* CurSpellMap;
Ted Kremenek8f174e12008-12-23 02:52:12 +000070
Ted Kremenekb978c662009-01-08 01:17:37 +000071 //// Get the persistent id for the given IdentifierInfo*.
72 uint32_t ResolveID(const IdentifierInfo* II);
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +000073
Ted Kremenekb978c662009-01-08 01:17:37 +000074 /// Emit a token to the PTH file.
75 void EmitToken(const Token& T);
76
77 void Emit8(uint32_t V) {
78 Out << (unsigned char)(V);
79 }
80
81 void Emit16(uint32_t V) {
82 Out << (unsigned char)(V);
83 Out << (unsigned char)(V >> 8);
84 assert((V >> 16) == 0);
85 }
86
87 void Emit24(uint32_t V) {
88 Out << (unsigned char)(V);
89 Out << (unsigned char)(V >> 8);
90 Out << (unsigned char)(V >> 16);
91 assert((V >> 24) == 0);
92 }
93
94 void Emit32(uint32_t V) {
95 Out << (unsigned char)(V);
96 Out << (unsigned char)(V >> 8);
97 Out << (unsigned char)(V >> 16);
98 Out << (unsigned char)(V >> 24);
99 }
100
101 void EmitBuf(const char* I, const char* E) {
102 for ( ; I != E ; ++I) Out << *I;
103 }
104
105 std::pair<Offset,Offset> EmitIdentifierTable();
106 Offset EmitFileTable();
Ted Kremenekbe295332009-01-08 02:44:06 +0000107 PCHEntry LexTokens(Lexer& L);
108 void EmitCachedSpellings();
109
Ted Kremenekb978c662009-01-08 01:17:37 +0000110public:
111 PTHWriter(llvm::raw_fd_ostream& out, Preprocessor& pp)
112 : Out(out), PP(pp), idcount(0) {}
113
114 void GeneratePTH();
115};
116} // end anonymous namespace
117
118uint32_t PTHWriter::ResolveID(const IdentifierInfo* II) {
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000119 // Null IdentifierInfo's map to the persistent ID 0.
120 if (!II)
121 return 0;
122
Ted Kremenek85888962008-10-21 00:54:44 +0000123 IDMap::iterator I = IM.find(II);
124
125 if (I == IM.end()) {
Ted Kremenekb978c662009-01-08 01:17:37 +0000126 IM[II] = ++idcount; // Pre-increment since '0' is reserved for NULL.
127 return idcount;
Ted Kremenek85888962008-10-21 00:54:44 +0000128 }
129
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000130 return I->second; // We've already added 1.
Ted Kremenek85888962008-10-21 00:54:44 +0000131}
132
Ted Kremenekb978c662009-01-08 01:17:37 +0000133void PTHWriter::EmitToken(const Token& T) {
134 uint32_t fpos = PP.getSourceManager().getFullFilePos(T.getLocation());
135 Emit8(T.getKind());
136 Emit8(T.getFlags());
137 Emit24(ResolveID(T.getIdentifierInfo()));
138 Emit32(fpos);
139 Emit16(T.getLength());
140
Ted Kremenekb978c662009-01-08 01:17:37 +0000141 // For specific tokens we cache their spelling.
142 if (T.getIdentifierInfo())
143 return;
144
145 switch (T.getKind()) {
146 default:
147 break;
148 case tok::string_literal:
149 case tok::wide_string_literal:
150 case tok::angle_string_literal:
151 case tok::numeric_constant:
Ted Kremenekbe295332009-01-08 02:44:06 +0000152 case tok::char_constant: {
153 // FIXME: This uses the slow getSpelling(). Perhaps we do better
154 // in the future? This only slows down PTH generation.
155 const std::string& spelling = PP.getSpelling(T);
156 const char* s = spelling.c_str();
157
158 // Get the string entry.
159 llvm::StringMapEntry<Offset> *E =
160 &CachedStrs.GetOrCreateValue(s, s+spelling.size());
161
162 // Store the address of the string entry in our spelling map.
163 (*CurSpellMap).push_back(std::make_pair(fpos, E));
164
Ted Kremenekb978c662009-01-08 01:17:37 +0000165 break;
Ted Kremenekbe295332009-01-08 02:44:06 +0000166 }
Ted Kremenekb978c662009-01-08 01:17:37 +0000167 }
Ted Kremenek85888962008-10-21 00:54:44 +0000168}
169
Ted Kremenekb978c662009-01-08 01:17:37 +0000170namespace {
171struct VISIBILITY_HIDDEN IDData {
Ted Kremenekfa59aad2008-11-26 23:58:26 +0000172 const IdentifierInfo* II;
173 uint32_t FileOffset;
174 const IdentifierTable::const_iterator::value_type* Str;
175};
Ted Kremenekb978c662009-01-08 01:17:37 +0000176}
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000177
Ted Kremenekb978c662009-01-08 01:17:37 +0000178std::pair<Offset,Offset> PTHWriter::EmitIdentifierTable() {
179
180 const IdentifierTable& T = PP.getIdentifierTable();
Ted Kremenekfa59aad2008-11-26 23:58:26 +0000181
182 // Build an inverse map from persistent IDs -> IdentifierInfo*.
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000183 typedef std::vector<IDData> InverseIDMap;
Ted Kremenekfa59aad2008-11-26 23:58:26 +0000184 InverseIDMap IIDMap;
Ted Kremenekb978c662009-01-08 01:17:37 +0000185 IIDMap.resize(idcount);
Ted Kremenek85888962008-10-21 00:54:44 +0000186
Ted Kremenekfa59aad2008-11-26 23:58:26 +0000187 // Generate mapping from persistent IDs -> IdentifierInfo*.
Ted Kremenekb978c662009-01-08 01:17:37 +0000188 for (IDMap::iterator I=IM.begin(), E=IM.end(); I!=E; ++I) {
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000189 // Decrement by 1 because we are using a vector for the lookup and
190 // 0 is reserved for NULL.
191 assert(I->second > 0);
192 assert(I->second-1 < IIDMap.size());
193 IIDMap[I->second-1].II = I->first;
194 }
Ted Kremenek85888962008-10-21 00:54:44 +0000195
Ted Kremenekfa59aad2008-11-26 23:58:26 +0000196 // Get the string data associated with the IdentifierInfo.
197 for (IdentifierTable::const_iterator I=T.begin(), E=T.end(); I!=E; ++I) {
Ted Kremenekb978c662009-01-08 01:17:37 +0000198 IDMap::iterator IDI = IM.find(&(I->getValue()));
Ted Kremenekfa59aad2008-11-26 23:58:26 +0000199 if (IDI == IM.end()) continue;
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000200 IIDMap[IDI->second-1].Str = &(*I);
Ted Kremenekfa59aad2008-11-26 23:58:26 +0000201 }
202
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000203 Offset DataOff = Out.tell();
Ted Kremenek6183e482008-12-03 01:16:39 +0000204
Ted Kremenekfa59aad2008-11-26 23:58:26 +0000205 for (InverseIDMap::iterator I=IIDMap.begin(), E=IIDMap.end(); I!=E; ++I) {
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000206 // Record the location for this data.
207 I->FileOffset = Out.tell();
208 // Write out the keyword.
209 unsigned len = I->Str->getKeyLength();
Ted Kremenekb978c662009-01-08 01:17:37 +0000210 Emit32(len);
Ted Kremenekfa59aad2008-11-26 23:58:26 +0000211 const char* buf = I->Str->getKeyData();
Ted Kremenekb978c662009-01-08 01:17:37 +0000212 EmitBuf(buf, buf+len);
Ted Kremenek85888962008-10-21 00:54:44 +0000213 }
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000214
Ted Kremenekfa59aad2008-11-26 23:58:26 +0000215 // Now emit the table mapping from persistent IDs to PTH file offsets.
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000216 Offset IDOff = Out.tell();
Ted Kremenekfa59aad2008-11-26 23:58:26 +0000217
Ted Kremenek6183e482008-12-03 01:16:39 +0000218 // Emit the number of identifiers.
Ted Kremenekb978c662009-01-08 01:17:37 +0000219 Emit32(idcount);
Ted Kremenek6183e482008-12-03 01:16:39 +0000220
Ted Kremenekfa59aad2008-11-26 23:58:26 +0000221 for (InverseIDMap::iterator I=IIDMap.begin(), E=IIDMap.end(); I!=E; ++I)
Ted Kremenekb978c662009-01-08 01:17:37 +0000222 Emit32(I->FileOffset);
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000223
Ted Kremenekfa59aad2008-11-26 23:58:26 +0000224 return std::make_pair(DataOff, IDOff);
Ted Kremenek85888962008-10-21 00:54:44 +0000225}
226
Ted Kremenekb978c662009-01-08 01:17:37 +0000227Offset PTHWriter::EmitFileTable() {
228 // Determine the offset where this table appears in the PTH file.
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000229 Offset off = (Offset) Out.tell();
Ted Kremenekb978c662009-01-08 01:17:37 +0000230
Ted Kremenekfa59aad2008-11-26 23:58:26 +0000231 // Output the size of the table.
Ted Kremenekb978c662009-01-08 01:17:37 +0000232 Emit32(PM.size());
Ted Kremenekfa59aad2008-11-26 23:58:26 +0000233
234 for (PCHMap::iterator I=PM.begin(), E=PM.end(); I!=E; ++I) {
Ted Kremenekfa59aad2008-11-26 23:58:26 +0000235 const FileEntry* FE = I->first;
Ted Kremenek8dffd9b2008-12-04 22:36:44 +0000236 const char* Name = FE->getName();
237 unsigned size = strlen(Name);
Ted Kremenekb978c662009-01-08 01:17:37 +0000238 Emit32(size);
239 EmitBuf(Name, Name+size);
Ted Kremenekbe295332009-01-08 02:44:06 +0000240 Emit32(I->second.getTokenOffset());
241 Emit32(I->second.getPPCondTableOffset());
242 Emit32(I->second.getSpellingTableOffset());
Ted Kremenekfa59aad2008-11-26 23:58:26 +0000243 }
244
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000245 return off;
246}
247
Ted Kremenekbe295332009-01-08 02:44:06 +0000248PCHEntry PTHWriter::LexTokens(Lexer& L) {
Ted Kremenekb978c662009-01-08 01:17:37 +0000249
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000250 // Record the location within the token file.
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000251 Offset off = (Offset) Out.tell();
Ted Kremenekfb645b62008-12-11 23:36:38 +0000252
253 // Keep track of matching '#if' ... '#endif'.
254 typedef std::vector<std::pair<Offset, unsigned> > PPCondTable;
255 PPCondTable PPCond;
Ted Kremenekdad7b342008-12-12 18:31:09 +0000256 std::vector<unsigned> PPStartCond;
Ted Kremeneke5680f32008-12-23 01:30:52 +0000257 bool ParsingPreprocessorDirective = false;
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000258
Ted Kremenekbe295332009-01-08 02:44:06 +0000259 // Allocate a spelling map for this source file.
260 llvm::OwningPtr<SpellMapTy> Spellings(new SpellMapTy());
261 CurSpellMap = Spellings.get();
262
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000263 Token Tok;
264
265 do {
266 L.LexFromRawLexer(Tok);
267
Ted Kremeneke5680f32008-12-23 01:30:52 +0000268 if ((Tok.isAtStartOfLine() || Tok.is(tok::eof)) &&
269 ParsingPreprocessorDirective) {
270 // Insert an eom token into the token cache. It has the same
271 // position as the next token that is not on the same line as the
272 // preprocessor directive. Observe that we continue processing
273 // 'Tok' when we exit this branch.
274 Token Tmp = Tok;
275 Tmp.setKind(tok::eom);
276 Tmp.clearFlag(Token::StartOfLine);
277 Tmp.setIdentifierInfo(0);
Ted Kremenekb978c662009-01-08 01:17:37 +0000278 EmitToken(Tmp);
Ted Kremeneke5680f32008-12-23 01:30:52 +0000279 ParsingPreprocessorDirective = false;
280 }
281
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000282 if (Tok.is(tok::identifier)) {
283 Tok.setIdentifierInfo(PP.LookUpIdentifierInfo(Tok));
Ted Kremeneke5680f32008-12-23 01:30:52 +0000284 continue;
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000285 }
Ted Kremeneke5680f32008-12-23 01:30:52 +0000286
287 if (Tok.is(tok::hash) && Tok.isAtStartOfLine()) {
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000288 // Special processing for #include. Store the '#' token and lex
289 // the next token.
Ted Kremeneke5680f32008-12-23 01:30:52 +0000290 assert(!ParsingPreprocessorDirective);
Ted Kremenekfb645b62008-12-11 23:36:38 +0000291 Offset HashOff = (Offset) Out.tell();
Ted Kremenekb978c662009-01-08 01:17:37 +0000292 EmitToken(Tok);
Ted Kremenekdad7b342008-12-12 18:31:09 +0000293
294 // Get the next token.
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000295 L.LexFromRawLexer(Tok);
Ted Kremeneke5680f32008-12-23 01:30:52 +0000296
297 assert(!Tok.isAtStartOfLine());
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000298
299 // Did we see 'include'/'import'/'include_next'?
300 if (!Tok.is(tok::identifier))
301 continue;
302
303 IdentifierInfo* II = PP.LookUpIdentifierInfo(Tok);
304 Tok.setIdentifierInfo(II);
305 tok::PPKeywordKind K = II->getPPKeywordID();
306
Ted Kremeneke5680f32008-12-23 01:30:52 +0000307 assert(K != tok::pp_not_keyword);
308 ParsingPreprocessorDirective = true;
309
310 switch (K) {
311 default:
312 break;
313 case tok::pp_include:
314 case tok::pp_import:
315 case tok::pp_include_next: {
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000316 // Save the 'include' token.
Ted Kremenekb978c662009-01-08 01:17:37 +0000317 EmitToken(Tok);
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000318 // Lex the next token as an include string.
319 L.setParsingPreprocessorDirective(true);
320 L.LexIncludeFilename(Tok);
321 L.setParsingPreprocessorDirective(false);
Ted Kremeneke5680f32008-12-23 01:30:52 +0000322 assert(!Tok.isAtStartOfLine());
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000323 if (Tok.is(tok::identifier))
324 Tok.setIdentifierInfo(PP.LookUpIdentifierInfo(Tok));
Ted Kremeneke5680f32008-12-23 01:30:52 +0000325
326 break;
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000327 }
Ted Kremeneke5680f32008-12-23 01:30:52 +0000328 case tok::pp_if:
329 case tok::pp_ifdef:
330 case tok::pp_ifndef: {
Ted Kremenekfb645b62008-12-11 23:36:38 +0000331 // Ad an entry for '#if' and friends. We initially set the target index
332 // to 0. This will get backpatched when we hit #endif.
333 PPStartCond.push_back(PPCond.size());
Ted Kremenekdad7b342008-12-12 18:31:09 +0000334 PPCond.push_back(std::make_pair(HashOff, 0U));
Ted Kremeneke5680f32008-12-23 01:30:52 +0000335 break;
Ted Kremenekfb645b62008-12-11 23:36:38 +0000336 }
Ted Kremeneke5680f32008-12-23 01:30:52 +0000337 case tok::pp_endif: {
Ted Kremenekfb645b62008-12-11 23:36:38 +0000338 // Add an entry for '#endif'. We set the target table index to itself.
Ted Kremenekdad7b342008-12-12 18:31:09 +0000339 // This will later be set to zero when emitting to the PTH file. We
340 // use 0 for uninitialized indices because that is easier to debug.
Ted Kremenekfb645b62008-12-11 23:36:38 +0000341 unsigned index = PPCond.size();
Ted Kremenekfb645b62008-12-11 23:36:38 +0000342 // Backpatch the opening '#if' entry.
Ted Kremenekdad7b342008-12-12 18:31:09 +0000343 assert(!PPStartCond.empty());
344 assert(PPCond.size() > PPStartCond.back());
Ted Kremenekfb645b62008-12-11 23:36:38 +0000345 assert(PPCond[PPStartCond.back()].second == 0);
346 PPCond[PPStartCond.back()].second = index;
347 PPStartCond.pop_back();
Ted Kremenekdad7b342008-12-12 18:31:09 +0000348 // Add the new entry to PPCond.
349 PPCond.push_back(std::make_pair(HashOff, index));
Ted Kremeneke5680f32008-12-23 01:30:52 +0000350 break;
Ted Kremenekfb645b62008-12-11 23:36:38 +0000351 }
Ted Kremeneke5680f32008-12-23 01:30:52 +0000352 case tok::pp_elif:
353 case tok::pp_else: {
354 // Add an entry for #elif or #else.
Ted Kremenekdad7b342008-12-12 18:31:09 +0000355 // This serves as both a closing and opening of a conditional block.
356 // This means that its entry will get backpatched later.
Ted Kremenekfb645b62008-12-11 23:36:38 +0000357 unsigned index = PPCond.size();
Ted Kremenekfb645b62008-12-11 23:36:38 +0000358 // Backpatch the previous '#if' entry.
Ted Kremenekdad7b342008-12-12 18:31:09 +0000359 assert(!PPStartCond.empty());
360 assert(PPCond.size() > PPStartCond.back());
Ted Kremenekfb645b62008-12-11 23:36:38 +0000361 assert(PPCond[PPStartCond.back()].second == 0);
362 PPCond[PPStartCond.back()].second = index;
363 PPStartCond.pop_back();
364 // Now add '#elif' as a new block opening.
Ted Kremenekdad7b342008-12-12 18:31:09 +0000365 PPCond.push_back(std::make_pair(HashOff, 0U));
366 PPStartCond.push_back(index);
Ted Kremeneke5680f32008-12-23 01:30:52 +0000367 break;
368 }
Ted Kremenekfb645b62008-12-11 23:36:38 +0000369 }
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000370 }
371 }
Ted Kremenekb978c662009-01-08 01:17:37 +0000372 while (EmitToken(Tok), Tok.isNot(tok::eof));
373
Ted Kremenekdad7b342008-12-12 18:31:09 +0000374 assert(PPStartCond.empty() && "Error: imblanced preprocessor conditionals.");
Ted Kremenekb978c662009-01-08 01:17:37 +0000375
Ted Kremenekfb645b62008-12-11 23:36:38 +0000376 // Next write out PPCond.
377 Offset PPCondOff = (Offset) Out.tell();
Ted Kremenekdad7b342008-12-12 18:31:09 +0000378
379 // Write out the size of PPCond so that clients can identifer empty tables.
Ted Kremenekb978c662009-01-08 01:17:37 +0000380 Emit32(PPCond.size());
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000381
Ted Kremenekdad7b342008-12-12 18:31:09 +0000382 for (unsigned i = 0, e = PPCond.size(); i!=e; ++i) {
Ted Kremenekb978c662009-01-08 01:17:37 +0000383 Emit32(PPCond[i].first - off);
Ted Kremenekdad7b342008-12-12 18:31:09 +0000384 uint32_t x = PPCond[i].second;
385 assert(x != 0 && "PPCond entry not backpatched.");
386 // Emit zero for #endifs. This allows us to do checking when
387 // we read the PTH file back in.
Ted Kremenekb978c662009-01-08 01:17:37 +0000388 Emit32(x == i ? 0 : x);
Ted Kremenekfb645b62008-12-11 23:36:38 +0000389 }
390
Ted Kremenekbe295332009-01-08 02:44:06 +0000391 return PCHEntry(off, PPCondOff, Spellings.take());
392}
393
394void PTHWriter::EmitCachedSpellings() {
395 // Write each cached string to the PTH file and update the
396 // the string map entry to contain the relevant offset.
397 //
398 // FIXME: We can write the strings out in order of their frequency. This
399 // may result in better locality.
400 //
401 for (CachedStrsTy::iterator I = CachedStrs.begin(), E = CachedStrs.end();
402 I!=E; ++I) {
403
404 Offset off = Out.tell();
405
406 // Write out the length of the string before the string itself.
407 unsigned len = I->getKeyLength();
408 Emit16(len);
409
410 // Write out the string data.
411 const char* data = I->getKeyData();
412 EmitBuf(data, data+len);
413
414 // Now patch the offset of the string in the PTH file into the string map.
415 I->setValue(off);
416 }
417
418 // Now emit the spelling tables.
419 for (PCHMap::iterator I=PM.begin(), E=PM.end(); I!=E; ++I) {
420 SpellMapTy& spellings = I->second.getSpellings();
421 I->second.setSpellingTableOffset(Out.tell());
422
423 // Write out the number of spellings.
424 unsigned n = spellings.size();
425 Emit32(n);
426
427 for (unsigned i = 0; i < n; ++i) {
428 ++count;
429 // Write out the offset of the token within the source file.
430 Emit32(spellings[i].first);
431
432 // Write out the offset of the spelling data within the PTH file.
433 Emit32(spellings[i].second->getValue());
434 }
435
436 // Delete the spelling map for this source file.
437 delete &spellings;
438 }
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000439}
Ted Kremenek85888962008-10-21 00:54:44 +0000440
Ted Kremenekb978c662009-01-08 01:17:37 +0000441void PTHWriter::GeneratePTH() {
Ted Kremenek85888962008-10-21 00:54:44 +0000442 // Iterate over all the files in SourceManager. Create a lexer
443 // for each file and cache the tokens.
444 SourceManager& SM = PP.getSourceManager();
445 const LangOptions& LOpts = PP.getLangOptions();
Ted Kremenek85888962008-10-21 00:54:44 +0000446
447 for (SourceManager::fileid_iterator I=SM.fileid_begin(), E=SM.fileid_end();
448 I!=E; ++I) {
449
450 const SrcMgr::ContentCache* C = I.getFileIDInfo().getContentCache();
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000451 if (!C) continue;
Ted Kremenek85888962008-10-21 00:54:44 +0000452
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000453 const FileEntry* FE = C->Entry; // Does this entry correspond to a file?
454 if (!FE) continue;
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000455
456 // FIXME: Handle files with non-absolute paths.
457 llvm::sys::Path P(FE->getName());
458 if (!P.isAbsolute())
459 continue;
Ted Kremenek85888962008-10-21 00:54:44 +0000460
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000461 PCHMap::iterator PI = PM.find(FE); // Have we already processed this file?
462 if (PI != PM.end()) continue;
463
Chris Lattner1b230142009-01-06 04:47:20 +0000464 const llvm::MemoryBuffer* B = C->getBuffer();
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000465 if (!B) continue;
466
Ted Kremenek85888962008-10-21 00:54:44 +0000467 Lexer L(SourceLocation::getFileLoc(I.getFileID(), 0), LOpts,
468 B->getBufferStart(), B->getBufferEnd(), B);
Ted Kremenekfb645b62008-12-11 23:36:38 +0000469
Ted Kremenekb978c662009-01-08 01:17:37 +0000470 PM[FE] = LexTokens(L);
Daniel Dunbar31309ab2008-11-26 02:18:33 +0000471 }
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000472
473 // Write out the identifier table.
Ted Kremenekb978c662009-01-08 01:17:37 +0000474 std::pair<Offset,Offset> IdTableOff = EmitIdentifierTable();
Ted Kremenek85888962008-10-21 00:54:44 +0000475
Ted Kremenekbe295332009-01-08 02:44:06 +0000476 // Write out the cached strings table.
477 EmitCachedSpellings();
478
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000479 // Write out the file table.
Ted Kremenekb978c662009-01-08 01:17:37 +0000480 Offset FileTableOff = EmitFileTable();
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000481
482 // Finally, write out the offset table at the end.
Ted Kremenekb978c662009-01-08 01:17:37 +0000483 Emit32(IdTableOff.first);
484 Emit32(IdTableOff.second);
485 Emit32(FileTableOff);
486}
487
488void clang::CacheTokens(Preprocessor& PP, const std::string& OutFile) {
489 // Lex through the entire file. This will populate SourceManager with
490 // all of the header information.
491 Token Tok;
492 PP.EnterMainSourceFile();
493 do { PP.Lex(Tok); } while (Tok.isNot(tok::eof));
494
495 // Open up the PTH file.
496 std::string ErrMsg;
497 llvm::raw_fd_ostream Out(OutFile.c_str(), true, ErrMsg);
498
499 if (!ErrMsg.empty()) {
500 llvm::errs() << "PTH error: " << ErrMsg << "\n";
501 return;
502 }
503
504 // Create the PTHWriter and generate the PTH file.
505 PTHWriter PW(Out, PP);
506 PW.GeneratePTH();
Ted Kremenek85888962008-10-21 00:54:44 +0000507}