1274b20863a728cc6a31ee75c670e3733600c1531Ted Kremenek//===--- PTHLexer.cpp - Lex from a token stream ---------------------------===//
2274b20863a728cc6a31ee75c670e3733600c1531Ted Kremenek//
3274b20863a728cc6a31ee75c670e3733600c1531Ted Kremenek//                     The LLVM Compiler Infrastructure
4274b20863a728cc6a31ee75c670e3733600c1531Ted Kremenek//
5274b20863a728cc6a31ee75c670e3733600c1531Ted Kremenek// This file is distributed under the University of Illinois Open Source
6274b20863a728cc6a31ee75c670e3733600c1531Ted Kremenek// License. See LICENSE.TXT for details.
7274b20863a728cc6a31ee75c670e3733600c1531Ted Kremenek//
8274b20863a728cc6a31ee75c670e3733600c1531Ted Kremenek//===----------------------------------------------------------------------===//
9274b20863a728cc6a31ee75c670e3733600c1531Ted Kremenek//
10274b20863a728cc6a31ee75c670e3733600c1531Ted Kremenek// This file implements the PTHLexer interface.
11274b20863a728cc6a31ee75c670e3733600c1531Ted Kremenek//
12274b20863a728cc6a31ee75c670e3733600c1531Ted Kremenek//===----------------------------------------------------------------------===//
13274b20863a728cc6a31ee75c670e3733600c1531Ted Kremenek
1455fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/Lex/PTHLexer.h"
150c6a77bc1f52f282a969538f139ebde429076ed3Ted Kremenek#include "clang/Basic/FileManager.h"
1610e286aa8d39fb51a21412850265d9dae74613eeChris Lattner#include "clang/Basic/FileSystemStatCache.h"
170c6a77bc1f52f282a969538f139ebde429076ed3Ted Kremenek#include "clang/Basic/IdentifierTable.h"
1855fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/Basic/TokenKinds.h"
193574f46cf495ec61618fd6864b045c5b1d0d5068Daniel Dunbar#include "clang/Lex/LexDiagnostic.h"
200c6a77bc1f52f282a969538f139ebde429076ed3Ted Kremenek#include "clang/Lex/PTHManager.h"
210c6a77bc1f52f282a969538f139ebde429076ed3Ted Kremenek#include "clang/Lex/Preprocessor.h"
2255fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/Lex/Token.h"
232596e429a61602312bdd149786045b8a90cd2d10Daniel Dunbar#include "llvm/ADT/StringExtras.h"
242596e429a61602312bdd149786045b8a90cd2d10Daniel Dunbar#include "llvm/ADT/StringMap.h"
25651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines#include "llvm/Support/EndianStream.h"
266f78c3b8b9343e7e9fbf2d457cccf00df6da5d47Chris Lattner#include "llvm/Support/MemoryBuffer.h"
276bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines#include "llvm/Support/OnDiskHashTable.h"
28651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines#include <memory>
29ef8225444452a1486bd721f3285301fe84643b00Stephen Hines#include <system_error>
30274b20863a728cc6a31ee75c670e3733600c1531Ted Kremenekusing namespace clang;
31274b20863a728cc6a31ee75c670e3733600c1531Ted Kremenek
32ef8225444452a1486bd721f3285301fe84643b00Stephen Hinesstatic const unsigned StoredTokenSize = 1 + 1 + 2 + 4 + 4;
33268ee7016a2811803989487c0ad3799486092c63Ted Kremenek
34e5680f3cd678014cf0872d34726dc804b0cbbdd4Ted Kremenek//===----------------------------------------------------------------------===//
35e5680f3cd678014cf0872d34726dc804b0cbbdd4Ted Kremenek// PTHLexer methods.
36e5680f3cd678014cf0872d34726dc804b0cbbdd4Ted Kremenek//===----------------------------------------------------------------------===//
37e5680f3cd678014cf0872d34726dc804b0cbbdd4Ted Kremenek
38da9d61c96c412f6babc7f824152609562f302388Chris LattnerPTHLexer::PTHLexer(Preprocessor &PP, FileID FID, const unsigned char *D,
39277faca30c9f8f72b79f55695cbe3395ec246e7cTed Kremenek                   const unsigned char *ppcond, PTHManager &PM)
406bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  : PreprocessorLexer(&PP, FID), TokBuf(D), CurPtr(D), LastHashTokPtr(nullptr),
41277faca30c9f8f72b79f55695cbe3395ec246e7cTed Kremenek    PPCond(ppcond), CurPPCondPtr(ppcond), PTHMgr(PM) {
421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
432b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  FileStartLoc = PP.getSourceManager().getLocForStartOfFile(FID);
445f074266cc59563036c40516c814d63825723e20Ted Kremenek}
45274b20863a728cc6a31ee75c670e3733600c1531Ted Kremenek
46d2f9308220af22bfc1bcd3bc2cad118dbd8be013Eli Friedmanbool PTHLexer::Lex(Token& Tok) {
47866bdf74547efe32c320554837ffce00fcc084feTed Kremenek  //===--------------------------------------==//
48866bdf74547efe32c320554837ffce00fcc084feTed Kremenek  // Read the raw token data.
49866bdf74547efe32c320554837ffce00fcc084feTed Kremenek  //===--------------------------------------==//
50651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  using namespace llvm::support;
511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
52866bdf74547efe32c320554837ffce00fcc084feTed Kremenek  // Shadow CurPtr into an automatic variable.
531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  const unsigned char *CurPtrShadow = CurPtr;
54866bdf74547efe32c320554837ffce00fcc084feTed Kremenek
551b5285e1ba31975864da356b2ed927e87670e654Chris Lattner  // Read in the data for the token.
56651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  unsigned Word0 = endian::readNext<uint32_t, little, aligned>(CurPtrShadow);
57651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  uint32_t IdentifierID =
58651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      endian::readNext<uint32_t, little, aligned>(CurPtrShadow);
59651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  uint32_t FileOffset =
60651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      endian::readNext<uint32_t, little, aligned>(CurPtrShadow);
611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
627b78b7c6d96deb1e63f8d0655ee6fa53de0b65efTed Kremenek  tok::TokenKind TKind = (tok::TokenKind) (Word0 & 0xFF);
637b78b7c6d96deb1e63f8d0655ee6fa53de0b65efTed Kremenek  Token::TokenFlags TFlags = (Token::TokenFlags) ((Word0 >> 8) & 0xFF);
64aff6ef8e7bc3c3739f984c390e0af693e60be064Chris Lattner  uint32_t Len = Word0 >> 16;
657b78b7c6d96deb1e63f8d0655ee6fa53de0b65efTed Kremenek
66aff6ef8e7bc3c3739f984c390e0af693e60be064Chris Lattner  CurPtr = CurPtrShadow;
671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
68866bdf74547efe32c320554837ffce00fcc084feTed Kremenek  //===--------------------------------------==//
69866bdf74547efe32c320554837ffce00fcc084feTed Kremenek  // Construct the token itself.
70866bdf74547efe32c320554837ffce00fcc084feTed Kremenek  //===--------------------------------------==//
711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
72866bdf74547efe32c320554837ffce00fcc084feTed Kremenek  Tok.startToken();
73898a0bb1972efb6e03cb1151412ec7392cef07deChris Lattner  Tok.setKind(TKind);
74898a0bb1972efb6e03cb1151412ec7392cef07deChris Lattner  Tok.setFlag(TFlags);
7559d08cb672136322375e5400578ee1fbd0947de2Ted Kremenek  assert(!LexingRawMode);
76a64ccefdf0ea4e03ec88805d71b0af74950c7472Argyrios Kyrtzidis  Tok.setLocation(FileStartLoc.getLocWithOffset(FileOffset));
77866bdf74547efe32c320554837ffce00fcc084feTed Kremenek  Tok.setLength(Len);
7889d7ee9619d2dbdfa8d956a695c612a104a92cadTed Kremenek
79d0a69696acca62798dfc8b98f97c92bfa7fa0490Chris Lattner  // Handle identifiers.
80277faca30c9f8f72b79f55695cbe3395ec246e7cTed Kremenek  if (Tok.isLiteral()) {
81277faca30c9f8f72b79f55695cbe3395ec246e7cTed Kremenek    Tok.setLiteralData((const char*) (PTHMgr.SpellingBase + IdentifierID));
82277faca30c9f8f72b79f55695cbe3395ec246e7cTed Kremenek  }
83277faca30c9f8f72b79f55695cbe3395ec246e7cTed Kremenek  else if (IdentifierID) {
84d0a69696acca62798dfc8b98f97c92bfa7fa0490Chris Lattner    MIOpt.ReadToken();
85d0a69696acca62798dfc8b98f97c92bfa7fa0490Chris Lattner    IdentifierInfo *II = PTHMgr.GetIdentifierInfo(IdentifierID-1);
861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
87d0a69696acca62798dfc8b98f97c92bfa7fa0490Chris Lattner    Tok.setIdentifierInfo(II);
881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
89863c486fcb6162495a94fddf7ac8409de2638995Chris Lattner    // Change the kind of this identifier to the appropriate token kind, e.g.
90863c486fcb6162495a94fddf7ac8409de2638995Chris Lattner    // turning "for" into a keyword.
91863c486fcb6162495a94fddf7ac8409de2638995Chris Lattner    Tok.setKind(II->getTokenID());
921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
93d0a69696acca62798dfc8b98f97c92bfa7fa0490Chris Lattner    if (II->isHandleIdentifierCase())
94d2f9308220af22bfc1bcd3bc2cad118dbd8be013Eli Friedman      return PP->HandleIdentifier(Tok);
95d2f9308220af22bfc1bcd3bc2cad118dbd8be013Eli Friedman
96d2f9308220af22bfc1bcd3bc2cad118dbd8be013Eli Friedman    return true;
97d0a69696acca62798dfc8b98f97c92bfa7fa0490Chris Lattner  }
981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
99866bdf74547efe32c320554837ffce00fcc084feTed Kremenek  //===--------------------------------------==//
100866bdf74547efe32c320554837ffce00fcc084feTed Kremenek  // Process the token.
101866bdf74547efe32c320554837ffce00fcc084feTed Kremenek  //===--------------------------------------==//
102898a0bb1972efb6e03cb1151412ec7392cef07deChris Lattner  if (TKind == tok::eof) {
103e5680f3cd678014cf0872d34726dc804b0cbbdd4Ted Kremenek    // Save the end-of-file token.
104e5680f3cd678014cf0872d34726dc804b0cbbdd4Ted Kremenek    EofToken = Tok;
1051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10659d08cb672136322375e5400578ee1fbd0947de2Ted Kremenek    assert(!ParsingPreprocessorDirective);
10759d08cb672136322375e5400578ee1fbd0947de2Ted Kremenek    assert(!LexingRawMode);
1081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
109d2f9308220af22bfc1bcd3bc2cad118dbd8be013Eli Friedman    return LexEndOfFile(Tok);
110cd4e2aecde5bb7810715d5d5a88ac63ce7946f34Ted Kremenek  }
1111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
112898a0bb1972efb6e03cb1151412ec7392cef07deChris Lattner  if (TKind == tok::hash && Tok.isAtStartOfLine()) {
113ef8225444452a1486bd721f3285301fe84643b00Stephen Hines    LastHashTokPtr = CurPtr - StoredTokenSize;
11459d08cb672136322375e5400578ee1fbd0947de2Ted Kremenek    assert(!LexingRawMode);
11559d08cb672136322375e5400578ee1fbd0947de2Ted Kremenek    PP->HandleDirective(Tok);
1161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
117d2f9308220af22bfc1bcd3bc2cad118dbd8be013Eli Friedman    return false;
118e5680f3cd678014cf0872d34726dc804b0cbbdd4Ted Kremenek  }
1191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12084021556baceb76eedf7d44be8ba71d9b8cfaccePeter Collingbourne  if (TKind == tok::eod) {
12159d08cb672136322375e5400578ee1fbd0947de2Ted Kremenek    assert(ParsingPreprocessorDirective);
12259d08cb672136322375e5400578ee1fbd0947de2Ted Kremenek    ParsingPreprocessorDirective = false;
123d2f9308220af22bfc1bcd3bc2cad118dbd8be013Eli Friedman    return true;
12459d08cb672136322375e5400578ee1fbd0947de2Ted Kremenek  }
125274b20863a728cc6a31ee75c670e3733600c1531Ted Kremenek
12659d08cb672136322375e5400578ee1fbd0947de2Ted Kremenek  MIOpt.ReadToken();
127d2f9308220af22bfc1bcd3bc2cad118dbd8be013Eli Friedman  return true;
128cd4e2aecde5bb7810715d5d5a88ac63ce7946f34Ted Kremenek}
129cd4e2aecde5bb7810715d5d5a88ac63ce7946f34Ted Kremenek
13094e3d1f0349634235606be77c08b3a48a6d563d6Ted Kremenekbool PTHLexer::LexEndOfFile(Token &Result) {
13194e3d1f0349634235606be77c08b3a48a6d563d6Ted Kremenek  // If we hit the end of the file while parsing a preprocessor directive,
13294e3d1f0349634235606be77c08b3a48a6d563d6Ted Kremenek  // end the preprocessor directive first.  The next token returned will
13394e3d1f0349634235606be77c08b3a48a6d563d6Ted Kremenek  // then be the end of file.
13494e3d1f0349634235606be77c08b3a48a6d563d6Ted Kremenek  if (ParsingPreprocessorDirective) {
13594e3d1f0349634235606be77c08b3a48a6d563d6Ted Kremenek    ParsingPreprocessorDirective = false; // Done parsing the "line".
13694e3d1f0349634235606be77c08b3a48a6d563d6Ted Kremenek    return true;  // Have a token.
13794e3d1f0349634235606be77c08b3a48a6d563d6Ted Kremenek  }
13894e3d1f0349634235606be77c08b3a48a6d563d6Ted Kremenek
13994e3d1f0349634235606be77c08b3a48a6d563d6Ted Kremenek  assert(!LexingRawMode);
14094e3d1f0349634235606be77c08b3a48a6d563d6Ted Kremenek
14194e3d1f0349634235606be77c08b3a48a6d563d6Ted Kremenek  // If we are in a #if directive, emit an error.
14294e3d1f0349634235606be77c08b3a48a6d563d6Ted Kremenek  while (!ConditionalStack.empty()) {
1437d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    if (PP->getCodeCompletionFileLoc() != FileStartLoc)
1442d474ba9e8ae43a1a5a9f72718c0d79092b9453fDouglas Gregor      PP->Diag(ConditionalStack.back().IfLoc,
1452d474ba9e8ae43a1a5a9f72718c0d79092b9453fDouglas Gregor               diag::err_pp_unterminated_conditional);
14694e3d1f0349634235606be77c08b3a48a6d563d6Ted Kremenek    ConditionalStack.pop_back();
14794e3d1f0349634235606be77c08b3a48a6d563d6Ted Kremenek  }
14894e3d1f0349634235606be77c08b3a48a6d563d6Ted Kremenek
14994e3d1f0349634235606be77c08b3a48a6d563d6Ted Kremenek  // Finally, let the preprocessor handle this.
15094e3d1f0349634235606be77c08b3a48a6d563d6Ted Kremenek  return PP->HandleEndOfFile(Result);
15194e3d1f0349634235606be77c08b3a48a6d563d6Ted Kremenek}
15294e3d1f0349634235606be77c08b3a48a6d563d6Ted Kremenek
153e5680f3cd678014cf0872d34726dc804b0cbbdd4Ted Kremenek// FIXME: We can just grab the last token instead of storing a copy
154e5680f3cd678014cf0872d34726dc804b0cbbdd4Ted Kremenek// into EofToken.
15559d08cb672136322375e5400578ee1fbd0947de2Ted Kremenekvoid PTHLexer::getEOF(Token& Tok) {
156defb7094c835998bb821e894253287625ce8c74dTed Kremenek  assert(EofToken.is(tok::eof));
1570c6a77bc1f52f282a969538f139ebde429076ed3Ted Kremenek  Tok = EofToken;
158274b20863a728cc6a31ee75c670e3733600c1531Ted Kremenek}
15917ff58a63197b398ae52697b088dc0fb8b255519Ted Kremenek
16017ff58a63197b398ae52697b088dc0fb8b255519Ted Kremenekvoid PTHLexer::DiscardToEndOfLine() {
16117ff58a63197b398ae52697b088dc0fb8b255519Ted Kremenek  assert(ParsingPreprocessorDirective && ParsingFilename == false &&
16217ff58a63197b398ae52697b088dc0fb8b255519Ted Kremenek         "Must be in a preprocessing directive!");
1634d35da2e41941965bbee8ed7e8c30e7c21000d71Ted Kremenek
164e5680f3cd678014cf0872d34726dc804b0cbbdd4Ted Kremenek  // We assume that if the preprocessor wishes to discard to the end of
165e5680f3cd678014cf0872d34726dc804b0cbbdd4Ted Kremenek  // the line that it also means to end the current preprocessor directive.
166e5680f3cd678014cf0872d34726dc804b0cbbdd4Ted Kremenek  ParsingPreprocessorDirective = false;
1671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16874c3e6e5e95af08096aab415d1ce15f15ffff02aTed Kremenek  // Skip tokens by only peeking at their token kind and the flags.
16974c3e6e5e95af08096aab415d1ce15f15ffff02aTed Kremenek  // We don't need to actually reconstruct full tokens from the token buffer.
17074c3e6e5e95af08096aab415d1ce15f15ffff02aTed Kremenek  // This saves some copies and it also reduces IdentifierInfo* lookup.
171da9d61c96c412f6babc7f824152609562f302388Chris Lattner  const unsigned char* p = CurPtr;
17274c3e6e5e95af08096aab415d1ce15f15ffff02aTed Kremenek  while (1) {
17374c3e6e5e95af08096aab415d1ce15f15ffff02aTed Kremenek    // Read the token kind.  Are we at the end of the file?
17474c3e6e5e95af08096aab415d1ce15f15ffff02aTed Kremenek    tok::TokenKind x = (tok::TokenKind) (uint8_t) *p;
17574c3e6e5e95af08096aab415d1ce15f15ffff02aTed Kremenek    if (x == tok::eof) break;
1761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
17774c3e6e5e95af08096aab415d1ce15f15ffff02aTed Kremenek    // Read the token flags.  Are we at the start of the next line?
17874c3e6e5e95af08096aab415d1ce15f15ffff02aTed Kremenek    Token::TokenFlags y = (Token::TokenFlags) (uint8_t) p[1];
17980d2f3059326f99ebf7c867db1c7f106ec9485f5Ted Kremenek    if (y & Token::StartOfLine) break;
18074c3e6e5e95af08096aab415d1ce15f15ffff02aTed Kremenek
18174c3e6e5e95af08096aab415d1ce15f15ffff02aTed Kremenek    // Skip to the next token.
182ef8225444452a1486bd721f3285301fe84643b00Stephen Hines    p += StoredTokenSize;
18374c3e6e5e95af08096aab415d1ce15f15ffff02aTed Kremenek  }
1841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
18574c3e6e5e95af08096aab415d1ce15f15ffff02aTed Kremenek  CurPtr = p;
18617ff58a63197b398ae52697b088dc0fb8b255519Ted Kremenek}
1870c6a77bc1f52f282a969538f139ebde429076ed3Ted Kremenek
188268ee7016a2811803989487c0ad3799486092c63Ted Kremenek/// SkipBlock - Used by Preprocessor to skip the current conditional block.
189268ee7016a2811803989487c0ad3799486092c63Ted Kremenekbool PTHLexer::SkipBlock() {
190651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  using namespace llvm::support;
191268ee7016a2811803989487c0ad3799486092c63Ted Kremenek  assert(CurPPCondPtr && "No cached PP conditional information.");
192268ee7016a2811803989487c0ad3799486092c63Ted Kremenek  assert(LastHashTokPtr && "No known '#' token.");
1931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1946bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  const unsigned char *HashEntryI = nullptr;
195268ee7016a2811803989487c0ad3799486092c63Ted Kremenek  uint32_t TableIdx;
1961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
197268ee7016a2811803989487c0ad3799486092c63Ted Kremenek  do {
19841a2660377d215d004fe413c03874bd066b5384cTed Kremenek    // Read the token offset from the side-table.
199651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    uint32_t Offset = endian::readNext<uint32_t, little, aligned>(CurPPCondPtr);
2001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // Read the target table index from the side-table.
202651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    TableIdx = endian::readNext<uint32_t, little, aligned>(CurPPCondPtr);
2031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20441a2660377d215d004fe413c03874bd066b5384cTed Kremenek    // Compute the actual memory address of the '#' token data for this entry.
20541a2660377d215d004fe413c03874bd066b5384cTed Kremenek    HashEntryI = TokBuf + Offset;
20641a2660377d215d004fe413c03874bd066b5384cTed Kremenek
20741a2660377d215d004fe413c03874bd066b5384cTed Kremenek    // Optmization: "Sibling jumping".  #if...#else...#endif blocks can
20841a2660377d215d004fe413c03874bd066b5384cTed Kremenek    //  contain nested blocks.  In the side-table we can jump over these
20941a2660377d215d004fe413c03874bd066b5384cTed Kremenek    //  nested blocks instead of doing a linear search if the next "sibling"
21041a2660377d215d004fe413c03874bd066b5384cTed Kremenek    //  entry is not at a location greater than LastHashTokPtr.
21141a2660377d215d004fe413c03874bd066b5384cTed Kremenek    if (HashEntryI < LastHashTokPtr && TableIdx) {
21241a2660377d215d004fe413c03874bd066b5384cTed Kremenek      // In the side-table we are still at an entry for a '#' token that
21341a2660377d215d004fe413c03874bd066b5384cTed Kremenek      // is earlier than the last one we saw.  Check if the location we would
21441a2660377d215d004fe413c03874bd066b5384cTed Kremenek      // stride gets us closer.
215da9d61c96c412f6babc7f824152609562f302388Chris Lattner      const unsigned char* NextPPCondPtr =
216da9d61c96c412f6babc7f824152609562f302388Chris Lattner        PPCond + TableIdx*(sizeof(uint32_t)*2);
21741a2660377d215d004fe413c03874bd066b5384cTed Kremenek      assert(NextPPCondPtr >= CurPPCondPtr);
21841a2660377d215d004fe413c03874bd066b5384cTed Kremenek      // Read where we should jump to.
219651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      const unsigned char *HashEntryJ =
220651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines          TokBuf + endian::readNext<uint32_t, little, aligned>(NextPPCondPtr);
2211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22241a2660377d215d004fe413c03874bd066b5384cTed Kremenek      if (HashEntryJ <= LastHashTokPtr) {
22341a2660377d215d004fe413c03874bd066b5384cTed Kremenek        // Jump directly to the next entry in the side table.
22441a2660377d215d004fe413c03874bd066b5384cTed Kremenek        HashEntryI = HashEntryJ;
225651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        TableIdx = endian::readNext<uint32_t, little, aligned>(NextPPCondPtr);
22641a2660377d215d004fe413c03874bd066b5384cTed Kremenek        CurPPCondPtr = NextPPCondPtr;
22741a2660377d215d004fe413c03874bd066b5384cTed Kremenek      }
22841a2660377d215d004fe413c03874bd066b5384cTed Kremenek    }
229268ee7016a2811803989487c0ad3799486092c63Ted Kremenek  }
2301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  while (HashEntryI < LastHashTokPtr);
23141a2660377d215d004fe413c03874bd066b5384cTed Kremenek  assert(HashEntryI == LastHashTokPtr && "No PP-cond entry found for '#'");
232268ee7016a2811803989487c0ad3799486092c63Ted Kremenek  assert(TableIdx && "No jumping from #endifs.");
2331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
234268ee7016a2811803989487c0ad3799486092c63Ted Kremenek  // Update our side-table iterator.
235da9d61c96c412f6babc7f824152609562f302388Chris Lattner  const unsigned char* NextPPCondPtr = PPCond + TableIdx*(sizeof(uint32_t)*2);
236268ee7016a2811803989487c0ad3799486092c63Ted Kremenek  assert(NextPPCondPtr >= CurPPCondPtr);
237268ee7016a2811803989487c0ad3799486092c63Ted Kremenek  CurPPCondPtr = NextPPCondPtr;
2381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
239268ee7016a2811803989487c0ad3799486092c63Ted Kremenek  // Read where we should jump to.
240651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  HashEntryI =
241651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      TokBuf + endian::readNext<uint32_t, little, aligned>(NextPPCondPtr);
242651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  uint32_t NextIdx = endian::readNext<uint32_t, little, aligned>(NextPPCondPtr);
2431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
244268ee7016a2811803989487c0ad3799486092c63Ted Kremenek  // By construction NextIdx will be zero if this is a #endif.  This is useful
245268ee7016a2811803989487c0ad3799486092c63Ted Kremenek  // to know to obviate lexing another token.
246268ee7016a2811803989487c0ad3799486092c63Ted Kremenek  bool isEndif = NextIdx == 0;
2471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
248268ee7016a2811803989487c0ad3799486092c63Ted Kremenek  // This case can occur when we see something like this:
249268ee7016a2811803989487c0ad3799486092c63Ted Kremenek  //
250268ee7016a2811803989487c0ad3799486092c63Ted Kremenek  //  #if ...
251268ee7016a2811803989487c0ad3799486092c63Ted Kremenek  //   /* a comment or nothing */
252268ee7016a2811803989487c0ad3799486092c63Ted Kremenek  //  #elif
253268ee7016a2811803989487c0ad3799486092c63Ted Kremenek  //
254268ee7016a2811803989487c0ad3799486092c63Ted Kremenek  // If we are skipping the first #if block it will be the case that CurPtr
255268ee7016a2811803989487c0ad3799486092c63Ted Kremenek  // already points 'elif'.  Just return.
2561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
25741a2660377d215d004fe413c03874bd066b5384cTed Kremenek  if (CurPtr > HashEntryI) {
258ef8225444452a1486bd721f3285301fe84643b00Stephen Hines    assert(CurPtr == HashEntryI + StoredTokenSize);
259268ee7016a2811803989487c0ad3799486092c63Ted Kremenek    // Did we reach a #endif?  If so, go ahead and consume that token as well.
260268ee7016a2811803989487c0ad3799486092c63Ted Kremenek    if (isEndif)
261ef8225444452a1486bd721f3285301fe84643b00Stephen Hines      CurPtr += StoredTokenSize * 2;
262268ee7016a2811803989487c0ad3799486092c63Ted Kremenek    else
26341a2660377d215d004fe413c03874bd066b5384cTed Kremenek      LastHashTokPtr = HashEntryI;
2641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
265268ee7016a2811803989487c0ad3799486092c63Ted Kremenek    return isEndif;
266268ee7016a2811803989487c0ad3799486092c63Ted Kremenek  }
267268ee7016a2811803989487c0ad3799486092c63Ted Kremenek
268268ee7016a2811803989487c0ad3799486092c63Ted Kremenek  // Otherwise, we need to advance.  Update CurPtr to point to the '#' token.
26941a2660377d215d004fe413c03874bd066b5384cTed Kremenek  CurPtr = HashEntryI;
2701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
271268ee7016a2811803989487c0ad3799486092c63Ted Kremenek  // Update the location of the last observed '#'.  This is useful if we
272268ee7016a2811803989487c0ad3799486092c63Ted Kremenek  // are skipping multiple blocks.
273268ee7016a2811803989487c0ad3799486092c63Ted Kremenek  LastHashTokPtr = CurPtr;
274268ee7016a2811803989487c0ad3799486092c63Ted Kremenek
275e5680f3cd678014cf0872d34726dc804b0cbbdd4Ted Kremenek  // Skip the '#' token.
276da9d61c96c412f6babc7f824152609562f302388Chris Lattner  assert(((tok::TokenKind)*CurPtr) == tok::hash);
277ef8225444452a1486bd721f3285301fe84643b00Stephen Hines  CurPtr += StoredTokenSize;
2781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
279268ee7016a2811803989487c0ad3799486092c63Ted Kremenek  // Did we reach a #endif?  If so, go ahead and consume that token as well.
280ef8225444452a1486bd721f3285301fe84643b00Stephen Hines  if (isEndif) {
281ef8225444452a1486bd721f3285301fe84643b00Stephen Hines    CurPtr += StoredTokenSize * 2;
282ef8225444452a1486bd721f3285301fe84643b00Stephen Hines  }
283268ee7016a2811803989487c0ad3799486092c63Ted Kremenek
284268ee7016a2811803989487c0ad3799486092c63Ted Kremenek  return isEndif;
285268ee7016a2811803989487c0ad3799486092c63Ted Kremenek}
286268ee7016a2811803989487c0ad3799486092c63Ted Kremenek
28730a12ec2a7f331d9e08acabe7cda853aaa7ba54bTed KremenekSourceLocation PTHLexer::getSourceLocation() {
2881b5285e1ba31975864da356b2ed927e87670e654Chris Lattner  // getSourceLocation is not on the hot path.  It is used to get the location
2891b5285e1ba31975864da356b2ed927e87670e654Chris Lattner  // of the next token when transitioning back to this lexer when done
29030a12ec2a7f331d9e08acabe7cda853aaa7ba54bTed Kremenek  // handling a #included file.  Just read the necessary data from the token
29130a12ec2a7f331d9e08acabe7cda853aaa7ba54bTed Kremenek  // data buffer to construct the SourceLocation object.
29230a12ec2a7f331d9e08acabe7cda853aaa7ba54bTed Kremenek  // NOTE: This is a virtual function; hence it is defined out-of-line.
293651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  using namespace llvm::support;
294651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
295ef8225444452a1486bd721f3285301fe84643b00Stephen Hines  const unsigned char *OffsetPtr = CurPtr + (StoredTokenSize - 4);
296651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  uint32_t Offset = endian::readNext<uint32_t, little, aligned>(OffsetPtr);
297a64ccefdf0ea4e03ec88805d71b0af74950c7472Argyrios Kyrtzidis  return FileStartLoc.getLocWithOffset(Offset);
29830a12ec2a7f331d9e08acabe7cda853aaa7ba54bTed Kremenek}
29930a12ec2a7f331d9e08acabe7cda853aaa7ba54bTed Kremenek
3005f074266cc59563036c40516c814d63825723e20Ted Kremenek//===----------------------------------------------------------------------===//
301d8c02929fe70f03111be73e7b8c402c724238ee9Ted Kremenek// PTH file lookup: map from strings to file data.
302d8c02929fe70f03111be73e7b8c402c724238ee9Ted Kremenek//===----------------------------------------------------------------------===//
3030c6a77bc1f52f282a969538f139ebde429076ed3Ted Kremenek
3040c6a77bc1f52f282a969538f139ebde429076ed3Ted Kremenek/// PTHFileLookup - This internal data structure is used by the PTHManager
3050c6a77bc1f52f282a969538f139ebde429076ed3Ted Kremenek///  to map from FileEntry objects managed by FileManager to offsets within
3060c6a77bc1f52f282a969538f139ebde429076ed3Ted Kremenek///  the PTH file.
3070c6a77bc1f52f282a969538f139ebde429076ed3Ted Kremeneknamespace {
30885b4521e34dcd4a0a4a1f0819e1123128e5a3125Benjamin Kramerclass PTHFileData {
309d8c02929fe70f03111be73e7b8c402c724238ee9Ted Kremenek  const uint32_t TokenOff;
310d8c02929fe70f03111be73e7b8c402c724238ee9Ted Kremenek  const uint32_t PPCondOff;
3110c6a77bc1f52f282a969538f139ebde429076ed3Ted Kremenekpublic:
312d8c02929fe70f03111be73e7b8c402c724238ee9Ted Kremenek  PTHFileData(uint32_t tokenOff, uint32_t ppCondOff)
313d8c02929fe70f03111be73e7b8c402c724238ee9Ted Kremenek    : TokenOff(tokenOff), PPCondOff(ppCondOff) {}
3141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  uint32_t getTokenOffset() const { return TokenOff; }
3161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  uint32_t getPPCondOffset() const { return PPCondOff; }
317d8c02929fe70f03111be73e7b8c402c724238ee9Ted Kremenek};
3181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
32085b4521e34dcd4a0a4a1f0819e1123128e5a3125Benjamin Kramerclass PTHFileLookupCommonTrait {
3210c6a77bc1f52f282a969538f139ebde429076ed3Ted Kremenekpublic:
322a4b44dd9d30929a35c44d85102e5241ee847b2f2Ted Kremenek  typedef std::pair<unsigned char, const char*> internal_key_type;
3236bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  typedef unsigned hash_value_type;
3246bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  typedef unsigned offset_type;
325a4b44dd9d30929a35c44d85102e5241ee847b2f2Ted Kremenek
3266bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  static hash_value_type ComputeHash(internal_key_type x) {
3272596e429a61602312bdd149786045b8a90cd2d10Daniel Dunbar    return llvm::HashString(x.second);
328d8c02929fe70f03111be73e7b8c402c724238ee9Ted Kremenek  }
3291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
330d8c02929fe70f03111be73e7b8c402c724238ee9Ted Kremenek  static std::pair<unsigned, unsigned>
331d8c02929fe70f03111be73e7b8c402c724238ee9Ted Kremenek  ReadKeyDataLength(const unsigned char*& d) {
332651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    using namespace llvm::support;
333651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    unsigned keyLen =
334651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        (unsigned)endian::readNext<uint16_t, little, unaligned>(d);
335a4b44dd9d30929a35c44d85102e5241ee847b2f2Ted Kremenek    unsigned dataLen = (unsigned) *(d++);
336a4b44dd9d30929a35c44d85102e5241ee847b2f2Ted Kremenek    return std::make_pair(keyLen, dataLen);
3370c6a77bc1f52f282a969538f139ebde429076ed3Ted Kremenek  }
3381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
339a4b44dd9d30929a35c44d85102e5241ee847b2f2Ted Kremenek  static internal_key_type ReadKey(const unsigned char* d, unsigned) {
340a4b44dd9d30929a35c44d85102e5241ee847b2f2Ted Kremenek    unsigned char k = *(d++); // Read the entry kind.
341a4b44dd9d30929a35c44d85102e5241ee847b2f2Ted Kremenek    return std::make_pair(k, (const char*) d);
342d8c02929fe70f03111be73e7b8c402c724238ee9Ted Kremenek  }
343337edcdbec05316b407d0d64865c88ff8597d910Ted Kremenek};
3441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
34585b4521e34dcd4a0a4a1f0819e1123128e5a3125Benjamin Kramerclass PTHFileLookupTrait : public PTHFileLookupCommonTrait {
346337edcdbec05316b407d0d64865c88ff8597d910Ted Kremenekpublic:
347337edcdbec05316b407d0d64865c88ff8597d910Ted Kremenek  typedef const FileEntry* external_key_type;
348337edcdbec05316b407d0d64865c88ff8597d910Ted Kremenek  typedef PTHFileData      data_type;
3491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
350a4b44dd9d30929a35c44d85102e5241ee847b2f2Ted Kremenek  static internal_key_type GetInternalKey(const FileEntry* FE) {
351a4b44dd9d30929a35c44d85102e5241ee847b2f2Ted Kremenek    return std::make_pair((unsigned char) 0x1, FE->getName());
352337edcdbec05316b407d0d64865c88ff8597d910Ted Kremenek  }
353a4b44dd9d30929a35c44d85102e5241ee847b2f2Ted Kremenek
354a4b44dd9d30929a35c44d85102e5241ee847b2f2Ted Kremenek  static bool EqualKey(internal_key_type a, internal_key_type b) {
355a4b44dd9d30929a35c44d85102e5241ee847b2f2Ted Kremenek    return a.first == b.first && strcmp(a.second, b.second) == 0;
3561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
3571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  static PTHFileData ReadData(const internal_key_type& k,
3591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                              const unsigned char* d, unsigned) {
360a4b44dd9d30929a35c44d85102e5241ee847b2f2Ted Kremenek    assert(k.first == 0x1 && "Only file lookups can match!");
361651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    using namespace llvm::support;
362651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    uint32_t x = endian::readNext<uint32_t, little, unaligned>(d);
363651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    uint32_t y = endian::readNext<uint32_t, little, unaligned>(d);
3641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    return PTHFileData(x, y);
3650c6a77bc1f52f282a969538f139ebde429076ed3Ted Kremenek  }
3660c6a77bc1f52f282a969538f139ebde429076ed3Ted Kremenek};
3677e3a004c6ed1fe87912203b9c5a113f8da89d261Ted Kremenek
36885b4521e34dcd4a0a4a1f0819e1123128e5a3125Benjamin Kramerclass PTHStringLookupTrait {
3697e3a004c6ed1fe87912203b9c5a113f8da89d261Ted Kremenekpublic:
3706bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  typedef uint32_t data_type;
3716bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  typedef const std::pair<const char*, unsigned> external_key_type;
3727e3a004c6ed1fe87912203b9c5a113f8da89d261Ted Kremenek  typedef external_key_type internal_key_type;
3736bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  typedef uint32_t hash_value_type;
3746bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  typedef unsigned offset_type;
3751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3767e3a004c6ed1fe87912203b9c5a113f8da89d261Ted Kremenek  static bool EqualKey(const internal_key_type& a,
3777e3a004c6ed1fe87912203b9c5a113f8da89d261Ted Kremenek                       const internal_key_type& b) {
3787e3a004c6ed1fe87912203b9c5a113f8da89d261Ted Kremenek    return (a.second == b.second) ? memcmp(a.first, b.first, a.second) == 0
3797e3a004c6ed1fe87912203b9c5a113f8da89d261Ted Kremenek                                  : false;
3807e3a004c6ed1fe87912203b9c5a113f8da89d261Ted Kremenek  }
3811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3826bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  static hash_value_type ComputeHash(const internal_key_type& a) {
3835f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    return llvm::HashString(StringRef(a.first, a.second));
3847e3a004c6ed1fe87912203b9c5a113f8da89d261Ted Kremenek  }
3851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3867e3a004c6ed1fe87912203b9c5a113f8da89d261Ted Kremenek  // This hopefully will just get inlined and removed by the optimizer.
3877e3a004c6ed1fe87912203b9c5a113f8da89d261Ted Kremenek  static const internal_key_type&
3887e3a004c6ed1fe87912203b9c5a113f8da89d261Ted Kremenek  GetInternalKey(const external_key_type& x) { return x; }
3891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3907e3a004c6ed1fe87912203b9c5a113f8da89d261Ted Kremenek  static std::pair<unsigned, unsigned>
3917e3a004c6ed1fe87912203b9c5a113f8da89d261Ted Kremenek  ReadKeyDataLength(const unsigned char*& d) {
392651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    using namespace llvm::support;
393651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    return std::make_pair(
394651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        (unsigned)endian::readNext<uint16_t, little, unaligned>(d),
395651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        sizeof(uint32_t));
3967e3a004c6ed1fe87912203b9c5a113f8da89d261Ted Kremenek  }
3971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3987e3a004c6ed1fe87912203b9c5a113f8da89d261Ted Kremenek  static std::pair<const char*, unsigned>
3997e3a004c6ed1fe87912203b9c5a113f8da89d261Ted Kremenek  ReadKey(const unsigned char* d, unsigned n) {
4007e3a004c6ed1fe87912203b9c5a113f8da89d261Ted Kremenek      assert(n >= 2 && d[n-1] == '\0');
4017e3a004c6ed1fe87912203b9c5a113f8da89d261Ted Kremenek      return std::make_pair((const char*) d, n-1);
4027e3a004c6ed1fe87912203b9c5a113f8da89d261Ted Kremenek    }
4031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
404a4b44dd9d30929a35c44d85102e5241ee847b2f2Ted Kremenek  static uint32_t ReadData(const internal_key_type& k, const unsigned char* d,
405a4b44dd9d30929a35c44d85102e5241ee847b2f2Ted Kremenek                           unsigned) {
406651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    using namespace llvm::support;
407651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    return endian::readNext<uint32_t, little, unaligned>(d);
4087e3a004c6ed1fe87912203b9c5a113f8da89d261Ted Kremenek  }
4097e3a004c6ed1fe87912203b9c5a113f8da89d261Ted Kremenek};
4101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump} // end anonymous namespace
412d8c02929fe70f03111be73e7b8c402c724238ee9Ted Kremenek
4136bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hinestypedef llvm::OnDiskChainedHashTable<PTHFileLookupTrait>   PTHFileLookup;
4146bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hinestypedef llvm::OnDiskChainedHashTable<PTHStringLookupTrait> PTHStringIdLookup;
4150c6a77bc1f52f282a969538f139ebde429076ed3Ted Kremenek
4160c6a77bc1f52f282a969538f139ebde429076ed3Ted Kremenek//===----------------------------------------------------------------------===//
4170c6a77bc1f52f282a969538f139ebde429076ed3Ted Kremenek// PTHManager methods.
4180c6a77bc1f52f282a969538f139ebde429076ed3Ted Kremenek//===----------------------------------------------------------------------===//
4190c6a77bc1f52f282a969538f139ebde429076ed3Ted Kremenek
4200c6a77bc1f52f282a969538f139ebde429076ed3Ted KremenekPTHManager::PTHManager(const llvm::MemoryBuffer* buf, void* fileLookup,
421da9d61c96c412f6babc7f824152609562f302388Chris Lattner                       const unsigned char* idDataTable,
4221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                       IdentifierInfo** perIDCache,
4237e3a004c6ed1fe87912203b9c5a113f8da89d261Ted Kremenek                       void* stringIdLookup, unsigned numIds,
42468228634016f644a1164fa1f024a9ce2093656bfTed Kremenek                       const unsigned char* spellingBase,
42568228634016f644a1164fa1f024a9ce2093656bfTed Kremenek                       const char* originalSourceFile)
4266183e4815a4019e97ad01bd880f12355599b75fdTed Kremenek: Buf(buf), PerIDCache(perIDCache), FileLookup(fileLookup),
4277e3a004c6ed1fe87912203b9c5a113f8da89d261Ted Kremenek  IdDataTable(idDataTable), StringIdLookup(stringIdLookup),
4286bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  NumIds(numIds), PP(nullptr), SpellingBase(spellingBase),
42968228634016f644a1164fa1f024a9ce2093656bfTed Kremenek  OriginalSourceFile(originalSourceFile) {}
4300c6a77bc1f52f282a969538f139ebde429076ed3Ted Kremenek
4310c6a77bc1f52f282a969538f139ebde429076ed3Ted KremenekPTHManager::~PTHManager() {
4320c6a77bc1f52f282a969538f139ebde429076ed3Ted Kremenek  delete Buf;
4330c6a77bc1f52f282a969538f139ebde429076ed3Ted Kremenek  delete (PTHFileLookup*) FileLookup;
4347e3a004c6ed1fe87912203b9c5a113f8da89d261Ted Kremenek  delete (PTHStringIdLookup*) StringIdLookup;
4350e50b6e7c104d00614baa3d80df62f1630a94d9cTed Kremenek  free(PerIDCache);
4360c6a77bc1f52f282a969538f139ebde429076ed3Ted Kremenek}
4370c6a77bc1f52f282a969538f139ebde429076ed3Ted Kremenek
438d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikiestatic void InvalidPTH(DiagnosticsEngine &Diags, const char *Msg) {
439651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  Diags.Report(Diags.getCustomDiagID(DiagnosticsEngine::Error, "%0")) << Msg;
44026555b18aa2c3b78744e77927acd3faa53ae7369Ted Kremenek}
44126555b18aa2c3b78744e77927acd3faa53ae7369Ted Kremenek
442d6471f7c1921c7802804ce3ff6fe9768310f72b9David BlaikiePTHManager *PTHManager::Create(const std::string &file,
443d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie                               DiagnosticsEngine &Diags) {
4440c6a77bc1f52f282a969538f139ebde429076ed3Ted Kremenek  // Memory map the PTH file.
445ef8225444452a1486bd721f3285301fe84643b00Stephen Hines  llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> FileOrErr =
446ef8225444452a1486bd721f3285301fe84643b00Stephen Hines      llvm::MemoryBuffer::getFile(file);
4471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
448ef8225444452a1486bd721f3285301fe84643b00Stephen Hines  if (!FileOrErr) {
4493a321e23f66128dbb986343927456ff6702af617Michael J. Spencer    // FIXME: Add ec.message() to this diag.
4503574f46cf495ec61618fd6864b045c5b1d0d5068Daniel Dunbar    Diags.Report(diag::err_invalid_pth_file) << file;
4516bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
4528a6aec620dbec1f292fe4116c0373ac81ab90234Ted Kremenek  }
453ef8225444452a1486bd721f3285301fe84643b00Stephen Hines  std::unique_ptr<llvm::MemoryBuffer> File = std::move(FileOrErr.get());
4541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
455651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  using namespace llvm::support;
456651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
4570c6a77bc1f52f282a969538f139ebde429076ed3Ted Kremenek  // Get the buffer ranges and check if there are at least three 32-bit
4580c6a77bc1f52f282a969538f139ebde429076ed3Ted Kremenek  // words at the end of the file.
45931ba6135375433b617a8587ea6cc836a014ebd86Roman Divacky  const unsigned char *BufBeg = (const unsigned char*)File->getBufferStart();
46031ba6135375433b617a8587ea6cc836a014ebd86Roman Divacky  const unsigned char *BufEnd = (const unsigned char*)File->getBufferEnd();
461e1b6498c41b94c3bc5cede17b0702282543385efTed Kremenek
462e1b6498c41b94c3bc5cede17b0702282543385efTed Kremenek  // Check the prologue of the file.
463c141b51d78b40c7b0b04f27adfa70ec5bbddc49cRichard Smith  if ((BufEnd - BufBeg) < (signed)(sizeof("cfe-pth") + 4 + 4) ||
464c141b51d78b40c7b0b04f27adfa70ec5bbddc49cRichard Smith      memcmp(BufBeg, "cfe-pth", sizeof("cfe-pth")) != 0) {
4653574f46cf495ec61618fd6864b045c5b1d0d5068Daniel Dunbar    Diags.Report(diag::err_invalid_pth_file) << file;
4666bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
46726555b18aa2c3b78744e77927acd3faa53ae7369Ted Kremenek  }
4681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
46967d15050bbea16ae256e204ecd464f2e454c3c99Ted Kremenek  // Read the PTH version.
470c141b51d78b40c7b0b04f27adfa70ec5bbddc49cRichard Smith  const unsigned char *p = BufBeg + (sizeof("cfe-pth"));
471651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  unsigned Version = endian::readNext<uint32_t, little, aligned>(p);
4721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4733574f46cf495ec61618fd6864b045c5b1d0d5068Daniel Dunbar  if (Version < PTHManager::Version) {
4743574f46cf495ec61618fd6864b045c5b1d0d5068Daniel Dunbar    InvalidPTH(Diags,
4751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        Version < PTHManager::Version
47626555b18aa2c3b78744e77927acd3faa53ae7369Ted Kremenek        ? "PTH file uses an older PTH format that is no longer supported"
47726555b18aa2c3b78744e77927acd3faa53ae7369Ted Kremenek        : "PTH file uses a newer PTH format that cannot be read");
4786bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
47926555b18aa2c3b78744e77927acd3faa53ae7369Ted Kremenek  }
48067d15050bbea16ae256e204ecd464f2e454c3c99Ted Kremenek
4811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // Compute the address of the index table at the end of the PTH file.
482a4bd8eb4d6d4b625f6bbb62fc180b02eab6433edTed Kremenek  const unsigned char *PrologueOffset = p;
4831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
484a4bd8eb4d6d4b625f6bbb62fc180b02eab6433edTed Kremenek  if (PrologueOffset >= BufEnd) {
4853574f46cf495ec61618fd6864b045c5b1d0d5068Daniel Dunbar    Diags.Report(diag::err_invalid_pth_file) << file;
4866bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
48726555b18aa2c3b78744e77927acd3faa53ae7369Ted Kremenek  }
4881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4890c6a77bc1f52f282a969538f139ebde429076ed3Ted Kremenek  // Construct the file lookup table.  This will be used for mapping from
4900c6a77bc1f52f282a969538f139ebde429076ed3Ted Kremenek  // FileEntry*'s to cached tokens.
491a4bd8eb4d6d4b625f6bbb62fc180b02eab6433edTed Kremenek  const unsigned char* FileTableOffset = PrologueOffset + sizeof(uint32_t)*2;
492651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  const unsigned char *FileTable =
493651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      BufBeg + endian::readNext<uint32_t, little, aligned>(FileTableOffset);
4941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4950c6a77bc1f52f282a969538f139ebde429076ed3Ted Kremenek  if (!(FileTable > BufBeg && FileTable < BufEnd)) {
4963574f46cf495ec61618fd6864b045c5b1d0d5068Daniel Dunbar    Diags.Report(diag::err_invalid_pth_file) << file;
4976bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr; // FIXME: Proper error diagnostic?
4980c6a77bc1f52f282a969538f139ebde429076ed3Ted Kremenek  }
4991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
500651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  std::unique_ptr<PTHFileLookup> FL(PTHFileLookup::Create(FileTable, BufBeg));
5011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5021d20197cb9478f3db17cbcfc57a1f8182843fc6aTed Kremenek  // Warn if the PTH file is empty.  We still want to create a PTHManager
5031d20197cb9478f3db17cbcfc57a1f8182843fc6aTed Kremenek  // as the PTH could be used with -include-pth.
5041d20197cb9478f3db17cbcfc57a1f8182843fc6aTed Kremenek  if (FL->isEmpty())
5053574f46cf495ec61618fd6864b045c5b1d0d5068Daniel Dunbar    InvalidPTH(Diags, "PTH file contains no cached source data");
5061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5070c6a77bc1f52f282a969538f139ebde429076ed3Ted Kremenek  // Get the location of the table mapping from persistent ids to the
5080c6a77bc1f52f282a969538f139ebde429076ed3Ted Kremenek  // data needed to reconstruct identifiers.
509a4bd8eb4d6d4b625f6bbb62fc180b02eab6433edTed Kremenek  const unsigned char* IDTableOffset = PrologueOffset + sizeof(uint32_t)*0;
510651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  const unsigned char *IData =
511651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      BufBeg + endian::readNext<uint32_t, little, aligned>(IDTableOffset);
5121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
513cdd8f2153e18796e9e2a126ebcbd4f3e1bd7135bTed Kremenek  if (!(IData >= BufBeg && IData < BufEnd)) {
5143574f46cf495ec61618fd6864b045c5b1d0d5068Daniel Dunbar    Diags.Report(diag::err_invalid_pth_file) << file;
5156bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
5160c6a77bc1f52f282a969538f139ebde429076ed3Ted Kremenek  }
5171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5187e3a004c6ed1fe87912203b9c5a113f8da89d261Ted Kremenek  // Get the location of the hashtable mapping between strings and
5197e3a004c6ed1fe87912203b9c5a113f8da89d261Ted Kremenek  // persistent IDs.
520a4bd8eb4d6d4b625f6bbb62fc180b02eab6433edTed Kremenek  const unsigned char* StringIdTableOffset = PrologueOffset + sizeof(uint32_t)*1;
521651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  const unsigned char *StringIdTable =
522651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      BufBeg + endian::readNext<uint32_t, little, aligned>(StringIdTableOffset);
5237e3a004c6ed1fe87912203b9c5a113f8da89d261Ted Kremenek  if (!(StringIdTable >= BufBeg && StringIdTable < BufEnd)) {
5243574f46cf495ec61618fd6864b045c5b1d0d5068Daniel Dunbar    Diags.Report(diag::err_invalid_pth_file) << file;
5256bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
52672b1b15ee88aac0a63e2c1dc53fe22f5ab297b20Ted Kremenek  }
5277e3a004c6ed1fe87912203b9c5a113f8da89d261Ted Kremenek
528651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  std::unique_ptr<PTHStringIdLookup> SL(
529651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      PTHStringIdLookup::Create(StringIdTable, BufBeg));
5301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
531277faca30c9f8f72b79f55695cbe3395ec246e7cTed Kremenek  // Get the location of the spelling cache.
532a4bd8eb4d6d4b625f6bbb62fc180b02eab6433edTed Kremenek  const unsigned char* spellingBaseOffset = PrologueOffset + sizeof(uint32_t)*3;
533651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  const unsigned char *spellingBase =
534651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      BufBeg + endian::readNext<uint32_t, little, aligned>(spellingBaseOffset);
535277faca30c9f8f72b79f55695cbe3395ec246e7cTed Kremenek  if (!(spellingBase >= BufBeg && spellingBase < BufEnd)) {
5363574f46cf495ec61618fd6864b045c5b1d0d5068Daniel Dunbar    Diags.Report(diag::err_invalid_pth_file) << file;
5376bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
538277faca30c9f8f72b79f55695cbe3395ec246e7cTed Kremenek  }
5391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5406183e4815a4019e97ad01bd880f12355599b75fdTed Kremenek  // Get the number of IdentifierInfos and pre-allocate the identifier cache.
541651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  uint32_t NumIds = endian::readNext<uint32_t, little, aligned>(IData);
5421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
543fc8f0e14ad142ed811e90fbd9a30e419e301c717Chris Lattner  // Pre-allocate the persistent ID -> IdentifierInfo* cache.  We use calloc()
5446183e4815a4019e97ad01bd880f12355599b75fdTed Kremenek  // so that we in the best case only zero out memory once when the OS returns
5456183e4815a4019e97ad01bd880f12355599b75fdTed Kremenek  // us new pages.
5466bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  IdentifierInfo **PerIDCache = nullptr;
5471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
548cdd8f2153e18796e9e2a126ebcbd4f3e1bd7135bTed Kremenek  if (NumIds) {
5491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    PerIDCache = (IdentifierInfo**)calloc(NumIds, sizeof(*PerIDCache));
550cdd8f2153e18796e9e2a126ebcbd4f3e1bd7135bTed Kremenek    if (!PerIDCache) {
5513574f46cf495ec61618fd6864b045c5b1d0d5068Daniel Dunbar      InvalidPTH(Diags, "Could not allocate memory for processing PTH file");
5526bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      return nullptr;
553cdd8f2153e18796e9e2a126ebcbd4f3e1bd7135bTed Kremenek    }
5546183e4815a4019e97ad01bd880f12355599b75fdTed Kremenek  }
555cdd8f2153e18796e9e2a126ebcbd4f3e1bd7135bTed Kremenek
55668228634016f644a1164fa1f024a9ce2093656bfTed Kremenek  // Compute the address of the original source file.
55768228634016f644a1164fa1f024a9ce2093656bfTed Kremenek  const unsigned char* originalSourceBase = PrologueOffset + sizeof(uint32_t)*4;
558651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  unsigned len =
559651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      endian::readNext<uint16_t, little, unaligned>(originalSourceBase);
5606bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  if (!len) originalSourceBase = nullptr;
5611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
56272b1b15ee88aac0a63e2c1dc53fe22f5ab297b20Ted Kremenek  // Create the new PTHManager.
563651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  return new PTHManager(File.release(), FL.release(), IData, PerIDCache,
564651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                        SL.release(), NumIds, spellingBase,
565651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                        (const char *)originalSourceBase);
5660c6a77bc1f52f282a969538f139ebde429076ed3Ted Kremenek}
56768228634016f644a1164fa1f024a9ce2093656bfTed Kremenek
56877ecb3a28f21496ecfdbb3d5f5b66b0d2abf48c9Chris LattnerIdentifierInfo* PTHManager::LazilyCreateIdentifierInfo(unsigned PersistentID) {
569651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  using namespace llvm::support;
5700c6a77bc1f52f282a969538f139ebde429076ed3Ted Kremenek  // Look in the PTH file for the string data for the IdentifierInfo object.
57177ecb3a28f21496ecfdbb3d5f5b66b0d2abf48c9Chris Lattner  const unsigned char* TableEntry = IdDataTable + sizeof(uint32_t)*PersistentID;
572651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  const unsigned char *IDData =
573651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      (const unsigned char *)Buf->getBufferStart() +
574651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      endian::readNext<uint32_t, little, aligned>(TableEntry);
575da9d61c96c412f6babc7f824152609562f302388Chris Lattner  assert(IDData < (const unsigned char*)Buf->getBufferEnd());
5761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
57772b1b15ee88aac0a63e2c1dc53fe22f5ab297b20Ted Kremenek  // Allocate the object.
578da9d61c96c412f6babc7f824152609562f302388Chris Lattner  std::pair<IdentifierInfo,const unsigned char*> *Mem =
579da9d61c96c412f6babc7f824152609562f302388Chris Lattner    Alloc.Allocate<std::pair<IdentifierInfo,const unsigned char*> >();
58072b1b15ee88aac0a63e2c1dc53fe22f5ab297b20Ted Kremenek
58172b1b15ee88aac0a63e2c1dc53fe22f5ab297b20Ted Kremenek  Mem->second = IDData;
5827e3a004c6ed1fe87912203b9c5a113f8da89d261Ted Kremenek  assert(IDData[0] != '\0');
583ea9c26b3dbd74a1497f5609ae6e19a85f42b6073Ted Kremenek  IdentifierInfo *II = new ((void*) Mem) IdentifierInfo();
5841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
58572b1b15ee88aac0a63e2c1dc53fe22f5ab297b20Ted Kremenek  // Store the new IdentifierInfo in the cache.
58677ecb3a28f21496ecfdbb3d5f5b66b0d2abf48c9Chris Lattner  PerIDCache[PersistentID] = II;
587e013d685c6689ac7ae103ee88acf573422d1ed6aDaniel Dunbar  assert(II->getNameStart() && II->getNameStart()[0] != '\0');
5880c6a77bc1f52f282a969538f139ebde429076ed3Ted Kremenek  return II;
5890c6a77bc1f52f282a969538f139ebde429076ed3Ted Kremenek}
5900c6a77bc1f52f282a969538f139ebde429076ed3Ted Kremenek
5915f9e272e632e951b1efe824cd16acb4d96077930Chris LattnerIdentifierInfo* PTHManager::get(StringRef Name) {
5927e3a004c6ed1fe87912203b9c5a113f8da89d261Ted Kremenek  PTHStringIdLookup& SL = *((PTHStringIdLookup*)StringIdLookup);
5937e3a004c6ed1fe87912203b9c5a113f8da89d261Ted Kremenek  // Double check our assumption that the last character isn't '\0'.
594681d83dd7e36ddb8a25313c446a91c00bbc652ddDavid Blaikie  assert(Name.empty() || Name.back() != '\0');
595700030ebddb987936d4fee14d9412821d96e4840Kovarththanan Rajaratnam  PTHStringIdLookup::iterator I = SL.find(std::make_pair(Name.data(),
596700030ebddb987936d4fee14d9412821d96e4840Kovarththanan Rajaratnam                                                         Name.size()));
5977e3a004c6ed1fe87912203b9c5a113f8da89d261Ted Kremenek  if (I == SL.end()) // No identifier found?
5986bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
59972b1b15ee88aac0a63e2c1dc53fe22f5ab297b20Ted Kremenek
6007e3a004c6ed1fe87912203b9c5a113f8da89d261Ted Kremenek  // Match found.  Return the identifier!
6017e3a004c6ed1fe87912203b9c5a113f8da89d261Ted Kremenek  assert(*I > 0);
6027e3a004c6ed1fe87912203b9c5a113f8da89d261Ted Kremenek  return GetIdentifierInfo(*I-1);
6037e3a004c6ed1fe87912203b9c5a113f8da89d261Ted Kremenek}
60472b1b15ee88aac0a63e2c1dc53fe22f5ab297b20Ted Kremenek
605f056d92e182cbe4f62c8d14102544dc38066dabcChris LattnerPTHLexer *PTHManager::CreateLexer(FileID FID) {
606f056d92e182cbe4f62c8d14102544dc38066dabcChris Lattner  const FileEntry *FE = PP->getSourceManager().getFileEntryForID(FID);
6070c6a77bc1f52f282a969538f139ebde429076ed3Ted Kremenek  if (!FE)
6086bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
6091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
610651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  using namespace llvm::support;
611651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
6120c6a77bc1f52f282a969538f139ebde429076ed3Ted Kremenek  // Lookup the FileEntry object in our file lookup data structure.  It will
6130c6a77bc1f52f282a969538f139ebde429076ed3Ted Kremenek  // return a variant that indicates whether or not there is an offset within
6140c6a77bc1f52f282a969538f139ebde429076ed3Ted Kremenek  // the PTH file that contains cached tokens.
615d8c02929fe70f03111be73e7b8c402c724238ee9Ted Kremenek  PTHFileLookup& PFL = *((PTHFileLookup*)FileLookup);
616d8c02929fe70f03111be73e7b8c402c724238ee9Ted Kremenek  PTHFileLookup::iterator I = PFL.find(FE);
6171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
618d8c02929fe70f03111be73e7b8c402c724238ee9Ted Kremenek  if (I == PFL.end()) // No tokens available?
6196bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
6201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  const PTHFileData& FileData = *I;
6221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
623da9d61c96c412f6babc7f824152609562f302388Chris Lattner  const unsigned char *BufStart = (const unsigned char *)Buf->getBufferStart();
6240c6a77bc1f52f282a969538f139ebde429076ed3Ted Kremenek  // Compute the offset of the token data within the buffer.
625da9d61c96c412f6babc7f824152609562f302388Chris Lattner  const unsigned char* data = BufStart + FileData.getTokenOffset();
626268ee7016a2811803989487c0ad3799486092c63Ted Kremenek
627268ee7016a2811803989487c0ad3799486092c63Ted Kremenek  // Get the location of pp-conditional table.
628da9d61c96c412f6babc7f824152609562f302388Chris Lattner  const unsigned char* ppcond = BufStart + FileData.getPPCondOffset();
629651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  uint32_t Len = endian::readNext<uint32_t, little, aligned>(ppcond);
6306bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  if (Len == 0) ppcond = nullptr;
6311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
63272b1b15ee88aac0a63e2c1dc53fe22f5ab297b20Ted Kremenek  assert(PP && "No preprocessor set yet!");
6331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return new PTHLexer(*PP, FID, data, ppcond, *this);
6340c6a77bc1f52f282a969538f139ebde429076ed3Ted Kremenek}
635337edcdbec05316b407d0d64865c88ff8597d910Ted Kremenek
636337edcdbec05316b407d0d64865c88ff8597d910Ted Kremenek//===----------------------------------------------------------------------===//
637337edcdbec05316b407d0d64865c88ff8597d910Ted Kremenek// 'stat' caching.
638337edcdbec05316b407d0d64865c88ff8597d910Ted Kremenek//===----------------------------------------------------------------------===//
639337edcdbec05316b407d0d64865c88ff8597d910Ted Kremenek
640337edcdbec05316b407d0d64865c88ff8597d910Ted Kremeneknamespace {
64185b4521e34dcd4a0a4a1f0819e1123128e5a3125Benjamin Kramerclass PTHStatData {
642337edcdbec05316b407d0d64865c88ff8597d910Ted Kremenekpublic:
6430fda0f75f053ad9afdb2bb8d4ea72c5e6a49d657Rafael Espindola  const bool HasData;
6440fda0f75f053ad9afdb2bb8d4ea72c5e6a49d657Rafael Espindola  uint64_t Size;
6450fda0f75f053ad9afdb2bb8d4ea72c5e6a49d657Rafael Espindola  time_t ModTime;
6460fda0f75f053ad9afdb2bb8d4ea72c5e6a49d657Rafael Espindola  llvm::sys::fs::UniqueID UniqueID;
6470fda0f75f053ad9afdb2bb8d4ea72c5e6a49d657Rafael Espindola  bool IsDirectory;
6480fda0f75f053ad9afdb2bb8d4ea72c5e6a49d657Rafael Espindola
6490fda0f75f053ad9afdb2bb8d4ea72c5e6a49d657Rafael Espindola  PTHStatData(uint64_t Size, time_t ModTime, llvm::sys::fs::UniqueID UniqueID,
6500fda0f75f053ad9afdb2bb8d4ea72c5e6a49d657Rafael Espindola              bool IsDirectory)
6510fda0f75f053ad9afdb2bb8d4ea72c5e6a49d657Rafael Espindola      : HasData(true), Size(Size), ModTime(ModTime), UniqueID(UniqueID),
6520fda0f75f053ad9afdb2bb8d4ea72c5e6a49d657Rafael Espindola        IsDirectory(IsDirectory) {}
6530fda0f75f053ad9afdb2bb8d4ea72c5e6a49d657Rafael Espindola
6540fda0f75f053ad9afdb2bb8d4ea72c5e6a49d657Rafael Espindola  PTHStatData() : HasData(false) {}
655337edcdbec05316b407d0d64865c88ff8597d910Ted Kremenek};
6561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
65785b4521e34dcd4a0a4a1f0819e1123128e5a3125Benjamin Kramerclass PTHStatLookupTrait : public PTHFileLookupCommonTrait {
658337edcdbec05316b407d0d64865c88ff8597d910Ted Kremenekpublic:
659a4b44dd9d30929a35c44d85102e5241ee847b2f2Ted Kremenek  typedef const char* external_key_type;  // const char*
660337edcdbec05316b407d0d64865c88ff8597d910Ted Kremenek  typedef PTHStatData data_type;
6611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
662a4b44dd9d30929a35c44d85102e5241ee847b2f2Ted Kremenek  static internal_key_type GetInternalKey(const char *path) {
663a4b44dd9d30929a35c44d85102e5241ee847b2f2Ted Kremenek    // The key 'kind' doesn't matter here because it is ignored in EqualKey.
664a4b44dd9d30929a35c44d85102e5241ee847b2f2Ted Kremenek    return std::make_pair((unsigned char) 0x0, path);
665a4b44dd9d30929a35c44d85102e5241ee847b2f2Ted Kremenek  }
666a4b44dd9d30929a35c44d85102e5241ee847b2f2Ted Kremenek
667a4b44dd9d30929a35c44d85102e5241ee847b2f2Ted Kremenek  static bool EqualKey(internal_key_type a, internal_key_type b) {
668a4b44dd9d30929a35c44d85102e5241ee847b2f2Ted Kremenek    // When doing 'stat' lookups we don't care about the kind of 'a' and 'b',
669a4b44dd9d30929a35c44d85102e5241ee847b2f2Ted Kremenek    // just the paths.
670a4b44dd9d30929a35c44d85102e5241ee847b2f2Ted Kremenek    return strcmp(a.second, b.second) == 0;
6711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
6721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
673a4b44dd9d30929a35c44d85102e5241ee847b2f2Ted Kremenek  static data_type ReadData(const internal_key_type& k, const unsigned char* d,
6741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                            unsigned) {
6751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
676ad6ce5cb0e3b5dd853d2b1e337797a6fc1091e21Ted Kremenek    if (k.first /* File or Directory */) {
6770fda0f75f053ad9afdb2bb8d4ea72c5e6a49d657Rafael Espindola      bool IsDirectory = true;
6780fda0f75f053ad9afdb2bb8d4ea72c5e6a49d657Rafael Espindola      if (k.first == 0x1 /* File */) {
6790fda0f75f053ad9afdb2bb8d4ea72c5e6a49d657Rafael Espindola        IsDirectory = false;
6800fda0f75f053ad9afdb2bb8d4ea72c5e6a49d657Rafael Espindola        d += 4 * 2; // Skip the first 2 words.
6810fda0f75f053ad9afdb2bb8d4ea72c5e6a49d657Rafael Espindola      }
6820fda0f75f053ad9afdb2bb8d4ea72c5e6a49d657Rafael Espindola
683651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      using namespace llvm::support;
684651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
685651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      uint64_t File = endian::readNext<uint64_t, little, unaligned>(d);
686651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      uint64_t Device = endian::readNext<uint64_t, little, unaligned>(d);
6870fda0f75f053ad9afdb2bb8d4ea72c5e6a49d657Rafael Espindola      llvm::sys::fs::UniqueID UniqueID(File, Device);
688651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      time_t ModTime = endian::readNext<uint64_t, little, unaligned>(d);
689651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      uint64_t Size = endian::readNext<uint64_t, little, unaligned>(d);
6900fda0f75f053ad9afdb2bb8d4ea72c5e6a49d657Rafael Espindola      return data_type(Size, ModTime, UniqueID, IsDirectory);
691a4b44dd9d30929a35c44d85102e5241ee847b2f2Ted Kremenek    }
692ad6ce5cb0e3b5dd853d2b1e337797a6fc1091e21Ted Kremenek
693ad6ce5cb0e3b5dd853d2b1e337797a6fc1091e21Ted Kremenek    // Negative stat.  Don't read anything.
694a4b44dd9d30929a35c44d85102e5241ee847b2f2Ted Kremenek    return data_type();
695337edcdbec05316b407d0d64865c88ff8597d910Ted Kremenek  }
696337edcdbec05316b407d0d64865c88ff8597d910Ted Kremenek};
697337edcdbec05316b407d0d64865c88ff8597d910Ted Kremenek
69810e286aa8d39fb51a21412850265d9dae74613eeChris Lattnerclass PTHStatCache : public FileSystemStatCache {
6996bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  typedef llvm::OnDiskChainedHashTable<PTHStatLookupTrait> CacheTy;
700337edcdbec05316b407d0d64865c88ff8597d910Ted Kremenek  CacheTy Cache;
701337edcdbec05316b407d0d64865c88ff8597d910Ted Kremenek
7021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumppublic:
703337edcdbec05316b407d0d64865c88ff8597d910Ted Kremenek  PTHStatCache(PTHFileLookup &FL) :
704337edcdbec05316b407d0d64865c88ff8597d910Ted Kremenek    Cache(FL.getNumBuckets(), FL.getNumEntries(), FL.getBuckets(),
705337edcdbec05316b407d0d64865c88ff8597d910Ted Kremenek          FL.getBase()) {}
706337edcdbec05316b407d0d64865c88ff8597d910Ted Kremenek
7070fda0f75f053ad9afdb2bb8d4ea72c5e6a49d657Rafael Espindola  LookupResult getStat(const char *Path, FileData &Data, bool isFile,
708ef8225444452a1486bd721f3285301fe84643b00Stephen Hines                       std::unique_ptr<vfs::File> *F,
709ef8225444452a1486bd721f3285301fe84643b00Stephen Hines                       vfs::FileSystem &FS) override {
710337edcdbec05316b407d0d64865c88ff8597d910Ted Kremenek    // Do the lookup for the file's data in the PTH file.
71110e286aa8d39fb51a21412850265d9dae74613eeChris Lattner    CacheTy::iterator I = Cache.find(Path);
712337edcdbec05316b407d0d64865c88ff8597d910Ted Kremenek
713337edcdbec05316b407d0d64865c88ff8597d910Ted Kremenek    // If we don't get a hit in the PTH file just forward to 'stat'.
714700030ebddb987936d4fee14d9412821d96e4840Kovarththanan Rajaratnam    if (I == Cache.end())
715651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      return statChained(Path, Data, isFile, F, FS);
7161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7170fda0f75f053ad9afdb2bb8d4ea72c5e6a49d657Rafael Espindola    const PTHStatData &D = *I;
7181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7190fda0f75f053ad9afdb2bb8d4ea72c5e6a49d657Rafael Espindola    if (!D.HasData)
720d6f611198089b78e32d3a15fe8bc986204aee1aaChris Lattner      return CacheMissing;
721ad6ce5cb0e3b5dd853d2b1e337797a6fc1091e21Ted Kremenek
722651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    Data.Name = Path;
7230fda0f75f053ad9afdb2bb8d4ea72c5e6a49d657Rafael Espindola    Data.Size = D.Size;
7240fda0f75f053ad9afdb2bb8d4ea72c5e6a49d657Rafael Espindola    Data.ModTime = D.ModTime;
7250fda0f75f053ad9afdb2bb8d4ea72c5e6a49d657Rafael Espindola    Data.UniqueID = D.UniqueID;
7260fda0f75f053ad9afdb2bb8d4ea72c5e6a49d657Rafael Espindola    Data.IsDirectory = D.IsDirectory;
7270fda0f75f053ad9afdb2bb8d4ea72c5e6a49d657Rafael Espindola    Data.IsNamedPipe = false;
7280fda0f75f053ad9afdb2bb8d4ea72c5e6a49d657Rafael Espindola    Data.InPCH = true;
7290fda0f75f053ad9afdb2bb8d4ea72c5e6a49d657Rafael Espindola
730d6f611198089b78e32d3a15fe8bc986204aee1aaChris Lattner    return CacheExists;
731337edcdbec05316b407d0d64865c88ff8597d910Ted Kremenek  }
732337edcdbec05316b407d0d64865c88ff8597d910Ted Kremenek};
733d578569d50adc6e9b1a2f91931add6b471b2378eTed Kremenek} // end anonymous namespace
734337edcdbec05316b407d0d64865c88ff8597d910Ted Kremenek
73510e286aa8d39fb51a21412850265d9dae74613eeChris LattnerFileSystemStatCache *PTHManager::createStatCache() {
7365f747d187bf8dc779a4acc180f9c21f1dbf83f6aTed Kremenek  return new PTHStatCache(*((PTHFileLookup*) FileLookup));
737337edcdbec05316b407d0d64865c88ff8597d910Ted Kremenek}
738