blob: c05b4b001042f3a76d0aa8e286dc24db0194a4f7 [file] [log] [blame]
Ted Kremenekca820862008-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 Kremenek325cd302008-12-03 00:38:03 +000014#include "clang/Basic/TokenKinds.h"
15#include "clang/Basic/FileManager.h"
16#include "clang/Basic/IdentifierTable.h"
Ted Kremenekca820862008-11-12 21:37:15 +000017#include "clang/Lex/PTHLexer.h"
18#include "clang/Lex/Preprocessor.h"
Ted Kremenek325cd302008-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 Kremenek325cd302008-12-03 00:38:03 +000026
Ted Kremenekca820862008-11-12 21:37:15 +000027using namespace clang;
28
Ted Kremenekc07091c2008-12-12 18:34:08 +000029#define DISK_TOKEN_SIZE (2+3*4)
30
Ted Kremenek325cd302008-12-03 00:38:03 +000031PTHLexer::PTHLexer(Preprocessor& pp, SourceLocation fileloc, const char* D,
Ted Kremenekc07091c2008-12-12 18:34:08 +000032 const char* ppcond, PTHManager& PM)
Ted Kremenekfe5c62d2008-12-11 22:41:47 +000033 : PreprocessorLexer(&pp, fileloc), TokBuf(D), CurPtr(D), LastHashTokPtr(0),
Ted Kremenekc07091c2008-12-12 18:34:08 +000034 PPCond(ppcond), CurPPCondPtr(ppcond), PTHMgr(PM), NeedsFetching(true) {
Ted Kremenek325cd302008-12-03 00:38:03 +000035 // Make sure the EofToken is completely clean.
36 EofToken.startToken();
37 }
Ted Kremenekca820862008-11-12 21:37:15 +000038
Ted Kremenek325cd302008-12-03 00:38:03 +000039Token PTHLexer::GetToken() {
40 // Read the next token, or if we haven't advanced yet, get the last
41 // token read.
42 if (NeedsFetching) {
43 NeedsFetching = false;
44 ReadToken(LastFetched);
45 }
46
47 Token Tok = LastFetched;
Ted Kremenek444b6bf2008-11-20 19:49:00 +000048
49 // If we are in raw mode, zero out identifier pointers. This is
50 // needed for 'pragma poison'. Note that this requires that the Preprocessor
51 // can go back to the original source when it calls getSpelling().
52 if (LexingRawMode && Tok.is(tok::identifier))
53 Tok.setIdentifierInfo(0);
54
55 return Tok;
56}
57
Ted Kremenekca820862008-11-12 21:37:15 +000058void PTHLexer::Lex(Token& Tok) {
Ted Kremeneke7cdb0a2008-11-20 07:58:05 +000059LexNextToken:
Ted Kremenekc37a3a22008-11-20 16:32:22 +000060 Tok = GetToken();
Ted Kremenekca820862008-11-12 21:37:15 +000061
Ted Kremenek4491ce52008-11-21 00:58:35 +000062 if (AtLastToken()) {
63 Preprocessor *PPCache = PP;
64
65 if (LexEndOfFile(Tok))
66 return;
67
68 assert(PPCache && "Raw buffer::LexEndOfFile should return a token");
69 return PPCache->Lex(Tok);
70 }
71
Ted Kremeneke7cdb0a2008-11-20 07:58:05 +000072 // Don't advance to the next token yet. Check if we are at the
73 // start of a new line and we're processing a directive. If so, we
74 // consume this token twice, once as an tok::eom.
75 if (Tok.isAtStartOfLine() && ParsingPreprocessorDirective) {
76 ParsingPreprocessorDirective = false;
77 Tok.setKind(tok::eom);
Ted Kremenekca820862008-11-12 21:37:15 +000078 MIOpt.ReadToken();
Ted Kremenekca820862008-11-12 21:37:15 +000079 return;
80 }
Ted Kremeneke7cdb0a2008-11-20 07:58:05 +000081
82 // Advance to the next token.
Ted Kremenekc37a3a22008-11-20 16:32:22 +000083 AdvanceToken();
Ted Kremenekca820862008-11-12 21:37:15 +000084
Ted Kremeneke7cdb0a2008-11-20 07:58:05 +000085 if (Tok.is(tok::hash)) {
Ted Kremenekc07091c2008-12-12 18:34:08 +000086 if (Tok.isAtStartOfLine()) {
87 LastHashTokPtr = CurPtr - DISK_TOKEN_SIZE;
88 if (!LexingRawMode) {
89 PP->HandleDirective(Tok);
Ted Kremeneke7cdb0a2008-11-20 07:58:05 +000090
Ted Kremenekc07091c2008-12-12 18:34:08 +000091 if (PP->isCurrentLexer(this))
92 goto LexNextToken;
93
94 return PP->Lex(Tok);
95 }
Ted Kremeneke7cdb0a2008-11-20 07:58:05 +000096 }
97 }
98
Ted Kremenekca820862008-11-12 21:37:15 +000099 MIOpt.ReadToken();
Ted Kremeneke7cdb0a2008-11-20 07:58:05 +0000100
101 if (Tok.is(tok::identifier)) {
102 if (LexingRawMode) return;
103 return PP->HandleIdentifier(Tok);
104 }
Ted Kremenekca820862008-11-12 21:37:15 +0000105}
106
Ted Kremenek4491ce52008-11-21 00:58:35 +0000107bool PTHLexer::LexEndOfFile(Token &Tok) {
108
109 if (ParsingPreprocessorDirective) {
110 ParsingPreprocessorDirective = false;
111 Tok.setKind(tok::eom);
112 MIOpt.ReadToken();
113 return true; // Have a token.
114 }
115
116 if (LexingRawMode) {
117 MIOpt.ReadToken();
118 return true; // Have an eof token.
119 }
120
121 // FIXME: Issue diagnostics similar to Lexer.
122 return PP->HandleEndOfFile(Tok, false);
123}
124
Ted Kremenekca820862008-11-12 21:37:15 +0000125void PTHLexer::setEOF(Token& Tok) {
Ted Kremenek325cd302008-12-03 00:38:03 +0000126 assert(!EofToken.is(tok::eof));
127 Tok = EofToken;
Ted Kremenekca820862008-11-12 21:37:15 +0000128}
Ted Kremenekb53b1f42008-11-19 22:21:33 +0000129
130void PTHLexer::DiscardToEndOfLine() {
131 assert(ParsingPreprocessorDirective && ParsingFilename == false &&
132 "Must be in a preprocessing directive!");
Ted Kremeneka295ef42008-11-20 01:16:50 +0000133
Ted Kremeneka90a5c42008-12-17 23:52:11 +0000134 // Skip tokens by only peeking at their token kind and the flags.
135 // We don't need to actually reconstruct full tokens from the token buffer.
136 // This saves some copies and it also reduces IdentifierInfo* lookup.
137 const char* p = CurPtr;
138 while (1) {
139 // Read the token kind. Are we at the end of the file?
140 tok::TokenKind x = (tok::TokenKind) (uint8_t) *p;
141 if (x == tok::eof) break;
Ted Kremeneka295ef42008-11-20 01:16:50 +0000142
Ted Kremeneka90a5c42008-12-17 23:52:11 +0000143 // Read the token flags. Are we at the start of the next line?
144 Token::TokenFlags y = (Token::TokenFlags) (uint8_t) p[1];
145 if (y == Token::StartOfLine) break;
146
147 // Skip to the next token.
148 p += DISK_TOKEN_SIZE;
149 }
150
151 CurPtr = p;
Ted Kremenekb53b1f42008-11-19 22:21:33 +0000152}
Ted Kremenek325cd302008-12-03 00:38:03 +0000153
154//===----------------------------------------------------------------------===//
155// Utility methods for reading from the mmap'ed PTH file.
156//===----------------------------------------------------------------------===//
157
158static inline uint8_t Read8(const char*& data) {
159 return (uint8_t) *(data++);
160}
161
162static inline uint32_t Read32(const char*& data) {
163 uint32_t V = (uint32_t) Read8(data);
164 V |= (((uint32_t) Read8(data)) << 8);
165 V |= (((uint32_t) Read8(data)) << 16);
166 V |= (((uint32_t) Read8(data)) << 24);
167 return V;
168}
169
Ted Kremenekc07091c2008-12-12 18:34:08 +0000170/// SkipBlock - Used by Preprocessor to skip the current conditional block.
171bool PTHLexer::SkipBlock() {
172 assert(CurPPCondPtr && "No cached PP conditional information.");
173 assert(LastHashTokPtr && "No known '#' token.");
174
Ted Kremenekbe3e84f2008-12-12 22:05:38 +0000175 const char* HashEntryI = 0;
Ted Kremenekc07091c2008-12-12 18:34:08 +0000176 uint32_t Offset;
177 uint32_t TableIdx;
178
179 do {
Ted Kremenekbe3e84f2008-12-12 22:05:38 +0000180 // Read the token offset from the side-table.
Ted Kremenekc07091c2008-12-12 18:34:08 +0000181 Offset = Read32(CurPPCondPtr);
Ted Kremenekbe3e84f2008-12-12 22:05:38 +0000182
183 // Read the target table index from the side-table.
Ted Kremenekc07091c2008-12-12 18:34:08 +0000184 TableIdx = Read32(CurPPCondPtr);
Ted Kremenekbe3e84f2008-12-12 22:05:38 +0000185
186 // Compute the actual memory address of the '#' token data for this entry.
187 HashEntryI = TokBuf + Offset;
188
189 // Optmization: "Sibling jumping". #if...#else...#endif blocks can
190 // contain nested blocks. In the side-table we can jump over these
191 // nested blocks instead of doing a linear search if the next "sibling"
192 // entry is not at a location greater than LastHashTokPtr.
193 if (HashEntryI < LastHashTokPtr && TableIdx) {
194 // In the side-table we are still at an entry for a '#' token that
195 // is earlier than the last one we saw. Check if the location we would
196 // stride gets us closer.
197 const char* NextPPCondPtr = PPCond + TableIdx*(sizeof(uint32_t)*2);
198 assert(NextPPCondPtr >= CurPPCondPtr);
199 // Read where we should jump to.
200 uint32_t TmpOffset = Read32(NextPPCondPtr);
201 const char* HashEntryJ = TokBuf + TmpOffset;
202
203 if (HashEntryJ <= LastHashTokPtr) {
204 // Jump directly to the next entry in the side table.
205 HashEntryI = HashEntryJ;
206 Offset = TmpOffset;
207 TableIdx = Read32(NextPPCondPtr);
208 CurPPCondPtr = NextPPCondPtr;
209 }
210 }
Ted Kremenekc07091c2008-12-12 18:34:08 +0000211 }
Ted Kremenekbe3e84f2008-12-12 22:05:38 +0000212 while (HashEntryI < LastHashTokPtr);
213 assert(HashEntryI == LastHashTokPtr && "No PP-cond entry found for '#'");
Ted Kremenekc07091c2008-12-12 18:34:08 +0000214 assert(TableIdx && "No jumping from #endifs.");
215
216 // Update our side-table iterator.
217 const char* NextPPCondPtr = PPCond + TableIdx*(sizeof(uint32_t)*2);
218 assert(NextPPCondPtr >= CurPPCondPtr);
219 CurPPCondPtr = NextPPCondPtr;
220
221 // Read where we should jump to.
Ted Kremenekbe3e84f2008-12-12 22:05:38 +0000222 HashEntryI = TokBuf + Read32(NextPPCondPtr);
Ted Kremenekc07091c2008-12-12 18:34:08 +0000223 uint32_t NextIdx = Read32(NextPPCondPtr);
224
225 // By construction NextIdx will be zero if this is a #endif. This is useful
226 // to know to obviate lexing another token.
227 bool isEndif = NextIdx == 0;
228 NeedsFetching = true;
229
230 // This case can occur when we see something like this:
231 //
232 // #if ...
233 // /* a comment or nothing */
234 // #elif
235 //
236 // If we are skipping the first #if block it will be the case that CurPtr
237 // already points 'elif'. Just return.
238
Ted Kremenekbe3e84f2008-12-12 22:05:38 +0000239 if (CurPtr > HashEntryI) {
240 assert(CurPtr == HashEntryI + DISK_TOKEN_SIZE);
Ted Kremenekc07091c2008-12-12 18:34:08 +0000241 // Did we reach a #endif? If so, go ahead and consume that token as well.
242 if (isEndif)
243 CurPtr += DISK_TOKEN_SIZE;
244 else
Ted Kremenekbe3e84f2008-12-12 22:05:38 +0000245 LastHashTokPtr = HashEntryI;
Ted Kremenekc07091c2008-12-12 18:34:08 +0000246
247 return isEndif;
248 }
249
250 // Otherwise, we need to advance. Update CurPtr to point to the '#' token.
Ted Kremenekbe3e84f2008-12-12 22:05:38 +0000251 CurPtr = HashEntryI;
Ted Kremenekc07091c2008-12-12 18:34:08 +0000252
253 // Update the location of the last observed '#'. This is useful if we
254 // are skipping multiple blocks.
255 LastHashTokPtr = CurPtr;
256
257#ifndef DEBUG
258 // In a debug build we should verify that the token is really a '#' that
259 // appears at the start of the line.
260 Token Tok;
261 ReadToken(Tok);
262 assert(Tok.isAtStartOfLine() && Tok.is(tok::hash));
263#else
264 // In a full release build we can just skip the token entirely.
265 CurPtr += DISK_TOKEN_SIZE;
266#endif
267
268 // Did we reach a #endif? If so, go ahead and consume that token as well.
269 if (isEndif) { CurPtr += DISK_TOKEN_SIZE; }
270
271 return isEndif;
272}
273
Ted Kremenekbeb57672008-12-17 23:36:32 +0000274SourceLocation PTHLexer::getSourceLocation() {
275 // getLocation is not on the hot path. It is used to get the location of
276 // the next token when transitioning back to this lexer when done
277 // handling a #included file. Just read the necessary data from the token
278 // data buffer to construct the SourceLocation object.
279 // NOTE: This is a virtual function; hence it is defined out-of-line.
280 const char* p = CurPtr + (1 + 1 + 4);
281 uint32_t offset =
282 ((uint32_t) ((uint8_t) p[0]))
283 | (((uint32_t) ((uint8_t) p[1])) << 8)
284 | (((uint32_t) ((uint8_t) p[2])) << 16)
285 | (((uint32_t) ((uint8_t) p[3])) << 24);
286 return SourceLocation::getFileLoc(FileID, offset);
287}
288
Ted Kremenek325cd302008-12-03 00:38:03 +0000289//===----------------------------------------------------------------------===//
290// Token reconstruction from the PTH file.
291//===----------------------------------------------------------------------===//
292
293void PTHLexer::ReadToken(Token& T) {
294 // Clear the token.
295 // FIXME: Setting the flags directly should obviate this step.
296 T.startToken();
297
Ted Kremeneke64d6d42008-12-17 18:38:19 +0000298 // Shadow CurPtr into an automatic variable so that Read8 doesn't load and
299 // store back into the instance variable.
300 const char *CurPtrShadow = CurPtr;
301
Ted Kremenek325cd302008-12-03 00:38:03 +0000302 // Read the type of the token.
Ted Kremeneke64d6d42008-12-17 18:38:19 +0000303 T.setKind((tok::TokenKind) Read8(CurPtrShadow));
Ted Kremenek325cd302008-12-03 00:38:03 +0000304
305 // Set flags. This is gross, since we are really setting multiple flags.
Ted Kremeneke64d6d42008-12-17 18:38:19 +0000306 T.setFlag((Token::TokenFlags) Read8(CurPtrShadow));
Ted Kremenek325cd302008-12-03 00:38:03 +0000307
308 // Set the IdentifierInfo* (if any).
Ted Kremeneke64d6d42008-12-17 18:38:19 +0000309 T.setIdentifierInfo(PTHMgr.ReadIdentifierInfo(CurPtrShadow));
Ted Kremenek325cd302008-12-03 00:38:03 +0000310
311 // Set the SourceLocation. Since all tokens are constructed using a
Chris Lattner71b94b32008-12-17 21:38:44 +0000312 // raw lexer, they will all be offseted from the same FileID.
Ted Kremeneke64d6d42008-12-17 18:38:19 +0000313 T.setLocation(SourceLocation::getFileLoc(FileID, Read32(CurPtrShadow)));
Ted Kremenek325cd302008-12-03 00:38:03 +0000314
315 // Finally, read and set the length of the token.
Ted Kremeneke64d6d42008-12-17 18:38:19 +0000316 T.setLength(Read32(CurPtrShadow));
317
318 CurPtr = CurPtrShadow;
Ted Kremenek325cd302008-12-03 00:38:03 +0000319}
320
321//===----------------------------------------------------------------------===//
322// Internal Data Structures for PTH file lookup and resolving identifiers.
323//===----------------------------------------------------------------------===//
324
Ted Kremenek325cd302008-12-03 00:38:03 +0000325
326/// PTHFileLookup - This internal data structure is used by the PTHManager
327/// to map from FileEntry objects managed by FileManager to offsets within
328/// the PTH file.
329namespace {
330class VISIBILITY_HIDDEN PTHFileLookup {
331public:
332 class Val {
Ted Kremenek8309c922008-12-11 23:36:38 +0000333 uint32_t TokenOff;
334 uint32_t PPCondOff;
Ted Kremenek325cd302008-12-03 00:38:03 +0000335
336 public:
Ted Kremenek8309c922008-12-11 23:36:38 +0000337 Val() : TokenOff(~0) {}
338 Val(uint32_t toff, uint32_t poff) : TokenOff(toff), PPCondOff(poff) {}
Ted Kremenek325cd302008-12-03 00:38:03 +0000339
Ted Kremenek8309c922008-12-11 23:36:38 +0000340 uint32_t getTokenOffset() const {
341 assert(TokenOff != ~((uint32_t)0) && "PTHFileLookup entry initialized.");
342 return TokenOff;
Ted Kremenek325cd302008-12-03 00:38:03 +0000343 }
344
Ted Kremenek8309c922008-12-11 23:36:38 +0000345 uint32_t gettPPCondOffset() const {
346 assert(TokenOff != ~((uint32_t)0) && "PTHFileLookup entry initialized.");
347 return PPCondOff;
348 }
349
350 bool isValid() const { return TokenOff != ~((uint32_t)0); }
Ted Kremenek325cd302008-12-03 00:38:03 +0000351 };
352
353private:
354 llvm::StringMap<Val> FileMap;
355
356public:
357 PTHFileLookup() {};
358
359 Val Lookup(const FileEntry* FE) {
360 const char* s = FE->getName();
361 unsigned size = strlen(s);
362 return FileMap.GetOrCreateValue(s, s+size).getValue();
363 }
364
365 void ReadTable(const char* D) {
366 uint32_t N = Read32(D); // Read the length of the table.
367
368 for ( ; N > 0; --N) { // The rest of the data is the table itself.
369 uint32_t len = Read32(D);
370 const char* s = D;
371 D += len;
Ted Kremenek8309c922008-12-11 23:36:38 +0000372 uint32_t TokenOff = Read32(D);
373 FileMap.GetOrCreateValue(s, s+len).getValue() = Val(TokenOff, Read32(D));
Ted Kremenek325cd302008-12-03 00:38:03 +0000374 }
375 }
376};
377} // end anonymous namespace
378
379//===----------------------------------------------------------------------===//
380// PTHManager methods.
381//===----------------------------------------------------------------------===//
382
383PTHManager::PTHManager(const llvm::MemoryBuffer* buf, void* fileLookup,
Ted Kremenek802fbd82008-12-10 19:40:23 +0000384 const char* idDataTable, IdentifierInfo** perIDCache,
Ted Kremenekdb4c8e82008-12-03 01:16:39 +0000385 Preprocessor& pp)
386: Buf(buf), PerIDCache(perIDCache), FileLookup(fileLookup),
387 IdDataTable(idDataTable), ITable(pp.getIdentifierTable()), PP(pp) {}
Ted Kremenek325cd302008-12-03 00:38:03 +0000388
389PTHManager::~PTHManager() {
390 delete Buf;
391 delete (PTHFileLookup*) FileLookup;
Ted Kremenek93bdc492008-12-04 22:47:11 +0000392 free(PerIDCache);
Ted Kremenek325cd302008-12-03 00:38:03 +0000393}
394
395PTHManager* PTHManager::Create(const std::string& file, Preprocessor& PP) {
396
397 // Memory map the PTH file.
398 llvm::OwningPtr<llvm::MemoryBuffer>
399 File(llvm::MemoryBuffer::getFile(file.c_str()));
400
401 if (!File)
402 return 0;
403
404 // Get the buffer ranges and check if there are at least three 32-bit
405 // words at the end of the file.
406 const char* BufBeg = File->getBufferStart();
407 const char* BufEnd = File->getBufferEnd();
408
409 if(!(BufEnd > BufBeg + sizeof(uint32_t)*3)) {
410 assert(false && "Invalid PTH file.");
411 return 0; // FIXME: Proper error diagnostic?
412 }
413
414 // Compute the address of the index table at the end of the PTH file.
415 // This table contains the offset of the file lookup table, the
416 // persistent ID -> identifer data table.
417 const char* EndTable = BufEnd - sizeof(uint32_t)*3;
418
419 // Construct the file lookup table. This will be used for mapping from
420 // FileEntry*'s to cached tokens.
421 const char* FileTableOffset = EndTable + sizeof(uint32_t)*2;
422 const char* FileTable = BufBeg + Read32(FileTableOffset);
423
424 if (!(FileTable > BufBeg && FileTable < BufEnd)) {
425 assert(false && "Invalid PTH file.");
426 return 0; // FIXME: Proper error diagnostic?
427 }
428
429 llvm::OwningPtr<PTHFileLookup> FL(new PTHFileLookup());
430 FL->ReadTable(FileTable);
431
432 // Get the location of the table mapping from persistent ids to the
433 // data needed to reconstruct identifiers.
434 const char* IDTableOffset = EndTable + sizeof(uint32_t)*1;
435 const char* IData = BufBeg + Read32(IDTableOffset);
436 if (!(IData > BufBeg && IData < BufEnd)) {
437 assert(false && "Invalid PTH file.");
438 return 0; // FIXME: Proper error diagnostic?
439 }
440
Ted Kremenekdb4c8e82008-12-03 01:16:39 +0000441 // Get the number of IdentifierInfos and pre-allocate the identifier cache.
442 uint32_t NumIds = Read32(IData);
443
444 // Pre-allocate the peristent ID -> IdentifierInfo* cache. We use calloc()
445 // so that we in the best case only zero out memory once when the OS returns
446 // us new pages.
447 IdentifierInfo** PerIDCache =
448 (IdentifierInfo**) calloc(NumIds, sizeof(*PerIDCache));
449
450 if (!PerIDCache) {
451 assert(false && "Could not allocate Persistent ID cache.");
452 return 0;
453 }
454
455 // Create the new lexer.
456 return new PTHManager(File.take(), FL.take(), IData, PerIDCache, PP);
Ted Kremenek325cd302008-12-03 00:38:03 +0000457}
458
459IdentifierInfo* PTHManager::ReadIdentifierInfo(const char*& D) {
460 // Read the persistent ID from the PTH file.
461 uint32_t persistentID = Read32(D);
462
463 // A persistent ID of '0' always maps to NULL.
464 if (!persistentID)
465 return 0;
466
467 // Adjust the persistent ID by subtracting '1' so that it can be used
468 // as an index within a table in the PTH file.
469 --persistentID;
470
471 // Check if the IdentifierInfo has already been resolved.
Ted Kremenek802fbd82008-12-10 19:40:23 +0000472 IdentifierInfo*& II = PerIDCache[persistentID];
Ted Kremenek325cd302008-12-03 00:38:03 +0000473 if (II) return II;
474
475 // Look in the PTH file for the string data for the IdentifierInfo object.
476 const char* TableEntry = IdDataTable + sizeof(uint32_t) * persistentID;
477 const char* IDData = Buf->getBufferStart() + Read32(TableEntry);
478 assert(IDData < Buf->getBufferEnd());
479
480 // Read the length of the string.
481 uint32_t len = Read32(IDData);
482
483 // Get the IdentifierInfo* with the specified string.
484 II = &ITable.get(IDData, IDData+len);
485 return II;
486}
487
488PTHLexer* PTHManager::CreateLexer(unsigned FileID, const FileEntry* FE) {
489
490 if (!FE)
491 return 0;
492
493 // Lookup the FileEntry object in our file lookup data structure. It will
494 // return a variant that indicates whether or not there is an offset within
495 // the PTH file that contains cached tokens.
Ted Kremenek8309c922008-12-11 23:36:38 +0000496 PTHFileLookup::Val FileData = ((PTHFileLookup*) FileLookup)->Lookup(FE);
Ted Kremenek325cd302008-12-03 00:38:03 +0000497
Ted Kremenek8309c922008-12-11 23:36:38 +0000498 if (!FileData.isValid()) // No tokens available.
Ted Kremenek325cd302008-12-03 00:38:03 +0000499 return 0;
500
501 // Compute the offset of the token data within the buffer.
Ted Kremenek8309c922008-12-11 23:36:38 +0000502 const char* data = Buf->getBufferStart() + FileData.getTokenOffset();
Ted Kremenekc07091c2008-12-12 18:34:08 +0000503
504 // Get the location of pp-conditional table.
505 const char* ppcond = Buf->getBufferStart() + FileData.gettPPCondOffset();
506 uint32_t len = Read32(ppcond);
507 if (len == 0) ppcond = 0;
508
Ted Kremenek325cd302008-12-03 00:38:03 +0000509 assert(data < Buf->getBufferEnd());
Ted Kremenekc07091c2008-12-12 18:34:08 +0000510 return new PTHLexer(PP, SourceLocation::getFileLoc(FileID, 0), data, ppcond,
511 *this);
Ted Kremenek325cd302008-12-03 00:38:03 +0000512}