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