PTHLexer.cpp 23 KB

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