1f3b0046bSRichard Trieu //=== Serialization/PCHContainerOperations.cpp - PCH Containers -*- C++ -*-===//
2f3b0046bSRichard Trieu //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6f3b0046bSRichard Trieu //
7f3b0046bSRichard Trieu //===----------------------------------------------------------------------===//
8f3b0046bSRichard Trieu //
9f3b0046bSRichard Trieu // This file defines PCHContainerOperations and RawPCHContainerOperation.
10f3b0046bSRichard Trieu //
11f3b0046bSRichard Trieu //===----------------------------------------------------------------------===//
12f3b0046bSRichard Trieu
13f3b0046bSRichard Trieu #include "clang/Serialization/PCHContainerOperations.h"
14f3b0046bSRichard Trieu #include "clang/AST/ASTConsumer.h"
15f3b0046bSRichard Trieu #include "llvm/Support/raw_ostream.h"
16f3b0046bSRichard Trieu #include <utility>
17f3b0046bSRichard Trieu
18f3b0046bSRichard Trieu using namespace clang;
19f3b0046bSRichard Trieu
~PCHContainerWriter()20f3b0046bSRichard Trieu PCHContainerWriter::~PCHContainerWriter() {}
~PCHContainerReader()21f3b0046bSRichard Trieu PCHContainerReader::~PCHContainerReader() {}
22f3b0046bSRichard Trieu
23f3b0046bSRichard Trieu namespace {
24f3b0046bSRichard Trieu
25f3b0046bSRichard Trieu /// A PCHContainerGenerator that writes out the PCH to a flat file.
26f3b0046bSRichard Trieu class RawPCHContainerGenerator : public ASTConsumer {
27f3b0046bSRichard Trieu std::shared_ptr<PCHBuffer> Buffer;
28f3b0046bSRichard Trieu std::unique_ptr<raw_pwrite_stream> OS;
29f3b0046bSRichard Trieu
30f3b0046bSRichard Trieu public:
RawPCHContainerGenerator(std::unique_ptr<llvm::raw_pwrite_stream> OS,std::shared_ptr<PCHBuffer> Buffer)31f3b0046bSRichard Trieu RawPCHContainerGenerator(std::unique_ptr<llvm::raw_pwrite_stream> OS,
32f3b0046bSRichard Trieu std::shared_ptr<PCHBuffer> Buffer)
33f3b0046bSRichard Trieu : Buffer(std::move(Buffer)), OS(std::move(OS)) {}
34f3b0046bSRichard Trieu
35f3b0046bSRichard Trieu ~RawPCHContainerGenerator() override = default;
36f3b0046bSRichard Trieu
HandleTranslationUnit(ASTContext & Ctx)37f3b0046bSRichard Trieu void HandleTranslationUnit(ASTContext &Ctx) override {
38f3b0046bSRichard Trieu if (Buffer->IsComplete) {
39f3b0046bSRichard Trieu // Make sure it hits disk now.
40f3b0046bSRichard Trieu *OS << Buffer->Data;
41f3b0046bSRichard Trieu OS->flush();
42f3b0046bSRichard Trieu }
43f3b0046bSRichard Trieu // Free the space of the temporary buffer.
44f3b0046bSRichard Trieu llvm::SmallVector<char, 0> Empty;
45f3b0046bSRichard Trieu Buffer->Data = std::move(Empty);
46f3b0046bSRichard Trieu }
47f3b0046bSRichard Trieu };
48f3b0046bSRichard Trieu
49f3b0046bSRichard Trieu } // anonymous namespace
50f3b0046bSRichard Trieu
CreatePCHContainerGenerator(CompilerInstance & CI,const std::string & MainFileName,const std::string & OutputFileName,std::unique_ptr<llvm::raw_pwrite_stream> OS,std::shared_ptr<PCHBuffer> Buffer) const51f3b0046bSRichard Trieu std::unique_ptr<ASTConsumer> RawPCHContainerWriter::CreatePCHContainerGenerator(
52f3b0046bSRichard Trieu CompilerInstance &CI, const std::string &MainFileName,
53f3b0046bSRichard Trieu const std::string &OutputFileName, std::unique_ptr<llvm::raw_pwrite_stream> OS,
54f3b0046bSRichard Trieu std::shared_ptr<PCHBuffer> Buffer) const {
552b3d49b6SJonas Devlieghere return std::make_unique<RawPCHContainerGenerator>(std::move(OS), Buffer);
56f3b0046bSRichard Trieu }
57f3b0046bSRichard Trieu
getFormats() const58*8fe8d69dSBen Langmuir ArrayRef<llvm::StringRef> RawPCHContainerReader::getFormats() const {
59*8fe8d69dSBen Langmuir static StringRef Raw("raw");
60*8fe8d69dSBen Langmuir return ArrayRef(Raw);
61*8fe8d69dSBen Langmuir }
62*8fe8d69dSBen Langmuir
63f3b0046bSRichard Trieu StringRef
ExtractPCH(llvm::MemoryBufferRef Buffer) const64f3b0046bSRichard Trieu RawPCHContainerReader::ExtractPCH(llvm::MemoryBufferRef Buffer) const {
65f3b0046bSRichard Trieu return Buffer.getBuffer();
66f3b0046bSRichard Trieu }
67f3b0046bSRichard Trieu
PCHContainerOperations()68f3b0046bSRichard Trieu PCHContainerOperations::PCHContainerOperations() {
692b3d49b6SJonas Devlieghere registerWriter(std::make_unique<RawPCHContainerWriter>());
702b3d49b6SJonas Devlieghere registerReader(std::make_unique<RawPCHContainerReader>());
71f3b0046bSRichard Trieu }
72