blob: 42629d41ab565e2899010dfde162273371d222a2 [file] [log] [blame]
Ted Kremenek274b2082008-11-12 21:37:15 +00001//===--- 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
Chandler Carruth55fc8732012-12-04 09:13:33 +000014#include "clang/Lex/PTHLexer.h"
Ted Kremenek0c6a77b2008-12-03 00:38:03 +000015#include "clang/Basic/FileManager.h"
Chris Lattner10e286a2010-11-23 19:19:34 +000016#include "clang/Basic/FileSystemStatCache.h"
Ted Kremenek0c6a77b2008-12-03 00:38:03 +000017#include "clang/Basic/IdentifierTable.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000018#include "clang/Basic/TokenKinds.h"
Daniel Dunbar3574f462009-11-12 02:53:48 +000019#include "clang/Lex/LexDiagnostic.h"
Ted Kremenek0c6a77b2008-12-03 00:38:03 +000020#include "clang/Lex/PTHManager.h"
Ted Kremenek0c6a77b2008-12-03 00:38:03 +000021#include "clang/Lex/Preprocessor.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000022#include "clang/Lex/Token.h"
Daniel Dunbar2596e422009-10-17 23:52:28 +000023#include "llvm/ADT/StringExtras.h"
24#include "llvm/ADT/StringMap.h"
Stephen Hines651f13c2014-04-23 16:59:28 -070025#include "llvm/Support/EndianStream.h"
Chris Lattner6f78c3b2009-01-22 23:50:07 +000026#include "llvm/Support/MemoryBuffer.h"
Stephen Hines6bcf27b2014-05-29 04:14:42 -070027#include "llvm/Support/OnDiskHashTable.h"
Michael J. Spencer3a321e22010-12-09 17:36:38 +000028#include "llvm/Support/system_error.h"
Stephen Hines651f13c2014-04-23 16:59:28 -070029#include <memory>
Ted Kremenek274b2082008-11-12 21:37:15 +000030using namespace clang;
31
Ted Kremenek7b78b7c2009-01-19 23:13:15 +000032#define DISK_TOKEN_SIZE (1+1+2+4+4)
Ted Kremenek268ee702008-12-12 18:34:08 +000033
Ted Kremenek0c6a77b2008-12-03 00:38:03 +000034//===----------------------------------------------------------------------===//
Ted Kremeneke5680f32008-12-23 01:30:52 +000035// PTHLexer methods.
36//===----------------------------------------------------------------------===//
37
Chris Lattnerda9d61c2009-01-18 01:57:14 +000038PTHLexer::PTHLexer(Preprocessor &PP, FileID FID, const unsigned char *D,
Ted Kremenek277faca2009-01-27 00:01:05 +000039 const unsigned char *ppcond, PTHManager &PM)
Stephen Hines6bcf27b2014-05-29 04:14:42 -070040 : PreprocessorLexer(&PP, FID), TokBuf(D), CurPtr(D), LastHashTokPtr(nullptr),
Ted Kremenek277faca2009-01-27 00:01:05 +000041 PPCond(ppcond), CurPPCondPtr(ppcond), PTHMgr(PM) {
Mike Stump1eb44332009-09-09 15:08:12 +000042
Chris Lattner2b2453a2009-01-17 06:22:33 +000043 FileStartLoc = PP.getSourceManager().getLocForStartOfFile(FID);
Ted Kremenek5f074262009-01-09 22:05:30 +000044}
Ted Kremeneke5680f32008-12-23 01:30:52 +000045
Eli Friedmand2f93082013-09-19 00:41:32 +000046bool PTHLexer::Lex(Token& Tok) {
Ted Kremenek866bdf72008-12-23 02:30:15 +000047 //===--------------------------------------==//
48 // Read the raw token data.
49 //===--------------------------------------==//
Stephen Hines651f13c2014-04-23 16:59:28 -070050 using namespace llvm::support;
Mike Stump1eb44332009-09-09 15:08:12 +000051
Ted Kremenek866bdf72008-12-23 02:30:15 +000052 // Shadow CurPtr into an automatic variable.
Mike Stump1eb44332009-09-09 15:08:12 +000053 const unsigned char *CurPtrShadow = CurPtr;
Ted Kremenek866bdf72008-12-23 02:30:15 +000054
Chris Lattner1b5285e2009-01-18 02:10:31 +000055 // Read in the data for the token.
Stephen Hines651f13c2014-04-23 16:59:28 -070056 unsigned Word0 = endian::readNext<uint32_t, little, aligned>(CurPtrShadow);
57 uint32_t IdentifierID =
58 endian::readNext<uint32_t, little, aligned>(CurPtrShadow);
59 uint32_t FileOffset =
60 endian::readNext<uint32_t, little, aligned>(CurPtrShadow);
Mike Stump1eb44332009-09-09 15:08:12 +000061
Ted Kremenek7b78b7c2009-01-19 23:13:15 +000062 tok::TokenKind TKind = (tok::TokenKind) (Word0 & 0xFF);
63 Token::TokenFlags TFlags = (Token::TokenFlags) ((Word0 >> 8) & 0xFF);
Chris Lattneraff6ef82009-01-21 07:21:56 +000064 uint32_t Len = Word0 >> 16;
Ted Kremenek7b78b7c2009-01-19 23:13:15 +000065
Chris Lattneraff6ef82009-01-21 07:21:56 +000066 CurPtr = CurPtrShadow;
Mike Stump1eb44332009-09-09 15:08:12 +000067
Ted Kremenek866bdf72008-12-23 02:30:15 +000068 //===--------------------------------------==//
69 // Construct the token itself.
70 //===--------------------------------------==//
Mike Stump1eb44332009-09-09 15:08:12 +000071
Ted Kremenek866bdf72008-12-23 02:30:15 +000072 Tok.startToken();
Chris Lattner898a0bb2009-01-18 02:34:01 +000073 Tok.setKind(TKind);
74 Tok.setFlag(TFlags);
Ted Kremenek59d08cb2008-12-23 19:24:24 +000075 assert(!LexingRawMode);
Argyrios Kyrtzidisa64ccef2011-09-19 20:40:19 +000076 Tok.setLocation(FileStartLoc.getLocWithOffset(FileOffset));
Ted Kremenek866bdf72008-12-23 02:30:15 +000077 Tok.setLength(Len);
78
Chris Lattnerd0a69692009-01-21 07:50:06 +000079 // Handle identifiers.
Ted Kremenek277faca2009-01-27 00:01:05 +000080 if (Tok.isLiteral()) {
81 Tok.setLiteralData((const char*) (PTHMgr.SpellingBase + IdentifierID));
82 }
83 else if (IdentifierID) {
Chris Lattnerd0a69692009-01-21 07:50:06 +000084 MIOpt.ReadToken();
85 IdentifierInfo *II = PTHMgr.GetIdentifierInfo(IdentifierID-1);
Mike Stump1eb44332009-09-09 15:08:12 +000086
Chris Lattnerd0a69692009-01-21 07:50:06 +000087 Tok.setIdentifierInfo(II);
Mike Stump1eb44332009-09-09 15:08:12 +000088
Chris Lattner863c4862009-01-23 18:35:48 +000089 // Change the kind of this identifier to the appropriate token kind, e.g.
90 // turning "for" into a keyword.
91 Tok.setKind(II->getTokenID());
Mike Stump1eb44332009-09-09 15:08:12 +000092
Chris Lattnerd0a69692009-01-21 07:50:06 +000093 if (II->isHandleIdentifierCase())
Eli Friedmand2f93082013-09-19 00:41:32 +000094 return PP->HandleIdentifier(Tok);
95
96 return true;
Chris Lattnerd0a69692009-01-21 07:50:06 +000097 }
Mike Stump1eb44332009-09-09 15:08:12 +000098
Ted Kremenek866bdf72008-12-23 02:30:15 +000099 //===--------------------------------------==//
100 // Process the token.
101 //===--------------------------------------==//
Chris Lattner898a0bb2009-01-18 02:34:01 +0000102 if (TKind == tok::eof) {
Ted Kremeneke5680f32008-12-23 01:30:52 +0000103 // Save the end-of-file token.
104 EofToken = Tok;
Mike Stump1eb44332009-09-09 15:08:12 +0000105
Ted Kremenek59d08cb2008-12-23 19:24:24 +0000106 assert(!ParsingPreprocessorDirective);
107 assert(!LexingRawMode);
Mike Stump1eb44332009-09-09 15:08:12 +0000108
Eli Friedmand2f93082013-09-19 00:41:32 +0000109 return LexEndOfFile(Tok);
Ted Kremeneke5680f32008-12-23 01:30:52 +0000110 }
Mike Stump1eb44332009-09-09 15:08:12 +0000111
Chris Lattner898a0bb2009-01-18 02:34:01 +0000112 if (TKind == tok::hash && Tok.isAtStartOfLine()) {
Ted Kremenek59d08cb2008-12-23 19:24:24 +0000113 LastHashTokPtr = CurPtr - DISK_TOKEN_SIZE;
114 assert(!LexingRawMode);
115 PP->HandleDirective(Tok);
Mike Stump1eb44332009-09-09 15:08:12 +0000116
Eli Friedmand2f93082013-09-19 00:41:32 +0000117 return false;
Ted Kremenek59d08cb2008-12-23 19:24:24 +0000118 }
Mike Stump1eb44332009-09-09 15:08:12 +0000119
Peter Collingbourne84021552011-02-28 02:37:51 +0000120 if (TKind == tok::eod) {
Ted Kremenek59d08cb2008-12-23 19:24:24 +0000121 assert(ParsingPreprocessorDirective);
Ted Kremeneke5680f32008-12-23 01:30:52 +0000122 ParsingPreprocessorDirective = false;
Eli Friedmand2f93082013-09-19 00:41:32 +0000123 return true;
Ted Kremeneke5680f32008-12-23 01:30:52 +0000124 }
Ted Kremeneke5680f32008-12-23 01:30:52 +0000125
Ted Kremenek59d08cb2008-12-23 19:24:24 +0000126 MIOpt.ReadToken();
Eli Friedmand2f93082013-09-19 00:41:32 +0000127 return true;
Ted Kremeneke5680f32008-12-23 01:30:52 +0000128}
129
Ted Kremenek94e3d1f2010-07-27 02:59:02 +0000130bool PTHLexer::LexEndOfFile(Token &Result) {
131 // If we hit the end of the file while parsing a preprocessor directive,
132 // end the preprocessor directive first. The next token returned will
133 // then be the end of file.
134 if (ParsingPreprocessorDirective) {
135 ParsingPreprocessorDirective = false; // Done parsing the "line".
136 return true; // Have a token.
137 }
138
139 assert(!LexingRawMode);
140
141 // If we are in a #if directive, emit an error.
142 while (!ConditionalStack.empty()) {
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000143 if (PP->getCodeCompletionFileLoc() != FileStartLoc)
Douglas Gregor2d474ba2010-08-12 17:04:55 +0000144 PP->Diag(ConditionalStack.back().IfLoc,
145 diag::err_pp_unterminated_conditional);
Ted Kremenek94e3d1f2010-07-27 02:59:02 +0000146 ConditionalStack.pop_back();
147 }
148
149 // Finally, let the preprocessor handle this.
150 return PP->HandleEndOfFile(Result);
151}
152
Ted Kremeneke5680f32008-12-23 01:30:52 +0000153// FIXME: We can just grab the last token instead of storing a copy
154// into EofToken.
Ted Kremenek59d08cb2008-12-23 19:24:24 +0000155void PTHLexer::getEOF(Token& Tok) {
Ted Kremenekdefb7092009-01-09 00:36:11 +0000156 assert(EofToken.is(tok::eof));
Ted Kremeneke5680f32008-12-23 01:30:52 +0000157 Tok = EofToken;
158}
159
160void PTHLexer::DiscardToEndOfLine() {
161 assert(ParsingPreprocessorDirective && ParsingFilename == false &&
162 "Must be in a preprocessing directive!");
163
164 // We assume that if the preprocessor wishes to discard to the end of
165 // the line that it also means to end the current preprocessor directive.
166 ParsingPreprocessorDirective = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000167
Ted Kremeneke5680f32008-12-23 01:30:52 +0000168 // Skip tokens by only peeking at their token kind and the flags.
169 // We don't need to actually reconstruct full tokens from the token buffer.
170 // This saves some copies and it also reduces IdentifierInfo* lookup.
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000171 const unsigned char* p = CurPtr;
Ted Kremeneke5680f32008-12-23 01:30:52 +0000172 while (1) {
173 // Read the token kind. Are we at the end of the file?
174 tok::TokenKind x = (tok::TokenKind) (uint8_t) *p;
175 if (x == tok::eof) break;
Mike Stump1eb44332009-09-09 15:08:12 +0000176
Ted Kremeneke5680f32008-12-23 01:30:52 +0000177 // Read the token flags. Are we at the start of the next line?
178 Token::TokenFlags y = (Token::TokenFlags) (uint8_t) p[1];
179 if (y & Token::StartOfLine) break;
180
181 // Skip to the next token.
182 p += DISK_TOKEN_SIZE;
183 }
Mike Stump1eb44332009-09-09 15:08:12 +0000184
Ted Kremeneke5680f32008-12-23 01:30:52 +0000185 CurPtr = p;
186}
187
Ted Kremenek268ee702008-12-12 18:34:08 +0000188/// SkipBlock - Used by Preprocessor to skip the current conditional block.
189bool PTHLexer::SkipBlock() {
Stephen Hines651f13c2014-04-23 16:59:28 -0700190 using namespace llvm::support;
Ted Kremenek268ee702008-12-12 18:34:08 +0000191 assert(CurPPCondPtr && "No cached PP conditional information.");
192 assert(LastHashTokPtr && "No known '#' token.");
Mike Stump1eb44332009-09-09 15:08:12 +0000193
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700194 const unsigned char *HashEntryI = nullptr;
Ted Kremenek268ee702008-12-12 18:34:08 +0000195 uint32_t TableIdx;
Mike Stump1eb44332009-09-09 15:08:12 +0000196
Ted Kremenek268ee702008-12-12 18:34:08 +0000197 do {
Ted Kremenek41a26602008-12-12 22:05:38 +0000198 // Read the token offset from the side-table.
Stephen Hines651f13c2014-04-23 16:59:28 -0700199 uint32_t Offset = endian::readNext<uint32_t, little, aligned>(CurPPCondPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000200
201 // Read the target table index from the side-table.
Stephen Hines651f13c2014-04-23 16:59:28 -0700202 TableIdx = endian::readNext<uint32_t, little, aligned>(CurPPCondPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000203
Ted Kremenek41a26602008-12-12 22:05:38 +0000204 // Compute the actual memory address of the '#' token data for this entry.
205 HashEntryI = TokBuf + Offset;
206
207 // Optmization: "Sibling jumping". #if...#else...#endif blocks can
208 // contain nested blocks. In the side-table we can jump over these
209 // nested blocks instead of doing a linear search if the next "sibling"
210 // entry is not at a location greater than LastHashTokPtr.
211 if (HashEntryI < LastHashTokPtr && TableIdx) {
212 // In the side-table we are still at an entry for a '#' token that
213 // is earlier than the last one we saw. Check if the location we would
214 // stride gets us closer.
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000215 const unsigned char* NextPPCondPtr =
216 PPCond + TableIdx*(sizeof(uint32_t)*2);
Ted Kremenek41a26602008-12-12 22:05:38 +0000217 assert(NextPPCondPtr >= CurPPCondPtr);
218 // Read where we should jump to.
Stephen Hines651f13c2014-04-23 16:59:28 -0700219 const unsigned char *HashEntryJ =
220 TokBuf + endian::readNext<uint32_t, little, aligned>(NextPPCondPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000221
Ted Kremenek41a26602008-12-12 22:05:38 +0000222 if (HashEntryJ <= LastHashTokPtr) {
223 // Jump directly to the next entry in the side table.
224 HashEntryI = HashEntryJ;
Stephen Hines651f13c2014-04-23 16:59:28 -0700225 TableIdx = endian::readNext<uint32_t, little, aligned>(NextPPCondPtr);
Ted Kremenek41a26602008-12-12 22:05:38 +0000226 CurPPCondPtr = NextPPCondPtr;
227 }
228 }
Ted Kremenek268ee702008-12-12 18:34:08 +0000229 }
Mike Stump1eb44332009-09-09 15:08:12 +0000230 while (HashEntryI < LastHashTokPtr);
Ted Kremenek41a26602008-12-12 22:05:38 +0000231 assert(HashEntryI == LastHashTokPtr && "No PP-cond entry found for '#'");
Ted Kremenek268ee702008-12-12 18:34:08 +0000232 assert(TableIdx && "No jumping from #endifs.");
Mike Stump1eb44332009-09-09 15:08:12 +0000233
Ted Kremenek268ee702008-12-12 18:34:08 +0000234 // Update our side-table iterator.
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000235 const unsigned char* NextPPCondPtr = PPCond + TableIdx*(sizeof(uint32_t)*2);
Ted Kremenek268ee702008-12-12 18:34:08 +0000236 assert(NextPPCondPtr >= CurPPCondPtr);
237 CurPPCondPtr = NextPPCondPtr;
Mike Stump1eb44332009-09-09 15:08:12 +0000238
Ted Kremenek268ee702008-12-12 18:34:08 +0000239 // Read where we should jump to.
Stephen Hines651f13c2014-04-23 16:59:28 -0700240 HashEntryI =
241 TokBuf + endian::readNext<uint32_t, little, aligned>(NextPPCondPtr);
242 uint32_t NextIdx = endian::readNext<uint32_t, little, aligned>(NextPPCondPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000243
Ted Kremenek268ee702008-12-12 18:34:08 +0000244 // By construction NextIdx will be zero if this is a #endif. This is useful
245 // to know to obviate lexing another token.
246 bool isEndif = NextIdx == 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000247
Ted Kremenek268ee702008-12-12 18:34:08 +0000248 // This case can occur when we see something like this:
249 //
250 // #if ...
251 // /* a comment or nothing */
252 // #elif
253 //
254 // If we are skipping the first #if block it will be the case that CurPtr
255 // already points 'elif'. Just return.
Mike Stump1eb44332009-09-09 15:08:12 +0000256
Ted Kremenek41a26602008-12-12 22:05:38 +0000257 if (CurPtr > HashEntryI) {
258 assert(CurPtr == HashEntryI + DISK_TOKEN_SIZE);
Ted Kremenek268ee702008-12-12 18:34:08 +0000259 // Did we reach a #endif? If so, go ahead and consume that token as well.
260 if (isEndif)
Ted Kremeneke5680f32008-12-23 01:30:52 +0000261 CurPtr += DISK_TOKEN_SIZE*2;
Ted Kremenek268ee702008-12-12 18:34:08 +0000262 else
Ted Kremenek41a26602008-12-12 22:05:38 +0000263 LastHashTokPtr = HashEntryI;
Mike Stump1eb44332009-09-09 15:08:12 +0000264
Ted Kremenek268ee702008-12-12 18:34:08 +0000265 return isEndif;
266 }
267
268 // Otherwise, we need to advance. Update CurPtr to point to the '#' token.
Ted Kremenek41a26602008-12-12 22:05:38 +0000269 CurPtr = HashEntryI;
Mike Stump1eb44332009-09-09 15:08:12 +0000270
Ted Kremenek268ee702008-12-12 18:34:08 +0000271 // Update the location of the last observed '#'. This is useful if we
272 // are skipping multiple blocks.
273 LastHashTokPtr = CurPtr;
Ted Kremenek268ee702008-12-12 18:34:08 +0000274
Ted Kremeneke5680f32008-12-23 01:30:52 +0000275 // Skip the '#' token.
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000276 assert(((tok::TokenKind)*CurPtr) == tok::hash);
Ted Kremeneke5680f32008-12-23 01:30:52 +0000277 CurPtr += DISK_TOKEN_SIZE;
Mike Stump1eb44332009-09-09 15:08:12 +0000278
Ted Kremenek268ee702008-12-12 18:34:08 +0000279 // Did we reach a #endif? If so, go ahead and consume that token as well.
Ted Kremeneke5680f32008-12-23 01:30:52 +0000280 if (isEndif) { CurPtr += DISK_TOKEN_SIZE*2; }
Ted Kremenek268ee702008-12-12 18:34:08 +0000281
282 return isEndif;
283}
284
Ted Kremenek30a12ec2008-12-17 23:36:32 +0000285SourceLocation PTHLexer::getSourceLocation() {
Chris Lattner1b5285e2009-01-18 02:10:31 +0000286 // getSourceLocation is not on the hot path. It is used to get the location
287 // of the next token when transitioning back to this lexer when done
Ted Kremenek30a12ec2008-12-17 23:36:32 +0000288 // handling a #included file. Just read the necessary data from the token
289 // data buffer to construct the SourceLocation object.
290 // NOTE: This is a virtual function; hence it is defined out-of-line.
Stephen Hines651f13c2014-04-23 16:59:28 -0700291 using namespace llvm::support;
292
Ted Kremenekb248d532009-01-21 22:41:38 +0000293 const unsigned char *OffsetPtr = CurPtr + (DISK_TOKEN_SIZE - 4);
Stephen Hines651f13c2014-04-23 16:59:28 -0700294 uint32_t Offset = endian::readNext<uint32_t, little, aligned>(OffsetPtr);
Argyrios Kyrtzidisa64ccef2011-09-19 20:40:19 +0000295 return FileStartLoc.getLocWithOffset(Offset);
Ted Kremenek30a12ec2008-12-17 23:36:32 +0000296}
297
Ted Kremenek5f074262009-01-09 22:05:30 +0000298//===----------------------------------------------------------------------===//
Ted Kremenekd8c02922009-02-10 22:16:22 +0000299// PTH file lookup: map from strings to file data.
300//===----------------------------------------------------------------------===//
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000301
302/// PTHFileLookup - This internal data structure is used by the PTHManager
303/// to map from FileEntry objects managed by FileManager to offsets within
304/// the PTH file.
305namespace {
Benjamin Kramer85b45212009-11-28 19:45:26 +0000306class PTHFileData {
Ted Kremenekd8c02922009-02-10 22:16:22 +0000307 const uint32_t TokenOff;
308 const uint32_t PPCondOff;
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000309public:
Ted Kremenekd8c02922009-02-10 22:16:22 +0000310 PTHFileData(uint32_t tokenOff, uint32_t ppCondOff)
311 : TokenOff(tokenOff), PPCondOff(ppCondOff) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000312
313 uint32_t getTokenOffset() const { return TokenOff; }
314 uint32_t getPPCondOffset() const { return PPCondOff; }
Ted Kremenekd8c02922009-02-10 22:16:22 +0000315};
Mike Stump1eb44332009-09-09 15:08:12 +0000316
317
Benjamin Kramer85b45212009-11-28 19:45:26 +0000318class PTHFileLookupCommonTrait {
Ted Kremenekd8c02922009-02-10 22:16:22 +0000319public:
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000320 typedef std::pair<unsigned char, const char*> internal_key_type;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700321 typedef unsigned hash_value_type;
322 typedef unsigned offset_type;
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000323
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700324 static hash_value_type ComputeHash(internal_key_type x) {
Daniel Dunbar2596e422009-10-17 23:52:28 +0000325 return llvm::HashString(x.second);
Ted Kremenekd8c02922009-02-10 22:16:22 +0000326 }
Mike Stump1eb44332009-09-09 15:08:12 +0000327
Ted Kremenekd8c02922009-02-10 22:16:22 +0000328 static std::pair<unsigned, unsigned>
329 ReadKeyDataLength(const unsigned char*& d) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700330 using namespace llvm::support;
331 unsigned keyLen =
332 (unsigned)endian::readNext<uint16_t, little, unaligned>(d);
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000333 unsigned dataLen = (unsigned) *(d++);
334 return std::make_pair(keyLen, dataLen);
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000335 }
Mike Stump1eb44332009-09-09 15:08:12 +0000336
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000337 static internal_key_type ReadKey(const unsigned char* d, unsigned) {
338 unsigned char k = *(d++); // Read the entry kind.
339 return std::make_pair(k, (const char*) d);
Ted Kremenekd8c02922009-02-10 22:16:22 +0000340 }
Ted Kremenek337edcd2009-02-12 03:26:59 +0000341};
Mike Stump1eb44332009-09-09 15:08:12 +0000342
Benjamin Kramer85b45212009-11-28 19:45:26 +0000343class PTHFileLookupTrait : public PTHFileLookupCommonTrait {
Ted Kremenek337edcd2009-02-12 03:26:59 +0000344public:
345 typedef const FileEntry* external_key_type;
346 typedef PTHFileData data_type;
Mike Stump1eb44332009-09-09 15:08:12 +0000347
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000348 static internal_key_type GetInternalKey(const FileEntry* FE) {
349 return std::make_pair((unsigned char) 0x1, FE->getName());
Ted Kremenek337edcd2009-02-12 03:26:59 +0000350 }
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000351
352 static bool EqualKey(internal_key_type a, internal_key_type b) {
353 return a.first == b.first && strcmp(a.second, b.second) == 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000354 }
355
356 static PTHFileData ReadData(const internal_key_type& k,
357 const unsigned char* d, unsigned) {
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000358 assert(k.first == 0x1 && "Only file lookups can match!");
Stephen Hines651f13c2014-04-23 16:59:28 -0700359 using namespace llvm::support;
360 uint32_t x = endian::readNext<uint32_t, little, unaligned>(d);
361 uint32_t y = endian::readNext<uint32_t, little, unaligned>(d);
Mike Stump1eb44332009-09-09 15:08:12 +0000362 return PTHFileData(x, y);
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000363 }
364};
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000365
Benjamin Kramer85b45212009-11-28 19:45:26 +0000366class PTHStringLookupTrait {
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000367public:
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700368 typedef uint32_t data_type;
369 typedef const std::pair<const char*, unsigned> external_key_type;
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000370 typedef external_key_type internal_key_type;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700371 typedef uint32_t hash_value_type;
372 typedef unsigned offset_type;
Mike Stump1eb44332009-09-09 15:08:12 +0000373
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000374 static bool EqualKey(const internal_key_type& a,
375 const internal_key_type& b) {
376 return (a.second == b.second) ? memcmp(a.first, b.first, a.second) == 0
377 : false;
378 }
Mike Stump1eb44332009-09-09 15:08:12 +0000379
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700380 static hash_value_type ComputeHash(const internal_key_type& a) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000381 return llvm::HashString(StringRef(a.first, a.second));
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000382 }
Mike Stump1eb44332009-09-09 15:08:12 +0000383
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000384 // This hopefully will just get inlined and removed by the optimizer.
385 static const internal_key_type&
386 GetInternalKey(const external_key_type& x) { return x; }
Mike Stump1eb44332009-09-09 15:08:12 +0000387
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000388 static std::pair<unsigned, unsigned>
389 ReadKeyDataLength(const unsigned char*& d) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700390 using namespace llvm::support;
391 return std::make_pair(
392 (unsigned)endian::readNext<uint16_t, little, unaligned>(d),
393 sizeof(uint32_t));
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000394 }
Mike Stump1eb44332009-09-09 15:08:12 +0000395
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000396 static std::pair<const char*, unsigned>
397 ReadKey(const unsigned char* d, unsigned n) {
398 assert(n >= 2 && d[n-1] == '\0');
399 return std::make_pair((const char*) d, n-1);
400 }
Mike Stump1eb44332009-09-09 15:08:12 +0000401
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000402 static uint32_t ReadData(const internal_key_type& k, const unsigned char* d,
403 unsigned) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700404 using namespace llvm::support;
405 return endian::readNext<uint32_t, little, unaligned>(d);
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000406 }
407};
Mike Stump1eb44332009-09-09 15:08:12 +0000408
409} // end anonymous namespace
Ted Kremenekd8c02922009-02-10 22:16:22 +0000410
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700411typedef llvm::OnDiskChainedHashTable<PTHFileLookupTrait> PTHFileLookup;
412typedef llvm::OnDiskChainedHashTable<PTHStringLookupTrait> PTHStringIdLookup;
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000413
414//===----------------------------------------------------------------------===//
415// PTHManager methods.
416//===----------------------------------------------------------------------===//
417
418PTHManager::PTHManager(const llvm::MemoryBuffer* buf, void* fileLookup,
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000419 const unsigned char* idDataTable,
Mike Stump1eb44332009-09-09 15:08:12 +0000420 IdentifierInfo** perIDCache,
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000421 void* stringIdLookup, unsigned numIds,
Ted Kremenek68228632009-03-19 22:19:30 +0000422 const unsigned char* spellingBase,
423 const char* originalSourceFile)
Ted Kremenek6183e482008-12-03 01:16:39 +0000424: Buf(buf), PerIDCache(perIDCache), FileLookup(fileLookup),
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000425 IdDataTable(idDataTable), StringIdLookup(stringIdLookup),
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700426 NumIds(numIds), PP(nullptr), SpellingBase(spellingBase),
Ted Kremenek68228632009-03-19 22:19:30 +0000427 OriginalSourceFile(originalSourceFile) {}
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000428
429PTHManager::~PTHManager() {
430 delete Buf;
431 delete (PTHFileLookup*) FileLookup;
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000432 delete (PTHStringIdLookup*) StringIdLookup;
Ted Kremenek0e50b6e2008-12-04 22:47:11 +0000433 free(PerIDCache);
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000434}
435
David Blaikied6471f72011-09-25 23:23:43 +0000436static void InvalidPTH(DiagnosticsEngine &Diags, const char *Msg) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700437 Diags.Report(Diags.getCustomDiagID(DiagnosticsEngine::Error, "%0")) << Msg;
Ted Kremenek26555b12009-01-28 21:02:43 +0000438}
439
David Blaikied6471f72011-09-25 23:23:43 +0000440PTHManager *PTHManager::Create(const std::string &file,
441 DiagnosticsEngine &Diags) {
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000442 // Memory map the PTH file.
Stephen Hines651f13c2014-04-23 16:59:28 -0700443 std::unique_ptr<llvm::MemoryBuffer> File;
Mike Stump1eb44332009-09-09 15:08:12 +0000444
Michael J. Spencer4eeebc42010-12-16 03:28:14 +0000445 if (llvm::MemoryBuffer::getFile(file, File)) {
Michael J. Spencer3a321e22010-12-09 17:36:38 +0000446 // FIXME: Add ec.message() to this diag.
Daniel Dunbar3574f462009-11-12 02:53:48 +0000447 Diags.Report(diag::err_invalid_pth_file) << file;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700448 return nullptr;
Ted Kremenek8a6aec62009-01-28 20:49:33 +0000449 }
Mike Stump1eb44332009-09-09 15:08:12 +0000450
Stephen Hines651f13c2014-04-23 16:59:28 -0700451 using namespace llvm::support;
452
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000453 // Get the buffer ranges and check if there are at least three 32-bit
454 // words at the end of the file.
Roman Divacky31ba6132012-09-06 15:59:27 +0000455 const unsigned char *BufBeg = (const unsigned char*)File->getBufferStart();
456 const unsigned char *BufEnd = (const unsigned char*)File->getBufferEnd();
Ted Kremeneke1b64982009-01-26 21:43:14 +0000457
458 // Check the prologue of the file.
Richard Smithc141b512012-08-17 03:55:43 +0000459 if ((BufEnd - BufBeg) < (signed)(sizeof("cfe-pth") + 4 + 4) ||
460 memcmp(BufBeg, "cfe-pth", sizeof("cfe-pth")) != 0) {
Daniel Dunbar3574f462009-11-12 02:53:48 +0000461 Diags.Report(diag::err_invalid_pth_file) << file;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700462 return nullptr;
Ted Kremenek26555b12009-01-28 21:02:43 +0000463 }
Mike Stump1eb44332009-09-09 15:08:12 +0000464
Ted Kremenek67d15052009-01-26 21:50:21 +0000465 // Read the PTH version.
Richard Smithc141b512012-08-17 03:55:43 +0000466 const unsigned char *p = BufBeg + (sizeof("cfe-pth"));
Stephen Hines651f13c2014-04-23 16:59:28 -0700467 unsigned Version = endian::readNext<uint32_t, little, aligned>(p);
Mike Stump1eb44332009-09-09 15:08:12 +0000468
Daniel Dunbar3574f462009-11-12 02:53:48 +0000469 if (Version < PTHManager::Version) {
470 InvalidPTH(Diags,
Mike Stump1eb44332009-09-09 15:08:12 +0000471 Version < PTHManager::Version
Ted Kremenek26555b12009-01-28 21:02:43 +0000472 ? "PTH file uses an older PTH format that is no longer supported"
473 : "PTH file uses a newer PTH format that cannot be read");
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700474 return nullptr;
Ted Kremenek26555b12009-01-28 21:02:43 +0000475 }
Ted Kremenek67d15052009-01-26 21:50:21 +0000476
Mike Stump1eb44332009-09-09 15:08:12 +0000477 // Compute the address of the index table at the end of the PTH file.
Ted Kremeneka4bd8eb2009-02-11 23:34:32 +0000478 const unsigned char *PrologueOffset = p;
Mike Stump1eb44332009-09-09 15:08:12 +0000479
Ted Kremeneka4bd8eb2009-02-11 23:34:32 +0000480 if (PrologueOffset >= BufEnd) {
Daniel Dunbar3574f462009-11-12 02:53:48 +0000481 Diags.Report(diag::err_invalid_pth_file) << file;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700482 return nullptr;
Ted Kremenek26555b12009-01-28 21:02:43 +0000483 }
Mike Stump1eb44332009-09-09 15:08:12 +0000484
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000485 // Construct the file lookup table. This will be used for mapping from
486 // FileEntry*'s to cached tokens.
Ted Kremeneka4bd8eb2009-02-11 23:34:32 +0000487 const unsigned char* FileTableOffset = PrologueOffset + sizeof(uint32_t)*2;
Stephen Hines651f13c2014-04-23 16:59:28 -0700488 const unsigned char *FileTable =
489 BufBeg + endian::readNext<uint32_t, little, aligned>(FileTableOffset);
Mike Stump1eb44332009-09-09 15:08:12 +0000490
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000491 if (!(FileTable > BufBeg && FileTable < BufEnd)) {
Daniel Dunbar3574f462009-11-12 02:53:48 +0000492 Diags.Report(diag::err_invalid_pth_file) << file;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700493 return nullptr; // FIXME: Proper error diagnostic?
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000494 }
Mike Stump1eb44332009-09-09 15:08:12 +0000495
Stephen Hines651f13c2014-04-23 16:59:28 -0700496 std::unique_ptr<PTHFileLookup> FL(PTHFileLookup::Create(FileTable, BufBeg));
Mike Stump1eb44332009-09-09 15:08:12 +0000497
Ted Kremenek1d201972009-03-20 17:54:25 +0000498 // Warn if the PTH file is empty. We still want to create a PTHManager
499 // as the PTH could be used with -include-pth.
500 if (FL->isEmpty())
Daniel Dunbar3574f462009-11-12 02:53:48 +0000501 InvalidPTH(Diags, "PTH file contains no cached source data");
Mike Stump1eb44332009-09-09 15:08:12 +0000502
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000503 // Get the location of the table mapping from persistent ids to the
504 // data needed to reconstruct identifiers.
Ted Kremeneka4bd8eb2009-02-11 23:34:32 +0000505 const unsigned char* IDTableOffset = PrologueOffset + sizeof(uint32_t)*0;
Stephen Hines651f13c2014-04-23 16:59:28 -0700506 const unsigned char *IData =
507 BufBeg + endian::readNext<uint32_t, little, aligned>(IDTableOffset);
Mike Stump1eb44332009-09-09 15:08:12 +0000508
Ted Kremenekcdd8f212009-01-21 07:34:28 +0000509 if (!(IData >= BufBeg && IData < BufEnd)) {
Daniel Dunbar3574f462009-11-12 02:53:48 +0000510 Diags.Report(diag::err_invalid_pth_file) << file;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700511 return nullptr;
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000512 }
Mike Stump1eb44332009-09-09 15:08:12 +0000513
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000514 // Get the location of the hashtable mapping between strings and
515 // persistent IDs.
Ted Kremeneka4bd8eb2009-02-11 23:34:32 +0000516 const unsigned char* StringIdTableOffset = PrologueOffset + sizeof(uint32_t)*1;
Stephen Hines651f13c2014-04-23 16:59:28 -0700517 const unsigned char *StringIdTable =
518 BufBeg + endian::readNext<uint32_t, little, aligned>(StringIdTableOffset);
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000519 if (!(StringIdTable >= BufBeg && StringIdTable < BufEnd)) {
Daniel Dunbar3574f462009-11-12 02:53:48 +0000520 Diags.Report(diag::err_invalid_pth_file) << file;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700521 return nullptr;
Ted Kremenek72b1b152009-01-15 18:47:46 +0000522 }
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000523
Stephen Hines651f13c2014-04-23 16:59:28 -0700524 std::unique_ptr<PTHStringIdLookup> SL(
525 PTHStringIdLookup::Create(StringIdTable, BufBeg));
Mike Stump1eb44332009-09-09 15:08:12 +0000526
Ted Kremenek277faca2009-01-27 00:01:05 +0000527 // Get the location of the spelling cache.
Ted Kremeneka4bd8eb2009-02-11 23:34:32 +0000528 const unsigned char* spellingBaseOffset = PrologueOffset + sizeof(uint32_t)*3;
Stephen Hines651f13c2014-04-23 16:59:28 -0700529 const unsigned char *spellingBase =
530 BufBeg + endian::readNext<uint32_t, little, aligned>(spellingBaseOffset);
Ted Kremenek277faca2009-01-27 00:01:05 +0000531 if (!(spellingBase >= BufBeg && spellingBase < BufEnd)) {
Daniel Dunbar3574f462009-11-12 02:53:48 +0000532 Diags.Report(diag::err_invalid_pth_file) << file;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700533 return nullptr;
Ted Kremenek277faca2009-01-27 00:01:05 +0000534 }
Mike Stump1eb44332009-09-09 15:08:12 +0000535
Ted Kremenek6183e482008-12-03 01:16:39 +0000536 // Get the number of IdentifierInfos and pre-allocate the identifier cache.
Stephen Hines651f13c2014-04-23 16:59:28 -0700537 uint32_t NumIds = endian::readNext<uint32_t, little, aligned>(IData);
Mike Stump1eb44332009-09-09 15:08:12 +0000538
Chris Lattnerfc8f0e12011-04-15 05:22:18 +0000539 // Pre-allocate the persistent ID -> IdentifierInfo* cache. We use calloc()
Ted Kremenek6183e482008-12-03 01:16:39 +0000540 // so that we in the best case only zero out memory once when the OS returns
541 // us new pages.
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700542 IdentifierInfo **PerIDCache = nullptr;
Mike Stump1eb44332009-09-09 15:08:12 +0000543
Ted Kremenekcdd8f212009-01-21 07:34:28 +0000544 if (NumIds) {
Mike Stump1eb44332009-09-09 15:08:12 +0000545 PerIDCache = (IdentifierInfo**)calloc(NumIds, sizeof(*PerIDCache));
Ted Kremenekcdd8f212009-01-21 07:34:28 +0000546 if (!PerIDCache) {
Daniel Dunbar3574f462009-11-12 02:53:48 +0000547 InvalidPTH(Diags, "Could not allocate memory for processing PTH file");
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700548 return nullptr;
Ted Kremenekcdd8f212009-01-21 07:34:28 +0000549 }
Ted Kremenek6183e482008-12-03 01:16:39 +0000550 }
Ted Kremenekcdd8f212009-01-21 07:34:28 +0000551
Ted Kremenek68228632009-03-19 22:19:30 +0000552 // Compute the address of the original source file.
553 const unsigned char* originalSourceBase = PrologueOffset + sizeof(uint32_t)*4;
Stephen Hines651f13c2014-04-23 16:59:28 -0700554 unsigned len =
555 endian::readNext<uint16_t, little, unaligned>(originalSourceBase);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700556 if (!len) originalSourceBase = nullptr;
Mike Stump1eb44332009-09-09 15:08:12 +0000557
Ted Kremenek72b1b152009-01-15 18:47:46 +0000558 // Create the new PTHManager.
Stephen Hines651f13c2014-04-23 16:59:28 -0700559 return new PTHManager(File.release(), FL.release(), IData, PerIDCache,
560 SL.release(), NumIds, spellingBase,
561 (const char *)originalSourceBase);
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000562}
Ted Kremenek68228632009-03-19 22:19:30 +0000563
Chris Lattner77ecb3a2009-01-18 02:57:21 +0000564IdentifierInfo* PTHManager::LazilyCreateIdentifierInfo(unsigned PersistentID) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700565 using namespace llvm::support;
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000566 // Look in the PTH file for the string data for the IdentifierInfo object.
Chris Lattner77ecb3a2009-01-18 02:57:21 +0000567 const unsigned char* TableEntry = IdDataTable + sizeof(uint32_t)*PersistentID;
Stephen Hines651f13c2014-04-23 16:59:28 -0700568 const unsigned char *IDData =
569 (const unsigned char *)Buf->getBufferStart() +
570 endian::readNext<uint32_t, little, aligned>(TableEntry);
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000571 assert(IDData < (const unsigned char*)Buf->getBufferEnd());
Mike Stump1eb44332009-09-09 15:08:12 +0000572
Ted Kremenek72b1b152009-01-15 18:47:46 +0000573 // Allocate the object.
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000574 std::pair<IdentifierInfo,const unsigned char*> *Mem =
575 Alloc.Allocate<std::pair<IdentifierInfo,const unsigned char*> >();
Ted Kremenek72b1b152009-01-15 18:47:46 +0000576
577 Mem->second = IDData;
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000578 assert(IDData[0] != '\0');
Ted Kremenekea9c26b2009-01-20 23:28:34 +0000579 IdentifierInfo *II = new ((void*) Mem) IdentifierInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000580
Ted Kremenek72b1b152009-01-15 18:47:46 +0000581 // Store the new IdentifierInfo in the cache.
Chris Lattner77ecb3a2009-01-18 02:57:21 +0000582 PerIDCache[PersistentID] = II;
Daniel Dunbare013d682009-10-18 20:26:12 +0000583 assert(II->getNameStart() && II->getNameStart()[0] != '\0');
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000584 return II;
585}
586
Chris Lattner5f9e2722011-07-23 10:55:15 +0000587IdentifierInfo* PTHManager::get(StringRef Name) {
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000588 PTHStringIdLookup& SL = *((PTHStringIdLookup*)StringIdLookup);
589 // Double check our assumption that the last character isn't '\0'.
David Blaikie681d83d2011-09-22 01:35:49 +0000590 assert(Name.empty() || Name.back() != '\0');
Kovarththanan Rajaratnam700030e2010-03-12 08:23:34 +0000591 PTHStringIdLookup::iterator I = SL.find(std::make_pair(Name.data(),
592 Name.size()));
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000593 if (I == SL.end()) // No identifier found?
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700594 return nullptr;
Ted Kremenek72b1b152009-01-15 18:47:46 +0000595
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000596 // Match found. Return the identifier!
597 assert(*I > 0);
598 return GetIdentifierInfo(*I-1);
599}
Ted Kremenek72b1b152009-01-15 18:47:46 +0000600
Chris Lattnerf056d922009-01-17 08:06:50 +0000601PTHLexer *PTHManager::CreateLexer(FileID FID) {
602 const FileEntry *FE = PP->getSourceManager().getFileEntryForID(FID);
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000603 if (!FE)
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700604 return nullptr;
Mike Stump1eb44332009-09-09 15:08:12 +0000605
Stephen Hines651f13c2014-04-23 16:59:28 -0700606 using namespace llvm::support;
607
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000608 // Lookup the FileEntry object in our file lookup data structure. It will
609 // return a variant that indicates whether or not there is an offset within
610 // the PTH file that contains cached tokens.
Ted Kremenekd8c02922009-02-10 22:16:22 +0000611 PTHFileLookup& PFL = *((PTHFileLookup*)FileLookup);
612 PTHFileLookup::iterator I = PFL.find(FE);
Mike Stump1eb44332009-09-09 15:08:12 +0000613
Ted Kremenekd8c02922009-02-10 22:16:22 +0000614 if (I == PFL.end()) // No tokens available?
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700615 return nullptr;
Mike Stump1eb44332009-09-09 15:08:12 +0000616
617 const PTHFileData& FileData = *I;
618
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000619 const unsigned char *BufStart = (const unsigned char *)Buf->getBufferStart();
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000620 // Compute the offset of the token data within the buffer.
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000621 const unsigned char* data = BufStart + FileData.getTokenOffset();
Ted Kremenek268ee702008-12-12 18:34:08 +0000622
623 // Get the location of pp-conditional table.
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000624 const unsigned char* ppcond = BufStart + FileData.getPPCondOffset();
Stephen Hines651f13c2014-04-23 16:59:28 -0700625 uint32_t Len = endian::readNext<uint32_t, little, aligned>(ppcond);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700626 if (Len == 0) ppcond = nullptr;
Mike Stump1eb44332009-09-09 15:08:12 +0000627
Ted Kremenek72b1b152009-01-15 18:47:46 +0000628 assert(PP && "No preprocessor set yet!");
Mike Stump1eb44332009-09-09 15:08:12 +0000629 return new PTHLexer(*PP, FID, data, ppcond, *this);
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000630}
Ted Kremenek337edcd2009-02-12 03:26:59 +0000631
632//===----------------------------------------------------------------------===//
633// 'stat' caching.
634//===----------------------------------------------------------------------===//
635
636namespace {
Benjamin Kramer85b45212009-11-28 19:45:26 +0000637class PTHStatData {
Ted Kremenek337edcd2009-02-12 03:26:59 +0000638public:
Rafael Espindola0fda0f72013-08-01 21:42:11 +0000639 const bool HasData;
640 uint64_t Size;
641 time_t ModTime;
642 llvm::sys::fs::UniqueID UniqueID;
643 bool IsDirectory;
Mike Stump1eb44332009-09-09 15:08:12 +0000644
Rafael Espindola0fda0f72013-08-01 21:42:11 +0000645 PTHStatData(uint64_t Size, time_t ModTime, llvm::sys::fs::UniqueID UniqueID,
646 bool IsDirectory)
647 : HasData(true), Size(Size), ModTime(ModTime), UniqueID(UniqueID),
648 IsDirectory(IsDirectory) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000649
Rafael Espindola0fda0f72013-08-01 21:42:11 +0000650 PTHStatData() : HasData(false) {}
Ted Kremenek337edcd2009-02-12 03:26:59 +0000651};
Mike Stump1eb44332009-09-09 15:08:12 +0000652
Benjamin Kramer85b45212009-11-28 19:45:26 +0000653class PTHStatLookupTrait : public PTHFileLookupCommonTrait {
Ted Kremenek337edcd2009-02-12 03:26:59 +0000654public:
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000655 typedef const char* external_key_type; // const char*
Ted Kremenek337edcd2009-02-12 03:26:59 +0000656 typedef PTHStatData data_type;
Mike Stump1eb44332009-09-09 15:08:12 +0000657
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000658 static internal_key_type GetInternalKey(const char *path) {
659 // The key 'kind' doesn't matter here because it is ignored in EqualKey.
660 return std::make_pair((unsigned char) 0x0, path);
661 }
662
663 static bool EqualKey(internal_key_type a, internal_key_type b) {
664 // When doing 'stat' lookups we don't care about the kind of 'a' and 'b',
665 // just the paths.
666 return strcmp(a.second, b.second) == 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000667 }
668
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000669 static data_type ReadData(const internal_key_type& k, const unsigned char* d,
Mike Stump1eb44332009-09-09 15:08:12 +0000670 unsigned) {
671
Ted Kremenekad6ce5c2009-02-13 22:07:44 +0000672 if (k.first /* File or Directory */) {
Rafael Espindola0fda0f72013-08-01 21:42:11 +0000673 bool IsDirectory = true;
674 if (k.first == 0x1 /* File */) {
675 IsDirectory = false;
676 d += 4 * 2; // Skip the first 2 words.
677 }
678
Stephen Hines651f13c2014-04-23 16:59:28 -0700679 using namespace llvm::support;
680
681 uint64_t File = endian::readNext<uint64_t, little, unaligned>(d);
682 uint64_t Device = endian::readNext<uint64_t, little, unaligned>(d);
Rafael Espindola0fda0f72013-08-01 21:42:11 +0000683 llvm::sys::fs::UniqueID UniqueID(File, Device);
Stephen Hines651f13c2014-04-23 16:59:28 -0700684 time_t ModTime = endian::readNext<uint64_t, little, unaligned>(d);
685 uint64_t Size = endian::readNext<uint64_t, little, unaligned>(d);
Rafael Espindola0fda0f72013-08-01 21:42:11 +0000686 return data_type(Size, ModTime, UniqueID, IsDirectory);
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000687 }
Ted Kremenekad6ce5c2009-02-13 22:07:44 +0000688
689 // Negative stat. Don't read anything.
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000690 return data_type();
Ted Kremenek337edcd2009-02-12 03:26:59 +0000691 }
692};
Ted Kremenek337edcd2009-02-12 03:26:59 +0000693
Chris Lattner10e286a2010-11-23 19:19:34 +0000694class PTHStatCache : public FileSystemStatCache {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700695 typedef llvm::OnDiskChainedHashTable<PTHStatLookupTrait> CacheTy;
Ted Kremenek337edcd2009-02-12 03:26:59 +0000696 CacheTy Cache;
697
Mike Stump1eb44332009-09-09 15:08:12 +0000698public:
Ted Kremenek337edcd2009-02-12 03:26:59 +0000699 PTHStatCache(PTHFileLookup &FL) :
700 Cache(FL.getNumBuckets(), FL.getNumEntries(), FL.getBuckets(),
701 FL.getBase()) {}
702
703 ~PTHStatCache() {}
Mike Stump1eb44332009-09-09 15:08:12 +0000704
Rafael Espindola0fda0f72013-08-01 21:42:11 +0000705 LookupResult getStat(const char *Path, FileData &Data, bool isFile,
Stephen Hines651f13c2014-04-23 16:59:28 -0700706 vfs::File **F, vfs::FileSystem &FS) override {
Ted Kremenek337edcd2009-02-12 03:26:59 +0000707 // Do the lookup for the file's data in the PTH file.
Chris Lattner10e286a2010-11-23 19:19:34 +0000708 CacheTy::iterator I = Cache.find(Path);
Ted Kremenek337edcd2009-02-12 03:26:59 +0000709
710 // If we don't get a hit in the PTH file just forward to 'stat'.
Kovarththanan Rajaratnam700030e2010-03-12 08:23:34 +0000711 if (I == Cache.end())
Stephen Hines651f13c2014-04-23 16:59:28 -0700712 return statChained(Path, Data, isFile, F, FS);
Mike Stump1eb44332009-09-09 15:08:12 +0000713
Rafael Espindola0fda0f72013-08-01 21:42:11 +0000714 const PTHStatData &D = *I;
Mike Stump1eb44332009-09-09 15:08:12 +0000715
Rafael Espindola0fda0f72013-08-01 21:42:11 +0000716 if (!D.HasData)
Chris Lattnerd6f61112010-11-23 20:05:15 +0000717 return CacheMissing;
Ted Kremenekad6ce5c2009-02-13 22:07:44 +0000718
Stephen Hines651f13c2014-04-23 16:59:28 -0700719 Data.Name = Path;
Rafael Espindola0fda0f72013-08-01 21:42:11 +0000720 Data.Size = D.Size;
721 Data.ModTime = D.ModTime;
722 Data.UniqueID = D.UniqueID;
723 Data.IsDirectory = D.IsDirectory;
724 Data.IsNamedPipe = false;
725 Data.InPCH = true;
726
Chris Lattnerd6f61112010-11-23 20:05:15 +0000727 return CacheExists;
Ted Kremenek337edcd2009-02-12 03:26:59 +0000728 }
729};
Ted Kremenekd5785692009-02-23 23:27:54 +0000730} // end anonymous namespace
Ted Kremenek337edcd2009-02-12 03:26:59 +0000731
Chris Lattner10e286a2010-11-23 19:19:34 +0000732FileSystemStatCache *PTHManager::createStatCache() {
Ted Kremenek5f747d12009-02-12 03:45:39 +0000733 return new PTHStatCache(*((PTHFileLookup*) FileLookup));
Ted Kremenek337edcd2009-02-12 03:26:59 +0000734}