blob: 8ca1ec016fe3fb5cde29bcb563db3e4794f18650 [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 //===--------------------------------------==//
Mike Stump11289f42009-09-09 15:08:12 +000099#if 0
Ted Kremenek47b8cf62009-01-09 22:05:30 +0000100 SourceManager& SM = PP->getSourceManager();
Benjamin Kramer89b422c2009-08-23 12:08:50 +0000101 llvm::errs() << SM.getFileEntryForID(FileID)->getName()
Ted Kremenek47b8cf62009-01-09 22:05:30 +0000102 << ':' << SM.getLogicalLineNumber(Tok.getLocation())
103 << ':' << SM.getLogicalColumnNumber(Tok.getLocation())
104 << '\n';
Mike Stump11289f42009-09-09 15:08:12 +0000105#endif
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000106
Chris Lattner18fc6ce2009-01-18 02:34:01 +0000107 if (TKind == tok::eof) {
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000108 // Save the end-of-file token.
109 EofToken = Tok;
Mike Stump11289f42009-09-09 15:08:12 +0000110
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000111 Preprocessor *PPCache = PP;
Mike Stump11289f42009-09-09 15:08:12 +0000112
Ted Kremenek78cc2472008-12-23 19:24:24 +0000113 assert(!ParsingPreprocessorDirective);
114 assert(!LexingRawMode);
Mike Stump11289f42009-09-09 15:08:12 +0000115
Ted Kremenek78cc2472008-12-23 19:24:24 +0000116 // FIXME: Issue diagnostics similar to Lexer.
117 if (PP->HandleEndOfFile(Tok, false))
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000118 return;
Mike Stump11289f42009-09-09 15:08:12 +0000119
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000120 assert(PPCache && "Raw buffer::LexEndOfFile should return a token");
121 return PPCache->Lex(Tok);
122 }
Mike Stump11289f42009-09-09 15:08:12 +0000123
Chris Lattner18fc6ce2009-01-18 02:34:01 +0000124 if (TKind == tok::hash && Tok.isAtStartOfLine()) {
Ted Kremenek78cc2472008-12-23 19:24:24 +0000125 LastHashTokPtr = CurPtr - DISK_TOKEN_SIZE;
126 assert(!LexingRawMode);
127 PP->HandleDirective(Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000128
Ted Kremenek78cc2472008-12-23 19:24:24 +0000129 if (PP->isCurrentLexer(this))
130 goto LexNextToken;
Mike Stump11289f42009-09-09 15:08:12 +0000131
Ted Kremenek78cc2472008-12-23 19:24:24 +0000132 return PP->Lex(Tok);
133 }
Mike Stump11289f42009-09-09 15:08:12 +0000134
Chris Lattner18fc6ce2009-01-18 02:34:01 +0000135 if (TKind == tok::eom) {
Ted Kremenek78cc2472008-12-23 19:24:24 +0000136 assert(ParsingPreprocessorDirective);
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000137 ParsingPreprocessorDirective = false;
138 return;
139 }
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000140
Ted Kremenek78cc2472008-12-23 19:24:24 +0000141 MIOpt.ReadToken();
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000142}
143
144// FIXME: We can just grab the last token instead of storing a copy
145// into EofToken.
Ted Kremenek78cc2472008-12-23 19:24:24 +0000146void PTHLexer::getEOF(Token& Tok) {
Ted Kremenek8ae06622009-01-09 00:36:11 +0000147 assert(EofToken.is(tok::eof));
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000148 Tok = EofToken;
149}
150
151void PTHLexer::DiscardToEndOfLine() {
152 assert(ParsingPreprocessorDirective && ParsingFilename == false &&
153 "Must be in a preprocessing directive!");
154
155 // We assume that if the preprocessor wishes to discard to the end of
156 // the line that it also means to end the current preprocessor directive.
157 ParsingPreprocessorDirective = false;
Mike Stump11289f42009-09-09 15:08:12 +0000158
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000159 // Skip tokens by only peeking at their token kind and the flags.
160 // We don't need to actually reconstruct full tokens from the token buffer.
161 // This saves some copies and it also reduces IdentifierInfo* lookup.
Chris Lattnereb097542009-01-18 01:57:14 +0000162 const unsigned char* p = CurPtr;
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000163 while (1) {
164 // Read the token kind. Are we at the end of the file?
165 tok::TokenKind x = (tok::TokenKind) (uint8_t) *p;
166 if (x == tok::eof) break;
Mike Stump11289f42009-09-09 15:08:12 +0000167
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000168 // Read the token flags. Are we at the start of the next line?
169 Token::TokenFlags y = (Token::TokenFlags) (uint8_t) p[1];
170 if (y & Token::StartOfLine) break;
171
172 // Skip to the next token.
173 p += DISK_TOKEN_SIZE;
174 }
Mike Stump11289f42009-09-09 15:08:12 +0000175
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000176 CurPtr = p;
177}
178
Ted Kremenek56572ab2008-12-12 18:34:08 +0000179/// SkipBlock - Used by Preprocessor to skip the current conditional block.
180bool PTHLexer::SkipBlock() {
181 assert(CurPPCondPtr && "No cached PP conditional information.");
182 assert(LastHashTokPtr && "No known '#' token.");
Mike Stump11289f42009-09-09 15:08:12 +0000183
Chris Lattnereb097542009-01-18 01:57:14 +0000184 const unsigned char* HashEntryI = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000185 uint32_t Offset;
Ted Kremenek56572ab2008-12-12 18:34:08 +0000186 uint32_t TableIdx;
Mike Stump11289f42009-09-09 15:08:12 +0000187
Ted Kremenek56572ab2008-12-12 18:34:08 +0000188 do {
Ted Kremenek877556f2008-12-12 22:05:38 +0000189 // Read the token offset from the side-table.
Chris Lattnerfec54702009-01-22 19:48:26 +0000190 Offset = ReadLE32(CurPPCondPtr);
Mike Stump11289f42009-09-09 15:08:12 +0000191
192 // Read the target table index from the side-table.
Chris Lattnerfec54702009-01-22 19:48:26 +0000193 TableIdx = ReadLE32(CurPPCondPtr);
Mike Stump11289f42009-09-09 15:08:12 +0000194
Ted Kremenek877556f2008-12-12 22:05:38 +0000195 // Compute the actual memory address of the '#' token data for this entry.
196 HashEntryI = TokBuf + Offset;
197
198 // Optmization: "Sibling jumping". #if...#else...#endif blocks can
199 // contain nested blocks. In the side-table we can jump over these
200 // nested blocks instead of doing a linear search if the next "sibling"
201 // entry is not at a location greater than LastHashTokPtr.
202 if (HashEntryI < LastHashTokPtr && TableIdx) {
203 // In the side-table we are still at an entry for a '#' token that
204 // is earlier than the last one we saw. Check if the location we would
205 // stride gets us closer.
Chris Lattnereb097542009-01-18 01:57:14 +0000206 const unsigned char* NextPPCondPtr =
207 PPCond + TableIdx*(sizeof(uint32_t)*2);
Ted Kremenek877556f2008-12-12 22:05:38 +0000208 assert(NextPPCondPtr >= CurPPCondPtr);
209 // Read where we should jump to.
Chris Lattnerfec54702009-01-22 19:48:26 +0000210 uint32_t TmpOffset = ReadLE32(NextPPCondPtr);
Chris Lattnereb097542009-01-18 01:57:14 +0000211 const unsigned char* HashEntryJ = TokBuf + TmpOffset;
Mike Stump11289f42009-09-09 15:08:12 +0000212
Ted Kremenek877556f2008-12-12 22:05:38 +0000213 if (HashEntryJ <= LastHashTokPtr) {
214 // Jump directly to the next entry in the side table.
215 HashEntryI = HashEntryJ;
216 Offset = TmpOffset;
Chris Lattnerfec54702009-01-22 19:48:26 +0000217 TableIdx = ReadLE32(NextPPCondPtr);
Ted Kremenek877556f2008-12-12 22:05:38 +0000218 CurPPCondPtr = NextPPCondPtr;
219 }
220 }
Ted Kremenek56572ab2008-12-12 18:34:08 +0000221 }
Mike Stump11289f42009-09-09 15:08:12 +0000222 while (HashEntryI < LastHashTokPtr);
Ted Kremenek877556f2008-12-12 22:05:38 +0000223 assert(HashEntryI == LastHashTokPtr && "No PP-cond entry found for '#'");
Ted Kremenek56572ab2008-12-12 18:34:08 +0000224 assert(TableIdx && "No jumping from #endifs.");
Mike Stump11289f42009-09-09 15:08:12 +0000225
Ted Kremenek56572ab2008-12-12 18:34:08 +0000226 // Update our side-table iterator.
Chris Lattnereb097542009-01-18 01:57:14 +0000227 const unsigned char* NextPPCondPtr = PPCond + TableIdx*(sizeof(uint32_t)*2);
Ted Kremenek56572ab2008-12-12 18:34:08 +0000228 assert(NextPPCondPtr >= CurPPCondPtr);
229 CurPPCondPtr = NextPPCondPtr;
Mike Stump11289f42009-09-09 15:08:12 +0000230
Ted Kremenek56572ab2008-12-12 18:34:08 +0000231 // Read where we should jump to.
Chris Lattnerfec54702009-01-22 19:48:26 +0000232 HashEntryI = TokBuf + ReadLE32(NextPPCondPtr);
233 uint32_t NextIdx = ReadLE32(NextPPCondPtr);
Mike Stump11289f42009-09-09 15:08:12 +0000234
Ted Kremenek56572ab2008-12-12 18:34:08 +0000235 // By construction NextIdx will be zero if this is a #endif. This is useful
236 // to know to obviate lexing another token.
237 bool isEndif = NextIdx == 0;
Mike Stump11289f42009-09-09 15:08:12 +0000238
Ted Kremenek56572ab2008-12-12 18:34:08 +0000239 // This case can occur when we see something like this:
240 //
241 // #if ...
242 // /* a comment or nothing */
243 // #elif
244 //
245 // If we are skipping the first #if block it will be the case that CurPtr
246 // already points 'elif'. Just return.
Mike Stump11289f42009-09-09 15:08:12 +0000247
Ted Kremenek877556f2008-12-12 22:05:38 +0000248 if (CurPtr > HashEntryI) {
249 assert(CurPtr == HashEntryI + DISK_TOKEN_SIZE);
Ted Kremenek56572ab2008-12-12 18:34:08 +0000250 // Did we reach a #endif? If so, go ahead and consume that token as well.
251 if (isEndif)
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000252 CurPtr += DISK_TOKEN_SIZE*2;
Ted Kremenek56572ab2008-12-12 18:34:08 +0000253 else
Ted Kremenek877556f2008-12-12 22:05:38 +0000254 LastHashTokPtr = HashEntryI;
Mike Stump11289f42009-09-09 15:08:12 +0000255
Ted Kremenek56572ab2008-12-12 18:34:08 +0000256 return isEndif;
257 }
258
259 // Otherwise, we need to advance. Update CurPtr to point to the '#' token.
Ted Kremenek877556f2008-12-12 22:05:38 +0000260 CurPtr = HashEntryI;
Mike Stump11289f42009-09-09 15:08:12 +0000261
Ted Kremenek56572ab2008-12-12 18:34:08 +0000262 // Update the location of the last observed '#'. This is useful if we
263 // are skipping multiple blocks.
264 LastHashTokPtr = CurPtr;
Ted Kremenek56572ab2008-12-12 18:34:08 +0000265
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000266 // Skip the '#' token.
Chris Lattnereb097542009-01-18 01:57:14 +0000267 assert(((tok::TokenKind)*CurPtr) == tok::hash);
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000268 CurPtr += DISK_TOKEN_SIZE;
Mike Stump11289f42009-09-09 15:08:12 +0000269
Ted Kremenek56572ab2008-12-12 18:34:08 +0000270 // Did we reach a #endif? If so, go ahead and consume that token as well.
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000271 if (isEndif) { CurPtr += DISK_TOKEN_SIZE*2; }
Ted Kremenek56572ab2008-12-12 18:34:08 +0000272
273 return isEndif;
274}
275
Ted Kremenek63ff81c2008-12-17 23:36:32 +0000276SourceLocation PTHLexer::getSourceLocation() {
Chris Lattner137d6492009-01-18 02:10:31 +0000277 // getSourceLocation is not on the hot path. It is used to get the location
278 // of the next token when transitioning back to this lexer when done
Ted Kremenek63ff81c2008-12-17 23:36:32 +0000279 // handling a #included file. Just read the necessary data from the token
280 // data buffer to construct the SourceLocation object.
281 // NOTE: This is a virtual function; hence it is defined out-of-line.
Ted Kremenekae54f2f2009-01-21 22:41:38 +0000282 const unsigned char *OffsetPtr = CurPtr + (DISK_TOKEN_SIZE - 4);
Chris Lattnerfec54702009-01-22 19:48:26 +0000283 uint32_t Offset = ReadLE32(OffsetPtr);
Chris Lattner137d6492009-01-18 02:10:31 +0000284 return FileStartLoc.getFileLocWithOffset(Offset);
Ted Kremenek63ff81c2008-12-17 23:36:32 +0000285}
286
Ted Kremenek47b8cf62009-01-09 22:05:30 +0000287//===----------------------------------------------------------------------===//
Ted Kremenek86423a92009-02-10 22:16:22 +0000288// PTH file lookup: map from strings to file data.
289//===----------------------------------------------------------------------===//
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000290
291/// PTHFileLookup - This internal data structure is used by the PTHManager
292/// to map from FileEntry objects managed by FileManager to offsets within
293/// the PTH file.
294namespace {
Ted Kremenek86423a92009-02-10 22:16:22 +0000295class VISIBILITY_HIDDEN PTHFileData {
296 const uint32_t TokenOff;
297 const uint32_t PPCondOff;
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000298public:
Ted Kremenek86423a92009-02-10 22:16:22 +0000299 PTHFileData(uint32_t tokenOff, uint32_t ppCondOff)
300 : TokenOff(tokenOff), PPCondOff(ppCondOff) {}
Mike Stump11289f42009-09-09 15:08:12 +0000301
302 uint32_t getTokenOffset() const { return TokenOff; }
303 uint32_t getPPCondOffset() const { return PPCondOff; }
Ted Kremenek86423a92009-02-10 22:16:22 +0000304};
Mike Stump11289f42009-09-09 15:08:12 +0000305
306
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000307class VISIBILITY_HIDDEN PTHFileLookupCommonTrait {
Ted Kremenek86423a92009-02-10 22:16:22 +0000308public:
Ted Kremenek29942a32009-02-13 19:13:46 +0000309 typedef std::pair<unsigned char, const char*> internal_key_type;
310
311 static unsigned ComputeHash(internal_key_type x) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +0000312 return llvm::HashString(x.second);
Ted Kremenek86423a92009-02-10 22:16:22 +0000313 }
Mike Stump11289f42009-09-09 15:08:12 +0000314
Ted Kremenek86423a92009-02-10 22:16:22 +0000315 static std::pair<unsigned, unsigned>
316 ReadKeyDataLength(const unsigned char*& d) {
Ted Kremenek29942a32009-02-13 19:13:46 +0000317 unsigned keyLen = (unsigned) ReadUnalignedLE16(d);
318 unsigned dataLen = (unsigned) *(d++);
319 return std::make_pair(keyLen, dataLen);
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000320 }
Mike Stump11289f42009-09-09 15:08:12 +0000321
Ted Kremenek29942a32009-02-13 19:13:46 +0000322 static internal_key_type ReadKey(const unsigned char* d, unsigned) {
323 unsigned char k = *(d++); // Read the entry kind.
324 return std::make_pair(k, (const char*) d);
Ted Kremenek86423a92009-02-10 22:16:22 +0000325 }
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000326};
Mike Stump11289f42009-09-09 15:08:12 +0000327
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000328class VISIBILITY_HIDDEN PTHFileLookupTrait : public PTHFileLookupCommonTrait {
329public:
330 typedef const FileEntry* external_key_type;
331 typedef PTHFileData data_type;
Mike Stump11289f42009-09-09 15:08:12 +0000332
Ted Kremenek29942a32009-02-13 19:13:46 +0000333 static internal_key_type GetInternalKey(const FileEntry* FE) {
334 return std::make_pair((unsigned char) 0x1, FE->getName());
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000335 }
Ted Kremenek29942a32009-02-13 19:13:46 +0000336
337 static bool EqualKey(internal_key_type a, internal_key_type b) {
338 return a.first == b.first && strcmp(a.second, b.second) == 0;
Mike Stump11289f42009-09-09 15:08:12 +0000339 }
340
341 static PTHFileData ReadData(const internal_key_type& k,
342 const unsigned char* d, unsigned) {
Ted Kremenek29942a32009-02-13 19:13:46 +0000343 assert(k.first == 0x1 && "Only file lookups can match!");
Ted Kremenek86423a92009-02-10 22:16:22 +0000344 uint32_t x = ::ReadUnalignedLE32(d);
345 uint32_t y = ::ReadUnalignedLE32(d);
Mike Stump11289f42009-09-09 15:08:12 +0000346 return PTHFileData(x, y);
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000347 }
348};
Ted Kremeneke5554de2009-02-11 21:29:16 +0000349
350class VISIBILITY_HIDDEN PTHStringLookupTrait {
351public:
Mike Stump11289f42009-09-09 15:08:12 +0000352 typedef uint32_t
Ted Kremeneke5554de2009-02-11 21:29:16 +0000353 data_type;
354
355 typedef const std::pair<const char*, unsigned>
356 external_key_type;
357
358 typedef external_key_type internal_key_type;
Mike Stump11289f42009-09-09 15:08:12 +0000359
Ted Kremeneke5554de2009-02-11 21:29:16 +0000360 static bool EqualKey(const internal_key_type& a,
361 const internal_key_type& b) {
362 return (a.second == b.second) ? memcmp(a.first, b.first, a.second) == 0
363 : false;
364 }
Mike Stump11289f42009-09-09 15:08:12 +0000365
Ted Kremeneke5554de2009-02-11 21:29:16 +0000366 static unsigned ComputeHash(const internal_key_type& a) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +0000367 return llvm::HashString(llvm::StringRef(a.first, a.second));
Ted Kremeneke5554de2009-02-11 21:29:16 +0000368 }
Mike Stump11289f42009-09-09 15:08:12 +0000369
Ted Kremeneke5554de2009-02-11 21:29:16 +0000370 // This hopefully will just get inlined and removed by the optimizer.
371 static const internal_key_type&
372 GetInternalKey(const external_key_type& x) { return x; }
Mike Stump11289f42009-09-09 15:08:12 +0000373
Ted Kremeneke5554de2009-02-11 21:29:16 +0000374 static std::pair<unsigned, unsigned>
375 ReadKeyDataLength(const unsigned char*& d) {
376 return std::make_pair((unsigned) ReadUnalignedLE16(d), sizeof(uint32_t));
377 }
Mike Stump11289f42009-09-09 15:08:12 +0000378
Ted Kremeneke5554de2009-02-11 21:29:16 +0000379 static std::pair<const char*, unsigned>
380 ReadKey(const unsigned char* d, unsigned n) {
381 assert(n >= 2 && d[n-1] == '\0');
382 return std::make_pair((const char*) d, n-1);
383 }
Mike Stump11289f42009-09-09 15:08:12 +0000384
Ted Kremenek29942a32009-02-13 19:13:46 +0000385 static uint32_t ReadData(const internal_key_type& k, const unsigned char* d,
386 unsigned) {
Ted Kremeneke5554de2009-02-11 21:29:16 +0000387 return ::ReadUnalignedLE32(d);
388 }
389};
Mike Stump11289f42009-09-09 15:08:12 +0000390
391} // end anonymous namespace
Ted Kremenek86423a92009-02-10 22:16:22 +0000392
Ted Kremeneke5554de2009-02-11 21:29:16 +0000393typedef OnDiskChainedHashTable<PTHFileLookupTrait> PTHFileLookup;
394typedef OnDiskChainedHashTable<PTHStringLookupTrait> PTHStringIdLookup;
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000395
396//===----------------------------------------------------------------------===//
397// PTHManager methods.
398//===----------------------------------------------------------------------===//
399
400PTHManager::PTHManager(const llvm::MemoryBuffer* buf, void* fileLookup,
Chris Lattnereb097542009-01-18 01:57:14 +0000401 const unsigned char* idDataTable,
Mike Stump11289f42009-09-09 15:08:12 +0000402 IdentifierInfo** perIDCache,
Ted Kremeneke5554de2009-02-11 21:29:16 +0000403 void* stringIdLookup, unsigned numIds,
Ted Kremenekf9ccd5c2009-03-19 22:19:30 +0000404 const unsigned char* spellingBase,
405 const char* originalSourceFile)
Ted Kremenek73a4d282008-12-03 01:16:39 +0000406: Buf(buf), PerIDCache(perIDCache), FileLookup(fileLookup),
Ted Kremeneke5554de2009-02-11 21:29:16 +0000407 IdDataTable(idDataTable), StringIdLookup(stringIdLookup),
Ted Kremenekf9ccd5c2009-03-19 22:19:30 +0000408 NumIds(numIds), PP(0), SpellingBase(spellingBase),
409 OriginalSourceFile(originalSourceFile) {}
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000410
411PTHManager::~PTHManager() {
412 delete Buf;
413 delete (PTHFileLookup*) FileLookup;
Ted Kremeneke5554de2009-02-11 21:29:16 +0000414 delete (PTHStringIdLookup*) StringIdLookup;
Ted Kremenek1aed3dd2008-12-04 22:47:11 +0000415 free(PerIDCache);
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000416}
417
Ted Kremenekdc637a02009-03-22 06:42:39 +0000418static void InvalidPTH(Diagnostic *Diags, Diagnostic::Level level,
419 const char* Msg = 0) {
Mike Stump11289f42009-09-09 15:08:12 +0000420 if (!Diags) return;
Ted Kremenek62224c12009-01-28 21:02:43 +0000421 if (!Msg) Msg = "Invalid or corrupted PTH file";
Ted Kremenekdc637a02009-03-22 06:42:39 +0000422 unsigned DiagID = Diags->getCustomDiagID(level, Msg);
Ted Kremenek62224c12009-01-28 21:02:43 +0000423 Diags->Report(FullSourceLoc(), DiagID);
424}
425
Ted Kremenekdc637a02009-03-22 06:42:39 +0000426PTHManager* PTHManager::Create(const std::string& file, Diagnostic* Diags,
427 Diagnostic::Level level) {
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000428 // Memory map the PTH file.
429 llvm::OwningPtr<llvm::MemoryBuffer>
430 File(llvm::MemoryBuffer::getFile(file.c_str()));
Mike Stump11289f42009-09-09 15:08:12 +0000431
Ted Kremenek3b0589e2009-01-28 20:49:33 +0000432 if (!File) {
433 if (Diags) {
Ted Kremenekdc637a02009-03-22 06:42:39 +0000434 unsigned DiagID = Diags->getCustomDiagID(level,
Ted Kremenek3b0589e2009-01-28 20:49:33 +0000435 "PTH file %0 could not be read");
436 Diags->Report(FullSourceLoc(), DiagID) << file;
437 }
Ted Kremeneke5554de2009-02-11 21:29:16 +0000438
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000439 return 0;
Ted Kremenek3b0589e2009-01-28 20:49:33 +0000440 }
Mike Stump11289f42009-09-09 15:08:12 +0000441
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000442 // Get the buffer ranges and check if there are at least three 32-bit
443 // words at the end of the file.
Chris Lattnereb097542009-01-18 01:57:14 +0000444 const unsigned char* BufBeg = (unsigned char*)File->getBufferStart();
445 const unsigned char* BufEnd = (unsigned char*)File->getBufferEnd();
Ted Kremenekeb8c8fb2009-01-26 21:43:14 +0000446
447 // Check the prologue of the file.
Ted Kremenek327d00c2009-01-26 22:16:12 +0000448 if ((BufEnd - BufBeg) < (signed) (sizeof("cfe-pth") + 3 + 4) ||
Ted Kremenek62224c12009-01-28 21:02:43 +0000449 memcmp(BufBeg, "cfe-pth", sizeof("cfe-pth") - 1) != 0) {
Ted Kremenekdc637a02009-03-22 06:42:39 +0000450 InvalidPTH(Diags, level);
Ted Kremenekeb8c8fb2009-01-26 21:43:14 +0000451 return 0;
Ted Kremenek62224c12009-01-28 21:02:43 +0000452 }
Mike Stump11289f42009-09-09 15:08:12 +0000453
Ted Kremenek978b5be2009-01-26 21:50:21 +0000454 // Read the PTH version.
Ted Kremenekeb8c8fb2009-01-26 21:43:14 +0000455 const unsigned char *p = BufBeg + (sizeof("cfe-pth") - 1);
Ted Kremenek978b5be2009-01-26 21:50:21 +0000456 unsigned Version = ReadLE32(p);
Mike Stump11289f42009-09-09 15:08:12 +0000457
Ted Kremenek62224c12009-01-28 21:02:43 +0000458 if (Version != PTHManager::Version) {
Ted Kremenekdc637a02009-03-22 06:42:39 +0000459 InvalidPTH(Diags, level,
Mike Stump11289f42009-09-09 15:08:12 +0000460 Version < PTHManager::Version
Ted Kremenek62224c12009-01-28 21:02:43 +0000461 ? "PTH file uses an older PTH format that is no longer supported"
462 : "PTH file uses a newer PTH format that cannot be read");
Ted Kremenek978b5be2009-01-26 21:50:21 +0000463 return 0;
Ted Kremenek62224c12009-01-28 21:02:43 +0000464 }
Ted Kremenek978b5be2009-01-26 21:50:21 +0000465
Mike Stump11289f42009-09-09 15:08:12 +0000466 // Compute the address of the index table at the end of the PTH file.
Ted Kremenek4c1d41f2009-02-11 23:34:32 +0000467 const unsigned char *PrologueOffset = p;
Mike Stump11289f42009-09-09 15:08:12 +0000468
Ted Kremenek4c1d41f2009-02-11 23:34:32 +0000469 if (PrologueOffset >= BufEnd) {
Ted Kremenekdc637a02009-03-22 06:42:39 +0000470 InvalidPTH(Diags, level);
Ted Kremenekeb8c8fb2009-01-26 21:43:14 +0000471 return 0;
Ted Kremenek62224c12009-01-28 21:02:43 +0000472 }
Mike Stump11289f42009-09-09 15:08:12 +0000473
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000474 // Construct the file lookup table. This will be used for mapping from
475 // FileEntry*'s to cached tokens.
Ted Kremenek4c1d41f2009-02-11 23:34:32 +0000476 const unsigned char* FileTableOffset = PrologueOffset + sizeof(uint32_t)*2;
Chris Lattnerfec54702009-01-22 19:48:26 +0000477 const unsigned char* FileTable = BufBeg + ReadLE32(FileTableOffset);
Mike Stump11289f42009-09-09 15:08:12 +0000478
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000479 if (!(FileTable > BufBeg && FileTable < BufEnd)) {
Ted Kremenekdc637a02009-03-22 06:42:39 +0000480 InvalidPTH(Diags, level);
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000481 return 0; // FIXME: Proper error diagnostic?
482 }
Mike Stump11289f42009-09-09 15:08:12 +0000483
Ted Kremenek86423a92009-02-10 22:16:22 +0000484 llvm::OwningPtr<PTHFileLookup> FL(PTHFileLookup::Create(FileTable, BufBeg));
Mike Stump11289f42009-09-09 15:08:12 +0000485
Ted Kremenek80e239a2009-03-20 17:54:25 +0000486 // Warn if the PTH file is empty. We still want to create a PTHManager
487 // as the PTH could be used with -include-pth.
488 if (FL->isEmpty())
Ted Kremenekdc637a02009-03-22 06:42:39 +0000489 InvalidPTH(Diags, level, "PTH file contains no cached source data");
Mike Stump11289f42009-09-09 15:08:12 +0000490
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000491 // Get the location of the table mapping from persistent ids to the
492 // data needed to reconstruct identifiers.
Ted Kremenek4c1d41f2009-02-11 23:34:32 +0000493 const unsigned char* IDTableOffset = PrologueOffset + sizeof(uint32_t)*0;
Chris Lattnerfec54702009-01-22 19:48:26 +0000494 const unsigned char* IData = BufBeg + ReadLE32(IDTableOffset);
Mike Stump11289f42009-09-09 15:08:12 +0000495
Ted Kremenek8d6c8282009-01-21 07:34:28 +0000496 if (!(IData >= BufBeg && IData < BufEnd)) {
Ted Kremenekdc637a02009-03-22 06:42:39 +0000497 InvalidPTH(Diags, level);
Ted Kremenek62224c12009-01-28 21:02:43 +0000498 return 0;
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000499 }
Mike Stump11289f42009-09-09 15:08:12 +0000500
Ted Kremeneke5554de2009-02-11 21:29:16 +0000501 // Get the location of the hashtable mapping between strings and
502 // persistent IDs.
Ted Kremenek4c1d41f2009-02-11 23:34:32 +0000503 const unsigned char* StringIdTableOffset = PrologueOffset + sizeof(uint32_t)*1;
Ted Kremeneke5554de2009-02-11 21:29:16 +0000504 const unsigned char* StringIdTable = BufBeg + ReadLE32(StringIdTableOffset);
505 if (!(StringIdTable >= BufBeg && StringIdTable < BufEnd)) {
Ted Kremenekdc637a02009-03-22 06:42:39 +0000506 InvalidPTH(Diags, level);
Ted Kremenek62224c12009-01-28 21:02:43 +0000507 return 0;
Ted Kremeneka705b042009-01-15 18:47:46 +0000508 }
Ted Kremeneke5554de2009-02-11 21:29:16 +0000509
510 llvm::OwningPtr<PTHStringIdLookup> SL(PTHStringIdLookup::Create(StringIdTable,
511 BufBeg));
Mike Stump11289f42009-09-09 15:08:12 +0000512
Ted Kremenek8d178f42009-01-27 00:01:05 +0000513 // Get the location of the spelling cache.
Ted Kremenek4c1d41f2009-02-11 23:34:32 +0000514 const unsigned char* spellingBaseOffset = PrologueOffset + sizeof(uint32_t)*3;
Ted Kremenek8d178f42009-01-27 00:01:05 +0000515 const unsigned char* spellingBase = BufBeg + ReadLE32(spellingBaseOffset);
516 if (!(spellingBase >= BufBeg && spellingBase < BufEnd)) {
Ted Kremenekdc637a02009-03-22 06:42:39 +0000517 InvalidPTH(Diags, level);
Ted Kremenek8d178f42009-01-27 00:01:05 +0000518 return 0;
519 }
Mike Stump11289f42009-09-09 15:08:12 +0000520
Ted Kremenek73a4d282008-12-03 01:16:39 +0000521 // Get the number of IdentifierInfos and pre-allocate the identifier cache.
Chris Lattnerfec54702009-01-22 19:48:26 +0000522 uint32_t NumIds = ReadLE32(IData);
Mike Stump11289f42009-09-09 15:08:12 +0000523
Ted Kremenek73a4d282008-12-03 01:16:39 +0000524 // Pre-allocate the peristent ID -> IdentifierInfo* cache. We use calloc()
525 // so that we in the best case only zero out memory once when the OS returns
526 // us new pages.
Ted Kremenek8d6c8282009-01-21 07:34:28 +0000527 IdentifierInfo** PerIDCache = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000528
Ted Kremenek8d6c8282009-01-21 07:34:28 +0000529 if (NumIds) {
Mike Stump11289f42009-09-09 15:08:12 +0000530 PerIDCache = (IdentifierInfo**)calloc(NumIds, sizeof(*PerIDCache));
Ted Kremenek8d6c8282009-01-21 07:34:28 +0000531 if (!PerIDCache) {
Mike Stump11289f42009-09-09 15:08:12 +0000532 InvalidPTH(Diags, level,
Ted Kremenekdc637a02009-03-22 06:42:39 +0000533 "Could not allocate memory for processing PTH file");
Ted Kremenek8d6c8282009-01-21 07:34:28 +0000534 return 0;
535 }
Ted Kremenek73a4d282008-12-03 01:16:39 +0000536 }
Ted Kremenek8d6c8282009-01-21 07:34:28 +0000537
Ted Kremenekf9ccd5c2009-03-19 22:19:30 +0000538 // Compute the address of the original source file.
539 const unsigned char* originalSourceBase = PrologueOffset + sizeof(uint32_t)*4;
540 unsigned len = ReadUnalignedLE16(originalSourceBase);
Mike Stump11289f42009-09-09 15:08:12 +0000541 if (!len) originalSourceBase = 0;
542
Ted Kremeneka705b042009-01-15 18:47:46 +0000543 // Create the new PTHManager.
544 return new PTHManager(File.take(), FL.take(), IData, PerIDCache,
Ted Kremenekf9ccd5c2009-03-19 22:19:30 +0000545 SL.take(), NumIds, spellingBase,
546 (const char*) originalSourceBase);
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000547}
Ted Kremenekf9ccd5c2009-03-19 22:19:30 +0000548
Chris Lattner144aacd2009-01-18 02:57:21 +0000549IdentifierInfo* PTHManager::LazilyCreateIdentifierInfo(unsigned PersistentID) {
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000550 // Look in the PTH file for the string data for the IdentifierInfo object.
Chris Lattner144aacd2009-01-18 02:57:21 +0000551 const unsigned char* TableEntry = IdDataTable + sizeof(uint32_t)*PersistentID;
Chris Lattnereb097542009-01-18 01:57:14 +0000552 const unsigned char* IDData =
Chris Lattnerfec54702009-01-22 19:48:26 +0000553 (const unsigned char*)Buf->getBufferStart() + ReadLE32(TableEntry);
Chris Lattnereb097542009-01-18 01:57:14 +0000554 assert(IDData < (const unsigned char*)Buf->getBufferEnd());
Mike Stump11289f42009-09-09 15:08:12 +0000555
Ted Kremeneka705b042009-01-15 18:47:46 +0000556 // Allocate the object.
Chris Lattnereb097542009-01-18 01:57:14 +0000557 std::pair<IdentifierInfo,const unsigned char*> *Mem =
558 Alloc.Allocate<std::pair<IdentifierInfo,const unsigned char*> >();
Ted Kremeneka705b042009-01-15 18:47:46 +0000559
560 Mem->second = IDData;
Ted Kremeneke5554de2009-02-11 21:29:16 +0000561 assert(IDData[0] != '\0');
Ted Kremenek52f73ca2009-01-20 23:28:34 +0000562 IdentifierInfo *II = new ((void*) Mem) IdentifierInfo();
Mike Stump11289f42009-09-09 15:08:12 +0000563
Ted Kremeneka705b042009-01-15 18:47:46 +0000564 // Store the new IdentifierInfo in the cache.
Chris Lattner144aacd2009-01-18 02:57:21 +0000565 PerIDCache[PersistentID] = II;
Ted Kremeneke5554de2009-02-11 21:29:16 +0000566 assert(II->getName() && II->getName()[0] != '\0');
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000567 return II;
568}
569
Ted Kremeneka705b042009-01-15 18:47:46 +0000570IdentifierInfo* PTHManager::get(const char *NameStart, const char *NameEnd) {
Ted Kremeneke5554de2009-02-11 21:29:16 +0000571 PTHStringIdLookup& SL = *((PTHStringIdLookup*)StringIdLookup);
572 // Double check our assumption that the last character isn't '\0'.
Daniel Dunbarad027c72009-02-12 19:31:53 +0000573 assert(NameEnd==NameStart || NameStart[NameEnd-NameStart-1] != '\0');
Ted Kremeneke5554de2009-02-11 21:29:16 +0000574 PTHStringIdLookup::iterator I = SL.find(std::make_pair(NameStart,
575 NameEnd - NameStart));
576 if (I == SL.end()) // No identifier found?
577 return 0;
Ted Kremeneka705b042009-01-15 18:47:46 +0000578
Ted Kremeneke5554de2009-02-11 21:29:16 +0000579 // Match found. Return the identifier!
580 assert(*I > 0);
581 return GetIdentifierInfo(*I-1);
582}
Ted Kremeneka705b042009-01-15 18:47:46 +0000583
Chris Lattnerab1d4b82009-01-17 08:06:50 +0000584PTHLexer *PTHManager::CreateLexer(FileID FID) {
585 const FileEntry *FE = PP->getSourceManager().getFileEntryForID(FID);
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000586 if (!FE)
587 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000588
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000589 // Lookup the FileEntry object in our file lookup data structure. It will
590 // return a variant that indicates whether or not there is an offset within
591 // the PTH file that contains cached tokens.
Ted Kremenek86423a92009-02-10 22:16:22 +0000592 PTHFileLookup& PFL = *((PTHFileLookup*)FileLookup);
593 PTHFileLookup::iterator I = PFL.find(FE);
Mike Stump11289f42009-09-09 15:08:12 +0000594
Ted Kremenek86423a92009-02-10 22:16:22 +0000595 if (I == PFL.end()) // No tokens available?
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000596 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000597
598 const PTHFileData& FileData = *I;
599
Chris Lattnereb097542009-01-18 01:57:14 +0000600 const unsigned char *BufStart = (const unsigned char *)Buf->getBufferStart();
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000601 // Compute the offset of the token data within the buffer.
Chris Lattnereb097542009-01-18 01:57:14 +0000602 const unsigned char* data = BufStart + FileData.getTokenOffset();
Ted Kremenek56572ab2008-12-12 18:34:08 +0000603
604 // Get the location of pp-conditional table.
Chris Lattnereb097542009-01-18 01:57:14 +0000605 const unsigned char* ppcond = BufStart + FileData.getPPCondOffset();
Chris Lattnerfec54702009-01-22 19:48:26 +0000606 uint32_t Len = ReadLE32(ppcond);
Chris Lattner137d6492009-01-18 02:10:31 +0000607 if (Len == 0) ppcond = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000608
Ted Kremeneka705b042009-01-15 18:47:46 +0000609 assert(PP && "No preprocessor set yet!");
Mike Stump11289f42009-09-09 15:08:12 +0000610 return new PTHLexer(*PP, FID, data, ppcond, *this);
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000611}
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000612
613//===----------------------------------------------------------------------===//
614// 'stat' caching.
615//===----------------------------------------------------------------------===//
616
617namespace {
618class VISIBILITY_HIDDEN PTHStatData {
619public:
Ted Kremenek29942a32009-02-13 19:13:46 +0000620 const bool hasStat;
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000621 const ino_t ino;
622 const dev_t dev;
623 const mode_t mode;
624 const time_t mtime;
625 const off_t size;
Mike Stump11289f42009-09-09 15:08:12 +0000626
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000627 PTHStatData(ino_t i, dev_t d, mode_t mo, time_t m, off_t s)
Mike Stump11289f42009-09-09 15:08:12 +0000628 : hasStat(true), ino(i), dev(d), mode(mo), mtime(m), size(s) {}
629
Ted Kremenek29942a32009-02-13 19:13:46 +0000630 PTHStatData()
631 : hasStat(false), ino(0), dev(0), mode(0), mtime(0), size(0) {}
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000632};
Mike Stump11289f42009-09-09 15:08:12 +0000633
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000634class VISIBILITY_HIDDEN PTHStatLookupTrait : public PTHFileLookupCommonTrait {
635public:
Ted Kremenek29942a32009-02-13 19:13:46 +0000636 typedef const char* external_key_type; // const char*
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000637 typedef PTHStatData data_type;
Mike Stump11289f42009-09-09 15:08:12 +0000638
Ted Kremenek29942a32009-02-13 19:13:46 +0000639 static internal_key_type GetInternalKey(const char *path) {
640 // The key 'kind' doesn't matter here because it is ignored in EqualKey.
641 return std::make_pair((unsigned char) 0x0, path);
642 }
643
644 static bool EqualKey(internal_key_type a, internal_key_type b) {
645 // When doing 'stat' lookups we don't care about the kind of 'a' and 'b',
646 // just the paths.
647 return strcmp(a.second, b.second) == 0;
Mike Stump11289f42009-09-09 15:08:12 +0000648 }
649
Ted Kremenek29942a32009-02-13 19:13:46 +0000650 static data_type ReadData(const internal_key_type& k, const unsigned char* d,
Mike Stump11289f42009-09-09 15:08:12 +0000651 unsigned) {
652
Ted Kremenek2fd18ec2009-02-13 22:07:44 +0000653 if (k.first /* File or Directory */) {
654 if (k.first == 0x1 /* File */) d += 4 * 2; // Skip the first 2 words.
Ted Kremenek29942a32009-02-13 19:13:46 +0000655 ino_t ino = (ino_t) ReadUnalignedLE32(d);
656 dev_t dev = (dev_t) ReadUnalignedLE32(d);
657 mode_t mode = (mode_t) ReadUnalignedLE16(d);
Mike Stump11289f42009-09-09 15:08:12 +0000658 time_t mtime = (time_t) ReadUnalignedLE64(d);
Ted Kremenek29942a32009-02-13 19:13:46 +0000659 return data_type(ino, dev, mode, mtime, (off_t) ReadUnalignedLE64(d));
660 }
Ted Kremenek2fd18ec2009-02-13 22:07:44 +0000661
662 // Negative stat. Don't read anything.
Ted Kremenek29942a32009-02-13 19:13:46 +0000663 return data_type();
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000664 }
665};
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000666
667class VISIBILITY_HIDDEN PTHStatCache : public StatSysCallCache {
668 typedef OnDiskChainedHashTable<PTHStatLookupTrait> CacheTy;
669 CacheTy Cache;
670
Mike Stump11289f42009-09-09 15:08:12 +0000671public:
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000672 PTHStatCache(PTHFileLookup &FL) :
673 Cache(FL.getNumBuckets(), FL.getNumEntries(), FL.getBuckets(),
674 FL.getBase()) {}
675
676 ~PTHStatCache() {}
Mike Stump11289f42009-09-09 15:08:12 +0000677
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000678 int stat(const char *path, struct stat *buf) {
679 // Do the lookup for the file's data in the PTH file.
680 CacheTy::iterator I = Cache.find(path);
681
682 // If we don't get a hit in the PTH file just forward to 'stat'.
Douglas Gregord2eb58a2009-10-16 18:18:30 +0000683 if (I == Cache.end())
684 return StatSysCallCache::stat(path, buf);
Mike Stump11289f42009-09-09 15:08:12 +0000685
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000686 const PTHStatData& Data = *I;
Mike Stump11289f42009-09-09 15:08:12 +0000687
Ted Kremenek2fd18ec2009-02-13 22:07:44 +0000688 if (!Data.hasStat)
689 return 1;
690
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000691 buf->st_ino = Data.ino;
692 buf->st_dev = Data.dev;
693 buf->st_mtime = Data.mtime;
694 buf->st_mode = Data.mode;
695 buf->st_size = Data.size;
696 return 0;
697 }
698};
Ted Kremenek29b86972009-02-23 23:27:54 +0000699} // end anonymous namespace
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000700
701StatSysCallCache *PTHManager::createStatCache() {
Ted Kremenekb4c85cc2009-02-12 03:45:39 +0000702 return new PTHStatCache(*((PTHFileLookup*) FileLookup));
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000703}