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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-12-03 08:38:03 +08:00
|
|
|
#include "clang/Basic/TokenKinds.h"
|
|
|
|
#include "clang/Basic/FileManager.h"
|
|
|
|
#include "clang/Basic/IdentifierTable.h"
|
2008-11-13 05:37:15 +08:00
|
|
|
#include "clang/Lex/PTHLexer.h"
|
|
|
|
#include "clang/Lex/Preprocessor.h"
|
2008-12-03 08:38:03 +08:00
|
|
|
#include "clang/Lex/PTHManager.h"
|
|
|
|
#include "clang/Lex/Token.h"
|
|
|
|
#include "clang/Lex/Preprocessor.h"
|
|
|
|
#include "llvm/Support/Compiler.h"
|
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
|
|
|
#include "llvm/ADT/StringMap.h"
|
|
|
|
#include "llvm/ADT/OwningPtr.h"
|
2008-11-13 05:37:15 +08:00
|
|
|
using namespace clang;
|
|
|
|
|
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
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Utility methods for reading from the mmap'ed PTH file.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-01-18 09:57:14 +08:00
|
|
|
static inline uint8_t Read8(const unsigned char *&Data) {
|
|
|
|
uint8_t V = Data[0];
|
|
|
|
Data += 1;
|
|
|
|
return V;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline uint16_t Read16(const unsigned char *&Data) {
|
2009-01-18 10:19:16 +08:00
|
|
|
// Targets that directly support unaligned little-endian 16-bit loads can just
|
|
|
|
// use them.
|
|
|
|
#if defined(__i386__) || defined(__x86_64__)
|
|
|
|
uint16_t V = *((uint16_t*)Data);
|
|
|
|
#else
|
2009-01-18 09:57:14 +08:00
|
|
|
uint16_t V = ((uint16_t)Data[0] << 0) |
|
|
|
|
((uint16_t)Data[1] << 8);
|
2009-01-18 10:19:16 +08:00
|
|
|
#endif
|
2009-01-18 09:57:14 +08:00
|
|
|
Data += 2;
|
|
|
|
return V;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline uint32_t Read32(const unsigned char *&Data) {
|
2009-01-18 10:19:16 +08:00
|
|
|
// Targets that directly support unaligned little-endian 32-bit loads can just
|
|
|
|
// use them.
|
|
|
|
#if defined(__i386__) || defined(__x86_64__)
|
|
|
|
uint32_t V = *((uint32_t*)Data);
|
|
|
|
#else
|
2009-01-18 09:57:14 +08:00
|
|
|
uint32_t V = ((uint32_t)Data[0] << 0) |
|
|
|
|
((uint32_t)Data[1] << 8) |
|
|
|
|
((uint32_t)Data[2] << 16) |
|
|
|
|
((uint32_t)Data[3] << 24);
|
2009-01-18 10:19:16 +08:00
|
|
|
#endif
|
2009-01-18 09:57:14 +08:00
|
|
|
Data += 4;
|
2008-12-23 09:30:52 +08:00
|
|
|
return V;
|
|
|
|
}
|
|
|
|
|
2009-01-18 09:57:14 +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,
|
|
|
|
const unsigned char *ppcond,
|
2009-01-17 14:22:33 +08:00
|
|
|
PTHSpellingSearch &mySpellingSrch, PTHManager &PM)
|
|
|
|
: PreprocessorLexer(&PP, FID), TokBuf(D), CurPtr(D), LastHashTokPtr(0),
|
2009-01-10 06:05:30 +08:00
|
|
|
PPCond(ppcond), CurPPCondPtr(ppcond), MySpellingSrch(mySpellingSrch),
|
2009-01-17 14:22:33 +08:00
|
|
|
PTHMgr(PM) {
|
|
|
|
|
|
|
|
FileStartLoc = PP.getSourceManager().getLocForStartOfFile(FID);
|
2009-01-10 06:05:30 +08:00
|
|
|
}
|
2008-11-13 05:37:15 +08:00
|
|
|
|
2008-12-23 09:30:52 +08:00
|
|
|
void PTHLexer::Lex(Token& Tok) {
|
|
|
|
LexNextToken:
|
2008-12-23 10:30:15 +08:00
|
|
|
|
|
|
|
//===--------------------------------------==//
|
|
|
|
// Read the raw token data.
|
|
|
|
//===--------------------------------------==//
|
2008-12-23 09:30:52 +08:00
|
|
|
|
2008-12-23 10:30:15 +08:00
|
|
|
// Shadow CurPtr into an automatic variable.
|
2009-01-21 15:06:08 +08:00
|
|
|
const unsigned *CurPtrShadow = (const unsigned *)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-21 15:06:08 +08:00
|
|
|
unsigned Word0 = CurPtrShadow[0];
|
|
|
|
unsigned IdentifierID = CurPtrShadow[1];
|
|
|
|
unsigned FileOffset = CurPtrShadow[2];
|
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:06:08 +08:00
|
|
|
unsigned Len = Word0 >> 16;
|
2009-01-20 07:13:15 +08:00
|
|
|
|
2009-01-21 15:06:08 +08:00
|
|
|
CurPtr = (const unsigned char*)(CurPtrShadow+3);
|
2008-12-23 09:30:52 +08:00
|
|
|
|
2008-12-23 10:30:15 +08:00
|
|
|
//===--------------------------------------==//
|
|
|
|
// Construct the token itself.
|
|
|
|
//===--------------------------------------==//
|
2008-12-23 09:30:52 +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);
|
2009-01-18 10:34:01 +08:00
|
|
|
if (IdentifierID)
|
|
|
|
Tok.setIdentifierInfo(PTHMgr.GetIdentifierInfo(IdentifierID-1));
|
2009-01-17 14:22:33 +08:00
|
|
|
Tok.setLocation(FileStartLoc.getFileLocWithOffset(FileOffset));
|
2008-12-23 10:30:15 +08:00
|
|
|
Tok.setLength(Len);
|
2008-11-21 03:49:00 +08:00
|
|
|
|
2008-12-23 10:30:15 +08:00
|
|
|
//===--------------------------------------==//
|
|
|
|
// Process the token.
|
|
|
|
//===--------------------------------------==//
|
2009-01-10 06:05:30 +08:00
|
|
|
#if 0
|
|
|
|
SourceManager& SM = PP->getSourceManager();
|
|
|
|
llvm::cerr << SM.getFileEntryForID(FileID)->getName()
|
|
|
|
<< ':' << SM.getLogicalLineNumber(Tok.getLocation())
|
|
|
|
<< ':' << SM.getLogicalColumnNumber(Tok.getLocation())
|
|
|
|
<< '\n';
|
|
|
|
#endif
|
2008-11-21 03:49:00 +08:00
|
|
|
|
2009-01-18 10:34:01 +08:00
|
|
|
if (TKind == tok::identifier) {
|
2008-12-24 03:24:24 +08:00
|
|
|
MIOpt.ReadToken();
|
|
|
|
return PP->HandleIdentifier(Tok);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
2008-11-21 08:58:35 +08:00
|
|
|
Preprocessor *PPCache = PP;
|
2008-12-24 03:24:24 +08:00
|
|
|
|
|
|
|
assert(!ParsingPreprocessorDirective);
|
|
|
|
assert(!LexingRawMode);
|
|
|
|
|
|
|
|
// FIXME: Issue diagnostics similar to Lexer.
|
|
|
|
if (PP->HandleEndOfFile(Tok, false))
|
2008-11-20 15:58:05 +08:00
|
|
|
return;
|
2008-12-24 03:24:24 +08:00
|
|
|
|
2008-11-21 08:58:35 +08:00
|
|
|
assert(PPCache && "Raw buffer::LexEndOfFile should return a token");
|
|
|
|
return PPCache->Lex(Tok);
|
|
|
|
}
|
2008-11-20 15:58:05 +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);
|
2008-11-20 15:58:05 +08:00
|
|
|
|
2008-12-24 03:24:24 +08:00
|
|
|
if (PP->isCurrentLexer(this))
|
|
|
|
goto LexNextToken;
|
2008-12-23 09:30:52 +08:00
|
|
|
|
2008-12-24 03:24:24 +08:00
|
|
|
return PP->Lex(Tok);
|
2008-12-23 09:30:52 +08:00
|
|
|
}
|
|
|
|
|
2009-01-18 10:34:01 +08:00
|
|
|
if (TKind == tok::eom) {
|
2008-12-24 03:24:24 +08:00
|
|
|
assert(ParsingPreprocessorDirective);
|
|
|
|
ParsingPreprocessorDirective = false;
|
|
|
|
return;
|
|
|
|
}
|
2008-11-13 05:37:15 +08:00
|
|
|
|
2008-12-24 03:24:24 +08:00
|
|
|
MIOpt.ReadToken();
|
2008-11-21 08:58:35 +08:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
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;
|
2008-12-23 09:30:52 +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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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-01-18 09:57:14 +08:00
|
|
|
const unsigned char* HashEntryI = 0;
|
2008-12-13 02:34:08 +08:00
|
|
|
uint32_t Offset;
|
|
|
|
uint32_t TableIdx;
|
|
|
|
|
|
|
|
do {
|
2008-12-13 06:05:38 +08:00
|
|
|
// Read the token offset from the side-table.
|
2008-12-13 02:34:08 +08:00
|
|
|
Offset = Read32(CurPPCondPtr);
|
2008-12-13 06:05:38 +08:00
|
|
|
|
|
|
|
// Read the target table index from the side-table.
|
2008-12-13 02:34:08 +08:00
|
|
|
TableIdx = Read32(CurPPCondPtr);
|
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.
|
|
|
|
uint32_t TmpOffset = Read32(NextPPCondPtr);
|
2009-01-18 09:57:14 +08:00
|
|
|
const unsigned char* HashEntryJ = TokBuf + TmpOffset;
|
2008-12-13 06:05:38 +08:00
|
|
|
|
|
|
|
if (HashEntryJ <= LastHashTokPtr) {
|
|
|
|
// Jump directly to the next entry in the side table.
|
|
|
|
HashEntryI = HashEntryJ;
|
|
|
|
Offset = TmpOffset;
|
|
|
|
TableIdx = Read32(NextPPCondPtr);
|
|
|
|
CurPPCondPtr = NextPPCondPtr;
|
|
|
|
}
|
|
|
|
}
|
2008-12-13 02:34:08 +08:00
|
|
|
}
|
2008-12-13 06:05:38 +08:00
|
|
|
while (HashEntryI < LastHashTokPtr);
|
|
|
|
assert(HashEntryI == LastHashTokPtr && "No PP-cond entry found for '#'");
|
2008-12-13 02:34:08 +08:00
|
|
|
assert(TableIdx && "No jumping from #endifs.");
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
|
|
|
// Read where we should jump to.
|
2008-12-13 06:05:38 +08:00
|
|
|
HashEntryI = TokBuf + Read32(NextPPCondPtr);
|
2008-12-13 02:34:08 +08:00
|
|
|
uint32_t NextIdx = Read32(NextPPCondPtr);
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
|
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;
|
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;
|
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;
|
|
|
|
|
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-18 10:10:31 +08:00
|
|
|
const unsigned char *OffsetPtr = CurPtr + (1 + 1 + 3);
|
|
|
|
uint32_t Offset = Read32(OffsetPtr);
|
|
|
|
return FileStartLoc.getFileLocWithOffset(Offset);
|
2008-12-18 07:36:32 +08:00
|
|
|
}
|
|
|
|
|
2009-01-10 06:05:30 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// getSpelling() - Use cached data in PTH files for getSpelling().
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-01-17 14:22:33 +08:00
|
|
|
unsigned PTHManager::getSpelling(FileID FID, unsigned FPos,
|
|
|
|
const char *&Buffer) {
|
|
|
|
llvm::DenseMap<FileID, PTHSpellingSearch*>::iterator I =SpellingMap.find(FID);
|
2009-01-10 06:05:30 +08:00
|
|
|
|
|
|
|
if (I == SpellingMap.end())
|
2009-01-18 10:10:31 +08:00
|
|
|
return 0;
|
2009-01-14 06:05:50 +08:00
|
|
|
|
2009-01-17 14:22:33 +08:00
|
|
|
return I->second->getSpellingBinarySearch(FPos, Buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned PTHManager::getSpelling(SourceLocation Loc, const char *&Buffer) {
|
2009-01-17 14:29:33 +08:00
|
|
|
SourceManager &SM = PP->getSourceManager();
|
|
|
|
Loc = SM.getSpellingLoc(Loc);
|
|
|
|
std::pair<FileID, unsigned> LocInfo = SM.getDecomposedFileLoc(Loc);
|
2009-01-17 14:22:33 +08:00
|
|
|
return getSpelling(LocInfo.first, LocInfo.second, Buffer);
|
2009-01-10 06:05:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned PTHManager::getSpellingAtPTHOffset(unsigned PTHOffset,
|
2009-01-18 09:57:14 +08:00
|
|
|
const char *&Buffer) {
|
2009-01-18 10:10:31 +08:00
|
|
|
assert(PTHOffset < Buf->getBufferSize());
|
|
|
|
const unsigned char* Ptr =
|
|
|
|
(const unsigned char*)Buf->getBufferStart() + PTHOffset;
|
2009-01-08 12:30:32 +08:00
|
|
|
|
|
|
|
// The string is prefixed by 16 bits for its length, followed by the string
|
|
|
|
// itself.
|
2009-01-18 10:10:31 +08:00
|
|
|
unsigned Len = Read16(Ptr);
|
|
|
|
Buffer = (const char *)Ptr;
|
|
|
|
return Len;
|
2009-01-08 12:30:32 +08:00
|
|
|
}
|
|
|
|
|
2009-01-18 10:10:31 +08:00
|
|
|
unsigned PTHSpellingSearch::getSpellingLinearSearch(unsigned FPos,
|
2009-01-10 06:05:30 +08:00
|
|
|
const char *&Buffer) {
|
2009-01-18 10:10:31 +08:00
|
|
|
const unsigned char *Ptr = LinearItr;
|
|
|
|
unsigned Len = 0;
|
2009-01-10 06:05:30 +08:00
|
|
|
|
2009-01-18 10:10:31 +08:00
|
|
|
if (Ptr == TableEnd)
|
|
|
|
return getSpellingBinarySearch(FPos, Buffer);
|
2009-01-10 06:05:30 +08:00
|
|
|
|
|
|
|
do {
|
2009-01-18 10:10:31 +08:00
|
|
|
uint32_t TokOffset = Read32(Ptr);
|
2009-01-08 12:30:32 +08:00
|
|
|
|
2009-01-18 10:10:31 +08:00
|
|
|
if (TokOffset > FPos)
|
|
|
|
return getSpellingBinarySearch(FPos, Buffer);
|
2009-01-08 12:30:32 +08:00
|
|
|
|
|
|
|
// Did we find a matching token offset for this spelling?
|
2009-01-18 10:10:31 +08:00
|
|
|
if (TokOffset == FPos) {
|
|
|
|
uint32_t SpellingPTHOffset = Read32(Ptr);
|
|
|
|
Len = PTHMgr.getSpellingAtPTHOffset(SpellingPTHOffset, Buffer);
|
2009-01-08 12:30:32 +08:00
|
|
|
break;
|
|
|
|
}
|
2009-01-18 10:10:31 +08:00
|
|
|
} while (Ptr != TableEnd);
|
2009-01-08 12:30:32 +08:00
|
|
|
|
2009-01-18 10:10:31 +08:00
|
|
|
LinearItr = Ptr;
|
|
|
|
return Len;
|
2009-01-08 10:47:16 +08:00
|
|
|
}
|
|
|
|
|
2009-01-18 09:57:14 +08:00
|
|
|
|
2009-01-18 10:10:31 +08:00
|
|
|
unsigned PTHSpellingSearch::getSpellingBinarySearch(unsigned FPos,
|
2009-01-18 09:57:14 +08:00
|
|
|
const char *&Buffer) {
|
2009-01-10 06:05:30 +08:00
|
|
|
|
2009-01-14 06:05:50 +08:00
|
|
|
assert((TableEnd - TableBeg) % SpellingEntrySize == 0);
|
2009-01-18 10:10:31 +08:00
|
|
|
assert(TableEnd >= TableBeg);
|
2009-01-14 06:05:50 +08:00
|
|
|
|
|
|
|
if (TableEnd == TableBeg)
|
|
|
|
return 0;
|
|
|
|
|
2009-01-10 06:05:30 +08:00
|
|
|
unsigned min = 0;
|
2009-01-18 10:10:31 +08:00
|
|
|
const unsigned char *tb = TableBeg;
|
2009-01-14 06:05:50 +08:00
|
|
|
unsigned max = NumSpellings;
|
2009-01-10 06:05:30 +08:00
|
|
|
|
2009-01-14 06:05:50 +08:00
|
|
|
do {
|
2009-01-10 06:05:30 +08:00
|
|
|
unsigned i = (max - min) / 2 + min;
|
2009-01-18 10:10:31 +08:00
|
|
|
const unsigned char *Ptr = tb + (i * SpellingEntrySize);
|
2009-01-10 06:05:30 +08:00
|
|
|
|
2009-01-18 10:10:31 +08:00
|
|
|
uint32_t TokOffset = Read32(Ptr);
|
|
|
|
if (TokOffset > FPos) {
|
2009-01-10 06:05:30 +08:00
|
|
|
max = i;
|
2009-01-14 06:05:50 +08:00
|
|
|
assert(!(max == min) || (min == i));
|
2009-01-10 06:05:30 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2009-01-18 10:10:31 +08:00
|
|
|
if (TokOffset < FPos) {
|
2009-01-14 06:16:45 +08:00
|
|
|
if (i == min)
|
|
|
|
break;
|
|
|
|
|
2009-01-10 06:05:30 +08:00
|
|
|
min = i;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2009-01-18 10:10:31 +08:00
|
|
|
uint32_t SpellingPTHOffset = Read32(Ptr);
|
2009-01-10 06:05:30 +08:00
|
|
|
return PTHMgr.getSpellingAtPTHOffset(SpellingPTHOffset, Buffer);
|
|
|
|
}
|
2009-01-14 06:05:50 +08:00
|
|
|
while (min != max);
|
2009-01-10 06:05:30 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-01-17 14:22:33 +08:00
|
|
|
unsigned PTHLexer::getSpelling(SourceLocation Loc, const char *&Buffer) {
|
|
|
|
SourceManager &SM = PP->getSourceManager();
|
|
|
|
Loc = SM.getSpellingLoc(Loc);
|
|
|
|
std::pair<FileID, unsigned> LocInfo = SM.getDecomposedFileLoc(Loc);
|
|
|
|
|
|
|
|
FileID FID = LocInfo.first;
|
|
|
|
unsigned FPos = LocInfo.second;
|
2009-01-10 06:05:30 +08:00
|
|
|
|
2009-01-17 14:22:33 +08:00
|
|
|
if (FID == getFileID())
|
|
|
|
return MySpellingSrch.getSpellingLinearSearch(FPos, Buffer);
|
|
|
|
return PTHMgr.getSpelling(FID, FPos, Buffer);
|
2009-01-10 06:05:30 +08:00
|
|
|
}
|
|
|
|
|
2008-12-03 08:38:03 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Internal Data Structures for PTH file lookup and resolving identifiers.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
|
|
|
|
/// 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 {
|
|
|
|
class VISIBILITY_HIDDEN PTHFileLookup {
|
|
|
|
public:
|
|
|
|
class Val {
|
2008-12-12 07:36:38 +08:00
|
|
|
uint32_t TokenOff;
|
|
|
|
uint32_t PPCondOff;
|
2009-01-08 10:47:16 +08:00
|
|
|
uint32_t SpellingOff;
|
2008-12-03 08:38:03 +08:00
|
|
|
public:
|
2008-12-12 07:36:38 +08:00
|
|
|
Val() : TokenOff(~0) {}
|
2009-01-08 10:47:16 +08:00
|
|
|
Val(uint32_t toff, uint32_t poff, uint32_t soff)
|
|
|
|
: TokenOff(toff), PPCondOff(poff), SpellingOff(soff) {}
|
2008-12-03 08:38:03 +08:00
|
|
|
|
2009-01-18 10:10:31 +08:00
|
|
|
bool isValid() const { return TokenOff != ~((uint32_t)0); }
|
|
|
|
|
2008-12-12 07:36:38 +08:00
|
|
|
uint32_t getTokenOffset() const {
|
2009-01-18 10:10:31 +08:00
|
|
|
assert(isValid() && "PTHFileLookup entry initialized.");
|
2008-12-12 07:36:38 +08:00
|
|
|
return TokenOff;
|
2008-12-03 08:38:03 +08:00
|
|
|
}
|
|
|
|
|
2009-01-08 10:47:16 +08:00
|
|
|
uint32_t getPPCondOffset() const {
|
2009-01-18 10:10:31 +08:00
|
|
|
assert(isValid() && "PTHFileLookup entry initialized.");
|
2008-12-12 07:36:38 +08:00
|
|
|
return PPCondOff;
|
|
|
|
}
|
|
|
|
|
2009-01-08 10:47:16 +08:00
|
|
|
uint32_t getSpellingOffset() const {
|
2009-01-18 10:10:31 +08:00
|
|
|
assert(isValid() && "PTHFileLookup entry initialized.");
|
2009-01-08 10:47:16 +08:00
|
|
|
return SpellingOff;
|
|
|
|
}
|
2008-12-03 08:38:03 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
|
|
|
llvm::StringMap<Val> FileMap;
|
|
|
|
|
|
|
|
public:
|
|
|
|
PTHFileLookup() {};
|
|
|
|
|
|
|
|
Val Lookup(const FileEntry* FE) {
|
|
|
|
const char* s = FE->getName();
|
|
|
|
unsigned size = strlen(s);
|
|
|
|
return FileMap.GetOrCreateValue(s, s+size).getValue();
|
|
|
|
}
|
|
|
|
|
2009-01-18 09:57:14 +08:00
|
|
|
void ReadTable(const unsigned char* D) {
|
2008-12-03 08:38:03 +08:00
|
|
|
uint32_t N = Read32(D); // Read the length of the table.
|
|
|
|
|
|
|
|
for ( ; N > 0; --N) { // The rest of the data is the table itself.
|
2009-01-18 10:10:31 +08:00
|
|
|
uint32_t Len = Read32(D);
|
2009-01-18 09:57:14 +08:00
|
|
|
const char* s = (const char *)D;
|
2009-01-18 10:10:31 +08:00
|
|
|
D += Len;
|
2009-01-08 10:47:16 +08:00
|
|
|
|
2008-12-12 07:36:38 +08:00
|
|
|
uint32_t TokenOff = Read32(D);
|
2009-01-08 10:47:16 +08:00
|
|
|
uint32_t PPCondOff = Read32(D);
|
|
|
|
uint32_t SpellingOff = Read32(D);
|
|
|
|
|
2009-01-18 10:10:31 +08:00
|
|
|
FileMap.GetOrCreateValue(s, s+Len).getValue() =
|
2009-01-08 10:47:16 +08:00
|
|
|
Val(TokenOff, PPCondOff, SpellingOff);
|
2008-12-03 08:38:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// PTHManager methods.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
PTHManager::PTHManager(const llvm::MemoryBuffer* buf, void* fileLookup,
|
2009-01-18 09:57:14 +08:00
|
|
|
const unsigned char* idDataTable,
|
|
|
|
IdentifierInfo** perIDCache,
|
|
|
|
const unsigned char* sortedIdTable, unsigned numIds)
|
2008-12-03 09:16:39 +08:00
|
|
|
: Buf(buf), PerIDCache(perIDCache), FileLookup(fileLookup),
|
2009-01-16 02:47:46 +08:00
|
|
|
IdDataTable(idDataTable), SortedIdTable(sortedIdTable),
|
|
|
|
NumIds(numIds), PP(0) {}
|
2008-12-03 08:38:03 +08:00
|
|
|
|
|
|
|
PTHManager::~PTHManager() {
|
|
|
|
delete Buf;
|
|
|
|
delete (PTHFileLookup*) FileLookup;
|
2008-12-05 06:47:11 +08:00
|
|
|
free(PerIDCache);
|
2008-12-03 08:38:03 +08:00
|
|
|
}
|
|
|
|
|
2009-01-16 02:47:46 +08:00
|
|
|
PTHManager* PTHManager::Create(const std::string& file) {
|
2008-12-03 08:38:03 +08:00
|
|
|
// Memory map the PTH file.
|
|
|
|
llvm::OwningPtr<llvm::MemoryBuffer>
|
|
|
|
File(llvm::MemoryBuffer::getFile(file.c_str()));
|
|
|
|
|
|
|
|
if (!File)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// Get the buffer ranges and check if there are at least three 32-bit
|
|
|
|
// words at the end of the file.
|
2009-01-18 09:57:14 +08:00
|
|
|
const unsigned char* BufBeg = (unsigned char*)File->getBufferStart();
|
|
|
|
const unsigned char* BufEnd = (unsigned char*)File->getBufferEnd();
|
2008-12-03 08:38:03 +08:00
|
|
|
|
|
|
|
if(!(BufEnd > BufBeg + sizeof(uint32_t)*3)) {
|
|
|
|
assert(false && "Invalid PTH file.");
|
|
|
|
return 0; // FIXME: Proper error diagnostic?
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compute the address of the index table at the end of the PTH file.
|
|
|
|
// This table contains the offset of the file lookup table, the
|
|
|
|
// persistent ID -> identifer data table.
|
2009-01-15 09:26:25 +08:00
|
|
|
// FIXME: We should just embed this offset in the PTH file.
|
2009-01-18 09:57:14 +08:00
|
|
|
const unsigned char* EndTable = BufEnd - sizeof(uint32_t)*4;
|
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-01-18 09:57:14 +08:00
|
|
|
const unsigned char* FileTableOffset = EndTable + sizeof(uint32_t)*3;
|
|
|
|
const unsigned char* FileTable = BufBeg + Read32(FileTableOffset);
|
2008-12-03 08:38:03 +08:00
|
|
|
|
|
|
|
if (!(FileTable > BufBeg && FileTable < BufEnd)) {
|
|
|
|
assert(false && "Invalid PTH file.");
|
|
|
|
return 0; // FIXME: Proper error diagnostic?
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::OwningPtr<PTHFileLookup> FL(new PTHFileLookup());
|
|
|
|
FL->ReadTable(FileTable);
|
|
|
|
|
|
|
|
// Get the location of the table mapping from persistent ids to the
|
|
|
|
// data needed to reconstruct identifiers.
|
2009-01-18 09:57:14 +08:00
|
|
|
const unsigned char* IDTableOffset = EndTable + sizeof(uint32_t)*1;
|
|
|
|
const unsigned char* IData = BufBeg + Read32(IDTableOffset);
|
2008-12-03 08:38:03 +08:00
|
|
|
if (!(IData > BufBeg && IData < BufEnd)) {
|
|
|
|
assert(false && "Invalid PTH file.");
|
|
|
|
return 0; // FIXME: Proper error diagnostic?
|
|
|
|
}
|
|
|
|
|
2009-01-16 02:47:46 +08:00
|
|
|
// Get the location of the lexigraphically-sorted table of persistent IDs.
|
2009-01-18 09:57:14 +08:00
|
|
|
const unsigned char* SortedIdTableOffset = EndTable + sizeof(uint32_t)*2;
|
|
|
|
const unsigned char* SortedIdTable = BufBeg + Read32(SortedIdTableOffset);
|
2009-01-16 02:47:46 +08:00
|
|
|
if (!(SortedIdTable > BufBeg && SortedIdTable < BufEnd)) {
|
|
|
|
assert(false && "Invalid PTH file.");
|
|
|
|
return 0; // FIXME: Proper error diagnostic?
|
|
|
|
}
|
|
|
|
|
2008-12-03 09:16:39 +08:00
|
|
|
// Get the number of IdentifierInfos and pre-allocate the identifier cache.
|
|
|
|
uint32_t NumIds = Read32(IData);
|
|
|
|
|
|
|
|
// Pre-allocate the peristent ID -> IdentifierInfo* cache. We use calloc()
|
|
|
|
// so that we in the best case only zero out memory once when the OS returns
|
|
|
|
// us new pages.
|
|
|
|
IdentifierInfo** PerIDCache =
|
2009-01-18 09:57:14 +08:00
|
|
|
(IdentifierInfo**)calloc(NumIds, sizeof(*PerIDCache));
|
2008-12-03 09:16:39 +08:00
|
|
|
|
|
|
|
if (!PerIDCache) {
|
|
|
|
assert(false && "Could not allocate Persistent ID cache.");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-01-16 02:47:46 +08:00
|
|
|
// Create the new PTHManager.
|
|
|
|
return new PTHManager(File.take(), FL.take(), IData, PerIDCache,
|
|
|
|
SortedIdTable, NumIds);
|
2008-12-03 08:38:03 +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 =
|
|
|
|
(const unsigned char*)Buf->getBufferStart() + Read32(TableEntry);
|
|
|
|
assert(IDData < (const unsigned char*)Buf->getBufferEnd());
|
2008-12-03 08:38:03 +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-01-21 07:28:34 +08:00
|
|
|
IdentifierInfo *II = new ((void*) Mem) IdentifierInfo();
|
2008-12-03 08:38:03 +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;
|
2008-12-03 08:38:03 +08:00
|
|
|
return II;
|
|
|
|
}
|
|
|
|
|
2009-01-16 02:47:46 +08:00
|
|
|
IdentifierInfo* PTHManager::get(const char *NameStart, const char *NameEnd) {
|
|
|
|
unsigned min = 0;
|
|
|
|
unsigned max = NumIds;
|
2009-01-18 10:10:31 +08:00
|
|
|
unsigned Len = NameEnd - NameStart;
|
2009-01-16 02:47:46 +08:00
|
|
|
|
|
|
|
do {
|
|
|
|
unsigned i = (max - min) / 2 + min;
|
2009-01-18 10:10:31 +08:00
|
|
|
const unsigned char *Ptr = SortedIdTable + (i * 4);
|
2009-01-16 02:47:46 +08:00
|
|
|
|
|
|
|
// Read the persistentID.
|
2009-01-18 10:10:31 +08:00
|
|
|
unsigned perID = Read32(Ptr);
|
2009-01-16 02:47:46 +08:00
|
|
|
|
|
|
|
// Get the IdentifierInfo.
|
|
|
|
IdentifierInfo* II = GetIdentifierInfo(perID);
|
|
|
|
|
|
|
|
// First compare the lengths.
|
|
|
|
unsigned IILen = II->getLength();
|
2009-01-18 10:10:31 +08:00
|
|
|
if (Len < IILen) goto IsLess;
|
|
|
|
if (Len > IILen) goto IsGreater;
|
2009-01-16 02:47:46 +08:00
|
|
|
|
|
|
|
// Now compare the strings!
|
|
|
|
{
|
2009-01-18 10:10:31 +08:00
|
|
|
signed comp = strncmp(NameStart, II->getName(), Len);
|
2009-01-16 02:47:46 +08:00
|
|
|
if (comp < 0) goto IsLess;
|
|
|
|
if (comp > 0) goto IsGreater;
|
|
|
|
}
|
|
|
|
// We found a match!
|
|
|
|
return II;
|
|
|
|
|
|
|
|
IsGreater:
|
|
|
|
if (i == min) break;
|
|
|
|
min = i;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
IsLess:
|
|
|
|
max = i;
|
|
|
|
assert(!(max == min) || (min == i));
|
|
|
|
}
|
2009-01-16 03:28:38 +08:00
|
|
|
while (min != max);
|
2009-01-16 02:47:46 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
// 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-01-17 14:22:33 +08:00
|
|
|
PTHFileLookup::Val FileData = ((PTHFileLookup*)FileLookup)->Lookup(FE);
|
2008-12-03 08:38:03 +08:00
|
|
|
|
2008-12-12 07:36:38 +08:00
|
|
|
if (!FileData.isValid()) // No tokens available.
|
2008-12-03 08:38:03 +08:00
|
|
|
return 0;
|
|
|
|
|
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-18 10:10:31 +08:00
|
|
|
uint32_t Len = Read32(ppcond);
|
|
|
|
if (Len == 0) ppcond = 0;
|
2009-01-08 12:30:32 +08:00
|
|
|
|
|
|
|
// Get the location of the spelling table.
|
2009-01-18 09:57:14 +08:00
|
|
|
const unsigned char* spellingTable = BufStart + FileData.getSpellingOffset();
|
2009-01-08 12:30:32 +08:00
|
|
|
|
2009-01-18 10:10:31 +08:00
|
|
|
Len = Read32(spellingTable);
|
|
|
|
if (Len == 0) spellingTable = 0;
|
2009-01-08 10:47:16 +08:00
|
|
|
|
2009-01-18 09:57:14 +08:00
|
|
|
assert(data < (const unsigned char*)Buf->getBufferEnd());
|
2009-01-10 06:05:30 +08:00
|
|
|
|
|
|
|
// Create the SpellingSearch object for this FileID.
|
2009-01-18 10:10:31 +08:00
|
|
|
PTHSpellingSearch* ss = new PTHSpellingSearch(*this, Len, spellingTable);
|
2009-01-17 14:22:33 +08:00
|
|
|
SpellingMap[FID] = ss;
|
2009-01-10 06:05:30 +08:00
|
|
|
|
2009-01-16 02:47:46 +08:00
|
|
|
assert(PP && "No preprocessor set yet!");
|
2009-01-17 14:22:33 +08:00
|
|
|
return new PTHLexer(*PP, FID, data, ppcond, *ss, *this);
|
2008-12-03 08:38:03 +08:00
|
|
|
}
|