2015-06-21 02:53:08 +08:00
|
|
|
//===--- Frontend/PCHContainerOperations.cpp - PCH Containers ---*- 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 PCHContainerOperations and RawPCHContainerOperation.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/Frontend/PCHContainerOperations.h"
|
|
|
|
#include "clang/AST/ASTConsumer.h"
|
|
|
|
#include "llvm/Bitcode/BitstreamReader.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include "clang/Lex/ModuleLoader.h"
|
|
|
|
using namespace clang;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
/// \brief A PCHContainerGenerator that writes out the PCH to a flat file.
|
2015-07-17 09:19:54 +08:00
|
|
|
class RawPCHContainerGenerator : public ASTConsumer {
|
2015-06-21 02:53:08 +08:00
|
|
|
std::shared_ptr<PCHBuffer> Buffer;
|
|
|
|
raw_pwrite_stream *OS;
|
|
|
|
|
|
|
|
public:
|
2015-07-17 09:19:54 +08:00
|
|
|
RawPCHContainerGenerator(DiagnosticsEngine &Diags,
|
|
|
|
const HeaderSearchOptions &HSO,
|
|
|
|
const PreprocessorOptions &PPO,
|
|
|
|
const TargetOptions &TO, const LangOptions &LO,
|
|
|
|
const std::string &MainFileName,
|
|
|
|
const std::string &OutputFileName,
|
|
|
|
llvm::raw_pwrite_stream *OS,
|
|
|
|
std::shared_ptr<PCHBuffer> Buffer)
|
2015-06-21 02:53:08 +08:00
|
|
|
: Buffer(Buffer), OS(OS) {}
|
|
|
|
|
2015-07-17 09:19:54 +08:00
|
|
|
virtual ~RawPCHContainerGenerator() {}
|
2015-06-21 02:53:08 +08:00
|
|
|
|
|
|
|
void HandleTranslationUnit(ASTContext &Ctx) override {
|
|
|
|
if (Buffer->IsComplete) {
|
|
|
|
// Make sure it hits disk now.
|
|
|
|
*OS << Buffer->Data;
|
|
|
|
OS->flush();
|
|
|
|
}
|
|
|
|
// Free the space of the temporary buffer.
|
|
|
|
llvm::SmallVector<char, 0> Empty;
|
|
|
|
Buffer->Data = std::move(Empty);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2015-07-17 09:19:54 +08:00
|
|
|
std::unique_ptr<ASTConsumer> RawPCHContainerWriter::CreatePCHContainerGenerator(
|
2015-06-21 02:53:08 +08:00
|
|
|
DiagnosticsEngine &Diags, const HeaderSearchOptions &HSO,
|
|
|
|
const PreprocessorOptions &PPO, const TargetOptions &TO,
|
|
|
|
const LangOptions &LO, const std::string &MainFileName,
|
|
|
|
const std::string &OutputFileName, llvm::raw_pwrite_stream *OS,
|
|
|
|
std::shared_ptr<PCHBuffer> Buffer) const {
|
2015-07-17 09:19:54 +08:00
|
|
|
return llvm::make_unique<RawPCHContainerGenerator>(
|
2015-06-21 02:53:08 +08:00
|
|
|
Diags, HSO, PPO, TO, LO, MainFileName, OutputFileName, OS, Buffer);
|
|
|
|
}
|
|
|
|
|
2015-07-17 09:19:54 +08:00
|
|
|
void RawPCHContainerReader::ExtractPCH(
|
2015-06-21 02:53:08 +08:00
|
|
|
llvm::MemoryBufferRef Buffer, llvm::BitstreamReader &StreamFile) const {
|
|
|
|
StreamFile.init((const unsigned char *)Buffer.getBufferStart(),
|
|
|
|
(const unsigned char *)Buffer.getBufferEnd());
|
|
|
|
}
|
2015-07-17 09:19:54 +08:00
|
|
|
|
|
|
|
PCHContainerOperations::PCHContainerOperations() {
|
|
|
|
registerWriter(llvm::make_unique<RawPCHContainerWriter>());
|
|
|
|
registerReader(llvm::make_unique<RawPCHContainerReader>());
|
|
|
|
}
|