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"
|
2016-05-27 22:27:13 +08:00
|
|
|
#include "clang/Lex/ModuleLoader.h"
|
2015-06-21 02:53:08 +08:00
|
|
|
#include "llvm/Bitcode/BitstreamReader.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2016-05-27 22:27:13 +08:00
|
|
|
#include <utility>
|
2015-09-11 01:07:54 +08:00
|
|
|
|
2015-06-21 02:53:08 +08:00
|
|
|
using namespace clang;
|
|
|
|
|
2016-02-01 21:22:39 +08:00
|
|
|
PCHContainerWriter::~PCHContainerWriter() {}
|
|
|
|
PCHContainerReader::~PCHContainerReader() {}
|
|
|
|
|
2015-06-21 02:53:08 +08:00
|
|
|
namespace {
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// 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;
|
2016-07-15 08:55:40 +08:00
|
|
|
std::unique_ptr<raw_pwrite_stream> OS;
|
2015-06-21 02:53:08 +08:00
|
|
|
|
|
|
|
public:
|
2016-07-15 08:55:40 +08:00
|
|
|
RawPCHContainerGenerator(std::unique_ptr<llvm::raw_pwrite_stream> OS,
|
2015-07-17 09:19:54 +08:00
|
|
|
std::shared_ptr<PCHBuffer> Buffer)
|
2016-07-15 08:55:40 +08:00
|
|
|
: Buffer(std::move(Buffer)), OS(std::move(OS)) {}
|
2015-06-21 02:53:08 +08:00
|
|
|
|
2015-09-11 01:07:54 +08:00
|
|
|
~RawPCHContainerGenerator() override = default;
|
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-09-11 01:07:54 +08:00
|
|
|
|
|
|
|
} // anonymous namespace
|
2015-06-21 02:53:08 +08:00
|
|
|
|
2015-07-17 09:19:54 +08:00
|
|
|
std::unique_ptr<ASTConsumer> RawPCHContainerWriter::CreatePCHContainerGenerator(
|
2015-09-20 05:42:52 +08:00
|
|
|
CompilerInstance &CI, const std::string &MainFileName,
|
2016-07-15 08:55:40 +08:00
|
|
|
const std::string &OutputFileName, std::unique_ptr<llvm::raw_pwrite_stream> OS,
|
2015-09-20 05:42:52 +08:00
|
|
|
std::shared_ptr<PCHBuffer> Buffer) const {
|
2016-07-15 08:55:40 +08:00
|
|
|
return llvm::make_unique<RawPCHContainerGenerator>(std::move(OS), Buffer);
|
2015-06-21 02:53:08 +08:00
|
|
|
}
|
|
|
|
|
2016-11-08 12:17:11 +08:00
|
|
|
StringRef
|
|
|
|
RawPCHContainerReader::ExtractPCH(llvm::MemoryBufferRef Buffer) const {
|
|
|
|
return Buffer.getBuffer();
|
2015-06-21 02:53:08 +08:00
|
|
|
}
|
2015-07-17 09:19:54 +08:00
|
|
|
|
|
|
|
PCHContainerOperations::PCHContainerOperations() {
|
|
|
|
registerWriter(llvm::make_unique<RawPCHContainerWriter>());
|
|
|
|
registerReader(llvm::make_unique<RawPCHContainerReader>());
|
|
|
|
}
|