blob: ef7cfb18d6174dac760eb5178df16a350f28102d [file] [log] [blame]
Ted Kremenek274b2082008-11-12 21:37:15 +00001//===--- PTHLexer.cpp - Lex from a token stream ---------------------------===//
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 file implements the PTHLexer interface.
11//
12//===----------------------------------------------------------------------===//
13
Ted Kremenek0c6a77b2008-12-03 00:38:03 +000014#include "clang/Basic/TokenKinds.h"
15#include "clang/Basic/FileManager.h"
16#include "clang/Basic/IdentifierTable.h"
Ted Kremenek274b2082008-11-12 21:37:15 +000017#include "clang/Lex/PTHLexer.h"
18#include "clang/Lex/Preprocessor.h"
Ted Kremenek0c6a77b2008-12-03 00:38:03 +000019#include "clang/Lex/PTHManager.h"
20#include "clang/Lex/Token.h"
21#include "clang/Lex/Preprocessor.h"
22#include "llvm/Support/Compiler.h"
23#include "llvm/Support/MemoryBuffer.h"
24#include "llvm/ADT/StringMap.h"
25#include "llvm/ADT/OwningPtr.h"
Ted Kremenek0c6a77b2008-12-03 00:38:03 +000026
Ted Kremenek274b2082008-11-12 21:37:15 +000027using namespace clang;
28
Ted Kremenek18d9afb2008-12-23 18:41:34 +000029#define DISK_TOKEN_SIZE (1+1+3+4+2)
Ted Kremenek268ee702008-12-12 18:34:08 +000030
Ted Kremenek0c6a77b2008-12-03 00:38:03 +000031//===----------------------------------------------------------------------===//
32// Utility methods for reading from the mmap'ed PTH file.
33//===----------------------------------------------------------------------===//
34
35static inline uint8_t Read8(const char*& data) {
36 return (uint8_t) *(data++);
37}
38
39static inline uint32_t Read32(const char*& data) {
40 uint32_t V = (uint32_t) Read8(data);
41 V |= (((uint32_t) Read8(data)) << 8);
42 V |= (((uint32_t) Read8(data)) << 16);
43 V |= (((uint32_t) Read8(data)) << 24);
44 return V;
45}
46
Ted Kremeneke5680f32008-12-23 01:30:52 +000047//===----------------------------------------------------------------------===//
48// PTHLexer methods.
49//===----------------------------------------------------------------------===//
50
51PTHLexer::PTHLexer(Preprocessor& pp, SourceLocation fileloc, const char* D,
52 const char* ppcond, PTHManager& PM)
53 : PreprocessorLexer(&pp, fileloc), TokBuf(D), CurPtr(D), LastHashTokPtr(0),
54 PPCond(ppcond), CurPPCondPtr(ppcond), PTHMgr(PM) {}
55
56void PTHLexer::Lex(Token& Tok) {
57LexNextToken:
Ted Kremeneke5680f32008-12-23 01:30:52 +000058
Ted Kremenek866bdf72008-12-23 02:30:15 +000059 //===--------------------------------------==//
60 // Read the raw token data.
61 //===--------------------------------------==//
62
63 // Shadow CurPtr into an automatic variable.
64 const unsigned char *CurPtrShadow = (const unsigned char*) CurPtr;
65
66 // Read in the data for the token. 14 bytes in total.
67 tok::TokenKind k = (tok::TokenKind) CurPtrShadow[0];
68 Token::TokenFlags flags = (Token::TokenFlags) CurPtrShadow[1];
69
Ted Kremenek6b1c9702008-12-23 18:27:26 +000070 uint32_t perID = ((uint32_t) CurPtrShadow[2])
Ted Kremenek866bdf72008-12-23 02:30:15 +000071 | (((uint32_t) CurPtrShadow[3]) << 8)
Ted Kremenek18d9afb2008-12-23 18:41:34 +000072 | (((uint32_t) CurPtrShadow[4]) << 16);
Ted Kremenek866bdf72008-12-23 02:30:15 +000073
Ted Kremenek18d9afb2008-12-23 18:41:34 +000074 uint32_t FileOffset = ((uint32_t) CurPtrShadow[5])
75 | (((uint32_t) CurPtrShadow[6]) << 8)
76 | (((uint32_t) CurPtrShadow[7]) << 16)
77 | (((uint32_t) CurPtrShadow[8]) << 24);
Ted Kremenek866bdf72008-12-23 02:30:15 +000078
Ted Kremenek18d9afb2008-12-23 18:41:34 +000079 uint32_t Len = ((uint32_t) CurPtrShadow[9])
80 | (((uint32_t) CurPtrShadow[10]) << 8);
Ted Kremenek866bdf72008-12-23 02:30:15 +000081
82 CurPtr = (const char*) (CurPtrShadow + DISK_TOKEN_SIZE);
83
84 //===--------------------------------------==//
85 // Construct the token itself.
86 //===--------------------------------------==//
87
88 Tok.startToken();
89 Tok.setKind(k);
Ted Kremenek59d08cb2008-12-23 19:24:24 +000090 Tok.setFlag(flags);
91 assert(!LexingRawMode);
Ted Kremenek6b1c9702008-12-23 18:27:26 +000092 Tok.setIdentifierInfo(perID ? PTHMgr.GetIdentifierInfo(perID-1) : 0);
Ted Kremenek866bdf72008-12-23 02:30:15 +000093 Tok.setLocation(SourceLocation::getFileLoc(FileID, FileOffset));
94 Tok.setLength(Len);
95
96 //===--------------------------------------==//
97 // Process the token.
98 //===--------------------------------------==//
Ted Kremeneke5680f32008-12-23 01:30:52 +000099
Ted Kremenek59d08cb2008-12-23 19:24:24 +0000100 if (k == tok::identifier) {
101 MIOpt.ReadToken();
102 return PP->HandleIdentifier(Tok);
103 }
104
105 if (k == tok::eof) {
Ted Kremeneke5680f32008-12-23 01:30:52 +0000106 // Save the end-of-file token.
107 EofToken = Tok;
108
109 Preprocessor *PPCache = PP;
Ted Kremenek59d08cb2008-12-23 19:24:24 +0000110
111 assert(!ParsingPreprocessorDirective);
112 assert(!LexingRawMode);
113
114 // FIXME: Issue diagnostics similar to Lexer.
115 if (PP->HandleEndOfFile(Tok, false))
Ted Kremeneke5680f32008-12-23 01:30:52 +0000116 return;
Ted Kremenek59d08cb2008-12-23 19:24:24 +0000117
Ted Kremeneke5680f32008-12-23 01:30:52 +0000118 assert(PPCache && "Raw buffer::LexEndOfFile should return a token");
119 return PPCache->Lex(Tok);
120 }
Ted Kremeneke5680f32008-12-23 01:30:52 +0000121
Ted Kremenek59d08cb2008-12-23 19:24:24 +0000122 if (k == tok::hash && Tok.isAtStartOfLine()) {
123 LastHashTokPtr = CurPtr - DISK_TOKEN_SIZE;
124 assert(!LexingRawMode);
125 PP->HandleDirective(Tok);
126
127 if (PP->isCurrentLexer(this))
128 goto LexNextToken;
129
130 return PP->Lex(Tok);
131 }
132
133 if (k == tok::eom) {
134 assert(ParsingPreprocessorDirective);
Ted Kremeneke5680f32008-12-23 01:30:52 +0000135 ParsingPreprocessorDirective = false;
136 return;
137 }
Ted Kremeneke5680f32008-12-23 01:30:52 +0000138
Ted Kremenek59d08cb2008-12-23 19:24:24 +0000139 MIOpt.ReadToken();
Ted Kremeneke5680f32008-12-23 01:30:52 +0000140}
141
142// FIXME: We can just grab the last token instead of storing a copy
143// into EofToken.
Ted Kremenek59d08cb2008-12-23 19:24:24 +0000144void PTHLexer::getEOF(Token& Tok) {
Ted Kremeneke5680f32008-12-23 01:30:52 +0000145 assert(!EofToken.is(tok::eof));
146 Tok = EofToken;
147}
148
149void PTHLexer::DiscardToEndOfLine() {
150 assert(ParsingPreprocessorDirective && ParsingFilename == false &&
151 "Must be in a preprocessing directive!");
152
153 // We assume that if the preprocessor wishes to discard to the end of
154 // the line that it also means to end the current preprocessor directive.
155 ParsingPreprocessorDirective = false;
156
157 // Skip tokens by only peeking at their token kind and the flags.
158 // We don't need to actually reconstruct full tokens from the token buffer.
159 // This saves some copies and it also reduces IdentifierInfo* lookup.
160 const char* p = CurPtr;
161 while (1) {
162 // Read the token kind. Are we at the end of the file?
163 tok::TokenKind x = (tok::TokenKind) (uint8_t) *p;
164 if (x == tok::eof) break;
165
166 // Read the token flags. Are we at the start of the next line?
167 Token::TokenFlags y = (Token::TokenFlags) (uint8_t) p[1];
168 if (y & Token::StartOfLine) break;
169
170 // Skip to the next token.
171 p += DISK_TOKEN_SIZE;
172 }
173
174 CurPtr = p;
175}
176
Ted Kremenek268ee702008-12-12 18:34:08 +0000177/// SkipBlock - Used by Preprocessor to skip the current conditional block.
178bool PTHLexer::SkipBlock() {
179 assert(CurPPCondPtr && "No cached PP conditional information.");
180 assert(LastHashTokPtr && "No known '#' token.");
181
Ted Kremenek41a26602008-12-12 22:05:38 +0000182 const char* HashEntryI = 0;
Ted Kremenek268ee702008-12-12 18:34:08 +0000183 uint32_t Offset;
184 uint32_t TableIdx;
185
186 do {
Ted Kremenek41a26602008-12-12 22:05:38 +0000187 // Read the token offset from the side-table.
Ted Kremenek268ee702008-12-12 18:34:08 +0000188 Offset = Read32(CurPPCondPtr);
Ted Kremenek41a26602008-12-12 22:05:38 +0000189
190 // Read the target table index from the side-table.
Ted Kremenek268ee702008-12-12 18:34:08 +0000191 TableIdx = Read32(CurPPCondPtr);
Ted Kremenek41a26602008-12-12 22:05:38 +0000192
193 // Compute the actual memory address of the '#' token data for this entry.
194 HashEntryI = TokBuf + Offset;
195
196 // Optmization: "Sibling jumping". #if...#else...#endif blocks can
197 // contain nested blocks. In the side-table we can jump over these
198 // nested blocks instead of doing a linear search if the next "sibling"
199 // entry is not at a location greater than LastHashTokPtr.
200 if (HashEntryI < LastHashTokPtr && TableIdx) {
201 // In the side-table we are still at an entry for a '#' token that
202 // is earlier than the last one we saw. Check if the location we would
203 // stride gets us closer.
204 const char* NextPPCondPtr = PPCond + TableIdx*(sizeof(uint32_t)*2);
205 assert(NextPPCondPtr >= CurPPCondPtr);
206 // Read where we should jump to.
207 uint32_t TmpOffset = Read32(NextPPCondPtr);
208 const char* HashEntryJ = TokBuf + TmpOffset;
209
210 if (HashEntryJ <= LastHashTokPtr) {
211 // Jump directly to the next entry in the side table.
212 HashEntryI = HashEntryJ;
213 Offset = TmpOffset;
214 TableIdx = Read32(NextPPCondPtr);
215 CurPPCondPtr = NextPPCondPtr;
216 }
217 }
Ted Kremenek268ee702008-12-12 18:34:08 +0000218 }
Ted Kremenek41a26602008-12-12 22:05:38 +0000219 while (HashEntryI < LastHashTokPtr);
220 assert(HashEntryI == LastHashTokPtr && "No PP-cond entry found for '#'");
Ted Kremenek268ee702008-12-12 18:34:08 +0000221 assert(TableIdx && "No jumping from #endifs.");
222
223 // Update our side-table iterator.
224 const char* NextPPCondPtr = PPCond + TableIdx*(sizeof(uint32_t)*2);
225 assert(NextPPCondPtr >= CurPPCondPtr);
226 CurPPCondPtr = NextPPCondPtr;
227
228 // Read where we should jump to.
Ted Kremenek41a26602008-12-12 22:05:38 +0000229 HashEntryI = TokBuf + Read32(NextPPCondPtr);
Ted Kremenek268ee702008-12-12 18:34:08 +0000230 uint32_t NextIdx = Read32(NextPPCondPtr);
231
232 // By construction NextIdx will be zero if this is a #endif. This is useful
233 // to know to obviate lexing another token.
234 bool isEndif = NextIdx == 0;
Ted Kremenek268ee702008-12-12 18:34:08 +0000235
236 // This case can occur when we see something like this:
237 //
238 // #if ...
239 // /* a comment or nothing */
240 // #elif
241 //
242 // If we are skipping the first #if block it will be the case that CurPtr
243 // already points 'elif'. Just return.
244
Ted Kremenek41a26602008-12-12 22:05:38 +0000245 if (CurPtr > HashEntryI) {
246 assert(CurPtr == HashEntryI + DISK_TOKEN_SIZE);
Ted Kremenek268ee702008-12-12 18:34:08 +0000247 // Did we reach a #endif? If so, go ahead and consume that token as well.
248 if (isEndif)
Ted Kremeneke5680f32008-12-23 01:30:52 +0000249 CurPtr += DISK_TOKEN_SIZE*2;
Ted Kremenek268ee702008-12-12 18:34:08 +0000250 else
Ted Kremenek41a26602008-12-12 22:05:38 +0000251 LastHashTokPtr = HashEntryI;
Ted Kremenek268ee702008-12-12 18:34:08 +0000252
253 return isEndif;
254 }
255
256 // Otherwise, we need to advance. Update CurPtr to point to the '#' token.
Ted Kremenek41a26602008-12-12 22:05:38 +0000257 CurPtr = HashEntryI;
Ted Kremenek268ee702008-12-12 18:34:08 +0000258
259 // Update the location of the last observed '#'. This is useful if we
260 // are skipping multiple blocks.
261 LastHashTokPtr = CurPtr;
Ted Kremenek268ee702008-12-12 18:34:08 +0000262
Ted Kremeneke5680f32008-12-23 01:30:52 +0000263 // Skip the '#' token.
264 assert(((tok::TokenKind) (unsigned char) *CurPtr) == tok::hash);
265 CurPtr += DISK_TOKEN_SIZE;
266
Ted Kremenek268ee702008-12-12 18:34:08 +0000267 // Did we reach a #endif? If so, go ahead and consume that token as well.
Ted Kremeneke5680f32008-12-23 01:30:52 +0000268 if (isEndif) { CurPtr += DISK_TOKEN_SIZE*2; }
Ted Kremenek268ee702008-12-12 18:34:08 +0000269
270 return isEndif;
271}
272
Ted Kremenek30a12ec2008-12-17 23:36:32 +0000273SourceLocation PTHLexer::getSourceLocation() {
274 // getLocation is not on the hot path. It is used to get the location of
275 // the next token when transitioning back to this lexer when done
276 // handling a #included file. Just read the necessary data from the token
277 // data buffer to construct the SourceLocation object.
278 // NOTE: This is a virtual function; hence it is defined out-of-line.
Ted Kremenek59d08cb2008-12-23 19:24:24 +0000279 const char* p = CurPtr + (1 + 1 + 3);
Ted Kremenek30a12ec2008-12-17 23:36:32 +0000280 uint32_t offset =
281 ((uint32_t) ((uint8_t) p[0]))
282 | (((uint32_t) ((uint8_t) p[1])) << 8)
283 | (((uint32_t) ((uint8_t) p[2])) << 16)
284 | (((uint32_t) ((uint8_t) p[3])) << 24);
285 return SourceLocation::getFileLoc(FileID, offset);
286}
287
Ted Kremenekb70e3da2009-01-08 02:47:16 +0000288unsigned PTHLexer::getSpelling(SourceLocation sloc, const char *&Buffer) {
289 return 0;
290}
291
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000292//===----------------------------------------------------------------------===//
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000293// Internal Data Structures for PTH file lookup and resolving identifiers.
294//===----------------------------------------------------------------------===//
295
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000296
297/// PTHFileLookup - This internal data structure is used by the PTHManager
298/// to map from FileEntry objects managed by FileManager to offsets within
299/// the PTH file.
300namespace {
301class VISIBILITY_HIDDEN PTHFileLookup {
302public:
303 class Val {
Ted Kremenekfb645b62008-12-11 23:36:38 +0000304 uint32_t TokenOff;
305 uint32_t PPCondOff;
Ted Kremenekb70e3da2009-01-08 02:47:16 +0000306 uint32_t SpellingOff;
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000307
308 public:
Ted Kremenekfb645b62008-12-11 23:36:38 +0000309 Val() : TokenOff(~0) {}
Ted Kremenekb70e3da2009-01-08 02:47:16 +0000310 Val(uint32_t toff, uint32_t poff, uint32_t soff)
311 : TokenOff(toff), PPCondOff(poff), SpellingOff(soff) {}
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000312
Ted Kremenekfb645b62008-12-11 23:36:38 +0000313 uint32_t getTokenOffset() const {
314 assert(TokenOff != ~((uint32_t)0) && "PTHFileLookup entry initialized.");
315 return TokenOff;
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000316 }
317
Ted Kremenekb70e3da2009-01-08 02:47:16 +0000318 uint32_t getPPCondOffset() const {
Ted Kremenekfb645b62008-12-11 23:36:38 +0000319 assert(TokenOff != ~((uint32_t)0) && "PTHFileLookup entry initialized.");
320 return PPCondOff;
321 }
322
Ted Kremenekb70e3da2009-01-08 02:47:16 +0000323 uint32_t getSpellingOffset() const {
324 assert(TokenOff != ~((uint32_t)0) && "PTHFileLookup entry initialized.");
325 return SpellingOff;
326 }
327
Ted Kremenekfb645b62008-12-11 23:36:38 +0000328 bool isValid() const { return TokenOff != ~((uint32_t)0); }
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000329 };
330
331private:
332 llvm::StringMap<Val> FileMap;
333
334public:
335 PTHFileLookup() {};
336
337 Val Lookup(const FileEntry* FE) {
338 const char* s = FE->getName();
339 unsigned size = strlen(s);
340 return FileMap.GetOrCreateValue(s, s+size).getValue();
341 }
342
343 void ReadTable(const char* D) {
344 uint32_t N = Read32(D); // Read the length of the table.
345
346 for ( ; N > 0; --N) { // The rest of the data is the table itself.
347 uint32_t len = Read32(D);
348 const char* s = D;
349 D += len;
Ted Kremenekb70e3da2009-01-08 02:47:16 +0000350
Ted Kremenekfb645b62008-12-11 23:36:38 +0000351 uint32_t TokenOff = Read32(D);
Ted Kremenekb70e3da2009-01-08 02:47:16 +0000352 uint32_t PPCondOff = Read32(D);
353 uint32_t SpellingOff = Read32(D);
354
355 FileMap.GetOrCreateValue(s, s+len).getValue() =
356 Val(TokenOff, PPCondOff, SpellingOff);
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000357 }
358 }
359};
360} // end anonymous namespace
361
362//===----------------------------------------------------------------------===//
363// PTHManager methods.
364//===----------------------------------------------------------------------===//
365
366PTHManager::PTHManager(const llvm::MemoryBuffer* buf, void* fileLookup,
Ted Kremenekcf58e622008-12-10 19:40:23 +0000367 const char* idDataTable, IdentifierInfo** perIDCache,
Ted Kremenek6183e482008-12-03 01:16:39 +0000368 Preprocessor& pp)
369: Buf(buf), PerIDCache(perIDCache), FileLookup(fileLookup),
370 IdDataTable(idDataTable), ITable(pp.getIdentifierTable()), PP(pp) {}
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000371
372PTHManager::~PTHManager() {
373 delete Buf;
374 delete (PTHFileLookup*) FileLookup;
Ted Kremenek0e50b6e2008-12-04 22:47:11 +0000375 free(PerIDCache);
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000376}
377
378PTHManager* PTHManager::Create(const std::string& file, Preprocessor& PP) {
379
380 // Memory map the PTH file.
381 llvm::OwningPtr<llvm::MemoryBuffer>
382 File(llvm::MemoryBuffer::getFile(file.c_str()));
383
384 if (!File)
385 return 0;
386
387 // Get the buffer ranges and check if there are at least three 32-bit
388 // words at the end of the file.
389 const char* BufBeg = File->getBufferStart();
390 const char* BufEnd = File->getBufferEnd();
391
392 if(!(BufEnd > BufBeg + sizeof(uint32_t)*3)) {
393 assert(false && "Invalid PTH file.");
394 return 0; // FIXME: Proper error diagnostic?
395 }
396
397 // Compute the address of the index table at the end of the PTH file.
398 // This table contains the offset of the file lookup table, the
399 // persistent ID -> identifer data table.
400 const char* EndTable = BufEnd - sizeof(uint32_t)*3;
401
402 // Construct the file lookup table. This will be used for mapping from
403 // FileEntry*'s to cached tokens.
404 const char* FileTableOffset = EndTable + sizeof(uint32_t)*2;
405 const char* FileTable = BufBeg + Read32(FileTableOffset);
406
407 if (!(FileTable > BufBeg && FileTable < BufEnd)) {
408 assert(false && "Invalid PTH file.");
409 return 0; // FIXME: Proper error diagnostic?
410 }
411
412 llvm::OwningPtr<PTHFileLookup> FL(new PTHFileLookup());
413 FL->ReadTable(FileTable);
414
415 // Get the location of the table mapping from persistent ids to the
416 // data needed to reconstruct identifiers.
417 const char* IDTableOffset = EndTable + sizeof(uint32_t)*1;
418 const char* IData = BufBeg + Read32(IDTableOffset);
419 if (!(IData > BufBeg && IData < BufEnd)) {
420 assert(false && "Invalid PTH file.");
421 return 0; // FIXME: Proper error diagnostic?
422 }
423
Ted Kremenek6183e482008-12-03 01:16:39 +0000424 // Get the number of IdentifierInfos and pre-allocate the identifier cache.
425 uint32_t NumIds = Read32(IData);
426
427 // Pre-allocate the peristent ID -> IdentifierInfo* cache. We use calloc()
428 // so that we in the best case only zero out memory once when the OS returns
429 // us new pages.
430 IdentifierInfo** PerIDCache =
431 (IdentifierInfo**) calloc(NumIds, sizeof(*PerIDCache));
432
433 if (!PerIDCache) {
434 assert(false && "Could not allocate Persistent ID cache.");
435 return 0;
436 }
437
438 // Create the new lexer.
439 return new PTHManager(File.take(), FL.take(), IData, PerIDCache, PP);
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000440}
441
Ted Kremenek866bdf72008-12-23 02:30:15 +0000442IdentifierInfo* PTHManager::GetIdentifierInfo(unsigned persistentID) {
443
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000444 // Check if the IdentifierInfo has already been resolved.
Ted Kremenekcf58e622008-12-10 19:40:23 +0000445 IdentifierInfo*& II = PerIDCache[persistentID];
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000446 if (II) return II;
447
448 // Look in the PTH file for the string data for the IdentifierInfo object.
449 const char* TableEntry = IdDataTable + sizeof(uint32_t) * persistentID;
450 const char* IDData = Buf->getBufferStart() + Read32(TableEntry);
451 assert(IDData < Buf->getBufferEnd());
452
453 // Read the length of the string.
454 uint32_t len = Read32(IDData);
455
456 // Get the IdentifierInfo* with the specified string.
457 II = &ITable.get(IDData, IDData+len);
458 return II;
459}
460
461PTHLexer* PTHManager::CreateLexer(unsigned FileID, const FileEntry* FE) {
462
463 if (!FE)
464 return 0;
465
466 // Lookup the FileEntry object in our file lookup data structure. It will
467 // return a variant that indicates whether or not there is an offset within
468 // the PTH file that contains cached tokens.
Ted Kremenekfb645b62008-12-11 23:36:38 +0000469 PTHFileLookup::Val FileData = ((PTHFileLookup*) FileLookup)->Lookup(FE);
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000470
Ted Kremenekfb645b62008-12-11 23:36:38 +0000471 if (!FileData.isValid()) // No tokens available.
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000472 return 0;
473
474 // Compute the offset of the token data within the buffer.
Ted Kremenekfb645b62008-12-11 23:36:38 +0000475 const char* data = Buf->getBufferStart() + FileData.getTokenOffset();
Ted Kremenek268ee702008-12-12 18:34:08 +0000476
477 // Get the location of pp-conditional table.
Ted Kremenekb70e3da2009-01-08 02:47:16 +0000478 const char* ppcond = Buf->getBufferStart() + FileData.getPPCondOffset();
479 uint32_t len = Read32(ppcond);
Ted Kremenek268ee702008-12-12 18:34:08 +0000480 if (len == 0) ppcond = 0;
Ted Kremenekb70e3da2009-01-08 02:47:16 +0000481
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000482 assert(data < Buf->getBufferEnd());
Ted Kremenek268ee702008-12-12 18:34:08 +0000483 return new PTHLexer(PP, SourceLocation::getFileLoc(FileID, 0), data, ppcond,
484 *this);
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000485}