1 //===--- Frontend/PCHContainerOperations.cpp - PCH Containers ---*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines PCHContainerOperations and RawPCHContainerOperation.
12 //===----------------------------------------------------------------------===//
14 #include "clang/Frontend/PCHContainerOperations.h"
15 #include "clang/AST/ASTConsumer.h"
16 #include "clang/Lex/ModuleLoader.h"
17 #include "llvm/Bitcode/BitstreamReader.h"
18 #include "llvm/Support/raw_ostream.h"
21 using namespace clang;
23 PCHContainerWriter::~PCHContainerWriter() {}
24 PCHContainerReader::~PCHContainerReader() {}
28 /// \brief A PCHContainerGenerator that writes out the PCH to a flat file.
29 class RawPCHContainerGenerator : public ASTConsumer {
30 std::shared_ptr<PCHBuffer> Buffer;
31 std::unique_ptr<raw_pwrite_stream> OS;
34 RawPCHContainerGenerator(std::unique_ptr<llvm::raw_pwrite_stream> OS,
35 std::shared_ptr<PCHBuffer> Buffer)
36 : Buffer(std::move(Buffer)), OS(std::move(OS)) {}
38 ~RawPCHContainerGenerator() override = default;
40 void HandleTranslationUnit(ASTContext &Ctx) override {
41 if (Buffer->IsComplete) {
42 // Make sure it hits disk now.
46 // Free the space of the temporary buffer.
47 llvm::SmallVector<char, 0> Empty;
48 Buffer->Data = std::move(Empty);
52 } // anonymous namespace
54 std::unique_ptr<ASTConsumer> RawPCHContainerWriter::CreatePCHContainerGenerator(
55 CompilerInstance &CI, const std::string &MainFileName,
56 const std::string &OutputFileName, std::unique_ptr<llvm::raw_pwrite_stream> OS,
57 std::shared_ptr<PCHBuffer> Buffer) const {
58 return llvm::make_unique<RawPCHContainerGenerator>(std::move(OS), Buffer);
62 RawPCHContainerReader::ExtractPCH(llvm::MemoryBufferRef Buffer) const {
63 return Buffer.getBuffer();
66 PCHContainerOperations::PCHContainerOperations() {
67 registerWriter(llvm::make_unique<RawPCHContainerWriter>());
68 registerReader(llvm::make_unique<RawPCHContainerReader>());