blob: 1ca16d39b4df39b3bf4ac2546cf1609c0856f5fe [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
Chandler Carruth55fc8732012-12-04 09:13:33 +000014#include "clang/Lex/PTHLexer.h"
Ted Kremenek0c6a77b2008-12-03 00:38:03 +000015#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"
Chandler Carruth55fc8732012-12-04 09:13:33 +000019#include "clang/Basic/TokenKinds.h"
Daniel Dunbar3574f462009-11-12 02:53:48 +000020#include "clang/Lex/LexDiagnostic.h"
Ted Kremenek0c6a77b2008-12-03 00:38:03 +000021#include "clang/Lex/PTHManager.h"
Ted Kremenek0c6a77b2008-12-03 00:38:03 +000022#include "clang/Lex/Preprocessor.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000023#include "clang/Lex/Token.h"
Daniel Dunbar2596e422009-10-17 23:52:28 +000024#include "llvm/ADT/StringExtras.h"
25#include "llvm/ADT/StringMap.h"
Stephen Hines651f13c2014-04-23 16:59:28 -070026#include "llvm/Support/EndianStream.h"
Chris Lattner6f78c3b2009-01-22 23:50:07 +000027#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer3a321e22010-12-09 17:36:38 +000028#include "llvm/Support/system_error.h"
Stephen Hines651f13c2014-04-23 16:59:28 -070029#include <memory>
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
Eli Friedmand2f93082013-09-19 00:41:32 +000047bool PTHLexer::Lex(Token& Tok) {
Ted Kremenek866bdf72008-12-23 02:30:15 +000048 //===--------------------------------------==//
49 // Read the raw token data.
50 //===--------------------------------------==//
Stephen Hines651f13c2014-04-23 16:59:28 -070051 using namespace llvm::support;
Mike Stump1eb44332009-09-09 15:08:12 +000052
Ted Kremenek866bdf72008-12-23 02:30:15 +000053 // Shadow CurPtr into an automatic variable.
Mike Stump1eb44332009-09-09 15:08:12 +000054 const unsigned char *CurPtrShadow = CurPtr;
Ted Kremenek866bdf72008-12-23 02:30:15 +000055
Chris Lattner1b5285e2009-01-18 02:10:31 +000056 // Read in the data for the token.
Stephen Hines651f13c2014-04-23 16:59:28 -070057 unsigned Word0 = endian::readNext<uint32_t, little, aligned>(CurPtrShadow);
58 uint32_t IdentifierID =
59 endian::readNext<uint32_t, little, aligned>(CurPtrShadow);
60 uint32_t FileOffset =
61 endian::readNext<uint32_t, little, aligned>(CurPtrShadow);
Mike Stump1eb44332009-09-09 15:08:12 +000062
Ted Kremenek7b78b7c2009-01-19 23:13:15 +000063 tok::TokenKind TKind = (tok::TokenKind) (Word0 & 0xFF);
64 Token::TokenFlags TFlags = (Token::TokenFlags) ((Word0 >> 8) & 0xFF);
Chris Lattneraff6ef82009-01-21 07:21:56 +000065 uint32_t Len = Word0 >> 16;
Ted Kremenek7b78b7c2009-01-19 23:13:15 +000066
Chris Lattneraff6ef82009-01-21 07:21:56 +000067 CurPtr = CurPtrShadow;
Mike Stump1eb44332009-09-09 15:08:12 +000068
Ted Kremenek866bdf72008-12-23 02:30:15 +000069 //===--------------------------------------==//
70 // Construct the token itself.
71 //===--------------------------------------==//
Mike Stump1eb44332009-09-09 15:08:12 +000072
Ted Kremenek866bdf72008-12-23 02:30:15 +000073 Tok.startToken();
Chris Lattner898a0bb2009-01-18 02:34:01 +000074 Tok.setKind(TKind);
75 Tok.setFlag(TFlags);
Ted Kremenek59d08cb2008-12-23 19:24:24 +000076 assert(!LexingRawMode);
Argyrios Kyrtzidisa64ccef2011-09-19 20:40:19 +000077 Tok.setLocation(FileStartLoc.getLocWithOffset(FileOffset));
Ted Kremenek866bdf72008-12-23 02:30:15 +000078 Tok.setLength(Len);
79
Chris Lattnerd0a69692009-01-21 07:50:06 +000080 // Handle identifiers.
Ted Kremenek277faca2009-01-27 00:01:05 +000081 if (Tok.isLiteral()) {
82 Tok.setLiteralData((const char*) (PTHMgr.SpellingBase + IdentifierID));
83 }
84 else if (IdentifierID) {
Chris Lattnerd0a69692009-01-21 07:50:06 +000085 MIOpt.ReadToken();
86 IdentifierInfo *II = PTHMgr.GetIdentifierInfo(IdentifierID-1);
Mike Stump1eb44332009-09-09 15:08:12 +000087
Chris Lattnerd0a69692009-01-21 07:50:06 +000088 Tok.setIdentifierInfo(II);
Mike Stump1eb44332009-09-09 15:08:12 +000089
Chris Lattner863c4862009-01-23 18:35:48 +000090 // Change the kind of this identifier to the appropriate token kind, e.g.
91 // turning "for" into a keyword.
92 Tok.setKind(II->getTokenID());
Mike Stump1eb44332009-09-09 15:08:12 +000093
Chris Lattnerd0a69692009-01-21 07:50:06 +000094 if (II->isHandleIdentifierCase())
Eli Friedmand2f93082013-09-19 00:41:32 +000095 return PP->HandleIdentifier(Tok);
96
97 return true;
Chris Lattnerd0a69692009-01-21 07:50:06 +000098 }
Mike Stump1eb44332009-09-09 15:08:12 +000099
Ted Kremenek866bdf72008-12-23 02:30:15 +0000100 //===--------------------------------------==//
101 // Process the token.
102 //===--------------------------------------==//
Chris Lattner898a0bb2009-01-18 02:34:01 +0000103 if (TKind == tok::eof) {
Ted Kremeneke5680f32008-12-23 01:30:52 +0000104 // Save the end-of-file token.
105 EofToken = Tok;
Mike Stump1eb44332009-09-09 15:08:12 +0000106
Ted Kremenek59d08cb2008-12-23 19:24:24 +0000107 assert(!ParsingPreprocessorDirective);
108 assert(!LexingRawMode);
Mike Stump1eb44332009-09-09 15:08:12 +0000109
Eli Friedmand2f93082013-09-19 00:41:32 +0000110 return LexEndOfFile(Tok);
Ted Kremeneke5680f32008-12-23 01:30:52 +0000111 }
Mike Stump1eb44332009-09-09 15:08:12 +0000112
Chris Lattner898a0bb2009-01-18 02:34:01 +0000113 if (TKind == tok::hash && Tok.isAtStartOfLine()) {
Ted Kremenek59d08cb2008-12-23 19:24:24 +0000114 LastHashTokPtr = CurPtr - DISK_TOKEN_SIZE;
115 assert(!LexingRawMode);
116 PP->HandleDirective(Tok);
Mike Stump1eb44332009-09-09 15:08:12 +0000117
Eli Friedmand2f93082013-09-19 00:41:32 +0000118 return false;
Ted Kremenek59d08cb2008-12-23 19:24:24 +0000119 }
Mike Stump1eb44332009-09-09 15:08:12 +0000120
Peter Collingbourne84021552011-02-28 02:37:51 +0000121 if (TKind == tok::eod) {
Ted Kremenek59d08cb2008-12-23 19:24:24 +0000122 assert(ParsingPreprocessorDirective);
Ted Kremeneke5680f32008-12-23 01:30:52 +0000123 ParsingPreprocessorDirective = false;
Eli Friedmand2f93082013-09-19 00:41:32 +0000124 return true;
Ted Kremeneke5680f32008-12-23 01:30:52 +0000125 }
Ted Kremeneke5680f32008-12-23 01:30:52 +0000126
Ted Kremenek59d08cb2008-12-23 19:24:24 +0000127 MIOpt.ReadToken();
Eli Friedmand2f93082013-09-19 00:41:32 +0000128 return true;
Ted Kremeneke5680f32008-12-23 01:30:52 +0000129}
130
Ted Kremenek94e3d1f2010-07-27 02:59:02 +0000131bool PTHLexer::LexEndOfFile(Token &Result) {
132 // If we hit the end of the file while parsing a preprocessor directive,
133 // end the preprocessor directive first. The next token returned will
134 // then be the end of file.
135 if (ParsingPreprocessorDirective) {
136 ParsingPreprocessorDirective = false; // Done parsing the "line".
137 return true; // Have a token.
138 }
139
140 assert(!LexingRawMode);
141
142 // If we are in a #if directive, emit an error.
143 while (!ConditionalStack.empty()) {
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000144 if (PP->getCodeCompletionFileLoc() != FileStartLoc)
Douglas Gregor2d474ba2010-08-12 17:04:55 +0000145 PP->Diag(ConditionalStack.back().IfLoc,
146 diag::err_pp_unterminated_conditional);
Ted Kremenek94e3d1f2010-07-27 02:59:02 +0000147 ConditionalStack.pop_back();
148 }
149
150 // Finally, let the preprocessor handle this.
151 return PP->HandleEndOfFile(Result);
152}
153
Ted Kremeneke5680f32008-12-23 01:30:52 +0000154// FIXME: We can just grab the last token instead of storing a copy
155// into EofToken.
Ted Kremenek59d08cb2008-12-23 19:24:24 +0000156void PTHLexer::getEOF(Token& Tok) {
Ted Kremenekdefb7092009-01-09 00:36:11 +0000157 assert(EofToken.is(tok::eof));
Ted Kremeneke5680f32008-12-23 01:30:52 +0000158 Tok = EofToken;
159}
160
161void PTHLexer::DiscardToEndOfLine() {
162 assert(ParsingPreprocessorDirective && ParsingFilename == false &&
163 "Must be in a preprocessing directive!");
164
165 // We assume that if the preprocessor wishes to discard to the end of
166 // the line that it also means to end the current preprocessor directive.
167 ParsingPreprocessorDirective = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000168
Ted Kremeneke5680f32008-12-23 01:30:52 +0000169 // Skip tokens by only peeking at their token kind and the flags.
170 // We don't need to actually reconstruct full tokens from the token buffer.
171 // This saves some copies and it also reduces IdentifierInfo* lookup.
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000172 const unsigned char* p = CurPtr;
Ted Kremeneke5680f32008-12-23 01:30:52 +0000173 while (1) {
174 // Read the token kind. Are we at the end of the file?
175 tok::TokenKind x = (tok::TokenKind) (uint8_t) *p;
176 if (x == tok::eof) break;
Mike Stump1eb44332009-09-09 15:08:12 +0000177
Ted Kremeneke5680f32008-12-23 01:30:52 +0000178 // Read the token flags. Are we at the start of the next line?
179 Token::TokenFlags y = (Token::TokenFlags) (uint8_t) p[1];
180 if (y & Token::StartOfLine) break;
181
182 // Skip to the next token.
183 p += DISK_TOKEN_SIZE;
184 }
Mike Stump1eb44332009-09-09 15:08:12 +0000185
Ted Kremeneke5680f32008-12-23 01:30:52 +0000186 CurPtr = p;
187}
188
Ted Kremenek268ee702008-12-12 18:34:08 +0000189/// SkipBlock - Used by Preprocessor to skip the current conditional block.
190bool PTHLexer::SkipBlock() {
Stephen Hines651f13c2014-04-23 16:59:28 -0700191 using namespace llvm::support;
Ted Kremenek268ee702008-12-12 18:34:08 +0000192 assert(CurPPCondPtr && "No cached PP conditional information.");
193 assert(LastHashTokPtr && "No known '#' token.");
Mike Stump1eb44332009-09-09 15:08:12 +0000194
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000195 const unsigned char* HashEntryI = 0;
Ted Kremenek268ee702008-12-12 18:34:08 +0000196 uint32_t TableIdx;
Mike Stump1eb44332009-09-09 15:08:12 +0000197
Ted Kremenek268ee702008-12-12 18:34:08 +0000198 do {
Ted Kremenek41a26602008-12-12 22:05:38 +0000199 // Read the token offset from the side-table.
Stephen Hines651f13c2014-04-23 16:59:28 -0700200 uint32_t Offset = endian::readNext<uint32_t, little, aligned>(CurPPCondPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000201
202 // Read the target table index from the side-table.
Stephen Hines651f13c2014-04-23 16:59:28 -0700203 TableIdx = endian::readNext<uint32_t, little, aligned>(CurPPCondPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000204
Ted Kremenek41a26602008-12-12 22:05:38 +0000205 // Compute the actual memory address of the '#' token data for this entry.
206 HashEntryI = TokBuf + Offset;
207
208 // Optmization: "Sibling jumping". #if...#else...#endif blocks can
209 // contain nested blocks. In the side-table we can jump over these
210 // nested blocks instead of doing a linear search if the next "sibling"
211 // entry is not at a location greater than LastHashTokPtr.
212 if (HashEntryI < LastHashTokPtr && TableIdx) {
213 // In the side-table we are still at an entry for a '#' token that
214 // is earlier than the last one we saw. Check if the location we would
215 // stride gets us closer.
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000216 const unsigned char* NextPPCondPtr =
217 PPCond + TableIdx*(sizeof(uint32_t)*2);
Ted Kremenek41a26602008-12-12 22:05:38 +0000218 assert(NextPPCondPtr >= CurPPCondPtr);
219 // Read where we should jump to.
Stephen Hines651f13c2014-04-23 16:59:28 -0700220 const unsigned char *HashEntryJ =
221 TokBuf + endian::readNext<uint32_t, little, aligned>(NextPPCondPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000222
Ted Kremenek41a26602008-12-12 22:05:38 +0000223 if (HashEntryJ <= LastHashTokPtr) {
224 // Jump directly to the next entry in the side table.
225 HashEntryI = HashEntryJ;
Stephen Hines651f13c2014-04-23 16:59:28 -0700226 TableIdx = endian::readNext<uint32_t, little, aligned>(NextPPCondPtr);
Ted Kremenek41a26602008-12-12 22:05:38 +0000227 CurPPCondPtr = NextPPCondPtr;
228 }
229 }
Ted Kremenek268ee702008-12-12 18:34:08 +0000230 }
Mike Stump1eb44332009-09-09 15:08:12 +0000231 while (HashEntryI < LastHashTokPtr);
Ted Kremenek41a26602008-12-12 22:05:38 +0000232 assert(HashEntryI == LastHashTokPtr && "No PP-cond entry found for '#'");
Ted Kremenek268ee702008-12-12 18:34:08 +0000233 assert(TableIdx && "No jumping from #endifs.");
Mike Stump1eb44332009-09-09 15:08:12 +0000234
Ted Kremenek268ee702008-12-12 18:34:08 +0000235 // Update our side-table iterator.
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000236 const unsigned char* NextPPCondPtr = PPCond + TableIdx*(sizeof(uint32_t)*2);
Ted Kremenek268ee702008-12-12 18:34:08 +0000237 assert(NextPPCondPtr >= CurPPCondPtr);
238 CurPPCondPtr = NextPPCondPtr;
Mike Stump1eb44332009-09-09 15:08:12 +0000239
Ted Kremenek268ee702008-12-12 18:34:08 +0000240 // Read where we should jump to.
Stephen Hines651f13c2014-04-23 16:59:28 -0700241 HashEntryI =
242 TokBuf + endian::readNext<uint32_t, little, aligned>(NextPPCondPtr);
243 uint32_t NextIdx = endian::readNext<uint32_t, little, aligned>(NextPPCondPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000244
Ted Kremenek268ee702008-12-12 18:34:08 +0000245 // By construction NextIdx will be zero if this is a #endif. This is useful
246 // to know to obviate lexing another token.
247 bool isEndif = NextIdx == 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000248
Ted Kremenek268ee702008-12-12 18:34:08 +0000249 // This case can occur when we see something like this:
250 //
251 // #if ...
252 // /* a comment or nothing */
253 // #elif
254 //
255 // If we are skipping the first #if block it will be the case that CurPtr
256 // already points 'elif'. Just return.
Mike Stump1eb44332009-09-09 15:08:12 +0000257
Ted Kremenek41a26602008-12-12 22:05:38 +0000258 if (CurPtr > HashEntryI) {
259 assert(CurPtr == HashEntryI + DISK_TOKEN_SIZE);
Ted Kremenek268ee702008-12-12 18:34:08 +0000260 // Did we reach a #endif? If so, go ahead and consume that token as well.
261 if (isEndif)
Ted Kremeneke5680f32008-12-23 01:30:52 +0000262 CurPtr += DISK_TOKEN_SIZE*2;
Ted Kremenek268ee702008-12-12 18:34:08 +0000263 else
Ted Kremenek41a26602008-12-12 22:05:38 +0000264 LastHashTokPtr = HashEntryI;
Mike Stump1eb44332009-09-09 15:08:12 +0000265
Ted Kremenek268ee702008-12-12 18:34:08 +0000266 return isEndif;
267 }
268
269 // Otherwise, we need to advance. Update CurPtr to point to the '#' token.
Ted Kremenek41a26602008-12-12 22:05:38 +0000270 CurPtr = HashEntryI;
Mike Stump1eb44332009-09-09 15:08:12 +0000271
Ted Kremenek268ee702008-12-12 18:34:08 +0000272 // Update the location of the last observed '#'. This is useful if we
273 // are skipping multiple blocks.
274 LastHashTokPtr = CurPtr;
Ted Kremenek268ee702008-12-12 18:34:08 +0000275
Ted Kremeneke5680f32008-12-23 01:30:52 +0000276 // Skip the '#' token.
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000277 assert(((tok::TokenKind)*CurPtr) == tok::hash);
Ted Kremeneke5680f32008-12-23 01:30:52 +0000278 CurPtr += DISK_TOKEN_SIZE;
Mike Stump1eb44332009-09-09 15:08:12 +0000279
Ted Kremenek268ee702008-12-12 18:34:08 +0000280 // Did we reach a #endif? If so, go ahead and consume that token as well.
Ted Kremeneke5680f32008-12-23 01:30:52 +0000281 if (isEndif) { CurPtr += DISK_TOKEN_SIZE*2; }
Ted Kremenek268ee702008-12-12 18:34:08 +0000282
283 return isEndif;
284}
285
Ted Kremenek30a12ec2008-12-17 23:36:32 +0000286SourceLocation PTHLexer::getSourceLocation() {
Chris Lattner1b5285e2009-01-18 02:10:31 +0000287 // getSourceLocation is not on the hot path. It is used to get the location
288 // of the next token when transitioning back to this lexer when done
Ted Kremenek30a12ec2008-12-17 23:36:32 +0000289 // handling a #included file. Just read the necessary data from the token
290 // data buffer to construct the SourceLocation object.
291 // NOTE: This is a virtual function; hence it is defined out-of-line.
Stephen Hines651f13c2014-04-23 16:59:28 -0700292 using namespace llvm::support;
293
Ted Kremenekb248d532009-01-21 22:41:38 +0000294 const unsigned char *OffsetPtr = CurPtr + (DISK_TOKEN_SIZE - 4);
Stephen Hines651f13c2014-04-23 16:59:28 -0700295 uint32_t Offset = endian::readNext<uint32_t, little, aligned>(OffsetPtr);
Argyrios Kyrtzidisa64ccef2011-09-19 20:40:19 +0000296 return FileStartLoc.getLocWithOffset(Offset);
Ted Kremenek30a12ec2008-12-17 23:36:32 +0000297}
298
Ted Kremenek5f074262009-01-09 22:05:30 +0000299//===----------------------------------------------------------------------===//
Ted Kremenekd8c02922009-02-10 22:16:22 +0000300// PTH file lookup: map from strings to file data.
301//===----------------------------------------------------------------------===//
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000302
303/// PTHFileLookup - This internal data structure is used by the PTHManager
304/// to map from FileEntry objects managed by FileManager to offsets within
305/// the PTH file.
306namespace {
Benjamin Kramer85b45212009-11-28 19:45:26 +0000307class PTHFileData {
Ted Kremenekd8c02922009-02-10 22:16:22 +0000308 const uint32_t TokenOff;
309 const uint32_t PPCondOff;
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000310public:
Ted Kremenekd8c02922009-02-10 22:16:22 +0000311 PTHFileData(uint32_t tokenOff, uint32_t ppCondOff)
312 : TokenOff(tokenOff), PPCondOff(ppCondOff) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000313
314 uint32_t getTokenOffset() const { return TokenOff; }
315 uint32_t getPPCondOffset() const { return PPCondOff; }
Ted Kremenekd8c02922009-02-10 22:16:22 +0000316};
Mike Stump1eb44332009-09-09 15:08:12 +0000317
318
Benjamin Kramer85b45212009-11-28 19:45:26 +0000319class PTHFileLookupCommonTrait {
Ted Kremenekd8c02922009-02-10 22:16:22 +0000320public:
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000321 typedef std::pair<unsigned char, const char*> internal_key_type;
322
323 static unsigned ComputeHash(internal_key_type x) {
Daniel Dunbar2596e422009-10-17 23:52:28 +0000324 return llvm::HashString(x.second);
Ted Kremenekd8c02922009-02-10 22:16:22 +0000325 }
Mike Stump1eb44332009-09-09 15:08:12 +0000326
Ted Kremenekd8c02922009-02-10 22:16:22 +0000327 static std::pair<unsigned, unsigned>
328 ReadKeyDataLength(const unsigned char*& d) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700329 using namespace llvm::support;
330 unsigned keyLen =
331 (unsigned)endian::readNext<uint16_t, little, unaligned>(d);
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000332 unsigned dataLen = (unsigned) *(d++);
333 return std::make_pair(keyLen, dataLen);
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000334 }
Mike Stump1eb44332009-09-09 15:08:12 +0000335
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000336 static internal_key_type ReadKey(const unsigned char* d, unsigned) {
337 unsigned char k = *(d++); // Read the entry kind.
338 return std::make_pair(k, (const char*) d);
Ted Kremenekd8c02922009-02-10 22:16:22 +0000339 }
Ted Kremenek337edcd2009-02-12 03:26:59 +0000340};
Mike Stump1eb44332009-09-09 15:08:12 +0000341
Benjamin Kramer85b45212009-11-28 19:45:26 +0000342class PTHFileLookupTrait : public PTHFileLookupCommonTrait {
Ted Kremenek337edcd2009-02-12 03:26:59 +0000343public:
344 typedef const FileEntry* external_key_type;
345 typedef PTHFileData data_type;
Mike Stump1eb44332009-09-09 15:08:12 +0000346
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000347 static internal_key_type GetInternalKey(const FileEntry* FE) {
348 return std::make_pair((unsigned char) 0x1, FE->getName());
Ted Kremenek337edcd2009-02-12 03:26:59 +0000349 }
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000350
351 static bool EqualKey(internal_key_type a, internal_key_type b) {
352 return a.first == b.first && strcmp(a.second, b.second) == 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000353 }
354
355 static PTHFileData ReadData(const internal_key_type& k,
356 const unsigned char* d, unsigned) {
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000357 assert(k.first == 0x1 && "Only file lookups can match!");
Stephen Hines651f13c2014-04-23 16:59:28 -0700358 using namespace llvm::support;
359 uint32_t x = endian::readNext<uint32_t, little, unaligned>(d);
360 uint32_t y = endian::readNext<uint32_t, little, unaligned>(d);
Mike Stump1eb44332009-09-09 15:08:12 +0000361 return PTHFileData(x, y);
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000362 }
363};
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000364
Benjamin Kramer85b45212009-11-28 19:45:26 +0000365class PTHStringLookupTrait {
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000366public:
Mike Stump1eb44332009-09-09 15:08:12 +0000367 typedef uint32_t
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000368 data_type;
369
370 typedef const std::pair<const char*, unsigned>
371 external_key_type;
372
373 typedef external_key_type internal_key_type;
Mike Stump1eb44332009-09-09 15:08:12 +0000374
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000375 static bool EqualKey(const internal_key_type& a,
376 const internal_key_type& b) {
377 return (a.second == b.second) ? memcmp(a.first, b.first, a.second) == 0
378 : false;
379 }
Mike Stump1eb44332009-09-09 15:08:12 +0000380
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000381 static unsigned ComputeHash(const internal_key_type& a) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000382 return llvm::HashString(StringRef(a.first, a.second));
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000383 }
Mike Stump1eb44332009-09-09 15:08:12 +0000384
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000385 // This hopefully will just get inlined and removed by the optimizer.
386 static const internal_key_type&
387 GetInternalKey(const external_key_type& x) { return x; }
Mike Stump1eb44332009-09-09 15:08:12 +0000388
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000389 static std::pair<unsigned, unsigned>
390 ReadKeyDataLength(const unsigned char*& d) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700391 using namespace llvm::support;
392 return std::make_pair(
393 (unsigned)endian::readNext<uint16_t, little, unaligned>(d),
394 sizeof(uint32_t));
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000395 }
Mike Stump1eb44332009-09-09 15:08:12 +0000396
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000397 static std::pair<const char*, unsigned>
398 ReadKey(const unsigned char* d, unsigned n) {
399 assert(n >= 2 && d[n-1] == '\0');
400 return std::make_pair((const char*) d, n-1);
401 }
Mike Stump1eb44332009-09-09 15:08:12 +0000402
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000403 static uint32_t ReadData(const internal_key_type& k, const unsigned char* d,
404 unsigned) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700405 using namespace llvm::support;
406 return endian::readNext<uint32_t, little, unaligned>(d);
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000407 }
408};
Mike Stump1eb44332009-09-09 15:08:12 +0000409
410} // end anonymous namespace
Ted Kremenekd8c02922009-02-10 22:16:22 +0000411
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000412typedef OnDiskChainedHashTable<PTHFileLookupTrait> PTHFileLookup;
413typedef OnDiskChainedHashTable<PTHStringLookupTrait> PTHStringIdLookup;
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000414
415//===----------------------------------------------------------------------===//
416// PTHManager methods.
417//===----------------------------------------------------------------------===//
418
419PTHManager::PTHManager(const llvm::MemoryBuffer* buf, void* fileLookup,
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000420 const unsigned char* idDataTable,
Mike Stump1eb44332009-09-09 15:08:12 +0000421 IdentifierInfo** perIDCache,
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000422 void* stringIdLookup, unsigned numIds,
Ted Kremenek68228632009-03-19 22:19:30 +0000423 const unsigned char* spellingBase,
424 const char* originalSourceFile)
Ted Kremenek6183e482008-12-03 01:16:39 +0000425: Buf(buf), PerIDCache(perIDCache), FileLookup(fileLookup),
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000426 IdDataTable(idDataTable), StringIdLookup(stringIdLookup),
Ted Kremenek68228632009-03-19 22:19:30 +0000427 NumIds(numIds), PP(0), SpellingBase(spellingBase),
428 OriginalSourceFile(originalSourceFile) {}
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000429
430PTHManager::~PTHManager() {
431 delete Buf;
432 delete (PTHFileLookup*) FileLookup;
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000433 delete (PTHStringIdLookup*) StringIdLookup;
Ted Kremenek0e50b6e2008-12-04 22:47:11 +0000434 free(PerIDCache);
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000435}
436
David Blaikied6471f72011-09-25 23:23:43 +0000437static void InvalidPTH(DiagnosticsEngine &Diags, const char *Msg) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700438 Diags.Report(Diags.getCustomDiagID(DiagnosticsEngine::Error, "%0")) << Msg;
Ted Kremenek26555b12009-01-28 21:02:43 +0000439}
440
David Blaikied6471f72011-09-25 23:23:43 +0000441PTHManager *PTHManager::Create(const std::string &file,
442 DiagnosticsEngine &Diags) {
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000443 // Memory map the PTH file.
Stephen Hines651f13c2014-04-23 16:59:28 -0700444 std::unique_ptr<llvm::MemoryBuffer> File;
Mike Stump1eb44332009-09-09 15:08:12 +0000445
Michael J. Spencer4eeebc42010-12-16 03:28:14 +0000446 if (llvm::MemoryBuffer::getFile(file, File)) {
Michael J. Spencer3a321e22010-12-09 17:36:38 +0000447 // FIXME: Add ec.message() to this diag.
Daniel Dunbar3574f462009-11-12 02:53:48 +0000448 Diags.Report(diag::err_invalid_pth_file) << file;
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000449 return 0;
Ted Kremenek8a6aec62009-01-28 20:49:33 +0000450 }
Mike Stump1eb44332009-09-09 15:08:12 +0000451
Stephen Hines651f13c2014-04-23 16:59:28 -0700452 using namespace llvm::support;
453
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000454 // Get the buffer ranges and check if there are at least three 32-bit
455 // words at the end of the file.
Roman Divacky31ba6132012-09-06 15:59:27 +0000456 const unsigned char *BufBeg = (const unsigned char*)File->getBufferStart();
457 const unsigned char *BufEnd = (const unsigned char*)File->getBufferEnd();
Ted Kremeneke1b64982009-01-26 21:43:14 +0000458
459 // Check the prologue of the file.
Richard Smithc141b512012-08-17 03:55:43 +0000460 if ((BufEnd - BufBeg) < (signed)(sizeof("cfe-pth") + 4 + 4) ||
461 memcmp(BufBeg, "cfe-pth", sizeof("cfe-pth")) != 0) {
Daniel Dunbar3574f462009-11-12 02:53:48 +0000462 Diags.Report(diag::err_invalid_pth_file) << file;
Ted Kremeneke1b64982009-01-26 21:43:14 +0000463 return 0;
Ted Kremenek26555b12009-01-28 21:02:43 +0000464 }
Mike Stump1eb44332009-09-09 15:08:12 +0000465
Ted Kremenek67d15052009-01-26 21:50:21 +0000466 // Read the PTH version.
Richard Smithc141b512012-08-17 03:55:43 +0000467 const unsigned char *p = BufBeg + (sizeof("cfe-pth"));
Stephen Hines651f13c2014-04-23 16:59:28 -0700468 unsigned Version = endian::readNext<uint32_t, little, aligned>(p);
Mike Stump1eb44332009-09-09 15:08:12 +0000469
Daniel Dunbar3574f462009-11-12 02:53:48 +0000470 if (Version < PTHManager::Version) {
471 InvalidPTH(Diags,
Mike Stump1eb44332009-09-09 15:08:12 +0000472 Version < PTHManager::Version
Ted Kremenek26555b12009-01-28 21:02:43 +0000473 ? "PTH file uses an older PTH format that is no longer supported"
474 : "PTH file uses a newer PTH format that cannot be read");
Ted Kremenek67d15052009-01-26 21:50:21 +0000475 return 0;
Ted Kremenek26555b12009-01-28 21:02:43 +0000476 }
Ted Kremenek67d15052009-01-26 21:50:21 +0000477
Mike Stump1eb44332009-09-09 15:08:12 +0000478 // Compute the address of the index table at the end of the PTH file.
Ted Kremeneka4bd8eb2009-02-11 23:34:32 +0000479 const unsigned char *PrologueOffset = p;
Mike Stump1eb44332009-09-09 15:08:12 +0000480
Ted Kremeneka4bd8eb2009-02-11 23:34:32 +0000481 if (PrologueOffset >= BufEnd) {
Daniel Dunbar3574f462009-11-12 02:53:48 +0000482 Diags.Report(diag::err_invalid_pth_file) << file;
Ted Kremeneke1b64982009-01-26 21:43:14 +0000483 return 0;
Ted Kremenek26555b12009-01-28 21:02:43 +0000484 }
Mike Stump1eb44332009-09-09 15:08:12 +0000485
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000486 // Construct the file lookup table. This will be used for mapping from
487 // FileEntry*'s to cached tokens.
Ted Kremeneka4bd8eb2009-02-11 23:34:32 +0000488 const unsigned char* FileTableOffset = PrologueOffset + sizeof(uint32_t)*2;
Stephen Hines651f13c2014-04-23 16:59:28 -0700489 const unsigned char *FileTable =
490 BufBeg + endian::readNext<uint32_t, little, aligned>(FileTableOffset);
Mike Stump1eb44332009-09-09 15:08:12 +0000491
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000492 if (!(FileTable > BufBeg && FileTable < BufEnd)) {
Daniel Dunbar3574f462009-11-12 02:53:48 +0000493 Diags.Report(diag::err_invalid_pth_file) << file;
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000494 return 0; // FIXME: Proper error diagnostic?
495 }
Mike Stump1eb44332009-09-09 15:08:12 +0000496
Stephen Hines651f13c2014-04-23 16:59:28 -0700497 std::unique_ptr<PTHFileLookup> FL(PTHFileLookup::Create(FileTable, BufBeg));
Mike Stump1eb44332009-09-09 15:08:12 +0000498
Ted Kremenek1d201972009-03-20 17:54:25 +0000499 // Warn if the PTH file is empty. We still want to create a PTHManager
500 // as the PTH could be used with -include-pth.
501 if (FL->isEmpty())
Daniel Dunbar3574f462009-11-12 02:53:48 +0000502 InvalidPTH(Diags, "PTH file contains no cached source data");
Mike Stump1eb44332009-09-09 15:08:12 +0000503
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000504 // Get the location of the table mapping from persistent ids to the
505 // data needed to reconstruct identifiers.
Ted Kremeneka4bd8eb2009-02-11 23:34:32 +0000506 const unsigned char* IDTableOffset = PrologueOffset + sizeof(uint32_t)*0;
Stephen Hines651f13c2014-04-23 16:59:28 -0700507 const unsigned char *IData =
508 BufBeg + endian::readNext<uint32_t, little, aligned>(IDTableOffset);
Mike Stump1eb44332009-09-09 15:08:12 +0000509
Ted Kremenekcdd8f212009-01-21 07:34:28 +0000510 if (!(IData >= BufBeg && IData < BufEnd)) {
Daniel Dunbar3574f462009-11-12 02:53:48 +0000511 Diags.Report(diag::err_invalid_pth_file) << file;
Ted Kremenek26555b12009-01-28 21:02:43 +0000512 return 0;
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000513 }
Mike Stump1eb44332009-09-09 15:08:12 +0000514
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000515 // Get the location of the hashtable mapping between strings and
516 // persistent IDs.
Ted Kremeneka4bd8eb2009-02-11 23:34:32 +0000517 const unsigned char* StringIdTableOffset = PrologueOffset + sizeof(uint32_t)*1;
Stephen Hines651f13c2014-04-23 16:59:28 -0700518 const unsigned char *StringIdTable =
519 BufBeg + endian::readNext<uint32_t, little, aligned>(StringIdTableOffset);
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000520 if (!(StringIdTable >= BufBeg && StringIdTable < BufEnd)) {
Daniel Dunbar3574f462009-11-12 02:53:48 +0000521 Diags.Report(diag::err_invalid_pth_file) << file;
Ted Kremenek26555b12009-01-28 21:02:43 +0000522 return 0;
Ted Kremenek72b1b152009-01-15 18:47:46 +0000523 }
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000524
Stephen Hines651f13c2014-04-23 16:59:28 -0700525 std::unique_ptr<PTHStringIdLookup> SL(
526 PTHStringIdLookup::Create(StringIdTable, BufBeg));
Mike Stump1eb44332009-09-09 15:08:12 +0000527
Ted Kremenek277faca2009-01-27 00:01:05 +0000528 // Get the location of the spelling cache.
Ted Kremeneka4bd8eb2009-02-11 23:34:32 +0000529 const unsigned char* spellingBaseOffset = PrologueOffset + sizeof(uint32_t)*3;
Stephen Hines651f13c2014-04-23 16:59:28 -0700530 const unsigned char *spellingBase =
531 BufBeg + endian::readNext<uint32_t, little, aligned>(spellingBaseOffset);
Ted Kremenek277faca2009-01-27 00:01:05 +0000532 if (!(spellingBase >= BufBeg && spellingBase < BufEnd)) {
Daniel Dunbar3574f462009-11-12 02:53:48 +0000533 Diags.Report(diag::err_invalid_pth_file) << file;
Ted Kremenek277faca2009-01-27 00:01:05 +0000534 return 0;
535 }
Mike Stump1eb44332009-09-09 15:08:12 +0000536
Ted Kremenek6183e482008-12-03 01:16:39 +0000537 // Get the number of IdentifierInfos and pre-allocate the identifier cache.
Stephen Hines651f13c2014-04-23 16:59:28 -0700538 uint32_t NumIds = endian::readNext<uint32_t, little, aligned>(IData);
Mike Stump1eb44332009-09-09 15:08:12 +0000539
Chris Lattnerfc8f0e12011-04-15 05:22:18 +0000540 // Pre-allocate the persistent ID -> IdentifierInfo* cache. We use calloc()
Ted Kremenek6183e482008-12-03 01:16:39 +0000541 // so that we in the best case only zero out memory once when the OS returns
542 // us new pages.
Ted Kremenekcdd8f212009-01-21 07:34:28 +0000543 IdentifierInfo** PerIDCache = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000544
Ted Kremenekcdd8f212009-01-21 07:34:28 +0000545 if (NumIds) {
Mike Stump1eb44332009-09-09 15:08:12 +0000546 PerIDCache = (IdentifierInfo**)calloc(NumIds, sizeof(*PerIDCache));
Ted Kremenekcdd8f212009-01-21 07:34:28 +0000547 if (!PerIDCache) {
Daniel Dunbar3574f462009-11-12 02:53:48 +0000548 InvalidPTH(Diags, "Could not allocate memory for processing PTH file");
Ted Kremenekcdd8f212009-01-21 07:34:28 +0000549 return 0;
550 }
Ted Kremenek6183e482008-12-03 01:16:39 +0000551 }
Ted Kremenekcdd8f212009-01-21 07:34:28 +0000552
Ted Kremenek68228632009-03-19 22:19:30 +0000553 // Compute the address of the original source file.
554 const unsigned char* originalSourceBase = PrologueOffset + sizeof(uint32_t)*4;
Stephen Hines651f13c2014-04-23 16:59:28 -0700555 unsigned len =
556 endian::readNext<uint16_t, little, unaligned>(originalSourceBase);
Mike Stump1eb44332009-09-09 15:08:12 +0000557 if (!len) originalSourceBase = 0;
558
Ted Kremenek72b1b152009-01-15 18:47:46 +0000559 // Create the new PTHManager.
Stephen Hines651f13c2014-04-23 16:59:28 -0700560 return new PTHManager(File.release(), FL.release(), IData, PerIDCache,
561 SL.release(), NumIds, spellingBase,
562 (const char *)originalSourceBase);
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000563}
Ted Kremenek68228632009-03-19 22:19:30 +0000564
Chris Lattner77ecb3a2009-01-18 02:57:21 +0000565IdentifierInfo* PTHManager::LazilyCreateIdentifierInfo(unsigned PersistentID) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700566 using namespace llvm::support;
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000567 // Look in the PTH file for the string data for the IdentifierInfo object.
Chris Lattner77ecb3a2009-01-18 02:57:21 +0000568 const unsigned char* TableEntry = IdDataTable + sizeof(uint32_t)*PersistentID;
Stephen Hines651f13c2014-04-23 16:59:28 -0700569 const unsigned char *IDData =
570 (const unsigned char *)Buf->getBufferStart() +
571 endian::readNext<uint32_t, little, aligned>(TableEntry);
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000572 assert(IDData < (const unsigned char*)Buf->getBufferEnd());
Mike Stump1eb44332009-09-09 15:08:12 +0000573
Ted Kremenek72b1b152009-01-15 18:47:46 +0000574 // Allocate the object.
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000575 std::pair<IdentifierInfo,const unsigned char*> *Mem =
576 Alloc.Allocate<std::pair<IdentifierInfo,const unsigned char*> >();
Ted Kremenek72b1b152009-01-15 18:47:46 +0000577
578 Mem->second = IDData;
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000579 assert(IDData[0] != '\0');
Ted Kremenekea9c26b2009-01-20 23:28:34 +0000580 IdentifierInfo *II = new ((void*) Mem) IdentifierInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000581
Ted Kremenek72b1b152009-01-15 18:47:46 +0000582 // Store the new IdentifierInfo in the cache.
Chris Lattner77ecb3a2009-01-18 02:57:21 +0000583 PerIDCache[PersistentID] = II;
Daniel Dunbare013d682009-10-18 20:26:12 +0000584 assert(II->getNameStart() && II->getNameStart()[0] != '\0');
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000585 return II;
586}
587
Chris Lattner5f9e2722011-07-23 10:55:15 +0000588IdentifierInfo* PTHManager::get(StringRef Name) {
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000589 PTHStringIdLookup& SL = *((PTHStringIdLookup*)StringIdLookup);
590 // Double check our assumption that the last character isn't '\0'.
David Blaikie681d83d2011-09-22 01:35:49 +0000591 assert(Name.empty() || Name.back() != '\0');
Kovarththanan Rajaratnam700030e2010-03-12 08:23:34 +0000592 PTHStringIdLookup::iterator I = SL.find(std::make_pair(Name.data(),
593 Name.size()));
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000594 if (I == SL.end()) // No identifier found?
595 return 0;
Ted Kremenek72b1b152009-01-15 18:47:46 +0000596
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000597 // Match found. Return the identifier!
598 assert(*I > 0);
599 return GetIdentifierInfo(*I-1);
600}
Ted Kremenek72b1b152009-01-15 18:47:46 +0000601
Chris Lattnerf056d922009-01-17 08:06:50 +0000602PTHLexer *PTHManager::CreateLexer(FileID FID) {
603 const FileEntry *FE = PP->getSourceManager().getFileEntryForID(FID);
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000604 if (!FE)
605 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000606
Stephen Hines651f13c2014-04-23 16:59:28 -0700607 using namespace llvm::support;
608
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000609 // Lookup the FileEntry object in our file lookup data structure. It will
610 // return a variant that indicates whether or not there is an offset within
611 // the PTH file that contains cached tokens.
Ted Kremenekd8c02922009-02-10 22:16:22 +0000612 PTHFileLookup& PFL = *((PTHFileLookup*)FileLookup);
613 PTHFileLookup::iterator I = PFL.find(FE);
Mike Stump1eb44332009-09-09 15:08:12 +0000614
Ted Kremenekd8c02922009-02-10 22:16:22 +0000615 if (I == PFL.end()) // No tokens available?
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000616 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000617
618 const PTHFileData& FileData = *I;
619
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000620 const unsigned char *BufStart = (const unsigned char *)Buf->getBufferStart();
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000621 // Compute the offset of the token data within the buffer.
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000622 const unsigned char* data = BufStart + FileData.getTokenOffset();
Ted Kremenek268ee702008-12-12 18:34:08 +0000623
624 // Get the location of pp-conditional table.
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000625 const unsigned char* ppcond = BufStart + FileData.getPPCondOffset();
Stephen Hines651f13c2014-04-23 16:59:28 -0700626 uint32_t Len = endian::readNext<uint32_t, little, aligned>(ppcond);
Chris Lattner1b5285e2009-01-18 02:10:31 +0000627 if (Len == 0) ppcond = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000628
Ted Kremenek72b1b152009-01-15 18:47:46 +0000629 assert(PP && "No preprocessor set yet!");
Mike Stump1eb44332009-09-09 15:08:12 +0000630 return new PTHLexer(*PP, FID, data, ppcond, *this);
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000631}
Ted Kremenek337edcd2009-02-12 03:26:59 +0000632
633//===----------------------------------------------------------------------===//
634// 'stat' caching.
635//===----------------------------------------------------------------------===//
636
637namespace {
Benjamin Kramer85b45212009-11-28 19:45:26 +0000638class PTHStatData {
Ted Kremenek337edcd2009-02-12 03:26:59 +0000639public:
Rafael Espindola0fda0f72013-08-01 21:42:11 +0000640 const bool HasData;
641 uint64_t Size;
642 time_t ModTime;
643 llvm::sys::fs::UniqueID UniqueID;
644 bool IsDirectory;
Mike Stump1eb44332009-09-09 15:08:12 +0000645
Rafael Espindola0fda0f72013-08-01 21:42:11 +0000646 PTHStatData(uint64_t Size, time_t ModTime, llvm::sys::fs::UniqueID UniqueID,
647 bool IsDirectory)
648 : HasData(true), Size(Size), ModTime(ModTime), UniqueID(UniqueID),
649 IsDirectory(IsDirectory) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000650
Rafael Espindola0fda0f72013-08-01 21:42:11 +0000651 PTHStatData() : HasData(false) {}
Ted Kremenek337edcd2009-02-12 03:26:59 +0000652};
Mike Stump1eb44332009-09-09 15:08:12 +0000653
Benjamin Kramer85b45212009-11-28 19:45:26 +0000654class PTHStatLookupTrait : public PTHFileLookupCommonTrait {
Ted Kremenek337edcd2009-02-12 03:26:59 +0000655public:
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000656 typedef const char* external_key_type; // const char*
Ted Kremenek337edcd2009-02-12 03:26:59 +0000657 typedef PTHStatData data_type;
Mike Stump1eb44332009-09-09 15:08:12 +0000658
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000659 static internal_key_type GetInternalKey(const char *path) {
660 // The key 'kind' doesn't matter here because it is ignored in EqualKey.
661 return std::make_pair((unsigned char) 0x0, path);
662 }
663
664 static bool EqualKey(internal_key_type a, internal_key_type b) {
665 // When doing 'stat' lookups we don't care about the kind of 'a' and 'b',
666 // just the paths.
667 return strcmp(a.second, b.second) == 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000668 }
669
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000670 static data_type ReadData(const internal_key_type& k, const unsigned char* d,
Mike Stump1eb44332009-09-09 15:08:12 +0000671 unsigned) {
672
Ted Kremenekad6ce5c2009-02-13 22:07:44 +0000673 if (k.first /* File or Directory */) {
Rafael Espindola0fda0f72013-08-01 21:42:11 +0000674 bool IsDirectory = true;
675 if (k.first == 0x1 /* File */) {
676 IsDirectory = false;
677 d += 4 * 2; // Skip the first 2 words.
678 }
679
Stephen Hines651f13c2014-04-23 16:59:28 -0700680 using namespace llvm::support;
681
682 uint64_t File = endian::readNext<uint64_t, little, unaligned>(d);
683 uint64_t Device = endian::readNext<uint64_t, little, unaligned>(d);
Rafael Espindola0fda0f72013-08-01 21:42:11 +0000684 llvm::sys::fs::UniqueID UniqueID(File, Device);
Stephen Hines651f13c2014-04-23 16:59:28 -0700685 time_t ModTime = endian::readNext<uint64_t, little, unaligned>(d);
686 uint64_t Size = endian::readNext<uint64_t, little, unaligned>(d);
Rafael Espindola0fda0f72013-08-01 21:42:11 +0000687 return data_type(Size, ModTime, UniqueID, IsDirectory);
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000688 }
Ted Kremenekad6ce5c2009-02-13 22:07:44 +0000689
690 // Negative stat. Don't read anything.
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000691 return data_type();
Ted Kremenek337edcd2009-02-12 03:26:59 +0000692 }
693};
Ted Kremenek337edcd2009-02-12 03:26:59 +0000694
Chris Lattner10e286a2010-11-23 19:19:34 +0000695class PTHStatCache : public FileSystemStatCache {
Ted Kremenek337edcd2009-02-12 03:26:59 +0000696 typedef OnDiskChainedHashTable<PTHStatLookupTrait> CacheTy;
697 CacheTy Cache;
698
Mike Stump1eb44332009-09-09 15:08:12 +0000699public:
Ted Kremenek337edcd2009-02-12 03:26:59 +0000700 PTHStatCache(PTHFileLookup &FL) :
701 Cache(FL.getNumBuckets(), FL.getNumEntries(), FL.getBuckets(),
702 FL.getBase()) {}
703
704 ~PTHStatCache() {}
Mike Stump1eb44332009-09-09 15:08:12 +0000705
Rafael Espindola0fda0f72013-08-01 21:42:11 +0000706 LookupResult getStat(const char *Path, FileData &Data, bool isFile,
Stephen Hines651f13c2014-04-23 16:59:28 -0700707 vfs::File **F, vfs::FileSystem &FS) override {
Ted Kremenek337edcd2009-02-12 03:26:59 +0000708 // Do the lookup for the file's data in the PTH file.
Chris Lattner10e286a2010-11-23 19:19:34 +0000709 CacheTy::iterator I = Cache.find(Path);
Ted Kremenek337edcd2009-02-12 03:26:59 +0000710
711 // If we don't get a hit in the PTH file just forward to 'stat'.
Kovarththanan Rajaratnam700030e2010-03-12 08:23:34 +0000712 if (I == Cache.end())
Stephen Hines651f13c2014-04-23 16:59:28 -0700713 return statChained(Path, Data, isFile, F, FS);
Mike Stump1eb44332009-09-09 15:08:12 +0000714
Rafael Espindola0fda0f72013-08-01 21:42:11 +0000715 const PTHStatData &D = *I;
Mike Stump1eb44332009-09-09 15:08:12 +0000716
Rafael Espindola0fda0f72013-08-01 21:42:11 +0000717 if (!D.HasData)
Chris Lattnerd6f61112010-11-23 20:05:15 +0000718 return CacheMissing;
Ted Kremenekad6ce5c2009-02-13 22:07:44 +0000719
Stephen Hines651f13c2014-04-23 16:59:28 -0700720 Data.Name = Path;
Rafael Espindola0fda0f72013-08-01 21:42:11 +0000721 Data.Size = D.Size;
722 Data.ModTime = D.ModTime;
723 Data.UniqueID = D.UniqueID;
724 Data.IsDirectory = D.IsDirectory;
725 Data.IsNamedPipe = false;
726 Data.InPCH = true;
727
Chris Lattnerd6f61112010-11-23 20:05:15 +0000728 return CacheExists;
Ted Kremenek337edcd2009-02-12 03:26:59 +0000729 }
730};
Ted Kremenekd5785692009-02-23 23:27:54 +0000731} // end anonymous namespace
Ted Kremenek337edcd2009-02-12 03:26:59 +0000732
Chris Lattner10e286a2010-11-23 19:19:34 +0000733FileSystemStatCache *PTHManager::createStatCache() {
Ted Kremenek5f747d12009-02-12 03:45:39 +0000734 return new PTHStatCache(*((PTHFileLookup*) FileLookup));
Ted Kremenek337edcd2009-02-12 03:26:59 +0000735}