blob: ec76a299845b1824d883f234e2c2f22911ffa683 [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);
324 std::pair<FileID, unsigned> LocInfo = SM.getDecomposedFileLoc(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();
410 Loc = SM.getSpellingLoc(Loc);
411 std::pair<FileID, unsigned> LocInfo = SM.getDecomposedFileLoc(Loc);
412
413 FileID FID = LocInfo.first;
414 unsigned FPos = LocInfo.second;
Ted Kremenek5f074262009-01-09 22:05:30 +0000415
Chris Lattner2b2453a2009-01-17 06:22:33 +0000416 if (FID == getFileID())
417 return MySpellingSrch.getSpellingLinearSearch(FPos, Buffer);
418 return PTHMgr.getSpelling(FID, FPos, Buffer);
Ted Kremenek5f074262009-01-09 22:05:30 +0000419}
420
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000421//===----------------------------------------------------------------------===//
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000422// Internal Data Structures for PTH file lookup and resolving identifiers.
423//===----------------------------------------------------------------------===//
424
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000425
426/// PTHFileLookup - This internal data structure is used by the PTHManager
427/// to map from FileEntry objects managed by FileManager to offsets within
428/// the PTH file.
429namespace {
430class VISIBILITY_HIDDEN PTHFileLookup {
431public:
432 class Val {
Ted Kremenekfb645b62008-12-11 23:36:38 +0000433 uint32_t TokenOff;
434 uint32_t PPCondOff;
Ted Kremenekb70e3da2009-01-08 02:47:16 +0000435 uint32_t SpellingOff;
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000436 public:
Ted Kremenekfb645b62008-12-11 23:36:38 +0000437 Val() : TokenOff(~0) {}
Ted Kremenekb70e3da2009-01-08 02:47:16 +0000438 Val(uint32_t toff, uint32_t poff, uint32_t soff)
439 : TokenOff(toff), PPCondOff(poff), SpellingOff(soff) {}
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000440
Chris Lattner1b5285e2009-01-18 02:10:31 +0000441 bool isValid() const { return TokenOff != ~((uint32_t)0); }
442
Ted Kremenekfb645b62008-12-11 23:36:38 +0000443 uint32_t getTokenOffset() const {
Chris Lattner1b5285e2009-01-18 02:10:31 +0000444 assert(isValid() && "PTHFileLookup entry initialized.");
Ted Kremenekfb645b62008-12-11 23:36:38 +0000445 return TokenOff;
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000446 }
447
Ted Kremenekb70e3da2009-01-08 02:47:16 +0000448 uint32_t getPPCondOffset() const {
Chris Lattner1b5285e2009-01-18 02:10:31 +0000449 assert(isValid() && "PTHFileLookup entry initialized.");
Ted Kremenekfb645b62008-12-11 23:36:38 +0000450 return PPCondOff;
451 }
452
Ted Kremenekb70e3da2009-01-08 02:47:16 +0000453 uint32_t getSpellingOffset() const {
Chris Lattner1b5285e2009-01-18 02:10:31 +0000454 assert(isValid() && "PTHFileLookup entry initialized.");
Ted Kremenekb70e3da2009-01-08 02:47:16 +0000455 return SpellingOff;
456 }
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000457 };
458
459private:
460 llvm::StringMap<Val> FileMap;
461
462public:
463 PTHFileLookup() {};
464
Ted Kremenekcdd8f212009-01-21 07:34:28 +0000465 bool isEmpty() const {
466 return FileMap.empty();
467 }
468
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000469 Val Lookup(const FileEntry* FE) {
470 const char* s = FE->getName();
471 unsigned size = strlen(s);
472 return FileMap.GetOrCreateValue(s, s+size).getValue();
473 }
474
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000475 void ReadTable(const unsigned char* D) {
Chris Lattner5ff43172009-01-22 19:48:26 +0000476 uint32_t N = ReadLE32(D); // Read the length of the table.
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000477
478 for ( ; N > 0; --N) { // The rest of the data is the table itself.
Chris Lattner5ff43172009-01-22 19:48:26 +0000479 uint32_t Len = ReadLE32(D);
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000480 const char* s = (const char *)D;
Chris Lattner1b5285e2009-01-18 02:10:31 +0000481 D += Len;
Ted Kremenekb70e3da2009-01-08 02:47:16 +0000482
Chris Lattner5ff43172009-01-22 19:48:26 +0000483 uint32_t TokenOff = ReadLE32(D);
484 uint32_t PPCondOff = ReadLE32(D);
485 uint32_t SpellingOff = ReadLE32(D);
Ted Kremenekb70e3da2009-01-08 02:47:16 +0000486
Chris Lattner1b5285e2009-01-18 02:10:31 +0000487 FileMap.GetOrCreateValue(s, s+Len).getValue() =
Ted Kremenekb70e3da2009-01-08 02:47:16 +0000488 Val(TokenOff, PPCondOff, SpellingOff);
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000489 }
490 }
491};
492} // end anonymous namespace
493
494//===----------------------------------------------------------------------===//
495// PTHManager methods.
496//===----------------------------------------------------------------------===//
497
498PTHManager::PTHManager(const llvm::MemoryBuffer* buf, void* fileLookup,
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000499 const unsigned char* idDataTable,
500 IdentifierInfo** perIDCache,
501 const unsigned char* sortedIdTable, unsigned numIds)
Ted Kremenek6183e482008-12-03 01:16:39 +0000502: Buf(buf), PerIDCache(perIDCache), FileLookup(fileLookup),
Ted Kremenek72b1b152009-01-15 18:47:46 +0000503 IdDataTable(idDataTable), SortedIdTable(sortedIdTable),
504 NumIds(numIds), PP(0) {}
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000505
506PTHManager::~PTHManager() {
507 delete Buf;
508 delete (PTHFileLookup*) FileLookup;
Ted Kremenek0e50b6e2008-12-04 22:47:11 +0000509 free(PerIDCache);
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000510}
511
Ted Kremenek72b1b152009-01-15 18:47:46 +0000512PTHManager* PTHManager::Create(const std::string& file) {
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000513 // Memory map the PTH file.
514 llvm::OwningPtr<llvm::MemoryBuffer>
515 File(llvm::MemoryBuffer::getFile(file.c_str()));
516
517 if (!File)
518 return 0;
519
520 // Get the buffer ranges and check if there are at least three 32-bit
521 // words at the end of the file.
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000522 const unsigned char* BufBeg = (unsigned char*)File->getBufferStart();
523 const unsigned char* BufEnd = (unsigned char*)File->getBufferEnd();
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000524
525 if(!(BufEnd > BufBeg + sizeof(uint32_t)*3)) {
526 assert(false && "Invalid PTH file.");
527 return 0; // FIXME: Proper error diagnostic?
528 }
529
530 // Compute the address of the index table at the end of the PTH file.
531 // This table contains the offset of the file lookup table, the
532 // persistent ID -> identifer data table.
Ted Kremenek293b4af2009-01-15 01:26:25 +0000533 // FIXME: We should just embed this offset in the PTH file.
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000534 const unsigned char* EndTable = BufEnd - sizeof(uint32_t)*4;
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000535
536 // Construct the file lookup table. This will be used for mapping from
537 // FileEntry*'s to cached tokens.
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000538 const unsigned char* FileTableOffset = EndTable + sizeof(uint32_t)*3;
Chris Lattner5ff43172009-01-22 19:48:26 +0000539 const unsigned char* FileTable = BufBeg + ReadLE32(FileTableOffset);
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000540
541 if (!(FileTable > BufBeg && FileTable < BufEnd)) {
542 assert(false && "Invalid PTH file.");
543 return 0; // FIXME: Proper error diagnostic?
544 }
545
546 llvm::OwningPtr<PTHFileLookup> FL(new PTHFileLookup());
547 FL->ReadTable(FileTable);
Ted Kremenekcdd8f212009-01-21 07:34:28 +0000548
549 if (FL->isEmpty())
550 return 0;
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000551
552 // Get the location of the table mapping from persistent ids to the
553 // data needed to reconstruct identifiers.
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000554 const unsigned char* IDTableOffset = EndTable + sizeof(uint32_t)*1;
Chris Lattner5ff43172009-01-22 19:48:26 +0000555 const unsigned char* IData = BufBeg + ReadLE32(IDTableOffset);
Ted Kremenekcdd8f212009-01-21 07:34:28 +0000556
557 if (!(IData >= BufBeg && IData < BufEnd)) {
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000558 assert(false && "Invalid PTH file.");
559 return 0; // FIXME: Proper error diagnostic?
560 }
561
Ted Kremenek72b1b152009-01-15 18:47:46 +0000562 // Get the location of the lexigraphically-sorted table of persistent IDs.
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000563 const unsigned char* SortedIdTableOffset = EndTable + sizeof(uint32_t)*2;
Chris Lattner5ff43172009-01-22 19:48:26 +0000564 const unsigned char* SortedIdTable = BufBeg + ReadLE32(SortedIdTableOffset);
Ted Kremenekcdd8f212009-01-21 07:34:28 +0000565 if (!(SortedIdTable >= BufBeg && SortedIdTable < BufEnd)) {
Ted Kremenek72b1b152009-01-15 18:47:46 +0000566 assert(false && "Invalid PTH file.");
567 return 0; // FIXME: Proper error diagnostic?
568 }
569
Ted Kremenek6183e482008-12-03 01:16:39 +0000570 // Get the number of IdentifierInfos and pre-allocate the identifier cache.
Chris Lattner5ff43172009-01-22 19:48:26 +0000571 uint32_t NumIds = ReadLE32(IData);
Ted Kremenekcdd8f212009-01-21 07:34:28 +0000572
Ted Kremenek6183e482008-12-03 01:16:39 +0000573 // Pre-allocate the peristent ID -> IdentifierInfo* cache. We use calloc()
574 // so that we in the best case only zero out memory once when the OS returns
575 // us new pages.
Ted Kremenekcdd8f212009-01-21 07:34:28 +0000576 IdentifierInfo** PerIDCache = 0;
Ted Kremenek6183e482008-12-03 01:16:39 +0000577
Ted Kremenekcdd8f212009-01-21 07:34:28 +0000578 if (NumIds) {
579 PerIDCache = (IdentifierInfo**)calloc(NumIds, sizeof(*PerIDCache));
580 if (!PerIDCache) {
581 assert(false && "Could not allocate Persistent ID cache.");
582 return 0;
583 }
Ted Kremenek6183e482008-12-03 01:16:39 +0000584 }
Ted Kremenekcdd8f212009-01-21 07:34:28 +0000585
Ted Kremenek72b1b152009-01-15 18:47:46 +0000586 // Create the new PTHManager.
587 return new PTHManager(File.take(), FL.take(), IData, PerIDCache,
588 SortedIdTable, NumIds);
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000589}
Chris Lattner77ecb3a2009-01-18 02:57:21 +0000590IdentifierInfo* PTHManager::LazilyCreateIdentifierInfo(unsigned PersistentID) {
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000591 // Look in the PTH file for the string data for the IdentifierInfo object.
Chris Lattner77ecb3a2009-01-18 02:57:21 +0000592 const unsigned char* TableEntry = IdDataTable + sizeof(uint32_t)*PersistentID;
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000593 const unsigned char* IDData =
Chris Lattner5ff43172009-01-22 19:48:26 +0000594 (const unsigned char*)Buf->getBufferStart() + ReadLE32(TableEntry);
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000595 assert(IDData < (const unsigned char*)Buf->getBufferEnd());
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000596
Ted Kremenek72b1b152009-01-15 18:47:46 +0000597 // Allocate the object.
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000598 std::pair<IdentifierInfo,const unsigned char*> *Mem =
599 Alloc.Allocate<std::pair<IdentifierInfo,const unsigned char*> >();
Ted Kremenek72b1b152009-01-15 18:47:46 +0000600
601 Mem->second = IDData;
Ted Kremenekea9c26b2009-01-20 23:28:34 +0000602 IdentifierInfo *II = new ((void*) Mem) IdentifierInfo();
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000603
Ted Kremenek72b1b152009-01-15 18:47:46 +0000604 // Store the new IdentifierInfo in the cache.
Chris Lattner77ecb3a2009-01-18 02:57:21 +0000605 PerIDCache[PersistentID] = II;
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000606 return II;
607}
608
Ted Kremenek72b1b152009-01-15 18:47:46 +0000609IdentifierInfo* PTHManager::get(const char *NameStart, const char *NameEnd) {
610 unsigned min = 0;
611 unsigned max = NumIds;
Chris Lattner1b5285e2009-01-18 02:10:31 +0000612 unsigned Len = NameEnd - NameStart;
Ted Kremenek72b1b152009-01-15 18:47:46 +0000613
614 do {
615 unsigned i = (max - min) / 2 + min;
Chris Lattner1b5285e2009-01-18 02:10:31 +0000616 const unsigned char *Ptr = SortedIdTable + (i * 4);
Ted Kremenek72b1b152009-01-15 18:47:46 +0000617
618 // Read the persistentID.
Chris Lattner5ff43172009-01-22 19:48:26 +0000619 unsigned perID = ReadLE32(Ptr);
Ted Kremenek72b1b152009-01-15 18:47:46 +0000620
621 // Get the IdentifierInfo.
622 IdentifierInfo* II = GetIdentifierInfo(perID);
623
624 // First compare the lengths.
625 unsigned IILen = II->getLength();
Chris Lattner1b5285e2009-01-18 02:10:31 +0000626 if (Len < IILen) goto IsLess;
627 if (Len > IILen) goto IsGreater;
Ted Kremenek72b1b152009-01-15 18:47:46 +0000628
629 // Now compare the strings!
630 {
Chris Lattner1b5285e2009-01-18 02:10:31 +0000631 signed comp = strncmp(NameStart, II->getName(), Len);
Ted Kremenek72b1b152009-01-15 18:47:46 +0000632 if (comp < 0) goto IsLess;
633 if (comp > 0) goto IsGreater;
634 }
635 // We found a match!
636 return II;
637
638 IsGreater:
639 if (i == min) break;
640 min = i;
641 continue;
642
643 IsLess:
644 max = i;
645 assert(!(max == min) || (min == i));
646 }
Ted Kremeneke1deaac2009-01-15 19:28:38 +0000647 while (min != max);
Ted Kremenek72b1b152009-01-15 18:47:46 +0000648
649 return 0;
650}
651
652
Chris Lattnerf056d922009-01-17 08:06:50 +0000653PTHLexer *PTHManager::CreateLexer(FileID FID) {
654 const FileEntry *FE = PP->getSourceManager().getFileEntryForID(FID);
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000655 if (!FE)
656 return 0;
657
658 // Lookup the FileEntry object in our file lookup data structure. It will
659 // return a variant that indicates whether or not there is an offset within
660 // the PTH file that contains cached tokens.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000661 PTHFileLookup::Val FileData = ((PTHFileLookup*)FileLookup)->Lookup(FE);
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000662
Ted Kremenekfb645b62008-12-11 23:36:38 +0000663 if (!FileData.isValid()) // No tokens available.
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000664 return 0;
665
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000666 const unsigned char *BufStart = (const unsigned char *)Buf->getBufferStart();
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000667 // Compute the offset of the token data within the buffer.
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000668 const unsigned char* data = BufStart + FileData.getTokenOffset();
Ted Kremenek268ee702008-12-12 18:34:08 +0000669
670 // Get the location of pp-conditional table.
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000671 const unsigned char* ppcond = BufStart + FileData.getPPCondOffset();
Chris Lattner5ff43172009-01-22 19:48:26 +0000672 uint32_t Len = ReadLE32(ppcond);
Chris Lattner1b5285e2009-01-18 02:10:31 +0000673 if (Len == 0) ppcond = 0;
Ted Kremenek32a8ad52009-01-08 04:30:32 +0000674
675 // Get the location of the spelling table.
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000676 const unsigned char* spellingTable = BufStart + FileData.getSpellingOffset();
Ted Kremenek32a8ad52009-01-08 04:30:32 +0000677
Chris Lattner5ff43172009-01-22 19:48:26 +0000678 Len = ReadLE32(spellingTable);
Chris Lattner1b5285e2009-01-18 02:10:31 +0000679 if (Len == 0) spellingTable = 0;
Ted Kremenekb70e3da2009-01-08 02:47:16 +0000680
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000681 assert(data < (const unsigned char*)Buf->getBufferEnd());
Ted Kremenek5f074262009-01-09 22:05:30 +0000682
683 // Create the SpellingSearch object for this FileID.
Chris Lattner1b5285e2009-01-18 02:10:31 +0000684 PTHSpellingSearch* ss = new PTHSpellingSearch(*this, Len, spellingTable);
Chris Lattner2b2453a2009-01-17 06:22:33 +0000685 SpellingMap[FID] = ss;
Ted Kremenek5f074262009-01-09 22:05:30 +0000686
Ted Kremenek72b1b152009-01-15 18:47:46 +0000687 assert(PP && "No preprocessor set yet!");
Chris Lattner2b2453a2009-01-17 06:22:33 +0000688 return new PTHLexer(*PP, FID, data, ppcond, *ss, *this);
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000689}