blob: 4ae3a7f93e3002d41637abff85176926955dae8d [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 Kremenek8186f822008-12-23 18:41:34 +000028#define DISK_TOKEN_SIZE (1+1+3+4+2)
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) {
41 uint16_t V = ((uint16_t)Data[0] << 0) |
42 ((uint16_t)Data[1] << 8);
43 Data += 2;
44 return V;
45}
46
47static inline uint32_t Read24(const unsigned char *&Data) {
48 uint32_t V = ((uint32_t)Data[0] << 0) |
49 ((uint32_t)Data[1] << 8) |
50 ((uint32_t)Data[2] << 16);
51 Data += 3;
52 return V;
53}
54
55static inline uint32_t Read32(const unsigned char *&Data) {
56 uint32_t V = ((uint32_t)Data[0] << 0) |
57 ((uint32_t)Data[1] << 8) |
58 ((uint32_t)Data[2] << 16) |
59 ((uint32_t)Data[3] << 24);
60 Data += 4;
61 return V;
62}
63
64
Ted Kremenek9ab79bf2008-12-23 01:30:52 +000065//===----------------------------------------------------------------------===//
66// PTHLexer methods.
67//===----------------------------------------------------------------------===//
68
Chris Lattnerefb35342009-01-18 01:57:14 +000069PTHLexer::PTHLexer(Preprocessor &PP, FileID FID, const unsigned char *D,
70 const unsigned char *ppcond,
Chris Lattnerf4f776a2009-01-17 06:22:33 +000071 PTHSpellingSearch &mySpellingSrch, PTHManager &PM)
72 : PreprocessorLexer(&PP, FID), TokBuf(D), CurPtr(D), LastHashTokPtr(0),
Ted Kremenekd310fde2009-01-09 22:05:30 +000073 PPCond(ppcond), CurPPCondPtr(ppcond), MySpellingSrch(mySpellingSrch),
Chris Lattnerf4f776a2009-01-17 06:22:33 +000074 PTHMgr(PM) {
75
76 FileStartLoc = PP.getSourceManager().getLocForStartOfFile(FID);
Ted Kremenekd310fde2009-01-09 22:05:30 +000077}
Ted Kremenek9ab79bf2008-12-23 01:30:52 +000078
79void PTHLexer::Lex(Token& Tok) {
80LexNextToken:
Ted Kremenek9ab79bf2008-12-23 01:30:52 +000081
Ted Kremenek08d3ff32008-12-23 02:30:15 +000082 //===--------------------------------------==//
83 // Read the raw token data.
84 //===--------------------------------------==//
85
86 // Shadow CurPtr into an automatic variable.
Chris Lattnerefb35342009-01-18 01:57:14 +000087 const unsigned char *CurPtrShadow = CurPtr;
Ted Kremenek08d3ff32008-12-23 02:30:15 +000088
89 // Read in the data for the token. 14 bytes in total.
90 tok::TokenKind k = (tok::TokenKind) CurPtrShadow[0];
91 Token::TokenFlags flags = (Token::TokenFlags) CurPtrShadow[1];
92
Ted Kremenek6176cd62008-12-23 18:27:26 +000093 uint32_t perID = ((uint32_t) CurPtrShadow[2])
Ted Kremenek08d3ff32008-12-23 02:30:15 +000094 | (((uint32_t) CurPtrShadow[3]) << 8)
Ted Kremenek8186f822008-12-23 18:41:34 +000095 | (((uint32_t) CurPtrShadow[4]) << 16);
Ted Kremenek08d3ff32008-12-23 02:30:15 +000096
Ted Kremenek8186f822008-12-23 18:41:34 +000097 uint32_t FileOffset = ((uint32_t) CurPtrShadow[5])
98 | (((uint32_t) CurPtrShadow[6]) << 8)
99 | (((uint32_t) CurPtrShadow[7]) << 16)
100 | (((uint32_t) CurPtrShadow[8]) << 24);
Ted Kremenek08d3ff32008-12-23 02:30:15 +0000101
Ted Kremenek8186f822008-12-23 18:41:34 +0000102 uint32_t Len = ((uint32_t) CurPtrShadow[9])
103 | (((uint32_t) CurPtrShadow[10]) << 8);
Ted Kremenek08d3ff32008-12-23 02:30:15 +0000104
Chris Lattnerefb35342009-01-18 01:57:14 +0000105 CurPtr = CurPtrShadow + DISK_TOKEN_SIZE;
Ted Kremenek08d3ff32008-12-23 02:30:15 +0000106
107 //===--------------------------------------==//
108 // Construct the token itself.
109 //===--------------------------------------==//
110
111 Tok.startToken();
112 Tok.setKind(k);
Ted Kremenekd94668d2008-12-23 19:24:24 +0000113 Tok.setFlag(flags);
114 assert(!LexingRawMode);
Ted Kremenek6176cd62008-12-23 18:27:26 +0000115 Tok.setIdentifierInfo(perID ? PTHMgr.GetIdentifierInfo(perID-1) : 0);
Chris Lattnerf4f776a2009-01-17 06:22:33 +0000116 Tok.setLocation(FileStartLoc.getFileLocWithOffset(FileOffset));
Ted Kremenek08d3ff32008-12-23 02:30:15 +0000117 Tok.setLength(Len);
118
119 //===--------------------------------------==//
120 // Process the token.
121 //===--------------------------------------==//
Ted Kremenekd310fde2009-01-09 22:05:30 +0000122#if 0
123 SourceManager& SM = PP->getSourceManager();
124 llvm::cerr << SM.getFileEntryForID(FileID)->getName()
125 << ':' << SM.getLogicalLineNumber(Tok.getLocation())
126 << ':' << SM.getLogicalColumnNumber(Tok.getLocation())
127 << '\n';
128#endif
Ted Kremenek9ab79bf2008-12-23 01:30:52 +0000129
Ted Kremenekd94668d2008-12-23 19:24:24 +0000130 if (k == tok::identifier) {
131 MIOpt.ReadToken();
132 return PP->HandleIdentifier(Tok);
133 }
134
135 if (k == tok::eof) {
Ted Kremenek9ab79bf2008-12-23 01:30:52 +0000136 // Save the end-of-file token.
137 EofToken = Tok;
138
139 Preprocessor *PPCache = PP;
Ted Kremenekd94668d2008-12-23 19:24:24 +0000140
141 assert(!ParsingPreprocessorDirective);
142 assert(!LexingRawMode);
143
144 // FIXME: Issue diagnostics similar to Lexer.
145 if (PP->HandleEndOfFile(Tok, false))
Ted Kremenek9ab79bf2008-12-23 01:30:52 +0000146 return;
Ted Kremenekd94668d2008-12-23 19:24:24 +0000147
Ted Kremenek9ab79bf2008-12-23 01:30:52 +0000148 assert(PPCache && "Raw buffer::LexEndOfFile should return a token");
149 return PPCache->Lex(Tok);
150 }
Ted Kremenek9ab79bf2008-12-23 01:30:52 +0000151
Ted Kremenekd94668d2008-12-23 19:24:24 +0000152 if (k == tok::hash && Tok.isAtStartOfLine()) {
153 LastHashTokPtr = CurPtr - DISK_TOKEN_SIZE;
154 assert(!LexingRawMode);
155 PP->HandleDirective(Tok);
156
157 if (PP->isCurrentLexer(this))
158 goto LexNextToken;
159
160 return PP->Lex(Tok);
161 }
162
163 if (k == tok::eom) {
164 assert(ParsingPreprocessorDirective);
Ted Kremenek9ab79bf2008-12-23 01:30:52 +0000165 ParsingPreprocessorDirective = false;
166 return;
167 }
Ted Kremenek9ab79bf2008-12-23 01:30:52 +0000168
Ted Kremenekd94668d2008-12-23 19:24:24 +0000169 MIOpt.ReadToken();
Ted Kremenek9ab79bf2008-12-23 01:30:52 +0000170}
171
172// FIXME: We can just grab the last token instead of storing a copy
173// into EofToken.
Ted Kremenekd94668d2008-12-23 19:24:24 +0000174void PTHLexer::getEOF(Token& Tok) {
Ted Kremeneke4caf142009-01-09 00:36:11 +0000175 assert(EofToken.is(tok::eof));
Ted Kremenek9ab79bf2008-12-23 01:30:52 +0000176 Tok = EofToken;
177}
178
179void PTHLexer::DiscardToEndOfLine() {
180 assert(ParsingPreprocessorDirective && ParsingFilename == false &&
181 "Must be in a preprocessing directive!");
182
183 // We assume that if the preprocessor wishes to discard to the end of
184 // the line that it also means to end the current preprocessor directive.
185 ParsingPreprocessorDirective = false;
186
187 // Skip tokens by only peeking at their token kind and the flags.
188 // We don't need to actually reconstruct full tokens from the token buffer.
189 // This saves some copies and it also reduces IdentifierInfo* lookup.
Chris Lattnerefb35342009-01-18 01:57:14 +0000190 const unsigned char* p = CurPtr;
Ted Kremenek9ab79bf2008-12-23 01:30:52 +0000191 while (1) {
192 // Read the token kind. Are we at the end of the file?
193 tok::TokenKind x = (tok::TokenKind) (uint8_t) *p;
194 if (x == tok::eof) break;
195
196 // Read the token flags. Are we at the start of the next line?
197 Token::TokenFlags y = (Token::TokenFlags) (uint8_t) p[1];
198 if (y & Token::StartOfLine) break;
199
200 // Skip to the next token.
201 p += DISK_TOKEN_SIZE;
202 }
203
204 CurPtr = p;
205}
206
Ted Kremenekc07091c2008-12-12 18:34:08 +0000207/// SkipBlock - Used by Preprocessor to skip the current conditional block.
208bool PTHLexer::SkipBlock() {
209 assert(CurPPCondPtr && "No cached PP conditional information.");
210 assert(LastHashTokPtr && "No known '#' token.");
211
Chris Lattnerefb35342009-01-18 01:57:14 +0000212 const unsigned char* HashEntryI = 0;
Ted Kremenekc07091c2008-12-12 18:34:08 +0000213 uint32_t Offset;
214 uint32_t TableIdx;
215
216 do {
Ted Kremenekbe3e84f2008-12-12 22:05:38 +0000217 // Read the token offset from the side-table.
Ted Kremenekc07091c2008-12-12 18:34:08 +0000218 Offset = Read32(CurPPCondPtr);
Ted Kremenekbe3e84f2008-12-12 22:05:38 +0000219
220 // Read the target table index from the side-table.
Ted Kremenekc07091c2008-12-12 18:34:08 +0000221 TableIdx = Read32(CurPPCondPtr);
Ted Kremenekbe3e84f2008-12-12 22:05:38 +0000222
223 // Compute the actual memory address of the '#' token data for this entry.
224 HashEntryI = TokBuf + Offset;
225
226 // Optmization: "Sibling jumping". #if...#else...#endif blocks can
227 // contain nested blocks. In the side-table we can jump over these
228 // nested blocks instead of doing a linear search if the next "sibling"
229 // entry is not at a location greater than LastHashTokPtr.
230 if (HashEntryI < LastHashTokPtr && TableIdx) {
231 // In the side-table we are still at an entry for a '#' token that
232 // is earlier than the last one we saw. Check if the location we would
233 // stride gets us closer.
Chris Lattnerefb35342009-01-18 01:57:14 +0000234 const unsigned char* NextPPCondPtr =
235 PPCond + TableIdx*(sizeof(uint32_t)*2);
Ted Kremenekbe3e84f2008-12-12 22:05:38 +0000236 assert(NextPPCondPtr >= CurPPCondPtr);
237 // Read where we should jump to.
238 uint32_t TmpOffset = Read32(NextPPCondPtr);
Chris Lattnerefb35342009-01-18 01:57:14 +0000239 const unsigned char* HashEntryJ = TokBuf + TmpOffset;
Ted Kremenekbe3e84f2008-12-12 22:05:38 +0000240
241 if (HashEntryJ <= LastHashTokPtr) {
242 // Jump directly to the next entry in the side table.
243 HashEntryI = HashEntryJ;
244 Offset = TmpOffset;
245 TableIdx = Read32(NextPPCondPtr);
246 CurPPCondPtr = NextPPCondPtr;
247 }
248 }
Ted Kremenekc07091c2008-12-12 18:34:08 +0000249 }
Ted Kremenekbe3e84f2008-12-12 22:05:38 +0000250 while (HashEntryI < LastHashTokPtr);
251 assert(HashEntryI == LastHashTokPtr && "No PP-cond entry found for '#'");
Ted Kremenekc07091c2008-12-12 18:34:08 +0000252 assert(TableIdx && "No jumping from #endifs.");
253
254 // Update our side-table iterator.
Chris Lattnerefb35342009-01-18 01:57:14 +0000255 const unsigned char* NextPPCondPtr = PPCond + TableIdx*(sizeof(uint32_t)*2);
Ted Kremenekc07091c2008-12-12 18:34:08 +0000256 assert(NextPPCondPtr >= CurPPCondPtr);
257 CurPPCondPtr = NextPPCondPtr;
258
259 // Read where we should jump to.
Ted Kremenekbe3e84f2008-12-12 22:05:38 +0000260 HashEntryI = TokBuf + Read32(NextPPCondPtr);
Ted Kremenekc07091c2008-12-12 18:34:08 +0000261 uint32_t NextIdx = Read32(NextPPCondPtr);
262
263 // By construction NextIdx will be zero if this is a #endif. This is useful
264 // to know to obviate lexing another token.
265 bool isEndif = NextIdx == 0;
Ted Kremenekc07091c2008-12-12 18:34:08 +0000266
267 // This case can occur when we see something like this:
268 //
269 // #if ...
270 // /* a comment or nothing */
271 // #elif
272 //
273 // If we are skipping the first #if block it will be the case that CurPtr
274 // already points 'elif'. Just return.
275
Ted Kremenekbe3e84f2008-12-12 22:05:38 +0000276 if (CurPtr > HashEntryI) {
277 assert(CurPtr == HashEntryI + DISK_TOKEN_SIZE);
Ted Kremenekc07091c2008-12-12 18:34:08 +0000278 // Did we reach a #endif? If so, go ahead and consume that token as well.
279 if (isEndif)
Ted Kremenek9ab79bf2008-12-23 01:30:52 +0000280 CurPtr += DISK_TOKEN_SIZE*2;
Ted Kremenekc07091c2008-12-12 18:34:08 +0000281 else
Ted Kremenekbe3e84f2008-12-12 22:05:38 +0000282 LastHashTokPtr = HashEntryI;
Ted Kremenekc07091c2008-12-12 18:34:08 +0000283
284 return isEndif;
285 }
286
287 // Otherwise, we need to advance. Update CurPtr to point to the '#' token.
Ted Kremenekbe3e84f2008-12-12 22:05:38 +0000288 CurPtr = HashEntryI;
Ted Kremenekc07091c2008-12-12 18:34:08 +0000289
290 // Update the location of the last observed '#'. This is useful if we
291 // are skipping multiple blocks.
292 LastHashTokPtr = CurPtr;
Ted Kremenekc07091c2008-12-12 18:34:08 +0000293
Ted Kremenek9ab79bf2008-12-23 01:30:52 +0000294 // Skip the '#' token.
Chris Lattnerefb35342009-01-18 01:57:14 +0000295 assert(((tok::TokenKind)*CurPtr) == tok::hash);
Ted Kremenek9ab79bf2008-12-23 01:30:52 +0000296 CurPtr += DISK_TOKEN_SIZE;
297
Ted Kremenekc07091c2008-12-12 18:34:08 +0000298 // Did we reach a #endif? If so, go ahead and consume that token as well.
Ted Kremenek9ab79bf2008-12-23 01:30:52 +0000299 if (isEndif) { CurPtr += DISK_TOKEN_SIZE*2; }
Ted Kremenekc07091c2008-12-12 18:34:08 +0000300
301 return isEndif;
302}
303
Ted Kremenekbeb57672008-12-17 23:36:32 +0000304SourceLocation PTHLexer::getSourceLocation() {
305 // getLocation is not on the hot path. It is used to get the location of
306 // the next token when transitioning back to this lexer when done
307 // handling a #included file. Just read the necessary data from the token
308 // data buffer to construct the SourceLocation object.
309 // NOTE: This is a virtual function; hence it is defined out-of-line.
Chris Lattnerefb35342009-01-18 01:57:14 +0000310 const unsigned char* p = CurPtr + (1 + 1 + 3);
Ted Kremenekbeb57672008-12-17 23:36:32 +0000311 uint32_t offset =
312 ((uint32_t) ((uint8_t) p[0]))
313 | (((uint32_t) ((uint8_t) p[1])) << 8)
314 | (((uint32_t) ((uint8_t) p[2])) << 16)
315 | (((uint32_t) ((uint8_t) p[3])) << 24);
Chris Lattnerf4f776a2009-01-17 06:22:33 +0000316 return FileStartLoc.getFileLocWithOffset(offset);
Ted Kremenekbeb57672008-12-17 23:36:32 +0000317}
318
Ted Kremenekd310fde2009-01-09 22:05:30 +0000319//===----------------------------------------------------------------------===//
320// getSpelling() - Use cached data in PTH files for getSpelling().
321//===----------------------------------------------------------------------===//
322
Chris Lattnerf4f776a2009-01-17 06:22:33 +0000323unsigned PTHManager::getSpelling(FileID FID, unsigned FPos,
324 const char *&Buffer) {
325 llvm::DenseMap<FileID, PTHSpellingSearch*>::iterator I =SpellingMap.find(FID);
Ted Kremenekd310fde2009-01-09 22:05:30 +0000326
327 if (I == SpellingMap.end())
328 return 0;
Ted Kremenek21069422009-01-13 22:05:50 +0000329
Chris Lattnerf4f776a2009-01-17 06:22:33 +0000330 return I->second->getSpellingBinarySearch(FPos, Buffer);
331}
332
333unsigned PTHManager::getSpelling(SourceLocation Loc, const char *&Buffer) {
Chris Lattnere7fc9622009-01-17 06:29:33 +0000334 SourceManager &SM = PP->getSourceManager();
335 Loc = SM.getSpellingLoc(Loc);
336 std::pair<FileID, unsigned> LocInfo = SM.getDecomposedFileLoc(Loc);
Chris Lattnerf4f776a2009-01-17 06:22:33 +0000337 return getSpelling(LocInfo.first, LocInfo.second, Buffer);
Ted Kremenekd310fde2009-01-09 22:05:30 +0000338}
339
340unsigned PTHManager::getSpellingAtPTHOffset(unsigned PTHOffset,
Chris Lattnerefb35342009-01-18 01:57:14 +0000341 const char *&Buffer) {
Ted Kremenekd310fde2009-01-09 22:05:30 +0000342
Ted Kremeneka4d5bf52009-01-08 04:30:32 +0000343 const char* p = Buf->getBufferStart() + PTHOffset;
344 assert(p < Buf->getBufferEnd());
345
346 // The string is prefixed by 16 bits for its length, followed by the string
347 // itself.
348 unsigned len = ((unsigned) ((uint8_t) p[0]))
349 | (((unsigned) ((uint8_t) p[1])) << 8);
350
351 Buffer = p + 2;
352 return len;
353}
354
Ted Kremenekd310fde2009-01-09 22:05:30 +0000355unsigned PTHSpellingSearch::getSpellingLinearSearch(unsigned fpos,
356 const char *&Buffer) {
Chris Lattnerefb35342009-01-18 01:57:14 +0000357 const unsigned char* p = LinearItr;
Ted Kremeneka4d5bf52009-01-08 04:30:32 +0000358 unsigned len = 0;
Ted Kremenekd310fde2009-01-09 22:05:30 +0000359
Ted Kremenek21069422009-01-13 22:05:50 +0000360 if (p == TableEnd)
Ted Kremenekd310fde2009-01-09 22:05:30 +0000361 return getSpellingBinarySearch(fpos, Buffer);
362
363 do {
Ted Kremeneka4d5bf52009-01-08 04:30:32 +0000364 uint32_t TokOffset =
365 ((uint32_t) ((uint8_t) p[0]))
366 | (((uint32_t) ((uint8_t) p[1])) << 8)
367 | (((uint32_t) ((uint8_t) p[2])) << 16)
368 | (((uint32_t) ((uint8_t) p[3])) << 24);
369
370 if (TokOffset > fpos)
Ted Kremenekd310fde2009-01-09 22:05:30 +0000371 return getSpellingBinarySearch(fpos, Buffer);
Ted Kremeneka4d5bf52009-01-08 04:30:32 +0000372
Ted Kremeneka4d5bf52009-01-08 04:30:32 +0000373 // Did we find a matching token offset for this spelling?
374 if (TokOffset == fpos) {
375 uint32_t SpellingPTHOffset =
376 ((uint32_t) ((uint8_t) p[4]))
377 | (((uint32_t) ((uint8_t) p[5])) << 8)
378 | (((uint32_t) ((uint8_t) p[6])) << 16)
379 | (((uint32_t) ((uint8_t) p[7])) << 24);
380
Ted Kremenek21069422009-01-13 22:05:50 +0000381 p += SpellingEntrySize;
Ted Kremenekd310fde2009-01-09 22:05:30 +0000382 len = PTHMgr.getSpellingAtPTHOffset(SpellingPTHOffset, Buffer);
Ted Kremeneka4d5bf52009-01-08 04:30:32 +0000383 break;
384 }
385
386 // No match. Keep on looking.
Ted Kremenek21069422009-01-13 22:05:50 +0000387 p += SpellingEntrySize;
Ted Kremeneka4d5bf52009-01-08 04:30:32 +0000388 }
Ted Kremenek21069422009-01-13 22:05:50 +0000389 while (p != TableEnd);
Ted Kremeneka4d5bf52009-01-08 04:30:32 +0000390
Ted Kremenekd310fde2009-01-09 22:05:30 +0000391 LinearItr = p;
Ted Kremeneka4d5bf52009-01-08 04:30:32 +0000392 return len;
Ted Kremenekd2c849d2009-01-08 02:47:16 +0000393}
394
Chris Lattnerefb35342009-01-18 01:57:14 +0000395
Ted Kremenekd310fde2009-01-09 22:05:30 +0000396unsigned PTHSpellingSearch::getSpellingBinarySearch(unsigned fpos,
Chris Lattnerefb35342009-01-18 01:57:14 +0000397 const char *&Buffer) {
Ted Kremenekd310fde2009-01-09 22:05:30 +0000398
Ted Kremenek21069422009-01-13 22:05:50 +0000399 assert((TableEnd - TableBeg) % SpellingEntrySize == 0);
400
401 if (TableEnd == TableBeg)
402 return 0;
403
404 assert(TableEnd > TableBeg);
Ted Kremenekd310fde2009-01-09 22:05:30 +0000405
406 unsigned min = 0;
Chris Lattnerefb35342009-01-18 01:57:14 +0000407 const unsigned char* tb = TableBeg;
Ted Kremenek21069422009-01-13 22:05:50 +0000408 unsigned max = NumSpellings;
Ted Kremenekd310fde2009-01-09 22:05:30 +0000409
Ted Kremenek21069422009-01-13 22:05:50 +0000410 do {
Ted Kremenekd310fde2009-01-09 22:05:30 +0000411 unsigned i = (max - min) / 2 + min;
Chris Lattnerefb35342009-01-18 01:57:14 +0000412 const unsigned char* p = tb + (i * SpellingEntrySize);
Ted Kremenekd310fde2009-01-09 22:05:30 +0000413
414 uint32_t TokOffset =
415 ((uint32_t) ((uint8_t) p[0]))
416 | (((uint32_t) ((uint8_t) p[1])) << 8)
417 | (((uint32_t) ((uint8_t) p[2])) << 16)
418 | (((uint32_t) ((uint8_t) p[3])) << 24);
419
420 if (TokOffset > fpos) {
421 max = i;
Ted Kremenek21069422009-01-13 22:05:50 +0000422 assert(!(max == min) || (min == i));
Ted Kremenekd310fde2009-01-09 22:05:30 +0000423 continue;
424 }
425
426 if (TokOffset < fpos) {
Ted Kremenekc20d0ab2009-01-13 22:16:45 +0000427 if (i == min)
428 break;
429
Ted Kremenekd310fde2009-01-09 22:05:30 +0000430 min = i;
431 continue;
432 }
433
434 uint32_t SpellingPTHOffset =
435 ((uint32_t) ((uint8_t) p[4]))
436 | (((uint32_t) ((uint8_t) p[5])) << 8)
437 | (((uint32_t) ((uint8_t) p[6])) << 16)
438 | (((uint32_t) ((uint8_t) p[7])) << 24);
439
440 return PTHMgr.getSpellingAtPTHOffset(SpellingPTHOffset, Buffer);
441 }
Ted Kremenek21069422009-01-13 22:05:50 +0000442 while (min != max);
Ted Kremenekd310fde2009-01-09 22:05:30 +0000443
444 return 0;
445}
446
Chris Lattnerf4f776a2009-01-17 06:22:33 +0000447unsigned PTHLexer::getSpelling(SourceLocation Loc, const char *&Buffer) {
448 SourceManager &SM = PP->getSourceManager();
449 Loc = SM.getSpellingLoc(Loc);
450 std::pair<FileID, unsigned> LocInfo = SM.getDecomposedFileLoc(Loc);
451
452 FileID FID = LocInfo.first;
453 unsigned FPos = LocInfo.second;
Ted Kremenekd310fde2009-01-09 22:05:30 +0000454
Chris Lattnerf4f776a2009-01-17 06:22:33 +0000455 if (FID == getFileID())
456 return MySpellingSrch.getSpellingLinearSearch(FPos, Buffer);
457 return PTHMgr.getSpelling(FID, FPos, Buffer);
Ted Kremenekd310fde2009-01-09 22:05:30 +0000458}
459
Ted Kremenek325cd302008-12-03 00:38:03 +0000460//===----------------------------------------------------------------------===//
Ted Kremenek325cd302008-12-03 00:38:03 +0000461// Internal Data Structures for PTH file lookup and resolving identifiers.
462//===----------------------------------------------------------------------===//
463
Ted Kremenek325cd302008-12-03 00:38:03 +0000464
465/// PTHFileLookup - This internal data structure is used by the PTHManager
466/// to map from FileEntry objects managed by FileManager to offsets within
467/// the PTH file.
468namespace {
469class VISIBILITY_HIDDEN PTHFileLookup {
470public:
471 class Val {
Ted Kremenek8309c922008-12-11 23:36:38 +0000472 uint32_t TokenOff;
473 uint32_t PPCondOff;
Ted Kremenekd2c849d2009-01-08 02:47:16 +0000474 uint32_t SpellingOff;
Ted Kremenek325cd302008-12-03 00:38:03 +0000475
476 public:
Ted Kremenek8309c922008-12-11 23:36:38 +0000477 Val() : TokenOff(~0) {}
Ted Kremenekd2c849d2009-01-08 02:47:16 +0000478 Val(uint32_t toff, uint32_t poff, uint32_t soff)
479 : TokenOff(toff), PPCondOff(poff), SpellingOff(soff) {}
Ted Kremenek325cd302008-12-03 00:38:03 +0000480
Ted Kremenek8309c922008-12-11 23:36:38 +0000481 uint32_t getTokenOffset() const {
482 assert(TokenOff != ~((uint32_t)0) && "PTHFileLookup entry initialized.");
483 return TokenOff;
Ted Kremenek325cd302008-12-03 00:38:03 +0000484 }
485
Ted Kremenekd2c849d2009-01-08 02:47:16 +0000486 uint32_t getPPCondOffset() const {
Ted Kremenek8309c922008-12-11 23:36:38 +0000487 assert(TokenOff != ~((uint32_t)0) && "PTHFileLookup entry initialized.");
488 return PPCondOff;
489 }
490
Ted Kremenekd2c849d2009-01-08 02:47:16 +0000491 uint32_t getSpellingOffset() const {
492 assert(TokenOff != ~((uint32_t)0) && "PTHFileLookup entry initialized.");
493 return SpellingOff;
494 }
495
Ted Kremenek8309c922008-12-11 23:36:38 +0000496 bool isValid() const { return TokenOff != ~((uint32_t)0); }
Ted Kremenek325cd302008-12-03 00:38:03 +0000497 };
498
499private:
500 llvm::StringMap<Val> FileMap;
501
502public:
503 PTHFileLookup() {};
504
505 Val Lookup(const FileEntry* FE) {
506 const char* s = FE->getName();
507 unsigned size = strlen(s);
508 return FileMap.GetOrCreateValue(s, s+size).getValue();
509 }
510
Chris Lattnerefb35342009-01-18 01:57:14 +0000511 void ReadTable(const unsigned char* D) {
Ted Kremenek325cd302008-12-03 00:38:03 +0000512 uint32_t N = Read32(D); // Read the length of the table.
513
514 for ( ; N > 0; --N) { // The rest of the data is the table itself.
515 uint32_t len = Read32(D);
Chris Lattnerefb35342009-01-18 01:57:14 +0000516 const char* s = (const char *)D;
Ted Kremenek325cd302008-12-03 00:38:03 +0000517 D += len;
Ted Kremenekd2c849d2009-01-08 02:47:16 +0000518
Ted Kremenek8309c922008-12-11 23:36:38 +0000519 uint32_t TokenOff = Read32(D);
Ted Kremenekd2c849d2009-01-08 02:47:16 +0000520 uint32_t PPCondOff = Read32(D);
521 uint32_t SpellingOff = Read32(D);
522
523 FileMap.GetOrCreateValue(s, s+len).getValue() =
524 Val(TokenOff, PPCondOff, SpellingOff);
Ted Kremenek325cd302008-12-03 00:38:03 +0000525 }
526 }
527};
528} // end anonymous namespace
529
530//===----------------------------------------------------------------------===//
531// PTHManager methods.
532//===----------------------------------------------------------------------===//
533
534PTHManager::PTHManager(const llvm::MemoryBuffer* buf, void* fileLookup,
Chris Lattnerefb35342009-01-18 01:57:14 +0000535 const unsigned char* idDataTable,
536 IdentifierInfo** perIDCache,
537 const unsigned char* sortedIdTable, unsigned numIds)
Ted Kremenekdb4c8e82008-12-03 01:16:39 +0000538: Buf(buf), PerIDCache(perIDCache), FileLookup(fileLookup),
Ted Kremenekd976c3d2009-01-15 18:47:46 +0000539 IdDataTable(idDataTable), SortedIdTable(sortedIdTable),
540 NumIds(numIds), PP(0) {}
Ted Kremenek325cd302008-12-03 00:38:03 +0000541
542PTHManager::~PTHManager() {
543 delete Buf;
544 delete (PTHFileLookup*) FileLookup;
Ted Kremenek93bdc492008-12-04 22:47:11 +0000545 free(PerIDCache);
Ted Kremenek325cd302008-12-03 00:38:03 +0000546}
547
Ted Kremenekd976c3d2009-01-15 18:47:46 +0000548PTHManager* PTHManager::Create(const std::string& file) {
Ted Kremenek325cd302008-12-03 00:38:03 +0000549
550 // Memory map the PTH file.
551 llvm::OwningPtr<llvm::MemoryBuffer>
552 File(llvm::MemoryBuffer::getFile(file.c_str()));
553
554 if (!File)
555 return 0;
556
557 // Get the buffer ranges and check if there are at least three 32-bit
558 // words at the end of the file.
Chris Lattnerefb35342009-01-18 01:57:14 +0000559 const unsigned char* BufBeg = (unsigned char*)File->getBufferStart();
560 const unsigned char* BufEnd = (unsigned char*)File->getBufferEnd();
Ted Kremenek325cd302008-12-03 00:38:03 +0000561
562 if(!(BufEnd > BufBeg + sizeof(uint32_t)*3)) {
563 assert(false && "Invalid PTH file.");
564 return 0; // FIXME: Proper error diagnostic?
565 }
566
567 // Compute the address of the index table at the end of the PTH file.
568 // This table contains the offset of the file lookup table, the
569 // persistent ID -> identifer data table.
Ted Kremenekd4d6d222009-01-15 01:26:25 +0000570 // FIXME: We should just embed this offset in the PTH file.
Chris Lattnerefb35342009-01-18 01:57:14 +0000571 const unsigned char* EndTable = BufEnd - sizeof(uint32_t)*4;
Ted Kremenek325cd302008-12-03 00:38:03 +0000572
573 // Construct the file lookup table. This will be used for mapping from
574 // FileEntry*'s to cached tokens.
Chris Lattnerefb35342009-01-18 01:57:14 +0000575 const unsigned char* FileTableOffset = EndTable + sizeof(uint32_t)*3;
576 const unsigned char* FileTable = BufBeg + Read32(FileTableOffset);
Ted Kremenek325cd302008-12-03 00:38:03 +0000577
578 if (!(FileTable > BufBeg && FileTable < BufEnd)) {
579 assert(false && "Invalid PTH file.");
580 return 0; // FIXME: Proper error diagnostic?
581 }
582
583 llvm::OwningPtr<PTHFileLookup> FL(new PTHFileLookup());
584 FL->ReadTable(FileTable);
585
586 // Get the location of the table mapping from persistent ids to the
587 // data needed to reconstruct identifiers.
Chris Lattnerefb35342009-01-18 01:57:14 +0000588 const unsigned char* IDTableOffset = EndTable + sizeof(uint32_t)*1;
589 const unsigned char* IData = BufBeg + Read32(IDTableOffset);
Ted Kremenek325cd302008-12-03 00:38:03 +0000590 if (!(IData > BufBeg && IData < BufEnd)) {
591 assert(false && "Invalid PTH file.");
592 return 0; // FIXME: Proper error diagnostic?
593 }
594
Ted Kremenekd976c3d2009-01-15 18:47:46 +0000595 // Get the location of the lexigraphically-sorted table of persistent IDs.
Chris Lattnerefb35342009-01-18 01:57:14 +0000596 const unsigned char* SortedIdTableOffset = EndTable + sizeof(uint32_t)*2;
597 const unsigned char* SortedIdTable = BufBeg + Read32(SortedIdTableOffset);
Ted Kremenekd976c3d2009-01-15 18:47:46 +0000598 if (!(SortedIdTable > BufBeg && SortedIdTable < BufEnd)) {
599 assert(false && "Invalid PTH file.");
600 return 0; // FIXME: Proper error diagnostic?
601 }
602
Ted Kremenekdb4c8e82008-12-03 01:16:39 +0000603 // Get the number of IdentifierInfos and pre-allocate the identifier cache.
604 uint32_t NumIds = Read32(IData);
605
606 // Pre-allocate the peristent ID -> IdentifierInfo* cache. We use calloc()
607 // so that we in the best case only zero out memory once when the OS returns
608 // us new pages.
609 IdentifierInfo** PerIDCache =
Chris Lattnerefb35342009-01-18 01:57:14 +0000610 (IdentifierInfo**)calloc(NumIds, sizeof(*PerIDCache));
Ted Kremenekdb4c8e82008-12-03 01:16:39 +0000611
612 if (!PerIDCache) {
613 assert(false && "Could not allocate Persistent ID cache.");
614 return 0;
615 }
616
Ted Kremenekd976c3d2009-01-15 18:47:46 +0000617 // Create the new PTHManager.
618 return new PTHManager(File.take(), FL.take(), IData, PerIDCache,
619 SortedIdTable, NumIds);
Ted Kremenek325cd302008-12-03 00:38:03 +0000620}
621
Ted Kremenek08d3ff32008-12-23 02:30:15 +0000622IdentifierInfo* PTHManager::GetIdentifierInfo(unsigned persistentID) {
623
Ted Kremenek325cd302008-12-03 00:38:03 +0000624 // Check if the IdentifierInfo has already been resolved.
Ted Kremenekd976c3d2009-01-15 18:47:46 +0000625 IdentifierInfo* II = PerIDCache[persistentID];
Ted Kremenek325cd302008-12-03 00:38:03 +0000626 if (II) return II;
627
628 // Look in the PTH file for the string data for the IdentifierInfo object.
Chris Lattnerefb35342009-01-18 01:57:14 +0000629 const unsigned char* TableEntry = IdDataTable + sizeof(uint32_t)*persistentID;
630 const unsigned char* IDData =
631 (const unsigned char*)Buf->getBufferStart() + Read32(TableEntry);
632 assert(IDData < (const unsigned char*)Buf->getBufferEnd());
Ted Kremenek325cd302008-12-03 00:38:03 +0000633
Ted Kremenekd976c3d2009-01-15 18:47:46 +0000634 // Allocate the object.
Chris Lattnerefb35342009-01-18 01:57:14 +0000635 std::pair<IdentifierInfo,const unsigned char*> *Mem =
636 Alloc.Allocate<std::pair<IdentifierInfo,const unsigned char*> >();
Ted Kremenekd976c3d2009-01-15 18:47:46 +0000637
638 Mem->second = IDData;
639 II = new ((void*) Mem) IdentifierInfo(true);
Ted Kremenek325cd302008-12-03 00:38:03 +0000640
Ted Kremenekd976c3d2009-01-15 18:47:46 +0000641 // Store the new IdentifierInfo in the cache.
642 PerIDCache[persistentID] = II;
Ted Kremenek325cd302008-12-03 00:38:03 +0000643 return II;
644}
645
Ted Kremenekd976c3d2009-01-15 18:47:46 +0000646IdentifierInfo* PTHManager::get(const char *NameStart, const char *NameEnd) {
647 unsigned min = 0;
648 unsigned max = NumIds;
649 unsigned len = NameEnd - NameStart;
650
651 do {
652 unsigned i = (max - min) / 2 + min;
Chris Lattnerefb35342009-01-18 01:57:14 +0000653 const unsigned char* p = SortedIdTable + (i * 4);
Ted Kremenekd976c3d2009-01-15 18:47:46 +0000654
655 // Read the persistentID.
656 unsigned perID =
657 ((unsigned) ((uint8_t) p[0]))
658 | (((unsigned) ((uint8_t) p[1])) << 8)
659 | (((unsigned) ((uint8_t) p[2])) << 16)
660 | (((unsigned) ((uint8_t) p[3])) << 24);
661
662 // Get the IdentifierInfo.
663 IdentifierInfo* II = GetIdentifierInfo(perID);
664
665 // First compare the lengths.
666 unsigned IILen = II->getLength();
667 if (len < IILen) goto IsLess;
668 if (len > IILen) goto IsGreater;
669
670 // Now compare the strings!
671 {
672 signed comp = strncmp(NameStart, II->getName(), len);
673 if (comp < 0) goto IsLess;
674 if (comp > 0) goto IsGreater;
675 }
676 // We found a match!
677 return II;
678
679 IsGreater:
680 if (i == min) break;
681 min = i;
682 continue;
683
684 IsLess:
685 max = i;
686 assert(!(max == min) || (min == i));
687 }
Ted Kremenek4795ad02009-01-15 19:28:38 +0000688 while (min != max);
Ted Kremenekd976c3d2009-01-15 18:47:46 +0000689
690 return 0;
691}
692
693
Chris Lattner3c727d62009-01-17 08:06:50 +0000694PTHLexer *PTHManager::CreateLexer(FileID FID) {
695 const FileEntry *FE = PP->getSourceManager().getFileEntryForID(FID);
Ted Kremenek325cd302008-12-03 00:38:03 +0000696 if (!FE)
697 return 0;
698
699 // Lookup the FileEntry object in our file lookup data structure. It will
700 // return a variant that indicates whether or not there is an offset within
701 // the PTH file that contains cached tokens.
Chris Lattnerf4f776a2009-01-17 06:22:33 +0000702 PTHFileLookup::Val FileData = ((PTHFileLookup*)FileLookup)->Lookup(FE);
Ted Kremenek325cd302008-12-03 00:38:03 +0000703
Ted Kremenek8309c922008-12-11 23:36:38 +0000704 if (!FileData.isValid()) // No tokens available.
Ted Kremenek325cd302008-12-03 00:38:03 +0000705 return 0;
706
Chris Lattnerefb35342009-01-18 01:57:14 +0000707 const unsigned char *BufStart = (const unsigned char *)Buf->getBufferStart();
Ted Kremenek325cd302008-12-03 00:38:03 +0000708 // Compute the offset of the token data within the buffer.
Chris Lattnerefb35342009-01-18 01:57:14 +0000709 const unsigned char* data = BufStart + FileData.getTokenOffset();
Ted Kremenekc07091c2008-12-12 18:34:08 +0000710
711 // Get the location of pp-conditional table.
Chris Lattnerefb35342009-01-18 01:57:14 +0000712 const unsigned char* ppcond = BufStart + FileData.getPPCondOffset();
Ted Kremenekd2c849d2009-01-08 02:47:16 +0000713 uint32_t len = Read32(ppcond);
Ted Kremenekc07091c2008-12-12 18:34:08 +0000714 if (len == 0) ppcond = 0;
Ted Kremeneka4d5bf52009-01-08 04:30:32 +0000715
716 // Get the location of the spelling table.
Chris Lattnerefb35342009-01-18 01:57:14 +0000717 const unsigned char* spellingTable = BufStart + FileData.getSpellingOffset();
Ted Kremeneka4d5bf52009-01-08 04:30:32 +0000718
719 len = Read32(spellingTable);
720 if (len == 0) spellingTable = 0;
Ted Kremenekd2c849d2009-01-08 02:47:16 +0000721
Chris Lattnerefb35342009-01-18 01:57:14 +0000722 assert(data < (const unsigned char*)Buf->getBufferEnd());
Ted Kremenekd310fde2009-01-09 22:05:30 +0000723
724 // Create the SpellingSearch object for this FileID.
725 PTHSpellingSearch* ss = new PTHSpellingSearch(*this, len, spellingTable);
Chris Lattnerf4f776a2009-01-17 06:22:33 +0000726 SpellingMap[FID] = ss;
Ted Kremenekd310fde2009-01-09 22:05:30 +0000727
Ted Kremenekd976c3d2009-01-15 18:47:46 +0000728 assert(PP && "No preprocessor set yet!");
Chris Lattnerf4f776a2009-01-17 06:22:33 +0000729 return new PTHLexer(*PP, FID, data, ppcond, *ss, *this);
Ted Kremenek325cd302008-12-03 00:38:03 +0000730}