PCHContainerOperations.cpp revision 284679
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"
19using namespace clang;
20
21PCHContainerOperations::~PCHContainerOperations() {}
22
23namespace {
24
25/// \brief A PCHContainerGenerator that writes out the PCH to a flat file.
26class PCHContainerGenerator : public ASTConsumer {
27  std::shared_ptr<PCHBuffer> Buffer;
28  raw_pwrite_stream *OS;
29
30public:
31  PCHContainerGenerator(DiagnosticsEngine &Diags,
32                        const HeaderSearchOptions &HSO,
33                        const PreprocessorOptions &PPO, const TargetOptions &TO,
34                        const LangOptions &LO, const std::string &MainFileName,
35                        const std::string &OutputFileName,
36                        llvm::raw_pwrite_stream *OS,
37                        std::shared_ptr<PCHBuffer> Buffer)
38      : Buffer(Buffer), OS(OS) {}
39
40  virtual ~PCHContainerGenerator() {}
41
42  void HandleTranslationUnit(ASTContext &Ctx) override {
43    if (Buffer->IsComplete) {
44      // Make sure it hits disk now.
45      *OS << Buffer->Data;
46      OS->flush();
47    }
48    // Free the space of the temporary buffer.
49    llvm::SmallVector<char, 0> Empty;
50    Buffer->Data = std::move(Empty);
51  }
52};
53}
54
55std::unique_ptr<ASTConsumer>
56RawPCHContainerOperations::CreatePCHContainerGenerator(
57    DiagnosticsEngine &Diags, const HeaderSearchOptions &HSO,
58    const PreprocessorOptions &PPO, const TargetOptions &TO,
59    const LangOptions &LO, const std::string &MainFileName,
60    const std::string &OutputFileName, llvm::raw_pwrite_stream *OS,
61    std::shared_ptr<PCHBuffer> Buffer) const {
62  return llvm::make_unique<PCHContainerGenerator>(
63      Diags, HSO, PPO, TO, LO, MainFileName, OutputFileName, OS, Buffer);
64}
65
66void RawPCHContainerOperations::ExtractPCH(
67    llvm::MemoryBufferRef Buffer, llvm::BitstreamReader &StreamFile) const {
68  StreamFile.init((const unsigned char *)Buffer.getBufferStart(),
69                  (const unsigned char *)Buffer.getBufferEnd());
70}
71