Ted Kremenek | ca82086 | 2008-11-12 21:37:15 +0000 | [diff] [blame] | 1 | //===--- 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 Kremenek | 325cd30 | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 14 | #include "clang/Basic/TokenKinds.h" |
| 15 | #include "clang/Basic/FileManager.h" |
| 16 | #include "clang/Basic/IdentifierTable.h" |
Ted Kremenek | ca82086 | 2008-11-12 21:37:15 +0000 | [diff] [blame] | 17 | #include "clang/Lex/PTHLexer.h" |
| 18 | #include "clang/Lex/Preprocessor.h" |
Ted Kremenek | 325cd30 | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 19 | #include "clang/Lex/PTHManager.h" |
| 20 | #include "clang/Lex/Token.h" |
| 21 | #include "clang/Lex/Preprocessor.h" |
Ted Kremenek | 325cd30 | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/StringMap.h" |
| 23 | #include "llvm/ADT/OwningPtr.h" |
Chris Lattner | 6b0f1c7 | 2009-01-22 23:50:07 +0000 | [diff] [blame] | 24 | #include "llvm/Support/Compiler.h" |
| 25 | #include "llvm/Support/MathExtras.h" |
| 26 | #include "llvm/Support/MemoryBuffer.h" |
| 27 | #include "llvm/System/Host.h" |
Ted Kremenek | ca82086 | 2008-11-12 21:37:15 +0000 | [diff] [blame] | 28 | using namespace clang; |
| 29 | |
Ted Kremenek | b8344ef | 2009-01-19 23:13:15 +0000 | [diff] [blame] | 30 | #define DISK_TOKEN_SIZE (1+1+2+4+4) |
Ted Kremenek | c07091c | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 31 | |
Ted Kremenek | 325cd30 | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 32 | //===----------------------------------------------------------------------===// |
| 33 | // Utility methods for reading from the mmap'ed PTH file. |
| 34 | //===----------------------------------------------------------------------===// |
| 35 | |
Chris Lattner | 673a286 | 2009-01-22 19:48:26 +0000 | [diff] [blame] | 36 | static inline uint16_t ReadUnalignedLE16(const unsigned char *&Data) { |
Chris Lattner | efb3534 | 2009-01-18 01:57:14 +0000 | [diff] [blame] | 37 | uint16_t V = ((uint16_t)Data[0] << 0) | |
| 38 | ((uint16_t)Data[1] << 8); |
| 39 | Data += 2; |
| 40 | return V; |
| 41 | } |
| 42 | |
Chris Lattner | 673a286 | 2009-01-22 19:48:26 +0000 | [diff] [blame] | 43 | static inline uint32_t ReadLE32(const unsigned char *&Data) { |
Chris Lattner | 007c4dc | 2009-01-23 00:13:28 +0000 | [diff] [blame] | 44 | // Hosts that directly support little-endian 32-bit loads can just |
| 45 | // use them. Big-endian hosts need a bswap. |
Chris Lattner | 5bffcaf | 2009-01-18 02:19:16 +0000 | [diff] [blame] | 46 | uint32_t V = *((uint32_t*)Data); |
Chris Lattner | 6b0f1c7 | 2009-01-22 23:50:07 +0000 | [diff] [blame] | 47 | if (llvm::sys::isBigEndianHost()) |
| 48 | V = llvm::ByteSwap_32(V); |
Chris Lattner | efb3534 | 2009-01-18 01:57:14 +0000 | [diff] [blame] | 49 | Data += 4; |
| 50 | return V; |
| 51 | } |
| 52 | |
| 53 | |
Ted Kremenek | 9ab79bf | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 54 | //===----------------------------------------------------------------------===// |
| 55 | // PTHLexer methods. |
| 56 | //===----------------------------------------------------------------------===// |
| 57 | |
Chris Lattner | efb3534 | 2009-01-18 01:57:14 +0000 | [diff] [blame] | 58 | PTHLexer::PTHLexer(Preprocessor &PP, FileID FID, const unsigned char *D, |
Ted Kremenek | 562db7f | 2009-01-27 00:01:05 +0000 | [diff] [blame^] | 59 | const unsigned char *ppcond, PTHManager &PM) |
Chris Lattner | f4f776a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 60 | : PreprocessorLexer(&PP, FID), TokBuf(D), CurPtr(D), LastHashTokPtr(0), |
Ted Kremenek | 562db7f | 2009-01-27 00:01:05 +0000 | [diff] [blame^] | 61 | PPCond(ppcond), CurPPCondPtr(ppcond), PTHMgr(PM) { |
Chris Lattner | f4f776a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 62 | |
| 63 | FileStartLoc = PP.getSourceManager().getLocForStartOfFile(FID); |
Ted Kremenek | d310fde | 2009-01-09 22:05:30 +0000 | [diff] [blame] | 64 | } |
Ted Kremenek | 9ab79bf | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 65 | |
| 66 | void PTHLexer::Lex(Token& Tok) { |
| 67 | LexNextToken: |
Ted Kremenek | 9ab79bf | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 68 | |
Ted Kremenek | 08d3ff3 | 2008-12-23 02:30:15 +0000 | [diff] [blame] | 69 | //===--------------------------------------==// |
| 70 | // Read the raw token data. |
| 71 | //===--------------------------------------==// |
| 72 | |
| 73 | // Shadow CurPtr into an automatic variable. |
Chris Lattner | ddf3b79 | 2009-01-21 07:21:56 +0000 | [diff] [blame] | 74 | const unsigned char *CurPtrShadow = CurPtr; |
Ted Kremenek | 08d3ff3 | 2008-12-23 02:30:15 +0000 | [diff] [blame] | 75 | |
Chris Lattner | 0a6ec5d | 2009-01-18 02:10:31 +0000 | [diff] [blame] | 76 | // Read in the data for the token. |
Chris Lattner | 673a286 | 2009-01-22 19:48:26 +0000 | [diff] [blame] | 77 | unsigned Word0 = ReadLE32(CurPtrShadow); |
| 78 | uint32_t IdentifierID = ReadLE32(CurPtrShadow); |
| 79 | uint32_t FileOffset = ReadLE32(CurPtrShadow); |
Ted Kremenek | b8344ef | 2009-01-19 23:13:15 +0000 | [diff] [blame] | 80 | |
| 81 | tok::TokenKind TKind = (tok::TokenKind) (Word0 & 0xFF); |
| 82 | Token::TokenFlags TFlags = (Token::TokenFlags) ((Word0 >> 8) & 0xFF); |
Chris Lattner | ddf3b79 | 2009-01-21 07:21:56 +0000 | [diff] [blame] | 83 | uint32_t Len = Word0 >> 16; |
Ted Kremenek | b8344ef | 2009-01-19 23:13:15 +0000 | [diff] [blame] | 84 | |
Chris Lattner | ddf3b79 | 2009-01-21 07:21:56 +0000 | [diff] [blame] | 85 | CurPtr = CurPtrShadow; |
Ted Kremenek | 08d3ff3 | 2008-12-23 02:30:15 +0000 | [diff] [blame] | 86 | |
| 87 | //===--------------------------------------==// |
| 88 | // Construct the token itself. |
| 89 | //===--------------------------------------==// |
| 90 | |
| 91 | Tok.startToken(); |
Chris Lattner | ce8670e | 2009-01-18 02:34:01 +0000 | [diff] [blame] | 92 | Tok.setKind(TKind); |
| 93 | Tok.setFlag(TFlags); |
Ted Kremenek | d94668d | 2008-12-23 19:24:24 +0000 | [diff] [blame] | 94 | assert(!LexingRawMode); |
Chris Lattner | f4f776a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 95 | Tok.setLocation(FileStartLoc.getFileLocWithOffset(FileOffset)); |
Ted Kremenek | 08d3ff3 | 2008-12-23 02:30:15 +0000 | [diff] [blame] | 96 | Tok.setLength(Len); |
| 97 | |
Chris Lattner | 8b2aa22 | 2009-01-21 07:50:06 +0000 | [diff] [blame] | 98 | // Handle identifiers. |
Ted Kremenek | 562db7f | 2009-01-27 00:01:05 +0000 | [diff] [blame^] | 99 | if (Tok.isLiteral()) { |
| 100 | Tok.setLiteralData((const char*) (PTHMgr.SpellingBase + IdentifierID)); |
| 101 | } |
| 102 | else if (IdentifierID) { |
Chris Lattner | 8b2aa22 | 2009-01-21 07:50:06 +0000 | [diff] [blame] | 103 | MIOpt.ReadToken(); |
| 104 | IdentifierInfo *II = PTHMgr.GetIdentifierInfo(IdentifierID-1); |
Chris Lattner | 9ac3dd9 | 2009-01-23 18:35:48 +0000 | [diff] [blame] | 105 | |
Chris Lattner | 8b2aa22 | 2009-01-21 07:50:06 +0000 | [diff] [blame] | 106 | Tok.setIdentifierInfo(II); |
Chris Lattner | 9ac3dd9 | 2009-01-23 18:35:48 +0000 | [diff] [blame] | 107 | |
| 108 | // Change the kind of this identifier to the appropriate token kind, e.g. |
| 109 | // turning "for" into a keyword. |
| 110 | Tok.setKind(II->getTokenID()); |
| 111 | |
Chris Lattner | 8b2aa22 | 2009-01-21 07:50:06 +0000 | [diff] [blame] | 112 | if (II->isHandleIdentifierCase()) |
| 113 | PP->HandleIdentifier(Tok); |
| 114 | return; |
| 115 | } |
| 116 | |
Ted Kremenek | 08d3ff3 | 2008-12-23 02:30:15 +0000 | [diff] [blame] | 117 | //===--------------------------------------==// |
| 118 | // Process the token. |
| 119 | //===--------------------------------------==// |
Ted Kremenek | d310fde | 2009-01-09 22:05:30 +0000 | [diff] [blame] | 120 | #if 0 |
| 121 | SourceManager& SM = PP->getSourceManager(); |
| 122 | llvm::cerr << SM.getFileEntryForID(FileID)->getName() |
| 123 | << ':' << SM.getLogicalLineNumber(Tok.getLocation()) |
| 124 | << ':' << SM.getLogicalColumnNumber(Tok.getLocation()) |
| 125 | << '\n'; |
| 126 | #endif |
Ted Kremenek | 9ab79bf | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 127 | |
Chris Lattner | ce8670e | 2009-01-18 02:34:01 +0000 | [diff] [blame] | 128 | if (TKind == tok::eof) { |
Ted Kremenek | 9ab79bf | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 129 | // Save the end-of-file token. |
| 130 | EofToken = Tok; |
| 131 | |
| 132 | Preprocessor *PPCache = PP; |
Ted Kremenek | d94668d | 2008-12-23 19:24:24 +0000 | [diff] [blame] | 133 | |
| 134 | assert(!ParsingPreprocessorDirective); |
| 135 | assert(!LexingRawMode); |
| 136 | |
| 137 | // FIXME: Issue diagnostics similar to Lexer. |
| 138 | if (PP->HandleEndOfFile(Tok, false)) |
Ted Kremenek | 9ab79bf | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 139 | return; |
Ted Kremenek | d94668d | 2008-12-23 19:24:24 +0000 | [diff] [blame] | 140 | |
Ted Kremenek | 9ab79bf | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 141 | assert(PPCache && "Raw buffer::LexEndOfFile should return a token"); |
| 142 | return PPCache->Lex(Tok); |
| 143 | } |
Ted Kremenek | 9ab79bf | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 144 | |
Chris Lattner | ce8670e | 2009-01-18 02:34:01 +0000 | [diff] [blame] | 145 | if (TKind == tok::hash && Tok.isAtStartOfLine()) { |
Ted Kremenek | d94668d | 2008-12-23 19:24:24 +0000 | [diff] [blame] | 146 | LastHashTokPtr = CurPtr - DISK_TOKEN_SIZE; |
| 147 | assert(!LexingRawMode); |
| 148 | PP->HandleDirective(Tok); |
| 149 | |
| 150 | if (PP->isCurrentLexer(this)) |
| 151 | goto LexNextToken; |
| 152 | |
| 153 | return PP->Lex(Tok); |
| 154 | } |
| 155 | |
Chris Lattner | ce8670e | 2009-01-18 02:34:01 +0000 | [diff] [blame] | 156 | if (TKind == tok::eom) { |
Ted Kremenek | d94668d | 2008-12-23 19:24:24 +0000 | [diff] [blame] | 157 | assert(ParsingPreprocessorDirective); |
Ted Kremenek | 9ab79bf | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 158 | ParsingPreprocessorDirective = false; |
| 159 | return; |
| 160 | } |
Ted Kremenek | 9ab79bf | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 161 | |
Ted Kremenek | d94668d | 2008-12-23 19:24:24 +0000 | [diff] [blame] | 162 | MIOpt.ReadToken(); |
Ted Kremenek | 9ab79bf | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | // FIXME: We can just grab the last token instead of storing a copy |
| 166 | // into EofToken. |
Ted Kremenek | d94668d | 2008-12-23 19:24:24 +0000 | [diff] [blame] | 167 | void PTHLexer::getEOF(Token& Tok) { |
Ted Kremenek | e4caf14 | 2009-01-09 00:36:11 +0000 | [diff] [blame] | 168 | assert(EofToken.is(tok::eof)); |
Ted Kremenek | 9ab79bf | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 169 | Tok = EofToken; |
| 170 | } |
| 171 | |
| 172 | void PTHLexer::DiscardToEndOfLine() { |
| 173 | assert(ParsingPreprocessorDirective && ParsingFilename == false && |
| 174 | "Must be in a preprocessing directive!"); |
| 175 | |
| 176 | // We assume that if the preprocessor wishes to discard to the end of |
| 177 | // the line that it also means to end the current preprocessor directive. |
| 178 | ParsingPreprocessorDirective = false; |
| 179 | |
| 180 | // Skip tokens by only peeking at their token kind and the flags. |
| 181 | // We don't need to actually reconstruct full tokens from the token buffer. |
| 182 | // This saves some copies and it also reduces IdentifierInfo* lookup. |
Chris Lattner | efb3534 | 2009-01-18 01:57:14 +0000 | [diff] [blame] | 183 | const unsigned char* p = CurPtr; |
Ted Kremenek | 9ab79bf | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 184 | while (1) { |
| 185 | // Read the token kind. Are we at the end of the file? |
| 186 | tok::TokenKind x = (tok::TokenKind) (uint8_t) *p; |
| 187 | if (x == tok::eof) break; |
| 188 | |
| 189 | // Read the token flags. Are we at the start of the next line? |
| 190 | Token::TokenFlags y = (Token::TokenFlags) (uint8_t) p[1]; |
| 191 | if (y & Token::StartOfLine) break; |
| 192 | |
| 193 | // Skip to the next token. |
| 194 | p += DISK_TOKEN_SIZE; |
| 195 | } |
| 196 | |
| 197 | CurPtr = p; |
| 198 | } |
| 199 | |
Ted Kremenek | c07091c | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 200 | /// SkipBlock - Used by Preprocessor to skip the current conditional block. |
| 201 | bool PTHLexer::SkipBlock() { |
| 202 | assert(CurPPCondPtr && "No cached PP conditional information."); |
| 203 | assert(LastHashTokPtr && "No known '#' token."); |
| 204 | |
Chris Lattner | efb3534 | 2009-01-18 01:57:14 +0000 | [diff] [blame] | 205 | const unsigned char* HashEntryI = 0; |
Ted Kremenek | c07091c | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 206 | uint32_t Offset; |
| 207 | uint32_t TableIdx; |
| 208 | |
| 209 | do { |
Ted Kremenek | be3e84f | 2008-12-12 22:05:38 +0000 | [diff] [blame] | 210 | // Read the token offset from the side-table. |
Chris Lattner | 673a286 | 2009-01-22 19:48:26 +0000 | [diff] [blame] | 211 | Offset = ReadLE32(CurPPCondPtr); |
Ted Kremenek | be3e84f | 2008-12-12 22:05:38 +0000 | [diff] [blame] | 212 | |
| 213 | // Read the target table index from the side-table. |
Chris Lattner | 673a286 | 2009-01-22 19:48:26 +0000 | [diff] [blame] | 214 | TableIdx = ReadLE32(CurPPCondPtr); |
Ted Kremenek | be3e84f | 2008-12-12 22:05:38 +0000 | [diff] [blame] | 215 | |
| 216 | // Compute the actual memory address of the '#' token data for this entry. |
| 217 | HashEntryI = TokBuf + Offset; |
| 218 | |
| 219 | // Optmization: "Sibling jumping". #if...#else...#endif blocks can |
| 220 | // contain nested blocks. In the side-table we can jump over these |
| 221 | // nested blocks instead of doing a linear search if the next "sibling" |
| 222 | // entry is not at a location greater than LastHashTokPtr. |
| 223 | if (HashEntryI < LastHashTokPtr && TableIdx) { |
| 224 | // In the side-table we are still at an entry for a '#' token that |
| 225 | // is earlier than the last one we saw. Check if the location we would |
| 226 | // stride gets us closer. |
Chris Lattner | efb3534 | 2009-01-18 01:57:14 +0000 | [diff] [blame] | 227 | const unsigned char* NextPPCondPtr = |
| 228 | PPCond + TableIdx*(sizeof(uint32_t)*2); |
Ted Kremenek | be3e84f | 2008-12-12 22:05:38 +0000 | [diff] [blame] | 229 | assert(NextPPCondPtr >= CurPPCondPtr); |
| 230 | // Read where we should jump to. |
Chris Lattner | 673a286 | 2009-01-22 19:48:26 +0000 | [diff] [blame] | 231 | uint32_t TmpOffset = ReadLE32(NextPPCondPtr); |
Chris Lattner | efb3534 | 2009-01-18 01:57:14 +0000 | [diff] [blame] | 232 | const unsigned char* HashEntryJ = TokBuf + TmpOffset; |
Ted Kremenek | be3e84f | 2008-12-12 22:05:38 +0000 | [diff] [blame] | 233 | |
| 234 | if (HashEntryJ <= LastHashTokPtr) { |
| 235 | // Jump directly to the next entry in the side table. |
| 236 | HashEntryI = HashEntryJ; |
| 237 | Offset = TmpOffset; |
Chris Lattner | 673a286 | 2009-01-22 19:48:26 +0000 | [diff] [blame] | 238 | TableIdx = ReadLE32(NextPPCondPtr); |
Ted Kremenek | be3e84f | 2008-12-12 22:05:38 +0000 | [diff] [blame] | 239 | CurPPCondPtr = NextPPCondPtr; |
| 240 | } |
| 241 | } |
Ted Kremenek | c07091c | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 242 | } |
Ted Kremenek | be3e84f | 2008-12-12 22:05:38 +0000 | [diff] [blame] | 243 | while (HashEntryI < LastHashTokPtr); |
| 244 | assert(HashEntryI == LastHashTokPtr && "No PP-cond entry found for '#'"); |
Ted Kremenek | c07091c | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 245 | assert(TableIdx && "No jumping from #endifs."); |
| 246 | |
| 247 | // Update our side-table iterator. |
Chris Lattner | efb3534 | 2009-01-18 01:57:14 +0000 | [diff] [blame] | 248 | const unsigned char* NextPPCondPtr = PPCond + TableIdx*(sizeof(uint32_t)*2); |
Ted Kremenek | c07091c | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 249 | assert(NextPPCondPtr >= CurPPCondPtr); |
| 250 | CurPPCondPtr = NextPPCondPtr; |
| 251 | |
| 252 | // Read where we should jump to. |
Chris Lattner | 673a286 | 2009-01-22 19:48:26 +0000 | [diff] [blame] | 253 | HashEntryI = TokBuf + ReadLE32(NextPPCondPtr); |
| 254 | uint32_t NextIdx = ReadLE32(NextPPCondPtr); |
Ted Kremenek | c07091c | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 255 | |
| 256 | // By construction NextIdx will be zero if this is a #endif. This is useful |
| 257 | // to know to obviate lexing another token. |
| 258 | bool isEndif = NextIdx == 0; |
Ted Kremenek | c07091c | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 259 | |
| 260 | // This case can occur when we see something like this: |
| 261 | // |
| 262 | // #if ... |
| 263 | // /* a comment or nothing */ |
| 264 | // #elif |
| 265 | // |
| 266 | // If we are skipping the first #if block it will be the case that CurPtr |
| 267 | // already points 'elif'. Just return. |
| 268 | |
Ted Kremenek | be3e84f | 2008-12-12 22:05:38 +0000 | [diff] [blame] | 269 | if (CurPtr > HashEntryI) { |
| 270 | assert(CurPtr == HashEntryI + DISK_TOKEN_SIZE); |
Ted Kremenek | c07091c | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 271 | // Did we reach a #endif? If so, go ahead and consume that token as well. |
| 272 | if (isEndif) |
Ted Kremenek | 9ab79bf | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 273 | CurPtr += DISK_TOKEN_SIZE*2; |
Ted Kremenek | c07091c | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 274 | else |
Ted Kremenek | be3e84f | 2008-12-12 22:05:38 +0000 | [diff] [blame] | 275 | LastHashTokPtr = HashEntryI; |
Ted Kremenek | c07091c | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 276 | |
| 277 | return isEndif; |
| 278 | } |
| 279 | |
| 280 | // Otherwise, we need to advance. Update CurPtr to point to the '#' token. |
Ted Kremenek | be3e84f | 2008-12-12 22:05:38 +0000 | [diff] [blame] | 281 | CurPtr = HashEntryI; |
Ted Kremenek | c07091c | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 282 | |
| 283 | // Update the location of the last observed '#'. This is useful if we |
| 284 | // are skipping multiple blocks. |
| 285 | LastHashTokPtr = CurPtr; |
Ted Kremenek | c07091c | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 286 | |
Ted Kremenek | 9ab79bf | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 287 | // Skip the '#' token. |
Chris Lattner | efb3534 | 2009-01-18 01:57:14 +0000 | [diff] [blame] | 288 | assert(((tok::TokenKind)*CurPtr) == tok::hash); |
Ted Kremenek | 9ab79bf | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 289 | CurPtr += DISK_TOKEN_SIZE; |
| 290 | |
Ted Kremenek | c07091c | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 291 | // Did we reach a #endif? If so, go ahead and consume that token as well. |
Ted Kremenek | 9ab79bf | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 292 | if (isEndif) { CurPtr += DISK_TOKEN_SIZE*2; } |
Ted Kremenek | c07091c | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 293 | |
| 294 | return isEndif; |
| 295 | } |
| 296 | |
Ted Kremenek | beb5767 | 2008-12-17 23:36:32 +0000 | [diff] [blame] | 297 | SourceLocation PTHLexer::getSourceLocation() { |
Chris Lattner | 0a6ec5d | 2009-01-18 02:10:31 +0000 | [diff] [blame] | 298 | // getSourceLocation is not on the hot path. It is used to get the location |
| 299 | // of the next token when transitioning back to this lexer when done |
Ted Kremenek | beb5767 | 2008-12-17 23:36:32 +0000 | [diff] [blame] | 300 | // handling a #included file. Just read the necessary data from the token |
| 301 | // data buffer to construct the SourceLocation object. |
| 302 | // NOTE: This is a virtual function; hence it is defined out-of-line. |
Ted Kremenek | de04cb4 | 2009-01-21 22:41:38 +0000 | [diff] [blame] | 303 | const unsigned char *OffsetPtr = CurPtr + (DISK_TOKEN_SIZE - 4); |
Chris Lattner | 673a286 | 2009-01-22 19:48:26 +0000 | [diff] [blame] | 304 | uint32_t Offset = ReadLE32(OffsetPtr); |
Chris Lattner | 0a6ec5d | 2009-01-18 02:10:31 +0000 | [diff] [blame] | 305 | return FileStartLoc.getFileLocWithOffset(Offset); |
Ted Kremenek | beb5767 | 2008-12-17 23:36:32 +0000 | [diff] [blame] | 306 | } |
| 307 | |
Ted Kremenek | d310fde | 2009-01-09 22:05:30 +0000 | [diff] [blame] | 308 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 325cd30 | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 309 | // Internal Data Structures for PTH file lookup and resolving identifiers. |
| 310 | //===----------------------------------------------------------------------===// |
| 311 | |
Ted Kremenek | 325cd30 | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 312 | |
| 313 | /// PTHFileLookup - This internal data structure is used by the PTHManager |
| 314 | /// to map from FileEntry objects managed by FileManager to offsets within |
| 315 | /// the PTH file. |
| 316 | namespace { |
| 317 | class VISIBILITY_HIDDEN PTHFileLookup { |
| 318 | public: |
| 319 | class Val { |
Ted Kremenek | 8309c92 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 320 | uint32_t TokenOff; |
| 321 | uint32_t PPCondOff; |
Ted Kremenek | 325cd30 | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 322 | public: |
Ted Kremenek | 8309c92 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 323 | Val() : TokenOff(~0) {} |
Ted Kremenek | 562db7f | 2009-01-27 00:01:05 +0000 | [diff] [blame^] | 324 | Val(uint32_t toff, uint32_t poff) |
| 325 | : TokenOff(toff), PPCondOff(poff) {} |
Ted Kremenek | 325cd30 | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 326 | |
Chris Lattner | 0a6ec5d | 2009-01-18 02:10:31 +0000 | [diff] [blame] | 327 | bool isValid() const { return TokenOff != ~((uint32_t)0); } |
| 328 | |
Ted Kremenek | 8309c92 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 329 | uint32_t getTokenOffset() const { |
Chris Lattner | 0a6ec5d | 2009-01-18 02:10:31 +0000 | [diff] [blame] | 330 | assert(isValid() && "PTHFileLookup entry initialized."); |
Ted Kremenek | 8309c92 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 331 | return TokenOff; |
Ted Kremenek | 325cd30 | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 332 | } |
| 333 | |
Ted Kremenek | d2c849d | 2009-01-08 02:47:16 +0000 | [diff] [blame] | 334 | uint32_t getPPCondOffset() const { |
Chris Lattner | 0a6ec5d | 2009-01-18 02:10:31 +0000 | [diff] [blame] | 335 | assert(isValid() && "PTHFileLookup entry initialized."); |
Ted Kremenek | 8309c92 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 336 | return PPCondOff; |
Ted Kremenek | 562db7f | 2009-01-27 00:01:05 +0000 | [diff] [blame^] | 337 | } |
Ted Kremenek | 325cd30 | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 338 | }; |
| 339 | |
| 340 | private: |
| 341 | llvm::StringMap<Val> FileMap; |
| 342 | |
| 343 | public: |
| 344 | PTHFileLookup() {}; |
| 345 | |
Ted Kremenek | abaf7eb | 2009-01-21 07:34:28 +0000 | [diff] [blame] | 346 | bool isEmpty() const { |
| 347 | return FileMap.empty(); |
| 348 | } |
| 349 | |
Ted Kremenek | 325cd30 | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 350 | Val Lookup(const FileEntry* FE) { |
| 351 | const char* s = FE->getName(); |
| 352 | unsigned size = strlen(s); |
| 353 | return FileMap.GetOrCreateValue(s, s+size).getValue(); |
| 354 | } |
| 355 | |
Chris Lattner | efb3534 | 2009-01-18 01:57:14 +0000 | [diff] [blame] | 356 | void ReadTable(const unsigned char* D) { |
Chris Lattner | 673a286 | 2009-01-22 19:48:26 +0000 | [diff] [blame] | 357 | uint32_t N = ReadLE32(D); // Read the length of the table. |
Ted Kremenek | 325cd30 | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 358 | |
| 359 | for ( ; N > 0; --N) { // The rest of the data is the table itself. |
Chris Lattner | 673a286 | 2009-01-22 19:48:26 +0000 | [diff] [blame] | 360 | uint32_t Len = ReadLE32(D); |
Chris Lattner | efb3534 | 2009-01-18 01:57:14 +0000 | [diff] [blame] | 361 | const char* s = (const char *)D; |
Chris Lattner | 0a6ec5d | 2009-01-18 02:10:31 +0000 | [diff] [blame] | 362 | D += Len; |
Ted Kremenek | d2c849d | 2009-01-08 02:47:16 +0000 | [diff] [blame] | 363 | |
Chris Lattner | 673a286 | 2009-01-22 19:48:26 +0000 | [diff] [blame] | 364 | uint32_t TokenOff = ReadLE32(D); |
| 365 | uint32_t PPCondOff = ReadLE32(D); |
Ted Kremenek | d2c849d | 2009-01-08 02:47:16 +0000 | [diff] [blame] | 366 | |
Chris Lattner | 0a6ec5d | 2009-01-18 02:10:31 +0000 | [diff] [blame] | 367 | FileMap.GetOrCreateValue(s, s+Len).getValue() = |
Ted Kremenek | 562db7f | 2009-01-27 00:01:05 +0000 | [diff] [blame^] | 368 | Val(TokenOff, PPCondOff); |
Ted Kremenek | 325cd30 | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 369 | } |
| 370 | } |
| 371 | }; |
| 372 | } // end anonymous namespace |
| 373 | |
| 374 | //===----------------------------------------------------------------------===// |
| 375 | // PTHManager methods. |
| 376 | //===----------------------------------------------------------------------===// |
| 377 | |
| 378 | PTHManager::PTHManager(const llvm::MemoryBuffer* buf, void* fileLookup, |
Chris Lattner | efb3534 | 2009-01-18 01:57:14 +0000 | [diff] [blame] | 379 | const unsigned char* idDataTable, |
| 380 | IdentifierInfo** perIDCache, |
Ted Kremenek | 562db7f | 2009-01-27 00:01:05 +0000 | [diff] [blame^] | 381 | const unsigned char* sortedIdTable, unsigned numIds, |
| 382 | const unsigned char* spellingBase) |
Ted Kremenek | db4c8e8 | 2008-12-03 01:16:39 +0000 | [diff] [blame] | 383 | : Buf(buf), PerIDCache(perIDCache), FileLookup(fileLookup), |
Ted Kremenek | d976c3d | 2009-01-15 18:47:46 +0000 | [diff] [blame] | 384 | IdDataTable(idDataTable), SortedIdTable(sortedIdTable), |
Ted Kremenek | 562db7f | 2009-01-27 00:01:05 +0000 | [diff] [blame^] | 385 | NumIds(numIds), PP(0), SpellingBase(spellingBase) {} |
Ted Kremenek | 325cd30 | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 386 | |
| 387 | PTHManager::~PTHManager() { |
| 388 | delete Buf; |
| 389 | delete (PTHFileLookup*) FileLookup; |
Ted Kremenek | 93bdc49 | 2008-12-04 22:47:11 +0000 | [diff] [blame] | 390 | free(PerIDCache); |
Ted Kremenek | 325cd30 | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 391 | } |
| 392 | |
Ted Kremenek | d976c3d | 2009-01-15 18:47:46 +0000 | [diff] [blame] | 393 | PTHManager* PTHManager::Create(const std::string& file) { |
Ted Kremenek | 325cd30 | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 394 | // Memory map the PTH file. |
| 395 | llvm::OwningPtr<llvm::MemoryBuffer> |
| 396 | File(llvm::MemoryBuffer::getFile(file.c_str())); |
| 397 | |
| 398 | if (!File) |
| 399 | return 0; |
| 400 | |
| 401 | // Get the buffer ranges and check if there are at least three 32-bit |
| 402 | // words at the end of the file. |
Chris Lattner | efb3534 | 2009-01-18 01:57:14 +0000 | [diff] [blame] | 403 | const unsigned char* BufBeg = (unsigned char*)File->getBufferStart(); |
| 404 | const unsigned char* BufEnd = (unsigned char*)File->getBufferEnd(); |
Ted Kremenek | 58b9f93 | 2009-01-26 21:43:14 +0000 | [diff] [blame] | 405 | |
| 406 | // Check the prologue of the file. |
Ted Kremenek | 497aba3 | 2009-01-26 22:16:12 +0000 | [diff] [blame] | 407 | if ((BufEnd - BufBeg) < (signed) (sizeof("cfe-pth") + 3 + 4) || |
Ted Kremenek | 58b9f93 | 2009-01-26 21:43:14 +0000 | [diff] [blame] | 408 | memcmp(BufBeg, "cfe-pth", sizeof("cfe-pth") - 1) != 0) |
| 409 | return 0; |
Ted Kremenek | 325cd30 | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 410 | |
Ted Kremenek | 169fc35 | 2009-01-26 21:50:21 +0000 | [diff] [blame] | 411 | // Read the PTH version. |
Ted Kremenek | 58b9f93 | 2009-01-26 21:43:14 +0000 | [diff] [blame] | 412 | const unsigned char *p = BufBeg + (sizeof("cfe-pth") - 1); |
Ted Kremenek | 169fc35 | 2009-01-26 21:50:21 +0000 | [diff] [blame] | 413 | unsigned Version = ReadLE32(p); |
| 414 | |
| 415 | if (Version != PTHManager::Version) |
| 416 | return 0; |
| 417 | |
| 418 | // Compute the address of the index table at the end of the PTH file. |
Ted Kremenek | 58b9f93 | 2009-01-26 21:43:14 +0000 | [diff] [blame] | 419 | const unsigned char *EndTable = BufBeg + ReadLE32(p); |
| 420 | |
| 421 | if (EndTable >= BufEnd) |
| 422 | return 0; |
Ted Kremenek | 325cd30 | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 423 | |
| 424 | // Construct the file lookup table. This will be used for mapping from |
| 425 | // FileEntry*'s to cached tokens. |
Chris Lattner | efb3534 | 2009-01-18 01:57:14 +0000 | [diff] [blame] | 426 | const unsigned char* FileTableOffset = EndTable + sizeof(uint32_t)*3; |
Chris Lattner | 673a286 | 2009-01-22 19:48:26 +0000 | [diff] [blame] | 427 | const unsigned char* FileTable = BufBeg + ReadLE32(FileTableOffset); |
Ted Kremenek | 325cd30 | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 428 | |
| 429 | if (!(FileTable > BufBeg && FileTable < BufEnd)) { |
| 430 | assert(false && "Invalid PTH file."); |
| 431 | return 0; // FIXME: Proper error diagnostic? |
| 432 | } |
| 433 | |
| 434 | llvm::OwningPtr<PTHFileLookup> FL(new PTHFileLookup()); |
| 435 | FL->ReadTable(FileTable); |
Ted Kremenek | abaf7eb | 2009-01-21 07:34:28 +0000 | [diff] [blame] | 436 | |
| 437 | if (FL->isEmpty()) |
| 438 | return 0; |
Ted Kremenek | 325cd30 | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 439 | |
| 440 | // Get the location of the table mapping from persistent ids to the |
| 441 | // data needed to reconstruct identifiers. |
Chris Lattner | efb3534 | 2009-01-18 01:57:14 +0000 | [diff] [blame] | 442 | const unsigned char* IDTableOffset = EndTable + sizeof(uint32_t)*1; |
Chris Lattner | 673a286 | 2009-01-22 19:48:26 +0000 | [diff] [blame] | 443 | const unsigned char* IData = BufBeg + ReadLE32(IDTableOffset); |
Ted Kremenek | abaf7eb | 2009-01-21 07:34:28 +0000 | [diff] [blame] | 444 | |
| 445 | if (!(IData >= BufBeg && IData < BufEnd)) { |
Ted Kremenek | 325cd30 | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 446 | assert(false && "Invalid PTH file."); |
| 447 | return 0; // FIXME: Proper error diagnostic? |
| 448 | } |
| 449 | |
Ted Kremenek | d976c3d | 2009-01-15 18:47:46 +0000 | [diff] [blame] | 450 | // Get the location of the lexigraphically-sorted table of persistent IDs. |
Chris Lattner | efb3534 | 2009-01-18 01:57:14 +0000 | [diff] [blame] | 451 | const unsigned char* SortedIdTableOffset = EndTable + sizeof(uint32_t)*2; |
Chris Lattner | 673a286 | 2009-01-22 19:48:26 +0000 | [diff] [blame] | 452 | const unsigned char* SortedIdTable = BufBeg + ReadLE32(SortedIdTableOffset); |
Ted Kremenek | abaf7eb | 2009-01-21 07:34:28 +0000 | [diff] [blame] | 453 | if (!(SortedIdTable >= BufBeg && SortedIdTable < BufEnd)) { |
Ted Kremenek | d976c3d | 2009-01-15 18:47:46 +0000 | [diff] [blame] | 454 | assert(false && "Invalid PTH file."); |
| 455 | return 0; // FIXME: Proper error diagnostic? |
| 456 | } |
| 457 | |
Ted Kremenek | 562db7f | 2009-01-27 00:01:05 +0000 | [diff] [blame^] | 458 | // Get the location of the spelling cache. |
| 459 | const unsigned char* spellingBaseOffset = EndTable + sizeof(uint32_t)*4; |
| 460 | const unsigned char* spellingBase = BufBeg + ReadLE32(spellingBaseOffset); |
| 461 | if (!(spellingBase >= BufBeg && spellingBase < BufEnd)) { |
| 462 | assert(false && "Invalid PTH file."); |
| 463 | return 0; |
| 464 | } |
| 465 | |
Ted Kremenek | db4c8e8 | 2008-12-03 01:16:39 +0000 | [diff] [blame] | 466 | // Get the number of IdentifierInfos and pre-allocate the identifier cache. |
Chris Lattner | 673a286 | 2009-01-22 19:48:26 +0000 | [diff] [blame] | 467 | uint32_t NumIds = ReadLE32(IData); |
Ted Kremenek | abaf7eb | 2009-01-21 07:34:28 +0000 | [diff] [blame] | 468 | |
Ted Kremenek | db4c8e8 | 2008-12-03 01:16:39 +0000 | [diff] [blame] | 469 | // Pre-allocate the peristent ID -> IdentifierInfo* cache. We use calloc() |
| 470 | // so that we in the best case only zero out memory once when the OS returns |
| 471 | // us new pages. |
Ted Kremenek | abaf7eb | 2009-01-21 07:34:28 +0000 | [diff] [blame] | 472 | IdentifierInfo** PerIDCache = 0; |
Ted Kremenek | db4c8e8 | 2008-12-03 01:16:39 +0000 | [diff] [blame] | 473 | |
Ted Kremenek | abaf7eb | 2009-01-21 07:34:28 +0000 | [diff] [blame] | 474 | if (NumIds) { |
| 475 | PerIDCache = (IdentifierInfo**)calloc(NumIds, sizeof(*PerIDCache)); |
| 476 | if (!PerIDCache) { |
| 477 | assert(false && "Could not allocate Persistent ID cache."); |
| 478 | return 0; |
| 479 | } |
Ted Kremenek | db4c8e8 | 2008-12-03 01:16:39 +0000 | [diff] [blame] | 480 | } |
Ted Kremenek | abaf7eb | 2009-01-21 07:34:28 +0000 | [diff] [blame] | 481 | |
Ted Kremenek | d976c3d | 2009-01-15 18:47:46 +0000 | [diff] [blame] | 482 | // Create the new PTHManager. |
| 483 | return new PTHManager(File.take(), FL.take(), IData, PerIDCache, |
Ted Kremenek | 562db7f | 2009-01-27 00:01:05 +0000 | [diff] [blame^] | 484 | SortedIdTable, NumIds, spellingBase); |
Ted Kremenek | 325cd30 | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 485 | } |
Chris Lattner | cbcd26c | 2009-01-18 02:57:21 +0000 | [diff] [blame] | 486 | IdentifierInfo* PTHManager::LazilyCreateIdentifierInfo(unsigned PersistentID) { |
Ted Kremenek | 325cd30 | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 487 | // Look in the PTH file for the string data for the IdentifierInfo object. |
Chris Lattner | cbcd26c | 2009-01-18 02:57:21 +0000 | [diff] [blame] | 488 | const unsigned char* TableEntry = IdDataTable + sizeof(uint32_t)*PersistentID; |
Chris Lattner | efb3534 | 2009-01-18 01:57:14 +0000 | [diff] [blame] | 489 | const unsigned char* IDData = |
Chris Lattner | 673a286 | 2009-01-22 19:48:26 +0000 | [diff] [blame] | 490 | (const unsigned char*)Buf->getBufferStart() + ReadLE32(TableEntry); |
Chris Lattner | efb3534 | 2009-01-18 01:57:14 +0000 | [diff] [blame] | 491 | assert(IDData < (const unsigned char*)Buf->getBufferEnd()); |
Ted Kremenek | 325cd30 | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 492 | |
Ted Kremenek | d976c3d | 2009-01-15 18:47:46 +0000 | [diff] [blame] | 493 | // Allocate the object. |
Chris Lattner | efb3534 | 2009-01-18 01:57:14 +0000 | [diff] [blame] | 494 | std::pair<IdentifierInfo,const unsigned char*> *Mem = |
| 495 | Alloc.Allocate<std::pair<IdentifierInfo,const unsigned char*> >(); |
Ted Kremenek | d976c3d | 2009-01-15 18:47:46 +0000 | [diff] [blame] | 496 | |
| 497 | Mem->second = IDData; |
Ted Kremenek | e807f85 | 2009-01-20 23:28:34 +0000 | [diff] [blame] | 498 | IdentifierInfo *II = new ((void*) Mem) IdentifierInfo(); |
Ted Kremenek | 325cd30 | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 499 | |
Ted Kremenek | d976c3d | 2009-01-15 18:47:46 +0000 | [diff] [blame] | 500 | // Store the new IdentifierInfo in the cache. |
Chris Lattner | cbcd26c | 2009-01-18 02:57:21 +0000 | [diff] [blame] | 501 | PerIDCache[PersistentID] = II; |
Ted Kremenek | 325cd30 | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 502 | return II; |
| 503 | } |
| 504 | |
Ted Kremenek | d976c3d | 2009-01-15 18:47:46 +0000 | [diff] [blame] | 505 | IdentifierInfo* PTHManager::get(const char *NameStart, const char *NameEnd) { |
| 506 | unsigned min = 0; |
| 507 | unsigned max = NumIds; |
Chris Lattner | 0a6ec5d | 2009-01-18 02:10:31 +0000 | [diff] [blame] | 508 | unsigned Len = NameEnd - NameStart; |
Ted Kremenek | d976c3d | 2009-01-15 18:47:46 +0000 | [diff] [blame] | 509 | |
| 510 | do { |
| 511 | unsigned i = (max - min) / 2 + min; |
Chris Lattner | 0a6ec5d | 2009-01-18 02:10:31 +0000 | [diff] [blame] | 512 | const unsigned char *Ptr = SortedIdTable + (i * 4); |
Ted Kremenek | d976c3d | 2009-01-15 18:47:46 +0000 | [diff] [blame] | 513 | |
| 514 | // Read the persistentID. |
Chris Lattner | 673a286 | 2009-01-22 19:48:26 +0000 | [diff] [blame] | 515 | unsigned perID = ReadLE32(Ptr); |
Ted Kremenek | d976c3d | 2009-01-15 18:47:46 +0000 | [diff] [blame] | 516 | |
| 517 | // Get the IdentifierInfo. |
| 518 | IdentifierInfo* II = GetIdentifierInfo(perID); |
| 519 | |
| 520 | // First compare the lengths. |
| 521 | unsigned IILen = II->getLength(); |
Chris Lattner | 0a6ec5d | 2009-01-18 02:10:31 +0000 | [diff] [blame] | 522 | if (Len < IILen) goto IsLess; |
| 523 | if (Len > IILen) goto IsGreater; |
Ted Kremenek | d976c3d | 2009-01-15 18:47:46 +0000 | [diff] [blame] | 524 | |
| 525 | // Now compare the strings! |
| 526 | { |
Chris Lattner | 0a6ec5d | 2009-01-18 02:10:31 +0000 | [diff] [blame] | 527 | signed comp = strncmp(NameStart, II->getName(), Len); |
Ted Kremenek | d976c3d | 2009-01-15 18:47:46 +0000 | [diff] [blame] | 528 | if (comp < 0) goto IsLess; |
| 529 | if (comp > 0) goto IsGreater; |
| 530 | } |
| 531 | // We found a match! |
| 532 | return II; |
| 533 | |
| 534 | IsGreater: |
| 535 | if (i == min) break; |
| 536 | min = i; |
| 537 | continue; |
| 538 | |
| 539 | IsLess: |
| 540 | max = i; |
| 541 | assert(!(max == min) || (min == i)); |
| 542 | } |
Ted Kremenek | 4795ad0 | 2009-01-15 19:28:38 +0000 | [diff] [blame] | 543 | while (min != max); |
Ted Kremenek | d976c3d | 2009-01-15 18:47:46 +0000 | [diff] [blame] | 544 | |
| 545 | return 0; |
| 546 | } |
| 547 | |
| 548 | |
Chris Lattner | 3c727d6 | 2009-01-17 08:06:50 +0000 | [diff] [blame] | 549 | PTHLexer *PTHManager::CreateLexer(FileID FID) { |
| 550 | const FileEntry *FE = PP->getSourceManager().getFileEntryForID(FID); |
Ted Kremenek | 325cd30 | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 551 | if (!FE) |
| 552 | return 0; |
| 553 | |
| 554 | // Lookup the FileEntry object in our file lookup data structure. It will |
| 555 | // return a variant that indicates whether or not there is an offset within |
| 556 | // the PTH file that contains cached tokens. |
Chris Lattner | f4f776a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 557 | PTHFileLookup::Val FileData = ((PTHFileLookup*)FileLookup)->Lookup(FE); |
Ted Kremenek | 325cd30 | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 558 | |
Ted Kremenek | 8309c92 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 559 | if (!FileData.isValid()) // No tokens available. |
Ted Kremenek | 325cd30 | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 560 | return 0; |
| 561 | |
Chris Lattner | efb3534 | 2009-01-18 01:57:14 +0000 | [diff] [blame] | 562 | const unsigned char *BufStart = (const unsigned char *)Buf->getBufferStart(); |
Ted Kremenek | 325cd30 | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 563 | // Compute the offset of the token data within the buffer. |
Chris Lattner | efb3534 | 2009-01-18 01:57:14 +0000 | [diff] [blame] | 564 | const unsigned char* data = BufStart + FileData.getTokenOffset(); |
Ted Kremenek | c07091c | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 565 | |
| 566 | // Get the location of pp-conditional table. |
Chris Lattner | efb3534 | 2009-01-18 01:57:14 +0000 | [diff] [blame] | 567 | const unsigned char* ppcond = BufStart + FileData.getPPCondOffset(); |
Chris Lattner | 673a286 | 2009-01-22 19:48:26 +0000 | [diff] [blame] | 568 | uint32_t Len = ReadLE32(ppcond); |
Chris Lattner | 0a6ec5d | 2009-01-18 02:10:31 +0000 | [diff] [blame] | 569 | if (Len == 0) ppcond = 0; |
Ted Kremenek | a4d5bf5 | 2009-01-08 04:30:32 +0000 | [diff] [blame] | 570 | |
Ted Kremenek | d976c3d | 2009-01-15 18:47:46 +0000 | [diff] [blame] | 571 | assert(PP && "No preprocessor set yet!"); |
Ted Kremenek | 562db7f | 2009-01-27 00:01:05 +0000 | [diff] [blame^] | 572 | return new PTHLexer(*PP, FID, data, ppcond, *this); |
Ted Kremenek | 325cd30 | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 573 | } |