blob: 5d73a86a00c7a1470d2f19b1cd626962d0d1b2a5 [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"
Hans Wennborg7eb54642015-09-10 17:07:54 +000019
Adrian Prantlbb165fb2015-06-20 18:53:08 +000020using namespace clang;
21
Adrian Prantlbb165fb2015-06-20 18:53:08 +000022namespace {
23
24/// \brief A PCHContainerGenerator that writes out the PCH to a flat file.
Adrian Prantlfb2398d2015-07-17 01:19:54 +000025class RawPCHContainerGenerator : public ASTConsumer {
Adrian Prantlbb165fb2015-06-20 18:53:08 +000026 std::shared_ptr<PCHBuffer> Buffer;
27 raw_pwrite_stream *OS;
28
29public:
Adrian Prantlfb2398d2015-07-17 01:19:54 +000030 RawPCHContainerGenerator(DiagnosticsEngine &Diags,
31 const HeaderSearchOptions &HSO,
32 const PreprocessorOptions &PPO,
33 const TargetOptions &TO, const LangOptions &LO,
34 const std::string &MainFileName,
35 const std::string &OutputFileName,
36 llvm::raw_pwrite_stream *OS,
37 std::shared_ptr<PCHBuffer> Buffer)
Adrian Prantlbb165fb2015-06-20 18:53:08 +000038 : Buffer(Buffer), OS(OS) {}
39
Hans Wennborg7eb54642015-09-10 17:07:54 +000040 ~RawPCHContainerGenerator() override = default;
Adrian Prantlbb165fb2015-06-20 18:53:08 +000041
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};
Hans Wennborg7eb54642015-09-10 17:07:54 +000053
54} // anonymous namespace
Adrian Prantlbb165fb2015-06-20 18:53:08 +000055
Adrian Prantlfb2398d2015-07-17 01:19:54 +000056std::unique_ptr<ASTConsumer> RawPCHContainerWriter::CreatePCHContainerGenerator(
Adrian Prantlbb165fb2015-06-20 18:53:08 +000057 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 {
Adrian Prantlfb2398d2015-07-17 01:19:54 +000062 return llvm::make_unique<RawPCHContainerGenerator>(
Adrian Prantlbb165fb2015-06-20 18:53:08 +000063 Diags, HSO, PPO, TO, LO, MainFileName, OutputFileName, OS, Buffer);
64}
65
Adrian Prantlfb2398d2015-07-17 01:19:54 +000066void RawPCHContainerReader::ExtractPCH(
Adrian Prantlbb165fb2015-06-20 18:53:08 +000067 llvm::MemoryBufferRef Buffer, llvm::BitstreamReader &StreamFile) const {
68 StreamFile.init((const unsigned char *)Buffer.getBufferStart(),
69 (const unsigned char *)Buffer.getBufferEnd());
70}
Adrian Prantlfb2398d2015-07-17 01:19:54 +000071
72PCHContainerOperations::PCHContainerOperations() {
73 registerWriter(llvm::make_unique<RawPCHContainerWriter>());
74 registerReader(llvm::make_unique<RawPCHContainerReader>());
75}