PTHLexer.cpp revision 898a0bb1972efb6e03cb1151412ec7392cef07de
1//===--- 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
14#include "clang/Basic/TokenKinds.h"
15#include "clang/Basic/FileManager.h"
16#include "clang/Basic/IdentifierTable.h"
17#include "clang/Lex/PTHLexer.h"
18#include "clang/Lex/Preprocessor.h"
19#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"
26using namespace clang;
27
28#define DISK_TOKEN_SIZE (1+1+3+4+2)
29
30//===----------------------------------------------------------------------===//
31// Utility methods for reading from the mmap'ed PTH file.
32//===----------------------------------------------------------------------===//
33
34static inline uint8_t Read8(const unsigned char *&Data) {
35  uint8_t V = Data[0];
36  Data += 1;
37  return V;
38}
39
40static inline uint16_t Read16(const unsigned char *&Data) {
41// Targets that directly support unaligned little-endian 16-bit loads can just
42// use them.
43#if defined(__i386__) || defined(__x86_64__)
44  uint16_t V = *((uint16_t*)Data);
45#else
46  uint16_t V = ((uint16_t)Data[0] <<  0) |
47               ((uint16_t)Data[1] <<  8);
48#endif
49  Data += 2;
50  return V;
51}
52
53static inline uint32_t Read24(const unsigned char *&Data) {
54// Targets that directly support unaligned little-endian 16-bit loads can just
55// use them.
56#if defined(__i386__) || defined(__x86_64__)
57  uint32_t V = ((uint16_t*)Data)[0] |
58                 ((uint32_t)Data[2] << 16);
59#else
60  uint32_t V = ((uint32_t)Data[0] <<  0) |
61               ((uint32_t)Data[1] <<  8) |
62               ((uint32_t)Data[2] << 16);
63#endif
64
65  Data += 3;
66  return V;
67}
68
69static inline uint32_t Read32(const unsigned char *&Data) {
70// Targets that directly support unaligned little-endian 32-bit loads can just
71// use them.
72#if defined(__i386__) || defined(__x86_64__)
73  uint32_t V = *((uint32_t*)Data);
74#else
75  uint32_t V = ((uint32_t)Data[0] <<  0) |
76               ((uint32_t)Data[1] <<  8) |
77               ((uint32_t)Data[2] << 16) |
78               ((uint32_t)Data[3] << 24);
79#endif
80  Data += 4;
81  return V;
82}
83
84
85//===----------------------------------------------------------------------===//
86// PTHLexer methods.
87//===----------------------------------------------------------------------===//
88
89PTHLexer::PTHLexer(Preprocessor &PP, FileID FID, const unsigned char *D,
90                   const unsigned char *ppcond,
91                   PTHSpellingSearch &mySpellingSrch, PTHManager &PM)
92  : PreprocessorLexer(&PP, FID), TokBuf(D), CurPtr(D), LastHashTokPtr(0),
93    PPCond(ppcond), CurPPCondPtr(ppcond), MySpellingSrch(mySpellingSrch),
94    PTHMgr(PM) {
95
96  FileStartLoc = PP.getSourceManager().getLocForStartOfFile(FID);
97}
98
99void PTHLexer::Lex(Token& Tok) {
100LexNextToken:
101
102  //===--------------------------------------==//
103  // Read the raw token data.
104  //===--------------------------------------==//
105
106  // Shadow CurPtr into an automatic variable.
107  const unsigned char *CurPtrShadow = CurPtr;
108
109  // Read in the data for the token.
110  tok::TokenKind TKind = (tok::TokenKind) Read8(CurPtrShadow);
111  Token::TokenFlags TFlags = (Token::TokenFlags) Read8(CurPtrShadow);
112  uint32_t IdentifierID = Read24(CurPtrShadow);
113  uint32_t FileOffset = Read32(CurPtrShadow);
114  uint32_t Len = Read16(CurPtrShadow);
115  CurPtr = CurPtrShadow;
116
117  //===--------------------------------------==//
118  // Construct the token itself.
119  //===--------------------------------------==//
120
121  Tok.startToken();
122  Tok.setKind(TKind);
123  Tok.setFlag(TFlags);
124  assert(!LexingRawMode);
125  if (IdentifierID)
126    Tok.setIdentifierInfo(PTHMgr.GetIdentifierInfo(IdentifierID-1));
127  Tok.setLocation(FileStartLoc.getFileLocWithOffset(FileOffset));
128  Tok.setLength(Len);
129
130  //===--------------------------------------==//
131  // Process the token.
132  //===--------------------------------------==//
133#if 0
134  SourceManager& SM = PP->getSourceManager();
135  llvm::cerr << SM.getFileEntryForID(FileID)->getName()
136    << ':' << SM.getLogicalLineNumber(Tok.getLocation())
137    << ':' << SM.getLogicalColumnNumber(Tok.getLocation())
138    << '\n';
139#endif
140
141  if (TKind == tok::identifier) {
142    MIOpt.ReadToken();
143    return PP->HandleIdentifier(Tok);
144  }
145
146  if (TKind == tok::eof) {
147    // Save the end-of-file token.
148    EofToken = Tok;
149
150    Preprocessor *PPCache = PP;
151
152    assert(!ParsingPreprocessorDirective);
153    assert(!LexingRawMode);
154
155    // FIXME: Issue diagnostics similar to Lexer.
156    if (PP->HandleEndOfFile(Tok, false))
157      return;
158
159    assert(PPCache && "Raw buffer::LexEndOfFile should return a token");
160    return PPCache->Lex(Tok);
161  }
162
163  if (TKind == tok::hash && Tok.isAtStartOfLine()) {
164    LastHashTokPtr = CurPtr - DISK_TOKEN_SIZE;
165    assert(!LexingRawMode);
166    PP->HandleDirective(Tok);
167
168    if (PP->isCurrentLexer(this))
169      goto LexNextToken;
170
171    return PP->Lex(Tok);
172  }
173
174  if (TKind == tok::eom) {
175    assert(ParsingPreprocessorDirective);
176    ParsingPreprocessorDirective = false;
177    return;
178  }
179
180  MIOpt.ReadToken();
181}
182
183// FIXME: We can just grab the last token instead of storing a copy
184// into EofToken.
185void PTHLexer::getEOF(Token& Tok) {
186  assert(EofToken.is(tok::eof));
187  Tok = EofToken;
188}
189
190void PTHLexer::DiscardToEndOfLine() {
191  assert(ParsingPreprocessorDirective && ParsingFilename == false &&
192         "Must be in a preprocessing directive!");
193
194  // We assume that if the preprocessor wishes to discard to the end of
195  // the line that it also means to end the current preprocessor directive.
196  ParsingPreprocessorDirective = false;
197
198  // Skip tokens by only peeking at their token kind and the flags.
199  // We don't need to actually reconstruct full tokens from the token buffer.
200  // This saves some copies and it also reduces IdentifierInfo* lookup.
201  const unsigned char* p = CurPtr;
202  while (1) {
203    // Read the token kind.  Are we at the end of the file?
204    tok::TokenKind x = (tok::TokenKind) (uint8_t) *p;
205    if (x == tok::eof) break;
206
207    // Read the token flags.  Are we at the start of the next line?
208    Token::TokenFlags y = (Token::TokenFlags) (uint8_t) p[1];
209    if (y & Token::StartOfLine) break;
210
211    // Skip to the next token.
212    p += DISK_TOKEN_SIZE;
213  }
214
215  CurPtr = p;
216}
217
218/// SkipBlock - Used by Preprocessor to skip the current conditional block.
219bool PTHLexer::SkipBlock() {
220  assert(CurPPCondPtr && "No cached PP conditional information.");
221  assert(LastHashTokPtr && "No known '#' token.");
222
223  const unsigned char* HashEntryI = 0;
224  uint32_t Offset;
225  uint32_t TableIdx;
226
227  do {
228    // Read the token offset from the side-table.
229    Offset = Read32(CurPPCondPtr);
230
231    // Read the target table index from the side-table.
232    TableIdx = Read32(CurPPCondPtr);
233
234    // Compute the actual memory address of the '#' token data for this entry.
235    HashEntryI = TokBuf + Offset;
236
237    // Optmization: "Sibling jumping".  #if...#else...#endif blocks can
238    //  contain nested blocks.  In the side-table we can jump over these
239    //  nested blocks instead of doing a linear search if the next "sibling"
240    //  entry is not at a location greater than LastHashTokPtr.
241    if (HashEntryI < LastHashTokPtr && TableIdx) {
242      // In the side-table we are still at an entry for a '#' token that
243      // is earlier than the last one we saw.  Check if the location we would
244      // stride gets us closer.
245      const unsigned char* NextPPCondPtr =
246        PPCond + TableIdx*(sizeof(uint32_t)*2);
247      assert(NextPPCondPtr >= CurPPCondPtr);
248      // Read where we should jump to.
249      uint32_t TmpOffset = Read32(NextPPCondPtr);
250      const unsigned char* HashEntryJ = TokBuf + TmpOffset;
251
252      if (HashEntryJ <= LastHashTokPtr) {
253        // Jump directly to the next entry in the side table.
254        HashEntryI = HashEntryJ;
255        Offset = TmpOffset;
256        TableIdx = Read32(NextPPCondPtr);
257        CurPPCondPtr = NextPPCondPtr;
258      }
259    }
260  }
261  while (HashEntryI < LastHashTokPtr);
262  assert(HashEntryI == LastHashTokPtr && "No PP-cond entry found for '#'");
263  assert(TableIdx && "No jumping from #endifs.");
264
265  // Update our side-table iterator.
266  const unsigned char* NextPPCondPtr = PPCond + TableIdx*(sizeof(uint32_t)*2);
267  assert(NextPPCondPtr >= CurPPCondPtr);
268  CurPPCondPtr = NextPPCondPtr;
269
270  // Read where we should jump to.
271  HashEntryI = TokBuf + Read32(NextPPCondPtr);
272  uint32_t NextIdx = Read32(NextPPCondPtr);
273
274  // By construction NextIdx will be zero if this is a #endif.  This is useful
275  // to know to obviate lexing another token.
276  bool isEndif = NextIdx == 0;
277
278  // This case can occur when we see something like this:
279  //
280  //  #if ...
281  //   /* a comment or nothing */
282  //  #elif
283  //
284  // If we are skipping the first #if block it will be the case that CurPtr
285  // already points 'elif'.  Just return.
286
287  if (CurPtr > HashEntryI) {
288    assert(CurPtr == HashEntryI + DISK_TOKEN_SIZE);
289    // Did we reach a #endif?  If so, go ahead and consume that token as well.
290    if (isEndif)
291      CurPtr += DISK_TOKEN_SIZE*2;
292    else
293      LastHashTokPtr = HashEntryI;
294
295    return isEndif;
296  }
297
298  // Otherwise, we need to advance.  Update CurPtr to point to the '#' token.
299  CurPtr = HashEntryI;
300
301  // Update the location of the last observed '#'.  This is useful if we
302  // are skipping multiple blocks.
303  LastHashTokPtr = CurPtr;
304
305  // Skip the '#' token.
306  assert(((tok::TokenKind)*CurPtr) == tok::hash);
307  CurPtr += DISK_TOKEN_SIZE;
308
309  // Did we reach a #endif?  If so, go ahead and consume that token as well.
310  if (isEndif) { CurPtr += DISK_TOKEN_SIZE*2; }
311
312  return isEndif;
313}
314
315SourceLocation PTHLexer::getSourceLocation() {
316  // getSourceLocation is not on the hot path.  It is used to get the location
317  // of the next token when transitioning back to this lexer when done
318  // handling a #included file.  Just read the necessary data from the token
319  // data buffer to construct the SourceLocation object.
320  // NOTE: This is a virtual function; hence it is defined out-of-line.
321  const unsigned char *OffsetPtr = CurPtr + (1 + 1 + 3);
322  uint32_t Offset = Read32(OffsetPtr);
323  return FileStartLoc.getFileLocWithOffset(Offset);
324}
325
326//===----------------------------------------------------------------------===//
327// getSpelling() - Use cached data in PTH files for getSpelling().
328//===----------------------------------------------------------------------===//
329
330unsigned PTHManager::getSpelling(FileID FID, unsigned FPos,
331                                 const char *&Buffer) {
332  llvm::DenseMap<FileID, PTHSpellingSearch*>::iterator I =SpellingMap.find(FID);
333
334  if (I == SpellingMap.end())
335    return 0;
336
337  return I->second->getSpellingBinarySearch(FPos, Buffer);
338}
339
340unsigned PTHManager::getSpelling(SourceLocation Loc, const char *&Buffer) {
341  SourceManager &SM = PP->getSourceManager();
342  Loc = SM.getSpellingLoc(Loc);
343  std::pair<FileID, unsigned> LocInfo = SM.getDecomposedFileLoc(Loc);
344  return getSpelling(LocInfo.first, LocInfo.second, Buffer);
345}
346
347unsigned PTHManager::getSpellingAtPTHOffset(unsigned PTHOffset,
348                                            const char *&Buffer) {
349  assert(PTHOffset < Buf->getBufferSize());
350  const unsigned char* Ptr =
351    (const unsigned char*)Buf->getBufferStart() + PTHOffset;
352
353  // The string is prefixed by 16 bits for its length, followed by the string
354  // itself.
355  unsigned Len = Read16(Ptr);
356  Buffer = (const char *)Ptr;
357  return Len;
358}
359
360unsigned PTHSpellingSearch::getSpellingLinearSearch(unsigned FPos,
361                                                    const char *&Buffer) {
362  const unsigned char *Ptr = LinearItr;
363  unsigned Len = 0;
364
365  if (Ptr == TableEnd)
366    return getSpellingBinarySearch(FPos, Buffer);
367
368  do {
369    uint32_t TokOffset = Read32(Ptr);
370
371    if (TokOffset > FPos)
372      return getSpellingBinarySearch(FPos, Buffer);
373
374    // Did we find a matching token offset for this spelling?
375    if (TokOffset == FPos) {
376      uint32_t SpellingPTHOffset = Read32(Ptr);
377      Len = PTHMgr.getSpellingAtPTHOffset(SpellingPTHOffset, Buffer);
378      break;
379    }
380  } while (Ptr != TableEnd);
381
382  LinearItr = Ptr;
383  return Len;
384}
385
386
387unsigned PTHSpellingSearch::getSpellingBinarySearch(unsigned FPos,
388                                                    const char *&Buffer) {
389
390  assert((TableEnd - TableBeg) % SpellingEntrySize == 0);
391  assert(TableEnd >= TableBeg);
392
393  if (TableEnd == TableBeg)
394    return 0;
395
396  unsigned min = 0;
397  const unsigned char *tb = TableBeg;
398  unsigned max = NumSpellings;
399
400  do {
401    unsigned i = (max - min) / 2 + min;
402    const unsigned char *Ptr = tb + (i * SpellingEntrySize);
403
404    uint32_t TokOffset = Read32(Ptr);
405    if (TokOffset > FPos) {
406      max = i;
407      assert(!(max == min) || (min == i));
408      continue;
409    }
410
411    if (TokOffset < FPos) {
412      if (i == min)
413        break;
414
415      min = i;
416      continue;
417    }
418
419    uint32_t SpellingPTHOffset = Read32(Ptr);
420    return PTHMgr.getSpellingAtPTHOffset(SpellingPTHOffset, Buffer);
421  }
422  while (min != max);
423
424  return 0;
425}
426
427unsigned PTHLexer::getSpelling(SourceLocation Loc, const char *&Buffer) {
428  SourceManager &SM = PP->getSourceManager();
429  Loc = SM.getSpellingLoc(Loc);
430  std::pair<FileID, unsigned> LocInfo = SM.getDecomposedFileLoc(Loc);
431
432  FileID FID = LocInfo.first;
433  unsigned FPos = LocInfo.second;
434
435  if (FID == getFileID())
436    return MySpellingSrch.getSpellingLinearSearch(FPos, Buffer);
437  return PTHMgr.getSpelling(FID, FPos, Buffer);
438}
439
440//===----------------------------------------------------------------------===//
441// Internal Data Structures for PTH file lookup and resolving identifiers.
442//===----------------------------------------------------------------------===//
443
444
445/// PTHFileLookup - This internal data structure is used by the PTHManager
446///  to map from FileEntry objects managed by FileManager to offsets within
447///  the PTH file.
448namespace {
449class VISIBILITY_HIDDEN PTHFileLookup {
450public:
451  class Val {
452    uint32_t TokenOff;
453    uint32_t PPCondOff;
454    uint32_t SpellingOff;
455  public:
456    Val() : TokenOff(~0) {}
457    Val(uint32_t toff, uint32_t poff, uint32_t soff)
458      : TokenOff(toff), PPCondOff(poff), SpellingOff(soff) {}
459
460    bool isValid() const { return TokenOff != ~((uint32_t)0); }
461
462    uint32_t getTokenOffset() const {
463      assert(isValid() && "PTHFileLookup entry initialized.");
464      return TokenOff;
465    }
466
467    uint32_t getPPCondOffset() const {
468      assert(isValid() && "PTHFileLookup entry initialized.");
469      return PPCondOff;
470    }
471
472    uint32_t getSpellingOffset() const {
473      assert(isValid() && "PTHFileLookup entry initialized.");
474      return SpellingOff;
475    }
476  };
477
478private:
479  llvm::StringMap<Val> FileMap;
480
481public:
482  PTHFileLookup() {};
483
484  Val Lookup(const FileEntry* FE) {
485    const char* s = FE->getName();
486    unsigned size = strlen(s);
487    return FileMap.GetOrCreateValue(s, s+size).getValue();
488  }
489
490  void ReadTable(const unsigned char* D) {
491    uint32_t N = Read32(D);     // Read the length of the table.
492
493    for ( ; N > 0; --N) {       // The rest of the data is the table itself.
494      uint32_t Len = Read32(D);
495      const char* s = (const char *)D;
496      D += Len;
497
498      uint32_t TokenOff = Read32(D);
499      uint32_t PPCondOff = Read32(D);
500      uint32_t SpellingOff = Read32(D);
501
502      FileMap.GetOrCreateValue(s, s+Len).getValue() =
503        Val(TokenOff, PPCondOff, SpellingOff);
504    }
505  }
506};
507} // end anonymous namespace
508
509//===----------------------------------------------------------------------===//
510// PTHManager methods.
511//===----------------------------------------------------------------------===//
512
513PTHManager::PTHManager(const llvm::MemoryBuffer* buf, void* fileLookup,
514                       const unsigned char* idDataTable,
515                       IdentifierInfo** perIDCache,
516                       const unsigned char* sortedIdTable, unsigned numIds)
517: Buf(buf), PerIDCache(perIDCache), FileLookup(fileLookup),
518  IdDataTable(idDataTable), SortedIdTable(sortedIdTable),
519  NumIds(numIds), PP(0) {}
520
521PTHManager::~PTHManager() {
522  delete Buf;
523  delete (PTHFileLookup*) FileLookup;
524  free(PerIDCache);
525}
526
527PTHManager* PTHManager::Create(const std::string& file) {
528  // Memory map the PTH file.
529  llvm::OwningPtr<llvm::MemoryBuffer>
530  File(llvm::MemoryBuffer::getFile(file.c_str()));
531
532  if (!File)
533    return 0;
534
535  // Get the buffer ranges and check if there are at least three 32-bit
536  // words at the end of the file.
537  const unsigned char* BufBeg = (unsigned char*)File->getBufferStart();
538  const unsigned char* BufEnd = (unsigned char*)File->getBufferEnd();
539
540  if(!(BufEnd > BufBeg + sizeof(uint32_t)*3)) {
541    assert(false && "Invalid PTH file.");
542    return 0; // FIXME: Proper error diagnostic?
543  }
544
545  // Compute the address of the index table at the end of the PTH file.
546  // This table contains the offset of the file lookup table, the
547  // persistent ID -> identifer data table.
548  // FIXME: We should just embed this offset in the PTH file.
549  const unsigned char* EndTable = BufEnd - sizeof(uint32_t)*4;
550
551  // Construct the file lookup table.  This will be used for mapping from
552  // FileEntry*'s to cached tokens.
553  const unsigned char* FileTableOffset = EndTable + sizeof(uint32_t)*3;
554  const unsigned char* FileTable = BufBeg + Read32(FileTableOffset);
555
556  if (!(FileTable > BufBeg && FileTable < BufEnd)) {
557    assert(false && "Invalid PTH file.");
558    return 0; // FIXME: Proper error diagnostic?
559  }
560
561  llvm::OwningPtr<PTHFileLookup> FL(new PTHFileLookup());
562  FL->ReadTable(FileTable);
563
564  // Get the location of the table mapping from persistent ids to the
565  // data needed to reconstruct identifiers.
566  const unsigned char* IDTableOffset = EndTable + sizeof(uint32_t)*1;
567  const unsigned char* IData = BufBeg + Read32(IDTableOffset);
568  if (!(IData > BufBeg && IData < BufEnd)) {
569    assert(false && "Invalid PTH file.");
570    return 0; // FIXME: Proper error diagnostic?
571  }
572
573  // Get the location of the lexigraphically-sorted table of persistent IDs.
574  const unsigned char* SortedIdTableOffset = EndTable + sizeof(uint32_t)*2;
575  const unsigned char* SortedIdTable = BufBeg + Read32(SortedIdTableOffset);
576  if (!(SortedIdTable > BufBeg && SortedIdTable < BufEnd)) {
577    assert(false && "Invalid PTH file.");
578    return 0; // FIXME: Proper error diagnostic?
579  }
580
581  // Get the number of IdentifierInfos and pre-allocate the identifier cache.
582  uint32_t NumIds = Read32(IData);
583
584  // Pre-allocate the peristent ID -> IdentifierInfo* cache.  We use calloc()
585  // so that we in the best case only zero out memory once when the OS returns
586  // us new pages.
587  IdentifierInfo** PerIDCache =
588    (IdentifierInfo**)calloc(NumIds, sizeof(*PerIDCache));
589
590  if (!PerIDCache) {
591    assert(false && "Could not allocate Persistent ID cache.");
592    return 0;
593  }
594
595  // Create the new PTHManager.
596  return new PTHManager(File.take(), FL.take(), IData, PerIDCache,
597                        SortedIdTable, NumIds);
598}
599
600IdentifierInfo* PTHManager::GetIdentifierInfo(unsigned persistentID) {
601
602  // Check if the IdentifierInfo has already been resolved.
603  IdentifierInfo* II = PerIDCache[persistentID];
604  if (II) return II;
605
606  // Look in the PTH file for the string data for the IdentifierInfo object.
607  const unsigned char* TableEntry = IdDataTable + sizeof(uint32_t)*persistentID;
608  const unsigned char* IDData =
609    (const unsigned char*)Buf->getBufferStart() + Read32(TableEntry);
610  assert(IDData < (const unsigned char*)Buf->getBufferEnd());
611
612  // Allocate the object.
613  std::pair<IdentifierInfo,const unsigned char*> *Mem =
614    Alloc.Allocate<std::pair<IdentifierInfo,const unsigned char*> >();
615
616  Mem->second = IDData;
617  II = new ((void*) Mem) IdentifierInfo(true);
618
619  // Store the new IdentifierInfo in the cache.
620  PerIDCache[persistentID] = II;
621  return II;
622}
623
624IdentifierInfo* PTHManager::get(const char *NameStart, const char *NameEnd) {
625  unsigned min = 0;
626  unsigned max = NumIds;
627  unsigned Len = NameEnd - NameStart;
628
629  do {
630    unsigned i = (max - min) / 2 + min;
631    const unsigned char *Ptr = SortedIdTable + (i * 4);
632
633    // Read the persistentID.
634    unsigned perID = Read32(Ptr);
635
636    // Get the IdentifierInfo.
637    IdentifierInfo* II = GetIdentifierInfo(perID);
638
639    // First compare the lengths.
640    unsigned IILen = II->getLength();
641    if (Len < IILen) goto IsLess;
642    if (Len > IILen) goto IsGreater;
643
644    // Now compare the strings!
645    {
646      signed comp = strncmp(NameStart, II->getName(), Len);
647      if (comp < 0) goto IsLess;
648      if (comp > 0) goto IsGreater;
649    }
650    // We found a match!
651    return II;
652
653  IsGreater:
654    if (i == min) break;
655    min = i;
656    continue;
657
658  IsLess:
659    max = i;
660    assert(!(max == min) || (min == i));
661  }
662  while (min != max);
663
664  return 0;
665}
666
667
668PTHLexer *PTHManager::CreateLexer(FileID FID) {
669  const FileEntry *FE = PP->getSourceManager().getFileEntryForID(FID);
670  if (!FE)
671    return 0;
672
673  // Lookup the FileEntry object in our file lookup data structure.  It will
674  // return a variant that indicates whether or not there is an offset within
675  // the PTH file that contains cached tokens.
676  PTHFileLookup::Val FileData = ((PTHFileLookup*)FileLookup)->Lookup(FE);
677
678  if (!FileData.isValid()) // No tokens available.
679    return 0;
680
681  const unsigned char *BufStart = (const unsigned char *)Buf->getBufferStart();
682  // Compute the offset of the token data within the buffer.
683  const unsigned char* data = BufStart + FileData.getTokenOffset();
684
685  // Get the location of pp-conditional table.
686  const unsigned char* ppcond = BufStart + FileData.getPPCondOffset();
687  uint32_t Len = Read32(ppcond);
688  if (Len == 0) ppcond = 0;
689
690  // Get the location of the spelling table.
691  const unsigned char* spellingTable = BufStart + FileData.getSpellingOffset();
692
693  Len = Read32(spellingTable);
694  if (Len == 0) spellingTable = 0;
695
696  assert(data < (const unsigned char*)Buf->getBufferEnd());
697
698  // Create the SpellingSearch object for this FileID.
699  PTHSpellingSearch* ss = new PTHSpellingSearch(*this, Len, spellingTable);
700  SpellingMap[FID] = ss;
701
702  assert(PP && "No preprocessor set yet!");
703  return new PTHLexer(*PP, FID, data, ppcond, *ss, *this);
704}
705