blob: e8f43f7e50a7cd89dbf7e13ca653b12b64088985 [file] [log] [blame]
Ted Kremenek7cd62452008-11-12 21:37:15 +00001//===--- PTHLexer.cpp - Lex from a token stream ---------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the PTHLexer interface.
11//
12//===----------------------------------------------------------------------===//
13
Chandler Carruth3a022472012-12-04 09:13:33 +000014#include "clang/Lex/PTHLexer.h"
Ted Kremenek33eeabd2008-12-03 00:38:03 +000015#include "clang/Basic/FileManager.h"
Chris Lattner226efd32010-11-23 19:19:34 +000016#include "clang/Basic/FileSystemStatCache.h"
Ted Kremenek33eeabd2008-12-03 00:38:03 +000017#include "clang/Basic/IdentifierTable.h"
Douglas Gregor52289d32009-04-20 07:08:21 +000018#include "clang/Basic/OnDiskHashTable.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000019#include "clang/Basic/TokenKinds.h"
Daniel Dunbar1a54e3f2009-11-12 02:53:48 +000020#include "clang/Lex/LexDiagnostic.h"
Ted Kremenek33eeabd2008-12-03 00:38:03 +000021#include "clang/Lex/PTHManager.h"
Ted Kremenek33eeabd2008-12-03 00:38:03 +000022#include "clang/Lex/Preprocessor.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000023#include "clang/Lex/Token.h"
Ted Kremenek33eeabd2008-12-03 00:38:03 +000024#include "llvm/ADT/OwningPtr.h"
Daniel Dunbarf8502d52009-10-17 23:52:28 +000025#include "llvm/ADT/StringExtras.h"
26#include "llvm/ADT/StringMap.h"
Chris Lattner34eab392009-01-22 23:50:07 +000027#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencerf25faaa2010-12-09 17:36:38 +000028#include "llvm/Support/system_error.h"
Ted Kremenek7cd62452008-11-12 21:37:15 +000029using namespace clang;
Douglas Gregor52289d32009-04-20 07:08:21 +000030using namespace clang::io;
Ted Kremenek7cd62452008-11-12 21:37:15 +000031
Ted Kremenek8433f0b2009-01-19 23:13:15 +000032#define DISK_TOKEN_SIZE (1+1+2+4+4)
Ted Kremenek56572ab2008-12-12 18:34:08 +000033
Ted Kremenek33eeabd2008-12-03 00:38:03 +000034//===----------------------------------------------------------------------===//
Ted Kremenek1b18ad22008-12-23 01:30:52 +000035// PTHLexer methods.
36//===----------------------------------------------------------------------===//
37
Chris Lattnereb097542009-01-18 01:57:14 +000038PTHLexer::PTHLexer(Preprocessor &PP, FileID FID, const unsigned char *D,
Ted Kremenek8d178f42009-01-27 00:01:05 +000039 const unsigned char *ppcond, PTHManager &PM)
Chris Lattnerd32480d2009-01-17 06:22:33 +000040 : PreprocessorLexer(&PP, FID), TokBuf(D), CurPtr(D), LastHashTokPtr(0),
Ted Kremenek8d178f42009-01-27 00:01:05 +000041 PPCond(ppcond), CurPPCondPtr(ppcond), PTHMgr(PM) {
Mike Stump11289f42009-09-09 15:08:12 +000042
Chris Lattnerd32480d2009-01-17 06:22:33 +000043 FileStartLoc = PP.getSourceManager().getLocForStartOfFile(FID);
Ted Kremenek47b8cf62009-01-09 22:05:30 +000044}
Ted Kremenek1b18ad22008-12-23 01:30:52 +000045
46void PTHLexer::Lex(Token& Tok) {
47LexNextToken:
Ted Kremenek1b18ad22008-12-23 01:30:52 +000048
Ted Kremenek66076a92008-12-23 02:30:15 +000049 //===--------------------------------------==//
50 // Read the raw token data.
51 //===--------------------------------------==//
Mike Stump11289f42009-09-09 15:08:12 +000052
Ted Kremenek66076a92008-12-23 02:30:15 +000053 // Shadow CurPtr into an automatic variable.
Mike Stump11289f42009-09-09 15:08:12 +000054 const unsigned char *CurPtrShadow = CurPtr;
Ted Kremenek66076a92008-12-23 02:30:15 +000055
Chris Lattner137d6492009-01-18 02:10:31 +000056 // Read in the data for the token.
Chris Lattnerfec54702009-01-22 19:48:26 +000057 unsigned Word0 = ReadLE32(CurPtrShadow);
58 uint32_t IdentifierID = ReadLE32(CurPtrShadow);
59 uint32_t FileOffset = ReadLE32(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())
93 PP->HandleIdentifier(Tok);
94 return;
95 }
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 Kremenek2bd41d12010-07-27 02:59:02 +0000104 // Save 'PP' to 'PPCache' as LexEndOfFile can delete 'this'.
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000105 Preprocessor *PPCache = PP;
Mike Stump11289f42009-09-09 15:08:12 +0000106
Ted Kremenek78cc2472008-12-23 19:24:24 +0000107 assert(!ParsingPreprocessorDirective);
108 assert(!LexingRawMode);
Ted Kremenek2bd41d12010-07-27 02:59:02 +0000109
110 if (LexEndOfFile(Tok))
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000111 return;
Mike Stump11289f42009-09-09 15:08:12 +0000112
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000113 return PPCache->Lex(Tok);
114 }
Mike Stump11289f42009-09-09 15:08:12 +0000115
Chris Lattner18fc6ce2009-01-18 02:34:01 +0000116 if (TKind == tok::hash && Tok.isAtStartOfLine()) {
Ted Kremenek78cc2472008-12-23 19:24:24 +0000117 LastHashTokPtr = CurPtr - DISK_TOKEN_SIZE;
118 assert(!LexingRawMode);
119 PP->HandleDirective(Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000120
Ted Kremenek78cc2472008-12-23 19:24:24 +0000121 if (PP->isCurrentLexer(this))
122 goto LexNextToken;
Mike Stump11289f42009-09-09 15:08:12 +0000123
Ted Kremenek78cc2472008-12-23 19:24:24 +0000124 return PP->Lex(Tok);
125 }
Mike Stump11289f42009-09-09 15:08:12 +0000126
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000127 if (TKind == tok::eod) {
Ted Kremenek78cc2472008-12-23 19:24:24 +0000128 assert(ParsingPreprocessorDirective);
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000129 ParsingPreprocessorDirective = false;
130 return;
131 }
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000132
Ted Kremenek78cc2472008-12-23 19:24:24 +0000133 MIOpt.ReadToken();
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000134}
135
Ted Kremenek2bd41d12010-07-27 02:59:02 +0000136bool PTHLexer::LexEndOfFile(Token &Result) {
137 // If we hit the end of the file while parsing a preprocessor directive,
138 // end the preprocessor directive first. The next token returned will
139 // then be the end of file.
140 if (ParsingPreprocessorDirective) {
141 ParsingPreprocessorDirective = false; // Done parsing the "line".
142 return true; // Have a token.
143 }
144
145 assert(!LexingRawMode);
146
147 // If we are in a #if directive, emit an error.
148 while (!ConditionalStack.empty()) {
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000149 if (PP->getCodeCompletionFileLoc() != FileStartLoc)
Douglas Gregor02690ba2010-08-12 17:04:55 +0000150 PP->Diag(ConditionalStack.back().IfLoc,
151 diag::err_pp_unterminated_conditional);
Ted Kremenek2bd41d12010-07-27 02:59:02 +0000152 ConditionalStack.pop_back();
153 }
154
155 // Finally, let the preprocessor handle this.
156 return PP->HandleEndOfFile(Result);
157}
158
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000159// FIXME: We can just grab the last token instead of storing a copy
160// into EofToken.
Ted Kremenek78cc2472008-12-23 19:24:24 +0000161void PTHLexer::getEOF(Token& Tok) {
Ted Kremenek8ae06622009-01-09 00:36:11 +0000162 assert(EofToken.is(tok::eof));
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000163 Tok = EofToken;
164}
165
166void PTHLexer::DiscardToEndOfLine() {
167 assert(ParsingPreprocessorDirective && ParsingFilename == false &&
168 "Must be in a preprocessing directive!");
169
170 // We assume that if the preprocessor wishes to discard to the end of
171 // the line that it also means to end the current preprocessor directive.
172 ParsingPreprocessorDirective = false;
Mike Stump11289f42009-09-09 15:08:12 +0000173
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000174 // Skip tokens by only peeking at their token kind and the flags.
175 // We don't need to actually reconstruct full tokens from the token buffer.
176 // This saves some copies and it also reduces IdentifierInfo* lookup.
Chris Lattnereb097542009-01-18 01:57:14 +0000177 const unsigned char* p = CurPtr;
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000178 while (1) {
179 // Read the token kind. Are we at the end of the file?
180 tok::TokenKind x = (tok::TokenKind) (uint8_t) *p;
181 if (x == tok::eof) break;
Mike Stump11289f42009-09-09 15:08:12 +0000182
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000183 // Read the token flags. Are we at the start of the next line?
184 Token::TokenFlags y = (Token::TokenFlags) (uint8_t) p[1];
185 if (y & Token::StartOfLine) break;
186
187 // Skip to the next token.
188 p += DISK_TOKEN_SIZE;
189 }
Mike Stump11289f42009-09-09 15:08:12 +0000190
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000191 CurPtr = p;
192}
193
Ted Kremenek56572ab2008-12-12 18:34:08 +0000194/// SkipBlock - Used by Preprocessor to skip the current conditional block.
195bool PTHLexer::SkipBlock() {
196 assert(CurPPCondPtr && "No cached PP conditional information.");
197 assert(LastHashTokPtr && "No known '#' token.");
Mike Stump11289f42009-09-09 15:08:12 +0000198
Chris Lattnereb097542009-01-18 01:57:14 +0000199 const unsigned char* HashEntryI = 0;
Ted Kremenek56572ab2008-12-12 18:34:08 +0000200 uint32_t TableIdx;
Mike Stump11289f42009-09-09 15:08:12 +0000201
Ted Kremenek56572ab2008-12-12 18:34:08 +0000202 do {
Ted Kremenek877556f2008-12-12 22:05:38 +0000203 // Read the token offset from the side-table.
Ted Kremenekdb908592012-09-16 06:18:45 +0000204 uint32_t Offset = ReadLE32(CurPPCondPtr);
Mike Stump11289f42009-09-09 15:08:12 +0000205
206 // Read the target table index from the side-table.
Chris Lattnerfec54702009-01-22 19:48:26 +0000207 TableIdx = ReadLE32(CurPPCondPtr);
Mike Stump11289f42009-09-09 15:08:12 +0000208
Ted Kremenek877556f2008-12-12 22:05:38 +0000209 // Compute the actual memory address of the '#' token data for this entry.
210 HashEntryI = TokBuf + Offset;
211
212 // Optmization: "Sibling jumping". #if...#else...#endif blocks can
213 // contain nested blocks. In the side-table we can jump over these
214 // nested blocks instead of doing a linear search if the next "sibling"
215 // entry is not at a location greater than LastHashTokPtr.
216 if (HashEntryI < LastHashTokPtr && TableIdx) {
217 // In the side-table we are still at an entry for a '#' token that
218 // is earlier than the last one we saw. Check if the location we would
219 // stride gets us closer.
Chris Lattnereb097542009-01-18 01:57:14 +0000220 const unsigned char* NextPPCondPtr =
221 PPCond + TableIdx*(sizeof(uint32_t)*2);
Ted Kremenek877556f2008-12-12 22:05:38 +0000222 assert(NextPPCondPtr >= CurPPCondPtr);
223 // Read where we should jump to.
Ted Kremenekdb908592012-09-16 06:18:45 +0000224 const unsigned char* HashEntryJ = TokBuf + ReadLE32(NextPPCondPtr);
Mike Stump11289f42009-09-09 15:08:12 +0000225
Ted Kremenek877556f2008-12-12 22:05:38 +0000226 if (HashEntryJ <= LastHashTokPtr) {
227 // Jump directly to the next entry in the side table.
228 HashEntryI = HashEntryJ;
Chris Lattnerfec54702009-01-22 19:48:26 +0000229 TableIdx = ReadLE32(NextPPCondPtr);
Ted Kremenek877556f2008-12-12 22:05:38 +0000230 CurPPCondPtr = NextPPCondPtr;
231 }
232 }
Ted Kremenek56572ab2008-12-12 18:34:08 +0000233 }
Mike Stump11289f42009-09-09 15:08:12 +0000234 while (HashEntryI < LastHashTokPtr);
Ted Kremenek877556f2008-12-12 22:05:38 +0000235 assert(HashEntryI == LastHashTokPtr && "No PP-cond entry found for '#'");
Ted Kremenek56572ab2008-12-12 18:34:08 +0000236 assert(TableIdx && "No jumping from #endifs.");
Mike Stump11289f42009-09-09 15:08:12 +0000237
Ted Kremenek56572ab2008-12-12 18:34:08 +0000238 // Update our side-table iterator.
Chris Lattnereb097542009-01-18 01:57:14 +0000239 const unsigned char* NextPPCondPtr = PPCond + TableIdx*(sizeof(uint32_t)*2);
Ted Kremenek56572ab2008-12-12 18:34:08 +0000240 assert(NextPPCondPtr >= CurPPCondPtr);
241 CurPPCondPtr = NextPPCondPtr;
Mike Stump11289f42009-09-09 15:08:12 +0000242
Ted Kremenek56572ab2008-12-12 18:34:08 +0000243 // Read where we should jump to.
Chris Lattnerfec54702009-01-22 19:48:26 +0000244 HashEntryI = TokBuf + ReadLE32(NextPPCondPtr);
245 uint32_t NextIdx = ReadLE32(NextPPCondPtr);
Mike Stump11289f42009-09-09 15:08:12 +0000246
Ted Kremenek56572ab2008-12-12 18:34:08 +0000247 // By construction NextIdx will be zero if this is a #endif. This is useful
248 // to know to obviate lexing another token.
249 bool isEndif = NextIdx == 0;
Mike Stump11289f42009-09-09 15:08:12 +0000250
Ted Kremenek56572ab2008-12-12 18:34:08 +0000251 // This case can occur when we see something like this:
252 //
253 // #if ...
254 // /* a comment or nothing */
255 // #elif
256 //
257 // If we are skipping the first #if block it will be the case that CurPtr
258 // already points 'elif'. Just return.
Mike Stump11289f42009-09-09 15:08:12 +0000259
Ted Kremenek877556f2008-12-12 22:05:38 +0000260 if (CurPtr > HashEntryI) {
261 assert(CurPtr == HashEntryI + DISK_TOKEN_SIZE);
Ted Kremenek56572ab2008-12-12 18:34:08 +0000262 // Did we reach a #endif? If so, go ahead and consume that token as well.
263 if (isEndif)
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000264 CurPtr += DISK_TOKEN_SIZE*2;
Ted Kremenek56572ab2008-12-12 18:34:08 +0000265 else
Ted Kremenek877556f2008-12-12 22:05:38 +0000266 LastHashTokPtr = HashEntryI;
Mike Stump11289f42009-09-09 15:08:12 +0000267
Ted Kremenek56572ab2008-12-12 18:34:08 +0000268 return isEndif;
269 }
270
271 // Otherwise, we need to advance. Update CurPtr to point to the '#' token.
Ted Kremenek877556f2008-12-12 22:05:38 +0000272 CurPtr = HashEntryI;
Mike Stump11289f42009-09-09 15:08:12 +0000273
Ted Kremenek56572ab2008-12-12 18:34:08 +0000274 // Update the location of the last observed '#'. This is useful if we
275 // are skipping multiple blocks.
276 LastHashTokPtr = CurPtr;
Ted Kremenek56572ab2008-12-12 18:34:08 +0000277
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000278 // Skip the '#' token.
Chris Lattnereb097542009-01-18 01:57:14 +0000279 assert(((tok::TokenKind)*CurPtr) == tok::hash);
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000280 CurPtr += DISK_TOKEN_SIZE;
Mike Stump11289f42009-09-09 15:08:12 +0000281
Ted Kremenek56572ab2008-12-12 18:34:08 +0000282 // Did we reach a #endif? If so, go ahead and consume that token as well.
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000283 if (isEndif) { CurPtr += DISK_TOKEN_SIZE*2; }
Ted Kremenek56572ab2008-12-12 18:34:08 +0000284
285 return isEndif;
286}
287
Ted Kremenek63ff81c2008-12-17 23:36:32 +0000288SourceLocation PTHLexer::getSourceLocation() {
Chris Lattner137d6492009-01-18 02:10:31 +0000289 // getSourceLocation is not on the hot path. It is used to get the location
290 // of the next token when transitioning back to this lexer when done
Ted Kremenek63ff81c2008-12-17 23:36:32 +0000291 // handling a #included file. Just read the necessary data from the token
292 // data buffer to construct the SourceLocation object.
293 // NOTE: This is a virtual function; hence it is defined out-of-line.
Ted Kremenekae54f2f2009-01-21 22:41:38 +0000294 const unsigned char *OffsetPtr = CurPtr + (DISK_TOKEN_SIZE - 4);
Chris Lattnerfec54702009-01-22 19:48:26 +0000295 uint32_t Offset = ReadLE32(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;
322
323 static unsigned ComputeHash(internal_key_type x) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +0000324 return llvm::HashString(x.second);
Ted Kremenek86423a92009-02-10 22:16:22 +0000325 }
Mike Stump11289f42009-09-09 15:08:12 +0000326
Ted Kremenek86423a92009-02-10 22:16:22 +0000327 static std::pair<unsigned, unsigned>
328 ReadKeyDataLength(const unsigned char*& d) {
Ted Kremenek29942a32009-02-13 19:13:46 +0000329 unsigned keyLen = (unsigned) ReadUnalignedLE16(d);
330 unsigned dataLen = (unsigned) *(d++);
331 return std::make_pair(keyLen, dataLen);
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000332 }
Mike Stump11289f42009-09-09 15:08:12 +0000333
Ted Kremenek29942a32009-02-13 19:13:46 +0000334 static internal_key_type ReadKey(const unsigned char* d, unsigned) {
335 unsigned char k = *(d++); // Read the entry kind.
336 return std::make_pair(k, (const char*) d);
Ted Kremenek86423a92009-02-10 22:16:22 +0000337 }
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000338};
Mike Stump11289f42009-09-09 15:08:12 +0000339
Benjamin Kramer337e3a52009-11-28 19:45:26 +0000340class PTHFileLookupTrait : public PTHFileLookupCommonTrait {
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000341public:
342 typedef const FileEntry* external_key_type;
343 typedef PTHFileData data_type;
Mike Stump11289f42009-09-09 15:08:12 +0000344
Ted Kremenek29942a32009-02-13 19:13:46 +0000345 static internal_key_type GetInternalKey(const FileEntry* FE) {
346 return std::make_pair((unsigned char) 0x1, FE->getName());
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000347 }
Ted Kremenek29942a32009-02-13 19:13:46 +0000348
349 static bool EqualKey(internal_key_type a, internal_key_type b) {
350 return a.first == b.first && strcmp(a.second, b.second) == 0;
Mike Stump11289f42009-09-09 15:08:12 +0000351 }
352
353 static PTHFileData ReadData(const internal_key_type& k,
354 const unsigned char* d, unsigned) {
Ted Kremenek29942a32009-02-13 19:13:46 +0000355 assert(k.first == 0x1 && "Only file lookups can match!");
Ted Kremenek86423a92009-02-10 22:16:22 +0000356 uint32_t x = ::ReadUnalignedLE32(d);
357 uint32_t y = ::ReadUnalignedLE32(d);
Mike Stump11289f42009-09-09 15:08:12 +0000358 return PTHFileData(x, y);
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000359 }
360};
Ted Kremeneke5554de2009-02-11 21:29:16 +0000361
Benjamin Kramer337e3a52009-11-28 19:45:26 +0000362class PTHStringLookupTrait {
Ted Kremeneke5554de2009-02-11 21:29:16 +0000363public:
Mike Stump11289f42009-09-09 15:08:12 +0000364 typedef uint32_t
Ted Kremeneke5554de2009-02-11 21:29:16 +0000365 data_type;
366
367 typedef const std::pair<const char*, unsigned>
368 external_key_type;
369
370 typedef external_key_type internal_key_type;
Mike Stump11289f42009-09-09 15:08:12 +0000371
Ted Kremeneke5554de2009-02-11 21:29:16 +0000372 static bool EqualKey(const internal_key_type& a,
373 const internal_key_type& b) {
374 return (a.second == b.second) ? memcmp(a.first, b.first, a.second) == 0
375 : false;
376 }
Mike Stump11289f42009-09-09 15:08:12 +0000377
Ted Kremeneke5554de2009-02-11 21:29:16 +0000378 static unsigned ComputeHash(const internal_key_type& a) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000379 return llvm::HashString(StringRef(a.first, a.second));
Ted Kremeneke5554de2009-02-11 21:29:16 +0000380 }
Mike Stump11289f42009-09-09 15:08:12 +0000381
Ted Kremeneke5554de2009-02-11 21:29:16 +0000382 // This hopefully will just get inlined and removed by the optimizer.
383 static const internal_key_type&
384 GetInternalKey(const external_key_type& x) { return x; }
Mike Stump11289f42009-09-09 15:08:12 +0000385
Ted Kremeneke5554de2009-02-11 21:29:16 +0000386 static std::pair<unsigned, unsigned>
387 ReadKeyDataLength(const unsigned char*& d) {
388 return std::make_pair((unsigned) ReadUnalignedLE16(d), sizeof(uint32_t));
389 }
Mike Stump11289f42009-09-09 15:08:12 +0000390
Ted Kremeneke5554de2009-02-11 21:29:16 +0000391 static std::pair<const char*, unsigned>
392 ReadKey(const unsigned char* d, unsigned n) {
393 assert(n >= 2 && d[n-1] == '\0');
394 return std::make_pair((const char*) d, n-1);
395 }
Mike Stump11289f42009-09-09 15:08:12 +0000396
Ted Kremenek29942a32009-02-13 19:13:46 +0000397 static uint32_t ReadData(const internal_key_type& k, const unsigned char* d,
398 unsigned) {
Ted Kremeneke5554de2009-02-11 21:29:16 +0000399 return ::ReadUnalignedLE32(d);
400 }
401};
Mike Stump11289f42009-09-09 15:08:12 +0000402
403} // end anonymous namespace
Ted Kremenek86423a92009-02-10 22:16:22 +0000404
Ted Kremeneke5554de2009-02-11 21:29:16 +0000405typedef OnDiskChainedHashTable<PTHFileLookupTrait> PTHFileLookup;
406typedef OnDiskChainedHashTable<PTHStringLookupTrait> PTHStringIdLookup;
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000407
408//===----------------------------------------------------------------------===//
409// PTHManager methods.
410//===----------------------------------------------------------------------===//
411
412PTHManager::PTHManager(const llvm::MemoryBuffer* buf, void* fileLookup,
Chris Lattnereb097542009-01-18 01:57:14 +0000413 const unsigned char* idDataTable,
Mike Stump11289f42009-09-09 15:08:12 +0000414 IdentifierInfo** perIDCache,
Ted Kremeneke5554de2009-02-11 21:29:16 +0000415 void* stringIdLookup, unsigned numIds,
Ted Kremenekf9ccd5c2009-03-19 22:19:30 +0000416 const unsigned char* spellingBase,
417 const char* originalSourceFile)
Ted Kremenek73a4d282008-12-03 01:16:39 +0000418: Buf(buf), PerIDCache(perIDCache), FileLookup(fileLookup),
Ted Kremeneke5554de2009-02-11 21:29:16 +0000419 IdDataTable(idDataTable), StringIdLookup(stringIdLookup),
Ted Kremenekf9ccd5c2009-03-19 22:19:30 +0000420 NumIds(numIds), PP(0), SpellingBase(spellingBase),
421 OriginalSourceFile(originalSourceFile) {}
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000422
423PTHManager::~PTHManager() {
424 delete Buf;
425 delete (PTHFileLookup*) FileLookup;
Ted Kremeneke5554de2009-02-11 21:29:16 +0000426 delete (PTHStringIdLookup*) StringIdLookup;
Ted Kremenek1aed3dd2008-12-04 22:47:11 +0000427 free(PerIDCache);
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000428}
429
David Blaikie9c902b52011-09-25 23:23:43 +0000430static void InvalidPTH(DiagnosticsEngine &Diags, const char *Msg) {
431 Diags.Report(Diags.getCustomDiagID(DiagnosticsEngine::Error, 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.
Dylan Noblesmithe2778992012-02-05 02:12:40 +0000437 OwningPtr<llvm::MemoryBuffer> File;
Mike Stump11289f42009-09-09 15:08:12 +0000438
Michael J. Spencerd9da7a12010-12-16 03:28:14 +0000439 if (llvm::MemoryBuffer::getFile(file, File)) {
Michael J. Spencerf25faaa2010-12-09 17:36:38 +0000440 // FIXME: Add ec.message() to this diag.
Daniel Dunbar1a54e3f2009-11-12 02:53:48 +0000441 Diags.Report(diag::err_invalid_pth_file) << file;
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000442 return 0;
Ted Kremenek3b0589e2009-01-28 20:49:33 +0000443 }
Mike Stump11289f42009-09-09 15:08:12 +0000444
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000445 // Get the buffer ranges and check if there are at least three 32-bit
446 // words at the end of the file.
Roman Divackye6377112012-09-06 15:59:27 +0000447 const unsigned char *BufBeg = (const unsigned char*)File->getBufferStart();
448 const unsigned char *BufEnd = (const unsigned char*)File->getBufferEnd();
Ted Kremenekeb8c8fb2009-01-26 21:43:14 +0000449
450 // Check the prologue of the file.
Richard Smith2683b4c2012-08-17 03:55:43 +0000451 if ((BufEnd - BufBeg) < (signed)(sizeof("cfe-pth") + 4 + 4) ||
452 memcmp(BufBeg, "cfe-pth", sizeof("cfe-pth")) != 0) {
Daniel Dunbar1a54e3f2009-11-12 02:53:48 +0000453 Diags.Report(diag::err_invalid_pth_file) << file;
Ted Kremenekeb8c8fb2009-01-26 21:43:14 +0000454 return 0;
Ted Kremenek62224c12009-01-28 21:02:43 +0000455 }
Mike Stump11289f42009-09-09 15:08:12 +0000456
Ted Kremenek978b5be2009-01-26 21:50:21 +0000457 // Read the PTH version.
Richard Smith2683b4c2012-08-17 03:55:43 +0000458 const unsigned char *p = BufBeg + (sizeof("cfe-pth"));
Ted Kremenek978b5be2009-01-26 21:50:21 +0000459 unsigned Version = ReadLE32(p);
Mike Stump11289f42009-09-09 15:08:12 +0000460
Daniel Dunbar1a54e3f2009-11-12 02:53:48 +0000461 if (Version < PTHManager::Version) {
462 InvalidPTH(Diags,
Mike Stump11289f42009-09-09 15:08:12 +0000463 Version < PTHManager::Version
Ted Kremenek62224c12009-01-28 21:02:43 +0000464 ? "PTH file uses an older PTH format that is no longer supported"
465 : "PTH file uses a newer PTH format that cannot be read");
Ted Kremenek978b5be2009-01-26 21:50:21 +0000466 return 0;
Ted Kremenek62224c12009-01-28 21:02:43 +0000467 }
Ted Kremenek978b5be2009-01-26 21:50:21 +0000468
Mike Stump11289f42009-09-09 15:08:12 +0000469 // Compute the address of the index table at the end of the PTH file.
Ted Kremenek4c1d41f2009-02-11 23:34:32 +0000470 const unsigned char *PrologueOffset = p;
Mike Stump11289f42009-09-09 15:08:12 +0000471
Ted Kremenek4c1d41f2009-02-11 23:34:32 +0000472 if (PrologueOffset >= BufEnd) {
Daniel Dunbar1a54e3f2009-11-12 02:53:48 +0000473 Diags.Report(diag::err_invalid_pth_file) << file;
Ted Kremenekeb8c8fb2009-01-26 21:43:14 +0000474 return 0;
Ted Kremenek62224c12009-01-28 21:02:43 +0000475 }
Mike Stump11289f42009-09-09 15:08:12 +0000476
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000477 // Construct the file lookup table. This will be used for mapping from
478 // FileEntry*'s to cached tokens.
Ted Kremenek4c1d41f2009-02-11 23:34:32 +0000479 const unsigned char* FileTableOffset = PrologueOffset + sizeof(uint32_t)*2;
Chris Lattnerfec54702009-01-22 19:48:26 +0000480 const unsigned char* FileTable = BufBeg + ReadLE32(FileTableOffset);
Mike Stump11289f42009-09-09 15:08:12 +0000481
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000482 if (!(FileTable > BufBeg && FileTable < BufEnd)) {
Daniel Dunbar1a54e3f2009-11-12 02:53:48 +0000483 Diags.Report(diag::err_invalid_pth_file) << file;
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000484 return 0; // FIXME: Proper error diagnostic?
485 }
Mike Stump11289f42009-09-09 15:08:12 +0000486
Dylan Noblesmithe2778992012-02-05 02:12:40 +0000487 OwningPtr<PTHFileLookup> FL(PTHFileLookup::Create(FileTable, BufBeg));
Mike Stump11289f42009-09-09 15:08:12 +0000488
Ted Kremenek80e239a2009-03-20 17:54:25 +0000489 // Warn if the PTH file is empty. We still want to create a PTHManager
490 // as the PTH could be used with -include-pth.
491 if (FL->isEmpty())
Daniel Dunbar1a54e3f2009-11-12 02:53:48 +0000492 InvalidPTH(Diags, "PTH file contains no cached source data");
Mike Stump11289f42009-09-09 15:08:12 +0000493
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000494 // Get the location of the table mapping from persistent ids to the
495 // data needed to reconstruct identifiers.
Ted Kremenek4c1d41f2009-02-11 23:34:32 +0000496 const unsigned char* IDTableOffset = PrologueOffset + sizeof(uint32_t)*0;
Chris Lattnerfec54702009-01-22 19:48:26 +0000497 const unsigned char* IData = BufBeg + ReadLE32(IDTableOffset);
Mike Stump11289f42009-09-09 15:08:12 +0000498
Ted Kremenek8d6c8282009-01-21 07:34:28 +0000499 if (!(IData >= BufBeg && IData < BufEnd)) {
Daniel Dunbar1a54e3f2009-11-12 02:53:48 +0000500 Diags.Report(diag::err_invalid_pth_file) << file;
Ted Kremenek62224c12009-01-28 21:02:43 +0000501 return 0;
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000502 }
Mike Stump11289f42009-09-09 15:08:12 +0000503
Ted Kremeneke5554de2009-02-11 21:29:16 +0000504 // Get the location of the hashtable mapping between strings and
505 // persistent IDs.
Ted Kremenek4c1d41f2009-02-11 23:34:32 +0000506 const unsigned char* StringIdTableOffset = PrologueOffset + sizeof(uint32_t)*1;
Ted Kremeneke5554de2009-02-11 21:29:16 +0000507 const unsigned char* StringIdTable = BufBeg + ReadLE32(StringIdTableOffset);
508 if (!(StringIdTable >= BufBeg && StringIdTable < BufEnd)) {
Daniel Dunbar1a54e3f2009-11-12 02:53:48 +0000509 Diags.Report(diag::err_invalid_pth_file) << file;
Ted Kremenek62224c12009-01-28 21:02:43 +0000510 return 0;
Ted Kremeneka705b042009-01-15 18:47:46 +0000511 }
Ted Kremeneke5554de2009-02-11 21:29:16 +0000512
Dylan Noblesmithe2778992012-02-05 02:12:40 +0000513 OwningPtr<PTHStringIdLookup> SL(PTHStringIdLookup::Create(StringIdTable,
Ted Kremeneke5554de2009-02-11 21:29:16 +0000514 BufBeg));
Mike Stump11289f42009-09-09 15:08:12 +0000515
Ted Kremenek8d178f42009-01-27 00:01:05 +0000516 // Get the location of the spelling cache.
Ted Kremenek4c1d41f2009-02-11 23:34:32 +0000517 const unsigned char* spellingBaseOffset = PrologueOffset + sizeof(uint32_t)*3;
Ted Kremenek8d178f42009-01-27 00:01:05 +0000518 const unsigned char* spellingBase = BufBeg + ReadLE32(spellingBaseOffset);
519 if (!(spellingBase >= BufBeg && spellingBase < BufEnd)) {
Daniel Dunbar1a54e3f2009-11-12 02:53:48 +0000520 Diags.Report(diag::err_invalid_pth_file) << file;
Ted Kremenek8d178f42009-01-27 00:01:05 +0000521 return 0;
522 }
Mike Stump11289f42009-09-09 15:08:12 +0000523
Ted Kremenek73a4d282008-12-03 01:16:39 +0000524 // Get the number of IdentifierInfos and pre-allocate the identifier cache.
Chris Lattnerfec54702009-01-22 19:48:26 +0000525 uint32_t NumIds = ReadLE32(IData);
Mike Stump11289f42009-09-09 15:08:12 +0000526
Chris Lattner57540c52011-04-15 05:22:18 +0000527 // Pre-allocate the persistent ID -> IdentifierInfo* cache. We use calloc()
Ted Kremenek73a4d282008-12-03 01:16:39 +0000528 // so that we in the best case only zero out memory once when the OS returns
529 // us new pages.
Ted Kremenek8d6c8282009-01-21 07:34:28 +0000530 IdentifierInfo** PerIDCache = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000531
Ted Kremenek8d6c8282009-01-21 07:34:28 +0000532 if (NumIds) {
Mike Stump11289f42009-09-09 15:08:12 +0000533 PerIDCache = (IdentifierInfo**)calloc(NumIds, sizeof(*PerIDCache));
Ted Kremenek8d6c8282009-01-21 07:34:28 +0000534 if (!PerIDCache) {
Daniel Dunbar1a54e3f2009-11-12 02:53:48 +0000535 InvalidPTH(Diags, "Could not allocate memory for processing PTH file");
Ted Kremenek8d6c8282009-01-21 07:34:28 +0000536 return 0;
537 }
Ted Kremenek73a4d282008-12-03 01:16:39 +0000538 }
Ted Kremenek8d6c8282009-01-21 07:34:28 +0000539
Ted Kremenekf9ccd5c2009-03-19 22:19:30 +0000540 // Compute the address of the original source file.
541 const unsigned char* originalSourceBase = PrologueOffset + sizeof(uint32_t)*4;
542 unsigned len = ReadUnalignedLE16(originalSourceBase);
Mike Stump11289f42009-09-09 15:08:12 +0000543 if (!len) originalSourceBase = 0;
544
Ted Kremeneka705b042009-01-15 18:47:46 +0000545 // Create the new PTHManager.
546 return new PTHManager(File.take(), FL.take(), IData, PerIDCache,
Ted Kremenekf9ccd5c2009-03-19 22:19:30 +0000547 SL.take(), NumIds, spellingBase,
548 (const char*) originalSourceBase);
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000549}
Ted Kremenekf9ccd5c2009-03-19 22:19:30 +0000550
Chris Lattner144aacd2009-01-18 02:57:21 +0000551IdentifierInfo* PTHManager::LazilyCreateIdentifierInfo(unsigned PersistentID) {
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000552 // Look in the PTH file for the string data for the IdentifierInfo object.
Chris Lattner144aacd2009-01-18 02:57:21 +0000553 const unsigned char* TableEntry = IdDataTable + sizeof(uint32_t)*PersistentID;
Chris Lattnereb097542009-01-18 01:57:14 +0000554 const unsigned char* IDData =
Chris Lattnerfec54702009-01-22 19:48:26 +0000555 (const unsigned char*)Buf->getBufferStart() + ReadLE32(TableEntry);
Chris Lattnereb097542009-01-18 01:57:14 +0000556 assert(IDData < (const unsigned char*)Buf->getBufferEnd());
Mike Stump11289f42009-09-09 15:08:12 +0000557
Ted Kremeneka705b042009-01-15 18:47:46 +0000558 // Allocate the object.
Chris Lattnereb097542009-01-18 01:57:14 +0000559 std::pair<IdentifierInfo,const unsigned char*> *Mem =
560 Alloc.Allocate<std::pair<IdentifierInfo,const unsigned char*> >();
Ted Kremeneka705b042009-01-15 18:47:46 +0000561
562 Mem->second = IDData;
Ted Kremeneke5554de2009-02-11 21:29:16 +0000563 assert(IDData[0] != '\0');
Ted Kremenek52f73ca2009-01-20 23:28:34 +0000564 IdentifierInfo *II = new ((void*) Mem) IdentifierInfo();
Mike Stump11289f42009-09-09 15:08:12 +0000565
Ted Kremeneka705b042009-01-15 18:47:46 +0000566 // Store the new IdentifierInfo in the cache.
Chris Lattner144aacd2009-01-18 02:57:21 +0000567 PerIDCache[PersistentID] = II;
Daniel Dunbar2c422dc92009-10-18 20:26:12 +0000568 assert(II->getNameStart() && II->getNameStart()[0] != '\0');
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000569 return II;
570}
571
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000572IdentifierInfo* PTHManager::get(StringRef Name) {
Ted Kremeneke5554de2009-02-11 21:29:16 +0000573 PTHStringIdLookup& SL = *((PTHStringIdLookup*)StringIdLookup);
574 // Double check our assumption that the last character isn't '\0'.
David Blaikie441417a2011-09-22 01:35:49 +0000575 assert(Name.empty() || Name.back() != '\0');
Kovarththanan Rajaratnam661a3092010-03-12 08:23:34 +0000576 PTHStringIdLookup::iterator I = SL.find(std::make_pair(Name.data(),
577 Name.size()));
Ted Kremeneke5554de2009-02-11 21:29:16 +0000578 if (I == SL.end()) // No identifier found?
579 return 0;
Ted Kremeneka705b042009-01-15 18:47:46 +0000580
Ted Kremeneke5554de2009-02-11 21:29:16 +0000581 // Match found. Return the identifier!
582 assert(*I > 0);
583 return GetIdentifierInfo(*I-1);
584}
Ted Kremeneka705b042009-01-15 18:47:46 +0000585
Chris Lattnerab1d4b82009-01-17 08:06:50 +0000586PTHLexer *PTHManager::CreateLexer(FileID FID) {
587 const FileEntry *FE = PP->getSourceManager().getFileEntryForID(FID);
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000588 if (!FE)
589 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000590
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000591 // Lookup the FileEntry object in our file lookup data structure. It will
592 // return a variant that indicates whether or not there is an offset within
593 // the PTH file that contains cached tokens.
Ted Kremenek86423a92009-02-10 22:16:22 +0000594 PTHFileLookup& PFL = *((PTHFileLookup*)FileLookup);
595 PTHFileLookup::iterator I = PFL.find(FE);
Mike Stump11289f42009-09-09 15:08:12 +0000596
Ted Kremenek86423a92009-02-10 22:16:22 +0000597 if (I == PFL.end()) // No tokens available?
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000598 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000599
600 const PTHFileData& FileData = *I;
601
Chris Lattnereb097542009-01-18 01:57:14 +0000602 const unsigned char *BufStart = (const unsigned char *)Buf->getBufferStart();
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000603 // Compute the offset of the token data within the buffer.
Chris Lattnereb097542009-01-18 01:57:14 +0000604 const unsigned char* data = BufStart + FileData.getTokenOffset();
Ted Kremenek56572ab2008-12-12 18:34:08 +0000605
606 // Get the location of pp-conditional table.
Chris Lattnereb097542009-01-18 01:57:14 +0000607 const unsigned char* ppcond = BufStart + FileData.getPPCondOffset();
Chris Lattnerfec54702009-01-22 19:48:26 +0000608 uint32_t Len = ReadLE32(ppcond);
Chris Lattner137d6492009-01-18 02:10:31 +0000609 if (Len == 0) ppcond = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000610
Ted Kremeneka705b042009-01-15 18:47:46 +0000611 assert(PP && "No preprocessor set yet!");
Mike Stump11289f42009-09-09 15:08:12 +0000612 return new PTHLexer(*PP, FID, data, ppcond, *this);
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000613}
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000614
615//===----------------------------------------------------------------------===//
616// 'stat' caching.
617//===----------------------------------------------------------------------===//
618
619namespace {
Benjamin Kramer337e3a52009-11-28 19:45:26 +0000620class PTHStatData {
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000621public:
Ted Kremenek29942a32009-02-13 19:13:46 +0000622 const bool hasStat;
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000623 const ino_t ino;
624 const dev_t dev;
625 const mode_t mode;
626 const time_t mtime;
627 const off_t size;
Mike Stump11289f42009-09-09 15:08:12 +0000628
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000629 PTHStatData(ino_t i, dev_t d, mode_t mo, time_t m, off_t s)
Mike Stump11289f42009-09-09 15:08:12 +0000630 : hasStat(true), ino(i), dev(d), mode(mo), mtime(m), size(s) {}
631
Ted Kremenek29942a32009-02-13 19:13:46 +0000632 PTHStatData()
633 : hasStat(false), ino(0), dev(0), mode(0), mtime(0), size(0) {}
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000634};
Mike Stump11289f42009-09-09 15:08:12 +0000635
Benjamin Kramer337e3a52009-11-28 19:45:26 +0000636class PTHStatLookupTrait : public PTHFileLookupCommonTrait {
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000637public:
Ted Kremenek29942a32009-02-13 19:13:46 +0000638 typedef const char* external_key_type; // const char*
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000639 typedef PTHStatData data_type;
Mike Stump11289f42009-09-09 15:08:12 +0000640
Ted Kremenek29942a32009-02-13 19:13:46 +0000641 static internal_key_type GetInternalKey(const char *path) {
642 // The key 'kind' doesn't matter here because it is ignored in EqualKey.
643 return std::make_pair((unsigned char) 0x0, path);
644 }
645
646 static bool EqualKey(internal_key_type a, internal_key_type b) {
647 // When doing 'stat' lookups we don't care about the kind of 'a' and 'b',
648 // just the paths.
649 return strcmp(a.second, b.second) == 0;
Mike Stump11289f42009-09-09 15:08:12 +0000650 }
651
Ted Kremenek29942a32009-02-13 19:13:46 +0000652 static data_type ReadData(const internal_key_type& k, const unsigned char* d,
Mike Stump11289f42009-09-09 15:08:12 +0000653 unsigned) {
654
Ted Kremenek2fd18ec2009-02-13 22:07:44 +0000655 if (k.first /* File or Directory */) {
656 if (k.first == 0x1 /* File */) d += 4 * 2; // Skip the first 2 words.
Ted Kremenek29942a32009-02-13 19:13:46 +0000657 ino_t ino = (ino_t) ReadUnalignedLE32(d);
658 dev_t dev = (dev_t) ReadUnalignedLE32(d);
659 mode_t mode = (mode_t) ReadUnalignedLE16(d);
Mike Stump11289f42009-09-09 15:08:12 +0000660 time_t mtime = (time_t) ReadUnalignedLE64(d);
Ted Kremenek29942a32009-02-13 19:13:46 +0000661 return data_type(ino, dev, mode, mtime, (off_t) ReadUnalignedLE64(d));
662 }
Ted Kremenek2fd18ec2009-02-13 22:07:44 +0000663
664 // Negative stat. Don't read anything.
Ted Kremenek29942a32009-02-13 19:13:46 +0000665 return data_type();
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000666 }
667};
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000668
Chris Lattner226efd32010-11-23 19:19:34 +0000669class PTHStatCache : public FileSystemStatCache {
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000670 typedef OnDiskChainedHashTable<PTHStatLookupTrait> CacheTy;
671 CacheTy Cache;
672
Mike Stump11289f42009-09-09 15:08:12 +0000673public:
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000674 PTHStatCache(PTHFileLookup &FL) :
675 Cache(FL.getNumBuckets(), FL.getNumEntries(), FL.getBuckets(),
676 FL.getBase()) {}
677
678 ~PTHStatCache() {}
Mike Stump11289f42009-09-09 15:08:12 +0000679
Chris Lattnerdd278432010-11-23 21:17:56 +0000680 LookupResult getStat(const char *Path, struct stat &StatBuf,
Argyrios Kyrtzidis3b779372012-12-11 07:48:23 +0000681 bool isFile, int *FileDescriptor) {
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000682 // Do the lookup for the file's data in the PTH file.
Chris Lattner226efd32010-11-23 19:19:34 +0000683 CacheTy::iterator I = Cache.find(Path);
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000684
685 // If we don't get a hit in the PTH file just forward to 'stat'.
Kovarththanan Rajaratnam661a3092010-03-12 08:23:34 +0000686 if (I == Cache.end())
Argyrios Kyrtzidis3b779372012-12-11 07:48:23 +0000687 return statChained(Path, StatBuf, isFile, FileDescriptor);
Mike Stump11289f42009-09-09 15:08:12 +0000688
Chris Lattner226efd32010-11-23 19:19:34 +0000689 const PTHStatData &Data = *I;
Mike Stump11289f42009-09-09 15:08:12 +0000690
Ted Kremenek2fd18ec2009-02-13 22:07:44 +0000691 if (!Data.hasStat)
Chris Lattner8f0583d2010-11-23 20:05:15 +0000692 return CacheMissing;
Ted Kremenek2fd18ec2009-02-13 22:07:44 +0000693
Chris Lattner226efd32010-11-23 19:19:34 +0000694 StatBuf.st_ino = Data.ino;
695 StatBuf.st_dev = Data.dev;
696 StatBuf.st_mtime = Data.mtime;
697 StatBuf.st_mode = Data.mode;
698 StatBuf.st_size = Data.size;
Chris Lattner8f0583d2010-11-23 20:05:15 +0000699 return CacheExists;
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000700 }
701};
Ted Kremenek29b86972009-02-23 23:27:54 +0000702} // end anonymous namespace
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000703
Chris Lattner226efd32010-11-23 19:19:34 +0000704FileSystemStatCache *PTHManager::createStatCache() {
Ted Kremenekb4c85cc2009-02-12 03:45:39 +0000705 return new PTHStatCache(*((PTHFileLookup*) FileLookup));
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000706}