blob: cde3ba139bcf43402a01ccbd77aca0443df7355f [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.
Adrian Prantlfb2398d2015-07-17 01:19:54 +000024class RawPCHContainerGenerator : public ASTConsumer {
Adrian Prantlbb165fb2015-06-20 18:53:08 +000025 std::shared_ptr<PCHBuffer> Buffer;
26 raw_pwrite_stream *OS;
27
28public:
Adrian Prantlfb2398d2015-07-17 01:19:54 +000029 RawPCHContainerGenerator(DiagnosticsEngine &Diags,
30 const HeaderSearchOptions &HSO,
31 const PreprocessorOptions &PPO,
32 const TargetOptions &TO, const LangOptions &LO,
33 const std::string &MainFileName,
34 const std::string &OutputFileName,
35 llvm::raw_pwrite_stream *OS,
36 std::shared_ptr<PCHBuffer> Buffer)
Adrian Prantlbb165fb2015-06-20 18:53:08 +000037 : Buffer(Buffer), OS(OS) {}
38
Adrian Prantlfb2398d2015-07-17 01:19:54 +000039 virtual ~RawPCHContainerGenerator() {}
Adrian Prantlbb165fb2015-06-20 18:53:08 +000040
41 void HandleTranslationUnit(ASTContext &Ctx) override {
42 if (Buffer->IsComplete) {
43 // Make sure it hits disk now.
44 *OS << Buffer->Data;
45 OS->flush();
46 }
47 // Free the space of the temporary buffer.
48 llvm::SmallVector<char, 0> Empty;
49 Buffer->Data = std::move(Empty);
50 }
51};
52}
53
Adrian Prantlfb2398d2015-07-17 01:19:54 +000054std::unique_ptr<ASTConsumer> RawPCHContainerWriter::CreatePCHContainerGenerator(
Adrian Prantlbb165fb2015-06-20 18:53:08 +000055 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 {
Adrian Prantlfb2398d2015-07-17 01:19:54 +000060 return llvm::make_unique<RawPCHContainerGenerator>(
Adrian Prantlbb165fb2015-06-20 18:53:08 +000061 Diags, HSO, PPO, TO, LO, MainFileName, OutputFileName, OS, Buffer);
62}
63
Adrian Prantlfb2398d2015-07-17 01:19:54 +000064void RawPCHContainerReader::ExtractPCH(
Adrian Prantlbb165fb2015-06-20 18:53:08 +000065 llvm::MemoryBufferRef Buffer, llvm::BitstreamReader &StreamFile) const {
66 StreamFile.init((const unsigned char *)Buffer.getBufferStart(),
67 (const unsigned char *)Buffer.getBufferEnd());
68}
Adrian Prantlfb2398d2015-07-17 01:19:54 +000069
70PCHContainerOperations::PCHContainerOperations() {
71 registerWriter(llvm::make_unique<RawPCHContainerWriter>());
72 registerReader(llvm::make_unique<RawPCHContainerReader>());
73}