blob: b1671721b630505850430e6e3cb6efa0cd432142 [file] [log] [blame]
Ted Kremenek274b2082008-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 Kremenek0c6a77b2008-12-03 00:38:03 +000014#include "clang/Basic/TokenKinds.h"
15#include "clang/Basic/FileManager.h"
Chris Lattner10e286a2010-11-23 19:19:34 +000016#include "clang/Basic/FileSystemStatCache.h"
Ted Kremenek0c6a77b2008-12-03 00:38:03 +000017#include "clang/Basic/IdentifierTable.h"
Douglas Gregor9378ba42009-04-20 07:08:21 +000018#include "clang/Basic/OnDiskHashTable.h"
Daniel Dunbar3574f462009-11-12 02:53:48 +000019#include "clang/Lex/LexDiagnostic.h"
Ted Kremenek274b2082008-11-12 21:37:15 +000020#include "clang/Lex/PTHLexer.h"
21#include "clang/Lex/Preprocessor.h"
Ted Kremenek0c6a77b2008-12-03 00:38:03 +000022#include "clang/Lex/PTHManager.h"
23#include "clang/Lex/Token.h"
24#include "clang/Lex/Preprocessor.h"
Ted Kremenek0c6a77b2008-12-03 00:38:03 +000025#include "llvm/ADT/OwningPtr.h"
Daniel Dunbar2596e422009-10-17 23:52:28 +000026#include "llvm/ADT/StringExtras.h"
27#include "llvm/ADT/StringMap.h"
Chris Lattner6f78c3b2009-01-22 23:50:07 +000028#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer3a321e22010-12-09 17:36:38 +000029#include "llvm/Support/system_error.h"
Ted Kremenek274b2082008-11-12 21:37:15 +000030using namespace clang;
Douglas Gregor9378ba42009-04-20 07:08:21 +000031using namespace clang::io;
Ted Kremenek274b2082008-11-12 21:37:15 +000032
Ted Kremenek7b78b7c2009-01-19 23:13:15 +000033#define DISK_TOKEN_SIZE (1+1+2+4+4)
Ted Kremenek268ee702008-12-12 18:34:08 +000034
Ted Kremenek0c6a77b2008-12-03 00:38:03 +000035//===----------------------------------------------------------------------===//
Ted Kremeneke5680f32008-12-23 01:30:52 +000036// PTHLexer methods.
37//===----------------------------------------------------------------------===//
38
Chris Lattnerda9d61c2009-01-18 01:57:14 +000039PTHLexer::PTHLexer(Preprocessor &PP, FileID FID, const unsigned char *D,
Ted Kremenek277faca2009-01-27 00:01:05 +000040 const unsigned char *ppcond, PTHManager &PM)
Chris Lattner2b2453a2009-01-17 06:22:33 +000041 : PreprocessorLexer(&PP, FID), TokBuf(D), CurPtr(D), LastHashTokPtr(0),
Ted Kremenek277faca2009-01-27 00:01:05 +000042 PPCond(ppcond), CurPPCondPtr(ppcond), PTHMgr(PM) {
Mike Stump1eb44332009-09-09 15:08:12 +000043
Chris Lattner2b2453a2009-01-17 06:22:33 +000044 FileStartLoc = PP.getSourceManager().getLocForStartOfFile(FID);
Ted Kremenek5f074262009-01-09 22:05:30 +000045}
Ted Kremeneke5680f32008-12-23 01:30:52 +000046
47void PTHLexer::Lex(Token& Tok) {
48LexNextToken:
Ted Kremeneke5680f32008-12-23 01:30:52 +000049
Ted Kremenek866bdf72008-12-23 02:30:15 +000050 //===--------------------------------------==//
51 // Read the raw token data.
52 //===--------------------------------------==//
Mike Stump1eb44332009-09-09 15:08:12 +000053
Ted Kremenek866bdf72008-12-23 02:30:15 +000054 // Shadow CurPtr into an automatic variable.
Mike Stump1eb44332009-09-09 15:08:12 +000055 const unsigned char *CurPtrShadow = CurPtr;
Ted Kremenek866bdf72008-12-23 02:30:15 +000056
Chris Lattner1b5285e2009-01-18 02:10:31 +000057 // Read in the data for the token.
Chris Lattner5ff43172009-01-22 19:48:26 +000058 unsigned Word0 = ReadLE32(CurPtrShadow);
59 uint32_t IdentifierID = ReadLE32(CurPtrShadow);
60 uint32_t FileOffset = ReadLE32(CurPtrShadow);
Mike Stump1eb44332009-09-09 15:08:12 +000061
Ted Kremenek7b78b7c2009-01-19 23:13:15 +000062 tok::TokenKind TKind = (tok::TokenKind) (Word0 & 0xFF);
63 Token::TokenFlags TFlags = (Token::TokenFlags) ((Word0 >> 8) & 0xFF);
Chris Lattneraff6ef82009-01-21 07:21:56 +000064 uint32_t Len = Word0 >> 16;
Ted Kremenek7b78b7c2009-01-19 23:13:15 +000065
Chris Lattneraff6ef82009-01-21 07:21:56 +000066 CurPtr = CurPtrShadow;
Mike Stump1eb44332009-09-09 15:08:12 +000067
Ted Kremenek866bdf72008-12-23 02:30:15 +000068 //===--------------------------------------==//
69 // Construct the token itself.
70 //===--------------------------------------==//
Mike Stump1eb44332009-09-09 15:08:12 +000071
Ted Kremenek866bdf72008-12-23 02:30:15 +000072 Tok.startToken();
Chris Lattner898a0bb2009-01-18 02:34:01 +000073 Tok.setKind(TKind);
74 Tok.setFlag(TFlags);
Ted Kremenek59d08cb2008-12-23 19:24:24 +000075 assert(!LexingRawMode);
Argyrios Kyrtzidisa64ccef2011-09-19 20:40:19 +000076 Tok.setLocation(FileStartLoc.getLocWithOffset(FileOffset));
Ted Kremenek866bdf72008-12-23 02:30:15 +000077 Tok.setLength(Len);
78
Chris Lattnerd0a69692009-01-21 07:50:06 +000079 // Handle identifiers.
Ted Kremenek277faca2009-01-27 00:01:05 +000080 if (Tok.isLiteral()) {
81 Tok.setLiteralData((const char*) (PTHMgr.SpellingBase + IdentifierID));
82 }
83 else if (IdentifierID) {
Chris Lattnerd0a69692009-01-21 07:50:06 +000084 MIOpt.ReadToken();
85 IdentifierInfo *II = PTHMgr.GetIdentifierInfo(IdentifierID-1);
Mike Stump1eb44332009-09-09 15:08:12 +000086
Chris Lattnerd0a69692009-01-21 07:50:06 +000087 Tok.setIdentifierInfo(II);
Mike Stump1eb44332009-09-09 15:08:12 +000088
Chris Lattner863c4862009-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 Stump1eb44332009-09-09 15:08:12 +000092
Chris Lattnerd0a69692009-01-21 07:50:06 +000093 if (II->isHandleIdentifierCase())
94 PP->HandleIdentifier(Tok);
95 return;
96 }
Mike Stump1eb44332009-09-09 15:08:12 +000097
Ted Kremenek866bdf72008-12-23 02:30:15 +000098 //===--------------------------------------==//
99 // Process the token.
100 //===--------------------------------------==//
Chris Lattner898a0bb2009-01-18 02:34:01 +0000101 if (TKind == tok::eof) {
Ted Kremeneke5680f32008-12-23 01:30:52 +0000102 // Save the end-of-file token.
103 EofToken = Tok;
Mike Stump1eb44332009-09-09 15:08:12 +0000104
Ted Kremenek94e3d1f2010-07-27 02:59:02 +0000105 // Save 'PP' to 'PPCache' as LexEndOfFile can delete 'this'.
Ted Kremeneke5680f32008-12-23 01:30:52 +0000106 Preprocessor *PPCache = PP;
Mike Stump1eb44332009-09-09 15:08:12 +0000107
Ted Kremenek59d08cb2008-12-23 19:24:24 +0000108 assert(!ParsingPreprocessorDirective);
109 assert(!LexingRawMode);
Ted Kremenek94e3d1f2010-07-27 02:59:02 +0000110
111 if (LexEndOfFile(Tok))
Ted Kremeneke5680f32008-12-23 01:30:52 +0000112 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000113
Ted Kremeneke5680f32008-12-23 01:30:52 +0000114 return PPCache->Lex(Tok);
115 }
Mike Stump1eb44332009-09-09 15:08:12 +0000116
Chris Lattner898a0bb2009-01-18 02:34:01 +0000117 if (TKind == tok::hash && Tok.isAtStartOfLine()) {
Ted Kremenek59d08cb2008-12-23 19:24:24 +0000118 LastHashTokPtr = CurPtr - DISK_TOKEN_SIZE;
119 assert(!LexingRawMode);
120 PP->HandleDirective(Tok);
Mike Stump1eb44332009-09-09 15:08:12 +0000121
Ted Kremenek59d08cb2008-12-23 19:24:24 +0000122 if (PP->isCurrentLexer(this))
123 goto LexNextToken;
Mike Stump1eb44332009-09-09 15:08:12 +0000124
Ted Kremenek59d08cb2008-12-23 19:24:24 +0000125 return PP->Lex(Tok);
126 }
Mike Stump1eb44332009-09-09 15:08:12 +0000127
Peter Collingbourne84021552011-02-28 02:37:51 +0000128 if (TKind == tok::eod) {
Ted Kremenek59d08cb2008-12-23 19:24:24 +0000129 assert(ParsingPreprocessorDirective);
Ted Kremeneke5680f32008-12-23 01:30:52 +0000130 ParsingPreprocessorDirective = false;
131 return;
132 }
Ted Kremeneke5680f32008-12-23 01:30:52 +0000133
Ted Kremenek59d08cb2008-12-23 19:24:24 +0000134 MIOpt.ReadToken();
Ted Kremeneke5680f32008-12-23 01:30:52 +0000135}
136
Ted Kremenek94e3d1f2010-07-27 02:59:02 +0000137bool PTHLexer::LexEndOfFile(Token &Result) {
138 // If we hit the end of the file while parsing a preprocessor directive,
139 // end the preprocessor directive first. The next token returned will
140 // then be the end of file.
141 if (ParsingPreprocessorDirective) {
142 ParsingPreprocessorDirective = false; // Done parsing the "line".
143 return true; // Have a token.
144 }
145
146 assert(!LexingRawMode);
147
148 // If we are in a #if directive, emit an error.
149 while (!ConditionalStack.empty()) {
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000150 if (PP->getCodeCompletionFileLoc() != FileStartLoc)
Douglas Gregor2d474ba2010-08-12 17:04:55 +0000151 PP->Diag(ConditionalStack.back().IfLoc,
152 diag::err_pp_unterminated_conditional);
Ted Kremenek94e3d1f2010-07-27 02:59:02 +0000153 ConditionalStack.pop_back();
154 }
155
156 // Finally, let the preprocessor handle this.
157 return PP->HandleEndOfFile(Result);
158}
159
Ted Kremeneke5680f32008-12-23 01:30:52 +0000160// FIXME: We can just grab the last token instead of storing a copy
161// into EofToken.
Ted Kremenek59d08cb2008-12-23 19:24:24 +0000162void PTHLexer::getEOF(Token& Tok) {
Ted Kremenekdefb7092009-01-09 00:36:11 +0000163 assert(EofToken.is(tok::eof));
Ted Kremeneke5680f32008-12-23 01:30:52 +0000164 Tok = EofToken;
165}
166
167void PTHLexer::DiscardToEndOfLine() {
168 assert(ParsingPreprocessorDirective && ParsingFilename == false &&
169 "Must be in a preprocessing directive!");
170
171 // We assume that if the preprocessor wishes to discard to the end of
172 // the line that it also means to end the current preprocessor directive.
173 ParsingPreprocessorDirective = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000174
Ted Kremeneke5680f32008-12-23 01:30:52 +0000175 // Skip tokens by only peeking at their token kind and the flags.
176 // We don't need to actually reconstruct full tokens from the token buffer.
177 // This saves some copies and it also reduces IdentifierInfo* lookup.
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000178 const unsigned char* p = CurPtr;
Ted Kremeneke5680f32008-12-23 01:30:52 +0000179 while (1) {
180 // Read the token kind. Are we at the end of the file?
181 tok::TokenKind x = (tok::TokenKind) (uint8_t) *p;
182 if (x == tok::eof) break;
Mike Stump1eb44332009-09-09 15:08:12 +0000183
Ted Kremeneke5680f32008-12-23 01:30:52 +0000184 // Read the token flags. Are we at the start of the next line?
185 Token::TokenFlags y = (Token::TokenFlags) (uint8_t) p[1];
186 if (y & Token::StartOfLine) break;
187
188 // Skip to the next token.
189 p += DISK_TOKEN_SIZE;
190 }
Mike Stump1eb44332009-09-09 15:08:12 +0000191
Ted Kremeneke5680f32008-12-23 01:30:52 +0000192 CurPtr = p;
193}
194
Ted Kremenek268ee702008-12-12 18:34:08 +0000195/// SkipBlock - Used by Preprocessor to skip the current conditional block.
196bool PTHLexer::SkipBlock() {
197 assert(CurPPCondPtr && "No cached PP conditional information.");
198 assert(LastHashTokPtr && "No known '#' token.");
Mike Stump1eb44332009-09-09 15:08:12 +0000199
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000200 const unsigned char* HashEntryI = 0;
Ted Kremenek268ee702008-12-12 18:34:08 +0000201 uint32_t TableIdx;
Mike Stump1eb44332009-09-09 15:08:12 +0000202
Ted Kremenek268ee702008-12-12 18:34:08 +0000203 do {
Ted Kremenek41a26602008-12-12 22:05:38 +0000204 // Read the token offset from the side-table.
Ted Kremenek536cebd2012-09-16 06:18:45 +0000205 uint32_t Offset = ReadLE32(CurPPCondPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000206
207 // Read the target table index from the side-table.
Chris Lattner5ff43172009-01-22 19:48:26 +0000208 TableIdx = ReadLE32(CurPPCondPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000209
Ted Kremenek41a26602008-12-12 22:05:38 +0000210 // Compute the actual memory address of the '#' token data for this entry.
211 HashEntryI = TokBuf + Offset;
212
213 // Optmization: "Sibling jumping". #if...#else...#endif blocks can
214 // contain nested blocks. In the side-table we can jump over these
215 // nested blocks instead of doing a linear search if the next "sibling"
216 // entry is not at a location greater than LastHashTokPtr.
217 if (HashEntryI < LastHashTokPtr && TableIdx) {
218 // In the side-table we are still at an entry for a '#' token that
219 // is earlier than the last one we saw. Check if the location we would
220 // stride gets us closer.
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000221 const unsigned char* NextPPCondPtr =
222 PPCond + TableIdx*(sizeof(uint32_t)*2);
Ted Kremenek41a26602008-12-12 22:05:38 +0000223 assert(NextPPCondPtr >= CurPPCondPtr);
224 // Read where we should jump to.
Ted Kremenek536cebd2012-09-16 06:18:45 +0000225 const unsigned char* HashEntryJ = TokBuf + ReadLE32(NextPPCondPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000226
Ted Kremenek41a26602008-12-12 22:05:38 +0000227 if (HashEntryJ <= LastHashTokPtr) {
228 // Jump directly to the next entry in the side table.
229 HashEntryI = HashEntryJ;
Chris Lattner5ff43172009-01-22 19:48:26 +0000230 TableIdx = ReadLE32(NextPPCondPtr);
Ted Kremenek41a26602008-12-12 22:05:38 +0000231 CurPPCondPtr = NextPPCondPtr;
232 }
233 }
Ted Kremenek268ee702008-12-12 18:34:08 +0000234 }
Mike Stump1eb44332009-09-09 15:08:12 +0000235 while (HashEntryI < LastHashTokPtr);
Ted Kremenek41a26602008-12-12 22:05:38 +0000236 assert(HashEntryI == LastHashTokPtr && "No PP-cond entry found for '#'");
Ted Kremenek268ee702008-12-12 18:34:08 +0000237 assert(TableIdx && "No jumping from #endifs.");
Mike Stump1eb44332009-09-09 15:08:12 +0000238
Ted Kremenek268ee702008-12-12 18:34:08 +0000239 // Update our side-table iterator.
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000240 const unsigned char* NextPPCondPtr = PPCond + TableIdx*(sizeof(uint32_t)*2);
Ted Kremenek268ee702008-12-12 18:34:08 +0000241 assert(NextPPCondPtr >= CurPPCondPtr);
242 CurPPCondPtr = NextPPCondPtr;
Mike Stump1eb44332009-09-09 15:08:12 +0000243
Ted Kremenek268ee702008-12-12 18:34:08 +0000244 // Read where we should jump to.
Chris Lattner5ff43172009-01-22 19:48:26 +0000245 HashEntryI = TokBuf + ReadLE32(NextPPCondPtr);
246 uint32_t NextIdx = ReadLE32(NextPPCondPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000247
Ted Kremenek268ee702008-12-12 18:34:08 +0000248 // By construction NextIdx will be zero if this is a #endif. This is useful
249 // to know to obviate lexing another token.
250 bool isEndif = NextIdx == 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000251
Ted Kremenek268ee702008-12-12 18:34:08 +0000252 // This case can occur when we see something like this:
253 //
254 // #if ...
255 // /* a comment or nothing */
256 // #elif
257 //
258 // If we are skipping the first #if block it will be the case that CurPtr
259 // already points 'elif'. Just return.
Mike Stump1eb44332009-09-09 15:08:12 +0000260
Ted Kremenek41a26602008-12-12 22:05:38 +0000261 if (CurPtr > HashEntryI) {
262 assert(CurPtr == HashEntryI + DISK_TOKEN_SIZE);
Ted Kremenek268ee702008-12-12 18:34:08 +0000263 // Did we reach a #endif? If so, go ahead and consume that token as well.
264 if (isEndif)
Ted Kremeneke5680f32008-12-23 01:30:52 +0000265 CurPtr += DISK_TOKEN_SIZE*2;
Ted Kremenek268ee702008-12-12 18:34:08 +0000266 else
Ted Kremenek41a26602008-12-12 22:05:38 +0000267 LastHashTokPtr = HashEntryI;
Mike Stump1eb44332009-09-09 15:08:12 +0000268
Ted Kremenek268ee702008-12-12 18:34:08 +0000269 return isEndif;
270 }
271
272 // Otherwise, we need to advance. Update CurPtr to point to the '#' token.
Ted Kremenek41a26602008-12-12 22:05:38 +0000273 CurPtr = HashEntryI;
Mike Stump1eb44332009-09-09 15:08:12 +0000274
Ted Kremenek268ee702008-12-12 18:34:08 +0000275 // Update the location of the last observed '#'. This is useful if we
276 // are skipping multiple blocks.
277 LastHashTokPtr = CurPtr;
Ted Kremenek268ee702008-12-12 18:34:08 +0000278
Ted Kremeneke5680f32008-12-23 01:30:52 +0000279 // Skip the '#' token.
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000280 assert(((tok::TokenKind)*CurPtr) == tok::hash);
Ted Kremeneke5680f32008-12-23 01:30:52 +0000281 CurPtr += DISK_TOKEN_SIZE;
Mike Stump1eb44332009-09-09 15:08:12 +0000282
Ted Kremenek268ee702008-12-12 18:34:08 +0000283 // Did we reach a #endif? If so, go ahead and consume that token as well.
Ted Kremeneke5680f32008-12-23 01:30:52 +0000284 if (isEndif) { CurPtr += DISK_TOKEN_SIZE*2; }
Ted Kremenek268ee702008-12-12 18:34:08 +0000285
286 return isEndif;
287}
288
Ted Kremenek30a12ec2008-12-17 23:36:32 +0000289SourceLocation PTHLexer::getSourceLocation() {
Chris Lattner1b5285e2009-01-18 02:10:31 +0000290 // getSourceLocation is not on the hot path. It is used to get the location
291 // of the next token when transitioning back to this lexer when done
Ted Kremenek30a12ec2008-12-17 23:36:32 +0000292 // handling a #included file. Just read the necessary data from the token
293 // data buffer to construct the SourceLocation object.
294 // NOTE: This is a virtual function; hence it is defined out-of-line.
Ted Kremenekb248d532009-01-21 22:41:38 +0000295 const unsigned char *OffsetPtr = CurPtr + (DISK_TOKEN_SIZE - 4);
Chris Lattner5ff43172009-01-22 19:48:26 +0000296 uint32_t Offset = ReadLE32(OffsetPtr);
Argyrios Kyrtzidisa64ccef2011-09-19 20:40:19 +0000297 return FileStartLoc.getLocWithOffset(Offset);
Ted Kremenek30a12ec2008-12-17 23:36:32 +0000298}
299
Ted Kremenek5f074262009-01-09 22:05:30 +0000300//===----------------------------------------------------------------------===//
Ted Kremenekd8c02922009-02-10 22:16:22 +0000301// PTH file lookup: map from strings to file data.
302//===----------------------------------------------------------------------===//
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000303
304/// PTHFileLookup - This internal data structure is used by the PTHManager
305/// to map from FileEntry objects managed by FileManager to offsets within
306/// the PTH file.
307namespace {
Benjamin Kramer85b45212009-11-28 19:45:26 +0000308class PTHFileData {
Ted Kremenekd8c02922009-02-10 22:16:22 +0000309 const uint32_t TokenOff;
310 const uint32_t PPCondOff;
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000311public:
Ted Kremenekd8c02922009-02-10 22:16:22 +0000312 PTHFileData(uint32_t tokenOff, uint32_t ppCondOff)
313 : TokenOff(tokenOff), PPCondOff(ppCondOff) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000314
315 uint32_t getTokenOffset() const { return TokenOff; }
316 uint32_t getPPCondOffset() const { return PPCondOff; }
Ted Kremenekd8c02922009-02-10 22:16:22 +0000317};
Mike Stump1eb44332009-09-09 15:08:12 +0000318
319
Benjamin Kramer85b45212009-11-28 19:45:26 +0000320class PTHFileLookupCommonTrait {
Ted Kremenekd8c02922009-02-10 22:16:22 +0000321public:
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000322 typedef std::pair<unsigned char, const char*> internal_key_type;
323
324 static unsigned ComputeHash(internal_key_type x) {
Daniel Dunbar2596e422009-10-17 23:52:28 +0000325 return llvm::HashString(x.second);
Ted Kremenekd8c02922009-02-10 22:16:22 +0000326 }
Mike Stump1eb44332009-09-09 15:08:12 +0000327
Ted Kremenekd8c02922009-02-10 22:16:22 +0000328 static std::pair<unsigned, unsigned>
329 ReadKeyDataLength(const unsigned char*& d) {
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000330 unsigned keyLen = (unsigned) ReadUnalignedLE16(d);
331 unsigned dataLen = (unsigned) *(d++);
332 return std::make_pair(keyLen, dataLen);
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000333 }
Mike Stump1eb44332009-09-09 15:08:12 +0000334
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000335 static internal_key_type ReadKey(const unsigned char* d, unsigned) {
336 unsigned char k = *(d++); // Read the entry kind.
337 return std::make_pair(k, (const char*) d);
Ted Kremenekd8c02922009-02-10 22:16:22 +0000338 }
Ted Kremenek337edcd2009-02-12 03:26:59 +0000339};
Mike Stump1eb44332009-09-09 15:08:12 +0000340
Benjamin Kramer85b45212009-11-28 19:45:26 +0000341class PTHFileLookupTrait : public PTHFileLookupCommonTrait {
Ted Kremenek337edcd2009-02-12 03:26:59 +0000342public:
343 typedef const FileEntry* external_key_type;
344 typedef PTHFileData data_type;
Mike Stump1eb44332009-09-09 15:08:12 +0000345
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000346 static internal_key_type GetInternalKey(const FileEntry* FE) {
347 return std::make_pair((unsigned char) 0x1, FE->getName());
Ted Kremenek337edcd2009-02-12 03:26:59 +0000348 }
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000349
350 static bool EqualKey(internal_key_type a, internal_key_type b) {
351 return a.first == b.first && strcmp(a.second, b.second) == 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000352 }
353
354 static PTHFileData ReadData(const internal_key_type& k,
355 const unsigned char* d, unsigned) {
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000356 assert(k.first == 0x1 && "Only file lookups can match!");
Ted Kremenekd8c02922009-02-10 22:16:22 +0000357 uint32_t x = ::ReadUnalignedLE32(d);
358 uint32_t y = ::ReadUnalignedLE32(d);
Mike Stump1eb44332009-09-09 15:08:12 +0000359 return PTHFileData(x, y);
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000360 }
361};
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000362
Benjamin Kramer85b45212009-11-28 19:45:26 +0000363class PTHStringLookupTrait {
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000364public:
Mike Stump1eb44332009-09-09 15:08:12 +0000365 typedef uint32_t
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000366 data_type;
367
368 typedef const std::pair<const char*, unsigned>
369 external_key_type;
370
371 typedef external_key_type internal_key_type;
Mike Stump1eb44332009-09-09 15:08:12 +0000372
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000373 static bool EqualKey(const internal_key_type& a,
374 const internal_key_type& b) {
375 return (a.second == b.second) ? memcmp(a.first, b.first, a.second) == 0
376 : false;
377 }
Mike Stump1eb44332009-09-09 15:08:12 +0000378
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000379 static unsigned ComputeHash(const internal_key_type& a) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000380 return llvm::HashString(StringRef(a.first, a.second));
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000381 }
Mike Stump1eb44332009-09-09 15:08:12 +0000382
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000383 // This hopefully will just get inlined and removed by the optimizer.
384 static const internal_key_type&
385 GetInternalKey(const external_key_type& x) { return x; }
Mike Stump1eb44332009-09-09 15:08:12 +0000386
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000387 static std::pair<unsigned, unsigned>
388 ReadKeyDataLength(const unsigned char*& d) {
389 return std::make_pair((unsigned) ReadUnalignedLE16(d), sizeof(uint32_t));
390 }
Mike Stump1eb44332009-09-09 15:08:12 +0000391
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000392 static std::pair<const char*, unsigned>
393 ReadKey(const unsigned char* d, unsigned n) {
394 assert(n >= 2 && d[n-1] == '\0');
395 return std::make_pair((const char*) d, n-1);
396 }
Mike Stump1eb44332009-09-09 15:08:12 +0000397
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000398 static uint32_t ReadData(const internal_key_type& k, const unsigned char* d,
399 unsigned) {
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000400 return ::ReadUnalignedLE32(d);
401 }
402};
Mike Stump1eb44332009-09-09 15:08:12 +0000403
404} // end anonymous namespace
Ted Kremenekd8c02922009-02-10 22:16:22 +0000405
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000406typedef OnDiskChainedHashTable<PTHFileLookupTrait> PTHFileLookup;
407typedef OnDiskChainedHashTable<PTHStringLookupTrait> PTHStringIdLookup;
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000408
409//===----------------------------------------------------------------------===//
410// PTHManager methods.
411//===----------------------------------------------------------------------===//
412
413PTHManager::PTHManager(const llvm::MemoryBuffer* buf, void* fileLookup,
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000414 const unsigned char* idDataTable,
Mike Stump1eb44332009-09-09 15:08:12 +0000415 IdentifierInfo** perIDCache,
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000416 void* stringIdLookup, unsigned numIds,
Ted Kremenek68228632009-03-19 22:19:30 +0000417 const unsigned char* spellingBase,
418 const char* originalSourceFile)
Ted Kremenek6183e482008-12-03 01:16:39 +0000419: Buf(buf), PerIDCache(perIDCache), FileLookup(fileLookup),
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000420 IdDataTable(idDataTable), StringIdLookup(stringIdLookup),
Ted Kremenek68228632009-03-19 22:19:30 +0000421 NumIds(numIds), PP(0), SpellingBase(spellingBase),
422 OriginalSourceFile(originalSourceFile) {}
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000423
424PTHManager::~PTHManager() {
425 delete Buf;
426 delete (PTHFileLookup*) FileLookup;
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000427 delete (PTHStringIdLookup*) StringIdLookup;
Ted Kremenek0e50b6e2008-12-04 22:47:11 +0000428 free(PerIDCache);
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000429}
430
David Blaikied6471f72011-09-25 23:23:43 +0000431static void InvalidPTH(DiagnosticsEngine &Diags, const char *Msg) {
432 Diags.Report(Diags.getCustomDiagID(DiagnosticsEngine::Error, Msg));
Ted Kremenek26555b12009-01-28 21:02:43 +0000433}
434
David Blaikied6471f72011-09-25 23:23:43 +0000435PTHManager *PTHManager::Create(const std::string &file,
436 DiagnosticsEngine &Diags) {
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000437 // Memory map the PTH file.
Dylan Noblesmith6f42b622012-02-05 02:12:40 +0000438 OwningPtr<llvm::MemoryBuffer> File;
Mike Stump1eb44332009-09-09 15:08:12 +0000439
Michael J. Spencer4eeebc42010-12-16 03:28:14 +0000440 if (llvm::MemoryBuffer::getFile(file, File)) {
Michael J. Spencer3a321e22010-12-09 17:36:38 +0000441 // FIXME: Add ec.message() to this diag.
Daniel Dunbar3574f462009-11-12 02:53:48 +0000442 Diags.Report(diag::err_invalid_pth_file) << file;
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000443 return 0;
Ted Kremenek8a6aec62009-01-28 20:49:33 +0000444 }
Mike Stump1eb44332009-09-09 15:08:12 +0000445
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000446 // Get the buffer ranges and check if there are at least three 32-bit
447 // words at the end of the file.
Roman Divacky31ba6132012-09-06 15:59:27 +0000448 const unsigned char *BufBeg = (const unsigned char*)File->getBufferStart();
449 const unsigned char *BufEnd = (const unsigned char*)File->getBufferEnd();
Ted Kremeneke1b64982009-01-26 21:43:14 +0000450
451 // Check the prologue of the file.
Richard Smithc141b512012-08-17 03:55:43 +0000452 if ((BufEnd - BufBeg) < (signed)(sizeof("cfe-pth") + 4 + 4) ||
453 memcmp(BufBeg, "cfe-pth", sizeof("cfe-pth")) != 0) {
Daniel Dunbar3574f462009-11-12 02:53:48 +0000454 Diags.Report(diag::err_invalid_pth_file) << file;
Ted Kremeneke1b64982009-01-26 21:43:14 +0000455 return 0;
Ted Kremenek26555b12009-01-28 21:02:43 +0000456 }
Mike Stump1eb44332009-09-09 15:08:12 +0000457
Ted Kremenek67d15052009-01-26 21:50:21 +0000458 // Read the PTH version.
Richard Smithc141b512012-08-17 03:55:43 +0000459 const unsigned char *p = BufBeg + (sizeof("cfe-pth"));
Ted Kremenek67d15052009-01-26 21:50:21 +0000460 unsigned Version = ReadLE32(p);
Mike Stump1eb44332009-09-09 15:08:12 +0000461
Daniel Dunbar3574f462009-11-12 02:53:48 +0000462 if (Version < PTHManager::Version) {
463 InvalidPTH(Diags,
Mike Stump1eb44332009-09-09 15:08:12 +0000464 Version < PTHManager::Version
Ted Kremenek26555b12009-01-28 21:02:43 +0000465 ? "PTH file uses an older PTH format that is no longer supported"
466 : "PTH file uses a newer PTH format that cannot be read");
Ted Kremenek67d15052009-01-26 21:50:21 +0000467 return 0;
Ted Kremenek26555b12009-01-28 21:02:43 +0000468 }
Ted Kremenek67d15052009-01-26 21:50:21 +0000469
Mike Stump1eb44332009-09-09 15:08:12 +0000470 // Compute the address of the index table at the end of the PTH file.
Ted Kremeneka4bd8eb2009-02-11 23:34:32 +0000471 const unsigned char *PrologueOffset = p;
Mike Stump1eb44332009-09-09 15:08:12 +0000472
Ted Kremeneka4bd8eb2009-02-11 23:34:32 +0000473 if (PrologueOffset >= BufEnd) {
Daniel Dunbar3574f462009-11-12 02:53:48 +0000474 Diags.Report(diag::err_invalid_pth_file) << file;
Ted Kremeneke1b64982009-01-26 21:43:14 +0000475 return 0;
Ted Kremenek26555b12009-01-28 21:02:43 +0000476 }
Mike Stump1eb44332009-09-09 15:08:12 +0000477
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000478 // Construct the file lookup table. This will be used for mapping from
479 // FileEntry*'s to cached tokens.
Ted Kremeneka4bd8eb2009-02-11 23:34:32 +0000480 const unsigned char* FileTableOffset = PrologueOffset + sizeof(uint32_t)*2;
Chris Lattner5ff43172009-01-22 19:48:26 +0000481 const unsigned char* FileTable = BufBeg + ReadLE32(FileTableOffset);
Mike Stump1eb44332009-09-09 15:08:12 +0000482
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000483 if (!(FileTable > BufBeg && FileTable < BufEnd)) {
Daniel Dunbar3574f462009-11-12 02:53:48 +0000484 Diags.Report(diag::err_invalid_pth_file) << file;
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000485 return 0; // FIXME: Proper error diagnostic?
486 }
Mike Stump1eb44332009-09-09 15:08:12 +0000487
Dylan Noblesmith6f42b622012-02-05 02:12:40 +0000488 OwningPtr<PTHFileLookup> FL(PTHFileLookup::Create(FileTable, BufBeg));
Mike Stump1eb44332009-09-09 15:08:12 +0000489
Ted Kremenek1d201972009-03-20 17:54:25 +0000490 // Warn if the PTH file is empty. We still want to create a PTHManager
491 // as the PTH could be used with -include-pth.
492 if (FL->isEmpty())
Daniel Dunbar3574f462009-11-12 02:53:48 +0000493 InvalidPTH(Diags, "PTH file contains no cached source data");
Mike Stump1eb44332009-09-09 15:08:12 +0000494
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000495 // Get the location of the table mapping from persistent ids to the
496 // data needed to reconstruct identifiers.
Ted Kremeneka4bd8eb2009-02-11 23:34:32 +0000497 const unsigned char* IDTableOffset = PrologueOffset + sizeof(uint32_t)*0;
Chris Lattner5ff43172009-01-22 19:48:26 +0000498 const unsigned char* IData = BufBeg + ReadLE32(IDTableOffset);
Mike Stump1eb44332009-09-09 15:08:12 +0000499
Ted Kremenekcdd8f212009-01-21 07:34:28 +0000500 if (!(IData >= BufBeg && IData < BufEnd)) {
Daniel Dunbar3574f462009-11-12 02:53:48 +0000501 Diags.Report(diag::err_invalid_pth_file) << file;
Ted Kremenek26555b12009-01-28 21:02:43 +0000502 return 0;
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000503 }
Mike Stump1eb44332009-09-09 15:08:12 +0000504
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000505 // Get the location of the hashtable mapping between strings and
506 // persistent IDs.
Ted Kremeneka4bd8eb2009-02-11 23:34:32 +0000507 const unsigned char* StringIdTableOffset = PrologueOffset + sizeof(uint32_t)*1;
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000508 const unsigned char* StringIdTable = BufBeg + ReadLE32(StringIdTableOffset);
509 if (!(StringIdTable >= BufBeg && StringIdTable < BufEnd)) {
Daniel Dunbar3574f462009-11-12 02:53:48 +0000510 Diags.Report(diag::err_invalid_pth_file) << file;
Ted Kremenek26555b12009-01-28 21:02:43 +0000511 return 0;
Ted Kremenek72b1b152009-01-15 18:47:46 +0000512 }
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000513
Dylan Noblesmith6f42b622012-02-05 02:12:40 +0000514 OwningPtr<PTHStringIdLookup> SL(PTHStringIdLookup::Create(StringIdTable,
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000515 BufBeg));
Mike Stump1eb44332009-09-09 15:08:12 +0000516
Ted Kremenek277faca2009-01-27 00:01:05 +0000517 // Get the location of the spelling cache.
Ted Kremeneka4bd8eb2009-02-11 23:34:32 +0000518 const unsigned char* spellingBaseOffset = PrologueOffset + sizeof(uint32_t)*3;
Ted Kremenek277faca2009-01-27 00:01:05 +0000519 const unsigned char* spellingBase = BufBeg + ReadLE32(spellingBaseOffset);
520 if (!(spellingBase >= BufBeg && spellingBase < BufEnd)) {
Daniel Dunbar3574f462009-11-12 02:53:48 +0000521 Diags.Report(diag::err_invalid_pth_file) << file;
Ted Kremenek277faca2009-01-27 00:01:05 +0000522 return 0;
523 }
Mike Stump1eb44332009-09-09 15:08:12 +0000524
Ted Kremenek6183e482008-12-03 01:16:39 +0000525 // Get the number of IdentifierInfos and pre-allocate the identifier cache.
Chris Lattner5ff43172009-01-22 19:48:26 +0000526 uint32_t NumIds = ReadLE32(IData);
Mike Stump1eb44332009-09-09 15:08:12 +0000527
Chris Lattnerfc8f0e12011-04-15 05:22:18 +0000528 // Pre-allocate the persistent ID -> IdentifierInfo* cache. We use calloc()
Ted Kremenek6183e482008-12-03 01:16:39 +0000529 // so that we in the best case only zero out memory once when the OS returns
530 // us new pages.
Ted Kremenekcdd8f212009-01-21 07:34:28 +0000531 IdentifierInfo** PerIDCache = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000532
Ted Kremenekcdd8f212009-01-21 07:34:28 +0000533 if (NumIds) {
Mike Stump1eb44332009-09-09 15:08:12 +0000534 PerIDCache = (IdentifierInfo**)calloc(NumIds, sizeof(*PerIDCache));
Ted Kremenekcdd8f212009-01-21 07:34:28 +0000535 if (!PerIDCache) {
Daniel Dunbar3574f462009-11-12 02:53:48 +0000536 InvalidPTH(Diags, "Could not allocate memory for processing PTH file");
Ted Kremenekcdd8f212009-01-21 07:34:28 +0000537 return 0;
538 }
Ted Kremenek6183e482008-12-03 01:16:39 +0000539 }
Ted Kremenekcdd8f212009-01-21 07:34:28 +0000540
Ted Kremenek68228632009-03-19 22:19:30 +0000541 // Compute the address of the original source file.
542 const unsigned char* originalSourceBase = PrologueOffset + sizeof(uint32_t)*4;
543 unsigned len = ReadUnalignedLE16(originalSourceBase);
Mike Stump1eb44332009-09-09 15:08:12 +0000544 if (!len) originalSourceBase = 0;
545
Ted Kremenek72b1b152009-01-15 18:47:46 +0000546 // Create the new PTHManager.
547 return new PTHManager(File.take(), FL.take(), IData, PerIDCache,
Ted Kremenek68228632009-03-19 22:19:30 +0000548 SL.take(), NumIds, spellingBase,
549 (const char*) originalSourceBase);
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000550}
Ted Kremenek68228632009-03-19 22:19:30 +0000551
Chris Lattner77ecb3a2009-01-18 02:57:21 +0000552IdentifierInfo* PTHManager::LazilyCreateIdentifierInfo(unsigned PersistentID) {
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000553 // Look in the PTH file for the string data for the IdentifierInfo object.
Chris Lattner77ecb3a2009-01-18 02:57:21 +0000554 const unsigned char* TableEntry = IdDataTable + sizeof(uint32_t)*PersistentID;
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000555 const unsigned char* IDData =
Chris Lattner5ff43172009-01-22 19:48:26 +0000556 (const unsigned char*)Buf->getBufferStart() + ReadLE32(TableEntry);
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000557 assert(IDData < (const unsigned char*)Buf->getBufferEnd());
Mike Stump1eb44332009-09-09 15:08:12 +0000558
Ted Kremenek72b1b152009-01-15 18:47:46 +0000559 // Allocate the object.
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000560 std::pair<IdentifierInfo,const unsigned char*> *Mem =
561 Alloc.Allocate<std::pair<IdentifierInfo,const unsigned char*> >();
Ted Kremenek72b1b152009-01-15 18:47:46 +0000562
563 Mem->second = IDData;
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000564 assert(IDData[0] != '\0');
Ted Kremenekea9c26b2009-01-20 23:28:34 +0000565 IdentifierInfo *II = new ((void*) Mem) IdentifierInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000566
Ted Kremenek72b1b152009-01-15 18:47:46 +0000567 // Store the new IdentifierInfo in the cache.
Chris Lattner77ecb3a2009-01-18 02:57:21 +0000568 PerIDCache[PersistentID] = II;
Daniel Dunbare013d682009-10-18 20:26:12 +0000569 assert(II->getNameStart() && II->getNameStart()[0] != '\0');
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000570 return II;
571}
572
Chris Lattner5f9e2722011-07-23 10:55:15 +0000573IdentifierInfo* PTHManager::get(StringRef Name) {
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000574 PTHStringIdLookup& SL = *((PTHStringIdLookup*)StringIdLookup);
575 // Double check our assumption that the last character isn't '\0'.
David Blaikie681d83d2011-09-22 01:35:49 +0000576 assert(Name.empty() || Name.back() != '\0');
Kovarththanan Rajaratnam700030e2010-03-12 08:23:34 +0000577 PTHStringIdLookup::iterator I = SL.find(std::make_pair(Name.data(),
578 Name.size()));
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000579 if (I == SL.end()) // No identifier found?
580 return 0;
Ted Kremenek72b1b152009-01-15 18:47:46 +0000581
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000582 // Match found. Return the identifier!
583 assert(*I > 0);
584 return GetIdentifierInfo(*I-1);
585}
Ted Kremenek72b1b152009-01-15 18:47:46 +0000586
Chris Lattnerf056d922009-01-17 08:06:50 +0000587PTHLexer *PTHManager::CreateLexer(FileID FID) {
588 const FileEntry *FE = PP->getSourceManager().getFileEntryForID(FID);
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000589 if (!FE)
590 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000591
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000592 // Lookup the FileEntry object in our file lookup data structure. It will
593 // return a variant that indicates whether or not there is an offset within
594 // the PTH file that contains cached tokens.
Ted Kremenekd8c02922009-02-10 22:16:22 +0000595 PTHFileLookup& PFL = *((PTHFileLookup*)FileLookup);
596 PTHFileLookup::iterator I = PFL.find(FE);
Mike Stump1eb44332009-09-09 15:08:12 +0000597
Ted Kremenekd8c02922009-02-10 22:16:22 +0000598 if (I == PFL.end()) // No tokens available?
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000599 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000600
601 const PTHFileData& FileData = *I;
602
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000603 const unsigned char *BufStart = (const unsigned char *)Buf->getBufferStart();
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000604 // Compute the offset of the token data within the buffer.
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000605 const unsigned char* data = BufStart + FileData.getTokenOffset();
Ted Kremenek268ee702008-12-12 18:34:08 +0000606
607 // Get the location of pp-conditional table.
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000608 const unsigned char* ppcond = BufStart + FileData.getPPCondOffset();
Chris Lattner5ff43172009-01-22 19:48:26 +0000609 uint32_t Len = ReadLE32(ppcond);
Chris Lattner1b5285e2009-01-18 02:10:31 +0000610 if (Len == 0) ppcond = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000611
Ted Kremenek72b1b152009-01-15 18:47:46 +0000612 assert(PP && "No preprocessor set yet!");
Mike Stump1eb44332009-09-09 15:08:12 +0000613 return new PTHLexer(*PP, FID, data, ppcond, *this);
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000614}
Ted Kremenek337edcd2009-02-12 03:26:59 +0000615
616//===----------------------------------------------------------------------===//
617// 'stat' caching.
618//===----------------------------------------------------------------------===//
619
620namespace {
Benjamin Kramer85b45212009-11-28 19:45:26 +0000621class PTHStatData {
Ted Kremenek337edcd2009-02-12 03:26:59 +0000622public:
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000623 const bool hasStat;
Ted Kremenek337edcd2009-02-12 03:26:59 +0000624 const ino_t ino;
625 const dev_t dev;
626 const mode_t mode;
627 const time_t mtime;
628 const off_t size;
Mike Stump1eb44332009-09-09 15:08:12 +0000629
Ted Kremenek337edcd2009-02-12 03:26:59 +0000630 PTHStatData(ino_t i, dev_t d, mode_t mo, time_t m, off_t s)
Mike Stump1eb44332009-09-09 15:08:12 +0000631 : hasStat(true), ino(i), dev(d), mode(mo), mtime(m), size(s) {}
632
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000633 PTHStatData()
634 : hasStat(false), ino(0), dev(0), mode(0), mtime(0), size(0) {}
Ted Kremenek337edcd2009-02-12 03:26:59 +0000635};
Mike Stump1eb44332009-09-09 15:08:12 +0000636
Benjamin Kramer85b45212009-11-28 19:45:26 +0000637class PTHStatLookupTrait : public PTHFileLookupCommonTrait {
Ted Kremenek337edcd2009-02-12 03:26:59 +0000638public:
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000639 typedef const char* external_key_type; // const char*
Ted Kremenek337edcd2009-02-12 03:26:59 +0000640 typedef PTHStatData data_type;
Mike Stump1eb44332009-09-09 15:08:12 +0000641
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000642 static internal_key_type GetInternalKey(const char *path) {
643 // The key 'kind' doesn't matter here because it is ignored in EqualKey.
644 return std::make_pair((unsigned char) 0x0, path);
645 }
646
647 static bool EqualKey(internal_key_type a, internal_key_type b) {
648 // When doing 'stat' lookups we don't care about the kind of 'a' and 'b',
649 // just the paths.
650 return strcmp(a.second, b.second) == 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000651 }
652
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000653 static data_type ReadData(const internal_key_type& k, const unsigned char* d,
Mike Stump1eb44332009-09-09 15:08:12 +0000654 unsigned) {
655
Ted Kremenekad6ce5c2009-02-13 22:07:44 +0000656 if (k.first /* File or Directory */) {
657 if (k.first == 0x1 /* File */) d += 4 * 2; // Skip the first 2 words.
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000658 ino_t ino = (ino_t) ReadUnalignedLE32(d);
659 dev_t dev = (dev_t) ReadUnalignedLE32(d);
660 mode_t mode = (mode_t) ReadUnalignedLE16(d);
Mike Stump1eb44332009-09-09 15:08:12 +0000661 time_t mtime = (time_t) ReadUnalignedLE64(d);
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000662 return data_type(ino, dev, mode, mtime, (off_t) ReadUnalignedLE64(d));
663 }
Ted Kremenekad6ce5c2009-02-13 22:07:44 +0000664
665 // Negative stat. Don't read anything.
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000666 return data_type();
Ted Kremenek337edcd2009-02-12 03:26:59 +0000667 }
668};
Ted Kremenek337edcd2009-02-12 03:26:59 +0000669
Chris Lattner10e286a2010-11-23 19:19:34 +0000670class PTHStatCache : public FileSystemStatCache {
Ted Kremenek337edcd2009-02-12 03:26:59 +0000671 typedef OnDiskChainedHashTable<PTHStatLookupTrait> CacheTy;
672 CacheTy Cache;
673
Mike Stump1eb44332009-09-09 15:08:12 +0000674public:
Ted Kremenek337edcd2009-02-12 03:26:59 +0000675 PTHStatCache(PTHFileLookup &FL) :
676 Cache(FL.getNumBuckets(), FL.getNumEntries(), FL.getBuckets(),
677 FL.getBase()) {}
678
679 ~PTHStatCache() {}
Mike Stump1eb44332009-09-09 15:08:12 +0000680
Chris Lattner898a0612010-11-23 21:17:56 +0000681 LookupResult getStat(const char *Path, struct stat &StatBuf,
682 int *FileDescriptor) {
Ted Kremenek337edcd2009-02-12 03:26:59 +0000683 // Do the lookup for the file's data in the PTH file.
Chris Lattner10e286a2010-11-23 19:19:34 +0000684 CacheTy::iterator I = Cache.find(Path);
Ted Kremenek337edcd2009-02-12 03:26:59 +0000685
686 // If we don't get a hit in the PTH file just forward to 'stat'.
Kovarththanan Rajaratnam700030e2010-03-12 08:23:34 +0000687 if (I == Cache.end())
Chris Lattner898a0612010-11-23 21:17:56 +0000688 return statChained(Path, StatBuf, FileDescriptor);
Mike Stump1eb44332009-09-09 15:08:12 +0000689
Chris Lattner10e286a2010-11-23 19:19:34 +0000690 const PTHStatData &Data = *I;
Mike Stump1eb44332009-09-09 15:08:12 +0000691
Ted Kremenekad6ce5c2009-02-13 22:07:44 +0000692 if (!Data.hasStat)
Chris Lattnerd6f61112010-11-23 20:05:15 +0000693 return CacheMissing;
Ted Kremenekad6ce5c2009-02-13 22:07:44 +0000694
Chris Lattner10e286a2010-11-23 19:19:34 +0000695 StatBuf.st_ino = Data.ino;
696 StatBuf.st_dev = Data.dev;
697 StatBuf.st_mtime = Data.mtime;
698 StatBuf.st_mode = Data.mode;
699 StatBuf.st_size = Data.size;
Chris Lattnerd6f61112010-11-23 20:05:15 +0000700 return CacheExists;
Ted Kremenek337edcd2009-02-12 03:26:59 +0000701 }
702};
Ted Kremenekd5785692009-02-23 23:27:54 +0000703} // end anonymous namespace
Ted Kremenek337edcd2009-02-12 03:26:59 +0000704
Chris Lattner10e286a2010-11-23 19:19:34 +0000705FileSystemStatCache *PTHManager::createStatCache() {
Ted Kremenek5f747d12009-02-12 03:45:39 +0000706 return new PTHStatCache(*((PTHFileLookup*) FileLookup));
Ted Kremenek337edcd2009-02-12 03:26:59 +0000707}