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