PTHLexer.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  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/IdentifierTable.h"
  16. #include "clang/Basic/OnDiskHashTable.h"
  17. #include "clang/Lex/LexDiagnostic.h"
  18. #include "clang/Lex/PTHLexer.h"
  19. #include "clang/Lex/Preprocessor.h"
  20. #include "clang/Lex/PTHManager.h"
  21. #include "clang/Lex/Token.h"
  22. #include "clang/Lex/Preprocessor.h"
  23. #include "llvm/ADT/OwningPtr.h"
  24. #include "llvm/ADT/StringExtras.h"
  25. #include "llvm/ADT/StringMap.h"
  26. #include "llvm/Support/MemoryBuffer.h"
  27. #include <sys/stat.h>
  28. using namespace clang;
  29. using namespace clang::io;
  30. #define DISK_TOKEN_SIZE (1+1+2+4+4)
  31. //===----------------------------------------------------------------------===//
  32. // PTHLexer methods.
  33. //===----------------------------------------------------------------------===//
  34. PTHLexer::PTHLexer(Preprocessor &PP, FileID FID, const unsigned char *D,
  35. const unsigned char *ppcond, PTHManager &PM)
  36. : PreprocessorLexer(&PP, FID), TokBuf(D), CurPtr(D), LastHashTokPtr(0),
  37. PPCond(ppcond), CurPPCondPtr(ppcond), PTHMgr(PM) {
  38. FileStartLoc = PP.getSourceManager().getLocForStartOfFile(FID);
  39. }
  40. void PTHLexer::Lex(Token& Tok) {
  41. LexNextToken:
  42. //===--------------------------------------==//
  43. // Read the raw token data.
  44. //===--------------------------------------==//
  45. // Shadow CurPtr into an automatic variable.
  46. const unsigned char *CurPtrShadow = CurPtr;
  47. // Read in the data for the token.
  48. unsigned Word0 = ReadLE32(CurPtrShadow);
  49. uint32_t IdentifierID = ReadLE32(CurPtrShadow);
  50. uint32_t FileOffset = ReadLE32(CurPtrShadow);
  51. tok::TokenKind TKind = (tok::TokenKind) (Word0 & 0xFF);
  52. Token::TokenFlags TFlags = (Token::TokenFlags) ((Word0 >> 8) & 0xFF);
  53. uint32_t Len = Word0 >> 16;
  54. CurPtr = CurPtrShadow;
  55. //===--------------------------------------==//
  56. // Construct the token itself.
  57. //===--------------------------------------==//
  58. Tok.startToken();
  59. Tok.setKind(TKind);
  60. Tok.setFlag(TFlags);
  61. assert(!LexingRawMode);
  62. Tok.setLocation(FileStartLoc.getFileLocWithOffset(FileOffset));
  63. Tok.setLength(Len);
  64. // Handle identifiers.
  65. if (Tok.isLiteral()) {
  66. Tok.setLiteralData((const char*) (PTHMgr.SpellingBase + IdentifierID));
  67. }
  68. else if (IdentifierID) {
  69. MIOpt.ReadToken();
  70. IdentifierInfo *II = PTHMgr.GetIdentifierInfo(IdentifierID-1);
  71. Tok.setIdentifierInfo(II);
  72. // Change the kind of this identifier to the appropriate token kind, e.g.
  73. // turning "for" into a keyword.
  74. Tok.setKind(II->getTokenID());
  75. if (II->isHandleIdentifierCase())
  76. PP->HandleIdentifier(Tok);
  77. return;
  78. }
  79. //===--------------------------------------==//
  80. // Process the token.
  81. //===--------------------------------------==//
  82. if (TKind == tok::eof) {
  83. // Save the end-of-file token.
  84. EofToken = Tok;
  85. // Save 'PP' to 'PPCache' as LexEndOfFile can delete 'this'.
  86. Preprocessor *PPCache = PP;
  87. assert(!ParsingPreprocessorDirective);
  88. assert(!LexingRawMode);
  89. if (LexEndOfFile(Tok))
  90. return;
  91. return PPCache->Lex(Tok);
  92. }
  93. if (TKind == tok::hash && Tok.isAtStartOfLine()) {
  94. LastHashTokPtr = CurPtr - DISK_TOKEN_SIZE;
  95. assert(!LexingRawMode);
  96. PP->HandleDirective(Tok);
  97. if (PP->isCurrentLexer(this))
  98. goto LexNextToken;
  99. return PP->Lex(Tok);
  100. }
  101. if (TKind == tok::eom) {
  102. assert(ParsingPreprocessorDirective);
  103. ParsingPreprocessorDirective = false;
  104. return;
  105. }
  106. MIOpt.ReadToken();
  107. }
  108. bool PTHLexer::LexEndOfFile(Token &Result) {
  109. // If we hit the end of the file while parsing a preprocessor directive,
  110. // end the preprocessor directive first. The next token returned will
  111. // then be the end of file.
  112. if (ParsingPreprocessorDirective) {
  113. ParsingPreprocessorDirective = false; // Done parsing the "line".
  114. return true; // Have a token.
  115. }
  116. assert(!LexingRawMode);
  117. // If we are in a #if directive, emit an error.
  118. while (!ConditionalStack.empty()) {
  119. if (!PP->isCodeCompletionFile(FileStartLoc))
  120. PP->Diag(ConditionalStack.back().IfLoc,
  121. diag::err_pp_unterminated_conditional);
  122. ConditionalStack.pop_back();
  123. }
  124. // Finally, let the preprocessor handle this.
  125. return PP->HandleEndOfFile(Result);
  126. }
  127. // FIXME: We can just grab the last token instead of storing a copy
  128. // into EofToken.
  129. void PTHLexer::getEOF(Token& Tok) {
  130. assert(EofToken.is(tok::eof));
  131. Tok = EofToken;
  132. }
  133. void PTHLexer::DiscardToEndOfLine() {
  134. assert(ParsingPreprocessorDirective && ParsingFilename == false &&
  135. "Must be in a preprocessing directive!");
  136. // We assume that if the preprocessor wishes to discard to the end of
  137. // the line that it also means to end the current preprocessor directive.
  138. ParsingPreprocessorDirective = false;
  139. // Skip tokens by only peeking at their token kind and the flags.
  140. // We don't need to actually reconstruct full tokens from the token buffer.
  141. // This saves some copies and it also reduces IdentifierInfo* lookup.
  142. const unsigned char* p = CurPtr;
  143. while (1) {
  144. // Read the token kind. Are we at the end of the file?
  145. tok::TokenKind x = (tok::TokenKind) (uint8_t) *p;
  146. if (x == tok::eof) break;
  147. // Read the token flags. Are we at the start of the next line?
  148. Token::TokenFlags y = (Token::TokenFlags) (uint8_t) p[1];
  149. if (y & Token::StartOfLine) break;
  150. // Skip to the next token.
  151. p += DISK_TOKEN_SIZE;
  152. }
  153. CurPtr = p;
  154. }
  155. /// SkipBlock - Used by Preprocessor to skip the current conditional block.
  156. bool PTHLexer::SkipBlock() {
  157. assert(CurPPCondPtr && "No cached PP conditional information.");
  158. assert(LastHashTokPtr && "No known '#' token.");
  159. const unsigned char* HashEntryI = 0;
  160. uint32_t Offset;
  161. uint32_t TableIdx;
  162. do {
  163. // Read the token offset from the side-table.
  164. Offset = ReadLE32(CurPPCondPtr);
  165. // Read the target table index from the side-table.
  166. TableIdx = ReadLE32(CurPPCondPtr);
  167. // Compute the actual memory address of the '#' token data for this entry.
  168. HashEntryI = TokBuf + Offset;
  169. // Optmization: "Sibling jumping". #if...#else...#endif blocks can
  170. // contain nested blocks. In the side-table we can jump over these
  171. // nested blocks instead of doing a linear search if the next "sibling"
  172. // entry is not at a location greater than LastHashTokPtr.
  173. if (HashEntryI < LastHashTokPtr && TableIdx) {
  174. // In the side-table we are still at an entry for a '#' token that
  175. // is earlier than the last one we saw. Check if the location we would
  176. // stride gets us closer.
  177. const unsigned char* NextPPCondPtr =
  178. PPCond + TableIdx*(sizeof(uint32_t)*2);
  179. assert(NextPPCondPtr >= CurPPCondPtr);
  180. // Read where we should jump to.
  181. uint32_t TmpOffset = ReadLE32(NextPPCondPtr);
  182. const unsigned char* HashEntryJ = TokBuf + TmpOffset;
  183. if (HashEntryJ <= LastHashTokPtr) {
  184. // Jump directly to the next entry in the side table.
  185. HashEntryI = HashEntryJ;
  186. Offset = TmpOffset;
  187. TableIdx = ReadLE32(NextPPCondPtr);
  188. CurPPCondPtr = NextPPCondPtr;
  189. }
  190. }
  191. }
  192. while (HashEntryI < LastHashTokPtr);
  193. assert(HashEntryI == LastHashTokPtr && "No PP-cond entry found for '#'");
  194. assert(TableIdx && "No jumping from #endifs.");
  195. // Update our side-table iterator.
  196. const unsigned char* NextPPCondPtr = PPCond + TableIdx*(sizeof(uint32_t)*2);
  197. assert(NextPPCondPtr >= CurPPCondPtr);
  198. CurPPCondPtr = NextPPCondPtr;
  199. // Read where we should jump to.
  200. HashEntryI = TokBuf + ReadLE32(NextPPCondPtr);
  201. uint32_t NextIdx = ReadLE32(NextPPCondPtr);
  202. // By construction NextIdx will be zero if this is a #endif. This is useful
  203. // to know to obviate lexing another token.
  204. bool isEndif = NextIdx == 0;
  205. // This case can occur when we see something like this:
  206. //
  207. // #if ...
  208. // /* a comment or nothing */
  209. // #elif
  210. //
  211. // If we are skipping the first #if block it will be the case that CurPtr
  212. // already points 'elif'. Just return.
  213. if (CurPtr > HashEntryI) {
  214. assert(CurPtr == HashEntryI + DISK_TOKEN_SIZE);
  215. // Did we reach a #endif? If so, go ahead and consume that token as well.
  216. if (isEndif)
  217. CurPtr += DISK_TOKEN_SIZE*2;
  218. else
  219. LastHashTokPtr = HashEntryI;
  220. return isEndif;
  221. }
  222. // Otherwise, we need to advance. Update CurPtr to point to the '#' token.
  223. CurPtr = HashEntryI;
  224. // Update the location of the last observed '#'. This is useful if we
  225. // are skipping multiple blocks.
  226. LastHashTokPtr = CurPtr;
  227. // Skip the '#' token.
  228. assert(((tok::TokenKind)*CurPtr) == tok::hash);
  229. CurPtr += DISK_TOKEN_SIZE;
  230. // Did we reach a #endif? If so, go ahead and consume that token as well.
  231. if (isEndif) { CurPtr += DISK_TOKEN_SIZE*2; }
  232. return isEndif;
  233. }
  234. SourceLocation PTHLexer::getSourceLocation() {
  235. // getSourceLocation is not on the hot path. It is used to get the location
  236. // of the next token when transitioning back to this lexer when done
  237. // handling a #included file. Just read the necessary data from the token
  238. // data buffer to construct the SourceLocation object.
  239. // NOTE: This is a virtual function; hence it is defined out-of-line.
  240. const unsigned char *OffsetPtr = CurPtr + (DISK_TOKEN_SIZE - 4);
  241. uint32_t Offset = ReadLE32(OffsetPtr);
  242. return FileStartLoc.getFileLocWithOffset(Offset);
  243. }
  244. //===----------------------------------------------------------------------===//
  245. // PTH file lookup: map from strings to file data.
  246. //===----------------------------------------------------------------------===//
  247. /// PTHFileLookup - This internal data structure is used by the PTHManager
  248. /// to map from FileEntry objects managed by FileManager to offsets within
  249. /// the PTH file.
  250. namespace {
  251. class PTHFileData {
  252. const uint32_t TokenOff;
  253. const uint32_t PPCondOff;
  254. public:
  255. PTHFileData(uint32_t tokenOff, uint32_t ppCondOff)
  256. : TokenOff(tokenOff), PPCondOff(ppCondOff) {}
  257. uint32_t getTokenOffset() const { return TokenOff; }
  258. uint32_t getPPCondOffset() const { return PPCondOff; }
  259. };
  260. class PTHFileLookupCommonTrait {
  261. public:
  262. typedef std::pair<unsigned char, const char*> internal_key_type;
  263. static unsigned ComputeHash(internal_key_type x) {
  264. return llvm::HashString(x.second);
  265. }
  266. static std::pair<unsigned, unsigned>
  267. ReadKeyDataLength(const unsigned char*& d) {
  268. unsigned keyLen = (unsigned) ReadUnalignedLE16(d);
  269. unsigned dataLen = (unsigned) *(d++);
  270. return std::make_pair(keyLen, dataLen);
  271. }
  272. static internal_key_type ReadKey(const unsigned char* d, unsigned) {
  273. unsigned char k = *(d++); // Read the entry kind.
  274. return std::make_pair(k, (const char*) d);
  275. }
  276. };
  277. class PTHFileLookupTrait : public PTHFileLookupCommonTrait {
  278. public:
  279. typedef const FileEntry* external_key_type;
  280. typedef PTHFileData data_type;
  281. static internal_key_type GetInternalKey(const FileEntry* FE) {
  282. return std::make_pair((unsigned char) 0x1, FE->getName());
  283. }
  284. static bool EqualKey(internal_key_type a, internal_key_type b) {
  285. return a.first == b.first && strcmp(a.second, b.second) == 0;
  286. }
  287. static PTHFileData ReadData(const internal_key_type& k,
  288. const unsigned char* d, unsigned) {
  289. assert(k.first == 0x1 && "Only file lookups can match!");
  290. uint32_t x = ::ReadUnalignedLE32(d);
  291. uint32_t y = ::ReadUnalignedLE32(d);
  292. return PTHFileData(x, y);
  293. }
  294. };
  295. class PTHStringLookupTrait {
  296. public:
  297. typedef uint32_t
  298. data_type;
  299. typedef const std::pair<const char*, unsigned>
  300. external_key_type;
  301. typedef external_key_type internal_key_type;
  302. static bool EqualKey(const internal_key_type& a,
  303. const internal_key_type& b) {
  304. return (a.second == b.second) ? memcmp(a.first, b.first, a.second) == 0
  305. : false;
  306. }
  307. static unsigned ComputeHash(const internal_key_type& a) {
  308. return llvm::HashString(llvm::StringRef(a.first, a.second));
  309. }
  310. // This hopefully will just get inlined and removed by the optimizer.
  311. static const internal_key_type&
  312. GetInternalKey(const external_key_type& x) { return x; }
  313. static std::pair<unsigned, unsigned>
  314. ReadKeyDataLength(const unsigned char*& d) {
  315. return std::make_pair((unsigned) ReadUnalignedLE16(d), sizeof(uint32_t));
  316. }
  317. static std::pair<const char*, unsigned>
  318. ReadKey(const unsigned char* d, unsigned n) {
  319. assert(n >= 2 && d[n-1] == '\0');
  320. return std::make_pair((const char*) d, n-1);
  321. }
  322. static uint32_t ReadData(const internal_key_type& k, const unsigned char* d,
  323. unsigned) {
  324. return ::ReadUnalignedLE32(d);
  325. }
  326. };
  327. } // end anonymous namespace
  328. typedef OnDiskChainedHashTable<PTHFileLookupTrait> PTHFileLookup;
  329. typedef OnDiskChainedHashTable<PTHStringLookupTrait> PTHStringIdLookup;
  330. //===----------------------------------------------------------------------===//
  331. // PTHManager methods.
  332. //===----------------------------------------------------------------------===//
  333. PTHManager::PTHManager(const llvm::MemoryBuffer* buf, void* fileLookup,
  334. const unsigned char* idDataTable,
  335. IdentifierInfo** perIDCache,
  336. void* stringIdLookup, unsigned numIds,
  337. const unsigned char* spellingBase,
  338. const char* originalSourceFile)
  339. : Buf(buf), PerIDCache(perIDCache), FileLookup(fileLookup),
  340. IdDataTable(idDataTable), StringIdLookup(stringIdLookup),
  341. NumIds(numIds), PP(0), SpellingBase(spellingBase),
  342. OriginalSourceFile(originalSourceFile) {}
  343. PTHManager::~PTHManager() {
  344. delete Buf;
  345. delete (PTHFileLookup*) FileLookup;
  346. delete (PTHStringIdLookup*) StringIdLookup;
  347. free(PerIDCache);
  348. }
  349. static void InvalidPTH(Diagnostic &Diags, const char *Msg) {
  350. Diags.Report(Diags.getCustomDiagID(Diagnostic::Error, Msg));
  351. }
  352. PTHManager* PTHManager::Create(const std::string& file, FileManager &FileMgr,
  353. const FileSystemOptions &FSOpts,
  354. Diagnostic &Diags) {
  355. // Memory map the PTH file.
  356. llvm::OwningPtr<llvm::MemoryBuffer>
  357. File(FileMgr.getBufferForFile(file, FSOpts));
  358. if (!File) {
  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") + 3 + 4) ||
  368. memcmp(BufBeg, "cfe-pth", sizeof("cfe-pth") - 1) != 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") - 1);
  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. llvm::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. llvm::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 peristent 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(llvm::StringRef Name) {
  466. PTHStringIdLookup& SL = *((PTHStringIdLookup*)StringIdLookup);
  467. // Double check our assumption that the last character isn't '\0'.
  468. assert(Name.empty() || Name.data()[Name.size()-1] != '\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 StatSysCallCache {
  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. int stat(const char *path, struct stat *buf) {
  552. // Do the lookup for the file's data in the PTH file.
  553. CacheTy::iterator I = Cache.find(path);
  554. // If we don't get a hit in the PTH file just forward to 'stat'.
  555. if (I == Cache.end())
  556. return StatSysCallCache::stat(path, buf);
  557. const PTHStatData& Data = *I;
  558. if (!Data.hasStat)
  559. return 1;
  560. buf->st_ino = Data.ino;
  561. buf->st_dev = Data.dev;
  562. buf->st_mtime = Data.mtime;
  563. buf->st_mode = Data.mode;
  564. buf->st_size = Data.size;
  565. return 0;
  566. }
  567. };
  568. } // end anonymous namespace
  569. StatSysCallCache *PTHManager::createStatCache() {
  570. return new PTHStatCache(*((PTHFileLookup*) FileLookup));
  571. }