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" |
Douglas Gregor | 9378ba4 | 2009-04-20 07:08:21 +0000 | [diff] [blame] | 17 | #include "clang/Basic/OnDiskHashTable.h" |
Ted Kremenek | 274b208 | 2008-11-12 21:37:15 +0000 | [diff] [blame] | 18 | #include "clang/Lex/PTHLexer.h" |
| 19 | #include "clang/Lex/Preprocessor.h" |
Ted Kremenek | 0c6a77b | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 20 | #include "clang/Lex/PTHManager.h" |
| 21 | #include "clang/Lex/Token.h" |
| 22 | #include "clang/Lex/Preprocessor.h" |
Ted Kremenek | 0c6a77b | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/StringMap.h" |
| 24 | #include "llvm/ADT/OwningPtr.h" |
Chris Lattner | 6f78c3b | 2009-01-22 23:50:07 +0000 | [diff] [blame] | 25 | #include "llvm/Support/MemoryBuffer.h" |
Ted Kremenek | 337edcd | 2009-02-12 03:26:59 +0000 | [diff] [blame] | 26 | #include <sys/stat.h> |
Ted Kremenek | 274b208 | 2008-11-12 21:37:15 +0000 | [diff] [blame] | 27 | using namespace clang; |
Douglas Gregor | 9378ba4 | 2009-04-20 07:08:21 +0000 | [diff] [blame] | 28 | using namespace clang::io; |
Ted Kremenek | 274b208 | 2008-11-12 21:37:15 +0000 | [diff] [blame] | 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 | //===----------------------------------------------------------------------===// |
Ted Kremenek | e5680f3 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 33 | // PTHLexer methods. |
| 34 | //===----------------------------------------------------------------------===// |
| 35 | |
Chris Lattner | da9d61c | 2009-01-18 01:57:14 +0000 | [diff] [blame] | 36 | PTHLexer::PTHLexer(Preprocessor &PP, FileID FID, const unsigned char *D, |
Ted Kremenek | 277faca | 2009-01-27 00:01:05 +0000 | [diff] [blame] | 37 | const unsigned char *ppcond, PTHManager &PM) |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 38 | : PreprocessorLexer(&PP, FID), TokBuf(D), CurPtr(D), LastHashTokPtr(0), |
Ted Kremenek | 277faca | 2009-01-27 00:01:05 +0000 | [diff] [blame] | 39 | PPCond(ppcond), CurPPCondPtr(ppcond), PTHMgr(PM) { |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 40 | |
| 41 | FileStartLoc = PP.getSourceManager().getLocForStartOfFile(FID); |
Ted Kremenek | 5f07426 | 2009-01-09 22:05:30 +0000 | [diff] [blame] | 42 | } |
Ted Kremenek | e5680f3 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 43 | |
| 44 | void PTHLexer::Lex(Token& Tok) { |
| 45 | LexNextToken: |
Ted Kremenek | e5680f3 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 46 | |
Ted Kremenek | 866bdf7 | 2008-12-23 02:30:15 +0000 | [diff] [blame] | 47 | //===--------------------------------------==// |
| 48 | // Read the raw token data. |
| 49 | //===--------------------------------------==// |
| 50 | |
| 51 | // Shadow CurPtr into an automatic variable. |
Chris Lattner | aff6ef8 | 2009-01-21 07:21:56 +0000 | [diff] [blame] | 52 | const unsigned char *CurPtrShadow = CurPtr; |
Ted Kremenek | 866bdf7 | 2008-12-23 02:30:15 +0000 | [diff] [blame] | 53 | |
Chris Lattner | 1b5285e | 2009-01-18 02:10:31 +0000 | [diff] [blame] | 54 | // Read in the data for the token. |
Chris Lattner | 5ff4317 | 2009-01-22 19:48:26 +0000 | [diff] [blame] | 55 | unsigned Word0 = ReadLE32(CurPtrShadow); |
| 56 | uint32_t IdentifierID = ReadLE32(CurPtrShadow); |
| 57 | uint32_t FileOffset = ReadLE32(CurPtrShadow); |
Ted Kremenek | 7b78b7c | 2009-01-19 23:13:15 +0000 | [diff] [blame] | 58 | |
| 59 | tok::TokenKind TKind = (tok::TokenKind) (Word0 & 0xFF); |
| 60 | Token::TokenFlags TFlags = (Token::TokenFlags) ((Word0 >> 8) & 0xFF); |
Chris Lattner | aff6ef8 | 2009-01-21 07:21:56 +0000 | [diff] [blame] | 61 | uint32_t Len = Word0 >> 16; |
Ted Kremenek | 7b78b7c | 2009-01-19 23:13:15 +0000 | [diff] [blame] | 62 | |
Chris Lattner | aff6ef8 | 2009-01-21 07:21:56 +0000 | [diff] [blame] | 63 | CurPtr = CurPtrShadow; |
Ted Kremenek | 866bdf7 | 2008-12-23 02:30:15 +0000 | [diff] [blame] | 64 | |
| 65 | //===--------------------------------------==// |
| 66 | // Construct the token itself. |
| 67 | //===--------------------------------------==// |
| 68 | |
| 69 | Tok.startToken(); |
Chris Lattner | 898a0bb | 2009-01-18 02:34:01 +0000 | [diff] [blame] | 70 | Tok.setKind(TKind); |
| 71 | Tok.setFlag(TFlags); |
Ted Kremenek | 59d08cb | 2008-12-23 19:24:24 +0000 | [diff] [blame] | 72 | assert(!LexingRawMode); |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 73 | Tok.setLocation(FileStartLoc.getFileLocWithOffset(FileOffset)); |
Ted Kremenek | 866bdf7 | 2008-12-23 02:30:15 +0000 | [diff] [blame] | 74 | Tok.setLength(Len); |
| 75 | |
Chris Lattner | d0a6969 | 2009-01-21 07:50:06 +0000 | [diff] [blame] | 76 | // Handle identifiers. |
Ted Kremenek | 277faca | 2009-01-27 00:01:05 +0000 | [diff] [blame] | 77 | if (Tok.isLiteral()) { |
| 78 | Tok.setLiteralData((const char*) (PTHMgr.SpellingBase + IdentifierID)); |
| 79 | } |
| 80 | else if (IdentifierID) { |
Chris Lattner | d0a6969 | 2009-01-21 07:50:06 +0000 | [diff] [blame] | 81 | MIOpt.ReadToken(); |
| 82 | IdentifierInfo *II = PTHMgr.GetIdentifierInfo(IdentifierID-1); |
Chris Lattner | 863c486 | 2009-01-23 18:35:48 +0000 | [diff] [blame] | 83 | |
Chris Lattner | d0a6969 | 2009-01-21 07:50:06 +0000 | [diff] [blame] | 84 | Tok.setIdentifierInfo(II); |
Chris Lattner | 863c486 | 2009-01-23 18:35:48 +0000 | [diff] [blame] | 85 | |
| 86 | // Change the kind of this identifier to the appropriate token kind, e.g. |
| 87 | // turning "for" into a keyword. |
| 88 | Tok.setKind(II->getTokenID()); |
| 89 | |
Chris Lattner | d0a6969 | 2009-01-21 07:50:06 +0000 | [diff] [blame] | 90 | if (II->isHandleIdentifierCase()) |
| 91 | PP->HandleIdentifier(Tok); |
| 92 | return; |
| 93 | } |
| 94 | |
Ted Kremenek | 866bdf7 | 2008-12-23 02:30:15 +0000 | [diff] [blame] | 95 | //===--------------------------------------==// |
| 96 | // Process the token. |
| 97 | //===--------------------------------------==// |
Ted Kremenek | 5f07426 | 2009-01-09 22:05:30 +0000 | [diff] [blame] | 98 | #if 0 |
| 99 | SourceManager& SM = PP->getSourceManager(); |
| 100 | llvm::cerr << SM.getFileEntryForID(FileID)->getName() |
| 101 | << ':' << SM.getLogicalLineNumber(Tok.getLocation()) |
| 102 | << ':' << SM.getLogicalColumnNumber(Tok.getLocation()) |
| 103 | << '\n'; |
| 104 | #endif |
Ted Kremenek | e5680f3 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 105 | |
Chris Lattner | 898a0bb | 2009-01-18 02:34:01 +0000 | [diff] [blame] | 106 | if (TKind == tok::eof) { |
Ted Kremenek | e5680f3 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 107 | // Save the end-of-file token. |
| 108 | EofToken = Tok; |
| 109 | |
| 110 | Preprocessor *PPCache = PP; |
Ted Kremenek | 59d08cb | 2008-12-23 19:24:24 +0000 | [diff] [blame] | 111 | |
| 112 | assert(!ParsingPreprocessorDirective); |
| 113 | assert(!LexingRawMode); |
| 114 | |
| 115 | // FIXME: Issue diagnostics similar to Lexer. |
| 116 | if (PP->HandleEndOfFile(Tok, false)) |
Ted Kremenek | e5680f3 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 117 | return; |
Ted Kremenek | 59d08cb | 2008-12-23 19:24:24 +0000 | [diff] [blame] | 118 | |
Ted Kremenek | e5680f3 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 119 | assert(PPCache && "Raw buffer::LexEndOfFile should return a token"); |
| 120 | return PPCache->Lex(Tok); |
| 121 | } |
Ted Kremenek | e5680f3 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 122 | |
Chris Lattner | 898a0bb | 2009-01-18 02:34:01 +0000 | [diff] [blame] | 123 | if (TKind == tok::hash && Tok.isAtStartOfLine()) { |
Ted Kremenek | 59d08cb | 2008-12-23 19:24:24 +0000 | [diff] [blame] | 124 | LastHashTokPtr = CurPtr - DISK_TOKEN_SIZE; |
| 125 | assert(!LexingRawMode); |
| 126 | PP->HandleDirective(Tok); |
| 127 | |
| 128 | if (PP->isCurrentLexer(this)) |
| 129 | goto LexNextToken; |
| 130 | |
| 131 | return PP->Lex(Tok); |
| 132 | } |
| 133 | |
Chris Lattner | 898a0bb | 2009-01-18 02:34:01 +0000 | [diff] [blame] | 134 | if (TKind == tok::eom) { |
Ted Kremenek | 59d08cb | 2008-12-23 19:24:24 +0000 | [diff] [blame] | 135 | assert(ParsingPreprocessorDirective); |
Ted Kremenek | e5680f3 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 136 | ParsingPreprocessorDirective = false; |
| 137 | return; |
| 138 | } |
Ted Kremenek | e5680f3 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 139 | |
Ted Kremenek | 59d08cb | 2008-12-23 19:24:24 +0000 | [diff] [blame] | 140 | MIOpt.ReadToken(); |
Ted Kremenek | e5680f3 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | // FIXME: We can just grab the last token instead of storing a copy |
| 144 | // into EofToken. |
Ted Kremenek | 59d08cb | 2008-12-23 19:24:24 +0000 | [diff] [blame] | 145 | void PTHLexer::getEOF(Token& Tok) { |
Ted Kremenek | defb709 | 2009-01-09 00:36:11 +0000 | [diff] [blame] | 146 | assert(EofToken.is(tok::eof)); |
Ted Kremenek | e5680f3 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 147 | Tok = EofToken; |
| 148 | } |
| 149 | |
| 150 | void PTHLexer::DiscardToEndOfLine() { |
| 151 | assert(ParsingPreprocessorDirective && ParsingFilename == false && |
| 152 | "Must be in a preprocessing directive!"); |
| 153 | |
| 154 | // We assume that if the preprocessor wishes to discard to the end of |
| 155 | // the line that it also means to end the current preprocessor directive. |
| 156 | ParsingPreprocessorDirective = false; |
| 157 | |
| 158 | // Skip tokens by only peeking at their token kind and the flags. |
| 159 | // We don't need to actually reconstruct full tokens from the token buffer. |
| 160 | // This saves some copies and it also reduces IdentifierInfo* lookup. |
Chris Lattner | da9d61c | 2009-01-18 01:57:14 +0000 | [diff] [blame] | 161 | const unsigned char* p = CurPtr; |
Ted Kremenek | e5680f3 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 162 | while (1) { |
| 163 | // Read the token kind. Are we at the end of the file? |
| 164 | tok::TokenKind x = (tok::TokenKind) (uint8_t) *p; |
| 165 | if (x == tok::eof) break; |
| 166 | |
| 167 | // Read the token flags. Are we at the start of the next line? |
| 168 | Token::TokenFlags y = (Token::TokenFlags) (uint8_t) p[1]; |
| 169 | if (y & Token::StartOfLine) break; |
| 170 | |
| 171 | // Skip to the next token. |
| 172 | p += DISK_TOKEN_SIZE; |
| 173 | } |
| 174 | |
| 175 | CurPtr = p; |
| 176 | } |
| 177 | |
Ted Kremenek | 268ee70 | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 178 | /// SkipBlock - Used by Preprocessor to skip the current conditional block. |
| 179 | bool PTHLexer::SkipBlock() { |
| 180 | assert(CurPPCondPtr && "No cached PP conditional information."); |
| 181 | assert(LastHashTokPtr && "No known '#' token."); |
| 182 | |
Chris Lattner | da9d61c | 2009-01-18 01:57:14 +0000 | [diff] [blame] | 183 | const unsigned char* HashEntryI = 0; |
Ted Kremenek | 268ee70 | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 184 | uint32_t Offset; |
| 185 | uint32_t TableIdx; |
| 186 | |
| 187 | do { |
Ted Kremenek | 41a2660 | 2008-12-12 22:05:38 +0000 | [diff] [blame] | 188 | // Read the token offset from the side-table. |
Chris Lattner | 5ff4317 | 2009-01-22 19:48:26 +0000 | [diff] [blame] | 189 | Offset = ReadLE32(CurPPCondPtr); |
Ted Kremenek | 41a2660 | 2008-12-12 22:05:38 +0000 | [diff] [blame] | 190 | |
| 191 | // Read the target table index from the side-table. |
Chris Lattner | 5ff4317 | 2009-01-22 19:48:26 +0000 | [diff] [blame] | 192 | TableIdx = ReadLE32(CurPPCondPtr); |
Ted Kremenek | 41a2660 | 2008-12-12 22:05:38 +0000 | [diff] [blame] | 193 | |
| 194 | // Compute the actual memory address of the '#' token data for this entry. |
| 195 | HashEntryI = TokBuf + Offset; |
| 196 | |
| 197 | // Optmization: "Sibling jumping". #if...#else...#endif blocks can |
| 198 | // contain nested blocks. In the side-table we can jump over these |
| 199 | // nested blocks instead of doing a linear search if the next "sibling" |
| 200 | // entry is not at a location greater than LastHashTokPtr. |
| 201 | if (HashEntryI < LastHashTokPtr && TableIdx) { |
| 202 | // In the side-table we are still at an entry for a '#' token that |
| 203 | // is earlier than the last one we saw. Check if the location we would |
| 204 | // stride gets us closer. |
Chris Lattner | da9d61c | 2009-01-18 01:57:14 +0000 | [diff] [blame] | 205 | const unsigned char* NextPPCondPtr = |
| 206 | PPCond + TableIdx*(sizeof(uint32_t)*2); |
Ted Kremenek | 41a2660 | 2008-12-12 22:05:38 +0000 | [diff] [blame] | 207 | assert(NextPPCondPtr >= CurPPCondPtr); |
| 208 | // Read where we should jump to. |
Chris Lattner | 5ff4317 | 2009-01-22 19:48:26 +0000 | [diff] [blame] | 209 | uint32_t TmpOffset = ReadLE32(NextPPCondPtr); |
Chris Lattner | da9d61c | 2009-01-18 01:57:14 +0000 | [diff] [blame] | 210 | const unsigned char* HashEntryJ = TokBuf + TmpOffset; |
Ted Kremenek | 41a2660 | 2008-12-12 22:05:38 +0000 | [diff] [blame] | 211 | |
| 212 | if (HashEntryJ <= LastHashTokPtr) { |
| 213 | // Jump directly to the next entry in the side table. |
| 214 | HashEntryI = HashEntryJ; |
| 215 | Offset = TmpOffset; |
Chris Lattner | 5ff4317 | 2009-01-22 19:48:26 +0000 | [diff] [blame] | 216 | TableIdx = ReadLE32(NextPPCondPtr); |
Ted Kremenek | 41a2660 | 2008-12-12 22:05:38 +0000 | [diff] [blame] | 217 | CurPPCondPtr = NextPPCondPtr; |
| 218 | } |
| 219 | } |
Ted Kremenek | 268ee70 | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 220 | } |
Ted Kremenek | 41a2660 | 2008-12-12 22:05:38 +0000 | [diff] [blame] | 221 | while (HashEntryI < LastHashTokPtr); |
| 222 | assert(HashEntryI == LastHashTokPtr && "No PP-cond entry found for '#'"); |
Ted Kremenek | 268ee70 | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 223 | assert(TableIdx && "No jumping from #endifs."); |
| 224 | |
| 225 | // Update our side-table iterator. |
Chris Lattner | da9d61c | 2009-01-18 01:57:14 +0000 | [diff] [blame] | 226 | const unsigned char* NextPPCondPtr = PPCond + TableIdx*(sizeof(uint32_t)*2); |
Ted Kremenek | 268ee70 | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 227 | assert(NextPPCondPtr >= CurPPCondPtr); |
| 228 | CurPPCondPtr = NextPPCondPtr; |
| 229 | |
| 230 | // Read where we should jump to. |
Chris Lattner | 5ff4317 | 2009-01-22 19:48:26 +0000 | [diff] [blame] | 231 | HashEntryI = TokBuf + ReadLE32(NextPPCondPtr); |
| 232 | uint32_t NextIdx = ReadLE32(NextPPCondPtr); |
Ted Kremenek | 268ee70 | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 233 | |
| 234 | // By construction NextIdx will be zero if this is a #endif. This is useful |
| 235 | // to know to obviate lexing another token. |
| 236 | bool isEndif = NextIdx == 0; |
Ted Kremenek | 268ee70 | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 237 | |
| 238 | // This case can occur when we see something like this: |
| 239 | // |
| 240 | // #if ... |
| 241 | // /* a comment or nothing */ |
| 242 | // #elif |
| 243 | // |
| 244 | // If we are skipping the first #if block it will be the case that CurPtr |
| 245 | // already points 'elif'. Just return. |
| 246 | |
Ted Kremenek | 41a2660 | 2008-12-12 22:05:38 +0000 | [diff] [blame] | 247 | if (CurPtr > HashEntryI) { |
| 248 | assert(CurPtr == HashEntryI + DISK_TOKEN_SIZE); |
Ted Kremenek | 268ee70 | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 249 | // Did we reach a #endif? If so, go ahead and consume that token as well. |
| 250 | if (isEndif) |
Ted Kremenek | e5680f3 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 251 | CurPtr += DISK_TOKEN_SIZE*2; |
Ted Kremenek | 268ee70 | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 252 | else |
Ted Kremenek | 41a2660 | 2008-12-12 22:05:38 +0000 | [diff] [blame] | 253 | LastHashTokPtr = HashEntryI; |
Ted Kremenek | 268ee70 | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 254 | |
| 255 | return isEndif; |
| 256 | } |
| 257 | |
| 258 | // Otherwise, we need to advance. Update CurPtr to point to the '#' token. |
Ted Kremenek | 41a2660 | 2008-12-12 22:05:38 +0000 | [diff] [blame] | 259 | CurPtr = HashEntryI; |
Ted Kremenek | 268ee70 | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 260 | |
| 261 | // Update the location of the last observed '#'. This is useful if we |
| 262 | // are skipping multiple blocks. |
| 263 | LastHashTokPtr = CurPtr; |
Ted Kremenek | 268ee70 | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 264 | |
Ted Kremenek | e5680f3 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 265 | // Skip the '#' token. |
Chris Lattner | da9d61c | 2009-01-18 01:57:14 +0000 | [diff] [blame] | 266 | assert(((tok::TokenKind)*CurPtr) == tok::hash); |
Ted Kremenek | e5680f3 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 267 | CurPtr += DISK_TOKEN_SIZE; |
| 268 | |
Ted Kremenek | 268ee70 | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 269 | // 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] | 270 | if (isEndif) { CurPtr += DISK_TOKEN_SIZE*2; } |
Ted Kremenek | 268ee70 | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 271 | |
| 272 | return isEndif; |
| 273 | } |
| 274 | |
Ted Kremenek | 30a12ec | 2008-12-17 23:36:32 +0000 | [diff] [blame] | 275 | SourceLocation PTHLexer::getSourceLocation() { |
Chris Lattner | 1b5285e | 2009-01-18 02:10:31 +0000 | [diff] [blame] | 276 | // getSourceLocation is not on the hot path. It is used to get the location |
| 277 | // of the next token when transitioning back to this lexer when done |
Ted Kremenek | 30a12ec | 2008-12-17 23:36:32 +0000 | [diff] [blame] | 278 | // handling a #included file. Just read the necessary data from the token |
| 279 | // data buffer to construct the SourceLocation object. |
| 280 | // 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] | 281 | const unsigned char *OffsetPtr = CurPtr + (DISK_TOKEN_SIZE - 4); |
Chris Lattner | 5ff4317 | 2009-01-22 19:48:26 +0000 | [diff] [blame] | 282 | uint32_t Offset = ReadLE32(OffsetPtr); |
Chris Lattner | 1b5285e | 2009-01-18 02:10:31 +0000 | [diff] [blame] | 283 | return FileStartLoc.getFileLocWithOffset(Offset); |
Ted Kremenek | 30a12ec | 2008-12-17 23:36:32 +0000 | [diff] [blame] | 284 | } |
| 285 | |
Ted Kremenek | 5f07426 | 2009-01-09 22:05:30 +0000 | [diff] [blame] | 286 | //===----------------------------------------------------------------------===// |
Ted Kremenek | d8c0292 | 2009-02-10 22:16:22 +0000 | [diff] [blame] | 287 | // PTH file lookup: map from strings to file data. |
| 288 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 0c6a77b | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 289 | |
| 290 | /// PTHFileLookup - This internal data structure is used by the PTHManager |
| 291 | /// to map from FileEntry objects managed by FileManager to offsets within |
| 292 | /// the PTH file. |
| 293 | namespace { |
Ted Kremenek | d8c0292 | 2009-02-10 22:16:22 +0000 | [diff] [blame] | 294 | class VISIBILITY_HIDDEN PTHFileData { |
| 295 | const uint32_t TokenOff; |
| 296 | const uint32_t PPCondOff; |
Ted Kremenek | 0c6a77b | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 297 | public: |
Ted Kremenek | d8c0292 | 2009-02-10 22:16:22 +0000 | [diff] [blame] | 298 | PTHFileData(uint32_t tokenOff, uint32_t ppCondOff) |
| 299 | : TokenOff(tokenOff), PPCondOff(ppCondOff) {} |
Ted Kremenek | 0c6a77b | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 300 | |
Ted Kremenek | d8c0292 | 2009-02-10 22:16:22 +0000 | [diff] [blame] | 301 | uint32_t getTokenOffset() const { return TokenOff; } |
| 302 | uint32_t getPPCondOffset() const { return PPCondOff; } |
| 303 | }; |
| 304 | |
Ted Kremenek | 337edcd | 2009-02-12 03:26:59 +0000 | [diff] [blame] | 305 | |
| 306 | class VISIBILITY_HIDDEN PTHFileLookupCommonTrait { |
Ted Kremenek | d8c0292 | 2009-02-10 22:16:22 +0000 | [diff] [blame] | 307 | public: |
Ted Kremenek | a4b44dd | 2009-02-13 19:13:46 +0000 | [diff] [blame] | 308 | typedef std::pair<unsigned char, const char*> internal_key_type; |
| 309 | |
| 310 | static unsigned ComputeHash(internal_key_type x) { |
| 311 | return BernsteinHash(x.second); |
Ted Kremenek | d8c0292 | 2009-02-10 22:16:22 +0000 | [diff] [blame] | 312 | } |
Ted Kremenek | cdd8f21 | 2009-01-21 07:34:28 +0000 | [diff] [blame] | 313 | |
Ted Kremenek | d8c0292 | 2009-02-10 22:16:22 +0000 | [diff] [blame] | 314 | static std::pair<unsigned, unsigned> |
| 315 | ReadKeyDataLength(const unsigned char*& d) { |
Ted Kremenek | a4b44dd | 2009-02-13 19:13:46 +0000 | [diff] [blame] | 316 | unsigned keyLen = (unsigned) ReadUnalignedLE16(d); |
| 317 | unsigned dataLen = (unsigned) *(d++); |
| 318 | return std::make_pair(keyLen, dataLen); |
Ted Kremenek | 0c6a77b | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 319 | } |
| 320 | |
Ted Kremenek | a4b44dd | 2009-02-13 19:13:46 +0000 | [diff] [blame] | 321 | static internal_key_type ReadKey(const unsigned char* d, unsigned) { |
| 322 | unsigned char k = *(d++); // Read the entry kind. |
| 323 | return std::make_pair(k, (const char*) d); |
Ted Kremenek | d8c0292 | 2009-02-10 22:16:22 +0000 | [diff] [blame] | 324 | } |
Ted Kremenek | 337edcd | 2009-02-12 03:26:59 +0000 | [diff] [blame] | 325 | }; |
| 326 | |
| 327 | class VISIBILITY_HIDDEN PTHFileLookupTrait : public PTHFileLookupCommonTrait { |
| 328 | public: |
| 329 | typedef const FileEntry* external_key_type; |
| 330 | typedef PTHFileData data_type; |
| 331 | |
Ted Kremenek | a4b44dd | 2009-02-13 19:13:46 +0000 | [diff] [blame] | 332 | static internal_key_type GetInternalKey(const FileEntry* FE) { |
| 333 | return std::make_pair((unsigned char) 0x1, FE->getName()); |
Ted Kremenek | 337edcd | 2009-02-12 03:26:59 +0000 | [diff] [blame] | 334 | } |
Ted Kremenek | a4b44dd | 2009-02-13 19:13:46 +0000 | [diff] [blame] | 335 | |
| 336 | static bool EqualKey(internal_key_type a, internal_key_type b) { |
| 337 | return a.first == b.first && strcmp(a.second, b.second) == 0; |
| 338 | } |
Ted Kremenek | d8c0292 | 2009-02-10 22:16:22 +0000 | [diff] [blame] | 339 | |
Ted Kremenek | a4b44dd | 2009-02-13 19:13:46 +0000 | [diff] [blame] | 340 | static PTHFileData ReadData(const internal_key_type& k, |
| 341 | const unsigned char* d, unsigned) { |
| 342 | assert(k.first == 0x1 && "Only file lookups can match!"); |
Ted Kremenek | d8c0292 | 2009-02-10 22:16:22 +0000 | [diff] [blame] | 343 | uint32_t x = ::ReadUnalignedLE32(d); |
| 344 | uint32_t y = ::ReadUnalignedLE32(d); |
| 345 | return PTHFileData(x, y); |
Ted Kremenek | 0c6a77b | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 346 | } |
| 347 | }; |
Ted Kremenek | 7e3a004 | 2009-02-11 21:29:16 +0000 | [diff] [blame] | 348 | |
| 349 | class VISIBILITY_HIDDEN PTHStringLookupTrait { |
| 350 | public: |
| 351 | typedef uint32_t |
| 352 | data_type; |
| 353 | |
| 354 | typedef const std::pair<const char*, unsigned> |
| 355 | external_key_type; |
| 356 | |
| 357 | typedef external_key_type internal_key_type; |
| 358 | |
| 359 | static bool EqualKey(const internal_key_type& a, |
| 360 | const internal_key_type& b) { |
| 361 | return (a.second == b.second) ? memcmp(a.first, b.first, a.second) == 0 |
| 362 | : false; |
| 363 | } |
| 364 | |
| 365 | static unsigned ComputeHash(const internal_key_type& a) { |
| 366 | return BernsteinHash(a.first, a.second); |
| 367 | } |
| 368 | |
| 369 | // This hopefully will just get inlined and removed by the optimizer. |
| 370 | static const internal_key_type& |
| 371 | GetInternalKey(const external_key_type& x) { return x; } |
| 372 | |
| 373 | static std::pair<unsigned, unsigned> |
| 374 | ReadKeyDataLength(const unsigned char*& d) { |
| 375 | return std::make_pair((unsigned) ReadUnalignedLE16(d), sizeof(uint32_t)); |
| 376 | } |
| 377 | |
| 378 | static std::pair<const char*, unsigned> |
| 379 | ReadKey(const unsigned char* d, unsigned n) { |
| 380 | assert(n >= 2 && d[n-1] == '\0'); |
| 381 | return std::make_pair((const char*) d, n-1); |
| 382 | } |
| 383 | |
Ted Kremenek | a4b44dd | 2009-02-13 19:13:46 +0000 | [diff] [blame] | 384 | static uint32_t ReadData(const internal_key_type& k, const unsigned char* d, |
| 385 | unsigned) { |
Ted Kremenek | 7e3a004 | 2009-02-11 21:29:16 +0000 | [diff] [blame] | 386 | return ::ReadUnalignedLE32(d); |
| 387 | } |
| 388 | }; |
| 389 | |
Ted Kremenek | d8c0292 | 2009-02-10 22:16:22 +0000 | [diff] [blame] | 390 | } // end anonymous namespace |
| 391 | |
Ted Kremenek | 7e3a004 | 2009-02-11 21:29:16 +0000 | [diff] [blame] | 392 | typedef OnDiskChainedHashTable<PTHFileLookupTrait> PTHFileLookup; |
| 393 | typedef OnDiskChainedHashTable<PTHStringLookupTrait> PTHStringIdLookup; |
Ted Kremenek | 0c6a77b | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 394 | |
| 395 | //===----------------------------------------------------------------------===// |
| 396 | // PTHManager methods. |
| 397 | //===----------------------------------------------------------------------===// |
| 398 | |
| 399 | PTHManager::PTHManager(const llvm::MemoryBuffer* buf, void* fileLookup, |
Chris Lattner | da9d61c | 2009-01-18 01:57:14 +0000 | [diff] [blame] | 400 | const unsigned char* idDataTable, |
| 401 | IdentifierInfo** perIDCache, |
Ted Kremenek | 7e3a004 | 2009-02-11 21:29:16 +0000 | [diff] [blame] | 402 | void* stringIdLookup, unsigned numIds, |
Ted Kremenek | 6822863 | 2009-03-19 22:19:30 +0000 | [diff] [blame] | 403 | const unsigned char* spellingBase, |
| 404 | const char* originalSourceFile) |
Ted Kremenek | 6183e48 | 2008-12-03 01:16:39 +0000 | [diff] [blame] | 405 | : Buf(buf), PerIDCache(perIDCache), FileLookup(fileLookup), |
Ted Kremenek | 7e3a004 | 2009-02-11 21:29:16 +0000 | [diff] [blame] | 406 | IdDataTable(idDataTable), StringIdLookup(stringIdLookup), |
Ted Kremenek | 6822863 | 2009-03-19 22:19:30 +0000 | [diff] [blame] | 407 | NumIds(numIds), PP(0), SpellingBase(spellingBase), |
| 408 | OriginalSourceFile(originalSourceFile) {} |
Ted Kremenek | 0c6a77b | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 409 | |
| 410 | PTHManager::~PTHManager() { |
| 411 | delete Buf; |
| 412 | delete (PTHFileLookup*) FileLookup; |
Ted Kremenek | 7e3a004 | 2009-02-11 21:29:16 +0000 | [diff] [blame] | 413 | delete (PTHStringIdLookup*) StringIdLookup; |
Ted Kremenek | 0e50b6e | 2008-12-04 22:47:11 +0000 | [diff] [blame] | 414 | free(PerIDCache); |
Ted Kremenek | 0c6a77b | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 415 | } |
| 416 | |
Ted Kremenek | 22f0d09 | 2009-03-22 06:42:39 +0000 | [diff] [blame] | 417 | static void InvalidPTH(Diagnostic *Diags, Diagnostic::Level level, |
| 418 | const char* Msg = 0) { |
Ted Kremenek | 26555b1 | 2009-01-28 21:02:43 +0000 | [diff] [blame] | 419 | if (!Diags) return; |
| 420 | if (!Msg) Msg = "Invalid or corrupted PTH file"; |
Ted Kremenek | 22f0d09 | 2009-03-22 06:42:39 +0000 | [diff] [blame] | 421 | unsigned DiagID = Diags->getCustomDiagID(level, Msg); |
Ted Kremenek | 26555b1 | 2009-01-28 21:02:43 +0000 | [diff] [blame] | 422 | Diags->Report(FullSourceLoc(), DiagID); |
| 423 | } |
| 424 | |
Ted Kremenek | 22f0d09 | 2009-03-22 06:42:39 +0000 | [diff] [blame] | 425 | PTHManager* PTHManager::Create(const std::string& file, Diagnostic* Diags, |
| 426 | Diagnostic::Level level) { |
Ted Kremenek | 0c6a77b | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 427 | // Memory map the PTH file. |
| 428 | llvm::OwningPtr<llvm::MemoryBuffer> |
| 429 | File(llvm::MemoryBuffer::getFile(file.c_str())); |
| 430 | |
Ted Kremenek | 8a6aec6 | 2009-01-28 20:49:33 +0000 | [diff] [blame] | 431 | if (!File) { |
| 432 | if (Diags) { |
Ted Kremenek | 22f0d09 | 2009-03-22 06:42:39 +0000 | [diff] [blame] | 433 | unsigned DiagID = Diags->getCustomDiagID(level, |
Ted Kremenek | 8a6aec6 | 2009-01-28 20:49:33 +0000 | [diff] [blame] | 434 | "PTH file %0 could not be read"); |
| 435 | Diags->Report(FullSourceLoc(), DiagID) << file; |
| 436 | } |
Ted Kremenek | 7e3a004 | 2009-02-11 21:29:16 +0000 | [diff] [blame] | 437 | |
Ted Kremenek | 0c6a77b | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 438 | return 0; |
Ted Kremenek | 8a6aec6 | 2009-01-28 20:49:33 +0000 | [diff] [blame] | 439 | } |
Ted Kremenek | 0c6a77b | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 440 | |
| 441 | // Get the buffer ranges and check if there are at least three 32-bit |
| 442 | // words at the end of the file. |
Chris Lattner | da9d61c | 2009-01-18 01:57:14 +0000 | [diff] [blame] | 443 | const unsigned char* BufBeg = (unsigned char*)File->getBufferStart(); |
| 444 | const unsigned char* BufEnd = (unsigned char*)File->getBufferEnd(); |
Ted Kremenek | e1b6498 | 2009-01-26 21:43:14 +0000 | [diff] [blame] | 445 | |
| 446 | // Check the prologue of the file. |
Ted Kremenek | 4adc71a | 2009-01-26 22:16:12 +0000 | [diff] [blame] | 447 | if ((BufEnd - BufBeg) < (signed) (sizeof("cfe-pth") + 3 + 4) || |
Ted Kremenek | 26555b1 | 2009-01-28 21:02:43 +0000 | [diff] [blame] | 448 | memcmp(BufBeg, "cfe-pth", sizeof("cfe-pth") - 1) != 0) { |
Ted Kremenek | 22f0d09 | 2009-03-22 06:42:39 +0000 | [diff] [blame] | 449 | InvalidPTH(Diags, level); |
Ted Kremenek | e1b6498 | 2009-01-26 21:43:14 +0000 | [diff] [blame] | 450 | return 0; |
Ted Kremenek | 26555b1 | 2009-01-28 21:02:43 +0000 | [diff] [blame] | 451 | } |
Ted Kremenek | 0c6a77b | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 452 | |
Ted Kremenek | 67d1505 | 2009-01-26 21:50:21 +0000 | [diff] [blame] | 453 | // Read the PTH version. |
Ted Kremenek | e1b6498 | 2009-01-26 21:43:14 +0000 | [diff] [blame] | 454 | const unsigned char *p = BufBeg + (sizeof("cfe-pth") - 1); |
Ted Kremenek | 67d1505 | 2009-01-26 21:50:21 +0000 | [diff] [blame] | 455 | unsigned Version = ReadLE32(p); |
| 456 | |
Ted Kremenek | 26555b1 | 2009-01-28 21:02:43 +0000 | [diff] [blame] | 457 | if (Version != PTHManager::Version) { |
Ted Kremenek | 22f0d09 | 2009-03-22 06:42:39 +0000 | [diff] [blame] | 458 | InvalidPTH(Diags, level, |
Ted Kremenek | 26555b1 | 2009-01-28 21:02:43 +0000 | [diff] [blame] | 459 | Version < PTHManager::Version |
| 460 | ? "PTH file uses an older PTH format that is no longer supported" |
| 461 | : "PTH file uses a newer PTH format that cannot be read"); |
Ted Kremenek | 67d1505 | 2009-01-26 21:50:21 +0000 | [diff] [blame] | 462 | return 0; |
Ted Kremenek | 26555b1 | 2009-01-28 21:02:43 +0000 | [diff] [blame] | 463 | } |
Ted Kremenek | 67d1505 | 2009-01-26 21:50:21 +0000 | [diff] [blame] | 464 | |
| 465 | // Compute the address of the index table at the end of the PTH file. |
Ted Kremenek | a4bd8eb | 2009-02-11 23:34:32 +0000 | [diff] [blame] | 466 | const unsigned char *PrologueOffset = p; |
Ted Kremenek | e1b6498 | 2009-01-26 21:43:14 +0000 | [diff] [blame] | 467 | |
Ted Kremenek | a4bd8eb | 2009-02-11 23:34:32 +0000 | [diff] [blame] | 468 | if (PrologueOffset >= BufEnd) { |
Ted Kremenek | 22f0d09 | 2009-03-22 06:42:39 +0000 | [diff] [blame] | 469 | InvalidPTH(Diags, level); |
Ted Kremenek | e1b6498 | 2009-01-26 21:43:14 +0000 | [diff] [blame] | 470 | return 0; |
Ted Kremenek | 26555b1 | 2009-01-28 21:02:43 +0000 | [diff] [blame] | 471 | } |
Ted Kremenek | 0c6a77b | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 472 | |
| 473 | // Construct the file lookup table. This will be used for mapping from |
| 474 | // FileEntry*'s to cached tokens. |
Ted Kremenek | a4bd8eb | 2009-02-11 23:34:32 +0000 | [diff] [blame] | 475 | const unsigned char* FileTableOffset = PrologueOffset + sizeof(uint32_t)*2; |
Chris Lattner | 5ff4317 | 2009-01-22 19:48:26 +0000 | [diff] [blame] | 476 | const unsigned char* FileTable = BufBeg + ReadLE32(FileTableOffset); |
Ted Kremenek | 0c6a77b | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 477 | |
| 478 | if (!(FileTable > BufBeg && FileTable < BufEnd)) { |
Ted Kremenek | 22f0d09 | 2009-03-22 06:42:39 +0000 | [diff] [blame] | 479 | InvalidPTH(Diags, level); |
Ted Kremenek | 0c6a77b | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 480 | return 0; // FIXME: Proper error diagnostic? |
| 481 | } |
| 482 | |
Ted Kremenek | d8c0292 | 2009-02-10 22:16:22 +0000 | [diff] [blame] | 483 | llvm::OwningPtr<PTHFileLookup> FL(PTHFileLookup::Create(FileTable, BufBeg)); |
Ted Kremenek | 1d20197 | 2009-03-20 17:54:25 +0000 | [diff] [blame] | 484 | |
| 485 | // Warn if the PTH file is empty. We still want to create a PTHManager |
| 486 | // as the PTH could be used with -include-pth. |
| 487 | if (FL->isEmpty()) |
Ted Kremenek | 22f0d09 | 2009-03-22 06:42:39 +0000 | [diff] [blame] | 488 | InvalidPTH(Diags, level, "PTH file contains no cached source data"); |
Ted Kremenek | 0c6a77b | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 489 | |
| 490 | // Get the location of the table mapping from persistent ids to the |
| 491 | // data needed to reconstruct identifiers. |
Ted Kremenek | a4bd8eb | 2009-02-11 23:34:32 +0000 | [diff] [blame] | 492 | const unsigned char* IDTableOffset = PrologueOffset + sizeof(uint32_t)*0; |
Chris Lattner | 5ff4317 | 2009-01-22 19:48:26 +0000 | [diff] [blame] | 493 | const unsigned char* IData = BufBeg + ReadLE32(IDTableOffset); |
Ted Kremenek | cdd8f21 | 2009-01-21 07:34:28 +0000 | [diff] [blame] | 494 | |
| 495 | if (!(IData >= BufBeg && IData < BufEnd)) { |
Ted Kremenek | 22f0d09 | 2009-03-22 06:42:39 +0000 | [diff] [blame] | 496 | InvalidPTH(Diags, level); |
Ted Kremenek | 26555b1 | 2009-01-28 21:02:43 +0000 | [diff] [blame] | 497 | return 0; |
Ted Kremenek | 0c6a77b | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 498 | } |
| 499 | |
Ted Kremenek | 7e3a004 | 2009-02-11 21:29:16 +0000 | [diff] [blame] | 500 | // Get the location of the hashtable mapping between strings and |
| 501 | // persistent IDs. |
Ted Kremenek | a4bd8eb | 2009-02-11 23:34:32 +0000 | [diff] [blame] | 502 | const unsigned char* StringIdTableOffset = PrologueOffset + sizeof(uint32_t)*1; |
Ted Kremenek | 7e3a004 | 2009-02-11 21:29:16 +0000 | [diff] [blame] | 503 | const unsigned char* StringIdTable = BufBeg + ReadLE32(StringIdTableOffset); |
| 504 | if (!(StringIdTable >= BufBeg && StringIdTable < BufEnd)) { |
Ted Kremenek | 22f0d09 | 2009-03-22 06:42:39 +0000 | [diff] [blame] | 505 | InvalidPTH(Diags, level); |
Ted Kremenek | 26555b1 | 2009-01-28 21:02:43 +0000 | [diff] [blame] | 506 | return 0; |
Ted Kremenek | 72b1b15 | 2009-01-15 18:47:46 +0000 | [diff] [blame] | 507 | } |
Ted Kremenek | 7e3a004 | 2009-02-11 21:29:16 +0000 | [diff] [blame] | 508 | |
| 509 | llvm::OwningPtr<PTHStringIdLookup> SL(PTHStringIdLookup::Create(StringIdTable, |
| 510 | BufBeg)); |
Ted Kremenek | 783bb74 | 2009-03-21 00:25:09 +0000 | [diff] [blame] | 511 | |
Ted Kremenek | 277faca | 2009-01-27 00:01:05 +0000 | [diff] [blame] | 512 | // Get the location of the spelling cache. |
Ted Kremenek | a4bd8eb | 2009-02-11 23:34:32 +0000 | [diff] [blame] | 513 | const unsigned char* spellingBaseOffset = PrologueOffset + sizeof(uint32_t)*3; |
Ted Kremenek | 277faca | 2009-01-27 00:01:05 +0000 | [diff] [blame] | 514 | const unsigned char* spellingBase = BufBeg + ReadLE32(spellingBaseOffset); |
| 515 | if (!(spellingBase >= BufBeg && spellingBase < BufEnd)) { |
Ted Kremenek | 22f0d09 | 2009-03-22 06:42:39 +0000 | [diff] [blame] | 516 | InvalidPTH(Diags, level); |
Ted Kremenek | 277faca | 2009-01-27 00:01:05 +0000 | [diff] [blame] | 517 | return 0; |
| 518 | } |
| 519 | |
Ted Kremenek | 6183e48 | 2008-12-03 01:16:39 +0000 | [diff] [blame] | 520 | // Get the number of IdentifierInfos and pre-allocate the identifier cache. |
Chris Lattner | 5ff4317 | 2009-01-22 19:48:26 +0000 | [diff] [blame] | 521 | uint32_t NumIds = ReadLE32(IData); |
Ted Kremenek | cdd8f21 | 2009-01-21 07:34:28 +0000 | [diff] [blame] | 522 | |
Ted Kremenek | 6183e48 | 2008-12-03 01:16:39 +0000 | [diff] [blame] | 523 | // Pre-allocate the peristent ID -> IdentifierInfo* cache. We use calloc() |
| 524 | // so that we in the best case only zero out memory once when the OS returns |
| 525 | // us new pages. |
Ted Kremenek | cdd8f21 | 2009-01-21 07:34:28 +0000 | [diff] [blame] | 526 | IdentifierInfo** PerIDCache = 0; |
Ted Kremenek | 6183e48 | 2008-12-03 01:16:39 +0000 | [diff] [blame] | 527 | |
Ted Kremenek | cdd8f21 | 2009-01-21 07:34:28 +0000 | [diff] [blame] | 528 | if (NumIds) { |
| 529 | PerIDCache = (IdentifierInfo**)calloc(NumIds, sizeof(*PerIDCache)); |
| 530 | if (!PerIDCache) { |
Ted Kremenek | 22f0d09 | 2009-03-22 06:42:39 +0000 | [diff] [blame] | 531 | InvalidPTH(Diags, level, |
| 532 | "Could not allocate memory for processing PTH file"); |
Ted Kremenek | cdd8f21 | 2009-01-21 07:34:28 +0000 | [diff] [blame] | 533 | return 0; |
| 534 | } |
Ted Kremenek | 6183e48 | 2008-12-03 01:16:39 +0000 | [diff] [blame] | 535 | } |
Ted Kremenek | cdd8f21 | 2009-01-21 07:34:28 +0000 | [diff] [blame] | 536 | |
Ted Kremenek | 6822863 | 2009-03-19 22:19:30 +0000 | [diff] [blame] | 537 | // Compute the address of the original source file. |
| 538 | const unsigned char* originalSourceBase = PrologueOffset + sizeof(uint32_t)*4; |
| 539 | unsigned len = ReadUnalignedLE16(originalSourceBase); |
| 540 | if (!len) originalSourceBase = 0; |
| 541 | |
Ted Kremenek | 72b1b15 | 2009-01-15 18:47:46 +0000 | [diff] [blame] | 542 | // Create the new PTHManager. |
| 543 | return new PTHManager(File.take(), FL.take(), IData, PerIDCache, |
Ted Kremenek | 6822863 | 2009-03-19 22:19:30 +0000 | [diff] [blame] | 544 | SL.take(), NumIds, spellingBase, |
| 545 | (const char*) originalSourceBase); |
Ted Kremenek | 0c6a77b | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 546 | } |
Ted Kremenek | 6822863 | 2009-03-19 22:19:30 +0000 | [diff] [blame] | 547 | |
Chris Lattner | 77ecb3a | 2009-01-18 02:57:21 +0000 | [diff] [blame] | 548 | IdentifierInfo* PTHManager::LazilyCreateIdentifierInfo(unsigned PersistentID) { |
Ted Kremenek | 0c6a77b | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 549 | // 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] | 550 | const unsigned char* TableEntry = IdDataTable + sizeof(uint32_t)*PersistentID; |
Chris Lattner | da9d61c | 2009-01-18 01:57:14 +0000 | [diff] [blame] | 551 | const unsigned char* IDData = |
Chris Lattner | 5ff4317 | 2009-01-22 19:48:26 +0000 | [diff] [blame] | 552 | (const unsigned char*)Buf->getBufferStart() + ReadLE32(TableEntry); |
Chris Lattner | da9d61c | 2009-01-18 01:57:14 +0000 | [diff] [blame] | 553 | assert(IDData < (const unsigned char*)Buf->getBufferEnd()); |
Ted Kremenek | 0c6a77b | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 554 | |
Ted Kremenek | 72b1b15 | 2009-01-15 18:47:46 +0000 | [diff] [blame] | 555 | // Allocate the object. |
Chris Lattner | da9d61c | 2009-01-18 01:57:14 +0000 | [diff] [blame] | 556 | std::pair<IdentifierInfo,const unsigned char*> *Mem = |
| 557 | Alloc.Allocate<std::pair<IdentifierInfo,const unsigned char*> >(); |
Ted Kremenek | 72b1b15 | 2009-01-15 18:47:46 +0000 | [diff] [blame] | 558 | |
| 559 | Mem->second = IDData; |
Ted Kremenek | 7e3a004 | 2009-02-11 21:29:16 +0000 | [diff] [blame] | 560 | assert(IDData[0] != '\0'); |
Ted Kremenek | ea9c26b | 2009-01-20 23:28:34 +0000 | [diff] [blame] | 561 | IdentifierInfo *II = new ((void*) Mem) IdentifierInfo(); |
Ted Kremenek | 0c6a77b | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 562 | |
Ted Kremenek | 72b1b15 | 2009-01-15 18:47:46 +0000 | [diff] [blame] | 563 | // Store the new IdentifierInfo in the cache. |
Chris Lattner | 77ecb3a | 2009-01-18 02:57:21 +0000 | [diff] [blame] | 564 | PerIDCache[PersistentID] = II; |
Ted Kremenek | 7e3a004 | 2009-02-11 21:29:16 +0000 | [diff] [blame] | 565 | assert(II->getName() && II->getName()[0] != '\0'); |
Ted Kremenek | 0c6a77b | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 566 | return II; |
| 567 | } |
| 568 | |
Ted Kremenek | 72b1b15 | 2009-01-15 18:47:46 +0000 | [diff] [blame] | 569 | IdentifierInfo* PTHManager::get(const char *NameStart, const char *NameEnd) { |
Ted Kremenek | 7e3a004 | 2009-02-11 21:29:16 +0000 | [diff] [blame] | 570 | PTHStringIdLookup& SL = *((PTHStringIdLookup*)StringIdLookup); |
| 571 | // Double check our assumption that the last character isn't '\0'. |
Daniel Dunbar | 82320e9 | 2009-02-12 19:31:53 +0000 | [diff] [blame] | 572 | assert(NameEnd==NameStart || NameStart[NameEnd-NameStart-1] != '\0'); |
Ted Kremenek | 7e3a004 | 2009-02-11 21:29:16 +0000 | [diff] [blame] | 573 | PTHStringIdLookup::iterator I = SL.find(std::make_pair(NameStart, |
| 574 | NameEnd - NameStart)); |
| 575 | if (I == SL.end()) // No identifier found? |
| 576 | return 0; |
Ted Kremenek | 72b1b15 | 2009-01-15 18:47:46 +0000 | [diff] [blame] | 577 | |
Ted Kremenek | 7e3a004 | 2009-02-11 21:29:16 +0000 | [diff] [blame] | 578 | // Match found. Return the identifier! |
| 579 | assert(*I > 0); |
| 580 | return GetIdentifierInfo(*I-1); |
| 581 | } |
Ted Kremenek | 72b1b15 | 2009-01-15 18:47:46 +0000 | [diff] [blame] | 582 | |
Chris Lattner | f056d92 | 2009-01-17 08:06:50 +0000 | [diff] [blame] | 583 | PTHLexer *PTHManager::CreateLexer(FileID FID) { |
| 584 | const FileEntry *FE = PP->getSourceManager().getFileEntryForID(FID); |
Ted Kremenek | 0c6a77b | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 585 | if (!FE) |
| 586 | return 0; |
| 587 | |
| 588 | // Lookup the FileEntry object in our file lookup data structure. It will |
| 589 | // return a variant that indicates whether or not there is an offset within |
| 590 | // the PTH file that contains cached tokens. |
Ted Kremenek | d8c0292 | 2009-02-10 22:16:22 +0000 | [diff] [blame] | 591 | PTHFileLookup& PFL = *((PTHFileLookup*)FileLookup); |
| 592 | PTHFileLookup::iterator I = PFL.find(FE); |
Ted Kremenek | 0c6a77b | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 593 | |
Ted Kremenek | d8c0292 | 2009-02-10 22:16:22 +0000 | [diff] [blame] | 594 | if (I == PFL.end()) // No tokens available? |
Ted Kremenek | 0c6a77b | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 595 | return 0; |
| 596 | |
Ted Kremenek | d8c0292 | 2009-02-10 22:16:22 +0000 | [diff] [blame] | 597 | const PTHFileData& FileData = *I; |
| 598 | |
Chris Lattner | da9d61c | 2009-01-18 01:57:14 +0000 | [diff] [blame] | 599 | const unsigned char *BufStart = (const unsigned char *)Buf->getBufferStart(); |
Ted Kremenek | 0c6a77b | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 600 | // Compute the offset of the token data within the buffer. |
Chris Lattner | da9d61c | 2009-01-18 01:57:14 +0000 | [diff] [blame] | 601 | const unsigned char* data = BufStart + FileData.getTokenOffset(); |
Ted Kremenek | 268ee70 | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 602 | |
| 603 | // Get the location of pp-conditional table. |
Chris Lattner | da9d61c | 2009-01-18 01:57:14 +0000 | [diff] [blame] | 604 | const unsigned char* ppcond = BufStart + FileData.getPPCondOffset(); |
Chris Lattner | 5ff4317 | 2009-01-22 19:48:26 +0000 | [diff] [blame] | 605 | uint32_t Len = ReadLE32(ppcond); |
Chris Lattner | 1b5285e | 2009-01-18 02:10:31 +0000 | [diff] [blame] | 606 | if (Len == 0) ppcond = 0; |
Ted Kremenek | 32a8ad5 | 2009-01-08 04:30:32 +0000 | [diff] [blame] | 607 | |
Ted Kremenek | 72b1b15 | 2009-01-15 18:47:46 +0000 | [diff] [blame] | 608 | assert(PP && "No preprocessor set yet!"); |
Ted Kremenek | 277faca | 2009-01-27 00:01:05 +0000 | [diff] [blame] | 609 | return new PTHLexer(*PP, FID, data, ppcond, *this); |
Ted Kremenek | 0c6a77b | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 610 | } |
Ted Kremenek | 337edcd | 2009-02-12 03:26:59 +0000 | [diff] [blame] | 611 | |
| 612 | //===----------------------------------------------------------------------===// |
| 613 | // 'stat' caching. |
| 614 | //===----------------------------------------------------------------------===// |
| 615 | |
| 616 | namespace { |
| 617 | class VISIBILITY_HIDDEN PTHStatData { |
| 618 | public: |
Ted Kremenek | a4b44dd | 2009-02-13 19:13:46 +0000 | [diff] [blame] | 619 | const bool hasStat; |
Ted Kremenek | 337edcd | 2009-02-12 03:26:59 +0000 | [diff] [blame] | 620 | const ino_t ino; |
| 621 | const dev_t dev; |
| 622 | const mode_t mode; |
| 623 | const time_t mtime; |
| 624 | const off_t size; |
| 625 | |
| 626 | PTHStatData(ino_t i, dev_t d, mode_t mo, time_t m, off_t s) |
Ted Kremenek | a4b44dd | 2009-02-13 19:13:46 +0000 | [diff] [blame] | 627 | : hasStat(true), ino(i), dev(d), mode(mo), mtime(m), size(s) {} |
| 628 | |
| 629 | PTHStatData() |
| 630 | : hasStat(false), ino(0), dev(0), mode(0), mtime(0), size(0) {} |
Ted Kremenek | 337edcd | 2009-02-12 03:26:59 +0000 | [diff] [blame] | 631 | }; |
| 632 | |
| 633 | class VISIBILITY_HIDDEN PTHStatLookupTrait : public PTHFileLookupCommonTrait { |
| 634 | public: |
Ted Kremenek | a4b44dd | 2009-02-13 19:13:46 +0000 | [diff] [blame] | 635 | typedef const char* external_key_type; // const char* |
Ted Kremenek | 337edcd | 2009-02-12 03:26:59 +0000 | [diff] [blame] | 636 | typedef PTHStatData data_type; |
| 637 | |
Ted Kremenek | a4b44dd | 2009-02-13 19:13:46 +0000 | [diff] [blame] | 638 | static internal_key_type GetInternalKey(const char *path) { |
| 639 | // The key 'kind' doesn't matter here because it is ignored in EqualKey. |
| 640 | return std::make_pair((unsigned char) 0x0, path); |
| 641 | } |
| 642 | |
| 643 | static bool EqualKey(internal_key_type a, internal_key_type b) { |
| 644 | // When doing 'stat' lookups we don't care about the kind of 'a' and 'b', |
| 645 | // just the paths. |
| 646 | return strcmp(a.second, b.second) == 0; |
| 647 | } |
Ted Kremenek | 337edcd | 2009-02-12 03:26:59 +0000 | [diff] [blame] | 648 | |
Ted Kremenek | a4b44dd | 2009-02-13 19:13:46 +0000 | [diff] [blame] | 649 | static data_type ReadData(const internal_key_type& k, const unsigned char* d, |
| 650 | unsigned) { |
Ted Kremenek | ad6ce5c | 2009-02-13 22:07:44 +0000 | [diff] [blame] | 651 | |
| 652 | if (k.first /* File or Directory */) { |
| 653 | if (k.first == 0x1 /* File */) d += 4 * 2; // Skip the first 2 words. |
Ted Kremenek | a4b44dd | 2009-02-13 19:13:46 +0000 | [diff] [blame] | 654 | ino_t ino = (ino_t) ReadUnalignedLE32(d); |
| 655 | dev_t dev = (dev_t) ReadUnalignedLE32(d); |
| 656 | mode_t mode = (mode_t) ReadUnalignedLE16(d); |
| 657 | time_t mtime = (time_t) ReadUnalignedLE64(d); |
| 658 | return data_type(ino, dev, mode, mtime, (off_t) ReadUnalignedLE64(d)); |
| 659 | } |
Ted Kremenek | ad6ce5c | 2009-02-13 22:07:44 +0000 | [diff] [blame] | 660 | |
| 661 | // Negative stat. Don't read anything. |
Ted Kremenek | a4b44dd | 2009-02-13 19:13:46 +0000 | [diff] [blame] | 662 | return data_type(); |
Ted Kremenek | 337edcd | 2009-02-12 03:26:59 +0000 | [diff] [blame] | 663 | } |
| 664 | }; |
Ted Kremenek | 337edcd | 2009-02-12 03:26:59 +0000 | [diff] [blame] | 665 | |
| 666 | class VISIBILITY_HIDDEN PTHStatCache : public StatSysCallCache { |
| 667 | typedef OnDiskChainedHashTable<PTHStatLookupTrait> CacheTy; |
| 668 | CacheTy Cache; |
| 669 | |
| 670 | public: |
| 671 | PTHStatCache(PTHFileLookup &FL) : |
| 672 | Cache(FL.getNumBuckets(), FL.getNumEntries(), FL.getBuckets(), |
| 673 | FL.getBase()) {} |
| 674 | |
| 675 | ~PTHStatCache() {} |
| 676 | |
| 677 | int stat(const char *path, struct stat *buf) { |
| 678 | // Do the lookup for the file's data in the PTH file. |
| 679 | CacheTy::iterator I = Cache.find(path); |
| 680 | |
| 681 | // If we don't get a hit in the PTH file just forward to 'stat'. |
| 682 | if (I == Cache.end()) return ::stat(path, buf); |
| 683 | |
| 684 | const PTHStatData& Data = *I; |
Ted Kremenek | ad6ce5c | 2009-02-13 22:07:44 +0000 | [diff] [blame] | 685 | |
| 686 | if (!Data.hasStat) |
| 687 | return 1; |
| 688 | |
Ted Kremenek | 337edcd | 2009-02-12 03:26:59 +0000 | [diff] [blame] | 689 | buf->st_ino = Data.ino; |
| 690 | buf->st_dev = Data.dev; |
| 691 | buf->st_mtime = Data.mtime; |
| 692 | buf->st_mode = Data.mode; |
| 693 | buf->st_size = Data.size; |
| 694 | return 0; |
| 695 | } |
| 696 | }; |
Ted Kremenek | d578569 | 2009-02-23 23:27:54 +0000 | [diff] [blame] | 697 | } // end anonymous namespace |
Ted Kremenek | 337edcd | 2009-02-12 03:26:59 +0000 | [diff] [blame] | 698 | |
| 699 | StatSysCallCache *PTHManager::createStatCache() { |
Ted Kremenek | 5f747d1 | 2009-02-12 03:45:39 +0000 | [diff] [blame] | 700 | return new PTHStatCache(*((PTHFileLookup*) FileLookup)); |
Ted Kremenek | 337edcd | 2009-02-12 03:26:59 +0000 | [diff] [blame] | 701 | } |