blob: 4fc3a591d910486b2a42e14a57d7ccb3a1ddd52c [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"
Daniel Dunbar1a54e3f2009-11-12 02:53:48 +000018#include "clang/Lex/LexDiagnostic.h"
Ted Kremenek7cd62452008-11-12 21:37:15 +000019#include "clang/Lex/PTHLexer.h"
20#include "clang/Lex/Preprocessor.h"
Ted Kremenek33eeabd2008-12-03 00:38:03 +000021#include "clang/Lex/PTHManager.h"
22#include "clang/Lex/Token.h"
23#include "clang/Lex/Preprocessor.h"
Ted Kremenek33eeabd2008-12-03 00:38:03 +000024#include "llvm/ADT/OwningPtr.h"
Daniel Dunbarf8502d52009-10-17 23:52:28 +000025#include "llvm/ADT/StringExtras.h"
26#include "llvm/ADT/StringMap.h"
Kovarththanan Rajaratnam930de0a2009-11-28 16:08:10 +000027#include "llvm/Support/Compiler.h"
Chris Lattner34eab392009-01-22 23:50:07 +000028#include "llvm/Support/MemoryBuffer.h"
Ted Kremeneka5c2c272009-02-12 03:26:59 +000029#include <sys/stat.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);
Chris Lattnerd32480d2009-01-17 06:22:33 +000076 Tok.setLocation(FileStartLoc.getFileLocWithOffset(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 Kremenek1b18ad22008-12-23 01:30:52 +0000105 Preprocessor *PPCache = PP;
Mike Stump11289f42009-09-09 15:08:12 +0000106
Ted Kremenek78cc2472008-12-23 19:24:24 +0000107 assert(!ParsingPreprocessorDirective);
108 assert(!LexingRawMode);
Mike Stump11289f42009-09-09 15:08:12 +0000109
Ted Kremenek78cc2472008-12-23 19:24:24 +0000110 // FIXME: Issue diagnostics similar to Lexer.
111 if (PP->HandleEndOfFile(Tok, false))
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 assert(PPCache && "Raw buffer::LexEndOfFile should return a token");
115 return PPCache->Lex(Tok);
116 }
Mike Stump11289f42009-09-09 15:08:12 +0000117
Chris Lattner18fc6ce2009-01-18 02:34:01 +0000118 if (TKind == tok::hash && Tok.isAtStartOfLine()) {
Ted Kremenek78cc2472008-12-23 19:24:24 +0000119 LastHashTokPtr = CurPtr - DISK_TOKEN_SIZE;
120 assert(!LexingRawMode);
121 PP->HandleDirective(Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000122
Ted Kremenek78cc2472008-12-23 19:24:24 +0000123 if (PP->isCurrentLexer(this))
124 goto LexNextToken;
Mike Stump11289f42009-09-09 15:08:12 +0000125
Ted Kremenek78cc2472008-12-23 19:24:24 +0000126 return PP->Lex(Tok);
127 }
Mike Stump11289f42009-09-09 15:08:12 +0000128
Chris Lattner18fc6ce2009-01-18 02:34:01 +0000129 if (TKind == tok::eom) {
Ted Kremenek78cc2472008-12-23 19:24:24 +0000130 assert(ParsingPreprocessorDirective);
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000131 ParsingPreprocessorDirective = false;
132 return;
133 }
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000134
Ted Kremenek78cc2472008-12-23 19:24:24 +0000135 MIOpt.ReadToken();
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000136}
137
138// FIXME: We can just grab the last token instead of storing a copy
139// into EofToken.
Ted Kremenek78cc2472008-12-23 19:24:24 +0000140void PTHLexer::getEOF(Token& Tok) {
Ted Kremenek8ae06622009-01-09 00:36:11 +0000141 assert(EofToken.is(tok::eof));
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000142 Tok = EofToken;
143}
144
145void PTHLexer::DiscardToEndOfLine() {
146 assert(ParsingPreprocessorDirective && ParsingFilename == false &&
147 "Must be in a preprocessing directive!");
148
149 // We assume that if the preprocessor wishes to discard to the end of
150 // the line that it also means to end the current preprocessor directive.
151 ParsingPreprocessorDirective = false;
Mike Stump11289f42009-09-09 15:08:12 +0000152
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000153 // Skip tokens by only peeking at their token kind and the flags.
154 // We don't need to actually reconstruct full tokens from the token buffer.
155 // This saves some copies and it also reduces IdentifierInfo* lookup.
Chris Lattnereb097542009-01-18 01:57:14 +0000156 const unsigned char* p = CurPtr;
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000157 while (1) {
158 // Read the token kind. Are we at the end of the file?
159 tok::TokenKind x = (tok::TokenKind) (uint8_t) *p;
160 if (x == tok::eof) break;
Mike Stump11289f42009-09-09 15:08:12 +0000161
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000162 // Read the token flags. Are we at the start of the next line?
163 Token::TokenFlags y = (Token::TokenFlags) (uint8_t) p[1];
164 if (y & Token::StartOfLine) break;
165
166 // Skip to the next token.
167 p += DISK_TOKEN_SIZE;
168 }
Mike Stump11289f42009-09-09 15:08:12 +0000169
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000170 CurPtr = p;
171}
172
Ted Kremenek56572ab2008-12-12 18:34:08 +0000173/// SkipBlock - Used by Preprocessor to skip the current conditional block.
174bool PTHLexer::SkipBlock() {
175 assert(CurPPCondPtr && "No cached PP conditional information.");
176 assert(LastHashTokPtr && "No known '#' token.");
Mike Stump11289f42009-09-09 15:08:12 +0000177
Chris Lattnereb097542009-01-18 01:57:14 +0000178 const unsigned char* HashEntryI = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000179 uint32_t Offset;
Ted Kremenek56572ab2008-12-12 18:34:08 +0000180 uint32_t TableIdx;
Mike Stump11289f42009-09-09 15:08:12 +0000181
Ted Kremenek56572ab2008-12-12 18:34:08 +0000182 do {
Ted Kremenek877556f2008-12-12 22:05:38 +0000183 // Read the token offset from the side-table.
Chris Lattnerfec54702009-01-22 19:48:26 +0000184 Offset = ReadLE32(CurPPCondPtr);
Mike Stump11289f42009-09-09 15:08:12 +0000185
186 // Read the target table index from the side-table.
Chris Lattnerfec54702009-01-22 19:48:26 +0000187 TableIdx = ReadLE32(CurPPCondPtr);
Mike Stump11289f42009-09-09 15:08:12 +0000188
Ted Kremenek877556f2008-12-12 22:05:38 +0000189 // Compute the actual memory address of the '#' token data for this entry.
190 HashEntryI = TokBuf + Offset;
191
192 // Optmization: "Sibling jumping". #if...#else...#endif blocks can
193 // contain nested blocks. In the side-table we can jump over these
194 // nested blocks instead of doing a linear search if the next "sibling"
195 // entry is not at a location greater than LastHashTokPtr.
196 if (HashEntryI < LastHashTokPtr && TableIdx) {
197 // In the side-table we are still at an entry for a '#' token that
198 // is earlier than the last one we saw. Check if the location we would
199 // stride gets us closer.
Chris Lattnereb097542009-01-18 01:57:14 +0000200 const unsigned char* NextPPCondPtr =
201 PPCond + TableIdx*(sizeof(uint32_t)*2);
Ted Kremenek877556f2008-12-12 22:05:38 +0000202 assert(NextPPCondPtr >= CurPPCondPtr);
203 // Read where we should jump to.
Chris Lattnerfec54702009-01-22 19:48:26 +0000204 uint32_t TmpOffset = ReadLE32(NextPPCondPtr);
Chris Lattnereb097542009-01-18 01:57:14 +0000205 const unsigned char* HashEntryJ = TokBuf + TmpOffset;
Mike Stump11289f42009-09-09 15:08:12 +0000206
Ted Kremenek877556f2008-12-12 22:05:38 +0000207 if (HashEntryJ <= LastHashTokPtr) {
208 // Jump directly to the next entry in the side table.
209 HashEntryI = HashEntryJ;
210 Offset = TmpOffset;
Chris Lattnerfec54702009-01-22 19:48:26 +0000211 TableIdx = ReadLE32(NextPPCondPtr);
Ted Kremenek877556f2008-12-12 22:05:38 +0000212 CurPPCondPtr = NextPPCondPtr;
213 }
214 }
Ted Kremenek56572ab2008-12-12 18:34:08 +0000215 }
Mike Stump11289f42009-09-09 15:08:12 +0000216 while (HashEntryI < LastHashTokPtr);
Ted Kremenek877556f2008-12-12 22:05:38 +0000217 assert(HashEntryI == LastHashTokPtr && "No PP-cond entry found for '#'");
Ted Kremenek56572ab2008-12-12 18:34:08 +0000218 assert(TableIdx && "No jumping from #endifs.");
Mike Stump11289f42009-09-09 15:08:12 +0000219
Ted Kremenek56572ab2008-12-12 18:34:08 +0000220 // Update our side-table iterator.
Chris Lattnereb097542009-01-18 01:57:14 +0000221 const unsigned char* NextPPCondPtr = PPCond + TableIdx*(sizeof(uint32_t)*2);
Ted Kremenek56572ab2008-12-12 18:34:08 +0000222 assert(NextPPCondPtr >= CurPPCondPtr);
223 CurPPCondPtr = NextPPCondPtr;
Mike Stump11289f42009-09-09 15:08:12 +0000224
Ted Kremenek56572ab2008-12-12 18:34:08 +0000225 // Read where we should jump to.
Chris Lattnerfec54702009-01-22 19:48:26 +0000226 HashEntryI = TokBuf + ReadLE32(NextPPCondPtr);
227 uint32_t NextIdx = ReadLE32(NextPPCondPtr);
Mike Stump11289f42009-09-09 15:08:12 +0000228
Ted Kremenek56572ab2008-12-12 18:34:08 +0000229 // By construction NextIdx will be zero if this is a #endif. This is useful
230 // to know to obviate lexing another token.
231 bool isEndif = NextIdx == 0;
Mike Stump11289f42009-09-09 15:08:12 +0000232
Ted Kremenek56572ab2008-12-12 18:34:08 +0000233 // This case can occur when we see something like this:
234 //
235 // #if ...
236 // /* a comment or nothing */
237 // #elif
238 //
239 // If we are skipping the first #if block it will be the case that CurPtr
240 // already points 'elif'. Just return.
Mike Stump11289f42009-09-09 15:08:12 +0000241
Ted Kremenek877556f2008-12-12 22:05:38 +0000242 if (CurPtr > HashEntryI) {
243 assert(CurPtr == HashEntryI + DISK_TOKEN_SIZE);
Ted Kremenek56572ab2008-12-12 18:34:08 +0000244 // Did we reach a #endif? If so, go ahead and consume that token as well.
245 if (isEndif)
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000246 CurPtr += DISK_TOKEN_SIZE*2;
Ted Kremenek56572ab2008-12-12 18:34:08 +0000247 else
Ted Kremenek877556f2008-12-12 22:05:38 +0000248 LastHashTokPtr = HashEntryI;
Mike Stump11289f42009-09-09 15:08:12 +0000249
Ted Kremenek56572ab2008-12-12 18:34:08 +0000250 return isEndif;
251 }
252
253 // Otherwise, we need to advance. Update CurPtr to point to the '#' token.
Ted Kremenek877556f2008-12-12 22:05:38 +0000254 CurPtr = HashEntryI;
Mike Stump11289f42009-09-09 15:08:12 +0000255
Ted Kremenek56572ab2008-12-12 18:34:08 +0000256 // Update the location of the last observed '#'. This is useful if we
257 // are skipping multiple blocks.
258 LastHashTokPtr = CurPtr;
Ted Kremenek56572ab2008-12-12 18:34:08 +0000259
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000260 // Skip the '#' token.
Chris Lattnereb097542009-01-18 01:57:14 +0000261 assert(((tok::TokenKind)*CurPtr) == tok::hash);
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000262 CurPtr += DISK_TOKEN_SIZE;
Mike Stump11289f42009-09-09 15:08:12 +0000263
Ted Kremenek56572ab2008-12-12 18:34:08 +0000264 // Did we reach a #endif? If so, go ahead and consume that token as well.
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000265 if (isEndif) { CurPtr += DISK_TOKEN_SIZE*2; }
Ted Kremenek56572ab2008-12-12 18:34:08 +0000266
267 return isEndif;
268}
269
Ted Kremenek63ff81c2008-12-17 23:36:32 +0000270SourceLocation PTHLexer::getSourceLocation() {
Chris Lattner137d6492009-01-18 02:10:31 +0000271 // getSourceLocation is not on the hot path. It is used to get the location
272 // of the next token when transitioning back to this lexer when done
Ted Kremenek63ff81c2008-12-17 23:36:32 +0000273 // handling a #included file. Just read the necessary data from the token
274 // data buffer to construct the SourceLocation object.
275 // NOTE: This is a virtual function; hence it is defined out-of-line.
Ted Kremenekae54f2f2009-01-21 22:41:38 +0000276 const unsigned char *OffsetPtr = CurPtr + (DISK_TOKEN_SIZE - 4);
Chris Lattnerfec54702009-01-22 19:48:26 +0000277 uint32_t Offset = ReadLE32(OffsetPtr);
Chris Lattner137d6492009-01-18 02:10:31 +0000278 return FileStartLoc.getFileLocWithOffset(Offset);
Ted Kremenek63ff81c2008-12-17 23:36:32 +0000279}
280
Ted Kremenek47b8cf62009-01-09 22:05:30 +0000281//===----------------------------------------------------------------------===//
Ted Kremenek86423a92009-02-10 22:16:22 +0000282// PTH file lookup: map from strings to file data.
283//===----------------------------------------------------------------------===//
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000284
285/// PTHFileLookup - This internal data structure is used by the PTHManager
286/// to map from FileEntry objects managed by FileManager to offsets within
287/// the PTH file.
288namespace {
Ted Kremenek86423a92009-02-10 22:16:22 +0000289class VISIBILITY_HIDDEN PTHFileData {
290 const uint32_t TokenOff;
291 const uint32_t PPCondOff;
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000292public:
Ted Kremenek86423a92009-02-10 22:16:22 +0000293 PTHFileData(uint32_t tokenOff, uint32_t ppCondOff)
294 : TokenOff(tokenOff), PPCondOff(ppCondOff) {}
Mike Stump11289f42009-09-09 15:08:12 +0000295
296 uint32_t getTokenOffset() const { return TokenOff; }
297 uint32_t getPPCondOffset() const { return PPCondOff; }
Ted Kremenek86423a92009-02-10 22:16:22 +0000298};
Mike Stump11289f42009-09-09 15:08:12 +0000299
300
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000301class VISIBILITY_HIDDEN PTHFileLookupCommonTrait {
Ted Kremenek86423a92009-02-10 22:16:22 +0000302public:
Ted Kremenek29942a32009-02-13 19:13:46 +0000303 typedef std::pair<unsigned char, const char*> internal_key_type;
304
305 static unsigned ComputeHash(internal_key_type x) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +0000306 return llvm::HashString(x.second);
Ted Kremenek86423a92009-02-10 22:16:22 +0000307 }
Mike Stump11289f42009-09-09 15:08:12 +0000308
Ted Kremenek86423a92009-02-10 22:16:22 +0000309 static std::pair<unsigned, unsigned>
310 ReadKeyDataLength(const unsigned char*& d) {
Ted Kremenek29942a32009-02-13 19:13:46 +0000311 unsigned keyLen = (unsigned) ReadUnalignedLE16(d);
312 unsigned dataLen = (unsigned) *(d++);
313 return std::make_pair(keyLen, dataLen);
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000314 }
Mike Stump11289f42009-09-09 15:08:12 +0000315
Ted Kremenek29942a32009-02-13 19:13:46 +0000316 static internal_key_type ReadKey(const unsigned char* d, unsigned) {
317 unsigned char k = *(d++); // Read the entry kind.
318 return std::make_pair(k, (const char*) d);
Ted Kremenek86423a92009-02-10 22:16:22 +0000319 }
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000320};
Mike Stump11289f42009-09-09 15:08:12 +0000321
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000322class VISIBILITY_HIDDEN PTHFileLookupTrait : public PTHFileLookupCommonTrait {
323public:
324 typedef const FileEntry* external_key_type;
325 typedef PTHFileData data_type;
Mike Stump11289f42009-09-09 15:08:12 +0000326
Ted Kremenek29942a32009-02-13 19:13:46 +0000327 static internal_key_type GetInternalKey(const FileEntry* FE) {
328 return std::make_pair((unsigned char) 0x1, FE->getName());
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000329 }
Ted Kremenek29942a32009-02-13 19:13:46 +0000330
331 static bool EqualKey(internal_key_type a, internal_key_type b) {
332 return a.first == b.first && strcmp(a.second, b.second) == 0;
Mike Stump11289f42009-09-09 15:08:12 +0000333 }
334
335 static PTHFileData ReadData(const internal_key_type& k,
336 const unsigned char* d, unsigned) {
Ted Kremenek29942a32009-02-13 19:13:46 +0000337 assert(k.first == 0x1 && "Only file lookups can match!");
Ted Kremenek86423a92009-02-10 22:16:22 +0000338 uint32_t x = ::ReadUnalignedLE32(d);
339 uint32_t y = ::ReadUnalignedLE32(d);
Mike Stump11289f42009-09-09 15:08:12 +0000340 return PTHFileData(x, y);
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000341 }
342};
Ted Kremeneke5554de2009-02-11 21:29:16 +0000343
344class VISIBILITY_HIDDEN PTHStringLookupTrait {
345public:
Mike Stump11289f42009-09-09 15:08:12 +0000346 typedef uint32_t
Ted Kremeneke5554de2009-02-11 21:29:16 +0000347 data_type;
348
349 typedef const std::pair<const char*, unsigned>
350 external_key_type;
351
352 typedef external_key_type internal_key_type;
Mike Stump11289f42009-09-09 15:08:12 +0000353
Ted Kremeneke5554de2009-02-11 21:29:16 +0000354 static bool EqualKey(const internal_key_type& a,
355 const internal_key_type& b) {
356 return (a.second == b.second) ? memcmp(a.first, b.first, a.second) == 0
357 : false;
358 }
Mike Stump11289f42009-09-09 15:08:12 +0000359
Ted Kremeneke5554de2009-02-11 21:29:16 +0000360 static unsigned ComputeHash(const internal_key_type& a) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +0000361 return llvm::HashString(llvm::StringRef(a.first, a.second));
Ted Kremeneke5554de2009-02-11 21:29:16 +0000362 }
Mike Stump11289f42009-09-09 15:08:12 +0000363
Ted Kremeneke5554de2009-02-11 21:29:16 +0000364 // This hopefully will just get inlined and removed by the optimizer.
365 static const internal_key_type&
366 GetInternalKey(const external_key_type& x) { return x; }
Mike Stump11289f42009-09-09 15:08:12 +0000367
Ted Kremeneke5554de2009-02-11 21:29:16 +0000368 static std::pair<unsigned, unsigned>
369 ReadKeyDataLength(const unsigned char*& d) {
370 return std::make_pair((unsigned) ReadUnalignedLE16(d), sizeof(uint32_t));
371 }
Mike Stump11289f42009-09-09 15:08:12 +0000372
Ted Kremeneke5554de2009-02-11 21:29:16 +0000373 static std::pair<const char*, unsigned>
374 ReadKey(const unsigned char* d, unsigned n) {
375 assert(n >= 2 && d[n-1] == '\0');
376 return std::make_pair((const char*) d, n-1);
377 }
Mike Stump11289f42009-09-09 15:08:12 +0000378
Ted Kremenek29942a32009-02-13 19:13:46 +0000379 static uint32_t ReadData(const internal_key_type& k, const unsigned char* d,
380 unsigned) {
Ted Kremeneke5554de2009-02-11 21:29:16 +0000381 return ::ReadUnalignedLE32(d);
382 }
383};
Mike Stump11289f42009-09-09 15:08:12 +0000384
385} // end anonymous namespace
Ted Kremenek86423a92009-02-10 22:16:22 +0000386
Ted Kremeneke5554de2009-02-11 21:29:16 +0000387typedef OnDiskChainedHashTable<PTHFileLookupTrait> PTHFileLookup;
388typedef OnDiskChainedHashTable<PTHStringLookupTrait> PTHStringIdLookup;
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000389
390//===----------------------------------------------------------------------===//
391// PTHManager methods.
392//===----------------------------------------------------------------------===//
393
394PTHManager::PTHManager(const llvm::MemoryBuffer* buf, void* fileLookup,
Chris Lattnereb097542009-01-18 01:57:14 +0000395 const unsigned char* idDataTable,
Mike Stump11289f42009-09-09 15:08:12 +0000396 IdentifierInfo** perIDCache,
Ted Kremeneke5554de2009-02-11 21:29:16 +0000397 void* stringIdLookup, unsigned numIds,
Ted Kremenekf9ccd5c2009-03-19 22:19:30 +0000398 const unsigned char* spellingBase,
399 const char* originalSourceFile)
Ted Kremenek73a4d282008-12-03 01:16:39 +0000400: Buf(buf), PerIDCache(perIDCache), FileLookup(fileLookup),
Ted Kremeneke5554de2009-02-11 21:29:16 +0000401 IdDataTable(idDataTable), StringIdLookup(stringIdLookup),
Ted Kremenekf9ccd5c2009-03-19 22:19:30 +0000402 NumIds(numIds), PP(0), SpellingBase(spellingBase),
403 OriginalSourceFile(originalSourceFile) {}
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000404
405PTHManager::~PTHManager() {
406 delete Buf;
407 delete (PTHFileLookup*) FileLookup;
Ted Kremeneke5554de2009-02-11 21:29:16 +0000408 delete (PTHStringIdLookup*) StringIdLookup;
Ted Kremenek1aed3dd2008-12-04 22:47:11 +0000409 free(PerIDCache);
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000410}
411
Daniel Dunbar1a54e3f2009-11-12 02:53:48 +0000412static void InvalidPTH(Diagnostic &Diags, const char *Msg) {
413 Diags.Report(Diags.getCustomDiagID(Diagnostic::Error, Msg));
Ted Kremenek62224c12009-01-28 21:02:43 +0000414}
415
Daniel Dunbar1a54e3f2009-11-12 02:53:48 +0000416PTHManager* PTHManager::Create(const std::string& file, Diagnostic &Diags) {
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000417 // Memory map the PTH file.
418 llvm::OwningPtr<llvm::MemoryBuffer>
419 File(llvm::MemoryBuffer::getFile(file.c_str()));
Mike Stump11289f42009-09-09 15:08:12 +0000420
Ted Kremenek3b0589e2009-01-28 20:49:33 +0000421 if (!File) {
Daniel Dunbar1a54e3f2009-11-12 02:53:48 +0000422 Diags.Report(diag::err_invalid_pth_file) << file;
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000423 return 0;
Ted Kremenek3b0589e2009-01-28 20:49:33 +0000424 }
Mike Stump11289f42009-09-09 15:08:12 +0000425
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000426 // Get the buffer ranges and check if there are at least three 32-bit
427 // words at the end of the file.
Chris Lattnereb097542009-01-18 01:57:14 +0000428 const unsigned char* BufBeg = (unsigned char*)File->getBufferStart();
429 const unsigned char* BufEnd = (unsigned char*)File->getBufferEnd();
Ted Kremenekeb8c8fb2009-01-26 21:43:14 +0000430
431 // Check the prologue of the file.
Ted Kremenek327d00c2009-01-26 22:16:12 +0000432 if ((BufEnd - BufBeg) < (signed) (sizeof("cfe-pth") + 3 + 4) ||
Ted Kremenek62224c12009-01-28 21:02:43 +0000433 memcmp(BufBeg, "cfe-pth", sizeof("cfe-pth") - 1) != 0) {
Daniel Dunbar1a54e3f2009-11-12 02:53:48 +0000434 Diags.Report(diag::err_invalid_pth_file) << file;
Ted Kremenekeb8c8fb2009-01-26 21:43:14 +0000435 return 0;
Ted Kremenek62224c12009-01-28 21:02:43 +0000436 }
Mike Stump11289f42009-09-09 15:08:12 +0000437
Ted Kremenek978b5be2009-01-26 21:50:21 +0000438 // Read the PTH version.
Ted Kremenekeb8c8fb2009-01-26 21:43:14 +0000439 const unsigned char *p = BufBeg + (sizeof("cfe-pth") - 1);
Ted Kremenek978b5be2009-01-26 21:50:21 +0000440 unsigned Version = ReadLE32(p);
Mike Stump11289f42009-09-09 15:08:12 +0000441
Daniel Dunbar1a54e3f2009-11-12 02:53:48 +0000442 if (Version < PTHManager::Version) {
443 InvalidPTH(Diags,
Mike Stump11289f42009-09-09 15:08:12 +0000444 Version < PTHManager::Version
Ted Kremenek62224c12009-01-28 21:02:43 +0000445 ? "PTH file uses an older PTH format that is no longer supported"
446 : "PTH file uses a newer PTH format that cannot be read");
Ted Kremenek978b5be2009-01-26 21:50:21 +0000447 return 0;
Ted Kremenek62224c12009-01-28 21:02:43 +0000448 }
Ted Kremenek978b5be2009-01-26 21:50:21 +0000449
Mike Stump11289f42009-09-09 15:08:12 +0000450 // Compute the address of the index table at the end of the PTH file.
Ted Kremenek4c1d41f2009-02-11 23:34:32 +0000451 const unsigned char *PrologueOffset = p;
Mike Stump11289f42009-09-09 15:08:12 +0000452
Ted Kremenek4c1d41f2009-02-11 23:34:32 +0000453 if (PrologueOffset >= BufEnd) {
Daniel Dunbar1a54e3f2009-11-12 02:53:48 +0000454 Diags.Report(diag::err_invalid_pth_file) << file;
Ted Kremenekeb8c8fb2009-01-26 21:43:14 +0000455 return 0;
Ted Kremenek62224c12009-01-28 21:02:43 +0000456 }
Mike Stump11289f42009-09-09 15:08:12 +0000457
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000458 // Construct the file lookup table. This will be used for mapping from
459 // FileEntry*'s to cached tokens.
Ted Kremenek4c1d41f2009-02-11 23:34:32 +0000460 const unsigned char* FileTableOffset = PrologueOffset + sizeof(uint32_t)*2;
Chris Lattnerfec54702009-01-22 19:48:26 +0000461 const unsigned char* FileTable = BufBeg + ReadLE32(FileTableOffset);
Mike Stump11289f42009-09-09 15:08:12 +0000462
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000463 if (!(FileTable > BufBeg && FileTable < BufEnd)) {
Daniel Dunbar1a54e3f2009-11-12 02:53:48 +0000464 Diags.Report(diag::err_invalid_pth_file) << file;
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000465 return 0; // FIXME: Proper error diagnostic?
466 }
Mike Stump11289f42009-09-09 15:08:12 +0000467
Ted Kremenek86423a92009-02-10 22:16:22 +0000468 llvm::OwningPtr<PTHFileLookup> FL(PTHFileLookup::Create(FileTable, BufBeg));
Mike Stump11289f42009-09-09 15:08:12 +0000469
Ted Kremenek80e239a2009-03-20 17:54:25 +0000470 // Warn if the PTH file is empty. We still want to create a PTHManager
471 // as the PTH could be used with -include-pth.
472 if (FL->isEmpty())
Daniel Dunbar1a54e3f2009-11-12 02:53:48 +0000473 InvalidPTH(Diags, "PTH file contains no cached source data");
Mike Stump11289f42009-09-09 15:08:12 +0000474
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000475 // Get the location of the table mapping from persistent ids to the
476 // data needed to reconstruct identifiers.
Ted Kremenek4c1d41f2009-02-11 23:34:32 +0000477 const unsigned char* IDTableOffset = PrologueOffset + sizeof(uint32_t)*0;
Chris Lattnerfec54702009-01-22 19:48:26 +0000478 const unsigned char* IData = BufBeg + ReadLE32(IDTableOffset);
Mike Stump11289f42009-09-09 15:08:12 +0000479
Ted Kremenek8d6c8282009-01-21 07:34:28 +0000480 if (!(IData >= BufBeg && IData < BufEnd)) {
Daniel Dunbar1a54e3f2009-11-12 02:53:48 +0000481 Diags.Report(diag::err_invalid_pth_file) << file;
Ted Kremenek62224c12009-01-28 21:02:43 +0000482 return 0;
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000483 }
Mike Stump11289f42009-09-09 15:08:12 +0000484
Ted Kremeneke5554de2009-02-11 21:29:16 +0000485 // Get the location of the hashtable mapping between strings and
486 // persistent IDs.
Ted Kremenek4c1d41f2009-02-11 23:34:32 +0000487 const unsigned char* StringIdTableOffset = PrologueOffset + sizeof(uint32_t)*1;
Ted Kremeneke5554de2009-02-11 21:29:16 +0000488 const unsigned char* StringIdTable = BufBeg + ReadLE32(StringIdTableOffset);
489 if (!(StringIdTable >= BufBeg && StringIdTable < BufEnd)) {
Daniel Dunbar1a54e3f2009-11-12 02:53:48 +0000490 Diags.Report(diag::err_invalid_pth_file) << file;
Ted Kremenek62224c12009-01-28 21:02:43 +0000491 return 0;
Ted Kremeneka705b042009-01-15 18:47:46 +0000492 }
Ted Kremeneke5554de2009-02-11 21:29:16 +0000493
494 llvm::OwningPtr<PTHStringIdLookup> SL(PTHStringIdLookup::Create(StringIdTable,
495 BufBeg));
Mike Stump11289f42009-09-09 15:08:12 +0000496
Ted Kremenek8d178f42009-01-27 00:01:05 +0000497 // Get the location of the spelling cache.
Ted Kremenek4c1d41f2009-02-11 23:34:32 +0000498 const unsigned char* spellingBaseOffset = PrologueOffset + sizeof(uint32_t)*3;
Ted Kremenek8d178f42009-01-27 00:01:05 +0000499 const unsigned char* spellingBase = BufBeg + ReadLE32(spellingBaseOffset);
500 if (!(spellingBase >= BufBeg && spellingBase < BufEnd)) {
Daniel Dunbar1a54e3f2009-11-12 02:53:48 +0000501 Diags.Report(diag::err_invalid_pth_file) << file;
Ted Kremenek8d178f42009-01-27 00:01:05 +0000502 return 0;
503 }
Mike Stump11289f42009-09-09 15:08:12 +0000504
Ted Kremenek73a4d282008-12-03 01:16:39 +0000505 // Get the number of IdentifierInfos and pre-allocate the identifier cache.
Chris Lattnerfec54702009-01-22 19:48:26 +0000506 uint32_t NumIds = ReadLE32(IData);
Mike Stump11289f42009-09-09 15:08:12 +0000507
Ted Kremenek73a4d282008-12-03 01:16:39 +0000508 // Pre-allocate the peristent ID -> IdentifierInfo* cache. We use calloc()
509 // so that we in the best case only zero out memory once when the OS returns
510 // us new pages.
Ted Kremenek8d6c8282009-01-21 07:34:28 +0000511 IdentifierInfo** PerIDCache = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000512
Ted Kremenek8d6c8282009-01-21 07:34:28 +0000513 if (NumIds) {
Mike Stump11289f42009-09-09 15:08:12 +0000514 PerIDCache = (IdentifierInfo**)calloc(NumIds, sizeof(*PerIDCache));
Ted Kremenek8d6c8282009-01-21 07:34:28 +0000515 if (!PerIDCache) {
Daniel Dunbar1a54e3f2009-11-12 02:53:48 +0000516 InvalidPTH(Diags, "Could not allocate memory for processing PTH file");
Ted Kremenek8d6c8282009-01-21 07:34:28 +0000517 return 0;
518 }
Ted Kremenek73a4d282008-12-03 01:16:39 +0000519 }
Ted Kremenek8d6c8282009-01-21 07:34:28 +0000520
Ted Kremenekf9ccd5c2009-03-19 22:19:30 +0000521 // Compute the address of the original source file.
522 const unsigned char* originalSourceBase = PrologueOffset + sizeof(uint32_t)*4;
523 unsigned len = ReadUnalignedLE16(originalSourceBase);
Mike Stump11289f42009-09-09 15:08:12 +0000524 if (!len) originalSourceBase = 0;
525
Ted Kremeneka705b042009-01-15 18:47:46 +0000526 // Create the new PTHManager.
527 return new PTHManager(File.take(), FL.take(), IData, PerIDCache,
Ted Kremenekf9ccd5c2009-03-19 22:19:30 +0000528 SL.take(), NumIds, spellingBase,
529 (const char*) originalSourceBase);
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000530}
Ted Kremenekf9ccd5c2009-03-19 22:19:30 +0000531
Chris Lattner144aacd2009-01-18 02:57:21 +0000532IdentifierInfo* PTHManager::LazilyCreateIdentifierInfo(unsigned PersistentID) {
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000533 // Look in the PTH file for the string data for the IdentifierInfo object.
Chris Lattner144aacd2009-01-18 02:57:21 +0000534 const unsigned char* TableEntry = IdDataTable + sizeof(uint32_t)*PersistentID;
Chris Lattnereb097542009-01-18 01:57:14 +0000535 const unsigned char* IDData =
Chris Lattnerfec54702009-01-22 19:48:26 +0000536 (const unsigned char*)Buf->getBufferStart() + ReadLE32(TableEntry);
Chris Lattnereb097542009-01-18 01:57:14 +0000537 assert(IDData < (const unsigned char*)Buf->getBufferEnd());
Mike Stump11289f42009-09-09 15:08:12 +0000538
Ted Kremeneka705b042009-01-15 18:47:46 +0000539 // Allocate the object.
Chris Lattnereb097542009-01-18 01:57:14 +0000540 std::pair<IdentifierInfo,const unsigned char*> *Mem =
541 Alloc.Allocate<std::pair<IdentifierInfo,const unsigned char*> >();
Ted Kremeneka705b042009-01-15 18:47:46 +0000542
543 Mem->second = IDData;
Ted Kremeneke5554de2009-02-11 21:29:16 +0000544 assert(IDData[0] != '\0');
Ted Kremenek52f73ca2009-01-20 23:28:34 +0000545 IdentifierInfo *II = new ((void*) Mem) IdentifierInfo();
Mike Stump11289f42009-09-09 15:08:12 +0000546
Ted Kremeneka705b042009-01-15 18:47:46 +0000547 // Store the new IdentifierInfo in the cache.
Chris Lattner144aacd2009-01-18 02:57:21 +0000548 PerIDCache[PersistentID] = II;
Daniel Dunbar2c422dc92009-10-18 20:26:12 +0000549 assert(II->getNameStart() && II->getNameStart()[0] != '\0');
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000550 return II;
551}
552
Ted Kremeneka705b042009-01-15 18:47:46 +0000553IdentifierInfo* PTHManager::get(const char *NameStart, const char *NameEnd) {
Ted Kremeneke5554de2009-02-11 21:29:16 +0000554 PTHStringIdLookup& SL = *((PTHStringIdLookup*)StringIdLookup);
555 // Double check our assumption that the last character isn't '\0'.
Daniel Dunbarad027c72009-02-12 19:31:53 +0000556 assert(NameEnd==NameStart || NameStart[NameEnd-NameStart-1] != '\0');
Ted Kremeneke5554de2009-02-11 21:29:16 +0000557 PTHStringIdLookup::iterator I = SL.find(std::make_pair(NameStart,
558 NameEnd - NameStart));
559 if (I == SL.end()) // No identifier found?
560 return 0;
Ted Kremeneka705b042009-01-15 18:47:46 +0000561
Ted Kremeneke5554de2009-02-11 21:29:16 +0000562 // Match found. Return the identifier!
563 assert(*I > 0);
564 return GetIdentifierInfo(*I-1);
565}
Ted Kremeneka705b042009-01-15 18:47:46 +0000566
Chris Lattnerab1d4b82009-01-17 08:06:50 +0000567PTHLexer *PTHManager::CreateLexer(FileID FID) {
568 const FileEntry *FE = PP->getSourceManager().getFileEntryForID(FID);
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000569 if (!FE)
570 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000571
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000572 // Lookup the FileEntry object in our file lookup data structure. It will
573 // return a variant that indicates whether or not there is an offset within
574 // the PTH file that contains cached tokens.
Ted Kremenek86423a92009-02-10 22:16:22 +0000575 PTHFileLookup& PFL = *((PTHFileLookup*)FileLookup);
576 PTHFileLookup::iterator I = PFL.find(FE);
Mike Stump11289f42009-09-09 15:08:12 +0000577
Ted Kremenek86423a92009-02-10 22:16:22 +0000578 if (I == PFL.end()) // No tokens available?
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000579 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000580
581 const PTHFileData& FileData = *I;
582
Chris Lattnereb097542009-01-18 01:57:14 +0000583 const unsigned char *BufStart = (const unsigned char *)Buf->getBufferStart();
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000584 // Compute the offset of the token data within the buffer.
Chris Lattnereb097542009-01-18 01:57:14 +0000585 const unsigned char* data = BufStart + FileData.getTokenOffset();
Ted Kremenek56572ab2008-12-12 18:34:08 +0000586
587 // Get the location of pp-conditional table.
Chris Lattnereb097542009-01-18 01:57:14 +0000588 const unsigned char* ppcond = BufStart + FileData.getPPCondOffset();
Chris Lattnerfec54702009-01-22 19:48:26 +0000589 uint32_t Len = ReadLE32(ppcond);
Chris Lattner137d6492009-01-18 02:10:31 +0000590 if (Len == 0) ppcond = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000591
Ted Kremeneka705b042009-01-15 18:47:46 +0000592 assert(PP && "No preprocessor set yet!");
Mike Stump11289f42009-09-09 15:08:12 +0000593 return new PTHLexer(*PP, FID, data, ppcond, *this);
Ted Kremenek33eeabd2008-12-03 00:38:03 +0000594}
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000595
596//===----------------------------------------------------------------------===//
597// 'stat' caching.
598//===----------------------------------------------------------------------===//
599
600namespace {
601class VISIBILITY_HIDDEN PTHStatData {
602public:
Ted Kremenek29942a32009-02-13 19:13:46 +0000603 const bool hasStat;
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000604 const ino_t ino;
605 const dev_t dev;
606 const mode_t mode;
607 const time_t mtime;
608 const off_t size;
Mike Stump11289f42009-09-09 15:08:12 +0000609
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000610 PTHStatData(ino_t i, dev_t d, mode_t mo, time_t m, off_t s)
Mike Stump11289f42009-09-09 15:08:12 +0000611 : hasStat(true), ino(i), dev(d), mode(mo), mtime(m), size(s) {}
612
Ted Kremenek29942a32009-02-13 19:13:46 +0000613 PTHStatData()
614 : hasStat(false), ino(0), dev(0), mode(0), mtime(0), size(0) {}
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000615};
Mike Stump11289f42009-09-09 15:08:12 +0000616
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000617class VISIBILITY_HIDDEN PTHStatLookupTrait : public PTHFileLookupCommonTrait {
618public:
Ted Kremenek29942a32009-02-13 19:13:46 +0000619 typedef const char* external_key_type; // const char*
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000620 typedef PTHStatData data_type;
Mike Stump11289f42009-09-09 15:08:12 +0000621
Ted Kremenek29942a32009-02-13 19:13:46 +0000622 static internal_key_type GetInternalKey(const char *path) {
623 // The key 'kind' doesn't matter here because it is ignored in EqualKey.
624 return std::make_pair((unsigned char) 0x0, path);
625 }
626
627 static bool EqualKey(internal_key_type a, internal_key_type b) {
628 // When doing 'stat' lookups we don't care about the kind of 'a' and 'b',
629 // just the paths.
630 return strcmp(a.second, b.second) == 0;
Mike Stump11289f42009-09-09 15:08:12 +0000631 }
632
Ted Kremenek29942a32009-02-13 19:13:46 +0000633 static data_type ReadData(const internal_key_type& k, const unsigned char* d,
Mike Stump11289f42009-09-09 15:08:12 +0000634 unsigned) {
635
Ted Kremenek2fd18ec2009-02-13 22:07:44 +0000636 if (k.first /* File or Directory */) {
637 if (k.first == 0x1 /* File */) d += 4 * 2; // Skip the first 2 words.
Ted Kremenek29942a32009-02-13 19:13:46 +0000638 ino_t ino = (ino_t) ReadUnalignedLE32(d);
639 dev_t dev = (dev_t) ReadUnalignedLE32(d);
640 mode_t mode = (mode_t) ReadUnalignedLE16(d);
Mike Stump11289f42009-09-09 15:08:12 +0000641 time_t mtime = (time_t) ReadUnalignedLE64(d);
Ted Kremenek29942a32009-02-13 19:13:46 +0000642 return data_type(ino, dev, mode, mtime, (off_t) ReadUnalignedLE64(d));
643 }
Ted Kremenek2fd18ec2009-02-13 22:07:44 +0000644
645 // Negative stat. Don't read anything.
Ted Kremenek29942a32009-02-13 19:13:46 +0000646 return data_type();
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000647 }
648};
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000649
650class VISIBILITY_HIDDEN PTHStatCache : public StatSysCallCache {
651 typedef OnDiskChainedHashTable<PTHStatLookupTrait> CacheTy;
652 CacheTy Cache;
653
Mike Stump11289f42009-09-09 15:08:12 +0000654public:
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000655 PTHStatCache(PTHFileLookup &FL) :
656 Cache(FL.getNumBuckets(), FL.getNumEntries(), FL.getBuckets(),
657 FL.getBase()) {}
658
659 ~PTHStatCache() {}
Mike Stump11289f42009-09-09 15:08:12 +0000660
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000661 int stat(const char *path, struct stat *buf) {
662 // Do the lookup for the file's data in the PTH file.
663 CacheTy::iterator I = Cache.find(path);
664
665 // If we don't get a hit in the PTH file just forward to 'stat'.
Douglas Gregord2eb58a2009-10-16 18:18:30 +0000666 if (I == Cache.end())
667 return StatSysCallCache::stat(path, buf);
Mike Stump11289f42009-09-09 15:08:12 +0000668
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000669 const PTHStatData& Data = *I;
Mike Stump11289f42009-09-09 15:08:12 +0000670
Ted Kremenek2fd18ec2009-02-13 22:07:44 +0000671 if (!Data.hasStat)
672 return 1;
673
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000674 buf->st_ino = Data.ino;
675 buf->st_dev = Data.dev;
676 buf->st_mtime = Data.mtime;
677 buf->st_mode = Data.mode;
678 buf->st_size = Data.size;
679 return 0;
680 }
681};
Ted Kremenek29b86972009-02-23 23:27:54 +0000682} // end anonymous namespace
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000683
684StatSysCallCache *PTHManager::createStatCache() {
Ted Kremenekb4c85cc2009-02-12 03:45:39 +0000685 return new PTHStatCache(*((PTHFileLookup*) FileLookup));
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000686}