blob: fd84678b80831edde18eb47ef822a3a4839f5cea [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
Benjamin Kramer6277b182016-02-01 13:22:39 +000022PCHContainerWriter::~PCHContainerWriter() {}
23PCHContainerReader::~PCHContainerReader() {}
24
Adrian Prantlbb165fb2015-06-20 18:53:08 +000025namespace {
26
27/// \brief A PCHContainerGenerator that writes out the PCH to a flat file.
Adrian Prantlfb2398d2015-07-17 01:19:54 +000028class RawPCHContainerGenerator : public ASTConsumer {
Adrian Prantlbb165fb2015-06-20 18:53:08 +000029 std::shared_ptr<PCHBuffer> Buffer;
30 raw_pwrite_stream *OS;
31
32public:
Adrian Prantl03914062015-09-18 22:10:59 +000033 RawPCHContainerGenerator(llvm::raw_pwrite_stream *OS,
Adrian Prantlfb2398d2015-07-17 01:19:54 +000034 std::shared_ptr<PCHBuffer> Buffer)
Adrian Prantlbb165fb2015-06-20 18:53:08 +000035 : Buffer(Buffer), OS(OS) {}
36
Hans Wennborg7eb54642015-09-10 17:07:54 +000037 ~RawPCHContainerGenerator() override = default;
Adrian Prantlbb165fb2015-06-20 18:53:08 +000038
39 void HandleTranslationUnit(ASTContext &Ctx) override {
40 if (Buffer->IsComplete) {
41 // Make sure it hits disk now.
42 *OS << Buffer->Data;
43 OS->flush();
44 }
45 // Free the space of the temporary buffer.
46 llvm::SmallVector<char, 0> Empty;
47 Buffer->Data = std::move(Empty);
48 }
49};
Hans Wennborg7eb54642015-09-10 17:07:54 +000050
51} // anonymous namespace
Adrian Prantlbb165fb2015-06-20 18:53:08 +000052
Adrian Prantlfb2398d2015-07-17 01:19:54 +000053std::unique_ptr<ASTConsumer> RawPCHContainerWriter::CreatePCHContainerGenerator(
Adrian Prantl1e63b2b2015-09-19 21:42:52 +000054 CompilerInstance &CI, const std::string &MainFileName,
55 const std::string &OutputFileName, llvm::raw_pwrite_stream *OS,
56 std::shared_ptr<PCHBuffer> Buffer) const {
Adrian Prantl03914062015-09-18 22:10:59 +000057 return llvm::make_unique<RawPCHContainerGenerator>(OS, Buffer);
Adrian Prantlbb165fb2015-06-20 18:53:08 +000058}
59
Adrian Prantlfb2398d2015-07-17 01:19:54 +000060void RawPCHContainerReader::ExtractPCH(
Adrian Prantlbb165fb2015-06-20 18:53:08 +000061 llvm::MemoryBufferRef Buffer, llvm::BitstreamReader &StreamFile) const {
62 StreamFile.init((const unsigned char *)Buffer.getBufferStart(),
63 (const unsigned char *)Buffer.getBufferEnd());
64}
Adrian Prantlfb2398d2015-07-17 01:19:54 +000065
66PCHContainerOperations::PCHContainerOperations() {
67 registerWriter(llvm::make_unique<RawPCHContainerWriter>());
68 registerReader(llvm::make_unique<RawPCHContainerReader>());
69}