blob: 81349171fe8bc1ff15450bea1ab1d8d35c706426 [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 Kremenek7b78b7c2009-01-19 23:13:15 +000028#define DISK_TOKEN_SIZE (1+1+2+4+4)
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) {
Chris Lattnerf15674c2009-01-18 02:19:16 +000041// Targets that directly support unaligned little-endian 16-bit loads can just
42// use them.
43#if defined(__i386__) || defined(__x86_64__)
44 uint16_t V = *((uint16_t*)Data);
45#else
Chris Lattnerda9d61c2009-01-18 01:57:14 +000046 uint16_t V = ((uint16_t)Data[0] << 0) |
47 ((uint16_t)Data[1] << 8);
Chris Lattnerf15674c2009-01-18 02:19:16 +000048#endif
Chris Lattnerda9d61c2009-01-18 01:57:14 +000049 Data += 2;
50 return V;
51}
52
Chris Lattneraff6ef82009-01-21 07:21:56 +000053static inline uint32_t Read24(const unsigned char *&Data) {
54// Targets that directly support unaligned little-endian 16-bit loads can just
55// use them.
56#if defined(__i386__) || defined(__x86_64__)
57 uint32_t V = ((uint16_t*)Data)[0] |
58 ((uint32_t)Data[2] << 16);
59#else
60 uint32_t V = ((uint32_t)Data[0] << 0) |
61 ((uint32_t)Data[1] << 8) |
62 ((uint32_t)Data[2] << 16);
63#endif
64
65 Data += 3;
66 return V;
67}
68
Chris Lattnerda9d61c2009-01-18 01:57:14 +000069static inline uint32_t Read32(const unsigned char *&Data) {
Chris Lattnerf15674c2009-01-18 02:19:16 +000070// Targets that directly support unaligned little-endian 32-bit loads can just
71// use them.
72#if defined(__i386__) || defined(__x86_64__)
73 uint32_t V = *((uint32_t*)Data);
74#else
Chris Lattnerda9d61c2009-01-18 01:57:14 +000075 uint32_t V = ((uint32_t)Data[0] << 0) |
76 ((uint32_t)Data[1] << 8) |
77 ((uint32_t)Data[2] << 16) |
78 ((uint32_t)Data[3] << 24);
Chris Lattnerf15674c2009-01-18 02:19:16 +000079#endif
Chris Lattnerda9d61c2009-01-18 01:57:14 +000080 Data += 4;
81 return V;
82}
83
84
Ted Kremeneke5680f32008-12-23 01:30:52 +000085//===----------------------------------------------------------------------===//
86// PTHLexer methods.
87//===----------------------------------------------------------------------===//
88
Chris Lattnerda9d61c2009-01-18 01:57:14 +000089PTHLexer::PTHLexer(Preprocessor &PP, FileID FID, const unsigned char *D,
90 const unsigned char *ppcond,
Chris Lattner2b2453a2009-01-17 06:22:33 +000091 PTHSpellingSearch &mySpellingSrch, PTHManager &PM)
92 : PreprocessorLexer(&PP, FID), TokBuf(D), CurPtr(D), LastHashTokPtr(0),
Ted Kremenek5f074262009-01-09 22:05:30 +000093 PPCond(ppcond), CurPPCondPtr(ppcond), MySpellingSrch(mySpellingSrch),
Chris Lattner2b2453a2009-01-17 06:22:33 +000094 PTHMgr(PM) {
95
96 FileStartLoc = PP.getSourceManager().getLocForStartOfFile(FID);
Ted Kremenek5f074262009-01-09 22:05:30 +000097}
Ted Kremeneke5680f32008-12-23 01:30:52 +000098
99void PTHLexer::Lex(Token& Tok) {
100LexNextToken:
Ted Kremeneke5680f32008-12-23 01:30:52 +0000101
Ted Kremenek866bdf72008-12-23 02:30:15 +0000102 //===--------------------------------------==//
103 // Read the raw token data.
104 //===--------------------------------------==//
105
106 // Shadow CurPtr into an automatic variable.
Chris Lattneraff6ef82009-01-21 07:21:56 +0000107 const unsigned char *CurPtrShadow = CurPtr;
Ted Kremenek866bdf72008-12-23 02:30:15 +0000108
Chris Lattner1b5285e2009-01-18 02:10:31 +0000109 // Read in the data for the token.
Chris Lattneraff6ef82009-01-21 07:21:56 +0000110 unsigned Word0 = Read32(CurPtrShadow);
111 uint32_t IdentifierID = Read32(CurPtrShadow);
112 uint32_t FileOffset = Read32(CurPtrShadow);
Ted Kremenek7b78b7c2009-01-19 23:13:15 +0000113
114 tok::TokenKind TKind = (tok::TokenKind) (Word0 & 0xFF);
115 Token::TokenFlags TFlags = (Token::TokenFlags) ((Word0 >> 8) & 0xFF);
Chris Lattneraff6ef82009-01-21 07:21:56 +0000116 uint32_t Len = Word0 >> 16;
Ted Kremenek7b78b7c2009-01-19 23:13:15 +0000117
Chris Lattneraff6ef82009-01-21 07:21:56 +0000118 CurPtr = CurPtrShadow;
Ted Kremenek866bdf72008-12-23 02:30:15 +0000119
120 //===--------------------------------------==//
121 // Construct the token itself.
122 //===--------------------------------------==//
123
124 Tok.startToken();
Chris Lattner898a0bb2009-01-18 02:34:01 +0000125 Tok.setKind(TKind);
126 Tok.setFlag(TFlags);
Ted Kremenek59d08cb2008-12-23 19:24:24 +0000127 assert(!LexingRawMode);
Chris Lattner898a0bb2009-01-18 02:34:01 +0000128 if (IdentifierID)
129 Tok.setIdentifierInfo(PTHMgr.GetIdentifierInfo(IdentifierID-1));
Chris Lattner2b2453a2009-01-17 06:22:33 +0000130 Tok.setLocation(FileStartLoc.getFileLocWithOffset(FileOffset));
Ted Kremenek866bdf72008-12-23 02:30:15 +0000131 Tok.setLength(Len);
132
133 //===--------------------------------------==//
134 // Process the token.
135 //===--------------------------------------==//
Ted Kremenek5f074262009-01-09 22:05:30 +0000136#if 0
137 SourceManager& SM = PP->getSourceManager();
138 llvm::cerr << SM.getFileEntryForID(FileID)->getName()
139 << ':' << SM.getLogicalLineNumber(Tok.getLocation())
140 << ':' << SM.getLogicalColumnNumber(Tok.getLocation())
141 << '\n';
142#endif
Ted Kremeneke5680f32008-12-23 01:30:52 +0000143
Chris Lattner898a0bb2009-01-18 02:34:01 +0000144 if (TKind == tok::identifier) {
Ted Kremenek59d08cb2008-12-23 19:24:24 +0000145 MIOpt.ReadToken();
146 return PP->HandleIdentifier(Tok);
147 }
148
Chris Lattner898a0bb2009-01-18 02:34:01 +0000149 if (TKind == tok::eof) {
Ted Kremeneke5680f32008-12-23 01:30:52 +0000150 // Save the end-of-file token.
151 EofToken = Tok;
152
153 Preprocessor *PPCache = PP;
Ted Kremenek59d08cb2008-12-23 19:24:24 +0000154
155 assert(!ParsingPreprocessorDirective);
156 assert(!LexingRawMode);
157
158 // FIXME: Issue diagnostics similar to Lexer.
159 if (PP->HandleEndOfFile(Tok, false))
Ted Kremeneke5680f32008-12-23 01:30:52 +0000160 return;
Ted Kremenek59d08cb2008-12-23 19:24:24 +0000161
Ted Kremeneke5680f32008-12-23 01:30:52 +0000162 assert(PPCache && "Raw buffer::LexEndOfFile should return a token");
163 return PPCache->Lex(Tok);
164 }
Ted Kremeneke5680f32008-12-23 01:30:52 +0000165
Chris Lattner898a0bb2009-01-18 02:34:01 +0000166 if (TKind == tok::hash && Tok.isAtStartOfLine()) {
Ted Kremenek59d08cb2008-12-23 19:24:24 +0000167 LastHashTokPtr = CurPtr - DISK_TOKEN_SIZE;
168 assert(!LexingRawMode);
169 PP->HandleDirective(Tok);
170
171 if (PP->isCurrentLexer(this))
172 goto LexNextToken;
173
174 return PP->Lex(Tok);
175 }
176
Chris Lattner898a0bb2009-01-18 02:34:01 +0000177 if (TKind == tok::eom) {
Ted Kremenek59d08cb2008-12-23 19:24:24 +0000178 assert(ParsingPreprocessorDirective);
Ted Kremeneke5680f32008-12-23 01:30:52 +0000179 ParsingPreprocessorDirective = false;
180 return;
181 }
Ted Kremeneke5680f32008-12-23 01:30:52 +0000182
Ted Kremenek59d08cb2008-12-23 19:24:24 +0000183 MIOpt.ReadToken();
Ted Kremeneke5680f32008-12-23 01:30:52 +0000184}
185
186// FIXME: We can just grab the last token instead of storing a copy
187// into EofToken.
Ted Kremenek59d08cb2008-12-23 19:24:24 +0000188void PTHLexer::getEOF(Token& Tok) {
Ted Kremenekdefb7092009-01-09 00:36:11 +0000189 assert(EofToken.is(tok::eof));
Ted Kremeneke5680f32008-12-23 01:30:52 +0000190 Tok = EofToken;
191}
192
193void PTHLexer::DiscardToEndOfLine() {
194 assert(ParsingPreprocessorDirective && ParsingFilename == false &&
195 "Must be in a preprocessing directive!");
196
197 // We assume that if the preprocessor wishes to discard to the end of
198 // the line that it also means to end the current preprocessor directive.
199 ParsingPreprocessorDirective = false;
200
201 // Skip tokens by only peeking at their token kind and the flags.
202 // We don't need to actually reconstruct full tokens from the token buffer.
203 // This saves some copies and it also reduces IdentifierInfo* lookup.
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000204 const unsigned char* p = CurPtr;
Ted Kremeneke5680f32008-12-23 01:30:52 +0000205 while (1) {
206 // Read the token kind. Are we at the end of the file?
207 tok::TokenKind x = (tok::TokenKind) (uint8_t) *p;
208 if (x == tok::eof) break;
209
210 // Read the token flags. Are we at the start of the next line?
211 Token::TokenFlags y = (Token::TokenFlags) (uint8_t) p[1];
212 if (y & Token::StartOfLine) break;
213
214 // Skip to the next token.
215 p += DISK_TOKEN_SIZE;
216 }
217
218 CurPtr = p;
219}
220
Ted Kremenek268ee702008-12-12 18:34:08 +0000221/// SkipBlock - Used by Preprocessor to skip the current conditional block.
222bool PTHLexer::SkipBlock() {
223 assert(CurPPCondPtr && "No cached PP conditional information.");
224 assert(LastHashTokPtr && "No known '#' token.");
225
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000226 const unsigned char* HashEntryI = 0;
Ted Kremenek268ee702008-12-12 18:34:08 +0000227 uint32_t Offset;
228 uint32_t TableIdx;
229
230 do {
Ted Kremenek41a26602008-12-12 22:05:38 +0000231 // Read the token offset from the side-table.
Ted Kremenek268ee702008-12-12 18:34:08 +0000232 Offset = Read32(CurPPCondPtr);
Ted Kremenek41a26602008-12-12 22:05:38 +0000233
234 // Read the target table index from the side-table.
Ted Kremenek268ee702008-12-12 18:34:08 +0000235 TableIdx = Read32(CurPPCondPtr);
Ted Kremenek41a26602008-12-12 22:05:38 +0000236
237 // Compute the actual memory address of the '#' token data for this entry.
238 HashEntryI = TokBuf + Offset;
239
240 // Optmization: "Sibling jumping". #if...#else...#endif blocks can
241 // contain nested blocks. In the side-table we can jump over these
242 // nested blocks instead of doing a linear search if the next "sibling"
243 // entry is not at a location greater than LastHashTokPtr.
244 if (HashEntryI < LastHashTokPtr && TableIdx) {
245 // In the side-table we are still at an entry for a '#' token that
246 // is earlier than the last one we saw. Check if the location we would
247 // stride gets us closer.
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000248 const unsigned char* NextPPCondPtr =
249 PPCond + TableIdx*(sizeof(uint32_t)*2);
Ted Kremenek41a26602008-12-12 22:05:38 +0000250 assert(NextPPCondPtr >= CurPPCondPtr);
251 // Read where we should jump to.
252 uint32_t TmpOffset = Read32(NextPPCondPtr);
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000253 const unsigned char* HashEntryJ = TokBuf + TmpOffset;
Ted Kremenek41a26602008-12-12 22:05:38 +0000254
255 if (HashEntryJ <= LastHashTokPtr) {
256 // Jump directly to the next entry in the side table.
257 HashEntryI = HashEntryJ;
258 Offset = TmpOffset;
259 TableIdx = Read32(NextPPCondPtr);
260 CurPPCondPtr = NextPPCondPtr;
261 }
262 }
Ted Kremenek268ee702008-12-12 18:34:08 +0000263 }
Ted Kremenek41a26602008-12-12 22:05:38 +0000264 while (HashEntryI < LastHashTokPtr);
265 assert(HashEntryI == LastHashTokPtr && "No PP-cond entry found for '#'");
Ted Kremenek268ee702008-12-12 18:34:08 +0000266 assert(TableIdx && "No jumping from #endifs.");
267
268 // Update our side-table iterator.
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000269 const unsigned char* NextPPCondPtr = PPCond + TableIdx*(sizeof(uint32_t)*2);
Ted Kremenek268ee702008-12-12 18:34:08 +0000270 assert(NextPPCondPtr >= CurPPCondPtr);
271 CurPPCondPtr = NextPPCondPtr;
272
273 // Read where we should jump to.
Ted Kremenek41a26602008-12-12 22:05:38 +0000274 HashEntryI = TokBuf + Read32(NextPPCondPtr);
Ted Kremenek268ee702008-12-12 18:34:08 +0000275 uint32_t NextIdx = Read32(NextPPCondPtr);
276
277 // By construction NextIdx will be zero if this is a #endif. This is useful
278 // to know to obviate lexing another token.
279 bool isEndif = NextIdx == 0;
Ted Kremenek268ee702008-12-12 18:34:08 +0000280
281 // This case can occur when we see something like this:
282 //
283 // #if ...
284 // /* a comment or nothing */
285 // #elif
286 //
287 // If we are skipping the first #if block it will be the case that CurPtr
288 // already points 'elif'. Just return.
289
Ted Kremenek41a26602008-12-12 22:05:38 +0000290 if (CurPtr > HashEntryI) {
291 assert(CurPtr == HashEntryI + DISK_TOKEN_SIZE);
Ted Kremenek268ee702008-12-12 18:34:08 +0000292 // Did we reach a #endif? If so, go ahead and consume that token as well.
293 if (isEndif)
Ted Kremeneke5680f32008-12-23 01:30:52 +0000294 CurPtr += DISK_TOKEN_SIZE*2;
Ted Kremenek268ee702008-12-12 18:34:08 +0000295 else
Ted Kremenek41a26602008-12-12 22:05:38 +0000296 LastHashTokPtr = HashEntryI;
Ted Kremenek268ee702008-12-12 18:34:08 +0000297
298 return isEndif;
299 }
300
301 // Otherwise, we need to advance. Update CurPtr to point to the '#' token.
Ted Kremenek41a26602008-12-12 22:05:38 +0000302 CurPtr = HashEntryI;
Ted Kremenek268ee702008-12-12 18:34:08 +0000303
304 // Update the location of the last observed '#'. This is useful if we
305 // are skipping multiple blocks.
306 LastHashTokPtr = CurPtr;
Ted Kremenek268ee702008-12-12 18:34:08 +0000307
Ted Kremeneke5680f32008-12-23 01:30:52 +0000308 // Skip the '#' token.
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000309 assert(((tok::TokenKind)*CurPtr) == tok::hash);
Ted Kremeneke5680f32008-12-23 01:30:52 +0000310 CurPtr += DISK_TOKEN_SIZE;
311
Ted Kremenek268ee702008-12-12 18:34:08 +0000312 // Did we reach a #endif? If so, go ahead and consume that token as well.
Ted Kremeneke5680f32008-12-23 01:30:52 +0000313 if (isEndif) { CurPtr += DISK_TOKEN_SIZE*2; }
Ted Kremenek268ee702008-12-12 18:34:08 +0000314
315 return isEndif;
316}
317
Ted Kremenek30a12ec2008-12-17 23:36:32 +0000318SourceLocation PTHLexer::getSourceLocation() {
Chris Lattner1b5285e2009-01-18 02:10:31 +0000319 // getSourceLocation is not on the hot path. It is used to get the location
320 // of the next token when transitioning back to this lexer when done
Ted Kremenek30a12ec2008-12-17 23:36:32 +0000321 // handling a #included file. Just read the necessary data from the token
322 // data buffer to construct the SourceLocation object.
323 // NOTE: This is a virtual function; hence it is defined out-of-line.
Chris Lattner1b5285e2009-01-18 02:10:31 +0000324 const unsigned char *OffsetPtr = CurPtr + (1 + 1 + 3);
325 uint32_t Offset = Read32(OffsetPtr);
326 return FileStartLoc.getFileLocWithOffset(Offset);
Ted Kremenek30a12ec2008-12-17 23:36:32 +0000327}
328
Ted Kremenek5f074262009-01-09 22:05:30 +0000329//===----------------------------------------------------------------------===//
330// getSpelling() - Use cached data in PTH files for getSpelling().
331//===----------------------------------------------------------------------===//
332
Chris Lattner2b2453a2009-01-17 06:22:33 +0000333unsigned PTHManager::getSpelling(FileID FID, unsigned FPos,
334 const char *&Buffer) {
335 llvm::DenseMap<FileID, PTHSpellingSearch*>::iterator I =SpellingMap.find(FID);
Ted Kremenek5f074262009-01-09 22:05:30 +0000336
337 if (I == SpellingMap.end())
Chris Lattner1b5285e2009-01-18 02:10:31 +0000338 return 0;
Ted Kremenekf02f6f02009-01-13 22:05:50 +0000339
Chris Lattner2b2453a2009-01-17 06:22:33 +0000340 return I->second->getSpellingBinarySearch(FPos, Buffer);
341}
342
343unsigned PTHManager::getSpelling(SourceLocation Loc, const char *&Buffer) {
Chris Lattner6b7b8402009-01-17 06:29:33 +0000344 SourceManager &SM = PP->getSourceManager();
345 Loc = SM.getSpellingLoc(Loc);
346 std::pair<FileID, unsigned> LocInfo = SM.getDecomposedFileLoc(Loc);
Chris Lattner2b2453a2009-01-17 06:22:33 +0000347 return getSpelling(LocInfo.first, LocInfo.second, Buffer);
Ted Kremenek5f074262009-01-09 22:05:30 +0000348}
349
350unsigned PTHManager::getSpellingAtPTHOffset(unsigned PTHOffset,
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000351 const char *&Buffer) {
Chris Lattner1b5285e2009-01-18 02:10:31 +0000352 assert(PTHOffset < Buf->getBufferSize());
353 const unsigned char* Ptr =
354 (const unsigned char*)Buf->getBufferStart() + PTHOffset;
Ted Kremenek32a8ad52009-01-08 04:30:32 +0000355
356 // The string is prefixed by 16 bits for its length, followed by the string
357 // itself.
Chris Lattner1b5285e2009-01-18 02:10:31 +0000358 unsigned Len = Read16(Ptr);
359 Buffer = (const char *)Ptr;
360 return Len;
Ted Kremenek32a8ad52009-01-08 04:30:32 +0000361}
362
Chris Lattner1b5285e2009-01-18 02:10:31 +0000363unsigned PTHSpellingSearch::getSpellingLinearSearch(unsigned FPos,
Ted Kremenek5f074262009-01-09 22:05:30 +0000364 const char *&Buffer) {
Chris Lattner1b5285e2009-01-18 02:10:31 +0000365 const unsigned char *Ptr = LinearItr;
366 unsigned Len = 0;
Ted Kremenek5f074262009-01-09 22:05:30 +0000367
Chris Lattner1b5285e2009-01-18 02:10:31 +0000368 if (Ptr == TableEnd)
369 return getSpellingBinarySearch(FPos, Buffer);
Ted Kremenek5f074262009-01-09 22:05:30 +0000370
371 do {
Chris Lattner1b5285e2009-01-18 02:10:31 +0000372 uint32_t TokOffset = Read32(Ptr);
Ted Kremenek32a8ad52009-01-08 04:30:32 +0000373
Chris Lattner1b5285e2009-01-18 02:10:31 +0000374 if (TokOffset > FPos)
375 return getSpellingBinarySearch(FPos, Buffer);
Ted Kremenek32a8ad52009-01-08 04:30:32 +0000376
Ted Kremenek32a8ad52009-01-08 04:30:32 +0000377 // Did we find a matching token offset for this spelling?
Chris Lattner1b5285e2009-01-18 02:10:31 +0000378 if (TokOffset == FPos) {
379 uint32_t SpellingPTHOffset = Read32(Ptr);
380 Len = PTHMgr.getSpellingAtPTHOffset(SpellingPTHOffset, Buffer);
Ted Kremenek32a8ad52009-01-08 04:30:32 +0000381 break;
382 }
Chris Lattner1b5285e2009-01-18 02:10:31 +0000383 } while (Ptr != TableEnd);
Ted Kremenek32a8ad52009-01-08 04:30:32 +0000384
Chris Lattner1b5285e2009-01-18 02:10:31 +0000385 LinearItr = Ptr;
386 return Len;
Ted Kremenekb70e3da2009-01-08 02:47:16 +0000387}
388
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000389
Chris Lattner1b5285e2009-01-18 02:10:31 +0000390unsigned PTHSpellingSearch::getSpellingBinarySearch(unsigned FPos,
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000391 const char *&Buffer) {
Ted Kremenek5f074262009-01-09 22:05:30 +0000392
Ted Kremenekf02f6f02009-01-13 22:05:50 +0000393 assert((TableEnd - TableBeg) % SpellingEntrySize == 0);
Chris Lattner1b5285e2009-01-18 02:10:31 +0000394 assert(TableEnd >= TableBeg);
Ted Kremenekf02f6f02009-01-13 22:05:50 +0000395
396 if (TableEnd == TableBeg)
397 return 0;
398
Ted Kremenek5f074262009-01-09 22:05:30 +0000399 unsigned min = 0;
Chris Lattner1b5285e2009-01-18 02:10:31 +0000400 const unsigned char *tb = TableBeg;
Ted Kremenekf02f6f02009-01-13 22:05:50 +0000401 unsigned max = NumSpellings;
Ted Kremenek5f074262009-01-09 22:05:30 +0000402
Ted Kremenekf02f6f02009-01-13 22:05:50 +0000403 do {
Ted Kremenek5f074262009-01-09 22:05:30 +0000404 unsigned i = (max - min) / 2 + min;
Chris Lattner1b5285e2009-01-18 02:10:31 +0000405 const unsigned char *Ptr = tb + (i * SpellingEntrySize);
Ted Kremenek5f074262009-01-09 22:05:30 +0000406
Chris Lattner1b5285e2009-01-18 02:10:31 +0000407 uint32_t TokOffset = Read32(Ptr);
408 if (TokOffset > FPos) {
Ted Kremenek5f074262009-01-09 22:05:30 +0000409 max = i;
Ted Kremenekf02f6f02009-01-13 22:05:50 +0000410 assert(!(max == min) || (min == i));
Ted Kremenek5f074262009-01-09 22:05:30 +0000411 continue;
412 }
413
Chris Lattner1b5285e2009-01-18 02:10:31 +0000414 if (TokOffset < FPos) {
Ted Kremenek7bd88aa2009-01-13 22:16:45 +0000415 if (i == min)
416 break;
417
Ted Kremenek5f074262009-01-09 22:05:30 +0000418 min = i;
419 continue;
420 }
421
Chris Lattner1b5285e2009-01-18 02:10:31 +0000422 uint32_t SpellingPTHOffset = Read32(Ptr);
Ted Kremenek5f074262009-01-09 22:05:30 +0000423 return PTHMgr.getSpellingAtPTHOffset(SpellingPTHOffset, Buffer);
424 }
Ted Kremenekf02f6f02009-01-13 22:05:50 +0000425 while (min != max);
Ted Kremenek5f074262009-01-09 22:05:30 +0000426
427 return 0;
428}
429
Chris Lattner2b2453a2009-01-17 06:22:33 +0000430unsigned PTHLexer::getSpelling(SourceLocation Loc, const char *&Buffer) {
431 SourceManager &SM = PP->getSourceManager();
432 Loc = SM.getSpellingLoc(Loc);
433 std::pair<FileID, unsigned> LocInfo = SM.getDecomposedFileLoc(Loc);
434
435 FileID FID = LocInfo.first;
436 unsigned FPos = LocInfo.second;
Ted Kremenek5f074262009-01-09 22:05:30 +0000437
Chris Lattner2b2453a2009-01-17 06:22:33 +0000438 if (FID == getFileID())
439 return MySpellingSrch.getSpellingLinearSearch(FPos, Buffer);
440 return PTHMgr.getSpelling(FID, FPos, Buffer);
Ted Kremenek5f074262009-01-09 22:05:30 +0000441}
442
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000443//===----------------------------------------------------------------------===//
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000444// Internal Data Structures for PTH file lookup and resolving identifiers.
445//===----------------------------------------------------------------------===//
446
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000447
448/// PTHFileLookup - This internal data structure is used by the PTHManager
449/// to map from FileEntry objects managed by FileManager to offsets within
450/// the PTH file.
451namespace {
452class VISIBILITY_HIDDEN PTHFileLookup {
453public:
454 class Val {
Ted Kremenekfb645b62008-12-11 23:36:38 +0000455 uint32_t TokenOff;
456 uint32_t PPCondOff;
Ted Kremenekb70e3da2009-01-08 02:47:16 +0000457 uint32_t SpellingOff;
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000458 public:
Ted Kremenekfb645b62008-12-11 23:36:38 +0000459 Val() : TokenOff(~0) {}
Ted Kremenekb70e3da2009-01-08 02:47:16 +0000460 Val(uint32_t toff, uint32_t poff, uint32_t soff)
461 : TokenOff(toff), PPCondOff(poff), SpellingOff(soff) {}
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000462
Chris Lattner1b5285e2009-01-18 02:10:31 +0000463 bool isValid() const { return TokenOff != ~((uint32_t)0); }
464
Ted Kremenekfb645b62008-12-11 23:36:38 +0000465 uint32_t getTokenOffset() const {
Chris Lattner1b5285e2009-01-18 02:10:31 +0000466 assert(isValid() && "PTHFileLookup entry initialized.");
Ted Kremenekfb645b62008-12-11 23:36:38 +0000467 return TokenOff;
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000468 }
469
Ted Kremenekb70e3da2009-01-08 02:47:16 +0000470 uint32_t getPPCondOffset() const {
Chris Lattner1b5285e2009-01-18 02:10:31 +0000471 assert(isValid() && "PTHFileLookup entry initialized.");
Ted Kremenekfb645b62008-12-11 23:36:38 +0000472 return PPCondOff;
473 }
474
Ted Kremenekb70e3da2009-01-08 02:47:16 +0000475 uint32_t getSpellingOffset() const {
Chris Lattner1b5285e2009-01-18 02:10:31 +0000476 assert(isValid() && "PTHFileLookup entry initialized.");
Ted Kremenekb70e3da2009-01-08 02:47:16 +0000477 return SpellingOff;
478 }
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000479 };
480
481private:
482 llvm::StringMap<Val> FileMap;
483
484public:
485 PTHFileLookup() {};
486
Ted Kremenekcdd8f212009-01-21 07:34:28 +0000487 bool isEmpty() const {
488 return FileMap.empty();
489 }
490
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000491 Val Lookup(const FileEntry* FE) {
492 const char* s = FE->getName();
493 unsigned size = strlen(s);
494 return FileMap.GetOrCreateValue(s, s+size).getValue();
495 }
496
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000497 void ReadTable(const unsigned char* D) {
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000498 uint32_t N = Read32(D); // Read the length of the table.
499
500 for ( ; N > 0; --N) { // The rest of the data is the table itself.
Chris Lattner1b5285e2009-01-18 02:10:31 +0000501 uint32_t Len = Read32(D);
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000502 const char* s = (const char *)D;
Chris Lattner1b5285e2009-01-18 02:10:31 +0000503 D += Len;
Ted Kremenekb70e3da2009-01-08 02:47:16 +0000504
Ted Kremenekfb645b62008-12-11 23:36:38 +0000505 uint32_t TokenOff = Read32(D);
Ted Kremenekb70e3da2009-01-08 02:47:16 +0000506 uint32_t PPCondOff = Read32(D);
507 uint32_t SpellingOff = Read32(D);
508
Chris Lattner1b5285e2009-01-18 02:10:31 +0000509 FileMap.GetOrCreateValue(s, s+Len).getValue() =
Ted Kremenekb70e3da2009-01-08 02:47:16 +0000510 Val(TokenOff, PPCondOff, SpellingOff);
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000511 }
512 }
513};
514} // end anonymous namespace
515
516//===----------------------------------------------------------------------===//
517// PTHManager methods.
518//===----------------------------------------------------------------------===//
519
520PTHManager::PTHManager(const llvm::MemoryBuffer* buf, void* fileLookup,
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000521 const unsigned char* idDataTable,
522 IdentifierInfo** perIDCache,
523 const unsigned char* sortedIdTable, unsigned numIds)
Ted Kremenek6183e482008-12-03 01:16:39 +0000524: Buf(buf), PerIDCache(perIDCache), FileLookup(fileLookup),
Ted Kremenek72b1b152009-01-15 18:47:46 +0000525 IdDataTable(idDataTable), SortedIdTable(sortedIdTable),
526 NumIds(numIds), PP(0) {}
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000527
528PTHManager::~PTHManager() {
529 delete Buf;
530 delete (PTHFileLookup*) FileLookup;
Ted Kremenek0e50b6e2008-12-04 22:47:11 +0000531 free(PerIDCache);
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000532}
533
Ted Kremenek72b1b152009-01-15 18:47:46 +0000534PTHManager* PTHManager::Create(const std::string& file) {
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000535 // Memory map the PTH file.
536 llvm::OwningPtr<llvm::MemoryBuffer>
537 File(llvm::MemoryBuffer::getFile(file.c_str()));
538
539 if (!File)
540 return 0;
541
542 // Get the buffer ranges and check if there are at least three 32-bit
543 // words at the end of the file.
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000544 const unsigned char* BufBeg = (unsigned char*)File->getBufferStart();
545 const unsigned char* BufEnd = (unsigned char*)File->getBufferEnd();
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000546
547 if(!(BufEnd > BufBeg + sizeof(uint32_t)*3)) {
548 assert(false && "Invalid PTH file.");
549 return 0; // FIXME: Proper error diagnostic?
550 }
551
552 // Compute the address of the index table at the end of the PTH file.
553 // This table contains the offset of the file lookup table, the
554 // persistent ID -> identifer data table.
Ted Kremenek293b4af2009-01-15 01:26:25 +0000555 // FIXME: We should just embed this offset in the PTH file.
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000556 const unsigned char* EndTable = BufEnd - sizeof(uint32_t)*4;
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000557
558 // Construct the file lookup table. This will be used for mapping from
559 // FileEntry*'s to cached tokens.
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000560 const unsigned char* FileTableOffset = EndTable + sizeof(uint32_t)*3;
561 const unsigned char* FileTable = BufBeg + Read32(FileTableOffset);
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000562
563 if (!(FileTable > BufBeg && FileTable < BufEnd)) {
564 assert(false && "Invalid PTH file.");
565 return 0; // FIXME: Proper error diagnostic?
566 }
567
568 llvm::OwningPtr<PTHFileLookup> FL(new PTHFileLookup());
569 FL->ReadTable(FileTable);
Ted Kremenekcdd8f212009-01-21 07:34:28 +0000570
571 if (FL->isEmpty())
572 return 0;
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000573
574 // Get the location of the table mapping from persistent ids to the
575 // data needed to reconstruct identifiers.
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000576 const unsigned char* IDTableOffset = EndTable + sizeof(uint32_t)*1;
577 const unsigned char* IData = BufBeg + Read32(IDTableOffset);
Ted Kremenekcdd8f212009-01-21 07:34:28 +0000578
579 if (!(IData >= BufBeg && IData < BufEnd)) {
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000580 assert(false && "Invalid PTH file.");
581 return 0; // FIXME: Proper error diagnostic?
582 }
583
Ted Kremenek72b1b152009-01-15 18:47:46 +0000584 // Get the location of the lexigraphically-sorted table of persistent IDs.
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000585 const unsigned char* SortedIdTableOffset = EndTable + sizeof(uint32_t)*2;
586 const unsigned char* SortedIdTable = BufBeg + Read32(SortedIdTableOffset);
Ted Kremenekcdd8f212009-01-21 07:34:28 +0000587 if (!(SortedIdTable >= BufBeg && SortedIdTable < BufEnd)) {
Ted Kremenek72b1b152009-01-15 18:47:46 +0000588 assert(false && "Invalid PTH file.");
589 return 0; // FIXME: Proper error diagnostic?
590 }
591
Ted Kremenek6183e482008-12-03 01:16:39 +0000592 // Get the number of IdentifierInfos and pre-allocate the identifier cache.
593 uint32_t NumIds = Read32(IData);
Ted Kremenekcdd8f212009-01-21 07:34:28 +0000594
Ted Kremenek6183e482008-12-03 01:16:39 +0000595 // Pre-allocate the peristent ID -> IdentifierInfo* cache. We use calloc()
596 // so that we in the best case only zero out memory once when the OS returns
597 // us new pages.
Ted Kremenekcdd8f212009-01-21 07:34:28 +0000598 IdentifierInfo** PerIDCache = 0;
Ted Kremenek6183e482008-12-03 01:16:39 +0000599
Ted Kremenekcdd8f212009-01-21 07:34:28 +0000600 if (NumIds) {
601 PerIDCache = (IdentifierInfo**)calloc(NumIds, sizeof(*PerIDCache));
602 if (!PerIDCache) {
603 assert(false && "Could not allocate Persistent ID cache.");
604 return 0;
605 }
Ted Kremenek6183e482008-12-03 01:16:39 +0000606 }
Ted Kremenekcdd8f212009-01-21 07:34:28 +0000607
Ted Kremenek72b1b152009-01-15 18:47:46 +0000608 // Create the new PTHManager.
609 return new PTHManager(File.take(), FL.take(), IData, PerIDCache,
610 SortedIdTable, NumIds);
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000611}
Chris Lattner77ecb3a2009-01-18 02:57:21 +0000612IdentifierInfo* PTHManager::LazilyCreateIdentifierInfo(unsigned PersistentID) {
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000613 // Look in the PTH file for the string data for the IdentifierInfo object.
Chris Lattner77ecb3a2009-01-18 02:57:21 +0000614 const unsigned char* TableEntry = IdDataTable + sizeof(uint32_t)*PersistentID;
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000615 const unsigned char* IDData =
616 (const unsigned char*)Buf->getBufferStart() + Read32(TableEntry);
617 assert(IDData < (const unsigned char*)Buf->getBufferEnd());
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000618
Ted Kremenek72b1b152009-01-15 18:47:46 +0000619 // Allocate the object.
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000620 std::pair<IdentifierInfo,const unsigned char*> *Mem =
621 Alloc.Allocate<std::pair<IdentifierInfo,const unsigned char*> >();
Ted Kremenek72b1b152009-01-15 18:47:46 +0000622
623 Mem->second = IDData;
Ted Kremenekea9c26b2009-01-20 23:28:34 +0000624 IdentifierInfo *II = new ((void*) Mem) IdentifierInfo();
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000625
Ted Kremenek72b1b152009-01-15 18:47:46 +0000626 // Store the new IdentifierInfo in the cache.
Chris Lattner77ecb3a2009-01-18 02:57:21 +0000627 PerIDCache[PersistentID] = II;
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000628 return II;
629}
630
Ted Kremenek72b1b152009-01-15 18:47:46 +0000631IdentifierInfo* PTHManager::get(const char *NameStart, const char *NameEnd) {
632 unsigned min = 0;
633 unsigned max = NumIds;
Chris Lattner1b5285e2009-01-18 02:10:31 +0000634 unsigned Len = NameEnd - NameStart;
Ted Kremenek72b1b152009-01-15 18:47:46 +0000635
636 do {
637 unsigned i = (max - min) / 2 + min;
Chris Lattner1b5285e2009-01-18 02:10:31 +0000638 const unsigned char *Ptr = SortedIdTable + (i * 4);
Ted Kremenek72b1b152009-01-15 18:47:46 +0000639
640 // Read the persistentID.
Chris Lattner1b5285e2009-01-18 02:10:31 +0000641 unsigned perID = Read32(Ptr);
Ted Kremenek72b1b152009-01-15 18:47:46 +0000642
643 // Get the IdentifierInfo.
644 IdentifierInfo* II = GetIdentifierInfo(perID);
645
646 // First compare the lengths.
647 unsigned IILen = II->getLength();
Chris Lattner1b5285e2009-01-18 02:10:31 +0000648 if (Len < IILen) goto IsLess;
649 if (Len > IILen) goto IsGreater;
Ted Kremenek72b1b152009-01-15 18:47:46 +0000650
651 // Now compare the strings!
652 {
Chris Lattner1b5285e2009-01-18 02:10:31 +0000653 signed comp = strncmp(NameStart, II->getName(), Len);
Ted Kremenek72b1b152009-01-15 18:47:46 +0000654 if (comp < 0) goto IsLess;
655 if (comp > 0) goto IsGreater;
656 }
657 // We found a match!
658 return II;
659
660 IsGreater:
661 if (i == min) break;
662 min = i;
663 continue;
664
665 IsLess:
666 max = i;
667 assert(!(max == min) || (min == i));
668 }
Ted Kremeneke1deaac2009-01-15 19:28:38 +0000669 while (min != max);
Ted Kremenek72b1b152009-01-15 18:47:46 +0000670
671 return 0;
672}
673
674
Chris Lattnerf056d922009-01-17 08:06:50 +0000675PTHLexer *PTHManager::CreateLexer(FileID FID) {
676 const FileEntry *FE = PP->getSourceManager().getFileEntryForID(FID);
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000677 if (!FE)
678 return 0;
679
680 // Lookup the FileEntry object in our file lookup data structure. It will
681 // return a variant that indicates whether or not there is an offset within
682 // the PTH file that contains cached tokens.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000683 PTHFileLookup::Val FileData = ((PTHFileLookup*)FileLookup)->Lookup(FE);
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000684
Ted Kremenekfb645b62008-12-11 23:36:38 +0000685 if (!FileData.isValid()) // No tokens available.
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000686 return 0;
687
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000688 const unsigned char *BufStart = (const unsigned char *)Buf->getBufferStart();
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000689 // Compute the offset of the token data within the buffer.
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000690 const unsigned char* data = BufStart + FileData.getTokenOffset();
Ted Kremenek268ee702008-12-12 18:34:08 +0000691
692 // Get the location of pp-conditional table.
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000693 const unsigned char* ppcond = BufStart + FileData.getPPCondOffset();
Chris Lattner1b5285e2009-01-18 02:10:31 +0000694 uint32_t Len = Read32(ppcond);
695 if (Len == 0) ppcond = 0;
Ted Kremenek32a8ad52009-01-08 04:30:32 +0000696
697 // Get the location of the spelling table.
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000698 const unsigned char* spellingTable = BufStart + FileData.getSpellingOffset();
Ted Kremenek32a8ad52009-01-08 04:30:32 +0000699
Chris Lattner1b5285e2009-01-18 02:10:31 +0000700 Len = Read32(spellingTable);
701 if (Len == 0) spellingTable = 0;
Ted Kremenekb70e3da2009-01-08 02:47:16 +0000702
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000703 assert(data < (const unsigned char*)Buf->getBufferEnd());
Ted Kremenek5f074262009-01-09 22:05:30 +0000704
705 // Create the SpellingSearch object for this FileID.
Chris Lattner1b5285e2009-01-18 02:10:31 +0000706 PTHSpellingSearch* ss = new PTHSpellingSearch(*this, Len, spellingTable);
Chris Lattner2b2453a2009-01-17 06:22:33 +0000707 SpellingMap[FID] = ss;
Ted Kremenek5f074262009-01-09 22:05:30 +0000708
Ted Kremenek72b1b152009-01-15 18:47:46 +0000709 assert(PP && "No preprocessor set yet!");
Chris Lattner2b2453a2009-01-17 06:22:33 +0000710 return new PTHLexer(*PP, FID, data, ppcond, *ss, *this);
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000711}