2008-11-13 05:37:15 +08:00
|
|
|
//===--- PTHLexer.cpp - Lex from a token stream ---------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the PTHLexer interface.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/Lex/PTHLexer.h"
|
2008-12-03 08:38:03 +08:00
|
|
|
#include "clang/Basic/FileManager.h"
|
2010-11-24 03:19:34 +08:00
|
|
|
#include "clang/Basic/FileSystemStatCache.h"
|
2008-12-03 08:38:03 +08:00
|
|
|
#include "clang/Basic/IdentifierTable.h"
|
2009-04-20 15:08:21 +08:00
|
|
|
#include "clang/Basic/OnDiskHashTable.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/Basic/TokenKinds.h"
|
2009-11-12 10:53:48 +08:00
|
|
|
#include "clang/Lex/LexDiagnostic.h"
|
2008-12-03 08:38:03 +08:00
|
|
|
#include "clang/Lex/PTHManager.h"
|
|
|
|
#include "clang/Lex/Preprocessor.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/Lex/Token.h"
|
2008-12-03 08:38:03 +08:00
|
|
|
#include "llvm/ADT/OwningPtr.h"
|
2009-10-18 07:52:28 +08:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
|
|
|
#include "llvm/ADT/StringMap.h"
|
2009-01-23 07:50:07 +08:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2010-12-10 01:36:38 +08:00
|
|
|
#include "llvm/Support/system_error.h"
|
2008-11-13 05:37:15 +08:00
|
|
|
using namespace clang;
|
2009-04-20 15:08:21 +08:00
|
|
|
using namespace clang::io;
|
2008-11-13 05:37:15 +08:00
|
|
|
|
2009-01-20 07:13:15 +08:00
|
|
|
#define DISK_TOKEN_SIZE (1+1+2+4+4)
|
2008-12-13 02:34:08 +08:00
|
|
|
|
2008-12-23 09:30:52 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// PTHLexer methods.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-01-18 09:57:14 +08:00
|
|
|
PTHLexer::PTHLexer(Preprocessor &PP, FileID FID, const unsigned char *D,
|
2009-01-27 08:01:05 +08:00
|
|
|
const unsigned char *ppcond, PTHManager &PM)
|
2009-01-17 14:22:33 +08:00
|
|
|
: PreprocessorLexer(&PP, FID), TokBuf(D), CurPtr(D), LastHashTokPtr(0),
|
2009-01-27 08:01:05 +08:00
|
|
|
PPCond(ppcond), CurPPCondPtr(ppcond), PTHMgr(PM) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-01-17 14:22:33 +08:00
|
|
|
FileStartLoc = PP.getSourceManager().getLocForStartOfFile(FID);
|
2009-01-10 06:05:30 +08:00
|
|
|
}
|
2008-11-13 05:37:15 +08:00
|
|
|
|
2013-09-19 08:41:32 +08:00
|
|
|
bool PTHLexer::Lex(Token& Tok) {
|
2008-12-23 10:30:15 +08:00
|
|
|
//===--------------------------------------==//
|
|
|
|
// Read the raw token data.
|
|
|
|
//===--------------------------------------==//
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-12-23 10:30:15 +08:00
|
|
|
// Shadow CurPtr into an automatic variable.
|
2009-09-09 23:08:12 +08:00
|
|
|
const unsigned char *CurPtrShadow = CurPtr;
|
2008-12-23 10:30:15 +08:00
|
|
|
|
2009-01-18 10:10:31 +08:00
|
|
|
// Read in the data for the token.
|
2009-01-23 03:48:26 +08:00
|
|
|
unsigned Word0 = ReadLE32(CurPtrShadow);
|
|
|
|
uint32_t IdentifierID = ReadLE32(CurPtrShadow);
|
|
|
|
uint32_t FileOffset = ReadLE32(CurPtrShadow);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-01-20 07:13:15 +08:00
|
|
|
tok::TokenKind TKind = (tok::TokenKind) (Word0 & 0xFF);
|
|
|
|
Token::TokenFlags TFlags = (Token::TokenFlags) ((Word0 >> 8) & 0xFF);
|
2009-01-21 15:21:56 +08:00
|
|
|
uint32_t Len = Word0 >> 16;
|
2009-01-20 07:13:15 +08:00
|
|
|
|
2009-01-21 15:21:56 +08:00
|
|
|
CurPtr = CurPtrShadow;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-12-23 10:30:15 +08:00
|
|
|
//===--------------------------------------==//
|
|
|
|
// Construct the token itself.
|
|
|
|
//===--------------------------------------==//
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-12-23 10:30:15 +08:00
|
|
|
Tok.startToken();
|
2009-01-18 10:34:01 +08:00
|
|
|
Tok.setKind(TKind);
|
|
|
|
Tok.setFlag(TFlags);
|
2008-12-24 03:24:24 +08:00
|
|
|
assert(!LexingRawMode);
|
2011-09-20 04:40:19 +08:00
|
|
|
Tok.setLocation(FileStartLoc.getLocWithOffset(FileOffset));
|
2008-12-23 10:30:15 +08:00
|
|
|
Tok.setLength(Len);
|
2008-11-21 03:49:00 +08:00
|
|
|
|
2009-01-21 15:50:06 +08:00
|
|
|
// Handle identifiers.
|
2009-01-27 08:01:05 +08:00
|
|
|
if (Tok.isLiteral()) {
|
|
|
|
Tok.setLiteralData((const char*) (PTHMgr.SpellingBase + IdentifierID));
|
|
|
|
}
|
|
|
|
else if (IdentifierID) {
|
2009-01-21 15:50:06 +08:00
|
|
|
MIOpt.ReadToken();
|
|
|
|
IdentifierInfo *II = PTHMgr.GetIdentifierInfo(IdentifierID-1);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-01-21 15:50:06 +08:00
|
|
|
Tok.setIdentifierInfo(II);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-01-24 02:35:48 +08:00
|
|
|
// Change the kind of this identifier to the appropriate token kind, e.g.
|
|
|
|
// turning "for" into a keyword.
|
|
|
|
Tok.setKind(II->getTokenID());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-01-21 15:50:06 +08:00
|
|
|
if (II->isHandleIdentifierCase())
|
2013-09-19 08:41:32 +08:00
|
|
|
return PP->HandleIdentifier(Tok);
|
|
|
|
|
|
|
|
return true;
|
2009-01-21 15:50:06 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-12-23 10:30:15 +08:00
|
|
|
//===--------------------------------------==//
|
|
|
|
// Process the token.
|
|
|
|
//===--------------------------------------==//
|
2009-01-18 10:34:01 +08:00
|
|
|
if (TKind == tok::eof) {
|
2008-12-23 09:30:52 +08:00
|
|
|
// Save the end-of-file token.
|
|
|
|
EofToken = Tok;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-12-24 03:24:24 +08:00
|
|
|
assert(!ParsingPreprocessorDirective);
|
|
|
|
assert(!LexingRawMode);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2013-09-19 08:41:32 +08:00
|
|
|
return LexEndOfFile(Tok);
|
2008-11-21 08:58:35 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-01-18 10:34:01 +08:00
|
|
|
if (TKind == tok::hash && Tok.isAtStartOfLine()) {
|
2008-12-24 03:24:24 +08:00
|
|
|
LastHashTokPtr = CurPtr - DISK_TOKEN_SIZE;
|
|
|
|
assert(!LexingRawMode);
|
|
|
|
PP->HandleDirective(Tok);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2013-09-19 08:41:32 +08:00
|
|
|
return false;
|
2008-12-23 09:30:52 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-02-28 10:37:51 +08:00
|
|
|
if (TKind == tok::eod) {
|
2008-12-24 03:24:24 +08:00
|
|
|
assert(ParsingPreprocessorDirective);
|
|
|
|
ParsingPreprocessorDirective = false;
|
2013-09-19 08:41:32 +08:00
|
|
|
return true;
|
2008-12-24 03:24:24 +08:00
|
|
|
}
|
2008-11-13 05:37:15 +08:00
|
|
|
|
2008-12-24 03:24:24 +08:00
|
|
|
MIOpt.ReadToken();
|
2013-09-19 08:41:32 +08:00
|
|
|
return true;
|
2008-11-21 08:58:35 +08:00
|
|
|
}
|
|
|
|
|
2010-07-27 10:59:02 +08:00
|
|
|
bool PTHLexer::LexEndOfFile(Token &Result) {
|
|
|
|
// If we hit the end of the file while parsing a preprocessor directive,
|
|
|
|
// end the preprocessor directive first. The next token returned will
|
|
|
|
// then be the end of file.
|
|
|
|
if (ParsingPreprocessorDirective) {
|
|
|
|
ParsingPreprocessorDirective = false; // Done parsing the "line".
|
|
|
|
return true; // Have a token.
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(!LexingRawMode);
|
|
|
|
|
|
|
|
// If we are in a #if directive, emit an error.
|
|
|
|
while (!ConditionalStack.empty()) {
|
2011-09-04 11:32:15 +08:00
|
|
|
if (PP->getCodeCompletionFileLoc() != FileStartLoc)
|
2010-08-13 01:04:55 +08:00
|
|
|
PP->Diag(ConditionalStack.back().IfLoc,
|
|
|
|
diag::err_pp_unterminated_conditional);
|
2010-07-27 10:59:02 +08:00
|
|
|
ConditionalStack.pop_back();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, let the preprocessor handle this.
|
|
|
|
return PP->HandleEndOfFile(Result);
|
|
|
|
}
|
|
|
|
|
2008-12-23 09:30:52 +08:00
|
|
|
// FIXME: We can just grab the last token instead of storing a copy
|
|
|
|
// into EofToken.
|
2008-12-24 03:24:24 +08:00
|
|
|
void PTHLexer::getEOF(Token& Tok) {
|
2009-01-09 08:36:11 +08:00
|
|
|
assert(EofToken.is(tok::eof));
|
2008-12-03 08:38:03 +08:00
|
|
|
Tok = EofToken;
|
2008-11-13 05:37:15 +08:00
|
|
|
}
|
2008-11-20 06:21:33 +08:00
|
|
|
|
|
|
|
void PTHLexer::DiscardToEndOfLine() {
|
|
|
|
assert(ParsingPreprocessorDirective && ParsingFilename == false &&
|
|
|
|
"Must be in a preprocessing directive!");
|
2008-11-20 09:16:50 +08:00
|
|
|
|
2008-12-23 09:30:52 +08:00
|
|
|
// We assume that if the preprocessor wishes to discard to the end of
|
|
|
|
// the line that it also means to end the current preprocessor directive.
|
|
|
|
ParsingPreprocessorDirective = false;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-12-18 07:52:11 +08:00
|
|
|
// Skip tokens by only peeking at their token kind and the flags.
|
|
|
|
// We don't need to actually reconstruct full tokens from the token buffer.
|
|
|
|
// This saves some copies and it also reduces IdentifierInfo* lookup.
|
2009-01-18 09:57:14 +08:00
|
|
|
const unsigned char* p = CurPtr;
|
2008-12-18 07:52:11 +08:00
|
|
|
while (1) {
|
|
|
|
// Read the token kind. Are we at the end of the file?
|
|
|
|
tok::TokenKind x = (tok::TokenKind) (uint8_t) *p;
|
|
|
|
if (x == tok::eof) break;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-12-18 07:52:11 +08:00
|
|
|
// Read the token flags. Are we at the start of the next line?
|
|
|
|
Token::TokenFlags y = (Token::TokenFlags) (uint8_t) p[1];
|
2008-12-19 02:15:29 +08:00
|
|
|
if (y & Token::StartOfLine) break;
|
2008-12-18 07:52:11 +08:00
|
|
|
|
|
|
|
// Skip to the next token.
|
|
|
|
p += DISK_TOKEN_SIZE;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-12-18 07:52:11 +08:00
|
|
|
CurPtr = p;
|
2008-11-20 06:21:33 +08:00
|
|
|
}
|
2008-12-03 08:38:03 +08:00
|
|
|
|
2008-12-13 02:34:08 +08:00
|
|
|
/// SkipBlock - Used by Preprocessor to skip the current conditional block.
|
|
|
|
bool PTHLexer::SkipBlock() {
|
|
|
|
assert(CurPPCondPtr && "No cached PP conditional information.");
|
|
|
|
assert(LastHashTokPtr && "No known '#' token.");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-01-18 09:57:14 +08:00
|
|
|
const unsigned char* HashEntryI = 0;
|
2008-12-13 02:34:08 +08:00
|
|
|
uint32_t TableIdx;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-12-13 02:34:08 +08:00
|
|
|
do {
|
2008-12-13 06:05:38 +08:00
|
|
|
// Read the token offset from the side-table.
|
2012-09-16 14:18:45 +08:00
|
|
|
uint32_t Offset = ReadLE32(CurPPCondPtr);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
// Read the target table index from the side-table.
|
2009-01-23 03:48:26 +08:00
|
|
|
TableIdx = ReadLE32(CurPPCondPtr);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-12-13 06:05:38 +08:00
|
|
|
// Compute the actual memory address of the '#' token data for this entry.
|
|
|
|
HashEntryI = TokBuf + Offset;
|
|
|
|
|
|
|
|
// Optmization: "Sibling jumping". #if...#else...#endif blocks can
|
|
|
|
// contain nested blocks. In the side-table we can jump over these
|
|
|
|
// nested blocks instead of doing a linear search if the next "sibling"
|
|
|
|
// entry is not at a location greater than LastHashTokPtr.
|
|
|
|
if (HashEntryI < LastHashTokPtr && TableIdx) {
|
|
|
|
// In the side-table we are still at an entry for a '#' token that
|
|
|
|
// is earlier than the last one we saw. Check if the location we would
|
|
|
|
// stride gets us closer.
|
2009-01-18 09:57:14 +08:00
|
|
|
const unsigned char* NextPPCondPtr =
|
|
|
|
PPCond + TableIdx*(sizeof(uint32_t)*2);
|
2008-12-13 06:05:38 +08:00
|
|
|
assert(NextPPCondPtr >= CurPPCondPtr);
|
|
|
|
// Read where we should jump to.
|
2012-09-16 14:18:45 +08:00
|
|
|
const unsigned char* HashEntryJ = TokBuf + ReadLE32(NextPPCondPtr);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-12-13 06:05:38 +08:00
|
|
|
if (HashEntryJ <= LastHashTokPtr) {
|
|
|
|
// Jump directly to the next entry in the side table.
|
|
|
|
HashEntryI = HashEntryJ;
|
2009-01-23 03:48:26 +08:00
|
|
|
TableIdx = ReadLE32(NextPPCondPtr);
|
2008-12-13 06:05:38 +08:00
|
|
|
CurPPCondPtr = NextPPCondPtr;
|
|
|
|
}
|
|
|
|
}
|
2008-12-13 02:34:08 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
while (HashEntryI < LastHashTokPtr);
|
2008-12-13 06:05:38 +08:00
|
|
|
assert(HashEntryI == LastHashTokPtr && "No PP-cond entry found for '#'");
|
2008-12-13 02:34:08 +08:00
|
|
|
assert(TableIdx && "No jumping from #endifs.");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-12-13 02:34:08 +08:00
|
|
|
// Update our side-table iterator.
|
2009-01-18 09:57:14 +08:00
|
|
|
const unsigned char* NextPPCondPtr = PPCond + TableIdx*(sizeof(uint32_t)*2);
|
2008-12-13 02:34:08 +08:00
|
|
|
assert(NextPPCondPtr >= CurPPCondPtr);
|
|
|
|
CurPPCondPtr = NextPPCondPtr;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-12-13 02:34:08 +08:00
|
|
|
// Read where we should jump to.
|
2009-01-23 03:48:26 +08:00
|
|
|
HashEntryI = TokBuf + ReadLE32(NextPPCondPtr);
|
|
|
|
uint32_t NextIdx = ReadLE32(NextPPCondPtr);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-12-13 02:34:08 +08:00
|
|
|
// By construction NextIdx will be zero if this is a #endif. This is useful
|
|
|
|
// to know to obviate lexing another token.
|
|
|
|
bool isEndif = NextIdx == 0;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-12-13 02:34:08 +08:00
|
|
|
// This case can occur when we see something like this:
|
|
|
|
//
|
|
|
|
// #if ...
|
|
|
|
// /* a comment or nothing */
|
|
|
|
// #elif
|
|
|
|
//
|
|
|
|
// If we are skipping the first #if block it will be the case that CurPtr
|
|
|
|
// already points 'elif'. Just return.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-12-13 06:05:38 +08:00
|
|
|
if (CurPtr > HashEntryI) {
|
|
|
|
assert(CurPtr == HashEntryI + DISK_TOKEN_SIZE);
|
2008-12-13 02:34:08 +08:00
|
|
|
// Did we reach a #endif? If so, go ahead and consume that token as well.
|
|
|
|
if (isEndif)
|
2008-12-23 09:30:52 +08:00
|
|
|
CurPtr += DISK_TOKEN_SIZE*2;
|
2008-12-13 02:34:08 +08:00
|
|
|
else
|
2008-12-13 06:05:38 +08:00
|
|
|
LastHashTokPtr = HashEntryI;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-12-13 02:34:08 +08:00
|
|
|
return isEndif;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, we need to advance. Update CurPtr to point to the '#' token.
|
2008-12-13 06:05:38 +08:00
|
|
|
CurPtr = HashEntryI;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-12-13 02:34:08 +08:00
|
|
|
// Update the location of the last observed '#'. This is useful if we
|
|
|
|
// are skipping multiple blocks.
|
|
|
|
LastHashTokPtr = CurPtr;
|
|
|
|
|
2008-12-23 09:30:52 +08:00
|
|
|
// Skip the '#' token.
|
2009-01-18 09:57:14 +08:00
|
|
|
assert(((tok::TokenKind)*CurPtr) == tok::hash);
|
2008-12-23 09:30:52 +08:00
|
|
|
CurPtr += DISK_TOKEN_SIZE;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-12-13 02:34:08 +08:00
|
|
|
// Did we reach a #endif? If so, go ahead and consume that token as well.
|
2008-12-23 09:30:52 +08:00
|
|
|
if (isEndif) { CurPtr += DISK_TOKEN_SIZE*2; }
|
2008-12-13 02:34:08 +08:00
|
|
|
|
|
|
|
return isEndif;
|
|
|
|
}
|
|
|
|
|
2008-12-18 07:36:32 +08:00
|
|
|
SourceLocation PTHLexer::getSourceLocation() {
|
2009-01-18 10:10:31 +08:00
|
|
|
// getSourceLocation is not on the hot path. It is used to get the location
|
|
|
|
// of the next token when transitioning back to this lexer when done
|
2008-12-18 07:36:32 +08:00
|
|
|
// handling a #included file. Just read the necessary data from the token
|
|
|
|
// data buffer to construct the SourceLocation object.
|
|
|
|
// NOTE: This is a virtual function; hence it is defined out-of-line.
|
2009-01-22 06:41:38 +08:00
|
|
|
const unsigned char *OffsetPtr = CurPtr + (DISK_TOKEN_SIZE - 4);
|
2009-01-23 03:48:26 +08:00
|
|
|
uint32_t Offset = ReadLE32(OffsetPtr);
|
2011-09-20 04:40:19 +08:00
|
|
|
return FileStartLoc.getLocWithOffset(Offset);
|
2008-12-18 07:36:32 +08:00
|
|
|
}
|
|
|
|
|
2009-02-11 06:16:22 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// PTH file lookup: map from strings to file data.
|
|
|
|
//===----------------------------------------------------------------------===//
|
2008-12-03 08:38:03 +08:00
|
|
|
|
|
|
|
/// PTHFileLookup - This internal data structure is used by the PTHManager
|
|
|
|
/// to map from FileEntry objects managed by FileManager to offsets within
|
|
|
|
/// the PTH file.
|
|
|
|
namespace {
|
2009-11-29 03:45:26 +08:00
|
|
|
class PTHFileData {
|
2009-02-11 06:16:22 +08:00
|
|
|
const uint32_t TokenOff;
|
|
|
|
const uint32_t PPCondOff;
|
2008-12-03 08:38:03 +08:00
|
|
|
public:
|
2009-02-11 06:16:22 +08:00
|
|
|
PTHFileData(uint32_t tokenOff, uint32_t ppCondOff)
|
|
|
|
: TokenOff(tokenOff), PPCondOff(ppCondOff) {}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
uint32_t getTokenOffset() const { return TokenOff; }
|
|
|
|
uint32_t getPPCondOffset() const { return PPCondOff; }
|
2009-02-11 06:16:22 +08:00
|
|
|
};
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
|
2009-11-29 03:45:26 +08:00
|
|
|
class PTHFileLookupCommonTrait {
|
2008-12-03 08:38:03 +08:00
|
|
|
public:
|
2009-02-14 03:13:46 +08:00
|
|
|
typedef std::pair<unsigned char, const char*> internal_key_type;
|
|
|
|
|
|
|
|
static unsigned ComputeHash(internal_key_type x) {
|
2009-10-18 07:52:28 +08:00
|
|
|
return llvm::HashString(x.second);
|
2009-02-11 06:16:22 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-11 06:16:22 +08:00
|
|
|
static std::pair<unsigned, unsigned>
|
|
|
|
ReadKeyDataLength(const unsigned char*& d) {
|
2009-02-14 03:13:46 +08:00
|
|
|
unsigned keyLen = (unsigned) ReadUnalignedLE16(d);
|
|
|
|
unsigned dataLen = (unsigned) *(d++);
|
|
|
|
return std::make_pair(keyLen, dataLen);
|
2008-12-03 08:38:03 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-14 03:13:46 +08:00
|
|
|
static internal_key_type ReadKey(const unsigned char* d, unsigned) {
|
|
|
|
unsigned char k = *(d++); // Read the entry kind.
|
|
|
|
return std::make_pair(k, (const char*) d);
|
2009-02-11 06:16:22 +08:00
|
|
|
}
|
2009-02-12 11:26:59 +08:00
|
|
|
};
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-11-29 03:45:26 +08:00
|
|
|
class PTHFileLookupTrait : public PTHFileLookupCommonTrait {
|
2009-02-12 11:26:59 +08:00
|
|
|
public:
|
|
|
|
typedef const FileEntry* external_key_type;
|
|
|
|
typedef PTHFileData data_type;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-14 03:13:46 +08:00
|
|
|
static internal_key_type GetInternalKey(const FileEntry* FE) {
|
|
|
|
return std::make_pair((unsigned char) 0x1, FE->getName());
|
2009-02-12 11:26:59 +08:00
|
|
|
}
|
2009-02-14 03:13:46 +08:00
|
|
|
|
|
|
|
static bool EqualKey(internal_key_type a, internal_key_type b) {
|
|
|
|
return a.first == b.first && strcmp(a.second, b.second) == 0;
|
2009-09-09 23:08:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static PTHFileData ReadData(const internal_key_type& k,
|
|
|
|
const unsigned char* d, unsigned) {
|
2009-02-14 03:13:46 +08:00
|
|
|
assert(k.first == 0x1 && "Only file lookups can match!");
|
2009-02-11 06:16:22 +08:00
|
|
|
uint32_t x = ::ReadUnalignedLE32(d);
|
|
|
|
uint32_t y = ::ReadUnalignedLE32(d);
|
2009-09-09 23:08:12 +08:00
|
|
|
return PTHFileData(x, y);
|
2008-12-03 08:38:03 +08:00
|
|
|
}
|
|
|
|
};
|
2009-02-12 05:29:16 +08:00
|
|
|
|
2009-11-29 03:45:26 +08:00
|
|
|
class PTHStringLookupTrait {
|
2009-02-12 05:29:16 +08:00
|
|
|
public:
|
2009-09-09 23:08:12 +08:00
|
|
|
typedef uint32_t
|
2009-02-12 05:29:16 +08:00
|
|
|
data_type;
|
|
|
|
|
|
|
|
typedef const std::pair<const char*, unsigned>
|
|
|
|
external_key_type;
|
|
|
|
|
|
|
|
typedef external_key_type internal_key_type;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-12 05:29:16 +08:00
|
|
|
static bool EqualKey(const internal_key_type& a,
|
|
|
|
const internal_key_type& b) {
|
|
|
|
return (a.second == b.second) ? memcmp(a.first, b.first, a.second) == 0
|
|
|
|
: false;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-12 05:29:16 +08:00
|
|
|
static unsigned ComputeHash(const internal_key_type& a) {
|
2011-07-23 18:55:15 +08:00
|
|
|
return llvm::HashString(StringRef(a.first, a.second));
|
2009-02-12 05:29:16 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-12 05:29:16 +08:00
|
|
|
// This hopefully will just get inlined and removed by the optimizer.
|
|
|
|
static const internal_key_type&
|
|
|
|
GetInternalKey(const external_key_type& x) { return x; }
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-12 05:29:16 +08:00
|
|
|
static std::pair<unsigned, unsigned>
|
|
|
|
ReadKeyDataLength(const unsigned char*& d) {
|
|
|
|
return std::make_pair((unsigned) ReadUnalignedLE16(d), sizeof(uint32_t));
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-12 05:29:16 +08:00
|
|
|
static std::pair<const char*, unsigned>
|
|
|
|
ReadKey(const unsigned char* d, unsigned n) {
|
|
|
|
assert(n >= 2 && d[n-1] == '\0');
|
|
|
|
return std::make_pair((const char*) d, n-1);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-14 03:13:46 +08:00
|
|
|
static uint32_t ReadData(const internal_key_type& k, const unsigned char* d,
|
|
|
|
unsigned) {
|
2009-02-12 05:29:16 +08:00
|
|
|
return ::ReadUnalignedLE32(d);
|
|
|
|
}
|
|
|
|
};
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
} // end anonymous namespace
|
2009-02-11 06:16:22 +08:00
|
|
|
|
2009-02-12 05:29:16 +08:00
|
|
|
typedef OnDiskChainedHashTable<PTHFileLookupTrait> PTHFileLookup;
|
|
|
|
typedef OnDiskChainedHashTable<PTHStringLookupTrait> PTHStringIdLookup;
|
2008-12-03 08:38:03 +08:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// PTHManager methods.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
PTHManager::PTHManager(const llvm::MemoryBuffer* buf, void* fileLookup,
|
2009-01-18 09:57:14 +08:00
|
|
|
const unsigned char* idDataTable,
|
2009-09-09 23:08:12 +08:00
|
|
|
IdentifierInfo** perIDCache,
|
2009-02-12 05:29:16 +08:00
|
|
|
void* stringIdLookup, unsigned numIds,
|
2009-03-20 06:19:30 +08:00
|
|
|
const unsigned char* spellingBase,
|
|
|
|
const char* originalSourceFile)
|
2008-12-03 09:16:39 +08:00
|
|
|
: Buf(buf), PerIDCache(perIDCache), FileLookup(fileLookup),
|
2009-02-12 05:29:16 +08:00
|
|
|
IdDataTable(idDataTable), StringIdLookup(stringIdLookup),
|
2009-03-20 06:19:30 +08:00
|
|
|
NumIds(numIds), PP(0), SpellingBase(spellingBase),
|
|
|
|
OriginalSourceFile(originalSourceFile) {}
|
2008-12-03 08:38:03 +08:00
|
|
|
|
|
|
|
PTHManager::~PTHManager() {
|
|
|
|
delete Buf;
|
|
|
|
delete (PTHFileLookup*) FileLookup;
|
2009-02-12 05:29:16 +08:00
|
|
|
delete (PTHStringIdLookup*) StringIdLookup;
|
2008-12-05 06:47:11 +08:00
|
|
|
free(PerIDCache);
|
2008-12-03 08:38:03 +08:00
|
|
|
}
|
|
|
|
|
2011-09-26 07:23:43 +08:00
|
|
|
static void InvalidPTH(DiagnosticsEngine &Diags, const char *Msg) {
|
2014-01-26 14:17:37 +08:00
|
|
|
Diags.Report(Diags.getCustomDiagID(DiagnosticsEngine::Error, "%0")) << Msg;
|
2009-01-29 05:02:43 +08:00
|
|
|
}
|
|
|
|
|
2011-09-26 07:23:43 +08:00
|
|
|
PTHManager *PTHManager::Create(const std::string &file,
|
|
|
|
DiagnosticsEngine &Diags) {
|
2008-12-03 08:38:03 +08:00
|
|
|
// Memory map the PTH file.
|
2012-02-05 10:12:40 +08:00
|
|
|
OwningPtr<llvm::MemoryBuffer> File;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-12-16 11:28:14 +08:00
|
|
|
if (llvm::MemoryBuffer::getFile(file, File)) {
|
2010-12-10 01:36:38 +08:00
|
|
|
// FIXME: Add ec.message() to this diag.
|
2009-11-12 10:53:48 +08:00
|
|
|
Diags.Report(diag::err_invalid_pth_file) << file;
|
2008-12-03 08:38:03 +08:00
|
|
|
return 0;
|
2009-01-29 04:49:33 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-12-03 08:38:03 +08:00
|
|
|
// Get the buffer ranges and check if there are at least three 32-bit
|
|
|
|
// words at the end of the file.
|
2012-09-06 23:59:27 +08:00
|
|
|
const unsigned char *BufBeg = (const unsigned char*)File->getBufferStart();
|
|
|
|
const unsigned char *BufEnd = (const unsigned char*)File->getBufferEnd();
|
2009-01-27 05:43:14 +08:00
|
|
|
|
|
|
|
// Check the prologue of the file.
|
2012-08-17 11:55:43 +08:00
|
|
|
if ((BufEnd - BufBeg) < (signed)(sizeof("cfe-pth") + 4 + 4) ||
|
|
|
|
memcmp(BufBeg, "cfe-pth", sizeof("cfe-pth")) != 0) {
|
2009-11-12 10:53:48 +08:00
|
|
|
Diags.Report(diag::err_invalid_pth_file) << file;
|
2009-01-27 05:43:14 +08:00
|
|
|
return 0;
|
2009-01-29 05:02:43 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-01-27 05:50:21 +08:00
|
|
|
// Read the PTH version.
|
2012-08-17 11:55:43 +08:00
|
|
|
const unsigned char *p = BufBeg + (sizeof("cfe-pth"));
|
2009-01-27 05:50:21 +08:00
|
|
|
unsigned Version = ReadLE32(p);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-11-12 10:53:48 +08:00
|
|
|
if (Version < PTHManager::Version) {
|
|
|
|
InvalidPTH(Diags,
|
2009-09-09 23:08:12 +08:00
|
|
|
Version < PTHManager::Version
|
2009-01-29 05:02:43 +08:00
|
|
|
? "PTH file uses an older PTH format that is no longer supported"
|
|
|
|
: "PTH file uses a newer PTH format that cannot be read");
|
2009-01-27 05:50:21 +08:00
|
|
|
return 0;
|
2009-01-29 05:02:43 +08:00
|
|
|
}
|
2009-01-27 05:50:21 +08:00
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
// Compute the address of the index table at the end of the PTH file.
|
2009-02-12 07:34:32 +08:00
|
|
|
const unsigned char *PrologueOffset = p;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-12 07:34:32 +08:00
|
|
|
if (PrologueOffset >= BufEnd) {
|
2009-11-12 10:53:48 +08:00
|
|
|
Diags.Report(diag::err_invalid_pth_file) << file;
|
2009-01-27 05:43:14 +08:00
|
|
|
return 0;
|
2009-01-29 05:02:43 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-12-03 08:38:03 +08:00
|
|
|
// Construct the file lookup table. This will be used for mapping from
|
|
|
|
// FileEntry*'s to cached tokens.
|
2009-02-12 07:34:32 +08:00
|
|
|
const unsigned char* FileTableOffset = PrologueOffset + sizeof(uint32_t)*2;
|
2009-01-23 03:48:26 +08:00
|
|
|
const unsigned char* FileTable = BufBeg + ReadLE32(FileTableOffset);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-12-03 08:38:03 +08:00
|
|
|
if (!(FileTable > BufBeg && FileTable < BufEnd)) {
|
2009-11-12 10:53:48 +08:00
|
|
|
Diags.Report(diag::err_invalid_pth_file) << file;
|
2008-12-03 08:38:03 +08:00
|
|
|
return 0; // FIXME: Proper error diagnostic?
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2012-02-05 10:12:40 +08:00
|
|
|
OwningPtr<PTHFileLookup> FL(PTHFileLookup::Create(FileTable, BufBeg));
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-21 01:54:25 +08:00
|
|
|
// Warn if the PTH file is empty. We still want to create a PTHManager
|
|
|
|
// as the PTH could be used with -include-pth.
|
|
|
|
if (FL->isEmpty())
|
2009-11-12 10:53:48 +08:00
|
|
|
InvalidPTH(Diags, "PTH file contains no cached source data");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-12-03 08:38:03 +08:00
|
|
|
// Get the location of the table mapping from persistent ids to the
|
|
|
|
// data needed to reconstruct identifiers.
|
2009-02-12 07:34:32 +08:00
|
|
|
const unsigned char* IDTableOffset = PrologueOffset + sizeof(uint32_t)*0;
|
2009-01-23 03:48:26 +08:00
|
|
|
const unsigned char* IData = BufBeg + ReadLE32(IDTableOffset);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-01-21 15:34:28 +08:00
|
|
|
if (!(IData >= BufBeg && IData < BufEnd)) {
|
2009-11-12 10:53:48 +08:00
|
|
|
Diags.Report(diag::err_invalid_pth_file) << file;
|
2009-01-29 05:02:43 +08:00
|
|
|
return 0;
|
2008-12-03 08:38:03 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-12 05:29:16 +08:00
|
|
|
// Get the location of the hashtable mapping between strings and
|
|
|
|
// persistent IDs.
|
2009-02-12 07:34:32 +08:00
|
|
|
const unsigned char* StringIdTableOffset = PrologueOffset + sizeof(uint32_t)*1;
|
2009-02-12 05:29:16 +08:00
|
|
|
const unsigned char* StringIdTable = BufBeg + ReadLE32(StringIdTableOffset);
|
|
|
|
if (!(StringIdTable >= BufBeg && StringIdTable < BufEnd)) {
|
2009-11-12 10:53:48 +08:00
|
|
|
Diags.Report(diag::err_invalid_pth_file) << file;
|
2009-01-29 05:02:43 +08:00
|
|
|
return 0;
|
2009-01-16 02:47:46 +08:00
|
|
|
}
|
2009-02-12 05:29:16 +08:00
|
|
|
|
2012-02-05 10:12:40 +08:00
|
|
|
OwningPtr<PTHStringIdLookup> SL(PTHStringIdLookup::Create(StringIdTable,
|
2009-02-12 05:29:16 +08:00
|
|
|
BufBeg));
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-01-27 08:01:05 +08:00
|
|
|
// Get the location of the spelling cache.
|
2009-02-12 07:34:32 +08:00
|
|
|
const unsigned char* spellingBaseOffset = PrologueOffset + sizeof(uint32_t)*3;
|
2009-01-27 08:01:05 +08:00
|
|
|
const unsigned char* spellingBase = BufBeg + ReadLE32(spellingBaseOffset);
|
|
|
|
if (!(spellingBase >= BufBeg && spellingBase < BufEnd)) {
|
2009-11-12 10:53:48 +08:00
|
|
|
Diags.Report(diag::err_invalid_pth_file) << file;
|
2009-01-27 08:01:05 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-12-03 09:16:39 +08:00
|
|
|
// Get the number of IdentifierInfos and pre-allocate the identifier cache.
|
2009-01-23 03:48:26 +08:00
|
|
|
uint32_t NumIds = ReadLE32(IData);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-04-15 13:22:18 +08:00
|
|
|
// Pre-allocate the persistent ID -> IdentifierInfo* cache. We use calloc()
|
2008-12-03 09:16:39 +08:00
|
|
|
// so that we in the best case only zero out memory once when the OS returns
|
|
|
|
// us new pages.
|
2009-01-21 15:34:28 +08:00
|
|
|
IdentifierInfo** PerIDCache = 0;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-01-21 15:34:28 +08:00
|
|
|
if (NumIds) {
|
2009-09-09 23:08:12 +08:00
|
|
|
PerIDCache = (IdentifierInfo**)calloc(NumIds, sizeof(*PerIDCache));
|
2009-01-21 15:34:28 +08:00
|
|
|
if (!PerIDCache) {
|
2009-11-12 10:53:48 +08:00
|
|
|
InvalidPTH(Diags, "Could not allocate memory for processing PTH file");
|
2009-01-21 15:34:28 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2008-12-03 09:16:39 +08:00
|
|
|
}
|
2009-01-21 15:34:28 +08:00
|
|
|
|
2009-03-20 06:19:30 +08:00
|
|
|
// Compute the address of the original source file.
|
|
|
|
const unsigned char* originalSourceBase = PrologueOffset + sizeof(uint32_t)*4;
|
|
|
|
unsigned len = ReadUnalignedLE16(originalSourceBase);
|
2009-09-09 23:08:12 +08:00
|
|
|
if (!len) originalSourceBase = 0;
|
|
|
|
|
2009-01-16 02:47:46 +08:00
|
|
|
// Create the new PTHManager.
|
|
|
|
return new PTHManager(File.take(), FL.take(), IData, PerIDCache,
|
2009-03-20 06:19:30 +08:00
|
|
|
SL.take(), NumIds, spellingBase,
|
|
|
|
(const char*) originalSourceBase);
|
2008-12-03 08:38:03 +08:00
|
|
|
}
|
2009-03-20 06:19:30 +08:00
|
|
|
|
2009-01-18 10:57:21 +08:00
|
|
|
IdentifierInfo* PTHManager::LazilyCreateIdentifierInfo(unsigned PersistentID) {
|
2008-12-03 08:38:03 +08:00
|
|
|
// Look in the PTH file for the string data for the IdentifierInfo object.
|
2009-01-18 10:57:21 +08:00
|
|
|
const unsigned char* TableEntry = IdDataTable + sizeof(uint32_t)*PersistentID;
|
2009-01-18 09:57:14 +08:00
|
|
|
const unsigned char* IDData =
|
2009-01-23 03:48:26 +08:00
|
|
|
(const unsigned char*)Buf->getBufferStart() + ReadLE32(TableEntry);
|
2009-01-18 09:57:14 +08:00
|
|
|
assert(IDData < (const unsigned char*)Buf->getBufferEnd());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-01-16 02:47:46 +08:00
|
|
|
// Allocate the object.
|
2009-01-18 09:57:14 +08:00
|
|
|
std::pair<IdentifierInfo,const unsigned char*> *Mem =
|
|
|
|
Alloc.Allocate<std::pair<IdentifierInfo,const unsigned char*> >();
|
2009-01-16 02:47:46 +08:00
|
|
|
|
|
|
|
Mem->second = IDData;
|
2009-02-12 05:29:16 +08:00
|
|
|
assert(IDData[0] != '\0');
|
2009-01-21 07:28:34 +08:00
|
|
|
IdentifierInfo *II = new ((void*) Mem) IdentifierInfo();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-01-16 02:47:46 +08:00
|
|
|
// Store the new IdentifierInfo in the cache.
|
2009-01-18 10:57:21 +08:00
|
|
|
PerIDCache[PersistentID] = II;
|
2009-10-19 04:26:12 +08:00
|
|
|
assert(II->getNameStart() && II->getNameStart()[0] != '\0');
|
2008-12-03 08:38:03 +08:00
|
|
|
return II;
|
|
|
|
}
|
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
IdentifierInfo* PTHManager::get(StringRef Name) {
|
2009-02-12 05:29:16 +08:00
|
|
|
PTHStringIdLookup& SL = *((PTHStringIdLookup*)StringIdLookup);
|
|
|
|
// Double check our assumption that the last character isn't '\0'.
|
2011-09-22 09:35:49 +08:00
|
|
|
assert(Name.empty() || Name.back() != '\0');
|
2010-03-12 16:23:34 +08:00
|
|
|
PTHStringIdLookup::iterator I = SL.find(std::make_pair(Name.data(),
|
|
|
|
Name.size()));
|
2009-02-12 05:29:16 +08:00
|
|
|
if (I == SL.end()) // No identifier found?
|
|
|
|
return 0;
|
2009-01-16 02:47:46 +08:00
|
|
|
|
2009-02-12 05:29:16 +08:00
|
|
|
// Match found. Return the identifier!
|
|
|
|
assert(*I > 0);
|
|
|
|
return GetIdentifierInfo(*I-1);
|
|
|
|
}
|
2009-01-16 02:47:46 +08:00
|
|
|
|
2009-01-17 16:06:50 +08:00
|
|
|
PTHLexer *PTHManager::CreateLexer(FileID FID) {
|
|
|
|
const FileEntry *FE = PP->getSourceManager().getFileEntryForID(FID);
|
2008-12-03 08:38:03 +08:00
|
|
|
if (!FE)
|
|
|
|
return 0;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-12-03 08:38:03 +08:00
|
|
|
// Lookup the FileEntry object in our file lookup data structure. It will
|
|
|
|
// return a variant that indicates whether or not there is an offset within
|
|
|
|
// the PTH file that contains cached tokens.
|
2009-02-11 06:16:22 +08:00
|
|
|
PTHFileLookup& PFL = *((PTHFileLookup*)FileLookup);
|
|
|
|
PTHFileLookup::iterator I = PFL.find(FE);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-11 06:16:22 +08:00
|
|
|
if (I == PFL.end()) // No tokens available?
|
2008-12-03 08:38:03 +08:00
|
|
|
return 0;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
const PTHFileData& FileData = *I;
|
|
|
|
|
2009-01-18 09:57:14 +08:00
|
|
|
const unsigned char *BufStart = (const unsigned char *)Buf->getBufferStart();
|
2008-12-03 08:38:03 +08:00
|
|
|
// Compute the offset of the token data within the buffer.
|
2009-01-18 09:57:14 +08:00
|
|
|
const unsigned char* data = BufStart + FileData.getTokenOffset();
|
2008-12-13 02:34:08 +08:00
|
|
|
|
|
|
|
// Get the location of pp-conditional table.
|
2009-01-18 09:57:14 +08:00
|
|
|
const unsigned char* ppcond = BufStart + FileData.getPPCondOffset();
|
2009-01-23 03:48:26 +08:00
|
|
|
uint32_t Len = ReadLE32(ppcond);
|
2009-01-18 10:10:31 +08:00
|
|
|
if (Len == 0) ppcond = 0;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-01-16 02:47:46 +08:00
|
|
|
assert(PP && "No preprocessor set yet!");
|
2009-09-09 23:08:12 +08:00
|
|
|
return new PTHLexer(*PP, FID, data, ppcond, *this);
|
2008-12-03 08:38:03 +08:00
|
|
|
}
|
2009-02-12 11:26:59 +08:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// 'stat' caching.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
2009-11-29 03:45:26 +08:00
|
|
|
class PTHStatData {
|
2009-02-12 11:26:59 +08:00
|
|
|
public:
|
2013-08-02 05:42:11 +08:00
|
|
|
const bool HasData;
|
|
|
|
uint64_t Size;
|
|
|
|
time_t ModTime;
|
|
|
|
llvm::sys::fs::UniqueID UniqueID;
|
|
|
|
bool IsDirectory;
|
|
|
|
|
|
|
|
PTHStatData(uint64_t Size, time_t ModTime, llvm::sys::fs::UniqueID UniqueID,
|
|
|
|
bool IsDirectory)
|
|
|
|
: HasData(true), Size(Size), ModTime(ModTime), UniqueID(UniqueID),
|
|
|
|
IsDirectory(IsDirectory) {}
|
|
|
|
|
|
|
|
PTHStatData() : HasData(false) {}
|
2009-02-12 11:26:59 +08:00
|
|
|
};
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-11-29 03:45:26 +08:00
|
|
|
class PTHStatLookupTrait : public PTHFileLookupCommonTrait {
|
2009-02-12 11:26:59 +08:00
|
|
|
public:
|
2009-02-14 03:13:46 +08:00
|
|
|
typedef const char* external_key_type; // const char*
|
2009-02-12 11:26:59 +08:00
|
|
|
typedef PTHStatData data_type;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-14 03:13:46 +08:00
|
|
|
static internal_key_type GetInternalKey(const char *path) {
|
|
|
|
// The key 'kind' doesn't matter here because it is ignored in EqualKey.
|
|
|
|
return std::make_pair((unsigned char) 0x0, path);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool EqualKey(internal_key_type a, internal_key_type b) {
|
|
|
|
// When doing 'stat' lookups we don't care about the kind of 'a' and 'b',
|
|
|
|
// just the paths.
|
|
|
|
return strcmp(a.second, b.second) == 0;
|
2009-09-09 23:08:12 +08:00
|
|
|
}
|
|
|
|
|
2009-02-14 03:13:46 +08:00
|
|
|
static data_type ReadData(const internal_key_type& k, const unsigned char* d,
|
2009-09-09 23:08:12 +08:00
|
|
|
unsigned) {
|
|
|
|
|
2009-02-14 06:07:44 +08:00
|
|
|
if (k.first /* File or Directory */) {
|
2013-08-02 05:42:11 +08:00
|
|
|
bool IsDirectory = true;
|
|
|
|
if (k.first == 0x1 /* File */) {
|
|
|
|
IsDirectory = false;
|
|
|
|
d += 4 * 2; // Skip the first 2 words.
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t File = ReadUnalignedLE64(d);
|
|
|
|
uint64_t Device = ReadUnalignedLE64(d);
|
|
|
|
llvm::sys::fs::UniqueID UniqueID(File, Device);
|
|
|
|
time_t ModTime = ReadUnalignedLE64(d);
|
|
|
|
uint64_t Size = ReadUnalignedLE64(d);
|
|
|
|
return data_type(Size, ModTime, UniqueID, IsDirectory);
|
2009-02-14 03:13:46 +08:00
|
|
|
}
|
2009-02-14 06:07:44 +08:00
|
|
|
|
|
|
|
// Negative stat. Don't read anything.
|
2009-02-14 03:13:46 +08:00
|
|
|
return data_type();
|
2009-02-12 11:26:59 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2010-11-24 03:19:34 +08:00
|
|
|
class PTHStatCache : public FileSystemStatCache {
|
2009-02-12 11:26:59 +08:00
|
|
|
typedef OnDiskChainedHashTable<PTHStatLookupTrait> CacheTy;
|
|
|
|
CacheTy Cache;
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
public:
|
2009-02-12 11:26:59 +08:00
|
|
|
PTHStatCache(PTHFileLookup &FL) :
|
|
|
|
Cache(FL.getNumBuckets(), FL.getNumEntries(), FL.getBuckets(),
|
|
|
|
FL.getBase()) {}
|
|
|
|
|
|
|
|
~PTHStatCache() {}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2013-08-02 05:42:11 +08:00
|
|
|
LookupResult getStat(const char *Path, FileData &Data, bool isFile,
|
2014-02-21 05:59:23 +08:00
|
|
|
vfs::File **F, vfs::FileSystem &FS) {
|
2009-02-12 11:26:59 +08:00
|
|
|
// Do the lookup for the file's data in the PTH file.
|
2010-11-24 03:19:34 +08:00
|
|
|
CacheTy::iterator I = Cache.find(Path);
|
2009-02-12 11:26:59 +08:00
|
|
|
|
|
|
|
// If we don't get a hit in the PTH file just forward to 'stat'.
|
2010-03-12 16:23:34 +08:00
|
|
|
if (I == Cache.end())
|
2014-02-21 05:59:23 +08:00
|
|
|
return statChained(Path, Data, isFile, F, FS);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2013-08-02 05:42:11 +08:00
|
|
|
const PTHStatData &D = *I;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2013-08-02 05:42:11 +08:00
|
|
|
if (!D.HasData)
|
2010-11-24 04:05:15 +08:00
|
|
|
return CacheMissing;
|
2009-02-14 06:07:44 +08:00
|
|
|
|
2014-03-01 05:16:07 +08:00
|
|
|
Data.Name = Path;
|
2013-08-02 05:42:11 +08:00
|
|
|
Data.Size = D.Size;
|
|
|
|
Data.ModTime = D.ModTime;
|
|
|
|
Data.UniqueID = D.UniqueID;
|
|
|
|
Data.IsDirectory = D.IsDirectory;
|
|
|
|
Data.IsNamedPipe = false;
|
|
|
|
Data.InPCH = true;
|
|
|
|
|
2010-11-24 04:05:15 +08:00
|
|
|
return CacheExists;
|
2009-02-12 11:26:59 +08:00
|
|
|
}
|
|
|
|
};
|
2009-02-24 07:27:54 +08:00
|
|
|
} // end anonymous namespace
|
2009-02-12 11:26:59 +08:00
|
|
|
|
2010-11-24 03:19:34 +08:00
|
|
|
FileSystemStatCache *PTHManager::createStatCache() {
|
2009-02-12 11:45:39 +08:00
|
|
|
return new PTHStatCache(*((PTHFileLookup*) FileLookup));
|
2009-02-12 11:26:59 +08:00
|
|
|
}
|