blob: af7a15384e3560d854d3460ecaff3110ed8bc015 [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"
Ahmed Charlesdfca6f92014-03-09 11:36:40 +000027#include <memory>
Rafael Espindola8a8e5542014-06-12 17:19:42 +000028#include <system_error>
Ted Kremenek7cd62452008-11-12 21:37:15 +000029using namespace clang;
30
Alp Toker25847352014-07-07 14:01:37 +000031static const unsigned StoredTokenSize = 1 + 1 + 2 + 4 + 4;
Ted Kremenek56572ab2008-12-12 18:34:08 +000032
Ted Kremenek33eeabd2008-12-03 00:38:03 +000033//===----------------------------------------------------------------------===//
Ted Kremenek1b18ad22008-12-23 01:30:52 +000034// PTHLexer methods.
35//===----------------------------------------------------------------------===//
36
Chris Lattnereb097542009-01-18 01:57:14 +000037PTHLexer::PTHLexer(Preprocessor &PP, FileID FID, const unsigned char *D,
Ted Kremenek8d178f42009-01-27 00:01:05 +000038 const unsigned char *ppcond, PTHManager &PM)
Craig Topperd2d442c2014-05-17 23:10:59 +000039 : PreprocessorLexer(&PP, FID), TokBuf(D), CurPtr(D), LastHashTokPtr(nullptr),
Ted Kremenek8d178f42009-01-27 00:01:05 +000040 PPCond(ppcond), CurPPCondPtr(ppcond), PTHMgr(PM) {
Mike Stump11289f42009-09-09 15:08:12 +000041
Chris Lattnerd32480d2009-01-17 06:22:33 +000042 FileStartLoc = PP.getSourceManager().getLocForStartOfFile(FID);
Ted Kremenek47b8cf62009-01-09 22:05:30 +000043}
Ted Kremenek1b18ad22008-12-23 01:30:52 +000044
Eli Friedman0834a4b2013-09-19 00:41:32 +000045bool PTHLexer::Lex(Token& Tok) {
Ted Kremenek66076a92008-12-23 02:30:15 +000046 //===--------------------------------------==//
47 // Read the raw token data.
48 //===--------------------------------------==//
Justin Bogner57ba0b22014-03-28 22:03:24 +000049 using namespace llvm::support;
Mike Stump11289f42009-09-09 15:08:12 +000050
Ted Kremenek66076a92008-12-23 02:30:15 +000051 // Shadow CurPtr into an automatic variable.
Mike Stump11289f42009-09-09 15:08:12 +000052 const unsigned char *CurPtrShadow = CurPtr;
Ted Kremenek66076a92008-12-23 02:30:15 +000053
Chris Lattner137d6492009-01-18 02:10:31 +000054 // Read in the data for the token.
Justin Bogner57ba0b22014-03-28 22:03:24 +000055 unsigned Word0 = endian::readNext<uint32_t, little, aligned>(CurPtrShadow);
56 uint32_t IdentifierID =
57 endian::readNext<uint32_t, little, aligned>(CurPtrShadow);
58 uint32_t FileOffset =
59 endian::readNext<uint32_t, little, aligned>(CurPtrShadow);
Mike Stump11289f42009-09-09 15:08:12 +000060
Ted Kremenek8433f0b2009-01-19 23:13:15 +000061 tok::TokenKind TKind = (tok::TokenKind) (Word0 & 0xFF);
62 Token::TokenFlags TFlags = (Token::TokenFlags) ((Word0 >> 8) & 0xFF);
Chris Lattner47def972009-01-21 07:21:56 +000063 uint32_t Len = Word0 >> 16;
Ted Kremenek8433f0b2009-01-19 23:13:15 +000064
Chris Lattner47def972009-01-21 07:21:56 +000065 CurPtr = CurPtrShadow;
Mike Stump11289f42009-09-09 15:08:12 +000066
Ted Kremenek66076a92008-12-23 02:30:15 +000067 //===--------------------------------------==//
68 // Construct the token itself.
69 //===--------------------------------------==//
Mike Stump11289f42009-09-09 15:08:12 +000070
Ted Kremenek66076a92008-12-23 02:30:15 +000071 Tok.startToken();
Chris Lattner18fc6ce2009-01-18 02:34:01 +000072 Tok.setKind(TKind);
73 Tok.setFlag(TFlags);
Ted Kremenek78cc2472008-12-23 19:24:24 +000074 assert(!LexingRawMode);
Argyrios Kyrtzidise6e67de2011-09-19 20:40:19 +000075 Tok.setLocation(FileStartLoc.getLocWithOffset(FileOffset));
Ted Kremenek66076a92008-12-23 02:30:15 +000076 Tok.setLength(Len);
77
Chris Lattner3029b352009-01-21 07:50:06 +000078 // Handle identifiers.
Ted Kremenek8d178f42009-01-27 00:01:05 +000079 if (Tok.isLiteral()) {
80 Tok.setLiteralData((const char*) (PTHMgr.SpellingBase + IdentifierID));
81 }
82 else if (IdentifierID) {
Chris Lattner3029b352009-01-21 07:50:06 +000083 MIOpt.ReadToken();
84 IdentifierInfo *II = PTHMgr.GetIdentifierInfo(IdentifierID-1);
Mike Stump11289f42009-09-09 15:08:12 +000085
Chris Lattner3029b352009-01-21 07:50:06 +000086 Tok.setIdentifierInfo(II);
Mike Stump11289f42009-09-09 15:08:12 +000087
Chris Lattner1f6c7fe2009-01-23 18:35:48 +000088 // Change the kind of this identifier to the appropriate token kind, e.g.
89 // turning "for" into a keyword.
90 Tok.setKind(II->getTokenID());
Mike Stump11289f42009-09-09 15:08:12 +000091
Chris Lattner3029b352009-01-21 07:50:06 +000092 if (II->isHandleIdentifierCase())
Eli Friedman0834a4b2013-09-19 00:41:32 +000093 return PP->HandleIdentifier(Tok);
94
95 return true;
Chris Lattner3029b352009-01-21 07:50:06 +000096 }
Mike Stump11289f42009-09-09 15:08:12 +000097
Ted Kremenek66076a92008-12-23 02:30:15 +000098 //===--------------------------------------==//
99 // Process the token.
100 //===--------------------------------------==//
Chris Lattner18fc6ce2009-01-18 02:34:01 +0000101 if (TKind == tok::eof) {
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000102 // Save the end-of-file token.
103 EofToken = Tok;
Mike Stump11289f42009-09-09 15:08:12 +0000104
Ted Kremenek78cc2472008-12-23 19:24:24 +0000105 assert(!ParsingPreprocessorDirective);
106 assert(!LexingRawMode);
Mike Stump11289f42009-09-09 15:08:12 +0000107
Eli Friedman0834a4b2013-09-19 00:41:32 +0000108 return LexEndOfFile(Tok);
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000109 }
Mike Stump11289f42009-09-09 15:08:12 +0000110
Chris Lattner18fc6ce2009-01-18 02:34:01 +0000111 if (TKind == tok::hash && Tok.isAtStartOfLine()) {
Alp Toker25847352014-07-07 14:01:37 +0000112 LastHashTokPtr = CurPtr - StoredTokenSize;
Ted Kremenek78cc2472008-12-23 19:24:24 +0000113 assert(!LexingRawMode);
114 PP->HandleDirective(Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000115
Eli Friedman0834a4b2013-09-19 00:41:32 +0000116 return false;
Ted Kremenek78cc2472008-12-23 19:24:24 +0000117 }
Mike Stump11289f42009-09-09 15:08:12 +0000118
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000119 if (TKind == tok::eod) {
Ted Kremenek78cc2472008-12-23 19:24:24 +0000120 assert(ParsingPreprocessorDirective);
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000121 ParsingPreprocessorDirective = false;
Eli Friedman0834a4b2013-09-19 00:41:32 +0000122 return true;
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000123 }
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000124
Ted Kremenek78cc2472008-12-23 19:24:24 +0000125 MIOpt.ReadToken();
Eli Friedman0834a4b2013-09-19 00:41:32 +0000126 return true;
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000127}
128
Ted Kremenek2bd41d12010-07-27 02:59:02 +0000129bool PTHLexer::LexEndOfFile(Token &Result) {
130 // If we hit the end of the file while parsing a preprocessor directive,
131 // end the preprocessor directive first. The next token returned will
132 // then be the end of file.
133 if (ParsingPreprocessorDirective) {
134 ParsingPreprocessorDirective = false; // Done parsing the "line".
135 return true; // Have a token.
136 }
137
138 assert(!LexingRawMode);
139
140 // If we are in a #if directive, emit an error.
141 while (!ConditionalStack.empty()) {
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000142 if (PP->getCodeCompletionFileLoc() != FileStartLoc)
Douglas Gregor02690ba2010-08-12 17:04:55 +0000143 PP->Diag(ConditionalStack.back().IfLoc,
144 diag::err_pp_unterminated_conditional);
Ted Kremenek2bd41d12010-07-27 02:59:02 +0000145 ConditionalStack.pop_back();
146 }
147
148 // Finally, let the preprocessor handle this.
149 return PP->HandleEndOfFile(Result);
150}
151
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000152// FIXME: We can just grab the last token instead of storing a copy
153// into EofToken.
Ted Kremenek78cc2472008-12-23 19:24:24 +0000154void PTHLexer::getEOF(Token& Tok) {
Ted Kremenek8ae06622009-01-09 00:36:11 +0000155 assert(EofToken.is(tok::eof));
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000156 Tok = EofToken;
157}
158
159void PTHLexer::DiscardToEndOfLine() {
160 assert(ParsingPreprocessorDirective && ParsingFilename == false &&
161 "Must be in a preprocessing directive!");
162
163 // We assume that if the preprocessor wishes to discard to the end of
164 // the line that it also means to end the current preprocessor directive.
165 ParsingPreprocessorDirective = false;
Mike Stump11289f42009-09-09 15:08:12 +0000166
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000167 // Skip tokens by only peeking at their token kind and the flags.
168 // We don't need to actually reconstruct full tokens from the token buffer.
169 // This saves some copies and it also reduces IdentifierInfo* lookup.
Chris Lattnereb097542009-01-18 01:57:14 +0000170 const unsigned char* p = CurPtr;
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000171 while (1) {
172 // Read the token kind. Are we at the end of the file?
173 tok::TokenKind x = (tok::TokenKind) (uint8_t) *p;
174 if (x == tok::eof) break;
Mike Stump11289f42009-09-09 15:08:12 +0000175
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000176 // Read the token flags. Are we at the start of the next line?
177 Token::TokenFlags y = (Token::TokenFlags) (uint8_t) p[1];
178 if (y & Token::StartOfLine) break;
179
180 // Skip to the next token.
Alp Toker25847352014-07-07 14:01:37 +0000181 p += StoredTokenSize;
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000182 }
Mike Stump11289f42009-09-09 15:08:12 +0000183
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000184 CurPtr = p;
185}
186
Ted Kremenek56572ab2008-12-12 18:34:08 +0000187/// SkipBlock - Used by Preprocessor to skip the current conditional block.
188bool PTHLexer::SkipBlock() {
Justin Bogner57ba0b22014-03-28 22:03:24 +0000189 using namespace llvm::support;
Ted Kremenek56572ab2008-12-12 18:34:08 +0000190 assert(CurPPCondPtr && "No cached PP conditional information.");
191 assert(LastHashTokPtr && "No known '#' token.");
Mike Stump11289f42009-09-09 15:08:12 +0000192
Craig Topperd2d442c2014-05-17 23:10:59 +0000193 const unsigned char *HashEntryI = nullptr;
Ted Kremenek56572ab2008-12-12 18:34:08 +0000194 uint32_t TableIdx;
Mike Stump11289f42009-09-09 15:08:12 +0000195
Ted Kremenek56572ab2008-12-12 18:34:08 +0000196 do {
Ted Kremenek877556f2008-12-12 22:05:38 +0000197 // Read the token offset from the side-table.
Justin Bogner57ba0b22014-03-28 22:03:24 +0000198 uint32_t Offset = endian::readNext<uint32_t, little, aligned>(CurPPCondPtr);
Mike Stump11289f42009-09-09 15:08:12 +0000199
200 // Read the target table index from the side-table.
Justin Bogner57ba0b22014-03-28 22:03:24 +0000201 TableIdx = endian::readNext<uint32_t, little, aligned>(CurPPCondPtr);
Mike Stump11289f42009-09-09 15:08:12 +0000202
Ted Kremenek877556f2008-12-12 22:05:38 +0000203 // Compute the actual memory address of the '#' token data for this entry.
204 HashEntryI = TokBuf + Offset;
205
206 // Optmization: "Sibling jumping". #if...#else...#endif blocks can
207 // contain nested blocks. In the side-table we can jump over these
208 // nested blocks instead of doing a linear search if the next "sibling"
209 // entry is not at a location greater than LastHashTokPtr.
210 if (HashEntryI < LastHashTokPtr && TableIdx) {
211 // In the side-table we are still at an entry for a '#' token that
212 // is earlier than the last one we saw. Check if the location we would
213 // stride gets us closer.
Chris Lattnereb097542009-01-18 01:57:14 +0000214 const unsigned char* NextPPCondPtr =
215 PPCond + TableIdx*(sizeof(uint32_t)*2);
Ted Kremenek877556f2008-12-12 22:05:38 +0000216 assert(NextPPCondPtr >= CurPPCondPtr);
217 // Read where we should jump to.
Justin Bogner57ba0b22014-03-28 22:03:24 +0000218 const unsigned char *HashEntryJ =
219 TokBuf + endian::readNext<uint32_t, little, aligned>(NextPPCondPtr);
Mike Stump11289f42009-09-09 15:08:12 +0000220
Ted Kremenek877556f2008-12-12 22:05:38 +0000221 if (HashEntryJ <= LastHashTokPtr) {
222 // Jump directly to the next entry in the side table.
223 HashEntryI = HashEntryJ;
Justin Bogner57ba0b22014-03-28 22:03:24 +0000224 TableIdx = endian::readNext<uint32_t, little, aligned>(NextPPCondPtr);
Ted Kremenek877556f2008-12-12 22:05:38 +0000225 CurPPCondPtr = NextPPCondPtr;
226 }
227 }
Ted Kremenek56572ab2008-12-12 18:34:08 +0000228 }
Mike Stump11289f42009-09-09 15:08:12 +0000229 while (HashEntryI < LastHashTokPtr);
Ted Kremenek877556f2008-12-12 22:05:38 +0000230 assert(HashEntryI == LastHashTokPtr && "No PP-cond entry found for '#'");
Ted Kremenek56572ab2008-12-12 18:34:08 +0000231 assert(TableIdx && "No jumping from #endifs.");
Mike Stump11289f42009-09-09 15:08:12 +0000232
Ted Kremenek56572ab2008-12-12 18:34:08 +0000233 // Update our side-table iterator.
Chris Lattnereb097542009-01-18 01:57:14 +0000234 const unsigned char* NextPPCondPtr = PPCond + TableIdx*(sizeof(uint32_t)*2);
Ted Kremenek56572ab2008-12-12 18:34:08 +0000235 assert(NextPPCondPtr >= CurPPCondPtr);
236 CurPPCondPtr = NextPPCondPtr;
Mike Stump11289f42009-09-09 15:08:12 +0000237
Ted Kremenek56572ab2008-12-12 18:34:08 +0000238 // Read where we should jump to.
Justin Bogner57ba0b22014-03-28 22:03:24 +0000239 HashEntryI =
240 TokBuf + endian::readNext<uint32_t, little, aligned>(NextPPCondPtr);
241 uint32_t NextIdx = endian::readNext<uint32_t, little, aligned>(NextPPCondPtr);
Mike Stump11289f42009-09-09 15:08:12 +0000242
Ted Kremenek56572ab2008-12-12 18:34:08 +0000243 // By construction NextIdx will be zero if this is a #endif. This is useful
244 // to know to obviate lexing another token.
245 bool isEndif = NextIdx == 0;
Mike Stump11289f42009-09-09 15:08:12 +0000246
Ted Kremenek56572ab2008-12-12 18:34:08 +0000247 // This case can occur when we see something like this:
248 //
249 // #if ...
250 // /* a comment or nothing */
251 // #elif
252 //
253 // If we are skipping the first #if block it will be the case that CurPtr
254 // already points 'elif'. Just return.
Mike Stump11289f42009-09-09 15:08:12 +0000255
Ted Kremenek877556f2008-12-12 22:05:38 +0000256 if (CurPtr > HashEntryI) {
Alp Toker25847352014-07-07 14:01:37 +0000257 assert(CurPtr == HashEntryI + StoredTokenSize);
Ted Kremenek56572ab2008-12-12 18:34:08 +0000258 // Did we reach a #endif? If so, go ahead and consume that token as well.
259 if (isEndif)
Alp Toker25847352014-07-07 14:01:37 +0000260 CurPtr += StoredTokenSize * 2;
Ted Kremenek56572ab2008-12-12 18:34:08 +0000261 else
Ted Kremenek877556f2008-12-12 22:05:38 +0000262 LastHashTokPtr = HashEntryI;
Mike Stump11289f42009-09-09 15:08:12 +0000263
Ted Kremenek56572ab2008-12-12 18:34:08 +0000264 return isEndif;
265 }
266
267 // Otherwise, we need to advance. Update CurPtr to point to the '#' token.
Ted Kremenek877556f2008-12-12 22:05:38 +0000268 CurPtr = HashEntryI;
Mike Stump11289f42009-09-09 15:08:12 +0000269
Ted Kremenek56572ab2008-12-12 18:34:08 +0000270 // Update the location of the last observed '#'. This is useful if we
271 // are skipping multiple blocks.
272 LastHashTokPtr = CurPtr;
Ted Kremenek56572ab2008-12-12 18:34:08 +0000273
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000274 // Skip the '#' token.
Chris Lattnereb097542009-01-18 01:57:14 +0000275 assert(((tok::TokenKind)*CurPtr) == tok::hash);
Alp Toker25847352014-07-07 14:01:37 +0000276 CurPtr += StoredTokenSize;
Mike Stump11289f42009-09-09 15:08:12 +0000277
Ted Kremenek56572ab2008-12-12 18:34:08 +0000278 // Did we reach a #endif? If so, go ahead and consume that token as well.
Alp Toker25847352014-07-07 14:01:37 +0000279 if (isEndif) {
280 CurPtr += StoredTokenSize * 2;
281 }
Ted Kremenek56572ab2008-12-12 18:34:08 +0000282
283 return isEndif;
284}
285
Ted Kremenek63ff81c2008-12-17 23:36:32 +0000286SourceLocation PTHLexer::getSourceLocation() {
Chris Lattner137d6492009-01-18 02:10:31 +0000287 // getSourceLocation is not on the hot path. It is used to get the location
288 // of the next token when transitioning back to this lexer when done
Ted Kremenek63ff81c2008-12-17 23:36:32 +0000289 // handling a #included file. Just read the necessary data from the token
290 // data buffer to construct the SourceLocation object.
291 // NOTE: This is a virtual function; hence it is defined out-of-line.
Justin Bogner57ba0b22014-03-28 22:03:24 +0000292 using namespace llvm::support;
293
Alp Toker25847352014-07-07 14:01:37 +0000294 const unsigned char *OffsetPtr = CurPtr + (StoredTokenSize - 4);
Justin Bogner57ba0b22014-03-28 22:03:24 +0000295 uint32_t Offset = endian::readNext<uint32_t, little, aligned>(OffsetPtr);
Argyrios Kyrtzidise6e67de2011-09-19 20:40:19 +0000296 return FileStartLoc.getLocWithOffset(Offset);
Ted Kremenek63ff81c2008-12-17 23:36:32 +0000297}
298
Ted Kremenek47b8cf62009-01-09 22:05:30 +0000299//===----------------------------------------------------------------------===//
Ted Kremenek86423a92009-02-10 22:16:22 +0000300// PTH file lookup: map from strings to file data.
301//===----------------------------------------------------------------------===//
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000302
303/// PTHFileLookup - This internal data structure is used by the PTHManager
304/// to map from FileEntry objects managed by FileManager to offsets within
305/// the PTH file.
306namespace {
Benjamin Kramer337e3a52009-11-28 19:45:26 +0000307class PTHFileData {
Ted Kremenek86423a92009-02-10 22:16:22 +0000308 const uint32_t TokenOff;
309 const uint32_t PPCondOff;
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000310public:
Ted Kremenek86423a92009-02-10 22:16:22 +0000311 PTHFileData(uint32_t tokenOff, uint32_t ppCondOff)
312 : TokenOff(tokenOff), PPCondOff(ppCondOff) {}
Mike Stump11289f42009-09-09 15:08:12 +0000313
314 uint32_t getTokenOffset() const { return TokenOff; }
315 uint32_t getPPCondOffset() const { return PPCondOff; }
Ted Kremenek86423a92009-02-10 22:16:22 +0000316};
Mike Stump11289f42009-09-09 15:08:12 +0000317
318
Benjamin Kramer337e3a52009-11-28 19:45:26 +0000319class PTHFileLookupCommonTrait {
Ted Kremenek86423a92009-02-10 22:16:22 +0000320public:
Ted Kremenek29942a32009-02-13 19:13:46 +0000321 typedef std::pair<unsigned char, const char*> internal_key_type;
Justin Bogner25463f12014-04-18 20:27:24 +0000322 typedef unsigned hash_value_type;
323 typedef unsigned offset_type;
Ted Kremenek29942a32009-02-13 19:13:46 +0000324
Justin Bogner25463f12014-04-18 20:27:24 +0000325 static hash_value_type ComputeHash(internal_key_type x) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +0000326 return llvm::HashString(x.second);
Ted Kremenek86423a92009-02-10 22:16:22 +0000327 }
Mike Stump11289f42009-09-09 15:08:12 +0000328
Ted Kremenek86423a92009-02-10 22:16:22 +0000329 static std::pair<unsigned, unsigned>
330 ReadKeyDataLength(const unsigned char*& d) {
Justin Bogner57ba0b22014-03-28 22:03:24 +0000331 using namespace llvm::support;
332 unsigned keyLen =
333 (unsigned)endian::readNext<uint16_t, little, unaligned>(d);
Ted Kremenek29942a32009-02-13 19:13:46 +0000334 unsigned dataLen = (unsigned) *(d++);
335 return std::make_pair(keyLen, dataLen);
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000336 }
Mike Stump11289f42009-09-09 15:08:12 +0000337
Ted Kremenek29942a32009-02-13 19:13:46 +0000338 static internal_key_type ReadKey(const unsigned char* d, unsigned) {
339 unsigned char k = *(d++); // Read the entry kind.
340 return std::make_pair(k, (const char*) d);
Ted Kremenek86423a92009-02-10 22:16:22 +0000341 }
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000342};
Mike Stump11289f42009-09-09 15:08:12 +0000343
David Blaikie60e836b2014-08-29 22:04:40 +0000344} // end anonymous namespace
345
346class PTHManager::PTHFileLookupTrait : public PTHFileLookupCommonTrait {
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000347public:
348 typedef const FileEntry* external_key_type;
349 typedef PTHFileData data_type;
Mike Stump11289f42009-09-09 15:08:12 +0000350
Ted Kremenek29942a32009-02-13 19:13:46 +0000351 static internal_key_type GetInternalKey(const FileEntry* FE) {
352 return std::make_pair((unsigned char) 0x1, FE->getName());
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000353 }
Ted Kremenek29942a32009-02-13 19:13:46 +0000354
355 static bool EqualKey(internal_key_type a, internal_key_type b) {
356 return a.first == b.first && strcmp(a.second, b.second) == 0;
Mike Stump11289f42009-09-09 15:08:12 +0000357 }
358
359 static PTHFileData ReadData(const internal_key_type& k,
360 const unsigned char* d, unsigned) {
Ted Kremenek29942a32009-02-13 19:13:46 +0000361 assert(k.first == 0x1 && "Only file lookups can match!");
Justin Bogner57ba0b22014-03-28 22:03:24 +0000362 using namespace llvm::support;
363 uint32_t x = endian::readNext<uint32_t, little, unaligned>(d);
364 uint32_t y = endian::readNext<uint32_t, little, unaligned>(d);
Mike Stump11289f42009-09-09 15:08:12 +0000365 return PTHFileData(x, y);
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000366 }
367};
Ted Kremeneke5554de2009-02-11 21:29:16 +0000368
David Blaikie60e836b2014-08-29 22:04:40 +0000369class PTHManager::PTHStringLookupTrait {
Ted Kremeneke5554de2009-02-11 21:29:16 +0000370public:
Justin Bogner25463f12014-04-18 20:27:24 +0000371 typedef uint32_t data_type;
372 typedef const std::pair<const char*, unsigned> external_key_type;
Ted Kremeneke5554de2009-02-11 21:29:16 +0000373 typedef external_key_type internal_key_type;
Justin Bogner25463f12014-04-18 20:27:24 +0000374 typedef uint32_t hash_value_type;
375 typedef unsigned offset_type;
Mike Stump11289f42009-09-09 15:08:12 +0000376
Ted Kremeneke5554de2009-02-11 21:29:16 +0000377 static bool EqualKey(const internal_key_type& a,
378 const internal_key_type& b) {
379 return (a.second == b.second) ? memcmp(a.first, b.first, a.second) == 0
380 : false;
381 }
Mike Stump11289f42009-09-09 15:08:12 +0000382
Justin Bogner25463f12014-04-18 20:27:24 +0000383 static hash_value_type ComputeHash(const internal_key_type& a) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000384 return llvm::HashString(StringRef(a.first, a.second));
Ted Kremeneke5554de2009-02-11 21:29:16 +0000385 }
Mike Stump11289f42009-09-09 15:08:12 +0000386
Ted Kremeneke5554de2009-02-11 21:29:16 +0000387 // This hopefully will just get inlined and removed by the optimizer.
388 static const internal_key_type&
389 GetInternalKey(const external_key_type& x) { return x; }
Mike Stump11289f42009-09-09 15:08:12 +0000390
Ted Kremeneke5554de2009-02-11 21:29:16 +0000391 static std::pair<unsigned, unsigned>
392 ReadKeyDataLength(const unsigned char*& d) {
Justin Bogner57ba0b22014-03-28 22:03:24 +0000393 using namespace llvm::support;
394 return std::make_pair(
395 (unsigned)endian::readNext<uint16_t, little, unaligned>(d),
396 sizeof(uint32_t));
Ted Kremeneke5554de2009-02-11 21:29:16 +0000397 }
Mike Stump11289f42009-09-09 15:08:12 +0000398
Ted Kremeneke5554de2009-02-11 21:29:16 +0000399 static std::pair<const char*, unsigned>
400 ReadKey(const unsigned char* d, unsigned n) {
401 assert(n >= 2 && d[n-1] == '\0');
402 return std::make_pair((const char*) d, n-1);
403 }
Mike Stump11289f42009-09-09 15:08:12 +0000404
Ted Kremenek29942a32009-02-13 19:13:46 +0000405 static uint32_t ReadData(const internal_key_type& k, const unsigned char* d,
406 unsigned) {
Justin Bogner57ba0b22014-03-28 22:03:24 +0000407 using namespace llvm::support;
408 return endian::readNext<uint32_t, little, unaligned>(d);
Ted Kremeneke5554de2009-02-11 21:29:16 +0000409 }
410};
Mike Stump11289f42009-09-09 15:08:12 +0000411
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000412//===----------------------------------------------------------------------===//
413// PTHManager methods.
414//===----------------------------------------------------------------------===//
415
David Blaikie02118ce2014-08-29 22:04:45 +0000416PTHManager::PTHManager(
417 std::unique_ptr<const llvm::MemoryBuffer> buf,
418 std::unique_ptr<PTHFileLookup> fileLookup, const unsigned char *idDataTable,
419 std::unique_ptr<IdentifierInfo *[], llvm::FreeDeleter> perIDCache,
420 std::unique_ptr<PTHStringIdLookup> stringIdLookup, unsigned numIds,
421 const unsigned char *spellingBase, const char *originalSourceFile)
422 : Buf(std::move(buf)), PerIDCache(std::move(perIDCache)),
David Blaikie60e836b2014-08-29 22:04:40 +0000423 FileLookup(std::move(fileLookup)), IdDataTable(idDataTable),
424 StringIdLookup(std::move(stringIdLookup)), NumIds(numIds), PP(nullptr),
425 SpellingBase(spellingBase), OriginalSourceFile(originalSourceFile) {}
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000426
427PTHManager::~PTHManager() {
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000428}
429
David Blaikie9c902b52011-09-25 23:23:43 +0000430static void InvalidPTH(DiagnosticsEngine &Diags, const char *Msg) {
Alp Toker29cb66b2014-01-26 06:17:37 +0000431 Diags.Report(Diags.getCustomDiagID(DiagnosticsEngine::Error, "%0")) << Msg;
Ted Kremenek62224c12009-01-28 21:02:43 +0000432}
433
David Blaikie9c902b52011-09-25 23:23:43 +0000434PTHManager *PTHManager::Create(const std::string &file,
435 DiagnosticsEngine &Diags) {
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000436 // Memory map the PTH file.
Rafael Espindola2d2b4202014-07-06 17:43:24 +0000437 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> FileOrErr =
438 llvm::MemoryBuffer::getFile(file);
Mike Stump11289f42009-09-09 15:08:12 +0000439
Rafael Espindola2d2b4202014-07-06 17:43:24 +0000440 if (!FileOrErr) {
Michael J. Spencerf25faaa2010-12-09 17:36:38 +0000441 // FIXME: Add ec.message() to this diag.
Daniel Dunbar1a54e3f2009-11-12 02:53:48 +0000442 Diags.Report(diag::err_invalid_pth_file) << file;
Craig Topperd2d442c2014-05-17 23:10:59 +0000443 return nullptr;
Ted Kremenek3b0589e2009-01-28 20:49:33 +0000444 }
Rafael Espindola2d2b4202014-07-06 17:43:24 +0000445 std::unique_ptr<llvm::MemoryBuffer> File = std::move(FileOrErr.get());
Mike Stump11289f42009-09-09 15:08:12 +0000446
Justin Bogner57ba0b22014-03-28 22:03:24 +0000447 using namespace llvm::support;
448
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000449 // Get the buffer ranges and check if there are at least three 32-bit
450 // words at the end of the file.
Roman Divackye6377112012-09-06 15:59:27 +0000451 const unsigned char *BufBeg = (const unsigned char*)File->getBufferStart();
452 const unsigned char *BufEnd = (const unsigned char*)File->getBufferEnd();
Ted Kremenekeb8c8fb2009-01-26 21:43:14 +0000453
454 // Check the prologue of the file.
Richard Smith2683b4c2012-08-17 03:55:43 +0000455 if ((BufEnd - BufBeg) < (signed)(sizeof("cfe-pth") + 4 + 4) ||
456 memcmp(BufBeg, "cfe-pth", sizeof("cfe-pth")) != 0) {
Daniel Dunbar1a54e3f2009-11-12 02:53:48 +0000457 Diags.Report(diag::err_invalid_pth_file) << file;
Craig Topperd2d442c2014-05-17 23:10:59 +0000458 return nullptr;
Ted Kremenek62224c12009-01-28 21:02:43 +0000459 }
Mike Stump11289f42009-09-09 15:08:12 +0000460
Ted Kremenek978b5be2009-01-26 21:50:21 +0000461 // Read the PTH version.
Richard Smith2683b4c2012-08-17 03:55:43 +0000462 const unsigned char *p = BufBeg + (sizeof("cfe-pth"));
Justin Bogner57ba0b22014-03-28 22:03:24 +0000463 unsigned Version = endian::readNext<uint32_t, little, aligned>(p);
Mike Stump11289f42009-09-09 15:08:12 +0000464
Daniel Dunbar1a54e3f2009-11-12 02:53:48 +0000465 if (Version < PTHManager::Version) {
466 InvalidPTH(Diags,
Mike Stump11289f42009-09-09 15:08:12 +0000467 Version < PTHManager::Version
Ted Kremenek62224c12009-01-28 21:02:43 +0000468 ? "PTH file uses an older PTH format that is no longer supported"
469 : "PTH file uses a newer PTH format that cannot be read");
Craig Topperd2d442c2014-05-17 23:10:59 +0000470 return nullptr;
Ted Kremenek62224c12009-01-28 21:02:43 +0000471 }
Ted Kremenek978b5be2009-01-26 21:50:21 +0000472
Mike Stump11289f42009-09-09 15:08:12 +0000473 // Compute the address of the index table at the end of the PTH file.
Ted Kremenek4c1d41f2009-02-11 23:34:32 +0000474 const unsigned char *PrologueOffset = p;
Mike Stump11289f42009-09-09 15:08:12 +0000475
Ted Kremenek4c1d41f2009-02-11 23:34:32 +0000476 if (PrologueOffset >= BufEnd) {
Daniel Dunbar1a54e3f2009-11-12 02:53:48 +0000477 Diags.Report(diag::err_invalid_pth_file) << file;
Craig Topperd2d442c2014-05-17 23:10:59 +0000478 return nullptr;
Ted Kremenek62224c12009-01-28 21:02:43 +0000479 }
Mike Stump11289f42009-09-09 15:08:12 +0000480
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000481 // Construct the file lookup table. This will be used for mapping from
482 // FileEntry*'s to cached tokens.
Ted Kremenek4c1d41f2009-02-11 23:34:32 +0000483 const unsigned char* FileTableOffset = PrologueOffset + sizeof(uint32_t)*2;
Justin Bogner57ba0b22014-03-28 22:03:24 +0000484 const unsigned char *FileTable =
485 BufBeg + endian::readNext<uint32_t, little, aligned>(FileTableOffset);
Mike Stump11289f42009-09-09 15:08:12 +0000486
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000487 if (!(FileTable > BufBeg && FileTable < BufEnd)) {
Daniel Dunbar1a54e3f2009-11-12 02:53:48 +0000488 Diags.Report(diag::err_invalid_pth_file) << file;
Craig Topperd2d442c2014-05-17 23:10:59 +0000489 return nullptr; // FIXME: Proper error diagnostic?
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000490 }
Mike Stump11289f42009-09-09 15:08:12 +0000491
Ahmed Charlesb8984322014-03-07 20:03:18 +0000492 std::unique_ptr<PTHFileLookup> FL(PTHFileLookup::Create(FileTable, BufBeg));
Mike Stump11289f42009-09-09 15:08:12 +0000493
Ted Kremenek80e239a2009-03-20 17:54:25 +0000494 // Warn if the PTH file is empty. We still want to create a PTHManager
495 // as the PTH could be used with -include-pth.
496 if (FL->isEmpty())
Daniel Dunbar1a54e3f2009-11-12 02:53:48 +0000497 InvalidPTH(Diags, "PTH file contains no cached source data");
Mike Stump11289f42009-09-09 15:08:12 +0000498
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000499 // Get the location of the table mapping from persistent ids to the
500 // data needed to reconstruct identifiers.
Ted Kremenek4c1d41f2009-02-11 23:34:32 +0000501 const unsigned char* IDTableOffset = PrologueOffset + sizeof(uint32_t)*0;
Justin Bogner57ba0b22014-03-28 22:03:24 +0000502 const unsigned char *IData =
503 BufBeg + endian::readNext<uint32_t, little, aligned>(IDTableOffset);
Mike Stump11289f42009-09-09 15:08:12 +0000504
Ted Kremenek8d6c8282009-01-21 07:34:28 +0000505 if (!(IData >= BufBeg && IData < BufEnd)) {
Daniel Dunbar1a54e3f2009-11-12 02:53:48 +0000506 Diags.Report(diag::err_invalid_pth_file) << file;
Craig Topperd2d442c2014-05-17 23:10:59 +0000507 return nullptr;
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000508 }
Mike Stump11289f42009-09-09 15:08:12 +0000509
Ted Kremeneke5554de2009-02-11 21:29:16 +0000510 // Get the location of the hashtable mapping between strings and
511 // persistent IDs.
Ted Kremenek4c1d41f2009-02-11 23:34:32 +0000512 const unsigned char* StringIdTableOffset = PrologueOffset + sizeof(uint32_t)*1;
Justin Bogner57ba0b22014-03-28 22:03:24 +0000513 const unsigned char *StringIdTable =
514 BufBeg + endian::readNext<uint32_t, little, aligned>(StringIdTableOffset);
Ted Kremeneke5554de2009-02-11 21:29:16 +0000515 if (!(StringIdTable >= BufBeg && StringIdTable < BufEnd)) {
Daniel Dunbar1a54e3f2009-11-12 02:53:48 +0000516 Diags.Report(diag::err_invalid_pth_file) << file;
Craig Topperd2d442c2014-05-17 23:10:59 +0000517 return nullptr;
Ted Kremeneka705b042009-01-15 18:47:46 +0000518 }
Ted Kremeneke5554de2009-02-11 21:29:16 +0000519
Ahmed Charlesb8984322014-03-07 20:03:18 +0000520 std::unique_ptr<PTHStringIdLookup> SL(
521 PTHStringIdLookup::Create(StringIdTable, BufBeg));
Mike Stump11289f42009-09-09 15:08:12 +0000522
Ted Kremenek8d178f42009-01-27 00:01:05 +0000523 // Get the location of the spelling cache.
Ted Kremenek4c1d41f2009-02-11 23:34:32 +0000524 const unsigned char* spellingBaseOffset = PrologueOffset + sizeof(uint32_t)*3;
Justin Bogner57ba0b22014-03-28 22:03:24 +0000525 const unsigned char *spellingBase =
526 BufBeg + endian::readNext<uint32_t, little, aligned>(spellingBaseOffset);
Ted Kremenek8d178f42009-01-27 00:01:05 +0000527 if (!(spellingBase >= BufBeg && spellingBase < BufEnd)) {
Daniel Dunbar1a54e3f2009-11-12 02:53:48 +0000528 Diags.Report(diag::err_invalid_pth_file) << file;
Craig Topperd2d442c2014-05-17 23:10:59 +0000529 return nullptr;
Ted Kremenek8d178f42009-01-27 00:01:05 +0000530 }
Mike Stump11289f42009-09-09 15:08:12 +0000531
Ted Kremenek73a4d282008-12-03 01:16:39 +0000532 // Get the number of IdentifierInfos and pre-allocate the identifier cache.
Justin Bogner57ba0b22014-03-28 22:03:24 +0000533 uint32_t NumIds = endian::readNext<uint32_t, little, aligned>(IData);
Mike Stump11289f42009-09-09 15:08:12 +0000534
Chris Lattner57540c52011-04-15 05:22:18 +0000535 // Pre-allocate the persistent ID -> IdentifierInfo* cache. We use calloc()
Ted Kremenek73a4d282008-12-03 01:16:39 +0000536 // so that we in the best case only zero out memory once when the OS returns
537 // us new pages.
David Blaikie02118ce2014-08-29 22:04:45 +0000538 std::unique_ptr<IdentifierInfo *[], llvm::FreeDeleter> PerIDCache;
Mike Stump11289f42009-09-09 15:08:12 +0000539
Ted Kremenek8d6c8282009-01-21 07:34:28 +0000540 if (NumIds) {
David Blaikie02118ce2014-08-29 22:04:45 +0000541 PerIDCache.reset((IdentifierInfo **)calloc(NumIds, sizeof(PerIDCache[0])));
Ted Kremenek8d6c8282009-01-21 07:34:28 +0000542 if (!PerIDCache) {
Daniel Dunbar1a54e3f2009-11-12 02:53:48 +0000543 InvalidPTH(Diags, "Could not allocate memory for processing PTH file");
Craig Topperd2d442c2014-05-17 23:10:59 +0000544 return nullptr;
Ted Kremenek8d6c8282009-01-21 07:34:28 +0000545 }
Ted Kremenek73a4d282008-12-03 01:16:39 +0000546 }
Ted Kremenek8d6c8282009-01-21 07:34:28 +0000547
Ted Kremenekf9ccd5c2009-03-19 22:19:30 +0000548 // Compute the address of the original source file.
549 const unsigned char* originalSourceBase = PrologueOffset + sizeof(uint32_t)*4;
Justin Bogner57ba0b22014-03-28 22:03:24 +0000550 unsigned len =
551 endian::readNext<uint16_t, little, unaligned>(originalSourceBase);
Craig Topperd2d442c2014-05-17 23:10:59 +0000552 if (!len) originalSourceBase = nullptr;
Mike Stump11289f42009-09-09 15:08:12 +0000553
Ted Kremeneka705b042009-01-15 18:47:46 +0000554 // Create the new PTHManager.
David Blaikie02118ce2014-08-29 22:04:45 +0000555 return new PTHManager(std::move(File), std::move(FL), IData,
556 std::move(PerIDCache), std::move(SL), NumIds,
557 spellingBase, (const char *)originalSourceBase);
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000558}
Ted Kremenekf9ccd5c2009-03-19 22:19:30 +0000559
Chris Lattner144aacd2009-01-18 02:57:21 +0000560IdentifierInfo* PTHManager::LazilyCreateIdentifierInfo(unsigned PersistentID) {
Justin Bogner57ba0b22014-03-28 22:03:24 +0000561 using namespace llvm::support;
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000562 // Look in the PTH file for the string data for the IdentifierInfo object.
Chris Lattner144aacd2009-01-18 02:57:21 +0000563 const unsigned char* TableEntry = IdDataTable + sizeof(uint32_t)*PersistentID;
Justin Bogner57ba0b22014-03-28 22:03:24 +0000564 const unsigned char *IDData =
565 (const unsigned char *)Buf->getBufferStart() +
566 endian::readNext<uint32_t, little, aligned>(TableEntry);
Chris Lattnereb097542009-01-18 01:57:14 +0000567 assert(IDData < (const unsigned char*)Buf->getBufferEnd());
Mike Stump11289f42009-09-09 15:08:12 +0000568
Ted Kremeneka705b042009-01-15 18:47:46 +0000569 // Allocate the object.
Chris Lattnereb097542009-01-18 01:57:14 +0000570 std::pair<IdentifierInfo,const unsigned char*> *Mem =
571 Alloc.Allocate<std::pair<IdentifierInfo,const unsigned char*> >();
Ted Kremeneka705b042009-01-15 18:47:46 +0000572
573 Mem->second = IDData;
Ted Kremeneke5554de2009-02-11 21:29:16 +0000574 assert(IDData[0] != '\0');
Ted Kremenek52f73ca2009-01-20 23:28:34 +0000575 IdentifierInfo *II = new ((void*) Mem) IdentifierInfo();
Mike Stump11289f42009-09-09 15:08:12 +0000576
Ted Kremeneka705b042009-01-15 18:47:46 +0000577 // Store the new IdentifierInfo in the cache.
Chris Lattner144aacd2009-01-18 02:57:21 +0000578 PerIDCache[PersistentID] = II;
Daniel Dunbar2c422dc92009-10-18 20:26:12 +0000579 assert(II->getNameStart() && II->getNameStart()[0] != '\0');
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000580 return II;
581}
582
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000583IdentifierInfo* PTHManager::get(StringRef Name) {
Ted Kremeneke5554de2009-02-11 21:29:16 +0000584 // Double check our assumption that the last character isn't '\0'.
David Blaikie441417a2011-09-22 01:35:49 +0000585 assert(Name.empty() || Name.back() != '\0');
David Blaikie60e836b2014-08-29 22:04:40 +0000586 PTHStringIdLookup::iterator I =
587 StringIdLookup->find(std::make_pair(Name.data(), Name.size()));
588 if (I == StringIdLookup->end()) // No identifier found?
Craig Topperd2d442c2014-05-17 23:10:59 +0000589 return nullptr;
Ted Kremeneka705b042009-01-15 18:47:46 +0000590
Ted Kremeneke5554de2009-02-11 21:29:16 +0000591 // Match found. Return the identifier!
592 assert(*I > 0);
593 return GetIdentifierInfo(*I-1);
594}
Ted Kremeneka705b042009-01-15 18:47:46 +0000595
Chris Lattnerab1d4b82009-01-17 08:06:50 +0000596PTHLexer *PTHManager::CreateLexer(FileID FID) {
597 const FileEntry *FE = PP->getSourceManager().getFileEntryForID(FID);
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000598 if (!FE)
Craig Topperd2d442c2014-05-17 23:10:59 +0000599 return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +0000600
Justin Bogner57ba0b22014-03-28 22:03:24 +0000601 using namespace llvm::support;
602
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000603 // Lookup the FileEntry object in our file lookup data structure. It will
604 // return a variant that indicates whether or not there is an offset within
605 // the PTH file that contains cached tokens.
David Blaikie60e836b2014-08-29 22:04:40 +0000606 PTHFileLookup::iterator I = FileLookup->find(FE);
Mike Stump11289f42009-09-09 15:08:12 +0000607
David Blaikie60e836b2014-08-29 22:04:40 +0000608 if (I == FileLookup->end()) // No tokens available?
Craig Topperd2d442c2014-05-17 23:10:59 +0000609 return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +0000610
611 const PTHFileData& FileData = *I;
612
Chris Lattnereb097542009-01-18 01:57:14 +0000613 const unsigned char *BufStart = (const unsigned char *)Buf->getBufferStart();
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000614 // Compute the offset of the token data within the buffer.
Chris Lattnereb097542009-01-18 01:57:14 +0000615 const unsigned char* data = BufStart + FileData.getTokenOffset();
Ted Kremenek56572ab2008-12-12 18:34:08 +0000616
617 // Get the location of pp-conditional table.
Chris Lattnereb097542009-01-18 01:57:14 +0000618 const unsigned char* ppcond = BufStart + FileData.getPPCondOffset();
Justin Bogner57ba0b22014-03-28 22:03:24 +0000619 uint32_t Len = endian::readNext<uint32_t, little, aligned>(ppcond);
Craig Topperd2d442c2014-05-17 23:10:59 +0000620 if (Len == 0) ppcond = nullptr;
Mike Stump11289f42009-09-09 15:08:12 +0000621
Ted Kremeneka705b042009-01-15 18:47:46 +0000622 assert(PP && "No preprocessor set yet!");
Mike Stump11289f42009-09-09 15:08:12 +0000623 return new PTHLexer(*PP, FID, data, ppcond, *this);
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000624}
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000625
626//===----------------------------------------------------------------------===//
627// 'stat' caching.
628//===----------------------------------------------------------------------===//
629
630namespace {
Benjamin Kramer337e3a52009-11-28 19:45:26 +0000631class PTHStatData {
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000632public:
Rafael Espindolaf8f91b82013-08-01 21:42:11 +0000633 const bool HasData;
634 uint64_t Size;
635 time_t ModTime;
636 llvm::sys::fs::UniqueID UniqueID;
637 bool IsDirectory;
Mike Stump11289f42009-09-09 15:08:12 +0000638
Rafael Espindolaf8f91b82013-08-01 21:42:11 +0000639 PTHStatData(uint64_t Size, time_t ModTime, llvm::sys::fs::UniqueID UniqueID,
640 bool IsDirectory)
641 : HasData(true), Size(Size), ModTime(ModTime), UniqueID(UniqueID),
642 IsDirectory(IsDirectory) {}
Mike Stump11289f42009-09-09 15:08:12 +0000643
Rafael Espindolaf8f91b82013-08-01 21:42:11 +0000644 PTHStatData() : HasData(false) {}
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000645};
Mike Stump11289f42009-09-09 15:08:12 +0000646
Benjamin Kramer337e3a52009-11-28 19:45:26 +0000647class PTHStatLookupTrait : public PTHFileLookupCommonTrait {
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000648public:
Ted Kremenek29942a32009-02-13 19:13:46 +0000649 typedef const char* external_key_type; // const char*
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000650 typedef PTHStatData data_type;
Mike Stump11289f42009-09-09 15:08:12 +0000651
Ted Kremenek29942a32009-02-13 19:13:46 +0000652 static internal_key_type GetInternalKey(const char *path) {
653 // The key 'kind' doesn't matter here because it is ignored in EqualKey.
654 return std::make_pair((unsigned char) 0x0, path);
655 }
656
657 static bool EqualKey(internal_key_type a, internal_key_type b) {
658 // When doing 'stat' lookups we don't care about the kind of 'a' and 'b',
659 // just the paths.
660 return strcmp(a.second, b.second) == 0;
Mike Stump11289f42009-09-09 15:08:12 +0000661 }
662
Ted Kremenek29942a32009-02-13 19:13:46 +0000663 static data_type ReadData(const internal_key_type& k, const unsigned char* d,
Mike Stump11289f42009-09-09 15:08:12 +0000664 unsigned) {
665
Ted Kremenek2fd18ec2009-02-13 22:07:44 +0000666 if (k.first /* File or Directory */) {
Rafael Espindolaf8f91b82013-08-01 21:42:11 +0000667 bool IsDirectory = true;
668 if (k.first == 0x1 /* File */) {
669 IsDirectory = false;
670 d += 4 * 2; // Skip the first 2 words.
671 }
672
Justin Bogner57ba0b22014-03-28 22:03:24 +0000673 using namespace llvm::support;
674
675 uint64_t File = endian::readNext<uint64_t, little, unaligned>(d);
676 uint64_t Device = endian::readNext<uint64_t, little, unaligned>(d);
Sylvestre Ledru0ae40742014-08-18 15:00:11 +0000677 llvm::sys::fs::UniqueID UniqueID(Device, File);
Justin Bogner57ba0b22014-03-28 22:03:24 +0000678 time_t ModTime = endian::readNext<uint64_t, little, unaligned>(d);
679 uint64_t Size = endian::readNext<uint64_t, little, unaligned>(d);
Rafael Espindolaf8f91b82013-08-01 21:42:11 +0000680 return data_type(Size, ModTime, UniqueID, IsDirectory);
Ted Kremenek29942a32009-02-13 19:13:46 +0000681 }
Ted Kremenek2fd18ec2009-02-13 22:07:44 +0000682
683 // Negative stat. Don't read anything.
Ted Kremenek29942a32009-02-13 19:13:46 +0000684 return data_type();
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000685 }
686};
David Blaikie60e836b2014-08-29 22:04:40 +0000687} // end anonymous namespace
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000688
David Blaikie60e836b2014-08-29 22:04:40 +0000689namespace clang {
Chris Lattner226efd32010-11-23 19:19:34 +0000690class PTHStatCache : public FileSystemStatCache {
Justin Bognerbb094f02014-04-18 19:57:06 +0000691 typedef llvm::OnDiskChainedHashTable<PTHStatLookupTrait> CacheTy;
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000692 CacheTy Cache;
693
Mike Stump11289f42009-09-09 15:08:12 +0000694public:
David Blaikie60e836b2014-08-29 22:04:40 +0000695 PTHStatCache(PTHManager::PTHFileLookup &FL)
696 : Cache(FL.getNumBuckets(), FL.getNumEntries(), FL.getBuckets(),
697 FL.getBase()) {}
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000698
Rafael Espindolaf8f91b82013-08-01 21:42:11 +0000699 LookupResult getStat(const char *Path, FileData &Data, bool isFile,
David Blaikie326ffb32014-07-08 15:46:02 +0000700 std::unique_ptr<vfs::File> *F,
701 vfs::FileSystem &FS) override {
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000702 // Do the lookup for the file's data in the PTH file.
Chris Lattner226efd32010-11-23 19:19:34 +0000703 CacheTy::iterator I = Cache.find(Path);
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000704
705 // If we don't get a hit in the PTH file just forward to 'stat'.
Kovarththanan Rajaratnam661a3092010-03-12 08:23:34 +0000706 if (I == Cache.end())
Ben Langmuirc8130a72014-02-20 21:59:23 +0000707 return statChained(Path, Data, isFile, F, FS);
Mike Stump11289f42009-09-09 15:08:12 +0000708
Rafael Espindolaf8f91b82013-08-01 21:42:11 +0000709 const PTHStatData &D = *I;
Mike Stump11289f42009-09-09 15:08:12 +0000710
Rafael Espindolaf8f91b82013-08-01 21:42:11 +0000711 if (!D.HasData)
Chris Lattner8f0583d2010-11-23 20:05:15 +0000712 return CacheMissing;
Ted Kremenek2fd18ec2009-02-13 22:07:44 +0000713
Ben Langmuird066d4c2014-02-28 21:16:07 +0000714 Data.Name = Path;
Rafael Espindolaf8f91b82013-08-01 21:42:11 +0000715 Data.Size = D.Size;
716 Data.ModTime = D.ModTime;
717 Data.UniqueID = D.UniqueID;
718 Data.IsDirectory = D.IsDirectory;
719 Data.IsNamedPipe = false;
720 Data.InPCH = true;
721
Chris Lattner8f0583d2010-11-23 20:05:15 +0000722 return CacheExists;
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000723 }
724};
David Blaikie60e836b2014-08-29 22:04:40 +0000725}
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000726
David Blaikie23430cc2014-08-11 21:29:24 +0000727std::unique_ptr<FileSystemStatCache> PTHManager::createStatCache() {
David Blaikie60e836b2014-08-29 22:04:40 +0000728 return llvm::make_unique<PTHStatCache>(*FileLookup);
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000729}