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