2008-10-21 08:54:44 +08:00
|
|
|
//===--- CacheTokens.cpp - Caching of lexer tokens for PCH support --------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This provides a possible implementation of PCH support for Clang that is
|
|
|
|
// based on caching lexed tokens and identifiers.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang.h"
|
|
|
|
#include "clang/Basic/FileManager.h"
|
|
|
|
#include "clang/Basic/SourceManager.h"
|
|
|
|
#include "clang/Basic/IdentifierTable.h"
|
|
|
|
#include "clang/Basic/Diagnostic.h"
|
|
|
|
#include "clang/Lex/Lexer.h"
|
|
|
|
#include "clang/Lex/Preprocessor.h"
|
2009-01-08 10:44:06 +08:00
|
|
|
#include "llvm/ADT/StringMap.h"
|
2008-10-21 08:54:44 +08:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2008-12-03 03:44:08 +08:00
|
|
|
#include "llvm/System/Path.h"
|
2009-01-08 09:17:37 +08:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2009-01-16 02:47:46 +08:00
|
|
|
#include "llvm/Support/Streams.h"
|
2008-10-21 08:54:44 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
|
2008-12-03 03:44:08 +08:00
|
|
|
typedef uint32_t Offset;
|
|
|
|
|
2009-02-10 09:06:17 +08:00
|
|
|
static void Emit8(llvm::raw_ostream& Out, uint32_t V) {
|
|
|
|
Out << (unsigned char)(V);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void Emit16(llvm::raw_ostream& Out, uint32_t V) {
|
|
|
|
Out << (unsigned char)(V);
|
|
|
|
Out << (unsigned char)(V >> 8);
|
|
|
|
assert((V >> 16) == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void Emit32(llvm::raw_ostream& Out, uint32_t V) {
|
|
|
|
Out << (unsigned char)(V);
|
|
|
|
Out << (unsigned char)(V >> 8);
|
|
|
|
Out << (unsigned char)(V >> 16);
|
|
|
|
Out << (unsigned char)(V >> 24);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void Pad(llvm::raw_fd_ostream& Out, unsigned Alignment) {
|
|
|
|
Offset off = (Offset) Out.tell();
|
|
|
|
for (unsigned Pad = off % Alignment ; Pad != 0 ; --Pad, ++off) Emit8(Out, 0);
|
|
|
|
}
|
|
|
|
|
2009-01-08 10:44:06 +08:00
|
|
|
namespace {
|
|
|
|
class VISIBILITY_HIDDEN PCHEntry {
|
|
|
|
Offset TokenData, PPCondData;
|
|
|
|
|
|
|
|
public:
|
|
|
|
PCHEntry() {}
|
|
|
|
|
2009-01-27 08:01:05 +08:00
|
|
|
PCHEntry(Offset td, Offset ppcd)
|
|
|
|
: TokenData(td), PPCondData(ppcd) {}
|
2009-01-08 10:44:06 +08:00
|
|
|
|
2009-01-27 08:01:05 +08:00
|
|
|
Offset getTokenOffset() const { return TokenData; }
|
2009-01-08 10:44:06 +08:00
|
|
|
Offset getPPCondTableOffset() const { return PPCondData; }
|
2009-01-27 08:01:05 +08:00
|
|
|
};
|
2009-01-08 10:44:06 +08:00
|
|
|
|
2009-01-27 08:01:05 +08:00
|
|
|
class OffsetOpt {
|
|
|
|
bool valid;
|
|
|
|
Offset off;
|
|
|
|
public:
|
|
|
|
OffsetOpt() : valid(false) {}
|
|
|
|
bool hasOffset() const { return valid; }
|
|
|
|
Offset getOffset() const { assert(valid); return off; }
|
|
|
|
void setOffset(Offset o) { off = o; valid = true; }
|
2009-01-08 10:44:06 +08:00
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
typedef llvm::DenseMap<const FileEntry*, PCHEntry> PCHMap;
|
2008-12-03 03:44:08 +08:00
|
|
|
typedef llvm::DenseMap<const IdentifierInfo*,uint32_t> IDMap;
|
2009-01-27 08:01:05 +08:00
|
|
|
typedef llvm::StringMap<OffsetOpt, llvm::BumpPtrAllocator> CachedStrsTy;
|
2008-10-21 08:54:44 +08:00
|
|
|
|
2009-01-08 09:17:37 +08:00
|
|
|
namespace {
|
|
|
|
class VISIBILITY_HIDDEN PTHWriter {
|
|
|
|
IDMap IM;
|
|
|
|
llvm::raw_fd_ostream& Out;
|
|
|
|
Preprocessor& PP;
|
|
|
|
uint32_t idcount;
|
|
|
|
PCHMap PM;
|
2009-01-08 10:44:06 +08:00
|
|
|
CachedStrsTy CachedStrs;
|
2009-01-27 08:01:05 +08:00
|
|
|
Offset CurStrOffset;
|
|
|
|
std::vector<llvm::StringMapEntry<OffsetOpt>*> StrEntries;
|
2008-10-21 08:54:44 +08:00
|
|
|
|
2009-01-08 09:17:37 +08:00
|
|
|
//// Get the persistent id for the given IdentifierInfo*.
|
|
|
|
uint32_t ResolveID(const IdentifierInfo* II);
|
|
|
|
|
|
|
|
/// Emit a token to the PTH file.
|
|
|
|
void EmitToken(const Token& T);
|
2008-10-21 08:54:44 +08:00
|
|
|
|
2009-01-08 09:17:37 +08:00
|
|
|
void Emit8(uint32_t V) {
|
|
|
|
Out << (unsigned char)(V);
|
|
|
|
}
|
|
|
|
|
2009-02-10 09:06:17 +08:00
|
|
|
void Emit16(uint32_t V) { ::Emit16(Out, V); }
|
2009-01-08 09:17:37 +08:00
|
|
|
|
|
|
|
void Emit24(uint32_t V) {
|
|
|
|
Out << (unsigned char)(V);
|
|
|
|
Out << (unsigned char)(V >> 8);
|
|
|
|
Out << (unsigned char)(V >> 16);
|
|
|
|
assert((V >> 24) == 0);
|
|
|
|
}
|
2008-12-24 02:41:34 +08:00
|
|
|
|
2009-02-10 09:06:17 +08:00
|
|
|
void Emit32(uint32_t V) { ::Emit32(Out, V); }
|
|
|
|
|
2009-01-08 09:17:37 +08:00
|
|
|
void EmitBuf(const char* I, const char* E) {
|
|
|
|
for ( ; I != E ; ++I) Out << *I;
|
|
|
|
}
|
|
|
|
|
2009-01-15 09:26:25 +08:00
|
|
|
std::pair<Offset,std::pair<Offset, Offset> > EmitIdentifierTable();
|
2009-01-08 09:17:37 +08:00
|
|
|
Offset EmitFileTable();
|
2009-01-08 10:44:06 +08:00
|
|
|
PCHEntry LexTokens(Lexer& L);
|
2009-01-27 08:01:05 +08:00
|
|
|
Offset EmitCachedSpellings();
|
2009-01-08 10:44:06 +08:00
|
|
|
|
2009-01-08 09:17:37 +08:00
|
|
|
public:
|
|
|
|
PTHWriter(llvm::raw_fd_ostream& out, Preprocessor& pp)
|
2009-01-27 08:01:05 +08:00
|
|
|
: Out(out), PP(pp), idcount(0), CurStrOffset(0) {}
|
2009-01-08 09:17:37 +08:00
|
|
|
|
|
|
|
void GeneratePTH();
|
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
2008-12-03 03:44:08 +08:00
|
|
|
|
2009-01-08 09:17:37 +08:00
|
|
|
uint32_t PTHWriter::ResolveID(const IdentifierInfo* II) {
|
2008-12-03 03:44:08 +08:00
|
|
|
// Null IdentifierInfo's map to the persistent ID 0.
|
|
|
|
if (!II)
|
|
|
|
return 0;
|
|
|
|
|
2008-10-21 08:54:44 +08:00
|
|
|
IDMap::iterator I = IM.find(II);
|
|
|
|
|
|
|
|
if (I == IM.end()) {
|
2009-01-08 09:17:37 +08:00
|
|
|
IM[II] = ++idcount; // Pre-increment since '0' is reserved for NULL.
|
|
|
|
return idcount;
|
2008-10-21 08:54:44 +08:00
|
|
|
}
|
|
|
|
|
2008-12-03 03:44:08 +08:00
|
|
|
return I->second; // We've already added 1.
|
2008-10-21 08:54:44 +08:00
|
|
|
}
|
|
|
|
|
2009-01-08 09:17:37 +08:00
|
|
|
void PTHWriter::EmitToken(const Token& T) {
|
2009-01-20 07:13:15 +08:00
|
|
|
Emit32(((uint32_t) T.getKind()) |
|
|
|
|
(((uint32_t) T.getFlags()) << 8) |
|
|
|
|
(((uint32_t) T.getLength()) << 16));
|
2009-01-27 08:01:05 +08:00
|
|
|
|
2009-01-27 03:29:26 +08:00
|
|
|
// Literals (strings, numbers, characters) get cached spellings.
|
|
|
|
if (T.isLiteral()) {
|
|
|
|
// FIXME: This uses the slow getSpelling(). Perhaps we do better
|
|
|
|
// in the future? This only slows down PTH generation.
|
|
|
|
const std::string &spelling = PP.getSpelling(T);
|
|
|
|
const char* s = spelling.c_str();
|
|
|
|
|
|
|
|
// Get the string entry.
|
2009-01-27 08:01:05 +08:00
|
|
|
llvm::StringMapEntry<OffsetOpt> *E =
|
|
|
|
&CachedStrs.GetOrCreateValue(s, s+spelling.size());
|
|
|
|
|
|
|
|
if (!E->getValue().hasOffset()) {
|
|
|
|
E->getValue().setOffset(CurStrOffset);
|
|
|
|
StrEntries.push_back(E);
|
|
|
|
CurStrOffset += spelling.size() + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
Emit32(E->getValue().getOffset());
|
2009-01-08 09:17:37 +08:00
|
|
|
}
|
2009-01-27 08:01:05 +08:00
|
|
|
else
|
|
|
|
Emit32(ResolveID(T.getIdentifierInfo()));
|
|
|
|
|
2009-01-27 14:27:13 +08:00
|
|
|
Emit32(PP.getSourceManager().getFileOffset(T.getLocation()));
|
2008-10-21 08:54:44 +08:00
|
|
|
}
|
|
|
|
|
2009-01-08 09:17:37 +08:00
|
|
|
namespace {
|
|
|
|
struct VISIBILITY_HIDDEN IDData {
|
2008-11-27 07:58:26 +08:00
|
|
|
const IdentifierInfo* II;
|
|
|
|
uint32_t FileOffset;
|
2009-01-15 09:26:25 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class VISIBILITY_HIDDEN CompareIDDataIndex {
|
|
|
|
IDData* Table;
|
|
|
|
public:
|
|
|
|
CompareIDDataIndex(IDData* table) : Table(table) {}
|
|
|
|
|
|
|
|
bool operator()(unsigned i, unsigned j) const {
|
2009-01-16 02:47:46 +08:00
|
|
|
const IdentifierInfo* II_i = Table[i].II;
|
|
|
|
const IdentifierInfo* II_j = Table[j].II;
|
|
|
|
|
|
|
|
unsigned i_len = II_i->getLength();
|
|
|
|
unsigned j_len = II_j->getLength();
|
|
|
|
|
|
|
|
if (i_len > j_len)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (i_len < j_len)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Otherwise, compare the strings themselves!
|
|
|
|
return strncmp(II_i->getName(), II_j->getName(), i_len) < 0;
|
2009-01-15 09:26:25 +08:00
|
|
|
}
|
2008-11-27 07:58:26 +08:00
|
|
|
};
|
2009-01-08 09:17:37 +08:00
|
|
|
}
|
2008-11-27 07:58:26 +08:00
|
|
|
|
2009-01-15 09:26:25 +08:00
|
|
|
std::pair<Offset,std::pair<Offset,Offset> >
|
|
|
|
PTHWriter::EmitIdentifierTable() {
|
|
|
|
llvm::BumpPtrAllocator Alloc;
|
2008-11-26 11:36:26 +08:00
|
|
|
|
2008-11-27 07:58:26 +08:00
|
|
|
// Build an inverse map from persistent IDs -> IdentifierInfo*.
|
2009-01-15 09:26:25 +08:00
|
|
|
IDData* IIDMap = Alloc.Allocate<IDData>(idcount);
|
2008-10-21 08:54:44 +08:00
|
|
|
|
2008-11-27 07:58:26 +08:00
|
|
|
// Generate mapping from persistent IDs -> IdentifierInfo*.
|
2009-01-08 09:17:37 +08:00
|
|
|
for (IDMap::iterator I=IM.begin(), E=IM.end(); I!=E; ++I) {
|
2008-12-03 03:44:08 +08:00
|
|
|
// Decrement by 1 because we are using a vector for the lookup and
|
|
|
|
// 0 is reserved for NULL.
|
|
|
|
assert(I->second > 0);
|
2009-01-15 09:26:25 +08:00
|
|
|
assert(I->second-1 < idcount);
|
|
|
|
unsigned idx = I->second-1;
|
|
|
|
IIDMap[idx].II = I->first;
|
|
|
|
}
|
2008-11-27 07:58:26 +08:00
|
|
|
|
2009-01-15 09:26:25 +08:00
|
|
|
// We want to write out the strings in lexical order to support binary
|
|
|
|
// search of strings to identifiers. Create such a table.
|
|
|
|
unsigned *LexicalOrder = Alloc.Allocate<unsigned>(idcount);
|
|
|
|
for (unsigned i = 0; i < idcount ; ++i ) LexicalOrder[i] = i;
|
|
|
|
std::sort(LexicalOrder, LexicalOrder+idcount, CompareIDDataIndex(IIDMap));
|
|
|
|
|
|
|
|
// Write out the lexically-sorted table of persistent ids.
|
|
|
|
Offset LexicalOff = Out.tell();
|
|
|
|
for (unsigned i = 0; i < idcount ; ++i) Emit32(LexicalOrder[i]);
|
|
|
|
|
|
|
|
// Write out the string data itself.
|
2008-12-03 03:44:08 +08:00
|
|
|
Offset DataOff = Out.tell();
|
2008-12-03 09:16:39 +08:00
|
|
|
|
2009-01-15 09:26:25 +08:00
|
|
|
for (unsigned i = 0; i < idcount; ++i) {
|
|
|
|
IDData& d = IIDMap[i];
|
|
|
|
d.FileOffset = Out.tell(); // Record the location for this data.
|
|
|
|
unsigned len = d.II->getLength(); // Write out the string length.
|
2009-01-08 09:17:37 +08:00
|
|
|
Emit32(len);
|
2009-01-15 09:26:25 +08:00
|
|
|
const char* buf = d.II->getName(); // Write out the string data.
|
2009-01-16 02:47:46 +08:00
|
|
|
EmitBuf(buf, buf+len);
|
|
|
|
// Emit a null character for those clients expecting that IdentifierInfo
|
|
|
|
// strings are null terminated.
|
|
|
|
Emit8('\0');
|
2008-10-21 08:54:44 +08:00
|
|
|
}
|
2008-11-26 11:36:26 +08:00
|
|
|
|
2008-11-27 07:58:26 +08:00
|
|
|
// Now emit the table mapping from persistent IDs to PTH file offsets.
|
2008-12-03 03:44:08 +08:00
|
|
|
Offset IDOff = Out.tell();
|
2009-01-15 09:26:25 +08:00
|
|
|
Emit32(idcount); // Emit the number of identifiers.
|
|
|
|
for (unsigned i = 0 ; i < idcount; ++i) Emit32(IIDMap[i].FileOffset);
|
2008-12-03 09:16:39 +08:00
|
|
|
|
2009-01-15 09:26:25 +08:00
|
|
|
return std::make_pair(DataOff, std::make_pair(IDOff, LexicalOff));
|
2008-10-21 08:54:44 +08:00
|
|
|
}
|
|
|
|
|
2008-11-26 11:36:26 +08:00
|
|
|
|
2009-01-08 10:44:06 +08:00
|
|
|
PCHEntry PTHWriter::LexTokens(Lexer& L) {
|
2009-01-20 07:13:15 +08:00
|
|
|
// Pad 0's so that we emit tokens to a 4-byte alignment.
|
|
|
|
// This speed up reading them back in.
|
2009-02-10 09:06:17 +08:00
|
|
|
Pad(Out, 4);
|
|
|
|
Offset off = (Offset) Out.tell();
|
2008-12-12 07:36:38 +08:00
|
|
|
|
|
|
|
// Keep track of matching '#if' ... '#endif'.
|
|
|
|
typedef std::vector<std::pair<Offset, unsigned> > PPCondTable;
|
|
|
|
PPCondTable PPCond;
|
2008-12-13 02:31:09 +08:00
|
|
|
std::vector<unsigned> PPStartCond;
|
2008-12-23 09:30:52 +08:00
|
|
|
bool ParsingPreprocessorDirective = false;
|
2008-11-26 11:36:26 +08:00
|
|
|
Token Tok;
|
|
|
|
|
|
|
|
do {
|
|
|
|
L.LexFromRawLexer(Tok);
|
|
|
|
|
2008-12-23 09:30:52 +08:00
|
|
|
if ((Tok.isAtStartOfLine() || Tok.is(tok::eof)) &&
|
|
|
|
ParsingPreprocessorDirective) {
|
|
|
|
// Insert an eom token into the token cache. It has the same
|
|
|
|
// position as the next token that is not on the same line as the
|
|
|
|
// preprocessor directive. Observe that we continue processing
|
|
|
|
// 'Tok' when we exit this branch.
|
|
|
|
Token Tmp = Tok;
|
|
|
|
Tmp.setKind(tok::eom);
|
|
|
|
Tmp.clearFlag(Token::StartOfLine);
|
|
|
|
Tmp.setIdentifierInfo(0);
|
2009-01-08 09:17:37 +08:00
|
|
|
EmitToken(Tmp);
|
2008-12-23 09:30:52 +08:00
|
|
|
ParsingPreprocessorDirective = false;
|
|
|
|
}
|
|
|
|
|
2008-11-26 11:36:26 +08:00
|
|
|
if (Tok.is(tok::identifier)) {
|
|
|
|
Tok.setIdentifierInfo(PP.LookUpIdentifierInfo(Tok));
|
2008-12-23 09:30:52 +08:00
|
|
|
continue;
|
2008-11-26 11:36:26 +08:00
|
|
|
}
|
2008-12-23 09:30:52 +08:00
|
|
|
|
|
|
|
if (Tok.is(tok::hash) && Tok.isAtStartOfLine()) {
|
2008-11-26 11:36:26 +08:00
|
|
|
// Special processing for #include. Store the '#' token and lex
|
|
|
|
// the next token.
|
2008-12-23 09:30:52 +08:00
|
|
|
assert(!ParsingPreprocessorDirective);
|
2008-12-12 07:36:38 +08:00
|
|
|
Offset HashOff = (Offset) Out.tell();
|
2009-01-08 09:17:37 +08:00
|
|
|
EmitToken(Tok);
|
2008-12-13 02:31:09 +08:00
|
|
|
|
|
|
|
// Get the next token.
|
2008-11-26 11:36:26 +08:00
|
|
|
L.LexFromRawLexer(Tok);
|
2008-12-23 09:30:52 +08:00
|
|
|
|
|
|
|
assert(!Tok.isAtStartOfLine());
|
2008-11-26 11:36:26 +08:00
|
|
|
|
|
|
|
// Did we see 'include'/'import'/'include_next'?
|
|
|
|
if (!Tok.is(tok::identifier))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
IdentifierInfo* II = PP.LookUpIdentifierInfo(Tok);
|
|
|
|
Tok.setIdentifierInfo(II);
|
|
|
|
tok::PPKeywordKind K = II->getPPKeywordID();
|
|
|
|
|
2008-12-23 09:30:52 +08:00
|
|
|
assert(K != tok::pp_not_keyword);
|
|
|
|
ParsingPreprocessorDirective = true;
|
|
|
|
|
|
|
|
switch (K) {
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
case tok::pp_include:
|
|
|
|
case tok::pp_import:
|
|
|
|
case tok::pp_include_next: {
|
2008-11-26 11:36:26 +08:00
|
|
|
// Save the 'include' token.
|
2009-01-08 09:17:37 +08:00
|
|
|
EmitToken(Tok);
|
2008-11-26 11:36:26 +08:00
|
|
|
// Lex the next token as an include string.
|
|
|
|
L.setParsingPreprocessorDirective(true);
|
|
|
|
L.LexIncludeFilename(Tok);
|
|
|
|
L.setParsingPreprocessorDirective(false);
|
2008-12-23 09:30:52 +08:00
|
|
|
assert(!Tok.isAtStartOfLine());
|
2008-11-26 11:36:26 +08:00
|
|
|
if (Tok.is(tok::identifier))
|
|
|
|
Tok.setIdentifierInfo(PP.LookUpIdentifierInfo(Tok));
|
2008-12-23 09:30:52 +08:00
|
|
|
|
|
|
|
break;
|
2008-11-26 11:36:26 +08:00
|
|
|
}
|
2008-12-23 09:30:52 +08:00
|
|
|
case tok::pp_if:
|
|
|
|
case tok::pp_ifdef:
|
|
|
|
case tok::pp_ifndef: {
|
2008-12-12 07:36:38 +08:00
|
|
|
// Ad an entry for '#if' and friends. We initially set the target index
|
|
|
|
// to 0. This will get backpatched when we hit #endif.
|
|
|
|
PPStartCond.push_back(PPCond.size());
|
2008-12-13 02:31:09 +08:00
|
|
|
PPCond.push_back(std::make_pair(HashOff, 0U));
|
2008-12-23 09:30:52 +08:00
|
|
|
break;
|
2008-12-12 07:36:38 +08:00
|
|
|
}
|
2008-12-23 09:30:52 +08:00
|
|
|
case tok::pp_endif: {
|
2008-12-12 07:36:38 +08:00
|
|
|
// Add an entry for '#endif'. We set the target table index to itself.
|
2008-12-13 02:31:09 +08:00
|
|
|
// This will later be set to zero when emitting to the PTH file. We
|
|
|
|
// use 0 for uninitialized indices because that is easier to debug.
|
2008-12-12 07:36:38 +08:00
|
|
|
unsigned index = PPCond.size();
|
|
|
|
// Backpatch the opening '#if' entry.
|
2008-12-13 02:31:09 +08:00
|
|
|
assert(!PPStartCond.empty());
|
|
|
|
assert(PPCond.size() > PPStartCond.back());
|
2008-12-12 07:36:38 +08:00
|
|
|
assert(PPCond[PPStartCond.back()].second == 0);
|
|
|
|
PPCond[PPStartCond.back()].second = index;
|
|
|
|
PPStartCond.pop_back();
|
2008-12-13 02:31:09 +08:00
|
|
|
// Add the new entry to PPCond.
|
|
|
|
PPCond.push_back(std::make_pair(HashOff, index));
|
2008-12-23 09:30:52 +08:00
|
|
|
break;
|
2008-12-12 07:36:38 +08:00
|
|
|
}
|
2008-12-23 09:30:52 +08:00
|
|
|
case tok::pp_elif:
|
|
|
|
case tok::pp_else: {
|
|
|
|
// Add an entry for #elif or #else.
|
2008-12-13 02:31:09 +08:00
|
|
|
// This serves as both a closing and opening of a conditional block.
|
|
|
|
// This means that its entry will get backpatched later.
|
2008-12-12 07:36:38 +08:00
|
|
|
unsigned index = PPCond.size();
|
|
|
|
// Backpatch the previous '#if' entry.
|
2008-12-13 02:31:09 +08:00
|
|
|
assert(!PPStartCond.empty());
|
|
|
|
assert(PPCond.size() > PPStartCond.back());
|
2008-12-12 07:36:38 +08:00
|
|
|
assert(PPCond[PPStartCond.back()].second == 0);
|
|
|
|
PPCond[PPStartCond.back()].second = index;
|
|
|
|
PPStartCond.pop_back();
|
|
|
|
// Now add '#elif' as a new block opening.
|
2008-12-13 02:31:09 +08:00
|
|
|
PPCond.push_back(std::make_pair(HashOff, 0U));
|
|
|
|
PPStartCond.push_back(index);
|
2008-12-23 09:30:52 +08:00
|
|
|
break;
|
|
|
|
}
|
2008-12-12 07:36:38 +08:00
|
|
|
}
|
2008-11-26 11:36:26 +08:00
|
|
|
}
|
|
|
|
}
|
2009-01-08 09:17:37 +08:00
|
|
|
while (EmitToken(Tok), Tok.isNot(tok::eof));
|
|
|
|
|
2008-12-13 02:31:09 +08:00
|
|
|
assert(PPStartCond.empty() && "Error: imblanced preprocessor conditionals.");
|
2009-01-08 09:17:37 +08:00
|
|
|
|
2008-12-12 07:36:38 +08:00
|
|
|
// Next write out PPCond.
|
|
|
|
Offset PPCondOff = (Offset) Out.tell();
|
2008-12-13 02:31:09 +08:00
|
|
|
|
|
|
|
// Write out the size of PPCond so that clients can identifer empty tables.
|
2009-01-08 09:17:37 +08:00
|
|
|
Emit32(PPCond.size());
|
2008-12-03 03:44:08 +08:00
|
|
|
|
2008-12-13 02:31:09 +08:00
|
|
|
for (unsigned i = 0, e = PPCond.size(); i!=e; ++i) {
|
2009-01-08 09:17:37 +08:00
|
|
|
Emit32(PPCond[i].first - off);
|
2008-12-13 02:31:09 +08:00
|
|
|
uint32_t x = PPCond[i].second;
|
|
|
|
assert(x != 0 && "PPCond entry not backpatched.");
|
|
|
|
// Emit zero for #endifs. This allows us to do checking when
|
|
|
|
// we read the PTH file back in.
|
2009-01-08 09:17:37 +08:00
|
|
|
Emit32(x == i ? 0 : x);
|
2008-12-12 07:36:38 +08:00
|
|
|
}
|
|
|
|
|
2009-01-27 08:01:05 +08:00
|
|
|
return PCHEntry(off, PPCondOff);
|
2009-01-08 10:44:06 +08:00
|
|
|
}
|
|
|
|
|
2009-01-27 08:01:05 +08:00
|
|
|
Offset PTHWriter::EmitCachedSpellings() {
|
|
|
|
// Write each cached strings to the PTH file.
|
|
|
|
Offset SpellingsOff = Out.tell();
|
|
|
|
|
|
|
|
for (std::vector<llvm::StringMapEntry<OffsetOpt>*>::iterator
|
|
|
|
I = StrEntries.begin(), E = StrEntries.end(); I!=E; ++I) {
|
2009-01-08 10:44:06 +08:00
|
|
|
|
2009-01-27 08:01:05 +08:00
|
|
|
const char* data = (*I)->getKeyData();
|
|
|
|
EmitBuf(data, data + (*I)->getKeyLength());
|
|
|
|
Emit8('\0');
|
2009-01-08 10:44:06 +08:00
|
|
|
}
|
|
|
|
|
2009-01-27 08:01:05 +08:00
|
|
|
return SpellingsOff;
|
2008-11-26 11:36:26 +08:00
|
|
|
}
|
2008-10-21 08:54:44 +08:00
|
|
|
|
2009-01-08 09:17:37 +08:00
|
|
|
void PTHWriter::GeneratePTH() {
|
2009-01-27 05:43:14 +08:00
|
|
|
// Generate the prologue.
|
|
|
|
Out << "cfe-pth";
|
2009-01-27 05:50:21 +08:00
|
|
|
Emit32(PTHManager::Version);
|
2009-01-27 05:43:14 +08:00
|
|
|
Offset JumpOffset = Out.tell();
|
|
|
|
Emit32(0);
|
|
|
|
|
2008-10-21 08:54:44 +08:00
|
|
|
// Iterate over all the files in SourceManager. Create a lexer
|
|
|
|
// for each file and cache the tokens.
|
2009-01-17 11:48:08 +08:00
|
|
|
SourceManager &SM = PP.getSourceManager();
|
|
|
|
const LangOptions &LOpts = PP.getLangOptions();
|
2008-10-21 08:54:44 +08:00
|
|
|
|
2009-01-17 11:48:08 +08:00
|
|
|
for (SourceManager::fileinfo_iterator I = SM.fileinfo_begin(),
|
|
|
|
E = SM.fileinfo_end(); I != E; ++I) {
|
2009-02-03 15:30:45 +08:00
|
|
|
const SrcMgr::ContentCache &C = *I->second;
|
2009-01-17 11:48:08 +08:00
|
|
|
const FileEntry *FE = C.Entry;
|
2008-12-03 03:44:08 +08:00
|
|
|
|
|
|
|
// FIXME: Handle files with non-absolute paths.
|
|
|
|
llvm::sys::Path P(FE->getName());
|
|
|
|
if (!P.isAbsolute())
|
|
|
|
continue;
|
2008-11-26 11:36:26 +08:00
|
|
|
|
2009-01-17 11:49:48 +08:00
|
|
|
assert(!PM.count(FE) && "fileinfo's are not uniqued on FileEntry?");
|
2008-10-21 08:54:44 +08:00
|
|
|
|
2009-01-17 11:48:08 +08:00
|
|
|
const llvm::MemoryBuffer *B = C.getBuffer();
|
2008-11-26 11:36:26 +08:00
|
|
|
if (!B) continue;
|
2008-12-12 07:36:38 +08:00
|
|
|
|
2009-01-17 14:22:33 +08:00
|
|
|
FileID FID = SM.createFileID(FE, SourceLocation(), SrcMgr::C_User);
|
2009-01-17 15:35:14 +08:00
|
|
|
Lexer L(FID, SM, LOpts);
|
2009-01-08 09:17:37 +08:00
|
|
|
PM[FE] = LexTokens(L);
|
2008-11-26 10:18:33 +08:00
|
|
|
}
|
2008-11-26 11:36:26 +08:00
|
|
|
|
|
|
|
// Write out the identifier table.
|
2009-01-15 09:26:25 +08:00
|
|
|
const std::pair<Offset, std::pair<Offset,Offset> >& IdTableOff
|
|
|
|
= EmitIdentifierTable();
|
2008-11-26 11:36:26 +08:00
|
|
|
|
2009-01-08 10:44:06 +08:00
|
|
|
// Write out the cached strings table.
|
2009-01-27 08:01:05 +08:00
|
|
|
Offset SpellingOff = EmitCachedSpellings();
|
2009-01-08 10:44:06 +08:00
|
|
|
|
2008-11-26 11:36:26 +08:00
|
|
|
// Write out the file table.
|
2009-01-08 09:17:37 +08:00
|
|
|
Offset FileTableOff = EmitFileTable();
|
2008-10-21 08:54:44 +08:00
|
|
|
|
2008-11-26 11:36:26 +08:00
|
|
|
// Finally, write out the offset table at the end.
|
2009-01-27 05:43:14 +08:00
|
|
|
Offset JumpTargetOffset = Out.tell();
|
2009-01-08 09:17:37 +08:00
|
|
|
Emit32(IdTableOff.first);
|
2009-01-15 09:26:25 +08:00
|
|
|
Emit32(IdTableOff.second.first);
|
|
|
|
Emit32(IdTableOff.second.second);
|
2009-01-08 09:17:37 +08:00
|
|
|
Emit32(FileTableOff);
|
2009-01-27 08:01:05 +08:00
|
|
|
Emit32(SpellingOff);
|
2009-01-27 05:43:14 +08:00
|
|
|
|
|
|
|
// Now write the offset in the prologue.
|
|
|
|
Out.seek(JumpOffset);
|
|
|
|
Emit32(JumpTargetOffset);
|
2009-01-08 09:17:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void clang::CacheTokens(Preprocessor& PP, const std::string& OutFile) {
|
|
|
|
// Lex through the entire file. This will populate SourceManager with
|
|
|
|
// all of the header information.
|
|
|
|
Token Tok;
|
|
|
|
PP.EnterMainSourceFile();
|
|
|
|
do { PP.Lex(Tok); } while (Tok.isNot(tok::eof));
|
|
|
|
|
|
|
|
// Open up the PTH file.
|
|
|
|
std::string ErrMsg;
|
|
|
|
llvm::raw_fd_ostream Out(OutFile.c_str(), true, ErrMsg);
|
|
|
|
|
|
|
|
if (!ErrMsg.empty()) {
|
|
|
|
llvm::errs() << "PTH error: " << ErrMsg << "\n";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the PTHWriter and generate the PTH file.
|
|
|
|
PTHWriter PW(Out, PP);
|
|
|
|
PW.GeneratePTH();
|
2008-10-21 08:54:44 +08:00
|
|
|
}
|
2009-02-10 09:06:17 +08:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// On Disk Hashtable Logic. This will eventually get refactored and put
|
|
|
|
// elsewhere.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
template<typename Info>
|
|
|
|
class OnDiskChainedHashTableGenerator {
|
|
|
|
unsigned NumBuckets;
|
|
|
|
unsigned NumEntries;
|
|
|
|
llvm::BumpPtrAllocator BA;
|
|
|
|
|
|
|
|
class Item {
|
|
|
|
public:
|
|
|
|
typename Info::KeyT key;
|
|
|
|
typename Info::DataT data;
|
|
|
|
Item *next;
|
|
|
|
const uint32_t hash;
|
|
|
|
|
|
|
|
Item(typename Info::KeyT_ref k, typename Info::DataT_ref d)
|
|
|
|
: key(k), data(d), next(0), hash(Info::getHash(k)) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
class Bucket {
|
|
|
|
public:
|
|
|
|
Offset off;
|
|
|
|
Item* head;
|
|
|
|
unsigned length;
|
|
|
|
|
|
|
|
Bucket() {}
|
|
|
|
};
|
|
|
|
|
|
|
|
Bucket* Buckets;
|
|
|
|
|
|
|
|
private:
|
|
|
|
void insert(Item** b, size_t size, Item* E) {
|
|
|
|
unsigned idx = E->hash & (size - 1);
|
|
|
|
Bucket& B = b[idx];
|
|
|
|
E->next = B.head;
|
|
|
|
++B.length;
|
|
|
|
B.head = E;
|
|
|
|
}
|
|
|
|
|
|
|
|
void resize(size_t newsize) {
|
|
|
|
Bucket* newBuckets = calloc(newsize, sizeof(Bucket));
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < NumBuckets; ++i)
|
|
|
|
for (Item* E = Buckets[i]; E ; ) {
|
|
|
|
Item* N = E->next;
|
|
|
|
E->Next = 0;
|
|
|
|
insert(newBuckets, newsize, E);
|
|
|
|
E = N;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(Buckets);
|
|
|
|
NumBuckets = newsize;
|
|
|
|
Buckets = newBuckets;
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
void insert(typename Info::Key_ref key, typename Info::DataT_ref data) {
|
|
|
|
++NumEntries;
|
|
|
|
if (4*NumEntries >= 3*NumBuckets) resize(NumBuckets*2);
|
|
|
|
insert(Buckets, NumBuckets, new (BA.Allocate<Item>()) Item(key, data));
|
|
|
|
}
|
|
|
|
|
|
|
|
Offset Emit(llvm::raw_fd_ostream& out) {
|
|
|
|
// Emit the payload of the table.
|
|
|
|
for (unsigned i = 0; i < NumBuckets; ++i) {
|
|
|
|
Bucket& B = Buckets[i];
|
|
|
|
if (!B.head) continue;
|
|
|
|
|
|
|
|
// Store the offset for the data of this bucket.
|
|
|
|
Pad(out, 4); // 4-byte alignment.
|
|
|
|
B.off = out.tell();
|
|
|
|
|
|
|
|
// Write out the number of items in the bucket. We just write out
|
|
|
|
// 4 bytes to keep things 4-byte aligned.
|
|
|
|
Emit32(out, B.length);
|
|
|
|
|
|
|
|
// Write out the entries in the bucket.
|
|
|
|
for (Item *I = B.head; I ; I = I->next) {
|
|
|
|
Emit32(out, I->hash);
|
|
|
|
Info::EmitKey(out, I->key);
|
|
|
|
Info::EmitData(out, I->data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Emit the hashtable itself.
|
|
|
|
Pad(out, 4);
|
|
|
|
Offset TableOff = out.tell();
|
|
|
|
Emit32(out, NumBuckets);
|
|
|
|
for (unsigned i = 0; i < NumBuckets; ++i) Emit32(out, Buckets[i].off);
|
|
|
|
|
|
|
|
return TableOff;
|
|
|
|
}
|
|
|
|
|
|
|
|
OnDiskChainedHashTableGenerator() {
|
|
|
|
NumEntries = 0;
|
|
|
|
NumBuckets = 64;
|
|
|
|
Buckets = calloc(NumBuckets, sizeof(Bucket));
|
|
|
|
}
|
|
|
|
|
|
|
|
~OnDiskChainedHashTableGenerator() {
|
|
|
|
free(Buckets);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Client code of on-disk hashtable logic.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
Offset PTHWriter::EmitFileTable() {
|
|
|
|
// Determine the offset where this table appears in the PTH file.
|
|
|
|
Offset off = (Offset) Out.tell();
|
|
|
|
|
|
|
|
// Output the size of the table.
|
|
|
|
Emit32(PM.size());
|
|
|
|
|
|
|
|
for (PCHMap::iterator I=PM.begin(), E=PM.end(); I!=E; ++I) {
|
|
|
|
const FileEntry* FE = I->first;
|
|
|
|
const char* Name = FE->getName();
|
|
|
|
unsigned size = strlen(Name);
|
|
|
|
Emit32(size);
|
|
|
|
EmitBuf(Name, Name+size);
|
|
|
|
Emit32(I->second.getTokenOffset());
|
|
|
|
Emit32(I->second.getPPCondTableOffset());
|
|
|
|
}
|
|
|
|
|
|
|
|
return off;
|
|
|
|
}
|
|
|
|
|