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