1 //=== Serialization/PCHContainerOperations.cpp - PCH Containers -*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file defines PCHContainerOperations and RawPCHContainerOperation. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "clang/Serialization/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" 19 #include <utility> 20 21 using namespace clang; 22 23 PCHContainerWriter::~PCHContainerWriter() {} 24 PCHContainerReader::~PCHContainerReader() {} 25 26 namespace { 27 28 /// 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; 32 33 public: 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)) {} 37 38 ~RawPCHContainerGenerator() override = default; 39 40 void HandleTranslationUnit(ASTContext &Ctx) override { 41 if (Buffer->IsComplete) { 42 // Make sure it hits disk now. 43 *OS << Buffer->Data; 44 OS->flush(); 45 } 46 // Free the space of the temporary buffer. 47 llvm::SmallVector<char, 0> Empty; 48 Buffer->Data = std::move(Empty); 49 } 50 }; 51 52 } // anonymous namespace 53 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); 59 } 60 61 StringRef 62 RawPCHContainerReader::ExtractPCH(llvm::MemoryBufferRef Buffer) const { 63 return Buffer.getBuffer(); 64 } 65 66 PCHContainerOperations::PCHContainerOperations() { 67 registerWriter(llvm::make_unique<RawPCHContainerWriter>()); 68 registerReader(llvm::make_unique<RawPCHContainerReader>()); 69 } 70