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