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