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