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