blob: f17a5d93a91ab9f6d04a58e61e08c004d2e68d73 [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"
16#include "clang/Basic/IdentifierTable.h"
Douglas Gregor52289d32009-04-20 07:08:21 +000017#include "clang/Basic/OnDiskHashTable.h"
Ted Kremenek7cd62452008-11-12 21:37:15 +000018#include "clang/Lex/PTHLexer.h"
19#include "clang/Lex/Preprocessor.h"
Ted Kremenek33eeabd2008-12-03 00:38:03 +000020#include "clang/Lex/PTHManager.h"
21#include "clang/Lex/Token.h"
22#include "clang/Lex/Preprocessor.h"
Ted Kremenek33eeabd2008-12-03 00:38:03 +000023#include "llvm/ADT/OwningPtr.h"
Daniel Dunbarf8502d52009-10-17 23:52:28 +000024#include "llvm/ADT/StringExtras.h"
25#include "llvm/ADT/StringMap.h"
Chris Lattner34eab392009-01-22 23:50:07 +000026#include "llvm/Support/MemoryBuffer.h"
Ted Kremeneka5c2c272009-02-12 03:26:59 +000027#include <sys/stat.h>
Ted Kremenek7cd62452008-11-12 21:37:15 +000028using namespace clang;
Douglas Gregor52289d32009-04-20 07:08:21 +000029using namespace clang::io;
Ted Kremenek7cd62452008-11-12 21:37:15 +000030
Ted Kremenek8433f0b2009-01-19 23:13:15 +000031#define DISK_TOKEN_SIZE (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)
Chris Lattnerd32480d2009-01-17 06:22:33 +000039 : PreprocessorLexer(&PP, FID), TokBuf(D), CurPtr(D), LastHashTokPtr(0),
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
45void PTHLexer::Lex(Token& Tok) {
46LexNextToken:
Ted Kremenek1b18ad22008-12-23 01:30:52 +000047
Ted Kremenek66076a92008-12-23 02:30:15 +000048 //===--------------------------------------==//
49 // Read the raw token data.
50 //===--------------------------------------==//
Mike Stump11289f42009-09-09 15:08:12 +000051
Ted Kremenek66076a92008-12-23 02:30:15 +000052 // Shadow CurPtr into an automatic variable.
Mike Stump11289f42009-09-09 15:08:12 +000053 const unsigned char *CurPtrShadow = CurPtr;
Ted Kremenek66076a92008-12-23 02:30:15 +000054
Chris Lattner137d6492009-01-18 02:10:31 +000055 // Read in the data for the token.
Chris Lattnerfec54702009-01-22 19:48:26 +000056 unsigned Word0 = ReadLE32(CurPtrShadow);
57 uint32_t IdentifierID = ReadLE32(CurPtrShadow);
58 uint32_t FileOffset = ReadLE32(CurPtrShadow);
Mike Stump11289f42009-09-09 15:08:12 +000059
Ted Kremenek8433f0b2009-01-19 23:13:15 +000060 tok::TokenKind TKind = (tok::TokenKind) (Word0 & 0xFF);
61 Token::TokenFlags TFlags = (Token::TokenFlags) ((Word0 >> 8) & 0xFF);
Chris Lattner47def972009-01-21 07:21:56 +000062 uint32_t Len = Word0 >> 16;
Ted Kremenek8433f0b2009-01-19 23:13:15 +000063
Chris Lattner47def972009-01-21 07:21:56 +000064 CurPtr = CurPtrShadow;
Mike Stump11289f42009-09-09 15:08:12 +000065
Ted Kremenek66076a92008-12-23 02:30:15 +000066 //===--------------------------------------==//
67 // Construct the token itself.
68 //===--------------------------------------==//
Mike Stump11289f42009-09-09 15:08:12 +000069
Ted Kremenek66076a92008-12-23 02:30:15 +000070 Tok.startToken();
Chris Lattner18fc6ce2009-01-18 02:34:01 +000071 Tok.setKind(TKind);
72 Tok.setFlag(TFlags);
Ted Kremenek78cc2472008-12-23 19:24:24 +000073 assert(!LexingRawMode);
Chris Lattnerd32480d2009-01-17 06:22:33 +000074 Tok.setLocation(FileStartLoc.getFileLocWithOffset(FileOffset));
Ted Kremenek66076a92008-12-23 02:30:15 +000075 Tok.setLength(Len);
76
Chris Lattner3029b352009-01-21 07:50:06 +000077 // Handle identifiers.
Ted Kremenek8d178f42009-01-27 00:01:05 +000078 if (Tok.isLiteral()) {
79 Tok.setLiteralData((const char*) (PTHMgr.SpellingBase + IdentifierID));
80 }
81 else if (IdentifierID) {
Chris Lattner3029b352009-01-21 07:50:06 +000082 MIOpt.ReadToken();
83 IdentifierInfo *II = PTHMgr.GetIdentifierInfo(IdentifierID-1);
Mike Stump11289f42009-09-09 15:08:12 +000084
Chris Lattner3029b352009-01-21 07:50:06 +000085 Tok.setIdentifierInfo(II);
Mike Stump11289f42009-09-09 15:08:12 +000086
Chris Lattner1f6c7fe2009-01-23 18:35:48 +000087 // Change the kind of this identifier to the appropriate token kind, e.g.
88 // turning "for" into a keyword.
89 Tok.setKind(II->getTokenID());
Mike Stump11289f42009-09-09 15:08:12 +000090
Chris Lattner3029b352009-01-21 07:50:06 +000091 if (II->isHandleIdentifierCase())
92 PP->HandleIdentifier(Tok);
93 return;
94 }
Mike Stump11289f42009-09-09 15:08:12 +000095
Ted Kremenek66076a92008-12-23 02:30:15 +000096 //===--------------------------------------==//
97 // Process the token.
98 //===--------------------------------------==//
Chris Lattner18fc6ce2009-01-18 02:34:01 +000099 if (TKind == tok::eof) {
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000100 // Save the end-of-file token.
101 EofToken = Tok;
Mike Stump11289f42009-09-09 15:08:12 +0000102
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000103 Preprocessor *PPCache = PP;
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
Ted Kremenek78cc2472008-12-23 19:24:24 +0000108 // FIXME: Issue diagnostics similar to Lexer.
109 if (PP->HandleEndOfFile(Tok, false))
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000110 return;
Mike Stump11289f42009-09-09 15:08:12 +0000111
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000112 assert(PPCache && "Raw buffer::LexEndOfFile should return a token");
113 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
Chris Lattner18fc6ce2009-01-18 02:34:01 +0000127 if (TKind == tok::eom) {
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
136// FIXME: We can just grab the last token instead of storing a copy
137// into EofToken.
Ted Kremenek78cc2472008-12-23 19:24:24 +0000138void PTHLexer::getEOF(Token& Tok) {
Ted Kremenek8ae06622009-01-09 00:36:11 +0000139 assert(EofToken.is(tok::eof));
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000140 Tok = EofToken;
141}
142
143void PTHLexer::DiscardToEndOfLine() {
144 assert(ParsingPreprocessorDirective && ParsingFilename == false &&
145 "Must be in a preprocessing directive!");
146
147 // We assume that if the preprocessor wishes to discard to the end of
148 // the line that it also means to end the current preprocessor directive.
149 ParsingPreprocessorDirective = false;
Mike Stump11289f42009-09-09 15:08:12 +0000150
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000151 // Skip tokens by only peeking at their token kind and the flags.
152 // We don't need to actually reconstruct full tokens from the token buffer.
153 // This saves some copies and it also reduces IdentifierInfo* lookup.
Chris Lattnereb097542009-01-18 01:57:14 +0000154 const unsigned char* p = CurPtr;
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000155 while (1) {
156 // Read the token kind. Are we at the end of the file?
157 tok::TokenKind x = (tok::TokenKind) (uint8_t) *p;
158 if (x == tok::eof) break;
Mike Stump11289f42009-09-09 15:08:12 +0000159
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000160 // Read the token flags. Are we at the start of the next line?
161 Token::TokenFlags y = (Token::TokenFlags) (uint8_t) p[1];
162 if (y & Token::StartOfLine) break;
163
164 // Skip to the next token.
165 p += DISK_TOKEN_SIZE;
166 }
Mike Stump11289f42009-09-09 15:08:12 +0000167
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000168 CurPtr = p;
169}
170
Ted Kremenek56572ab2008-12-12 18:34:08 +0000171/// SkipBlock - Used by Preprocessor to skip the current conditional block.
172bool PTHLexer::SkipBlock() {
173 assert(CurPPCondPtr && "No cached PP conditional information.");
174 assert(LastHashTokPtr && "No known '#' token.");
Mike Stump11289f42009-09-09 15:08:12 +0000175
Chris Lattnereb097542009-01-18 01:57:14 +0000176 const unsigned char* HashEntryI = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000177 uint32_t Offset;
Ted Kremenek56572ab2008-12-12 18:34:08 +0000178 uint32_t TableIdx;
Mike Stump11289f42009-09-09 15:08:12 +0000179
Ted Kremenek56572ab2008-12-12 18:34:08 +0000180 do {
Ted Kremenek877556f2008-12-12 22:05:38 +0000181 // Read the token offset from the side-table.
Chris Lattnerfec54702009-01-22 19:48:26 +0000182 Offset = ReadLE32(CurPPCondPtr);
Mike Stump11289f42009-09-09 15:08:12 +0000183
184 // Read the target table index from the side-table.
Chris Lattnerfec54702009-01-22 19:48:26 +0000185 TableIdx = ReadLE32(CurPPCondPtr);
Mike Stump11289f42009-09-09 15:08:12 +0000186
Ted Kremenek877556f2008-12-12 22:05:38 +0000187 // Compute the actual memory address of the '#' token data for this entry.
188 HashEntryI = TokBuf + Offset;
189
190 // Optmization: "Sibling jumping". #if...#else...#endif blocks can
191 // contain nested blocks. In the side-table we can jump over these
192 // nested blocks instead of doing a linear search if the next "sibling"
193 // entry is not at a location greater than LastHashTokPtr.
194 if (HashEntryI < LastHashTokPtr && TableIdx) {
195 // In the side-table we are still at an entry for a '#' token that
196 // is earlier than the last one we saw. Check if the location we would
197 // stride gets us closer.
Chris Lattnereb097542009-01-18 01:57:14 +0000198 const unsigned char* NextPPCondPtr =
199 PPCond + TableIdx*(sizeof(uint32_t)*2);
Ted Kremenek877556f2008-12-12 22:05:38 +0000200 assert(NextPPCondPtr >= CurPPCondPtr);
201 // Read where we should jump to.
Chris Lattnerfec54702009-01-22 19:48:26 +0000202 uint32_t TmpOffset = ReadLE32(NextPPCondPtr);
Chris Lattnereb097542009-01-18 01:57:14 +0000203 const unsigned char* HashEntryJ = TokBuf + TmpOffset;
Mike Stump11289f42009-09-09 15:08:12 +0000204
Ted Kremenek877556f2008-12-12 22:05:38 +0000205 if (HashEntryJ <= LastHashTokPtr) {
206 // Jump directly to the next entry in the side table.
207 HashEntryI = HashEntryJ;
208 Offset = TmpOffset;
Chris Lattnerfec54702009-01-22 19:48:26 +0000209 TableIdx = ReadLE32(NextPPCondPtr);
Ted Kremenek877556f2008-12-12 22:05:38 +0000210 CurPPCondPtr = NextPPCondPtr;
211 }
212 }
Ted Kremenek56572ab2008-12-12 18:34:08 +0000213 }
Mike Stump11289f42009-09-09 15:08:12 +0000214 while (HashEntryI < LastHashTokPtr);
Ted Kremenek877556f2008-12-12 22:05:38 +0000215 assert(HashEntryI == LastHashTokPtr && "No PP-cond entry found for '#'");
Ted Kremenek56572ab2008-12-12 18:34:08 +0000216 assert(TableIdx && "No jumping from #endifs.");
Mike Stump11289f42009-09-09 15:08:12 +0000217
Ted Kremenek56572ab2008-12-12 18:34:08 +0000218 // Update our side-table iterator.
Chris Lattnereb097542009-01-18 01:57:14 +0000219 const unsigned char* NextPPCondPtr = PPCond + TableIdx*(sizeof(uint32_t)*2);
Ted Kremenek56572ab2008-12-12 18:34:08 +0000220 assert(NextPPCondPtr >= CurPPCondPtr);
221 CurPPCondPtr = NextPPCondPtr;
Mike Stump11289f42009-09-09 15:08:12 +0000222
Ted Kremenek56572ab2008-12-12 18:34:08 +0000223 // Read where we should jump to.
Chris Lattnerfec54702009-01-22 19:48:26 +0000224 HashEntryI = TokBuf + ReadLE32(NextPPCondPtr);
225 uint32_t NextIdx = ReadLE32(NextPPCondPtr);
Mike Stump11289f42009-09-09 15:08:12 +0000226
Ted Kremenek56572ab2008-12-12 18:34:08 +0000227 // By construction NextIdx will be zero if this is a #endif. This is useful
228 // to know to obviate lexing another token.
229 bool isEndif = NextIdx == 0;
Mike Stump11289f42009-09-09 15:08:12 +0000230
Ted Kremenek56572ab2008-12-12 18:34:08 +0000231 // This case can occur when we see something like this:
232 //
233 // #if ...
234 // /* a comment or nothing */
235 // #elif
236 //
237 // If we are skipping the first #if block it will be the case that CurPtr
238 // already points 'elif'. Just return.
Mike Stump11289f42009-09-09 15:08:12 +0000239
Ted Kremenek877556f2008-12-12 22:05:38 +0000240 if (CurPtr > HashEntryI) {
241 assert(CurPtr == HashEntryI + DISK_TOKEN_SIZE);
Ted Kremenek56572ab2008-12-12 18:34:08 +0000242 // Did we reach a #endif? If so, go ahead and consume that token as well.
243 if (isEndif)
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000244 CurPtr += DISK_TOKEN_SIZE*2;
Ted Kremenek56572ab2008-12-12 18:34:08 +0000245 else
Ted Kremenek877556f2008-12-12 22:05:38 +0000246 LastHashTokPtr = HashEntryI;
Mike Stump11289f42009-09-09 15:08:12 +0000247
Ted Kremenek56572ab2008-12-12 18:34:08 +0000248 return isEndif;
249 }
250
251 // Otherwise, we need to advance. Update CurPtr to point to the '#' token.
Ted Kremenek877556f2008-12-12 22:05:38 +0000252 CurPtr = HashEntryI;
Mike Stump11289f42009-09-09 15:08:12 +0000253
Ted Kremenek56572ab2008-12-12 18:34:08 +0000254 // Update the location of the last observed '#'. This is useful if we
255 // are skipping multiple blocks.
256 LastHashTokPtr = CurPtr;
Ted Kremenek56572ab2008-12-12 18:34:08 +0000257
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000258 // Skip the '#' token.
Chris Lattnereb097542009-01-18 01:57:14 +0000259 assert(((tok::TokenKind)*CurPtr) == tok::hash);
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000260 CurPtr += DISK_TOKEN_SIZE;
Mike Stump11289f42009-09-09 15:08:12 +0000261
Ted Kremenek56572ab2008-12-12 18:34:08 +0000262 // Did we reach a #endif? If so, go ahead and consume that token as well.
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000263 if (isEndif) { CurPtr += DISK_TOKEN_SIZE*2; }
Ted Kremenek56572ab2008-12-12 18:34:08 +0000264
265 return isEndif;
266}
267
Ted Kremenek63ff81c2008-12-17 23:36:32 +0000268SourceLocation PTHLexer::getSourceLocation() {
Chris Lattner137d6492009-01-18 02:10:31 +0000269 // getSourceLocation is not on the hot path. It is used to get the location
270 // of the next token when transitioning back to this lexer when done
Ted Kremenek63ff81c2008-12-17 23:36:32 +0000271 // handling a #included file. Just read the necessary data from the token
272 // data buffer to construct the SourceLocation object.
273 // NOTE: This is a virtual function; hence it is defined out-of-line.
Ted Kremenekae54f2f2009-01-21 22:41:38 +0000274 const unsigned char *OffsetPtr = CurPtr + (DISK_TOKEN_SIZE - 4);
Chris Lattnerfec54702009-01-22 19:48:26 +0000275 uint32_t Offset = ReadLE32(OffsetPtr);
Chris Lattner137d6492009-01-18 02:10:31 +0000276 return FileStartLoc.getFileLocWithOffset(Offset);
Ted Kremenek63ff81c2008-12-17 23:36:32 +0000277}
278
Ted Kremenek47b8cf62009-01-09 22:05:30 +0000279//===----------------------------------------------------------------------===//
Ted Kremenek86423a92009-02-10 22:16:22 +0000280// PTH file lookup: map from strings to file data.
281//===----------------------------------------------------------------------===//
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000282
283/// PTHFileLookup - This internal data structure is used by the PTHManager
284/// to map from FileEntry objects managed by FileManager to offsets within
285/// the PTH file.
286namespace {
Ted Kremenek86423a92009-02-10 22:16:22 +0000287class VISIBILITY_HIDDEN PTHFileData {
288 const uint32_t TokenOff;
289 const uint32_t PPCondOff;
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000290public:
Ted Kremenek86423a92009-02-10 22:16:22 +0000291 PTHFileData(uint32_t tokenOff, uint32_t ppCondOff)
292 : TokenOff(tokenOff), PPCondOff(ppCondOff) {}
Mike Stump11289f42009-09-09 15:08:12 +0000293
294 uint32_t getTokenOffset() const { return TokenOff; }
295 uint32_t getPPCondOffset() const { return PPCondOff; }
Ted Kremenek86423a92009-02-10 22:16:22 +0000296};
Mike Stump11289f42009-09-09 15:08:12 +0000297
298
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000299class VISIBILITY_HIDDEN PTHFileLookupCommonTrait {
Ted Kremenek86423a92009-02-10 22:16:22 +0000300public:
Ted Kremenek29942a32009-02-13 19:13:46 +0000301 typedef std::pair<unsigned char, const char*> internal_key_type;
302
303 static unsigned ComputeHash(internal_key_type x) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +0000304 return llvm::HashString(x.second);
Ted Kremenek86423a92009-02-10 22:16:22 +0000305 }
Mike Stump11289f42009-09-09 15:08:12 +0000306
Ted Kremenek86423a92009-02-10 22:16:22 +0000307 static std::pair<unsigned, unsigned>
308 ReadKeyDataLength(const unsigned char*& d) {
Ted Kremenek29942a32009-02-13 19:13:46 +0000309 unsigned keyLen = (unsigned) ReadUnalignedLE16(d);
310 unsigned dataLen = (unsigned) *(d++);
311 return std::make_pair(keyLen, dataLen);
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000312 }
Mike Stump11289f42009-09-09 15:08:12 +0000313
Ted Kremenek29942a32009-02-13 19:13:46 +0000314 static internal_key_type ReadKey(const unsigned char* d, unsigned) {
315 unsigned char k = *(d++); // Read the entry kind.
316 return std::make_pair(k, (const char*) d);
Ted Kremenek86423a92009-02-10 22:16:22 +0000317 }
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000318};
Mike Stump11289f42009-09-09 15:08:12 +0000319
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000320class VISIBILITY_HIDDEN PTHFileLookupTrait : public PTHFileLookupCommonTrait {
321public:
322 typedef const FileEntry* external_key_type;
323 typedef PTHFileData data_type;
Mike Stump11289f42009-09-09 15:08:12 +0000324
Ted Kremenek29942a32009-02-13 19:13:46 +0000325 static internal_key_type GetInternalKey(const FileEntry* FE) {
326 return std::make_pair((unsigned char) 0x1, FE->getName());
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000327 }
Ted Kremenek29942a32009-02-13 19:13:46 +0000328
329 static bool EqualKey(internal_key_type a, internal_key_type b) {
330 return a.first == b.first && strcmp(a.second, b.second) == 0;
Mike Stump11289f42009-09-09 15:08:12 +0000331 }
332
333 static PTHFileData ReadData(const internal_key_type& k,
334 const unsigned char* d, unsigned) {
Ted Kremenek29942a32009-02-13 19:13:46 +0000335 assert(k.first == 0x1 && "Only file lookups can match!");
Ted Kremenek86423a92009-02-10 22:16:22 +0000336 uint32_t x = ::ReadUnalignedLE32(d);
337 uint32_t y = ::ReadUnalignedLE32(d);
Mike Stump11289f42009-09-09 15:08:12 +0000338 return PTHFileData(x, y);
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000339 }
340};
Ted Kremeneke5554de2009-02-11 21:29:16 +0000341
342class VISIBILITY_HIDDEN PTHStringLookupTrait {
343public:
Mike Stump11289f42009-09-09 15:08:12 +0000344 typedef uint32_t
Ted Kremeneke5554de2009-02-11 21:29:16 +0000345 data_type;
346
347 typedef const std::pair<const char*, unsigned>
348 external_key_type;
349
350 typedef external_key_type internal_key_type;
Mike Stump11289f42009-09-09 15:08:12 +0000351
Ted Kremeneke5554de2009-02-11 21:29:16 +0000352 static bool EqualKey(const internal_key_type& a,
353 const internal_key_type& b) {
354 return (a.second == b.second) ? memcmp(a.first, b.first, a.second) == 0
355 : false;
356 }
Mike Stump11289f42009-09-09 15:08:12 +0000357
Ted Kremeneke5554de2009-02-11 21:29:16 +0000358 static unsigned ComputeHash(const internal_key_type& a) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +0000359 return llvm::HashString(llvm::StringRef(a.first, a.second));
Ted Kremeneke5554de2009-02-11 21:29:16 +0000360 }
Mike Stump11289f42009-09-09 15:08:12 +0000361
Ted Kremeneke5554de2009-02-11 21:29:16 +0000362 // This hopefully will just get inlined and removed by the optimizer.
363 static const internal_key_type&
364 GetInternalKey(const external_key_type& x) { return x; }
Mike Stump11289f42009-09-09 15:08:12 +0000365
Ted Kremeneke5554de2009-02-11 21:29:16 +0000366 static std::pair<unsigned, unsigned>
367 ReadKeyDataLength(const unsigned char*& d) {
368 return std::make_pair((unsigned) ReadUnalignedLE16(d), sizeof(uint32_t));
369 }
Mike Stump11289f42009-09-09 15:08:12 +0000370
Ted Kremeneke5554de2009-02-11 21:29:16 +0000371 static std::pair<const char*, unsigned>
372 ReadKey(const unsigned char* d, unsigned n) {
373 assert(n >= 2 && d[n-1] == '\0');
374 return std::make_pair((const char*) d, n-1);
375 }
Mike Stump11289f42009-09-09 15:08:12 +0000376
Ted Kremenek29942a32009-02-13 19:13:46 +0000377 static uint32_t ReadData(const internal_key_type& k, const unsigned char* d,
378 unsigned) {
Ted Kremeneke5554de2009-02-11 21:29:16 +0000379 return ::ReadUnalignedLE32(d);
380 }
381};
Mike Stump11289f42009-09-09 15:08:12 +0000382
383} // end anonymous namespace
Ted Kremenek86423a92009-02-10 22:16:22 +0000384
Ted Kremeneke5554de2009-02-11 21:29:16 +0000385typedef OnDiskChainedHashTable<PTHFileLookupTrait> PTHFileLookup;
386typedef OnDiskChainedHashTable<PTHStringLookupTrait> PTHStringIdLookup;
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000387
388//===----------------------------------------------------------------------===//
389// PTHManager methods.
390//===----------------------------------------------------------------------===//
391
392PTHManager::PTHManager(const llvm::MemoryBuffer* buf, void* fileLookup,
Chris Lattnereb097542009-01-18 01:57:14 +0000393 const unsigned char* idDataTable,
Mike Stump11289f42009-09-09 15:08:12 +0000394 IdentifierInfo** perIDCache,
Ted Kremeneke5554de2009-02-11 21:29:16 +0000395 void* stringIdLookup, unsigned numIds,
Ted Kremenekf9ccd5c2009-03-19 22:19:30 +0000396 const unsigned char* spellingBase,
397 const char* originalSourceFile)
Ted Kremenek73a4d282008-12-03 01:16:39 +0000398: Buf(buf), PerIDCache(perIDCache), FileLookup(fileLookup),
Ted Kremeneke5554de2009-02-11 21:29:16 +0000399 IdDataTable(idDataTable), StringIdLookup(stringIdLookup),
Ted Kremenekf9ccd5c2009-03-19 22:19:30 +0000400 NumIds(numIds), PP(0), SpellingBase(spellingBase),
401 OriginalSourceFile(originalSourceFile) {}
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000402
403PTHManager::~PTHManager() {
404 delete Buf;
405 delete (PTHFileLookup*) FileLookup;
Ted Kremeneke5554de2009-02-11 21:29:16 +0000406 delete (PTHStringIdLookup*) StringIdLookup;
Ted Kremenek1aed3dd2008-12-04 22:47:11 +0000407 free(PerIDCache);
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000408}
409
Ted Kremenekdc637a02009-03-22 06:42:39 +0000410static void InvalidPTH(Diagnostic *Diags, Diagnostic::Level level,
411 const char* Msg = 0) {
Mike Stump11289f42009-09-09 15:08:12 +0000412 if (!Diags) return;
Ted Kremenek62224c12009-01-28 21:02:43 +0000413 if (!Msg) Msg = "Invalid or corrupted PTH file";
Ted Kremenekdc637a02009-03-22 06:42:39 +0000414 unsigned DiagID = Diags->getCustomDiagID(level, Msg);
Ted Kremenek62224c12009-01-28 21:02:43 +0000415 Diags->Report(FullSourceLoc(), DiagID);
416}
417
Ted Kremenekdc637a02009-03-22 06:42:39 +0000418PTHManager* PTHManager::Create(const std::string& file, Diagnostic* Diags,
419 Diagnostic::Level level) {
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000420 // Memory map the PTH file.
421 llvm::OwningPtr<llvm::MemoryBuffer>
422 File(llvm::MemoryBuffer::getFile(file.c_str()));
Mike Stump11289f42009-09-09 15:08:12 +0000423
Ted Kremenek3b0589e2009-01-28 20:49:33 +0000424 if (!File) {
425 if (Diags) {
Ted Kremenekdc637a02009-03-22 06:42:39 +0000426 unsigned DiagID = Diags->getCustomDiagID(level,
Ted Kremenek3b0589e2009-01-28 20:49:33 +0000427 "PTH file %0 could not be read");
428 Diags->Report(FullSourceLoc(), DiagID) << file;
429 }
Ted Kremeneke5554de2009-02-11 21:29:16 +0000430
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000431 return 0;
Ted Kremenek3b0589e2009-01-28 20:49:33 +0000432 }
Mike Stump11289f42009-09-09 15:08:12 +0000433
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000434 // Get the buffer ranges and check if there are at least three 32-bit
435 // words at the end of the file.
Chris Lattnereb097542009-01-18 01:57:14 +0000436 const unsigned char* BufBeg = (unsigned char*)File->getBufferStart();
437 const unsigned char* BufEnd = (unsigned char*)File->getBufferEnd();
Ted Kremenekeb8c8fb2009-01-26 21:43:14 +0000438
439 // Check the prologue of the file.
Ted Kremenek327d00c2009-01-26 22:16:12 +0000440 if ((BufEnd - BufBeg) < (signed) (sizeof("cfe-pth") + 3 + 4) ||
Ted Kremenek62224c12009-01-28 21:02:43 +0000441 memcmp(BufBeg, "cfe-pth", sizeof("cfe-pth") - 1) != 0) {
Ted Kremenekdc637a02009-03-22 06:42:39 +0000442 InvalidPTH(Diags, level);
Ted Kremenekeb8c8fb2009-01-26 21:43:14 +0000443 return 0;
Ted Kremenek62224c12009-01-28 21:02:43 +0000444 }
Mike Stump11289f42009-09-09 15:08:12 +0000445
Ted Kremenek978b5be2009-01-26 21:50:21 +0000446 // Read the PTH version.
Ted Kremenekeb8c8fb2009-01-26 21:43:14 +0000447 const unsigned char *p = BufBeg + (sizeof("cfe-pth") - 1);
Ted Kremenek978b5be2009-01-26 21:50:21 +0000448 unsigned Version = ReadLE32(p);
Mike Stump11289f42009-09-09 15:08:12 +0000449
Ted Kremenek62224c12009-01-28 21:02:43 +0000450 if (Version != PTHManager::Version) {
Ted Kremenekdc637a02009-03-22 06:42:39 +0000451 InvalidPTH(Diags, level,
Mike Stump11289f42009-09-09 15:08:12 +0000452 Version < PTHManager::Version
Ted Kremenek62224c12009-01-28 21:02:43 +0000453 ? "PTH file uses an older PTH format that is no longer supported"
454 : "PTH file uses a newer PTH format that cannot be read");
Ted Kremenek978b5be2009-01-26 21:50:21 +0000455 return 0;
Ted Kremenek62224c12009-01-28 21:02:43 +0000456 }
Ted Kremenek978b5be2009-01-26 21:50:21 +0000457
Mike Stump11289f42009-09-09 15:08:12 +0000458 // Compute the address of the index table at the end of the PTH file.
Ted Kremenek4c1d41f2009-02-11 23:34:32 +0000459 const unsigned char *PrologueOffset = p;
Mike Stump11289f42009-09-09 15:08:12 +0000460
Ted Kremenek4c1d41f2009-02-11 23:34:32 +0000461 if (PrologueOffset >= BufEnd) {
Ted Kremenekdc637a02009-03-22 06:42:39 +0000462 InvalidPTH(Diags, level);
Ted Kremenekeb8c8fb2009-01-26 21:43:14 +0000463 return 0;
Ted Kremenek62224c12009-01-28 21:02:43 +0000464 }
Mike Stump11289f42009-09-09 15:08:12 +0000465
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000466 // Construct the file lookup table. This will be used for mapping from
467 // FileEntry*'s to cached tokens.
Ted Kremenek4c1d41f2009-02-11 23:34:32 +0000468 const unsigned char* FileTableOffset = PrologueOffset + sizeof(uint32_t)*2;
Chris Lattnerfec54702009-01-22 19:48:26 +0000469 const unsigned char* FileTable = BufBeg + ReadLE32(FileTableOffset);
Mike Stump11289f42009-09-09 15:08:12 +0000470
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000471 if (!(FileTable > BufBeg && FileTable < BufEnd)) {
Ted Kremenekdc637a02009-03-22 06:42:39 +0000472 InvalidPTH(Diags, level);
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000473 return 0; // FIXME: Proper error diagnostic?
474 }
Mike Stump11289f42009-09-09 15:08:12 +0000475
Ted Kremenek86423a92009-02-10 22:16:22 +0000476 llvm::OwningPtr<PTHFileLookup> FL(PTHFileLookup::Create(FileTable, BufBeg));
Mike Stump11289f42009-09-09 15:08:12 +0000477
Ted Kremenek80e239a2009-03-20 17:54:25 +0000478 // Warn if the PTH file is empty. We still want to create a PTHManager
479 // as the PTH could be used with -include-pth.
480 if (FL->isEmpty())
Ted Kremenekdc637a02009-03-22 06:42:39 +0000481 InvalidPTH(Diags, level, "PTH file contains no cached source data");
Mike Stump11289f42009-09-09 15:08:12 +0000482
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000483 // Get the location of the table mapping from persistent ids to the
484 // data needed to reconstruct identifiers.
Ted Kremenek4c1d41f2009-02-11 23:34:32 +0000485 const unsigned char* IDTableOffset = PrologueOffset + sizeof(uint32_t)*0;
Chris Lattnerfec54702009-01-22 19:48:26 +0000486 const unsigned char* IData = BufBeg + ReadLE32(IDTableOffset);
Mike Stump11289f42009-09-09 15:08:12 +0000487
Ted Kremenek8d6c8282009-01-21 07:34:28 +0000488 if (!(IData >= BufBeg && IData < BufEnd)) {
Ted Kremenekdc637a02009-03-22 06:42:39 +0000489 InvalidPTH(Diags, level);
Ted Kremenek62224c12009-01-28 21:02:43 +0000490 return 0;
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000491 }
Mike Stump11289f42009-09-09 15:08:12 +0000492
Ted Kremeneke5554de2009-02-11 21:29:16 +0000493 // Get the location of the hashtable mapping between strings and
494 // persistent IDs.
Ted Kremenek4c1d41f2009-02-11 23:34:32 +0000495 const unsigned char* StringIdTableOffset = PrologueOffset + sizeof(uint32_t)*1;
Ted Kremeneke5554de2009-02-11 21:29:16 +0000496 const unsigned char* StringIdTable = BufBeg + ReadLE32(StringIdTableOffset);
497 if (!(StringIdTable >= BufBeg && StringIdTable < BufEnd)) {
Ted Kremenekdc637a02009-03-22 06:42:39 +0000498 InvalidPTH(Diags, level);
Ted Kremenek62224c12009-01-28 21:02:43 +0000499 return 0;
Ted Kremeneka705b042009-01-15 18:47:46 +0000500 }
Ted Kremeneke5554de2009-02-11 21:29:16 +0000501
502 llvm::OwningPtr<PTHStringIdLookup> SL(PTHStringIdLookup::Create(StringIdTable,
503 BufBeg));
Mike Stump11289f42009-09-09 15:08:12 +0000504
Ted Kremenek8d178f42009-01-27 00:01:05 +0000505 // Get the location of the spelling cache.
Ted Kremenek4c1d41f2009-02-11 23:34:32 +0000506 const unsigned char* spellingBaseOffset = PrologueOffset + sizeof(uint32_t)*3;
Ted Kremenek8d178f42009-01-27 00:01:05 +0000507 const unsigned char* spellingBase = BufBeg + ReadLE32(spellingBaseOffset);
508 if (!(spellingBase >= BufBeg && spellingBase < BufEnd)) {
Ted Kremenekdc637a02009-03-22 06:42:39 +0000509 InvalidPTH(Diags, level);
Ted Kremenek8d178f42009-01-27 00:01:05 +0000510 return 0;
511 }
Mike Stump11289f42009-09-09 15:08:12 +0000512
Ted Kremenek73a4d282008-12-03 01:16:39 +0000513 // Get the number of IdentifierInfos and pre-allocate the identifier cache.
Chris Lattnerfec54702009-01-22 19:48:26 +0000514 uint32_t NumIds = ReadLE32(IData);
Mike Stump11289f42009-09-09 15:08:12 +0000515
Ted Kremenek73a4d282008-12-03 01:16:39 +0000516 // Pre-allocate the peristent ID -> IdentifierInfo* cache. We use calloc()
517 // so that we in the best case only zero out memory once when the OS returns
518 // us new pages.
Ted Kremenek8d6c8282009-01-21 07:34:28 +0000519 IdentifierInfo** PerIDCache = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000520
Ted Kremenek8d6c8282009-01-21 07:34:28 +0000521 if (NumIds) {
Mike Stump11289f42009-09-09 15:08:12 +0000522 PerIDCache = (IdentifierInfo**)calloc(NumIds, sizeof(*PerIDCache));
Ted Kremenek8d6c8282009-01-21 07:34:28 +0000523 if (!PerIDCache) {
Mike Stump11289f42009-09-09 15:08:12 +0000524 InvalidPTH(Diags, level,
Ted Kremenekdc637a02009-03-22 06:42:39 +0000525 "Could not allocate memory for processing PTH file");
Ted Kremenek8d6c8282009-01-21 07:34:28 +0000526 return 0;
527 }
Ted Kremenek73a4d282008-12-03 01:16:39 +0000528 }
Ted Kremenek8d6c8282009-01-21 07:34:28 +0000529
Ted Kremenekf9ccd5c2009-03-19 22:19:30 +0000530 // Compute the address of the original source file.
531 const unsigned char* originalSourceBase = PrologueOffset + sizeof(uint32_t)*4;
532 unsigned len = ReadUnalignedLE16(originalSourceBase);
Mike Stump11289f42009-09-09 15:08:12 +0000533 if (!len) originalSourceBase = 0;
534
Ted Kremeneka705b042009-01-15 18:47:46 +0000535 // Create the new PTHManager.
536 return new PTHManager(File.take(), FL.take(), IData, PerIDCache,
Ted Kremenekf9ccd5c2009-03-19 22:19:30 +0000537 SL.take(), NumIds, spellingBase,
538 (const char*) originalSourceBase);
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000539}
Ted Kremenekf9ccd5c2009-03-19 22:19:30 +0000540
Chris Lattner144aacd2009-01-18 02:57:21 +0000541IdentifierInfo* PTHManager::LazilyCreateIdentifierInfo(unsigned PersistentID) {
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000542 // Look in the PTH file for the string data for the IdentifierInfo object.
Chris Lattner144aacd2009-01-18 02:57:21 +0000543 const unsigned char* TableEntry = IdDataTable + sizeof(uint32_t)*PersistentID;
Chris Lattnereb097542009-01-18 01:57:14 +0000544 const unsigned char* IDData =
Chris Lattnerfec54702009-01-22 19:48:26 +0000545 (const unsigned char*)Buf->getBufferStart() + ReadLE32(TableEntry);
Chris Lattnereb097542009-01-18 01:57:14 +0000546 assert(IDData < (const unsigned char*)Buf->getBufferEnd());
Mike Stump11289f42009-09-09 15:08:12 +0000547
Ted Kremeneka705b042009-01-15 18:47:46 +0000548 // Allocate the object.
Chris Lattnereb097542009-01-18 01:57:14 +0000549 std::pair<IdentifierInfo,const unsigned char*> *Mem =
550 Alloc.Allocate<std::pair<IdentifierInfo,const unsigned char*> >();
Ted Kremeneka705b042009-01-15 18:47:46 +0000551
552 Mem->second = IDData;
Ted Kremeneke5554de2009-02-11 21:29:16 +0000553 assert(IDData[0] != '\0');
Ted Kremenek52f73ca2009-01-20 23:28:34 +0000554 IdentifierInfo *II = new ((void*) Mem) IdentifierInfo();
Mike Stump11289f42009-09-09 15:08:12 +0000555
Ted Kremeneka705b042009-01-15 18:47:46 +0000556 // Store the new IdentifierInfo in the cache.
Chris Lattner144aacd2009-01-18 02:57:21 +0000557 PerIDCache[PersistentID] = II;
Daniel Dunbar2c422dc92009-10-18 20:26:12 +0000558 assert(II->getNameStart() && II->getNameStart()[0] != '\0');
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000559 return II;
560}
561
Ted Kremeneka705b042009-01-15 18:47:46 +0000562IdentifierInfo* PTHManager::get(const char *NameStart, const char *NameEnd) {
Ted Kremeneke5554de2009-02-11 21:29:16 +0000563 PTHStringIdLookup& SL = *((PTHStringIdLookup*)StringIdLookup);
564 // Double check our assumption that the last character isn't '\0'.
Daniel Dunbarad027c72009-02-12 19:31:53 +0000565 assert(NameEnd==NameStart || NameStart[NameEnd-NameStart-1] != '\0');
Ted Kremeneke5554de2009-02-11 21:29:16 +0000566 PTHStringIdLookup::iterator I = SL.find(std::make_pair(NameStart,
567 NameEnd - NameStart));
568 if (I == SL.end()) // No identifier found?
569 return 0;
Ted Kremeneka705b042009-01-15 18:47:46 +0000570
Ted Kremeneke5554de2009-02-11 21:29:16 +0000571 // Match found. Return the identifier!
572 assert(*I > 0);
573 return GetIdentifierInfo(*I-1);
574}
Ted Kremeneka705b042009-01-15 18:47:46 +0000575
Chris Lattnerab1d4b82009-01-17 08:06:50 +0000576PTHLexer *PTHManager::CreateLexer(FileID FID) {
577 const FileEntry *FE = PP->getSourceManager().getFileEntryForID(FID);
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000578 if (!FE)
579 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000580
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000581 // Lookup the FileEntry object in our file lookup data structure. It will
582 // return a variant that indicates whether or not there is an offset within
583 // the PTH file that contains cached tokens.
Ted Kremenek86423a92009-02-10 22:16:22 +0000584 PTHFileLookup& PFL = *((PTHFileLookup*)FileLookup);
585 PTHFileLookup::iterator I = PFL.find(FE);
Mike Stump11289f42009-09-09 15:08:12 +0000586
Ted Kremenek86423a92009-02-10 22:16:22 +0000587 if (I == PFL.end()) // No tokens available?
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000588 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000589
590 const PTHFileData& FileData = *I;
591
Chris Lattnereb097542009-01-18 01:57:14 +0000592 const unsigned char *BufStart = (const unsigned char *)Buf->getBufferStart();
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000593 // Compute the offset of the token data within the buffer.
Chris Lattnereb097542009-01-18 01:57:14 +0000594 const unsigned char* data = BufStart + FileData.getTokenOffset();
Ted Kremenek56572ab2008-12-12 18:34:08 +0000595
596 // Get the location of pp-conditional table.
Chris Lattnereb097542009-01-18 01:57:14 +0000597 const unsigned char* ppcond = BufStart + FileData.getPPCondOffset();
Chris Lattnerfec54702009-01-22 19:48:26 +0000598 uint32_t Len = ReadLE32(ppcond);
Chris Lattner137d6492009-01-18 02:10:31 +0000599 if (Len == 0) ppcond = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000600
Ted Kremeneka705b042009-01-15 18:47:46 +0000601 assert(PP && "No preprocessor set yet!");
Mike Stump11289f42009-09-09 15:08:12 +0000602 return new PTHLexer(*PP, FID, data, ppcond, *this);
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000603}
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000604
605//===----------------------------------------------------------------------===//
606// 'stat' caching.
607//===----------------------------------------------------------------------===//
608
609namespace {
610class VISIBILITY_HIDDEN PTHStatData {
611public:
Ted Kremenek29942a32009-02-13 19:13:46 +0000612 const bool hasStat;
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000613 const ino_t ino;
614 const dev_t dev;
615 const mode_t mode;
616 const time_t mtime;
617 const off_t size;
Mike Stump11289f42009-09-09 15:08:12 +0000618
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000619 PTHStatData(ino_t i, dev_t d, mode_t mo, time_t m, off_t s)
Mike Stump11289f42009-09-09 15:08:12 +0000620 : hasStat(true), ino(i), dev(d), mode(mo), mtime(m), size(s) {}
621
Ted Kremenek29942a32009-02-13 19:13:46 +0000622 PTHStatData()
623 : hasStat(false), ino(0), dev(0), mode(0), mtime(0), size(0) {}
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000624};
Mike Stump11289f42009-09-09 15:08:12 +0000625
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000626class VISIBILITY_HIDDEN PTHStatLookupTrait : public PTHFileLookupCommonTrait {
627public:
Ted Kremenek29942a32009-02-13 19:13:46 +0000628 typedef const char* external_key_type; // const char*
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000629 typedef PTHStatData data_type;
Mike Stump11289f42009-09-09 15:08:12 +0000630
Ted Kremenek29942a32009-02-13 19:13:46 +0000631 static internal_key_type GetInternalKey(const char *path) {
632 // The key 'kind' doesn't matter here because it is ignored in EqualKey.
633 return std::make_pair((unsigned char) 0x0, path);
634 }
635
636 static bool EqualKey(internal_key_type a, internal_key_type b) {
637 // When doing 'stat' lookups we don't care about the kind of 'a' and 'b',
638 // just the paths.
639 return strcmp(a.second, b.second) == 0;
Mike Stump11289f42009-09-09 15:08:12 +0000640 }
641
Ted Kremenek29942a32009-02-13 19:13:46 +0000642 static data_type ReadData(const internal_key_type& k, const unsigned char* d,
Mike Stump11289f42009-09-09 15:08:12 +0000643 unsigned) {
644
Ted Kremenek2fd18ec2009-02-13 22:07:44 +0000645 if (k.first /* File or Directory */) {
646 if (k.first == 0x1 /* File */) d += 4 * 2; // Skip the first 2 words.
Ted Kremenek29942a32009-02-13 19:13:46 +0000647 ino_t ino = (ino_t) ReadUnalignedLE32(d);
648 dev_t dev = (dev_t) ReadUnalignedLE32(d);
649 mode_t mode = (mode_t) ReadUnalignedLE16(d);
Mike Stump11289f42009-09-09 15:08:12 +0000650 time_t mtime = (time_t) ReadUnalignedLE64(d);
Ted Kremenek29942a32009-02-13 19:13:46 +0000651 return data_type(ino, dev, mode, mtime, (off_t) ReadUnalignedLE64(d));
652 }
Ted Kremenek2fd18ec2009-02-13 22:07:44 +0000653
654 // Negative stat. Don't read anything.
Ted Kremenek29942a32009-02-13 19:13:46 +0000655 return data_type();
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000656 }
657};
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000658
659class VISIBILITY_HIDDEN PTHStatCache : public StatSysCallCache {
660 typedef OnDiskChainedHashTable<PTHStatLookupTrait> CacheTy;
661 CacheTy Cache;
662
Mike Stump11289f42009-09-09 15:08:12 +0000663public:
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000664 PTHStatCache(PTHFileLookup &FL) :
665 Cache(FL.getNumBuckets(), FL.getNumEntries(), FL.getBuckets(),
666 FL.getBase()) {}
667
668 ~PTHStatCache() {}
Mike Stump11289f42009-09-09 15:08:12 +0000669
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000670 int stat(const char *path, struct stat *buf) {
671 // Do the lookup for the file's data in the PTH file.
672 CacheTy::iterator I = Cache.find(path);
673
674 // If we don't get a hit in the PTH file just forward to 'stat'.
Douglas Gregord2eb58a2009-10-16 18:18:30 +0000675 if (I == Cache.end())
676 return StatSysCallCache::stat(path, buf);
Mike Stump11289f42009-09-09 15:08:12 +0000677
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000678 const PTHStatData& Data = *I;
Mike Stump11289f42009-09-09 15:08:12 +0000679
Ted Kremenek2fd18ec2009-02-13 22:07:44 +0000680 if (!Data.hasStat)
681 return 1;
682
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000683 buf->st_ino = Data.ino;
684 buf->st_dev = Data.dev;
685 buf->st_mtime = Data.mtime;
686 buf->st_mode = Data.mode;
687 buf->st_size = Data.size;
688 return 0;
689 }
690};
Ted Kremenek29b86972009-02-23 23:27:54 +0000691} // end anonymous namespace
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000692
693StatSysCallCache *PTHManager::createStatCache() {
Ted Kremenekb4c85cc2009-02-12 03:45:39 +0000694 return new PTHStatCache(*((PTHFileLookup*) FileLookup));
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000695}