blob: d834eba39c3e01ac3866163fcc53b04dd92db027 [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"
22#include "llvm/Support/MemoryBuffer.h"
23#include "llvm/Support/raw_ostream.h"
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +000024#include "llvm/System/Path.h"
Ted Kremenekb978c662009-01-08 01:17:37 +000025#include "llvm/Support/Compiler.h"
Ted Kremenek85888962008-10-21 00:54:44 +000026
27using namespace clang;
28
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +000029typedef uint32_t Offset;
30
Ted Kremenekfb645b62008-12-11 23:36:38 +000031typedef llvm::DenseMap<const FileEntry*,std::pair<Offset,Offset> > PCHMap;
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +000032typedef llvm::DenseMap<const IdentifierInfo*,uint32_t> IDMap;
Ted Kremenek85888962008-10-21 00:54:44 +000033
Ted Kremenekb978c662009-01-08 01:17:37 +000034namespace {
35class VISIBILITY_HIDDEN PTHWriter {
36 IDMap IM;
37 llvm::raw_fd_ostream& Out;
38 Preprocessor& PP;
39 uint32_t idcount;
40 PCHMap PM;
Ted Kremenek8f174e12008-12-23 02:52:12 +000041
Ted Kremenekb978c662009-01-08 01:17:37 +000042 //// Get the persistent id for the given IdentifierInfo*.
43 uint32_t ResolveID(const IdentifierInfo* II);
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +000044
Ted Kremenekb978c662009-01-08 01:17:37 +000045 /// Emit a token to the PTH file.
46 void EmitToken(const Token& T);
47
48 void Emit8(uint32_t V) {
49 Out << (unsigned char)(V);
50 }
51
52 void Emit16(uint32_t V) {
53 Out << (unsigned char)(V);
54 Out << (unsigned char)(V >> 8);
55 assert((V >> 16) == 0);
56 }
57
58 void Emit24(uint32_t V) {
59 Out << (unsigned char)(V);
60 Out << (unsigned char)(V >> 8);
61 Out << (unsigned char)(V >> 16);
62 assert((V >> 24) == 0);
63 }
64
65 void Emit32(uint32_t V) {
66 Out << (unsigned char)(V);
67 Out << (unsigned char)(V >> 8);
68 Out << (unsigned char)(V >> 16);
69 Out << (unsigned char)(V >> 24);
70 }
71
72 void EmitBuf(const char* I, const char* E) {
73 for ( ; I != E ; ++I) Out << *I;
74 }
75
76 std::pair<Offset,Offset> EmitIdentifierTable();
77 Offset EmitFileTable();
78 std::pair<Offset,Offset> LexTokens(Lexer& L);
79
80public:
81 PTHWriter(llvm::raw_fd_ostream& out, Preprocessor& pp)
82 : Out(out), PP(pp), idcount(0) {}
83
84 void GeneratePTH();
85};
86} // end anonymous namespace
87
88uint32_t PTHWriter::ResolveID(const IdentifierInfo* II) {
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +000089 // Null IdentifierInfo's map to the persistent ID 0.
90 if (!II)
91 return 0;
92
Ted Kremenek85888962008-10-21 00:54:44 +000093 IDMap::iterator I = IM.find(II);
94
95 if (I == IM.end()) {
Ted Kremenekb978c662009-01-08 01:17:37 +000096 IM[II] = ++idcount; // Pre-increment since '0' is reserved for NULL.
97 return idcount;
Ted Kremenek85888962008-10-21 00:54:44 +000098 }
99
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000100 return I->second; // We've already added 1.
Ted Kremenek85888962008-10-21 00:54:44 +0000101}
102
Ted Kremenekb978c662009-01-08 01:17:37 +0000103void PTHWriter::EmitToken(const Token& T) {
104 uint32_t fpos = PP.getSourceManager().getFullFilePos(T.getLocation());
105 Emit8(T.getKind());
106 Emit8(T.getFlags());
107 Emit24(ResolveID(T.getIdentifierInfo()));
108 Emit32(fpos);
109 Emit16(T.getLength());
110
111#if 0
112 // For specific tokens we cache their spelling.
113 if (T.getIdentifierInfo())
114 return;
115
116 switch (T.getKind()) {
117 default:
118 break;
119 case tok::string_literal:
120 case tok::wide_string_literal:
121 case tok::angle_string_literal:
122 case tok::numeric_constant:
123 case tok::char_constant:
124 CacheSpelling(T, fpos);
125 break;
126 }
127#endif
Ted Kremenek85888962008-10-21 00:54:44 +0000128}
129
Ted Kremenekb978c662009-01-08 01:17:37 +0000130namespace {
131struct VISIBILITY_HIDDEN IDData {
Ted Kremenekfa59aad2008-11-26 23:58:26 +0000132 const IdentifierInfo* II;
133 uint32_t FileOffset;
134 const IdentifierTable::const_iterator::value_type* Str;
135};
Ted Kremenekb978c662009-01-08 01:17:37 +0000136}
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000137
Ted Kremenekb978c662009-01-08 01:17:37 +0000138std::pair<Offset,Offset> PTHWriter::EmitIdentifierTable() {
139
140 const IdentifierTable& T = PP.getIdentifierTable();
Ted Kremenekfa59aad2008-11-26 23:58:26 +0000141
142 // Build an inverse map from persistent IDs -> IdentifierInfo*.
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000143 typedef std::vector<IDData> InverseIDMap;
Ted Kremenekfa59aad2008-11-26 23:58:26 +0000144 InverseIDMap IIDMap;
Ted Kremenekb978c662009-01-08 01:17:37 +0000145 IIDMap.resize(idcount);
Ted Kremenek85888962008-10-21 00:54:44 +0000146
Ted Kremenekfa59aad2008-11-26 23:58:26 +0000147 // Generate mapping from persistent IDs -> IdentifierInfo*.
Ted Kremenekb978c662009-01-08 01:17:37 +0000148 for (IDMap::iterator I=IM.begin(), E=IM.end(); I!=E; ++I) {
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000149 // Decrement by 1 because we are using a vector for the lookup and
150 // 0 is reserved for NULL.
151 assert(I->second > 0);
152 assert(I->second-1 < IIDMap.size());
153 IIDMap[I->second-1].II = I->first;
154 }
Ted Kremenek85888962008-10-21 00:54:44 +0000155
Ted Kremenekfa59aad2008-11-26 23:58:26 +0000156 // Get the string data associated with the IdentifierInfo.
157 for (IdentifierTable::const_iterator I=T.begin(), E=T.end(); I!=E; ++I) {
Ted Kremenekb978c662009-01-08 01:17:37 +0000158 IDMap::iterator IDI = IM.find(&(I->getValue()));
Ted Kremenekfa59aad2008-11-26 23:58:26 +0000159 if (IDI == IM.end()) continue;
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000160 IIDMap[IDI->second-1].Str = &(*I);
Ted Kremenekfa59aad2008-11-26 23:58:26 +0000161 }
162
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000163 Offset DataOff = Out.tell();
Ted Kremenek6183e482008-12-03 01:16:39 +0000164
Ted Kremenekfa59aad2008-11-26 23:58:26 +0000165 for (InverseIDMap::iterator I=IIDMap.begin(), E=IIDMap.end(); I!=E; ++I) {
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000166 // Record the location for this data.
167 I->FileOffset = Out.tell();
168 // Write out the keyword.
169 unsigned len = I->Str->getKeyLength();
Ted Kremenekb978c662009-01-08 01:17:37 +0000170 Emit32(len);
Ted Kremenekfa59aad2008-11-26 23:58:26 +0000171 const char* buf = I->Str->getKeyData();
Ted Kremenekb978c662009-01-08 01:17:37 +0000172 EmitBuf(buf, buf+len);
Ted Kremenek85888962008-10-21 00:54:44 +0000173 }
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000174
Ted Kremenekfa59aad2008-11-26 23:58:26 +0000175 // Now emit the table mapping from persistent IDs to PTH file offsets.
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000176 Offset IDOff = Out.tell();
Ted Kremenekfa59aad2008-11-26 23:58:26 +0000177
Ted Kremenek6183e482008-12-03 01:16:39 +0000178 // Emit the number of identifiers.
Ted Kremenekb978c662009-01-08 01:17:37 +0000179 Emit32(idcount);
Ted Kremenek6183e482008-12-03 01:16:39 +0000180
Ted Kremenekfa59aad2008-11-26 23:58:26 +0000181 for (InverseIDMap::iterator I=IIDMap.begin(), E=IIDMap.end(); I!=E; ++I)
Ted Kremenekb978c662009-01-08 01:17:37 +0000182 Emit32(I->FileOffset);
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000183
Ted Kremenekfa59aad2008-11-26 23:58:26 +0000184 return std::make_pair(DataOff, IDOff);
Ted Kremenek85888962008-10-21 00:54:44 +0000185}
186
Ted Kremenekb978c662009-01-08 01:17:37 +0000187Offset PTHWriter::EmitFileTable() {
188 // Determine the offset where this table appears in the PTH file.
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000189 Offset off = (Offset) Out.tell();
Ted Kremenekb978c662009-01-08 01:17:37 +0000190
Ted Kremenekfa59aad2008-11-26 23:58:26 +0000191 // Output the size of the table.
Ted Kremenekb978c662009-01-08 01:17:37 +0000192 Emit32(PM.size());
Ted Kremenekfa59aad2008-11-26 23:58:26 +0000193
194 for (PCHMap::iterator I=PM.begin(), E=PM.end(); I!=E; ++I) {
Ted Kremenekfa59aad2008-11-26 23:58:26 +0000195 const FileEntry* FE = I->first;
Ted Kremenek8dffd9b2008-12-04 22:36:44 +0000196 const char* Name = FE->getName();
197 unsigned size = strlen(Name);
Ted Kremenekb978c662009-01-08 01:17:37 +0000198 Emit32(size);
199 EmitBuf(Name, Name+size);
200 Emit32(I->second.first);
201 Emit32(I->second.second);
Ted Kremenekfa59aad2008-11-26 23:58:26 +0000202 }
203
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000204 return off;
205}
206
Ted Kremenekb978c662009-01-08 01:17:37 +0000207std::pair<Offset,Offset> PTHWriter::LexTokens(Lexer& L) {
208
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000209 // Record the location within the token file.
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000210 Offset off = (Offset) Out.tell();
Ted Kremenekfb645b62008-12-11 23:36:38 +0000211
212 // Keep track of matching '#if' ... '#endif'.
213 typedef std::vector<std::pair<Offset, unsigned> > PPCondTable;
214 PPCondTable PPCond;
Ted Kremenekdad7b342008-12-12 18:31:09 +0000215 std::vector<unsigned> PPStartCond;
Ted Kremeneke5680f32008-12-23 01:30:52 +0000216 bool ParsingPreprocessorDirective = false;
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000217
218 Token Tok;
219
220 do {
221 L.LexFromRawLexer(Tok);
222
Ted Kremeneke5680f32008-12-23 01:30:52 +0000223 if ((Tok.isAtStartOfLine() || Tok.is(tok::eof)) &&
224 ParsingPreprocessorDirective) {
225 // Insert an eom token into the token cache. It has the same
226 // position as the next token that is not on the same line as the
227 // preprocessor directive. Observe that we continue processing
228 // 'Tok' when we exit this branch.
229 Token Tmp = Tok;
230 Tmp.setKind(tok::eom);
231 Tmp.clearFlag(Token::StartOfLine);
232 Tmp.setIdentifierInfo(0);
Ted Kremenekb978c662009-01-08 01:17:37 +0000233 EmitToken(Tmp);
Ted Kremeneke5680f32008-12-23 01:30:52 +0000234 ParsingPreprocessorDirective = false;
235 }
236
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000237 if (Tok.is(tok::identifier)) {
238 Tok.setIdentifierInfo(PP.LookUpIdentifierInfo(Tok));
Ted Kremeneke5680f32008-12-23 01:30:52 +0000239 continue;
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000240 }
Ted Kremeneke5680f32008-12-23 01:30:52 +0000241
242 if (Tok.is(tok::hash) && Tok.isAtStartOfLine()) {
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000243 // Special processing for #include. Store the '#' token and lex
244 // the next token.
Ted Kremeneke5680f32008-12-23 01:30:52 +0000245 assert(!ParsingPreprocessorDirective);
Ted Kremenekfb645b62008-12-11 23:36:38 +0000246 Offset HashOff = (Offset) Out.tell();
Ted Kremenekb978c662009-01-08 01:17:37 +0000247 EmitToken(Tok);
Ted Kremenekdad7b342008-12-12 18:31:09 +0000248
249 // Get the next token.
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000250 L.LexFromRawLexer(Tok);
Ted Kremeneke5680f32008-12-23 01:30:52 +0000251
252 assert(!Tok.isAtStartOfLine());
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000253
254 // Did we see 'include'/'import'/'include_next'?
255 if (!Tok.is(tok::identifier))
256 continue;
257
258 IdentifierInfo* II = PP.LookUpIdentifierInfo(Tok);
259 Tok.setIdentifierInfo(II);
260 tok::PPKeywordKind K = II->getPPKeywordID();
261
Ted Kremeneke5680f32008-12-23 01:30:52 +0000262 assert(K != tok::pp_not_keyword);
263 ParsingPreprocessorDirective = true;
264
265 switch (K) {
266 default:
267 break;
268 case tok::pp_include:
269 case tok::pp_import:
270 case tok::pp_include_next: {
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000271 // Save the 'include' token.
Ted Kremenekb978c662009-01-08 01:17:37 +0000272 EmitToken(Tok);
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000273 // Lex the next token as an include string.
274 L.setParsingPreprocessorDirective(true);
275 L.LexIncludeFilename(Tok);
276 L.setParsingPreprocessorDirective(false);
Ted Kremeneke5680f32008-12-23 01:30:52 +0000277 assert(!Tok.isAtStartOfLine());
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000278 if (Tok.is(tok::identifier))
279 Tok.setIdentifierInfo(PP.LookUpIdentifierInfo(Tok));
Ted Kremeneke5680f32008-12-23 01:30:52 +0000280
281 break;
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000282 }
Ted Kremeneke5680f32008-12-23 01:30:52 +0000283 case tok::pp_if:
284 case tok::pp_ifdef:
285 case tok::pp_ifndef: {
Ted Kremenekfb645b62008-12-11 23:36:38 +0000286 // Ad an entry for '#if' and friends. We initially set the target index
287 // to 0. This will get backpatched when we hit #endif.
288 PPStartCond.push_back(PPCond.size());
Ted Kremenekdad7b342008-12-12 18:31:09 +0000289 PPCond.push_back(std::make_pair(HashOff, 0U));
Ted Kremeneke5680f32008-12-23 01:30:52 +0000290 break;
Ted Kremenekfb645b62008-12-11 23:36:38 +0000291 }
Ted Kremeneke5680f32008-12-23 01:30:52 +0000292 case tok::pp_endif: {
Ted Kremenekfb645b62008-12-11 23:36:38 +0000293 // Add an entry for '#endif'. We set the target table index to itself.
Ted Kremenekdad7b342008-12-12 18:31:09 +0000294 // This will later be set to zero when emitting to the PTH file. We
295 // use 0 for uninitialized indices because that is easier to debug.
Ted Kremenekfb645b62008-12-11 23:36:38 +0000296 unsigned index = PPCond.size();
Ted Kremenekfb645b62008-12-11 23:36:38 +0000297 // Backpatch the opening '#if' entry.
Ted Kremenekdad7b342008-12-12 18:31:09 +0000298 assert(!PPStartCond.empty());
299 assert(PPCond.size() > PPStartCond.back());
Ted Kremenekfb645b62008-12-11 23:36:38 +0000300 assert(PPCond[PPStartCond.back()].second == 0);
301 PPCond[PPStartCond.back()].second = index;
302 PPStartCond.pop_back();
Ted Kremenekdad7b342008-12-12 18:31:09 +0000303 // Add the new entry to PPCond.
304 PPCond.push_back(std::make_pair(HashOff, index));
Ted Kremeneke5680f32008-12-23 01:30:52 +0000305 break;
Ted Kremenekfb645b62008-12-11 23:36:38 +0000306 }
Ted Kremeneke5680f32008-12-23 01:30:52 +0000307 case tok::pp_elif:
308 case tok::pp_else: {
309 // Add an entry for #elif or #else.
Ted Kremenekdad7b342008-12-12 18:31:09 +0000310 // This serves as both a closing and opening of a conditional block.
311 // This means that its entry will get backpatched later.
Ted Kremenekfb645b62008-12-11 23:36:38 +0000312 unsigned index = PPCond.size();
Ted Kremenekfb645b62008-12-11 23:36:38 +0000313 // Backpatch the previous '#if' entry.
Ted Kremenekdad7b342008-12-12 18:31:09 +0000314 assert(!PPStartCond.empty());
315 assert(PPCond.size() > PPStartCond.back());
Ted Kremenekfb645b62008-12-11 23:36:38 +0000316 assert(PPCond[PPStartCond.back()].second == 0);
317 PPCond[PPStartCond.back()].second = index;
318 PPStartCond.pop_back();
319 // Now add '#elif' as a new block opening.
Ted Kremenekdad7b342008-12-12 18:31:09 +0000320 PPCond.push_back(std::make_pair(HashOff, 0U));
321 PPStartCond.push_back(index);
Ted Kremeneke5680f32008-12-23 01:30:52 +0000322 break;
323 }
Ted Kremenekfb645b62008-12-11 23:36:38 +0000324 }
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000325 }
326 }
Ted Kremenekb978c662009-01-08 01:17:37 +0000327 while (EmitToken(Tok), Tok.isNot(tok::eof));
328
Ted Kremenekdad7b342008-12-12 18:31:09 +0000329 assert(PPStartCond.empty() && "Error: imblanced preprocessor conditionals.");
Ted Kremenekb978c662009-01-08 01:17:37 +0000330
Ted Kremenekfb645b62008-12-11 23:36:38 +0000331 // Next write out PPCond.
332 Offset PPCondOff = (Offset) Out.tell();
Ted Kremenekdad7b342008-12-12 18:31:09 +0000333
334 // Write out the size of PPCond so that clients can identifer empty tables.
Ted Kremenekb978c662009-01-08 01:17:37 +0000335 Emit32(PPCond.size());
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000336
Ted Kremenekdad7b342008-12-12 18:31:09 +0000337 for (unsigned i = 0, e = PPCond.size(); i!=e; ++i) {
Ted Kremenekb978c662009-01-08 01:17:37 +0000338 Emit32(PPCond[i].first - off);
Ted Kremenekdad7b342008-12-12 18:31:09 +0000339 uint32_t x = PPCond[i].second;
340 assert(x != 0 && "PPCond entry not backpatched.");
341 // Emit zero for #endifs. This allows us to do checking when
342 // we read the PTH file back in.
Ted Kremenekb978c662009-01-08 01:17:37 +0000343 Emit32(x == i ? 0 : x);
Ted Kremenekfb645b62008-12-11 23:36:38 +0000344 }
345
346 return std::make_pair(off,PPCondOff);
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000347}
Ted Kremenek85888962008-10-21 00:54:44 +0000348
Ted Kremenekb978c662009-01-08 01:17:37 +0000349void PTHWriter::GeneratePTH() {
Ted Kremenek85888962008-10-21 00:54:44 +0000350 // Iterate over all the files in SourceManager. Create a lexer
351 // for each file and cache the tokens.
352 SourceManager& SM = PP.getSourceManager();
353 const LangOptions& LOpts = PP.getLangOptions();
Ted Kremenek85888962008-10-21 00:54:44 +0000354
355 for (SourceManager::fileid_iterator I=SM.fileid_begin(), E=SM.fileid_end();
356 I!=E; ++I) {
357
358 const SrcMgr::ContentCache* C = I.getFileIDInfo().getContentCache();
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000359 if (!C) continue;
Ted Kremenek85888962008-10-21 00:54:44 +0000360
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000361 const FileEntry* FE = C->Entry; // Does this entry correspond to a file?
362 if (!FE) continue;
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000363
364 // FIXME: Handle files with non-absolute paths.
365 llvm::sys::Path P(FE->getName());
366 if (!P.isAbsolute())
367 continue;
Ted Kremenek85888962008-10-21 00:54:44 +0000368
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000369 PCHMap::iterator PI = PM.find(FE); // Have we already processed this file?
370 if (PI != PM.end()) continue;
371
Chris Lattner1b230142009-01-06 04:47:20 +0000372 const llvm::MemoryBuffer* B = C->getBuffer();
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000373 if (!B) continue;
374
Ted Kremenek85888962008-10-21 00:54:44 +0000375 Lexer L(SourceLocation::getFileLoc(I.getFileID(), 0), LOpts,
376 B->getBufferStart(), B->getBufferEnd(), B);
Ted Kremenekfb645b62008-12-11 23:36:38 +0000377
Ted Kremenekb978c662009-01-08 01:17:37 +0000378 PM[FE] = LexTokens(L);
Daniel Dunbar31309ab2008-11-26 02:18:33 +0000379 }
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000380
381 // Write out the identifier table.
Ted Kremenekb978c662009-01-08 01:17:37 +0000382 std::pair<Offset,Offset> IdTableOff = EmitIdentifierTable();
Ted Kremenek85888962008-10-21 00:54:44 +0000383
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000384 // Write out the file table.
Ted Kremenekb978c662009-01-08 01:17:37 +0000385 Offset FileTableOff = EmitFileTable();
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000386
387 // Finally, write out the offset table at the end.
Ted Kremenekb978c662009-01-08 01:17:37 +0000388 Emit32(IdTableOff.first);
389 Emit32(IdTableOff.second);
390 Emit32(FileTableOff);
391}
392
393void clang::CacheTokens(Preprocessor& PP, const std::string& OutFile) {
394 // Lex through the entire file. This will populate SourceManager with
395 // all of the header information.
396 Token Tok;
397 PP.EnterMainSourceFile();
398 do { PP.Lex(Tok); } while (Tok.isNot(tok::eof));
399
400 // Open up the PTH file.
401 std::string ErrMsg;
402 llvm::raw_fd_ostream Out(OutFile.c_str(), true, ErrMsg);
403
404 if (!ErrMsg.empty()) {
405 llvm::errs() << "PTH error: " << ErrMsg << "\n";
406 return;
407 }
408
409 // Create the PTHWriter and generate the PTH file.
410 PTHWriter PW(Out, PP);
411 PW.GeneratePTH();
Ted Kremenek85888962008-10-21 00:54:44 +0000412}