blob: 497e225b47066ae109c64abe146b42bd6fd0b465 [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 Lattner673a2862009-01-22 19:48:26 +000034static inline uint16_t ReadUnalignedLE16(const unsigned char *&Data) {
Chris Lattnerefb35342009-01-18 01:57:14 +000035 uint16_t V = ((uint16_t)Data[0] << 0) |
36 ((uint16_t)Data[1] << 8);
37 Data += 2;
38 return V;
39}
40
Chris Lattner673a2862009-01-22 19:48:26 +000041static inline uint32_t ReadLE32(const unsigned char *&Data) {
Chris Lattner5bffcaf2009-01-18 02:19:16 +000042// Targets that directly support unaligned little-endian 32-bit loads can just
43// use them.
44#if defined(__i386__) || defined(__x86_64__)
45 uint32_t V = *((uint32_t*)Data);
46#else
Chris Lattnerefb35342009-01-18 01:57:14 +000047 uint32_t V = ((uint32_t)Data[0] << 0) |
48 ((uint32_t)Data[1] << 8) |
49 ((uint32_t)Data[2] << 16) |
50 ((uint32_t)Data[3] << 24);
Chris Lattner5bffcaf2009-01-18 02:19:16 +000051#endif
Chris Lattnerefb35342009-01-18 01:57:14 +000052 Data += 4;
53 return V;
54}
55
56
Ted Kremenek9ab79bf2008-12-23 01:30:52 +000057//===----------------------------------------------------------------------===//
58// PTHLexer methods.
59//===----------------------------------------------------------------------===//
60
Chris Lattnerefb35342009-01-18 01:57:14 +000061PTHLexer::PTHLexer(Preprocessor &PP, FileID FID, const unsigned char *D,
62 const unsigned char *ppcond,
Chris Lattnerf4f776a2009-01-17 06:22:33 +000063 PTHSpellingSearch &mySpellingSrch, PTHManager &PM)
64 : PreprocessorLexer(&PP, FID), TokBuf(D), CurPtr(D), LastHashTokPtr(0),
Ted Kremenekd310fde2009-01-09 22:05:30 +000065 PPCond(ppcond), CurPPCondPtr(ppcond), MySpellingSrch(mySpellingSrch),
Chris Lattnerf4f776a2009-01-17 06:22:33 +000066 PTHMgr(PM) {
67
68 FileStartLoc = PP.getSourceManager().getLocForStartOfFile(FID);
Ted Kremenekd310fde2009-01-09 22:05:30 +000069}
Ted Kremenek9ab79bf2008-12-23 01:30:52 +000070
71void PTHLexer::Lex(Token& Tok) {
72LexNextToken:
Ted Kremenek9ab79bf2008-12-23 01:30:52 +000073
Ted Kremenek08d3ff32008-12-23 02:30:15 +000074 //===--------------------------------------==//
75 // Read the raw token data.
76 //===--------------------------------------==//
77
78 // Shadow CurPtr into an automatic variable.
Chris Lattnerddf3b792009-01-21 07:21:56 +000079 const unsigned char *CurPtrShadow = CurPtr;
Ted Kremenek08d3ff32008-12-23 02:30:15 +000080
Chris Lattner0a6ec5d2009-01-18 02:10:31 +000081 // Read in the data for the token.
Chris Lattner673a2862009-01-22 19:48:26 +000082 unsigned Word0 = ReadLE32(CurPtrShadow);
83 uint32_t IdentifierID = ReadLE32(CurPtrShadow);
84 uint32_t FileOffset = ReadLE32(CurPtrShadow);
Ted Kremenekb8344ef2009-01-19 23:13:15 +000085
86 tok::TokenKind TKind = (tok::TokenKind) (Word0 & 0xFF);
87 Token::TokenFlags TFlags = (Token::TokenFlags) ((Word0 >> 8) & 0xFF);
Chris Lattnerddf3b792009-01-21 07:21:56 +000088 uint32_t Len = Word0 >> 16;
Ted Kremenekb8344ef2009-01-19 23:13:15 +000089
Chris Lattnerddf3b792009-01-21 07:21:56 +000090 CurPtr = CurPtrShadow;
Ted Kremenek08d3ff32008-12-23 02:30:15 +000091
92 //===--------------------------------------==//
93 // Construct the token itself.
94 //===--------------------------------------==//
95
96 Tok.startToken();
Chris Lattnerce8670e2009-01-18 02:34:01 +000097 Tok.setKind(TKind);
98 Tok.setFlag(TFlags);
Ted Kremenekd94668d2008-12-23 19:24:24 +000099 assert(!LexingRawMode);
Chris Lattnerf4f776a2009-01-17 06:22:33 +0000100 Tok.setLocation(FileStartLoc.getFileLocWithOffset(FileOffset));
Ted Kremenek08d3ff32008-12-23 02:30:15 +0000101 Tok.setLength(Len);
102
Chris Lattner8b2aa222009-01-21 07:50:06 +0000103 // Handle identifiers.
104 if (IdentifierID) {
105 MIOpt.ReadToken();
106 IdentifierInfo *II = PTHMgr.GetIdentifierInfo(IdentifierID-1);
107 Tok.setIdentifierInfo(II);
108 if (II->isHandleIdentifierCase())
109 PP->HandleIdentifier(Tok);
110 return;
111 }
112
Ted Kremenek08d3ff32008-12-23 02:30:15 +0000113 //===--------------------------------------==//
114 // Process the token.
115 //===--------------------------------------==//
Ted Kremenekd310fde2009-01-09 22:05:30 +0000116#if 0
117 SourceManager& SM = PP->getSourceManager();
118 llvm::cerr << SM.getFileEntryForID(FileID)->getName()
119 << ':' << SM.getLogicalLineNumber(Tok.getLocation())
120 << ':' << SM.getLogicalColumnNumber(Tok.getLocation())
121 << '\n';
122#endif
Ted Kremenek9ab79bf2008-12-23 01:30:52 +0000123
Chris Lattnerce8670e2009-01-18 02:34:01 +0000124 if (TKind == tok::eof) {
Ted Kremenek9ab79bf2008-12-23 01:30:52 +0000125 // Save the end-of-file token.
126 EofToken = Tok;
127
128 Preprocessor *PPCache = PP;
Ted Kremenekd94668d2008-12-23 19:24:24 +0000129
130 assert(!ParsingPreprocessorDirective);
131 assert(!LexingRawMode);
132
133 // FIXME: Issue diagnostics similar to Lexer.
134 if (PP->HandleEndOfFile(Tok, false))
Ted Kremenek9ab79bf2008-12-23 01:30:52 +0000135 return;
Ted Kremenekd94668d2008-12-23 19:24:24 +0000136
Ted Kremenek9ab79bf2008-12-23 01:30:52 +0000137 assert(PPCache && "Raw buffer::LexEndOfFile should return a token");
138 return PPCache->Lex(Tok);
139 }
Ted Kremenek9ab79bf2008-12-23 01:30:52 +0000140
Chris Lattnerce8670e2009-01-18 02:34:01 +0000141 if (TKind == tok::hash && Tok.isAtStartOfLine()) {
Ted Kremenekd94668d2008-12-23 19:24:24 +0000142 LastHashTokPtr = CurPtr - DISK_TOKEN_SIZE;
143 assert(!LexingRawMode);
144 PP->HandleDirective(Tok);
145
146 if (PP->isCurrentLexer(this))
147 goto LexNextToken;
148
149 return PP->Lex(Tok);
150 }
151
Chris Lattnerce8670e2009-01-18 02:34:01 +0000152 if (TKind == tok::eom) {
Ted Kremenekd94668d2008-12-23 19:24:24 +0000153 assert(ParsingPreprocessorDirective);
Ted Kremenek9ab79bf2008-12-23 01:30:52 +0000154 ParsingPreprocessorDirective = false;
155 return;
156 }
Ted Kremenek9ab79bf2008-12-23 01:30:52 +0000157
Ted Kremenekd94668d2008-12-23 19:24:24 +0000158 MIOpt.ReadToken();
Ted Kremenek9ab79bf2008-12-23 01:30:52 +0000159}
160
161// FIXME: We can just grab the last token instead of storing a copy
162// into EofToken.
Ted Kremenekd94668d2008-12-23 19:24:24 +0000163void PTHLexer::getEOF(Token& Tok) {
Ted Kremeneke4caf142009-01-09 00:36:11 +0000164 assert(EofToken.is(tok::eof));
Ted Kremenek9ab79bf2008-12-23 01:30:52 +0000165 Tok = EofToken;
166}
167
168void PTHLexer::DiscardToEndOfLine() {
169 assert(ParsingPreprocessorDirective && ParsingFilename == false &&
170 "Must be in a preprocessing directive!");
171
172 // We assume that if the preprocessor wishes to discard to the end of
173 // the line that it also means to end the current preprocessor directive.
174 ParsingPreprocessorDirective = false;
175
176 // Skip tokens by only peeking at their token kind and the flags.
177 // We don't need to actually reconstruct full tokens from the token buffer.
178 // This saves some copies and it also reduces IdentifierInfo* lookup.
Chris Lattnerefb35342009-01-18 01:57:14 +0000179 const unsigned char* p = CurPtr;
Ted Kremenek9ab79bf2008-12-23 01:30:52 +0000180 while (1) {
181 // Read the token kind. Are we at the end of the file?
182 tok::TokenKind x = (tok::TokenKind) (uint8_t) *p;
183 if (x == tok::eof) break;
184
185 // Read the token flags. Are we at the start of the next line?
186 Token::TokenFlags y = (Token::TokenFlags) (uint8_t) p[1];
187 if (y & Token::StartOfLine) break;
188
189 // Skip to the next token.
190 p += DISK_TOKEN_SIZE;
191 }
192
193 CurPtr = p;
194}
195
Ted Kremenekc07091c2008-12-12 18:34:08 +0000196/// SkipBlock - Used by Preprocessor to skip the current conditional block.
197bool PTHLexer::SkipBlock() {
198 assert(CurPPCondPtr && "No cached PP conditional information.");
199 assert(LastHashTokPtr && "No known '#' token.");
200
Chris Lattnerefb35342009-01-18 01:57:14 +0000201 const unsigned char* HashEntryI = 0;
Ted Kremenekc07091c2008-12-12 18:34:08 +0000202 uint32_t Offset;
203 uint32_t TableIdx;
204
205 do {
Ted Kremenekbe3e84f2008-12-12 22:05:38 +0000206 // Read the token offset from the side-table.
Chris Lattner673a2862009-01-22 19:48:26 +0000207 Offset = ReadLE32(CurPPCondPtr);
Ted Kremenekbe3e84f2008-12-12 22:05:38 +0000208
209 // Read the target table index from the side-table.
Chris Lattner673a2862009-01-22 19:48:26 +0000210 TableIdx = ReadLE32(CurPPCondPtr);
Ted Kremenekbe3e84f2008-12-12 22:05:38 +0000211
212 // Compute the actual memory address of the '#' token data for this entry.
213 HashEntryI = TokBuf + Offset;
214
215 // Optmization: "Sibling jumping". #if...#else...#endif blocks can
216 // contain nested blocks. In the side-table we can jump over these
217 // nested blocks instead of doing a linear search if the next "sibling"
218 // entry is not at a location greater than LastHashTokPtr.
219 if (HashEntryI < LastHashTokPtr && TableIdx) {
220 // In the side-table we are still at an entry for a '#' token that
221 // is earlier than the last one we saw. Check if the location we would
222 // stride gets us closer.
Chris Lattnerefb35342009-01-18 01:57:14 +0000223 const unsigned char* NextPPCondPtr =
224 PPCond + TableIdx*(sizeof(uint32_t)*2);
Ted Kremenekbe3e84f2008-12-12 22:05:38 +0000225 assert(NextPPCondPtr >= CurPPCondPtr);
226 // Read where we should jump to.
Chris Lattner673a2862009-01-22 19:48:26 +0000227 uint32_t TmpOffset = ReadLE32(NextPPCondPtr);
Chris Lattnerefb35342009-01-18 01:57:14 +0000228 const unsigned char* HashEntryJ = TokBuf + TmpOffset;
Ted Kremenekbe3e84f2008-12-12 22:05:38 +0000229
230 if (HashEntryJ <= LastHashTokPtr) {
231 // Jump directly to the next entry in the side table.
232 HashEntryI = HashEntryJ;
233 Offset = TmpOffset;
Chris Lattner673a2862009-01-22 19:48:26 +0000234 TableIdx = ReadLE32(NextPPCondPtr);
Ted Kremenekbe3e84f2008-12-12 22:05:38 +0000235 CurPPCondPtr = NextPPCondPtr;
236 }
237 }
Ted Kremenekc07091c2008-12-12 18:34:08 +0000238 }
Ted Kremenekbe3e84f2008-12-12 22:05:38 +0000239 while (HashEntryI < LastHashTokPtr);
240 assert(HashEntryI == LastHashTokPtr && "No PP-cond entry found for '#'");
Ted Kremenekc07091c2008-12-12 18:34:08 +0000241 assert(TableIdx && "No jumping from #endifs.");
242
243 // Update our side-table iterator.
Chris Lattnerefb35342009-01-18 01:57:14 +0000244 const unsigned char* NextPPCondPtr = PPCond + TableIdx*(sizeof(uint32_t)*2);
Ted Kremenekc07091c2008-12-12 18:34:08 +0000245 assert(NextPPCondPtr >= CurPPCondPtr);
246 CurPPCondPtr = NextPPCondPtr;
247
248 // Read where we should jump to.
Chris Lattner673a2862009-01-22 19:48:26 +0000249 HashEntryI = TokBuf + ReadLE32(NextPPCondPtr);
250 uint32_t NextIdx = ReadLE32(NextPPCondPtr);
Ted Kremenekc07091c2008-12-12 18:34:08 +0000251
252 // By construction NextIdx will be zero if this is a #endif. This is useful
253 // to know to obviate lexing another token.
254 bool isEndif = NextIdx == 0;
Ted Kremenekc07091c2008-12-12 18:34:08 +0000255
256 // This case can occur when we see something like this:
257 //
258 // #if ...
259 // /* a comment or nothing */
260 // #elif
261 //
262 // If we are skipping the first #if block it will be the case that CurPtr
263 // already points 'elif'. Just return.
264
Ted Kremenekbe3e84f2008-12-12 22:05:38 +0000265 if (CurPtr > HashEntryI) {
266 assert(CurPtr == HashEntryI + DISK_TOKEN_SIZE);
Ted Kremenekc07091c2008-12-12 18:34:08 +0000267 // Did we reach a #endif? If so, go ahead and consume that token as well.
268 if (isEndif)
Ted Kremenek9ab79bf2008-12-23 01:30:52 +0000269 CurPtr += DISK_TOKEN_SIZE*2;
Ted Kremenekc07091c2008-12-12 18:34:08 +0000270 else
Ted Kremenekbe3e84f2008-12-12 22:05:38 +0000271 LastHashTokPtr = HashEntryI;
Ted Kremenekc07091c2008-12-12 18:34:08 +0000272
273 return isEndif;
274 }
275
276 // Otherwise, we need to advance. Update CurPtr to point to the '#' token.
Ted Kremenekbe3e84f2008-12-12 22:05:38 +0000277 CurPtr = HashEntryI;
Ted Kremenekc07091c2008-12-12 18:34:08 +0000278
279 // Update the location of the last observed '#'. This is useful if we
280 // are skipping multiple blocks.
281 LastHashTokPtr = CurPtr;
Ted Kremenekc07091c2008-12-12 18:34:08 +0000282
Ted Kremenek9ab79bf2008-12-23 01:30:52 +0000283 // Skip the '#' token.
Chris Lattnerefb35342009-01-18 01:57:14 +0000284 assert(((tok::TokenKind)*CurPtr) == tok::hash);
Ted Kremenek9ab79bf2008-12-23 01:30:52 +0000285 CurPtr += DISK_TOKEN_SIZE;
286
Ted Kremenekc07091c2008-12-12 18:34:08 +0000287 // Did we reach a #endif? If so, go ahead and consume that token as well.
Ted Kremenek9ab79bf2008-12-23 01:30:52 +0000288 if (isEndif) { CurPtr += DISK_TOKEN_SIZE*2; }
Ted Kremenekc07091c2008-12-12 18:34:08 +0000289
290 return isEndif;
291}
292
Ted Kremenekbeb57672008-12-17 23:36:32 +0000293SourceLocation PTHLexer::getSourceLocation() {
Chris Lattner0a6ec5d2009-01-18 02:10:31 +0000294 // getSourceLocation is not on the hot path. It is used to get the location
295 // of the next token when transitioning back to this lexer when done
Ted Kremenekbeb57672008-12-17 23:36:32 +0000296 // handling a #included file. Just read the necessary data from the token
297 // data buffer to construct the SourceLocation object.
298 // NOTE: This is a virtual function; hence it is defined out-of-line.
Ted Kremenekde04cb42009-01-21 22:41:38 +0000299 const unsigned char *OffsetPtr = CurPtr + (DISK_TOKEN_SIZE - 4);
Chris Lattner673a2862009-01-22 19:48:26 +0000300 uint32_t Offset = ReadLE32(OffsetPtr);
Chris Lattner0a6ec5d2009-01-18 02:10:31 +0000301 return FileStartLoc.getFileLocWithOffset(Offset);
Ted Kremenekbeb57672008-12-17 23:36:32 +0000302}
303
Ted Kremenekd310fde2009-01-09 22:05:30 +0000304//===----------------------------------------------------------------------===//
305// getSpelling() - Use cached data in PTH files for getSpelling().
306//===----------------------------------------------------------------------===//
307
Chris Lattnerf4f776a2009-01-17 06:22:33 +0000308unsigned PTHManager::getSpelling(FileID FID, unsigned FPos,
309 const char *&Buffer) {
310 llvm::DenseMap<FileID, PTHSpellingSearch*>::iterator I =SpellingMap.find(FID);
Ted Kremenekd310fde2009-01-09 22:05:30 +0000311
312 if (I == SpellingMap.end())
Chris Lattner0a6ec5d2009-01-18 02:10:31 +0000313 return 0;
Ted Kremenek21069422009-01-13 22:05:50 +0000314
Chris Lattnerf4f776a2009-01-17 06:22:33 +0000315 return I->second->getSpellingBinarySearch(FPos, Buffer);
316}
317
318unsigned PTHManager::getSpelling(SourceLocation Loc, const char *&Buffer) {
Chris Lattnere7fc9622009-01-17 06:29:33 +0000319 SourceManager &SM = PP->getSourceManager();
320 Loc = SM.getSpellingLoc(Loc);
321 std::pair<FileID, unsigned> LocInfo = SM.getDecomposedFileLoc(Loc);
Chris Lattnerf4f776a2009-01-17 06:22:33 +0000322 return getSpelling(LocInfo.first, LocInfo.second, Buffer);
Ted Kremenekd310fde2009-01-09 22:05:30 +0000323}
324
325unsigned PTHManager::getSpellingAtPTHOffset(unsigned PTHOffset,
Chris Lattnerefb35342009-01-18 01:57:14 +0000326 const char *&Buffer) {
Chris Lattner0a6ec5d2009-01-18 02:10:31 +0000327 assert(PTHOffset < Buf->getBufferSize());
328 const unsigned char* Ptr =
329 (const unsigned char*)Buf->getBufferStart() + PTHOffset;
Ted Kremeneka4d5bf52009-01-08 04:30:32 +0000330
331 // The string is prefixed by 16 bits for its length, followed by the string
332 // itself.
Chris Lattner673a2862009-01-22 19:48:26 +0000333 unsigned Len = ReadUnalignedLE16(Ptr);
Chris Lattner0a6ec5d2009-01-18 02:10:31 +0000334 Buffer = (const char *)Ptr;
335 return Len;
Ted Kremeneka4d5bf52009-01-08 04:30:32 +0000336}
337
Chris Lattner0a6ec5d2009-01-18 02:10:31 +0000338unsigned PTHSpellingSearch::getSpellingLinearSearch(unsigned FPos,
Ted Kremenekd310fde2009-01-09 22:05:30 +0000339 const char *&Buffer) {
Chris Lattner0a6ec5d2009-01-18 02:10:31 +0000340 const unsigned char *Ptr = LinearItr;
341 unsigned Len = 0;
Ted Kremenekd310fde2009-01-09 22:05:30 +0000342
Chris Lattner0a6ec5d2009-01-18 02:10:31 +0000343 if (Ptr == TableEnd)
344 return getSpellingBinarySearch(FPos, Buffer);
Ted Kremenekd310fde2009-01-09 22:05:30 +0000345
346 do {
Chris Lattner673a2862009-01-22 19:48:26 +0000347 uint32_t TokOffset = ReadLE32(Ptr);
Ted Kremeneka4d5bf52009-01-08 04:30:32 +0000348
Chris Lattner0a6ec5d2009-01-18 02:10:31 +0000349 if (TokOffset > FPos)
350 return getSpellingBinarySearch(FPos, Buffer);
Ted Kremeneka4d5bf52009-01-08 04:30:32 +0000351
Ted Kremeneka4d5bf52009-01-08 04:30:32 +0000352 // Did we find a matching token offset for this spelling?
Chris Lattner0a6ec5d2009-01-18 02:10:31 +0000353 if (TokOffset == FPos) {
Chris Lattner673a2862009-01-22 19:48:26 +0000354 uint32_t SpellingPTHOffset = ReadLE32(Ptr);
Chris Lattner0a6ec5d2009-01-18 02:10:31 +0000355 Len = PTHMgr.getSpellingAtPTHOffset(SpellingPTHOffset, Buffer);
Ted Kremeneka4d5bf52009-01-08 04:30:32 +0000356 break;
357 }
Chris Lattner0a6ec5d2009-01-18 02:10:31 +0000358 } while (Ptr != TableEnd);
Ted Kremeneka4d5bf52009-01-08 04:30:32 +0000359
Chris Lattner0a6ec5d2009-01-18 02:10:31 +0000360 LinearItr = Ptr;
361 return Len;
Ted Kremenekd2c849d2009-01-08 02:47:16 +0000362}
363
Chris Lattnerefb35342009-01-18 01:57:14 +0000364
Chris Lattner0a6ec5d2009-01-18 02:10:31 +0000365unsigned PTHSpellingSearch::getSpellingBinarySearch(unsigned FPos,
Chris Lattnerefb35342009-01-18 01:57:14 +0000366 const char *&Buffer) {
Ted Kremenekd310fde2009-01-09 22:05:30 +0000367
Ted Kremenek21069422009-01-13 22:05:50 +0000368 assert((TableEnd - TableBeg) % SpellingEntrySize == 0);
Chris Lattner0a6ec5d2009-01-18 02:10:31 +0000369 assert(TableEnd >= TableBeg);
Ted Kremenek21069422009-01-13 22:05:50 +0000370
371 if (TableEnd == TableBeg)
372 return 0;
373
Ted Kremenekd310fde2009-01-09 22:05:30 +0000374 unsigned min = 0;
Chris Lattner0a6ec5d2009-01-18 02:10:31 +0000375 const unsigned char *tb = TableBeg;
Ted Kremenek21069422009-01-13 22:05:50 +0000376 unsigned max = NumSpellings;
Ted Kremenekd310fde2009-01-09 22:05:30 +0000377
Ted Kremenek21069422009-01-13 22:05:50 +0000378 do {
Ted Kremenekd310fde2009-01-09 22:05:30 +0000379 unsigned i = (max - min) / 2 + min;
Chris Lattner0a6ec5d2009-01-18 02:10:31 +0000380 const unsigned char *Ptr = tb + (i * SpellingEntrySize);
Ted Kremenekd310fde2009-01-09 22:05:30 +0000381
Chris Lattner673a2862009-01-22 19:48:26 +0000382 uint32_t TokOffset = ReadLE32(Ptr);
Chris Lattner0a6ec5d2009-01-18 02:10:31 +0000383 if (TokOffset > FPos) {
Ted Kremenekd310fde2009-01-09 22:05:30 +0000384 max = i;
Ted Kremenek21069422009-01-13 22:05:50 +0000385 assert(!(max == min) || (min == i));
Ted Kremenekd310fde2009-01-09 22:05:30 +0000386 continue;
387 }
388
Chris Lattner0a6ec5d2009-01-18 02:10:31 +0000389 if (TokOffset < FPos) {
Ted Kremenekc20d0ab2009-01-13 22:16:45 +0000390 if (i == min)
391 break;
392
Ted Kremenekd310fde2009-01-09 22:05:30 +0000393 min = i;
394 continue;
395 }
396
Chris Lattner673a2862009-01-22 19:48:26 +0000397 uint32_t SpellingPTHOffset = ReadLE32(Ptr);
Ted Kremenekd310fde2009-01-09 22:05:30 +0000398 return PTHMgr.getSpellingAtPTHOffset(SpellingPTHOffset, Buffer);
399 }
Ted Kremenek21069422009-01-13 22:05:50 +0000400 while (min != max);
Ted Kremenekd310fde2009-01-09 22:05:30 +0000401
402 return 0;
403}
404
Chris Lattnerf4f776a2009-01-17 06:22:33 +0000405unsigned PTHLexer::getSpelling(SourceLocation Loc, const char *&Buffer) {
406 SourceManager &SM = PP->getSourceManager();
407 Loc = SM.getSpellingLoc(Loc);
408 std::pair<FileID, unsigned> LocInfo = SM.getDecomposedFileLoc(Loc);
409
410 FileID FID = LocInfo.first;
411 unsigned FPos = LocInfo.second;
Ted Kremenekd310fde2009-01-09 22:05:30 +0000412
Chris Lattnerf4f776a2009-01-17 06:22:33 +0000413 if (FID == getFileID())
414 return MySpellingSrch.getSpellingLinearSearch(FPos, Buffer);
415 return PTHMgr.getSpelling(FID, FPos, Buffer);
Ted Kremenekd310fde2009-01-09 22:05:30 +0000416}
417
Ted Kremenek325cd302008-12-03 00:38:03 +0000418//===----------------------------------------------------------------------===//
Ted Kremenek325cd302008-12-03 00:38:03 +0000419// Internal Data Structures for PTH file lookup and resolving identifiers.
420//===----------------------------------------------------------------------===//
421
Ted Kremenek325cd302008-12-03 00:38:03 +0000422
423/// PTHFileLookup - This internal data structure is used by the PTHManager
424/// to map from FileEntry objects managed by FileManager to offsets within
425/// the PTH file.
426namespace {
427class VISIBILITY_HIDDEN PTHFileLookup {
428public:
429 class Val {
Ted Kremenek8309c922008-12-11 23:36:38 +0000430 uint32_t TokenOff;
431 uint32_t PPCondOff;
Ted Kremenekd2c849d2009-01-08 02:47:16 +0000432 uint32_t SpellingOff;
Ted Kremenek325cd302008-12-03 00:38:03 +0000433 public:
Ted Kremenek8309c922008-12-11 23:36:38 +0000434 Val() : TokenOff(~0) {}
Ted Kremenekd2c849d2009-01-08 02:47:16 +0000435 Val(uint32_t toff, uint32_t poff, uint32_t soff)
436 : TokenOff(toff), PPCondOff(poff), SpellingOff(soff) {}
Ted Kremenek325cd302008-12-03 00:38:03 +0000437
Chris Lattner0a6ec5d2009-01-18 02:10:31 +0000438 bool isValid() const { return TokenOff != ~((uint32_t)0); }
439
Ted Kremenek8309c922008-12-11 23:36:38 +0000440 uint32_t getTokenOffset() const {
Chris Lattner0a6ec5d2009-01-18 02:10:31 +0000441 assert(isValid() && "PTHFileLookup entry initialized.");
Ted Kremenek8309c922008-12-11 23:36:38 +0000442 return TokenOff;
Ted Kremenek325cd302008-12-03 00:38:03 +0000443 }
444
Ted Kremenekd2c849d2009-01-08 02:47:16 +0000445 uint32_t getPPCondOffset() const {
Chris Lattner0a6ec5d2009-01-18 02:10:31 +0000446 assert(isValid() && "PTHFileLookup entry initialized.");
Ted Kremenek8309c922008-12-11 23:36:38 +0000447 return PPCondOff;
448 }
449
Ted Kremenekd2c849d2009-01-08 02:47:16 +0000450 uint32_t getSpellingOffset() const {
Chris Lattner0a6ec5d2009-01-18 02:10:31 +0000451 assert(isValid() && "PTHFileLookup entry initialized.");
Ted Kremenekd2c849d2009-01-08 02:47:16 +0000452 return SpellingOff;
453 }
Ted Kremenek325cd302008-12-03 00:38:03 +0000454 };
455
456private:
457 llvm::StringMap<Val> FileMap;
458
459public:
460 PTHFileLookup() {};
461
Ted Kremenekabaf7eb2009-01-21 07:34:28 +0000462 bool isEmpty() const {
463 return FileMap.empty();
464 }
465
Ted Kremenek325cd302008-12-03 00:38:03 +0000466 Val Lookup(const FileEntry* FE) {
467 const char* s = FE->getName();
468 unsigned size = strlen(s);
469 return FileMap.GetOrCreateValue(s, s+size).getValue();
470 }
471
Chris Lattnerefb35342009-01-18 01:57:14 +0000472 void ReadTable(const unsigned char* D) {
Chris Lattner673a2862009-01-22 19:48:26 +0000473 uint32_t N = ReadLE32(D); // Read the length of the table.
Ted Kremenek325cd302008-12-03 00:38:03 +0000474
475 for ( ; N > 0; --N) { // The rest of the data is the table itself.
Chris Lattner673a2862009-01-22 19:48:26 +0000476 uint32_t Len = ReadLE32(D);
Chris Lattnerefb35342009-01-18 01:57:14 +0000477 const char* s = (const char *)D;
Chris Lattner0a6ec5d2009-01-18 02:10:31 +0000478 D += Len;
Ted Kremenekd2c849d2009-01-08 02:47:16 +0000479
Chris Lattner673a2862009-01-22 19:48:26 +0000480 uint32_t TokenOff = ReadLE32(D);
481 uint32_t PPCondOff = ReadLE32(D);
482 uint32_t SpellingOff = ReadLE32(D);
Ted Kremenekd2c849d2009-01-08 02:47:16 +0000483
Chris Lattner0a6ec5d2009-01-18 02:10:31 +0000484 FileMap.GetOrCreateValue(s, s+Len).getValue() =
Ted Kremenekd2c849d2009-01-08 02:47:16 +0000485 Val(TokenOff, PPCondOff, SpellingOff);
Ted Kremenek325cd302008-12-03 00:38:03 +0000486 }
487 }
488};
489} // end anonymous namespace
490
491//===----------------------------------------------------------------------===//
492// PTHManager methods.
493//===----------------------------------------------------------------------===//
494
495PTHManager::PTHManager(const llvm::MemoryBuffer* buf, void* fileLookup,
Chris Lattnerefb35342009-01-18 01:57:14 +0000496 const unsigned char* idDataTable,
497 IdentifierInfo** perIDCache,
498 const unsigned char* sortedIdTable, unsigned numIds)
Ted Kremenekdb4c8e82008-12-03 01:16:39 +0000499: Buf(buf), PerIDCache(perIDCache), FileLookup(fileLookup),
Ted Kremenekd976c3d2009-01-15 18:47:46 +0000500 IdDataTable(idDataTable), SortedIdTable(sortedIdTable),
501 NumIds(numIds), PP(0) {}
Ted Kremenek325cd302008-12-03 00:38:03 +0000502
503PTHManager::~PTHManager() {
504 delete Buf;
505 delete (PTHFileLookup*) FileLookup;
Ted Kremenek93bdc492008-12-04 22:47:11 +0000506 free(PerIDCache);
Ted Kremenek325cd302008-12-03 00:38:03 +0000507}
508
Ted Kremenekd976c3d2009-01-15 18:47:46 +0000509PTHManager* PTHManager::Create(const std::string& file) {
Ted Kremenek325cd302008-12-03 00:38:03 +0000510 // Memory map the PTH file.
511 llvm::OwningPtr<llvm::MemoryBuffer>
512 File(llvm::MemoryBuffer::getFile(file.c_str()));
513
514 if (!File)
515 return 0;
516
517 // Get the buffer ranges and check if there are at least three 32-bit
518 // words at the end of the file.
Chris Lattnerefb35342009-01-18 01:57:14 +0000519 const unsigned char* BufBeg = (unsigned char*)File->getBufferStart();
520 const unsigned char* BufEnd = (unsigned char*)File->getBufferEnd();
Ted Kremenek325cd302008-12-03 00:38:03 +0000521
522 if(!(BufEnd > BufBeg + sizeof(uint32_t)*3)) {
523 assert(false && "Invalid PTH file.");
524 return 0; // FIXME: Proper error diagnostic?
525 }
526
527 // Compute the address of the index table at the end of the PTH file.
528 // This table contains the offset of the file lookup table, the
529 // persistent ID -> identifer data table.
Ted Kremenekd4d6d222009-01-15 01:26:25 +0000530 // FIXME: We should just embed this offset in the PTH file.
Chris Lattnerefb35342009-01-18 01:57:14 +0000531 const unsigned char* EndTable = BufEnd - sizeof(uint32_t)*4;
Ted Kremenek325cd302008-12-03 00:38:03 +0000532
533 // Construct the file lookup table. This will be used for mapping from
534 // FileEntry*'s to cached tokens.
Chris Lattnerefb35342009-01-18 01:57:14 +0000535 const unsigned char* FileTableOffset = EndTable + sizeof(uint32_t)*3;
Chris Lattner673a2862009-01-22 19:48:26 +0000536 const unsigned char* FileTable = BufBeg + ReadLE32(FileTableOffset);
Ted Kremenek325cd302008-12-03 00:38:03 +0000537
538 if (!(FileTable > BufBeg && FileTable < BufEnd)) {
539 assert(false && "Invalid PTH file.");
540 return 0; // FIXME: Proper error diagnostic?
541 }
542
543 llvm::OwningPtr<PTHFileLookup> FL(new PTHFileLookup());
544 FL->ReadTable(FileTable);
Ted Kremenekabaf7eb2009-01-21 07:34:28 +0000545
546 if (FL->isEmpty())
547 return 0;
Ted Kremenek325cd302008-12-03 00:38:03 +0000548
549 // Get the location of the table mapping from persistent ids to the
550 // data needed to reconstruct identifiers.
Chris Lattnerefb35342009-01-18 01:57:14 +0000551 const unsigned char* IDTableOffset = EndTable + sizeof(uint32_t)*1;
Chris Lattner673a2862009-01-22 19:48:26 +0000552 const unsigned char* IData = BufBeg + ReadLE32(IDTableOffset);
Ted Kremenekabaf7eb2009-01-21 07:34:28 +0000553
554 if (!(IData >= BufBeg && IData < BufEnd)) {
Ted Kremenek325cd302008-12-03 00:38:03 +0000555 assert(false && "Invalid PTH file.");
556 return 0; // FIXME: Proper error diagnostic?
557 }
558
Ted Kremenekd976c3d2009-01-15 18:47:46 +0000559 // Get the location of the lexigraphically-sorted table of persistent IDs.
Chris Lattnerefb35342009-01-18 01:57:14 +0000560 const unsigned char* SortedIdTableOffset = EndTable + sizeof(uint32_t)*2;
Chris Lattner673a2862009-01-22 19:48:26 +0000561 const unsigned char* SortedIdTable = BufBeg + ReadLE32(SortedIdTableOffset);
Ted Kremenekabaf7eb2009-01-21 07:34:28 +0000562 if (!(SortedIdTable >= BufBeg && SortedIdTable < BufEnd)) {
Ted Kremenekd976c3d2009-01-15 18:47:46 +0000563 assert(false && "Invalid PTH file.");
564 return 0; // FIXME: Proper error diagnostic?
565 }
566
Ted Kremenekdb4c8e82008-12-03 01:16:39 +0000567 // Get the number of IdentifierInfos and pre-allocate the identifier cache.
Chris Lattner673a2862009-01-22 19:48:26 +0000568 uint32_t NumIds = ReadLE32(IData);
Ted Kremenekabaf7eb2009-01-21 07:34:28 +0000569
Ted Kremenekdb4c8e82008-12-03 01:16:39 +0000570 // Pre-allocate the peristent ID -> IdentifierInfo* cache. We use calloc()
571 // so that we in the best case only zero out memory once when the OS returns
572 // us new pages.
Ted Kremenekabaf7eb2009-01-21 07:34:28 +0000573 IdentifierInfo** PerIDCache = 0;
Ted Kremenekdb4c8e82008-12-03 01:16:39 +0000574
Ted Kremenekabaf7eb2009-01-21 07:34:28 +0000575 if (NumIds) {
576 PerIDCache = (IdentifierInfo**)calloc(NumIds, sizeof(*PerIDCache));
577 if (!PerIDCache) {
578 assert(false && "Could not allocate Persistent ID cache.");
579 return 0;
580 }
Ted Kremenekdb4c8e82008-12-03 01:16:39 +0000581 }
Ted Kremenekabaf7eb2009-01-21 07:34:28 +0000582
Ted Kremenekd976c3d2009-01-15 18:47:46 +0000583 // Create the new PTHManager.
584 return new PTHManager(File.take(), FL.take(), IData, PerIDCache,
585 SortedIdTable, NumIds);
Ted Kremenek325cd302008-12-03 00:38:03 +0000586}
Chris Lattnercbcd26c2009-01-18 02:57:21 +0000587IdentifierInfo* PTHManager::LazilyCreateIdentifierInfo(unsigned PersistentID) {
Ted Kremenek325cd302008-12-03 00:38:03 +0000588 // Look in the PTH file for the string data for the IdentifierInfo object.
Chris Lattnercbcd26c2009-01-18 02:57:21 +0000589 const unsigned char* TableEntry = IdDataTable + sizeof(uint32_t)*PersistentID;
Chris Lattnerefb35342009-01-18 01:57:14 +0000590 const unsigned char* IDData =
Chris Lattner673a2862009-01-22 19:48:26 +0000591 (const unsigned char*)Buf->getBufferStart() + ReadLE32(TableEntry);
Chris Lattnerefb35342009-01-18 01:57:14 +0000592 assert(IDData < (const unsigned char*)Buf->getBufferEnd());
Ted Kremenek325cd302008-12-03 00:38:03 +0000593
Ted Kremenekd976c3d2009-01-15 18:47:46 +0000594 // Allocate the object.
Chris Lattnerefb35342009-01-18 01:57:14 +0000595 std::pair<IdentifierInfo,const unsigned char*> *Mem =
596 Alloc.Allocate<std::pair<IdentifierInfo,const unsigned char*> >();
Ted Kremenekd976c3d2009-01-15 18:47:46 +0000597
598 Mem->second = IDData;
Ted Kremeneke807f852009-01-20 23:28:34 +0000599 IdentifierInfo *II = new ((void*) Mem) IdentifierInfo();
Ted Kremenek325cd302008-12-03 00:38:03 +0000600
Ted Kremenekd976c3d2009-01-15 18:47:46 +0000601 // Store the new IdentifierInfo in the cache.
Chris Lattnercbcd26c2009-01-18 02:57:21 +0000602 PerIDCache[PersistentID] = II;
Ted Kremenek325cd302008-12-03 00:38:03 +0000603 return II;
604}
605
Ted Kremenekd976c3d2009-01-15 18:47:46 +0000606IdentifierInfo* PTHManager::get(const char *NameStart, const char *NameEnd) {
607 unsigned min = 0;
608 unsigned max = NumIds;
Chris Lattner0a6ec5d2009-01-18 02:10:31 +0000609 unsigned Len = NameEnd - NameStart;
Ted Kremenekd976c3d2009-01-15 18:47:46 +0000610
611 do {
612 unsigned i = (max - min) / 2 + min;
Chris Lattner0a6ec5d2009-01-18 02:10:31 +0000613 const unsigned char *Ptr = SortedIdTable + (i * 4);
Ted Kremenekd976c3d2009-01-15 18:47:46 +0000614
615 // Read the persistentID.
Chris Lattner673a2862009-01-22 19:48:26 +0000616 unsigned perID = ReadLE32(Ptr);
Ted Kremenekd976c3d2009-01-15 18:47:46 +0000617
618 // Get the IdentifierInfo.
619 IdentifierInfo* II = GetIdentifierInfo(perID);
620
621 // First compare the lengths.
622 unsigned IILen = II->getLength();
Chris Lattner0a6ec5d2009-01-18 02:10:31 +0000623 if (Len < IILen) goto IsLess;
624 if (Len > IILen) goto IsGreater;
Ted Kremenekd976c3d2009-01-15 18:47:46 +0000625
626 // Now compare the strings!
627 {
Chris Lattner0a6ec5d2009-01-18 02:10:31 +0000628 signed comp = strncmp(NameStart, II->getName(), Len);
Ted Kremenekd976c3d2009-01-15 18:47:46 +0000629 if (comp < 0) goto IsLess;
630 if (comp > 0) goto IsGreater;
631 }
632 // We found a match!
633 return II;
634
635 IsGreater:
636 if (i == min) break;
637 min = i;
638 continue;
639
640 IsLess:
641 max = i;
642 assert(!(max == min) || (min == i));
643 }
Ted Kremenek4795ad02009-01-15 19:28:38 +0000644 while (min != max);
Ted Kremenekd976c3d2009-01-15 18:47:46 +0000645
646 return 0;
647}
648
649
Chris Lattner3c727d62009-01-17 08:06:50 +0000650PTHLexer *PTHManager::CreateLexer(FileID FID) {
651 const FileEntry *FE = PP->getSourceManager().getFileEntryForID(FID);
Ted Kremenek325cd302008-12-03 00:38:03 +0000652 if (!FE)
653 return 0;
654
655 // Lookup the FileEntry object in our file lookup data structure. It will
656 // return a variant that indicates whether or not there is an offset within
657 // the PTH file that contains cached tokens.
Chris Lattnerf4f776a2009-01-17 06:22:33 +0000658 PTHFileLookup::Val FileData = ((PTHFileLookup*)FileLookup)->Lookup(FE);
Ted Kremenek325cd302008-12-03 00:38:03 +0000659
Ted Kremenek8309c922008-12-11 23:36:38 +0000660 if (!FileData.isValid()) // No tokens available.
Ted Kremenek325cd302008-12-03 00:38:03 +0000661 return 0;
662
Chris Lattnerefb35342009-01-18 01:57:14 +0000663 const unsigned char *BufStart = (const unsigned char *)Buf->getBufferStart();
Ted Kremenek325cd302008-12-03 00:38:03 +0000664 // Compute the offset of the token data within the buffer.
Chris Lattnerefb35342009-01-18 01:57:14 +0000665 const unsigned char* data = BufStart + FileData.getTokenOffset();
Ted Kremenekc07091c2008-12-12 18:34:08 +0000666
667 // Get the location of pp-conditional table.
Chris Lattnerefb35342009-01-18 01:57:14 +0000668 const unsigned char* ppcond = BufStart + FileData.getPPCondOffset();
Chris Lattner673a2862009-01-22 19:48:26 +0000669 uint32_t Len = ReadLE32(ppcond);
Chris Lattner0a6ec5d2009-01-18 02:10:31 +0000670 if (Len == 0) ppcond = 0;
Ted Kremeneka4d5bf52009-01-08 04:30:32 +0000671
672 // Get the location of the spelling table.
Chris Lattnerefb35342009-01-18 01:57:14 +0000673 const unsigned char* spellingTable = BufStart + FileData.getSpellingOffset();
Ted Kremeneka4d5bf52009-01-08 04:30:32 +0000674
Chris Lattner673a2862009-01-22 19:48:26 +0000675 Len = ReadLE32(spellingTable);
Chris Lattner0a6ec5d2009-01-18 02:10:31 +0000676 if (Len == 0) spellingTable = 0;
Ted Kremenekd2c849d2009-01-08 02:47:16 +0000677
Chris Lattnerefb35342009-01-18 01:57:14 +0000678 assert(data < (const unsigned char*)Buf->getBufferEnd());
Ted Kremenekd310fde2009-01-09 22:05:30 +0000679
680 // Create the SpellingSearch object for this FileID.
Chris Lattner0a6ec5d2009-01-18 02:10:31 +0000681 PTHSpellingSearch* ss = new PTHSpellingSearch(*this, Len, spellingTable);
Chris Lattnerf4f776a2009-01-17 06:22:33 +0000682 SpellingMap[FID] = ss;
Ted Kremenekd310fde2009-01-09 22:05:30 +0000683
Ted Kremenekd976c3d2009-01-15 18:47:46 +0000684 assert(PP && "No preprocessor set yet!");
Chris Lattnerf4f776a2009-01-17 06:22:33 +0000685 return new PTHLexer(*PP, FID, data, ppcond, *ss, *this);
Ted Kremenek325cd302008-12-03 00:38:03 +0000686}