2018-12-12 10:53:59 +08:00
|
|
|
//=== Serialization/PCHContainerOperations.cpp - PCH Containers -*- C++ -*-===//
|
2015-06-21 02:53:08 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2015-06-21 02:53:08 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines PCHContainerOperations and RawPCHContainerOperation.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2018-12-12 10:53:59 +08:00
|
|
|
#include "clang/Serialization/PCHContainerOperations.h"
|
2015-06-21 02:53:08 +08:00
|
|
|
#include "clang/AST/ASTConsumer.h"
|
2016-05-27 22:27:13 +08:00
|
|
|
#include "clang/Lex/ModuleLoader.h"
|
2019-07-04 06:40:07 +08:00
|
|
|
#include "llvm/Bitstream/BitstreamReader.h"
|
2015-06-21 02:53:08 +08:00
|
|
|
#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 {
|
2019-08-15 07:04:18 +08:00
|
|
|
return std::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() {
|
2019-08-15 07:04:18 +08:00
|
|
|
registerWriter(std::make_unique<RawPCHContainerWriter>());
|
|
|
|
registerReader(std::make_unique<RawPCHContainerReader>());
|
2015-07-17 09:19:54 +08:00
|
|
|
}
|