blob: 5cf8de44dcabf12da2f5f9842a1599a14102954c [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 Prantl03914062015-09-18 22:10:59 +000030 RawPCHContainerGenerator(llvm::raw_pwrite_stream *OS,
Adrian Prantlfb2398d2015-07-17 01:19:54 +000031 std::shared_ptr<PCHBuffer> Buffer)
Adrian Prantlbb165fb2015-06-20 18:53:08 +000032 : Buffer(Buffer), OS(OS) {}
33
Hans Wennborg7eb54642015-09-10 17:07:54 +000034 ~RawPCHContainerGenerator() override = default;
Adrian Prantlbb165fb2015-06-20 18:53:08 +000035
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};
Hans Wennborg7eb54642015-09-10 17:07:54 +000047
48} // anonymous namespace
Adrian Prantlbb165fb2015-06-20 18:53:08 +000049
Adrian Prantlfb2398d2015-07-17 01:19:54 +000050std::unique_ptr<ASTConsumer> RawPCHContainerWriter::CreatePCHContainerGenerator(
Adrian Prantl03914062015-09-18 22:10:59 +000051 DiagnosticsEngine &Diags, const CompilerInstance &CI,
52 const std::string &MainFileName, const std::string &OutputFileName,
53 llvm::raw_pwrite_stream *OS, std::shared_ptr<PCHBuffer> Buffer) const {
54 return llvm::make_unique<RawPCHContainerGenerator>(OS, Buffer);
Adrian Prantlbb165fb2015-06-20 18:53:08 +000055}
56
Adrian Prantlfb2398d2015-07-17 01:19:54 +000057void RawPCHContainerReader::ExtractPCH(
Adrian Prantlbb165fb2015-06-20 18:53:08 +000058 llvm::MemoryBufferRef Buffer, llvm::BitstreamReader &StreamFile) const {
59 StreamFile.init((const unsigned char *)Buffer.getBufferStart(),
60 (const unsigned char *)Buffer.getBufferEnd());
61}
Adrian Prantlfb2398d2015-07-17 01:19:54 +000062
63PCHContainerOperations::PCHContainerOperations() {
64 registerWriter(llvm::make_unique<RawPCHContainerWriter>());
65 registerReader(llvm::make_unique<RawPCHContainerReader>());
66}