Ted Kremenek | 274b208 | 2008-11-12 21:37:15 +0000 | [diff] [blame] | 1 | //===--- PTHLexer.cpp - Lex from a token stream ---------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the PTHLexer interface. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Ted Kremenek | 0c6a77b | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 14 | #include "clang/Basic/TokenKinds.h" |
| 15 | #include "clang/Basic/FileManager.h" |
| 16 | #include "clang/Basic/IdentifierTable.h" |
Ted Kremenek | 274b208 | 2008-11-12 21:37:15 +0000 | [diff] [blame] | 17 | #include "clang/Lex/PTHLexer.h" |
| 18 | #include "clang/Lex/Preprocessor.h" |
Ted Kremenek | 0c6a77b | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 19 | #include "clang/Lex/PTHManager.h" |
| 20 | #include "clang/Lex/Token.h" |
| 21 | #include "clang/Lex/Preprocessor.h" |
| 22 | #include "llvm/Support/Compiler.h" |
| 23 | #include "llvm/Support/MemoryBuffer.h" |
| 24 | #include "llvm/ADT/StringMap.h" |
| 25 | #include "llvm/ADT/OwningPtr.h" |
Ted Kremenek | 0c6a77b | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 26 | |
Ted Kremenek | 274b208 | 2008-11-12 21:37:15 +0000 | [diff] [blame] | 27 | using namespace clang; |
| 28 | |
Ted Kremenek | 18d9afb | 2008-12-23 18:41:34 +0000 | [diff] [blame] | 29 | #define DISK_TOKEN_SIZE (1+1+3+4+2) |
Ted Kremenek | 268ee70 | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 30 | |
Ted Kremenek | 0c6a77b | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 31 | //===----------------------------------------------------------------------===// |
| 32 | // Utility methods for reading from the mmap'ed PTH file. |
| 33 | //===----------------------------------------------------------------------===// |
| 34 | |
| 35 | static inline uint8_t Read8(const char*& data) { |
| 36 | return (uint8_t) *(data++); |
| 37 | } |
| 38 | |
| 39 | static inline uint32_t Read32(const char*& data) { |
| 40 | uint32_t V = (uint32_t) Read8(data); |
| 41 | V |= (((uint32_t) Read8(data)) << 8); |
| 42 | V |= (((uint32_t) Read8(data)) << 16); |
| 43 | V |= (((uint32_t) Read8(data)) << 24); |
| 44 | return V; |
| 45 | } |
| 46 | |
Ted Kremenek | e5680f3 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 47 | //===----------------------------------------------------------------------===// |
| 48 | // PTHLexer methods. |
| 49 | //===----------------------------------------------------------------------===// |
| 50 | |
| 51 | PTHLexer::PTHLexer(Preprocessor& pp, SourceLocation fileloc, const char* D, |
| 52 | const char* ppcond, PTHManager& PM) |
| 53 | : PreprocessorLexer(&pp, fileloc), TokBuf(D), CurPtr(D), LastHashTokPtr(0), |
| 54 | PPCond(ppcond), CurPPCondPtr(ppcond), PTHMgr(PM) {} |
| 55 | |
| 56 | void PTHLexer::Lex(Token& Tok) { |
| 57 | LexNextToken: |
Ted Kremenek | e5680f3 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 58 | |
Ted Kremenek | 866bdf7 | 2008-12-23 02:30:15 +0000 | [diff] [blame] | 59 | //===--------------------------------------==// |
| 60 | // Read the raw token data. |
| 61 | //===--------------------------------------==// |
| 62 | |
| 63 | // Shadow CurPtr into an automatic variable. |
| 64 | const unsigned char *CurPtrShadow = (const unsigned char*) CurPtr; |
| 65 | |
| 66 | // Read in the data for the token. 14 bytes in total. |
| 67 | tok::TokenKind k = (tok::TokenKind) CurPtrShadow[0]; |
| 68 | Token::TokenFlags flags = (Token::TokenFlags) CurPtrShadow[1]; |
| 69 | |
Ted Kremenek | 6b1c970 | 2008-12-23 18:27:26 +0000 | [diff] [blame] | 70 | uint32_t perID = ((uint32_t) CurPtrShadow[2]) |
Ted Kremenek | 866bdf7 | 2008-12-23 02:30:15 +0000 | [diff] [blame] | 71 | | (((uint32_t) CurPtrShadow[3]) << 8) |
Ted Kremenek | 18d9afb | 2008-12-23 18:41:34 +0000 | [diff] [blame] | 72 | | (((uint32_t) CurPtrShadow[4]) << 16); |
Ted Kremenek | 866bdf7 | 2008-12-23 02:30:15 +0000 | [diff] [blame] | 73 | |
Ted Kremenek | 18d9afb | 2008-12-23 18:41:34 +0000 | [diff] [blame] | 74 | uint32_t FileOffset = ((uint32_t) CurPtrShadow[5]) |
| 75 | | (((uint32_t) CurPtrShadow[6]) << 8) |
| 76 | | (((uint32_t) CurPtrShadow[7]) << 16) |
| 77 | | (((uint32_t) CurPtrShadow[8]) << 24); |
Ted Kremenek | 866bdf7 | 2008-12-23 02:30:15 +0000 | [diff] [blame] | 78 | |
Ted Kremenek | 18d9afb | 2008-12-23 18:41:34 +0000 | [diff] [blame] | 79 | uint32_t Len = ((uint32_t) CurPtrShadow[9]) |
| 80 | | (((uint32_t) CurPtrShadow[10]) << 8); |
Ted Kremenek | 866bdf7 | 2008-12-23 02:30:15 +0000 | [diff] [blame] | 81 | |
| 82 | CurPtr = (const char*) (CurPtrShadow + DISK_TOKEN_SIZE); |
| 83 | |
| 84 | //===--------------------------------------==// |
| 85 | // Construct the token itself. |
| 86 | //===--------------------------------------==// |
| 87 | |
| 88 | Tok.startToken(); |
| 89 | Tok.setKind(k); |
Ted Kremenek | 59d08cb | 2008-12-23 19:24:24 +0000 | [diff] [blame] | 90 | Tok.setFlag(flags); |
| 91 | assert(!LexingRawMode); |
Ted Kremenek | 6b1c970 | 2008-12-23 18:27:26 +0000 | [diff] [blame] | 92 | Tok.setIdentifierInfo(perID ? PTHMgr.GetIdentifierInfo(perID-1) : 0); |
Ted Kremenek | 866bdf7 | 2008-12-23 02:30:15 +0000 | [diff] [blame] | 93 | Tok.setLocation(SourceLocation::getFileLoc(FileID, FileOffset)); |
| 94 | Tok.setLength(Len); |
| 95 | |
| 96 | //===--------------------------------------==// |
| 97 | // Process the token. |
| 98 | //===--------------------------------------==// |
Ted Kremenek | e5680f3 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 99 | |
Ted Kremenek | 59d08cb | 2008-12-23 19:24:24 +0000 | [diff] [blame] | 100 | if (k == tok::identifier) { |
| 101 | MIOpt.ReadToken(); |
| 102 | return PP->HandleIdentifier(Tok); |
| 103 | } |
| 104 | |
| 105 | if (k == tok::eof) { |
Ted Kremenek | e5680f3 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 106 | // Save the end-of-file token. |
| 107 | EofToken = Tok; |
| 108 | |
| 109 | Preprocessor *PPCache = PP; |
Ted Kremenek | 59d08cb | 2008-12-23 19:24:24 +0000 | [diff] [blame] | 110 | |
| 111 | assert(!ParsingPreprocessorDirective); |
| 112 | assert(!LexingRawMode); |
| 113 | |
| 114 | // FIXME: Issue diagnostics similar to Lexer. |
| 115 | if (PP->HandleEndOfFile(Tok, false)) |
Ted Kremenek | e5680f3 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 116 | return; |
Ted Kremenek | 59d08cb | 2008-12-23 19:24:24 +0000 | [diff] [blame] | 117 | |
Ted Kremenek | e5680f3 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 118 | assert(PPCache && "Raw buffer::LexEndOfFile should return a token"); |
| 119 | return PPCache->Lex(Tok); |
| 120 | } |
Ted Kremenek | e5680f3 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 121 | |
Ted Kremenek | 59d08cb | 2008-12-23 19:24:24 +0000 | [diff] [blame] | 122 | if (k == tok::hash && Tok.isAtStartOfLine()) { |
| 123 | LastHashTokPtr = CurPtr - DISK_TOKEN_SIZE; |
| 124 | assert(!LexingRawMode); |
| 125 | PP->HandleDirective(Tok); |
| 126 | |
| 127 | if (PP->isCurrentLexer(this)) |
| 128 | goto LexNextToken; |
| 129 | |
| 130 | return PP->Lex(Tok); |
| 131 | } |
| 132 | |
| 133 | if (k == tok::eom) { |
| 134 | assert(ParsingPreprocessorDirective); |
Ted Kremenek | e5680f3 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 135 | ParsingPreprocessorDirective = false; |
| 136 | return; |
| 137 | } |
Ted Kremenek | e5680f3 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 138 | |
Ted Kremenek | 59d08cb | 2008-12-23 19:24:24 +0000 | [diff] [blame] | 139 | MIOpt.ReadToken(); |
Ted Kremenek | e5680f3 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | // FIXME: We can just grab the last token instead of storing a copy |
| 143 | // into EofToken. |
Ted Kremenek | 59d08cb | 2008-12-23 19:24:24 +0000 | [diff] [blame] | 144 | void PTHLexer::getEOF(Token& Tok) { |
Ted Kremenek | e5680f3 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 145 | assert(!EofToken.is(tok::eof)); |
| 146 | Tok = EofToken; |
| 147 | } |
| 148 | |
| 149 | void PTHLexer::DiscardToEndOfLine() { |
| 150 | assert(ParsingPreprocessorDirective && ParsingFilename == false && |
| 151 | "Must be in a preprocessing directive!"); |
| 152 | |
| 153 | // We assume that if the preprocessor wishes to discard to the end of |
| 154 | // the line that it also means to end the current preprocessor directive. |
| 155 | ParsingPreprocessorDirective = false; |
| 156 | |
| 157 | // Skip tokens by only peeking at their token kind and the flags. |
| 158 | // We don't need to actually reconstruct full tokens from the token buffer. |
| 159 | // This saves some copies and it also reduces IdentifierInfo* lookup. |
| 160 | const char* p = CurPtr; |
| 161 | while (1) { |
| 162 | // Read the token kind. Are we at the end of the file? |
| 163 | tok::TokenKind x = (tok::TokenKind) (uint8_t) *p; |
| 164 | if (x == tok::eof) break; |
| 165 | |
| 166 | // Read the token flags. Are we at the start of the next line? |
| 167 | Token::TokenFlags y = (Token::TokenFlags) (uint8_t) p[1]; |
| 168 | if (y & Token::StartOfLine) break; |
| 169 | |
| 170 | // Skip to the next token. |
| 171 | p += DISK_TOKEN_SIZE; |
| 172 | } |
| 173 | |
| 174 | CurPtr = p; |
| 175 | } |
| 176 | |
Ted Kremenek | 268ee70 | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 177 | /// SkipBlock - Used by Preprocessor to skip the current conditional block. |
| 178 | bool PTHLexer::SkipBlock() { |
| 179 | assert(CurPPCondPtr && "No cached PP conditional information."); |
| 180 | assert(LastHashTokPtr && "No known '#' token."); |
| 181 | |
Ted Kremenek | 41a2660 | 2008-12-12 22:05:38 +0000 | [diff] [blame] | 182 | const char* HashEntryI = 0; |
Ted Kremenek | 268ee70 | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 183 | uint32_t Offset; |
| 184 | uint32_t TableIdx; |
| 185 | |
| 186 | do { |
Ted Kremenek | 41a2660 | 2008-12-12 22:05:38 +0000 | [diff] [blame] | 187 | // Read the token offset from the side-table. |
Ted Kremenek | 268ee70 | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 188 | Offset = Read32(CurPPCondPtr); |
Ted Kremenek | 41a2660 | 2008-12-12 22:05:38 +0000 | [diff] [blame] | 189 | |
| 190 | // Read the target table index from the side-table. |
Ted Kremenek | 268ee70 | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 191 | TableIdx = Read32(CurPPCondPtr); |
Ted Kremenek | 41a2660 | 2008-12-12 22:05:38 +0000 | [diff] [blame] | 192 | |
| 193 | // Compute the actual memory address of the '#' token data for this entry. |
| 194 | HashEntryI = TokBuf + Offset; |
| 195 | |
| 196 | // Optmization: "Sibling jumping". #if...#else...#endif blocks can |
| 197 | // contain nested blocks. In the side-table we can jump over these |
| 198 | // nested blocks instead of doing a linear search if the next "sibling" |
| 199 | // entry is not at a location greater than LastHashTokPtr. |
| 200 | if (HashEntryI < LastHashTokPtr && TableIdx) { |
| 201 | // In the side-table we are still at an entry for a '#' token that |
| 202 | // is earlier than the last one we saw. Check if the location we would |
| 203 | // stride gets us closer. |
| 204 | const char* NextPPCondPtr = PPCond + TableIdx*(sizeof(uint32_t)*2); |
| 205 | assert(NextPPCondPtr >= CurPPCondPtr); |
| 206 | // Read where we should jump to. |
| 207 | uint32_t TmpOffset = Read32(NextPPCondPtr); |
| 208 | const char* HashEntryJ = TokBuf + TmpOffset; |
| 209 | |
| 210 | if (HashEntryJ <= LastHashTokPtr) { |
| 211 | // Jump directly to the next entry in the side table. |
| 212 | HashEntryI = HashEntryJ; |
| 213 | Offset = TmpOffset; |
| 214 | TableIdx = Read32(NextPPCondPtr); |
| 215 | CurPPCondPtr = NextPPCondPtr; |
| 216 | } |
| 217 | } |
Ted Kremenek | 268ee70 | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 218 | } |
Ted Kremenek | 41a2660 | 2008-12-12 22:05:38 +0000 | [diff] [blame] | 219 | while (HashEntryI < LastHashTokPtr); |
| 220 | assert(HashEntryI == LastHashTokPtr && "No PP-cond entry found for '#'"); |
Ted Kremenek | 268ee70 | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 221 | assert(TableIdx && "No jumping from #endifs."); |
| 222 | |
| 223 | // Update our side-table iterator. |
| 224 | const char* NextPPCondPtr = PPCond + TableIdx*(sizeof(uint32_t)*2); |
| 225 | assert(NextPPCondPtr >= CurPPCondPtr); |
| 226 | CurPPCondPtr = NextPPCondPtr; |
| 227 | |
| 228 | // Read where we should jump to. |
Ted Kremenek | 41a2660 | 2008-12-12 22:05:38 +0000 | [diff] [blame] | 229 | HashEntryI = TokBuf + Read32(NextPPCondPtr); |
Ted Kremenek | 268ee70 | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 230 | uint32_t NextIdx = Read32(NextPPCondPtr); |
| 231 | |
| 232 | // By construction NextIdx will be zero if this is a #endif. This is useful |
| 233 | // to know to obviate lexing another token. |
| 234 | bool isEndif = NextIdx == 0; |
Ted Kremenek | 268ee70 | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 235 | |
| 236 | // This case can occur when we see something like this: |
| 237 | // |
| 238 | // #if ... |
| 239 | // /* a comment or nothing */ |
| 240 | // #elif |
| 241 | // |
| 242 | // If we are skipping the first #if block it will be the case that CurPtr |
| 243 | // already points 'elif'. Just return. |
| 244 | |
Ted Kremenek | 41a2660 | 2008-12-12 22:05:38 +0000 | [diff] [blame] | 245 | if (CurPtr > HashEntryI) { |
| 246 | assert(CurPtr == HashEntryI + DISK_TOKEN_SIZE); |
Ted Kremenek | 268ee70 | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 247 | // Did we reach a #endif? If so, go ahead and consume that token as well. |
| 248 | if (isEndif) |
Ted Kremenek | e5680f3 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 249 | CurPtr += DISK_TOKEN_SIZE*2; |
Ted Kremenek | 268ee70 | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 250 | else |
Ted Kremenek | 41a2660 | 2008-12-12 22:05:38 +0000 | [diff] [blame] | 251 | LastHashTokPtr = HashEntryI; |
Ted Kremenek | 268ee70 | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 252 | |
| 253 | return isEndif; |
| 254 | } |
| 255 | |
| 256 | // Otherwise, we need to advance. Update CurPtr to point to the '#' token. |
Ted Kremenek | 41a2660 | 2008-12-12 22:05:38 +0000 | [diff] [blame] | 257 | CurPtr = HashEntryI; |
Ted Kremenek | 268ee70 | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 258 | |
| 259 | // Update the location of the last observed '#'. This is useful if we |
| 260 | // are skipping multiple blocks. |
| 261 | LastHashTokPtr = CurPtr; |
Ted Kremenek | 268ee70 | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 262 | |
Ted Kremenek | e5680f3 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 263 | // Skip the '#' token. |
| 264 | assert(((tok::TokenKind) (unsigned char) *CurPtr) == tok::hash); |
| 265 | CurPtr += DISK_TOKEN_SIZE; |
| 266 | |
Ted Kremenek | 268ee70 | 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. |
Ted Kremenek | e5680f3 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 268 | if (isEndif) { CurPtr += DISK_TOKEN_SIZE*2; } |
Ted Kremenek | 268ee70 | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 269 | |
| 270 | return isEndif; |
| 271 | } |
| 272 | |
Ted Kremenek | 30a12ec | 2008-12-17 23:36:32 +0000 | [diff] [blame] | 273 | SourceLocation PTHLexer::getSourceLocation() { |
| 274 | // getLocation is not on the hot path. It is used to get the location of |
| 275 | // the next token when transitioning back to this lexer when done |
| 276 | // handling a #included file. Just read the necessary data from the token |
| 277 | // data buffer to construct the SourceLocation object. |
| 278 | // NOTE: This is a virtual function; hence it is defined out-of-line. |
Ted Kremenek | 59d08cb | 2008-12-23 19:24:24 +0000 | [diff] [blame] | 279 | const char* p = CurPtr + (1 + 1 + 3); |
Ted Kremenek | 30a12ec | 2008-12-17 23:36:32 +0000 | [diff] [blame] | 280 | uint32_t offset = |
| 281 | ((uint32_t) ((uint8_t) p[0])) |
| 282 | | (((uint32_t) ((uint8_t) p[1])) << 8) |
| 283 | | (((uint32_t) ((uint8_t) p[2])) << 16) |
| 284 | | (((uint32_t) ((uint8_t) p[3])) << 24); |
| 285 | return SourceLocation::getFileLoc(FileID, offset); |
| 286 | } |
| 287 | |
Ted Kremenek | 0c6a77b | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 288 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 0c6a77b | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 289 | // Internal Data Structures for PTH file lookup and resolving identifiers. |
| 290 | //===----------------------------------------------------------------------===// |
| 291 | |
Ted Kremenek | 0c6a77b | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 292 | |
| 293 | /// PTHFileLookup - This internal data structure is used by the PTHManager |
| 294 | /// to map from FileEntry objects managed by FileManager to offsets within |
| 295 | /// the PTH file. |
| 296 | namespace { |
| 297 | class VISIBILITY_HIDDEN PTHFileLookup { |
| 298 | public: |
| 299 | class Val { |
Ted Kremenek | fb645b6 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 300 | uint32_t TokenOff; |
| 301 | uint32_t PPCondOff; |
Ted Kremenek | 0c6a77b | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 302 | |
| 303 | public: |
Ted Kremenek | fb645b6 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 304 | Val() : TokenOff(~0) {} |
| 305 | Val(uint32_t toff, uint32_t poff) : TokenOff(toff), PPCondOff(poff) {} |
Ted Kremenek | 0c6a77b | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 306 | |
Ted Kremenek | fb645b6 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 307 | uint32_t getTokenOffset() const { |
| 308 | assert(TokenOff != ~((uint32_t)0) && "PTHFileLookup entry initialized."); |
| 309 | return TokenOff; |
Ted Kremenek | 0c6a77b | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 310 | } |
| 311 | |
Ted Kremenek | fb645b6 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 312 | uint32_t gettPPCondOffset() const { |
| 313 | assert(TokenOff != ~((uint32_t)0) && "PTHFileLookup entry initialized."); |
| 314 | return PPCondOff; |
| 315 | } |
| 316 | |
| 317 | bool isValid() const { return TokenOff != ~((uint32_t)0); } |
Ted Kremenek | 0c6a77b | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 318 | }; |
| 319 | |
| 320 | private: |
| 321 | llvm::StringMap<Val> FileMap; |
| 322 | |
| 323 | public: |
| 324 | PTHFileLookup() {}; |
| 325 | |
| 326 | Val Lookup(const FileEntry* FE) { |
| 327 | const char* s = FE->getName(); |
| 328 | unsigned size = strlen(s); |
| 329 | return FileMap.GetOrCreateValue(s, s+size).getValue(); |
| 330 | } |
| 331 | |
| 332 | void ReadTable(const char* D) { |
| 333 | uint32_t N = Read32(D); // Read the length of the table. |
| 334 | |
| 335 | for ( ; N > 0; --N) { // The rest of the data is the table itself. |
| 336 | uint32_t len = Read32(D); |
| 337 | const char* s = D; |
| 338 | D += len; |
Ted Kremenek | fb645b6 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 339 | uint32_t TokenOff = Read32(D); |
| 340 | FileMap.GetOrCreateValue(s, s+len).getValue() = Val(TokenOff, Read32(D)); |
Ted Kremenek | 0c6a77b | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 341 | } |
| 342 | } |
| 343 | }; |
| 344 | } // end anonymous namespace |
| 345 | |
| 346 | //===----------------------------------------------------------------------===// |
| 347 | // PTHManager methods. |
| 348 | //===----------------------------------------------------------------------===// |
| 349 | |
| 350 | PTHManager::PTHManager(const llvm::MemoryBuffer* buf, void* fileLookup, |
Ted Kremenek | cf58e62 | 2008-12-10 19:40:23 +0000 | [diff] [blame] | 351 | const char* idDataTable, IdentifierInfo** perIDCache, |
Ted Kremenek | 6183e48 | 2008-12-03 01:16:39 +0000 | [diff] [blame] | 352 | Preprocessor& pp) |
| 353 | : Buf(buf), PerIDCache(perIDCache), FileLookup(fileLookup), |
| 354 | IdDataTable(idDataTable), ITable(pp.getIdentifierTable()), PP(pp) {} |
Ted Kremenek | 0c6a77b | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 355 | |
| 356 | PTHManager::~PTHManager() { |
| 357 | delete Buf; |
| 358 | delete (PTHFileLookup*) FileLookup; |
Ted Kremenek | 0e50b6e | 2008-12-04 22:47:11 +0000 | [diff] [blame] | 359 | free(PerIDCache); |
Ted Kremenek | 0c6a77b | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | PTHManager* PTHManager::Create(const std::string& file, Preprocessor& PP) { |
| 363 | |
| 364 | // Memory map the PTH file. |
| 365 | llvm::OwningPtr<llvm::MemoryBuffer> |
| 366 | File(llvm::MemoryBuffer::getFile(file.c_str())); |
| 367 | |
| 368 | if (!File) |
| 369 | return 0; |
| 370 | |
| 371 | // Get the buffer ranges and check if there are at least three 32-bit |
| 372 | // words at the end of the file. |
| 373 | const char* BufBeg = File->getBufferStart(); |
| 374 | const char* BufEnd = File->getBufferEnd(); |
| 375 | |
| 376 | if(!(BufEnd > BufBeg + sizeof(uint32_t)*3)) { |
| 377 | assert(false && "Invalid PTH file."); |
| 378 | return 0; // FIXME: Proper error diagnostic? |
| 379 | } |
| 380 | |
| 381 | // Compute the address of the index table at the end of the PTH file. |
| 382 | // This table contains the offset of the file lookup table, the |
| 383 | // persistent ID -> identifer data table. |
| 384 | const char* EndTable = BufEnd - sizeof(uint32_t)*3; |
| 385 | |
| 386 | // Construct the file lookup table. This will be used for mapping from |
| 387 | // FileEntry*'s to cached tokens. |
| 388 | const char* FileTableOffset = EndTable + sizeof(uint32_t)*2; |
| 389 | const char* FileTable = BufBeg + Read32(FileTableOffset); |
| 390 | |
| 391 | if (!(FileTable > BufBeg && FileTable < BufEnd)) { |
| 392 | assert(false && "Invalid PTH file."); |
| 393 | return 0; // FIXME: Proper error diagnostic? |
| 394 | } |
| 395 | |
| 396 | llvm::OwningPtr<PTHFileLookup> FL(new PTHFileLookup()); |
| 397 | FL->ReadTable(FileTable); |
| 398 | |
| 399 | // Get the location of the table mapping from persistent ids to the |
| 400 | // data needed to reconstruct identifiers. |
| 401 | const char* IDTableOffset = EndTable + sizeof(uint32_t)*1; |
| 402 | const char* IData = BufBeg + Read32(IDTableOffset); |
| 403 | if (!(IData > BufBeg && IData < BufEnd)) { |
| 404 | assert(false && "Invalid PTH file."); |
| 405 | return 0; // FIXME: Proper error diagnostic? |
| 406 | } |
| 407 | |
Ted Kremenek | 6183e48 | 2008-12-03 01:16:39 +0000 | [diff] [blame] | 408 | // Get the number of IdentifierInfos and pre-allocate the identifier cache. |
| 409 | uint32_t NumIds = Read32(IData); |
| 410 | |
| 411 | // Pre-allocate the peristent ID -> IdentifierInfo* cache. We use calloc() |
| 412 | // so that we in the best case only zero out memory once when the OS returns |
| 413 | // us new pages. |
| 414 | IdentifierInfo** PerIDCache = |
| 415 | (IdentifierInfo**) calloc(NumIds, sizeof(*PerIDCache)); |
| 416 | |
| 417 | if (!PerIDCache) { |
| 418 | assert(false && "Could not allocate Persistent ID cache."); |
| 419 | return 0; |
| 420 | } |
| 421 | |
| 422 | // Create the new lexer. |
| 423 | return new PTHManager(File.take(), FL.take(), IData, PerIDCache, PP); |
Ted Kremenek | 0c6a77b | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 424 | } |
| 425 | |
Ted Kremenek | 866bdf7 | 2008-12-23 02:30:15 +0000 | [diff] [blame] | 426 | IdentifierInfo* PTHManager::GetIdentifierInfo(unsigned persistentID) { |
| 427 | |
Ted Kremenek | 0c6a77b | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 428 | // Check if the IdentifierInfo has already been resolved. |
Ted Kremenek | cf58e62 | 2008-12-10 19:40:23 +0000 | [diff] [blame] | 429 | IdentifierInfo*& II = PerIDCache[persistentID]; |
Ted Kremenek | 0c6a77b | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 430 | if (II) return II; |
| 431 | |
| 432 | // Look in the PTH file for the string data for the IdentifierInfo object. |
| 433 | const char* TableEntry = IdDataTable + sizeof(uint32_t) * persistentID; |
| 434 | const char* IDData = Buf->getBufferStart() + Read32(TableEntry); |
| 435 | assert(IDData < Buf->getBufferEnd()); |
| 436 | |
| 437 | // Read the length of the string. |
| 438 | uint32_t len = Read32(IDData); |
| 439 | |
| 440 | // Get the IdentifierInfo* with the specified string. |
| 441 | II = &ITable.get(IDData, IDData+len); |
| 442 | return II; |
| 443 | } |
| 444 | |
| 445 | PTHLexer* PTHManager::CreateLexer(unsigned FileID, const FileEntry* FE) { |
| 446 | |
| 447 | if (!FE) |
| 448 | return 0; |
| 449 | |
| 450 | // Lookup the FileEntry object in our file lookup data structure. It will |
| 451 | // return a variant that indicates whether or not there is an offset within |
| 452 | // the PTH file that contains cached tokens. |
Ted Kremenek | fb645b6 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 453 | PTHFileLookup::Val FileData = ((PTHFileLookup*) FileLookup)->Lookup(FE); |
Ted Kremenek | 0c6a77b | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 454 | |
Ted Kremenek | fb645b6 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 455 | if (!FileData.isValid()) // No tokens available. |
Ted Kremenek | 0c6a77b | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 456 | return 0; |
| 457 | |
| 458 | // Compute the offset of the token data within the buffer. |
Ted Kremenek | fb645b6 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 459 | const char* data = Buf->getBufferStart() + FileData.getTokenOffset(); |
Ted Kremenek | 268ee70 | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 460 | |
| 461 | // Get the location of pp-conditional table. |
| 462 | const char* ppcond = Buf->getBufferStart() + FileData.gettPPCondOffset(); |
| 463 | uint32_t len = Read32(ppcond); |
| 464 | if (len == 0) ppcond = 0; |
| 465 | |
Ted Kremenek | 0c6a77b | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 466 | assert(data < Buf->getBufferEnd()); |
Ted Kremenek | 268ee70 | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 467 | return new PTHLexer(PP, SourceLocation::getFileLoc(FileID, 0), data, ppcond, |
| 468 | *this); |
Ted Kremenek | 0c6a77b | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 469 | } |