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
20using namespace clang;
21
22namespace {
23
24/// \brief A PCHContainerGenerator that writes out the PCH to a flat file.
25class RawPCHContainerGenerator : public ASTConsumer {
26  std::shared_ptr<PCHBuffer> Buffer;
27  raw_pwrite_stream *OS;
28
29public:
30  RawPCHContainerGenerator(llvm::raw_pwrite_stream *OS,
31                           std::shared_ptr<PCHBuffer> Buffer)
32      : Buffer(Buffer), OS(OS) {}
33
34  ~RawPCHContainerGenerator() override = default;
35
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
50std::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
57void 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
63PCHContainerOperations::PCHContainerOperations() {
64  registerWriter(llvm::make_unique<RawPCHContainerWriter>());
65  registerReader(llvm::make_unique<RawPCHContainerReader>());
66}
67