blob: d985abac6b3121781709b4d373c566297d44cf4f [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"
Chandler Carruth3a022472012-12-04 09:13:33 +000018#include "clang/Basic/TokenKinds.h"
Daniel Dunbar1a54e3f2009-11-12 02:53:48 +000019#include "clang/Lex/LexDiagnostic.h"
Ted Kremenek33eeabd2008-12-03 00:38:03 +000020#include "clang/Lex/PTHManager.h"
Ted Kremenek33eeabd2008-12-03 00:38:03 +000021#include "clang/Lex/Preprocessor.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000022#include "clang/Lex/Token.h"
Daniel Dunbarf8502d52009-10-17 23:52:28 +000023#include "llvm/ADT/StringExtras.h"
24#include "llvm/ADT/StringMap.h"
Justin Bognere1c147c2014-03-28 22:03:19 +000025#include "llvm/Support/EndianStream.h"
Chris Lattner34eab392009-01-22 23:50:07 +000026#include "llvm/Support/MemoryBuffer.h"
Justin Bognerbb094f02014-04-18 19:57:06 +000027#include "llvm/Support/OnDiskHashTable.h"
Ahmed Charlesdfca6f92014-03-09 11:36:40 +000028#include <memory>
Rafael Espindola8a8e5542014-06-12 17:19:42 +000029#include <system_error>
Ted Kremenek7cd62452008-11-12 21:37:15 +000030using namespace clang;
31
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)
Craig Topperd2d442c2014-05-17 23:10:59 +000040 : PreprocessorLexer(&PP, FID), TokBuf(D), CurPtr(D), LastHashTokPtr(nullptr),
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 //===--------------------------------------==//
Justin Bogner57ba0b22014-03-28 22:03:24 +000050 using namespace llvm::support;
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 Bogner57ba0b22014-03-28 22:03:24 +000056 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 Stump11289f42009-09-09 15:08:12 +000061
Ted Kremenek8433f0b2009-01-19 23:13:15 +000062 tok::TokenKind TKind = (tok::TokenKind) (Word0 & 0xFF);
63 Token::TokenFlags TFlags = (Token::TokenFlags) ((Word0 >> 8) & 0xFF);
Chris Lattner47def972009-01-21 07:21:56 +000064 uint32_t Len = Word0 >> 16;
Ted Kremenek8433f0b2009-01-19 23:13:15 +000065
Chris Lattner47def972009-01-21 07:21:56 +000066 CurPtr = CurPtrShadow;
Mike Stump11289f42009-09-09 15:08:12 +000067
Ted Kremenek66076a92008-12-23 02:30:15 +000068 //===--------------------------------------==//
69 // Construct the token itself.
70 //===--------------------------------------==//
Mike Stump11289f42009-09-09 15:08:12 +000071
Ted Kremenek66076a92008-12-23 02:30:15 +000072 Tok.startToken();
Chris Lattner18fc6ce2009-01-18 02:34:01 +000073 Tok.setKind(TKind);
74 Tok.setFlag(TFlags);
Ted Kremenek78cc2472008-12-23 19:24:24 +000075 assert(!LexingRawMode);
Argyrios Kyrtzidise6e67de2011-09-19 20:40:19 +000076 Tok.setLocation(FileStartLoc.getLocWithOffset(FileOffset));
Ted Kremenek66076a92008-12-23 02:30:15 +000077 Tok.setLength(Len);
78
Chris Lattner3029b352009-01-21 07:50:06 +000079 // Handle identifiers.
Ted Kremenek8d178f42009-01-27 00:01:05 +000080 if (Tok.isLiteral()) {
81 Tok.setLiteralData((const char*) (PTHMgr.SpellingBase + IdentifierID));
82 }
83 else if (IdentifierID) {
Chris Lattner3029b352009-01-21 07:50:06 +000084 MIOpt.ReadToken();
85 IdentifierInfo *II = PTHMgr.GetIdentifierInfo(IdentifierID-1);
Mike Stump11289f42009-09-09 15:08:12 +000086
Chris Lattner3029b352009-01-21 07:50:06 +000087 Tok.setIdentifierInfo(II);
Mike Stump11289f42009-09-09 15:08:12 +000088
Chris Lattner1f6c7fe2009-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 Stump11289f42009-09-09 15:08:12 +000092
Chris Lattner3029b352009-01-21 07:50:06 +000093 if (II->isHandleIdentifierCase())
Eli Friedman0834a4b2013-09-19 00:41:32 +000094 return PP->HandleIdentifier(Tok);
95
96 return true;
Chris Lattner3029b352009-01-21 07:50:06 +000097 }
Mike Stump11289f42009-09-09 15:08:12 +000098
Ted Kremenek66076a92008-12-23 02:30:15 +000099 //===--------------------------------------==//
100 // Process the token.
101 //===--------------------------------------==//
Chris Lattner18fc6ce2009-01-18 02:34:01 +0000102 if (TKind == tok::eof) {
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000103 // Save the end-of-file token.
104 EofToken = Tok;
Mike Stump11289f42009-09-09 15:08:12 +0000105
Ted Kremenek78cc2472008-12-23 19:24:24 +0000106 assert(!ParsingPreprocessorDirective);
107 assert(!LexingRawMode);
Mike Stump11289f42009-09-09 15:08:12 +0000108
Eli Friedman0834a4b2013-09-19 00:41:32 +0000109 return LexEndOfFile(Tok);
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000110 }
Mike Stump11289f42009-09-09 15:08:12 +0000111
Chris Lattner18fc6ce2009-01-18 02:34:01 +0000112 if (TKind == tok::hash && Tok.isAtStartOfLine()) {
Ted Kremenek78cc2472008-12-23 19:24:24 +0000113 LastHashTokPtr = CurPtr - DISK_TOKEN_SIZE;
114 assert(!LexingRawMode);
115 PP->HandleDirective(Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000116
Eli Friedman0834a4b2013-09-19 00:41:32 +0000117 return false;
Ted Kremenek78cc2472008-12-23 19:24:24 +0000118 }
Mike Stump11289f42009-09-09 15:08:12 +0000119
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000120 if (TKind == tok::eod) {
Ted Kremenek78cc2472008-12-23 19:24:24 +0000121 assert(ParsingPreprocessorDirective);
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000122 ParsingPreprocessorDirective = false;
Eli Friedman0834a4b2013-09-19 00:41:32 +0000123 return true;
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000124 }
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000125
Ted Kremenek78cc2472008-12-23 19:24:24 +0000126 MIOpt.ReadToken();
Eli Friedman0834a4b2013-09-19 00:41:32 +0000127 return true;
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000128}
129
Ted Kremenek2bd41d12010-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 Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000143 if (PP->getCodeCompletionFileLoc() != FileStartLoc)
Douglas Gregor02690ba2010-08-12 17:04:55 +0000144 PP->Diag(ConditionalStack.back().IfLoc,
145 diag::err_pp_unterminated_conditional);
Ted Kremenek2bd41d12010-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 Kremenek1b18ad22008-12-23 01:30:52 +0000153// FIXME: We can just grab the last token instead of storing a copy
154// into EofToken.
Ted Kremenek78cc2472008-12-23 19:24:24 +0000155void PTHLexer::getEOF(Token& Tok) {
Ted Kremenek8ae06622009-01-09 00:36:11 +0000156 assert(EofToken.is(tok::eof));
Ted Kremenek1b18ad22008-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 Stump11289f42009-09-09 15:08:12 +0000167
Ted Kremenek1b18ad22008-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 Lattnereb097542009-01-18 01:57:14 +0000171 const unsigned char* p = CurPtr;
Ted Kremenek1b18ad22008-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 Stump11289f42009-09-09 15:08:12 +0000176
Ted Kremenek1b18ad22008-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 Stump11289f42009-09-09 15:08:12 +0000184
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000185 CurPtr = p;
186}
187
Ted Kremenek56572ab2008-12-12 18:34:08 +0000188/// SkipBlock - Used by Preprocessor to skip the current conditional block.
189bool PTHLexer::SkipBlock() {
Justin Bogner57ba0b22014-03-28 22:03:24 +0000190 using namespace llvm::support;
Ted Kremenek56572ab2008-12-12 18:34:08 +0000191 assert(CurPPCondPtr && "No cached PP conditional information.");
192 assert(LastHashTokPtr && "No known '#' token.");
Mike Stump11289f42009-09-09 15:08:12 +0000193
Craig Topperd2d442c2014-05-17 23:10:59 +0000194 const unsigned char *HashEntryI = nullptr;
Ted Kremenek56572ab2008-12-12 18:34:08 +0000195 uint32_t TableIdx;
Mike Stump11289f42009-09-09 15:08:12 +0000196
Ted Kremenek56572ab2008-12-12 18:34:08 +0000197 do {
Ted Kremenek877556f2008-12-12 22:05:38 +0000198 // Read the token offset from the side-table.
Justin Bogner57ba0b22014-03-28 22:03:24 +0000199 uint32_t Offset = endian::readNext<uint32_t, little, aligned>(CurPPCondPtr);
Mike Stump11289f42009-09-09 15:08:12 +0000200
201 // Read the target table index from the side-table.
Justin Bogner57ba0b22014-03-28 22:03:24 +0000202 TableIdx = endian::readNext<uint32_t, little, aligned>(CurPPCondPtr);
Mike Stump11289f42009-09-09 15:08:12 +0000203
Ted Kremenek877556f2008-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 Lattnereb097542009-01-18 01:57:14 +0000215 const unsigned char* NextPPCondPtr =
216 PPCond + TableIdx*(sizeof(uint32_t)*2);
Ted Kremenek877556f2008-12-12 22:05:38 +0000217 assert(NextPPCondPtr >= CurPPCondPtr);
218 // Read where we should jump to.
Justin Bogner57ba0b22014-03-28 22:03:24 +0000219 const unsigned char *HashEntryJ =
220 TokBuf + endian::readNext<uint32_t, little, aligned>(NextPPCondPtr);
Mike Stump11289f42009-09-09 15:08:12 +0000221
Ted Kremenek877556f2008-12-12 22:05:38 +0000222 if (HashEntryJ <= LastHashTokPtr) {
223 // Jump directly to the next entry in the side table.
224 HashEntryI = HashEntryJ;
Justin Bogner57ba0b22014-03-28 22:03:24 +0000225 TableIdx = endian::readNext<uint32_t, little, aligned>(NextPPCondPtr);
Ted Kremenek877556f2008-12-12 22:05:38 +0000226 CurPPCondPtr = NextPPCondPtr;
227 }
228 }
Ted Kremenek56572ab2008-12-12 18:34:08 +0000229 }
Mike Stump11289f42009-09-09 15:08:12 +0000230 while (HashEntryI < LastHashTokPtr);
Ted Kremenek877556f2008-12-12 22:05:38 +0000231 assert(HashEntryI == LastHashTokPtr && "No PP-cond entry found for '#'");
Ted Kremenek56572ab2008-12-12 18:34:08 +0000232 assert(TableIdx && "No jumping from #endifs.");
Mike Stump11289f42009-09-09 15:08:12 +0000233
Ted Kremenek56572ab2008-12-12 18:34:08 +0000234 // Update our side-table iterator.
Chris Lattnereb097542009-01-18 01:57:14 +0000235 const unsigned char* NextPPCondPtr = PPCond + TableIdx*(sizeof(uint32_t)*2);
Ted Kremenek56572ab2008-12-12 18:34:08 +0000236 assert(NextPPCondPtr >= CurPPCondPtr);
237 CurPPCondPtr = NextPPCondPtr;
Mike Stump11289f42009-09-09 15:08:12 +0000238
Ted Kremenek56572ab2008-12-12 18:34:08 +0000239 // Read where we should jump to.
Justin Bogner57ba0b22014-03-28 22:03:24 +0000240 HashEntryI =
241 TokBuf + endian::readNext<uint32_t, little, aligned>(NextPPCondPtr);
242 uint32_t NextIdx = endian::readNext<uint32_t, little, aligned>(NextPPCondPtr);
Mike Stump11289f42009-09-09 15:08:12 +0000243
Ted Kremenek56572ab2008-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 Stump11289f42009-09-09 15:08:12 +0000247
Ted Kremenek56572ab2008-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 Stump11289f42009-09-09 15:08:12 +0000256
Ted Kremenek877556f2008-12-12 22:05:38 +0000257 if (CurPtr > HashEntryI) {
258 assert(CurPtr == HashEntryI + DISK_TOKEN_SIZE);
Ted Kremenek56572ab2008-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 Kremenek1b18ad22008-12-23 01:30:52 +0000261 CurPtr += DISK_TOKEN_SIZE*2;
Ted Kremenek56572ab2008-12-12 18:34:08 +0000262 else
Ted Kremenek877556f2008-12-12 22:05:38 +0000263 LastHashTokPtr = HashEntryI;
Mike Stump11289f42009-09-09 15:08:12 +0000264
Ted Kremenek56572ab2008-12-12 18:34:08 +0000265 return isEndif;
266 }
267
268 // Otherwise, we need to advance. Update CurPtr to point to the '#' token.
Ted Kremenek877556f2008-12-12 22:05:38 +0000269 CurPtr = HashEntryI;
Mike Stump11289f42009-09-09 15:08:12 +0000270
Ted Kremenek56572ab2008-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 Kremenek56572ab2008-12-12 18:34:08 +0000274
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000275 // Skip the '#' token.
Chris Lattnereb097542009-01-18 01:57:14 +0000276 assert(((tok::TokenKind)*CurPtr) == tok::hash);
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000277 CurPtr += DISK_TOKEN_SIZE;
Mike Stump11289f42009-09-09 15:08:12 +0000278
Ted Kremenek56572ab2008-12-12 18:34:08 +0000279 // Did we reach a #endif? If so, go ahead and consume that token as well.
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000280 if (isEndif) { CurPtr += DISK_TOKEN_SIZE*2; }
Ted Kremenek56572ab2008-12-12 18:34:08 +0000281
282 return isEndif;
283}
284
Ted Kremenek63ff81c2008-12-17 23:36:32 +0000285SourceLocation PTHLexer::getSourceLocation() {
Chris Lattner137d6492009-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 Kremenek63ff81c2008-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.
Justin Bogner57ba0b22014-03-28 22:03:24 +0000291 using namespace llvm::support;
292
Ted Kremenekae54f2f2009-01-21 22:41:38 +0000293 const unsigned char *OffsetPtr = CurPtr + (DISK_TOKEN_SIZE - 4);
Justin Bogner57ba0b22014-03-28 22:03:24 +0000294 uint32_t Offset = endian::readNext<uint32_t, little, aligned>(OffsetPtr);
Argyrios Kyrtzidise6e67de2011-09-19 20:40:19 +0000295 return FileStartLoc.getLocWithOffset(Offset);
Ted Kremenek63ff81c2008-12-17 23:36:32 +0000296}
297
Ted Kremenek47b8cf62009-01-09 22:05:30 +0000298//===----------------------------------------------------------------------===//
Ted Kremenek86423a92009-02-10 22:16:22 +0000299// PTH file lookup: map from strings to file data.
300//===----------------------------------------------------------------------===//
Ted Kremenek33eeabd2008-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 Kramer337e3a52009-11-28 19:45:26 +0000306class PTHFileData {
Ted Kremenek86423a92009-02-10 22:16:22 +0000307 const uint32_t TokenOff;
308 const uint32_t PPCondOff;
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000309public:
Ted Kremenek86423a92009-02-10 22:16:22 +0000310 PTHFileData(uint32_t tokenOff, uint32_t ppCondOff)
311 : TokenOff(tokenOff), PPCondOff(ppCondOff) {}
Mike Stump11289f42009-09-09 15:08:12 +0000312
313 uint32_t getTokenOffset() const { return TokenOff; }
314 uint32_t getPPCondOffset() const { return PPCondOff; }
Ted Kremenek86423a92009-02-10 22:16:22 +0000315};
Mike Stump11289f42009-09-09 15:08:12 +0000316
317
Benjamin Kramer337e3a52009-11-28 19:45:26 +0000318class PTHFileLookupCommonTrait {
Ted Kremenek86423a92009-02-10 22:16:22 +0000319public:
Ted Kremenek29942a32009-02-13 19:13:46 +0000320 typedef std::pair<unsigned char, const char*> internal_key_type;
Justin Bogner25463f12014-04-18 20:27:24 +0000321 typedef unsigned hash_value_type;
322 typedef unsigned offset_type;
Ted Kremenek29942a32009-02-13 19:13:46 +0000323
Justin Bogner25463f12014-04-18 20:27:24 +0000324 static hash_value_type ComputeHash(internal_key_type x) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +0000325 return llvm::HashString(x.second);
Ted Kremenek86423a92009-02-10 22:16:22 +0000326 }
Mike Stump11289f42009-09-09 15:08:12 +0000327
Ted Kremenek86423a92009-02-10 22:16:22 +0000328 static std::pair<unsigned, unsigned>
329 ReadKeyDataLength(const unsigned char*& d) {
Justin Bogner57ba0b22014-03-28 22:03:24 +0000330 using namespace llvm::support;
331 unsigned keyLen =
332 (unsigned)endian::readNext<uint16_t, little, unaligned>(d);
Ted Kremenek29942a32009-02-13 19:13:46 +0000333 unsigned dataLen = (unsigned) *(d++);
334 return std::make_pair(keyLen, dataLen);
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000335 }
Mike Stump11289f42009-09-09 15:08:12 +0000336
Ted Kremenek29942a32009-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 Kremenek86423a92009-02-10 22:16:22 +0000340 }
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000341};
Mike Stump11289f42009-09-09 15:08:12 +0000342
Benjamin Kramer337e3a52009-11-28 19:45:26 +0000343class PTHFileLookupTrait : public PTHFileLookupCommonTrait {
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000344public:
345 typedef const FileEntry* external_key_type;
346 typedef PTHFileData data_type;
Mike Stump11289f42009-09-09 15:08:12 +0000347
Ted Kremenek29942a32009-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 Kremeneka5c2c272009-02-12 03:26:59 +0000350 }
Ted Kremenek29942a32009-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 Stump11289f42009-09-09 15:08:12 +0000354 }
355
356 static PTHFileData ReadData(const internal_key_type& k,
357 const unsigned char* d, unsigned) {
Ted Kremenek29942a32009-02-13 19:13:46 +0000358 assert(k.first == 0x1 && "Only file lookups can match!");
Justin Bogner57ba0b22014-03-28 22:03:24 +0000359 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 Stump11289f42009-09-09 15:08:12 +0000362 return PTHFileData(x, y);
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000363 }
364};
Ted Kremeneke5554de2009-02-11 21:29:16 +0000365
Benjamin Kramer337e3a52009-11-28 19:45:26 +0000366class PTHStringLookupTrait {
Ted Kremeneke5554de2009-02-11 21:29:16 +0000367public:
Justin Bogner25463f12014-04-18 20:27:24 +0000368 typedef uint32_t data_type;
369 typedef const std::pair<const char*, unsigned> external_key_type;
Ted Kremeneke5554de2009-02-11 21:29:16 +0000370 typedef external_key_type internal_key_type;
Justin Bogner25463f12014-04-18 20:27:24 +0000371 typedef uint32_t hash_value_type;
372 typedef unsigned offset_type;
Mike Stump11289f42009-09-09 15:08:12 +0000373
Ted Kremeneke5554de2009-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 Stump11289f42009-09-09 15:08:12 +0000379
Justin Bogner25463f12014-04-18 20:27:24 +0000380 static hash_value_type ComputeHash(const internal_key_type& a) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000381 return llvm::HashString(StringRef(a.first, a.second));
Ted Kremeneke5554de2009-02-11 21:29:16 +0000382 }
Mike Stump11289f42009-09-09 15:08:12 +0000383
Ted Kremeneke5554de2009-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 Stump11289f42009-09-09 15:08:12 +0000387
Ted Kremeneke5554de2009-02-11 21:29:16 +0000388 static std::pair<unsigned, unsigned>
389 ReadKeyDataLength(const unsigned char*& d) {
Justin Bogner57ba0b22014-03-28 22:03:24 +0000390 using namespace llvm::support;
391 return std::make_pair(
392 (unsigned)endian::readNext<uint16_t, little, unaligned>(d),
393 sizeof(uint32_t));
Ted Kremeneke5554de2009-02-11 21:29:16 +0000394 }
Mike Stump11289f42009-09-09 15:08:12 +0000395
Ted Kremeneke5554de2009-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 Stump11289f42009-09-09 15:08:12 +0000401
Ted Kremenek29942a32009-02-13 19:13:46 +0000402 static uint32_t ReadData(const internal_key_type& k, const unsigned char* d,
403 unsigned) {
Justin Bogner57ba0b22014-03-28 22:03:24 +0000404 using namespace llvm::support;
405 return endian::readNext<uint32_t, little, unaligned>(d);
Ted Kremeneke5554de2009-02-11 21:29:16 +0000406 }
407};
Mike Stump11289f42009-09-09 15:08:12 +0000408
409} // end anonymous namespace
Ted Kremenek86423a92009-02-10 22:16:22 +0000410
Justin Bognerbb094f02014-04-18 19:57:06 +0000411typedef llvm::OnDiskChainedHashTable<PTHFileLookupTrait> PTHFileLookup;
412typedef llvm::OnDiskChainedHashTable<PTHStringLookupTrait> PTHStringIdLookup;
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000413
414//===----------------------------------------------------------------------===//
415// PTHManager methods.
416//===----------------------------------------------------------------------===//
417
418PTHManager::PTHManager(const llvm::MemoryBuffer* buf, void* fileLookup,
Chris Lattnereb097542009-01-18 01:57:14 +0000419 const unsigned char* idDataTable,
Mike Stump11289f42009-09-09 15:08:12 +0000420 IdentifierInfo** perIDCache,
Ted Kremeneke5554de2009-02-11 21:29:16 +0000421 void* stringIdLookup, unsigned numIds,
Ted Kremenekf9ccd5c2009-03-19 22:19:30 +0000422 const unsigned char* spellingBase,
423 const char* originalSourceFile)
Ted Kremenek73a4d282008-12-03 01:16:39 +0000424: Buf(buf), PerIDCache(perIDCache), FileLookup(fileLookup),
Ted Kremeneke5554de2009-02-11 21:29:16 +0000425 IdDataTable(idDataTable), StringIdLookup(stringIdLookup),
Craig Topperd2d442c2014-05-17 23:10:59 +0000426 NumIds(numIds), PP(nullptr), SpellingBase(spellingBase),
Ted Kremenekf9ccd5c2009-03-19 22:19:30 +0000427 OriginalSourceFile(originalSourceFile) {}
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000428
429PTHManager::~PTHManager() {
430 delete Buf;
431 delete (PTHFileLookup*) FileLookup;
Ted Kremeneke5554de2009-02-11 21:29:16 +0000432 delete (PTHStringIdLookup*) StringIdLookup;
Ted Kremenek1aed3dd2008-12-04 22:47:11 +0000433 free(PerIDCache);
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000434}
435
David Blaikie9c902b52011-09-25 23:23:43 +0000436static void InvalidPTH(DiagnosticsEngine &Diags, const char *Msg) {
Alp Toker29cb66b2014-01-26 06:17:37 +0000437 Diags.Report(Diags.getCustomDiagID(DiagnosticsEngine::Error, "%0")) << Msg;
Ted Kremenek62224c12009-01-28 21:02:43 +0000438}
439
David Blaikie9c902b52011-09-25 23:23:43 +0000440PTHManager *PTHManager::Create(const std::string &file,
441 DiagnosticsEngine &Diags) {
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000442 // Memory map the PTH file.
Rafael Espindola2d2b4202014-07-06 17:43:24 +0000443 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> FileOrErr =
444 llvm::MemoryBuffer::getFile(file);
Mike Stump11289f42009-09-09 15:08:12 +0000445
Rafael Espindola2d2b4202014-07-06 17:43:24 +0000446 if (!FileOrErr) {
Michael J. Spencerf25faaa2010-12-09 17:36:38 +0000447 // FIXME: Add ec.message() to this diag.
Daniel Dunbar1a54e3f2009-11-12 02:53:48 +0000448 Diags.Report(diag::err_invalid_pth_file) << file;
Craig Topperd2d442c2014-05-17 23:10:59 +0000449 return nullptr;
Ted Kremenek3b0589e2009-01-28 20:49:33 +0000450 }
Rafael Espindola2d2b4202014-07-06 17:43:24 +0000451 std::unique_ptr<llvm::MemoryBuffer> File = std::move(FileOrErr.get());
Mike Stump11289f42009-09-09 15:08:12 +0000452
Justin Bogner57ba0b22014-03-28 22:03:24 +0000453 using namespace llvm::support;
454
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000455 // Get the buffer ranges and check if there are at least three 32-bit
456 // words at the end of the file.
Roman Divackye6377112012-09-06 15:59:27 +0000457 const unsigned char *BufBeg = (const unsigned char*)File->getBufferStart();
458 const unsigned char *BufEnd = (const unsigned char*)File->getBufferEnd();
Ted Kremenekeb8c8fb2009-01-26 21:43:14 +0000459
460 // Check the prologue of the file.
Richard Smith2683b4c2012-08-17 03:55:43 +0000461 if ((BufEnd - BufBeg) < (signed)(sizeof("cfe-pth") + 4 + 4) ||
462 memcmp(BufBeg, "cfe-pth", sizeof("cfe-pth")) != 0) {
Daniel Dunbar1a54e3f2009-11-12 02:53:48 +0000463 Diags.Report(diag::err_invalid_pth_file) << file;
Craig Topperd2d442c2014-05-17 23:10:59 +0000464 return nullptr;
Ted Kremenek62224c12009-01-28 21:02:43 +0000465 }
Mike Stump11289f42009-09-09 15:08:12 +0000466
Ted Kremenek978b5be2009-01-26 21:50:21 +0000467 // Read the PTH version.
Richard Smith2683b4c2012-08-17 03:55:43 +0000468 const unsigned char *p = BufBeg + (sizeof("cfe-pth"));
Justin Bogner57ba0b22014-03-28 22:03:24 +0000469 unsigned Version = endian::readNext<uint32_t, little, aligned>(p);
Mike Stump11289f42009-09-09 15:08:12 +0000470
Daniel Dunbar1a54e3f2009-11-12 02:53:48 +0000471 if (Version < PTHManager::Version) {
472 InvalidPTH(Diags,
Mike Stump11289f42009-09-09 15:08:12 +0000473 Version < PTHManager::Version
Ted Kremenek62224c12009-01-28 21:02:43 +0000474 ? "PTH file uses an older PTH format that is no longer supported"
475 : "PTH file uses a newer PTH format that cannot be read");
Craig Topperd2d442c2014-05-17 23:10:59 +0000476 return nullptr;
Ted Kremenek62224c12009-01-28 21:02:43 +0000477 }
Ted Kremenek978b5be2009-01-26 21:50:21 +0000478
Mike Stump11289f42009-09-09 15:08:12 +0000479 // Compute the address of the index table at the end of the PTH file.
Ted Kremenek4c1d41f2009-02-11 23:34:32 +0000480 const unsigned char *PrologueOffset = p;
Mike Stump11289f42009-09-09 15:08:12 +0000481
Ted Kremenek4c1d41f2009-02-11 23:34:32 +0000482 if (PrologueOffset >= BufEnd) {
Daniel Dunbar1a54e3f2009-11-12 02:53:48 +0000483 Diags.Report(diag::err_invalid_pth_file) << file;
Craig Topperd2d442c2014-05-17 23:10:59 +0000484 return nullptr;
Ted Kremenek62224c12009-01-28 21:02:43 +0000485 }
Mike Stump11289f42009-09-09 15:08:12 +0000486
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000487 // Construct the file lookup table. This will be used for mapping from
488 // FileEntry*'s to cached tokens.
Ted Kremenek4c1d41f2009-02-11 23:34:32 +0000489 const unsigned char* FileTableOffset = PrologueOffset + sizeof(uint32_t)*2;
Justin Bogner57ba0b22014-03-28 22:03:24 +0000490 const unsigned char *FileTable =
491 BufBeg + endian::readNext<uint32_t, little, aligned>(FileTableOffset);
Mike Stump11289f42009-09-09 15:08:12 +0000492
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000493 if (!(FileTable > BufBeg && FileTable < BufEnd)) {
Daniel Dunbar1a54e3f2009-11-12 02:53:48 +0000494 Diags.Report(diag::err_invalid_pth_file) << file;
Craig Topperd2d442c2014-05-17 23:10:59 +0000495 return nullptr; // FIXME: Proper error diagnostic?
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000496 }
Mike Stump11289f42009-09-09 15:08:12 +0000497
Ahmed Charlesb8984322014-03-07 20:03:18 +0000498 std::unique_ptr<PTHFileLookup> FL(PTHFileLookup::Create(FileTable, BufBeg));
Mike Stump11289f42009-09-09 15:08:12 +0000499
Ted Kremenek80e239a2009-03-20 17:54:25 +0000500 // Warn if the PTH file is empty. We still want to create a PTHManager
501 // as the PTH could be used with -include-pth.
502 if (FL->isEmpty())
Daniel Dunbar1a54e3f2009-11-12 02:53:48 +0000503 InvalidPTH(Diags, "PTH file contains no cached source data");
Mike Stump11289f42009-09-09 15:08:12 +0000504
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000505 // Get the location of the table mapping from persistent ids to the
506 // data needed to reconstruct identifiers.
Ted Kremenek4c1d41f2009-02-11 23:34:32 +0000507 const unsigned char* IDTableOffset = PrologueOffset + sizeof(uint32_t)*0;
Justin Bogner57ba0b22014-03-28 22:03:24 +0000508 const unsigned char *IData =
509 BufBeg + endian::readNext<uint32_t, little, aligned>(IDTableOffset);
Mike Stump11289f42009-09-09 15:08:12 +0000510
Ted Kremenek8d6c8282009-01-21 07:34:28 +0000511 if (!(IData >= BufBeg && IData < BufEnd)) {
Daniel Dunbar1a54e3f2009-11-12 02:53:48 +0000512 Diags.Report(diag::err_invalid_pth_file) << file;
Craig Topperd2d442c2014-05-17 23:10:59 +0000513 return nullptr;
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000514 }
Mike Stump11289f42009-09-09 15:08:12 +0000515
Ted Kremeneke5554de2009-02-11 21:29:16 +0000516 // Get the location of the hashtable mapping between strings and
517 // persistent IDs.
Ted Kremenek4c1d41f2009-02-11 23:34:32 +0000518 const unsigned char* StringIdTableOffset = PrologueOffset + sizeof(uint32_t)*1;
Justin Bogner57ba0b22014-03-28 22:03:24 +0000519 const unsigned char *StringIdTable =
520 BufBeg + endian::readNext<uint32_t, little, aligned>(StringIdTableOffset);
Ted Kremeneke5554de2009-02-11 21:29:16 +0000521 if (!(StringIdTable >= BufBeg && StringIdTable < BufEnd)) {
Daniel Dunbar1a54e3f2009-11-12 02:53:48 +0000522 Diags.Report(diag::err_invalid_pth_file) << file;
Craig Topperd2d442c2014-05-17 23:10:59 +0000523 return nullptr;
Ted Kremeneka705b042009-01-15 18:47:46 +0000524 }
Ted Kremeneke5554de2009-02-11 21:29:16 +0000525
Ahmed Charlesb8984322014-03-07 20:03:18 +0000526 std::unique_ptr<PTHStringIdLookup> SL(
527 PTHStringIdLookup::Create(StringIdTable, BufBeg));
Mike Stump11289f42009-09-09 15:08:12 +0000528
Ted Kremenek8d178f42009-01-27 00:01:05 +0000529 // Get the location of the spelling cache.
Ted Kremenek4c1d41f2009-02-11 23:34:32 +0000530 const unsigned char* spellingBaseOffset = PrologueOffset + sizeof(uint32_t)*3;
Justin Bogner57ba0b22014-03-28 22:03:24 +0000531 const unsigned char *spellingBase =
532 BufBeg + endian::readNext<uint32_t, little, aligned>(spellingBaseOffset);
Ted Kremenek8d178f42009-01-27 00:01:05 +0000533 if (!(spellingBase >= BufBeg && spellingBase < BufEnd)) {
Daniel Dunbar1a54e3f2009-11-12 02:53:48 +0000534 Diags.Report(diag::err_invalid_pth_file) << file;
Craig Topperd2d442c2014-05-17 23:10:59 +0000535 return nullptr;
Ted Kremenek8d178f42009-01-27 00:01:05 +0000536 }
Mike Stump11289f42009-09-09 15:08:12 +0000537
Ted Kremenek73a4d282008-12-03 01:16:39 +0000538 // Get the number of IdentifierInfos and pre-allocate the identifier cache.
Justin Bogner57ba0b22014-03-28 22:03:24 +0000539 uint32_t NumIds = endian::readNext<uint32_t, little, aligned>(IData);
Mike Stump11289f42009-09-09 15:08:12 +0000540
Chris Lattner57540c52011-04-15 05:22:18 +0000541 // Pre-allocate the persistent ID -> IdentifierInfo* cache. We use calloc()
Ted Kremenek73a4d282008-12-03 01:16:39 +0000542 // so that we in the best case only zero out memory once when the OS returns
543 // us new pages.
Craig Topperd2d442c2014-05-17 23:10:59 +0000544 IdentifierInfo **PerIDCache = nullptr;
Mike Stump11289f42009-09-09 15:08:12 +0000545
Ted Kremenek8d6c8282009-01-21 07:34:28 +0000546 if (NumIds) {
Mike Stump11289f42009-09-09 15:08:12 +0000547 PerIDCache = (IdentifierInfo**)calloc(NumIds, sizeof(*PerIDCache));
Ted Kremenek8d6c8282009-01-21 07:34:28 +0000548 if (!PerIDCache) {
Daniel Dunbar1a54e3f2009-11-12 02:53:48 +0000549 InvalidPTH(Diags, "Could not allocate memory for processing PTH file");
Craig Topperd2d442c2014-05-17 23:10:59 +0000550 return nullptr;
Ted Kremenek8d6c8282009-01-21 07:34:28 +0000551 }
Ted Kremenek73a4d282008-12-03 01:16:39 +0000552 }
Ted Kremenek8d6c8282009-01-21 07:34:28 +0000553
Ted Kremenekf9ccd5c2009-03-19 22:19:30 +0000554 // Compute the address of the original source file.
555 const unsigned char* originalSourceBase = PrologueOffset + sizeof(uint32_t)*4;
Justin Bogner57ba0b22014-03-28 22:03:24 +0000556 unsigned len =
557 endian::readNext<uint16_t, little, unaligned>(originalSourceBase);
Craig Topperd2d442c2014-05-17 23:10:59 +0000558 if (!len) originalSourceBase = nullptr;
Mike Stump11289f42009-09-09 15:08:12 +0000559
Ted Kremeneka705b042009-01-15 18:47:46 +0000560 // Create the new PTHManager.
Ahmed Charles9a16beb2014-03-07 19:33:25 +0000561 return new PTHManager(File.release(), FL.release(), IData, PerIDCache,
562 SL.release(), NumIds, spellingBase,
563 (const char *)originalSourceBase);
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000564}
Ted Kremenekf9ccd5c2009-03-19 22:19:30 +0000565
Chris Lattner144aacd2009-01-18 02:57:21 +0000566IdentifierInfo* PTHManager::LazilyCreateIdentifierInfo(unsigned PersistentID) {
Justin Bogner57ba0b22014-03-28 22:03:24 +0000567 using namespace llvm::support;
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000568 // Look in the PTH file for the string data for the IdentifierInfo object.
Chris Lattner144aacd2009-01-18 02:57:21 +0000569 const unsigned char* TableEntry = IdDataTable + sizeof(uint32_t)*PersistentID;
Justin Bogner57ba0b22014-03-28 22:03:24 +0000570 const unsigned char *IDData =
571 (const unsigned char *)Buf->getBufferStart() +
572 endian::readNext<uint32_t, little, aligned>(TableEntry);
Chris Lattnereb097542009-01-18 01:57:14 +0000573 assert(IDData < (const unsigned char*)Buf->getBufferEnd());
Mike Stump11289f42009-09-09 15:08:12 +0000574
Ted Kremeneka705b042009-01-15 18:47:46 +0000575 // Allocate the object.
Chris Lattnereb097542009-01-18 01:57:14 +0000576 std::pair<IdentifierInfo,const unsigned char*> *Mem =
577 Alloc.Allocate<std::pair<IdentifierInfo,const unsigned char*> >();
Ted Kremeneka705b042009-01-15 18:47:46 +0000578
579 Mem->second = IDData;
Ted Kremeneke5554de2009-02-11 21:29:16 +0000580 assert(IDData[0] != '\0');
Ted Kremenek52f73ca2009-01-20 23:28:34 +0000581 IdentifierInfo *II = new ((void*) Mem) IdentifierInfo();
Mike Stump11289f42009-09-09 15:08:12 +0000582
Ted Kremeneka705b042009-01-15 18:47:46 +0000583 // Store the new IdentifierInfo in the cache.
Chris Lattner144aacd2009-01-18 02:57:21 +0000584 PerIDCache[PersistentID] = II;
Daniel Dunbar2c422dc92009-10-18 20:26:12 +0000585 assert(II->getNameStart() && II->getNameStart()[0] != '\0');
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000586 return II;
587}
588
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000589IdentifierInfo* PTHManager::get(StringRef Name) {
Ted Kremeneke5554de2009-02-11 21:29:16 +0000590 PTHStringIdLookup& SL = *((PTHStringIdLookup*)StringIdLookup);
591 // Double check our assumption that the last character isn't '\0'.
David Blaikie441417a2011-09-22 01:35:49 +0000592 assert(Name.empty() || Name.back() != '\0');
Kovarththanan Rajaratnam661a3092010-03-12 08:23:34 +0000593 PTHStringIdLookup::iterator I = SL.find(std::make_pair(Name.data(),
594 Name.size()));
Ted Kremeneke5554de2009-02-11 21:29:16 +0000595 if (I == SL.end()) // No identifier found?
Craig Topperd2d442c2014-05-17 23:10:59 +0000596 return nullptr;
Ted Kremeneka705b042009-01-15 18:47:46 +0000597
Ted Kremeneke5554de2009-02-11 21:29:16 +0000598 // Match found. Return the identifier!
599 assert(*I > 0);
600 return GetIdentifierInfo(*I-1);
601}
Ted Kremeneka705b042009-01-15 18:47:46 +0000602
Chris Lattnerab1d4b82009-01-17 08:06:50 +0000603PTHLexer *PTHManager::CreateLexer(FileID FID) {
604 const FileEntry *FE = PP->getSourceManager().getFileEntryForID(FID);
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000605 if (!FE)
Craig Topperd2d442c2014-05-17 23:10:59 +0000606 return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +0000607
Justin Bogner57ba0b22014-03-28 22:03:24 +0000608 using namespace llvm::support;
609
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000610 // Lookup the FileEntry object in our file lookup data structure. It will
611 // return a variant that indicates whether or not there is an offset within
612 // the PTH file that contains cached tokens.
Ted Kremenek86423a92009-02-10 22:16:22 +0000613 PTHFileLookup& PFL = *((PTHFileLookup*)FileLookup);
614 PTHFileLookup::iterator I = PFL.find(FE);
Mike Stump11289f42009-09-09 15:08:12 +0000615
Ted Kremenek86423a92009-02-10 22:16:22 +0000616 if (I == PFL.end()) // No tokens available?
Craig Topperd2d442c2014-05-17 23:10:59 +0000617 return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +0000618
619 const PTHFileData& FileData = *I;
620
Chris Lattnereb097542009-01-18 01:57:14 +0000621 const unsigned char *BufStart = (const unsigned char *)Buf->getBufferStart();
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000622 // Compute the offset of the token data within the buffer.
Chris Lattnereb097542009-01-18 01:57:14 +0000623 const unsigned char* data = BufStart + FileData.getTokenOffset();
Ted Kremenek56572ab2008-12-12 18:34:08 +0000624
625 // Get the location of pp-conditional table.
Chris Lattnereb097542009-01-18 01:57:14 +0000626 const unsigned char* ppcond = BufStart + FileData.getPPCondOffset();
Justin Bogner57ba0b22014-03-28 22:03:24 +0000627 uint32_t Len = endian::readNext<uint32_t, little, aligned>(ppcond);
Craig Topperd2d442c2014-05-17 23:10:59 +0000628 if (Len == 0) ppcond = nullptr;
Mike Stump11289f42009-09-09 15:08:12 +0000629
Ted Kremeneka705b042009-01-15 18:47:46 +0000630 assert(PP && "No preprocessor set yet!");
Mike Stump11289f42009-09-09 15:08:12 +0000631 return new PTHLexer(*PP, FID, data, ppcond, *this);
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000632}
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000633
634//===----------------------------------------------------------------------===//
635// 'stat' caching.
636//===----------------------------------------------------------------------===//
637
638namespace {
Benjamin Kramer337e3a52009-11-28 19:45:26 +0000639class PTHStatData {
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000640public:
Rafael Espindolaf8f91b82013-08-01 21:42:11 +0000641 const bool HasData;
642 uint64_t Size;
643 time_t ModTime;
644 llvm::sys::fs::UniqueID UniqueID;
645 bool IsDirectory;
Mike Stump11289f42009-09-09 15:08:12 +0000646
Rafael Espindolaf8f91b82013-08-01 21:42:11 +0000647 PTHStatData(uint64_t Size, time_t ModTime, llvm::sys::fs::UniqueID UniqueID,
648 bool IsDirectory)
649 : HasData(true), Size(Size), ModTime(ModTime), UniqueID(UniqueID),
650 IsDirectory(IsDirectory) {}
Mike Stump11289f42009-09-09 15:08:12 +0000651
Rafael Espindolaf8f91b82013-08-01 21:42:11 +0000652 PTHStatData() : HasData(false) {}
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000653};
Mike Stump11289f42009-09-09 15:08:12 +0000654
Benjamin Kramer337e3a52009-11-28 19:45:26 +0000655class PTHStatLookupTrait : public PTHFileLookupCommonTrait {
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000656public:
Ted Kremenek29942a32009-02-13 19:13:46 +0000657 typedef const char* external_key_type; // const char*
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000658 typedef PTHStatData data_type;
Mike Stump11289f42009-09-09 15:08:12 +0000659
Ted Kremenek29942a32009-02-13 19:13:46 +0000660 static internal_key_type GetInternalKey(const char *path) {
661 // The key 'kind' doesn't matter here because it is ignored in EqualKey.
662 return std::make_pair((unsigned char) 0x0, path);
663 }
664
665 static bool EqualKey(internal_key_type a, internal_key_type b) {
666 // When doing 'stat' lookups we don't care about the kind of 'a' and 'b',
667 // just the paths.
668 return strcmp(a.second, b.second) == 0;
Mike Stump11289f42009-09-09 15:08:12 +0000669 }
670
Ted Kremenek29942a32009-02-13 19:13:46 +0000671 static data_type ReadData(const internal_key_type& k, const unsigned char* d,
Mike Stump11289f42009-09-09 15:08:12 +0000672 unsigned) {
673
Ted Kremenek2fd18ec2009-02-13 22:07:44 +0000674 if (k.first /* File or Directory */) {
Rafael Espindolaf8f91b82013-08-01 21:42:11 +0000675 bool IsDirectory = true;
676 if (k.first == 0x1 /* File */) {
677 IsDirectory = false;
678 d += 4 * 2; // Skip the first 2 words.
679 }
680
Justin Bogner57ba0b22014-03-28 22:03:24 +0000681 using namespace llvm::support;
682
683 uint64_t File = endian::readNext<uint64_t, little, unaligned>(d);
684 uint64_t Device = endian::readNext<uint64_t, little, unaligned>(d);
Rafael Espindolaf8f91b82013-08-01 21:42:11 +0000685 llvm::sys::fs::UniqueID UniqueID(File, Device);
Justin Bogner57ba0b22014-03-28 22:03:24 +0000686 time_t ModTime = endian::readNext<uint64_t, little, unaligned>(d);
687 uint64_t Size = endian::readNext<uint64_t, little, unaligned>(d);
Rafael Espindolaf8f91b82013-08-01 21:42:11 +0000688 return data_type(Size, ModTime, UniqueID, IsDirectory);
Ted Kremenek29942a32009-02-13 19:13:46 +0000689 }
Ted Kremenek2fd18ec2009-02-13 22:07:44 +0000690
691 // Negative stat. Don't read anything.
Ted Kremenek29942a32009-02-13 19:13:46 +0000692 return data_type();
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000693 }
694};
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000695
Chris Lattner226efd32010-11-23 19:19:34 +0000696class PTHStatCache : public FileSystemStatCache {
Justin Bognerbb094f02014-04-18 19:57:06 +0000697 typedef llvm::OnDiskChainedHashTable<PTHStatLookupTrait> CacheTy;
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000698 CacheTy Cache;
699
Mike Stump11289f42009-09-09 15:08:12 +0000700public:
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000701 PTHStatCache(PTHFileLookup &FL) :
702 Cache(FL.getNumBuckets(), FL.getNumEntries(), FL.getBuckets(),
703 FL.getBase()) {}
704
705 ~PTHStatCache() {}
Mike Stump11289f42009-09-09 15:08:12 +0000706
Rafael Espindolaf8f91b82013-08-01 21:42:11 +0000707 LookupResult getStat(const char *Path, FileData &Data, bool isFile,
Craig Topper9140dd22014-03-11 06:50:42 +0000708 vfs::File **F, vfs::FileSystem &FS) override {
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000709 // Do the lookup for the file's data in the PTH file.
Chris Lattner226efd32010-11-23 19:19:34 +0000710 CacheTy::iterator I = Cache.find(Path);
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000711
712 // If we don't get a hit in the PTH file just forward to 'stat'.
Kovarththanan Rajaratnam661a3092010-03-12 08:23:34 +0000713 if (I == Cache.end())
Ben Langmuirc8130a72014-02-20 21:59:23 +0000714 return statChained(Path, Data, isFile, F, FS);
Mike Stump11289f42009-09-09 15:08:12 +0000715
Rafael Espindolaf8f91b82013-08-01 21:42:11 +0000716 const PTHStatData &D = *I;
Mike Stump11289f42009-09-09 15:08:12 +0000717
Rafael Espindolaf8f91b82013-08-01 21:42:11 +0000718 if (!D.HasData)
Chris Lattner8f0583d2010-11-23 20:05:15 +0000719 return CacheMissing;
Ted Kremenek2fd18ec2009-02-13 22:07:44 +0000720
Ben Langmuird066d4c2014-02-28 21:16:07 +0000721 Data.Name = Path;
Rafael Espindolaf8f91b82013-08-01 21:42:11 +0000722 Data.Size = D.Size;
723 Data.ModTime = D.ModTime;
724 Data.UniqueID = D.UniqueID;
725 Data.IsDirectory = D.IsDirectory;
726 Data.IsNamedPipe = false;
727 Data.InPCH = true;
728
Chris Lattner8f0583d2010-11-23 20:05:15 +0000729 return CacheExists;
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000730 }
731};
Ted Kremenek29b86972009-02-23 23:27:54 +0000732} // end anonymous namespace
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000733
Chris Lattner226efd32010-11-23 19:19:34 +0000734FileSystemStatCache *PTHManager::createStatCache() {
Ted Kremenekb4c85cc2009-02-12 03:45:39 +0000735 return new PTHStatCache(*((PTHFileLookup*) FileLookup));
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000736}