blob: e2629a3b2c498cbc894a313a5f1b284b35adce51 [file] [log] [blame]
Ted Kremenek7cd62452008-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 Carruth3a022472012-12-04 09:13:33 +000014#include "clang/Lex/PTHLexer.h"
Ted Kremenek33eeabd2008-12-03 00:38:03 +000015#include "clang/Basic/FileManager.h"
Chris Lattner226efd32010-11-23 19:19:34 +000016#include "clang/Basic/FileSystemStatCache.h"
Ted Kremenek33eeabd2008-12-03 00:38:03 +000017#include "clang/Basic/IdentifierTable.h"
Douglas Gregor52289d32009-04-20 07:08:21 +000018#include "clang/Basic/OnDiskHashTable.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000019#include "clang/Basic/TokenKinds.h"
Daniel Dunbar1a54e3f2009-11-12 02:53:48 +000020#include "clang/Lex/LexDiagnostic.h"
Ted Kremenek33eeabd2008-12-03 00:38:03 +000021#include "clang/Lex/PTHManager.h"
Ted Kremenek33eeabd2008-12-03 00:38:03 +000022#include "clang/Lex/Preprocessor.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000023#include "clang/Lex/Token.h"
Ted Kremenek33eeabd2008-12-03 00:38:03 +000024#include "llvm/ADT/OwningPtr.h"
Daniel Dunbarf8502d52009-10-17 23:52:28 +000025#include "llvm/ADT/StringExtras.h"
26#include "llvm/ADT/StringMap.h"
Chris Lattner34eab392009-01-22 23:50:07 +000027#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencerf25faaa2010-12-09 17:36:38 +000028#include "llvm/Support/system_error.h"
Ted Kremenek7cd62452008-11-12 21:37:15 +000029using namespace clang;
Douglas Gregor52289d32009-04-20 07:08:21 +000030using namespace clang::io;
Ted Kremenek7cd62452008-11-12 21:37:15 +000031
Ted Kremenek8433f0b2009-01-19 23:13:15 +000032#define DISK_TOKEN_SIZE (1+1+2+4+4)
Ted Kremenek56572ab2008-12-12 18:34:08 +000033
Ted Kremenek33eeabd2008-12-03 00:38:03 +000034//===----------------------------------------------------------------------===//
Ted Kremenek1b18ad22008-12-23 01:30:52 +000035// PTHLexer methods.
36//===----------------------------------------------------------------------===//
37
Chris Lattnereb097542009-01-18 01:57:14 +000038PTHLexer::PTHLexer(Preprocessor &PP, FileID FID, const unsigned char *D,
Ted Kremenek8d178f42009-01-27 00:01:05 +000039 const unsigned char *ppcond, PTHManager &PM)
Chris Lattnerd32480d2009-01-17 06:22:33 +000040 : PreprocessorLexer(&PP, FID), TokBuf(D), CurPtr(D), LastHashTokPtr(0),
Ted Kremenek8d178f42009-01-27 00:01:05 +000041 PPCond(ppcond), CurPPCondPtr(ppcond), PTHMgr(PM) {
Mike Stump11289f42009-09-09 15:08:12 +000042
Chris Lattnerd32480d2009-01-17 06:22:33 +000043 FileStartLoc = PP.getSourceManager().getLocForStartOfFile(FID);
Ted Kremenek47b8cf62009-01-09 22:05:30 +000044}
Ted Kremenek1b18ad22008-12-23 01:30:52 +000045
Eli Friedman0834a4b2013-09-19 00:41:32 +000046bool PTHLexer::Lex(Token& Tok) {
Ted Kremenek66076a92008-12-23 02:30:15 +000047 //===--------------------------------------==//
48 // Read the raw token data.
49 //===--------------------------------------==//
Mike Stump11289f42009-09-09 15:08:12 +000050
Ted Kremenek66076a92008-12-23 02:30:15 +000051 // Shadow CurPtr into an automatic variable.
Mike Stump11289f42009-09-09 15:08:12 +000052 const unsigned char *CurPtrShadow = CurPtr;
Ted Kremenek66076a92008-12-23 02:30:15 +000053
Chris Lattner137d6492009-01-18 02:10:31 +000054 // Read in the data for the token.
Chris Lattnerfec54702009-01-22 19:48:26 +000055 unsigned Word0 = ReadLE32(CurPtrShadow);
56 uint32_t IdentifierID = ReadLE32(CurPtrShadow);
57 uint32_t FileOffset = ReadLE32(CurPtrShadow);
Mike Stump11289f42009-09-09 15:08:12 +000058
Ted Kremenek8433f0b2009-01-19 23:13:15 +000059 tok::TokenKind TKind = (tok::TokenKind) (Word0 & 0xFF);
60 Token::TokenFlags TFlags = (Token::TokenFlags) ((Word0 >> 8) & 0xFF);
Chris Lattner47def972009-01-21 07:21:56 +000061 uint32_t Len = Word0 >> 16;
Ted Kremenek8433f0b2009-01-19 23:13:15 +000062
Chris Lattner47def972009-01-21 07:21:56 +000063 CurPtr = CurPtrShadow;
Mike Stump11289f42009-09-09 15:08:12 +000064
Ted Kremenek66076a92008-12-23 02:30:15 +000065 //===--------------------------------------==//
66 // Construct the token itself.
67 //===--------------------------------------==//
Mike Stump11289f42009-09-09 15:08:12 +000068
Ted Kremenek66076a92008-12-23 02:30:15 +000069 Tok.startToken();
Chris Lattner18fc6ce2009-01-18 02:34:01 +000070 Tok.setKind(TKind);
71 Tok.setFlag(TFlags);
Ted Kremenek78cc2472008-12-23 19:24:24 +000072 assert(!LexingRawMode);
Argyrios Kyrtzidise6e67de2011-09-19 20:40:19 +000073 Tok.setLocation(FileStartLoc.getLocWithOffset(FileOffset));
Ted Kremenek66076a92008-12-23 02:30:15 +000074 Tok.setLength(Len);
75
Chris Lattner3029b352009-01-21 07:50:06 +000076 // Handle identifiers.
Ted Kremenek8d178f42009-01-27 00:01:05 +000077 if (Tok.isLiteral()) {
78 Tok.setLiteralData((const char*) (PTHMgr.SpellingBase + IdentifierID));
79 }
80 else if (IdentifierID) {
Chris Lattner3029b352009-01-21 07:50:06 +000081 MIOpt.ReadToken();
82 IdentifierInfo *II = PTHMgr.GetIdentifierInfo(IdentifierID-1);
Mike Stump11289f42009-09-09 15:08:12 +000083
Chris Lattner3029b352009-01-21 07:50:06 +000084 Tok.setIdentifierInfo(II);
Mike Stump11289f42009-09-09 15:08:12 +000085
Chris Lattner1f6c7fe2009-01-23 18:35:48 +000086 // Change the kind of this identifier to the appropriate token kind, e.g.
87 // turning "for" into a keyword.
88 Tok.setKind(II->getTokenID());
Mike Stump11289f42009-09-09 15:08:12 +000089
Chris Lattner3029b352009-01-21 07:50:06 +000090 if (II->isHandleIdentifierCase())
Eli Friedman0834a4b2013-09-19 00:41:32 +000091 return PP->HandleIdentifier(Tok);
92
93 return true;
Chris Lattner3029b352009-01-21 07:50:06 +000094 }
Mike Stump11289f42009-09-09 15:08:12 +000095
Ted Kremenek66076a92008-12-23 02:30:15 +000096 //===--------------------------------------==//
97 // Process the token.
98 //===--------------------------------------==//
Chris Lattner18fc6ce2009-01-18 02:34:01 +000099 if (TKind == tok::eof) {
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000100 // Save the end-of-file token.
101 EofToken = Tok;
Mike Stump11289f42009-09-09 15:08:12 +0000102
Ted Kremenek78cc2472008-12-23 19:24:24 +0000103 assert(!ParsingPreprocessorDirective);
104 assert(!LexingRawMode);
Mike Stump11289f42009-09-09 15:08:12 +0000105
Eli Friedman0834a4b2013-09-19 00:41:32 +0000106 return LexEndOfFile(Tok);
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000107 }
Mike Stump11289f42009-09-09 15:08:12 +0000108
Chris Lattner18fc6ce2009-01-18 02:34:01 +0000109 if (TKind == tok::hash && Tok.isAtStartOfLine()) {
Ted Kremenek78cc2472008-12-23 19:24:24 +0000110 LastHashTokPtr = CurPtr - DISK_TOKEN_SIZE;
111 assert(!LexingRawMode);
112 PP->HandleDirective(Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000113
Eli Friedman0834a4b2013-09-19 00:41:32 +0000114 return false;
Ted Kremenek78cc2472008-12-23 19:24:24 +0000115 }
Mike Stump11289f42009-09-09 15:08:12 +0000116
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000117 if (TKind == tok::eod) {
Ted Kremenek78cc2472008-12-23 19:24:24 +0000118 assert(ParsingPreprocessorDirective);
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000119 ParsingPreprocessorDirective = false;
Eli Friedman0834a4b2013-09-19 00:41:32 +0000120 return true;
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000121 }
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000122
Ted Kremenek78cc2472008-12-23 19:24:24 +0000123 MIOpt.ReadToken();
Eli Friedman0834a4b2013-09-19 00:41:32 +0000124 return true;
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000125}
126
Ted Kremenek2bd41d12010-07-27 02:59:02 +0000127bool PTHLexer::LexEndOfFile(Token &Result) {
128 // If we hit the end of the file while parsing a preprocessor directive,
129 // end the preprocessor directive first. The next token returned will
130 // then be the end of file.
131 if (ParsingPreprocessorDirective) {
132 ParsingPreprocessorDirective = false; // Done parsing the "line".
133 return true; // Have a token.
134 }
135
136 assert(!LexingRawMode);
137
138 // If we are in a #if directive, emit an error.
139 while (!ConditionalStack.empty()) {
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000140 if (PP->getCodeCompletionFileLoc() != FileStartLoc)
Douglas Gregor02690ba2010-08-12 17:04:55 +0000141 PP->Diag(ConditionalStack.back().IfLoc,
142 diag::err_pp_unterminated_conditional);
Ted Kremenek2bd41d12010-07-27 02:59:02 +0000143 ConditionalStack.pop_back();
144 }
145
146 // Finally, let the preprocessor handle this.
147 return PP->HandleEndOfFile(Result);
148}
149
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000150// FIXME: We can just grab the last token instead of storing a copy
151// into EofToken.
Ted Kremenek78cc2472008-12-23 19:24:24 +0000152void PTHLexer::getEOF(Token& Tok) {
Ted Kremenek8ae06622009-01-09 00:36:11 +0000153 assert(EofToken.is(tok::eof));
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000154 Tok = EofToken;
155}
156
157void PTHLexer::DiscardToEndOfLine() {
158 assert(ParsingPreprocessorDirective && ParsingFilename == false &&
159 "Must be in a preprocessing directive!");
160
161 // We assume that if the preprocessor wishes to discard to the end of
162 // the line that it also means to end the current preprocessor directive.
163 ParsingPreprocessorDirective = false;
Mike Stump11289f42009-09-09 15:08:12 +0000164
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000165 // Skip tokens by only peeking at their token kind and the flags.
166 // We don't need to actually reconstruct full tokens from the token buffer.
167 // This saves some copies and it also reduces IdentifierInfo* lookup.
Chris Lattnereb097542009-01-18 01:57:14 +0000168 const unsigned char* p = CurPtr;
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000169 while (1) {
170 // Read the token kind. Are we at the end of the file?
171 tok::TokenKind x = (tok::TokenKind) (uint8_t) *p;
172 if (x == tok::eof) break;
Mike Stump11289f42009-09-09 15:08:12 +0000173
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000174 // Read the token flags. Are we at the start of the next line?
175 Token::TokenFlags y = (Token::TokenFlags) (uint8_t) p[1];
176 if (y & Token::StartOfLine) break;
177
178 // Skip to the next token.
179 p += DISK_TOKEN_SIZE;
180 }
Mike Stump11289f42009-09-09 15:08:12 +0000181
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000182 CurPtr = p;
183}
184
Ted Kremenek56572ab2008-12-12 18:34:08 +0000185/// SkipBlock - Used by Preprocessor to skip the current conditional block.
186bool PTHLexer::SkipBlock() {
187 assert(CurPPCondPtr && "No cached PP conditional information.");
188 assert(LastHashTokPtr && "No known '#' token.");
Mike Stump11289f42009-09-09 15:08:12 +0000189
Chris Lattnereb097542009-01-18 01:57:14 +0000190 const unsigned char* HashEntryI = 0;
Ted Kremenek56572ab2008-12-12 18:34:08 +0000191 uint32_t TableIdx;
Mike Stump11289f42009-09-09 15:08:12 +0000192
Ted Kremenek56572ab2008-12-12 18:34:08 +0000193 do {
Ted Kremenek877556f2008-12-12 22:05:38 +0000194 // Read the token offset from the side-table.
Ted Kremenekdb908592012-09-16 06:18:45 +0000195 uint32_t Offset = ReadLE32(CurPPCondPtr);
Mike Stump11289f42009-09-09 15:08:12 +0000196
197 // Read the target table index from the side-table.
Chris Lattnerfec54702009-01-22 19:48:26 +0000198 TableIdx = ReadLE32(CurPPCondPtr);
Mike Stump11289f42009-09-09 15:08:12 +0000199
Ted Kremenek877556f2008-12-12 22:05:38 +0000200 // Compute the actual memory address of the '#' token data for this entry.
201 HashEntryI = TokBuf + Offset;
202
203 // Optmization: "Sibling jumping". #if...#else...#endif blocks can
204 // contain nested blocks. In the side-table we can jump over these
205 // nested blocks instead of doing a linear search if the next "sibling"
206 // entry is not at a location greater than LastHashTokPtr.
207 if (HashEntryI < LastHashTokPtr && TableIdx) {
208 // In the side-table we are still at an entry for a '#' token that
209 // is earlier than the last one we saw. Check if the location we would
210 // stride gets us closer.
Chris Lattnereb097542009-01-18 01:57:14 +0000211 const unsigned char* NextPPCondPtr =
212 PPCond + TableIdx*(sizeof(uint32_t)*2);
Ted Kremenek877556f2008-12-12 22:05:38 +0000213 assert(NextPPCondPtr >= CurPPCondPtr);
214 // Read where we should jump to.
Ted Kremenekdb908592012-09-16 06:18:45 +0000215 const unsigned char* HashEntryJ = TokBuf + ReadLE32(NextPPCondPtr);
Mike Stump11289f42009-09-09 15:08:12 +0000216
Ted Kremenek877556f2008-12-12 22:05:38 +0000217 if (HashEntryJ <= LastHashTokPtr) {
218 // Jump directly to the next entry in the side table.
219 HashEntryI = HashEntryJ;
Chris Lattnerfec54702009-01-22 19:48:26 +0000220 TableIdx = ReadLE32(NextPPCondPtr);
Ted Kremenek877556f2008-12-12 22:05:38 +0000221 CurPPCondPtr = NextPPCondPtr;
222 }
223 }
Ted Kremenek56572ab2008-12-12 18:34:08 +0000224 }
Mike Stump11289f42009-09-09 15:08:12 +0000225 while (HashEntryI < LastHashTokPtr);
Ted Kremenek877556f2008-12-12 22:05:38 +0000226 assert(HashEntryI == LastHashTokPtr && "No PP-cond entry found for '#'");
Ted Kremenek56572ab2008-12-12 18:34:08 +0000227 assert(TableIdx && "No jumping from #endifs.");
Mike Stump11289f42009-09-09 15:08:12 +0000228
Ted Kremenek56572ab2008-12-12 18:34:08 +0000229 // Update our side-table iterator.
Chris Lattnereb097542009-01-18 01:57:14 +0000230 const unsigned char* NextPPCondPtr = PPCond + TableIdx*(sizeof(uint32_t)*2);
Ted Kremenek56572ab2008-12-12 18:34:08 +0000231 assert(NextPPCondPtr >= CurPPCondPtr);
232 CurPPCondPtr = NextPPCondPtr;
Mike Stump11289f42009-09-09 15:08:12 +0000233
Ted Kremenek56572ab2008-12-12 18:34:08 +0000234 // Read where we should jump to.
Chris Lattnerfec54702009-01-22 19:48:26 +0000235 HashEntryI = TokBuf + ReadLE32(NextPPCondPtr);
236 uint32_t NextIdx = ReadLE32(NextPPCondPtr);
Mike Stump11289f42009-09-09 15:08:12 +0000237
Ted Kremenek56572ab2008-12-12 18:34:08 +0000238 // By construction NextIdx will be zero if this is a #endif. This is useful
239 // to know to obviate lexing another token.
240 bool isEndif = NextIdx == 0;
Mike Stump11289f42009-09-09 15:08:12 +0000241
Ted Kremenek56572ab2008-12-12 18:34:08 +0000242 // This case can occur when we see something like this:
243 //
244 // #if ...
245 // /* a comment or nothing */
246 // #elif
247 //
248 // If we are skipping the first #if block it will be the case that CurPtr
249 // already points 'elif'. Just return.
Mike Stump11289f42009-09-09 15:08:12 +0000250
Ted Kremenek877556f2008-12-12 22:05:38 +0000251 if (CurPtr > HashEntryI) {
252 assert(CurPtr == HashEntryI + DISK_TOKEN_SIZE);
Ted Kremenek56572ab2008-12-12 18:34:08 +0000253 // Did we reach a #endif? If so, go ahead and consume that token as well.
254 if (isEndif)
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000255 CurPtr += DISK_TOKEN_SIZE*2;
Ted Kremenek56572ab2008-12-12 18:34:08 +0000256 else
Ted Kremenek877556f2008-12-12 22:05:38 +0000257 LastHashTokPtr = HashEntryI;
Mike Stump11289f42009-09-09 15:08:12 +0000258
Ted Kremenek56572ab2008-12-12 18:34:08 +0000259 return isEndif;
260 }
261
262 // Otherwise, we need to advance. Update CurPtr to point to the '#' token.
Ted Kremenek877556f2008-12-12 22:05:38 +0000263 CurPtr = HashEntryI;
Mike Stump11289f42009-09-09 15:08:12 +0000264
Ted Kremenek56572ab2008-12-12 18:34:08 +0000265 // Update the location of the last observed '#'. This is useful if we
266 // are skipping multiple blocks.
267 LastHashTokPtr = CurPtr;
Ted Kremenek56572ab2008-12-12 18:34:08 +0000268
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000269 // Skip the '#' token.
Chris Lattnereb097542009-01-18 01:57:14 +0000270 assert(((tok::TokenKind)*CurPtr) == tok::hash);
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000271 CurPtr += DISK_TOKEN_SIZE;
Mike Stump11289f42009-09-09 15:08:12 +0000272
Ted Kremenek56572ab2008-12-12 18:34:08 +0000273 // Did we reach a #endif? If so, go ahead and consume that token as well.
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000274 if (isEndif) { CurPtr += DISK_TOKEN_SIZE*2; }
Ted Kremenek56572ab2008-12-12 18:34:08 +0000275
276 return isEndif;
277}
278
Ted Kremenek63ff81c2008-12-17 23:36:32 +0000279SourceLocation PTHLexer::getSourceLocation() {
Chris Lattner137d6492009-01-18 02:10:31 +0000280 // getSourceLocation is not on the hot path. It is used to get the location
281 // of the next token when transitioning back to this lexer when done
Ted Kremenek63ff81c2008-12-17 23:36:32 +0000282 // handling a #included file. Just read the necessary data from the token
283 // data buffer to construct the SourceLocation object.
284 // NOTE: This is a virtual function; hence it is defined out-of-line.
Ted Kremenekae54f2f2009-01-21 22:41:38 +0000285 const unsigned char *OffsetPtr = CurPtr + (DISK_TOKEN_SIZE - 4);
Chris Lattnerfec54702009-01-22 19:48:26 +0000286 uint32_t Offset = ReadLE32(OffsetPtr);
Argyrios Kyrtzidise6e67de2011-09-19 20:40:19 +0000287 return FileStartLoc.getLocWithOffset(Offset);
Ted Kremenek63ff81c2008-12-17 23:36:32 +0000288}
289
Ted Kremenek47b8cf62009-01-09 22:05:30 +0000290//===----------------------------------------------------------------------===//
Ted Kremenek86423a92009-02-10 22:16:22 +0000291// PTH file lookup: map from strings to file data.
292//===----------------------------------------------------------------------===//
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000293
294/// PTHFileLookup - This internal data structure is used by the PTHManager
295/// to map from FileEntry objects managed by FileManager to offsets within
296/// the PTH file.
297namespace {
Benjamin Kramer337e3a52009-11-28 19:45:26 +0000298class PTHFileData {
Ted Kremenek86423a92009-02-10 22:16:22 +0000299 const uint32_t TokenOff;
300 const uint32_t PPCondOff;
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000301public:
Ted Kremenek86423a92009-02-10 22:16:22 +0000302 PTHFileData(uint32_t tokenOff, uint32_t ppCondOff)
303 : TokenOff(tokenOff), PPCondOff(ppCondOff) {}
Mike Stump11289f42009-09-09 15:08:12 +0000304
305 uint32_t getTokenOffset() const { return TokenOff; }
306 uint32_t getPPCondOffset() const { return PPCondOff; }
Ted Kremenek86423a92009-02-10 22:16:22 +0000307};
Mike Stump11289f42009-09-09 15:08:12 +0000308
309
Benjamin Kramer337e3a52009-11-28 19:45:26 +0000310class PTHFileLookupCommonTrait {
Ted Kremenek86423a92009-02-10 22:16:22 +0000311public:
Ted Kremenek29942a32009-02-13 19:13:46 +0000312 typedef std::pair<unsigned char, const char*> internal_key_type;
313
314 static unsigned ComputeHash(internal_key_type x) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +0000315 return llvm::HashString(x.second);
Ted Kremenek86423a92009-02-10 22:16:22 +0000316 }
Mike Stump11289f42009-09-09 15:08:12 +0000317
Ted Kremenek86423a92009-02-10 22:16:22 +0000318 static std::pair<unsigned, unsigned>
319 ReadKeyDataLength(const unsigned char*& d) {
Ted Kremenek29942a32009-02-13 19:13:46 +0000320 unsigned keyLen = (unsigned) ReadUnalignedLE16(d);
321 unsigned dataLen = (unsigned) *(d++);
322 return std::make_pair(keyLen, dataLen);
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000323 }
Mike Stump11289f42009-09-09 15:08:12 +0000324
Ted Kremenek29942a32009-02-13 19:13:46 +0000325 static internal_key_type ReadKey(const unsigned char* d, unsigned) {
326 unsigned char k = *(d++); // Read the entry kind.
327 return std::make_pair(k, (const char*) d);
Ted Kremenek86423a92009-02-10 22:16:22 +0000328 }
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000329};
Mike Stump11289f42009-09-09 15:08:12 +0000330
Benjamin Kramer337e3a52009-11-28 19:45:26 +0000331class PTHFileLookupTrait : public PTHFileLookupCommonTrait {
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000332public:
333 typedef const FileEntry* external_key_type;
334 typedef PTHFileData data_type;
Mike Stump11289f42009-09-09 15:08:12 +0000335
Ted Kremenek29942a32009-02-13 19:13:46 +0000336 static internal_key_type GetInternalKey(const FileEntry* FE) {
337 return std::make_pair((unsigned char) 0x1, FE->getName());
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000338 }
Ted Kremenek29942a32009-02-13 19:13:46 +0000339
340 static bool EqualKey(internal_key_type a, internal_key_type b) {
341 return a.first == b.first && strcmp(a.second, b.second) == 0;
Mike Stump11289f42009-09-09 15:08:12 +0000342 }
343
344 static PTHFileData ReadData(const internal_key_type& k,
345 const unsigned char* d, unsigned) {
Ted Kremenek29942a32009-02-13 19:13:46 +0000346 assert(k.first == 0x1 && "Only file lookups can match!");
Ted Kremenek86423a92009-02-10 22:16:22 +0000347 uint32_t x = ::ReadUnalignedLE32(d);
348 uint32_t y = ::ReadUnalignedLE32(d);
Mike Stump11289f42009-09-09 15:08:12 +0000349 return PTHFileData(x, y);
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000350 }
351};
Ted Kremeneke5554de2009-02-11 21:29:16 +0000352
Benjamin Kramer337e3a52009-11-28 19:45:26 +0000353class PTHStringLookupTrait {
Ted Kremeneke5554de2009-02-11 21:29:16 +0000354public:
Mike Stump11289f42009-09-09 15:08:12 +0000355 typedef uint32_t
Ted Kremeneke5554de2009-02-11 21:29:16 +0000356 data_type;
357
358 typedef const std::pair<const char*, unsigned>
359 external_key_type;
360
361 typedef external_key_type internal_key_type;
Mike Stump11289f42009-09-09 15:08:12 +0000362
Ted Kremeneke5554de2009-02-11 21:29:16 +0000363 static bool EqualKey(const internal_key_type& a,
364 const internal_key_type& b) {
365 return (a.second == b.second) ? memcmp(a.first, b.first, a.second) == 0
366 : false;
367 }
Mike Stump11289f42009-09-09 15:08:12 +0000368
Ted Kremeneke5554de2009-02-11 21:29:16 +0000369 static unsigned ComputeHash(const internal_key_type& a) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000370 return llvm::HashString(StringRef(a.first, a.second));
Ted Kremeneke5554de2009-02-11 21:29:16 +0000371 }
Mike Stump11289f42009-09-09 15:08:12 +0000372
Ted Kremeneke5554de2009-02-11 21:29:16 +0000373 // This hopefully will just get inlined and removed by the optimizer.
374 static const internal_key_type&
375 GetInternalKey(const external_key_type& x) { return x; }
Mike Stump11289f42009-09-09 15:08:12 +0000376
Ted Kremeneke5554de2009-02-11 21:29:16 +0000377 static std::pair<unsigned, unsigned>
378 ReadKeyDataLength(const unsigned char*& d) {
379 return std::make_pair((unsigned) ReadUnalignedLE16(d), sizeof(uint32_t));
380 }
Mike Stump11289f42009-09-09 15:08:12 +0000381
Ted Kremeneke5554de2009-02-11 21:29:16 +0000382 static std::pair<const char*, unsigned>
383 ReadKey(const unsigned char* d, unsigned n) {
384 assert(n >= 2 && d[n-1] == '\0');
385 return std::make_pair((const char*) d, n-1);
386 }
Mike Stump11289f42009-09-09 15:08:12 +0000387
Ted Kremenek29942a32009-02-13 19:13:46 +0000388 static uint32_t ReadData(const internal_key_type& k, const unsigned char* d,
389 unsigned) {
Ted Kremeneke5554de2009-02-11 21:29:16 +0000390 return ::ReadUnalignedLE32(d);
391 }
392};
Mike Stump11289f42009-09-09 15:08:12 +0000393
394} // end anonymous namespace
Ted Kremenek86423a92009-02-10 22:16:22 +0000395
Ted Kremeneke5554de2009-02-11 21:29:16 +0000396typedef OnDiskChainedHashTable<PTHFileLookupTrait> PTHFileLookup;
397typedef OnDiskChainedHashTable<PTHStringLookupTrait> PTHStringIdLookup;
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000398
399//===----------------------------------------------------------------------===//
400// PTHManager methods.
401//===----------------------------------------------------------------------===//
402
403PTHManager::PTHManager(const llvm::MemoryBuffer* buf, void* fileLookup,
Chris Lattnereb097542009-01-18 01:57:14 +0000404 const unsigned char* idDataTable,
Mike Stump11289f42009-09-09 15:08:12 +0000405 IdentifierInfo** perIDCache,
Ted Kremeneke5554de2009-02-11 21:29:16 +0000406 void* stringIdLookup, unsigned numIds,
Ted Kremenekf9ccd5c2009-03-19 22:19:30 +0000407 const unsigned char* spellingBase,
408 const char* originalSourceFile)
Ted Kremenek73a4d282008-12-03 01:16:39 +0000409: Buf(buf), PerIDCache(perIDCache), FileLookup(fileLookup),
Ted Kremeneke5554de2009-02-11 21:29:16 +0000410 IdDataTable(idDataTable), StringIdLookup(stringIdLookup),
Ted Kremenekf9ccd5c2009-03-19 22:19:30 +0000411 NumIds(numIds), PP(0), SpellingBase(spellingBase),
412 OriginalSourceFile(originalSourceFile) {}
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000413
414PTHManager::~PTHManager() {
415 delete Buf;
416 delete (PTHFileLookup*) FileLookup;
Ted Kremeneke5554de2009-02-11 21:29:16 +0000417 delete (PTHStringIdLookup*) StringIdLookup;
Ted Kremenek1aed3dd2008-12-04 22:47:11 +0000418 free(PerIDCache);
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000419}
420
David Blaikie9c902b52011-09-25 23:23:43 +0000421static void InvalidPTH(DiagnosticsEngine &Diags, const char *Msg) {
422 Diags.Report(Diags.getCustomDiagID(DiagnosticsEngine::Error, Msg));
Ted Kremenek62224c12009-01-28 21:02:43 +0000423}
424
David Blaikie9c902b52011-09-25 23:23:43 +0000425PTHManager *PTHManager::Create(const std::string &file,
426 DiagnosticsEngine &Diags) {
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000427 // Memory map the PTH file.
Dylan Noblesmithe2778992012-02-05 02:12:40 +0000428 OwningPtr<llvm::MemoryBuffer> File;
Mike Stump11289f42009-09-09 15:08:12 +0000429
Michael J. Spencerd9da7a12010-12-16 03:28:14 +0000430 if (llvm::MemoryBuffer::getFile(file, File)) {
Michael J. Spencerf25faaa2010-12-09 17:36:38 +0000431 // FIXME: Add ec.message() to this diag.
Daniel Dunbar1a54e3f2009-11-12 02:53:48 +0000432 Diags.Report(diag::err_invalid_pth_file) << file;
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000433 return 0;
Ted Kremenek3b0589e2009-01-28 20:49:33 +0000434 }
Mike Stump11289f42009-09-09 15:08:12 +0000435
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000436 // Get the buffer ranges and check if there are at least three 32-bit
437 // words at the end of the file.
Roman Divackye6377112012-09-06 15:59:27 +0000438 const unsigned char *BufBeg = (const unsigned char*)File->getBufferStart();
439 const unsigned char *BufEnd = (const unsigned char*)File->getBufferEnd();
Ted Kremenekeb8c8fb2009-01-26 21:43:14 +0000440
441 // Check the prologue of the file.
Richard Smith2683b4c2012-08-17 03:55:43 +0000442 if ((BufEnd - BufBeg) < (signed)(sizeof("cfe-pth") + 4 + 4) ||
443 memcmp(BufBeg, "cfe-pth", sizeof("cfe-pth")) != 0) {
Daniel Dunbar1a54e3f2009-11-12 02:53:48 +0000444 Diags.Report(diag::err_invalid_pth_file) << file;
Ted Kremenekeb8c8fb2009-01-26 21:43:14 +0000445 return 0;
Ted Kremenek62224c12009-01-28 21:02:43 +0000446 }
Mike Stump11289f42009-09-09 15:08:12 +0000447
Ted Kremenek978b5be2009-01-26 21:50:21 +0000448 // Read the PTH version.
Richard Smith2683b4c2012-08-17 03:55:43 +0000449 const unsigned char *p = BufBeg + (sizeof("cfe-pth"));
Ted Kremenek978b5be2009-01-26 21:50:21 +0000450 unsigned Version = ReadLE32(p);
Mike Stump11289f42009-09-09 15:08:12 +0000451
Daniel Dunbar1a54e3f2009-11-12 02:53:48 +0000452 if (Version < PTHManager::Version) {
453 InvalidPTH(Diags,
Mike Stump11289f42009-09-09 15:08:12 +0000454 Version < PTHManager::Version
Ted Kremenek62224c12009-01-28 21:02:43 +0000455 ? "PTH file uses an older PTH format that is no longer supported"
456 : "PTH file uses a newer PTH format that cannot be read");
Ted Kremenek978b5be2009-01-26 21:50:21 +0000457 return 0;
Ted Kremenek62224c12009-01-28 21:02:43 +0000458 }
Ted Kremenek978b5be2009-01-26 21:50:21 +0000459
Mike Stump11289f42009-09-09 15:08:12 +0000460 // Compute the address of the index table at the end of the PTH file.
Ted Kremenek4c1d41f2009-02-11 23:34:32 +0000461 const unsigned char *PrologueOffset = p;
Mike Stump11289f42009-09-09 15:08:12 +0000462
Ted Kremenek4c1d41f2009-02-11 23:34:32 +0000463 if (PrologueOffset >= BufEnd) {
Daniel Dunbar1a54e3f2009-11-12 02:53:48 +0000464 Diags.Report(diag::err_invalid_pth_file) << file;
Ted Kremenekeb8c8fb2009-01-26 21:43:14 +0000465 return 0;
Ted Kremenek62224c12009-01-28 21:02:43 +0000466 }
Mike Stump11289f42009-09-09 15:08:12 +0000467
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000468 // Construct the file lookup table. This will be used for mapping from
469 // FileEntry*'s to cached tokens.
Ted Kremenek4c1d41f2009-02-11 23:34:32 +0000470 const unsigned char* FileTableOffset = PrologueOffset + sizeof(uint32_t)*2;
Chris Lattnerfec54702009-01-22 19:48:26 +0000471 const unsigned char* FileTable = BufBeg + ReadLE32(FileTableOffset);
Mike Stump11289f42009-09-09 15:08:12 +0000472
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000473 if (!(FileTable > BufBeg && FileTable < BufEnd)) {
Daniel Dunbar1a54e3f2009-11-12 02:53:48 +0000474 Diags.Report(diag::err_invalid_pth_file) << file;
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000475 return 0; // FIXME: Proper error diagnostic?
476 }
Mike Stump11289f42009-09-09 15:08:12 +0000477
Dylan Noblesmithe2778992012-02-05 02:12:40 +0000478 OwningPtr<PTHFileLookup> FL(PTHFileLookup::Create(FileTable, BufBeg));
Mike Stump11289f42009-09-09 15:08:12 +0000479
Ted Kremenek80e239a2009-03-20 17:54:25 +0000480 // Warn if the PTH file is empty. We still want to create a PTHManager
481 // as the PTH could be used with -include-pth.
482 if (FL->isEmpty())
Daniel Dunbar1a54e3f2009-11-12 02:53:48 +0000483 InvalidPTH(Diags, "PTH file contains no cached source data");
Mike Stump11289f42009-09-09 15:08:12 +0000484
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000485 // Get the location of the table mapping from persistent ids to the
486 // data needed to reconstruct identifiers.
Ted Kremenek4c1d41f2009-02-11 23:34:32 +0000487 const unsigned char* IDTableOffset = PrologueOffset + sizeof(uint32_t)*0;
Chris Lattnerfec54702009-01-22 19:48:26 +0000488 const unsigned char* IData = BufBeg + ReadLE32(IDTableOffset);
Mike Stump11289f42009-09-09 15:08:12 +0000489
Ted Kremenek8d6c8282009-01-21 07:34:28 +0000490 if (!(IData >= BufBeg && IData < BufEnd)) {
Daniel Dunbar1a54e3f2009-11-12 02:53:48 +0000491 Diags.Report(diag::err_invalid_pth_file) << file;
Ted Kremenek62224c12009-01-28 21:02:43 +0000492 return 0;
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000493 }
Mike Stump11289f42009-09-09 15:08:12 +0000494
Ted Kremeneke5554de2009-02-11 21:29:16 +0000495 // Get the location of the hashtable mapping between strings and
496 // persistent IDs.
Ted Kremenek4c1d41f2009-02-11 23:34:32 +0000497 const unsigned char* StringIdTableOffset = PrologueOffset + sizeof(uint32_t)*1;
Ted Kremeneke5554de2009-02-11 21:29:16 +0000498 const unsigned char* StringIdTable = BufBeg + ReadLE32(StringIdTableOffset);
499 if (!(StringIdTable >= BufBeg && StringIdTable < BufEnd)) {
Daniel Dunbar1a54e3f2009-11-12 02:53:48 +0000500 Diags.Report(diag::err_invalid_pth_file) << file;
Ted Kremenek62224c12009-01-28 21:02:43 +0000501 return 0;
Ted Kremeneka705b042009-01-15 18:47:46 +0000502 }
Ted Kremeneke5554de2009-02-11 21:29:16 +0000503
Dylan Noblesmithe2778992012-02-05 02:12:40 +0000504 OwningPtr<PTHStringIdLookup> SL(PTHStringIdLookup::Create(StringIdTable,
Ted Kremeneke5554de2009-02-11 21:29:16 +0000505 BufBeg));
Mike Stump11289f42009-09-09 15:08:12 +0000506
Ted Kremenek8d178f42009-01-27 00:01:05 +0000507 // Get the location of the spelling cache.
Ted Kremenek4c1d41f2009-02-11 23:34:32 +0000508 const unsigned char* spellingBaseOffset = PrologueOffset + sizeof(uint32_t)*3;
Ted Kremenek8d178f42009-01-27 00:01:05 +0000509 const unsigned char* spellingBase = BufBeg + ReadLE32(spellingBaseOffset);
510 if (!(spellingBase >= BufBeg && spellingBase < BufEnd)) {
Daniel Dunbar1a54e3f2009-11-12 02:53:48 +0000511 Diags.Report(diag::err_invalid_pth_file) << file;
Ted Kremenek8d178f42009-01-27 00:01:05 +0000512 return 0;
513 }
Mike Stump11289f42009-09-09 15:08:12 +0000514
Ted Kremenek73a4d282008-12-03 01:16:39 +0000515 // Get the number of IdentifierInfos and pre-allocate the identifier cache.
Chris Lattnerfec54702009-01-22 19:48:26 +0000516 uint32_t NumIds = ReadLE32(IData);
Mike Stump11289f42009-09-09 15:08:12 +0000517
Chris Lattner57540c52011-04-15 05:22:18 +0000518 // Pre-allocate the persistent ID -> IdentifierInfo* cache. We use calloc()
Ted Kremenek73a4d282008-12-03 01:16:39 +0000519 // so that we in the best case only zero out memory once when the OS returns
520 // us new pages.
Ted Kremenek8d6c8282009-01-21 07:34:28 +0000521 IdentifierInfo** PerIDCache = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000522
Ted Kremenek8d6c8282009-01-21 07:34:28 +0000523 if (NumIds) {
Mike Stump11289f42009-09-09 15:08:12 +0000524 PerIDCache = (IdentifierInfo**)calloc(NumIds, sizeof(*PerIDCache));
Ted Kremenek8d6c8282009-01-21 07:34:28 +0000525 if (!PerIDCache) {
Daniel Dunbar1a54e3f2009-11-12 02:53:48 +0000526 InvalidPTH(Diags, "Could not allocate memory for processing PTH file");
Ted Kremenek8d6c8282009-01-21 07:34:28 +0000527 return 0;
528 }
Ted Kremenek73a4d282008-12-03 01:16:39 +0000529 }
Ted Kremenek8d6c8282009-01-21 07:34:28 +0000530
Ted Kremenekf9ccd5c2009-03-19 22:19:30 +0000531 // Compute the address of the original source file.
532 const unsigned char* originalSourceBase = PrologueOffset + sizeof(uint32_t)*4;
533 unsigned len = ReadUnalignedLE16(originalSourceBase);
Mike Stump11289f42009-09-09 15:08:12 +0000534 if (!len) originalSourceBase = 0;
535
Ted Kremeneka705b042009-01-15 18:47:46 +0000536 // Create the new PTHManager.
537 return new PTHManager(File.take(), FL.take(), IData, PerIDCache,
Ted Kremenekf9ccd5c2009-03-19 22:19:30 +0000538 SL.take(), NumIds, spellingBase,
539 (const char*) originalSourceBase);
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000540}
Ted Kremenekf9ccd5c2009-03-19 22:19:30 +0000541
Chris Lattner144aacd2009-01-18 02:57:21 +0000542IdentifierInfo* PTHManager::LazilyCreateIdentifierInfo(unsigned PersistentID) {
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000543 // Look in the PTH file for the string data for the IdentifierInfo object.
Chris Lattner144aacd2009-01-18 02:57:21 +0000544 const unsigned char* TableEntry = IdDataTable + sizeof(uint32_t)*PersistentID;
Chris Lattnereb097542009-01-18 01:57:14 +0000545 const unsigned char* IDData =
Chris Lattnerfec54702009-01-22 19:48:26 +0000546 (const unsigned char*)Buf->getBufferStart() + ReadLE32(TableEntry);
Chris Lattnereb097542009-01-18 01:57:14 +0000547 assert(IDData < (const unsigned char*)Buf->getBufferEnd());
Mike Stump11289f42009-09-09 15:08:12 +0000548
Ted Kremeneka705b042009-01-15 18:47:46 +0000549 // Allocate the object.
Chris Lattnereb097542009-01-18 01:57:14 +0000550 std::pair<IdentifierInfo,const unsigned char*> *Mem =
551 Alloc.Allocate<std::pair<IdentifierInfo,const unsigned char*> >();
Ted Kremeneka705b042009-01-15 18:47:46 +0000552
553 Mem->second = IDData;
Ted Kremeneke5554de2009-02-11 21:29:16 +0000554 assert(IDData[0] != '\0');
Ted Kremenek52f73ca2009-01-20 23:28:34 +0000555 IdentifierInfo *II = new ((void*) Mem) IdentifierInfo();
Mike Stump11289f42009-09-09 15:08:12 +0000556
Ted Kremeneka705b042009-01-15 18:47:46 +0000557 // Store the new IdentifierInfo in the cache.
Chris Lattner144aacd2009-01-18 02:57:21 +0000558 PerIDCache[PersistentID] = II;
Daniel Dunbar2c422dc92009-10-18 20:26:12 +0000559 assert(II->getNameStart() && II->getNameStart()[0] != '\0');
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000560 return II;
561}
562
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000563IdentifierInfo* PTHManager::get(StringRef Name) {
Ted Kremeneke5554de2009-02-11 21:29:16 +0000564 PTHStringIdLookup& SL = *((PTHStringIdLookup*)StringIdLookup);
565 // Double check our assumption that the last character isn't '\0'.
David Blaikie441417a2011-09-22 01:35:49 +0000566 assert(Name.empty() || Name.back() != '\0');
Kovarththanan Rajaratnam661a3092010-03-12 08:23:34 +0000567 PTHStringIdLookup::iterator I = SL.find(std::make_pair(Name.data(),
568 Name.size()));
Ted Kremeneke5554de2009-02-11 21:29:16 +0000569 if (I == SL.end()) // No identifier found?
570 return 0;
Ted Kremeneka705b042009-01-15 18:47:46 +0000571
Ted Kremeneke5554de2009-02-11 21:29:16 +0000572 // Match found. Return the identifier!
573 assert(*I > 0);
574 return GetIdentifierInfo(*I-1);
575}
Ted Kremeneka705b042009-01-15 18:47:46 +0000576
Chris Lattnerab1d4b82009-01-17 08:06:50 +0000577PTHLexer *PTHManager::CreateLexer(FileID FID) {
578 const FileEntry *FE = PP->getSourceManager().getFileEntryForID(FID);
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000579 if (!FE)
580 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000581
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000582 // Lookup the FileEntry object in our file lookup data structure. It will
583 // return a variant that indicates whether or not there is an offset within
584 // the PTH file that contains cached tokens.
Ted Kremenek86423a92009-02-10 22:16:22 +0000585 PTHFileLookup& PFL = *((PTHFileLookup*)FileLookup);
586 PTHFileLookup::iterator I = PFL.find(FE);
Mike Stump11289f42009-09-09 15:08:12 +0000587
Ted Kremenek86423a92009-02-10 22:16:22 +0000588 if (I == PFL.end()) // No tokens available?
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000589 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000590
591 const PTHFileData& FileData = *I;
592
Chris Lattnereb097542009-01-18 01:57:14 +0000593 const unsigned char *BufStart = (const unsigned char *)Buf->getBufferStart();
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000594 // Compute the offset of the token data within the buffer.
Chris Lattnereb097542009-01-18 01:57:14 +0000595 const unsigned char* data = BufStart + FileData.getTokenOffset();
Ted Kremenek56572ab2008-12-12 18:34:08 +0000596
597 // Get the location of pp-conditional table.
Chris Lattnereb097542009-01-18 01:57:14 +0000598 const unsigned char* ppcond = BufStart + FileData.getPPCondOffset();
Chris Lattnerfec54702009-01-22 19:48:26 +0000599 uint32_t Len = ReadLE32(ppcond);
Chris Lattner137d6492009-01-18 02:10:31 +0000600 if (Len == 0) ppcond = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000601
Ted Kremeneka705b042009-01-15 18:47:46 +0000602 assert(PP && "No preprocessor set yet!");
Mike Stump11289f42009-09-09 15:08:12 +0000603 return new PTHLexer(*PP, FID, data, ppcond, *this);
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000604}
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000605
606//===----------------------------------------------------------------------===//
607// 'stat' caching.
608//===----------------------------------------------------------------------===//
609
610namespace {
Benjamin Kramer337e3a52009-11-28 19:45:26 +0000611class PTHStatData {
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000612public:
Rafael Espindolaf8f91b82013-08-01 21:42:11 +0000613 const bool HasData;
614 uint64_t Size;
615 time_t ModTime;
616 llvm::sys::fs::UniqueID UniqueID;
617 bool IsDirectory;
Mike Stump11289f42009-09-09 15:08:12 +0000618
Rafael Espindolaf8f91b82013-08-01 21:42:11 +0000619 PTHStatData(uint64_t Size, time_t ModTime, llvm::sys::fs::UniqueID UniqueID,
620 bool IsDirectory)
621 : HasData(true), Size(Size), ModTime(ModTime), UniqueID(UniqueID),
622 IsDirectory(IsDirectory) {}
Mike Stump11289f42009-09-09 15:08:12 +0000623
Rafael Espindolaf8f91b82013-08-01 21:42:11 +0000624 PTHStatData() : HasData(false) {}
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000625};
Mike Stump11289f42009-09-09 15:08:12 +0000626
Benjamin Kramer337e3a52009-11-28 19:45:26 +0000627class PTHStatLookupTrait : public PTHFileLookupCommonTrait {
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000628public:
Ted Kremenek29942a32009-02-13 19:13:46 +0000629 typedef const char* external_key_type; // const char*
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000630 typedef PTHStatData data_type;
Mike Stump11289f42009-09-09 15:08:12 +0000631
Ted Kremenek29942a32009-02-13 19:13:46 +0000632 static internal_key_type GetInternalKey(const char *path) {
633 // The key 'kind' doesn't matter here because it is ignored in EqualKey.
634 return std::make_pair((unsigned char) 0x0, path);
635 }
636
637 static bool EqualKey(internal_key_type a, internal_key_type b) {
638 // When doing 'stat' lookups we don't care about the kind of 'a' and 'b',
639 // just the paths.
640 return strcmp(a.second, b.second) == 0;
Mike Stump11289f42009-09-09 15:08:12 +0000641 }
642
Ted Kremenek29942a32009-02-13 19:13:46 +0000643 static data_type ReadData(const internal_key_type& k, const unsigned char* d,
Mike Stump11289f42009-09-09 15:08:12 +0000644 unsigned) {
645
Ted Kremenek2fd18ec2009-02-13 22:07:44 +0000646 if (k.first /* File or Directory */) {
Rafael Espindolaf8f91b82013-08-01 21:42:11 +0000647 bool IsDirectory = true;
648 if (k.first == 0x1 /* File */) {
649 IsDirectory = false;
650 d += 4 * 2; // Skip the first 2 words.
651 }
652
653 uint64_t File = ReadUnalignedLE64(d);
654 uint64_t Device = ReadUnalignedLE64(d);
655 llvm::sys::fs::UniqueID UniqueID(File, Device);
656 time_t ModTime = ReadUnalignedLE64(d);
657 uint64_t Size = ReadUnalignedLE64(d);
658 return data_type(Size, ModTime, UniqueID, IsDirectory);
Ted Kremenek29942a32009-02-13 19:13:46 +0000659 }
Ted Kremenek2fd18ec2009-02-13 22:07:44 +0000660
661 // Negative stat. Don't read anything.
Ted Kremenek29942a32009-02-13 19:13:46 +0000662 return data_type();
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000663 }
664};
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000665
Chris Lattner226efd32010-11-23 19:19:34 +0000666class PTHStatCache : public FileSystemStatCache {
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000667 typedef OnDiskChainedHashTable<PTHStatLookupTrait> CacheTy;
668 CacheTy Cache;
669
Mike Stump11289f42009-09-09 15:08:12 +0000670public:
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000671 PTHStatCache(PTHFileLookup &FL) :
672 Cache(FL.getNumBuckets(), FL.getNumEntries(), FL.getBuckets(),
673 FL.getBase()) {}
674
675 ~PTHStatCache() {}
Mike Stump11289f42009-09-09 15:08:12 +0000676
Rafael Espindolaf8f91b82013-08-01 21:42:11 +0000677 LookupResult getStat(const char *Path, FileData &Data, bool isFile,
678 int *FileDescriptor) {
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000679 // Do the lookup for the file's data in the PTH file.
Chris Lattner226efd32010-11-23 19:19:34 +0000680 CacheTy::iterator I = Cache.find(Path);
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000681
682 // If we don't get a hit in the PTH file just forward to 'stat'.
Kovarththanan Rajaratnam661a3092010-03-12 08:23:34 +0000683 if (I == Cache.end())
Rafael Espindolaf8f91b82013-08-01 21:42:11 +0000684 return statChained(Path, Data, isFile, FileDescriptor);
Mike Stump11289f42009-09-09 15:08:12 +0000685
Rafael Espindolaf8f91b82013-08-01 21:42:11 +0000686 const PTHStatData &D = *I;
Mike Stump11289f42009-09-09 15:08:12 +0000687
Rafael Espindolaf8f91b82013-08-01 21:42:11 +0000688 if (!D.HasData)
Chris Lattner8f0583d2010-11-23 20:05:15 +0000689 return CacheMissing;
Ted Kremenek2fd18ec2009-02-13 22:07:44 +0000690
Rafael Espindolaf8f91b82013-08-01 21:42:11 +0000691 Data.Size = D.Size;
692 Data.ModTime = D.ModTime;
693 Data.UniqueID = D.UniqueID;
694 Data.IsDirectory = D.IsDirectory;
695 Data.IsNamedPipe = false;
696 Data.InPCH = true;
697
Chris Lattner8f0583d2010-11-23 20:05:15 +0000698 return CacheExists;
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000699 }
700};
Ted Kremenek29b86972009-02-23 23:27:54 +0000701} // end anonymous namespace
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000702
Chris Lattner226efd32010-11-23 19:19:34 +0000703FileSystemStatCache *PTHManager::createStatCache() {
Ted Kremenekb4c85cc2009-02-12 03:45:39 +0000704 return new PTHStatCache(*((PTHFileLookup*) FileLookup));
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000705}