blob: c749bb5c8db4089c1befcc1c315d025adc86d06f [file] [log] [blame]
Adrian Prantlbb165fb2015-06-20 18:53:08 +00001//===--- 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
Adrian Prantlbb165fb2015-06-20 18:53:08 +000021namespace {
22
23/// \brief A PCHContainerGenerator that writes out the PCH to a flat file.
24class PCHContainerGenerator : public ASTConsumer {
25 std::shared_ptr<PCHBuffer> Buffer;
26 raw_pwrite_stream *OS;
27
28public:
29 PCHContainerGenerator(DiagnosticsEngine &Diags,
30 const HeaderSearchOptions &HSO,
31 const PreprocessorOptions &PPO, const TargetOptions &TO,
32 const LangOptions &LO, const std::string &MainFileName,
33 const std::string &OutputFileName,
34 llvm::raw_pwrite_stream *OS,
35 std::shared_ptr<PCHBuffer> Buffer)
36 : Buffer(Buffer), OS(OS) {}
37
38 virtual ~PCHContainerGenerator() {}
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
53std::unique_ptr<ASTConsumer>
54RawPCHContainerOperations::CreatePCHContainerGenerator(
55 DiagnosticsEngine &Diags, const HeaderSearchOptions &HSO,
56 const PreprocessorOptions &PPO, const TargetOptions &TO,
57 const LangOptions &LO, const std::string &MainFileName,
58 const std::string &OutputFileName, llvm::raw_pwrite_stream *OS,
59 std::shared_ptr<PCHBuffer> Buffer) const {
60 return llvm::make_unique<PCHContainerGenerator>(
61 Diags, HSO, PPO, TO, LO, MainFileName, OutputFileName, OS, Buffer);
62}
63
64void RawPCHContainerOperations::ExtractPCH(
65 llvm::MemoryBufferRef Buffer, llvm::BitstreamReader &StreamFile) const {
66 StreamFile.init((const unsigned char *)Buffer.getBufferStart(),
67 (const unsigned char *)Buffer.getBufferEnd());
68}