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