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