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