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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/Basic/TargetInfo.h"
|
2011-03-10 01:21:42 +08:00
|
|
|
#include "clang/Frontend/ASTUnit.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/Frontend/CompilerInstance.h"
|
|
|
|
#include "clang/Frontend/TextDiagnosticPrinter.h"
|
|
|
|
#include "clang/Lex/Preprocessor.h"
|
|
|
|
#include "clang/Parse/ParseAST.h"
|
2011-12-09 09:55:54 +08:00
|
|
|
#include "clang/Serialization/ASTReader.h"
|
|
|
|
#include "clang/Serialization/ASTWriter.h"
|
2011-03-10 01:21:42 +08:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
|
2014-07-07 19:07:10 +08:00
|
|
|
namespace {
|
|
|
|
class ChainedIncludesSource : public ExternalSemaSource {
|
|
|
|
public:
|
2015-04-11 10:00:23 +08:00
|
|
|
~ChainedIncludesSource() override;
|
2014-07-07 19:07:10 +08:00
|
|
|
|
|
|
|
ExternalSemaSource &getFinalReader() const { return *FinalReader; }
|
|
|
|
|
|
|
|
std::vector<CompilerInstance *> CIs;
|
|
|
|
IntrusiveRefCntPtr<ExternalSemaSource> FinalReader;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// ExternalASTSource interface.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
Decl *GetExternalDecl(uint32_t ID) override;
|
|
|
|
Selector GetExternalSelector(uint32_t ID) override;
|
|
|
|
uint32_t GetNumExternalSelectors() override;
|
|
|
|
Stmt *GetExternalDeclStmt(uint64_t Offset) override;
|
2015-03-24 14:36:48 +08:00
|
|
|
CXXCtorInitializer **GetExternalCXXCtorInitializers(uint64_t Offset) override;
|
2014-07-07 19:07:10 +08:00
|
|
|
CXXBaseSpecifier *GetExternalCXXBaseSpecifiers(uint64_t Offset) override;
|
|
|
|
bool FindExternalVisibleDeclsByName(const DeclContext *DC,
|
|
|
|
DeclarationName Name) override;
|
2015-08-06 06:41:45 +08:00
|
|
|
void
|
2014-07-07 19:07:10 +08:00
|
|
|
FindExternalLexicalDecls(const DeclContext *DC,
|
2015-08-06 06:41:45 +08:00
|
|
|
llvm::function_ref<bool(Decl::Kind)> IsKindWeWant,
|
2014-07-07 19:07:10 +08:00
|
|
|
SmallVectorImpl<Decl *> &Result) override;
|
|
|
|
void CompleteType(TagDecl *Tag) override;
|
|
|
|
void CompleteType(ObjCInterfaceDecl *Class) override;
|
|
|
|
void StartedDeserializing() override;
|
|
|
|
void FinishedDeserializing() override;
|
|
|
|
void StartTranslationUnit(ASTConsumer *Consumer) override;
|
|
|
|
void PrintStats() override;
|
|
|
|
|
|
|
|
/// Return the amount of memory used by memory buffers, breaking down
|
|
|
|
/// by heap-backed versus mmap'ed memory.
|
|
|
|
void getMemoryBufferSizes(MemoryBufferSizes &sizes) const override;
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// ExternalSemaSource interface.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
void InitializeSema(Sema &S) override;
|
|
|
|
void ForgetSema() override;
|
|
|
|
void ReadMethodPool(Selector Sel) override;
|
|
|
|
bool LookupUnqualified(LookupResult &R, Scope *S) override;
|
|
|
|
};
|
2015-06-23 07:07:51 +08:00
|
|
|
}
|
2014-07-07 19:07:10 +08:00
|
|
|
|
2014-05-22 12:46:25 +08:00
|
|
|
static ASTReader *
|
|
|
|
createASTReader(CompilerInstance &CI, StringRef pchFile,
|
2014-08-19 03:16:31 +08:00
|
|
|
SmallVectorImpl<std::unique_ptr<llvm::MemoryBuffer>> &MemBufs,
|
2014-05-22 12:46:25 +08:00
|
|
|
SmallVectorImpl<std::string> &bufNames,
|
|
|
|
ASTDeserializationListener *deserialListener = nullptr) {
|
2011-03-10 01:21:42 +08:00
|
|
|
Preprocessor &PP = CI.getPreprocessor();
|
2014-03-08 04:03:18 +08:00
|
|
|
std::unique_ptr<ASTReader> Reader;
|
2015-06-21 02:53:08 +08:00
|
|
|
Reader.reset(new ASTReader(PP, CI.getASTContext(),
|
2015-07-17 09:19:54 +08:00
|
|
|
CI.getPCHContainerReader(),
|
2015-06-21 02:53:08 +08:00
|
|
|
/*isysroot=*/"", /*DisableValidation=*/true));
|
2011-08-03 01:40:32 +08:00
|
|
|
for (unsigned ti = 0; ti < bufNames.size(); ++ti) {
|
|
|
|
StringRef sr(bufNames[ti]);
|
2014-08-19 03:16:31 +08:00
|
|
|
Reader->addInMemoryBuffer(sr, std::move(MemBufs[ti]));
|
2011-08-03 01:40:32 +08:00
|
|
|
}
|
2011-03-10 01:21:42 +08:00
|
|
|
Reader->setDeserializationListener(deserialListener);
|
2012-11-16 02:57:22 +08:00
|
|
|
switch (Reader->ReadAST(pchFile, serialization::MK_PCH, SourceLocation(),
|
2012-10-23 07:51:00 +08:00
|
|
|
ASTReader::ARR_None)) {
|
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());
|
2014-03-08 03:33:25 +08:00
|
|
|
return Reader.release();
|
2011-03-10 01:21:42 +08:00
|
|
|
|
|
|
|
case ASTReader::Failure:
|
2013-03-19 08:28:20 +08:00
|
|
|
case ASTReader::Missing:
|
2012-10-23 06:50:17 +08:00
|
|
|
case ASTReader::OutOfDate:
|
|
|
|
case ASTReader::VersionMismatch:
|
|
|
|
case ASTReader::ConfigurationMismatch:
|
|
|
|
case ASTReader::HadErrors:
|
2011-03-10 01:21:42 +08:00
|
|
|
break;
|
|
|
|
}
|
2014-05-22 12:46:25 +08:00
|
|
|
return nullptr;
|
2011-03-10 01:21:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ChainedIncludesSource::~ChainedIncludesSource() {
|
|
|
|
for (unsigned i = 0, e = CIs.size(); i != e; ++i)
|
|
|
|
delete CIs[i];
|
|
|
|
}
|
|
|
|
|
2014-07-07 19:07:10 +08:00
|
|
|
IntrusiveRefCntPtr<ExternalSemaSource> clang::createChainedIncludesSource(
|
|
|
|
CompilerInstance &CI, IntrusiveRefCntPtr<ExternalSemaSource> &Reader) {
|
2011-03-10 01:21:42 +08:00
|
|
|
|
|
|
|
std::vector<std::string> &includes = CI.getPreprocessorOpts().ChainedIncludes;
|
|
|
|
assert(!includes.empty() && "No '-chain-include' in options!");
|
|
|
|
|
2014-02-27 12:11:59 +08:00
|
|
|
IntrusiveRefCntPtr<ChainedIncludesSource> source(new ChainedIncludesSource());
|
2012-11-10 03:40:39 +08:00
|
|
|
InputKind IK = CI.getFrontendOpts().Inputs[0].getKind();
|
2011-03-10 01:21:42 +08:00
|
|
|
|
2014-08-19 03:16:31 +08:00
|
|
|
SmallVector<std::unique_ptr<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);
|
2014-03-08 04:03:18 +08:00
|
|
|
std::unique_ptr<CompilerInvocation> CInvok;
|
2011-03-10 01:21:42 +08:00
|
|
|
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();
|
2012-11-10 03:40:33 +08:00
|
|
|
FrontendInputFile InputFile(includes[i], IK);
|
|
|
|
CInvok->getFrontendOpts().Inputs.push_back(InputFile);
|
2011-03-10 01:21:42 +08:00
|
|
|
|
|
|
|
TextDiagnosticPrinter *DiagClient =
|
2012-10-24 06:26:28 +08:00
|
|
|
new TextDiagnosticPrinter(llvm::errs(), new DiagnosticOptions());
|
2012-02-20 22:00:23 +08:00
|
|
|
IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
|
|
|
|
IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
|
2012-10-24 06:26:28 +08:00
|
|
|
new DiagnosticsEngine(DiagID, &CI.getDiagnosticOpts(), DiagClient));
|
2011-03-10 01:21:42 +08:00
|
|
|
|
2015-06-21 02:53:08 +08:00
|
|
|
std::unique_ptr<CompilerInstance> Clang(
|
|
|
|
new CompilerInstance(CI.getPCHContainerOperations()));
|
2014-03-08 03:33:25 +08:00
|
|
|
Clang->setInvocation(CInvok.release());
|
2014-07-05 11:08:06 +08:00
|
|
|
Clang->setDiagnostics(Diags.get());
|
2014-07-06 13:26:44 +08:00
|
|
|
Clang->setTarget(TargetInfo::CreateTargetInfo(
|
|
|
|
Clang->getDiagnostics(), Clang->getInvocation().TargetOpts));
|
2011-03-10 01:21:42 +08:00
|
|
|
Clang->createFileManager();
|
|
|
|
Clang->createSourceManager(Clang->getFileManager());
|
2014-03-07 15:47:58 +08:00
|
|
|
Clang->createPreprocessor(TU_Prefix);
|
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();
|
|
|
|
|
2015-06-21 02:53:08 +08:00
|
|
|
auto Buffer = std::make_shared<PCHBuffer>();
|
|
|
|
auto consumer = llvm::make_unique<PCHGenerator>(
|
|
|
|
Clang->getPreprocessor(), "-", nullptr, /*isysroot=*/"", Buffer);
|
2011-03-10 01:21:42 +08:00
|
|
|
Clang->getASTContext().setASTMutationListener(
|
|
|
|
consumer->GetASTMutationListener());
|
2014-08-11 03:56:51 +08:00
|
|
|
Clang->setASTConsumer(std::move(consumer));
|
2014-05-22 12:46:25 +08:00
|
|
|
Clang->createSema(TU_Prefix, nullptr);
|
2011-03-10 01:21:42 +08:00
|
|
|
|
|
|
|
if (firstInclude) {
|
|
|
|
Preprocessor &PP = Clang->getPreprocessor();
|
2015-08-06 09:01:12 +08:00
|
|
|
PP.getBuiltinInfo().initializeBuiltins(PP.getIdentifierTable(),
|
2012-03-11 15:00:24 +08:00
|
|
|
PP.getLangOpts());
|
2011-03-10 01:21:42 +08:00
|
|
|
} else {
|
2014-08-19 03:16:31 +08:00
|
|
|
assert(!SerialBufs.empty());
|
|
|
|
SmallVector<std::unique_ptr<llvm::MemoryBuffer>, 4> Bufs;
|
2014-07-07 19:06:51 +08:00
|
|
|
// TODO: Pass through the existing MemoryBuffer instances instead of
|
|
|
|
// allocating new ones.
|
2014-08-19 03:16:31 +08:00
|
|
|
for (auto &SB : SerialBufs)
|
2014-08-28 04:03:29 +08:00
|
|
|
Bufs.push_back(llvm::MemoryBuffer::getMemBuffer(SB->getBuffer()));
|
2011-03-10 01:21:42 +08:00
|
|
|
std::string pchName = includes[i-1];
|
|
|
|
llvm::raw_string_ostream os(pchName);
|
|
|
|
os << ".pch" << i-1;
|
2014-07-07 19:06:51 +08:00
|
|
|
serialBufNames.push_back(os.str());
|
2011-08-03 01:40:32 +08:00
|
|
|
|
2014-02-27 12:11:59 +08:00
|
|
|
IntrusiveRefCntPtr<ASTReader> Reader;
|
2014-08-19 03:16:31 +08:00
|
|
|
Reader = createASTReader(
|
|
|
|
*Clang, pchName, Bufs, serialBufNames,
|
|
|
|
Clang->getASTConsumer().GetASTDeserializationListener());
|
2011-03-10 01:21:42 +08:00
|
|
|
if (!Reader)
|
2014-05-22 12:46:25 +08:00
|
|
|
return nullptr;
|
2014-02-27 12:11:59 +08:00
|
|
|
Clang->setModuleManager(Reader);
|
2011-03-10 01:21:42 +08:00
|
|
|
Clang->getASTContext().setExternalSource(Reader);
|
|
|
|
}
|
|
|
|
|
2012-11-10 03:40:33 +08:00
|
|
|
if (!Clang->InitializeSourceManager(InputFile))
|
2014-05-22 12:46:25 +08:00
|
|
|
return nullptr;
|
2011-03-10 01:21:42 +08:00
|
|
|
|
|
|
|
ParseAST(Clang->getSema());
|
2011-04-01 03:29:18 +08:00
|
|
|
Clang->getDiagnosticClient().EndSourceFile();
|
2015-06-21 02:53:08 +08:00
|
|
|
assert(Buffer->IsComplete && "serialization did not complete");
|
|
|
|
auto &serialAST = Buffer->Data;
|
|
|
|
SerialBufs.push_back(llvm::MemoryBuffer::getMemBufferCopy(
|
|
|
|
StringRef(serialAST.data(), serialAST.size())));
|
|
|
|
serialAST.clear();
|
2014-03-08 03:33:25 +08:00
|
|
|
source->CIs.push_back(Clang.release());
|
2011-03-10 01:21:42 +08:00
|
|
|
}
|
|
|
|
|
2014-08-19 03:16:31 +08:00
|
|
|
assert(!SerialBufs.empty());
|
2011-03-10 01:21:42 +08:00
|
|
|
std::string pchName = includes.back() + ".pch-final";
|
2011-08-03 01:40:32 +08:00
|
|
|
serialBufNames.push_back(pchName);
|
2014-08-19 03:16:31 +08:00
|
|
|
Reader = createASTReader(CI, pchName, SerialBufs, serialBufNames);
|
2011-03-10 01:21:42 +08:00
|
|
|
if (!Reader)
|
2014-05-22 12:46:25 +08:00
|
|
|
return nullptr;
|
2011-03-10 01:21:42 +08:00
|
|
|
|
2014-02-27 12:11:59 +08:00
|
|
|
source->FinalReader = Reader;
|
|
|
|
return source;
|
2011-03-10 01:21:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// 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);
|
|
|
|
}
|
2015-03-24 14:36:48 +08:00
|
|
|
CXXCtorInitializer **
|
|
|
|
ChainedIncludesSource::GetExternalCXXCtorInitializers(uint64_t Offset) {
|
|
|
|
return getFinalReader().GetExternalCXXCtorInitializers(Offset);
|
|
|
|
}
|
2013-02-07 11:30:24 +08:00
|
|
|
bool
|
2011-03-10 01:21:42 +08:00
|
|
|
ChainedIncludesSource::FindExternalVisibleDeclsByName(const DeclContext *DC,
|
|
|
|
DeclarationName Name) {
|
|
|
|
return getFinalReader().FindExternalVisibleDeclsByName(DC, Name);
|
|
|
|
}
|
2015-08-06 06:41:45 +08:00
|
|
|
void ChainedIncludesSource::FindExternalLexicalDecls(
|
|
|
|
const DeclContext *DC, llvm::function_ref<bool(Decl::Kind)> IsKindWeWant,
|
|
|
|
SmallVectorImpl<Decl *> &Result) {
|
|
|
|
return getFinalReader().FindExternalLexicalDecls(DC, IsKindWeWant, Result);
|
2011-03-10 01:21:42 +08:00
|
|
|
}
|
|
|
|
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();
|
|
|
|
}
|
2012-01-25 08:49:42 +08:00
|
|
|
void ChainedIncludesSource::ReadMethodPool(Selector Sel) {
|
|
|
|
getFinalReader().ReadMethodPool(Sel);
|
2011-04-01 03:29:24 +08:00
|
|
|
}
|
|
|
|
bool ChainedIncludesSource::LookupUnqualified(LookupResult &R, Scope *S) {
|
|
|
|
return getFinalReader().LookupUnqualified(R, S);
|
|
|
|
}
|
|
|
|
|