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