2011-03-10 01:21:42 +08:00
|
|
|
//===- ChainedIncludesSource.cpp - Chained PCHs in Memory -------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines the ChainedIncludesSource class, which converts headers
|
|
|
|
// to chained PCHs in memory, mainly used for testing.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/Serialization/ChainedIncludesSource.h"
|
|
|
|
#include "clang/Serialization/ASTReader.h"
|
|
|
|
#include "clang/Serialization/ASTWriter.h"
|
|
|
|
#include "clang/Frontend/TextDiagnosticPrinter.h"
|
|
|
|
#include "clang/Frontend/CompilerInstance.h"
|
|
|
|
#include "clang/Frontend/ASTUnit.h"
|
|
|
|
#include "clang/Parse/ParseAST.h"
|
|
|
|
#include "clang/Lex/Preprocessor.h"
|
|
|
|
#include "clang/Basic/TargetInfo.h"
|
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
|
|
|
|
static ASTReader *createASTReader(CompilerInstance &CI,
|
2011-08-03 01:40:32 +08:00
|
|
|
StringRef pchFile,
|
|
|
|
SmallVector<llvm::MemoryBuffer *, 4> &memBufs,
|
|
|
|
SmallVector<std::string, 4> &bufNames,
|
2011-03-10 01:21:42 +08:00
|
|
|
ASTDeserializationListener *deserialListener = 0) {
|
|
|
|
Preprocessor &PP = CI.getPreprocessor();
|
|
|
|
llvm::OwningPtr<ASTReader> Reader;
|
2011-07-23 00:35:34 +08:00
|
|
|
Reader.reset(new ASTReader(PP, &CI.getASTContext(), /*isysroot=*/"",
|
2011-03-10 01:21:42 +08:00
|
|
|
/*DisableValidation=*/true));
|
2011-08-03 01:40:32 +08:00
|
|
|
for (unsigned ti = 0; ti < bufNames.size(); ++ti) {
|
|
|
|
StringRef sr(bufNames[ti]);
|
|
|
|
Reader->addInMemoryBuffer(sr, memBufs[ti]);
|
|
|
|
}
|
2011-03-10 01:21:42 +08:00
|
|
|
Reader->setDeserializationListener(deserialListener);
|
2011-07-23 00:00:58 +08:00
|
|
|
switch (Reader->ReadAST(pchFile, serialization::MK_PCH)) {
|
2011-03-10 01:21:42 +08:00
|
|
|
case ASTReader::Success:
|
|
|
|
// Set the predefines buffer as suggested by the PCH reader.
|
|
|
|
PP.setPredefines(Reader->getSuggestedPredefines());
|
|
|
|
return Reader.take();
|
|
|
|
|
|
|
|
case ASTReader::Failure:
|
|
|
|
case ASTReader::IgnorePCH:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ChainedIncludesSource::~ChainedIncludesSource() {
|
|
|
|
for (unsigned i = 0, e = CIs.size(); i != e; ++i)
|
|
|
|
delete CIs[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
ChainedIncludesSource *ChainedIncludesSource::create(CompilerInstance &CI) {
|
|
|
|
|
|
|
|
std::vector<std::string> &includes = CI.getPreprocessorOpts().ChainedIncludes;
|
|
|
|
assert(!includes.empty() && "No '-chain-include' in options!");
|
|
|
|
|
|
|
|
llvm::OwningPtr<ChainedIncludesSource> source(new ChainedIncludesSource());
|
|
|
|
InputKind IK = CI.getFrontendOpts().Inputs[0].first;
|
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
SmallVector<llvm::MemoryBuffer *, 4> serialBufs;
|
2011-08-03 01:40:32 +08:00
|
|
|
SmallVector<std::string, 4> serialBufNames;
|
2011-03-10 01:21:42 +08:00
|
|
|
|
|
|
|
for (unsigned i = 0, e = includes.size(); i != e; ++i) {
|
|
|
|
bool firstInclude = (i == 0);
|
|
|
|
llvm::OwningPtr<CompilerInvocation> CInvok;
|
|
|
|
CInvok.reset(new CompilerInvocation(CI.getInvocation()));
|
|
|
|
|
|
|
|
CInvok->getPreprocessorOpts().ChainedIncludes.clear();
|
|
|
|
CInvok->getPreprocessorOpts().ImplicitPCHInclude.clear();
|
|
|
|
CInvok->getPreprocessorOpts().ImplicitPTHInclude.clear();
|
|
|
|
CInvok->getPreprocessorOpts().DisablePCHValidation = true;
|
|
|
|
CInvok->getPreprocessorOpts().Includes.clear();
|
|
|
|
CInvok->getPreprocessorOpts().MacroIncludes.clear();
|
|
|
|
CInvok->getPreprocessorOpts().Macros.clear();
|
|
|
|
|
|
|
|
CInvok->getFrontendOpts().Inputs.clear();
|
|
|
|
CInvok->getFrontendOpts().Inputs.push_back(std::make_pair(IK, includes[i]));
|
|
|
|
|
|
|
|
TextDiagnosticPrinter *DiagClient =
|
|
|
|
new TextDiagnosticPrinter(llvm::errs(), DiagnosticOptions());
|
|
|
|
llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
|
|
|
|
llvm::IntrusiveRefCntPtr<Diagnostic> Diags(new Diagnostic(DiagID,
|
|
|
|
DiagClient));
|
|
|
|
|
|
|
|
llvm::OwningPtr<CompilerInstance> Clang(new CompilerInstance());
|
|
|
|
Clang->setInvocation(CInvok.take());
|
|
|
|
Clang->setDiagnostics(Diags.getPtr());
|
|
|
|
Clang->setTarget(TargetInfo::CreateTargetInfo(Clang->getDiagnostics(),
|
|
|
|
Clang->getTargetOpts()));
|
|
|
|
Clang->createFileManager();
|
|
|
|
Clang->createSourceManager(Clang->getFileManager());
|
|
|
|
Clang->createPreprocessor();
|
2011-04-01 03:29:18 +08:00
|
|
|
Clang->getDiagnosticClient().BeginSourceFile(Clang->getLangOpts(),
|
|
|
|
&Clang->getPreprocessor());
|
2011-03-10 01:21:42 +08:00
|
|
|
Clang->createASTContext();
|
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
SmallVector<char, 256> serialAST;
|
2011-03-10 01:21:42 +08:00
|
|
|
llvm::raw_svector_ostream OS(serialAST);
|
|
|
|
llvm::OwningPtr<ASTConsumer> consumer;
|
|
|
|
consumer.reset(new PCHGenerator(Clang->getPreprocessor(), "-",
|
|
|
|
/*Chaining=*/!firstInclude,
|
2011-07-23 00:35:34 +08:00
|
|
|
/*isysroot=*/"", &OS));
|
2011-03-10 01:21:42 +08:00
|
|
|
Clang->getASTContext().setASTMutationListener(
|
|
|
|
consumer->GetASTMutationListener());
|
|
|
|
Clang->setASTConsumer(consumer.take());
|
|
|
|
Clang->createSema(/*CompleteTranslationUnit=*/false, 0);
|
|
|
|
|
|
|
|
if (firstInclude) {
|
|
|
|
Preprocessor &PP = Clang->getPreprocessor();
|
|
|
|
PP.getBuiltinInfo().InitializeBuiltins(PP.getIdentifierTable(),
|
|
|
|
PP.getLangOptions());
|
|
|
|
} else {
|
|
|
|
assert(!serialBufs.empty());
|
2011-07-23 18:55:15 +08:00
|
|
|
SmallVector<llvm::MemoryBuffer *, 4> bufs;
|
2011-03-10 07:15:22 +08:00
|
|
|
for (unsigned si = 0, se = serialBufs.size(); si != se; ++si) {
|
2011-03-10 01:21:42 +08:00
|
|
|
bufs.push_back(llvm::MemoryBuffer::getMemBufferCopy(
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef(serialBufs[si]->getBufferStart(),
|
2011-03-10 07:15:22 +08:00
|
|
|
serialBufs[si]->getBufferSize())));
|
2011-03-10 01:21:42 +08:00
|
|
|
}
|
|
|
|
std::string pchName = includes[i-1];
|
|
|
|
llvm::raw_string_ostream os(pchName);
|
|
|
|
os << ".pch" << i-1;
|
|
|
|
os.flush();
|
2011-08-03 01:40:32 +08:00
|
|
|
|
|
|
|
serialBufNames.push_back(pchName);
|
|
|
|
|
2011-03-10 01:21:42 +08:00
|
|
|
llvm::OwningPtr<ExternalASTSource> Reader;
|
2011-08-03 01:40:32 +08:00
|
|
|
|
|
|
|
Reader.reset(createASTReader(*Clang, pchName, bufs, serialBufNames,
|
|
|
|
Clang->getASTConsumer().GetASTDeserializationListener()));
|
2011-03-10 01:21:42 +08:00
|
|
|
if (!Reader)
|
|
|
|
return 0;
|
|
|
|
Clang->getASTContext().setExternalSource(Reader);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Clang->InitializeSourceManager(includes[i]))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
ParseAST(Clang->getSema());
|
|
|
|
OS.flush();
|
2011-04-01 03:29:18 +08:00
|
|
|
Clang->getDiagnosticClient().EndSourceFile();
|
2011-03-10 01:21:42 +08:00
|
|
|
serialBufs.push_back(
|
2011-07-23 18:55:15 +08:00
|
|
|
llvm::MemoryBuffer::getMemBufferCopy(StringRef(serialAST.data(),
|
2011-03-10 01:21:42 +08:00
|
|
|
serialAST.size())));
|
|
|
|
source->CIs.push_back(Clang.take());
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(!serialBufs.empty());
|
|
|
|
std::string pchName = includes.back() + ".pch-final";
|
2011-08-03 01:40:32 +08:00
|
|
|
serialBufNames.push_back(pchName);
|
2011-03-10 01:21:42 +08:00
|
|
|
llvm::OwningPtr<ASTReader> Reader;
|
2011-08-03 01:40:32 +08:00
|
|
|
Reader.reset(createASTReader(CI, pchName, serialBufs, serialBufNames));
|
2011-03-10 01:21:42 +08:00
|
|
|
if (!Reader)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
source->FinalReader.reset(Reader.take());
|
|
|
|
return source.take();
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// ExternalASTSource interface.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
Decl *ChainedIncludesSource::GetExternalDecl(uint32_t ID) {
|
|
|
|
return getFinalReader().GetExternalDecl(ID);
|
|
|
|
}
|
|
|
|
Selector ChainedIncludesSource::GetExternalSelector(uint32_t ID) {
|
|
|
|
return getFinalReader().GetExternalSelector(ID);
|
|
|
|
}
|
|
|
|
uint32_t ChainedIncludesSource::GetNumExternalSelectors() {
|
|
|
|
return getFinalReader().GetNumExternalSelectors();
|
|
|
|
}
|
|
|
|
Stmt *ChainedIncludesSource::GetExternalDeclStmt(uint64_t Offset) {
|
|
|
|
return getFinalReader().GetExternalDeclStmt(Offset);
|
|
|
|
}
|
|
|
|
CXXBaseSpecifier *
|
|
|
|
ChainedIncludesSource::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
|
|
|
|
return getFinalReader().GetExternalCXXBaseSpecifiers(Offset);
|
|
|
|
}
|
|
|
|
DeclContextLookupResult
|
|
|
|
ChainedIncludesSource::FindExternalVisibleDeclsByName(const DeclContext *DC,
|
|
|
|
DeclarationName Name) {
|
|
|
|
return getFinalReader().FindExternalVisibleDeclsByName(DC, Name);
|
|
|
|
}
|
|
|
|
void ChainedIncludesSource::MaterializeVisibleDecls(const DeclContext *DC) {
|
|
|
|
return getFinalReader().MaterializeVisibleDecls(DC);
|
|
|
|
}
|
2011-07-16 05:46:17 +08:00
|
|
|
ExternalLoadResult
|
|
|
|
ChainedIncludesSource::FindExternalLexicalDecls(const DeclContext *DC,
|
2011-03-10 01:21:42 +08:00
|
|
|
bool (*isKindWeWant)(Decl::Kind),
|
2011-07-23 18:55:15 +08:00
|
|
|
SmallVectorImpl<Decl*> &Result) {
|
2011-03-10 01:21:42 +08:00
|
|
|
return getFinalReader().FindExternalLexicalDecls(DC, isKindWeWant, Result);
|
|
|
|
}
|
|
|
|
void ChainedIncludesSource::CompleteType(TagDecl *Tag) {
|
|
|
|
return getFinalReader().CompleteType(Tag);
|
|
|
|
}
|
|
|
|
void ChainedIncludesSource::CompleteType(ObjCInterfaceDecl *Class) {
|
|
|
|
return getFinalReader().CompleteType(Class);
|
|
|
|
}
|
|
|
|
void ChainedIncludesSource::StartedDeserializing() {
|
|
|
|
return getFinalReader().StartedDeserializing();
|
|
|
|
}
|
|
|
|
void ChainedIncludesSource::FinishedDeserializing() {
|
|
|
|
return getFinalReader().FinishedDeserializing();
|
|
|
|
}
|
|
|
|
void ChainedIncludesSource::StartTranslationUnit(ASTConsumer *Consumer) {
|
|
|
|
return getFinalReader().StartTranslationUnit(Consumer);
|
|
|
|
}
|
|
|
|
void ChainedIncludesSource::PrintStats() {
|
|
|
|
return getFinalReader().PrintStats();
|
|
|
|
}
|
2011-04-29 07:46:20 +08:00
|
|
|
void ChainedIncludesSource::getMemoryBufferSizes(MemoryBufferSizes &sizes)const{
|
|
|
|
for (unsigned i = 0, e = CIs.size(); i != e; ++i) {
|
|
|
|
if (const ExternalASTSource *eSrc =
|
|
|
|
CIs[i]->getASTContext().getExternalSource()) {
|
|
|
|
eSrc->getMemoryBufferSizes(sizes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
getFinalReader().getMemoryBufferSizes(sizes);
|
|
|
|
}
|
2011-04-01 03:29:24 +08:00
|
|
|
|
|
|
|
void ChainedIncludesSource::InitializeSema(Sema &S) {
|
|
|
|
return getFinalReader().InitializeSema(S);
|
|
|
|
}
|
|
|
|
void ChainedIncludesSource::ForgetSema() {
|
|
|
|
return getFinalReader().ForgetSema();
|
|
|
|
}
|
|
|
|
std::pair<ObjCMethodList,ObjCMethodList>
|
|
|
|
ChainedIncludesSource::ReadMethodPool(Selector Sel) {
|
|
|
|
return getFinalReader().ReadMethodPool(Sel);
|
|
|
|
}
|
|
|
|
bool ChainedIncludesSource::LookupUnqualified(LookupResult &R, Scope *S) {
|
|
|
|
return getFinalReader().LookupUnqualified(R, S);
|
|
|
|
}
|
|
|
|
|