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