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 "llvm/Bitcode/BitstreamReader.h"
17 #include "llvm/Support/raw_ostream.h"
18 #include "clang/Lex/ModuleLoader.h"
19
20 using namespace clang;
21
22 namespace {
23
24 /// \brief A PCHContainerGenerator that writes out the PCH to a flat file.
25 class RawPCHContainerGenerator : public ASTConsumer {
26 std::shared_ptr<PCHBuffer> Buffer;
27 raw_pwrite_stream *OS;
28
29 public:
RawPCHContainerGenerator(llvm::raw_pwrite_stream * OS,std::shared_ptr<PCHBuffer> Buffer)30 RawPCHContainerGenerator(llvm::raw_pwrite_stream *OS,
31 std::shared_ptr<PCHBuffer> Buffer)
32 : Buffer(Buffer), OS(OS) {}
33
34 ~RawPCHContainerGenerator() override = default;
35
HandleTranslationUnit(ASTContext & Ctx)36 void HandleTranslationUnit(ASTContext &Ctx) override {
37 if (Buffer->IsComplete) {
38 // Make sure it hits disk now.
39 *OS << Buffer->Data;
40 OS->flush();
41 }
42 // Free the space of the temporary buffer.
43 llvm::SmallVector<char, 0> Empty;
44 Buffer->Data = std::move(Empty);
45 }
46 };
47
48 } // anonymous namespace
49
CreatePCHContainerGenerator(CompilerInstance & CI,const std::string & MainFileName,const std::string & OutputFileName,llvm::raw_pwrite_stream * OS,std::shared_ptr<PCHBuffer> Buffer) const50 std::unique_ptr<ASTConsumer> RawPCHContainerWriter::CreatePCHContainerGenerator(
51 CompilerInstance &CI, const std::string &MainFileName,
52 const std::string &OutputFileName, llvm::raw_pwrite_stream *OS,
53 std::shared_ptr<PCHBuffer> Buffer) const {
54 return llvm::make_unique<RawPCHContainerGenerator>(OS, Buffer);
55 }
56
ExtractPCH(llvm::MemoryBufferRef Buffer,llvm::BitstreamReader & StreamFile) const57 void RawPCHContainerReader::ExtractPCH(
58 llvm::MemoryBufferRef Buffer, llvm::BitstreamReader &StreamFile) const {
59 StreamFile.init((const unsigned char *)Buffer.getBufferStart(),
60 (const unsigned char *)Buffer.getBufferEnd());
61 }
62
PCHContainerOperations()63 PCHContainerOperations::PCHContainerOperations() {
64 registerWriter(llvm::make_unique<RawPCHContainerWriter>());
65 registerReader(llvm::make_unique<RawPCHContainerReader>());
66 }
67