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"
|
2008-10-21 08:54:44 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
|
2008-12-03 03:44:08 +08:00
|
|
|
typedef uint32_t Offset;
|
|
|
|
|
2009-01-09 08:37:37 +08:00
|
|
|
typedef std::vector<std::pair<Offset, llvm::StringMapEntry<Offset>*> >
|
2009-01-08 10:44:06 +08:00
|
|
|
SpellMapTy;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class VISIBILITY_HIDDEN PCHEntry {
|
|
|
|
Offset TokenData, PPCondData;
|
|
|
|
union { Offset SpellingOff; SpellMapTy* Spellings; };
|
|
|
|
|
|
|
|
public:
|
|
|
|
PCHEntry() {}
|
|
|
|
|
|
|
|
PCHEntry(Offset td, Offset ppcd, SpellMapTy* sp)
|
|
|
|
: TokenData(td), PPCondData(ppcd), Spellings(sp) {}
|
|
|
|
|
|
|
|
Offset getTokenOffset() const { return TokenData; }
|
|
|
|
Offset getPPCondTableOffset() const { return PPCondData; }
|
|
|
|
SpellMapTy& getSpellings() const { return *Spellings; }
|
|
|
|
|
|
|
|
void setSpellingTableOffset(Offset off) { SpellingOff = off; }
|
|
|
|
Offset getSpellingTableOffset() const { return SpellingOff; }
|
|
|
|
|
|
|
|
};
|
|
|
|
} // 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-09 08:37:37 +08:00
|
|
|
typedef llvm::StringMap<Offset, 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;
|
|
|
|
|
|
|
|
SpellMapTy* CurSpellMap;
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Emit16(uint32_t V) {
|
|
|
|
Out << (unsigned char)(V);
|
|
|
|
Out << (unsigned char)(V >> 8);
|
|
|
|
assert((V >> 16) == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
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-01-08 09:17:37 +08:00
|
|
|
void Emit32(uint32_t V) {
|
|
|
|
Out << (unsigned char)(V);
|
|
|
|
Out << (unsigned char)(V >> 8);
|
|
|
|
Out << (unsigned char)(V >> 16);
|
|
|
|
Out << (unsigned char)(V >> 24);
|
|
|
|
}
|
|
|
|
|
|
|
|
void EmitBuf(const char* I, const char* E) {
|
|
|
|
for ( ; I != E ; ++I) Out << *I;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::pair<Offset,Offset> EmitIdentifierTable();
|
|
|
|
Offset EmitFileTable();
|
2009-01-08 10:44:06 +08:00
|
|
|
PCHEntry LexTokens(Lexer& L);
|
|
|
|
void EmitCachedSpellings();
|
|
|
|
|
2009-01-08 09:17:37 +08:00
|
|
|
public:
|
|
|
|
PTHWriter(llvm::raw_fd_ostream& out, Preprocessor& pp)
|
|
|
|
: Out(out), PP(pp), idcount(0) {}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
uint32_t fpos = PP.getSourceManager().getFullFilePos(T.getLocation());
|
|
|
|
Emit8(T.getKind());
|
|
|
|
Emit8(T.getFlags());
|
|
|
|
Emit24(ResolveID(T.getIdentifierInfo()));
|
|
|
|
Emit32(fpos);
|
|
|
|
Emit16(T.getLength());
|
|
|
|
|
|
|
|
// For specific tokens we cache their spelling.
|
|
|
|
if (T.getIdentifierInfo())
|
|
|
|
return;
|
|
|
|
|
|
|
|
switch (T.getKind()) {
|
2009-01-09 08:37:37 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
case tok::string_literal:
|
2009-01-08 09:17:37 +08:00
|
|
|
case tok::wide_string_literal:
|
|
|
|
case tok::angle_string_literal:
|
2009-01-09 08:37:37 +08:00
|
|
|
case tok::numeric_constant:
|
2009-01-08 10:44:06 +08:00
|
|
|
case tok::char_constant: {
|
|
|
|
// FIXME: This uses the slow getSpelling(). Perhaps we do better
|
|
|
|
// in the future? This only slows down PTH generation.
|
2009-01-09 08:37:37 +08:00
|
|
|
const std::string& spelling = PP.getSpelling(T);
|
2009-01-08 10:44:06 +08:00
|
|
|
const char* s = spelling.c_str();
|
|
|
|
|
|
|
|
// Get the string entry.
|
2009-01-09 08:37:37 +08:00
|
|
|
llvm::StringMapEntry<Offset> *E =
|
|
|
|
&CachedStrs.GetOrCreateValue(s, s+spelling.size());
|
2009-01-08 10:44:06 +08:00
|
|
|
|
|
|
|
// Store the address of the string entry in our spelling map.
|
|
|
|
(*CurSpellMap).push_back(std::make_pair(fpos, E));
|
|
|
|
|
2009-01-08 09:17:37 +08:00
|
|
|
break;
|
2009-01-08 10:44:06 +08:00
|
|
|
}
|
2009-01-08 09:17:37 +08:00
|
|
|
}
|
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;
|
|
|
|
const IdentifierTable::const_iterator::value_type* Str;
|
|
|
|
};
|
2009-01-08 09:17:37 +08:00
|
|
|
}
|
2008-11-27 07:58:26 +08:00
|
|
|
|
2009-01-08 09:17:37 +08:00
|
|
|
std::pair<Offset,Offset> PTHWriter::EmitIdentifierTable() {
|
|
|
|
|
|
|
|
const IdentifierTable& T = PP.getIdentifierTable();
|
2008-11-26 11:36:26 +08:00
|
|
|
|
2008-11-27 07:58:26 +08:00
|
|
|
// Build an inverse map from persistent IDs -> IdentifierInfo*.
|
2008-12-03 03:44:08 +08:00
|
|
|
typedef std::vector<IDData> InverseIDMap;
|
2008-11-27 07:58:26 +08:00
|
|
|
InverseIDMap IIDMap;
|
2009-01-08 09:17:37 +08:00
|
|
|
IIDMap.resize(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);
|
|
|
|
assert(I->second-1 < IIDMap.size());
|
|
|
|
IIDMap[I->second-1].II = I->first;
|
|
|
|
}
|
2008-10-21 08:54:44 +08:00
|
|
|
|
2008-11-27 07:58:26 +08:00
|
|
|
// Get the string data associated with the IdentifierInfo.
|
|
|
|
for (IdentifierTable::const_iterator I=T.begin(), E=T.end(); I!=E; ++I) {
|
2009-01-08 09:17:37 +08:00
|
|
|
IDMap::iterator IDI = IM.find(&(I->getValue()));
|
2008-11-27 07:58:26 +08:00
|
|
|
if (IDI == IM.end()) continue;
|
2008-12-03 03:44:08 +08:00
|
|
|
IIDMap[IDI->second-1].Str = &(*I);
|
2008-11-27 07:58:26 +08:00
|
|
|
}
|
|
|
|
|
2008-12-03 03:44:08 +08:00
|
|
|
Offset DataOff = Out.tell();
|
2008-12-03 09:16:39 +08:00
|
|
|
|
2008-11-27 07:58:26 +08:00
|
|
|
for (InverseIDMap::iterator I=IIDMap.begin(), E=IIDMap.end(); I!=E; ++I) {
|
2008-12-03 03:44:08 +08:00
|
|
|
// Record the location for this data.
|
|
|
|
I->FileOffset = Out.tell();
|
|
|
|
// Write out the keyword.
|
|
|
|
unsigned len = I->Str->getKeyLength();
|
2009-01-08 09:17:37 +08:00
|
|
|
Emit32(len);
|
2008-11-27 07:58:26 +08:00
|
|
|
const char* buf = I->Str->getKeyData();
|
2009-01-08 09:17:37 +08:00
|
|
|
EmitBuf(buf, buf+len);
|
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();
|
2008-11-27 07:58:26 +08:00
|
|
|
|
2008-12-03 09:16:39 +08:00
|
|
|
// Emit the number of identifiers.
|
2009-01-08 09:17:37 +08:00
|
|
|
Emit32(idcount);
|
2008-12-03 09:16:39 +08:00
|
|
|
|
2008-11-27 07:58:26 +08:00
|
|
|
for (InverseIDMap::iterator I=IIDMap.begin(), E=IIDMap.end(); I!=E; ++I)
|
2009-01-08 09:17:37 +08:00
|
|
|
Emit32(I->FileOffset);
|
2008-12-03 03:44:08 +08:00
|
|
|
|
2008-11-27 07:58:26 +08:00
|
|
|
return std::make_pair(DataOff, IDOff);
|
2008-10-21 08:54:44 +08:00
|
|
|
}
|
|
|
|
|
2009-01-08 09:17:37 +08:00
|
|
|
Offset PTHWriter::EmitFileTable() {
|
|
|
|
// Determine the offset where this table appears in the PTH file.
|
2008-12-03 03:44:08 +08:00
|
|
|
Offset off = (Offset) Out.tell();
|
2009-01-08 09:17:37 +08:00
|
|
|
|
2008-11-27 07:58:26 +08:00
|
|
|
// Output the size of the table.
|
2009-01-08 09:17:37 +08:00
|
|
|
Emit32(PM.size());
|
2008-11-27 07:58:26 +08:00
|
|
|
|
|
|
|
for (PCHMap::iterator I=PM.begin(), E=PM.end(); I!=E; ++I) {
|
|
|
|
const FileEntry* FE = I->first;
|
2008-12-05 06:36:44 +08:00
|
|
|
const char* Name = FE->getName();
|
|
|
|
unsigned size = strlen(Name);
|
2009-01-08 09:17:37 +08:00
|
|
|
Emit32(size);
|
|
|
|
EmitBuf(Name, Name+size);
|
2009-01-08 10:44:06 +08:00
|
|
|
Emit32(I->second.getTokenOffset());
|
|
|
|
Emit32(I->second.getPPCondTableOffset());
|
|
|
|
Emit32(I->second.getSpellingTableOffset());
|
2008-11-27 07:58:26 +08:00
|
|
|
}
|
|
|
|
|
2008-11-26 11:36:26 +08:00
|
|
|
return off;
|
|
|
|
}
|
|
|
|
|
2009-01-08 10:44:06 +08:00
|
|
|
PCHEntry PTHWriter::LexTokens(Lexer& L) {
|
2009-01-08 09:17:37 +08:00
|
|
|
|
2008-11-26 11:36:26 +08:00
|
|
|
// Record the location within the token file.
|
2008-12-03 03:44:08 +08:00
|
|
|
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
|
|
|
|
2009-01-08 10:44:06 +08:00
|
|
|
// Allocate a spelling map for this source file.
|
|
|
|
llvm::OwningPtr<SpellMapTy> Spellings(new SpellMapTy());
|
|
|
|
CurSpellMap = Spellings.get();
|
|
|
|
|
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-08 10:44:06 +08:00
|
|
|
return PCHEntry(off, PPCondOff, Spellings.take());
|
|
|
|
}
|
|
|
|
|
|
|
|
void PTHWriter::EmitCachedSpellings() {
|
|
|
|
// Write each cached string to the PTH file and update the
|
|
|
|
// the string map entry to contain the relevant offset.
|
|
|
|
//
|
|
|
|
// FIXME: We can write the strings out in order of their frequency. This
|
|
|
|
// may result in better locality.
|
|
|
|
//
|
|
|
|
for (CachedStrsTy::iterator I = CachedStrs.begin(), E = CachedStrs.end();
|
|
|
|
I!=E; ++I) {
|
|
|
|
|
|
|
|
Offset off = Out.tell();
|
|
|
|
|
|
|
|
// Write out the length of the string before the string itself.
|
|
|
|
unsigned len = I->getKeyLength();
|
2009-01-09 08:37:37 +08:00
|
|
|
Emit16(len);
|
2009-01-08 10:44:06 +08:00
|
|
|
|
|
|
|
// Write out the string data.
|
|
|
|
const char* data = I->getKeyData();
|
|
|
|
EmitBuf(data, data+len);
|
|
|
|
|
2009-01-09 08:37:37 +08:00
|
|
|
// Write out a single blank character.
|
|
|
|
Emit8(' ');
|
|
|
|
|
2009-01-08 10:44:06 +08:00
|
|
|
// Now patch the offset of the string in the PTH file into the string map.
|
2009-01-09 08:37:37 +08:00
|
|
|
I->setValue(off);
|
2009-01-08 10:44:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Now emit the spelling tables.
|
|
|
|
for (PCHMap::iterator I=PM.begin(), E=PM.end(); I!=E; ++I) {
|
|
|
|
SpellMapTy& spellings = I->second.getSpellings();
|
|
|
|
I->second.setSpellingTableOffset(Out.tell());
|
|
|
|
|
|
|
|
// Write out the number of spellings.
|
|
|
|
unsigned n = spellings.size();
|
|
|
|
Emit32(n);
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < n; ++i) {
|
|
|
|
// Write out the offset of the token within the source file.
|
|
|
|
Emit32(spellings[i].first);
|
|
|
|
|
|
|
|
// Write out the offset of the spelling data within the PTH file.
|
2009-01-09 08:37:37 +08:00
|
|
|
Emit32(spellings[i].second->getValue());
|
2009-01-08 10:44:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete the spelling map for this source file.
|
|
|
|
delete &spellings;
|
|
|
|
}
|
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() {
|
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.
|
|
|
|
SourceManager& SM = PP.getSourceManager();
|
|
|
|
const LangOptions& LOpts = PP.getLangOptions();
|
|
|
|
|
|
|
|
for (SourceManager::fileid_iterator I=SM.fileid_begin(), E=SM.fileid_end();
|
|
|
|
I!=E; ++I) {
|
|
|
|
|
|
|
|
const SrcMgr::ContentCache* C = I.getFileIDInfo().getContentCache();
|
2008-11-26 11:36:26 +08:00
|
|
|
if (!C) continue;
|
2008-11-26 08:57:55 +08:00
|
|
|
|
2008-11-26 11:36:26 +08:00
|
|
|
const FileEntry* FE = C->Entry; // Does this entry correspond to a file?
|
|
|
|
if (!FE) continue;
|
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
|
|
|
|
|
|
|
PCHMap::iterator PI = PM.find(FE); // Have we already processed this file?
|
|
|
|
if (PI != PM.end()) continue;
|
2008-10-21 08:54:44 +08:00
|
|
|
|
2009-01-06 12:47:20 +08:00
|
|
|
const llvm::MemoryBuffer* B = C->getBuffer();
|
2008-11-26 11:36:26 +08:00
|
|
|
if (!B) continue;
|
2008-10-21 08:54:44 +08:00
|
|
|
|
|
|
|
Lexer L(SourceLocation::getFileLoc(I.getFileID(), 0), LOpts,
|
|
|
|
B->getBufferStart(), B->getBufferEnd(), B);
|
2008-12-12 07:36:38 +08:00
|
|
|
|
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-08 09:17:37 +08:00
|
|
|
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.
|
|
|
|
EmitCachedSpellings();
|
|
|
|
|
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-08 09:17:37 +08:00
|
|
|
Emit32(IdTableOff.first);
|
|
|
|
Emit32(IdTableOff.second);
|
|
|
|
Emit32(FileTableOff);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|