PTHLexer.cpp revision 2d474ba9e8ae43a1a5a9f72718c0d79092b9453f
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/Basic/OnDiskHashTable.h"
18#include "clang/Lex/LexDiagnostic.h"
19#include "clang/Lex/PTHLexer.h"
20#include "clang/Lex/Preprocessor.h"
21#include "clang/Lex/PTHManager.h"
22#include "clang/Lex/Token.h"
23#include "clang/Lex/Preprocessor.h"
24#include "llvm/ADT/OwningPtr.h"
25#include "llvm/ADT/StringExtras.h"
26#include "llvm/ADT/StringMap.h"
27#include "llvm/Support/MemoryBuffer.h"
28#include <sys/stat.h>
29using namespace clang;
30using namespace clang::io;
31
32#define DISK_TOKEN_SIZE (1+1+2+4+4)
33
34//===----------------------------------------------------------------------===//
35// PTHLexer methods.
36//===----------------------------------------------------------------------===//
37
38PTHLexer::PTHLexer(Preprocessor &PP, FileID FID, const unsigned char *D,
39                   const unsigned char *ppcond, PTHManager &PM)
40  : PreprocessorLexer(&PP, FID), TokBuf(D), CurPtr(D), LastHashTokPtr(0),
41    PPCond(ppcond), CurPPCondPtr(ppcond), PTHMgr(PM) {
42
43  FileStartLoc = PP.getSourceManager().getLocForStartOfFile(FID);
44}
45
46void PTHLexer::Lex(Token& Tok) {
47LexNextToken:
48
49  //===--------------------------------------==//
50  // Read the raw token data.
51  //===--------------------------------------==//
52
53  // Shadow CurPtr into an automatic variable.
54  const unsigned char *CurPtrShadow = CurPtr;
55
56  // Read in the data for the token.
57  unsigned Word0 = ReadLE32(CurPtrShadow);
58  uint32_t IdentifierID = ReadLE32(CurPtrShadow);
59  uint32_t FileOffset = ReadLE32(CurPtrShadow);
60
61  tok::TokenKind TKind = (tok::TokenKind) (Word0 & 0xFF);
62  Token::TokenFlags TFlags = (Token::TokenFlags) ((Word0 >> 8) & 0xFF);
63  uint32_t Len = Word0 >> 16;
64
65  CurPtr = CurPtrShadow;
66
67  //===--------------------------------------==//
68  // Construct the token itself.
69  //===--------------------------------------==//
70
71  Tok.startToken();
72  Tok.setKind(TKind);
73  Tok.setFlag(TFlags);
74  assert(!LexingRawMode);
75  Tok.setLocation(FileStartLoc.getFileLocWithOffset(FileOffset));
76  Tok.setLength(Len);
77
78  // Handle identifiers.
79  if (Tok.isLiteral()) {
80    Tok.setLiteralData((const char*) (PTHMgr.SpellingBase + IdentifierID));
81  }
82  else if (IdentifierID) {
83    MIOpt.ReadToken();
84    IdentifierInfo *II = PTHMgr.GetIdentifierInfo(IdentifierID-1);
85
86    Tok.setIdentifierInfo(II);
87
88    // Change the kind of this identifier to the appropriate token kind, e.g.
89    // turning "for" into a keyword.
90    Tok.setKind(II->getTokenID());
91
92    if (II->isHandleIdentifierCase())
93      PP->HandleIdentifier(Tok);
94    return;
95  }
96
97  //===--------------------------------------==//
98  // Process the token.
99  //===--------------------------------------==//
100  if (TKind == tok::eof) {
101    // Save the end-of-file token.
102    EofToken = Tok;
103
104    // Save 'PP' to 'PPCache' as LexEndOfFile can delete 'this'.
105    Preprocessor *PPCache = PP;
106
107    assert(!ParsingPreprocessorDirective);
108    assert(!LexingRawMode);
109
110    if (LexEndOfFile(Tok))
111      return;
112
113    return PPCache->Lex(Tok);
114  }
115
116  if (TKind == tok::hash && Tok.isAtStartOfLine()) {
117    LastHashTokPtr = CurPtr - DISK_TOKEN_SIZE;
118    assert(!LexingRawMode);
119    PP->HandleDirective(Tok);
120
121    if (PP->isCurrentLexer(this))
122      goto LexNextToken;
123
124    return PP->Lex(Tok);
125  }
126
127  if (TKind == tok::eom) {
128    assert(ParsingPreprocessorDirective);
129    ParsingPreprocessorDirective = false;
130    return;
131  }
132
133  MIOpt.ReadToken();
134}
135
136bool PTHLexer::LexEndOfFile(Token &Result) {
137  // If we hit the end of the file while parsing a preprocessor directive,
138  // end the preprocessor directive first.  The next token returned will
139  // then be the end of file.
140  if (ParsingPreprocessorDirective) {
141    ParsingPreprocessorDirective = false; // Done parsing the "line".
142    return true;  // Have a token.
143  }
144
145  assert(!LexingRawMode);
146
147  // If we are in a #if directive, emit an error.
148  while (!ConditionalStack.empty()) {
149    if (!PP->isCodeCompletionFile(FileStartLoc))
150      PP->Diag(ConditionalStack.back().IfLoc,
151               diag::err_pp_unterminated_conditional);
152    ConditionalStack.pop_back();
153  }
154
155  // Finally, let the preprocessor handle this.
156  return PP->HandleEndOfFile(Result);
157}
158
159// FIXME: We can just grab the last token instead of storing a copy
160// into EofToken.
161void PTHLexer::getEOF(Token& Tok) {
162  assert(EofToken.is(tok::eof));
163  Tok = EofToken;
164}
165
166void PTHLexer::DiscardToEndOfLine() {
167  assert(ParsingPreprocessorDirective && ParsingFilename == false &&
168         "Must be in a preprocessing directive!");
169
170  // We assume that if the preprocessor wishes to discard to the end of
171  // the line that it also means to end the current preprocessor directive.
172  ParsingPreprocessorDirective = false;
173
174  // Skip tokens by only peeking at their token kind and the flags.
175  // We don't need to actually reconstruct full tokens from the token buffer.
176  // This saves some copies and it also reduces IdentifierInfo* lookup.
177  const unsigned char* p = CurPtr;
178  while (1) {
179    // Read the token kind.  Are we at the end of the file?
180    tok::TokenKind x = (tok::TokenKind) (uint8_t) *p;
181    if (x == tok::eof) break;
182
183    // Read the token flags.  Are we at the start of the next line?
184    Token::TokenFlags y = (Token::TokenFlags) (uint8_t) p[1];
185    if (y & Token::StartOfLine) break;
186
187    // Skip to the next token.
188    p += DISK_TOKEN_SIZE;
189  }
190
191  CurPtr = p;
192}
193
194/// SkipBlock - Used by Preprocessor to skip the current conditional block.
195bool PTHLexer::SkipBlock() {
196  assert(CurPPCondPtr && "No cached PP conditional information.");
197  assert(LastHashTokPtr && "No known '#' token.");
198
199  const unsigned char* HashEntryI = 0;
200  uint32_t Offset;
201  uint32_t TableIdx;
202
203  do {
204    // Read the token offset from the side-table.
205    Offset = ReadLE32(CurPPCondPtr);
206
207    // Read the target table index from the side-table.
208    TableIdx = ReadLE32(CurPPCondPtr);
209
210    // Compute the actual memory address of the '#' token data for this entry.
211    HashEntryI = TokBuf + Offset;
212
213    // Optmization: "Sibling jumping".  #if...#else...#endif blocks can
214    //  contain nested blocks.  In the side-table we can jump over these
215    //  nested blocks instead of doing a linear search if the next "sibling"
216    //  entry is not at a location greater than LastHashTokPtr.
217    if (HashEntryI < LastHashTokPtr && TableIdx) {
218      // In the side-table we are still at an entry for a '#' token that
219      // is earlier than the last one we saw.  Check if the location we would
220      // stride gets us closer.
221      const unsigned char* NextPPCondPtr =
222        PPCond + TableIdx*(sizeof(uint32_t)*2);
223      assert(NextPPCondPtr >= CurPPCondPtr);
224      // Read where we should jump to.
225      uint32_t TmpOffset = ReadLE32(NextPPCondPtr);
226      const unsigned char* HashEntryJ = TokBuf + TmpOffset;
227
228      if (HashEntryJ <= LastHashTokPtr) {
229        // Jump directly to the next entry in the side table.
230        HashEntryI = HashEntryJ;
231        Offset = TmpOffset;
232        TableIdx = ReadLE32(NextPPCondPtr);
233        CurPPCondPtr = NextPPCondPtr;
234      }
235    }
236  }
237  while (HashEntryI < LastHashTokPtr);
238  assert(HashEntryI == LastHashTokPtr && "No PP-cond entry found for '#'");
239  assert(TableIdx && "No jumping from #endifs.");
240
241  // Update our side-table iterator.
242  const unsigned char* NextPPCondPtr = PPCond + TableIdx*(sizeof(uint32_t)*2);
243  assert(NextPPCondPtr >= CurPPCondPtr);
244  CurPPCondPtr = NextPPCondPtr;
245
246  // Read where we should jump to.
247  HashEntryI = TokBuf + ReadLE32(NextPPCondPtr);
248  uint32_t NextIdx = ReadLE32(NextPPCondPtr);
249
250  // By construction NextIdx will be zero if this is a #endif.  This is useful
251  // to know to obviate lexing another token.
252  bool isEndif = NextIdx == 0;
253
254  // This case can occur when we see something like this:
255  //
256  //  #if ...
257  //   /* a comment or nothing */
258  //  #elif
259  //
260  // If we are skipping the first #if block it will be the case that CurPtr
261  // already points 'elif'.  Just return.
262
263  if (CurPtr > HashEntryI) {
264    assert(CurPtr == HashEntryI + DISK_TOKEN_SIZE);
265    // Did we reach a #endif?  If so, go ahead and consume that token as well.
266    if (isEndif)
267      CurPtr += DISK_TOKEN_SIZE*2;
268    else
269      LastHashTokPtr = HashEntryI;
270
271    return isEndif;
272  }
273
274  // Otherwise, we need to advance.  Update CurPtr to point to the '#' token.
275  CurPtr = HashEntryI;
276
277  // Update the location of the last observed '#'.  This is useful if we
278  // are skipping multiple blocks.
279  LastHashTokPtr = CurPtr;
280
281  // Skip the '#' token.
282  assert(((tok::TokenKind)*CurPtr) == tok::hash);
283  CurPtr += DISK_TOKEN_SIZE;
284
285  // Did we reach a #endif?  If so, go ahead and consume that token as well.
286  if (isEndif) { CurPtr += DISK_TOKEN_SIZE*2; }
287
288  return isEndif;
289}
290
291SourceLocation PTHLexer::getSourceLocation() {
292  // getSourceLocation is not on the hot path.  It is used to get the location
293  // of the next token when transitioning back to this lexer when done
294  // handling a #included file.  Just read the necessary data from the token
295  // data buffer to construct the SourceLocation object.
296  // NOTE: This is a virtual function; hence it is defined out-of-line.
297  const unsigned char *OffsetPtr = CurPtr + (DISK_TOKEN_SIZE - 4);
298  uint32_t Offset = ReadLE32(OffsetPtr);
299  return FileStartLoc.getFileLocWithOffset(Offset);
300}
301
302//===----------------------------------------------------------------------===//
303// PTH file lookup: map from strings to file data.
304//===----------------------------------------------------------------------===//
305
306/// PTHFileLookup - This internal data structure is used by the PTHManager
307///  to map from FileEntry objects managed by FileManager to offsets within
308///  the PTH file.
309namespace {
310class PTHFileData {
311  const uint32_t TokenOff;
312  const uint32_t PPCondOff;
313public:
314  PTHFileData(uint32_t tokenOff, uint32_t ppCondOff)
315    : TokenOff(tokenOff), PPCondOff(ppCondOff) {}
316
317  uint32_t getTokenOffset() const { return TokenOff; }
318  uint32_t getPPCondOffset() const { return PPCondOff; }
319};
320
321
322class PTHFileLookupCommonTrait {
323public:
324  typedef std::pair<unsigned char, const char*> internal_key_type;
325
326  static unsigned ComputeHash(internal_key_type x) {
327    return llvm::HashString(x.second);
328  }
329
330  static std::pair<unsigned, unsigned>
331  ReadKeyDataLength(const unsigned char*& d) {
332    unsigned keyLen = (unsigned) ReadUnalignedLE16(d);
333    unsigned dataLen = (unsigned) *(d++);
334    return std::make_pair(keyLen, dataLen);
335  }
336
337  static internal_key_type ReadKey(const unsigned char* d, unsigned) {
338    unsigned char k = *(d++); // Read the entry kind.
339    return std::make_pair(k, (const char*) d);
340  }
341};
342
343class PTHFileLookupTrait : public PTHFileLookupCommonTrait {
344public:
345  typedef const FileEntry* external_key_type;
346  typedef PTHFileData      data_type;
347
348  static internal_key_type GetInternalKey(const FileEntry* FE) {
349    return std::make_pair((unsigned char) 0x1, FE->getName());
350  }
351
352  static bool EqualKey(internal_key_type a, internal_key_type b) {
353    return a.first == b.first && strcmp(a.second, b.second) == 0;
354  }
355
356  static PTHFileData ReadData(const internal_key_type& k,
357                              const unsigned char* d, unsigned) {
358    assert(k.first == 0x1 && "Only file lookups can match!");
359    uint32_t x = ::ReadUnalignedLE32(d);
360    uint32_t y = ::ReadUnalignedLE32(d);
361    return PTHFileData(x, y);
362  }
363};
364
365class PTHStringLookupTrait {
366public:
367  typedef uint32_t
368          data_type;
369
370  typedef const std::pair<const char*, unsigned>
371          external_key_type;
372
373  typedef external_key_type internal_key_type;
374
375  static bool EqualKey(const internal_key_type& a,
376                       const internal_key_type& b) {
377    return (a.second == b.second) ? memcmp(a.first, b.first, a.second) == 0
378                                  : false;
379  }
380
381  static unsigned ComputeHash(const internal_key_type& a) {
382    return llvm::HashString(llvm::StringRef(a.first, a.second));
383  }
384
385  // This hopefully will just get inlined and removed by the optimizer.
386  static const internal_key_type&
387  GetInternalKey(const external_key_type& x) { return x; }
388
389  static std::pair<unsigned, unsigned>
390  ReadKeyDataLength(const unsigned char*& d) {
391    return std::make_pair((unsigned) ReadUnalignedLE16(d), sizeof(uint32_t));
392  }
393
394  static std::pair<const char*, unsigned>
395  ReadKey(const unsigned char* d, unsigned n) {
396      assert(n >= 2 && d[n-1] == '\0');
397      return std::make_pair((const char*) d, n-1);
398    }
399
400  static uint32_t ReadData(const internal_key_type& k, const unsigned char* d,
401                           unsigned) {
402    return ::ReadUnalignedLE32(d);
403  }
404};
405
406} // end anonymous namespace
407
408typedef OnDiskChainedHashTable<PTHFileLookupTrait>   PTHFileLookup;
409typedef OnDiskChainedHashTable<PTHStringLookupTrait> PTHStringIdLookup;
410
411//===----------------------------------------------------------------------===//
412// PTHManager methods.
413//===----------------------------------------------------------------------===//
414
415PTHManager::PTHManager(const llvm::MemoryBuffer* buf, void* fileLookup,
416                       const unsigned char* idDataTable,
417                       IdentifierInfo** perIDCache,
418                       void* stringIdLookup, unsigned numIds,
419                       const unsigned char* spellingBase,
420                       const char* originalSourceFile)
421: Buf(buf), PerIDCache(perIDCache), FileLookup(fileLookup),
422  IdDataTable(idDataTable), StringIdLookup(stringIdLookup),
423  NumIds(numIds), PP(0), SpellingBase(spellingBase),
424  OriginalSourceFile(originalSourceFile) {}
425
426PTHManager::~PTHManager() {
427  delete Buf;
428  delete (PTHFileLookup*) FileLookup;
429  delete (PTHStringIdLookup*) StringIdLookup;
430  free(PerIDCache);
431}
432
433static void InvalidPTH(Diagnostic &Diags, const char *Msg) {
434  Diags.Report(Diags.getCustomDiagID(Diagnostic::Error, Msg));
435}
436
437PTHManager* PTHManager::Create(const std::string& file, Diagnostic &Diags) {
438  // Memory map the PTH file.
439  llvm::OwningPtr<llvm::MemoryBuffer>
440  File(llvm::MemoryBuffer::getFile(file.c_str()));
441
442  if (!File) {
443    Diags.Report(diag::err_invalid_pth_file) << file;
444    return 0;
445  }
446
447  // Get the buffer ranges and check if there are at least three 32-bit
448  // words at the end of the file.
449  const unsigned char* BufBeg = (unsigned char*)File->getBufferStart();
450  const unsigned char* BufEnd = (unsigned char*)File->getBufferEnd();
451
452  // Check the prologue of the file.
453  if ((BufEnd - BufBeg) < (signed) (sizeof("cfe-pth") + 3 + 4) ||
454      memcmp(BufBeg, "cfe-pth", sizeof("cfe-pth") - 1) != 0) {
455    Diags.Report(diag::err_invalid_pth_file) << file;
456    return 0;
457  }
458
459  // Read the PTH version.
460  const unsigned char *p = BufBeg + (sizeof("cfe-pth") - 1);
461  unsigned Version = ReadLE32(p);
462
463  if (Version < PTHManager::Version) {
464    InvalidPTH(Diags,
465        Version < PTHManager::Version
466        ? "PTH file uses an older PTH format that is no longer supported"
467        : "PTH file uses a newer PTH format that cannot be read");
468    return 0;
469  }
470
471  // Compute the address of the index table at the end of the PTH file.
472  const unsigned char *PrologueOffset = p;
473
474  if (PrologueOffset >= BufEnd) {
475    Diags.Report(diag::err_invalid_pth_file) << file;
476    return 0;
477  }
478
479  // Construct the file lookup table.  This will be used for mapping from
480  // FileEntry*'s to cached tokens.
481  const unsigned char* FileTableOffset = PrologueOffset + sizeof(uint32_t)*2;
482  const unsigned char* FileTable = BufBeg + ReadLE32(FileTableOffset);
483
484  if (!(FileTable > BufBeg && FileTable < BufEnd)) {
485    Diags.Report(diag::err_invalid_pth_file) << file;
486    return 0; // FIXME: Proper error diagnostic?
487  }
488
489  llvm::OwningPtr<PTHFileLookup> FL(PTHFileLookup::Create(FileTable, BufBeg));
490
491  // Warn if the PTH file is empty.  We still want to create a PTHManager
492  // as the PTH could be used with -include-pth.
493  if (FL->isEmpty())
494    InvalidPTH(Diags, "PTH file contains no cached source data");
495
496  // Get the location of the table mapping from persistent ids to the
497  // data needed to reconstruct identifiers.
498  const unsigned char* IDTableOffset = PrologueOffset + sizeof(uint32_t)*0;
499  const unsigned char* IData = BufBeg + ReadLE32(IDTableOffset);
500
501  if (!(IData >= BufBeg && IData < BufEnd)) {
502    Diags.Report(diag::err_invalid_pth_file) << file;
503    return 0;
504  }
505
506  // Get the location of the hashtable mapping between strings and
507  // persistent IDs.
508  const unsigned char* StringIdTableOffset = PrologueOffset + sizeof(uint32_t)*1;
509  const unsigned char* StringIdTable = BufBeg + ReadLE32(StringIdTableOffset);
510  if (!(StringIdTable >= BufBeg && StringIdTable < BufEnd)) {
511    Diags.Report(diag::err_invalid_pth_file) << file;
512    return 0;
513  }
514
515  llvm::OwningPtr<PTHStringIdLookup> SL(PTHStringIdLookup::Create(StringIdTable,
516                                                                  BufBeg));
517
518  // Get the location of the spelling cache.
519  const unsigned char* spellingBaseOffset = PrologueOffset + sizeof(uint32_t)*3;
520  const unsigned char* spellingBase = BufBeg + ReadLE32(spellingBaseOffset);
521  if (!(spellingBase >= BufBeg && spellingBase < BufEnd)) {
522    Diags.Report(diag::err_invalid_pth_file) << file;
523    return 0;
524  }
525
526  // Get the number of IdentifierInfos and pre-allocate the identifier cache.
527  uint32_t NumIds = ReadLE32(IData);
528
529  // Pre-allocate the peristent ID -> IdentifierInfo* cache.  We use calloc()
530  // so that we in the best case only zero out memory once when the OS returns
531  // us new pages.
532  IdentifierInfo** PerIDCache = 0;
533
534  if (NumIds) {
535    PerIDCache = (IdentifierInfo**)calloc(NumIds, sizeof(*PerIDCache));
536    if (!PerIDCache) {
537      InvalidPTH(Diags, "Could not allocate memory for processing PTH file");
538      return 0;
539    }
540  }
541
542  // Compute the address of the original source file.
543  const unsigned char* originalSourceBase = PrologueOffset + sizeof(uint32_t)*4;
544  unsigned len = ReadUnalignedLE16(originalSourceBase);
545  if (!len) originalSourceBase = 0;
546
547  // Create the new PTHManager.
548  return new PTHManager(File.take(), FL.take(), IData, PerIDCache,
549                        SL.take(), NumIds, spellingBase,
550                        (const char*) originalSourceBase);
551}
552
553IdentifierInfo* PTHManager::LazilyCreateIdentifierInfo(unsigned PersistentID) {
554  // Look in the PTH file for the string data for the IdentifierInfo object.
555  const unsigned char* TableEntry = IdDataTable + sizeof(uint32_t)*PersistentID;
556  const unsigned char* IDData =
557    (const unsigned char*)Buf->getBufferStart() + ReadLE32(TableEntry);
558  assert(IDData < (const unsigned char*)Buf->getBufferEnd());
559
560  // Allocate the object.
561  std::pair<IdentifierInfo,const unsigned char*> *Mem =
562    Alloc.Allocate<std::pair<IdentifierInfo,const unsigned char*> >();
563
564  Mem->second = IDData;
565  assert(IDData[0] != '\0');
566  IdentifierInfo *II = new ((void*) Mem) IdentifierInfo();
567
568  // Store the new IdentifierInfo in the cache.
569  PerIDCache[PersistentID] = II;
570  assert(II->getNameStart() && II->getNameStart()[0] != '\0');
571  return II;
572}
573
574IdentifierInfo* PTHManager::get(llvm::StringRef Name) {
575  PTHStringIdLookup& SL = *((PTHStringIdLookup*)StringIdLookup);
576  // Double check our assumption that the last character isn't '\0'.
577  assert(Name.empty() || Name.data()[Name.size()-1] != '\0');
578  PTHStringIdLookup::iterator I = SL.find(std::make_pair(Name.data(),
579                                                         Name.size()));
580  if (I == SL.end()) // No identifier found?
581    return 0;
582
583  // Match found.  Return the identifier!
584  assert(*I > 0);
585  return GetIdentifierInfo(*I-1);
586}
587
588PTHLexer *PTHManager::CreateLexer(FileID FID) {
589  const FileEntry *FE = PP->getSourceManager().getFileEntryForID(FID);
590  if (!FE)
591    return 0;
592
593  // Lookup the FileEntry object in our file lookup data structure.  It will
594  // return a variant that indicates whether or not there is an offset within
595  // the PTH file that contains cached tokens.
596  PTHFileLookup& PFL = *((PTHFileLookup*)FileLookup);
597  PTHFileLookup::iterator I = PFL.find(FE);
598
599  if (I == PFL.end()) // No tokens available?
600    return 0;
601
602  const PTHFileData& FileData = *I;
603
604  const unsigned char *BufStart = (const unsigned char *)Buf->getBufferStart();
605  // Compute the offset of the token data within the buffer.
606  const unsigned char* data = BufStart + FileData.getTokenOffset();
607
608  // Get the location of pp-conditional table.
609  const unsigned char* ppcond = BufStart + FileData.getPPCondOffset();
610  uint32_t Len = ReadLE32(ppcond);
611  if (Len == 0) ppcond = 0;
612
613  assert(PP && "No preprocessor set yet!");
614  return new PTHLexer(*PP, FID, data, ppcond, *this);
615}
616
617//===----------------------------------------------------------------------===//
618// 'stat' caching.
619//===----------------------------------------------------------------------===//
620
621namespace {
622class PTHStatData {
623public:
624  const bool hasStat;
625  const ino_t ino;
626  const dev_t dev;
627  const mode_t mode;
628  const time_t mtime;
629  const off_t size;
630
631  PTHStatData(ino_t i, dev_t d, mode_t mo, time_t m, off_t s)
632  : hasStat(true), ino(i), dev(d), mode(mo), mtime(m), size(s) {}
633
634  PTHStatData()
635    : hasStat(false), ino(0), dev(0), mode(0), mtime(0), size(0) {}
636};
637
638class PTHStatLookupTrait : public PTHFileLookupCommonTrait {
639public:
640  typedef const char* external_key_type;  // const char*
641  typedef PTHStatData data_type;
642
643  static internal_key_type GetInternalKey(const char *path) {
644    // The key 'kind' doesn't matter here because it is ignored in EqualKey.
645    return std::make_pair((unsigned char) 0x0, path);
646  }
647
648  static bool EqualKey(internal_key_type a, internal_key_type b) {
649    // When doing 'stat' lookups we don't care about the kind of 'a' and 'b',
650    // just the paths.
651    return strcmp(a.second, b.second) == 0;
652  }
653
654  static data_type ReadData(const internal_key_type& k, const unsigned char* d,
655                            unsigned) {
656
657    if (k.first /* File or Directory */) {
658      if (k.first == 0x1 /* File */) d += 4 * 2; // Skip the first 2 words.
659      ino_t ino = (ino_t) ReadUnalignedLE32(d);
660      dev_t dev = (dev_t) ReadUnalignedLE32(d);
661      mode_t mode = (mode_t) ReadUnalignedLE16(d);
662      time_t mtime = (time_t) ReadUnalignedLE64(d);
663      return data_type(ino, dev, mode, mtime, (off_t) ReadUnalignedLE64(d));
664    }
665
666    // Negative stat.  Don't read anything.
667    return data_type();
668  }
669};
670
671class PTHStatCache : public StatSysCallCache {
672  typedef OnDiskChainedHashTable<PTHStatLookupTrait> CacheTy;
673  CacheTy Cache;
674
675public:
676  PTHStatCache(PTHFileLookup &FL) :
677    Cache(FL.getNumBuckets(), FL.getNumEntries(), FL.getBuckets(),
678          FL.getBase()) {}
679
680  ~PTHStatCache() {}
681
682  int stat(const char *path, struct stat *buf) {
683    // Do the lookup for the file's data in the PTH file.
684    CacheTy::iterator I = Cache.find(path);
685
686    // If we don't get a hit in the PTH file just forward to 'stat'.
687    if (I == Cache.end())
688      return StatSysCallCache::stat(path, buf);
689
690    const PTHStatData& Data = *I;
691
692    if (!Data.hasStat)
693      return 1;
694
695    buf->st_ino = Data.ino;
696    buf->st_dev = Data.dev;
697    buf->st_mtime = Data.mtime;
698    buf->st_mode = Data.mode;
699    buf->st_size = Data.size;
700    return 0;
701  }
702};
703} // end anonymous namespace
704
705StatSysCallCache *PTHManager::createStatCache() {
706  return new PTHStatCache(*((PTHFileLookup*) FileLookup));
707}
708