blob: b671f9b4c18af62ae30d945b667c9cd29ebd622f [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"
Daniel Dunbarf8502d52009-10-17 23:52:28 +000024#include "llvm/ADT/StringExtras.h"
25#include "llvm/ADT/StringMap.h"
Justin Bognere1c147c2014-03-28 22:03:19 +000026#include "llvm/Support/EndianStream.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"
Ahmed Charlesdfca6f92014-03-09 11:36:40 +000029#include <memory>
Ted Kremenek7cd62452008-11-12 21:37:15 +000030using namespace clang;
Douglas Gregor52289d32009-04-20 07:08:21 +000031using namespace clang::io;
Ted Kremenek7cd62452008-11-12 21:37:15 +000032
Ted Kremenek8433f0b2009-01-19 23:13:15 +000033#define DISK_TOKEN_SIZE (1+1+2+4+4)
Ted Kremenek56572ab2008-12-12 18:34:08 +000034
Ted Kremenek33eeabd2008-12-03 00:38:03 +000035//===----------------------------------------------------------------------===//
Ted Kremenek1b18ad22008-12-23 01:30:52 +000036// PTHLexer methods.
37//===----------------------------------------------------------------------===//
38
Chris Lattnereb097542009-01-18 01:57:14 +000039PTHLexer::PTHLexer(Preprocessor &PP, FileID FID, const unsigned char *D,
Ted Kremenek8d178f42009-01-27 00:01:05 +000040 const unsigned char *ppcond, PTHManager &PM)
Chris Lattnerd32480d2009-01-17 06:22:33 +000041 : PreprocessorLexer(&PP, FID), TokBuf(D), CurPtr(D), LastHashTokPtr(0),
Ted Kremenek8d178f42009-01-27 00:01:05 +000042 PPCond(ppcond), CurPPCondPtr(ppcond), PTHMgr(PM) {
Mike Stump11289f42009-09-09 15:08:12 +000043
Chris Lattnerd32480d2009-01-17 06:22:33 +000044 FileStartLoc = PP.getSourceManager().getLocForStartOfFile(FID);
Ted Kremenek47b8cf62009-01-09 22:05:30 +000045}
Ted Kremenek1b18ad22008-12-23 01:30:52 +000046
Eli Friedman0834a4b2013-09-19 00:41:32 +000047bool PTHLexer::Lex(Token& Tok) {
Ted Kremenek66076a92008-12-23 02:30:15 +000048 //===--------------------------------------==//
49 // Read the raw token data.
50 //===--------------------------------------==//
Mike Stump11289f42009-09-09 15:08:12 +000051
Ted Kremenek66076a92008-12-23 02:30:15 +000052 // Shadow CurPtr into an automatic variable.
Mike Stump11289f42009-09-09 15:08:12 +000053 const unsigned char *CurPtrShadow = CurPtr;
Ted Kremenek66076a92008-12-23 02:30:15 +000054
Chris Lattner137d6492009-01-18 02:10:31 +000055 // Read in the data for the token.
Justin Bognera0dafb72014-03-28 20:32:17 +000056 unsigned Word0 = ReadLE32(CurPtrShadow);
57 uint32_t IdentifierID = ReadLE32(CurPtrShadow);
58 uint32_t FileOffset = ReadLE32(CurPtrShadow);
Mike Stump11289f42009-09-09 15:08:12 +000059
Ted Kremenek8433f0b2009-01-19 23:13:15 +000060 tok::TokenKind TKind = (tok::TokenKind) (Word0 & 0xFF);
61 Token::TokenFlags TFlags = (Token::TokenFlags) ((Word0 >> 8) & 0xFF);
Chris Lattner47def972009-01-21 07:21:56 +000062 uint32_t Len = Word0 >> 16;
Ted Kremenek8433f0b2009-01-19 23:13:15 +000063
Chris Lattner47def972009-01-21 07:21:56 +000064 CurPtr = CurPtrShadow;
Mike Stump11289f42009-09-09 15:08:12 +000065
Ted Kremenek66076a92008-12-23 02:30:15 +000066 //===--------------------------------------==//
67 // Construct the token itself.
68 //===--------------------------------------==//
Mike Stump11289f42009-09-09 15:08:12 +000069
Ted Kremenek66076a92008-12-23 02:30:15 +000070 Tok.startToken();
Chris Lattner18fc6ce2009-01-18 02:34:01 +000071 Tok.setKind(TKind);
72 Tok.setFlag(TFlags);
Ted Kremenek78cc2472008-12-23 19:24:24 +000073 assert(!LexingRawMode);
Argyrios Kyrtzidise6e67de2011-09-19 20:40:19 +000074 Tok.setLocation(FileStartLoc.getLocWithOffset(FileOffset));
Ted Kremenek66076a92008-12-23 02:30:15 +000075 Tok.setLength(Len);
76
Chris Lattner3029b352009-01-21 07:50:06 +000077 // Handle identifiers.
Ted Kremenek8d178f42009-01-27 00:01:05 +000078 if (Tok.isLiteral()) {
79 Tok.setLiteralData((const char*) (PTHMgr.SpellingBase + IdentifierID));
80 }
81 else if (IdentifierID) {
Chris Lattner3029b352009-01-21 07:50:06 +000082 MIOpt.ReadToken();
83 IdentifierInfo *II = PTHMgr.GetIdentifierInfo(IdentifierID-1);
Mike Stump11289f42009-09-09 15:08:12 +000084
Chris Lattner3029b352009-01-21 07:50:06 +000085 Tok.setIdentifierInfo(II);
Mike Stump11289f42009-09-09 15:08:12 +000086
Chris Lattner1f6c7fe2009-01-23 18:35:48 +000087 // Change the kind of this identifier to the appropriate token kind, e.g.
88 // turning "for" into a keyword.
89 Tok.setKind(II->getTokenID());
Mike Stump11289f42009-09-09 15:08:12 +000090
Chris Lattner3029b352009-01-21 07:50:06 +000091 if (II->isHandleIdentifierCase())
Eli Friedman0834a4b2013-09-19 00:41:32 +000092 return PP->HandleIdentifier(Tok);
93
94 return true;
Chris Lattner3029b352009-01-21 07:50:06 +000095 }
Mike Stump11289f42009-09-09 15:08:12 +000096
Ted Kremenek66076a92008-12-23 02:30:15 +000097 //===--------------------------------------==//
98 // Process the token.
99 //===--------------------------------------==//
Chris Lattner18fc6ce2009-01-18 02:34:01 +0000100 if (TKind == tok::eof) {
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000101 // Save the end-of-file token.
102 EofToken = Tok;
Mike Stump11289f42009-09-09 15:08:12 +0000103
Ted Kremenek78cc2472008-12-23 19:24:24 +0000104 assert(!ParsingPreprocessorDirective);
105 assert(!LexingRawMode);
Mike Stump11289f42009-09-09 15:08:12 +0000106
Eli Friedman0834a4b2013-09-19 00:41:32 +0000107 return LexEndOfFile(Tok);
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000108 }
Mike Stump11289f42009-09-09 15:08:12 +0000109
Chris Lattner18fc6ce2009-01-18 02:34:01 +0000110 if (TKind == tok::hash && Tok.isAtStartOfLine()) {
Ted Kremenek78cc2472008-12-23 19:24:24 +0000111 LastHashTokPtr = CurPtr - DISK_TOKEN_SIZE;
112 assert(!LexingRawMode);
113 PP->HandleDirective(Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000114
Eli Friedman0834a4b2013-09-19 00:41:32 +0000115 return false;
Ted Kremenek78cc2472008-12-23 19:24:24 +0000116 }
Mike Stump11289f42009-09-09 15:08:12 +0000117
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000118 if (TKind == tok::eod) {
Ted Kremenek78cc2472008-12-23 19:24:24 +0000119 assert(ParsingPreprocessorDirective);
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000120 ParsingPreprocessorDirective = false;
Eli Friedman0834a4b2013-09-19 00:41:32 +0000121 return true;
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000122 }
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000123
Ted Kremenek78cc2472008-12-23 19:24:24 +0000124 MIOpt.ReadToken();
Eli Friedman0834a4b2013-09-19 00:41:32 +0000125 return true;
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000126}
127
Ted Kremenek2bd41d12010-07-27 02:59:02 +0000128bool PTHLexer::LexEndOfFile(Token &Result) {
129 // If we hit the end of the file while parsing a preprocessor directive,
130 // end the preprocessor directive first. The next token returned will
131 // then be the end of file.
132 if (ParsingPreprocessorDirective) {
133 ParsingPreprocessorDirective = false; // Done parsing the "line".
134 return true; // Have a token.
135 }
136
137 assert(!LexingRawMode);
138
139 // If we are in a #if directive, emit an error.
140 while (!ConditionalStack.empty()) {
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000141 if (PP->getCodeCompletionFileLoc() != FileStartLoc)
Douglas Gregor02690ba2010-08-12 17:04:55 +0000142 PP->Diag(ConditionalStack.back().IfLoc,
143 diag::err_pp_unterminated_conditional);
Ted Kremenek2bd41d12010-07-27 02:59:02 +0000144 ConditionalStack.pop_back();
145 }
146
147 // Finally, let the preprocessor handle this.
148 return PP->HandleEndOfFile(Result);
149}
150
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000151// FIXME: We can just grab the last token instead of storing a copy
152// into EofToken.
Ted Kremenek78cc2472008-12-23 19:24:24 +0000153void PTHLexer::getEOF(Token& Tok) {
Ted Kremenek8ae06622009-01-09 00:36:11 +0000154 assert(EofToken.is(tok::eof));
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000155 Tok = EofToken;
156}
157
158void PTHLexer::DiscardToEndOfLine() {
159 assert(ParsingPreprocessorDirective && ParsingFilename == false &&
160 "Must be in a preprocessing directive!");
161
162 // We assume that if the preprocessor wishes to discard to the end of
163 // the line that it also means to end the current preprocessor directive.
164 ParsingPreprocessorDirective = false;
Mike Stump11289f42009-09-09 15:08:12 +0000165
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000166 // Skip tokens by only peeking at their token kind and the flags.
167 // We don't need to actually reconstruct full tokens from the token buffer.
168 // This saves some copies and it also reduces IdentifierInfo* lookup.
Chris Lattnereb097542009-01-18 01:57:14 +0000169 const unsigned char* p = CurPtr;
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000170 while (1) {
171 // Read the token kind. Are we at the end of the file?
172 tok::TokenKind x = (tok::TokenKind) (uint8_t) *p;
173 if (x == tok::eof) break;
Mike Stump11289f42009-09-09 15:08:12 +0000174
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000175 // Read the token flags. Are we at the start of the next line?
176 Token::TokenFlags y = (Token::TokenFlags) (uint8_t) p[1];
177 if (y & Token::StartOfLine) break;
178
179 // Skip to the next token.
180 p += DISK_TOKEN_SIZE;
181 }
Mike Stump11289f42009-09-09 15:08:12 +0000182
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000183 CurPtr = p;
184}
185
Ted Kremenek56572ab2008-12-12 18:34:08 +0000186/// SkipBlock - Used by Preprocessor to skip the current conditional block.
187bool PTHLexer::SkipBlock() {
188 assert(CurPPCondPtr && "No cached PP conditional information.");
189 assert(LastHashTokPtr && "No known '#' token.");
Mike Stump11289f42009-09-09 15:08:12 +0000190
Chris Lattnereb097542009-01-18 01:57:14 +0000191 const unsigned char* HashEntryI = 0;
Ted Kremenek56572ab2008-12-12 18:34:08 +0000192 uint32_t TableIdx;
Mike Stump11289f42009-09-09 15:08:12 +0000193
Ted Kremenek56572ab2008-12-12 18:34:08 +0000194 do {
Ted Kremenek877556f2008-12-12 22:05:38 +0000195 // Read the token offset from the side-table.
Justin Bognera0dafb72014-03-28 20:32:17 +0000196 uint32_t Offset = ReadLE32(CurPPCondPtr);
Mike Stump11289f42009-09-09 15:08:12 +0000197
198 // Read the target table index from the side-table.
Justin Bognera0dafb72014-03-28 20:32:17 +0000199 TableIdx = ReadLE32(CurPPCondPtr);
Mike Stump11289f42009-09-09 15:08:12 +0000200
Ted Kremenek877556f2008-12-12 22:05:38 +0000201 // Compute the actual memory address of the '#' token data for this entry.
202 HashEntryI = TokBuf + Offset;
203
204 // Optmization: "Sibling jumping". #if...#else...#endif blocks can
205 // contain nested blocks. In the side-table we can jump over these
206 // nested blocks instead of doing a linear search if the next "sibling"
207 // entry is not at a location greater than LastHashTokPtr.
208 if (HashEntryI < LastHashTokPtr && TableIdx) {
209 // In the side-table we are still at an entry for a '#' token that
210 // is earlier than the last one we saw. Check if the location we would
211 // stride gets us closer.
Chris Lattnereb097542009-01-18 01:57:14 +0000212 const unsigned char* NextPPCondPtr =
213 PPCond + TableIdx*(sizeof(uint32_t)*2);
Ted Kremenek877556f2008-12-12 22:05:38 +0000214 assert(NextPPCondPtr >= CurPPCondPtr);
215 // Read where we should jump to.
Justin Bognera0dafb72014-03-28 20:32:17 +0000216 const unsigned char* HashEntryJ = TokBuf + ReadLE32(NextPPCondPtr);
Mike Stump11289f42009-09-09 15:08:12 +0000217
Ted Kremenek877556f2008-12-12 22:05:38 +0000218 if (HashEntryJ <= LastHashTokPtr) {
219 // Jump directly to the next entry in the side table.
220 HashEntryI = HashEntryJ;
Justin Bognera0dafb72014-03-28 20:32:17 +0000221 TableIdx = ReadLE32(NextPPCondPtr);
Ted Kremenek877556f2008-12-12 22:05:38 +0000222 CurPPCondPtr = NextPPCondPtr;
223 }
224 }
Ted Kremenek56572ab2008-12-12 18:34:08 +0000225 }
Mike Stump11289f42009-09-09 15:08:12 +0000226 while (HashEntryI < LastHashTokPtr);
Ted Kremenek877556f2008-12-12 22:05:38 +0000227 assert(HashEntryI == LastHashTokPtr && "No PP-cond entry found for '#'");
Ted Kremenek56572ab2008-12-12 18:34:08 +0000228 assert(TableIdx && "No jumping from #endifs.");
Mike Stump11289f42009-09-09 15:08:12 +0000229
Ted Kremenek56572ab2008-12-12 18:34:08 +0000230 // Update our side-table iterator.
Chris Lattnereb097542009-01-18 01:57:14 +0000231 const unsigned char* NextPPCondPtr = PPCond + TableIdx*(sizeof(uint32_t)*2);
Ted Kremenek56572ab2008-12-12 18:34:08 +0000232 assert(NextPPCondPtr >= CurPPCondPtr);
233 CurPPCondPtr = NextPPCondPtr;
Mike Stump11289f42009-09-09 15:08:12 +0000234
Ted Kremenek56572ab2008-12-12 18:34:08 +0000235 // Read where we should jump to.
Justin Bognera0dafb72014-03-28 20:32:17 +0000236 HashEntryI = TokBuf + ReadLE32(NextPPCondPtr);
237 uint32_t NextIdx = ReadLE32(NextPPCondPtr);
Mike Stump11289f42009-09-09 15:08:12 +0000238
Ted Kremenek56572ab2008-12-12 18:34:08 +0000239 // By construction NextIdx will be zero if this is a #endif. This is useful
240 // to know to obviate lexing another token.
241 bool isEndif = NextIdx == 0;
Mike Stump11289f42009-09-09 15:08:12 +0000242
Ted Kremenek56572ab2008-12-12 18:34:08 +0000243 // This case can occur when we see something like this:
244 //
245 // #if ...
246 // /* a comment or nothing */
247 // #elif
248 //
249 // If we are skipping the first #if block it will be the case that CurPtr
250 // already points 'elif'. Just return.
Mike Stump11289f42009-09-09 15:08:12 +0000251
Ted Kremenek877556f2008-12-12 22:05:38 +0000252 if (CurPtr > HashEntryI) {
253 assert(CurPtr == HashEntryI + DISK_TOKEN_SIZE);
Ted Kremenek56572ab2008-12-12 18:34:08 +0000254 // Did we reach a #endif? If so, go ahead and consume that token as well.
255 if (isEndif)
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000256 CurPtr += DISK_TOKEN_SIZE*2;
Ted Kremenek56572ab2008-12-12 18:34:08 +0000257 else
Ted Kremenek877556f2008-12-12 22:05:38 +0000258 LastHashTokPtr = HashEntryI;
Mike Stump11289f42009-09-09 15:08:12 +0000259
Ted Kremenek56572ab2008-12-12 18:34:08 +0000260 return isEndif;
261 }
262
263 // Otherwise, we need to advance. Update CurPtr to point to the '#' token.
Ted Kremenek877556f2008-12-12 22:05:38 +0000264 CurPtr = HashEntryI;
Mike Stump11289f42009-09-09 15:08:12 +0000265
Ted Kremenek56572ab2008-12-12 18:34:08 +0000266 // Update the location of the last observed '#'. This is useful if we
267 // are skipping multiple blocks.
268 LastHashTokPtr = CurPtr;
Ted Kremenek56572ab2008-12-12 18:34:08 +0000269
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000270 // Skip the '#' token.
Chris Lattnereb097542009-01-18 01:57:14 +0000271 assert(((tok::TokenKind)*CurPtr) == tok::hash);
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000272 CurPtr += DISK_TOKEN_SIZE;
Mike Stump11289f42009-09-09 15:08:12 +0000273
Ted Kremenek56572ab2008-12-12 18:34:08 +0000274 // Did we reach a #endif? If so, go ahead and consume that token as well.
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000275 if (isEndif) { CurPtr += DISK_TOKEN_SIZE*2; }
Ted Kremenek56572ab2008-12-12 18:34:08 +0000276
277 return isEndif;
278}
279
Ted Kremenek63ff81c2008-12-17 23:36:32 +0000280SourceLocation PTHLexer::getSourceLocation() {
Chris Lattner137d6492009-01-18 02:10:31 +0000281 // getSourceLocation is not on the hot path. It is used to get the location
282 // of the next token when transitioning back to this lexer when done
Ted Kremenek63ff81c2008-12-17 23:36:32 +0000283 // handling a #included file. Just read the necessary data from the token
284 // data buffer to construct the SourceLocation object.
285 // NOTE: This is a virtual function; hence it is defined out-of-line.
Ted Kremenekae54f2f2009-01-21 22:41:38 +0000286 const unsigned char *OffsetPtr = CurPtr + (DISK_TOKEN_SIZE - 4);
Justin Bognera0dafb72014-03-28 20:32:17 +0000287 uint32_t Offset = ReadLE32(OffsetPtr);
Argyrios Kyrtzidise6e67de2011-09-19 20:40:19 +0000288 return FileStartLoc.getLocWithOffset(Offset);
Ted Kremenek63ff81c2008-12-17 23:36:32 +0000289}
290
Ted Kremenek47b8cf62009-01-09 22:05:30 +0000291//===----------------------------------------------------------------------===//
Ted Kremenek86423a92009-02-10 22:16:22 +0000292// PTH file lookup: map from strings to file data.
293//===----------------------------------------------------------------------===//
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000294
295/// PTHFileLookup - This internal data structure is used by the PTHManager
296/// to map from FileEntry objects managed by FileManager to offsets within
297/// the PTH file.
298namespace {
Benjamin Kramer337e3a52009-11-28 19:45:26 +0000299class PTHFileData {
Ted Kremenek86423a92009-02-10 22:16:22 +0000300 const uint32_t TokenOff;
301 const uint32_t PPCondOff;
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000302public:
Ted Kremenek86423a92009-02-10 22:16:22 +0000303 PTHFileData(uint32_t tokenOff, uint32_t ppCondOff)
304 : TokenOff(tokenOff), PPCondOff(ppCondOff) {}
Mike Stump11289f42009-09-09 15:08:12 +0000305
306 uint32_t getTokenOffset() const { return TokenOff; }
307 uint32_t getPPCondOffset() const { return PPCondOff; }
Ted Kremenek86423a92009-02-10 22:16:22 +0000308};
Mike Stump11289f42009-09-09 15:08:12 +0000309
310
Benjamin Kramer337e3a52009-11-28 19:45:26 +0000311class PTHFileLookupCommonTrait {
Ted Kremenek86423a92009-02-10 22:16:22 +0000312public:
Ted Kremenek29942a32009-02-13 19:13:46 +0000313 typedef std::pair<unsigned char, const char*> internal_key_type;
314
315 static unsigned ComputeHash(internal_key_type x) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +0000316 return llvm::HashString(x.second);
Ted Kremenek86423a92009-02-10 22:16:22 +0000317 }
Mike Stump11289f42009-09-09 15:08:12 +0000318
Ted Kremenek86423a92009-02-10 22:16:22 +0000319 static std::pair<unsigned, unsigned>
320 ReadKeyDataLength(const unsigned char*& d) {
Justin Bognera0dafb72014-03-28 20:32:17 +0000321 unsigned keyLen = (unsigned) ReadUnalignedLE16(d);
Ted Kremenek29942a32009-02-13 19:13:46 +0000322 unsigned dataLen = (unsigned) *(d++);
323 return std::make_pair(keyLen, dataLen);
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000324 }
Mike Stump11289f42009-09-09 15:08:12 +0000325
Ted Kremenek29942a32009-02-13 19:13:46 +0000326 static internal_key_type ReadKey(const unsigned char* d, unsigned) {
327 unsigned char k = *(d++); // Read the entry kind.
328 return std::make_pair(k, (const char*) d);
Ted Kremenek86423a92009-02-10 22:16:22 +0000329 }
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000330};
Mike Stump11289f42009-09-09 15:08:12 +0000331
Benjamin Kramer337e3a52009-11-28 19:45:26 +0000332class PTHFileLookupTrait : public PTHFileLookupCommonTrait {
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000333public:
334 typedef const FileEntry* external_key_type;
335 typedef PTHFileData data_type;
Mike Stump11289f42009-09-09 15:08:12 +0000336
Ted Kremenek29942a32009-02-13 19:13:46 +0000337 static internal_key_type GetInternalKey(const FileEntry* FE) {
338 return std::make_pair((unsigned char) 0x1, FE->getName());
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000339 }
Ted Kremenek29942a32009-02-13 19:13:46 +0000340
341 static bool EqualKey(internal_key_type a, internal_key_type b) {
342 return a.first == b.first && strcmp(a.second, b.second) == 0;
Mike Stump11289f42009-09-09 15:08:12 +0000343 }
344
345 static PTHFileData ReadData(const internal_key_type& k,
346 const unsigned char* d, unsigned) {
Ted Kremenek29942a32009-02-13 19:13:46 +0000347 assert(k.first == 0x1 && "Only file lookups can match!");
Justin Bognera0dafb72014-03-28 20:32:17 +0000348 uint32_t x = ::ReadUnalignedLE32(d);
349 uint32_t y = ::ReadUnalignedLE32(d);
Mike Stump11289f42009-09-09 15:08:12 +0000350 return PTHFileData(x, y);
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000351 }
352};
Ted Kremeneke5554de2009-02-11 21:29:16 +0000353
Benjamin Kramer337e3a52009-11-28 19:45:26 +0000354class PTHStringLookupTrait {
Ted Kremeneke5554de2009-02-11 21:29:16 +0000355public:
Mike Stump11289f42009-09-09 15:08:12 +0000356 typedef uint32_t
Ted Kremeneke5554de2009-02-11 21:29:16 +0000357 data_type;
358
359 typedef const std::pair<const char*, unsigned>
360 external_key_type;
361
362 typedef external_key_type internal_key_type;
Mike Stump11289f42009-09-09 15:08:12 +0000363
Ted Kremeneke5554de2009-02-11 21:29:16 +0000364 static bool EqualKey(const internal_key_type& a,
365 const internal_key_type& b) {
366 return (a.second == b.second) ? memcmp(a.first, b.first, a.second) == 0
367 : false;
368 }
Mike Stump11289f42009-09-09 15:08:12 +0000369
Ted Kremeneke5554de2009-02-11 21:29:16 +0000370 static unsigned ComputeHash(const internal_key_type& a) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000371 return llvm::HashString(StringRef(a.first, a.second));
Ted Kremeneke5554de2009-02-11 21:29:16 +0000372 }
Mike Stump11289f42009-09-09 15:08:12 +0000373
Ted Kremeneke5554de2009-02-11 21:29:16 +0000374 // This hopefully will just get inlined and removed by the optimizer.
375 static const internal_key_type&
376 GetInternalKey(const external_key_type& x) { return x; }
Mike Stump11289f42009-09-09 15:08:12 +0000377
Ted Kremeneke5554de2009-02-11 21:29:16 +0000378 static std::pair<unsigned, unsigned>
379 ReadKeyDataLength(const unsigned char*& d) {
Justin Bognera0dafb72014-03-28 20:32:17 +0000380 return std::make_pair((unsigned) ReadUnalignedLE16(d), sizeof(uint32_t));
Ted Kremeneke5554de2009-02-11 21:29:16 +0000381 }
Mike Stump11289f42009-09-09 15:08:12 +0000382
Ted Kremeneke5554de2009-02-11 21:29:16 +0000383 static std::pair<const char*, unsigned>
384 ReadKey(const unsigned char* d, unsigned n) {
385 assert(n >= 2 && d[n-1] == '\0');
386 return std::make_pair((const char*) d, n-1);
387 }
Mike Stump11289f42009-09-09 15:08:12 +0000388
Ted Kremenek29942a32009-02-13 19:13:46 +0000389 static uint32_t ReadData(const internal_key_type& k, const unsigned char* d,
390 unsigned) {
Justin Bognera0dafb72014-03-28 20:32:17 +0000391 return ::ReadUnalignedLE32(d);
Ted Kremeneke5554de2009-02-11 21:29:16 +0000392 }
393};
Mike Stump11289f42009-09-09 15:08:12 +0000394
395} // end anonymous namespace
Ted Kremenek86423a92009-02-10 22:16:22 +0000396
Ted Kremeneke5554de2009-02-11 21:29:16 +0000397typedef OnDiskChainedHashTable<PTHFileLookupTrait> PTHFileLookup;
398typedef OnDiskChainedHashTable<PTHStringLookupTrait> PTHStringIdLookup;
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000399
400//===----------------------------------------------------------------------===//
401// PTHManager methods.
402//===----------------------------------------------------------------------===//
403
404PTHManager::PTHManager(const llvm::MemoryBuffer* buf, void* fileLookup,
Chris Lattnereb097542009-01-18 01:57:14 +0000405 const unsigned char* idDataTable,
Mike Stump11289f42009-09-09 15:08:12 +0000406 IdentifierInfo** perIDCache,
Ted Kremeneke5554de2009-02-11 21:29:16 +0000407 void* stringIdLookup, unsigned numIds,
Ted Kremenekf9ccd5c2009-03-19 22:19:30 +0000408 const unsigned char* spellingBase,
409 const char* originalSourceFile)
Ted Kremenek73a4d282008-12-03 01:16:39 +0000410: Buf(buf), PerIDCache(perIDCache), FileLookup(fileLookup),
Ted Kremeneke5554de2009-02-11 21:29:16 +0000411 IdDataTable(idDataTable), StringIdLookup(stringIdLookup),
Ted Kremenekf9ccd5c2009-03-19 22:19:30 +0000412 NumIds(numIds), PP(0), SpellingBase(spellingBase),
413 OriginalSourceFile(originalSourceFile) {}
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000414
415PTHManager::~PTHManager() {
416 delete Buf;
417 delete (PTHFileLookup*) FileLookup;
Ted Kremeneke5554de2009-02-11 21:29:16 +0000418 delete (PTHStringIdLookup*) StringIdLookup;
Ted Kremenek1aed3dd2008-12-04 22:47:11 +0000419 free(PerIDCache);
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000420}
421
David Blaikie9c902b52011-09-25 23:23:43 +0000422static void InvalidPTH(DiagnosticsEngine &Diags, const char *Msg) {
Alp Toker29cb66b2014-01-26 06:17:37 +0000423 Diags.Report(Diags.getCustomDiagID(DiagnosticsEngine::Error, "%0")) << Msg;
Ted Kremenek62224c12009-01-28 21:02:43 +0000424}
425
David Blaikie9c902b52011-09-25 23:23:43 +0000426PTHManager *PTHManager::Create(const std::string &file,
427 DiagnosticsEngine &Diags) {
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000428 // Memory map the PTH file.
Ahmed Charlesb8984322014-03-07 20:03:18 +0000429 std::unique_ptr<llvm::MemoryBuffer> File;
Mike Stump11289f42009-09-09 15:08:12 +0000430
Michael J. Spencerd9da7a12010-12-16 03:28:14 +0000431 if (llvm::MemoryBuffer::getFile(file, File)) {
Michael J. Spencerf25faaa2010-12-09 17:36:38 +0000432 // FIXME: Add ec.message() to this diag.
Daniel Dunbar1a54e3f2009-11-12 02:53:48 +0000433 Diags.Report(diag::err_invalid_pth_file) << file;
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000434 return 0;
Ted Kremenek3b0589e2009-01-28 20:49:33 +0000435 }
Mike Stump11289f42009-09-09 15:08:12 +0000436
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000437 // Get the buffer ranges and check if there are at least three 32-bit
438 // words at the end of the file.
Roman Divackye6377112012-09-06 15:59:27 +0000439 const unsigned char *BufBeg = (const unsigned char*)File->getBufferStart();
440 const unsigned char *BufEnd = (const unsigned char*)File->getBufferEnd();
Ted Kremenekeb8c8fb2009-01-26 21:43:14 +0000441
442 // Check the prologue of the file.
Richard Smith2683b4c2012-08-17 03:55:43 +0000443 if ((BufEnd - BufBeg) < (signed)(sizeof("cfe-pth") + 4 + 4) ||
444 memcmp(BufBeg, "cfe-pth", sizeof("cfe-pth")) != 0) {
Daniel Dunbar1a54e3f2009-11-12 02:53:48 +0000445 Diags.Report(diag::err_invalid_pth_file) << file;
Ted Kremenekeb8c8fb2009-01-26 21:43:14 +0000446 return 0;
Ted Kremenek62224c12009-01-28 21:02:43 +0000447 }
Mike Stump11289f42009-09-09 15:08:12 +0000448
Ted Kremenek978b5be2009-01-26 21:50:21 +0000449 // Read the PTH version.
Richard Smith2683b4c2012-08-17 03:55:43 +0000450 const unsigned char *p = BufBeg + (sizeof("cfe-pth"));
Justin Bognera0dafb72014-03-28 20:32:17 +0000451 unsigned Version = ReadLE32(p);
Mike Stump11289f42009-09-09 15:08:12 +0000452
Daniel Dunbar1a54e3f2009-11-12 02:53:48 +0000453 if (Version < PTHManager::Version) {
454 InvalidPTH(Diags,
Mike Stump11289f42009-09-09 15:08:12 +0000455 Version < PTHManager::Version
Ted Kremenek62224c12009-01-28 21:02:43 +0000456 ? "PTH file uses an older PTH format that is no longer supported"
457 : "PTH file uses a newer PTH format that cannot be read");
Ted Kremenek978b5be2009-01-26 21:50:21 +0000458 return 0;
Ted Kremenek62224c12009-01-28 21:02:43 +0000459 }
Ted Kremenek978b5be2009-01-26 21:50:21 +0000460
Mike Stump11289f42009-09-09 15:08:12 +0000461 // Compute the address of the index table at the end of the PTH file.
Ted Kremenek4c1d41f2009-02-11 23:34:32 +0000462 const unsigned char *PrologueOffset = p;
Mike Stump11289f42009-09-09 15:08:12 +0000463
Ted Kremenek4c1d41f2009-02-11 23:34:32 +0000464 if (PrologueOffset >= BufEnd) {
Daniel Dunbar1a54e3f2009-11-12 02:53:48 +0000465 Diags.Report(diag::err_invalid_pth_file) << file;
Ted Kremenekeb8c8fb2009-01-26 21:43:14 +0000466 return 0;
Ted Kremenek62224c12009-01-28 21:02:43 +0000467 }
Mike Stump11289f42009-09-09 15:08:12 +0000468
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000469 // Construct the file lookup table. This will be used for mapping from
470 // FileEntry*'s to cached tokens.
Ted Kremenek4c1d41f2009-02-11 23:34:32 +0000471 const unsigned char* FileTableOffset = PrologueOffset + sizeof(uint32_t)*2;
Justin Bognera0dafb72014-03-28 20:32:17 +0000472 const unsigned char* FileTable = BufBeg + ReadLE32(FileTableOffset);
Mike Stump11289f42009-09-09 15:08:12 +0000473
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000474 if (!(FileTable > BufBeg && FileTable < BufEnd)) {
Daniel Dunbar1a54e3f2009-11-12 02:53:48 +0000475 Diags.Report(diag::err_invalid_pth_file) << file;
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000476 return 0; // FIXME: Proper error diagnostic?
477 }
Mike Stump11289f42009-09-09 15:08:12 +0000478
Ahmed Charlesb8984322014-03-07 20:03:18 +0000479 std::unique_ptr<PTHFileLookup> FL(PTHFileLookup::Create(FileTable, BufBeg));
Mike Stump11289f42009-09-09 15:08:12 +0000480
Ted Kremenek80e239a2009-03-20 17:54:25 +0000481 // Warn if the PTH file is empty. We still want to create a PTHManager
482 // as the PTH could be used with -include-pth.
483 if (FL->isEmpty())
Daniel Dunbar1a54e3f2009-11-12 02:53:48 +0000484 InvalidPTH(Diags, "PTH file contains no cached source data");
Mike Stump11289f42009-09-09 15:08:12 +0000485
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000486 // Get the location of the table mapping from persistent ids to the
487 // data needed to reconstruct identifiers.
Ted Kremenek4c1d41f2009-02-11 23:34:32 +0000488 const unsigned char* IDTableOffset = PrologueOffset + sizeof(uint32_t)*0;
Justin Bognera0dafb72014-03-28 20:32:17 +0000489 const unsigned char* IData = BufBeg + ReadLE32(IDTableOffset);
Mike Stump11289f42009-09-09 15:08:12 +0000490
Ted Kremenek8d6c8282009-01-21 07:34:28 +0000491 if (!(IData >= BufBeg && IData < BufEnd)) {
Daniel Dunbar1a54e3f2009-11-12 02:53:48 +0000492 Diags.Report(diag::err_invalid_pth_file) << file;
Ted Kremenek62224c12009-01-28 21:02:43 +0000493 return 0;
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000494 }
Mike Stump11289f42009-09-09 15:08:12 +0000495
Ted Kremeneke5554de2009-02-11 21:29:16 +0000496 // Get the location of the hashtable mapping between strings and
497 // persistent IDs.
Ted Kremenek4c1d41f2009-02-11 23:34:32 +0000498 const unsigned char* StringIdTableOffset = PrologueOffset + sizeof(uint32_t)*1;
Justin Bognera0dafb72014-03-28 20:32:17 +0000499 const unsigned char* StringIdTable = BufBeg + ReadLE32(StringIdTableOffset);
Ted Kremeneke5554de2009-02-11 21:29:16 +0000500 if (!(StringIdTable >= BufBeg && StringIdTable < BufEnd)) {
Daniel Dunbar1a54e3f2009-11-12 02:53:48 +0000501 Diags.Report(diag::err_invalid_pth_file) << file;
Ted Kremenek62224c12009-01-28 21:02:43 +0000502 return 0;
Ted Kremeneka705b042009-01-15 18:47:46 +0000503 }
Ted Kremeneke5554de2009-02-11 21:29:16 +0000504
Ahmed Charlesb8984322014-03-07 20:03:18 +0000505 std::unique_ptr<PTHStringIdLookup> SL(
506 PTHStringIdLookup::Create(StringIdTable, BufBeg));
Mike Stump11289f42009-09-09 15:08:12 +0000507
Ted Kremenek8d178f42009-01-27 00:01:05 +0000508 // Get the location of the spelling cache.
Ted Kremenek4c1d41f2009-02-11 23:34:32 +0000509 const unsigned char* spellingBaseOffset = PrologueOffset + sizeof(uint32_t)*3;
Justin Bognera0dafb72014-03-28 20:32:17 +0000510 const unsigned char* spellingBase = BufBeg + ReadLE32(spellingBaseOffset);
Ted Kremenek8d178f42009-01-27 00:01:05 +0000511 if (!(spellingBase >= BufBeg && spellingBase < BufEnd)) {
Daniel Dunbar1a54e3f2009-11-12 02:53:48 +0000512 Diags.Report(diag::err_invalid_pth_file) << file;
Ted Kremenek8d178f42009-01-27 00:01:05 +0000513 return 0;
514 }
Mike Stump11289f42009-09-09 15:08:12 +0000515
Ted Kremenek73a4d282008-12-03 01:16:39 +0000516 // Get the number of IdentifierInfos and pre-allocate the identifier cache.
Justin Bognera0dafb72014-03-28 20:32:17 +0000517 uint32_t NumIds = ReadLE32(IData);
Mike Stump11289f42009-09-09 15:08:12 +0000518
Chris Lattner57540c52011-04-15 05:22:18 +0000519 // Pre-allocate the persistent ID -> IdentifierInfo* cache. We use calloc()
Ted Kremenek73a4d282008-12-03 01:16:39 +0000520 // so that we in the best case only zero out memory once when the OS returns
521 // us new pages.
Ted Kremenek8d6c8282009-01-21 07:34:28 +0000522 IdentifierInfo** PerIDCache = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000523
Ted Kremenek8d6c8282009-01-21 07:34:28 +0000524 if (NumIds) {
Mike Stump11289f42009-09-09 15:08:12 +0000525 PerIDCache = (IdentifierInfo**)calloc(NumIds, sizeof(*PerIDCache));
Ted Kremenek8d6c8282009-01-21 07:34:28 +0000526 if (!PerIDCache) {
Daniel Dunbar1a54e3f2009-11-12 02:53:48 +0000527 InvalidPTH(Diags, "Could not allocate memory for processing PTH file");
Ted Kremenek8d6c8282009-01-21 07:34:28 +0000528 return 0;
529 }
Ted Kremenek73a4d282008-12-03 01:16:39 +0000530 }
Ted Kremenek8d6c8282009-01-21 07:34:28 +0000531
Ted Kremenekf9ccd5c2009-03-19 22:19:30 +0000532 // Compute the address of the original source file.
533 const unsigned char* originalSourceBase = PrologueOffset + sizeof(uint32_t)*4;
Justin Bognera0dafb72014-03-28 20:32:17 +0000534 unsigned len = ReadUnalignedLE16(originalSourceBase);
Mike Stump11289f42009-09-09 15:08:12 +0000535 if (!len) originalSourceBase = 0;
536
Ted Kremeneka705b042009-01-15 18:47:46 +0000537 // Create the new PTHManager.
Ahmed Charles9a16beb2014-03-07 19:33:25 +0000538 return new PTHManager(File.release(), FL.release(), IData, PerIDCache,
539 SL.release(), NumIds, spellingBase,
540 (const char *)originalSourceBase);
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000541}
Ted Kremenekf9ccd5c2009-03-19 22:19:30 +0000542
Chris Lattner144aacd2009-01-18 02:57:21 +0000543IdentifierInfo* PTHManager::LazilyCreateIdentifierInfo(unsigned PersistentID) {
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000544 // Look in the PTH file for the string data for the IdentifierInfo object.
Chris Lattner144aacd2009-01-18 02:57:21 +0000545 const unsigned char* TableEntry = IdDataTable + sizeof(uint32_t)*PersistentID;
Justin Bognera0dafb72014-03-28 20:32:17 +0000546 const unsigned char* IDData =
547 (const unsigned char*)Buf->getBufferStart() + ReadLE32(TableEntry);
Chris Lattnereb097542009-01-18 01:57:14 +0000548 assert(IDData < (const unsigned char*)Buf->getBufferEnd());
Mike Stump11289f42009-09-09 15:08:12 +0000549
Ted Kremeneka705b042009-01-15 18:47:46 +0000550 // Allocate the object.
Chris Lattnereb097542009-01-18 01:57:14 +0000551 std::pair<IdentifierInfo,const unsigned char*> *Mem =
552 Alloc.Allocate<std::pair<IdentifierInfo,const unsigned char*> >();
Ted Kremeneka705b042009-01-15 18:47:46 +0000553
554 Mem->second = IDData;
Ted Kremeneke5554de2009-02-11 21:29:16 +0000555 assert(IDData[0] != '\0');
Ted Kremenek52f73ca2009-01-20 23:28:34 +0000556 IdentifierInfo *II = new ((void*) Mem) IdentifierInfo();
Mike Stump11289f42009-09-09 15:08:12 +0000557
Ted Kremeneka705b042009-01-15 18:47:46 +0000558 // Store the new IdentifierInfo in the cache.
Chris Lattner144aacd2009-01-18 02:57:21 +0000559 PerIDCache[PersistentID] = II;
Daniel Dunbar2c422dc92009-10-18 20:26:12 +0000560 assert(II->getNameStart() && II->getNameStart()[0] != '\0');
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000561 return II;
562}
563
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000564IdentifierInfo* PTHManager::get(StringRef Name) {
Ted Kremeneke5554de2009-02-11 21:29:16 +0000565 PTHStringIdLookup& SL = *((PTHStringIdLookup*)StringIdLookup);
566 // Double check our assumption that the last character isn't '\0'.
David Blaikie441417a2011-09-22 01:35:49 +0000567 assert(Name.empty() || Name.back() != '\0');
Kovarththanan Rajaratnam661a3092010-03-12 08:23:34 +0000568 PTHStringIdLookup::iterator I = SL.find(std::make_pair(Name.data(),
569 Name.size()));
Ted Kremeneke5554de2009-02-11 21:29:16 +0000570 if (I == SL.end()) // No identifier found?
571 return 0;
Ted Kremeneka705b042009-01-15 18:47:46 +0000572
Ted Kremeneke5554de2009-02-11 21:29:16 +0000573 // Match found. Return the identifier!
574 assert(*I > 0);
575 return GetIdentifierInfo(*I-1);
576}
Ted Kremeneka705b042009-01-15 18:47:46 +0000577
Chris Lattnerab1d4b82009-01-17 08:06:50 +0000578PTHLexer *PTHManager::CreateLexer(FileID FID) {
579 const FileEntry *FE = PP->getSourceManager().getFileEntryForID(FID);
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000580 if (!FE)
581 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000582
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000583 // Lookup the FileEntry object in our file lookup data structure. It will
584 // return a variant that indicates whether or not there is an offset within
585 // the PTH file that contains cached tokens.
Ted Kremenek86423a92009-02-10 22:16:22 +0000586 PTHFileLookup& PFL = *((PTHFileLookup*)FileLookup);
587 PTHFileLookup::iterator I = PFL.find(FE);
Mike Stump11289f42009-09-09 15:08:12 +0000588
Ted Kremenek86423a92009-02-10 22:16:22 +0000589 if (I == PFL.end()) // No tokens available?
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000590 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000591
592 const PTHFileData& FileData = *I;
593
Chris Lattnereb097542009-01-18 01:57:14 +0000594 const unsigned char *BufStart = (const unsigned char *)Buf->getBufferStart();
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000595 // Compute the offset of the token data within the buffer.
Chris Lattnereb097542009-01-18 01:57:14 +0000596 const unsigned char* data = BufStart + FileData.getTokenOffset();
Ted Kremenek56572ab2008-12-12 18:34:08 +0000597
598 // Get the location of pp-conditional table.
Chris Lattnereb097542009-01-18 01:57:14 +0000599 const unsigned char* ppcond = BufStart + FileData.getPPCondOffset();
Justin Bognera0dafb72014-03-28 20:32:17 +0000600 uint32_t Len = ReadLE32(ppcond);
Chris Lattner137d6492009-01-18 02:10:31 +0000601 if (Len == 0) ppcond = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000602
Ted Kremeneka705b042009-01-15 18:47:46 +0000603 assert(PP && "No preprocessor set yet!");
Mike Stump11289f42009-09-09 15:08:12 +0000604 return new PTHLexer(*PP, FID, data, ppcond, *this);
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000605}
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000606
607//===----------------------------------------------------------------------===//
608// 'stat' caching.
609//===----------------------------------------------------------------------===//
610
611namespace {
Benjamin Kramer337e3a52009-11-28 19:45:26 +0000612class PTHStatData {
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000613public:
Rafael Espindolaf8f91b82013-08-01 21:42:11 +0000614 const bool HasData;
615 uint64_t Size;
616 time_t ModTime;
617 llvm::sys::fs::UniqueID UniqueID;
618 bool IsDirectory;
Mike Stump11289f42009-09-09 15:08:12 +0000619
Rafael Espindolaf8f91b82013-08-01 21:42:11 +0000620 PTHStatData(uint64_t Size, time_t ModTime, llvm::sys::fs::UniqueID UniqueID,
621 bool IsDirectory)
622 : HasData(true), Size(Size), ModTime(ModTime), UniqueID(UniqueID),
623 IsDirectory(IsDirectory) {}
Mike Stump11289f42009-09-09 15:08:12 +0000624
Rafael Espindolaf8f91b82013-08-01 21:42:11 +0000625 PTHStatData() : HasData(false) {}
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000626};
Mike Stump11289f42009-09-09 15:08:12 +0000627
Benjamin Kramer337e3a52009-11-28 19:45:26 +0000628class PTHStatLookupTrait : public PTHFileLookupCommonTrait {
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000629public:
Ted Kremenek29942a32009-02-13 19:13:46 +0000630 typedef const char* external_key_type; // const char*
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000631 typedef PTHStatData data_type;
Mike Stump11289f42009-09-09 15:08:12 +0000632
Ted Kremenek29942a32009-02-13 19:13:46 +0000633 static internal_key_type GetInternalKey(const char *path) {
634 // The key 'kind' doesn't matter here because it is ignored in EqualKey.
635 return std::make_pair((unsigned char) 0x0, path);
636 }
637
638 static bool EqualKey(internal_key_type a, internal_key_type b) {
639 // When doing 'stat' lookups we don't care about the kind of 'a' and 'b',
640 // just the paths.
641 return strcmp(a.second, b.second) == 0;
Mike Stump11289f42009-09-09 15:08:12 +0000642 }
643
Ted Kremenek29942a32009-02-13 19:13:46 +0000644 static data_type ReadData(const internal_key_type& k, const unsigned char* d,
Mike Stump11289f42009-09-09 15:08:12 +0000645 unsigned) {
646
Ted Kremenek2fd18ec2009-02-13 22:07:44 +0000647 if (k.first /* File or Directory */) {
Rafael Espindolaf8f91b82013-08-01 21:42:11 +0000648 bool IsDirectory = true;
649 if (k.first == 0x1 /* File */) {
650 IsDirectory = false;
651 d += 4 * 2; // Skip the first 2 words.
652 }
653
Justin Bognera0dafb72014-03-28 20:32:17 +0000654 uint64_t File = ReadUnalignedLE64(d);
655 uint64_t Device = ReadUnalignedLE64(d);
Rafael Espindolaf8f91b82013-08-01 21:42:11 +0000656 llvm::sys::fs::UniqueID UniqueID(File, Device);
Justin Bognera0dafb72014-03-28 20:32:17 +0000657 time_t ModTime = ReadUnalignedLE64(d);
658 uint64_t Size = ReadUnalignedLE64(d);
Rafael Espindolaf8f91b82013-08-01 21:42:11 +0000659 return data_type(Size, ModTime, UniqueID, IsDirectory);
Ted Kremenek29942a32009-02-13 19:13:46 +0000660 }
Ted Kremenek2fd18ec2009-02-13 22:07:44 +0000661
662 // Negative stat. Don't read anything.
Ted Kremenek29942a32009-02-13 19:13:46 +0000663 return data_type();
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000664 }
665};
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000666
Chris Lattner226efd32010-11-23 19:19:34 +0000667class PTHStatCache : public FileSystemStatCache {
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000668 typedef OnDiskChainedHashTable<PTHStatLookupTrait> CacheTy;
669 CacheTy Cache;
670
Mike Stump11289f42009-09-09 15:08:12 +0000671public:
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000672 PTHStatCache(PTHFileLookup &FL) :
673 Cache(FL.getNumBuckets(), FL.getNumEntries(), FL.getBuckets(),
674 FL.getBase()) {}
675
676 ~PTHStatCache() {}
Mike Stump11289f42009-09-09 15:08:12 +0000677
Rafael Espindolaf8f91b82013-08-01 21:42:11 +0000678 LookupResult getStat(const char *Path, FileData &Data, bool isFile,
Craig Topper9140dd22014-03-11 06:50:42 +0000679 vfs::File **F, vfs::FileSystem &FS) override {
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000680 // Do the lookup for the file's data in the PTH file.
Chris Lattner226efd32010-11-23 19:19:34 +0000681 CacheTy::iterator I = Cache.find(Path);
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000682
683 // If we don't get a hit in the PTH file just forward to 'stat'.
Kovarththanan Rajaratnam661a3092010-03-12 08:23:34 +0000684 if (I == Cache.end())
Ben Langmuirc8130a72014-02-20 21:59:23 +0000685 return statChained(Path, Data, isFile, F, FS);
Mike Stump11289f42009-09-09 15:08:12 +0000686
Rafael Espindolaf8f91b82013-08-01 21:42:11 +0000687 const PTHStatData &D = *I;
Mike Stump11289f42009-09-09 15:08:12 +0000688
Rafael Espindolaf8f91b82013-08-01 21:42:11 +0000689 if (!D.HasData)
Chris Lattner8f0583d2010-11-23 20:05:15 +0000690 return CacheMissing;
Ted Kremenek2fd18ec2009-02-13 22:07:44 +0000691
Ben Langmuird066d4c2014-02-28 21:16:07 +0000692 Data.Name = Path;
Rafael Espindolaf8f91b82013-08-01 21:42:11 +0000693 Data.Size = D.Size;
694 Data.ModTime = D.ModTime;
695 Data.UniqueID = D.UniqueID;
696 Data.IsDirectory = D.IsDirectory;
697 Data.IsNamedPipe = false;
698 Data.InPCH = true;
699
Chris Lattner8f0583d2010-11-23 20:05:15 +0000700 return CacheExists;
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000701 }
702};
Ted Kremenek29b86972009-02-23 23:27:54 +0000703} // end anonymous namespace
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000704
Chris Lattner226efd32010-11-23 19:19:34 +0000705FileSystemStatCache *PTHManager::createStatCache() {
Ted Kremenekb4c85cc2009-02-12 03:45:39 +0000706 return new PTHStatCache(*((PTHFileLookup*) FileLookup));
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000707}