blob: ec806e8445311c2da4b0c572833eab7a319b1b2b [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"
Justin Bognere1c147c2014-03-28 22:03:19 +000024#include "llvm/Support/EndianStream.h"
Chris Lattner34eab392009-01-22 23:50:07 +000025#include "llvm/Support/MemoryBuffer.h"
Ahmed Charlesdfca6f92014-03-09 11:36:40 +000026#include <memory>
Rafael Espindola8a8e5542014-06-12 17:19:42 +000027#include <system_error>
Ted Kremenek7cd62452008-11-12 21:37:15 +000028using namespace clang;
29
Alp Toker25847352014-07-07 14:01:37 +000030static const unsigned StoredTokenSize = 1 + 1 + 2 + 4 + 4;
Ted Kremenek56572ab2008-12-12 18:34:08 +000031
Ted Kremenek33eeabd2008-12-03 00:38:03 +000032//===----------------------------------------------------------------------===//
Ted Kremenek1b18ad22008-12-23 01:30:52 +000033// PTHLexer methods.
34//===----------------------------------------------------------------------===//
35
Chris Lattnereb097542009-01-18 01:57:14 +000036PTHLexer::PTHLexer(Preprocessor &PP, FileID FID, const unsigned char *D,
Ted Kremenek8d178f42009-01-27 00:01:05 +000037 const unsigned char *ppcond, PTHManager &PM)
Craig Topperd2d442c2014-05-17 23:10:59 +000038 : PreprocessorLexer(&PP, FID), TokBuf(D), CurPtr(D), LastHashTokPtr(nullptr),
Ted Kremenek8d178f42009-01-27 00:01:05 +000039 PPCond(ppcond), CurPPCondPtr(ppcond), PTHMgr(PM) {
Mike Stump11289f42009-09-09 15:08:12 +000040
Chris Lattnerd32480d2009-01-17 06:22:33 +000041 FileStartLoc = PP.getSourceManager().getLocForStartOfFile(FID);
Ted Kremenek47b8cf62009-01-09 22:05:30 +000042}
Ted Kremenek1b18ad22008-12-23 01:30:52 +000043
Eli Friedman0834a4b2013-09-19 00:41:32 +000044bool PTHLexer::Lex(Token& Tok) {
Ted Kremenek66076a92008-12-23 02:30:15 +000045 //===--------------------------------------==//
46 // Read the raw token data.
47 //===--------------------------------------==//
Justin Bogner57ba0b22014-03-28 22:03:24 +000048 using namespace llvm::support;
Mike Stump11289f42009-09-09 15:08:12 +000049
Ted Kremenek66076a92008-12-23 02:30:15 +000050 // Shadow CurPtr into an automatic variable.
Mike Stump11289f42009-09-09 15:08:12 +000051 const unsigned char *CurPtrShadow = CurPtr;
Ted Kremenek66076a92008-12-23 02:30:15 +000052
Chris Lattner137d6492009-01-18 02:10:31 +000053 // Read in the data for the token.
Justin Bogner57ba0b22014-03-28 22:03:24 +000054 unsigned Word0 = endian::readNext<uint32_t, little, aligned>(CurPtrShadow);
55 uint32_t IdentifierID =
56 endian::readNext<uint32_t, little, aligned>(CurPtrShadow);
57 uint32_t FileOffset =
58 endian::readNext<uint32_t, little, aligned>(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()) {
Alp Toker25847352014-07-07 14:01:37 +0000111 LastHashTokPtr = CurPtr - StoredTokenSize;
Ted Kremenek78cc2472008-12-23 19:24:24 +0000112 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.
Alp Toker25847352014-07-07 14:01:37 +0000180 p += StoredTokenSize;
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000181 }
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() {
Justin Bogner57ba0b22014-03-28 22:03:24 +0000188 using namespace llvm::support;
Ted Kremenek56572ab2008-12-12 18:34:08 +0000189 assert(CurPPCondPtr && "No cached PP conditional information.");
190 assert(LastHashTokPtr && "No known '#' token.");
Mike Stump11289f42009-09-09 15:08:12 +0000191
Craig Topperd2d442c2014-05-17 23:10:59 +0000192 const unsigned char *HashEntryI = nullptr;
Ted Kremenek56572ab2008-12-12 18:34:08 +0000193 uint32_t TableIdx;
Mike Stump11289f42009-09-09 15:08:12 +0000194
Ted Kremenek56572ab2008-12-12 18:34:08 +0000195 do {
Ted Kremenek877556f2008-12-12 22:05:38 +0000196 // Read the token offset from the side-table.
Justin Bogner57ba0b22014-03-28 22:03:24 +0000197 uint32_t Offset = endian::readNext<uint32_t, little, aligned>(CurPPCondPtr);
Mike Stump11289f42009-09-09 15:08:12 +0000198
199 // Read the target table index from the side-table.
Justin Bogner57ba0b22014-03-28 22:03:24 +0000200 TableIdx = endian::readNext<uint32_t, little, aligned>(CurPPCondPtr);
Mike Stump11289f42009-09-09 15:08:12 +0000201
Ted Kremenek877556f2008-12-12 22:05:38 +0000202 // Compute the actual memory address of the '#' token data for this entry.
203 HashEntryI = TokBuf + Offset;
204
205 // Optmization: "Sibling jumping". #if...#else...#endif blocks can
206 // contain nested blocks. In the side-table we can jump over these
207 // nested blocks instead of doing a linear search if the next "sibling"
208 // entry is not at a location greater than LastHashTokPtr.
209 if (HashEntryI < LastHashTokPtr && TableIdx) {
210 // In the side-table we are still at an entry for a '#' token that
211 // is earlier than the last one we saw. Check if the location we would
212 // stride gets us closer.
Chris Lattnereb097542009-01-18 01:57:14 +0000213 const unsigned char* NextPPCondPtr =
214 PPCond + TableIdx*(sizeof(uint32_t)*2);
Ted Kremenek877556f2008-12-12 22:05:38 +0000215 assert(NextPPCondPtr >= CurPPCondPtr);
216 // Read where we should jump to.
Justin Bogner57ba0b22014-03-28 22:03:24 +0000217 const unsigned char *HashEntryJ =
218 TokBuf + endian::readNext<uint32_t, little, aligned>(NextPPCondPtr);
Mike Stump11289f42009-09-09 15:08:12 +0000219
Ted Kremenek877556f2008-12-12 22:05:38 +0000220 if (HashEntryJ <= LastHashTokPtr) {
221 // Jump directly to the next entry in the side table.
222 HashEntryI = HashEntryJ;
Justin Bogner57ba0b22014-03-28 22:03:24 +0000223 TableIdx = endian::readNext<uint32_t, little, aligned>(NextPPCondPtr);
Ted Kremenek877556f2008-12-12 22:05:38 +0000224 CurPPCondPtr = NextPPCondPtr;
225 }
226 }
Ted Kremenek56572ab2008-12-12 18:34:08 +0000227 }
Mike Stump11289f42009-09-09 15:08:12 +0000228 while (HashEntryI < LastHashTokPtr);
Ted Kremenek877556f2008-12-12 22:05:38 +0000229 assert(HashEntryI == LastHashTokPtr && "No PP-cond entry found for '#'");
Ted Kremenek56572ab2008-12-12 18:34:08 +0000230 assert(TableIdx && "No jumping from #endifs.");
Mike Stump11289f42009-09-09 15:08:12 +0000231
Ted Kremenek56572ab2008-12-12 18:34:08 +0000232 // Update our side-table iterator.
Chris Lattnereb097542009-01-18 01:57:14 +0000233 const unsigned char* NextPPCondPtr = PPCond + TableIdx*(sizeof(uint32_t)*2);
Ted Kremenek56572ab2008-12-12 18:34:08 +0000234 assert(NextPPCondPtr >= CurPPCondPtr);
235 CurPPCondPtr = NextPPCondPtr;
Mike Stump11289f42009-09-09 15:08:12 +0000236
Ted Kremenek56572ab2008-12-12 18:34:08 +0000237 // Read where we should jump to.
Justin Bogner57ba0b22014-03-28 22:03:24 +0000238 HashEntryI =
239 TokBuf + endian::readNext<uint32_t, little, aligned>(NextPPCondPtr);
240 uint32_t NextIdx = endian::readNext<uint32_t, little, aligned>(NextPPCondPtr);
Mike Stump11289f42009-09-09 15:08:12 +0000241
Ted Kremenek56572ab2008-12-12 18:34:08 +0000242 // By construction NextIdx will be zero if this is a #endif. This is useful
243 // to know to obviate lexing another token.
244 bool isEndif = NextIdx == 0;
Mike Stump11289f42009-09-09 15:08:12 +0000245
Ted Kremenek56572ab2008-12-12 18:34:08 +0000246 // This case can occur when we see something like this:
247 //
248 // #if ...
249 // /* a comment or nothing */
250 // #elif
251 //
252 // If we are skipping the first #if block it will be the case that CurPtr
253 // already points 'elif'. Just return.
Mike Stump11289f42009-09-09 15:08:12 +0000254
Ted Kremenek877556f2008-12-12 22:05:38 +0000255 if (CurPtr > HashEntryI) {
Alp Toker25847352014-07-07 14:01:37 +0000256 assert(CurPtr == HashEntryI + StoredTokenSize);
Ted Kremenek56572ab2008-12-12 18:34:08 +0000257 // Did we reach a #endif? If so, go ahead and consume that token as well.
258 if (isEndif)
Alp Toker25847352014-07-07 14:01:37 +0000259 CurPtr += StoredTokenSize * 2;
Ted Kremenek56572ab2008-12-12 18:34:08 +0000260 else
Ted Kremenek877556f2008-12-12 22:05:38 +0000261 LastHashTokPtr = HashEntryI;
Mike Stump11289f42009-09-09 15:08:12 +0000262
Ted Kremenek56572ab2008-12-12 18:34:08 +0000263 return isEndif;
264 }
265
266 // Otherwise, we need to advance. Update CurPtr to point to the '#' token.
Ted Kremenek877556f2008-12-12 22:05:38 +0000267 CurPtr = HashEntryI;
Mike Stump11289f42009-09-09 15:08:12 +0000268
Ted Kremenek56572ab2008-12-12 18:34:08 +0000269 // Update the location of the last observed '#'. This is useful if we
270 // are skipping multiple blocks.
271 LastHashTokPtr = CurPtr;
Ted Kremenek56572ab2008-12-12 18:34:08 +0000272
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000273 // Skip the '#' token.
Chris Lattnereb097542009-01-18 01:57:14 +0000274 assert(((tok::TokenKind)*CurPtr) == tok::hash);
Alp Toker25847352014-07-07 14:01:37 +0000275 CurPtr += StoredTokenSize;
Mike Stump11289f42009-09-09 15:08:12 +0000276
Ted Kremenek56572ab2008-12-12 18:34:08 +0000277 // Did we reach a #endif? If so, go ahead and consume that token as well.
Alp Toker25847352014-07-07 14:01:37 +0000278 if (isEndif) {
279 CurPtr += StoredTokenSize * 2;
280 }
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
Alp Toker25847352014-07-07 14:01:37 +0000293 const unsigned char *OffsetPtr = CurPtr + (StoredTokenSize - 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:
Mehdi Amini004b9c72016-10-10 22:52:47 +0000320 typedef std::pair<unsigned char, StringRef> 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
David Blaikie60e836b2014-08-29 22:04:40 +0000343} // end anonymous namespace
344
345class PTHManager::PTHFileLookupTrait : public PTHFileLookupCommonTrait {
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000346public:
347 typedef const FileEntry* external_key_type;
348 typedef PTHFileData data_type;
Mike Stump11289f42009-09-09 15:08:12 +0000349
Ted Kremenek29942a32009-02-13 19:13:46 +0000350 static internal_key_type GetInternalKey(const FileEntry* FE) {
351 return std::make_pair((unsigned char) 0x1, FE->getName());
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000352 }
Ted Kremenek29942a32009-02-13 19:13:46 +0000353
354 static bool EqualKey(internal_key_type a, internal_key_type b) {
Mehdi Amini004b9c72016-10-10 22:52:47 +0000355 return a.first == b.first && a.second == b.second;
Mike Stump11289f42009-09-09 15:08:12 +0000356 }
357
358 static PTHFileData ReadData(const internal_key_type& k,
359 const unsigned char* d, unsigned) {
Ted Kremenek29942a32009-02-13 19:13:46 +0000360 assert(k.first == 0x1 && "Only file lookups can match!");
Justin Bogner57ba0b22014-03-28 22:03:24 +0000361 using namespace llvm::support;
362 uint32_t x = endian::readNext<uint32_t, little, unaligned>(d);
363 uint32_t y = endian::readNext<uint32_t, little, unaligned>(d);
Mike Stump11289f42009-09-09 15:08:12 +0000364 return PTHFileData(x, y);
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000365 }
366};
Ted Kremeneke5554de2009-02-11 21:29:16 +0000367
David Blaikie60e836b2014-08-29 22:04:40 +0000368class PTHManager::PTHStringLookupTrait {
Ted Kremeneke5554de2009-02-11 21:29:16 +0000369public:
Justin Bogner25463f12014-04-18 20:27:24 +0000370 typedef uint32_t data_type;
371 typedef const std::pair<const char*, unsigned> external_key_type;
Ted Kremeneke5554de2009-02-11 21:29:16 +0000372 typedef external_key_type internal_key_type;
Justin Bogner25463f12014-04-18 20:27:24 +0000373 typedef uint32_t hash_value_type;
374 typedef unsigned offset_type;
Mike Stump11289f42009-09-09 15:08:12 +0000375
Ted Kremeneke5554de2009-02-11 21:29:16 +0000376 static bool EqualKey(const internal_key_type& a,
377 const internal_key_type& b) {
378 return (a.second == b.second) ? memcmp(a.first, b.first, a.second) == 0
379 : false;
380 }
Mike Stump11289f42009-09-09 15:08:12 +0000381
Justin Bogner25463f12014-04-18 20:27:24 +0000382 static hash_value_type ComputeHash(const internal_key_type& a) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000383 return llvm::HashString(StringRef(a.first, a.second));
Ted Kremeneke5554de2009-02-11 21:29:16 +0000384 }
Mike Stump11289f42009-09-09 15:08:12 +0000385
Ted Kremeneke5554de2009-02-11 21:29:16 +0000386 // This hopefully will just get inlined and removed by the optimizer.
387 static const internal_key_type&
388 GetInternalKey(const external_key_type& x) { return x; }
Mike Stump11289f42009-09-09 15:08:12 +0000389
Ted Kremeneke5554de2009-02-11 21:29:16 +0000390 static std::pair<unsigned, unsigned>
391 ReadKeyDataLength(const unsigned char*& d) {
Justin Bogner57ba0b22014-03-28 22:03:24 +0000392 using namespace llvm::support;
393 return std::make_pair(
394 (unsigned)endian::readNext<uint16_t, little, unaligned>(d),
395 sizeof(uint32_t));
Ted Kremeneke5554de2009-02-11 21:29:16 +0000396 }
Mike Stump11289f42009-09-09 15:08:12 +0000397
Ted Kremeneke5554de2009-02-11 21:29:16 +0000398 static std::pair<const char*, unsigned>
399 ReadKey(const unsigned char* d, unsigned n) {
400 assert(n >= 2 && d[n-1] == '\0');
401 return std::make_pair((const char*) d, n-1);
402 }
Mike Stump11289f42009-09-09 15:08:12 +0000403
Ted Kremenek29942a32009-02-13 19:13:46 +0000404 static uint32_t ReadData(const internal_key_type& k, const unsigned char* d,
405 unsigned) {
Justin Bogner57ba0b22014-03-28 22:03:24 +0000406 using namespace llvm::support;
407 return endian::readNext<uint32_t, little, unaligned>(d);
Ted Kremeneke5554de2009-02-11 21:29:16 +0000408 }
409};
Mike Stump11289f42009-09-09 15:08:12 +0000410
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000411//===----------------------------------------------------------------------===//
412// PTHManager methods.
413//===----------------------------------------------------------------------===//
414
David Blaikie02118ce2014-08-29 22:04:45 +0000415PTHManager::PTHManager(
416 std::unique_ptr<const llvm::MemoryBuffer> buf,
417 std::unique_ptr<PTHFileLookup> fileLookup, const unsigned char *idDataTable,
418 std::unique_ptr<IdentifierInfo *[], llvm::FreeDeleter> perIDCache,
419 std::unique_ptr<PTHStringIdLookup> stringIdLookup, unsigned numIds,
420 const unsigned char *spellingBase, const char *originalSourceFile)
421 : Buf(std::move(buf)), PerIDCache(std::move(perIDCache)),
David Blaikie60e836b2014-08-29 22:04:40 +0000422 FileLookup(std::move(fileLookup)), IdDataTable(idDataTable),
423 StringIdLookup(std::move(stringIdLookup)), NumIds(numIds), PP(nullptr),
424 SpellingBase(spellingBase), OriginalSourceFile(originalSourceFile) {}
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000425
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +0000426PTHManager::~PTHManager() {
427}
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000428
David Blaikie9c902b52011-09-25 23:23:43 +0000429static void InvalidPTH(DiagnosticsEngine &Diags, const char *Msg) {
Alp Toker29cb66b2014-01-26 06:17:37 +0000430 Diags.Report(Diags.getCustomDiagID(DiagnosticsEngine::Error, "%0")) << Msg;
Ted Kremenek62224c12009-01-28 21:02:43 +0000431}
432
Rafael Espindolac0f18a92015-06-01 20:00:16 +0000433PTHManager *PTHManager::Create(StringRef file, DiagnosticsEngine &Diags) {
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000434 // Memory map the PTH file.
Rafael Espindola2d2b4202014-07-06 17:43:24 +0000435 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> FileOrErr =
436 llvm::MemoryBuffer::getFile(file);
Mike Stump11289f42009-09-09 15:08:12 +0000437
Rafael Espindola2d2b4202014-07-06 17:43:24 +0000438 if (!FileOrErr) {
Michael J. Spencerf25faaa2010-12-09 17:36:38 +0000439 // FIXME: Add ec.message() to this diag.
Daniel Dunbar1a54e3f2009-11-12 02:53:48 +0000440 Diags.Report(diag::err_invalid_pth_file) << file;
Craig Topperd2d442c2014-05-17 23:10:59 +0000441 return nullptr;
Ted Kremenek3b0589e2009-01-28 20:49:33 +0000442 }
Rafael Espindola2d2b4202014-07-06 17:43:24 +0000443 std::unique_ptr<llvm::MemoryBuffer> File = std::move(FileOrErr.get());
Mike Stump11289f42009-09-09 15:08:12 +0000444
Justin Bogner57ba0b22014-03-28 22:03:24 +0000445 using namespace llvm::support;
446
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000447 // Get the buffer ranges and check if there are at least three 32-bit
448 // words at the end of the file.
Roman Divackye6377112012-09-06 15:59:27 +0000449 const unsigned char *BufBeg = (const unsigned char*)File->getBufferStart();
450 const unsigned char *BufEnd = (const unsigned char*)File->getBufferEnd();
Ted Kremenekeb8c8fb2009-01-26 21:43:14 +0000451
452 // Check the prologue of the file.
Richard Smith2683b4c2012-08-17 03:55:43 +0000453 if ((BufEnd - BufBeg) < (signed)(sizeof("cfe-pth") + 4 + 4) ||
454 memcmp(BufBeg, "cfe-pth", sizeof("cfe-pth")) != 0) {
Daniel Dunbar1a54e3f2009-11-12 02:53:48 +0000455 Diags.Report(diag::err_invalid_pth_file) << file;
Craig Topperd2d442c2014-05-17 23:10:59 +0000456 return nullptr;
Ted Kremenek62224c12009-01-28 21:02:43 +0000457 }
Mike Stump11289f42009-09-09 15:08:12 +0000458
Ted Kremenek978b5be2009-01-26 21:50:21 +0000459 // Read the PTH version.
Richard Smith2683b4c2012-08-17 03:55:43 +0000460 const unsigned char *p = BufBeg + (sizeof("cfe-pth"));
Justin Bogner57ba0b22014-03-28 22:03:24 +0000461 unsigned Version = endian::readNext<uint32_t, little, aligned>(p);
Mike Stump11289f42009-09-09 15:08:12 +0000462
Daniel Dunbar1a54e3f2009-11-12 02:53:48 +0000463 if (Version < PTHManager::Version) {
464 InvalidPTH(Diags,
Mike Stump11289f42009-09-09 15:08:12 +0000465 Version < PTHManager::Version
Ted Kremenek62224c12009-01-28 21:02:43 +0000466 ? "PTH file uses an older PTH format that is no longer supported"
467 : "PTH file uses a newer PTH format that cannot be read");
Craig Topperd2d442c2014-05-17 23:10:59 +0000468 return nullptr;
Ted Kremenek62224c12009-01-28 21:02:43 +0000469 }
Ted Kremenek978b5be2009-01-26 21:50:21 +0000470
Mike Stump11289f42009-09-09 15:08:12 +0000471 // Compute the address of the index table at the end of the PTH file.
Ted Kremenek4c1d41f2009-02-11 23:34:32 +0000472 const unsigned char *PrologueOffset = p;
Mike Stump11289f42009-09-09 15:08:12 +0000473
Ted Kremenek4c1d41f2009-02-11 23:34:32 +0000474 if (PrologueOffset >= BufEnd) {
Daniel Dunbar1a54e3f2009-11-12 02:53:48 +0000475 Diags.Report(diag::err_invalid_pth_file) << file;
Craig Topperd2d442c2014-05-17 23:10:59 +0000476 return nullptr;
Ted Kremenek62224c12009-01-28 21:02:43 +0000477 }
Mike Stump11289f42009-09-09 15:08:12 +0000478
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000479 // Construct the file lookup table. This will be used for mapping from
480 // FileEntry*'s to cached tokens.
Ted Kremenek4c1d41f2009-02-11 23:34:32 +0000481 const unsigned char* FileTableOffset = PrologueOffset + sizeof(uint32_t)*2;
Justin Bogner57ba0b22014-03-28 22:03:24 +0000482 const unsigned char *FileTable =
483 BufBeg + endian::readNext<uint32_t, little, aligned>(FileTableOffset);
Mike Stump11289f42009-09-09 15:08:12 +0000484
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000485 if (!(FileTable > BufBeg && FileTable < BufEnd)) {
Daniel Dunbar1a54e3f2009-11-12 02:53:48 +0000486 Diags.Report(diag::err_invalid_pth_file) << file;
Craig Topperd2d442c2014-05-17 23:10:59 +0000487 return nullptr; // FIXME: Proper error diagnostic?
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000488 }
Mike Stump11289f42009-09-09 15:08:12 +0000489
Ahmed Charlesb8984322014-03-07 20:03:18 +0000490 std::unique_ptr<PTHFileLookup> FL(PTHFileLookup::Create(FileTable, BufBeg));
Mike Stump11289f42009-09-09 15:08:12 +0000491
Ted Kremenek80e239a2009-03-20 17:54:25 +0000492 // Warn if the PTH file is empty. We still want to create a PTHManager
493 // as the PTH could be used with -include-pth.
494 if (FL->isEmpty())
Daniel Dunbar1a54e3f2009-11-12 02:53:48 +0000495 InvalidPTH(Diags, "PTH file contains no cached source data");
Mike Stump11289f42009-09-09 15:08:12 +0000496
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000497 // Get the location of the table mapping from persistent ids to the
498 // data needed to reconstruct identifiers.
Ted Kremenek4c1d41f2009-02-11 23:34:32 +0000499 const unsigned char* IDTableOffset = PrologueOffset + sizeof(uint32_t)*0;
Justin Bogner57ba0b22014-03-28 22:03:24 +0000500 const unsigned char *IData =
501 BufBeg + endian::readNext<uint32_t, little, aligned>(IDTableOffset);
Mike Stump11289f42009-09-09 15:08:12 +0000502
Ted Kremenek8d6c8282009-01-21 07:34:28 +0000503 if (!(IData >= BufBeg && IData < BufEnd)) {
Daniel Dunbar1a54e3f2009-11-12 02:53:48 +0000504 Diags.Report(diag::err_invalid_pth_file) << file;
Craig Topperd2d442c2014-05-17 23:10:59 +0000505 return nullptr;
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000506 }
Mike Stump11289f42009-09-09 15:08:12 +0000507
Ted Kremeneke5554de2009-02-11 21:29:16 +0000508 // Get the location of the hashtable mapping between strings and
509 // persistent IDs.
Ted Kremenek4c1d41f2009-02-11 23:34:32 +0000510 const unsigned char* StringIdTableOffset = PrologueOffset + sizeof(uint32_t)*1;
Justin Bogner57ba0b22014-03-28 22:03:24 +0000511 const unsigned char *StringIdTable =
512 BufBeg + endian::readNext<uint32_t, little, aligned>(StringIdTableOffset);
Ted Kremeneke5554de2009-02-11 21:29:16 +0000513 if (!(StringIdTable >= BufBeg && StringIdTable < BufEnd)) {
Daniel Dunbar1a54e3f2009-11-12 02:53:48 +0000514 Diags.Report(diag::err_invalid_pth_file) << file;
Craig Topperd2d442c2014-05-17 23:10:59 +0000515 return nullptr;
Ted Kremeneka705b042009-01-15 18:47:46 +0000516 }
Ted Kremeneke5554de2009-02-11 21:29:16 +0000517
Ahmed Charlesb8984322014-03-07 20:03:18 +0000518 std::unique_ptr<PTHStringIdLookup> SL(
519 PTHStringIdLookup::Create(StringIdTable, BufBeg));
Mike Stump11289f42009-09-09 15:08:12 +0000520
Ted Kremenek8d178f42009-01-27 00:01:05 +0000521 // Get the location of the spelling cache.
Ted Kremenek4c1d41f2009-02-11 23:34:32 +0000522 const unsigned char* spellingBaseOffset = PrologueOffset + sizeof(uint32_t)*3;
Justin Bogner57ba0b22014-03-28 22:03:24 +0000523 const unsigned char *spellingBase =
524 BufBeg + endian::readNext<uint32_t, little, aligned>(spellingBaseOffset);
Ted Kremenek8d178f42009-01-27 00:01:05 +0000525 if (!(spellingBase >= BufBeg && spellingBase < BufEnd)) {
Daniel Dunbar1a54e3f2009-11-12 02:53:48 +0000526 Diags.Report(diag::err_invalid_pth_file) << file;
Craig Topperd2d442c2014-05-17 23:10:59 +0000527 return nullptr;
Ted Kremenek8d178f42009-01-27 00:01:05 +0000528 }
Mike Stump11289f42009-09-09 15:08:12 +0000529
Ted Kremenek73a4d282008-12-03 01:16:39 +0000530 // Get the number of IdentifierInfos and pre-allocate the identifier cache.
Justin Bogner57ba0b22014-03-28 22:03:24 +0000531 uint32_t NumIds = endian::readNext<uint32_t, little, aligned>(IData);
Mike Stump11289f42009-09-09 15:08:12 +0000532
Chris Lattner57540c52011-04-15 05:22:18 +0000533 // Pre-allocate the persistent ID -> IdentifierInfo* cache. We use calloc()
Ted Kremenek73a4d282008-12-03 01:16:39 +0000534 // so that we in the best case only zero out memory once when the OS returns
535 // us new pages.
David Blaikie02118ce2014-08-29 22:04:45 +0000536 std::unique_ptr<IdentifierInfo *[], llvm::FreeDeleter> PerIDCache;
Mike Stump11289f42009-09-09 15:08:12 +0000537
Ted Kremenek8d6c8282009-01-21 07:34:28 +0000538 if (NumIds) {
David Blaikie02118ce2014-08-29 22:04:45 +0000539 PerIDCache.reset((IdentifierInfo **)calloc(NumIds, sizeof(PerIDCache[0])));
Ted Kremenek8d6c8282009-01-21 07:34:28 +0000540 if (!PerIDCache) {
Daniel Dunbar1a54e3f2009-11-12 02:53:48 +0000541 InvalidPTH(Diags, "Could not allocate memory for processing PTH file");
Craig Topperd2d442c2014-05-17 23:10:59 +0000542 return nullptr;
Ted Kremenek8d6c8282009-01-21 07:34:28 +0000543 }
Ted Kremenek73a4d282008-12-03 01:16:39 +0000544 }
Ted Kremenek8d6c8282009-01-21 07:34:28 +0000545
Ted Kremenekf9ccd5c2009-03-19 22:19:30 +0000546 // Compute the address of the original source file.
547 const unsigned char* originalSourceBase = PrologueOffset + sizeof(uint32_t)*4;
Justin Bogner57ba0b22014-03-28 22:03:24 +0000548 unsigned len =
549 endian::readNext<uint16_t, little, unaligned>(originalSourceBase);
Craig Topperd2d442c2014-05-17 23:10:59 +0000550 if (!len) originalSourceBase = nullptr;
Mike Stump11289f42009-09-09 15:08:12 +0000551
Ted Kremeneka705b042009-01-15 18:47:46 +0000552 // Create the new PTHManager.
David Blaikie02118ce2014-08-29 22:04:45 +0000553 return new PTHManager(std::move(File), std::move(FL), IData,
554 std::move(PerIDCache), std::move(SL), NumIds,
555 spellingBase, (const char *)originalSourceBase);
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000556}
Ted Kremenekf9ccd5c2009-03-19 22:19:30 +0000557
Chris Lattner144aacd2009-01-18 02:57:21 +0000558IdentifierInfo* PTHManager::LazilyCreateIdentifierInfo(unsigned PersistentID) {
Justin Bogner57ba0b22014-03-28 22:03:24 +0000559 using namespace llvm::support;
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000560 // Look in the PTH file for the string data for the IdentifierInfo object.
Chris Lattner144aacd2009-01-18 02:57:21 +0000561 const unsigned char* TableEntry = IdDataTable + sizeof(uint32_t)*PersistentID;
Justin Bogner57ba0b22014-03-28 22:03:24 +0000562 const unsigned char *IDData =
563 (const unsigned char *)Buf->getBufferStart() +
564 endian::readNext<uint32_t, little, aligned>(TableEntry);
Chris Lattnereb097542009-01-18 01:57:14 +0000565 assert(IDData < (const unsigned char*)Buf->getBufferEnd());
Mike Stump11289f42009-09-09 15:08:12 +0000566
Ted Kremeneka705b042009-01-15 18:47:46 +0000567 // Allocate the object.
Chris Lattnereb097542009-01-18 01:57:14 +0000568 std::pair<IdentifierInfo,const unsigned char*> *Mem =
569 Alloc.Allocate<std::pair<IdentifierInfo,const unsigned char*> >();
Ted Kremeneka705b042009-01-15 18:47:46 +0000570
571 Mem->second = IDData;
Ted Kremeneke5554de2009-02-11 21:29:16 +0000572 assert(IDData[0] != '\0');
Ted Kremenek52f73ca2009-01-20 23:28:34 +0000573 IdentifierInfo *II = new ((void*) Mem) IdentifierInfo();
Mike Stump11289f42009-09-09 15:08:12 +0000574
Ted Kremeneka705b042009-01-15 18:47:46 +0000575 // Store the new IdentifierInfo in the cache.
Chris Lattner144aacd2009-01-18 02:57:21 +0000576 PerIDCache[PersistentID] = II;
Daniel Dunbar2c422dc92009-10-18 20:26:12 +0000577 assert(II->getNameStart() && II->getNameStart()[0] != '\0');
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000578 return II;
579}
580
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000581IdentifierInfo* PTHManager::get(StringRef Name) {
Ted Kremeneke5554de2009-02-11 21:29:16 +0000582 // Double check our assumption that the last character isn't '\0'.
David Blaikie441417a2011-09-22 01:35:49 +0000583 assert(Name.empty() || Name.back() != '\0');
David Blaikie60e836b2014-08-29 22:04:40 +0000584 PTHStringIdLookup::iterator I =
585 StringIdLookup->find(std::make_pair(Name.data(), Name.size()));
586 if (I == StringIdLookup->end()) // No identifier found?
Craig Topperd2d442c2014-05-17 23:10:59 +0000587 return nullptr;
Ted Kremeneka705b042009-01-15 18:47:46 +0000588
Ted Kremeneke5554de2009-02-11 21:29:16 +0000589 // Match found. Return the identifier!
590 assert(*I > 0);
591 return GetIdentifierInfo(*I-1);
592}
Ted Kremeneka705b042009-01-15 18:47:46 +0000593
Chris Lattnerab1d4b82009-01-17 08:06:50 +0000594PTHLexer *PTHManager::CreateLexer(FileID FID) {
595 const FileEntry *FE = PP->getSourceManager().getFileEntryForID(FID);
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000596 if (!FE)
Craig Topperd2d442c2014-05-17 23:10:59 +0000597 return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +0000598
Justin Bogner57ba0b22014-03-28 22:03:24 +0000599 using namespace llvm::support;
600
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000601 // Lookup the FileEntry object in our file lookup data structure. It will
602 // return a variant that indicates whether or not there is an offset within
603 // the PTH file that contains cached tokens.
David Blaikie60e836b2014-08-29 22:04:40 +0000604 PTHFileLookup::iterator I = FileLookup->find(FE);
Mike Stump11289f42009-09-09 15:08:12 +0000605
David Blaikie60e836b2014-08-29 22:04:40 +0000606 if (I == FileLookup->end()) // No tokens available?
Craig Topperd2d442c2014-05-17 23:10:59 +0000607 return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +0000608
609 const PTHFileData& FileData = *I;
610
Chris Lattnereb097542009-01-18 01:57:14 +0000611 const unsigned char *BufStart = (const unsigned char *)Buf->getBufferStart();
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000612 // Compute the offset of the token data within the buffer.
Chris Lattnereb097542009-01-18 01:57:14 +0000613 const unsigned char* data = BufStart + FileData.getTokenOffset();
Ted Kremenek56572ab2008-12-12 18:34:08 +0000614
615 // Get the location of pp-conditional table.
Chris Lattnereb097542009-01-18 01:57:14 +0000616 const unsigned char* ppcond = BufStart + FileData.getPPCondOffset();
Justin Bogner57ba0b22014-03-28 22:03:24 +0000617 uint32_t Len = endian::readNext<uint32_t, little, aligned>(ppcond);
Craig Topperd2d442c2014-05-17 23:10:59 +0000618 if (Len == 0) ppcond = nullptr;
Mike Stump11289f42009-09-09 15:08:12 +0000619
Ted Kremeneka705b042009-01-15 18:47:46 +0000620 assert(PP && "No preprocessor set yet!");
Mike Stump11289f42009-09-09 15:08:12 +0000621 return new PTHLexer(*PP, FID, data, ppcond, *this);
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000622}
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000623
624//===----------------------------------------------------------------------===//
625// 'stat' caching.
626//===----------------------------------------------------------------------===//
627
628namespace {
Benjamin Kramer337e3a52009-11-28 19:45:26 +0000629class PTHStatData {
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000630public:
Rafael Espindolaf8f91b82013-08-01 21:42:11 +0000631 uint64_t Size;
632 time_t ModTime;
633 llvm::sys::fs::UniqueID UniqueID;
Mehdi Amini2862ba62016-08-24 04:26:57 +0000634 const bool HasData;
Rafael Espindolaf8f91b82013-08-01 21:42:11 +0000635 bool IsDirectory;
Mike Stump11289f42009-09-09 15:08:12 +0000636
Rafael Espindolaf8f91b82013-08-01 21:42:11 +0000637 PTHStatData(uint64_t Size, time_t ModTime, llvm::sys::fs::UniqueID UniqueID,
638 bool IsDirectory)
Mehdi Amini2862ba62016-08-24 04:26:57 +0000639 : Size(Size), ModTime(ModTime), UniqueID(UniqueID), HasData(true),
Rafael Espindolaf8f91b82013-08-01 21:42:11 +0000640 IsDirectory(IsDirectory) {}
Mike Stump11289f42009-09-09 15:08:12 +0000641
Rafael Espindolaf8f91b82013-08-01 21:42:11 +0000642 PTHStatData() : HasData(false) {}
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000643};
Mike Stump11289f42009-09-09 15:08:12 +0000644
Benjamin Kramer337e3a52009-11-28 19:45:26 +0000645class PTHStatLookupTrait : public PTHFileLookupCommonTrait {
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000646public:
Mehdi Amini0df59d82016-10-11 07:31:29 +0000647 typedef StringRef external_key_type; // const char*
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000648 typedef PTHStatData data_type;
Mike Stump11289f42009-09-09 15:08:12 +0000649
Mehdi Amini0df59d82016-10-11 07:31:29 +0000650 static internal_key_type GetInternalKey(StringRef path) {
Ted Kremenek29942a32009-02-13 19:13:46 +0000651 // The key 'kind' doesn't matter here because it is ignored in EqualKey.
652 return std::make_pair((unsigned char) 0x0, path);
653 }
654
655 static bool EqualKey(internal_key_type a, internal_key_type b) {
656 // When doing 'stat' lookups we don't care about the kind of 'a' and 'b',
657 // just the paths.
Mehdi Amini004b9c72016-10-10 22:52:47 +0000658 return a.second == b.second;
Mike Stump11289f42009-09-09 15:08:12 +0000659 }
660
Ted Kremenek29942a32009-02-13 19:13:46 +0000661 static data_type ReadData(const internal_key_type& k, const unsigned char* d,
Mike Stump11289f42009-09-09 15:08:12 +0000662 unsigned) {
663
Ted Kremenek2fd18ec2009-02-13 22:07:44 +0000664 if (k.first /* File or Directory */) {
Rafael Espindolaf8f91b82013-08-01 21:42:11 +0000665 bool IsDirectory = true;
666 if (k.first == 0x1 /* File */) {
667 IsDirectory = false;
668 d += 4 * 2; // Skip the first 2 words.
669 }
670
Justin Bogner57ba0b22014-03-28 22:03:24 +0000671 using namespace llvm::support;
672
673 uint64_t File = endian::readNext<uint64_t, little, unaligned>(d);
674 uint64_t Device = endian::readNext<uint64_t, little, unaligned>(d);
Sylvestre Ledru0ae40742014-08-18 15:00:11 +0000675 llvm::sys::fs::UniqueID UniqueID(Device, File);
Justin Bogner57ba0b22014-03-28 22:03:24 +0000676 time_t ModTime = endian::readNext<uint64_t, little, unaligned>(d);
677 uint64_t Size = endian::readNext<uint64_t, little, unaligned>(d);
Rafael Espindolaf8f91b82013-08-01 21:42:11 +0000678 return data_type(Size, ModTime, UniqueID, IsDirectory);
Ted Kremenek29942a32009-02-13 19:13:46 +0000679 }
Ted Kremenek2fd18ec2009-02-13 22:07:44 +0000680
681 // Negative stat. Don't read anything.
Ted Kremenek29942a32009-02-13 19:13:46 +0000682 return data_type();
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000683 }
684};
David Blaikie60e836b2014-08-29 22:04:40 +0000685} // end anonymous namespace
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000686
David Blaikie60e836b2014-08-29 22:04:40 +0000687namespace clang {
Chris Lattner226efd32010-11-23 19:19:34 +0000688class PTHStatCache : public FileSystemStatCache {
Justin Bognerbb094f02014-04-18 19:57:06 +0000689 typedef llvm::OnDiskChainedHashTable<PTHStatLookupTrait> CacheTy;
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000690 CacheTy Cache;
691
Mike Stump11289f42009-09-09 15:08:12 +0000692public:
David Blaikie60e836b2014-08-29 22:04:40 +0000693 PTHStatCache(PTHManager::PTHFileLookup &FL)
694 : Cache(FL.getNumBuckets(), FL.getNumEntries(), FL.getBuckets(),
695 FL.getBase()) {}
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000696
Mehdi Amini0df59d82016-10-11 07:31:29 +0000697 LookupResult getStat(StringRef Path, FileData &Data, bool isFile,
David Blaikie326ffb32014-07-08 15:46:02 +0000698 std::unique_ptr<vfs::File> *F,
699 vfs::FileSystem &FS) override {
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000700 // Do the lookup for the file's data in the PTH file.
Chris Lattner226efd32010-11-23 19:19:34 +0000701 CacheTy::iterator I = Cache.find(Path);
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000702
703 // If we don't get a hit in the PTH file just forward to 'stat'.
Kovarththanan Rajaratnam661a3092010-03-12 08:23:34 +0000704 if (I == Cache.end())
Ben Langmuirc8130a72014-02-20 21:59:23 +0000705 return statChained(Path, Data, isFile, F, FS);
Mike Stump11289f42009-09-09 15:08:12 +0000706
Rafael Espindolaf8f91b82013-08-01 21:42:11 +0000707 const PTHStatData &D = *I;
Mike Stump11289f42009-09-09 15:08:12 +0000708
Rafael Espindolaf8f91b82013-08-01 21:42:11 +0000709 if (!D.HasData)
Chris Lattner8f0583d2010-11-23 20:05:15 +0000710 return CacheMissing;
Ted Kremenek2fd18ec2009-02-13 22:07:44 +0000711
Ben Langmuird066d4c2014-02-28 21:16:07 +0000712 Data.Name = Path;
Rafael Espindolaf8f91b82013-08-01 21:42:11 +0000713 Data.Size = D.Size;
714 Data.ModTime = D.ModTime;
715 Data.UniqueID = D.UniqueID;
716 Data.IsDirectory = D.IsDirectory;
717 Data.IsNamedPipe = false;
718 Data.InPCH = true;
719
Chris Lattner8f0583d2010-11-23 20:05:15 +0000720 return CacheExists;
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000721 }
722};
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000723}
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000724
David Blaikie23430cc2014-08-11 21:29:24 +0000725std::unique_ptr<FileSystemStatCache> PTHManager::createStatCache() {
David Blaikie60e836b2014-08-29 22:04:40 +0000726 return llvm::make_unique<PTHStatCache>(*FileLookup);
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000727}