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