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