clang API Documentation

PTHLexer.cpp
Go to the documentation of this file.
00001 //===--- PTHLexer.cpp - Lex from a token stream ---------------------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements the PTHLexer interface.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "clang/Basic/TokenKinds.h"
00015 #include "clang/Basic/FileManager.h"
00016 #include "clang/Basic/FileSystemStatCache.h"
00017 #include "clang/Basic/IdentifierTable.h"
00018 #include "clang/Basic/OnDiskHashTable.h"
00019 #include "clang/Lex/LexDiagnostic.h"
00020 #include "clang/Lex/PTHLexer.h"
00021 #include "clang/Lex/Preprocessor.h"
00022 #include "clang/Lex/PTHManager.h"
00023 #include "clang/Lex/Token.h"
00024 #include "clang/Lex/Preprocessor.h"
00025 #include "llvm/ADT/OwningPtr.h"
00026 #include "llvm/ADT/StringExtras.h"
00027 #include "llvm/ADT/StringMap.h"
00028 #include "llvm/Support/MemoryBuffer.h"
00029 #include "llvm/Support/system_error.h"
00030 using namespace clang;
00031 using namespace clang::io;
00032 
00033 #define DISK_TOKEN_SIZE (1+1+2+4+4)
00034 
00035 //===----------------------------------------------------------------------===//
00036 // PTHLexer methods.
00037 //===----------------------------------------------------------------------===//
00038 
00039 PTHLexer::PTHLexer(Preprocessor &PP, FileID FID, const unsigned char *D,
00040                    const unsigned char *ppcond, PTHManager &PM)
00041   : PreprocessorLexer(&PP, FID), TokBuf(D), CurPtr(D), LastHashTokPtr(0),
00042     PPCond(ppcond), CurPPCondPtr(ppcond), PTHMgr(PM) {
00043 
00044   FileStartLoc = PP.getSourceManager().getLocForStartOfFile(FID);
00045 }
00046 
00047 void PTHLexer::Lex(Token& Tok) {
00048 LexNextToken:
00049 
00050   //===--------------------------------------==//
00051   // Read the raw token data.
00052   //===--------------------------------------==//
00053 
00054   // Shadow CurPtr into an automatic variable.
00055   const unsigned char *CurPtrShadow = CurPtr;
00056 
00057   // Read in the data for the token.
00058   unsigned Word0 = ReadLE32(CurPtrShadow);
00059   uint32_t IdentifierID = ReadLE32(CurPtrShadow);
00060   uint32_t FileOffset = ReadLE32(CurPtrShadow);
00061 
00062   tok::TokenKind TKind = (tok::TokenKind) (Word0 & 0xFF);
00063   Token::TokenFlags TFlags = (Token::TokenFlags) ((Word0 >> 8) & 0xFF);
00064   uint32_t Len = Word0 >> 16;
00065 
00066   CurPtr = CurPtrShadow;
00067 
00068   //===--------------------------------------==//
00069   // Construct the token itself.
00070   //===--------------------------------------==//
00071 
00072   Tok.startToken();
00073   Tok.setKind(TKind);
00074   Tok.setFlag(TFlags);
00075   assert(!LexingRawMode);
00076   Tok.setLocation(FileStartLoc.getLocWithOffset(FileOffset));
00077   Tok.setLength(Len);
00078 
00079   // Handle identifiers.
00080   if (Tok.isLiteral()) {
00081     Tok.setLiteralData((const char*) (PTHMgr.SpellingBase + IdentifierID));
00082   }
00083   else if (IdentifierID) {
00084     MIOpt.ReadToken();
00085     IdentifierInfo *II = PTHMgr.GetIdentifierInfo(IdentifierID-1);
00086 
00087     Tok.setIdentifierInfo(II);
00088 
00089     // Change the kind of this identifier to the appropriate token kind, e.g.
00090     // turning "for" into a keyword.
00091     Tok.setKind(II->getTokenID());
00092 
00093     if (II->isHandleIdentifierCase())
00094       PP->HandleIdentifier(Tok);
00095     return;
00096   }
00097 
00098   //===--------------------------------------==//
00099   // Process the token.
00100   //===--------------------------------------==//
00101   if (TKind == tok::eof) {
00102     // Save the end-of-file token.
00103     EofToken = Tok;
00104 
00105     // Save 'PP' to 'PPCache' as LexEndOfFile can delete 'this'.
00106     Preprocessor *PPCache = PP;
00107 
00108     assert(!ParsingPreprocessorDirective);
00109     assert(!LexingRawMode);
00110     
00111     if (LexEndOfFile(Tok))
00112       return;
00113 
00114     return PPCache->Lex(Tok);
00115   }
00116 
00117   if (TKind == tok::hash && Tok.isAtStartOfLine()) {
00118     LastHashTokPtr = CurPtr - DISK_TOKEN_SIZE;
00119     assert(!LexingRawMode);
00120     PP->HandleDirective(Tok);
00121 
00122     if (PP->isCurrentLexer(this))
00123       goto LexNextToken;
00124 
00125     return PP->Lex(Tok);
00126   }
00127 
00128   if (TKind == tok::eod) {
00129     assert(ParsingPreprocessorDirective);
00130     ParsingPreprocessorDirective = false;
00131     return;
00132   }
00133 
00134   MIOpt.ReadToken();
00135 }
00136 
00137 bool PTHLexer::LexEndOfFile(Token &Result) {
00138   // If we hit the end of the file while parsing a preprocessor directive,
00139   // end the preprocessor directive first.  The next token returned will
00140   // then be the end of file.
00141   if (ParsingPreprocessorDirective) {
00142     ParsingPreprocessorDirective = false; // Done parsing the "line".
00143     return true;  // Have a token.
00144   }
00145   
00146   assert(!LexingRawMode);
00147 
00148   // If we are in a #if directive, emit an error.
00149   while (!ConditionalStack.empty()) {
00150     if (PP->getCodeCompletionFileLoc() != FileStartLoc)
00151       PP->Diag(ConditionalStack.back().IfLoc,
00152                diag::err_pp_unterminated_conditional);
00153     ConditionalStack.pop_back();
00154   }
00155 
00156   // Finally, let the preprocessor handle this.
00157   return PP->HandleEndOfFile(Result);
00158 }
00159 
00160 // FIXME: We can just grab the last token instead of storing a copy
00161 // into EofToken.
00162 void PTHLexer::getEOF(Token& Tok) {
00163   assert(EofToken.is(tok::eof));
00164   Tok = EofToken;
00165 }
00166 
00167 void PTHLexer::DiscardToEndOfLine() {
00168   assert(ParsingPreprocessorDirective && ParsingFilename == false &&
00169          "Must be in a preprocessing directive!");
00170 
00171   // We assume that if the preprocessor wishes to discard to the end of
00172   // the line that it also means to end the current preprocessor directive.
00173   ParsingPreprocessorDirective = false;
00174 
00175   // Skip tokens by only peeking at their token kind and the flags.
00176   // We don't need to actually reconstruct full tokens from the token buffer.
00177   // This saves some copies and it also reduces IdentifierInfo* lookup.
00178   const unsigned char* p = CurPtr;
00179   while (1) {
00180     // Read the token kind.  Are we at the end of the file?
00181     tok::TokenKind x = (tok::TokenKind) (uint8_t) *p;
00182     if (x == tok::eof) break;
00183 
00184     // Read the token flags.  Are we at the start of the next line?
00185     Token::TokenFlags y = (Token::TokenFlags) (uint8_t) p[1];
00186     if (y & Token::StartOfLine) break;
00187 
00188     // Skip to the next token.
00189     p += DISK_TOKEN_SIZE;
00190   }
00191 
00192   CurPtr = p;
00193 }
00194 
00195 /// SkipBlock - Used by Preprocessor to skip the current conditional block.
00196 bool PTHLexer::SkipBlock() {
00197   assert(CurPPCondPtr && "No cached PP conditional information.");
00198   assert(LastHashTokPtr && "No known '#' token.");
00199 
00200   const unsigned char* HashEntryI = 0;
00201   uint32_t Offset;
00202   uint32_t TableIdx;
00203 
00204   do {
00205     // Read the token offset from the side-table.
00206     Offset = ReadLE32(CurPPCondPtr);
00207 
00208     // Read the target table index from the side-table.
00209     TableIdx = ReadLE32(CurPPCondPtr);
00210 
00211     // Compute the actual memory address of the '#' token data for this entry.
00212     HashEntryI = TokBuf + Offset;
00213 
00214     // Optmization: "Sibling jumping".  #if...#else...#endif blocks can
00215     //  contain nested blocks.  In the side-table we can jump over these
00216     //  nested blocks instead of doing a linear search if the next "sibling"
00217     //  entry is not at a location greater than LastHashTokPtr.
00218     if (HashEntryI < LastHashTokPtr && TableIdx) {
00219       // In the side-table we are still at an entry for a '#' token that
00220       // is earlier than the last one we saw.  Check if the location we would
00221       // stride gets us closer.
00222       const unsigned char* NextPPCondPtr =
00223         PPCond + TableIdx*(sizeof(uint32_t)*2);
00224       assert(NextPPCondPtr >= CurPPCondPtr);
00225       // Read where we should jump to.
00226       uint32_t TmpOffset = ReadLE32(NextPPCondPtr);
00227       const unsigned char* HashEntryJ = TokBuf + TmpOffset;
00228 
00229       if (HashEntryJ <= LastHashTokPtr) {
00230         // Jump directly to the next entry in the side table.
00231         HashEntryI = HashEntryJ;
00232         Offset = TmpOffset;
00233         TableIdx = ReadLE32(NextPPCondPtr);
00234         CurPPCondPtr = NextPPCondPtr;
00235       }
00236     }
00237   }
00238   while (HashEntryI < LastHashTokPtr);
00239   assert(HashEntryI == LastHashTokPtr && "No PP-cond entry found for '#'");
00240   assert(TableIdx && "No jumping from #endifs.");
00241 
00242   // Update our side-table iterator.
00243   const unsigned char* NextPPCondPtr = PPCond + TableIdx*(sizeof(uint32_t)*2);
00244   assert(NextPPCondPtr >= CurPPCondPtr);
00245   CurPPCondPtr = NextPPCondPtr;
00246 
00247   // Read where we should jump to.
00248   HashEntryI = TokBuf + ReadLE32(NextPPCondPtr);
00249   uint32_t NextIdx = ReadLE32(NextPPCondPtr);
00250 
00251   // By construction NextIdx will be zero if this is a #endif.  This is useful
00252   // to know to obviate lexing another token.
00253   bool isEndif = NextIdx == 0;
00254 
00255   // This case can occur when we see something like this:
00256   //
00257   //  #if ...
00258   //   /* a comment or nothing */
00259   //  #elif
00260   //
00261   // If we are skipping the first #if block it will be the case that CurPtr
00262   // already points 'elif'.  Just return.
00263 
00264   if (CurPtr > HashEntryI) {
00265     assert(CurPtr == HashEntryI + DISK_TOKEN_SIZE);
00266     // Did we reach a #endif?  If so, go ahead and consume that token as well.
00267     if (isEndif)
00268       CurPtr += DISK_TOKEN_SIZE*2;
00269     else
00270       LastHashTokPtr = HashEntryI;
00271 
00272     return isEndif;
00273   }
00274 
00275   // Otherwise, we need to advance.  Update CurPtr to point to the '#' token.
00276   CurPtr = HashEntryI;
00277 
00278   // Update the location of the last observed '#'.  This is useful if we
00279   // are skipping multiple blocks.
00280   LastHashTokPtr = CurPtr;
00281 
00282   // Skip the '#' token.
00283   assert(((tok::TokenKind)*CurPtr) == tok::hash);
00284   CurPtr += DISK_TOKEN_SIZE;
00285 
00286   // Did we reach a #endif?  If so, go ahead and consume that token as well.
00287   if (isEndif) { CurPtr += DISK_TOKEN_SIZE*2; }
00288 
00289   return isEndif;
00290 }
00291 
00292 SourceLocation PTHLexer::getSourceLocation() {
00293   // getSourceLocation is not on the hot path.  It is used to get the location
00294   // of the next token when transitioning back to this lexer when done
00295   // handling a #included file.  Just read the necessary data from the token
00296   // data buffer to construct the SourceLocation object.
00297   // NOTE: This is a virtual function; hence it is defined out-of-line.
00298   const unsigned char *OffsetPtr = CurPtr + (DISK_TOKEN_SIZE - 4);
00299   uint32_t Offset = ReadLE32(OffsetPtr);
00300   return FileStartLoc.getLocWithOffset(Offset);
00301 }
00302 
00303 //===----------------------------------------------------------------------===//
00304 // PTH file lookup: map from strings to file data.
00305 //===----------------------------------------------------------------------===//
00306 
00307 /// PTHFileLookup - This internal data structure is used by the PTHManager
00308 ///  to map from FileEntry objects managed by FileManager to offsets within
00309 ///  the PTH file.
00310 namespace {
00311 class PTHFileData {
00312   const uint32_t TokenOff;
00313   const uint32_t PPCondOff;
00314 public:
00315   PTHFileData(uint32_t tokenOff, uint32_t ppCondOff)
00316     : TokenOff(tokenOff), PPCondOff(ppCondOff) {}
00317 
00318   uint32_t getTokenOffset() const { return TokenOff; }
00319   uint32_t getPPCondOffset() const { return PPCondOff; }
00320 };
00321 
00322 
00323 class PTHFileLookupCommonTrait {
00324 public:
00325   typedef std::pair<unsigned char, const char*> internal_key_type;
00326 
00327   static unsigned ComputeHash(internal_key_type x) {
00328     return llvm::HashString(x.second);
00329   }
00330 
00331   static std::pair<unsigned, unsigned>
00332   ReadKeyDataLength(const unsigned char*& d) {
00333     unsigned keyLen = (unsigned) ReadUnalignedLE16(d);
00334     unsigned dataLen = (unsigned) *(d++);
00335     return std::make_pair(keyLen, dataLen);
00336   }
00337 
00338   static internal_key_type ReadKey(const unsigned char* d, unsigned) {
00339     unsigned char k = *(d++); // Read the entry kind.
00340     return std::make_pair(k, (const char*) d);
00341   }
00342 };
00343 
00344 class PTHFileLookupTrait : public PTHFileLookupCommonTrait {
00345 public:
00346   typedef const FileEntry* external_key_type;
00347   typedef PTHFileData      data_type;
00348 
00349   static internal_key_type GetInternalKey(const FileEntry* FE) {
00350     return std::make_pair((unsigned char) 0x1, FE->getName());
00351   }
00352 
00353   static bool EqualKey(internal_key_type a, internal_key_type b) {
00354     return a.first == b.first && strcmp(a.second, b.second) == 0;
00355   }
00356 
00357   static PTHFileData ReadData(const internal_key_type& k,
00358                               const unsigned char* d, unsigned) {
00359     assert(k.first == 0x1 && "Only file lookups can match!");
00360     uint32_t x = ::ReadUnalignedLE32(d);
00361     uint32_t y = ::ReadUnalignedLE32(d);
00362     return PTHFileData(x, y);
00363   }
00364 };
00365 
00366 class PTHStringLookupTrait {
00367 public:
00368   typedef uint32_t
00369           data_type;
00370 
00371   typedef const std::pair<const char*, unsigned>
00372           external_key_type;
00373 
00374   typedef external_key_type internal_key_type;
00375 
00376   static bool EqualKey(const internal_key_type& a,
00377                        const internal_key_type& b) {
00378     return (a.second == b.second) ? memcmp(a.first, b.first, a.second) == 0
00379                                   : false;
00380   }
00381 
00382   static unsigned ComputeHash(const internal_key_type& a) {
00383     return llvm::HashString(StringRef(a.first, a.second));
00384   }
00385 
00386   // This hopefully will just get inlined and removed by the optimizer.
00387   static const internal_key_type&
00388   GetInternalKey(const external_key_type& x) { return x; }
00389 
00390   static std::pair<unsigned, unsigned>
00391   ReadKeyDataLength(const unsigned char*& d) {
00392     return std::make_pair((unsigned) ReadUnalignedLE16(d), sizeof(uint32_t));
00393   }
00394 
00395   static std::pair<const char*, unsigned>
00396   ReadKey(const unsigned char* d, unsigned n) {
00397       assert(n >= 2 && d[n-1] == '\0');
00398       return std::make_pair((const char*) d, n-1);
00399     }
00400 
00401   static uint32_t ReadData(const internal_key_type& k, const unsigned char* d,
00402                            unsigned) {
00403     return ::ReadUnalignedLE32(d);
00404   }
00405 };
00406 
00407 } // end anonymous namespace
00408 
00409 typedef OnDiskChainedHashTable<PTHFileLookupTrait>   PTHFileLookup;
00410 typedef OnDiskChainedHashTable<PTHStringLookupTrait> PTHStringIdLookup;
00411 
00412 //===----------------------------------------------------------------------===//
00413 // PTHManager methods.
00414 //===----------------------------------------------------------------------===//
00415 
00416 PTHManager::PTHManager(const llvm::MemoryBuffer* buf, void* fileLookup,
00417                        const unsigned char* idDataTable,
00418                        IdentifierInfo** perIDCache,
00419                        void* stringIdLookup, unsigned numIds,
00420                        const unsigned char* spellingBase,
00421                        const char* originalSourceFile)
00422 : Buf(buf), PerIDCache(perIDCache), FileLookup(fileLookup),
00423   IdDataTable(idDataTable), StringIdLookup(stringIdLookup),
00424   NumIds(numIds), PP(0), SpellingBase(spellingBase),
00425   OriginalSourceFile(originalSourceFile) {}
00426 
00427 PTHManager::~PTHManager() {
00428   delete Buf;
00429   delete (PTHFileLookup*) FileLookup;
00430   delete (PTHStringIdLookup*) StringIdLookup;
00431   free(PerIDCache);
00432 }
00433 
00434 static void InvalidPTH(DiagnosticsEngine &Diags, const char *Msg) {
00435   Diags.Report(Diags.getCustomDiagID(DiagnosticsEngine::Error, Msg));
00436 }
00437 
00438 PTHManager *PTHManager::Create(const std::string &file,
00439                                DiagnosticsEngine &Diags) {
00440   // Memory map the PTH file.
00441   OwningPtr<llvm::MemoryBuffer> File;
00442 
00443   if (llvm::MemoryBuffer::getFile(file, File)) {
00444     // FIXME: Add ec.message() to this diag.
00445     Diags.Report(diag::err_invalid_pth_file) << file;
00446     return 0;
00447   }
00448 
00449   // Get the buffer ranges and check if there are at least three 32-bit
00450   // words at the end of the file.
00451   const unsigned char *BufBeg = (unsigned char*)File->getBufferStart();
00452   const unsigned char *BufEnd = (unsigned char*)File->getBufferEnd();
00453 
00454   // Check the prologue of the file.
00455   if ((BufEnd - BufBeg) < (signed)(sizeof("cfe-pth") + 3 + 4) ||
00456       memcmp(BufBeg, "cfe-pth", sizeof("cfe-pth") - 1) != 0) {
00457     Diags.Report(diag::err_invalid_pth_file) << file;
00458     return 0;
00459   }
00460 
00461   // Read the PTH version.
00462   const unsigned char *p = BufBeg + (sizeof("cfe-pth") - 1);
00463   unsigned Version = ReadLE32(p);
00464 
00465   if (Version < PTHManager::Version) {
00466     InvalidPTH(Diags,
00467         Version < PTHManager::Version
00468         ? "PTH file uses an older PTH format that is no longer supported"
00469         : "PTH file uses a newer PTH format that cannot be read");
00470     return 0;
00471   }
00472 
00473   // Compute the address of the index table at the end of the PTH file.
00474   const unsigned char *PrologueOffset = p;
00475 
00476   if (PrologueOffset >= BufEnd) {
00477     Diags.Report(diag::err_invalid_pth_file) << file;
00478     return 0;
00479   }
00480 
00481   // Construct the file lookup table.  This will be used for mapping from
00482   // FileEntry*'s to cached tokens.
00483   const unsigned char* FileTableOffset = PrologueOffset + sizeof(uint32_t)*2;
00484   const unsigned char* FileTable = BufBeg + ReadLE32(FileTableOffset);
00485 
00486   if (!(FileTable > BufBeg && FileTable < BufEnd)) {
00487     Diags.Report(diag::err_invalid_pth_file) << file;
00488     return 0; // FIXME: Proper error diagnostic?
00489   }
00490 
00491   OwningPtr<PTHFileLookup> FL(PTHFileLookup::Create(FileTable, BufBeg));
00492 
00493   // Warn if the PTH file is empty.  We still want to create a PTHManager
00494   // as the PTH could be used with -include-pth.
00495   if (FL->isEmpty())
00496     InvalidPTH(Diags, "PTH file contains no cached source data");
00497 
00498   // Get the location of the table mapping from persistent ids to the
00499   // data needed to reconstruct identifiers.
00500   const unsigned char* IDTableOffset = PrologueOffset + sizeof(uint32_t)*0;
00501   const unsigned char* IData = BufBeg + ReadLE32(IDTableOffset);
00502 
00503   if (!(IData >= BufBeg && IData < BufEnd)) {
00504     Diags.Report(diag::err_invalid_pth_file) << file;
00505     return 0;
00506   }
00507 
00508   // Get the location of the hashtable mapping between strings and
00509   // persistent IDs.
00510   const unsigned char* StringIdTableOffset = PrologueOffset + sizeof(uint32_t)*1;
00511   const unsigned char* StringIdTable = BufBeg + ReadLE32(StringIdTableOffset);
00512   if (!(StringIdTable >= BufBeg && StringIdTable < BufEnd)) {
00513     Diags.Report(diag::err_invalid_pth_file) << file;
00514     return 0;
00515   }
00516 
00517   OwningPtr<PTHStringIdLookup> SL(PTHStringIdLookup::Create(StringIdTable,
00518                                                                   BufBeg));
00519 
00520   // Get the location of the spelling cache.
00521   const unsigned char* spellingBaseOffset = PrologueOffset + sizeof(uint32_t)*3;
00522   const unsigned char* spellingBase = BufBeg + ReadLE32(spellingBaseOffset);
00523   if (!(spellingBase >= BufBeg && spellingBase < BufEnd)) {
00524     Diags.Report(diag::err_invalid_pth_file) << file;
00525     return 0;
00526   }
00527 
00528   // Get the number of IdentifierInfos and pre-allocate the identifier cache.
00529   uint32_t NumIds = ReadLE32(IData);
00530 
00531   // Pre-allocate the persistent ID -> IdentifierInfo* cache.  We use calloc()
00532   // so that we in the best case only zero out memory once when the OS returns
00533   // us new pages.
00534   IdentifierInfo** PerIDCache = 0;
00535 
00536   if (NumIds) {
00537     PerIDCache = (IdentifierInfo**)calloc(NumIds, sizeof(*PerIDCache));
00538     if (!PerIDCache) {
00539       InvalidPTH(Diags, "Could not allocate memory for processing PTH file");
00540       return 0;
00541     }
00542   }
00543 
00544   // Compute the address of the original source file.
00545   const unsigned char* originalSourceBase = PrologueOffset + sizeof(uint32_t)*4;
00546   unsigned len = ReadUnalignedLE16(originalSourceBase);
00547   if (!len) originalSourceBase = 0;
00548 
00549   // Create the new PTHManager.
00550   return new PTHManager(File.take(), FL.take(), IData, PerIDCache,
00551                         SL.take(), NumIds, spellingBase,
00552                         (const char*) originalSourceBase);
00553 }
00554 
00555 IdentifierInfo* PTHManager::LazilyCreateIdentifierInfo(unsigned PersistentID) {
00556   // Look in the PTH file for the string data for the IdentifierInfo object.
00557   const unsigned char* TableEntry = IdDataTable + sizeof(uint32_t)*PersistentID;
00558   const unsigned char* IDData =
00559     (const unsigned char*)Buf->getBufferStart() + ReadLE32(TableEntry);
00560   assert(IDData < (const unsigned char*)Buf->getBufferEnd());
00561 
00562   // Allocate the object.
00563   std::pair<IdentifierInfo,const unsigned char*> *Mem =
00564     Alloc.Allocate<std::pair<IdentifierInfo,const unsigned char*> >();
00565 
00566   Mem->second = IDData;
00567   assert(IDData[0] != '\0');
00568   IdentifierInfo *II = new ((void*) Mem) IdentifierInfo();
00569 
00570   // Store the new IdentifierInfo in the cache.
00571   PerIDCache[PersistentID] = II;
00572   assert(II->getNameStart() && II->getNameStart()[0] != '\0');
00573   return II;
00574 }
00575 
00576 IdentifierInfo* PTHManager::get(StringRef Name) {
00577   PTHStringIdLookup& SL = *((PTHStringIdLookup*)StringIdLookup);
00578   // Double check our assumption that the last character isn't '\0'.
00579   assert(Name.empty() || Name.back() != '\0');
00580   PTHStringIdLookup::iterator I = SL.find(std::make_pair(Name.data(),
00581                                                          Name.size()));
00582   if (I == SL.end()) // No identifier found?
00583     return 0;
00584 
00585   // Match found.  Return the identifier!
00586   assert(*I > 0);
00587   return GetIdentifierInfo(*I-1);
00588 }
00589 
00590 PTHLexer *PTHManager::CreateLexer(FileID FID) {
00591   const FileEntry *FE = PP->getSourceManager().getFileEntryForID(FID);
00592   if (!FE)
00593     return 0;
00594 
00595   // Lookup the FileEntry object in our file lookup data structure.  It will
00596   // return a variant that indicates whether or not there is an offset within
00597   // the PTH file that contains cached tokens.
00598   PTHFileLookup& PFL = *((PTHFileLookup*)FileLookup);
00599   PTHFileLookup::iterator I = PFL.find(FE);
00600 
00601   if (I == PFL.end()) // No tokens available?
00602     return 0;
00603 
00604   const PTHFileData& FileData = *I;
00605 
00606   const unsigned char *BufStart = (const unsigned char *)Buf->getBufferStart();
00607   // Compute the offset of the token data within the buffer.
00608   const unsigned char* data = BufStart + FileData.getTokenOffset();
00609 
00610   // Get the location of pp-conditional table.
00611   const unsigned char* ppcond = BufStart + FileData.getPPCondOffset();
00612   uint32_t Len = ReadLE32(ppcond);
00613   if (Len == 0) ppcond = 0;
00614 
00615   assert(PP && "No preprocessor set yet!");
00616   return new PTHLexer(*PP, FID, data, ppcond, *this);
00617 }
00618 
00619 //===----------------------------------------------------------------------===//
00620 // 'stat' caching.
00621 //===----------------------------------------------------------------------===//
00622 
00623 namespace {
00624 class PTHStatData {
00625 public:
00626   const bool hasStat;
00627   const ino_t ino;
00628   const dev_t dev;
00629   const mode_t mode;
00630   const time_t mtime;
00631   const off_t size;
00632 
00633   PTHStatData(ino_t i, dev_t d, mode_t mo, time_t m, off_t s)
00634   : hasStat(true), ino(i), dev(d), mode(mo), mtime(m), size(s) {}
00635 
00636   PTHStatData()
00637     : hasStat(false), ino(0), dev(0), mode(0), mtime(0), size(0) {}
00638 };
00639 
00640 class PTHStatLookupTrait : public PTHFileLookupCommonTrait {
00641 public:
00642   typedef const char* external_key_type;  // const char*
00643   typedef PTHStatData data_type;
00644 
00645   static internal_key_type GetInternalKey(const char *path) {
00646     // The key 'kind' doesn't matter here because it is ignored in EqualKey.
00647     return std::make_pair((unsigned char) 0x0, path);
00648   }
00649 
00650   static bool EqualKey(internal_key_type a, internal_key_type b) {
00651     // When doing 'stat' lookups we don't care about the kind of 'a' and 'b',
00652     // just the paths.
00653     return strcmp(a.second, b.second) == 0;
00654   }
00655 
00656   static data_type ReadData(const internal_key_type& k, const unsigned char* d,
00657                             unsigned) {
00658 
00659     if (k.first /* File or Directory */) {
00660       if (k.first == 0x1 /* File */) d += 4 * 2; // Skip the first 2 words.
00661       ino_t ino = (ino_t) ReadUnalignedLE32(d);
00662       dev_t dev = (dev_t) ReadUnalignedLE32(d);
00663       mode_t mode = (mode_t) ReadUnalignedLE16(d);
00664       time_t mtime = (time_t) ReadUnalignedLE64(d);
00665       return data_type(ino, dev, mode, mtime, (off_t) ReadUnalignedLE64(d));
00666     }
00667 
00668     // Negative stat.  Don't read anything.
00669     return data_type();
00670   }
00671 };
00672 
00673 class PTHStatCache : public FileSystemStatCache {
00674   typedef OnDiskChainedHashTable<PTHStatLookupTrait> CacheTy;
00675   CacheTy Cache;
00676 
00677 public:
00678   PTHStatCache(PTHFileLookup &FL) :
00679     Cache(FL.getNumBuckets(), FL.getNumEntries(), FL.getBuckets(),
00680           FL.getBase()) {}
00681 
00682   ~PTHStatCache() {}
00683 
00684   LookupResult getStat(const char *Path, struct stat &StatBuf,
00685                        int *FileDescriptor) {
00686     // Do the lookup for the file's data in the PTH file.
00687     CacheTy::iterator I = Cache.find(Path);
00688 
00689     // If we don't get a hit in the PTH file just forward to 'stat'.
00690     if (I == Cache.end())
00691       return statChained(Path, StatBuf, FileDescriptor);
00692 
00693     const PTHStatData &Data = *I;
00694 
00695     if (!Data.hasStat)
00696       return CacheMissing;
00697 
00698     StatBuf.st_ino = Data.ino;
00699     StatBuf.st_dev = Data.dev;
00700     StatBuf.st_mtime = Data.mtime;
00701     StatBuf.st_mode = Data.mode;
00702     StatBuf.st_size = Data.size;
00703     return CacheExists;
00704   }
00705 };
00706 } // end anonymous namespace
00707 
00708 FileSystemStatCache *PTHManager::createStatCache() {
00709   return new PTHStatCache(*((PTHFileLookup*) FileLookup));
00710 }