• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===--- Frontend/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/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"
19 #include <utility>
20 
21 using namespace clang;
22 
~PCHContainerWriter()23 PCHContainerWriter::~PCHContainerWriter() {}
~PCHContainerReader()24 PCHContainerReader::~PCHContainerReader() {}
25 
26 namespace {
27 
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   raw_pwrite_stream *OS;
32 
33 public:
RawPCHContainerGenerator(llvm::raw_pwrite_stream * OS,std::shared_ptr<PCHBuffer> Buffer)34   RawPCHContainerGenerator(llvm::raw_pwrite_stream *OS,
35                            std::shared_ptr<PCHBuffer> Buffer)
36       : Buffer(std::move(Buffer)), OS(OS) {}
37 
38   ~RawPCHContainerGenerator() override = default;
39 
HandleTranslationUnit(ASTContext & Ctx)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 
CreatePCHContainerGenerator(CompilerInstance & CI,const std::string & MainFileName,const std::string & OutputFileName,llvm::raw_pwrite_stream * OS,std::shared_ptr<PCHBuffer> Buffer) const54 std::unique_ptr<ASTConsumer> RawPCHContainerWriter::CreatePCHContainerGenerator(
55     CompilerInstance &CI, const std::string &MainFileName,
56     const std::string &OutputFileName, llvm::raw_pwrite_stream *OS,
57     std::shared_ptr<PCHBuffer> Buffer) const {
58   return llvm::make_unique<RawPCHContainerGenerator>(OS, Buffer);
59 }
60 
ExtractPCH(llvm::MemoryBufferRef Buffer,llvm::BitstreamReader & StreamFile) const61 void RawPCHContainerReader::ExtractPCH(
62     llvm::MemoryBufferRef Buffer, llvm::BitstreamReader &StreamFile) const {
63   StreamFile.init((const unsigned char *)Buffer.getBufferStart(),
64                   (const unsigned char *)Buffer.getBufferEnd());
65 }
66 
PCHContainerOperations()67 PCHContainerOperations::PCHContainerOperations() {
68   registerWriter(llvm::make_unique<RawPCHContainerWriter>());
69   registerReader(llvm::make_unique<RawPCHContainerReader>());
70 }
71