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