blob: 54269c9ee15b3587d3e8a181da363c35c326e891 [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"
16#include "clang/Basic/IdentifierTable.h"
Ted Kremenek274b2082008-11-12 21:37:15 +000017#include "clang/Lex/PTHLexer.h"
18#include "clang/Lex/Preprocessor.h"
Ted Kremenek0c6a77b2008-12-03 00:38:03 +000019#include "clang/Lex/PTHManager.h"
20#include "clang/Lex/Token.h"
21#include "clang/Lex/Preprocessor.h"
Ted Kremenek0c6a77b2008-12-03 00:38:03 +000022#include "llvm/ADT/StringMap.h"
23#include "llvm/ADT/OwningPtr.h"
Chris Lattner6f78c3b2009-01-22 23:50:07 +000024#include "llvm/Support/Compiler.h"
25#include "llvm/Support/MathExtras.h"
26#include "llvm/Support/MemoryBuffer.h"
27#include "llvm/System/Host.h"
Ted Kremenek274b2082008-11-12 21:37:15 +000028using namespace clang;
29
Ted Kremenek7b78b7c2009-01-19 23:13:15 +000030#define DISK_TOKEN_SIZE (1+1+2+4+4)
Ted Kremenek268ee702008-12-12 18:34:08 +000031
Ted Kremenek0c6a77b2008-12-03 00:38:03 +000032//===----------------------------------------------------------------------===//
33// Utility methods for reading from the mmap'ed PTH file.
34//===----------------------------------------------------------------------===//
35
Chris Lattner5ff43172009-01-22 19:48:26 +000036static inline uint16_t ReadUnalignedLE16(const unsigned char *&Data) {
Chris Lattnerda9d61c2009-01-18 01:57:14 +000037 uint16_t V = ((uint16_t)Data[0] << 0) |
38 ((uint16_t)Data[1] << 8);
39 Data += 2;
40 return V;
41}
42
Chris Lattner5ff43172009-01-22 19:48:26 +000043static inline uint32_t ReadLE32(const unsigned char *&Data) {
Chris Lattnerfbc33382009-01-23 00:13:28 +000044 // Hosts that directly support little-endian 32-bit loads can just
45 // use them. Big-endian hosts need a bswap.
Chris Lattnerf15674c2009-01-18 02:19:16 +000046 uint32_t V = *((uint32_t*)Data);
Chris Lattner6f78c3b2009-01-22 23:50:07 +000047 if (llvm::sys::isBigEndianHost())
48 V = llvm::ByteSwap_32(V);
Chris Lattnerda9d61c2009-01-18 01:57:14 +000049 Data += 4;
50 return V;
51}
52
53
Ted Kremeneke5680f32008-12-23 01:30:52 +000054//===----------------------------------------------------------------------===//
55// PTHLexer methods.
56//===----------------------------------------------------------------------===//
57
Chris Lattnerda9d61c2009-01-18 01:57:14 +000058PTHLexer::PTHLexer(Preprocessor &PP, FileID FID, const unsigned char *D,
59 const unsigned char *ppcond,
Chris Lattner2b2453a2009-01-17 06:22:33 +000060 PTHSpellingSearch &mySpellingSrch, PTHManager &PM)
61 : PreprocessorLexer(&PP, FID), TokBuf(D), CurPtr(D), LastHashTokPtr(0),
Ted Kremenek5f074262009-01-09 22:05:30 +000062 PPCond(ppcond), CurPPCondPtr(ppcond), MySpellingSrch(mySpellingSrch),
Chris Lattner2b2453a2009-01-17 06:22:33 +000063 PTHMgr(PM) {
64
65 FileStartLoc = PP.getSourceManager().getLocForStartOfFile(FID);
Ted Kremenek5f074262009-01-09 22:05:30 +000066}
Ted Kremeneke5680f32008-12-23 01:30:52 +000067
68void PTHLexer::Lex(Token& Tok) {
69LexNextToken:
Ted Kremeneke5680f32008-12-23 01:30:52 +000070
Ted Kremenek866bdf72008-12-23 02:30:15 +000071 //===--------------------------------------==//
72 // Read the raw token data.
73 //===--------------------------------------==//
74
75 // Shadow CurPtr into an automatic variable.
Chris Lattneraff6ef82009-01-21 07:21:56 +000076 const unsigned char *CurPtrShadow = CurPtr;
Ted Kremenek866bdf72008-12-23 02:30:15 +000077
Chris Lattner1b5285e2009-01-18 02:10:31 +000078 // Read in the data for the token.
Chris Lattner5ff43172009-01-22 19:48:26 +000079 unsigned Word0 = ReadLE32(CurPtrShadow);
80 uint32_t IdentifierID = ReadLE32(CurPtrShadow);
81 uint32_t FileOffset = ReadLE32(CurPtrShadow);
Ted Kremenek7b78b7c2009-01-19 23:13:15 +000082
83 tok::TokenKind TKind = (tok::TokenKind) (Word0 & 0xFF);
84 Token::TokenFlags TFlags = (Token::TokenFlags) ((Word0 >> 8) & 0xFF);
Chris Lattneraff6ef82009-01-21 07:21:56 +000085 uint32_t Len = Word0 >> 16;
Ted Kremenek7b78b7c2009-01-19 23:13:15 +000086
Chris Lattneraff6ef82009-01-21 07:21:56 +000087 CurPtr = CurPtrShadow;
Ted Kremenek866bdf72008-12-23 02:30:15 +000088
89 //===--------------------------------------==//
90 // Construct the token itself.
91 //===--------------------------------------==//
92
93 Tok.startToken();
Chris Lattner898a0bb2009-01-18 02:34:01 +000094 Tok.setKind(TKind);
95 Tok.setFlag(TFlags);
Ted Kremenek59d08cb2008-12-23 19:24:24 +000096 assert(!LexingRawMode);
Chris Lattner2b2453a2009-01-17 06:22:33 +000097 Tok.setLocation(FileStartLoc.getFileLocWithOffset(FileOffset));
Ted Kremenek866bdf72008-12-23 02:30:15 +000098 Tok.setLength(Len);
99
Chris Lattnerd0a69692009-01-21 07:50:06 +0000100 // Handle identifiers.
101 if (IdentifierID) {
102 MIOpt.ReadToken();
103 IdentifierInfo *II = PTHMgr.GetIdentifierInfo(IdentifierID-1);
Chris Lattner863c4862009-01-23 18:35:48 +0000104
Chris Lattnerd0a69692009-01-21 07:50:06 +0000105 Tok.setIdentifierInfo(II);
Chris Lattner863c4862009-01-23 18:35:48 +0000106
107 // Change the kind of this identifier to the appropriate token kind, e.g.
108 // turning "for" into a keyword.
109 Tok.setKind(II->getTokenID());
110
Chris Lattnerd0a69692009-01-21 07:50:06 +0000111 if (II->isHandleIdentifierCase())
112 PP->HandleIdentifier(Tok);
113 return;
114 }
115
Ted Kremenek866bdf72008-12-23 02:30:15 +0000116 //===--------------------------------------==//
117 // Process the token.
118 //===--------------------------------------==//
Ted Kremenek5f074262009-01-09 22:05:30 +0000119#if 0
120 SourceManager& SM = PP->getSourceManager();
121 llvm::cerr << SM.getFileEntryForID(FileID)->getName()
122 << ':' << SM.getLogicalLineNumber(Tok.getLocation())
123 << ':' << SM.getLogicalColumnNumber(Tok.getLocation())
124 << '\n';
125#endif
Ted Kremeneke5680f32008-12-23 01:30:52 +0000126
Chris Lattner898a0bb2009-01-18 02:34:01 +0000127 if (TKind == tok::eof) {
Ted Kremeneke5680f32008-12-23 01:30:52 +0000128 // Save the end-of-file token.
129 EofToken = Tok;
130
131 Preprocessor *PPCache = PP;
Ted Kremenek59d08cb2008-12-23 19:24:24 +0000132
133 assert(!ParsingPreprocessorDirective);
134 assert(!LexingRawMode);
135
136 // FIXME: Issue diagnostics similar to Lexer.
137 if (PP->HandleEndOfFile(Tok, false))
Ted Kremeneke5680f32008-12-23 01:30:52 +0000138 return;
Ted Kremenek59d08cb2008-12-23 19:24:24 +0000139
Ted Kremeneke5680f32008-12-23 01:30:52 +0000140 assert(PPCache && "Raw buffer::LexEndOfFile should return a token");
141 return PPCache->Lex(Tok);
142 }
Ted Kremeneke5680f32008-12-23 01:30:52 +0000143
Chris Lattner898a0bb2009-01-18 02:34:01 +0000144 if (TKind == tok::hash && Tok.isAtStartOfLine()) {
Ted Kremenek59d08cb2008-12-23 19:24:24 +0000145 LastHashTokPtr = CurPtr - DISK_TOKEN_SIZE;
146 assert(!LexingRawMode);
147 PP->HandleDirective(Tok);
148
149 if (PP->isCurrentLexer(this))
150 goto LexNextToken;
151
152 return PP->Lex(Tok);
153 }
154
Chris Lattner898a0bb2009-01-18 02:34:01 +0000155 if (TKind == tok::eom) {
Ted Kremenek59d08cb2008-12-23 19:24:24 +0000156 assert(ParsingPreprocessorDirective);
Ted Kremeneke5680f32008-12-23 01:30:52 +0000157 ParsingPreprocessorDirective = false;
158 return;
159 }
Ted Kremeneke5680f32008-12-23 01:30:52 +0000160
Ted Kremenek59d08cb2008-12-23 19:24:24 +0000161 MIOpt.ReadToken();
Ted Kremeneke5680f32008-12-23 01:30:52 +0000162}
163
164// FIXME: We can just grab the last token instead of storing a copy
165// into EofToken.
Ted Kremenek59d08cb2008-12-23 19:24:24 +0000166void PTHLexer::getEOF(Token& Tok) {
Ted Kremenekdefb7092009-01-09 00:36:11 +0000167 assert(EofToken.is(tok::eof));
Ted Kremeneke5680f32008-12-23 01:30:52 +0000168 Tok = EofToken;
169}
170
171void PTHLexer::DiscardToEndOfLine() {
172 assert(ParsingPreprocessorDirective && ParsingFilename == false &&
173 "Must be in a preprocessing directive!");
174
175 // We assume that if the preprocessor wishes to discard to the end of
176 // the line that it also means to end the current preprocessor directive.
177 ParsingPreprocessorDirective = false;
178
179 // Skip tokens by only peeking at their token kind and the flags.
180 // We don't need to actually reconstruct full tokens from the token buffer.
181 // This saves some copies and it also reduces IdentifierInfo* lookup.
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000182 const unsigned char* p = CurPtr;
Ted Kremeneke5680f32008-12-23 01:30:52 +0000183 while (1) {
184 // Read the token kind. Are we at the end of the file?
185 tok::TokenKind x = (tok::TokenKind) (uint8_t) *p;
186 if (x == tok::eof) break;
187
188 // Read the token flags. Are we at the start of the next line?
189 Token::TokenFlags y = (Token::TokenFlags) (uint8_t) p[1];
190 if (y & Token::StartOfLine) break;
191
192 // Skip to the next token.
193 p += DISK_TOKEN_SIZE;
194 }
195
196 CurPtr = p;
197}
198
Ted Kremenek268ee702008-12-12 18:34:08 +0000199/// SkipBlock - Used by Preprocessor to skip the current conditional block.
200bool PTHLexer::SkipBlock() {
201 assert(CurPPCondPtr && "No cached PP conditional information.");
202 assert(LastHashTokPtr && "No known '#' token.");
203
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000204 const unsigned char* HashEntryI = 0;
Ted Kremenek268ee702008-12-12 18:34:08 +0000205 uint32_t Offset;
206 uint32_t TableIdx;
207
208 do {
Ted Kremenek41a26602008-12-12 22:05:38 +0000209 // Read the token offset from the side-table.
Chris Lattner5ff43172009-01-22 19:48:26 +0000210 Offset = ReadLE32(CurPPCondPtr);
Ted Kremenek41a26602008-12-12 22:05:38 +0000211
212 // Read the target table index from the side-table.
Chris Lattner5ff43172009-01-22 19:48:26 +0000213 TableIdx = ReadLE32(CurPPCondPtr);
Ted Kremenek41a26602008-12-12 22:05:38 +0000214
215 // Compute the actual memory address of the '#' token data for this entry.
216 HashEntryI = TokBuf + Offset;
217
218 // Optmization: "Sibling jumping". #if...#else...#endif blocks can
219 // contain nested blocks. In the side-table we can jump over these
220 // nested blocks instead of doing a linear search if the next "sibling"
221 // entry is not at a location greater than LastHashTokPtr.
222 if (HashEntryI < LastHashTokPtr && TableIdx) {
223 // In the side-table we are still at an entry for a '#' token that
224 // is earlier than the last one we saw. Check if the location we would
225 // stride gets us closer.
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000226 const unsigned char* NextPPCondPtr =
227 PPCond + TableIdx*(sizeof(uint32_t)*2);
Ted Kremenek41a26602008-12-12 22:05:38 +0000228 assert(NextPPCondPtr >= CurPPCondPtr);
229 // Read where we should jump to.
Chris Lattner5ff43172009-01-22 19:48:26 +0000230 uint32_t TmpOffset = ReadLE32(NextPPCondPtr);
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000231 const unsigned char* HashEntryJ = TokBuf + TmpOffset;
Ted Kremenek41a26602008-12-12 22:05:38 +0000232
233 if (HashEntryJ <= LastHashTokPtr) {
234 // Jump directly to the next entry in the side table.
235 HashEntryI = HashEntryJ;
236 Offset = TmpOffset;
Chris Lattner5ff43172009-01-22 19:48:26 +0000237 TableIdx = ReadLE32(NextPPCondPtr);
Ted Kremenek41a26602008-12-12 22:05:38 +0000238 CurPPCondPtr = NextPPCondPtr;
239 }
240 }
Ted Kremenek268ee702008-12-12 18:34:08 +0000241 }
Ted Kremenek41a26602008-12-12 22:05:38 +0000242 while (HashEntryI < LastHashTokPtr);
243 assert(HashEntryI == LastHashTokPtr && "No PP-cond entry found for '#'");
Ted Kremenek268ee702008-12-12 18:34:08 +0000244 assert(TableIdx && "No jumping from #endifs.");
245
246 // Update our side-table iterator.
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000247 const unsigned char* NextPPCondPtr = PPCond + TableIdx*(sizeof(uint32_t)*2);
Ted Kremenek268ee702008-12-12 18:34:08 +0000248 assert(NextPPCondPtr >= CurPPCondPtr);
249 CurPPCondPtr = NextPPCondPtr;
250
251 // Read where we should jump to.
Chris Lattner5ff43172009-01-22 19:48:26 +0000252 HashEntryI = TokBuf + ReadLE32(NextPPCondPtr);
253 uint32_t NextIdx = ReadLE32(NextPPCondPtr);
Ted Kremenek268ee702008-12-12 18:34:08 +0000254
255 // By construction NextIdx will be zero if this is a #endif. This is useful
256 // to know to obviate lexing another token.
257 bool isEndif = NextIdx == 0;
Ted Kremenek268ee702008-12-12 18:34:08 +0000258
259 // This case can occur when we see something like this:
260 //
261 // #if ...
262 // /* a comment or nothing */
263 // #elif
264 //
265 // If we are skipping the first #if block it will be the case that CurPtr
266 // already points 'elif'. Just return.
267
Ted Kremenek41a26602008-12-12 22:05:38 +0000268 if (CurPtr > HashEntryI) {
269 assert(CurPtr == HashEntryI + DISK_TOKEN_SIZE);
Ted Kremenek268ee702008-12-12 18:34:08 +0000270 // Did we reach a #endif? If so, go ahead and consume that token as well.
271 if (isEndif)
Ted Kremeneke5680f32008-12-23 01:30:52 +0000272 CurPtr += DISK_TOKEN_SIZE*2;
Ted Kremenek268ee702008-12-12 18:34:08 +0000273 else
Ted Kremenek41a26602008-12-12 22:05:38 +0000274 LastHashTokPtr = HashEntryI;
Ted Kremenek268ee702008-12-12 18:34:08 +0000275
276 return isEndif;
277 }
278
279 // Otherwise, we need to advance. Update CurPtr to point to the '#' token.
Ted Kremenek41a26602008-12-12 22:05:38 +0000280 CurPtr = HashEntryI;
Ted Kremenek268ee702008-12-12 18:34:08 +0000281
282 // Update the location of the last observed '#'. This is useful if we
283 // are skipping multiple blocks.
284 LastHashTokPtr = CurPtr;
Ted Kremenek268ee702008-12-12 18:34:08 +0000285
Ted Kremeneke5680f32008-12-23 01:30:52 +0000286 // Skip the '#' token.
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000287 assert(((tok::TokenKind)*CurPtr) == tok::hash);
Ted Kremeneke5680f32008-12-23 01:30:52 +0000288 CurPtr += DISK_TOKEN_SIZE;
289
Ted Kremenek268ee702008-12-12 18:34:08 +0000290 // Did we reach a #endif? If so, go ahead and consume that token as well.
Ted Kremeneke5680f32008-12-23 01:30:52 +0000291 if (isEndif) { CurPtr += DISK_TOKEN_SIZE*2; }
Ted Kremenek268ee702008-12-12 18:34:08 +0000292
293 return isEndif;
294}
295
Ted Kremenek30a12ec2008-12-17 23:36:32 +0000296SourceLocation PTHLexer::getSourceLocation() {
Chris Lattner1b5285e2009-01-18 02:10:31 +0000297 // getSourceLocation is not on the hot path. It is used to get the location
298 // of the next token when transitioning back to this lexer when done
Ted Kremenek30a12ec2008-12-17 23:36:32 +0000299 // handling a #included file. Just read the necessary data from the token
300 // data buffer to construct the SourceLocation object.
301 // NOTE: This is a virtual function; hence it is defined out-of-line.
Ted Kremenekb248d532009-01-21 22:41:38 +0000302 const unsigned char *OffsetPtr = CurPtr + (DISK_TOKEN_SIZE - 4);
Chris Lattner5ff43172009-01-22 19:48:26 +0000303 uint32_t Offset = ReadLE32(OffsetPtr);
Chris Lattner1b5285e2009-01-18 02:10:31 +0000304 return FileStartLoc.getFileLocWithOffset(Offset);
Ted Kremenek30a12ec2008-12-17 23:36:32 +0000305}
306
Ted Kremenek5f074262009-01-09 22:05:30 +0000307//===----------------------------------------------------------------------===//
308// getSpelling() - Use cached data in PTH files for getSpelling().
309//===----------------------------------------------------------------------===//
310
Chris Lattner2b2453a2009-01-17 06:22:33 +0000311unsigned PTHManager::getSpelling(FileID FID, unsigned FPos,
312 const char *&Buffer) {
313 llvm::DenseMap<FileID, PTHSpellingSearch*>::iterator I =SpellingMap.find(FID);
Ted Kremenek5f074262009-01-09 22:05:30 +0000314
315 if (I == SpellingMap.end())
Chris Lattner1b5285e2009-01-18 02:10:31 +0000316 return 0;
Ted Kremenekf02f6f02009-01-13 22:05:50 +0000317
Chris Lattner2b2453a2009-01-17 06:22:33 +0000318 return I->second->getSpellingBinarySearch(FPos, Buffer);
319}
320
321unsigned PTHManager::getSpelling(SourceLocation Loc, const char *&Buffer) {
Chris Lattner6b7b8402009-01-17 06:29:33 +0000322 SourceManager &SM = PP->getSourceManager();
323 Loc = SM.getSpellingLoc(Loc);
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000324 std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc);
Chris Lattner2b2453a2009-01-17 06:22:33 +0000325 return getSpelling(LocInfo.first, LocInfo.second, Buffer);
Ted Kremenek5f074262009-01-09 22:05:30 +0000326}
327
328unsigned PTHManager::getSpellingAtPTHOffset(unsigned PTHOffset,
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000329 const char *&Buffer) {
Chris Lattner1b5285e2009-01-18 02:10:31 +0000330 assert(PTHOffset < Buf->getBufferSize());
331 const unsigned char* Ptr =
332 (const unsigned char*)Buf->getBufferStart() + PTHOffset;
Ted Kremenek32a8ad52009-01-08 04:30:32 +0000333
334 // The string is prefixed by 16 bits for its length, followed by the string
335 // itself.
Chris Lattner5ff43172009-01-22 19:48:26 +0000336 unsigned Len = ReadUnalignedLE16(Ptr);
Chris Lattner1b5285e2009-01-18 02:10:31 +0000337 Buffer = (const char *)Ptr;
338 return Len;
Ted Kremenek32a8ad52009-01-08 04:30:32 +0000339}
340
Chris Lattner1b5285e2009-01-18 02:10:31 +0000341unsigned PTHSpellingSearch::getSpellingLinearSearch(unsigned FPos,
Ted Kremenek5f074262009-01-09 22:05:30 +0000342 const char *&Buffer) {
Chris Lattner1b5285e2009-01-18 02:10:31 +0000343 const unsigned char *Ptr = LinearItr;
344 unsigned Len = 0;
Ted Kremenek5f074262009-01-09 22:05:30 +0000345
Chris Lattner1b5285e2009-01-18 02:10:31 +0000346 if (Ptr == TableEnd)
347 return getSpellingBinarySearch(FPos, Buffer);
Ted Kremenek5f074262009-01-09 22:05:30 +0000348
349 do {
Chris Lattner5ff43172009-01-22 19:48:26 +0000350 uint32_t TokOffset = ReadLE32(Ptr);
Ted Kremenek32a8ad52009-01-08 04:30:32 +0000351
Chris Lattner1b5285e2009-01-18 02:10:31 +0000352 if (TokOffset > FPos)
353 return getSpellingBinarySearch(FPos, Buffer);
Ted Kremenek32a8ad52009-01-08 04:30:32 +0000354
Ted Kremenek32a8ad52009-01-08 04:30:32 +0000355 // Did we find a matching token offset for this spelling?
Chris Lattner1b5285e2009-01-18 02:10:31 +0000356 if (TokOffset == FPos) {
Chris Lattner5ff43172009-01-22 19:48:26 +0000357 uint32_t SpellingPTHOffset = ReadLE32(Ptr);
Chris Lattner1b5285e2009-01-18 02:10:31 +0000358 Len = PTHMgr.getSpellingAtPTHOffset(SpellingPTHOffset, Buffer);
Ted Kremenek32a8ad52009-01-08 04:30:32 +0000359 break;
360 }
Chris Lattner1b5285e2009-01-18 02:10:31 +0000361 } while (Ptr != TableEnd);
Ted Kremenek32a8ad52009-01-08 04:30:32 +0000362
Chris Lattner1b5285e2009-01-18 02:10:31 +0000363 LinearItr = Ptr;
364 return Len;
Ted Kremenekb70e3da2009-01-08 02:47:16 +0000365}
366
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000367
Chris Lattner1b5285e2009-01-18 02:10:31 +0000368unsigned PTHSpellingSearch::getSpellingBinarySearch(unsigned FPos,
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000369 const char *&Buffer) {
Ted Kremenek5f074262009-01-09 22:05:30 +0000370
Ted Kremenekf02f6f02009-01-13 22:05:50 +0000371 assert((TableEnd - TableBeg) % SpellingEntrySize == 0);
Chris Lattner1b5285e2009-01-18 02:10:31 +0000372 assert(TableEnd >= TableBeg);
Ted Kremenekf02f6f02009-01-13 22:05:50 +0000373
374 if (TableEnd == TableBeg)
375 return 0;
376
Ted Kremenek5f074262009-01-09 22:05:30 +0000377 unsigned min = 0;
Chris Lattner1b5285e2009-01-18 02:10:31 +0000378 const unsigned char *tb = TableBeg;
Ted Kremenekf02f6f02009-01-13 22:05:50 +0000379 unsigned max = NumSpellings;
Ted Kremenek5f074262009-01-09 22:05:30 +0000380
Ted Kremenekf02f6f02009-01-13 22:05:50 +0000381 do {
Ted Kremenek5f074262009-01-09 22:05:30 +0000382 unsigned i = (max - min) / 2 + min;
Chris Lattner1b5285e2009-01-18 02:10:31 +0000383 const unsigned char *Ptr = tb + (i * SpellingEntrySize);
Ted Kremenek5f074262009-01-09 22:05:30 +0000384
Chris Lattner5ff43172009-01-22 19:48:26 +0000385 uint32_t TokOffset = ReadLE32(Ptr);
Chris Lattner1b5285e2009-01-18 02:10:31 +0000386 if (TokOffset > FPos) {
Ted Kremenek5f074262009-01-09 22:05:30 +0000387 max = i;
Ted Kremenekf02f6f02009-01-13 22:05:50 +0000388 assert(!(max == min) || (min == i));
Ted Kremenek5f074262009-01-09 22:05:30 +0000389 continue;
390 }
391
Chris Lattner1b5285e2009-01-18 02:10:31 +0000392 if (TokOffset < FPos) {
Ted Kremenek7bd88aa2009-01-13 22:16:45 +0000393 if (i == min)
394 break;
395
Ted Kremenek5f074262009-01-09 22:05:30 +0000396 min = i;
397 continue;
398 }
399
Chris Lattner5ff43172009-01-22 19:48:26 +0000400 uint32_t SpellingPTHOffset = ReadLE32(Ptr);
Ted Kremenek5f074262009-01-09 22:05:30 +0000401 return PTHMgr.getSpellingAtPTHOffset(SpellingPTHOffset, Buffer);
402 }
Ted Kremenekf02f6f02009-01-13 22:05:50 +0000403 while (min != max);
Ted Kremenek5f074262009-01-09 22:05:30 +0000404
405 return 0;
406}
407
Chris Lattner2b2453a2009-01-17 06:22:33 +0000408unsigned PTHLexer::getSpelling(SourceLocation Loc, const char *&Buffer) {
409 SourceManager &SM = PP->getSourceManager();
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000410 std::pair<FileID, unsigned> LocInfo = SM.getDecomposedSpellingLoc(Loc);
Chris Lattner2b2453a2009-01-17 06:22:33 +0000411
412 FileID FID = LocInfo.first;
413 unsigned FPos = LocInfo.second;
Ted Kremenek5f074262009-01-09 22:05:30 +0000414
Chris Lattner2b2453a2009-01-17 06:22:33 +0000415 if (FID == getFileID())
416 return MySpellingSrch.getSpellingLinearSearch(FPos, Buffer);
417 return PTHMgr.getSpelling(FID, FPos, Buffer);
Ted Kremenek5f074262009-01-09 22:05:30 +0000418}
419
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000420//===----------------------------------------------------------------------===//
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000421// Internal Data Structures for PTH file lookup and resolving identifiers.
422//===----------------------------------------------------------------------===//
423
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000424
425/// PTHFileLookup - This internal data structure is used by the PTHManager
426/// to map from FileEntry objects managed by FileManager to offsets within
427/// the PTH file.
428namespace {
429class VISIBILITY_HIDDEN PTHFileLookup {
430public:
431 class Val {
Ted Kremenekfb645b62008-12-11 23:36:38 +0000432 uint32_t TokenOff;
433 uint32_t PPCondOff;
Ted Kremenekb70e3da2009-01-08 02:47:16 +0000434 uint32_t SpellingOff;
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000435 public:
Ted Kremenekfb645b62008-12-11 23:36:38 +0000436 Val() : TokenOff(~0) {}
Ted Kremenekb70e3da2009-01-08 02:47:16 +0000437 Val(uint32_t toff, uint32_t poff, uint32_t soff)
438 : TokenOff(toff), PPCondOff(poff), SpellingOff(soff) {}
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000439
Chris Lattner1b5285e2009-01-18 02:10:31 +0000440 bool isValid() const { return TokenOff != ~((uint32_t)0); }
441
Ted Kremenekfb645b62008-12-11 23:36:38 +0000442 uint32_t getTokenOffset() const {
Chris Lattner1b5285e2009-01-18 02:10:31 +0000443 assert(isValid() && "PTHFileLookup entry initialized.");
Ted Kremenekfb645b62008-12-11 23:36:38 +0000444 return TokenOff;
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000445 }
446
Ted Kremenekb70e3da2009-01-08 02:47:16 +0000447 uint32_t getPPCondOffset() const {
Chris Lattner1b5285e2009-01-18 02:10:31 +0000448 assert(isValid() && "PTHFileLookup entry initialized.");
Ted Kremenekfb645b62008-12-11 23:36:38 +0000449 return PPCondOff;
450 }
451
Ted Kremenekb70e3da2009-01-08 02:47:16 +0000452 uint32_t getSpellingOffset() const {
Chris Lattner1b5285e2009-01-18 02:10:31 +0000453 assert(isValid() && "PTHFileLookup entry initialized.");
Ted Kremenekb70e3da2009-01-08 02:47:16 +0000454 return SpellingOff;
455 }
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000456 };
457
458private:
459 llvm::StringMap<Val> FileMap;
460
461public:
462 PTHFileLookup() {};
463
Ted Kremenekcdd8f212009-01-21 07:34:28 +0000464 bool isEmpty() const {
465 return FileMap.empty();
466 }
467
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000468 Val Lookup(const FileEntry* FE) {
469 const char* s = FE->getName();
470 unsigned size = strlen(s);
471 return FileMap.GetOrCreateValue(s, s+size).getValue();
472 }
473
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000474 void ReadTable(const unsigned char* D) {
Chris Lattner5ff43172009-01-22 19:48:26 +0000475 uint32_t N = ReadLE32(D); // Read the length of the table.
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000476
477 for ( ; N > 0; --N) { // The rest of the data is the table itself.
Chris Lattner5ff43172009-01-22 19:48:26 +0000478 uint32_t Len = ReadLE32(D);
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000479 const char* s = (const char *)D;
Chris Lattner1b5285e2009-01-18 02:10:31 +0000480 D += Len;
Ted Kremenekb70e3da2009-01-08 02:47:16 +0000481
Chris Lattner5ff43172009-01-22 19:48:26 +0000482 uint32_t TokenOff = ReadLE32(D);
483 uint32_t PPCondOff = ReadLE32(D);
484 uint32_t SpellingOff = ReadLE32(D);
Ted Kremenekb70e3da2009-01-08 02:47:16 +0000485
Chris Lattner1b5285e2009-01-18 02:10:31 +0000486 FileMap.GetOrCreateValue(s, s+Len).getValue() =
Ted Kremenekb70e3da2009-01-08 02:47:16 +0000487 Val(TokenOff, PPCondOff, SpellingOff);
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000488 }
489 }
490};
491} // end anonymous namespace
492
493//===----------------------------------------------------------------------===//
494// PTHManager methods.
495//===----------------------------------------------------------------------===//
496
497PTHManager::PTHManager(const llvm::MemoryBuffer* buf, void* fileLookup,
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000498 const unsigned char* idDataTable,
499 IdentifierInfo** perIDCache,
500 const unsigned char* sortedIdTable, unsigned numIds)
Ted Kremenek6183e482008-12-03 01:16:39 +0000501: Buf(buf), PerIDCache(perIDCache), FileLookup(fileLookup),
Ted Kremenek72b1b152009-01-15 18:47:46 +0000502 IdDataTable(idDataTable), SortedIdTable(sortedIdTable),
503 NumIds(numIds), PP(0) {}
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000504
505PTHManager::~PTHManager() {
506 delete Buf;
507 delete (PTHFileLookup*) FileLookup;
Ted Kremenek0e50b6e2008-12-04 22:47:11 +0000508 free(PerIDCache);
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000509}
510
Ted Kremenek72b1b152009-01-15 18:47:46 +0000511PTHManager* PTHManager::Create(const std::string& file) {
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000512 // Memory map the PTH file.
513 llvm::OwningPtr<llvm::MemoryBuffer>
514 File(llvm::MemoryBuffer::getFile(file.c_str()));
515
516 if (!File)
517 return 0;
518
519 // Get the buffer ranges and check if there are at least three 32-bit
520 // words at the end of the file.
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000521 const unsigned char* BufBeg = (unsigned char*)File->getBufferStart();
522 const unsigned char* BufEnd = (unsigned char*)File->getBufferEnd();
Ted Kremeneke1b64982009-01-26 21:43:14 +0000523
524 // Check the prologue of the file.
Ted Kremenek67d15052009-01-26 21:50:21 +0000525 if ((BufEnd - BufBeg) < (unsigned) (sizeof("cfe-pth") + 3 + 4) ||
Ted Kremeneke1b64982009-01-26 21:43:14 +0000526 memcmp(BufBeg, "cfe-pth", sizeof("cfe-pth") - 1) != 0)
527 return 0;
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000528
Ted Kremenek67d15052009-01-26 21:50:21 +0000529 // Read the PTH version.
Ted Kremeneke1b64982009-01-26 21:43:14 +0000530 const unsigned char *p = BufBeg + (sizeof("cfe-pth") - 1);
Ted Kremenek67d15052009-01-26 21:50:21 +0000531 unsigned Version = ReadLE32(p);
532
533 if (Version != PTHManager::Version)
534 return 0;
535
536 // Compute the address of the index table at the end of the PTH file.
Ted Kremeneke1b64982009-01-26 21:43:14 +0000537 const unsigned char *EndTable = BufBeg + ReadLE32(p);
538
539 if (EndTable >= BufEnd)
540 return 0;
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000541
542 // Construct the file lookup table. This will be used for mapping from
543 // FileEntry*'s to cached tokens.
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000544 const unsigned char* FileTableOffset = EndTable + sizeof(uint32_t)*3;
Chris Lattner5ff43172009-01-22 19:48:26 +0000545 const unsigned char* FileTable = BufBeg + ReadLE32(FileTableOffset);
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000546
547 if (!(FileTable > BufBeg && FileTable < BufEnd)) {
548 assert(false && "Invalid PTH file.");
549 return 0; // FIXME: Proper error diagnostic?
550 }
551
552 llvm::OwningPtr<PTHFileLookup> FL(new PTHFileLookup());
553 FL->ReadTable(FileTable);
Ted Kremenekcdd8f212009-01-21 07:34:28 +0000554
555 if (FL->isEmpty())
556 return 0;
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000557
558 // Get the location of the table mapping from persistent ids to the
559 // data needed to reconstruct identifiers.
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000560 const unsigned char* IDTableOffset = EndTable + sizeof(uint32_t)*1;
Chris Lattner5ff43172009-01-22 19:48:26 +0000561 const unsigned char* IData = BufBeg + ReadLE32(IDTableOffset);
Ted Kremenekcdd8f212009-01-21 07:34:28 +0000562
563 if (!(IData >= BufBeg && IData < BufEnd)) {
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000564 assert(false && "Invalid PTH file.");
565 return 0; // FIXME: Proper error diagnostic?
566 }
567
Ted Kremenek72b1b152009-01-15 18:47:46 +0000568 // Get the location of the lexigraphically-sorted table of persistent IDs.
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000569 const unsigned char* SortedIdTableOffset = EndTable + sizeof(uint32_t)*2;
Chris Lattner5ff43172009-01-22 19:48:26 +0000570 const unsigned char* SortedIdTable = BufBeg + ReadLE32(SortedIdTableOffset);
Ted Kremenekcdd8f212009-01-21 07:34:28 +0000571 if (!(SortedIdTable >= BufBeg && SortedIdTable < BufEnd)) {
Ted Kremenek72b1b152009-01-15 18:47:46 +0000572 assert(false && "Invalid PTH file.");
573 return 0; // FIXME: Proper error diagnostic?
574 }
575
Ted Kremenek6183e482008-12-03 01:16:39 +0000576 // Get the number of IdentifierInfos and pre-allocate the identifier cache.
Chris Lattner5ff43172009-01-22 19:48:26 +0000577 uint32_t NumIds = ReadLE32(IData);
Ted Kremenekcdd8f212009-01-21 07:34:28 +0000578
Ted Kremenek6183e482008-12-03 01:16:39 +0000579 // Pre-allocate the peristent ID -> IdentifierInfo* cache. We use calloc()
580 // so that we in the best case only zero out memory once when the OS returns
581 // us new pages.
Ted Kremenekcdd8f212009-01-21 07:34:28 +0000582 IdentifierInfo** PerIDCache = 0;
Ted Kremenek6183e482008-12-03 01:16:39 +0000583
Ted Kremenekcdd8f212009-01-21 07:34:28 +0000584 if (NumIds) {
585 PerIDCache = (IdentifierInfo**)calloc(NumIds, sizeof(*PerIDCache));
586 if (!PerIDCache) {
587 assert(false && "Could not allocate Persistent ID cache.");
588 return 0;
589 }
Ted Kremenek6183e482008-12-03 01:16:39 +0000590 }
Ted Kremenekcdd8f212009-01-21 07:34:28 +0000591
Ted Kremenek72b1b152009-01-15 18:47:46 +0000592 // Create the new PTHManager.
593 return new PTHManager(File.take(), FL.take(), IData, PerIDCache,
594 SortedIdTable, NumIds);
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000595}
Chris Lattner77ecb3a2009-01-18 02:57:21 +0000596IdentifierInfo* PTHManager::LazilyCreateIdentifierInfo(unsigned PersistentID) {
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000597 // Look in the PTH file for the string data for the IdentifierInfo object.
Chris Lattner77ecb3a2009-01-18 02:57:21 +0000598 const unsigned char* TableEntry = IdDataTable + sizeof(uint32_t)*PersistentID;
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000599 const unsigned char* IDData =
Chris Lattner5ff43172009-01-22 19:48:26 +0000600 (const unsigned char*)Buf->getBufferStart() + ReadLE32(TableEntry);
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000601 assert(IDData < (const unsigned char*)Buf->getBufferEnd());
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000602
Ted Kremenek72b1b152009-01-15 18:47:46 +0000603 // Allocate the object.
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000604 std::pair<IdentifierInfo,const unsigned char*> *Mem =
605 Alloc.Allocate<std::pair<IdentifierInfo,const unsigned char*> >();
Ted Kremenek72b1b152009-01-15 18:47:46 +0000606
607 Mem->second = IDData;
Ted Kremenekea9c26b2009-01-20 23:28:34 +0000608 IdentifierInfo *II = new ((void*) Mem) IdentifierInfo();
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000609
Ted Kremenek72b1b152009-01-15 18:47:46 +0000610 // Store the new IdentifierInfo in the cache.
Chris Lattner77ecb3a2009-01-18 02:57:21 +0000611 PerIDCache[PersistentID] = II;
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000612 return II;
613}
614
Ted Kremenek72b1b152009-01-15 18:47:46 +0000615IdentifierInfo* PTHManager::get(const char *NameStart, const char *NameEnd) {
616 unsigned min = 0;
617 unsigned max = NumIds;
Chris Lattner1b5285e2009-01-18 02:10:31 +0000618 unsigned Len = NameEnd - NameStart;
Ted Kremenek72b1b152009-01-15 18:47:46 +0000619
620 do {
621 unsigned i = (max - min) / 2 + min;
Chris Lattner1b5285e2009-01-18 02:10:31 +0000622 const unsigned char *Ptr = SortedIdTable + (i * 4);
Ted Kremenek72b1b152009-01-15 18:47:46 +0000623
624 // Read the persistentID.
Chris Lattner5ff43172009-01-22 19:48:26 +0000625 unsigned perID = ReadLE32(Ptr);
Ted Kremenek72b1b152009-01-15 18:47:46 +0000626
627 // Get the IdentifierInfo.
628 IdentifierInfo* II = GetIdentifierInfo(perID);
629
630 // First compare the lengths.
631 unsigned IILen = II->getLength();
Chris Lattner1b5285e2009-01-18 02:10:31 +0000632 if (Len < IILen) goto IsLess;
633 if (Len > IILen) goto IsGreater;
Ted Kremenek72b1b152009-01-15 18:47:46 +0000634
635 // Now compare the strings!
636 {
Chris Lattner1b5285e2009-01-18 02:10:31 +0000637 signed comp = strncmp(NameStart, II->getName(), Len);
Ted Kremenek72b1b152009-01-15 18:47:46 +0000638 if (comp < 0) goto IsLess;
639 if (comp > 0) goto IsGreater;
640 }
641 // We found a match!
642 return II;
643
644 IsGreater:
645 if (i == min) break;
646 min = i;
647 continue;
648
649 IsLess:
650 max = i;
651 assert(!(max == min) || (min == i));
652 }
Ted Kremeneke1deaac2009-01-15 19:28:38 +0000653 while (min != max);
Ted Kremenek72b1b152009-01-15 18:47:46 +0000654
655 return 0;
656}
657
658
Chris Lattnerf056d922009-01-17 08:06:50 +0000659PTHLexer *PTHManager::CreateLexer(FileID FID) {
660 const FileEntry *FE = PP->getSourceManager().getFileEntryForID(FID);
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000661 if (!FE)
662 return 0;
663
664 // Lookup the FileEntry object in our file lookup data structure. It will
665 // return a variant that indicates whether or not there is an offset within
666 // the PTH file that contains cached tokens.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000667 PTHFileLookup::Val FileData = ((PTHFileLookup*)FileLookup)->Lookup(FE);
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000668
Ted Kremenekfb645b62008-12-11 23:36:38 +0000669 if (!FileData.isValid()) // No tokens available.
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000670 return 0;
671
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000672 const unsigned char *BufStart = (const unsigned char *)Buf->getBufferStart();
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000673 // Compute the offset of the token data within the buffer.
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000674 const unsigned char* data = BufStart + FileData.getTokenOffset();
Ted Kremenek268ee702008-12-12 18:34:08 +0000675
676 // Get the location of pp-conditional table.
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000677 const unsigned char* ppcond = BufStart + FileData.getPPCondOffset();
Chris Lattner5ff43172009-01-22 19:48:26 +0000678 uint32_t Len = ReadLE32(ppcond);
Chris Lattner1b5285e2009-01-18 02:10:31 +0000679 if (Len == 0) ppcond = 0;
Ted Kremenek32a8ad52009-01-08 04:30:32 +0000680
681 // Get the location of the spelling table.
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000682 const unsigned char* spellingTable = BufStart + FileData.getSpellingOffset();
Ted Kremenek32a8ad52009-01-08 04:30:32 +0000683
Chris Lattner5ff43172009-01-22 19:48:26 +0000684 Len = ReadLE32(spellingTable);
Chris Lattner1b5285e2009-01-18 02:10:31 +0000685 if (Len == 0) spellingTable = 0;
Ted Kremenekb70e3da2009-01-08 02:47:16 +0000686
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000687 assert(data < (const unsigned char*)Buf->getBufferEnd());
Ted Kremenek5f074262009-01-09 22:05:30 +0000688
689 // Create the SpellingSearch object for this FileID.
Chris Lattner1b5285e2009-01-18 02:10:31 +0000690 PTHSpellingSearch* ss = new PTHSpellingSearch(*this, Len, spellingTable);
Chris Lattner2b2453a2009-01-17 06:22:33 +0000691 SpellingMap[FID] = ss;
Ted Kremenek5f074262009-01-09 22:05:30 +0000692
Ted Kremenek72b1b152009-01-15 18:47:46 +0000693 assert(PP && "No preprocessor set yet!");
Chris Lattner2b2453a2009-01-17 06:22:33 +0000694 return new PTHLexer(*PP, FID, data, ppcond, *ss, *this);
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000695}