blob: 8d06ac6bbcaa6ff2e5f06cf0010cc46063b8199a [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"
22#include "llvm/Support/Compiler.h"
23#include "llvm/Support/MemoryBuffer.h"
24#include "llvm/ADT/StringMap.h"
25#include "llvm/ADT/OwningPtr.h"
Ted Kremenek274b2082008-11-12 21:37:15 +000026using namespace clang;
27
Ted Kremenek18d9afb2008-12-23 18:41:34 +000028#define DISK_TOKEN_SIZE (1+1+3+4+2)
Ted Kremenek268ee702008-12-12 18:34:08 +000029
Ted Kremenek0c6a77b2008-12-03 00:38:03 +000030//===----------------------------------------------------------------------===//
31// Utility methods for reading from the mmap'ed PTH file.
32//===----------------------------------------------------------------------===//
33
Chris Lattnerda9d61c2009-01-18 01:57:14 +000034static inline uint8_t Read8(const unsigned char *&Data) {
35 uint8_t V = Data[0];
36 Data += 1;
Ted Kremenek0c6a77b2008-12-03 00:38:03 +000037 return V;
38}
39
Chris Lattnerda9d61c2009-01-18 01:57:14 +000040static inline uint16_t Read16(const unsigned char *&Data) {
41 uint16_t V = ((uint16_t)Data[0] << 0) |
42 ((uint16_t)Data[1] << 8);
43 Data += 2;
44 return V;
45}
46
47static inline uint32_t Read24(const unsigned char *&Data) {
48 uint32_t V = ((uint32_t)Data[0] << 0) |
49 ((uint32_t)Data[1] << 8) |
50 ((uint32_t)Data[2] << 16);
51 Data += 3;
52 return V;
53}
54
55static inline uint32_t Read32(const unsigned char *&Data) {
56 uint32_t V = ((uint32_t)Data[0] << 0) |
57 ((uint32_t)Data[1] << 8) |
58 ((uint32_t)Data[2] << 16) |
59 ((uint32_t)Data[3] << 24);
60 Data += 4;
61 return V;
62}
63
64
Ted Kremeneke5680f32008-12-23 01:30:52 +000065//===----------------------------------------------------------------------===//
66// PTHLexer methods.
67//===----------------------------------------------------------------------===//
68
Chris Lattnerda9d61c2009-01-18 01:57:14 +000069PTHLexer::PTHLexer(Preprocessor &PP, FileID FID, const unsigned char *D,
70 const unsigned char *ppcond,
Chris Lattner2b2453a2009-01-17 06:22:33 +000071 PTHSpellingSearch &mySpellingSrch, PTHManager &PM)
72 : PreprocessorLexer(&PP, FID), TokBuf(D), CurPtr(D), LastHashTokPtr(0),
Ted Kremenek5f074262009-01-09 22:05:30 +000073 PPCond(ppcond), CurPPCondPtr(ppcond), MySpellingSrch(mySpellingSrch),
Chris Lattner2b2453a2009-01-17 06:22:33 +000074 PTHMgr(PM) {
75
76 FileStartLoc = PP.getSourceManager().getLocForStartOfFile(FID);
Ted Kremenek5f074262009-01-09 22:05:30 +000077}
Ted Kremeneke5680f32008-12-23 01:30:52 +000078
79void PTHLexer::Lex(Token& Tok) {
80LexNextToken:
Ted Kremeneke5680f32008-12-23 01:30:52 +000081
Ted Kremenek866bdf72008-12-23 02:30:15 +000082 //===--------------------------------------==//
83 // Read the raw token data.
84 //===--------------------------------------==//
85
86 // Shadow CurPtr into an automatic variable.
Chris Lattnerda9d61c2009-01-18 01:57:14 +000087 const unsigned char *CurPtrShadow = CurPtr;
Ted Kremenek866bdf72008-12-23 02:30:15 +000088
Chris Lattner1b5285e2009-01-18 02:10:31 +000089 // Read in the data for the token.
90 tok::TokenKind k = (tok::TokenKind) Read8(CurPtrShadow);
91 Token::TokenFlags flags = (Token::TokenFlags) Read8(CurPtrShadow);
92 uint32_t perID = Read24(CurPtrShadow);
93 uint32_t FileOffset = Read32(CurPtrShadow);
94 uint32_t Len = Read16(CurPtrShadow);
95 CurPtr = CurPtrShadow;
Ted Kremenek866bdf72008-12-23 02:30:15 +000096
97 //===--------------------------------------==//
98 // Construct the token itself.
99 //===--------------------------------------==//
100
101 Tok.startToken();
102 Tok.setKind(k);
Ted Kremenek59d08cb2008-12-23 19:24:24 +0000103 Tok.setFlag(flags);
104 assert(!LexingRawMode);
Ted Kremenek6b1c9702008-12-23 18:27:26 +0000105 Tok.setIdentifierInfo(perID ? PTHMgr.GetIdentifierInfo(perID-1) : 0);
Chris Lattner2b2453a2009-01-17 06:22:33 +0000106 Tok.setLocation(FileStartLoc.getFileLocWithOffset(FileOffset));
Ted Kremenek866bdf72008-12-23 02:30:15 +0000107 Tok.setLength(Len);
108
109 //===--------------------------------------==//
110 // Process the token.
111 //===--------------------------------------==//
Ted Kremenek5f074262009-01-09 22:05:30 +0000112#if 0
113 SourceManager& SM = PP->getSourceManager();
114 llvm::cerr << SM.getFileEntryForID(FileID)->getName()
115 << ':' << SM.getLogicalLineNumber(Tok.getLocation())
116 << ':' << SM.getLogicalColumnNumber(Tok.getLocation())
117 << '\n';
118#endif
Ted Kremeneke5680f32008-12-23 01:30:52 +0000119
Ted Kremenek59d08cb2008-12-23 19:24:24 +0000120 if (k == tok::identifier) {
121 MIOpt.ReadToken();
122 return PP->HandleIdentifier(Tok);
123 }
124
125 if (k == tok::eof) {
Ted Kremeneke5680f32008-12-23 01:30:52 +0000126 // Save the end-of-file token.
127 EofToken = Tok;
128
129 Preprocessor *PPCache = PP;
Ted Kremenek59d08cb2008-12-23 19:24:24 +0000130
131 assert(!ParsingPreprocessorDirective);
132 assert(!LexingRawMode);
133
134 // FIXME: Issue diagnostics similar to Lexer.
135 if (PP->HandleEndOfFile(Tok, false))
Ted Kremeneke5680f32008-12-23 01:30:52 +0000136 return;
Ted Kremenek59d08cb2008-12-23 19:24:24 +0000137
Ted Kremeneke5680f32008-12-23 01:30:52 +0000138 assert(PPCache && "Raw buffer::LexEndOfFile should return a token");
139 return PPCache->Lex(Tok);
140 }
Ted Kremeneke5680f32008-12-23 01:30:52 +0000141
Ted Kremenek59d08cb2008-12-23 19:24:24 +0000142 if (k == tok::hash && Tok.isAtStartOfLine()) {
143 LastHashTokPtr = CurPtr - DISK_TOKEN_SIZE;
144 assert(!LexingRawMode);
145 PP->HandleDirective(Tok);
146
147 if (PP->isCurrentLexer(this))
148 goto LexNextToken;
149
150 return PP->Lex(Tok);
151 }
152
153 if (k == tok::eom) {
154 assert(ParsingPreprocessorDirective);
Ted Kremeneke5680f32008-12-23 01:30:52 +0000155 ParsingPreprocessorDirective = false;
156 return;
157 }
Ted Kremeneke5680f32008-12-23 01:30:52 +0000158
Ted Kremenek59d08cb2008-12-23 19:24:24 +0000159 MIOpt.ReadToken();
Ted Kremeneke5680f32008-12-23 01:30:52 +0000160}
161
162// FIXME: We can just grab the last token instead of storing a copy
163// into EofToken.
Ted Kremenek59d08cb2008-12-23 19:24:24 +0000164void PTHLexer::getEOF(Token& Tok) {
Ted Kremenekdefb7092009-01-09 00:36:11 +0000165 assert(EofToken.is(tok::eof));
Ted Kremeneke5680f32008-12-23 01:30:52 +0000166 Tok = EofToken;
167}
168
169void PTHLexer::DiscardToEndOfLine() {
170 assert(ParsingPreprocessorDirective && ParsingFilename == false &&
171 "Must be in a preprocessing directive!");
172
173 // We assume that if the preprocessor wishes to discard to the end of
174 // the line that it also means to end the current preprocessor directive.
175 ParsingPreprocessorDirective = false;
176
177 // Skip tokens by only peeking at their token kind and the flags.
178 // We don't need to actually reconstruct full tokens from the token buffer.
179 // This saves some copies and it also reduces IdentifierInfo* lookup.
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000180 const unsigned char* p = CurPtr;
Ted Kremeneke5680f32008-12-23 01:30:52 +0000181 while (1) {
182 // Read the token kind. Are we at the end of the file?
183 tok::TokenKind x = (tok::TokenKind) (uint8_t) *p;
184 if (x == tok::eof) break;
185
186 // Read the token flags. Are we at the start of the next line?
187 Token::TokenFlags y = (Token::TokenFlags) (uint8_t) p[1];
188 if (y & Token::StartOfLine) break;
189
190 // Skip to the next token.
191 p += DISK_TOKEN_SIZE;
192 }
193
194 CurPtr = p;
195}
196
Ted Kremenek268ee702008-12-12 18:34:08 +0000197/// SkipBlock - Used by Preprocessor to skip the current conditional block.
198bool PTHLexer::SkipBlock() {
199 assert(CurPPCondPtr && "No cached PP conditional information.");
200 assert(LastHashTokPtr && "No known '#' token.");
201
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000202 const unsigned char* HashEntryI = 0;
Ted Kremenek268ee702008-12-12 18:34:08 +0000203 uint32_t Offset;
204 uint32_t TableIdx;
205
206 do {
Ted Kremenek41a26602008-12-12 22:05:38 +0000207 // Read the token offset from the side-table.
Ted Kremenek268ee702008-12-12 18:34:08 +0000208 Offset = Read32(CurPPCondPtr);
Ted Kremenek41a26602008-12-12 22:05:38 +0000209
210 // Read the target table index from the side-table.
Ted Kremenek268ee702008-12-12 18:34:08 +0000211 TableIdx = Read32(CurPPCondPtr);
Ted Kremenek41a26602008-12-12 22:05:38 +0000212
213 // Compute the actual memory address of the '#' token data for this entry.
214 HashEntryI = TokBuf + Offset;
215
216 // Optmization: "Sibling jumping". #if...#else...#endif blocks can
217 // contain nested blocks. In the side-table we can jump over these
218 // nested blocks instead of doing a linear search if the next "sibling"
219 // entry is not at a location greater than LastHashTokPtr.
220 if (HashEntryI < LastHashTokPtr && TableIdx) {
221 // In the side-table we are still at an entry for a '#' token that
222 // is earlier than the last one we saw. Check if the location we would
223 // stride gets us closer.
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000224 const unsigned char* NextPPCondPtr =
225 PPCond + TableIdx*(sizeof(uint32_t)*2);
Ted Kremenek41a26602008-12-12 22:05:38 +0000226 assert(NextPPCondPtr >= CurPPCondPtr);
227 // Read where we should jump to.
228 uint32_t TmpOffset = Read32(NextPPCondPtr);
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000229 const unsigned char* HashEntryJ = TokBuf + TmpOffset;
Ted Kremenek41a26602008-12-12 22:05:38 +0000230
231 if (HashEntryJ <= LastHashTokPtr) {
232 // Jump directly to the next entry in the side table.
233 HashEntryI = HashEntryJ;
234 Offset = TmpOffset;
235 TableIdx = Read32(NextPPCondPtr);
236 CurPPCondPtr = NextPPCondPtr;
237 }
238 }
Ted Kremenek268ee702008-12-12 18:34:08 +0000239 }
Ted Kremenek41a26602008-12-12 22:05:38 +0000240 while (HashEntryI < LastHashTokPtr);
241 assert(HashEntryI == LastHashTokPtr && "No PP-cond entry found for '#'");
Ted Kremenek268ee702008-12-12 18:34:08 +0000242 assert(TableIdx && "No jumping from #endifs.");
243
244 // Update our side-table iterator.
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000245 const unsigned char* NextPPCondPtr = PPCond + TableIdx*(sizeof(uint32_t)*2);
Ted Kremenek268ee702008-12-12 18:34:08 +0000246 assert(NextPPCondPtr >= CurPPCondPtr);
247 CurPPCondPtr = NextPPCondPtr;
248
249 // Read where we should jump to.
Ted Kremenek41a26602008-12-12 22:05:38 +0000250 HashEntryI = TokBuf + Read32(NextPPCondPtr);
Ted Kremenek268ee702008-12-12 18:34:08 +0000251 uint32_t NextIdx = Read32(NextPPCondPtr);
252
253 // By construction NextIdx will be zero if this is a #endif. This is useful
254 // to know to obviate lexing another token.
255 bool isEndif = NextIdx == 0;
Ted Kremenek268ee702008-12-12 18:34:08 +0000256
257 // This case can occur when we see something like this:
258 //
259 // #if ...
260 // /* a comment or nothing */
261 // #elif
262 //
263 // If we are skipping the first #if block it will be the case that CurPtr
264 // already points 'elif'. Just return.
265
Ted Kremenek41a26602008-12-12 22:05:38 +0000266 if (CurPtr > HashEntryI) {
267 assert(CurPtr == HashEntryI + DISK_TOKEN_SIZE);
Ted Kremenek268ee702008-12-12 18:34:08 +0000268 // Did we reach a #endif? If so, go ahead and consume that token as well.
269 if (isEndif)
Ted Kremeneke5680f32008-12-23 01:30:52 +0000270 CurPtr += DISK_TOKEN_SIZE*2;
Ted Kremenek268ee702008-12-12 18:34:08 +0000271 else
Ted Kremenek41a26602008-12-12 22:05:38 +0000272 LastHashTokPtr = HashEntryI;
Ted Kremenek268ee702008-12-12 18:34:08 +0000273
274 return isEndif;
275 }
276
277 // Otherwise, we need to advance. Update CurPtr to point to the '#' token.
Ted Kremenek41a26602008-12-12 22:05:38 +0000278 CurPtr = HashEntryI;
Ted Kremenek268ee702008-12-12 18:34:08 +0000279
280 // Update the location of the last observed '#'. This is useful if we
281 // are skipping multiple blocks.
282 LastHashTokPtr = CurPtr;
Ted Kremenek268ee702008-12-12 18:34:08 +0000283
Ted Kremeneke5680f32008-12-23 01:30:52 +0000284 // Skip the '#' token.
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000285 assert(((tok::TokenKind)*CurPtr) == tok::hash);
Ted Kremeneke5680f32008-12-23 01:30:52 +0000286 CurPtr += DISK_TOKEN_SIZE;
287
Ted Kremenek268ee702008-12-12 18:34:08 +0000288 // Did we reach a #endif? If so, go ahead and consume that token as well.
Ted Kremeneke5680f32008-12-23 01:30:52 +0000289 if (isEndif) { CurPtr += DISK_TOKEN_SIZE*2; }
Ted Kremenek268ee702008-12-12 18:34:08 +0000290
291 return isEndif;
292}
293
Ted Kremenek30a12ec2008-12-17 23:36:32 +0000294SourceLocation PTHLexer::getSourceLocation() {
Chris Lattner1b5285e2009-01-18 02:10:31 +0000295 // getSourceLocation is not on the hot path. It is used to get the location
296 // of the next token when transitioning back to this lexer when done
Ted Kremenek30a12ec2008-12-17 23:36:32 +0000297 // handling a #included file. Just read the necessary data from the token
298 // data buffer to construct the SourceLocation object.
299 // NOTE: This is a virtual function; hence it is defined out-of-line.
Chris Lattner1b5285e2009-01-18 02:10:31 +0000300 const unsigned char *OffsetPtr = CurPtr + (1 + 1 + 3);
301 uint32_t Offset = Read32(OffsetPtr);
302 return FileStartLoc.getFileLocWithOffset(Offset);
Ted Kremenek30a12ec2008-12-17 23:36:32 +0000303}
304
Ted Kremenek5f074262009-01-09 22:05:30 +0000305//===----------------------------------------------------------------------===//
306// getSpelling() - Use cached data in PTH files for getSpelling().
307//===----------------------------------------------------------------------===//
308
Chris Lattner2b2453a2009-01-17 06:22:33 +0000309unsigned PTHManager::getSpelling(FileID FID, unsigned FPos,
310 const char *&Buffer) {
311 llvm::DenseMap<FileID, PTHSpellingSearch*>::iterator I =SpellingMap.find(FID);
Ted Kremenek5f074262009-01-09 22:05:30 +0000312
313 if (I == SpellingMap.end())
Chris Lattner1b5285e2009-01-18 02:10:31 +0000314 return 0;
Ted Kremenekf02f6f02009-01-13 22:05:50 +0000315
Chris Lattner2b2453a2009-01-17 06:22:33 +0000316 return I->second->getSpellingBinarySearch(FPos, Buffer);
317}
318
319unsigned PTHManager::getSpelling(SourceLocation Loc, const char *&Buffer) {
Chris Lattner6b7b8402009-01-17 06:29:33 +0000320 SourceManager &SM = PP->getSourceManager();
321 Loc = SM.getSpellingLoc(Loc);
322 std::pair<FileID, unsigned> LocInfo = SM.getDecomposedFileLoc(Loc);
Chris Lattner2b2453a2009-01-17 06:22:33 +0000323 return getSpelling(LocInfo.first, LocInfo.second, Buffer);
Ted Kremenek5f074262009-01-09 22:05:30 +0000324}
325
326unsigned PTHManager::getSpellingAtPTHOffset(unsigned PTHOffset,
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000327 const char *&Buffer) {
Chris Lattner1b5285e2009-01-18 02:10:31 +0000328 assert(PTHOffset < Buf->getBufferSize());
329 const unsigned char* Ptr =
330 (const unsigned char*)Buf->getBufferStart() + PTHOffset;
Ted Kremenek32a8ad52009-01-08 04:30:32 +0000331
332 // The string is prefixed by 16 bits for its length, followed by the string
333 // itself.
Chris Lattner1b5285e2009-01-18 02:10:31 +0000334 unsigned Len = Read16(Ptr);
335 Buffer = (const char *)Ptr;
336 return Len;
Ted Kremenek32a8ad52009-01-08 04:30:32 +0000337}
338
Chris Lattner1b5285e2009-01-18 02:10:31 +0000339unsigned PTHSpellingSearch::getSpellingLinearSearch(unsigned FPos,
Ted Kremenek5f074262009-01-09 22:05:30 +0000340 const char *&Buffer) {
Chris Lattner1b5285e2009-01-18 02:10:31 +0000341 const unsigned char *Ptr = LinearItr;
342 unsigned Len = 0;
Ted Kremenek5f074262009-01-09 22:05:30 +0000343
Chris Lattner1b5285e2009-01-18 02:10:31 +0000344 if (Ptr == TableEnd)
345 return getSpellingBinarySearch(FPos, Buffer);
Ted Kremenek5f074262009-01-09 22:05:30 +0000346
347 do {
Chris Lattner1b5285e2009-01-18 02:10:31 +0000348 uint32_t TokOffset = Read32(Ptr);
Ted Kremenek32a8ad52009-01-08 04:30:32 +0000349
Chris Lattner1b5285e2009-01-18 02:10:31 +0000350 if (TokOffset > FPos)
351 return getSpellingBinarySearch(FPos, Buffer);
Ted Kremenek32a8ad52009-01-08 04:30:32 +0000352
Ted Kremenek32a8ad52009-01-08 04:30:32 +0000353 // Did we find a matching token offset for this spelling?
Chris Lattner1b5285e2009-01-18 02:10:31 +0000354 if (TokOffset == FPos) {
355 uint32_t SpellingPTHOffset = Read32(Ptr);
356 Len = PTHMgr.getSpellingAtPTHOffset(SpellingPTHOffset, Buffer);
Ted Kremenek32a8ad52009-01-08 04:30:32 +0000357 break;
358 }
Chris Lattner1b5285e2009-01-18 02:10:31 +0000359 } while (Ptr != TableEnd);
Ted Kremenek32a8ad52009-01-08 04:30:32 +0000360
Chris Lattner1b5285e2009-01-18 02:10:31 +0000361 LinearItr = Ptr;
362 return Len;
Ted Kremenekb70e3da2009-01-08 02:47:16 +0000363}
364
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000365
Chris Lattner1b5285e2009-01-18 02:10:31 +0000366unsigned PTHSpellingSearch::getSpellingBinarySearch(unsigned FPos,
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000367 const char *&Buffer) {
Ted Kremenek5f074262009-01-09 22:05:30 +0000368
Ted Kremenekf02f6f02009-01-13 22:05:50 +0000369 assert((TableEnd - TableBeg) % SpellingEntrySize == 0);
Chris Lattner1b5285e2009-01-18 02:10:31 +0000370 assert(TableEnd >= TableBeg);
Ted Kremenekf02f6f02009-01-13 22:05:50 +0000371
372 if (TableEnd == TableBeg)
373 return 0;
374
Ted Kremenek5f074262009-01-09 22:05:30 +0000375 unsigned min = 0;
Chris Lattner1b5285e2009-01-18 02:10:31 +0000376 const unsigned char *tb = TableBeg;
Ted Kremenekf02f6f02009-01-13 22:05:50 +0000377 unsigned max = NumSpellings;
Ted Kremenek5f074262009-01-09 22:05:30 +0000378
Ted Kremenekf02f6f02009-01-13 22:05:50 +0000379 do {
Ted Kremenek5f074262009-01-09 22:05:30 +0000380 unsigned i = (max - min) / 2 + min;
Chris Lattner1b5285e2009-01-18 02:10:31 +0000381 const unsigned char *Ptr = tb + (i * SpellingEntrySize);
Ted Kremenek5f074262009-01-09 22:05:30 +0000382
Chris Lattner1b5285e2009-01-18 02:10:31 +0000383 uint32_t TokOffset = Read32(Ptr);
384 if (TokOffset > FPos) {
Ted Kremenek5f074262009-01-09 22:05:30 +0000385 max = i;
Ted Kremenekf02f6f02009-01-13 22:05:50 +0000386 assert(!(max == min) || (min == i));
Ted Kremenek5f074262009-01-09 22:05:30 +0000387 continue;
388 }
389
Chris Lattner1b5285e2009-01-18 02:10:31 +0000390 if (TokOffset < FPos) {
Ted Kremenek7bd88aa2009-01-13 22:16:45 +0000391 if (i == min)
392 break;
393
Ted Kremenek5f074262009-01-09 22:05:30 +0000394 min = i;
395 continue;
396 }
397
Chris Lattner1b5285e2009-01-18 02:10:31 +0000398 uint32_t SpellingPTHOffset = Read32(Ptr);
Ted Kremenek5f074262009-01-09 22:05:30 +0000399 return PTHMgr.getSpellingAtPTHOffset(SpellingPTHOffset, Buffer);
400 }
Ted Kremenekf02f6f02009-01-13 22:05:50 +0000401 while (min != max);
Ted Kremenek5f074262009-01-09 22:05:30 +0000402
403 return 0;
404}
405
Chris Lattner2b2453a2009-01-17 06:22:33 +0000406unsigned PTHLexer::getSpelling(SourceLocation Loc, const char *&Buffer) {
407 SourceManager &SM = PP->getSourceManager();
408 Loc = SM.getSpellingLoc(Loc);
409 std::pair<FileID, unsigned> LocInfo = SM.getDecomposedFileLoc(Loc);
410
411 FileID FID = LocInfo.first;
412 unsigned FPos = LocInfo.second;
Ted Kremenek5f074262009-01-09 22:05:30 +0000413
Chris Lattner2b2453a2009-01-17 06:22:33 +0000414 if (FID == getFileID())
415 return MySpellingSrch.getSpellingLinearSearch(FPos, Buffer);
416 return PTHMgr.getSpelling(FID, FPos, Buffer);
Ted Kremenek5f074262009-01-09 22:05:30 +0000417}
418
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000419//===----------------------------------------------------------------------===//
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000420// Internal Data Structures for PTH file lookup and resolving identifiers.
421//===----------------------------------------------------------------------===//
422
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000423
424/// PTHFileLookup - This internal data structure is used by the PTHManager
425/// to map from FileEntry objects managed by FileManager to offsets within
426/// the PTH file.
427namespace {
428class VISIBILITY_HIDDEN PTHFileLookup {
429public:
430 class Val {
Ted Kremenekfb645b62008-12-11 23:36:38 +0000431 uint32_t TokenOff;
432 uint32_t PPCondOff;
Ted Kremenekb70e3da2009-01-08 02:47:16 +0000433 uint32_t SpellingOff;
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000434 public:
Ted Kremenekfb645b62008-12-11 23:36:38 +0000435 Val() : TokenOff(~0) {}
Ted Kremenekb70e3da2009-01-08 02:47:16 +0000436 Val(uint32_t toff, uint32_t poff, uint32_t soff)
437 : TokenOff(toff), PPCondOff(poff), SpellingOff(soff) {}
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000438
Chris Lattner1b5285e2009-01-18 02:10:31 +0000439 bool isValid() const { return TokenOff != ~((uint32_t)0); }
440
Ted Kremenekfb645b62008-12-11 23:36:38 +0000441 uint32_t getTokenOffset() const {
Chris Lattner1b5285e2009-01-18 02:10:31 +0000442 assert(isValid() && "PTHFileLookup entry initialized.");
Ted Kremenekfb645b62008-12-11 23:36:38 +0000443 return TokenOff;
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000444 }
445
Ted Kremenekb70e3da2009-01-08 02:47:16 +0000446 uint32_t getPPCondOffset() const {
Chris Lattner1b5285e2009-01-18 02:10:31 +0000447 assert(isValid() && "PTHFileLookup entry initialized.");
Ted Kremenekfb645b62008-12-11 23:36:38 +0000448 return PPCondOff;
449 }
450
Ted Kremenekb70e3da2009-01-08 02:47:16 +0000451 uint32_t getSpellingOffset() const {
Chris Lattner1b5285e2009-01-18 02:10:31 +0000452 assert(isValid() && "PTHFileLookup entry initialized.");
Ted Kremenekb70e3da2009-01-08 02:47:16 +0000453 return SpellingOff;
454 }
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000455 };
456
457private:
458 llvm::StringMap<Val> FileMap;
459
460public:
461 PTHFileLookup() {};
462
463 Val Lookup(const FileEntry* FE) {
464 const char* s = FE->getName();
465 unsigned size = strlen(s);
466 return FileMap.GetOrCreateValue(s, s+size).getValue();
467 }
468
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000469 void ReadTable(const unsigned char* D) {
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000470 uint32_t N = Read32(D); // Read the length of the table.
471
472 for ( ; N > 0; --N) { // The rest of the data is the table itself.
Chris Lattner1b5285e2009-01-18 02:10:31 +0000473 uint32_t Len = Read32(D);
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000474 const char* s = (const char *)D;
Chris Lattner1b5285e2009-01-18 02:10:31 +0000475 D += Len;
Ted Kremenekb70e3da2009-01-08 02:47:16 +0000476
Ted Kremenekfb645b62008-12-11 23:36:38 +0000477 uint32_t TokenOff = Read32(D);
Ted Kremenekb70e3da2009-01-08 02:47:16 +0000478 uint32_t PPCondOff = Read32(D);
479 uint32_t SpellingOff = Read32(D);
480
Chris Lattner1b5285e2009-01-18 02:10:31 +0000481 FileMap.GetOrCreateValue(s, s+Len).getValue() =
Ted Kremenekb70e3da2009-01-08 02:47:16 +0000482 Val(TokenOff, PPCondOff, SpellingOff);
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000483 }
484 }
485};
486} // end anonymous namespace
487
488//===----------------------------------------------------------------------===//
489// PTHManager methods.
490//===----------------------------------------------------------------------===//
491
492PTHManager::PTHManager(const llvm::MemoryBuffer* buf, void* fileLookup,
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000493 const unsigned char* idDataTable,
494 IdentifierInfo** perIDCache,
495 const unsigned char* sortedIdTable, unsigned numIds)
Ted Kremenek6183e482008-12-03 01:16:39 +0000496: Buf(buf), PerIDCache(perIDCache), FileLookup(fileLookup),
Ted Kremenek72b1b152009-01-15 18:47:46 +0000497 IdDataTable(idDataTable), SortedIdTable(sortedIdTable),
498 NumIds(numIds), PP(0) {}
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000499
500PTHManager::~PTHManager() {
501 delete Buf;
502 delete (PTHFileLookup*) FileLookup;
Ted Kremenek0e50b6e2008-12-04 22:47:11 +0000503 free(PerIDCache);
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000504}
505
Ted Kremenek72b1b152009-01-15 18:47:46 +0000506PTHManager* PTHManager::Create(const std::string& file) {
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000507 // Memory map the PTH file.
508 llvm::OwningPtr<llvm::MemoryBuffer>
509 File(llvm::MemoryBuffer::getFile(file.c_str()));
510
511 if (!File)
512 return 0;
513
514 // Get the buffer ranges and check if there are at least three 32-bit
515 // words at the end of the file.
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000516 const unsigned char* BufBeg = (unsigned char*)File->getBufferStart();
517 const unsigned char* BufEnd = (unsigned char*)File->getBufferEnd();
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000518
519 if(!(BufEnd > BufBeg + sizeof(uint32_t)*3)) {
520 assert(false && "Invalid PTH file.");
521 return 0; // FIXME: Proper error diagnostic?
522 }
523
524 // Compute the address of the index table at the end of the PTH file.
525 // This table contains the offset of the file lookup table, the
526 // persistent ID -> identifer data table.
Ted Kremenek293b4af2009-01-15 01:26:25 +0000527 // FIXME: We should just embed this offset in the PTH file.
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000528 const unsigned char* EndTable = BufEnd - sizeof(uint32_t)*4;
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000529
530 // Construct the file lookup table. This will be used for mapping from
531 // FileEntry*'s to cached tokens.
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000532 const unsigned char* FileTableOffset = EndTable + sizeof(uint32_t)*3;
533 const unsigned char* FileTable = BufBeg + Read32(FileTableOffset);
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000534
535 if (!(FileTable > BufBeg && FileTable < BufEnd)) {
536 assert(false && "Invalid PTH file.");
537 return 0; // FIXME: Proper error diagnostic?
538 }
539
540 llvm::OwningPtr<PTHFileLookup> FL(new PTHFileLookup());
541 FL->ReadTable(FileTable);
542
543 // Get the location of the table mapping from persistent ids to the
544 // data needed to reconstruct identifiers.
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000545 const unsigned char* IDTableOffset = EndTable + sizeof(uint32_t)*1;
546 const unsigned char* IData = BufBeg + Read32(IDTableOffset);
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000547 if (!(IData > BufBeg && IData < BufEnd)) {
548 assert(false && "Invalid PTH file.");
549 return 0; // FIXME: Proper error diagnostic?
550 }
551
Ted Kremenek72b1b152009-01-15 18:47:46 +0000552 // Get the location of the lexigraphically-sorted table of persistent IDs.
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000553 const unsigned char* SortedIdTableOffset = EndTable + sizeof(uint32_t)*2;
554 const unsigned char* SortedIdTable = BufBeg + Read32(SortedIdTableOffset);
Ted Kremenek72b1b152009-01-15 18:47:46 +0000555 if (!(SortedIdTable > BufBeg && SortedIdTable < BufEnd)) {
556 assert(false && "Invalid PTH file.");
557 return 0; // FIXME: Proper error diagnostic?
558 }
559
Ted Kremenek6183e482008-12-03 01:16:39 +0000560 // Get the number of IdentifierInfos and pre-allocate the identifier cache.
561 uint32_t NumIds = Read32(IData);
562
563 // Pre-allocate the peristent ID -> IdentifierInfo* cache. We use calloc()
564 // so that we in the best case only zero out memory once when the OS returns
565 // us new pages.
566 IdentifierInfo** PerIDCache =
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000567 (IdentifierInfo**)calloc(NumIds, sizeof(*PerIDCache));
Ted Kremenek6183e482008-12-03 01:16:39 +0000568
569 if (!PerIDCache) {
570 assert(false && "Could not allocate Persistent ID cache.");
571 return 0;
572 }
573
Ted Kremenek72b1b152009-01-15 18:47:46 +0000574 // Create the new PTHManager.
575 return new PTHManager(File.take(), FL.take(), IData, PerIDCache,
576 SortedIdTable, NumIds);
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000577}
578
Ted Kremenek866bdf72008-12-23 02:30:15 +0000579IdentifierInfo* PTHManager::GetIdentifierInfo(unsigned persistentID) {
580
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000581 // Check if the IdentifierInfo has already been resolved.
Ted Kremenek72b1b152009-01-15 18:47:46 +0000582 IdentifierInfo* II = PerIDCache[persistentID];
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000583 if (II) return II;
584
585 // Look in the PTH file for the string data for the IdentifierInfo object.
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000586 const unsigned char* TableEntry = IdDataTable + sizeof(uint32_t)*persistentID;
587 const unsigned char* IDData =
588 (const unsigned char*)Buf->getBufferStart() + Read32(TableEntry);
589 assert(IDData < (const unsigned char*)Buf->getBufferEnd());
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000590
Ted Kremenek72b1b152009-01-15 18:47:46 +0000591 // Allocate the object.
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000592 std::pair<IdentifierInfo,const unsigned char*> *Mem =
593 Alloc.Allocate<std::pair<IdentifierInfo,const unsigned char*> >();
Ted Kremenek72b1b152009-01-15 18:47:46 +0000594
595 Mem->second = IDData;
596 II = new ((void*) Mem) IdentifierInfo(true);
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000597
Ted Kremenek72b1b152009-01-15 18:47:46 +0000598 // Store the new IdentifierInfo in the cache.
599 PerIDCache[persistentID] = II;
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000600 return II;
601}
602
Ted Kremenek72b1b152009-01-15 18:47:46 +0000603IdentifierInfo* PTHManager::get(const char *NameStart, const char *NameEnd) {
604 unsigned min = 0;
605 unsigned max = NumIds;
Chris Lattner1b5285e2009-01-18 02:10:31 +0000606 unsigned Len = NameEnd - NameStart;
Ted Kremenek72b1b152009-01-15 18:47:46 +0000607
608 do {
609 unsigned i = (max - min) / 2 + min;
Chris Lattner1b5285e2009-01-18 02:10:31 +0000610 const unsigned char *Ptr = SortedIdTable + (i * 4);
Ted Kremenek72b1b152009-01-15 18:47:46 +0000611
612 // Read the persistentID.
Chris Lattner1b5285e2009-01-18 02:10:31 +0000613 unsigned perID = Read32(Ptr);
Ted Kremenek72b1b152009-01-15 18:47:46 +0000614
615 // Get the IdentifierInfo.
616 IdentifierInfo* II = GetIdentifierInfo(perID);
617
618 // First compare the lengths.
619 unsigned IILen = II->getLength();
Chris Lattner1b5285e2009-01-18 02:10:31 +0000620 if (Len < IILen) goto IsLess;
621 if (Len > IILen) goto IsGreater;
Ted Kremenek72b1b152009-01-15 18:47:46 +0000622
623 // Now compare the strings!
624 {
Chris Lattner1b5285e2009-01-18 02:10:31 +0000625 signed comp = strncmp(NameStart, II->getName(), Len);
Ted Kremenek72b1b152009-01-15 18:47:46 +0000626 if (comp < 0) goto IsLess;
627 if (comp > 0) goto IsGreater;
628 }
629 // We found a match!
630 return II;
631
632 IsGreater:
633 if (i == min) break;
634 min = i;
635 continue;
636
637 IsLess:
638 max = i;
639 assert(!(max == min) || (min == i));
640 }
Ted Kremeneke1deaac2009-01-15 19:28:38 +0000641 while (min != max);
Ted Kremenek72b1b152009-01-15 18:47:46 +0000642
643 return 0;
644}
645
646
Chris Lattnerf056d922009-01-17 08:06:50 +0000647PTHLexer *PTHManager::CreateLexer(FileID FID) {
648 const FileEntry *FE = PP->getSourceManager().getFileEntryForID(FID);
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000649 if (!FE)
650 return 0;
651
652 // Lookup the FileEntry object in our file lookup data structure. It will
653 // return a variant that indicates whether or not there is an offset within
654 // the PTH file that contains cached tokens.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000655 PTHFileLookup::Val FileData = ((PTHFileLookup*)FileLookup)->Lookup(FE);
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000656
Ted Kremenekfb645b62008-12-11 23:36:38 +0000657 if (!FileData.isValid()) // No tokens available.
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000658 return 0;
659
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000660 const unsigned char *BufStart = (const unsigned char *)Buf->getBufferStart();
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000661 // Compute the offset of the token data within the buffer.
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000662 const unsigned char* data = BufStart + FileData.getTokenOffset();
Ted Kremenek268ee702008-12-12 18:34:08 +0000663
664 // Get the location of pp-conditional table.
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000665 const unsigned char* ppcond = BufStart + FileData.getPPCondOffset();
Chris Lattner1b5285e2009-01-18 02:10:31 +0000666 uint32_t Len = Read32(ppcond);
667 if (Len == 0) ppcond = 0;
Ted Kremenek32a8ad52009-01-08 04:30:32 +0000668
669 // Get the location of the spelling table.
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000670 const unsigned char* spellingTable = BufStart + FileData.getSpellingOffset();
Ted Kremenek32a8ad52009-01-08 04:30:32 +0000671
Chris Lattner1b5285e2009-01-18 02:10:31 +0000672 Len = Read32(spellingTable);
673 if (Len == 0) spellingTable = 0;
Ted Kremenekb70e3da2009-01-08 02:47:16 +0000674
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000675 assert(data < (const unsigned char*)Buf->getBufferEnd());
Ted Kremenek5f074262009-01-09 22:05:30 +0000676
677 // Create the SpellingSearch object for this FileID.
Chris Lattner1b5285e2009-01-18 02:10:31 +0000678 PTHSpellingSearch* ss = new PTHSpellingSearch(*this, Len, spellingTable);
Chris Lattner2b2453a2009-01-17 06:22:33 +0000679 SpellingMap[FID] = ss;
Ted Kremenek5f074262009-01-09 22:05:30 +0000680
Ted Kremenek72b1b152009-01-15 18:47:46 +0000681 assert(PP && "No preprocessor set yet!");
Chris Lattner2b2453a2009-01-17 06:22:33 +0000682 return new PTHLexer(*PP, FID, data, ppcond, *ss, *this);
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000683}