10b57cec5SDimitry Andric //=== Serialization/PCHContainerOperations.cpp - PCH Containers -*- C++ -*-===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // 90b57cec5SDimitry Andric // This file defines PCHContainerOperations and RawPCHContainerOperation. 100b57cec5SDimitry Andric // 110b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 120b57cec5SDimitry Andric 130b57cec5SDimitry Andric #include "clang/Serialization/PCHContainerOperations.h" 140b57cec5SDimitry Andric #include "clang/AST/ASTConsumer.h" 150b57cec5SDimitry Andric #include "clang/Lex/ModuleLoader.h" 160b57cec5SDimitry Andric #include "llvm/Bitstream/BitstreamReader.h" 170b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h" 180b57cec5SDimitry Andric #include <utility> 190b57cec5SDimitry Andric 200b57cec5SDimitry Andric using namespace clang; 210b57cec5SDimitry Andric 220b57cec5SDimitry Andric PCHContainerWriter::~PCHContainerWriter() {} 230b57cec5SDimitry Andric PCHContainerReader::~PCHContainerReader() {} 240b57cec5SDimitry Andric 250b57cec5SDimitry Andric namespace { 260b57cec5SDimitry Andric 270b57cec5SDimitry Andric /// A PCHContainerGenerator that writes out the PCH to a flat file. 280b57cec5SDimitry Andric class RawPCHContainerGenerator : public ASTConsumer { 290b57cec5SDimitry Andric std::shared_ptr<PCHBuffer> Buffer; 300b57cec5SDimitry Andric std::unique_ptr<raw_pwrite_stream> OS; 310b57cec5SDimitry Andric 320b57cec5SDimitry Andric public: 330b57cec5SDimitry Andric RawPCHContainerGenerator(std::unique_ptr<llvm::raw_pwrite_stream> OS, 340b57cec5SDimitry Andric std::shared_ptr<PCHBuffer> Buffer) 350b57cec5SDimitry Andric : Buffer(std::move(Buffer)), OS(std::move(OS)) {} 360b57cec5SDimitry Andric 370b57cec5SDimitry Andric ~RawPCHContainerGenerator() override = default; 380b57cec5SDimitry Andric 390b57cec5SDimitry Andric void HandleTranslationUnit(ASTContext &Ctx) override { 400b57cec5SDimitry Andric if (Buffer->IsComplete) { 410b57cec5SDimitry Andric // Make sure it hits disk now. 420b57cec5SDimitry Andric *OS << Buffer->Data; 430b57cec5SDimitry Andric OS->flush(); 440b57cec5SDimitry Andric } 450b57cec5SDimitry Andric // Free the space of the temporary buffer. 460b57cec5SDimitry Andric llvm::SmallVector<char, 0> Empty; 470b57cec5SDimitry Andric Buffer->Data = std::move(Empty); 480b57cec5SDimitry Andric } 490b57cec5SDimitry Andric }; 500b57cec5SDimitry Andric 510b57cec5SDimitry Andric } // anonymous namespace 520b57cec5SDimitry Andric 530b57cec5SDimitry Andric std::unique_ptr<ASTConsumer> RawPCHContainerWriter::CreatePCHContainerGenerator( 540b57cec5SDimitry Andric CompilerInstance &CI, const std::string &MainFileName, 550b57cec5SDimitry Andric const std::string &OutputFileName, std::unique_ptr<llvm::raw_pwrite_stream> OS, 560b57cec5SDimitry Andric std::shared_ptr<PCHBuffer> Buffer) const { 57*a7dea167SDimitry Andric return std::make_unique<RawPCHContainerGenerator>(std::move(OS), Buffer); 580b57cec5SDimitry Andric } 590b57cec5SDimitry Andric 600b57cec5SDimitry Andric StringRef 610b57cec5SDimitry Andric RawPCHContainerReader::ExtractPCH(llvm::MemoryBufferRef Buffer) const { 620b57cec5SDimitry Andric return Buffer.getBuffer(); 630b57cec5SDimitry Andric } 640b57cec5SDimitry Andric 650b57cec5SDimitry Andric PCHContainerOperations::PCHContainerOperations() { 66*a7dea167SDimitry Andric registerWriter(std::make_unique<RawPCHContainerWriter>()); 67*a7dea167SDimitry Andric registerReader(std::make_unique<RawPCHContainerReader>()); 680b57cec5SDimitry Andric } 69