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