Ted Kremenek | 7cd6245 | 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 | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 14 | #include "clang/Lex/PTHLexer.h" |
Ted Kremenek | 33eeabd | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 15 | #include "clang/Basic/FileManager.h" |
Chris Lattner | 226efd3 | 2010-11-23 19:19:34 +0000 | [diff] [blame] | 16 | #include "clang/Basic/FileSystemStatCache.h" |
Ted Kremenek | 33eeabd | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 17 | #include "clang/Basic/IdentifierTable.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 18 | #include "clang/Basic/TokenKinds.h" |
Daniel Dunbar | 1a54e3f | 2009-11-12 02:53:48 +0000 | [diff] [blame] | 19 | #include "clang/Lex/LexDiagnostic.h" |
Ted Kremenek | 33eeabd | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 20 | #include "clang/Lex/PTHManager.h" |
Ted Kremenek | 33eeabd | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 21 | #include "clang/Lex/Preprocessor.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 22 | #include "clang/Lex/Token.h" |
Daniel Dunbar | f8502d5 | 2009-10-17 23:52:28 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/StringExtras.h" |
| 24 | #include "llvm/ADT/StringMap.h" |
Justin Bogner | e1c147c | 2014-03-28 22:03:19 +0000 | [diff] [blame] | 25 | #include "llvm/Support/EndianStream.h" |
Chris Lattner | 34eab39 | 2009-01-22 23:50:07 +0000 | [diff] [blame] | 26 | #include "llvm/Support/MemoryBuffer.h" |
Ahmed Charles | dfca6f9 | 2014-03-09 11:36:40 +0000 | [diff] [blame] | 27 | #include <memory> |
Rafael Espindola | 8a8e554 | 2014-06-12 17:19:42 +0000 | [diff] [blame] | 28 | #include <system_error> |
Ted Kremenek | 7cd6245 | 2008-11-12 21:37:15 +0000 | [diff] [blame] | 29 | using namespace clang; |
| 30 | |
Alp Toker | 2584735 | 2014-07-07 14:01:37 +0000 | [diff] [blame] | 31 | static const unsigned StoredTokenSize = 1 + 1 + 2 + 4 + 4; |
Ted Kremenek | 56572ab | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 32 | |
Ted Kremenek | 33eeabd | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 33 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 1b18ad2 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 34 | // PTHLexer methods. |
| 35 | //===----------------------------------------------------------------------===// |
| 36 | |
Chris Lattner | eb09754 | 2009-01-18 01:57:14 +0000 | [diff] [blame] | 37 | PTHLexer::PTHLexer(Preprocessor &PP, FileID FID, const unsigned char *D, |
Ted Kremenek | 8d178f4 | 2009-01-27 00:01:05 +0000 | [diff] [blame] | 38 | const unsigned char *ppcond, PTHManager &PM) |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 39 | : PreprocessorLexer(&PP, FID), TokBuf(D), CurPtr(D), LastHashTokPtr(nullptr), |
Ted Kremenek | 8d178f4 | 2009-01-27 00:01:05 +0000 | [diff] [blame] | 40 | PPCond(ppcond), CurPPCondPtr(ppcond), PTHMgr(PM) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 41 | |
Chris Lattner | d32480d | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 42 | FileStartLoc = PP.getSourceManager().getLocForStartOfFile(FID); |
Ted Kremenek | 47b8cf6 | 2009-01-09 22:05:30 +0000 | [diff] [blame] | 43 | } |
Ted Kremenek | 1b18ad2 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 44 | |
Eli Friedman | 0834a4b | 2013-09-19 00:41:32 +0000 | [diff] [blame] | 45 | bool PTHLexer::Lex(Token& Tok) { |
Ted Kremenek | 66076a9 | 2008-12-23 02:30:15 +0000 | [diff] [blame] | 46 | //===--------------------------------------==// |
| 47 | // Read the raw token data. |
| 48 | //===--------------------------------------==// |
Justin Bogner | 57ba0b2 | 2014-03-28 22:03:24 +0000 | [diff] [blame] | 49 | using namespace llvm::support; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 50 | |
Ted Kremenek | 66076a9 | 2008-12-23 02:30:15 +0000 | [diff] [blame] | 51 | // Shadow CurPtr into an automatic variable. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 52 | const unsigned char *CurPtrShadow = CurPtr; |
Ted Kremenek | 66076a9 | 2008-12-23 02:30:15 +0000 | [diff] [blame] | 53 | |
Chris Lattner | 137d649 | 2009-01-18 02:10:31 +0000 | [diff] [blame] | 54 | // Read in the data for the token. |
Justin Bogner | 57ba0b2 | 2014-03-28 22:03:24 +0000 | [diff] [blame] | 55 | unsigned Word0 = endian::readNext<uint32_t, little, aligned>(CurPtrShadow); |
| 56 | uint32_t IdentifierID = |
| 57 | endian::readNext<uint32_t, little, aligned>(CurPtrShadow); |
| 58 | uint32_t FileOffset = |
| 59 | endian::readNext<uint32_t, little, aligned>(CurPtrShadow); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 60 | |
Ted Kremenek | 8433f0b | 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 | 47def97 | 2009-01-21 07:21:56 +0000 | [diff] [blame] | 63 | uint32_t Len = Word0 >> 16; |
Ted Kremenek | 8433f0b | 2009-01-19 23:13:15 +0000 | [diff] [blame] | 64 | |
Chris Lattner | 47def97 | 2009-01-21 07:21:56 +0000 | [diff] [blame] | 65 | CurPtr = CurPtrShadow; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 66 | |
Ted Kremenek | 66076a9 | 2008-12-23 02:30:15 +0000 | [diff] [blame] | 67 | //===--------------------------------------==// |
| 68 | // Construct the token itself. |
| 69 | //===--------------------------------------==// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 70 | |
Ted Kremenek | 66076a9 | 2008-12-23 02:30:15 +0000 | [diff] [blame] | 71 | Tok.startToken(); |
Chris Lattner | 18fc6ce | 2009-01-18 02:34:01 +0000 | [diff] [blame] | 72 | Tok.setKind(TKind); |
| 73 | Tok.setFlag(TFlags); |
Ted Kremenek | 78cc247 | 2008-12-23 19:24:24 +0000 | [diff] [blame] | 74 | assert(!LexingRawMode); |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 75 | Tok.setLocation(FileStartLoc.getLocWithOffset(FileOffset)); |
Ted Kremenek | 66076a9 | 2008-12-23 02:30:15 +0000 | [diff] [blame] | 76 | Tok.setLength(Len); |
| 77 | |
Chris Lattner | 3029b35 | 2009-01-21 07:50:06 +0000 | [diff] [blame] | 78 | // Handle identifiers. |
Ted Kremenek | 8d178f4 | 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 | 3029b35 | 2009-01-21 07:50:06 +0000 | [diff] [blame] | 83 | MIOpt.ReadToken(); |
| 84 | IdentifierInfo *II = PTHMgr.GetIdentifierInfo(IdentifierID-1); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 85 | |
Chris Lattner | 3029b35 | 2009-01-21 07:50:06 +0000 | [diff] [blame] | 86 | Tok.setIdentifierInfo(II); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 87 | |
Chris Lattner | 1f6c7fe | 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 | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 91 | |
Chris Lattner | 3029b35 | 2009-01-21 07:50:06 +0000 | [diff] [blame] | 92 | if (II->isHandleIdentifierCase()) |
Eli Friedman | 0834a4b | 2013-09-19 00:41:32 +0000 | [diff] [blame] | 93 | return PP->HandleIdentifier(Tok); |
| 94 | |
| 95 | return true; |
Chris Lattner | 3029b35 | 2009-01-21 07:50:06 +0000 | [diff] [blame] | 96 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 97 | |
Ted Kremenek | 66076a9 | 2008-12-23 02:30:15 +0000 | [diff] [blame] | 98 | //===--------------------------------------==// |
| 99 | // Process the token. |
| 100 | //===--------------------------------------==// |
Chris Lattner | 18fc6ce | 2009-01-18 02:34:01 +0000 | [diff] [blame] | 101 | if (TKind == tok::eof) { |
Ted Kremenek | 1b18ad2 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 102 | // Save the end-of-file token. |
| 103 | EofToken = Tok; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 104 | |
Ted Kremenek | 78cc247 | 2008-12-23 19:24:24 +0000 | [diff] [blame] | 105 | assert(!ParsingPreprocessorDirective); |
| 106 | assert(!LexingRawMode); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 107 | |
Eli Friedman | 0834a4b | 2013-09-19 00:41:32 +0000 | [diff] [blame] | 108 | return LexEndOfFile(Tok); |
Ted Kremenek | 1b18ad2 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 109 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 110 | |
Chris Lattner | 18fc6ce | 2009-01-18 02:34:01 +0000 | [diff] [blame] | 111 | if (TKind == tok::hash && Tok.isAtStartOfLine()) { |
Alp Toker | 2584735 | 2014-07-07 14:01:37 +0000 | [diff] [blame] | 112 | LastHashTokPtr = CurPtr - StoredTokenSize; |
Ted Kremenek | 78cc247 | 2008-12-23 19:24:24 +0000 | [diff] [blame] | 113 | assert(!LexingRawMode); |
| 114 | PP->HandleDirective(Tok); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 115 | |
Eli Friedman | 0834a4b | 2013-09-19 00:41:32 +0000 | [diff] [blame] | 116 | return false; |
Ted Kremenek | 78cc247 | 2008-12-23 19:24:24 +0000 | [diff] [blame] | 117 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 118 | |
Peter Collingbourne | 2f1e36b | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 119 | if (TKind == tok::eod) { |
Ted Kremenek | 78cc247 | 2008-12-23 19:24:24 +0000 | [diff] [blame] | 120 | assert(ParsingPreprocessorDirective); |
Ted Kremenek | 1b18ad2 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 121 | ParsingPreprocessorDirective = false; |
Eli Friedman | 0834a4b | 2013-09-19 00:41:32 +0000 | [diff] [blame] | 122 | return true; |
Ted Kremenek | 1b18ad2 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 123 | } |
Ted Kremenek | 1b18ad2 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 124 | |
Ted Kremenek | 78cc247 | 2008-12-23 19:24:24 +0000 | [diff] [blame] | 125 | MIOpt.ReadToken(); |
Eli Friedman | 0834a4b | 2013-09-19 00:41:32 +0000 | [diff] [blame] | 126 | return true; |
Ted Kremenek | 1b18ad2 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Ted Kremenek | 2bd41d1 | 2010-07-27 02:59:02 +0000 | [diff] [blame] | 129 | bool PTHLexer::LexEndOfFile(Token &Result) { |
| 130 | // If we hit the end of the file while parsing a preprocessor directive, |
| 131 | // end the preprocessor directive first. The next token returned will |
| 132 | // then be the end of file. |
| 133 | if (ParsingPreprocessorDirective) { |
| 134 | ParsingPreprocessorDirective = false; // Done parsing the "line". |
| 135 | return true; // Have a token. |
| 136 | } |
| 137 | |
| 138 | assert(!LexingRawMode); |
| 139 | |
| 140 | // If we are in a #if directive, emit an error. |
| 141 | while (!ConditionalStack.empty()) { |
Argyrios Kyrtzidis | 5cec2ae | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 142 | if (PP->getCodeCompletionFileLoc() != FileStartLoc) |
Douglas Gregor | 02690ba | 2010-08-12 17:04:55 +0000 | [diff] [blame] | 143 | PP->Diag(ConditionalStack.back().IfLoc, |
| 144 | diag::err_pp_unterminated_conditional); |
Ted Kremenek | 2bd41d1 | 2010-07-27 02:59:02 +0000 | [diff] [blame] | 145 | ConditionalStack.pop_back(); |
| 146 | } |
| 147 | |
| 148 | // Finally, let the preprocessor handle this. |
| 149 | return PP->HandleEndOfFile(Result); |
| 150 | } |
| 151 | |
Ted Kremenek | 1b18ad2 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 152 | // FIXME: We can just grab the last token instead of storing a copy |
| 153 | // into EofToken. |
Ted Kremenek | 78cc247 | 2008-12-23 19:24:24 +0000 | [diff] [blame] | 154 | void PTHLexer::getEOF(Token& Tok) { |
Ted Kremenek | 8ae0662 | 2009-01-09 00:36:11 +0000 | [diff] [blame] | 155 | assert(EofToken.is(tok::eof)); |
Ted Kremenek | 1b18ad2 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 156 | Tok = EofToken; |
| 157 | } |
| 158 | |
| 159 | void PTHLexer::DiscardToEndOfLine() { |
| 160 | assert(ParsingPreprocessorDirective && ParsingFilename == false && |
| 161 | "Must be in a preprocessing directive!"); |
| 162 | |
| 163 | // We assume that if the preprocessor wishes to discard to the end of |
| 164 | // the line that it also means to end the current preprocessor directive. |
| 165 | ParsingPreprocessorDirective = false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 166 | |
Ted Kremenek | 1b18ad2 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 167 | // Skip tokens by only peeking at their token kind and the flags. |
| 168 | // We don't need to actually reconstruct full tokens from the token buffer. |
| 169 | // This saves some copies and it also reduces IdentifierInfo* lookup. |
Chris Lattner | eb09754 | 2009-01-18 01:57:14 +0000 | [diff] [blame] | 170 | const unsigned char* p = CurPtr; |
Ted Kremenek | 1b18ad2 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 171 | while (1) { |
| 172 | // Read the token kind. Are we at the end of the file? |
| 173 | tok::TokenKind x = (tok::TokenKind) (uint8_t) *p; |
| 174 | if (x == tok::eof) break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 175 | |
Ted Kremenek | 1b18ad2 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 176 | // Read the token flags. Are we at the start of the next line? |
| 177 | Token::TokenFlags y = (Token::TokenFlags) (uint8_t) p[1]; |
| 178 | if (y & Token::StartOfLine) break; |
| 179 | |
| 180 | // Skip to the next token. |
Alp Toker | 2584735 | 2014-07-07 14:01:37 +0000 | [diff] [blame] | 181 | p += StoredTokenSize; |
Ted Kremenek | 1b18ad2 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 182 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 183 | |
Ted Kremenek | 1b18ad2 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 184 | CurPtr = p; |
| 185 | } |
| 186 | |
Ted Kremenek | 56572ab | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 187 | /// SkipBlock - Used by Preprocessor to skip the current conditional block. |
| 188 | bool PTHLexer::SkipBlock() { |
Justin Bogner | 57ba0b2 | 2014-03-28 22:03:24 +0000 | [diff] [blame] | 189 | using namespace llvm::support; |
Ted Kremenek | 56572ab | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 190 | assert(CurPPCondPtr && "No cached PP conditional information."); |
| 191 | assert(LastHashTokPtr && "No known '#' token."); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 192 | |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 193 | const unsigned char *HashEntryI = nullptr; |
Ted Kremenek | 56572ab | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 194 | uint32_t TableIdx; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 195 | |
Ted Kremenek | 56572ab | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 196 | do { |
Ted Kremenek | 877556f | 2008-12-12 22:05:38 +0000 | [diff] [blame] | 197 | // Read the token offset from the side-table. |
Justin Bogner | 57ba0b2 | 2014-03-28 22:03:24 +0000 | [diff] [blame] | 198 | uint32_t Offset = endian::readNext<uint32_t, little, aligned>(CurPPCondPtr); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 199 | |
| 200 | // Read the target table index from the side-table. |
Justin Bogner | 57ba0b2 | 2014-03-28 22:03:24 +0000 | [diff] [blame] | 201 | TableIdx = endian::readNext<uint32_t, little, aligned>(CurPPCondPtr); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 202 | |
Ted Kremenek | 877556f | 2008-12-12 22:05:38 +0000 | [diff] [blame] | 203 | // Compute the actual memory address of the '#' token data for this entry. |
| 204 | HashEntryI = TokBuf + Offset; |
| 205 | |
| 206 | // Optmization: "Sibling jumping". #if...#else...#endif blocks can |
| 207 | // contain nested blocks. In the side-table we can jump over these |
| 208 | // nested blocks instead of doing a linear search if the next "sibling" |
| 209 | // entry is not at a location greater than LastHashTokPtr. |
| 210 | if (HashEntryI < LastHashTokPtr && TableIdx) { |
| 211 | // In the side-table we are still at an entry for a '#' token that |
| 212 | // is earlier than the last one we saw. Check if the location we would |
| 213 | // stride gets us closer. |
Chris Lattner | eb09754 | 2009-01-18 01:57:14 +0000 | [diff] [blame] | 214 | const unsigned char* NextPPCondPtr = |
| 215 | PPCond + TableIdx*(sizeof(uint32_t)*2); |
Ted Kremenek | 877556f | 2008-12-12 22:05:38 +0000 | [diff] [blame] | 216 | assert(NextPPCondPtr >= CurPPCondPtr); |
| 217 | // Read where we should jump to. |
Justin Bogner | 57ba0b2 | 2014-03-28 22:03:24 +0000 | [diff] [blame] | 218 | const unsigned char *HashEntryJ = |
| 219 | TokBuf + endian::readNext<uint32_t, little, aligned>(NextPPCondPtr); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 220 | |
Ted Kremenek | 877556f | 2008-12-12 22:05:38 +0000 | [diff] [blame] | 221 | if (HashEntryJ <= LastHashTokPtr) { |
| 222 | // Jump directly to the next entry in the side table. |
| 223 | HashEntryI = HashEntryJ; |
Justin Bogner | 57ba0b2 | 2014-03-28 22:03:24 +0000 | [diff] [blame] | 224 | TableIdx = endian::readNext<uint32_t, little, aligned>(NextPPCondPtr); |
Ted Kremenek | 877556f | 2008-12-12 22:05:38 +0000 | [diff] [blame] | 225 | CurPPCondPtr = NextPPCondPtr; |
| 226 | } |
| 227 | } |
Ted Kremenek | 56572ab | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 228 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 229 | while (HashEntryI < LastHashTokPtr); |
Ted Kremenek | 877556f | 2008-12-12 22:05:38 +0000 | [diff] [blame] | 230 | assert(HashEntryI == LastHashTokPtr && "No PP-cond entry found for '#'"); |
Ted Kremenek | 56572ab | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 231 | assert(TableIdx && "No jumping from #endifs."); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 232 | |
Ted Kremenek | 56572ab | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 233 | // Update our side-table iterator. |
Chris Lattner | eb09754 | 2009-01-18 01:57:14 +0000 | [diff] [blame] | 234 | const unsigned char* NextPPCondPtr = PPCond + TableIdx*(sizeof(uint32_t)*2); |
Ted Kremenek | 56572ab | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 235 | assert(NextPPCondPtr >= CurPPCondPtr); |
| 236 | CurPPCondPtr = NextPPCondPtr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 237 | |
Ted Kremenek | 56572ab | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 238 | // Read where we should jump to. |
Justin Bogner | 57ba0b2 | 2014-03-28 22:03:24 +0000 | [diff] [blame] | 239 | HashEntryI = |
| 240 | TokBuf + endian::readNext<uint32_t, little, aligned>(NextPPCondPtr); |
| 241 | uint32_t NextIdx = endian::readNext<uint32_t, little, aligned>(NextPPCondPtr); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 242 | |
Ted Kremenek | 56572ab | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 243 | // By construction NextIdx will be zero if this is a #endif. This is useful |
| 244 | // to know to obviate lexing another token. |
| 245 | bool isEndif = NextIdx == 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 246 | |
Ted Kremenek | 56572ab | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 247 | // This case can occur when we see something like this: |
| 248 | // |
| 249 | // #if ... |
| 250 | // /* a comment or nothing */ |
| 251 | // #elif |
| 252 | // |
| 253 | // If we are skipping the first #if block it will be the case that CurPtr |
| 254 | // already points 'elif'. Just return. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 255 | |
Ted Kremenek | 877556f | 2008-12-12 22:05:38 +0000 | [diff] [blame] | 256 | if (CurPtr > HashEntryI) { |
Alp Toker | 2584735 | 2014-07-07 14:01:37 +0000 | [diff] [blame] | 257 | assert(CurPtr == HashEntryI + StoredTokenSize); |
Ted Kremenek | 56572ab | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 258 | // Did we reach a #endif? If so, go ahead and consume that token as well. |
| 259 | if (isEndif) |
Alp Toker | 2584735 | 2014-07-07 14:01:37 +0000 | [diff] [blame] | 260 | CurPtr += StoredTokenSize * 2; |
Ted Kremenek | 56572ab | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 261 | else |
Ted Kremenek | 877556f | 2008-12-12 22:05:38 +0000 | [diff] [blame] | 262 | LastHashTokPtr = HashEntryI; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 263 | |
Ted Kremenek | 56572ab | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 264 | return isEndif; |
| 265 | } |
| 266 | |
| 267 | // Otherwise, we need to advance. Update CurPtr to point to the '#' token. |
Ted Kremenek | 877556f | 2008-12-12 22:05:38 +0000 | [diff] [blame] | 268 | CurPtr = HashEntryI; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 269 | |
Ted Kremenek | 56572ab | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 270 | // Update the location of the last observed '#'. This is useful if we |
| 271 | // are skipping multiple blocks. |
| 272 | LastHashTokPtr = CurPtr; |
Ted Kremenek | 56572ab | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 273 | |
Ted Kremenek | 1b18ad2 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 274 | // Skip the '#' token. |
Chris Lattner | eb09754 | 2009-01-18 01:57:14 +0000 | [diff] [blame] | 275 | assert(((tok::TokenKind)*CurPtr) == tok::hash); |
Alp Toker | 2584735 | 2014-07-07 14:01:37 +0000 | [diff] [blame] | 276 | CurPtr += StoredTokenSize; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 277 | |
Ted Kremenek | 56572ab | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 278 | // Did we reach a #endif? If so, go ahead and consume that token as well. |
Alp Toker | 2584735 | 2014-07-07 14:01:37 +0000 | [diff] [blame] | 279 | if (isEndif) { |
| 280 | CurPtr += StoredTokenSize * 2; |
| 281 | } |
Ted Kremenek | 56572ab | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 282 | |
| 283 | return isEndif; |
| 284 | } |
| 285 | |
Ted Kremenek | 63ff81c | 2008-12-17 23:36:32 +0000 | [diff] [blame] | 286 | SourceLocation PTHLexer::getSourceLocation() { |
Chris Lattner | 137d649 | 2009-01-18 02:10:31 +0000 | [diff] [blame] | 287 | // getSourceLocation is not on the hot path. It is used to get the location |
| 288 | // of the next token when transitioning back to this lexer when done |
Ted Kremenek | 63ff81c | 2008-12-17 23:36:32 +0000 | [diff] [blame] | 289 | // handling a #included file. Just read the necessary data from the token |
| 290 | // data buffer to construct the SourceLocation object. |
| 291 | // NOTE: This is a virtual function; hence it is defined out-of-line. |
Justin Bogner | 57ba0b2 | 2014-03-28 22:03:24 +0000 | [diff] [blame] | 292 | using namespace llvm::support; |
| 293 | |
Alp Toker | 2584735 | 2014-07-07 14:01:37 +0000 | [diff] [blame] | 294 | const unsigned char *OffsetPtr = CurPtr + (StoredTokenSize - 4); |
Justin Bogner | 57ba0b2 | 2014-03-28 22:03:24 +0000 | [diff] [blame] | 295 | uint32_t Offset = endian::readNext<uint32_t, little, aligned>(OffsetPtr); |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 296 | return FileStartLoc.getLocWithOffset(Offset); |
Ted Kremenek | 63ff81c | 2008-12-17 23:36:32 +0000 | [diff] [blame] | 297 | } |
| 298 | |
Ted Kremenek | 47b8cf6 | 2009-01-09 22:05:30 +0000 | [diff] [blame] | 299 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 86423a9 | 2009-02-10 22:16:22 +0000 | [diff] [blame] | 300 | // PTH file lookup: map from strings to file data. |
| 301 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 33eeabd | 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 | 337e3a5 | 2009-11-28 19:45:26 +0000 | [diff] [blame] | 307 | class PTHFileData { |
Ted Kremenek | 86423a9 | 2009-02-10 22:16:22 +0000 | [diff] [blame] | 308 | const uint32_t TokenOff; |
| 309 | const uint32_t PPCondOff; |
Ted Kremenek | 33eeabd | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 310 | public: |
Ted Kremenek | 86423a9 | 2009-02-10 22:16:22 +0000 | [diff] [blame] | 311 | PTHFileData(uint32_t tokenOff, uint32_t ppCondOff) |
| 312 | : TokenOff(tokenOff), PPCondOff(ppCondOff) {} |
Mike Stump | 11289f4 | 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 | 86423a9 | 2009-02-10 22:16:22 +0000 | [diff] [blame] | 316 | }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 317 | |
| 318 | |
Benjamin Kramer | 337e3a5 | 2009-11-28 19:45:26 +0000 | [diff] [blame] | 319 | class PTHFileLookupCommonTrait { |
Ted Kremenek | 86423a9 | 2009-02-10 22:16:22 +0000 | [diff] [blame] | 320 | public: |
Ted Kremenek | 29942a3 | 2009-02-13 19:13:46 +0000 | [diff] [blame] | 321 | typedef std::pair<unsigned char, const char*> internal_key_type; |
Justin Bogner | 25463f1 | 2014-04-18 20:27:24 +0000 | [diff] [blame] | 322 | typedef unsigned hash_value_type; |
| 323 | typedef unsigned offset_type; |
Ted Kremenek | 29942a3 | 2009-02-13 19:13:46 +0000 | [diff] [blame] | 324 | |
Justin Bogner | 25463f1 | 2014-04-18 20:27:24 +0000 | [diff] [blame] | 325 | static hash_value_type ComputeHash(internal_key_type x) { |
Daniel Dunbar | f8502d5 | 2009-10-17 23:52:28 +0000 | [diff] [blame] | 326 | return llvm::HashString(x.second); |
Ted Kremenek | 86423a9 | 2009-02-10 22:16:22 +0000 | [diff] [blame] | 327 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 328 | |
Ted Kremenek | 86423a9 | 2009-02-10 22:16:22 +0000 | [diff] [blame] | 329 | static std::pair<unsigned, unsigned> |
| 330 | ReadKeyDataLength(const unsigned char*& d) { |
Justin Bogner | 57ba0b2 | 2014-03-28 22:03:24 +0000 | [diff] [blame] | 331 | using namespace llvm::support; |
| 332 | unsigned keyLen = |
| 333 | (unsigned)endian::readNext<uint16_t, little, unaligned>(d); |
Ted Kremenek | 29942a3 | 2009-02-13 19:13:46 +0000 | [diff] [blame] | 334 | unsigned dataLen = (unsigned) *(d++); |
| 335 | return std::make_pair(keyLen, dataLen); |
Ted Kremenek | 33eeabd | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 336 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 337 | |
Ted Kremenek | 29942a3 | 2009-02-13 19:13:46 +0000 | [diff] [blame] | 338 | static internal_key_type ReadKey(const unsigned char* d, unsigned) { |
| 339 | unsigned char k = *(d++); // Read the entry kind. |
| 340 | return std::make_pair(k, (const char*) d); |
Ted Kremenek | 86423a9 | 2009-02-10 22:16:22 +0000 | [diff] [blame] | 341 | } |
Ted Kremenek | a5c2c27 | 2009-02-12 03:26:59 +0000 | [diff] [blame] | 342 | }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 343 | |
David Blaikie | 60e836b | 2014-08-29 22:04:40 +0000 | [diff] [blame] | 344 | } // end anonymous namespace |
| 345 | |
| 346 | class PTHManager::PTHFileLookupTrait : public PTHFileLookupCommonTrait { |
Ted Kremenek | a5c2c27 | 2009-02-12 03:26:59 +0000 | [diff] [blame] | 347 | public: |
| 348 | typedef const FileEntry* external_key_type; |
| 349 | typedef PTHFileData data_type; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 350 | |
Ted Kremenek | 29942a3 | 2009-02-13 19:13:46 +0000 | [diff] [blame] | 351 | static internal_key_type GetInternalKey(const FileEntry* FE) { |
| 352 | return std::make_pair((unsigned char) 0x1, FE->getName()); |
Ted Kremenek | a5c2c27 | 2009-02-12 03:26:59 +0000 | [diff] [blame] | 353 | } |
Ted Kremenek | 29942a3 | 2009-02-13 19:13:46 +0000 | [diff] [blame] | 354 | |
| 355 | static bool EqualKey(internal_key_type a, internal_key_type b) { |
| 356 | return a.first == b.first && strcmp(a.second, b.second) == 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | static PTHFileData ReadData(const internal_key_type& k, |
| 360 | const unsigned char* d, unsigned) { |
Ted Kremenek | 29942a3 | 2009-02-13 19:13:46 +0000 | [diff] [blame] | 361 | assert(k.first == 0x1 && "Only file lookups can match!"); |
Justin Bogner | 57ba0b2 | 2014-03-28 22:03:24 +0000 | [diff] [blame] | 362 | using namespace llvm::support; |
| 363 | uint32_t x = endian::readNext<uint32_t, little, unaligned>(d); |
| 364 | uint32_t y = endian::readNext<uint32_t, little, unaligned>(d); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 365 | return PTHFileData(x, y); |
Ted Kremenek | 33eeabd | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 366 | } |
| 367 | }; |
Ted Kremenek | e5554de | 2009-02-11 21:29:16 +0000 | [diff] [blame] | 368 | |
David Blaikie | 60e836b | 2014-08-29 22:04:40 +0000 | [diff] [blame] | 369 | class PTHManager::PTHStringLookupTrait { |
Ted Kremenek | e5554de | 2009-02-11 21:29:16 +0000 | [diff] [blame] | 370 | public: |
Justin Bogner | 25463f1 | 2014-04-18 20:27:24 +0000 | [diff] [blame] | 371 | typedef uint32_t data_type; |
| 372 | typedef const std::pair<const char*, unsigned> external_key_type; |
Ted Kremenek | e5554de | 2009-02-11 21:29:16 +0000 | [diff] [blame] | 373 | typedef external_key_type internal_key_type; |
Justin Bogner | 25463f1 | 2014-04-18 20:27:24 +0000 | [diff] [blame] | 374 | typedef uint32_t hash_value_type; |
| 375 | typedef unsigned offset_type; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 376 | |
Ted Kremenek | e5554de | 2009-02-11 21:29:16 +0000 | [diff] [blame] | 377 | static bool EqualKey(const internal_key_type& a, |
| 378 | const internal_key_type& b) { |
| 379 | return (a.second == b.second) ? memcmp(a.first, b.first, a.second) == 0 |
| 380 | : false; |
| 381 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 382 | |
Justin Bogner | 25463f1 | 2014-04-18 20:27:24 +0000 | [diff] [blame] | 383 | static hash_value_type ComputeHash(const internal_key_type& a) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 384 | return llvm::HashString(StringRef(a.first, a.second)); |
Ted Kremenek | e5554de | 2009-02-11 21:29:16 +0000 | [diff] [blame] | 385 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 386 | |
Ted Kremenek | e5554de | 2009-02-11 21:29:16 +0000 | [diff] [blame] | 387 | // This hopefully will just get inlined and removed by the optimizer. |
| 388 | static const internal_key_type& |
| 389 | GetInternalKey(const external_key_type& x) { return x; } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 390 | |
Ted Kremenek | e5554de | 2009-02-11 21:29:16 +0000 | [diff] [blame] | 391 | static std::pair<unsigned, unsigned> |
| 392 | ReadKeyDataLength(const unsigned char*& d) { |
Justin Bogner | 57ba0b2 | 2014-03-28 22:03:24 +0000 | [diff] [blame] | 393 | using namespace llvm::support; |
| 394 | return std::make_pair( |
| 395 | (unsigned)endian::readNext<uint16_t, little, unaligned>(d), |
| 396 | sizeof(uint32_t)); |
Ted Kremenek | e5554de | 2009-02-11 21:29:16 +0000 | [diff] [blame] | 397 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 398 | |
Ted Kremenek | e5554de | 2009-02-11 21:29:16 +0000 | [diff] [blame] | 399 | static std::pair<const char*, unsigned> |
| 400 | ReadKey(const unsigned char* d, unsigned n) { |
| 401 | assert(n >= 2 && d[n-1] == '\0'); |
| 402 | return std::make_pair((const char*) d, n-1); |
| 403 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 404 | |
Ted Kremenek | 29942a3 | 2009-02-13 19:13:46 +0000 | [diff] [blame] | 405 | static uint32_t ReadData(const internal_key_type& k, const unsigned char* d, |
| 406 | unsigned) { |
Justin Bogner | 57ba0b2 | 2014-03-28 22:03:24 +0000 | [diff] [blame] | 407 | using namespace llvm::support; |
| 408 | return endian::readNext<uint32_t, little, unaligned>(d); |
Ted Kremenek | e5554de | 2009-02-11 21:29:16 +0000 | [diff] [blame] | 409 | } |
| 410 | }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 411 | |
Ted Kremenek | 33eeabd | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 412 | //===----------------------------------------------------------------------===// |
| 413 | // PTHManager methods. |
| 414 | //===----------------------------------------------------------------------===// |
| 415 | |
David Blaikie | 02118ce | 2014-08-29 22:04:45 +0000 | [diff] [blame] | 416 | PTHManager::PTHManager( |
| 417 | std::unique_ptr<const llvm::MemoryBuffer> buf, |
| 418 | std::unique_ptr<PTHFileLookup> fileLookup, const unsigned char *idDataTable, |
| 419 | std::unique_ptr<IdentifierInfo *[], llvm::FreeDeleter> perIDCache, |
| 420 | std::unique_ptr<PTHStringIdLookup> stringIdLookup, unsigned numIds, |
| 421 | const unsigned char *spellingBase, const char *originalSourceFile) |
| 422 | : Buf(std::move(buf)), PerIDCache(std::move(perIDCache)), |
David Blaikie | 60e836b | 2014-08-29 22:04:40 +0000 | [diff] [blame] | 423 | FileLookup(std::move(fileLookup)), IdDataTable(idDataTable), |
| 424 | StringIdLookup(std::move(stringIdLookup)), NumIds(numIds), PP(nullptr), |
| 425 | SpellingBase(spellingBase), OriginalSourceFile(originalSourceFile) {} |
Ted Kremenek | 33eeabd | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 426 | |
| 427 | PTHManager::~PTHManager() { |
Ted Kremenek | 33eeabd | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 428 | } |
| 429 | |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 430 | static void InvalidPTH(DiagnosticsEngine &Diags, const char *Msg) { |
Alp Toker | 29cb66b | 2014-01-26 06:17:37 +0000 | [diff] [blame] | 431 | Diags.Report(Diags.getCustomDiagID(DiagnosticsEngine::Error, "%0")) << Msg; |
Ted Kremenek | 62224c1 | 2009-01-28 21:02:43 +0000 | [diff] [blame] | 432 | } |
| 433 | |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 434 | PTHManager *PTHManager::Create(const std::string &file, |
| 435 | DiagnosticsEngine &Diags) { |
Ted Kremenek | 33eeabd | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 436 | // Memory map the PTH file. |
Rafael Espindola | 2d2b420 | 2014-07-06 17:43:24 +0000 | [diff] [blame] | 437 | llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> FileOrErr = |
| 438 | llvm::MemoryBuffer::getFile(file); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 439 | |
Rafael Espindola | 2d2b420 | 2014-07-06 17:43:24 +0000 | [diff] [blame] | 440 | if (!FileOrErr) { |
Michael J. Spencer | f25faaa | 2010-12-09 17:36:38 +0000 | [diff] [blame] | 441 | // FIXME: Add ec.message() to this diag. |
Daniel Dunbar | 1a54e3f | 2009-11-12 02:53:48 +0000 | [diff] [blame] | 442 | Diags.Report(diag::err_invalid_pth_file) << file; |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 443 | return nullptr; |
Ted Kremenek | 3b0589e | 2009-01-28 20:49:33 +0000 | [diff] [blame] | 444 | } |
Rafael Espindola | 2d2b420 | 2014-07-06 17:43:24 +0000 | [diff] [blame] | 445 | std::unique_ptr<llvm::MemoryBuffer> File = std::move(FileOrErr.get()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 446 | |
Justin Bogner | 57ba0b2 | 2014-03-28 22:03:24 +0000 | [diff] [blame] | 447 | using namespace llvm::support; |
| 448 | |
Ted Kremenek | 33eeabd | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 449 | // Get the buffer ranges and check if there are at least three 32-bit |
| 450 | // words at the end of the file. |
Roman Divacky | e637711 | 2012-09-06 15:59:27 +0000 | [diff] [blame] | 451 | const unsigned char *BufBeg = (const unsigned char*)File->getBufferStart(); |
| 452 | const unsigned char *BufEnd = (const unsigned char*)File->getBufferEnd(); |
Ted Kremenek | eb8c8fb | 2009-01-26 21:43:14 +0000 | [diff] [blame] | 453 | |
| 454 | // Check the prologue of the file. |
Richard Smith | 2683b4c | 2012-08-17 03:55:43 +0000 | [diff] [blame] | 455 | if ((BufEnd - BufBeg) < (signed)(sizeof("cfe-pth") + 4 + 4) || |
| 456 | memcmp(BufBeg, "cfe-pth", sizeof("cfe-pth")) != 0) { |
Daniel Dunbar | 1a54e3f | 2009-11-12 02:53:48 +0000 | [diff] [blame] | 457 | Diags.Report(diag::err_invalid_pth_file) << file; |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 458 | return nullptr; |
Ted Kremenek | 62224c1 | 2009-01-28 21:02:43 +0000 | [diff] [blame] | 459 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 460 | |
Ted Kremenek | 978b5be | 2009-01-26 21:50:21 +0000 | [diff] [blame] | 461 | // Read the PTH version. |
Richard Smith | 2683b4c | 2012-08-17 03:55:43 +0000 | [diff] [blame] | 462 | const unsigned char *p = BufBeg + (sizeof("cfe-pth")); |
Justin Bogner | 57ba0b2 | 2014-03-28 22:03:24 +0000 | [diff] [blame] | 463 | unsigned Version = endian::readNext<uint32_t, little, aligned>(p); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 464 | |
Daniel Dunbar | 1a54e3f | 2009-11-12 02:53:48 +0000 | [diff] [blame] | 465 | if (Version < PTHManager::Version) { |
| 466 | InvalidPTH(Diags, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 467 | Version < PTHManager::Version |
Ted Kremenek | 62224c1 | 2009-01-28 21:02:43 +0000 | [diff] [blame] | 468 | ? "PTH file uses an older PTH format that is no longer supported" |
| 469 | : "PTH file uses a newer PTH format that cannot be read"); |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 470 | return nullptr; |
Ted Kremenek | 62224c1 | 2009-01-28 21:02:43 +0000 | [diff] [blame] | 471 | } |
Ted Kremenek | 978b5be | 2009-01-26 21:50:21 +0000 | [diff] [blame] | 472 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 473 | // Compute the address of the index table at the end of the PTH file. |
Ted Kremenek | 4c1d41f | 2009-02-11 23:34:32 +0000 | [diff] [blame] | 474 | const unsigned char *PrologueOffset = p; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 475 | |
Ted Kremenek | 4c1d41f | 2009-02-11 23:34:32 +0000 | [diff] [blame] | 476 | if (PrologueOffset >= BufEnd) { |
Daniel Dunbar | 1a54e3f | 2009-11-12 02:53:48 +0000 | [diff] [blame] | 477 | Diags.Report(diag::err_invalid_pth_file) << file; |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 478 | return nullptr; |
Ted Kremenek | 62224c1 | 2009-01-28 21:02:43 +0000 | [diff] [blame] | 479 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 480 | |
Ted Kremenek | 33eeabd | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 481 | // Construct the file lookup table. This will be used for mapping from |
| 482 | // FileEntry*'s to cached tokens. |
Ted Kremenek | 4c1d41f | 2009-02-11 23:34:32 +0000 | [diff] [blame] | 483 | const unsigned char* FileTableOffset = PrologueOffset + sizeof(uint32_t)*2; |
Justin Bogner | 57ba0b2 | 2014-03-28 22:03:24 +0000 | [diff] [blame] | 484 | const unsigned char *FileTable = |
| 485 | BufBeg + endian::readNext<uint32_t, little, aligned>(FileTableOffset); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 486 | |
Ted Kremenek | 33eeabd | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 487 | if (!(FileTable > BufBeg && FileTable < BufEnd)) { |
Daniel Dunbar | 1a54e3f | 2009-11-12 02:53:48 +0000 | [diff] [blame] | 488 | Diags.Report(diag::err_invalid_pth_file) << file; |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 489 | return nullptr; // FIXME: Proper error diagnostic? |
Ted Kremenek | 33eeabd | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 490 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 491 | |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 492 | std::unique_ptr<PTHFileLookup> FL(PTHFileLookup::Create(FileTable, BufBeg)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 493 | |
Ted Kremenek | 80e239a | 2009-03-20 17:54:25 +0000 | [diff] [blame] | 494 | // Warn if the PTH file is empty. We still want to create a PTHManager |
| 495 | // as the PTH could be used with -include-pth. |
| 496 | if (FL->isEmpty()) |
Daniel Dunbar | 1a54e3f | 2009-11-12 02:53:48 +0000 | [diff] [blame] | 497 | InvalidPTH(Diags, "PTH file contains no cached source data"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 498 | |
Ted Kremenek | 33eeabd | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 499 | // Get the location of the table mapping from persistent ids to the |
| 500 | // data needed to reconstruct identifiers. |
Ted Kremenek | 4c1d41f | 2009-02-11 23:34:32 +0000 | [diff] [blame] | 501 | const unsigned char* IDTableOffset = PrologueOffset + sizeof(uint32_t)*0; |
Justin Bogner | 57ba0b2 | 2014-03-28 22:03:24 +0000 | [diff] [blame] | 502 | const unsigned char *IData = |
| 503 | BufBeg + endian::readNext<uint32_t, little, aligned>(IDTableOffset); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 504 | |
Ted Kremenek | 8d6c828 | 2009-01-21 07:34:28 +0000 | [diff] [blame] | 505 | if (!(IData >= BufBeg && IData < BufEnd)) { |
Daniel Dunbar | 1a54e3f | 2009-11-12 02:53:48 +0000 | [diff] [blame] | 506 | Diags.Report(diag::err_invalid_pth_file) << file; |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 507 | return nullptr; |
Ted Kremenek | 33eeabd | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 508 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 509 | |
Ted Kremenek | e5554de | 2009-02-11 21:29:16 +0000 | [diff] [blame] | 510 | // Get the location of the hashtable mapping between strings and |
| 511 | // persistent IDs. |
Ted Kremenek | 4c1d41f | 2009-02-11 23:34:32 +0000 | [diff] [blame] | 512 | const unsigned char* StringIdTableOffset = PrologueOffset + sizeof(uint32_t)*1; |
Justin Bogner | 57ba0b2 | 2014-03-28 22:03:24 +0000 | [diff] [blame] | 513 | const unsigned char *StringIdTable = |
| 514 | BufBeg + endian::readNext<uint32_t, little, aligned>(StringIdTableOffset); |
Ted Kremenek | e5554de | 2009-02-11 21:29:16 +0000 | [diff] [blame] | 515 | if (!(StringIdTable >= BufBeg && StringIdTable < BufEnd)) { |
Daniel Dunbar | 1a54e3f | 2009-11-12 02:53:48 +0000 | [diff] [blame] | 516 | Diags.Report(diag::err_invalid_pth_file) << file; |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 517 | return nullptr; |
Ted Kremenek | a705b04 | 2009-01-15 18:47:46 +0000 | [diff] [blame] | 518 | } |
Ted Kremenek | e5554de | 2009-02-11 21:29:16 +0000 | [diff] [blame] | 519 | |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 520 | std::unique_ptr<PTHStringIdLookup> SL( |
| 521 | PTHStringIdLookup::Create(StringIdTable, BufBeg)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 522 | |
Ted Kremenek | 8d178f4 | 2009-01-27 00:01:05 +0000 | [diff] [blame] | 523 | // Get the location of the spelling cache. |
Ted Kremenek | 4c1d41f | 2009-02-11 23:34:32 +0000 | [diff] [blame] | 524 | const unsigned char* spellingBaseOffset = PrologueOffset + sizeof(uint32_t)*3; |
Justin Bogner | 57ba0b2 | 2014-03-28 22:03:24 +0000 | [diff] [blame] | 525 | const unsigned char *spellingBase = |
| 526 | BufBeg + endian::readNext<uint32_t, little, aligned>(spellingBaseOffset); |
Ted Kremenek | 8d178f4 | 2009-01-27 00:01:05 +0000 | [diff] [blame] | 527 | if (!(spellingBase >= BufBeg && spellingBase < BufEnd)) { |
Daniel Dunbar | 1a54e3f | 2009-11-12 02:53:48 +0000 | [diff] [blame] | 528 | Diags.Report(diag::err_invalid_pth_file) << file; |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 529 | return nullptr; |
Ted Kremenek | 8d178f4 | 2009-01-27 00:01:05 +0000 | [diff] [blame] | 530 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 531 | |
Ted Kremenek | 73a4d28 | 2008-12-03 01:16:39 +0000 | [diff] [blame] | 532 | // Get the number of IdentifierInfos and pre-allocate the identifier cache. |
Justin Bogner | 57ba0b2 | 2014-03-28 22:03:24 +0000 | [diff] [blame] | 533 | uint32_t NumIds = endian::readNext<uint32_t, little, aligned>(IData); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 534 | |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 535 | // Pre-allocate the persistent ID -> IdentifierInfo* cache. We use calloc() |
Ted Kremenek | 73a4d28 | 2008-12-03 01:16:39 +0000 | [diff] [blame] | 536 | // so that we in the best case only zero out memory once when the OS returns |
| 537 | // us new pages. |
David Blaikie | 02118ce | 2014-08-29 22:04:45 +0000 | [diff] [blame] | 538 | std::unique_ptr<IdentifierInfo *[], llvm::FreeDeleter> PerIDCache; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 539 | |
Ted Kremenek | 8d6c828 | 2009-01-21 07:34:28 +0000 | [diff] [blame] | 540 | if (NumIds) { |
David Blaikie | 02118ce | 2014-08-29 22:04:45 +0000 | [diff] [blame] | 541 | PerIDCache.reset((IdentifierInfo **)calloc(NumIds, sizeof(PerIDCache[0]))); |
Ted Kremenek | 8d6c828 | 2009-01-21 07:34:28 +0000 | [diff] [blame] | 542 | if (!PerIDCache) { |
Daniel Dunbar | 1a54e3f | 2009-11-12 02:53:48 +0000 | [diff] [blame] | 543 | InvalidPTH(Diags, "Could not allocate memory for processing PTH file"); |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 544 | return nullptr; |
Ted Kremenek | 8d6c828 | 2009-01-21 07:34:28 +0000 | [diff] [blame] | 545 | } |
Ted Kremenek | 73a4d28 | 2008-12-03 01:16:39 +0000 | [diff] [blame] | 546 | } |
Ted Kremenek | 8d6c828 | 2009-01-21 07:34:28 +0000 | [diff] [blame] | 547 | |
Ted Kremenek | f9ccd5c | 2009-03-19 22:19:30 +0000 | [diff] [blame] | 548 | // Compute the address of the original source file. |
| 549 | const unsigned char* originalSourceBase = PrologueOffset + sizeof(uint32_t)*4; |
Justin Bogner | 57ba0b2 | 2014-03-28 22:03:24 +0000 | [diff] [blame] | 550 | unsigned len = |
| 551 | endian::readNext<uint16_t, little, unaligned>(originalSourceBase); |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 552 | if (!len) originalSourceBase = nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 553 | |
Ted Kremenek | a705b04 | 2009-01-15 18:47:46 +0000 | [diff] [blame] | 554 | // Create the new PTHManager. |
David Blaikie | 02118ce | 2014-08-29 22:04:45 +0000 | [diff] [blame] | 555 | return new PTHManager(std::move(File), std::move(FL), IData, |
| 556 | std::move(PerIDCache), std::move(SL), NumIds, |
| 557 | spellingBase, (const char *)originalSourceBase); |
Ted Kremenek | 33eeabd | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 558 | } |
Ted Kremenek | f9ccd5c | 2009-03-19 22:19:30 +0000 | [diff] [blame] | 559 | |
Chris Lattner | 144aacd | 2009-01-18 02:57:21 +0000 | [diff] [blame] | 560 | IdentifierInfo* PTHManager::LazilyCreateIdentifierInfo(unsigned PersistentID) { |
Justin Bogner | 57ba0b2 | 2014-03-28 22:03:24 +0000 | [diff] [blame] | 561 | using namespace llvm::support; |
Ted Kremenek | 33eeabd | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 562 | // Look in the PTH file for the string data for the IdentifierInfo object. |
Chris Lattner | 144aacd | 2009-01-18 02:57:21 +0000 | [diff] [blame] | 563 | const unsigned char* TableEntry = IdDataTable + sizeof(uint32_t)*PersistentID; |
Justin Bogner | 57ba0b2 | 2014-03-28 22:03:24 +0000 | [diff] [blame] | 564 | const unsigned char *IDData = |
| 565 | (const unsigned char *)Buf->getBufferStart() + |
| 566 | endian::readNext<uint32_t, little, aligned>(TableEntry); |
Chris Lattner | eb09754 | 2009-01-18 01:57:14 +0000 | [diff] [blame] | 567 | assert(IDData < (const unsigned char*)Buf->getBufferEnd()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 568 | |
Ted Kremenek | a705b04 | 2009-01-15 18:47:46 +0000 | [diff] [blame] | 569 | // Allocate the object. |
Chris Lattner | eb09754 | 2009-01-18 01:57:14 +0000 | [diff] [blame] | 570 | std::pair<IdentifierInfo,const unsigned char*> *Mem = |
| 571 | Alloc.Allocate<std::pair<IdentifierInfo,const unsigned char*> >(); |
Ted Kremenek | a705b04 | 2009-01-15 18:47:46 +0000 | [diff] [blame] | 572 | |
| 573 | Mem->second = IDData; |
Ted Kremenek | e5554de | 2009-02-11 21:29:16 +0000 | [diff] [blame] | 574 | assert(IDData[0] != '\0'); |
Ted Kremenek | 52f73ca | 2009-01-20 23:28:34 +0000 | [diff] [blame] | 575 | IdentifierInfo *II = new ((void*) Mem) IdentifierInfo(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 576 | |
Ted Kremenek | a705b04 | 2009-01-15 18:47:46 +0000 | [diff] [blame] | 577 | // Store the new IdentifierInfo in the cache. |
Chris Lattner | 144aacd | 2009-01-18 02:57:21 +0000 | [diff] [blame] | 578 | PerIDCache[PersistentID] = II; |
Daniel Dunbar | 2c422dc9 | 2009-10-18 20:26:12 +0000 | [diff] [blame] | 579 | assert(II->getNameStart() && II->getNameStart()[0] != '\0'); |
Ted Kremenek | 33eeabd | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 580 | return II; |
| 581 | } |
| 582 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 583 | IdentifierInfo* PTHManager::get(StringRef Name) { |
Ted Kremenek | e5554de | 2009-02-11 21:29:16 +0000 | [diff] [blame] | 584 | // Double check our assumption that the last character isn't '\0'. |
David Blaikie | 441417a | 2011-09-22 01:35:49 +0000 | [diff] [blame] | 585 | assert(Name.empty() || Name.back() != '\0'); |
David Blaikie | 60e836b | 2014-08-29 22:04:40 +0000 | [diff] [blame] | 586 | PTHStringIdLookup::iterator I = |
| 587 | StringIdLookup->find(std::make_pair(Name.data(), Name.size())); |
| 588 | if (I == StringIdLookup->end()) // No identifier found? |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 589 | return nullptr; |
Ted Kremenek | a705b04 | 2009-01-15 18:47:46 +0000 | [diff] [blame] | 590 | |
Ted Kremenek | e5554de | 2009-02-11 21:29:16 +0000 | [diff] [blame] | 591 | // Match found. Return the identifier! |
| 592 | assert(*I > 0); |
| 593 | return GetIdentifierInfo(*I-1); |
| 594 | } |
Ted Kremenek | a705b04 | 2009-01-15 18:47:46 +0000 | [diff] [blame] | 595 | |
Chris Lattner | ab1d4b8 | 2009-01-17 08:06:50 +0000 | [diff] [blame] | 596 | PTHLexer *PTHManager::CreateLexer(FileID FID) { |
| 597 | const FileEntry *FE = PP->getSourceManager().getFileEntryForID(FID); |
Ted Kremenek | 33eeabd | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 598 | if (!FE) |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 599 | return nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 600 | |
Justin Bogner | 57ba0b2 | 2014-03-28 22:03:24 +0000 | [diff] [blame] | 601 | using namespace llvm::support; |
| 602 | |
Ted Kremenek | 33eeabd | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 603 | // Lookup the FileEntry object in our file lookup data structure. It will |
| 604 | // return a variant that indicates whether or not there is an offset within |
| 605 | // the PTH file that contains cached tokens. |
David Blaikie | 60e836b | 2014-08-29 22:04:40 +0000 | [diff] [blame] | 606 | PTHFileLookup::iterator I = FileLookup->find(FE); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 607 | |
David Blaikie | 60e836b | 2014-08-29 22:04:40 +0000 | [diff] [blame] | 608 | if (I == FileLookup->end()) // No tokens available? |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 609 | return nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 610 | |
| 611 | const PTHFileData& FileData = *I; |
| 612 | |
Chris Lattner | eb09754 | 2009-01-18 01:57:14 +0000 | [diff] [blame] | 613 | const unsigned char *BufStart = (const unsigned char *)Buf->getBufferStart(); |
Ted Kremenek | 33eeabd | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 614 | // Compute the offset of the token data within the buffer. |
Chris Lattner | eb09754 | 2009-01-18 01:57:14 +0000 | [diff] [blame] | 615 | const unsigned char* data = BufStart + FileData.getTokenOffset(); |
Ted Kremenek | 56572ab | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 616 | |
| 617 | // Get the location of pp-conditional table. |
Chris Lattner | eb09754 | 2009-01-18 01:57:14 +0000 | [diff] [blame] | 618 | const unsigned char* ppcond = BufStart + FileData.getPPCondOffset(); |
Justin Bogner | 57ba0b2 | 2014-03-28 22:03:24 +0000 | [diff] [blame] | 619 | uint32_t Len = endian::readNext<uint32_t, little, aligned>(ppcond); |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 620 | if (Len == 0) ppcond = nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 621 | |
Ted Kremenek | a705b04 | 2009-01-15 18:47:46 +0000 | [diff] [blame] | 622 | assert(PP && "No preprocessor set yet!"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 623 | return new PTHLexer(*PP, FID, data, ppcond, *this); |
Ted Kremenek | 33eeabd | 2008-12-03 00:38:03 +0000 | [diff] [blame] | 624 | } |
Ted Kremenek | a5c2c27 | 2009-02-12 03:26:59 +0000 | [diff] [blame] | 625 | |
| 626 | //===----------------------------------------------------------------------===// |
| 627 | // 'stat' caching. |
| 628 | //===----------------------------------------------------------------------===// |
| 629 | |
| 630 | namespace { |
Benjamin Kramer | 337e3a5 | 2009-11-28 19:45:26 +0000 | [diff] [blame] | 631 | class PTHStatData { |
Ted Kremenek | a5c2c27 | 2009-02-12 03:26:59 +0000 | [diff] [blame] | 632 | public: |
Rafael Espindola | f8f91b8 | 2013-08-01 21:42:11 +0000 | [diff] [blame] | 633 | const bool HasData; |
| 634 | uint64_t Size; |
| 635 | time_t ModTime; |
| 636 | llvm::sys::fs::UniqueID UniqueID; |
| 637 | bool IsDirectory; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 638 | |
Rafael Espindola | f8f91b8 | 2013-08-01 21:42:11 +0000 | [diff] [blame] | 639 | PTHStatData(uint64_t Size, time_t ModTime, llvm::sys::fs::UniqueID UniqueID, |
| 640 | bool IsDirectory) |
| 641 | : HasData(true), Size(Size), ModTime(ModTime), UniqueID(UniqueID), |
| 642 | IsDirectory(IsDirectory) {} |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 643 | |
Rafael Espindola | f8f91b8 | 2013-08-01 21:42:11 +0000 | [diff] [blame] | 644 | PTHStatData() : HasData(false) {} |
Ted Kremenek | a5c2c27 | 2009-02-12 03:26:59 +0000 | [diff] [blame] | 645 | }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 646 | |
Benjamin Kramer | 337e3a5 | 2009-11-28 19:45:26 +0000 | [diff] [blame] | 647 | class PTHStatLookupTrait : public PTHFileLookupCommonTrait { |
Ted Kremenek | a5c2c27 | 2009-02-12 03:26:59 +0000 | [diff] [blame] | 648 | public: |
Ted Kremenek | 29942a3 | 2009-02-13 19:13:46 +0000 | [diff] [blame] | 649 | typedef const char* external_key_type; // const char* |
Ted Kremenek | a5c2c27 | 2009-02-12 03:26:59 +0000 | [diff] [blame] | 650 | typedef PTHStatData data_type; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 651 | |
Ted Kremenek | 29942a3 | 2009-02-13 19:13:46 +0000 | [diff] [blame] | 652 | static internal_key_type GetInternalKey(const char *path) { |
| 653 | // The key 'kind' doesn't matter here because it is ignored in EqualKey. |
| 654 | return std::make_pair((unsigned char) 0x0, path); |
| 655 | } |
| 656 | |
| 657 | static bool EqualKey(internal_key_type a, internal_key_type b) { |
| 658 | // When doing 'stat' lookups we don't care about the kind of 'a' and 'b', |
| 659 | // just the paths. |
| 660 | return strcmp(a.second, b.second) == 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 661 | } |
| 662 | |
Ted Kremenek | 29942a3 | 2009-02-13 19:13:46 +0000 | [diff] [blame] | 663 | static data_type ReadData(const internal_key_type& k, const unsigned char* d, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 664 | unsigned) { |
| 665 | |
Ted Kremenek | 2fd18ec | 2009-02-13 22:07:44 +0000 | [diff] [blame] | 666 | if (k.first /* File or Directory */) { |
Rafael Espindola | f8f91b8 | 2013-08-01 21:42:11 +0000 | [diff] [blame] | 667 | bool IsDirectory = true; |
| 668 | if (k.first == 0x1 /* File */) { |
| 669 | IsDirectory = false; |
| 670 | d += 4 * 2; // Skip the first 2 words. |
| 671 | } |
| 672 | |
Justin Bogner | 57ba0b2 | 2014-03-28 22:03:24 +0000 | [diff] [blame] | 673 | using namespace llvm::support; |
| 674 | |
| 675 | uint64_t File = endian::readNext<uint64_t, little, unaligned>(d); |
| 676 | uint64_t Device = endian::readNext<uint64_t, little, unaligned>(d); |
Sylvestre Ledru | 0ae4074 | 2014-08-18 15:00:11 +0000 | [diff] [blame] | 677 | llvm::sys::fs::UniqueID UniqueID(Device, File); |
Justin Bogner | 57ba0b2 | 2014-03-28 22:03:24 +0000 | [diff] [blame] | 678 | time_t ModTime = endian::readNext<uint64_t, little, unaligned>(d); |
| 679 | uint64_t Size = endian::readNext<uint64_t, little, unaligned>(d); |
Rafael Espindola | f8f91b8 | 2013-08-01 21:42:11 +0000 | [diff] [blame] | 680 | return data_type(Size, ModTime, UniqueID, IsDirectory); |
Ted Kremenek | 29942a3 | 2009-02-13 19:13:46 +0000 | [diff] [blame] | 681 | } |
Ted Kremenek | 2fd18ec | 2009-02-13 22:07:44 +0000 | [diff] [blame] | 682 | |
| 683 | // Negative stat. Don't read anything. |
Ted Kremenek | 29942a3 | 2009-02-13 19:13:46 +0000 | [diff] [blame] | 684 | return data_type(); |
Ted Kremenek | a5c2c27 | 2009-02-12 03:26:59 +0000 | [diff] [blame] | 685 | } |
| 686 | }; |
David Blaikie | 60e836b | 2014-08-29 22:04:40 +0000 | [diff] [blame] | 687 | } // end anonymous namespace |
Ted Kremenek | a5c2c27 | 2009-02-12 03:26:59 +0000 | [diff] [blame] | 688 | |
David Blaikie | 60e836b | 2014-08-29 22:04:40 +0000 | [diff] [blame] | 689 | namespace clang { |
Chris Lattner | 226efd3 | 2010-11-23 19:19:34 +0000 | [diff] [blame] | 690 | class PTHStatCache : public FileSystemStatCache { |
Justin Bogner | bb094f0 | 2014-04-18 19:57:06 +0000 | [diff] [blame] | 691 | typedef llvm::OnDiskChainedHashTable<PTHStatLookupTrait> CacheTy; |
Ted Kremenek | a5c2c27 | 2009-02-12 03:26:59 +0000 | [diff] [blame] | 692 | CacheTy Cache; |
| 693 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 694 | public: |
David Blaikie | 60e836b | 2014-08-29 22:04:40 +0000 | [diff] [blame] | 695 | PTHStatCache(PTHManager::PTHFileLookup &FL) |
| 696 | : Cache(FL.getNumBuckets(), FL.getNumEntries(), FL.getBuckets(), |
| 697 | FL.getBase()) {} |
Ted Kremenek | a5c2c27 | 2009-02-12 03:26:59 +0000 | [diff] [blame] | 698 | |
Rafael Espindola | f8f91b8 | 2013-08-01 21:42:11 +0000 | [diff] [blame] | 699 | LookupResult getStat(const char *Path, FileData &Data, bool isFile, |
David Blaikie | 326ffb3 | 2014-07-08 15:46:02 +0000 | [diff] [blame] | 700 | std::unique_ptr<vfs::File> *F, |
| 701 | vfs::FileSystem &FS) override { |
Ted Kremenek | a5c2c27 | 2009-02-12 03:26:59 +0000 | [diff] [blame] | 702 | // Do the lookup for the file's data in the PTH file. |
Chris Lattner | 226efd3 | 2010-11-23 19:19:34 +0000 | [diff] [blame] | 703 | CacheTy::iterator I = Cache.find(Path); |
Ted Kremenek | a5c2c27 | 2009-02-12 03:26:59 +0000 | [diff] [blame] | 704 | |
| 705 | // If we don't get a hit in the PTH file just forward to 'stat'. |
Kovarththanan Rajaratnam | 661a309 | 2010-03-12 08:23:34 +0000 | [diff] [blame] | 706 | if (I == Cache.end()) |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 707 | return statChained(Path, Data, isFile, F, FS); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 708 | |
Rafael Espindola | f8f91b8 | 2013-08-01 21:42:11 +0000 | [diff] [blame] | 709 | const PTHStatData &D = *I; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 710 | |
Rafael Espindola | f8f91b8 | 2013-08-01 21:42:11 +0000 | [diff] [blame] | 711 | if (!D.HasData) |
Chris Lattner | 8f0583d | 2010-11-23 20:05:15 +0000 | [diff] [blame] | 712 | return CacheMissing; |
Ted Kremenek | 2fd18ec | 2009-02-13 22:07:44 +0000 | [diff] [blame] | 713 | |
Ben Langmuir | d066d4c | 2014-02-28 21:16:07 +0000 | [diff] [blame] | 714 | Data.Name = Path; |
Rafael Espindola | f8f91b8 | 2013-08-01 21:42:11 +0000 | [diff] [blame] | 715 | Data.Size = D.Size; |
| 716 | Data.ModTime = D.ModTime; |
| 717 | Data.UniqueID = D.UniqueID; |
| 718 | Data.IsDirectory = D.IsDirectory; |
| 719 | Data.IsNamedPipe = false; |
| 720 | Data.InPCH = true; |
| 721 | |
Chris Lattner | 8f0583d | 2010-11-23 20:05:15 +0000 | [diff] [blame] | 722 | return CacheExists; |
Ted Kremenek | a5c2c27 | 2009-02-12 03:26:59 +0000 | [diff] [blame] | 723 | } |
| 724 | }; |
David Blaikie | 60e836b | 2014-08-29 22:04:40 +0000 | [diff] [blame] | 725 | } |
Ted Kremenek | a5c2c27 | 2009-02-12 03:26:59 +0000 | [diff] [blame] | 726 | |
David Blaikie | 23430cc | 2014-08-11 21:29:24 +0000 | [diff] [blame] | 727 | std::unique_ptr<FileSystemStatCache> PTHManager::createStatCache() { |
David Blaikie | 60e836b | 2014-08-29 22:04:40 +0000 | [diff] [blame] | 728 | return llvm::make_unique<PTHStatCache>(*FileLookup); |
Ted Kremenek | a5c2c27 | 2009-02-12 03:26:59 +0000 | [diff] [blame] | 729 | } |