1 //=== Serialization/PCHContainerOperations.cpp - PCH Containers -*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file defines PCHContainerOperations and RawPCHContainerOperation.
11 //===----------------------------------------------------------------------===//
13 #include "clang/Serialization/PCHContainerOperations.h"
14 #include "clang/AST/ASTConsumer.h"
15 #include "clang/Lex/ModuleLoader.h"
16 #include "llvm/Bitstream/BitstreamReader.h"
17 #include "llvm/Support/raw_ostream.h"
20 using namespace clang
;
22 PCHContainerWriter::~PCHContainerWriter() {}
23 PCHContainerReader::~PCHContainerReader() {}
27 /// A PCHContainerGenerator that writes out the PCH to a flat file.
28 class RawPCHContainerGenerator
: public ASTConsumer
{
29 std::shared_ptr
<PCHBuffer
> Buffer
;
30 std::unique_ptr
<raw_pwrite_stream
> OS
;
33 RawPCHContainerGenerator(std::unique_ptr
<llvm::raw_pwrite_stream
> OS
,
34 std::shared_ptr
<PCHBuffer
> Buffer
)
35 : Buffer(std::move(Buffer
)), OS(std::move(OS
)) {}
37 ~RawPCHContainerGenerator() override
= default;
39 void HandleTranslationUnit(ASTContext
&Ctx
) override
{
40 if (Buffer
->IsComplete
) {
41 // Make sure it hits disk now.
45 // Free the space of the temporary buffer.
46 llvm::SmallVector
<char, 0> Empty
;
47 Buffer
->Data
= std::move(Empty
);
51 } // anonymous namespace
53 std::unique_ptr
<ASTConsumer
> RawPCHContainerWriter::CreatePCHContainerGenerator(
54 CompilerInstance
&CI
, const std::string
&MainFileName
,
55 const std::string
&OutputFileName
, std::unique_ptr
<llvm::raw_pwrite_stream
> OS
,
56 std::shared_ptr
<PCHBuffer
> Buffer
) const {
57 return std::make_unique
<RawPCHContainerGenerator
>(std::move(OS
), Buffer
);
60 ArrayRef
<llvm::StringRef
> RawPCHContainerReader::getFormats() const {
61 static StringRef
Raw("raw");
66 RawPCHContainerReader::ExtractPCH(llvm::MemoryBufferRef Buffer
) const {
67 return Buffer
.getBuffer();
70 PCHContainerOperations::PCHContainerOperations() {
71 registerWriter(std::make_unique
<RawPCHContainerWriter
>());
72 registerReader(std::make_unique
<RawPCHContainerReader
>());