blob: 3fd4b55bbdf10c714f06689adc80620c31a2976d [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
Ted Kremenek33eeabd2008-12-03 00:38:03 +000014#include "clang/Basic/TokenKinds.h"
15#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"
Daniel Dunbar1a54e3f2009-11-12 02:53:48 +000019#include "clang/Lex/LexDiagnostic.h"
Ted Kremenek7cd62452008-11-12 21:37:15 +000020#include "clang/Lex/PTHLexer.h"
21#include "clang/Lex/Preprocessor.h"
Ted Kremenek33eeabd2008-12-03 00:38:03 +000022#include "clang/Lex/PTHManager.h"
23#include "clang/Lex/Token.h"
24#include "clang/Lex/Preprocessor.h"
Ted Kremenek33eeabd2008-12-03 00:38:03 +000025#include "llvm/ADT/OwningPtr.h"
Daniel Dunbarf8502d52009-10-17 23:52:28 +000026#include "llvm/ADT/StringExtras.h"
27#include "llvm/ADT/StringMap.h"
Chris Lattner34eab392009-01-22 23:50:07 +000028#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencerf25faaa2010-12-09 17:36:38 +000029#include "llvm/Support/system_error.h"
Ted Kremenek7cd62452008-11-12 21:37:15 +000030using namespace clang;
Douglas Gregor52289d32009-04-20 07:08:21 +000031using namespace clang::io;
Ted Kremenek7cd62452008-11-12 21:37:15 +000032
Ted Kremenek8433f0b2009-01-19 23:13:15 +000033#define DISK_TOKEN_SIZE (1+1+2+4+4)
Ted Kremenek56572ab2008-12-12 18:34:08 +000034
Ted Kremenek33eeabd2008-12-03 00:38:03 +000035//===----------------------------------------------------------------------===//
Ted Kremenek1b18ad22008-12-23 01:30:52 +000036// PTHLexer methods.
37//===----------------------------------------------------------------------===//
38
Chris Lattnereb097542009-01-18 01:57:14 +000039PTHLexer::PTHLexer(Preprocessor &PP, FileID FID, const unsigned char *D,
Ted Kremenek8d178f42009-01-27 00:01:05 +000040 const unsigned char *ppcond, PTHManager &PM)
Chris Lattnerd32480d2009-01-17 06:22:33 +000041 : PreprocessorLexer(&PP, FID), TokBuf(D), CurPtr(D), LastHashTokPtr(0),
Ted Kremenek8d178f42009-01-27 00:01:05 +000042 PPCond(ppcond), CurPPCondPtr(ppcond), PTHMgr(PM) {
Mike Stump11289f42009-09-09 15:08:12 +000043
Chris Lattnerd32480d2009-01-17 06:22:33 +000044 FileStartLoc = PP.getSourceManager().getLocForStartOfFile(FID);
Ted Kremenek47b8cf62009-01-09 22:05:30 +000045}
Ted Kremenek1b18ad22008-12-23 01:30:52 +000046
47void PTHLexer::Lex(Token& Tok) {
48LexNextToken:
Ted Kremenek1b18ad22008-12-23 01:30:52 +000049
Ted Kremenek66076a92008-12-23 02:30:15 +000050 //===--------------------------------------==//
51 // Read the raw token data.
52 //===--------------------------------------==//
Mike Stump11289f42009-09-09 15:08:12 +000053
Ted Kremenek66076a92008-12-23 02:30:15 +000054 // Shadow CurPtr into an automatic variable.
Mike Stump11289f42009-09-09 15:08:12 +000055 const unsigned char *CurPtrShadow = CurPtr;
Ted Kremenek66076a92008-12-23 02:30:15 +000056
Chris Lattner137d6492009-01-18 02:10:31 +000057 // Read in the data for the token.
Chris Lattnerfec54702009-01-22 19:48:26 +000058 unsigned Word0 = ReadLE32(CurPtrShadow);
59 uint32_t IdentifierID = ReadLE32(CurPtrShadow);
60 uint32_t FileOffset = ReadLE32(CurPtrShadow);
Mike Stump11289f42009-09-09 15:08:12 +000061
Ted Kremenek8433f0b2009-01-19 23:13:15 +000062 tok::TokenKind TKind = (tok::TokenKind) (Word0 & 0xFF);
63 Token::TokenFlags TFlags = (Token::TokenFlags) ((Word0 >> 8) & 0xFF);
Chris Lattner47def972009-01-21 07:21:56 +000064 uint32_t Len = Word0 >> 16;
Ted Kremenek8433f0b2009-01-19 23:13:15 +000065
Chris Lattner47def972009-01-21 07:21:56 +000066 CurPtr = CurPtrShadow;
Mike Stump11289f42009-09-09 15:08:12 +000067
Ted Kremenek66076a92008-12-23 02:30:15 +000068 //===--------------------------------------==//
69 // Construct the token itself.
70 //===--------------------------------------==//
Mike Stump11289f42009-09-09 15:08:12 +000071
Ted Kremenek66076a92008-12-23 02:30:15 +000072 Tok.startToken();
Chris Lattner18fc6ce2009-01-18 02:34:01 +000073 Tok.setKind(TKind);
74 Tok.setFlag(TFlags);
Ted Kremenek78cc2472008-12-23 19:24:24 +000075 assert(!LexingRawMode);
Argyrios Kyrtzidise6e67de2011-09-19 20:40:19 +000076 Tok.setLocation(FileStartLoc.getLocWithOffset(FileOffset));
Ted Kremenek66076a92008-12-23 02:30:15 +000077 Tok.setLength(Len);
78
Chris Lattner3029b352009-01-21 07:50:06 +000079 // Handle identifiers.
Ted Kremenek8d178f42009-01-27 00:01:05 +000080 if (Tok.isLiteral()) {
81 Tok.setLiteralData((const char*) (PTHMgr.SpellingBase + IdentifierID));
82 }
83 else if (IdentifierID) {
Chris Lattner3029b352009-01-21 07:50:06 +000084 MIOpt.ReadToken();
85 IdentifierInfo *II = PTHMgr.GetIdentifierInfo(IdentifierID-1);
Mike Stump11289f42009-09-09 15:08:12 +000086
Chris Lattner3029b352009-01-21 07:50:06 +000087 Tok.setIdentifierInfo(II);
Mike Stump11289f42009-09-09 15:08:12 +000088
Chris Lattner1f6c7fe2009-01-23 18:35:48 +000089 // Change the kind of this identifier to the appropriate token kind, e.g.
90 // turning "for" into a keyword.
91 Tok.setKind(II->getTokenID());
Mike Stump11289f42009-09-09 15:08:12 +000092
Chris Lattner3029b352009-01-21 07:50:06 +000093 if (II->isHandleIdentifierCase())
94 PP->HandleIdentifier(Tok);
95 return;
96 }
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 Kremenek2bd41d12010-07-27 02:59:02 +0000105 // Save 'PP' to 'PPCache' as LexEndOfFile can delete 'this'.
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000106 Preprocessor *PPCache = PP;
Mike Stump11289f42009-09-09 15:08:12 +0000107
Ted Kremenek78cc2472008-12-23 19:24:24 +0000108 assert(!ParsingPreprocessorDirective);
109 assert(!LexingRawMode);
Ted Kremenek2bd41d12010-07-27 02:59:02 +0000110
111 if (LexEndOfFile(Tok))
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000112 return;
Mike Stump11289f42009-09-09 15:08:12 +0000113
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000114 return PPCache->Lex(Tok);
115 }
Mike Stump11289f42009-09-09 15:08:12 +0000116
Chris Lattner18fc6ce2009-01-18 02:34:01 +0000117 if (TKind == tok::hash && Tok.isAtStartOfLine()) {
Ted Kremenek78cc2472008-12-23 19:24:24 +0000118 LastHashTokPtr = CurPtr - DISK_TOKEN_SIZE;
119 assert(!LexingRawMode);
120 PP->HandleDirective(Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000121
Ted Kremenek78cc2472008-12-23 19:24:24 +0000122 if (PP->isCurrentLexer(this))
123 goto LexNextToken;
Mike Stump11289f42009-09-09 15:08:12 +0000124
Ted Kremenek78cc2472008-12-23 19:24:24 +0000125 return PP->Lex(Tok);
126 }
Mike Stump11289f42009-09-09 15:08:12 +0000127
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000128 if (TKind == tok::eod) {
Ted Kremenek78cc2472008-12-23 19:24:24 +0000129 assert(ParsingPreprocessorDirective);
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000130 ParsingPreprocessorDirective = false;
131 return;
132 }
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000133
Ted Kremenek78cc2472008-12-23 19:24:24 +0000134 MIOpt.ReadToken();
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000135}
136
Ted Kremenek2bd41d12010-07-27 02:59:02 +0000137bool PTHLexer::LexEndOfFile(Token &Result) {
138 // If we hit the end of the file while parsing a preprocessor directive,
139 // end the preprocessor directive first. The next token returned will
140 // then be the end of file.
141 if (ParsingPreprocessorDirective) {
142 ParsingPreprocessorDirective = false; // Done parsing the "line".
143 return true; // Have a token.
144 }
145
146 assert(!LexingRawMode);
147
148 // If we are in a #if directive, emit an error.
149 while (!ConditionalStack.empty()) {
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000150 if (PP->getCodeCompletionFileLoc() != FileStartLoc)
Douglas Gregor02690ba2010-08-12 17:04:55 +0000151 PP->Diag(ConditionalStack.back().IfLoc,
152 diag::err_pp_unterminated_conditional);
Ted Kremenek2bd41d12010-07-27 02:59:02 +0000153 ConditionalStack.pop_back();
154 }
155
156 // Finally, let the preprocessor handle this.
157 return PP->HandleEndOfFile(Result);
158}
159
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000160// FIXME: We can just grab the last token instead of storing a copy
161// into EofToken.
Ted Kremenek78cc2472008-12-23 19:24:24 +0000162void PTHLexer::getEOF(Token& Tok) {
Ted Kremenek8ae06622009-01-09 00:36:11 +0000163 assert(EofToken.is(tok::eof));
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000164 Tok = EofToken;
165}
166
167void PTHLexer::DiscardToEndOfLine() {
168 assert(ParsingPreprocessorDirective && ParsingFilename == false &&
169 "Must be in a preprocessing directive!");
170
171 // We assume that if the preprocessor wishes to discard to the end of
172 // the line that it also means to end the current preprocessor directive.
173 ParsingPreprocessorDirective = false;
Mike Stump11289f42009-09-09 15:08:12 +0000174
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000175 // Skip tokens by only peeking at their token kind and the flags.
176 // We don't need to actually reconstruct full tokens from the token buffer.
177 // This saves some copies and it also reduces IdentifierInfo* lookup.
Chris Lattnereb097542009-01-18 01:57:14 +0000178 const unsigned char* p = CurPtr;
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000179 while (1) {
180 // Read the token kind. Are we at the end of the file?
181 tok::TokenKind x = (tok::TokenKind) (uint8_t) *p;
182 if (x == tok::eof) break;
Mike Stump11289f42009-09-09 15:08:12 +0000183
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000184 // Read the token flags. Are we at the start of the next line?
185 Token::TokenFlags y = (Token::TokenFlags) (uint8_t) p[1];
186 if (y & Token::StartOfLine) break;
187
188 // Skip to the next token.
189 p += DISK_TOKEN_SIZE;
190 }
Mike Stump11289f42009-09-09 15:08:12 +0000191
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000192 CurPtr = p;
193}
194
Ted Kremenek56572ab2008-12-12 18:34:08 +0000195/// SkipBlock - Used by Preprocessor to skip the current conditional block.
196bool PTHLexer::SkipBlock() {
197 assert(CurPPCondPtr && "No cached PP conditional information.");
198 assert(LastHashTokPtr && "No known '#' token.");
Mike Stump11289f42009-09-09 15:08:12 +0000199
Chris Lattnereb097542009-01-18 01:57:14 +0000200 const unsigned char* HashEntryI = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000201 uint32_t Offset;
Ted Kremenek56572ab2008-12-12 18:34:08 +0000202 uint32_t TableIdx;
Mike Stump11289f42009-09-09 15:08:12 +0000203
Ted Kremenek56572ab2008-12-12 18:34:08 +0000204 do {
Ted Kremenek877556f2008-12-12 22:05:38 +0000205 // Read the token offset from the side-table.
Chris Lattnerfec54702009-01-22 19:48:26 +0000206 Offset = ReadLE32(CurPPCondPtr);
Mike Stump11289f42009-09-09 15:08:12 +0000207
208 // Read the target table index from the side-table.
Chris Lattnerfec54702009-01-22 19:48:26 +0000209 TableIdx = ReadLE32(CurPPCondPtr);
Mike Stump11289f42009-09-09 15:08:12 +0000210
Ted Kremenek877556f2008-12-12 22:05:38 +0000211 // Compute the actual memory address of the '#' token data for this entry.
212 HashEntryI = TokBuf + Offset;
213
214 // Optmization: "Sibling jumping". #if...#else...#endif blocks can
215 // contain nested blocks. In the side-table we can jump over these
216 // nested blocks instead of doing a linear search if the next "sibling"
217 // entry is not at a location greater than LastHashTokPtr.
218 if (HashEntryI < LastHashTokPtr && TableIdx) {
219 // In the side-table we are still at an entry for a '#' token that
220 // is earlier than the last one we saw. Check if the location we would
221 // stride gets us closer.
Chris Lattnereb097542009-01-18 01:57:14 +0000222 const unsigned char* NextPPCondPtr =
223 PPCond + TableIdx*(sizeof(uint32_t)*2);
Ted Kremenek877556f2008-12-12 22:05:38 +0000224 assert(NextPPCondPtr >= CurPPCondPtr);
225 // Read where we should jump to.
Chris Lattnerfec54702009-01-22 19:48:26 +0000226 uint32_t TmpOffset = ReadLE32(NextPPCondPtr);
Chris Lattnereb097542009-01-18 01:57:14 +0000227 const unsigned char* HashEntryJ = TokBuf + TmpOffset;
Mike Stump11289f42009-09-09 15:08:12 +0000228
Ted Kremenek877556f2008-12-12 22:05:38 +0000229 if (HashEntryJ <= LastHashTokPtr) {
230 // Jump directly to the next entry in the side table.
231 HashEntryI = HashEntryJ;
232 Offset = TmpOffset;
Chris Lattnerfec54702009-01-22 19:48:26 +0000233 TableIdx = ReadLE32(NextPPCondPtr);
Ted Kremenek877556f2008-12-12 22:05:38 +0000234 CurPPCondPtr = NextPPCondPtr;
235 }
236 }
Ted Kremenek56572ab2008-12-12 18:34:08 +0000237 }
Mike Stump11289f42009-09-09 15:08:12 +0000238 while (HashEntryI < LastHashTokPtr);
Ted Kremenek877556f2008-12-12 22:05:38 +0000239 assert(HashEntryI == LastHashTokPtr && "No PP-cond entry found for '#'");
Ted Kremenek56572ab2008-12-12 18:34:08 +0000240 assert(TableIdx && "No jumping from #endifs.");
Mike Stump11289f42009-09-09 15:08:12 +0000241
Ted Kremenek56572ab2008-12-12 18:34:08 +0000242 // Update our side-table iterator.
Chris Lattnereb097542009-01-18 01:57:14 +0000243 const unsigned char* NextPPCondPtr = PPCond + TableIdx*(sizeof(uint32_t)*2);
Ted Kremenek56572ab2008-12-12 18:34:08 +0000244 assert(NextPPCondPtr >= CurPPCondPtr);
245 CurPPCondPtr = NextPPCondPtr;
Mike Stump11289f42009-09-09 15:08:12 +0000246
Ted Kremenek56572ab2008-12-12 18:34:08 +0000247 // Read where we should jump to.
Chris Lattnerfec54702009-01-22 19:48:26 +0000248 HashEntryI = TokBuf + ReadLE32(NextPPCondPtr);
249 uint32_t NextIdx = ReadLE32(NextPPCondPtr);
Mike Stump11289f42009-09-09 15:08:12 +0000250
Ted Kremenek56572ab2008-12-12 18:34:08 +0000251 // By construction NextIdx will be zero if this is a #endif. This is useful
252 // to know to obviate lexing another token.
253 bool isEndif = NextIdx == 0;
Mike Stump11289f42009-09-09 15:08:12 +0000254
Ted Kremenek56572ab2008-12-12 18:34:08 +0000255 // This case can occur when we see something like this:
256 //
257 // #if ...
258 // /* a comment or nothing */
259 // #elif
260 //
261 // If we are skipping the first #if block it will be the case that CurPtr
262 // already points 'elif'. Just return.
Mike Stump11289f42009-09-09 15:08:12 +0000263
Ted Kremenek877556f2008-12-12 22:05:38 +0000264 if (CurPtr > HashEntryI) {
265 assert(CurPtr == HashEntryI + DISK_TOKEN_SIZE);
Ted Kremenek56572ab2008-12-12 18:34:08 +0000266 // Did we reach a #endif? If so, go ahead and consume that token as well.
267 if (isEndif)
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000268 CurPtr += DISK_TOKEN_SIZE*2;
Ted Kremenek56572ab2008-12-12 18:34:08 +0000269 else
Ted Kremenek877556f2008-12-12 22:05:38 +0000270 LastHashTokPtr = HashEntryI;
Mike Stump11289f42009-09-09 15:08:12 +0000271
Ted Kremenek56572ab2008-12-12 18:34:08 +0000272 return isEndif;
273 }
274
275 // Otherwise, we need to advance. Update CurPtr to point to the '#' token.
Ted Kremenek877556f2008-12-12 22:05:38 +0000276 CurPtr = HashEntryI;
Mike Stump11289f42009-09-09 15:08:12 +0000277
Ted Kremenek56572ab2008-12-12 18:34:08 +0000278 // Update the location of the last observed '#'. This is useful if we
279 // are skipping multiple blocks.
280 LastHashTokPtr = CurPtr;
Ted Kremenek56572ab2008-12-12 18:34:08 +0000281
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000282 // Skip the '#' token.
Chris Lattnereb097542009-01-18 01:57:14 +0000283 assert(((tok::TokenKind)*CurPtr) == tok::hash);
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000284 CurPtr += DISK_TOKEN_SIZE;
Mike Stump11289f42009-09-09 15:08:12 +0000285
Ted Kremenek56572ab2008-12-12 18:34:08 +0000286 // Did we reach a #endif? If so, go ahead and consume that token as well.
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000287 if (isEndif) { CurPtr += DISK_TOKEN_SIZE*2; }
Ted Kremenek56572ab2008-12-12 18:34:08 +0000288
289 return isEndif;
290}
291
Ted Kremenek63ff81c2008-12-17 23:36:32 +0000292SourceLocation PTHLexer::getSourceLocation() {
Chris Lattner137d6492009-01-18 02:10:31 +0000293 // getSourceLocation is not on the hot path. It is used to get the location
294 // of the next token when transitioning back to this lexer when done
Ted Kremenek63ff81c2008-12-17 23:36:32 +0000295 // handling a #included file. Just read the necessary data from the token
296 // data buffer to construct the SourceLocation object.
297 // NOTE: This is a virtual function; hence it is defined out-of-line.
Ted Kremenekae54f2f2009-01-21 22:41:38 +0000298 const unsigned char *OffsetPtr = CurPtr + (DISK_TOKEN_SIZE - 4);
Chris Lattnerfec54702009-01-22 19:48:26 +0000299 uint32_t Offset = ReadLE32(OffsetPtr);
Argyrios Kyrtzidise6e67de2011-09-19 20:40:19 +0000300 return FileStartLoc.getLocWithOffset(Offset);
Ted Kremenek63ff81c2008-12-17 23:36:32 +0000301}
302
Ted Kremenek47b8cf62009-01-09 22:05:30 +0000303//===----------------------------------------------------------------------===//
Ted Kremenek86423a92009-02-10 22:16:22 +0000304// PTH file lookup: map from strings to file data.
305//===----------------------------------------------------------------------===//
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000306
307/// PTHFileLookup - This internal data structure is used by the PTHManager
308/// to map from FileEntry objects managed by FileManager to offsets within
309/// the PTH file.
310namespace {
Benjamin Kramer337e3a52009-11-28 19:45:26 +0000311class PTHFileData {
Ted Kremenek86423a92009-02-10 22:16:22 +0000312 const uint32_t TokenOff;
313 const uint32_t PPCondOff;
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000314public:
Ted Kremenek86423a92009-02-10 22:16:22 +0000315 PTHFileData(uint32_t tokenOff, uint32_t ppCondOff)
316 : TokenOff(tokenOff), PPCondOff(ppCondOff) {}
Mike Stump11289f42009-09-09 15:08:12 +0000317
318 uint32_t getTokenOffset() const { return TokenOff; }
319 uint32_t getPPCondOffset() const { return PPCondOff; }
Ted Kremenek86423a92009-02-10 22:16:22 +0000320};
Mike Stump11289f42009-09-09 15:08:12 +0000321
322
Benjamin Kramer337e3a52009-11-28 19:45:26 +0000323class PTHFileLookupCommonTrait {
Ted Kremenek86423a92009-02-10 22:16:22 +0000324public:
Ted Kremenek29942a32009-02-13 19:13:46 +0000325 typedef std::pair<unsigned char, const char*> internal_key_type;
326
327 static unsigned ComputeHash(internal_key_type x) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +0000328 return llvm::HashString(x.second);
Ted Kremenek86423a92009-02-10 22:16:22 +0000329 }
Mike Stump11289f42009-09-09 15:08:12 +0000330
Ted Kremenek86423a92009-02-10 22:16:22 +0000331 static std::pair<unsigned, unsigned>
332 ReadKeyDataLength(const unsigned char*& d) {
Ted Kremenek29942a32009-02-13 19:13:46 +0000333 unsigned keyLen = (unsigned) ReadUnalignedLE16(d);
334 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
Benjamin Kramer337e3a52009-11-28 19:45:26 +0000344class PTHFileLookupTrait : public PTHFileLookupCommonTrait {
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000345public:
346 typedef const FileEntry* external_key_type;
347 typedef PTHFileData data_type;
Mike Stump11289f42009-09-09 15:08:12 +0000348
Ted Kremenek29942a32009-02-13 19:13:46 +0000349 static internal_key_type GetInternalKey(const FileEntry* FE) {
350 return std::make_pair((unsigned char) 0x1, FE->getName());
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000351 }
Ted Kremenek29942a32009-02-13 19:13:46 +0000352
353 static bool EqualKey(internal_key_type a, internal_key_type b) {
354 return a.first == b.first && strcmp(a.second, b.second) == 0;
Mike Stump11289f42009-09-09 15:08:12 +0000355 }
356
357 static PTHFileData ReadData(const internal_key_type& k,
358 const unsigned char* d, unsigned) {
Ted Kremenek29942a32009-02-13 19:13:46 +0000359 assert(k.first == 0x1 && "Only file lookups can match!");
Ted Kremenek86423a92009-02-10 22:16:22 +0000360 uint32_t x = ::ReadUnalignedLE32(d);
361 uint32_t y = ::ReadUnalignedLE32(d);
Mike Stump11289f42009-09-09 15:08:12 +0000362 return PTHFileData(x, y);
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000363 }
364};
Ted Kremeneke5554de2009-02-11 21:29:16 +0000365
Benjamin Kramer337e3a52009-11-28 19:45:26 +0000366class PTHStringLookupTrait {
Ted Kremeneke5554de2009-02-11 21:29:16 +0000367public:
Mike Stump11289f42009-09-09 15:08:12 +0000368 typedef uint32_t
Ted Kremeneke5554de2009-02-11 21:29:16 +0000369 data_type;
370
371 typedef const std::pair<const char*, unsigned>
372 external_key_type;
373
374 typedef external_key_type internal_key_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
Ted Kremeneke5554de2009-02-11 21:29:16 +0000382 static unsigned 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) {
392 return std::make_pair((unsigned) ReadUnalignedLE16(d), sizeof(uint32_t));
393 }
Mike Stump11289f42009-09-09 15:08:12 +0000394
Ted Kremeneke5554de2009-02-11 21:29:16 +0000395 static std::pair<const char*, unsigned>
396 ReadKey(const unsigned char* d, unsigned n) {
397 assert(n >= 2 && d[n-1] == '\0');
398 return std::make_pair((const char*) d, n-1);
399 }
Mike Stump11289f42009-09-09 15:08:12 +0000400
Ted Kremenek29942a32009-02-13 19:13:46 +0000401 static uint32_t ReadData(const internal_key_type& k, const unsigned char* d,
402 unsigned) {
Ted Kremeneke5554de2009-02-11 21:29:16 +0000403 return ::ReadUnalignedLE32(d);
404 }
405};
Mike Stump11289f42009-09-09 15:08:12 +0000406
407} // end anonymous namespace
Ted Kremenek86423a92009-02-10 22:16:22 +0000408
Ted Kremeneke5554de2009-02-11 21:29:16 +0000409typedef OnDiskChainedHashTable<PTHFileLookupTrait> PTHFileLookup;
410typedef OnDiskChainedHashTable<PTHStringLookupTrait> PTHStringIdLookup;
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000411
412//===----------------------------------------------------------------------===//
413// PTHManager methods.
414//===----------------------------------------------------------------------===//
415
416PTHManager::PTHManager(const llvm::MemoryBuffer* buf, void* fileLookup,
Chris Lattnereb097542009-01-18 01:57:14 +0000417 const unsigned char* idDataTable,
Mike Stump11289f42009-09-09 15:08:12 +0000418 IdentifierInfo** perIDCache,
Ted Kremeneke5554de2009-02-11 21:29:16 +0000419 void* stringIdLookup, unsigned numIds,
Ted Kremenekf9ccd5c2009-03-19 22:19:30 +0000420 const unsigned char* spellingBase,
421 const char* originalSourceFile)
Ted Kremenek73a4d282008-12-03 01:16:39 +0000422: Buf(buf), PerIDCache(perIDCache), FileLookup(fileLookup),
Ted Kremeneke5554de2009-02-11 21:29:16 +0000423 IdDataTable(idDataTable), StringIdLookup(stringIdLookup),
Ted Kremenekf9ccd5c2009-03-19 22:19:30 +0000424 NumIds(numIds), PP(0), SpellingBase(spellingBase),
425 OriginalSourceFile(originalSourceFile) {}
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000426
427PTHManager::~PTHManager() {
428 delete Buf;
429 delete (PTHFileLookup*) FileLookup;
Ted Kremeneke5554de2009-02-11 21:29:16 +0000430 delete (PTHStringIdLookup*) StringIdLookup;
Ted Kremenek1aed3dd2008-12-04 22:47:11 +0000431 free(PerIDCache);
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000432}
433
Daniel Dunbar1a54e3f2009-11-12 02:53:48 +0000434static void InvalidPTH(Diagnostic &Diags, const char *Msg) {
435 Diags.Report(Diags.getCustomDiagID(Diagnostic::Error, Msg));
Ted Kremenek62224c12009-01-28 21:02:43 +0000436}
437
Chris Lattner7219a5d2010-11-23 09:01:31 +0000438PTHManager *PTHManager::Create(const std::string &file, Diagnostic &Diags) {
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000439 // Memory map the PTH file.
Michael J. Spencerd9da7a12010-12-16 03:28:14 +0000440 llvm::OwningPtr<llvm::MemoryBuffer> File;
Mike Stump11289f42009-09-09 15:08:12 +0000441
Michael J. Spencerd9da7a12010-12-16 03:28:14 +0000442 if (llvm::MemoryBuffer::getFile(file, File)) {
Michael J. Spencerf25faaa2010-12-09 17:36:38 +0000443 // FIXME: Add ec.message() to this diag.
Daniel Dunbar1a54e3f2009-11-12 02:53:48 +0000444 Diags.Report(diag::err_invalid_pth_file) << file;
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000445 return 0;
Ted Kremenek3b0589e2009-01-28 20:49:33 +0000446 }
Mike Stump11289f42009-09-09 15:08:12 +0000447
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000448 // Get the buffer ranges and check if there are at least three 32-bit
449 // words at the end of the file.
Chris Lattner7c434b62010-11-23 04:40:26 +0000450 const unsigned char *BufBeg = (unsigned char*)File->getBufferStart();
451 const unsigned char *BufEnd = (unsigned char*)File->getBufferEnd();
Ted Kremenekeb8c8fb2009-01-26 21:43:14 +0000452
453 // Check the prologue of the file.
Chris Lattner7c434b62010-11-23 04:40:26 +0000454 if ((BufEnd - BufBeg) < (signed)(sizeof("cfe-pth") + 3 + 4) ||
Ted Kremenek62224c12009-01-28 21:02:43 +0000455 memcmp(BufBeg, "cfe-pth", sizeof("cfe-pth") - 1) != 0) {
Daniel Dunbar1a54e3f2009-11-12 02:53:48 +0000456 Diags.Report(diag::err_invalid_pth_file) << file;
Ted Kremenekeb8c8fb2009-01-26 21:43:14 +0000457 return 0;
Ted Kremenek62224c12009-01-28 21:02:43 +0000458 }
Mike Stump11289f42009-09-09 15:08:12 +0000459
Ted Kremenek978b5be2009-01-26 21:50:21 +0000460 // Read the PTH version.
Ted Kremenekeb8c8fb2009-01-26 21:43:14 +0000461 const unsigned char *p = BufBeg + (sizeof("cfe-pth") - 1);
Ted Kremenek978b5be2009-01-26 21:50:21 +0000462 unsigned Version = ReadLE32(p);
Mike Stump11289f42009-09-09 15:08:12 +0000463
Daniel Dunbar1a54e3f2009-11-12 02:53:48 +0000464 if (Version < PTHManager::Version) {
465 InvalidPTH(Diags,
Mike Stump11289f42009-09-09 15:08:12 +0000466 Version < PTHManager::Version
Ted Kremenek62224c12009-01-28 21:02:43 +0000467 ? "PTH file uses an older PTH format that is no longer supported"
468 : "PTH file uses a newer PTH format that cannot be read");
Ted Kremenek978b5be2009-01-26 21:50:21 +0000469 return 0;
Ted Kremenek62224c12009-01-28 21:02:43 +0000470 }
Ted Kremenek978b5be2009-01-26 21:50:21 +0000471
Mike Stump11289f42009-09-09 15:08:12 +0000472 // Compute the address of the index table at the end of the PTH file.
Ted Kremenek4c1d41f2009-02-11 23:34:32 +0000473 const unsigned char *PrologueOffset = p;
Mike Stump11289f42009-09-09 15:08:12 +0000474
Ted Kremenek4c1d41f2009-02-11 23:34:32 +0000475 if (PrologueOffset >= BufEnd) {
Daniel Dunbar1a54e3f2009-11-12 02:53:48 +0000476 Diags.Report(diag::err_invalid_pth_file) << file;
Ted Kremenekeb8c8fb2009-01-26 21:43:14 +0000477 return 0;
Ted Kremenek62224c12009-01-28 21:02:43 +0000478 }
Mike Stump11289f42009-09-09 15:08:12 +0000479
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000480 // Construct the file lookup table. This will be used for mapping from
481 // FileEntry*'s to cached tokens.
Ted Kremenek4c1d41f2009-02-11 23:34:32 +0000482 const unsigned char* FileTableOffset = PrologueOffset + sizeof(uint32_t)*2;
Chris Lattnerfec54702009-01-22 19:48:26 +0000483 const unsigned char* FileTable = BufBeg + ReadLE32(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;
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000487 return 0; // FIXME: Proper error diagnostic?
488 }
Mike Stump11289f42009-09-09 15:08:12 +0000489
Ted Kremenek86423a92009-02-10 22:16:22 +0000490 llvm::OwningPtr<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;
Chris Lattnerfec54702009-01-22 19:48:26 +0000500 const unsigned char* IData = BufBeg + ReadLE32(IDTableOffset);
Mike Stump11289f42009-09-09 15:08:12 +0000501
Ted Kremenek8d6c8282009-01-21 07:34:28 +0000502 if (!(IData >= BufBeg && IData < BufEnd)) {
Daniel Dunbar1a54e3f2009-11-12 02:53:48 +0000503 Diags.Report(diag::err_invalid_pth_file) << file;
Ted Kremenek62224c12009-01-28 21:02:43 +0000504 return 0;
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000505 }
Mike Stump11289f42009-09-09 15:08:12 +0000506
Ted Kremeneke5554de2009-02-11 21:29:16 +0000507 // Get the location of the hashtable mapping between strings and
508 // persistent IDs.
Ted Kremenek4c1d41f2009-02-11 23:34:32 +0000509 const unsigned char* StringIdTableOffset = PrologueOffset + sizeof(uint32_t)*1;
Ted Kremeneke5554de2009-02-11 21:29:16 +0000510 const unsigned char* StringIdTable = BufBeg + ReadLE32(StringIdTableOffset);
511 if (!(StringIdTable >= BufBeg && StringIdTable < BufEnd)) {
Daniel Dunbar1a54e3f2009-11-12 02:53:48 +0000512 Diags.Report(diag::err_invalid_pth_file) << file;
Ted Kremenek62224c12009-01-28 21:02:43 +0000513 return 0;
Ted Kremeneka705b042009-01-15 18:47:46 +0000514 }
Ted Kremeneke5554de2009-02-11 21:29:16 +0000515
516 llvm::OwningPtr<PTHStringIdLookup> SL(PTHStringIdLookup::Create(StringIdTable,
517 BufBeg));
Mike Stump11289f42009-09-09 15:08:12 +0000518
Ted Kremenek8d178f42009-01-27 00:01:05 +0000519 // Get the location of the spelling cache.
Ted Kremenek4c1d41f2009-02-11 23:34:32 +0000520 const unsigned char* spellingBaseOffset = PrologueOffset + sizeof(uint32_t)*3;
Ted Kremenek8d178f42009-01-27 00:01:05 +0000521 const unsigned char* spellingBase = BufBeg + ReadLE32(spellingBaseOffset);
522 if (!(spellingBase >= BufBeg && spellingBase < BufEnd)) {
Daniel Dunbar1a54e3f2009-11-12 02:53:48 +0000523 Diags.Report(diag::err_invalid_pth_file) << file;
Ted Kremenek8d178f42009-01-27 00:01:05 +0000524 return 0;
525 }
Mike Stump11289f42009-09-09 15:08:12 +0000526
Ted Kremenek73a4d282008-12-03 01:16:39 +0000527 // Get the number of IdentifierInfos and pre-allocate the identifier cache.
Chris Lattnerfec54702009-01-22 19:48:26 +0000528 uint32_t NumIds = ReadLE32(IData);
Mike Stump11289f42009-09-09 15:08:12 +0000529
Chris Lattner57540c52011-04-15 05:22:18 +0000530 // Pre-allocate the persistent ID -> IdentifierInfo* cache. We use calloc()
Ted Kremenek73a4d282008-12-03 01:16:39 +0000531 // so that we in the best case only zero out memory once when the OS returns
532 // us new pages.
Ted Kremenek8d6c8282009-01-21 07:34:28 +0000533 IdentifierInfo** PerIDCache = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000534
Ted Kremenek8d6c8282009-01-21 07:34:28 +0000535 if (NumIds) {
Mike Stump11289f42009-09-09 15:08:12 +0000536 PerIDCache = (IdentifierInfo**)calloc(NumIds, sizeof(*PerIDCache));
Ted Kremenek8d6c8282009-01-21 07:34:28 +0000537 if (!PerIDCache) {
Daniel Dunbar1a54e3f2009-11-12 02:53:48 +0000538 InvalidPTH(Diags, "Could not allocate memory for processing PTH file");
Ted Kremenek8d6c8282009-01-21 07:34:28 +0000539 return 0;
540 }
Ted Kremenek73a4d282008-12-03 01:16:39 +0000541 }
Ted Kremenek8d6c8282009-01-21 07:34:28 +0000542
Ted Kremenekf9ccd5c2009-03-19 22:19:30 +0000543 // Compute the address of the original source file.
544 const unsigned char* originalSourceBase = PrologueOffset + sizeof(uint32_t)*4;
545 unsigned len = ReadUnalignedLE16(originalSourceBase);
Mike Stump11289f42009-09-09 15:08:12 +0000546 if (!len) originalSourceBase = 0;
547
Ted Kremeneka705b042009-01-15 18:47:46 +0000548 // Create the new PTHManager.
549 return new PTHManager(File.take(), FL.take(), IData, PerIDCache,
Ted Kremenekf9ccd5c2009-03-19 22:19:30 +0000550 SL.take(), NumIds, spellingBase,
551 (const char*) originalSourceBase);
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000552}
Ted Kremenekf9ccd5c2009-03-19 22:19:30 +0000553
Chris Lattner144aacd2009-01-18 02:57:21 +0000554IdentifierInfo* PTHManager::LazilyCreateIdentifierInfo(unsigned PersistentID) {
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000555 // Look in the PTH file for the string data for the IdentifierInfo object.
Chris Lattner144aacd2009-01-18 02:57:21 +0000556 const unsigned char* TableEntry = IdDataTable + sizeof(uint32_t)*PersistentID;
Chris Lattnereb097542009-01-18 01:57:14 +0000557 const unsigned char* IDData =
Chris Lattnerfec54702009-01-22 19:48:26 +0000558 (const unsigned char*)Buf->getBufferStart() + ReadLE32(TableEntry);
Chris Lattnereb097542009-01-18 01:57:14 +0000559 assert(IDData < (const unsigned char*)Buf->getBufferEnd());
Mike Stump11289f42009-09-09 15:08:12 +0000560
Ted Kremeneka705b042009-01-15 18:47:46 +0000561 // Allocate the object.
Chris Lattnereb097542009-01-18 01:57:14 +0000562 std::pair<IdentifierInfo,const unsigned char*> *Mem =
563 Alloc.Allocate<std::pair<IdentifierInfo,const unsigned char*> >();
Ted Kremeneka705b042009-01-15 18:47:46 +0000564
565 Mem->second = IDData;
Ted Kremeneke5554de2009-02-11 21:29:16 +0000566 assert(IDData[0] != '\0');
Ted Kremenek52f73ca2009-01-20 23:28:34 +0000567 IdentifierInfo *II = new ((void*) Mem) IdentifierInfo();
Mike Stump11289f42009-09-09 15:08:12 +0000568
Ted Kremeneka705b042009-01-15 18:47:46 +0000569 // Store the new IdentifierInfo in the cache.
Chris Lattner144aacd2009-01-18 02:57:21 +0000570 PerIDCache[PersistentID] = II;
Daniel Dunbar2c422dc92009-10-18 20:26:12 +0000571 assert(II->getNameStart() && II->getNameStart()[0] != '\0');
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000572 return II;
573}
574
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000575IdentifierInfo* PTHManager::get(StringRef Name) {
Ted Kremeneke5554de2009-02-11 21:29:16 +0000576 PTHStringIdLookup& SL = *((PTHStringIdLookup*)StringIdLookup);
577 // Double check our assumption that the last character isn't '\0'.
Kovarththanan Rajaratnam661a3092010-03-12 08:23:34 +0000578 assert(Name.empty() || Name.data()[Name.size()-1] != '\0');
579 PTHStringIdLookup::iterator I = SL.find(std::make_pair(Name.data(),
580 Name.size()));
Ted Kremeneke5554de2009-02-11 21:29:16 +0000581 if (I == SL.end()) // No identifier found?
582 return 0;
Ted Kremeneka705b042009-01-15 18:47:46 +0000583
Ted Kremeneke5554de2009-02-11 21:29:16 +0000584 // Match found. Return the identifier!
585 assert(*I > 0);
586 return GetIdentifierInfo(*I-1);
587}
Ted Kremeneka705b042009-01-15 18:47:46 +0000588
Chris Lattnerab1d4b82009-01-17 08:06:50 +0000589PTHLexer *PTHManager::CreateLexer(FileID FID) {
590 const FileEntry *FE = PP->getSourceManager().getFileEntryForID(FID);
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000591 if (!FE)
592 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000593
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000594 // Lookup the FileEntry object in our file lookup data structure. It will
595 // return a variant that indicates whether or not there is an offset within
596 // the PTH file that contains cached tokens.
Ted Kremenek86423a92009-02-10 22:16:22 +0000597 PTHFileLookup& PFL = *((PTHFileLookup*)FileLookup);
598 PTHFileLookup::iterator I = PFL.find(FE);
Mike Stump11289f42009-09-09 15:08:12 +0000599
Ted Kremenek86423a92009-02-10 22:16:22 +0000600 if (I == PFL.end()) // No tokens available?
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000601 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000602
603 const PTHFileData& FileData = *I;
604
Chris Lattnereb097542009-01-18 01:57:14 +0000605 const unsigned char *BufStart = (const unsigned char *)Buf->getBufferStart();
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000606 // Compute the offset of the token data within the buffer.
Chris Lattnereb097542009-01-18 01:57:14 +0000607 const unsigned char* data = BufStart + FileData.getTokenOffset();
Ted Kremenek56572ab2008-12-12 18:34:08 +0000608
609 // Get the location of pp-conditional table.
Chris Lattnereb097542009-01-18 01:57:14 +0000610 const unsigned char* ppcond = BufStart + FileData.getPPCondOffset();
Chris Lattnerfec54702009-01-22 19:48:26 +0000611 uint32_t Len = ReadLE32(ppcond);
Chris Lattner137d6492009-01-18 02:10:31 +0000612 if (Len == 0) ppcond = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000613
Ted Kremeneka705b042009-01-15 18:47:46 +0000614 assert(PP && "No preprocessor set yet!");
Mike Stump11289f42009-09-09 15:08:12 +0000615 return new PTHLexer(*PP, FID, data, ppcond, *this);
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000616}
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000617
618//===----------------------------------------------------------------------===//
619// 'stat' caching.
620//===----------------------------------------------------------------------===//
621
622namespace {
Benjamin Kramer337e3a52009-11-28 19:45:26 +0000623class PTHStatData {
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000624public:
Ted Kremenek29942a32009-02-13 19:13:46 +0000625 const bool hasStat;
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000626 const ino_t ino;
627 const dev_t dev;
628 const mode_t mode;
629 const time_t mtime;
630 const off_t size;
Mike Stump11289f42009-09-09 15:08:12 +0000631
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000632 PTHStatData(ino_t i, dev_t d, mode_t mo, time_t m, off_t s)
Mike Stump11289f42009-09-09 15:08:12 +0000633 : hasStat(true), ino(i), dev(d), mode(mo), mtime(m), size(s) {}
634
Ted Kremenek29942a32009-02-13 19:13:46 +0000635 PTHStatData()
636 : hasStat(false), ino(0), dev(0), mode(0), mtime(0), size(0) {}
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000637};
Mike Stump11289f42009-09-09 15:08:12 +0000638
Benjamin Kramer337e3a52009-11-28 19:45:26 +0000639class PTHStatLookupTrait : public PTHFileLookupCommonTrait {
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000640public:
Ted Kremenek29942a32009-02-13 19:13:46 +0000641 typedef const char* external_key_type; // const char*
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000642 typedef PTHStatData data_type;
Mike Stump11289f42009-09-09 15:08:12 +0000643
Ted Kremenek29942a32009-02-13 19:13:46 +0000644 static internal_key_type GetInternalKey(const char *path) {
645 // The key 'kind' doesn't matter here because it is ignored in EqualKey.
646 return std::make_pair((unsigned char) 0x0, path);
647 }
648
649 static bool EqualKey(internal_key_type a, internal_key_type b) {
650 // When doing 'stat' lookups we don't care about the kind of 'a' and 'b',
651 // just the paths.
652 return strcmp(a.second, b.second) == 0;
Mike Stump11289f42009-09-09 15:08:12 +0000653 }
654
Ted Kremenek29942a32009-02-13 19:13:46 +0000655 static data_type ReadData(const internal_key_type& k, const unsigned char* d,
Mike Stump11289f42009-09-09 15:08:12 +0000656 unsigned) {
657
Ted Kremenek2fd18ec2009-02-13 22:07:44 +0000658 if (k.first /* File or Directory */) {
659 if (k.first == 0x1 /* File */) d += 4 * 2; // Skip the first 2 words.
Ted Kremenek29942a32009-02-13 19:13:46 +0000660 ino_t ino = (ino_t) ReadUnalignedLE32(d);
661 dev_t dev = (dev_t) ReadUnalignedLE32(d);
662 mode_t mode = (mode_t) ReadUnalignedLE16(d);
Mike Stump11289f42009-09-09 15:08:12 +0000663 time_t mtime = (time_t) ReadUnalignedLE64(d);
Ted Kremenek29942a32009-02-13 19:13:46 +0000664 return data_type(ino, dev, mode, mtime, (off_t) ReadUnalignedLE64(d));
665 }
Ted Kremenek2fd18ec2009-02-13 22:07:44 +0000666
667 // Negative stat. Don't read anything.
Ted Kremenek29942a32009-02-13 19:13:46 +0000668 return data_type();
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000669 }
670};
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000671
Chris Lattner226efd32010-11-23 19:19:34 +0000672class PTHStatCache : public FileSystemStatCache {
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000673 typedef OnDiskChainedHashTable<PTHStatLookupTrait> CacheTy;
674 CacheTy Cache;
675
Mike Stump11289f42009-09-09 15:08:12 +0000676public:
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000677 PTHStatCache(PTHFileLookup &FL) :
678 Cache(FL.getNumBuckets(), FL.getNumEntries(), FL.getBuckets(),
679 FL.getBase()) {}
680
681 ~PTHStatCache() {}
Mike Stump11289f42009-09-09 15:08:12 +0000682
Chris Lattnerdd278432010-11-23 21:17:56 +0000683 LookupResult getStat(const char *Path, struct stat &StatBuf,
684 int *FileDescriptor) {
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000685 // Do the lookup for the file's data in the PTH file.
Chris Lattner226efd32010-11-23 19:19:34 +0000686 CacheTy::iterator I = Cache.find(Path);
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000687
688 // If we don't get a hit in the PTH file just forward to 'stat'.
Kovarththanan Rajaratnam661a3092010-03-12 08:23:34 +0000689 if (I == Cache.end())
Chris Lattnerdd278432010-11-23 21:17:56 +0000690 return statChained(Path, StatBuf, FileDescriptor);
Mike Stump11289f42009-09-09 15:08:12 +0000691
Chris Lattner226efd32010-11-23 19:19:34 +0000692 const PTHStatData &Data = *I;
Mike Stump11289f42009-09-09 15:08:12 +0000693
Ted Kremenek2fd18ec2009-02-13 22:07:44 +0000694 if (!Data.hasStat)
Chris Lattner8f0583d2010-11-23 20:05:15 +0000695 return CacheMissing;
Ted Kremenek2fd18ec2009-02-13 22:07:44 +0000696
Chris Lattner226efd32010-11-23 19:19:34 +0000697 StatBuf.st_ino = Data.ino;
698 StatBuf.st_dev = Data.dev;
699 StatBuf.st_mtime = Data.mtime;
700 StatBuf.st_mode = Data.mode;
701 StatBuf.st_size = Data.size;
Chris Lattner8f0583d2010-11-23 20:05:15 +0000702 return CacheExists;
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000703 }
704};
Ted Kremenek29b86972009-02-23 23:27:54 +0000705} // end anonymous namespace
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000706
Chris Lattner226efd32010-11-23 19:19:34 +0000707FileSystemStatCache *PTHManager::createStatCache() {
Ted Kremenekb4c85cc2009-02-12 03:45:39 +0000708 return new PTHStatCache(*((PTHFileLookup*) FileLookup));
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000709}