blob: 6e449e4ba926f8a1e4dfa096dffa376dbdb4f743 [file] [log] [blame]
Ted Kremenekca820862008-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 Kremenek325cd302008-12-03 00:38:03 +000014#include "clang/Basic/TokenKinds.h"
15#include "clang/Basic/FileManager.h"
16#include "clang/Basic/IdentifierTable.h"
Ted Kremenekca820862008-11-12 21:37:15 +000017#include "clang/Lex/PTHLexer.h"
18#include "clang/Lex/Preprocessor.h"
Ted Kremenek325cd302008-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 Kremenekca820862008-11-12 21:37:15 +000026using namespace clang;
27
Ted Kremenekb8344ef2009-01-19 23:13:15 +000028#define DISK_TOKEN_SIZE (1+1+2+4+4)
Ted Kremenekc07091c2008-12-12 18:34:08 +000029
Ted Kremenek325cd302008-12-03 00:38:03 +000030//===----------------------------------------------------------------------===//
31// Utility methods for reading from the mmap'ed PTH file.
32//===----------------------------------------------------------------------===//
33
Chris Lattnerefb35342009-01-18 01:57:14 +000034static inline uint8_t Read8(const unsigned char *&Data) {
35 uint8_t V = Data[0];
36 Data += 1;
Ted Kremenek325cd302008-12-03 00:38:03 +000037 return V;
38}
39
Chris Lattnerefb35342009-01-18 01:57:14 +000040static inline uint16_t Read16(const unsigned char *&Data) {
Chris Lattner5bffcaf2009-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 Lattnerefb35342009-01-18 01:57:14 +000046 uint16_t V = ((uint16_t)Data[0] << 0) |
47 ((uint16_t)Data[1] << 8);
Chris Lattner5bffcaf2009-01-18 02:19:16 +000048#endif
Chris Lattnerefb35342009-01-18 01:57:14 +000049 Data += 2;
50 return V;
51}
52
Chris Lattnerefb35342009-01-18 01:57:14 +000053static inline uint32_t Read32(const unsigned char *&Data) {
Chris Lattner5bffcaf2009-01-18 02:19:16 +000054// Targets that directly support unaligned little-endian 32-bit loads can just
55// use them.
56#if defined(__i386__) || defined(__x86_64__)
57 uint32_t V = *((uint32_t*)Data);
58#else
Chris Lattnerefb35342009-01-18 01:57:14 +000059 uint32_t V = ((uint32_t)Data[0] << 0) |
60 ((uint32_t)Data[1] << 8) |
61 ((uint32_t)Data[2] << 16) |
62 ((uint32_t)Data[3] << 24);
Chris Lattner5bffcaf2009-01-18 02:19:16 +000063#endif
Chris Lattnerefb35342009-01-18 01:57:14 +000064 Data += 4;
65 return V;
66}
67
68
Ted Kremenek9ab79bf2008-12-23 01:30:52 +000069//===----------------------------------------------------------------------===//
70// PTHLexer methods.
71//===----------------------------------------------------------------------===//
72
Chris Lattnerefb35342009-01-18 01:57:14 +000073PTHLexer::PTHLexer(Preprocessor &PP, FileID FID, const unsigned char *D,
74 const unsigned char *ppcond,
Chris Lattnerf4f776a2009-01-17 06:22:33 +000075 PTHSpellingSearch &mySpellingSrch, PTHManager &PM)
76 : PreprocessorLexer(&PP, FID), TokBuf(D), CurPtr(D), LastHashTokPtr(0),
Ted Kremenekd310fde2009-01-09 22:05:30 +000077 PPCond(ppcond), CurPPCondPtr(ppcond), MySpellingSrch(mySpellingSrch),
Chris Lattnerf4f776a2009-01-17 06:22:33 +000078 PTHMgr(PM) {
79
80 FileStartLoc = PP.getSourceManager().getLocForStartOfFile(FID);
Ted Kremenekd310fde2009-01-09 22:05:30 +000081}
Ted Kremenek9ab79bf2008-12-23 01:30:52 +000082
83void PTHLexer::Lex(Token& Tok) {
84LexNextToken:
Ted Kremenek9ab79bf2008-12-23 01:30:52 +000085
Ted Kremenek08d3ff32008-12-23 02:30:15 +000086 //===--------------------------------------==//
87 // Read the raw token data.
88 //===--------------------------------------==//
89
90 // Shadow CurPtr into an automatic variable.
Chris Lattnerc706f072009-01-21 07:06:08 +000091 const unsigned *CurPtrShadow = (const unsigned *)CurPtr;
Ted Kremenek08d3ff32008-12-23 02:30:15 +000092
Chris Lattner0a6ec5d2009-01-18 02:10:31 +000093 // Read in the data for the token.
Chris Lattnerc706f072009-01-21 07:06:08 +000094 unsigned Word0 = CurPtrShadow[0];
95 unsigned IdentifierID = CurPtrShadow[1];
96 unsigned FileOffset = CurPtrShadow[2];
Ted Kremenekb8344ef2009-01-19 23:13:15 +000097
98 tok::TokenKind TKind = (tok::TokenKind) (Word0 & 0xFF);
99 Token::TokenFlags TFlags = (Token::TokenFlags) ((Word0 >> 8) & 0xFF);
Chris Lattnerc706f072009-01-21 07:06:08 +0000100 unsigned Len = Word0 >> 16;
Ted Kremenekb8344ef2009-01-19 23:13:15 +0000101
Chris Lattnerc706f072009-01-21 07:06:08 +0000102 CurPtr = (const unsigned char*)(CurPtrShadow+3);
Ted Kremenek08d3ff32008-12-23 02:30:15 +0000103
104 //===--------------------------------------==//
105 // Construct the token itself.
106 //===--------------------------------------==//
107
108 Tok.startToken();
Chris Lattnerce8670e2009-01-18 02:34:01 +0000109 Tok.setKind(TKind);
110 Tok.setFlag(TFlags);
Ted Kremenekd94668d2008-12-23 19:24:24 +0000111 assert(!LexingRawMode);
Chris Lattnerce8670e2009-01-18 02:34:01 +0000112 if (IdentifierID)
113 Tok.setIdentifierInfo(PTHMgr.GetIdentifierInfo(IdentifierID-1));
Chris Lattnerf4f776a2009-01-17 06:22:33 +0000114 Tok.setLocation(FileStartLoc.getFileLocWithOffset(FileOffset));
Ted Kremenek08d3ff32008-12-23 02:30:15 +0000115 Tok.setLength(Len);
116
117 //===--------------------------------------==//
118 // Process the token.
119 //===--------------------------------------==//
Ted Kremenekd310fde2009-01-09 22:05:30 +0000120#if 0
121 SourceManager& SM = PP->getSourceManager();
122 llvm::cerr << SM.getFileEntryForID(FileID)->getName()
123 << ':' << SM.getLogicalLineNumber(Tok.getLocation())
124 << ':' << SM.getLogicalColumnNumber(Tok.getLocation())
125 << '\n';
126#endif
Ted Kremenek9ab79bf2008-12-23 01:30:52 +0000127
Chris Lattnerce8670e2009-01-18 02:34:01 +0000128 if (TKind == tok::identifier) {
Ted Kremenekd94668d2008-12-23 19:24:24 +0000129 MIOpt.ReadToken();
130 return PP->HandleIdentifier(Tok);
131 }
132
Chris Lattnerce8670e2009-01-18 02:34:01 +0000133 if (TKind == tok::eof) {
Ted Kremenek9ab79bf2008-12-23 01:30:52 +0000134 // Save the end-of-file token.
135 EofToken = Tok;
136
137 Preprocessor *PPCache = PP;
Ted Kremenekd94668d2008-12-23 19:24:24 +0000138
139 assert(!ParsingPreprocessorDirective);
140 assert(!LexingRawMode);
141
142 // FIXME: Issue diagnostics similar to Lexer.
143 if (PP->HandleEndOfFile(Tok, false))
Ted Kremenek9ab79bf2008-12-23 01:30:52 +0000144 return;
Ted Kremenekd94668d2008-12-23 19:24:24 +0000145
Ted Kremenek9ab79bf2008-12-23 01:30:52 +0000146 assert(PPCache && "Raw buffer::LexEndOfFile should return a token");
147 return PPCache->Lex(Tok);
148 }
Ted Kremenek9ab79bf2008-12-23 01:30:52 +0000149
Chris Lattnerce8670e2009-01-18 02:34:01 +0000150 if (TKind == tok::hash && Tok.isAtStartOfLine()) {
Ted Kremenekd94668d2008-12-23 19:24:24 +0000151 LastHashTokPtr = CurPtr - DISK_TOKEN_SIZE;
152 assert(!LexingRawMode);
153 PP->HandleDirective(Tok);
154
155 if (PP->isCurrentLexer(this))
156 goto LexNextToken;
157
158 return PP->Lex(Tok);
159 }
160
Chris Lattnerce8670e2009-01-18 02:34:01 +0000161 if (TKind == tok::eom) {
Ted Kremenekd94668d2008-12-23 19:24:24 +0000162 assert(ParsingPreprocessorDirective);
Ted Kremenek9ab79bf2008-12-23 01:30:52 +0000163 ParsingPreprocessorDirective = false;
164 return;
165 }
Ted Kremenek9ab79bf2008-12-23 01:30:52 +0000166
Ted Kremenekd94668d2008-12-23 19:24:24 +0000167 MIOpt.ReadToken();
Ted Kremenek9ab79bf2008-12-23 01:30:52 +0000168}
169
170// FIXME: We can just grab the last token instead of storing a copy
171// into EofToken.
Ted Kremenekd94668d2008-12-23 19:24:24 +0000172void PTHLexer::getEOF(Token& Tok) {
Ted Kremeneke4caf142009-01-09 00:36:11 +0000173 assert(EofToken.is(tok::eof));
Ted Kremenek9ab79bf2008-12-23 01:30:52 +0000174 Tok = EofToken;
175}
176
177void PTHLexer::DiscardToEndOfLine() {
178 assert(ParsingPreprocessorDirective && ParsingFilename == false &&
179 "Must be in a preprocessing directive!");
180
181 // We assume that if the preprocessor wishes to discard to the end of
182 // the line that it also means to end the current preprocessor directive.
183 ParsingPreprocessorDirective = false;
184
185 // Skip tokens by only peeking at their token kind and the flags.
186 // We don't need to actually reconstruct full tokens from the token buffer.
187 // This saves some copies and it also reduces IdentifierInfo* lookup.
Chris Lattnerefb35342009-01-18 01:57:14 +0000188 const unsigned char* p = CurPtr;
Ted Kremenek9ab79bf2008-12-23 01:30:52 +0000189 while (1) {
190 // Read the token kind. Are we at the end of the file?
191 tok::TokenKind x = (tok::TokenKind) (uint8_t) *p;
192 if (x == tok::eof) break;
193
194 // Read the token flags. Are we at the start of the next line?
195 Token::TokenFlags y = (Token::TokenFlags) (uint8_t) p[1];
196 if (y & Token::StartOfLine) break;
197
198 // Skip to the next token.
199 p += DISK_TOKEN_SIZE;
200 }
201
202 CurPtr = p;
203}
204
Ted Kremenekc07091c2008-12-12 18:34:08 +0000205/// SkipBlock - Used by Preprocessor to skip the current conditional block.
206bool PTHLexer::SkipBlock() {
207 assert(CurPPCondPtr && "No cached PP conditional information.");
208 assert(LastHashTokPtr && "No known '#' token.");
209
Chris Lattnerefb35342009-01-18 01:57:14 +0000210 const unsigned char* HashEntryI = 0;
Ted Kremenekc07091c2008-12-12 18:34:08 +0000211 uint32_t Offset;
212 uint32_t TableIdx;
213
214 do {
Ted Kremenekbe3e84f2008-12-12 22:05:38 +0000215 // Read the token offset from the side-table.
Ted Kremenekc07091c2008-12-12 18:34:08 +0000216 Offset = Read32(CurPPCondPtr);
Ted Kremenekbe3e84f2008-12-12 22:05:38 +0000217
218 // Read the target table index from the side-table.
Ted Kremenekc07091c2008-12-12 18:34:08 +0000219 TableIdx = Read32(CurPPCondPtr);
Ted Kremenekbe3e84f2008-12-12 22:05:38 +0000220
221 // Compute the actual memory address of the '#' token data for this entry.
222 HashEntryI = TokBuf + Offset;
223
224 // Optmization: "Sibling jumping". #if...#else...#endif blocks can
225 // contain nested blocks. In the side-table we can jump over these
226 // nested blocks instead of doing a linear search if the next "sibling"
227 // entry is not at a location greater than LastHashTokPtr.
228 if (HashEntryI < LastHashTokPtr && TableIdx) {
229 // In the side-table we are still at an entry for a '#' token that
230 // is earlier than the last one we saw. Check if the location we would
231 // stride gets us closer.
Chris Lattnerefb35342009-01-18 01:57:14 +0000232 const unsigned char* NextPPCondPtr =
233 PPCond + TableIdx*(sizeof(uint32_t)*2);
Ted Kremenekbe3e84f2008-12-12 22:05:38 +0000234 assert(NextPPCondPtr >= CurPPCondPtr);
235 // Read where we should jump to.
236 uint32_t TmpOffset = Read32(NextPPCondPtr);
Chris Lattnerefb35342009-01-18 01:57:14 +0000237 const unsigned char* HashEntryJ = TokBuf + TmpOffset;
Ted Kremenekbe3e84f2008-12-12 22:05:38 +0000238
239 if (HashEntryJ <= LastHashTokPtr) {
240 // Jump directly to the next entry in the side table.
241 HashEntryI = HashEntryJ;
242 Offset = TmpOffset;
243 TableIdx = Read32(NextPPCondPtr);
244 CurPPCondPtr = NextPPCondPtr;
245 }
246 }
Ted Kremenekc07091c2008-12-12 18:34:08 +0000247 }
Ted Kremenekbe3e84f2008-12-12 22:05:38 +0000248 while (HashEntryI < LastHashTokPtr);
249 assert(HashEntryI == LastHashTokPtr && "No PP-cond entry found for '#'");
Ted Kremenekc07091c2008-12-12 18:34:08 +0000250 assert(TableIdx && "No jumping from #endifs.");
251
252 // Update our side-table iterator.
Chris Lattnerefb35342009-01-18 01:57:14 +0000253 const unsigned char* NextPPCondPtr = PPCond + TableIdx*(sizeof(uint32_t)*2);
Ted Kremenekc07091c2008-12-12 18:34:08 +0000254 assert(NextPPCondPtr >= CurPPCondPtr);
255 CurPPCondPtr = NextPPCondPtr;
256
257 // Read where we should jump to.
Ted Kremenekbe3e84f2008-12-12 22:05:38 +0000258 HashEntryI = TokBuf + Read32(NextPPCondPtr);
Ted Kremenekc07091c2008-12-12 18:34:08 +0000259 uint32_t NextIdx = Read32(NextPPCondPtr);
260
261 // By construction NextIdx will be zero if this is a #endif. This is useful
262 // to know to obviate lexing another token.
263 bool isEndif = NextIdx == 0;
Ted Kremenekc07091c2008-12-12 18:34:08 +0000264
265 // This case can occur when we see something like this:
266 //
267 // #if ...
268 // /* a comment or nothing */
269 // #elif
270 //
271 // If we are skipping the first #if block it will be the case that CurPtr
272 // already points 'elif'. Just return.
273
Ted Kremenekbe3e84f2008-12-12 22:05:38 +0000274 if (CurPtr > HashEntryI) {
275 assert(CurPtr == HashEntryI + DISK_TOKEN_SIZE);
Ted Kremenekc07091c2008-12-12 18:34:08 +0000276 // Did we reach a #endif? If so, go ahead and consume that token as well.
277 if (isEndif)
Ted Kremenek9ab79bf2008-12-23 01:30:52 +0000278 CurPtr += DISK_TOKEN_SIZE*2;
Ted Kremenekc07091c2008-12-12 18:34:08 +0000279 else
Ted Kremenekbe3e84f2008-12-12 22:05:38 +0000280 LastHashTokPtr = HashEntryI;
Ted Kremenekc07091c2008-12-12 18:34:08 +0000281
282 return isEndif;
283 }
284
285 // Otherwise, we need to advance. Update CurPtr to point to the '#' token.
Ted Kremenekbe3e84f2008-12-12 22:05:38 +0000286 CurPtr = HashEntryI;
Ted Kremenekc07091c2008-12-12 18:34:08 +0000287
288 // Update the location of the last observed '#'. This is useful if we
289 // are skipping multiple blocks.
290 LastHashTokPtr = CurPtr;
Ted Kremenekc07091c2008-12-12 18:34:08 +0000291
Ted Kremenek9ab79bf2008-12-23 01:30:52 +0000292 // Skip the '#' token.
Chris Lattnerefb35342009-01-18 01:57:14 +0000293 assert(((tok::TokenKind)*CurPtr) == tok::hash);
Ted Kremenek9ab79bf2008-12-23 01:30:52 +0000294 CurPtr += DISK_TOKEN_SIZE;
295
Ted Kremenekc07091c2008-12-12 18:34:08 +0000296 // Did we reach a #endif? If so, go ahead and consume that token as well.
Ted Kremenek9ab79bf2008-12-23 01:30:52 +0000297 if (isEndif) { CurPtr += DISK_TOKEN_SIZE*2; }
Ted Kremenekc07091c2008-12-12 18:34:08 +0000298
299 return isEndif;
300}
301
Ted Kremenekbeb57672008-12-17 23:36:32 +0000302SourceLocation PTHLexer::getSourceLocation() {
Chris Lattner0a6ec5d2009-01-18 02:10:31 +0000303 // getSourceLocation is not on the hot path. It is used to get the location
304 // of the next token when transitioning back to this lexer when done
Ted Kremenekbeb57672008-12-17 23:36:32 +0000305 // handling a #included file. Just read the necessary data from the token
306 // data buffer to construct the SourceLocation object.
307 // NOTE: This is a virtual function; hence it is defined out-of-line.
Chris Lattner0a6ec5d2009-01-18 02:10:31 +0000308 const unsigned char *OffsetPtr = CurPtr + (1 + 1 + 3);
309 uint32_t Offset = Read32(OffsetPtr);
310 return FileStartLoc.getFileLocWithOffset(Offset);
Ted Kremenekbeb57672008-12-17 23:36:32 +0000311}
312
Ted Kremenekd310fde2009-01-09 22:05:30 +0000313//===----------------------------------------------------------------------===//
314// getSpelling() - Use cached data in PTH files for getSpelling().
315//===----------------------------------------------------------------------===//
316
Chris Lattnerf4f776a2009-01-17 06:22:33 +0000317unsigned PTHManager::getSpelling(FileID FID, unsigned FPos,
318 const char *&Buffer) {
319 llvm::DenseMap<FileID, PTHSpellingSearch*>::iterator I =SpellingMap.find(FID);
Ted Kremenekd310fde2009-01-09 22:05:30 +0000320
321 if (I == SpellingMap.end())
Chris Lattner0a6ec5d2009-01-18 02:10:31 +0000322 return 0;
Ted Kremenek21069422009-01-13 22:05:50 +0000323
Chris Lattnerf4f776a2009-01-17 06:22:33 +0000324 return I->second->getSpellingBinarySearch(FPos, Buffer);
325}
326
327unsigned PTHManager::getSpelling(SourceLocation Loc, const char *&Buffer) {
Chris Lattnere7fc9622009-01-17 06:29:33 +0000328 SourceManager &SM = PP->getSourceManager();
329 Loc = SM.getSpellingLoc(Loc);
330 std::pair<FileID, unsigned> LocInfo = SM.getDecomposedFileLoc(Loc);
Chris Lattnerf4f776a2009-01-17 06:22:33 +0000331 return getSpelling(LocInfo.first, LocInfo.second, Buffer);
Ted Kremenekd310fde2009-01-09 22:05:30 +0000332}
333
334unsigned PTHManager::getSpellingAtPTHOffset(unsigned PTHOffset,
Chris Lattnerefb35342009-01-18 01:57:14 +0000335 const char *&Buffer) {
Chris Lattner0a6ec5d2009-01-18 02:10:31 +0000336 assert(PTHOffset < Buf->getBufferSize());
337 const unsigned char* Ptr =
338 (const unsigned char*)Buf->getBufferStart() + PTHOffset;
Ted Kremeneka4d5bf52009-01-08 04:30:32 +0000339
340 // The string is prefixed by 16 bits for its length, followed by the string
341 // itself.
Chris Lattner0a6ec5d2009-01-18 02:10:31 +0000342 unsigned Len = Read16(Ptr);
343 Buffer = (const char *)Ptr;
344 return Len;
Ted Kremeneka4d5bf52009-01-08 04:30:32 +0000345}
346
Chris Lattner0a6ec5d2009-01-18 02:10:31 +0000347unsigned PTHSpellingSearch::getSpellingLinearSearch(unsigned FPos,
Ted Kremenekd310fde2009-01-09 22:05:30 +0000348 const char *&Buffer) {
Chris Lattner0a6ec5d2009-01-18 02:10:31 +0000349 const unsigned char *Ptr = LinearItr;
350 unsigned Len = 0;
Ted Kremenekd310fde2009-01-09 22:05:30 +0000351
Chris Lattner0a6ec5d2009-01-18 02:10:31 +0000352 if (Ptr == TableEnd)
353 return getSpellingBinarySearch(FPos, Buffer);
Ted Kremenekd310fde2009-01-09 22:05:30 +0000354
355 do {
Chris Lattner0a6ec5d2009-01-18 02:10:31 +0000356 uint32_t TokOffset = Read32(Ptr);
Ted Kremeneka4d5bf52009-01-08 04:30:32 +0000357
Chris Lattner0a6ec5d2009-01-18 02:10:31 +0000358 if (TokOffset > FPos)
359 return getSpellingBinarySearch(FPos, Buffer);
Ted Kremeneka4d5bf52009-01-08 04:30:32 +0000360
Ted Kremeneka4d5bf52009-01-08 04:30:32 +0000361 // Did we find a matching token offset for this spelling?
Chris Lattner0a6ec5d2009-01-18 02:10:31 +0000362 if (TokOffset == FPos) {
363 uint32_t SpellingPTHOffset = Read32(Ptr);
364 Len = PTHMgr.getSpellingAtPTHOffset(SpellingPTHOffset, Buffer);
Ted Kremeneka4d5bf52009-01-08 04:30:32 +0000365 break;
366 }
Chris Lattner0a6ec5d2009-01-18 02:10:31 +0000367 } while (Ptr != TableEnd);
Ted Kremeneka4d5bf52009-01-08 04:30:32 +0000368
Chris Lattner0a6ec5d2009-01-18 02:10:31 +0000369 LinearItr = Ptr;
370 return Len;
Ted Kremenekd2c849d2009-01-08 02:47:16 +0000371}
372
Chris Lattnerefb35342009-01-18 01:57:14 +0000373
Chris Lattner0a6ec5d2009-01-18 02:10:31 +0000374unsigned PTHSpellingSearch::getSpellingBinarySearch(unsigned FPos,
Chris Lattnerefb35342009-01-18 01:57:14 +0000375 const char *&Buffer) {
Ted Kremenekd310fde2009-01-09 22:05:30 +0000376
Ted Kremenek21069422009-01-13 22:05:50 +0000377 assert((TableEnd - TableBeg) % SpellingEntrySize == 0);
Chris Lattner0a6ec5d2009-01-18 02:10:31 +0000378 assert(TableEnd >= TableBeg);
Ted Kremenek21069422009-01-13 22:05:50 +0000379
380 if (TableEnd == TableBeg)
381 return 0;
382
Ted Kremenekd310fde2009-01-09 22:05:30 +0000383 unsigned min = 0;
Chris Lattner0a6ec5d2009-01-18 02:10:31 +0000384 const unsigned char *tb = TableBeg;
Ted Kremenek21069422009-01-13 22:05:50 +0000385 unsigned max = NumSpellings;
Ted Kremenekd310fde2009-01-09 22:05:30 +0000386
Ted Kremenek21069422009-01-13 22:05:50 +0000387 do {
Ted Kremenekd310fde2009-01-09 22:05:30 +0000388 unsigned i = (max - min) / 2 + min;
Chris Lattner0a6ec5d2009-01-18 02:10:31 +0000389 const unsigned char *Ptr = tb + (i * SpellingEntrySize);
Ted Kremenekd310fde2009-01-09 22:05:30 +0000390
Chris Lattner0a6ec5d2009-01-18 02:10:31 +0000391 uint32_t TokOffset = Read32(Ptr);
392 if (TokOffset > FPos) {
Ted Kremenekd310fde2009-01-09 22:05:30 +0000393 max = i;
Ted Kremenek21069422009-01-13 22:05:50 +0000394 assert(!(max == min) || (min == i));
Ted Kremenekd310fde2009-01-09 22:05:30 +0000395 continue;
396 }
397
Chris Lattner0a6ec5d2009-01-18 02:10:31 +0000398 if (TokOffset < FPos) {
Ted Kremenekc20d0ab2009-01-13 22:16:45 +0000399 if (i == min)
400 break;
401
Ted Kremenekd310fde2009-01-09 22:05:30 +0000402 min = i;
403 continue;
404 }
405
Chris Lattner0a6ec5d2009-01-18 02:10:31 +0000406 uint32_t SpellingPTHOffset = Read32(Ptr);
Ted Kremenekd310fde2009-01-09 22:05:30 +0000407 return PTHMgr.getSpellingAtPTHOffset(SpellingPTHOffset, Buffer);
408 }
Ted Kremenek21069422009-01-13 22:05:50 +0000409 while (min != max);
Ted Kremenekd310fde2009-01-09 22:05:30 +0000410
411 return 0;
412}
413
Chris Lattnerf4f776a2009-01-17 06:22:33 +0000414unsigned PTHLexer::getSpelling(SourceLocation Loc, const char *&Buffer) {
415 SourceManager &SM = PP->getSourceManager();
416 Loc = SM.getSpellingLoc(Loc);
417 std::pair<FileID, unsigned> LocInfo = SM.getDecomposedFileLoc(Loc);
418
419 FileID FID = LocInfo.first;
420 unsigned FPos = LocInfo.second;
Ted Kremenekd310fde2009-01-09 22:05:30 +0000421
Chris Lattnerf4f776a2009-01-17 06:22:33 +0000422 if (FID == getFileID())
423 return MySpellingSrch.getSpellingLinearSearch(FPos, Buffer);
424 return PTHMgr.getSpelling(FID, FPos, Buffer);
Ted Kremenekd310fde2009-01-09 22:05:30 +0000425}
426
Ted Kremenek325cd302008-12-03 00:38:03 +0000427//===----------------------------------------------------------------------===//
Ted Kremenek325cd302008-12-03 00:38:03 +0000428// Internal Data Structures for PTH file lookup and resolving identifiers.
429//===----------------------------------------------------------------------===//
430
Ted Kremenek325cd302008-12-03 00:38:03 +0000431
432/// PTHFileLookup - This internal data structure is used by the PTHManager
433/// to map from FileEntry objects managed by FileManager to offsets within
434/// the PTH file.
435namespace {
436class VISIBILITY_HIDDEN PTHFileLookup {
437public:
438 class Val {
Ted Kremenek8309c922008-12-11 23:36:38 +0000439 uint32_t TokenOff;
440 uint32_t PPCondOff;
Ted Kremenekd2c849d2009-01-08 02:47:16 +0000441 uint32_t SpellingOff;
Ted Kremenek325cd302008-12-03 00:38:03 +0000442 public:
Ted Kremenek8309c922008-12-11 23:36:38 +0000443 Val() : TokenOff(~0) {}
Ted Kremenekd2c849d2009-01-08 02:47:16 +0000444 Val(uint32_t toff, uint32_t poff, uint32_t soff)
445 : TokenOff(toff), PPCondOff(poff), SpellingOff(soff) {}
Ted Kremenek325cd302008-12-03 00:38:03 +0000446
Chris Lattner0a6ec5d2009-01-18 02:10:31 +0000447 bool isValid() const { return TokenOff != ~((uint32_t)0); }
448
Ted Kremenek8309c922008-12-11 23:36:38 +0000449 uint32_t getTokenOffset() const {
Chris Lattner0a6ec5d2009-01-18 02:10:31 +0000450 assert(isValid() && "PTHFileLookup entry initialized.");
Ted Kremenek8309c922008-12-11 23:36:38 +0000451 return TokenOff;
Ted Kremenek325cd302008-12-03 00:38:03 +0000452 }
453
Ted Kremenekd2c849d2009-01-08 02:47:16 +0000454 uint32_t getPPCondOffset() const {
Chris Lattner0a6ec5d2009-01-18 02:10:31 +0000455 assert(isValid() && "PTHFileLookup entry initialized.");
Ted Kremenek8309c922008-12-11 23:36:38 +0000456 return PPCondOff;
457 }
458
Ted Kremenekd2c849d2009-01-08 02:47:16 +0000459 uint32_t getSpellingOffset() const {
Chris Lattner0a6ec5d2009-01-18 02:10:31 +0000460 assert(isValid() && "PTHFileLookup entry initialized.");
Ted Kremenekd2c849d2009-01-08 02:47:16 +0000461 return SpellingOff;
462 }
Ted Kremenek325cd302008-12-03 00:38:03 +0000463 };
464
465private:
466 llvm::StringMap<Val> FileMap;
467
468public:
469 PTHFileLookup() {};
470
471 Val Lookup(const FileEntry* FE) {
472 const char* s = FE->getName();
473 unsigned size = strlen(s);
474 return FileMap.GetOrCreateValue(s, s+size).getValue();
475 }
476
Chris Lattnerefb35342009-01-18 01:57:14 +0000477 void ReadTable(const unsigned char* D) {
Ted Kremenek325cd302008-12-03 00:38:03 +0000478 uint32_t N = Read32(D); // Read the length of the table.
479
480 for ( ; N > 0; --N) { // The rest of the data is the table itself.
Chris Lattner0a6ec5d2009-01-18 02:10:31 +0000481 uint32_t Len = Read32(D);
Chris Lattnerefb35342009-01-18 01:57:14 +0000482 const char* s = (const char *)D;
Chris Lattner0a6ec5d2009-01-18 02:10:31 +0000483 D += Len;
Ted Kremenekd2c849d2009-01-08 02:47:16 +0000484
Ted Kremenek8309c922008-12-11 23:36:38 +0000485 uint32_t TokenOff = Read32(D);
Ted Kremenekd2c849d2009-01-08 02:47:16 +0000486 uint32_t PPCondOff = Read32(D);
487 uint32_t SpellingOff = Read32(D);
488
Chris Lattner0a6ec5d2009-01-18 02:10:31 +0000489 FileMap.GetOrCreateValue(s, s+Len).getValue() =
Ted Kremenekd2c849d2009-01-08 02:47:16 +0000490 Val(TokenOff, PPCondOff, SpellingOff);
Ted Kremenek325cd302008-12-03 00:38:03 +0000491 }
492 }
493};
494} // end anonymous namespace
495
496//===----------------------------------------------------------------------===//
497// PTHManager methods.
498//===----------------------------------------------------------------------===//
499
500PTHManager::PTHManager(const llvm::MemoryBuffer* buf, void* fileLookup,
Chris Lattnerefb35342009-01-18 01:57:14 +0000501 const unsigned char* idDataTable,
502 IdentifierInfo** perIDCache,
503 const unsigned char* sortedIdTable, unsigned numIds)
Ted Kremenekdb4c8e82008-12-03 01:16:39 +0000504: Buf(buf), PerIDCache(perIDCache), FileLookup(fileLookup),
Ted Kremenekd976c3d2009-01-15 18:47:46 +0000505 IdDataTable(idDataTable), SortedIdTable(sortedIdTable),
506 NumIds(numIds), PP(0) {}
Ted Kremenek325cd302008-12-03 00:38:03 +0000507
508PTHManager::~PTHManager() {
509 delete Buf;
510 delete (PTHFileLookup*) FileLookup;
Ted Kremenek93bdc492008-12-04 22:47:11 +0000511 free(PerIDCache);
Ted Kremenek325cd302008-12-03 00:38:03 +0000512}
513
Ted Kremenekd976c3d2009-01-15 18:47:46 +0000514PTHManager* PTHManager::Create(const std::string& file) {
Ted Kremenek325cd302008-12-03 00:38:03 +0000515 // Memory map the PTH file.
516 llvm::OwningPtr<llvm::MemoryBuffer>
517 File(llvm::MemoryBuffer::getFile(file.c_str()));
518
519 if (!File)
520 return 0;
521
522 // Get the buffer ranges and check if there are at least three 32-bit
523 // words at the end of the file.
Chris Lattnerefb35342009-01-18 01:57:14 +0000524 const unsigned char* BufBeg = (unsigned char*)File->getBufferStart();
525 const unsigned char* BufEnd = (unsigned char*)File->getBufferEnd();
Ted Kremenek325cd302008-12-03 00:38:03 +0000526
527 if(!(BufEnd > BufBeg + sizeof(uint32_t)*3)) {
528 assert(false && "Invalid PTH file.");
529 return 0; // FIXME: Proper error diagnostic?
530 }
531
532 // Compute the address of the index table at the end of the PTH file.
533 // This table contains the offset of the file lookup table, the
534 // persistent ID -> identifer data table.
Ted Kremenekd4d6d222009-01-15 01:26:25 +0000535 // FIXME: We should just embed this offset in the PTH file.
Chris Lattnerefb35342009-01-18 01:57:14 +0000536 const unsigned char* EndTable = BufEnd - sizeof(uint32_t)*4;
Ted Kremenek325cd302008-12-03 00:38:03 +0000537
538 // Construct the file lookup table. This will be used for mapping from
539 // FileEntry*'s to cached tokens.
Chris Lattnerefb35342009-01-18 01:57:14 +0000540 const unsigned char* FileTableOffset = EndTable + sizeof(uint32_t)*3;
541 const unsigned char* FileTable = BufBeg + Read32(FileTableOffset);
Ted Kremenek325cd302008-12-03 00:38:03 +0000542
543 if (!(FileTable > BufBeg && FileTable < BufEnd)) {
544 assert(false && "Invalid PTH file.");
545 return 0; // FIXME: Proper error diagnostic?
546 }
547
548 llvm::OwningPtr<PTHFileLookup> FL(new PTHFileLookup());
549 FL->ReadTable(FileTable);
550
551 // Get the location of the table mapping from persistent ids to the
552 // data needed to reconstruct identifiers.
Chris Lattnerefb35342009-01-18 01:57:14 +0000553 const unsigned char* IDTableOffset = EndTable + sizeof(uint32_t)*1;
554 const unsigned char* IData = BufBeg + Read32(IDTableOffset);
Ted Kremenek325cd302008-12-03 00:38:03 +0000555 if (!(IData > BufBeg && IData < BufEnd)) {
556 assert(false && "Invalid PTH file.");
557 return 0; // FIXME: Proper error diagnostic?
558 }
559
Ted Kremenekd976c3d2009-01-15 18:47:46 +0000560 // Get the location of the lexigraphically-sorted table of persistent IDs.
Chris Lattnerefb35342009-01-18 01:57:14 +0000561 const unsigned char* SortedIdTableOffset = EndTable + sizeof(uint32_t)*2;
562 const unsigned char* SortedIdTable = BufBeg + Read32(SortedIdTableOffset);
Ted Kremenekd976c3d2009-01-15 18:47:46 +0000563 if (!(SortedIdTable > BufBeg && SortedIdTable < BufEnd)) {
564 assert(false && "Invalid PTH file.");
565 return 0; // FIXME: Proper error diagnostic?
566 }
567
Ted Kremenekdb4c8e82008-12-03 01:16:39 +0000568 // Get the number of IdentifierInfos and pre-allocate the identifier cache.
569 uint32_t NumIds = Read32(IData);
570
571 // Pre-allocate the peristent ID -> IdentifierInfo* cache. We use calloc()
572 // so that we in the best case only zero out memory once when the OS returns
573 // us new pages.
574 IdentifierInfo** PerIDCache =
Chris Lattnerefb35342009-01-18 01:57:14 +0000575 (IdentifierInfo**)calloc(NumIds, sizeof(*PerIDCache));
Ted Kremenekdb4c8e82008-12-03 01:16:39 +0000576
577 if (!PerIDCache) {
578 assert(false && "Could not allocate Persistent ID cache.");
579 return 0;
580 }
581
Ted Kremenekd976c3d2009-01-15 18:47:46 +0000582 // Create the new PTHManager.
583 return new PTHManager(File.take(), FL.take(), IData, PerIDCache,
584 SortedIdTable, NumIds);
Ted Kremenek325cd302008-12-03 00:38:03 +0000585}
Chris Lattnercbcd26c2009-01-18 02:57:21 +0000586IdentifierInfo* PTHManager::LazilyCreateIdentifierInfo(unsigned PersistentID) {
Ted Kremenek325cd302008-12-03 00:38:03 +0000587 // Look in the PTH file for the string data for the IdentifierInfo object.
Chris Lattnercbcd26c2009-01-18 02:57:21 +0000588 const unsigned char* TableEntry = IdDataTable + sizeof(uint32_t)*PersistentID;
Chris Lattnerefb35342009-01-18 01:57:14 +0000589 const unsigned char* IDData =
590 (const unsigned char*)Buf->getBufferStart() + Read32(TableEntry);
591 assert(IDData < (const unsigned char*)Buf->getBufferEnd());
Ted Kremenek325cd302008-12-03 00:38:03 +0000592
Ted Kremenekd976c3d2009-01-15 18:47:46 +0000593 // Allocate the object.
Chris Lattnerefb35342009-01-18 01:57:14 +0000594 std::pair<IdentifierInfo,const unsigned char*> *Mem =
595 Alloc.Allocate<std::pair<IdentifierInfo,const unsigned char*> >();
Ted Kremenekd976c3d2009-01-15 18:47:46 +0000596
597 Mem->second = IDData;
Ted Kremeneke807f852009-01-20 23:28:34 +0000598 IdentifierInfo *II = new ((void*) Mem) IdentifierInfo();
Ted Kremenek325cd302008-12-03 00:38:03 +0000599
Ted Kremenekd976c3d2009-01-15 18:47:46 +0000600 // Store the new IdentifierInfo in the cache.
Chris Lattnercbcd26c2009-01-18 02:57:21 +0000601 PerIDCache[PersistentID] = II;
Ted Kremenek325cd302008-12-03 00:38:03 +0000602 return II;
603}
604
Ted Kremenekd976c3d2009-01-15 18:47:46 +0000605IdentifierInfo* PTHManager::get(const char *NameStart, const char *NameEnd) {
606 unsigned min = 0;
607 unsigned max = NumIds;
Chris Lattner0a6ec5d2009-01-18 02:10:31 +0000608 unsigned Len = NameEnd - NameStart;
Ted Kremenekd976c3d2009-01-15 18:47:46 +0000609
610 do {
611 unsigned i = (max - min) / 2 + min;
Chris Lattner0a6ec5d2009-01-18 02:10:31 +0000612 const unsigned char *Ptr = SortedIdTable + (i * 4);
Ted Kremenekd976c3d2009-01-15 18:47:46 +0000613
614 // Read the persistentID.
Chris Lattner0a6ec5d2009-01-18 02:10:31 +0000615 unsigned perID = Read32(Ptr);
Ted Kremenekd976c3d2009-01-15 18:47:46 +0000616
617 // Get the IdentifierInfo.
618 IdentifierInfo* II = GetIdentifierInfo(perID);
619
620 // First compare the lengths.
621 unsigned IILen = II->getLength();
Chris Lattner0a6ec5d2009-01-18 02:10:31 +0000622 if (Len < IILen) goto IsLess;
623 if (Len > IILen) goto IsGreater;
Ted Kremenekd976c3d2009-01-15 18:47:46 +0000624
625 // Now compare the strings!
626 {
Chris Lattner0a6ec5d2009-01-18 02:10:31 +0000627 signed comp = strncmp(NameStart, II->getName(), Len);
Ted Kremenekd976c3d2009-01-15 18:47:46 +0000628 if (comp < 0) goto IsLess;
629 if (comp > 0) goto IsGreater;
630 }
631 // We found a match!
632 return II;
633
634 IsGreater:
635 if (i == min) break;
636 min = i;
637 continue;
638
639 IsLess:
640 max = i;
641 assert(!(max == min) || (min == i));
642 }
Ted Kremenek4795ad02009-01-15 19:28:38 +0000643 while (min != max);
Ted Kremenekd976c3d2009-01-15 18:47:46 +0000644
645 return 0;
646}
647
648
Chris Lattner3c727d62009-01-17 08:06:50 +0000649PTHLexer *PTHManager::CreateLexer(FileID FID) {
650 const FileEntry *FE = PP->getSourceManager().getFileEntryForID(FID);
Ted Kremenek325cd302008-12-03 00:38:03 +0000651 if (!FE)
652 return 0;
653
654 // Lookup the FileEntry object in our file lookup data structure. It will
655 // return a variant that indicates whether or not there is an offset within
656 // the PTH file that contains cached tokens.
Chris Lattnerf4f776a2009-01-17 06:22:33 +0000657 PTHFileLookup::Val FileData = ((PTHFileLookup*)FileLookup)->Lookup(FE);
Ted Kremenek325cd302008-12-03 00:38:03 +0000658
Ted Kremenek8309c922008-12-11 23:36:38 +0000659 if (!FileData.isValid()) // No tokens available.
Ted Kremenek325cd302008-12-03 00:38:03 +0000660 return 0;
661
Chris Lattnerefb35342009-01-18 01:57:14 +0000662 const unsigned char *BufStart = (const unsigned char *)Buf->getBufferStart();
Ted Kremenek325cd302008-12-03 00:38:03 +0000663 // Compute the offset of the token data within the buffer.
Chris Lattnerefb35342009-01-18 01:57:14 +0000664 const unsigned char* data = BufStart + FileData.getTokenOffset();
Ted Kremenekc07091c2008-12-12 18:34:08 +0000665
666 // Get the location of pp-conditional table.
Chris Lattnerefb35342009-01-18 01:57:14 +0000667 const unsigned char* ppcond = BufStart + FileData.getPPCondOffset();
Chris Lattner0a6ec5d2009-01-18 02:10:31 +0000668 uint32_t Len = Read32(ppcond);
669 if (Len == 0) ppcond = 0;
Ted Kremeneka4d5bf52009-01-08 04:30:32 +0000670
671 // Get the location of the spelling table.
Chris Lattnerefb35342009-01-18 01:57:14 +0000672 const unsigned char* spellingTable = BufStart + FileData.getSpellingOffset();
Ted Kremeneka4d5bf52009-01-08 04:30:32 +0000673
Chris Lattner0a6ec5d2009-01-18 02:10:31 +0000674 Len = Read32(spellingTable);
675 if (Len == 0) spellingTable = 0;
Ted Kremenekd2c849d2009-01-08 02:47:16 +0000676
Chris Lattnerefb35342009-01-18 01:57:14 +0000677 assert(data < (const unsigned char*)Buf->getBufferEnd());
Ted Kremenekd310fde2009-01-09 22:05:30 +0000678
679 // Create the SpellingSearch object for this FileID.
Chris Lattner0a6ec5d2009-01-18 02:10:31 +0000680 PTHSpellingSearch* ss = new PTHSpellingSearch(*this, Len, spellingTable);
Chris Lattnerf4f776a2009-01-17 06:22:33 +0000681 SpellingMap[FID] = ss;
Ted Kremenekd310fde2009-01-09 22:05:30 +0000682
Ted Kremenekd976c3d2009-01-15 18:47:46 +0000683 assert(PP && "No preprocessor set yet!");
Chris Lattnerf4f776a2009-01-17 06:22:33 +0000684 return new PTHLexer(*PP, FID, data, ppcond, *ss, *this);
Ted Kremenek325cd302008-12-03 00:38:03 +0000685}