blob: b0f06271c4fbd3c4cc2f7a73408bb384c02e051c [file] [log] [blame]
Ted Kremenek274b2082008-11-12 21:37:15 +00001//===--- PTHLexer.cpp - Lex from a token stream ---------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the PTHLexer interface.
11//
12//===----------------------------------------------------------------------===//
13
Ted Kremenek0c6a77b2008-12-03 00:38:03 +000014#include "clang/Basic/TokenKinds.h"
15#include "clang/Basic/FileManager.h"
16#include "clang/Basic/IdentifierTable.h"
Ted Kremenek274b2082008-11-12 21:37:15 +000017#include "clang/Lex/PTHLexer.h"
18#include "clang/Lex/Preprocessor.h"
Ted Kremenek0c6a77b2008-12-03 00:38:03 +000019#include "clang/Lex/PTHManager.h"
20#include "clang/Lex/Token.h"
21#include "clang/Lex/Preprocessor.h"
Ted Kremenek0c6a77b2008-12-03 00:38:03 +000022#include "llvm/ADT/StringMap.h"
23#include "llvm/ADT/OwningPtr.h"
Chris Lattner6f78c3b2009-01-22 23:50:07 +000024#include "llvm/Support/Compiler.h"
25#include "llvm/Support/MathExtras.h"
26#include "llvm/Support/MemoryBuffer.h"
27#include "llvm/System/Host.h"
Ted Kremenek274b2082008-11-12 21:37:15 +000028using namespace clang;
29
Ted Kremenek7b78b7c2009-01-19 23:13:15 +000030#define DISK_TOKEN_SIZE (1+1+2+4+4)
Ted Kremenek268ee702008-12-12 18:34:08 +000031
Ted Kremenek0c6a77b2008-12-03 00:38:03 +000032//===----------------------------------------------------------------------===//
33// Utility methods for reading from the mmap'ed PTH file.
34//===----------------------------------------------------------------------===//
35
Chris Lattner5ff43172009-01-22 19:48:26 +000036static inline uint16_t ReadUnalignedLE16(const unsigned char *&Data) {
Chris Lattnerda9d61c2009-01-18 01:57:14 +000037 uint16_t V = ((uint16_t)Data[0] << 0) |
38 ((uint16_t)Data[1] << 8);
39 Data += 2;
40 return V;
41}
42
Chris Lattner5ff43172009-01-22 19:48:26 +000043static inline uint32_t ReadLE32(const unsigned char *&Data) {
Chris Lattnerfbc33382009-01-23 00:13:28 +000044 // Hosts that directly support little-endian 32-bit loads can just
45 // use them. Big-endian hosts need a bswap.
Chris Lattnerf15674c2009-01-18 02:19:16 +000046 uint32_t V = *((uint32_t*)Data);
Chris Lattner6f78c3b2009-01-22 23:50:07 +000047 if (llvm::sys::isBigEndianHost())
48 V = llvm::ByteSwap_32(V);
Chris Lattnerda9d61c2009-01-18 01:57:14 +000049 Data += 4;
50 return V;
51}
52
53
Ted Kremeneke5680f32008-12-23 01:30:52 +000054//===----------------------------------------------------------------------===//
55// PTHLexer methods.
56//===----------------------------------------------------------------------===//
57
Chris Lattnerda9d61c2009-01-18 01:57:14 +000058PTHLexer::PTHLexer(Preprocessor &PP, FileID FID, const unsigned char *D,
59 const unsigned char *ppcond,
Chris Lattner2b2453a2009-01-17 06:22:33 +000060 PTHSpellingSearch &mySpellingSrch, PTHManager &PM)
61 : PreprocessorLexer(&PP, FID), TokBuf(D), CurPtr(D), LastHashTokPtr(0),
Ted Kremenek5f074262009-01-09 22:05:30 +000062 PPCond(ppcond), CurPPCondPtr(ppcond), MySpellingSrch(mySpellingSrch),
Chris Lattner2b2453a2009-01-17 06:22:33 +000063 PTHMgr(PM) {
64
65 FileStartLoc = PP.getSourceManager().getLocForStartOfFile(FID);
Ted Kremenek5f074262009-01-09 22:05:30 +000066}
Ted Kremeneke5680f32008-12-23 01:30:52 +000067
68void PTHLexer::Lex(Token& Tok) {
69LexNextToken:
Ted Kremeneke5680f32008-12-23 01:30:52 +000070
Ted Kremenek866bdf72008-12-23 02:30:15 +000071 //===--------------------------------------==//
72 // Read the raw token data.
73 //===--------------------------------------==//
74
75 // Shadow CurPtr into an automatic variable.
Chris Lattneraff6ef82009-01-21 07:21:56 +000076 const unsigned char *CurPtrShadow = CurPtr;
Ted Kremenek866bdf72008-12-23 02:30:15 +000077
Chris Lattner1b5285e2009-01-18 02:10:31 +000078 // Read in the data for the token.
Chris Lattner5ff43172009-01-22 19:48:26 +000079 unsigned Word0 = ReadLE32(CurPtrShadow);
80 uint32_t IdentifierID = ReadLE32(CurPtrShadow);
81 uint32_t FileOffset = ReadLE32(CurPtrShadow);
Ted Kremenek7b78b7c2009-01-19 23:13:15 +000082
83 tok::TokenKind TKind = (tok::TokenKind) (Word0 & 0xFF);
84 Token::TokenFlags TFlags = (Token::TokenFlags) ((Word0 >> 8) & 0xFF);
Chris Lattneraff6ef82009-01-21 07:21:56 +000085 uint32_t Len = Word0 >> 16;
Ted Kremenek7b78b7c2009-01-19 23:13:15 +000086
Chris Lattneraff6ef82009-01-21 07:21:56 +000087 CurPtr = CurPtrShadow;
Ted Kremenek866bdf72008-12-23 02:30:15 +000088
89 //===--------------------------------------==//
90 // Construct the token itself.
91 //===--------------------------------------==//
92
93 Tok.startToken();
Chris Lattner898a0bb2009-01-18 02:34:01 +000094 Tok.setKind(TKind);
95 Tok.setFlag(TFlags);
Ted Kremenek59d08cb2008-12-23 19:24:24 +000096 assert(!LexingRawMode);
Chris Lattner2b2453a2009-01-17 06:22:33 +000097 Tok.setLocation(FileStartLoc.getFileLocWithOffset(FileOffset));
Ted Kremenek866bdf72008-12-23 02:30:15 +000098 Tok.setLength(Len);
99
Chris Lattnerd0a69692009-01-21 07:50:06 +0000100 // Handle identifiers.
101 if (IdentifierID) {
102 MIOpt.ReadToken();
103 IdentifierInfo *II = PTHMgr.GetIdentifierInfo(IdentifierID-1);
104 Tok.setIdentifierInfo(II);
105 if (II->isHandleIdentifierCase())
106 PP->HandleIdentifier(Tok);
107 return;
108 }
109
Ted Kremenek866bdf72008-12-23 02:30:15 +0000110 //===--------------------------------------==//
111 // Process the token.
112 //===--------------------------------------==//
Ted Kremenek5f074262009-01-09 22:05:30 +0000113#if 0
114 SourceManager& SM = PP->getSourceManager();
115 llvm::cerr << SM.getFileEntryForID(FileID)->getName()
116 << ':' << SM.getLogicalLineNumber(Tok.getLocation())
117 << ':' << SM.getLogicalColumnNumber(Tok.getLocation())
118 << '\n';
119#endif
Ted Kremeneke5680f32008-12-23 01:30:52 +0000120
Chris Lattner898a0bb2009-01-18 02:34:01 +0000121 if (TKind == tok::eof) {
Ted Kremeneke5680f32008-12-23 01:30:52 +0000122 // Save the end-of-file token.
123 EofToken = Tok;
124
125 Preprocessor *PPCache = PP;
Ted Kremenek59d08cb2008-12-23 19:24:24 +0000126
127 assert(!ParsingPreprocessorDirective);
128 assert(!LexingRawMode);
129
130 // FIXME: Issue diagnostics similar to Lexer.
131 if (PP->HandleEndOfFile(Tok, false))
Ted Kremeneke5680f32008-12-23 01:30:52 +0000132 return;
Ted Kremenek59d08cb2008-12-23 19:24:24 +0000133
Ted Kremeneke5680f32008-12-23 01:30:52 +0000134 assert(PPCache && "Raw buffer::LexEndOfFile should return a token");
135 return PPCache->Lex(Tok);
136 }
Ted Kremeneke5680f32008-12-23 01:30:52 +0000137
Chris Lattner898a0bb2009-01-18 02:34:01 +0000138 if (TKind == tok::hash && Tok.isAtStartOfLine()) {
Ted Kremenek59d08cb2008-12-23 19:24:24 +0000139 LastHashTokPtr = CurPtr - DISK_TOKEN_SIZE;
140 assert(!LexingRawMode);
141 PP->HandleDirective(Tok);
142
143 if (PP->isCurrentLexer(this))
144 goto LexNextToken;
145
146 return PP->Lex(Tok);
147 }
148
Chris Lattner898a0bb2009-01-18 02:34:01 +0000149 if (TKind == tok::eom) {
Ted Kremenek59d08cb2008-12-23 19:24:24 +0000150 assert(ParsingPreprocessorDirective);
Ted Kremeneke5680f32008-12-23 01:30:52 +0000151 ParsingPreprocessorDirective = false;
152 return;
153 }
Ted Kremeneke5680f32008-12-23 01:30:52 +0000154
Ted Kremenek59d08cb2008-12-23 19:24:24 +0000155 MIOpt.ReadToken();
Ted Kremeneke5680f32008-12-23 01:30:52 +0000156}
157
158// FIXME: We can just grab the last token instead of storing a copy
159// into EofToken.
Ted Kremenek59d08cb2008-12-23 19:24:24 +0000160void PTHLexer::getEOF(Token& Tok) {
Ted Kremenekdefb7092009-01-09 00:36:11 +0000161 assert(EofToken.is(tok::eof));
Ted Kremeneke5680f32008-12-23 01:30:52 +0000162 Tok = EofToken;
163}
164
165void PTHLexer::DiscardToEndOfLine() {
166 assert(ParsingPreprocessorDirective && ParsingFilename == false &&
167 "Must be in a preprocessing directive!");
168
169 // We assume that if the preprocessor wishes to discard to the end of
170 // the line that it also means to end the current preprocessor directive.
171 ParsingPreprocessorDirective = false;
172
173 // Skip tokens by only peeking at their token kind and the flags.
174 // We don't need to actually reconstruct full tokens from the token buffer.
175 // This saves some copies and it also reduces IdentifierInfo* lookup.
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000176 const unsigned char* p = CurPtr;
Ted Kremeneke5680f32008-12-23 01:30:52 +0000177 while (1) {
178 // Read the token kind. Are we at the end of the file?
179 tok::TokenKind x = (tok::TokenKind) (uint8_t) *p;
180 if (x == tok::eof) break;
181
182 // Read the token flags. Are we at the start of the next line?
183 Token::TokenFlags y = (Token::TokenFlags) (uint8_t) p[1];
184 if (y & Token::StartOfLine) break;
185
186 // Skip to the next token.
187 p += DISK_TOKEN_SIZE;
188 }
189
190 CurPtr = p;
191}
192
Ted Kremenek268ee702008-12-12 18:34:08 +0000193/// SkipBlock - Used by Preprocessor to skip the current conditional block.
194bool PTHLexer::SkipBlock() {
195 assert(CurPPCondPtr && "No cached PP conditional information.");
196 assert(LastHashTokPtr && "No known '#' token.");
197
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000198 const unsigned char* HashEntryI = 0;
Ted Kremenek268ee702008-12-12 18:34:08 +0000199 uint32_t Offset;
200 uint32_t TableIdx;
201
202 do {
Ted Kremenek41a26602008-12-12 22:05:38 +0000203 // Read the token offset from the side-table.
Chris Lattner5ff43172009-01-22 19:48:26 +0000204 Offset = ReadLE32(CurPPCondPtr);
Ted Kremenek41a26602008-12-12 22:05:38 +0000205
206 // Read the target table index from the side-table.
Chris Lattner5ff43172009-01-22 19:48:26 +0000207 TableIdx = ReadLE32(CurPPCondPtr);
Ted Kremenek41a26602008-12-12 22:05:38 +0000208
209 // Compute the actual memory address of the '#' token data for this entry.
210 HashEntryI = TokBuf + Offset;
211
212 // Optmization: "Sibling jumping". #if...#else...#endif blocks can
213 // contain nested blocks. In the side-table we can jump over these
214 // nested blocks instead of doing a linear search if the next "sibling"
215 // entry is not at a location greater than LastHashTokPtr.
216 if (HashEntryI < LastHashTokPtr && TableIdx) {
217 // In the side-table we are still at an entry for a '#' token that
218 // is earlier than the last one we saw. Check if the location we would
219 // stride gets us closer.
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000220 const unsigned char* NextPPCondPtr =
221 PPCond + TableIdx*(sizeof(uint32_t)*2);
Ted Kremenek41a26602008-12-12 22:05:38 +0000222 assert(NextPPCondPtr >= CurPPCondPtr);
223 // Read where we should jump to.
Chris Lattner5ff43172009-01-22 19:48:26 +0000224 uint32_t TmpOffset = ReadLE32(NextPPCondPtr);
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000225 const unsigned char* HashEntryJ = TokBuf + TmpOffset;
Ted Kremenek41a26602008-12-12 22:05:38 +0000226
227 if (HashEntryJ <= LastHashTokPtr) {
228 // Jump directly to the next entry in the side table.
229 HashEntryI = HashEntryJ;
230 Offset = TmpOffset;
Chris Lattner5ff43172009-01-22 19:48:26 +0000231 TableIdx = ReadLE32(NextPPCondPtr);
Ted Kremenek41a26602008-12-12 22:05:38 +0000232 CurPPCondPtr = NextPPCondPtr;
233 }
234 }
Ted Kremenek268ee702008-12-12 18:34:08 +0000235 }
Ted Kremenek41a26602008-12-12 22:05:38 +0000236 while (HashEntryI < LastHashTokPtr);
237 assert(HashEntryI == LastHashTokPtr && "No PP-cond entry found for '#'");
Ted Kremenek268ee702008-12-12 18:34:08 +0000238 assert(TableIdx && "No jumping from #endifs.");
239
240 // Update our side-table iterator.
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000241 const unsigned char* NextPPCondPtr = PPCond + TableIdx*(sizeof(uint32_t)*2);
Ted Kremenek268ee702008-12-12 18:34:08 +0000242 assert(NextPPCondPtr >= CurPPCondPtr);
243 CurPPCondPtr = NextPPCondPtr;
244
245 // Read where we should jump to.
Chris Lattner5ff43172009-01-22 19:48:26 +0000246 HashEntryI = TokBuf + ReadLE32(NextPPCondPtr);
247 uint32_t NextIdx = ReadLE32(NextPPCondPtr);
Ted Kremenek268ee702008-12-12 18:34:08 +0000248
249 // By construction NextIdx will be zero if this is a #endif. This is useful
250 // to know to obviate lexing another token.
251 bool isEndif = NextIdx == 0;
Ted Kremenek268ee702008-12-12 18:34:08 +0000252
253 // This case can occur when we see something like this:
254 //
255 // #if ...
256 // /* a comment or nothing */
257 // #elif
258 //
259 // If we are skipping the first #if block it will be the case that CurPtr
260 // already points 'elif'. Just return.
261
Ted Kremenek41a26602008-12-12 22:05:38 +0000262 if (CurPtr > HashEntryI) {
263 assert(CurPtr == HashEntryI + DISK_TOKEN_SIZE);
Ted Kremenek268ee702008-12-12 18:34:08 +0000264 // Did we reach a #endif? If so, go ahead and consume that token as well.
265 if (isEndif)
Ted Kremeneke5680f32008-12-23 01:30:52 +0000266 CurPtr += DISK_TOKEN_SIZE*2;
Ted Kremenek268ee702008-12-12 18:34:08 +0000267 else
Ted Kremenek41a26602008-12-12 22:05:38 +0000268 LastHashTokPtr = HashEntryI;
Ted Kremenek268ee702008-12-12 18:34:08 +0000269
270 return isEndif;
271 }
272
273 // Otherwise, we need to advance. Update CurPtr to point to the '#' token.
Ted Kremenek41a26602008-12-12 22:05:38 +0000274 CurPtr = HashEntryI;
Ted Kremenek268ee702008-12-12 18:34:08 +0000275
276 // Update the location of the last observed '#'. This is useful if we
277 // are skipping multiple blocks.
278 LastHashTokPtr = CurPtr;
Ted Kremenek268ee702008-12-12 18:34:08 +0000279
Ted Kremeneke5680f32008-12-23 01:30:52 +0000280 // Skip the '#' token.
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000281 assert(((tok::TokenKind)*CurPtr) == tok::hash);
Ted Kremeneke5680f32008-12-23 01:30:52 +0000282 CurPtr += DISK_TOKEN_SIZE;
283
Ted Kremenek268ee702008-12-12 18:34:08 +0000284 // Did we reach a #endif? If so, go ahead and consume that token as well.
Ted Kremeneke5680f32008-12-23 01:30:52 +0000285 if (isEndif) { CurPtr += DISK_TOKEN_SIZE*2; }
Ted Kremenek268ee702008-12-12 18:34:08 +0000286
287 return isEndif;
288}
289
Ted Kremenek30a12ec2008-12-17 23:36:32 +0000290SourceLocation PTHLexer::getSourceLocation() {
Chris Lattner1b5285e2009-01-18 02:10:31 +0000291 // getSourceLocation is not on the hot path. It is used to get the location
292 // of the next token when transitioning back to this lexer when done
Ted Kremenek30a12ec2008-12-17 23:36:32 +0000293 // handling a #included file. Just read the necessary data from the token
294 // data buffer to construct the SourceLocation object.
295 // NOTE: This is a virtual function; hence it is defined out-of-line.
Ted Kremenekb248d532009-01-21 22:41:38 +0000296 const unsigned char *OffsetPtr = CurPtr + (DISK_TOKEN_SIZE - 4);
Chris Lattner5ff43172009-01-22 19:48:26 +0000297 uint32_t Offset = ReadLE32(OffsetPtr);
Chris Lattner1b5285e2009-01-18 02:10:31 +0000298 return FileStartLoc.getFileLocWithOffset(Offset);
Ted Kremenek30a12ec2008-12-17 23:36:32 +0000299}
300
Ted Kremenek5f074262009-01-09 22:05:30 +0000301//===----------------------------------------------------------------------===//
302// getSpelling() - Use cached data in PTH files for getSpelling().
303//===----------------------------------------------------------------------===//
304
Chris Lattner2b2453a2009-01-17 06:22:33 +0000305unsigned PTHManager::getSpelling(FileID FID, unsigned FPos,
306 const char *&Buffer) {
307 llvm::DenseMap<FileID, PTHSpellingSearch*>::iterator I =SpellingMap.find(FID);
Ted Kremenek5f074262009-01-09 22:05:30 +0000308
309 if (I == SpellingMap.end())
Chris Lattner1b5285e2009-01-18 02:10:31 +0000310 return 0;
Ted Kremenekf02f6f02009-01-13 22:05:50 +0000311
Chris Lattner2b2453a2009-01-17 06:22:33 +0000312 return I->second->getSpellingBinarySearch(FPos, Buffer);
313}
314
315unsigned PTHManager::getSpelling(SourceLocation Loc, const char *&Buffer) {
Chris Lattner6b7b8402009-01-17 06:29:33 +0000316 SourceManager &SM = PP->getSourceManager();
317 Loc = SM.getSpellingLoc(Loc);
318 std::pair<FileID, unsigned> LocInfo = SM.getDecomposedFileLoc(Loc);
Chris Lattner2b2453a2009-01-17 06:22:33 +0000319 return getSpelling(LocInfo.first, LocInfo.second, Buffer);
Ted Kremenek5f074262009-01-09 22:05:30 +0000320}
321
322unsigned PTHManager::getSpellingAtPTHOffset(unsigned PTHOffset,
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000323 const char *&Buffer) {
Chris Lattner1b5285e2009-01-18 02:10:31 +0000324 assert(PTHOffset < Buf->getBufferSize());
325 const unsigned char* Ptr =
326 (const unsigned char*)Buf->getBufferStart() + PTHOffset;
Ted Kremenek32a8ad52009-01-08 04:30:32 +0000327
328 // The string is prefixed by 16 bits for its length, followed by the string
329 // itself.
Chris Lattner5ff43172009-01-22 19:48:26 +0000330 unsigned Len = ReadUnalignedLE16(Ptr);
Chris Lattner1b5285e2009-01-18 02:10:31 +0000331 Buffer = (const char *)Ptr;
332 return Len;
Ted Kremenek32a8ad52009-01-08 04:30:32 +0000333}
334
Chris Lattner1b5285e2009-01-18 02:10:31 +0000335unsigned PTHSpellingSearch::getSpellingLinearSearch(unsigned FPos,
Ted Kremenek5f074262009-01-09 22:05:30 +0000336 const char *&Buffer) {
Chris Lattner1b5285e2009-01-18 02:10:31 +0000337 const unsigned char *Ptr = LinearItr;
338 unsigned Len = 0;
Ted Kremenek5f074262009-01-09 22:05:30 +0000339
Chris Lattner1b5285e2009-01-18 02:10:31 +0000340 if (Ptr == TableEnd)
341 return getSpellingBinarySearch(FPos, Buffer);
Ted Kremenek5f074262009-01-09 22:05:30 +0000342
343 do {
Chris Lattner5ff43172009-01-22 19:48:26 +0000344 uint32_t TokOffset = ReadLE32(Ptr);
Ted Kremenek32a8ad52009-01-08 04:30:32 +0000345
Chris Lattner1b5285e2009-01-18 02:10:31 +0000346 if (TokOffset > FPos)
347 return getSpellingBinarySearch(FPos, Buffer);
Ted Kremenek32a8ad52009-01-08 04:30:32 +0000348
Ted Kremenek32a8ad52009-01-08 04:30:32 +0000349 // Did we find a matching token offset for this spelling?
Chris Lattner1b5285e2009-01-18 02:10:31 +0000350 if (TokOffset == FPos) {
Chris Lattner5ff43172009-01-22 19:48:26 +0000351 uint32_t SpellingPTHOffset = ReadLE32(Ptr);
Chris Lattner1b5285e2009-01-18 02:10:31 +0000352 Len = PTHMgr.getSpellingAtPTHOffset(SpellingPTHOffset, Buffer);
Ted Kremenek32a8ad52009-01-08 04:30:32 +0000353 break;
354 }
Chris Lattner1b5285e2009-01-18 02:10:31 +0000355 } while (Ptr != TableEnd);
Ted Kremenek32a8ad52009-01-08 04:30:32 +0000356
Chris Lattner1b5285e2009-01-18 02:10:31 +0000357 LinearItr = Ptr;
358 return Len;
Ted Kremenekb70e3da2009-01-08 02:47:16 +0000359}
360
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000361
Chris Lattner1b5285e2009-01-18 02:10:31 +0000362unsigned PTHSpellingSearch::getSpellingBinarySearch(unsigned FPos,
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000363 const char *&Buffer) {
Ted Kremenek5f074262009-01-09 22:05:30 +0000364
Ted Kremenekf02f6f02009-01-13 22:05:50 +0000365 assert((TableEnd - TableBeg) % SpellingEntrySize == 0);
Chris Lattner1b5285e2009-01-18 02:10:31 +0000366 assert(TableEnd >= TableBeg);
Ted Kremenekf02f6f02009-01-13 22:05:50 +0000367
368 if (TableEnd == TableBeg)
369 return 0;
370
Ted Kremenek5f074262009-01-09 22:05:30 +0000371 unsigned min = 0;
Chris Lattner1b5285e2009-01-18 02:10:31 +0000372 const unsigned char *tb = TableBeg;
Ted Kremenekf02f6f02009-01-13 22:05:50 +0000373 unsigned max = NumSpellings;
Ted Kremenek5f074262009-01-09 22:05:30 +0000374
Ted Kremenekf02f6f02009-01-13 22:05:50 +0000375 do {
Ted Kremenek5f074262009-01-09 22:05:30 +0000376 unsigned i = (max - min) / 2 + min;
Chris Lattner1b5285e2009-01-18 02:10:31 +0000377 const unsigned char *Ptr = tb + (i * SpellingEntrySize);
Ted Kremenek5f074262009-01-09 22:05:30 +0000378
Chris Lattner5ff43172009-01-22 19:48:26 +0000379 uint32_t TokOffset = ReadLE32(Ptr);
Chris Lattner1b5285e2009-01-18 02:10:31 +0000380 if (TokOffset > FPos) {
Ted Kremenek5f074262009-01-09 22:05:30 +0000381 max = i;
Ted Kremenekf02f6f02009-01-13 22:05:50 +0000382 assert(!(max == min) || (min == i));
Ted Kremenek5f074262009-01-09 22:05:30 +0000383 continue;
384 }
385
Chris Lattner1b5285e2009-01-18 02:10:31 +0000386 if (TokOffset < FPos) {
Ted Kremenek7bd88aa2009-01-13 22:16:45 +0000387 if (i == min)
388 break;
389
Ted Kremenek5f074262009-01-09 22:05:30 +0000390 min = i;
391 continue;
392 }
393
Chris Lattner5ff43172009-01-22 19:48:26 +0000394 uint32_t SpellingPTHOffset = ReadLE32(Ptr);
Ted Kremenek5f074262009-01-09 22:05:30 +0000395 return PTHMgr.getSpellingAtPTHOffset(SpellingPTHOffset, Buffer);
396 }
Ted Kremenekf02f6f02009-01-13 22:05:50 +0000397 while (min != max);
Ted Kremenek5f074262009-01-09 22:05:30 +0000398
399 return 0;
400}
401
Chris Lattner2b2453a2009-01-17 06:22:33 +0000402unsigned PTHLexer::getSpelling(SourceLocation Loc, const char *&Buffer) {
403 SourceManager &SM = PP->getSourceManager();
404 Loc = SM.getSpellingLoc(Loc);
405 std::pair<FileID, unsigned> LocInfo = SM.getDecomposedFileLoc(Loc);
406
407 FileID FID = LocInfo.first;
408 unsigned FPos = LocInfo.second;
Ted Kremenek5f074262009-01-09 22:05:30 +0000409
Chris Lattner2b2453a2009-01-17 06:22:33 +0000410 if (FID == getFileID())
411 return MySpellingSrch.getSpellingLinearSearch(FPos, Buffer);
412 return PTHMgr.getSpelling(FID, FPos, Buffer);
Ted Kremenek5f074262009-01-09 22:05:30 +0000413}
414
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000415//===----------------------------------------------------------------------===//
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000416// Internal Data Structures for PTH file lookup and resolving identifiers.
417//===----------------------------------------------------------------------===//
418
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000419
420/// PTHFileLookup - This internal data structure is used by the PTHManager
421/// to map from FileEntry objects managed by FileManager to offsets within
422/// the PTH file.
423namespace {
424class VISIBILITY_HIDDEN PTHFileLookup {
425public:
426 class Val {
Ted Kremenekfb645b62008-12-11 23:36:38 +0000427 uint32_t TokenOff;
428 uint32_t PPCondOff;
Ted Kremenekb70e3da2009-01-08 02:47:16 +0000429 uint32_t SpellingOff;
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000430 public:
Ted Kremenekfb645b62008-12-11 23:36:38 +0000431 Val() : TokenOff(~0) {}
Ted Kremenekb70e3da2009-01-08 02:47:16 +0000432 Val(uint32_t toff, uint32_t poff, uint32_t soff)
433 : TokenOff(toff), PPCondOff(poff), SpellingOff(soff) {}
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000434
Chris Lattner1b5285e2009-01-18 02:10:31 +0000435 bool isValid() const { return TokenOff != ~((uint32_t)0); }
436
Ted Kremenekfb645b62008-12-11 23:36:38 +0000437 uint32_t getTokenOffset() const {
Chris Lattner1b5285e2009-01-18 02:10:31 +0000438 assert(isValid() && "PTHFileLookup entry initialized.");
Ted Kremenekfb645b62008-12-11 23:36:38 +0000439 return TokenOff;
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000440 }
441
Ted Kremenekb70e3da2009-01-08 02:47:16 +0000442 uint32_t getPPCondOffset() const {
Chris Lattner1b5285e2009-01-18 02:10:31 +0000443 assert(isValid() && "PTHFileLookup entry initialized.");
Ted Kremenekfb645b62008-12-11 23:36:38 +0000444 return PPCondOff;
445 }
446
Ted Kremenekb70e3da2009-01-08 02:47:16 +0000447 uint32_t getSpellingOffset() const {
Chris Lattner1b5285e2009-01-18 02:10:31 +0000448 assert(isValid() && "PTHFileLookup entry initialized.");
Ted Kremenekb70e3da2009-01-08 02:47:16 +0000449 return SpellingOff;
450 }
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000451 };
452
453private:
454 llvm::StringMap<Val> FileMap;
455
456public:
457 PTHFileLookup() {};
458
Ted Kremenekcdd8f212009-01-21 07:34:28 +0000459 bool isEmpty() const {
460 return FileMap.empty();
461 }
462
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000463 Val Lookup(const FileEntry* FE) {
464 const char* s = FE->getName();
465 unsigned size = strlen(s);
466 return FileMap.GetOrCreateValue(s, s+size).getValue();
467 }
468
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000469 void ReadTable(const unsigned char* D) {
Chris Lattner5ff43172009-01-22 19:48:26 +0000470 uint32_t N = ReadLE32(D); // Read the length of the table.
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000471
472 for ( ; N > 0; --N) { // The rest of the data is the table itself.
Chris Lattner5ff43172009-01-22 19:48:26 +0000473 uint32_t Len = ReadLE32(D);
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000474 const char* s = (const char *)D;
Chris Lattner1b5285e2009-01-18 02:10:31 +0000475 D += Len;
Ted Kremenekb70e3da2009-01-08 02:47:16 +0000476
Chris Lattner5ff43172009-01-22 19:48:26 +0000477 uint32_t TokenOff = ReadLE32(D);
478 uint32_t PPCondOff = ReadLE32(D);
479 uint32_t SpellingOff = ReadLE32(D);
Ted Kremenekb70e3da2009-01-08 02:47:16 +0000480
Chris Lattner1b5285e2009-01-18 02:10:31 +0000481 FileMap.GetOrCreateValue(s, s+Len).getValue() =
Ted Kremenekb70e3da2009-01-08 02:47:16 +0000482 Val(TokenOff, PPCondOff, SpellingOff);
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000483 }
484 }
485};
486} // end anonymous namespace
487
488//===----------------------------------------------------------------------===//
489// PTHManager methods.
490//===----------------------------------------------------------------------===//
491
492PTHManager::PTHManager(const llvm::MemoryBuffer* buf, void* fileLookup,
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000493 const unsigned char* idDataTable,
494 IdentifierInfo** perIDCache,
495 const unsigned char* sortedIdTable, unsigned numIds)
Ted Kremenek6183e482008-12-03 01:16:39 +0000496: Buf(buf), PerIDCache(perIDCache), FileLookup(fileLookup),
Ted Kremenek72b1b152009-01-15 18:47:46 +0000497 IdDataTable(idDataTable), SortedIdTable(sortedIdTable),
498 NumIds(numIds), PP(0) {}
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000499
500PTHManager::~PTHManager() {
501 delete Buf;
502 delete (PTHFileLookup*) FileLookup;
Ted Kremenek0e50b6e2008-12-04 22:47:11 +0000503 free(PerIDCache);
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000504}
505
Ted Kremenek72b1b152009-01-15 18:47:46 +0000506PTHManager* PTHManager::Create(const std::string& file) {
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000507 // Memory map the PTH file.
508 llvm::OwningPtr<llvm::MemoryBuffer>
509 File(llvm::MemoryBuffer::getFile(file.c_str()));
510
511 if (!File)
512 return 0;
513
514 // Get the buffer ranges and check if there are at least three 32-bit
515 // words at the end of the file.
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000516 const unsigned char* BufBeg = (unsigned char*)File->getBufferStart();
517 const unsigned char* BufEnd = (unsigned char*)File->getBufferEnd();
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000518
519 if(!(BufEnd > BufBeg + sizeof(uint32_t)*3)) {
520 assert(false && "Invalid PTH file.");
521 return 0; // FIXME: Proper error diagnostic?
522 }
523
524 // Compute the address of the index table at the end of the PTH file.
525 // This table contains the offset of the file lookup table, the
526 // persistent ID -> identifer data table.
Ted Kremenek293b4af2009-01-15 01:26:25 +0000527 // FIXME: We should just embed this offset in the PTH file.
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000528 const unsigned char* EndTable = BufEnd - sizeof(uint32_t)*4;
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000529
530 // Construct the file lookup table. This will be used for mapping from
531 // FileEntry*'s to cached tokens.
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000532 const unsigned char* FileTableOffset = EndTable + sizeof(uint32_t)*3;
Chris Lattner5ff43172009-01-22 19:48:26 +0000533 const unsigned char* FileTable = BufBeg + ReadLE32(FileTableOffset);
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000534
535 if (!(FileTable > BufBeg && FileTable < BufEnd)) {
536 assert(false && "Invalid PTH file.");
537 return 0; // FIXME: Proper error diagnostic?
538 }
539
540 llvm::OwningPtr<PTHFileLookup> FL(new PTHFileLookup());
541 FL->ReadTable(FileTable);
Ted Kremenekcdd8f212009-01-21 07:34:28 +0000542
543 if (FL->isEmpty())
544 return 0;
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000545
546 // Get the location of the table mapping from persistent ids to the
547 // data needed to reconstruct identifiers.
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000548 const unsigned char* IDTableOffset = EndTable + sizeof(uint32_t)*1;
Chris Lattner5ff43172009-01-22 19:48:26 +0000549 const unsigned char* IData = BufBeg + ReadLE32(IDTableOffset);
Ted Kremenekcdd8f212009-01-21 07:34:28 +0000550
551 if (!(IData >= BufBeg && IData < BufEnd)) {
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000552 assert(false && "Invalid PTH file.");
553 return 0; // FIXME: Proper error diagnostic?
554 }
555
Ted Kremenek72b1b152009-01-15 18:47:46 +0000556 // Get the location of the lexigraphically-sorted table of persistent IDs.
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000557 const unsigned char* SortedIdTableOffset = EndTable + sizeof(uint32_t)*2;
Chris Lattner5ff43172009-01-22 19:48:26 +0000558 const unsigned char* SortedIdTable = BufBeg + ReadLE32(SortedIdTableOffset);
Ted Kremenekcdd8f212009-01-21 07:34:28 +0000559 if (!(SortedIdTable >= BufBeg && SortedIdTable < BufEnd)) {
Ted Kremenek72b1b152009-01-15 18:47:46 +0000560 assert(false && "Invalid PTH file.");
561 return 0; // FIXME: Proper error diagnostic?
562 }
563
Ted Kremenek6183e482008-12-03 01:16:39 +0000564 // Get the number of IdentifierInfos and pre-allocate the identifier cache.
Chris Lattner5ff43172009-01-22 19:48:26 +0000565 uint32_t NumIds = ReadLE32(IData);
Ted Kremenekcdd8f212009-01-21 07:34:28 +0000566
Ted Kremenek6183e482008-12-03 01:16:39 +0000567 // Pre-allocate the peristent ID -> IdentifierInfo* cache. We use calloc()
568 // so that we in the best case only zero out memory once when the OS returns
569 // us new pages.
Ted Kremenekcdd8f212009-01-21 07:34:28 +0000570 IdentifierInfo** PerIDCache = 0;
Ted Kremenek6183e482008-12-03 01:16:39 +0000571
Ted Kremenekcdd8f212009-01-21 07:34:28 +0000572 if (NumIds) {
573 PerIDCache = (IdentifierInfo**)calloc(NumIds, sizeof(*PerIDCache));
574 if (!PerIDCache) {
575 assert(false && "Could not allocate Persistent ID cache.");
576 return 0;
577 }
Ted Kremenek6183e482008-12-03 01:16:39 +0000578 }
Ted Kremenekcdd8f212009-01-21 07:34:28 +0000579
Ted Kremenek72b1b152009-01-15 18:47:46 +0000580 // Create the new PTHManager.
581 return new PTHManager(File.take(), FL.take(), IData, PerIDCache,
582 SortedIdTable, NumIds);
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000583}
Chris Lattner77ecb3a2009-01-18 02:57:21 +0000584IdentifierInfo* PTHManager::LazilyCreateIdentifierInfo(unsigned PersistentID) {
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000585 // Look in the PTH file for the string data for the IdentifierInfo object.
Chris Lattner77ecb3a2009-01-18 02:57:21 +0000586 const unsigned char* TableEntry = IdDataTable + sizeof(uint32_t)*PersistentID;
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000587 const unsigned char* IDData =
Chris Lattner5ff43172009-01-22 19:48:26 +0000588 (const unsigned char*)Buf->getBufferStart() + ReadLE32(TableEntry);
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000589 assert(IDData < (const unsigned char*)Buf->getBufferEnd());
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000590
Ted Kremenek72b1b152009-01-15 18:47:46 +0000591 // Allocate the object.
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000592 std::pair<IdentifierInfo,const unsigned char*> *Mem =
593 Alloc.Allocate<std::pair<IdentifierInfo,const unsigned char*> >();
Ted Kremenek72b1b152009-01-15 18:47:46 +0000594
595 Mem->second = IDData;
Ted Kremenekea9c26b2009-01-20 23:28:34 +0000596 IdentifierInfo *II = new ((void*) Mem) IdentifierInfo();
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000597
Ted Kremenek72b1b152009-01-15 18:47:46 +0000598 // Store the new IdentifierInfo in the cache.
Chris Lattner77ecb3a2009-01-18 02:57:21 +0000599 PerIDCache[PersistentID] = II;
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000600 return II;
601}
602
Ted Kremenek72b1b152009-01-15 18:47:46 +0000603IdentifierInfo* PTHManager::get(const char *NameStart, const char *NameEnd) {
604 unsigned min = 0;
605 unsigned max = NumIds;
Chris Lattner1b5285e2009-01-18 02:10:31 +0000606 unsigned Len = NameEnd - NameStart;
Ted Kremenek72b1b152009-01-15 18:47:46 +0000607
608 do {
609 unsigned i = (max - min) / 2 + min;
Chris Lattner1b5285e2009-01-18 02:10:31 +0000610 const unsigned char *Ptr = SortedIdTable + (i * 4);
Ted Kremenek72b1b152009-01-15 18:47:46 +0000611
612 // Read the persistentID.
Chris Lattner5ff43172009-01-22 19:48:26 +0000613 unsigned perID = ReadLE32(Ptr);
Ted Kremenek72b1b152009-01-15 18:47:46 +0000614
615 // Get the IdentifierInfo.
616 IdentifierInfo* II = GetIdentifierInfo(perID);
617
618 // First compare the lengths.
619 unsigned IILen = II->getLength();
Chris Lattner1b5285e2009-01-18 02:10:31 +0000620 if (Len < IILen) goto IsLess;
621 if (Len > IILen) goto IsGreater;
Ted Kremenek72b1b152009-01-15 18:47:46 +0000622
623 // Now compare the strings!
624 {
Chris Lattner1b5285e2009-01-18 02:10:31 +0000625 signed comp = strncmp(NameStart, II->getName(), Len);
Ted Kremenek72b1b152009-01-15 18:47:46 +0000626 if (comp < 0) goto IsLess;
627 if (comp > 0) goto IsGreater;
628 }
629 // We found a match!
630 return II;
631
632 IsGreater:
633 if (i == min) break;
634 min = i;
635 continue;
636
637 IsLess:
638 max = i;
639 assert(!(max == min) || (min == i));
640 }
Ted Kremeneke1deaac2009-01-15 19:28:38 +0000641 while (min != max);
Ted Kremenek72b1b152009-01-15 18:47:46 +0000642
643 return 0;
644}
645
646
Chris Lattnerf056d922009-01-17 08:06:50 +0000647PTHLexer *PTHManager::CreateLexer(FileID FID) {
648 const FileEntry *FE = PP->getSourceManager().getFileEntryForID(FID);
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000649 if (!FE)
650 return 0;
651
652 // Lookup the FileEntry object in our file lookup data structure. It will
653 // return a variant that indicates whether or not there is an offset within
654 // the PTH file that contains cached tokens.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000655 PTHFileLookup::Val FileData = ((PTHFileLookup*)FileLookup)->Lookup(FE);
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000656
Ted Kremenekfb645b62008-12-11 23:36:38 +0000657 if (!FileData.isValid()) // No tokens available.
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000658 return 0;
659
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000660 const unsigned char *BufStart = (const unsigned char *)Buf->getBufferStart();
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000661 // Compute the offset of the token data within the buffer.
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000662 const unsigned char* data = BufStart + FileData.getTokenOffset();
Ted Kremenek268ee702008-12-12 18:34:08 +0000663
664 // Get the location of pp-conditional table.
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000665 const unsigned char* ppcond = BufStart + FileData.getPPCondOffset();
Chris Lattner5ff43172009-01-22 19:48:26 +0000666 uint32_t Len = ReadLE32(ppcond);
Chris Lattner1b5285e2009-01-18 02:10:31 +0000667 if (Len == 0) ppcond = 0;
Ted Kremenek32a8ad52009-01-08 04:30:32 +0000668
669 // Get the location of the spelling table.
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000670 const unsigned char* spellingTable = BufStart + FileData.getSpellingOffset();
Ted Kremenek32a8ad52009-01-08 04:30:32 +0000671
Chris Lattner5ff43172009-01-22 19:48:26 +0000672 Len = ReadLE32(spellingTable);
Chris Lattner1b5285e2009-01-18 02:10:31 +0000673 if (Len == 0) spellingTable = 0;
Ted Kremenekb70e3da2009-01-08 02:47:16 +0000674
Chris Lattnerda9d61c2009-01-18 01:57:14 +0000675 assert(data < (const unsigned char*)Buf->getBufferEnd());
Ted Kremenek5f074262009-01-09 22:05:30 +0000676
677 // Create the SpellingSearch object for this FileID.
Chris Lattner1b5285e2009-01-18 02:10:31 +0000678 PTHSpellingSearch* ss = new PTHSpellingSearch(*this, Len, spellingTable);
Chris Lattner2b2453a2009-01-17 06:22:33 +0000679 SpellingMap[FID] = ss;
Ted Kremenek5f074262009-01-09 22:05:30 +0000680
Ted Kremenek72b1b152009-01-15 18:47:46 +0000681 assert(PP && "No preprocessor set yet!");
Chris Lattner2b2453a2009-01-17 06:22:33 +0000682 return new PTHLexer(*PP, FID, data, ppcond, *ss, *this);
Ted Kremenek0c6a77b2008-12-03 00:38:03 +0000683}