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"
|
2016-07-19 03:02:11 +08:00
|
|
|
#include "clang/Lex/PreprocessorOptions.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/Parse/ParseAST.h"
|
2016-07-16 08:35:14 +08:00
|
|
|
#include "clang/Sema/MultiplexExternalSemaSource.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 {
|
2016-07-16 08:35:14 +08:00
|
|
|
class ChainedIncludesSourceImpl : public ExternalSemaSource {
|
2014-07-07 19:07:10 +08:00
|
|
|
public:
|
2016-07-16 08:35:14 +08:00
|
|
|
ChainedIncludesSourceImpl(std::vector<std::unique_ptr<CompilerInstance>> CIs)
|
|
|
|
: CIs(std::move(CIs)) {}
|
2014-07-07 19:07:10 +08:00
|
|
|
|
|
|
|
protected:
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// ExternalASTSource interface.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
/// Return the amount of memory used by memory buffers, breaking down
|
|
|
|
/// by heap-backed versus mmap'ed memory.
|
2016-07-16 08:35:14 +08:00
|
|
|
void getMemoryBufferSizes(MemoryBufferSizes &sizes) const override {
|
|
|
|
for (unsigned i = 0, e = CIs.size(); i != e; ++i) {
|
|
|
|
if (const ExternalASTSource *eSrc =
|
|
|
|
CIs[i]->getASTContext().getExternalSource()) {
|
|
|
|
eSrc->getMemoryBufferSizes(sizes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-07-07 19:07:10 +08:00
|
|
|
|
2016-07-16 08:35:14 +08:00
|
|
|
private:
|
|
|
|
std::vector<std::unique_ptr<CompilerInstance>> CIs;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Members of ChainedIncludesSource, factored out so we can initialize
|
|
|
|
/// them before we initialize the ExternalSemaSource base class.
|
|
|
|
struct ChainedIncludesSourceMembers {
|
2016-07-18 04:00:59 +08:00
|
|
|
ChainedIncludesSourceMembers(
|
|
|
|
std::vector<std::unique_ptr<CompilerInstance>> CIs,
|
|
|
|
IntrusiveRefCntPtr<ExternalSemaSource> FinalReader)
|
|
|
|
: Impl(std::move(CIs)), FinalReader(std::move(FinalReader)) {}
|
2016-07-16 08:35:14 +08:00
|
|
|
ChainedIncludesSourceImpl Impl;
|
|
|
|
IntrusiveRefCntPtr<ExternalSemaSource> FinalReader;
|
|
|
|
};
|
2014-07-07 19:07:10 +08:00
|
|
|
|
2016-07-16 08:35:14 +08:00
|
|
|
/// Use MultiplexExternalSemaSource to dispatch all ExternalSemaSource
|
|
|
|
/// calls to the final reader.
|
|
|
|
class ChainedIncludesSource
|
|
|
|
: private ChainedIncludesSourceMembers,
|
|
|
|
public MultiplexExternalSemaSource {
|
|
|
|
public:
|
|
|
|
ChainedIncludesSource(std::vector<std::unique_ptr<CompilerInstance>> CIs,
|
|
|
|
IntrusiveRefCntPtr<ExternalSemaSource> FinalReader)
|
2016-07-18 04:00:59 +08:00
|
|
|
: ChainedIncludesSourceMembers(std::move(CIs), std::move(FinalReader)),
|
2016-07-16 08:35:14 +08:00
|
|
|
MultiplexExternalSemaSource(Impl, *this->FinalReader) {}
|
2014-07-07 19:07:10 +08:00
|
|
|
};
|
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-11-04 02:33:07 +08:00
|
|
|
/*Extensions=*/{ },
|
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
|
|
|
}
|
|
|
|
|
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!");
|
|
|
|
|
2016-07-16 08:35:14 +08:00
|
|
|
std::vector<std::unique_ptr<CompilerInstance>> CIs;
|
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()));
|
2017-01-07 03:49:01 +08:00
|
|
|
Clang->setInvocation(std::move(CInvok));
|
2014-07-05 11:08:06 +08:00
|
|
|
Clang->setDiagnostics(Diags.get());
|
2014-07-06 13:26:44 +08:00
|
|
|
Clang->setTarget(TargetInfo::CreateTargetInfo(
|
2016-04-09 00:52:00 +08:00
|
|
|
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>();
|
2017-01-06 02:23:18 +08:00
|
|
|
ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions;
|
2015-06-21 02:53:08 +08:00
|
|
|
auto consumer = llvm::make_unique<PCHGenerator>(
|
2016-08-26 02:26:30 +08:00
|
|
|
Clang->getPreprocessor(), "-", /*isysroot=*/"", Buffer,
|
2016-07-14 04:35:26 +08:00
|
|
|
Extensions, /*AllowASTWithErrors=*/true);
|
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();
|
2016-07-16 08:35:14 +08:00
|
|
|
CIs.push_back(std::move(Clang));
|
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
|
|
|
|
2016-07-16 08:35:14 +08:00
|
|
|
return IntrusiveRefCntPtr<ChainedIncludesSource>(
|
|
|
|
new ChainedIncludesSource(std::move(CIs), Reader));
|
2011-03-10 01:21:42 +08:00
|
|
|
}
|