blob: fce31c4be2960671c83818a08d468f177fbf9a11 [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"
Chandler Carruth55fc8732012-12-04 09:13:33 +000018#include "clang/Basic/TokenKinds.h"
Daniel Dunbar3574f462009-11-12 02:53:48 +000019#include "clang/Lex/LexDiagnostic.h"
Ted Kremenek0c6a77b2008-12-03 00:38:03 +000020#include "clang/Lex/PTHManager.h"
Ted Kremenek0c6a77b2008-12-03 00:38:03 +000021#include "clang/Lex/Preprocessor.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000022#include "clang/Lex/Token.h"
Daniel Dunbar2596e422009-10-17 23:52:28 +000023#include "llvm/ADT/StringExtras.h"
24#include "llvm/ADT/StringMap.h"
Stephen Hines651f13c2014-04-23 16:59:28 -070025#include "llvm/Support/EndianStream.h"
Chris Lattner6f78c3b2009-01-22 23:50:07 +000026#include "llvm/Support/MemoryBuffer.h"
Stephen Hines6bcf27b2014-05-29 04:14:42 -070027#include "llvm/Support/OnDiskHashTable.h"
Stephen Hines651f13c2014-04-23 16:59:28 -070028#include <memory>
Stephen Hinesc568f1e2014-07-21 00:47:37 -070029#include <system_error>
Ted Kremenek274b2082008-11-12 21:37:15 +000030using namespace clang;
31
Stephen Hinesc568f1e2014-07-21 00:47:37 -070032static const unsigned StoredTokenSize = 1 + 1 + 2 + 4 + 4;
Ted Kremenek268ee702008-12-12 18:34:08 +000033
Ted Kremenek0c6a77b2008-12-03 00:38:03 +000034//===----------------------------------------------------------------------===//
Ted Kremeneke5680f32008-12-23 01:30:52 +000035// PTHLexer methods.
36//===----------------------------------------------------------------------===//
37
Chris Lattnerda9d61c2009-01-18 01:57:14 +000038PTHLexer::PTHLexer(Preprocessor &PP, FileID FID, const unsigned char *D,
Ted Kremenek277faca2009-01-27 00:01:05 +000039 const unsigned char *ppcond, PTHManager &PM)
Stephen Hines6bcf27b2014-05-29 04:14:42 -070040 : PreprocessorLexer(&PP, FID), TokBuf(D), CurPtr(D), LastHashTokPtr(nullptr),
Ted Kremenek277faca2009-01-27 00:01:05 +000041 PPCond(ppcond), CurPPCondPtr(ppcond), PTHMgr(PM) {
Mike Stump1eb44332009-09-09 15:08:12 +000042
Chris Lattner2b2453a2009-01-17 06:22:33 +000043 FileStartLoc = PP.getSourceManager().getLocForStartOfFile(FID);
Ted Kremenek5f074262009-01-09 22:05:30 +000044}
Ted Kremeneke5680f32008-12-23 01:30:52 +000045
Eli Friedmand2f93082013-09-19 00:41:32 +000046bool PTHLexer::Lex(Token& Tok) {
Ted Kremenek866bdf72008-12-23 02:30:15 +000047 //===--------------------------------------==//
48 // Read the raw token data.
49 //===--------------------------------------==//
Stephen Hines651f13c2014-04-23 16:59:28 -070050 using namespace llvm::support;
Mike Stump1eb44332009-09-09 15:08:12 +000051
Ted Kremenek866bdf72008-12-23 02:30:15 +000052 // Shadow CurPtr into an automatic variable.
Mike Stump1eb44332009-09-09 15:08:12 +000053 const unsigned char *CurPtrShadow = CurPtr;
Ted Kremenek866bdf72008-12-23 02:30:15 +000054
Chris Lattner1b5285e2009-01-18 02:10:31 +000055 // Read in the data for the token.
Stephen Hines651f13c2014-04-23 16:59:28 -070056 unsigned Word0 = endian::readNext<uint32_t, little, aligned>(CurPtrShadow);
57 uint32_t IdentifierID =
58 endian::readNext<uint32_t, little, aligned>(CurPtrShadow);
59 uint32_t FileOffset =
60 endian::readNext<uint32_t, little, aligned>(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())
Eli Friedmand2f93082013-09-19 00:41:32 +000094 return PP->HandleIdentifier(Tok);
95
96 return true;
Chris Lattnerd0a69692009-01-21 07:50:06 +000097 }
Mike Stump1eb44332009-09-09 15:08:12 +000098
Ted Kremenek866bdf72008-12-23 02:30:15 +000099 //===--------------------------------------==//
100 // Process the token.
101 //===--------------------------------------==//
Chris Lattner898a0bb2009-01-18 02:34:01 +0000102 if (TKind == tok::eof) {
Ted Kremeneke5680f32008-12-23 01:30:52 +0000103 // Save the end-of-file token.
104 EofToken = Tok;
Mike Stump1eb44332009-09-09 15:08:12 +0000105
Ted Kremenek59d08cb2008-12-23 19:24:24 +0000106 assert(!ParsingPreprocessorDirective);
107 assert(!LexingRawMode);
Mike Stump1eb44332009-09-09 15:08:12 +0000108
Eli Friedmand2f93082013-09-19 00:41:32 +0000109 return LexEndOfFile(Tok);
Ted Kremeneke5680f32008-12-23 01:30:52 +0000110 }
Mike Stump1eb44332009-09-09 15:08:12 +0000111
Chris Lattner898a0bb2009-01-18 02:34:01 +0000112 if (TKind == tok::hash && Tok.isAtStartOfLine()) {
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700113 LastHashTokPtr = CurPtr - StoredTokenSize;
Ted Kremenek59d08cb2008-12-23 19:24:24 +0000114 assert(!LexingRawMode);
115 PP->HandleDirective(Tok);
Mike Stump1eb44332009-09-09 15:08:12 +0000116
Eli Friedmand2f93082013-09-19 00:41:32 +0000117 return false;
Ted Kremenek59d08cb2008-12-23 19:24:24 +0000118 }
Mike Stump1eb44332009-09-09 15:08:12 +0000119
Peter Collingbourne84021552011-02-28 02:37:51 +0000120 if (TKind == tok::eod) {
Ted Kremenek59d08cb2008-12-23 19:24:24 +0000121 assert(ParsingPreprocessorDirective);
Ted Kremeneke5680f32008-12-23 01:30:52 +0000122 ParsingPreprocessorDirective = false;
Eli Friedmand2f93082013-09-19 00:41:32 +0000123 return true;
Ted Kremeneke5680f32008-12-23 01:30:52 +0000124 }
Ted Kremeneke5680f32008-12-23 01:30:52 +0000125
Ted Kremenek59d08cb2008-12-23 19:24:24 +0000126 MIOpt.ReadToken();
Eli Friedmand2f93082013-09-19 00:41:32 +0000127 return true;
Ted Kremeneke5680f32008-12-23 01:30:52 +0000128}
129
Ted Kremenek94e3d1f2010-07-27 02:59:02 +0000130bool PTHLexer::LexEndOfFile(Token &Result) {
131 // If we hit the end of the file while parsing a preprocessor directive,
132 // end the preprocessor directive first. The next token returned will
133 // then be the end of file.
134 if (ParsingPreprocessorDirective) {
135 ParsingPreprocessorDirective = false; // Done parsing the "line".
136 return true; // Have a token.
137 }
138
139 assert(!LexingRawMode);
140
141 // If we are in a #if directive, emit an error.
142 while (!ConditionalStack.empty()) {
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000143 if (PP->getCodeCompletionFileLoc() != FileStartLoc)
Douglas Gregor2d474ba2010-08-12 17:04:55 +0000144 PP->Diag(ConditionalStack.back().IfLoc,
145 diag::err_pp_unterminated_conditional);
Ted Kremenek94e3d1f2010-07-27 02:59:02 +0000146 ConditionalStack.pop_back();
147 }
148
149 // Finally, let the preprocessor handle this.
150 return PP->HandleEndOfFile(Result);
151}
152
Ted Kremeneke5680f32008-12-23 01:30:52 +0000153// FIXME: We can just grab the last token instead of storing a copy
154// into EofToken.
Ted Kremenek59d08cb2008-12-23 19:24:24 +0000155void PTHLexer::getEOF(Token& Tok) {
Ted Kremenekdefb7092009-01-09 00:36:11 +0000156 assert(EofToken.is(tok::eof));
Ted Kremeneke5680f32008-12-23 01:30:52 +0000157 Tok = EofToken;
158}
159
160void PTHLexer::DiscardToEndOfLine() {
161 assert(ParsingPreprocessorDirective && ParsingFilename == false &&
162 "Must be in a preprocessing directive!");
163
164 // We assume that if the preprocessor wishes to discard to the end of
165 // the line that it also means to end the current preprocessor directive.
166 ParsingPreprocessorDirective = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000167
Ted Kremeneke5680f32008-12-23 01:30:52 +0000168 // Skip tokens by only peeking at their token kind and the flags.
169 // We don't need to actually reconstruct full tokens from the token buffer.
170 // This saves some copies and it also reduces IdentifierInfo* lookup.
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000171 const unsigned char* p = CurPtr;
Ted Kremeneke5680f32008-12-23 01:30:52 +0000172 while (1) {
173 // Read the token kind. Are we at the end of the file?
174 tok::TokenKind x = (tok::TokenKind) (uint8_t) *p;
175 if (x == tok::eof) break;
Mike Stump1eb44332009-09-09 15:08:12 +0000176
Ted Kremeneke5680f32008-12-23 01:30:52 +0000177 // Read the token flags. Are we at the start of the next line?
178 Token::TokenFlags y = (Token::TokenFlags) (uint8_t) p[1];
179 if (y & Token::StartOfLine) break;
180
181 // Skip to the next token.
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700182 p += StoredTokenSize;
Ted Kremeneke5680f32008-12-23 01:30:52 +0000183 }
Mike Stump1eb44332009-09-09 15:08:12 +0000184
Ted Kremeneke5680f32008-12-23 01:30:52 +0000185 CurPtr = p;
186}
187
Ted Kremenek268ee702008-12-12 18:34:08 +0000188/// SkipBlock - Used by Preprocessor to skip the current conditional block.
189bool PTHLexer::SkipBlock() {
Stephen Hines651f13c2014-04-23 16:59:28 -0700190 using namespace llvm::support;
Ted Kremenek268ee702008-12-12 18:34:08 +0000191 assert(CurPPCondPtr && "No cached PP conditional information.");
192 assert(LastHashTokPtr && "No known '#' token.");
Mike Stump1eb44332009-09-09 15:08:12 +0000193
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700194 const unsigned char *HashEntryI = nullptr;
Ted Kremenek268ee702008-12-12 18:34:08 +0000195 uint32_t TableIdx;
Mike Stump1eb44332009-09-09 15:08:12 +0000196
Ted Kremenek268ee702008-12-12 18:34:08 +0000197 do {
Ted Kremenek41a26602008-12-12 22:05:38 +0000198 // Read the token offset from the side-table.
Stephen Hines651f13c2014-04-23 16:59:28 -0700199 uint32_t Offset = endian::readNext<uint32_t, little, aligned>(CurPPCondPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000200
201 // Read the target table index from the side-table.
Stephen Hines651f13c2014-04-23 16:59:28 -0700202 TableIdx = endian::readNext<uint32_t, little, aligned>(CurPPCondPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000203
Ted Kremenek41a26602008-12-12 22:05:38 +0000204 // Compute the actual memory address of the '#' token data for this entry.
205 HashEntryI = TokBuf + Offset;
206
207 // Optmization: "Sibling jumping". #if...#else...#endif blocks can
208 // contain nested blocks. In the side-table we can jump over these
209 // nested blocks instead of doing a linear search if the next "sibling"
210 // entry is not at a location greater than LastHashTokPtr.
211 if (HashEntryI < LastHashTokPtr && TableIdx) {
212 // In the side-table we are still at an entry for a '#' token that
213 // is earlier than the last one we saw. Check if the location we would
214 // stride gets us closer.
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000215 const unsigned char* NextPPCondPtr =
216 PPCond + TableIdx*(sizeof(uint32_t)*2);
Ted Kremenek41a26602008-12-12 22:05:38 +0000217 assert(NextPPCondPtr >= CurPPCondPtr);
218 // Read where we should jump to.
Stephen Hines651f13c2014-04-23 16:59:28 -0700219 const unsigned char *HashEntryJ =
220 TokBuf + endian::readNext<uint32_t, little, aligned>(NextPPCondPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000221
Ted Kremenek41a26602008-12-12 22:05:38 +0000222 if (HashEntryJ <= LastHashTokPtr) {
223 // Jump directly to the next entry in the side table.
224 HashEntryI = HashEntryJ;
Stephen Hines651f13c2014-04-23 16:59:28 -0700225 TableIdx = endian::readNext<uint32_t, little, aligned>(NextPPCondPtr);
Ted Kremenek41a26602008-12-12 22:05:38 +0000226 CurPPCondPtr = NextPPCondPtr;
227 }
228 }
Ted Kremenek268ee702008-12-12 18:34:08 +0000229 }
Mike Stump1eb44332009-09-09 15:08:12 +0000230 while (HashEntryI < LastHashTokPtr);
Ted Kremenek41a26602008-12-12 22:05:38 +0000231 assert(HashEntryI == LastHashTokPtr && "No PP-cond entry found for '#'");
Ted Kremenek268ee702008-12-12 18:34:08 +0000232 assert(TableIdx && "No jumping from #endifs.");
Mike Stump1eb44332009-09-09 15:08:12 +0000233
Ted Kremenek268ee702008-12-12 18:34:08 +0000234 // Update our side-table iterator.
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000235 const unsigned char* NextPPCondPtr = PPCond + TableIdx*(sizeof(uint32_t)*2);
Ted Kremenek268ee702008-12-12 18:34:08 +0000236 assert(NextPPCondPtr >= CurPPCondPtr);
237 CurPPCondPtr = NextPPCondPtr;
Mike Stump1eb44332009-09-09 15:08:12 +0000238
Ted Kremenek268ee702008-12-12 18:34:08 +0000239 // Read where we should jump to.
Stephen Hines651f13c2014-04-23 16:59:28 -0700240 HashEntryI =
241 TokBuf + endian::readNext<uint32_t, little, aligned>(NextPPCondPtr);
242 uint32_t NextIdx = endian::readNext<uint32_t, little, aligned>(NextPPCondPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000243
Ted Kremenek268ee702008-12-12 18:34:08 +0000244 // By construction NextIdx will be zero if this is a #endif. This is useful
245 // to know to obviate lexing another token.
246 bool isEndif = NextIdx == 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000247
Ted Kremenek268ee702008-12-12 18:34:08 +0000248 // This case can occur when we see something like this:
249 //
250 // #if ...
251 // /* a comment or nothing */
252 // #elif
253 //
254 // If we are skipping the first #if block it will be the case that CurPtr
255 // already points 'elif'. Just return.
Mike Stump1eb44332009-09-09 15:08:12 +0000256
Ted Kremenek41a26602008-12-12 22:05:38 +0000257 if (CurPtr > HashEntryI) {
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700258 assert(CurPtr == HashEntryI + StoredTokenSize);
Ted Kremenek268ee702008-12-12 18:34:08 +0000259 // Did we reach a #endif? If so, go ahead and consume that token as well.
260 if (isEndif)
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700261 CurPtr += StoredTokenSize * 2;
Ted Kremenek268ee702008-12-12 18:34:08 +0000262 else
Ted Kremenek41a26602008-12-12 22:05:38 +0000263 LastHashTokPtr = HashEntryI;
Mike Stump1eb44332009-09-09 15:08:12 +0000264
Ted Kremenek268ee702008-12-12 18:34:08 +0000265 return isEndif;
266 }
267
268 // Otherwise, we need to advance. Update CurPtr to point to the '#' token.
Ted Kremenek41a26602008-12-12 22:05:38 +0000269 CurPtr = HashEntryI;
Mike Stump1eb44332009-09-09 15:08:12 +0000270
Ted Kremenek268ee702008-12-12 18:34:08 +0000271 // Update the location of the last observed '#'. This is useful if we
272 // are skipping multiple blocks.
273 LastHashTokPtr = CurPtr;
Ted Kremenek268ee702008-12-12 18:34:08 +0000274
Ted Kremeneke5680f32008-12-23 01:30:52 +0000275 // Skip the '#' token.
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000276 assert(((tok::TokenKind)*CurPtr) == tok::hash);
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700277 CurPtr += StoredTokenSize;
Mike Stump1eb44332009-09-09 15:08:12 +0000278
Ted Kremenek268ee702008-12-12 18:34:08 +0000279 // Did we reach a #endif? If so, go ahead and consume that token as well.
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700280 if (isEndif) {
281 CurPtr += StoredTokenSize * 2;
282 }
Ted Kremenek268ee702008-12-12 18:34:08 +0000283
284 return isEndif;
285}
286
Ted Kremenek30a12ec2008-12-17 23:36:32 +0000287SourceLocation PTHLexer::getSourceLocation() {
Chris Lattner1b5285e2009-01-18 02:10:31 +0000288 // getSourceLocation is not on the hot path. It is used to get the location
289 // of the next token when transitioning back to this lexer when done
Ted Kremenek30a12ec2008-12-17 23:36:32 +0000290 // handling a #included file. Just read the necessary data from the token
291 // data buffer to construct the SourceLocation object.
292 // NOTE: This is a virtual function; hence it is defined out-of-line.
Stephen Hines651f13c2014-04-23 16:59:28 -0700293 using namespace llvm::support;
294
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700295 const unsigned char *OffsetPtr = CurPtr + (StoredTokenSize - 4);
Stephen Hines651f13c2014-04-23 16:59:28 -0700296 uint32_t Offset = endian::readNext<uint32_t, little, aligned>(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;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700323 typedef unsigned hash_value_type;
324 typedef unsigned offset_type;
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000325
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700326 static hash_value_type ComputeHash(internal_key_type x) {
Daniel Dunbar2596e422009-10-17 23:52:28 +0000327 return llvm::HashString(x.second);
Ted Kremenekd8c02922009-02-10 22:16:22 +0000328 }
Mike Stump1eb44332009-09-09 15:08:12 +0000329
Ted Kremenekd8c02922009-02-10 22:16:22 +0000330 static std::pair<unsigned, unsigned>
331 ReadKeyDataLength(const unsigned char*& d) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700332 using namespace llvm::support;
333 unsigned keyLen =
334 (unsigned)endian::readNext<uint16_t, little, unaligned>(d);
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000335 unsigned dataLen = (unsigned) *(d++);
336 return std::make_pair(keyLen, dataLen);
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000337 }
Mike Stump1eb44332009-09-09 15:08:12 +0000338
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000339 static internal_key_type ReadKey(const unsigned char* d, unsigned) {
340 unsigned char k = *(d++); // Read the entry kind.
341 return std::make_pair(k, (const char*) d);
Ted Kremenekd8c02922009-02-10 22:16:22 +0000342 }
Ted Kremenek337edcd2009-02-12 03:26:59 +0000343};
Mike Stump1eb44332009-09-09 15:08:12 +0000344
Benjamin Kramer85b45212009-11-28 19:45:26 +0000345class PTHFileLookupTrait : public PTHFileLookupCommonTrait {
Ted Kremenek337edcd2009-02-12 03:26:59 +0000346public:
347 typedef const FileEntry* external_key_type;
348 typedef PTHFileData data_type;
Mike Stump1eb44332009-09-09 15:08:12 +0000349
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000350 static internal_key_type GetInternalKey(const FileEntry* FE) {
351 return std::make_pair((unsigned char) 0x1, FE->getName());
Ted Kremenek337edcd2009-02-12 03:26:59 +0000352 }
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000353
354 static bool EqualKey(internal_key_type a, internal_key_type b) {
355 return a.first == b.first && strcmp(a.second, b.second) == 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000356 }
357
358 static PTHFileData ReadData(const internal_key_type& k,
359 const unsigned char* d, unsigned) {
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000360 assert(k.first == 0x1 && "Only file lookups can match!");
Stephen Hines651f13c2014-04-23 16:59:28 -0700361 using namespace llvm::support;
362 uint32_t x = endian::readNext<uint32_t, little, unaligned>(d);
363 uint32_t y = endian::readNext<uint32_t, little, unaligned>(d);
Mike Stump1eb44332009-09-09 15:08:12 +0000364 return PTHFileData(x, y);
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000365 }
366};
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000367
Benjamin Kramer85b45212009-11-28 19:45:26 +0000368class PTHStringLookupTrait {
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000369public:
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700370 typedef uint32_t data_type;
371 typedef const std::pair<const char*, unsigned> external_key_type;
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000372 typedef external_key_type internal_key_type;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700373 typedef uint32_t hash_value_type;
374 typedef unsigned offset_type;
Mike Stump1eb44332009-09-09 15:08:12 +0000375
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000376 static bool EqualKey(const internal_key_type& a,
377 const internal_key_type& b) {
378 return (a.second == b.second) ? memcmp(a.first, b.first, a.second) == 0
379 : false;
380 }
Mike Stump1eb44332009-09-09 15:08:12 +0000381
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700382 static hash_value_type ComputeHash(const internal_key_type& a) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000383 return llvm::HashString(StringRef(a.first, a.second));
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000384 }
Mike Stump1eb44332009-09-09 15:08:12 +0000385
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000386 // This hopefully will just get inlined and removed by the optimizer.
387 static const internal_key_type&
388 GetInternalKey(const external_key_type& x) { return x; }
Mike Stump1eb44332009-09-09 15:08:12 +0000389
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000390 static std::pair<unsigned, unsigned>
391 ReadKeyDataLength(const unsigned char*& d) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700392 using namespace llvm::support;
393 return std::make_pair(
394 (unsigned)endian::readNext<uint16_t, little, unaligned>(d),
395 sizeof(uint32_t));
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000396 }
Mike Stump1eb44332009-09-09 15:08:12 +0000397
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000398 static std::pair<const char*, unsigned>
399 ReadKey(const unsigned char* d, unsigned n) {
400 assert(n >= 2 && d[n-1] == '\0');
401 return std::make_pair((const char*) d, n-1);
402 }
Mike Stump1eb44332009-09-09 15:08:12 +0000403
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000404 static uint32_t ReadData(const internal_key_type& k, const unsigned char* d,
405 unsigned) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700406 using namespace llvm::support;
407 return endian::readNext<uint32_t, little, unaligned>(d);
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000408 }
409};
Mike Stump1eb44332009-09-09 15:08:12 +0000410
411} // end anonymous namespace
Ted Kremenekd8c02922009-02-10 22:16:22 +0000412
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700413typedef llvm::OnDiskChainedHashTable<PTHFileLookupTrait> PTHFileLookup;
414typedef llvm::OnDiskChainedHashTable<PTHStringLookupTrait> PTHStringIdLookup;
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000415
416//===----------------------------------------------------------------------===//
417// PTHManager methods.
418//===----------------------------------------------------------------------===//
419
420PTHManager::PTHManager(const llvm::MemoryBuffer* buf, void* fileLookup,
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000421 const unsigned char* idDataTable,
Mike Stump1eb44332009-09-09 15:08:12 +0000422 IdentifierInfo** perIDCache,
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000423 void* stringIdLookup, unsigned numIds,
Ted Kremenek68228632009-03-19 22:19:30 +0000424 const unsigned char* spellingBase,
425 const char* originalSourceFile)
Ted Kremenek6183e482008-12-03 01:16:39 +0000426: Buf(buf), PerIDCache(perIDCache), FileLookup(fileLookup),
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000427 IdDataTable(idDataTable), StringIdLookup(stringIdLookup),
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700428 NumIds(numIds), PP(nullptr), SpellingBase(spellingBase),
Ted Kremenek68228632009-03-19 22:19:30 +0000429 OriginalSourceFile(originalSourceFile) {}
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000430
431PTHManager::~PTHManager() {
432 delete Buf;
433 delete (PTHFileLookup*) FileLookup;
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000434 delete (PTHStringIdLookup*) StringIdLookup;
Ted Kremenek0e50b6e2008-12-04 22:47:11 +0000435 free(PerIDCache);
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000436}
437
David Blaikied6471f72011-09-25 23:23:43 +0000438static void InvalidPTH(DiagnosticsEngine &Diags, const char *Msg) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700439 Diags.Report(Diags.getCustomDiagID(DiagnosticsEngine::Error, "%0")) << Msg;
Ted Kremenek26555b12009-01-28 21:02:43 +0000440}
441
David Blaikied6471f72011-09-25 23:23:43 +0000442PTHManager *PTHManager::Create(const std::string &file,
443 DiagnosticsEngine &Diags) {
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000444 // Memory map the PTH file.
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700445 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> FileOrErr =
446 llvm::MemoryBuffer::getFile(file);
Mike Stump1eb44332009-09-09 15:08:12 +0000447
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700448 if (!FileOrErr) {
Michael J. Spencer3a321e22010-12-09 17:36:38 +0000449 // FIXME: Add ec.message() to this diag.
Daniel Dunbar3574f462009-11-12 02:53:48 +0000450 Diags.Report(diag::err_invalid_pth_file) << file;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700451 return nullptr;
Ted Kremenek8a6aec62009-01-28 20:49:33 +0000452 }
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700453 std::unique_ptr<llvm::MemoryBuffer> File = std::move(FileOrErr.get());
Mike Stump1eb44332009-09-09 15:08:12 +0000454
Stephen Hines651f13c2014-04-23 16:59:28 -0700455 using namespace llvm::support;
456
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000457 // Get the buffer ranges and check if there are at least three 32-bit
458 // words at the end of the file.
Roman Divacky31ba6132012-09-06 15:59:27 +0000459 const unsigned char *BufBeg = (const unsigned char*)File->getBufferStart();
460 const unsigned char *BufEnd = (const unsigned char*)File->getBufferEnd();
Ted Kremeneke1b64982009-01-26 21:43:14 +0000461
462 // Check the prologue of the file.
Richard Smithc141b512012-08-17 03:55:43 +0000463 if ((BufEnd - BufBeg) < (signed)(sizeof("cfe-pth") + 4 + 4) ||
464 memcmp(BufBeg, "cfe-pth", sizeof("cfe-pth")) != 0) {
Daniel Dunbar3574f462009-11-12 02:53:48 +0000465 Diags.Report(diag::err_invalid_pth_file) << file;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700466 return nullptr;
Ted Kremenek26555b12009-01-28 21:02:43 +0000467 }
Mike Stump1eb44332009-09-09 15:08:12 +0000468
Ted Kremenek67d15052009-01-26 21:50:21 +0000469 // Read the PTH version.
Richard Smithc141b512012-08-17 03:55:43 +0000470 const unsigned char *p = BufBeg + (sizeof("cfe-pth"));
Stephen Hines651f13c2014-04-23 16:59:28 -0700471 unsigned Version = endian::readNext<uint32_t, little, aligned>(p);
Mike Stump1eb44332009-09-09 15:08:12 +0000472
Daniel Dunbar3574f462009-11-12 02:53:48 +0000473 if (Version < PTHManager::Version) {
474 InvalidPTH(Diags,
Mike Stump1eb44332009-09-09 15:08:12 +0000475 Version < PTHManager::Version
Ted Kremenek26555b12009-01-28 21:02:43 +0000476 ? "PTH file uses an older PTH format that is no longer supported"
477 : "PTH file uses a newer PTH format that cannot be read");
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700478 return nullptr;
Ted Kremenek26555b12009-01-28 21:02:43 +0000479 }
Ted Kremenek67d15052009-01-26 21:50:21 +0000480
Mike Stump1eb44332009-09-09 15:08:12 +0000481 // Compute the address of the index table at the end of the PTH file.
Ted Kremeneka4bd8eb2009-02-11 23:34:32 +0000482 const unsigned char *PrologueOffset = p;
Mike Stump1eb44332009-09-09 15:08:12 +0000483
Ted Kremeneka4bd8eb2009-02-11 23:34:32 +0000484 if (PrologueOffset >= BufEnd) {
Daniel Dunbar3574f462009-11-12 02:53:48 +0000485 Diags.Report(diag::err_invalid_pth_file) << file;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700486 return nullptr;
Ted Kremenek26555b12009-01-28 21:02:43 +0000487 }
Mike Stump1eb44332009-09-09 15:08:12 +0000488
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000489 // Construct the file lookup table. This will be used for mapping from
490 // FileEntry*'s to cached tokens.
Ted Kremeneka4bd8eb2009-02-11 23:34:32 +0000491 const unsigned char* FileTableOffset = PrologueOffset + sizeof(uint32_t)*2;
Stephen Hines651f13c2014-04-23 16:59:28 -0700492 const unsigned char *FileTable =
493 BufBeg + endian::readNext<uint32_t, little, aligned>(FileTableOffset);
Mike Stump1eb44332009-09-09 15:08:12 +0000494
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000495 if (!(FileTable > BufBeg && FileTable < BufEnd)) {
Daniel Dunbar3574f462009-11-12 02:53:48 +0000496 Diags.Report(diag::err_invalid_pth_file) << file;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700497 return nullptr; // FIXME: Proper error diagnostic?
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000498 }
Mike Stump1eb44332009-09-09 15:08:12 +0000499
Stephen Hines651f13c2014-04-23 16:59:28 -0700500 std::unique_ptr<PTHFileLookup> FL(PTHFileLookup::Create(FileTable, BufBeg));
Mike Stump1eb44332009-09-09 15:08:12 +0000501
Ted Kremenek1d201972009-03-20 17:54:25 +0000502 // Warn if the PTH file is empty. We still want to create a PTHManager
503 // as the PTH could be used with -include-pth.
504 if (FL->isEmpty())
Daniel Dunbar3574f462009-11-12 02:53:48 +0000505 InvalidPTH(Diags, "PTH file contains no cached source data");
Mike Stump1eb44332009-09-09 15:08:12 +0000506
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000507 // Get the location of the table mapping from persistent ids to the
508 // data needed to reconstruct identifiers.
Ted Kremeneka4bd8eb2009-02-11 23:34:32 +0000509 const unsigned char* IDTableOffset = PrologueOffset + sizeof(uint32_t)*0;
Stephen Hines651f13c2014-04-23 16:59:28 -0700510 const unsigned char *IData =
511 BufBeg + endian::readNext<uint32_t, little, aligned>(IDTableOffset);
Mike Stump1eb44332009-09-09 15:08:12 +0000512
Ted Kremenekcdd8f212009-01-21 07:34:28 +0000513 if (!(IData >= BufBeg && IData < BufEnd)) {
Daniel Dunbar3574f462009-11-12 02:53:48 +0000514 Diags.Report(diag::err_invalid_pth_file) << file;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700515 return nullptr;
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000516 }
Mike Stump1eb44332009-09-09 15:08:12 +0000517
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000518 // Get the location of the hashtable mapping between strings and
519 // persistent IDs.
Ted Kremeneka4bd8eb2009-02-11 23:34:32 +0000520 const unsigned char* StringIdTableOffset = PrologueOffset + sizeof(uint32_t)*1;
Stephen Hines651f13c2014-04-23 16:59:28 -0700521 const unsigned char *StringIdTable =
522 BufBeg + endian::readNext<uint32_t, little, aligned>(StringIdTableOffset);
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000523 if (!(StringIdTable >= BufBeg && StringIdTable < BufEnd)) {
Daniel Dunbar3574f462009-11-12 02:53:48 +0000524 Diags.Report(diag::err_invalid_pth_file) << file;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700525 return nullptr;
Ted Kremenek72b1b152009-01-15 18:47:46 +0000526 }
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000527
Stephen Hines651f13c2014-04-23 16:59:28 -0700528 std::unique_ptr<PTHStringIdLookup> SL(
529 PTHStringIdLookup::Create(StringIdTable, BufBeg));
Mike Stump1eb44332009-09-09 15:08:12 +0000530
Ted Kremenek277faca2009-01-27 00:01:05 +0000531 // Get the location of the spelling cache.
Ted Kremeneka4bd8eb2009-02-11 23:34:32 +0000532 const unsigned char* spellingBaseOffset = PrologueOffset + sizeof(uint32_t)*3;
Stephen Hines651f13c2014-04-23 16:59:28 -0700533 const unsigned char *spellingBase =
534 BufBeg + endian::readNext<uint32_t, little, aligned>(spellingBaseOffset);
Ted Kremenek277faca2009-01-27 00:01:05 +0000535 if (!(spellingBase >= BufBeg && spellingBase < BufEnd)) {
Daniel Dunbar3574f462009-11-12 02:53:48 +0000536 Diags.Report(diag::err_invalid_pth_file) << file;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700537 return nullptr;
Ted Kremenek277faca2009-01-27 00:01:05 +0000538 }
Mike Stump1eb44332009-09-09 15:08:12 +0000539
Ted Kremenek6183e482008-12-03 01:16:39 +0000540 // Get the number of IdentifierInfos and pre-allocate the identifier cache.
Stephen Hines651f13c2014-04-23 16:59:28 -0700541 uint32_t NumIds = endian::readNext<uint32_t, little, aligned>(IData);
Mike Stump1eb44332009-09-09 15:08:12 +0000542
Chris Lattnerfc8f0e12011-04-15 05:22:18 +0000543 // Pre-allocate the persistent ID -> IdentifierInfo* cache. We use calloc()
Ted Kremenek6183e482008-12-03 01:16:39 +0000544 // so that we in the best case only zero out memory once when the OS returns
545 // us new pages.
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700546 IdentifierInfo **PerIDCache = nullptr;
Mike Stump1eb44332009-09-09 15:08:12 +0000547
Ted Kremenekcdd8f212009-01-21 07:34:28 +0000548 if (NumIds) {
Mike Stump1eb44332009-09-09 15:08:12 +0000549 PerIDCache = (IdentifierInfo**)calloc(NumIds, sizeof(*PerIDCache));
Ted Kremenekcdd8f212009-01-21 07:34:28 +0000550 if (!PerIDCache) {
Daniel Dunbar3574f462009-11-12 02:53:48 +0000551 InvalidPTH(Diags, "Could not allocate memory for processing PTH file");
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700552 return nullptr;
Ted Kremenekcdd8f212009-01-21 07:34:28 +0000553 }
Ted Kremenek6183e482008-12-03 01:16:39 +0000554 }
Ted Kremenekcdd8f212009-01-21 07:34:28 +0000555
Ted Kremenek68228632009-03-19 22:19:30 +0000556 // Compute the address of the original source file.
557 const unsigned char* originalSourceBase = PrologueOffset + sizeof(uint32_t)*4;
Stephen Hines651f13c2014-04-23 16:59:28 -0700558 unsigned len =
559 endian::readNext<uint16_t, little, unaligned>(originalSourceBase);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700560 if (!len) originalSourceBase = nullptr;
Mike Stump1eb44332009-09-09 15:08:12 +0000561
Ted Kremenek72b1b152009-01-15 18:47:46 +0000562 // Create the new PTHManager.
Stephen Hines651f13c2014-04-23 16:59:28 -0700563 return new PTHManager(File.release(), FL.release(), IData, PerIDCache,
564 SL.release(), NumIds, spellingBase,
565 (const char *)originalSourceBase);
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000566}
Ted Kremenek68228632009-03-19 22:19:30 +0000567
Chris Lattner77ecb3a2009-01-18 02:57:21 +0000568IdentifierInfo* PTHManager::LazilyCreateIdentifierInfo(unsigned PersistentID) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700569 using namespace llvm::support;
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000570 // Look in the PTH file for the string data for the IdentifierInfo object.
Chris Lattner77ecb3a2009-01-18 02:57:21 +0000571 const unsigned char* TableEntry = IdDataTable + sizeof(uint32_t)*PersistentID;
Stephen Hines651f13c2014-04-23 16:59:28 -0700572 const unsigned char *IDData =
573 (const unsigned char *)Buf->getBufferStart() +
574 endian::readNext<uint32_t, little, aligned>(TableEntry);
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000575 assert(IDData < (const unsigned char*)Buf->getBufferEnd());
Mike Stump1eb44332009-09-09 15:08:12 +0000576
Ted Kremenek72b1b152009-01-15 18:47:46 +0000577 // Allocate the object.
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000578 std::pair<IdentifierInfo,const unsigned char*> *Mem =
579 Alloc.Allocate<std::pair<IdentifierInfo,const unsigned char*> >();
Ted Kremenek72b1b152009-01-15 18:47:46 +0000580
581 Mem->second = IDData;
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000582 assert(IDData[0] != '\0');
Ted Kremenekea9c26b2009-01-20 23:28:34 +0000583 IdentifierInfo *II = new ((void*) Mem) IdentifierInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000584
Ted Kremenek72b1b152009-01-15 18:47:46 +0000585 // Store the new IdentifierInfo in the cache.
Chris Lattner77ecb3a2009-01-18 02:57:21 +0000586 PerIDCache[PersistentID] = II;
Daniel Dunbare013d682009-10-18 20:26:12 +0000587 assert(II->getNameStart() && II->getNameStart()[0] != '\0');
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000588 return II;
589}
590
Chris Lattner5f9e2722011-07-23 10:55:15 +0000591IdentifierInfo* PTHManager::get(StringRef Name) {
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000592 PTHStringIdLookup& SL = *((PTHStringIdLookup*)StringIdLookup);
593 // Double check our assumption that the last character isn't '\0'.
David Blaikie681d83d2011-09-22 01:35:49 +0000594 assert(Name.empty() || Name.back() != '\0');
Kovarththanan Rajaratnam700030e2010-03-12 08:23:34 +0000595 PTHStringIdLookup::iterator I = SL.find(std::make_pair(Name.data(),
596 Name.size()));
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000597 if (I == SL.end()) // No identifier found?
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700598 return nullptr;
Ted Kremenek72b1b152009-01-15 18:47:46 +0000599
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000600 // Match found. Return the identifier!
601 assert(*I > 0);
602 return GetIdentifierInfo(*I-1);
603}
Ted Kremenek72b1b152009-01-15 18:47:46 +0000604
Chris Lattnerf056d922009-01-17 08:06:50 +0000605PTHLexer *PTHManager::CreateLexer(FileID FID) {
606 const FileEntry *FE = PP->getSourceManager().getFileEntryForID(FID);
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000607 if (!FE)
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700608 return nullptr;
Mike Stump1eb44332009-09-09 15:08:12 +0000609
Stephen Hines651f13c2014-04-23 16:59:28 -0700610 using namespace llvm::support;
611
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000612 // Lookup the FileEntry object in our file lookup data structure. It will
613 // return a variant that indicates whether or not there is an offset within
614 // the PTH file that contains cached tokens.
Ted Kremenekd8c02922009-02-10 22:16:22 +0000615 PTHFileLookup& PFL = *((PTHFileLookup*)FileLookup);
616 PTHFileLookup::iterator I = PFL.find(FE);
Mike Stump1eb44332009-09-09 15:08:12 +0000617
Ted Kremenekd8c02922009-02-10 22:16:22 +0000618 if (I == PFL.end()) // No tokens available?
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700619 return nullptr;
Mike Stump1eb44332009-09-09 15:08:12 +0000620
621 const PTHFileData& FileData = *I;
622
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000623 const unsigned char *BufStart = (const unsigned char *)Buf->getBufferStart();
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000624 // Compute the offset of the token data within the buffer.
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000625 const unsigned char* data = BufStart + FileData.getTokenOffset();
Ted Kremenek268ee702008-12-12 18:34:08 +0000626
627 // Get the location of pp-conditional table.
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000628 const unsigned char* ppcond = BufStart + FileData.getPPCondOffset();
Stephen Hines651f13c2014-04-23 16:59:28 -0700629 uint32_t Len = endian::readNext<uint32_t, little, aligned>(ppcond);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700630 if (Len == 0) ppcond = nullptr;
Mike Stump1eb44332009-09-09 15:08:12 +0000631
Ted Kremenek72b1b152009-01-15 18:47:46 +0000632 assert(PP && "No preprocessor set yet!");
Mike Stump1eb44332009-09-09 15:08:12 +0000633 return new PTHLexer(*PP, FID, data, ppcond, *this);
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000634}
Ted Kremenek337edcd2009-02-12 03:26:59 +0000635
636//===----------------------------------------------------------------------===//
637// 'stat' caching.
638//===----------------------------------------------------------------------===//
639
640namespace {
Benjamin Kramer85b45212009-11-28 19:45:26 +0000641class PTHStatData {
Ted Kremenek337edcd2009-02-12 03:26:59 +0000642public:
Rafael Espindola0fda0f72013-08-01 21:42:11 +0000643 const bool HasData;
644 uint64_t Size;
645 time_t ModTime;
646 llvm::sys::fs::UniqueID UniqueID;
647 bool IsDirectory;
Mike Stump1eb44332009-09-09 15:08:12 +0000648
Rafael Espindola0fda0f72013-08-01 21:42:11 +0000649 PTHStatData(uint64_t Size, time_t ModTime, llvm::sys::fs::UniqueID UniqueID,
650 bool IsDirectory)
651 : HasData(true), Size(Size), ModTime(ModTime), UniqueID(UniqueID),
652 IsDirectory(IsDirectory) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000653
Rafael Espindola0fda0f72013-08-01 21:42:11 +0000654 PTHStatData() : HasData(false) {}
Ted Kremenek337edcd2009-02-12 03:26:59 +0000655};
Mike Stump1eb44332009-09-09 15:08:12 +0000656
Benjamin Kramer85b45212009-11-28 19:45:26 +0000657class PTHStatLookupTrait : public PTHFileLookupCommonTrait {
Ted Kremenek337edcd2009-02-12 03:26:59 +0000658public:
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000659 typedef const char* external_key_type; // const char*
Ted Kremenek337edcd2009-02-12 03:26:59 +0000660 typedef PTHStatData data_type;
Mike Stump1eb44332009-09-09 15:08:12 +0000661
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000662 static internal_key_type GetInternalKey(const char *path) {
663 // The key 'kind' doesn't matter here because it is ignored in EqualKey.
664 return std::make_pair((unsigned char) 0x0, path);
665 }
666
667 static bool EqualKey(internal_key_type a, internal_key_type b) {
668 // When doing 'stat' lookups we don't care about the kind of 'a' and 'b',
669 // just the paths.
670 return strcmp(a.second, b.second) == 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000671 }
672
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000673 static data_type ReadData(const internal_key_type& k, const unsigned char* d,
Mike Stump1eb44332009-09-09 15:08:12 +0000674 unsigned) {
675
Ted Kremenekad6ce5c2009-02-13 22:07:44 +0000676 if (k.first /* File or Directory */) {
Rafael Espindola0fda0f72013-08-01 21:42:11 +0000677 bool IsDirectory = true;
678 if (k.first == 0x1 /* File */) {
679 IsDirectory = false;
680 d += 4 * 2; // Skip the first 2 words.
681 }
682
Stephen Hines651f13c2014-04-23 16:59:28 -0700683 using namespace llvm::support;
684
685 uint64_t File = endian::readNext<uint64_t, little, unaligned>(d);
686 uint64_t Device = endian::readNext<uint64_t, little, unaligned>(d);
Rafael Espindola0fda0f72013-08-01 21:42:11 +0000687 llvm::sys::fs::UniqueID UniqueID(File, Device);
Stephen Hines651f13c2014-04-23 16:59:28 -0700688 time_t ModTime = endian::readNext<uint64_t, little, unaligned>(d);
689 uint64_t Size = endian::readNext<uint64_t, little, unaligned>(d);
Rafael Espindola0fda0f72013-08-01 21:42:11 +0000690 return data_type(Size, ModTime, UniqueID, IsDirectory);
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000691 }
Ted Kremenekad6ce5c2009-02-13 22:07:44 +0000692
693 // Negative stat. Don't read anything.
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000694 return data_type();
Ted Kremenek337edcd2009-02-12 03:26:59 +0000695 }
696};
Ted Kremenek337edcd2009-02-12 03:26:59 +0000697
Chris Lattner10e286a2010-11-23 19:19:34 +0000698class PTHStatCache : public FileSystemStatCache {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700699 typedef llvm::OnDiskChainedHashTable<PTHStatLookupTrait> CacheTy;
Ted Kremenek337edcd2009-02-12 03:26:59 +0000700 CacheTy Cache;
701
Mike Stump1eb44332009-09-09 15:08:12 +0000702public:
Ted Kremenek337edcd2009-02-12 03:26:59 +0000703 PTHStatCache(PTHFileLookup &FL) :
704 Cache(FL.getNumBuckets(), FL.getNumEntries(), FL.getBuckets(),
705 FL.getBase()) {}
706
Rafael Espindola0fda0f72013-08-01 21:42:11 +0000707 LookupResult getStat(const char *Path, FileData &Data, bool isFile,
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700708 std::unique_ptr<vfs::File> *F,
709 vfs::FileSystem &FS) override {
Ted Kremenek337edcd2009-02-12 03:26:59 +0000710 // Do the lookup for the file's data in the PTH file.
Chris Lattner10e286a2010-11-23 19:19:34 +0000711 CacheTy::iterator I = Cache.find(Path);
Ted Kremenek337edcd2009-02-12 03:26:59 +0000712
713 // If we don't get a hit in the PTH file just forward to 'stat'.
Kovarththanan Rajaratnam700030e2010-03-12 08:23:34 +0000714 if (I == Cache.end())
Stephen Hines651f13c2014-04-23 16:59:28 -0700715 return statChained(Path, Data, isFile, F, FS);
Mike Stump1eb44332009-09-09 15:08:12 +0000716
Rafael Espindola0fda0f72013-08-01 21:42:11 +0000717 const PTHStatData &D = *I;
Mike Stump1eb44332009-09-09 15:08:12 +0000718
Rafael Espindola0fda0f72013-08-01 21:42:11 +0000719 if (!D.HasData)
Chris Lattnerd6f61112010-11-23 20:05:15 +0000720 return CacheMissing;
Ted Kremenekad6ce5c2009-02-13 22:07:44 +0000721
Stephen Hines651f13c2014-04-23 16:59:28 -0700722 Data.Name = Path;
Rafael Espindola0fda0f72013-08-01 21:42:11 +0000723 Data.Size = D.Size;
724 Data.ModTime = D.ModTime;
725 Data.UniqueID = D.UniqueID;
726 Data.IsDirectory = D.IsDirectory;
727 Data.IsNamedPipe = false;
728 Data.InPCH = true;
729
Chris Lattnerd6f61112010-11-23 20:05:15 +0000730 return CacheExists;
Ted Kremenek337edcd2009-02-12 03:26:59 +0000731 }
732};
Ted Kremenekd5785692009-02-23 23:27:54 +0000733} // end anonymous namespace
Ted Kremenek337edcd2009-02-12 03:26:59 +0000734
Chris Lattner10e286a2010-11-23 19:19:34 +0000735FileSystemStatCache *PTHManager::createStatCache() {
Ted Kremenek5f747d12009-02-12 03:45:39 +0000736 return new PTHStatCache(*((PTHFileLookup*) FileLookup));
Ted Kremenek337edcd2009-02-12 03:26:59 +0000737}